117 lines
3.9 KiB
C#
117 lines
3.9 KiB
C#
using Newtonsoft.Json;
|
|
using QuestShare.Services;
|
|
|
|
namespace QuestShare.Common;
|
|
|
|
public class ConfigurationManager
|
|
{
|
|
public class Configuration
|
|
{
|
|
public string Token { get; set; } = string.Empty;
|
|
public bool ConnectOnStartup { get; set; } = false;
|
|
public bool AutoShareMsq { get; set; } = false;
|
|
public bool AutoShareNewQuests { get; set; } = false;
|
|
public bool TrackMSQ { get; set; } = false;
|
|
public bool HideFutureStepsHost { get; set; } = true;
|
|
public bool HideFutureStepsMember { get; set; } = false;
|
|
public bool EnableHosting { get; set; } = false;
|
|
public Objects.OwnedSession? OwnedSession { get; set; }
|
|
public List<Objects.ShareCode> KnownShareCodes { get; set; } = [];
|
|
public int ActiveQuestId { get; set; } = 0;
|
|
public byte ActiveQuestStep { get; set; } = 0;
|
|
public Dictionary<long, string> KnownCharacters { get; set; } = [];
|
|
public ApiConfiguration[] ApiConfigurations { get; set; } = [new ApiConfiguration { DisplayName = "Primary Server - US East", Url = "https://api.nathanc.tech/Hub", Active = false }];
|
|
public string ApiUrl => ApiConfigurations.FirstOrDefault(x => x.Active)?.Url ?? "https://api.nathanc.tech/Hub";
|
|
public string ApiDisplayName => ApiConfigurations.FirstOrDefault(x => x.Active)?.DisplayName ?? "Primary Server - US East";
|
|
}
|
|
|
|
public class ApiConfiguration
|
|
{
|
|
public required string DisplayName;
|
|
public required string Url;
|
|
public bool Active = false;
|
|
}
|
|
|
|
public static Configuration Instance { get; private set; } = new Configuration();
|
|
private static ulong LocalContentId = 0;
|
|
|
|
public ConfigurationManager()
|
|
{
|
|
Log.Debug("ConfigurationManager constructor");
|
|
|
|
ClientState.Login += OnLogin;
|
|
ClientState.Logout += OnLogout;
|
|
if (ClientState.LocalContentId != 0)
|
|
{
|
|
LocalContentId = ClientState.LocalContentId;
|
|
Framework.Run(Load);
|
|
}
|
|
}
|
|
|
|
public void OnLogin()
|
|
{
|
|
LocalContentId = ClientState.LocalContentId;
|
|
Framework.Run(Load);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
Save();
|
|
ClientState.Login -= OnLogin;
|
|
ClientState.Logout -= OnLogout;
|
|
}
|
|
|
|
public void OnLogout(int code, int state)
|
|
{
|
|
Framework.RunOnTick(Save);
|
|
}
|
|
|
|
public void Load()
|
|
{
|
|
if (File.Exists(Path.Join(PluginInterface.ConfigDirectory.FullName, $"{LocalContentId}.json")))
|
|
{
|
|
var config = File.ReadAllText(Path.Join(PluginInterface.ConfigDirectory.FullName, $"{LocalContentId}.json"));
|
|
if (config != null)
|
|
{
|
|
var deserialized = JsonConvert.DeserializeObject<Configuration>(config);
|
|
if (deserialized != null)
|
|
{
|
|
Instance = deserialized;
|
|
#if DEBUG
|
|
if (Instance.ApiConfigurations.All(x => x.DisplayName != "Local"))
|
|
Instance.ApiConfigurations.Append(new ApiConfiguration { DisplayName = "Local", Url = "http://localhost:8080/Hub", Active = true });
|
|
foreach (var item in Instance.ApiConfigurations)
|
|
{
|
|
if (item.DisplayName != "Local") item.Active = false;
|
|
}
|
|
#endif
|
|
}
|
|
else
|
|
{
|
|
Log.Error($"Failed to deserialize configuration for {LocalContentId}");
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Instance = new Configuration();
|
|
Save();
|
|
}
|
|
}
|
|
public static void Save()
|
|
{
|
|
Log.Debug("Saving configuration");
|
|
File.WriteAllText(Path.Join(PluginInterface.ConfigDirectory.FullName, $"{LocalContentId}.json"), JsonConvert.SerializeObject(Instance));
|
|
Log.Debug($"Wrote config: {JsonConvert.SerializeObject(Instance)}");
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public enum ShareMode
|
|
{
|
|
None,
|
|
Host,
|
|
Member
|
|
}
|