namespace Aeshnidae.Junk;
///
/// Per-mod configuration, read from Settings.json in the deployed mod folder
/// (next to the dll, not in source). Written with defaults on first run so it is
/// easy to find, and openable in-game with "/mod settings Aeshnidae.Junk".
/// Edit and /junk-reload.
///
public class Settings
{
public const string FileName = "Settings.json";
///
/// The shortest text /junk accepts. "/junk a" would match most of a pack;
/// three characters is enough for "gem" or "bag" and stops a slip from
/// emptying it.
///
public int MinimumTextLength { get; set; } = 3;
private static readonly JsonSerializerOptions JsonOptions = new()
{
WriteIndented = true,
ReadCommentHandling = JsonCommentHandling.Skip,
AllowTrailingCommas = true,
};
public static Settings Load(string modPath)
{
var path = Path.Combine(modPath, FileName);
try
{
if (File.Exists(path))
return JsonSerializer.Deserialize(File.ReadAllText(path), JsonOptions) ?? new Settings();
var defaults = new Settings();
defaults.Save(modPath);
ModManager.Log($"[{Mod.Name}] wrote default settings to {path}");
return defaults;
}
catch (Exception ex)
{
ModManager.Log($"[{Mod.Name}] could not read {path}, using defaults: {ex.Message}", ModManager.LogLevel.Warn);
return new Settings();
}
}
public void Save(string modPath)
{
try
{
File.WriteAllText(Path.Combine(modPath, FileName), JsonSerializer.Serialize(this, JsonOptions));
}
catch (Exception ex)
{
ModManager.Log($"[{Mod.Name}] could not save settings: {ex.Message}", ModManager.LogLevel.Error);
}
}
}