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; RemoteCommandsEnabled = PluginSettings.Instance.RemoteCommandsEnabled;
HttpServerEnabled = PluginSettings.Instance.HttpServerEnabled; HttpServerEnabled = PluginSettings.Instance.HttpServerEnabled;
TelemetryEnabled = PluginSettings.Instance.TelemetryEnabled; TelemetryEnabled = PluginSettings.Instance.TelemetryEnabled;
CharTag = PluginSettings.Instance.CharTag;
MainView.SetRareMetaToggleState(RareMetaEnabled); MainView.SetRareMetaToggleState(RareMetaEnabled);
if (TelemetryEnabled) if (TelemetryEnabled)
Telemetry.Start(); Telemetry.Start();
WriteToChat("Settings loaded.");
} }
private void OnChatText(object sender, ChatTextInterceptEventArgs e) private void OnChatText(object sender, ChatTextInterceptEventArgs e)

View file

@ -26,8 +26,7 @@ namespace MosswartMassacre
{ {
// determine settings file path // determine settings file path
string characterName = CoreManager.Current.CharacterFilter.Name; string characterName = CoreManager.Current.CharacterFilter.Name;
string pluginFolder = Path.GetDirectoryName( string pluginFolder = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
typeof(PluginSettings).Assembly.Location);
_filePath = Path.Combine(pluginFolder, $"{characterName}.yaml"); _filePath = Path.Combine(pluginFolder, $"{characterName}.yaml");
// build serializer/deserializer once // build serializer/deserializer once
@ -60,14 +59,8 @@ namespace MosswartMassacre
else else
{ {
_instance = loaded; _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() public static void Save()
@ -76,20 +69,32 @@ namespace MosswartMassacre
{ {
try try
{ {
// serialize to YAML
var serializer = new SerializerBuilder() var serializer = new SerializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance) .WithNamingConvention(UnderscoredNamingConvention.Instance)
.Build(); .Build();
var yaml = serializer.Serialize(_instance); var yaml = serializer.Serialize(_instance);
// atomic write: write to .tmp then replace // write temp file
var temp = _filePath + ".tmp"; var temp = _filePath + ".tmp";
File.WriteAllText(temp, yaml); File.WriteAllText(temp, yaml);
File.Replace(temp, _filePath, null);
// 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) catch (Exception ex)
{ {
PluginCore.DispatchChatToBoxWithPluginIntercept( // log to chat so you can see what went wrong
$"[MosswartMassacre] Error saving settings: {ex.Message}"); CoreManager.Current.Actions.AddChatText(
$"[MosswartMassacre] Error saving settings: {ex.Message}", 5);
} }
} }
} }