MosswartMassacre/MosswartMassacre/PluginSettings.cs
2025-04-29 23:49:28 +02:00

133 lines
4.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.IO;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
using Decal.Adapter;
namespace MosswartMassacre
{
public class PluginSettings
{
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 bool _telemetryEnabled = false;
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);
_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))
{
try
{
string yaml = File.ReadAllText(_filePath);
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 = loaded;
Save();
}
}
public static void Save()
{
lock (_sync)
{
try
{
// serialize to YAML
var serializer = new SerializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.Build();
var yaml = serializer.Serialize(_instance);
// 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)
{
// log to chat so you can see what went wrong
CoreManager.Current.Actions.AddChatText(
$"[MosswartMassacre] Error saving settings: {ex.Message}", 5);
}
}
}
// public properties
public bool RemoteCommandsEnabled
{
get => _remoteCommandsEnabled;
set { _remoteCommandsEnabled = value; Save(); }
}
public bool RareMetaEnabled
{
get => _rareMetaEnabled;
set { _rareMetaEnabled = value; Save(); }
}
public bool HttpServerEnabled
{
get => _httpServerEnabled;
set { _httpServerEnabled = value; Save(); }
}
public bool TelemetryEnabled
{
get => _telemetryEnabled;
set { _telemetryEnabled = value; Save(); }
}
public string CharTag
{
get => _charTag;
set { _charTag = value; Save(); }
}
}
}