using System.Text.Json; using System.Text.Json.Serialization; namespace RioJoy.Core.Overlay; /// /// Loads and saves an as a regions.json — the /// cockpit cell geometry extracted from the source artwork /// (docs/reference/customBackground/riojoy.xcf). Same JSON conventions as /// (indented, enums as strings, null-skipping). /// public static class OverlayTemplateStore { private static readonly JsonSerializerOptions Options = new() { WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, Converters = { new JsonStringEnumConverter() }, }; public static string Serialize(OverlayTemplate template) { if (template is null) throw new ArgumentNullException(nameof(template)); return JsonSerializer.Serialize(template, Options); } public static OverlayTemplate Deserialize(string json) { if (string.IsNullOrWhiteSpace(json)) throw new ArgumentException("Value cannot be null or whitespace.", nameof(json)); return JsonSerializer.Deserialize(json, Options) ?? throw new JsonException("Overlay template JSON deserialized to null."); } public static void Save(OverlayTemplate template, string path) { if (string.IsNullOrWhiteSpace(path)) throw new ArgumentException("Value cannot be null or whitespace.", nameof(path)); string? dir = Path.GetDirectoryName(Path.GetFullPath(path)); if (!string.IsNullOrEmpty(dir)) Directory.CreateDirectory(dir); File.WriteAllText(path, Serialize(template)); } public static OverlayTemplate Load(string path) { if (string.IsNullOrWhiteSpace(path)) throw new ArgumentException("Value cannot be null or whitespace.", nameof(path)); return Deserialize(File.ReadAllText(path)); } }