Fixed saving of settings file

This commit is contained in:
erikn 2025-04-29 23:49:28 +02:00
parent 755890d88b
commit 2ea4945aba
2 changed files with 21 additions and 14 deletions

View file

@ -105,11 +105,13 @@ namespace MosswartMassacre
RemoteCommandsEnabled = PluginSettings.Instance.RemoteCommandsEnabled;
HttpServerEnabled = PluginSettings.Instance.HttpServerEnabled;
TelemetryEnabled = PluginSettings.Instance.TelemetryEnabled;
CharTag = PluginSettings.Instance.CharTag;
MainView.SetRareMetaToggleState(RareMetaEnabled);
if (TelemetryEnabled)
Telemetry.Start();
WriteToChat("Settings loaded.");
}
private void OnChatText(object sender, ChatTextInterceptEventArgs e)

View file

@ -26,8 +26,7 @@ namespace MosswartMassacre
{
// determine settings file path
string characterName = CoreManager.Current.CharacterFilter.Name;
string pluginFolder = Path.GetDirectoryName(
typeof(PluginSettings).Assembly.Location);
string pluginFolder = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
_filePath = Path.Combine(pluginFolder, $"{characterName}.yaml");
// build serializer/deserializer once
@ -60,14 +59,8 @@ namespace MosswartMassacre
else
{
_instance = loaded;
Save();
}
// apply into runtime
PluginCore.RareMetaEnabled = _instance.RareMetaEnabled;
PluginCore.RemoteCommandsEnabled = _instance.RemoteCommandsEnabled;
PluginCore.HttpServerEnabled = _instance.HttpServerEnabled;
PluginCore.TelemetryEnabled = _instance.TelemetryEnabled;
PluginCore.CharTag = _instance.CharTag;
}
public static void Save()
@ -76,20 +69,32 @@ namespace MosswartMassacre
{
try
{
// serialize to YAML
var serializer = new SerializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.Build();
var yaml = serializer.Serialize(_instance);
// atomic write: write to .tmp then replace
// write temp file
var temp = _filePath + ".tmp";
File.WriteAllText(temp, yaml);
// if the settings file already exists, do an atomic Replace
if (File.Exists(_filePath))
{
File.Replace(temp, _filePath, null);
}
else
{
// firsttime save: just move the temp into place
File.Move(temp, _filePath);
}
}
catch (Exception ex)
{
PluginCore.DispatchChatToBoxWithPluginIntercept(
$"[MosswartMassacre] Error saving settings: {ex.Message}");
// log to chat so you can see what went wrong
CoreManager.Current.Actions.AddChatText(
$"[MosswartMassacre] Error saving settings: {ex.Message}", 5);
}
}
}