// HudState.cs — Persisted per-character settings for the floating HUD windows // (\huds.json). Fields, not properties, so the source-generated // RynthAiJsonContext (IncludeFields) round-trips them under NativeAOT. using System; using System.Collections.Generic; using System.IO; using System.Text.Json; using System.Text.Json.Serialization; namespace RynthCore.Plugin.RynthAi.Huds; public sealed class HudState { /// Mini Remote gem grid size (UB layout: 5 columns × 6 rows). public const int MiniRemoteColumns = 5; public const int MiniRemoteSlotCount = 30; /// Mini Remote slot icon edge in pixels: the grow / shrink range and the default. public const float MinMiniRemoteSlotSize = 18f, MaxMiniRemoteSlotSize = 64f, DefaultMiniRemoteSlotSize = 28f; /// The "Inventory HUDs" setup window is open. public bool ShowSetup; // ── Pack item count HUD ── public bool ShowItemHud; public bool ItemHudShowNames; public bool ItemHudLocked; public float ItemHudIconSize = 20f; /// Items shown on the HUD, in display order (matched by name). public List ItemHudItems = new(); // ── Mini Remote ── public bool ShowMiniRemote; public bool MiniRemoteLocked; /// Sections laid out side by side in columns (the V/H button) instead of stacked. public bool MiniRemoteHorizontal; public bool MiniShowStats = true; /// Attack target row: name, health bar, distance. public bool MiniShowTarget = true; /// Pet row: the summon that is out (health, time left), else the next combat essence. public bool MiniShowPet = true; public bool MiniShowGems = true; public bool MiniShowToggles = true; /// Obsolete since 2026-10-20 (kept so huds.json loads): the Bank section's switch is RynthILT's (copied once). public bool MiniShowBank = true; public bool MiniShowRebuff = true; /// Chat translator row: on/off, receive / send language swap. public bool MiniShowTranslate = true; /// Obsolete since 2026-10-20 (kept so huds.json loads): the Guardian line's switch is RynthILT's (copied once). public bool MiniShowGuardian = true; /// Gem grid slots, row-major from the top-left (empty Name = unassigned). public List MiniRemoteSlots = new(); /// /// Slot icon edge in pixels (Ctrl+wheel over the slots, or Options > Slot size). /// Clamped to ... /// public float MiniRemoteSlotSize = DefaultMiniRemoteSlotSize; /// /// The Mini Remote's selected tab: "" for the remote itself, else a tab id: its own /// (quests, pets) or RynthILT's (character, banking, gear, charms, games). /// public string MiniRemoteTab = string.Empty; } /// An item pinned to a HUD. Matched by name; Wcid / IconDid are remembered so the /// entry still shows its icon while none are carried. public sealed class HudItemEntry { public string Name = string.Empty; public uint Wcid; public uint IconDid; [JsonIgnore] public bool IsEmpty => string.IsNullOrWhiteSpace(Name); } /// Loads / dirty-checked saves of . internal sealed class HudStore { private readonly string _charFolder; private string _lastJson = string.Empty; public HudStore(string charFolder) => _charFolder = charFolder ?? string.Empty; private string StatePath => Path.Combine(_charFolder, "huds.json"); public HudState Load() { HudState? s = null; try { if (_charFolder.Length > 0 && File.Exists(StatePath)) { string json = File.ReadAllText(StatePath); s = JsonSerializer.Deserialize(json, RynthAiJsonContext.Default.HudState); if (s != null) _lastJson = json; } } catch (Exception ex) { RynthLog.Exception(LogCat.Huds, ex, $"load {StatePath}"); } s ??= new HudState(); Normalize(s); return s; } /// Writes the state only when its JSON differs from the last write. public void SaveIfDirty(HudState state) { if (_charFolder.Length == 0) return; try { string json = JsonSerializer.Serialize(state, RynthAiJsonContext.Default.HudState); if (json == _lastJson) return; Directory.CreateDirectory(_charFolder); string tmp = StatePath + ".tmp"; File.WriteAllText(tmp, json); File.Move(tmp, StatePath, overwrite: true); _lastJson = json; } catch (Exception ex) { RynthLog.Exception(LogCat.Huds, ex, $"save {StatePath}"); } } /// Repairs null lists / out-of-range values after deserialization. private static void Normalize(HudState s) { s.ItemHudItems ??= new(); s.ItemHudItems.RemoveAll(e => e == null || e.IsEmpty); s.ItemHudIconSize = Math.Clamp(s.ItemHudIconSize, 12f, 48f); s.MiniRemoteSlots ??= new(); for (int i = 0; i < s.MiniRemoteSlots.Count; i++) s.MiniRemoteSlots[i] ??= new HudItemEntry(); while (s.MiniRemoteSlots.Count < HudState.MiniRemoteSlotCount) s.MiniRemoteSlots.Add(new HudItemEntry()); if (s.MiniRemoteSlots.Count > HudState.MiniRemoteSlotCount) s.MiniRemoteSlots.RemoveRange(HudState.MiniRemoteSlotCount, s.MiniRemoteSlots.Count - HudState.MiniRemoteSlotCount); s.MiniRemoteTab ??= string.Empty; // A huds.json from before the setting has no value (0): use the default. s.MiniRemoteSlotSize = s.MiniRemoteSlotSize <= 0f || float.IsNaN(s.MiniRemoteSlotSize) ? HudState.DefaultMiniRemoteSlotSize : Math.Clamp(s.MiniRemoteSlotSize, HudState.MinMiniRemoteSlotSize, HudState.MaxMiniRemoteSlotSize); } }