Bugfix in PluginSettings.
This commit is contained in:
parent
347cfe6423
commit
fc2575833b
1 changed files with 62 additions and 21 deletions
|
|
@ -10,35 +10,59 @@ namespace MosswartMassacre
|
||||||
{
|
{
|
||||||
private static PluginSettings _instance;
|
private static PluginSettings _instance;
|
||||||
private static string _filePath;
|
private static string _filePath;
|
||||||
|
private static readonly object _sync = new object();
|
||||||
|
|
||||||
|
// backing fields
|
||||||
private bool _remoteCommandsEnabled = false;
|
private bool _remoteCommandsEnabled = false;
|
||||||
private bool _rareMetaEnabled = true;
|
private bool _rareMetaEnabled = true;
|
||||||
private bool _httpServerEnabled = false;
|
private bool _httpServerEnabled = false;
|
||||||
private string _charTag = "default";
|
|
||||||
private bool _telemetryEnabled = false;
|
private bool _telemetryEnabled = false;
|
||||||
public static PluginSettings Instance => _instance;
|
private string _charTag = "default";
|
||||||
|
|
||||||
|
public static PluginSettings Instance => _instance
|
||||||
|
?? throw new InvalidOperationException("PluginSettings not initialized");
|
||||||
|
|
||||||
public static void Initialize()
|
public static void Initialize()
|
||||||
{
|
{
|
||||||
|
// determine settings file path
|
||||||
string characterName = CoreManager.Current.CharacterFilter.Name;
|
string characterName = CoreManager.Current.CharacterFilter.Name;
|
||||||
string pluginFolder = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
|
string pluginFolder = Path.GetDirectoryName(
|
||||||
|
typeof(PluginSettings).Assembly.Location);
|
||||||
_filePath = Path.Combine(pluginFolder, $"{characterName}.yaml");
|
_filePath = Path.Combine(pluginFolder, $"{characterName}.yaml");
|
||||||
|
|
||||||
|
// build serializer/deserializer once
|
||||||
|
var builder = new DeserializerBuilder()
|
||||||
|
.WithNamingConvention(UnderscoredNamingConvention.Instance);
|
||||||
|
var deserializer = builder.Build();
|
||||||
|
|
||||||
|
PluginSettings loaded = null;
|
||||||
|
|
||||||
if (File.Exists(_filePath))
|
if (File.Exists(_filePath))
|
||||||
{
|
{
|
||||||
var deserializer = new DeserializerBuilder()
|
try
|
||||||
.WithNamingConvention(UnderscoredNamingConvention.Instance)
|
{
|
||||||
.Build();
|
|
||||||
|
|
||||||
string yaml = File.ReadAllText(_filePath);
|
string yaml = File.ReadAllText(_filePath);
|
||||||
_instance = deserializer.Deserialize<PluginSettings>(yaml);
|
loaded = deserializer.Deserialize<PluginSettings>(yaml);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
PluginCore.DispatchChatToBoxWithPluginIntercept(
|
||||||
|
$"[MosswartMassacre] Error reading settings, using defaults: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (loaded == null)
|
||||||
|
{
|
||||||
|
// either file didn't exist, was empty, or deserialized as null
|
||||||
|
_instance = new PluginSettings();
|
||||||
|
Save(); // write out default skeleton
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_instance = new PluginSettings();
|
_instance = loaded;
|
||||||
Save();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply settings to runtime state
|
// apply into runtime
|
||||||
PluginCore.RareMetaEnabled = _instance.RareMetaEnabled;
|
PluginCore.RareMetaEnabled = _instance.RareMetaEnabled;
|
||||||
PluginCore.RemoteCommandsEnabled = _instance.RemoteCommandsEnabled;
|
PluginCore.RemoteCommandsEnabled = _instance.RemoteCommandsEnabled;
|
||||||
PluginCore.HttpServerEnabled = _instance.HttpServerEnabled;
|
PluginCore.HttpServerEnabled = _instance.HttpServerEnabled;
|
||||||
|
|
@ -47,15 +71,30 @@ namespace MosswartMassacre
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Save()
|
public static void Save()
|
||||||
|
{
|
||||||
|
lock (_sync)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
var serializer = new SerializerBuilder()
|
var serializer = new SerializerBuilder()
|
||||||
.WithNamingConvention(UnderscoredNamingConvention.Instance)
|
.WithNamingConvention(UnderscoredNamingConvention.Instance)
|
||||||
.Build();
|
.Build();
|
||||||
|
var yaml = serializer.Serialize(_instance);
|
||||||
|
|
||||||
string yaml = serializer.Serialize(_instance);
|
// atomic write: write to .tmp then replace
|
||||||
File.WriteAllText(_filePath, yaml);
|
var temp = _filePath + ".tmp";
|
||||||
|
File.WriteAllText(temp, yaml);
|
||||||
|
File.Replace(temp, _filePath, null);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
PluginCore.DispatchChatToBoxWithPluginIntercept(
|
||||||
|
$"[MosswartMassacre] Error saving settings: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// public properties
|
||||||
public bool RemoteCommandsEnabled
|
public bool RemoteCommandsEnabled
|
||||||
{
|
{
|
||||||
get => _remoteCommandsEnabled;
|
get => _remoteCommandsEnabled;
|
||||||
|
|
@ -73,15 +112,17 @@ namespace MosswartMassacre
|
||||||
get => _httpServerEnabled;
|
get => _httpServerEnabled;
|
||||||
set { _httpServerEnabled = value; Save(); }
|
set { _httpServerEnabled = value; Save(); }
|
||||||
}
|
}
|
||||||
public string CharTag
|
|
||||||
{
|
|
||||||
get => _charTag;
|
|
||||||
set { _charTag = value; Save(); }
|
|
||||||
}
|
|
||||||
public bool TelemetryEnabled
|
public bool TelemetryEnabled
|
||||||
{
|
{
|
||||||
get => _telemetryEnabled;
|
get => _telemetryEnabled;
|
||||||
set { _telemetryEnabled = value; Save(); }
|
set { _telemetryEnabled = value; Save(); }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public string CharTag
|
||||||
|
{
|
||||||
|
get => _charTag;
|
||||||
|
set { _charTag = value; Save(); }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue