102 lines
3.0 KiB
C#
102 lines
3.0 KiB
C#
using FFXIVClientStructs.FFXIV.Client.Game.Character;
|
|
using Newtonsoft.Json;
|
|
using QuestShare.Services;
|
|
|
|
namespace QuestShare.Common;
|
|
|
|
public class ConfigurationManager
|
|
{
|
|
public class Configuration
|
|
{
|
|
public string Token { get; set; } = string.Empty;
|
|
public string ShareCode { get; set; } = string.Empty;
|
|
public string LastShareCode { get; set; } = string.Empty;
|
|
public ShareMode LastShareMode { get; set; } = ShareMode.None;
|
|
public bool ConnectOnStartup { get; set; } = false;
|
|
public bool ResumeOnStartup { get; set; } = false;
|
|
public bool LastConnectionState { get; set; } = false;
|
|
public bool AutoShareMsq { get; set; } = false;
|
|
public bool AutoShareNewQuests { get; set; } = false;
|
|
public bool BroadcastToParty { get; set; } = false;
|
|
}
|
|
|
|
public Configuration Instance { get; private set; } = new Configuration();
|
|
private 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()
|
|
{
|
|
Framework.Run(Save);
|
|
ClientState.Login -= OnLogin;
|
|
ClientState.Logout -= OnLogout;
|
|
}
|
|
|
|
public void OnLogout(int code, int state)
|
|
{
|
|
Framework.RunOnTick(Save);
|
|
ShareService.SetShareCode(string.Empty);
|
|
ShareService.SetHostedShareCode(string.Empty);
|
|
ShareService.Token = string.Empty;
|
|
}
|
|
|
|
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;
|
|
|
|
ShareService.SetShareCode(Instance.LastShareCode);
|
|
ShareService.Token = Instance.Token;
|
|
Log.Warning($"Writing token {Instance.Token} to ShareService");
|
|
}
|
|
else
|
|
{
|
|
Log.Error($"Failed to deserialize configuration for {localContentId}");
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Instance = new Configuration();
|
|
Save();
|
|
}
|
|
}
|
|
public void Save()
|
|
{
|
|
Log.Debug("Saving configuration");
|
|
File.WriteAllText(Path.Join(PluginInterface.ConfigDirectory.FullName, $"{localContentId}.json"), JsonConvert.SerializeObject(Instance));
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public enum ShareMode
|
|
{
|
|
None,
|
|
Host,
|
|
Member
|
|
}
|