namespace Aeshnidae.LootTables;
/// Settings.json beside the dll. The overrides themselves are their own file.
public class Settings
{
public const string FileName = "Settings.json";
///
/// The overrides: { "Tables": { "WorkmanshipChance.T3_Chances": [ {"Value":"4","Chance":0.2}, ... ] } }.
/// Written by the staff site; a table not in it is retail.
///
public string OverridesFile { get; set; } = "overrides.json";
/// Every table, retail and current, written after each apply for the staff site to read.
public string CatalogFile { get; set; } = "catalog.json";
private static readonly JsonSerializerOptions JsonOptions = new()
{
WriteIndented = true,
ReadCommentHandling = JsonCommentHandling.Skip,
AllowTrailingCommas = true,
PropertyNameCaseInsensitive = 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();
File.WriteAllText(path, JsonSerializer.Serialize(defaults, JsonOptions));
return defaults;
}
catch (Exception ex)
{
ModManager.Log($"[{Mod.Name}] could not read {path}, using defaults: {ex.Message}", ModManager.LogLevel.Warn);
return new Settings();
}
}
/// The overrides as they stand; none (retail everywhere) when the file is missing or broken.
public Tables.OverrideFile ReadOverrides(string modPath)
{
var path = Path.Combine(modPath, OverridesFile);
if (!File.Exists(path))
return new Tables.OverrideFile();
try
{
return JsonSerializer.Deserialize(File.ReadAllText(path), JsonOptions) ?? new Tables.OverrideFile();
}
catch (Exception ex)
{
ModManager.Log($"[{Mod.Name}] {path} is not valid - no overrides applied: {ex.Message}", ModManager.LogLevel.Error);
return new Tables.OverrideFile();
}
}
}