Bugfix in PluginSettings.

This commit is contained in:
erik 2025-04-29 20:22:19 +02:00
parent 347cfe6423
commit fc2575833b

View file

@ -10,35 +10,59 @@ namespace MosswartMassacre
{
private static PluginSettings _instance;
private static string _filePath;
private static readonly object _sync = new object();
// backing fields
private bool _remoteCommandsEnabled = false;
private bool _rareMetaEnabled = true;
private bool _httpServerEnabled = false;
private string _charTag = "default";
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()
{
// determine settings file path
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");
// build serializer/deserializer once
var builder = new DeserializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance);
var deserializer = builder.Build();
PluginSettings loaded = null;
if (File.Exists(_filePath))
{
var deserializer = new DeserializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.Build();
try
{
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
{
_instance = new PluginSettings();
Save();
_instance = loaded;
}
// Apply settings to runtime state
// apply into runtime
PluginCore.RareMetaEnabled = _instance.RareMetaEnabled;
PluginCore.RemoteCommandsEnabled = _instance.RemoteCommandsEnabled;
PluginCore.HttpServerEnabled = _instance.HttpServerEnabled;
@ -47,15 +71,30 @@ namespace MosswartMassacre
}
public static void Save()
{
lock (_sync)
{
try
{
var serializer = new SerializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.Build();
var yaml = serializer.Serialize(_instance);
string yaml = serializer.Serialize(_instance);
File.WriteAllText(_filePath, yaml);
// atomic write: write to .tmp then replace
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
{
get => _remoteCommandsEnabled;
@ -73,15 +112,17 @@ namespace MosswartMassacre
get => _httpServerEnabled;
set { _httpServerEnabled = value; Save(); }
}
public string CharTag
{
get => _charTag;
set { _charTag = value; Save(); }
}
public bool TelemetryEnabled
{
get => _telemetryEnabled;
set { _telemetryEnabled = value; Save(); }
}
public string CharTag
{
get => _charTag;
set { _charTag = value; Save(); }
}
}
}