// ============================================================================ // RynthCore.Engine - UI/PanelLayout.cs // Panel layout per character (PanelLayoutPolicy.cs has the rules and names): // which layout's files this client uses, the switch between them, and the // engine's own saving of imgui.ini. // // Switching. Tick (EndScene, AC's render thread, every frame) works out the // layout the client should use: the PC-wide default until a character is in // the world, then that character's (SessionStateRegistry's server and name, // known straight after LoginComplete, which a hot reload marks at engine init; // a client launched with no character picked has no name there, so the // player's own name from the object snapshot, 2026-11-05). // A switch then runs in three steps: // 1. render thread: snapshot the ImGui windows of the layout being left; // 2. UI writer (UiBackgroundWriter, in order with every other UI save): write // out the layout being left when it is a character's, seed a new // character's files from the default, PanelStateStore.SwitchTo the new // panel_state.txt, read the new imgui.ini; // 3. render thread, before any panel draws: LoadIniSettingsFromMemory (Dear // ImGui 1.91.6 applies loaded settings to windows that already exist, // WindowSettingsHandler_ApplyAll), and the bar and the open panels take // their places from the new store (ImGuiBar / ImGuiPanelHost.OnLayoutSwitched). // After a login, panels draw only once step 3 is done for that login // (ReadyToDraw, EngineFrameController.PanelsLive): never first at the default // places and then a jump. // // imgui.ini. The context's IniFilename stays null (EngineFrameController.Init): // the engine loads the file of the layout in use (step 3) and saves it when // Dear ImGui asks (io.WantSaveIniSettings), at a switch and at shutdown, off // the render thread and atomically, always to the file of the layout the // context holds. // // The default pair is written by clients that use it with the setting off (as // before) and by /rc layout default. With the setting on nothing else writes // it (2026-11-05; PanelLayoutPolicy.MaySaveLayoutInUse, PanelStateStore // .ReadOnlyPath): not a switch, not character select, not a client in the // world that doesn't know its character yet, not Save Layout Now. Before, a // client with no character name saved its places there all session, every // such client overwrote the others', and a reload read back whoever won. // ============================================================================ using System; using System.IO; using System.Threading; using RynthCore.Engine.Compatibility; using RynthCore.Engine.Plugins; namespace RynthCore.Engine.UI; internal static class PanelLayout { private static readonly PanelLayoutSession Session = new(); private static readonly LatestTextWrites IniWrites = new(PanelLayoutFiles.WriteText); private static volatile bool _initialized; /// A finished step 2, waiting for the render thread. private sealed record Loaded(LayoutSwitch Switch, LayoutFiles Files, string? IniText, string? Error, Action? After); private static Loaded? _loaded; // written by the UI writer, read by the render thread // Render thread: the layout applied (step 3; one reference, so other threads read it whole) // and the ImGui windows' text for it. private static Loaded? _applied; private static string? _activeIniText; private static LayoutOwner? _placedFor; // the layout the open panels were last placed from private static bool _placedForSet; /// The character whose layout is applied; null = the default (or none yet). private static LayoutOwner? Active => Volatile.Read(ref _applied)?.Switch.To; // The identity Tick last read (SessionStateRegistry's strings and the player's own name), so it // isn't rebuilt every frame. private static string? _idServer, _idCharacter, _idLive; private static LayoutOwner? _idOwner; // The player's own name for the login it was read in (render thread): the launch context has // none when the client was launched to character select (2026-11-05). private static string _liveName = string.Empty; private static long _liveNameLogin = -1; // The setting the default's read-only mark (PanelStateStore.ReadOnlyPath) was set for. private static bool? _readOnlyFor; /// An /rc layout default or reset waiting for the render thread (one at a time). private sealed record PendingOp(LayoutCommand Kind, Action Reply); private static PendingOp? _pendingOp; private static Action? _resetReply; // render thread: the reset switch's answer /// True once the engine runs layouts (Initialize). Until then imgui.ini is Dear ImGui's own, as before. public static bool Initialized => _initialized; private static string LocalRoot => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "RynthCore"); /// The PC-wide default imgui.ini (the file every client used before). internal static string DefaultIniPath => Path.Combine(RynthCore.Install.RynthInstallPaths.RynthAiDir, PanelLayoutPolicy.ImGuiIniFileName); private static LayoutFiles FilesFor(LayoutOwner? who) => PanelLayoutPolicy.FilesFor(LocalRoot, DefaultIniPath, who); /// Engine init (InitWorker), before the EndScene hook goes in. From here the engine owns imgui.ini. public static void Initialize() { if (_initialized) return; _initialized = true; MarkDefaultReadOnly(EngineSettings.PanelLayoutPerCharacter); RynthLog.UI($"PanelLayout: per character {(EngineSettings.PanelLayoutPerCharacter ? "on" : "off")}; layouts under {Path.Combine(LocalRoot, PanelLayoutPolicy.LayoutsFolder)}."); } /// With the setting on, no save writes the default's panel_state.txt (PanelLayoutPolicy.MaySaveLayoutInUse). private static void MarkDefaultReadOnly(bool perCharacter) { if (_readOnlyFor == perCharacter) return; _readOnlyFor = perCharacter; PanelStateStore.ReadOnlyPath = perCharacter ? FilesFor(null).PanelState : null; } private static bool InWorld => LoginLifecycleHooks.HasObservedLoginComplete; private static LayoutOwner? Identity() { string server = SessionStateRegistry.LastServerName, character = SessionStateRegistry.LastCharacterName; string live = string.IsNullOrWhiteSpace(character) && InWorld ? LiveName() : string.Empty; if (!ReferenceEquals(server, _idServer) || !ReferenceEquals(character, _idCharacter) || !ReferenceEquals(live, _idLive)) { _idServer = server; _idCharacter = character; _idLive = live; _idOwner = PanelLayoutPolicy.Identity(server, character, live); } return _idOwner; } /// /// The name of the player in the world, read once per login from the object snapshot (no native /// call: the main-thread walk fills it every half second; the live name lookup at LoginComplete /// stays off in SessionStateRegistry). Empty until the snapshot has the player. Render thread. /// private static string LiveName() { long login = LoginLifecycleHooks.LoginCompleteAtMs; if (login != _liveNameLogin) { _liveNameLogin = login; _liveName = string.Empty; } if (_liveName.Length > 0 || login == 0) return _liveName; uint me = ClientHelperHooks.CachedPlayerId; if (me != 0 && ClientObjectHooks.TryGetSnapshotName(me, out string name) && !string.IsNullOrWhiteSpace(name)) { _liveName = name.Trim(); RynthLog.UI($"PanelLayout: the launch context names no character; the player in the world is {_liveName}."); } return _liveName; } /// /// True when panels may draw (EngineFrameController.PanelsLive): after a login, only once that /// login's layout is in place. Always true before Initialize. Render thread. /// public static bool ReadyToDraw => !_initialized || Session.ReadyToDraw(Environment.TickCount64, InWorld, LoginLifecycleHooks.LoginCompleteAtMs); // ── the frame (render thread) ───────────────────────────────────────── /// /// Once a frame, from EndScene (render thread, outside the ImGui frame). : /// the ImGui layer is on and didn't fail, so a layout waits for its context; : /// the context exists. / run on it. /// public static void Tick(bool imguiInUse, bool imguiReady, Func snapshotIni, Action loadIni) { if (!_initialized) return; try { ApplyLoaded(imguiInUse, imguiReady, loadIni); if (!Session.InFlight && Volatile.Read(ref _pendingOp) != null && Interlocked.Exchange(ref _pendingOp, null) is { } op) RunOp(op, imguiReady ? snapshotIni : null); MarkDefaultReadOnly(EngineSettings.PanelLayoutPerCharacter); // the setting can change in Settings > Display LayoutSwitch? sw = Session.Tick(Environment.TickCount64, EngineSettings.PanelLayoutPerCharacter, InWorld, LoginLifecycleHooks.LoginCompleteAtMs, SessionStateRegistry.LoginRecorded, Identity()); if (sw is { } s) StartSwitch(s, s.FlushFrom && imguiReady ? snapshotIni() : null); if (_resetReply is { } unanswered) { // The reset asked for this frame didn't become a switch: the layout in use changed first. _resetReply = null; unanswered(PanelLayoutPolicy.FailedReply("reset the panel layout", "the layout in use changed first; try again"), false); } } catch (Exception ex) { RynthLog.UI($"PanelLayout: tick threw {ex.GetType().Name}: {ex.Message}"); } } /// Step 1: the switch goes to the UI writer, with the windows of the layout being left. private static void StartSwitch(LayoutSwitch sw, string? fromIni) { LayoutFiles from = FilesFor(sw.From), to = FilesFor(sw.To); if (sw.FlushFrom && fromIni != null) IniWrites.Queue(from.ImGuiIni, fromIni); Action? after = null; if (sw.Reseed && _resetReply is { } reply && sw.To is { } who) { _resetReply = null; after = error => reply(error == null ? PanelLayoutPolicy.ResetDoneReply(who) : PanelLayoutPolicy.FailedReply("reset " + PanelLayoutPolicy.Describe(who) + "'s panel layout", error), error == null); } UiBackgroundWriter.Enqueue("panel layout switch", () => LoadOnWriter(sw, from, to, after)); } /// Step 2 (UI writer): write out what is left, seed or reseed, switch the store, read the new imgui.ini. private static void LoadOnWriter(LayoutSwitch sw, LayoutFiles from, LayoutFiles to, Action? after) { string? ini = null, error = null; string seeded = string.Empty; try { if (sw.FlushFrom) IniWrites.Flush(from.ImGuiIni); } catch (Exception ex) { RynthLog.UI($"PanelLayout: writing out {from.ImGuiIni} failed - {ex.Message}"); } try { if (sw.To != null) { LayoutFiles defaults = FilesFor(null); if (sw.Reseed) { PanelLayoutFiles.CopyOver(defaults, to); seeded = " (reset from the default)"; } else { var copied = PanelLayoutFiles.SeedMissing(defaults, to); if (copied.PanelState || copied.Ini) seeded = " (new: " + (copied.PanelState && copied.Ini ? "both files" : copied.PanelState ? "panel_state.txt" : "imgui.ini") + " copied from the default)"; } } } catch (Exception ex) { // The switch still happens: the character's files are made by its first save. error = ex.Message; } // Never throws (it logs a file it can't read and starts empty). PanelStateStore.SwitchTo(to.PanelState, flushCurrent: sw.FlushFrom); try { ini = PanelLayoutFiles.ReadText(to.ImGuiIni); } catch (Exception ex) { error ??= ex.Message; } string who = sw.To is { } owner ? PanelLayoutPolicy.Describe(owner) + "'s" : "the PC-wide default"; if (error == null) RynthLog.UI($"PanelLayout: switched to {who} layout{seeded}: {to.PanelState}, {to.ImGuiIni}."); else RynthLog.Warn($"PanelLayout: switched to {who} layout ({to.PanelState}), but its files had a problem - {error}."); Volatile.Write(ref _loaded, new Loaded(sw, to, ini, error, after)); try { after?.Invoke(error); } catch { } } /// Step 3 (render thread): the loaded layout goes into the ImGui context, the bar and the open panels. private static void ApplyLoaded(bool imguiInUse, bool imguiReady, Action loadIni) { Loaded? l = Volatile.Read(ref _loaded); if (l == null || ReferenceEquals(l, _applied)) return; if (imguiInUse && !imguiReady) return; // the context is made this frame: apply on the next if (imguiReady && !string.IsNullOrEmpty(l.IniText)) { try { loadIni(l.IniText); } catch (Exception ex) { RynthLog.UI($"PanelLayout: loading {l.Files.ImGuiIni} threw {ex.GetType().Name}: {ex.Message}"); } } Volatile.Write(ref _applied, l); _activeIniText = l.IniText; Session.MarkApplied(l.Switch.Gen); // A reset places the panels again even though the layout's owner is the same. if (InWorld && (l.Switch.Reseed || !_placedForSet || !Equals(l.Switch.To, _placedFor))) { // At character select nothing draws: the panels are placed when a character comes in. _placedFor = l.Switch.To; _placedForSet = true; ImGuiBackend.ImGuiBar.OnLayoutSwitched(); ImGuiBackend.ImGuiPanelHost.OnLayoutSwitched(); } } /// /// The ImGui context was just made (EngineFrameController.Init, context current, before its first /// frame): it starts with the windows of the layout in use, if one is applied already. /// public static void OnContextCreated(Action loadIni) { if (!_initialized || _applied == null || string.IsNullOrEmpty(_activeIniText)) return; try { loadIni(_activeIniText); } catch (Exception ex) { RynthLog.UI($"PanelLayout: loading the layout into the new context threw {ex.GetType().Name}: {ex.Message}"); } } // ── saving imgui.ini ────────────────────────────────────────────────── /// /// The ImGui windows' text to save (render thread): Dear ImGui's dirty timer (io.WantSaveIniSettings), /// or for Save Layout Now. Written in the background, atomically, to /// the file of the layout the context holds. /// public static void QueueIniSave(string text, bool explicitSave) { if (!_initialized) { // Dear ImGui owns the file (IniFilename pinned): Save Layout Now writes the default, as before. if (explicitSave) UiBackgroundWriter.Enqueue("imgui.ini", () => PanelLayoutFiles.WriteText(DefaultIniPath, text)); return; } if (!MaySave(explicitSave, out string path)) return; _activeIniText = text; IniWrites.Queue(path, text); UiBackgroundWriter.Enqueue("imgui.ini", () => { try { IniWrites.Flush(path); } catch (Exception ex) { RynthLog.UI($"PanelLayout: saving {path} failed - {ex.Message}"); } }); } /// The ImGui context is about to go (engine shutdown, a hot reload): its windows are written now. public static void SaveIniOnShutdown(string text) { if (!_initialized || !MaySave(explicitSave: false, out string path)) return; try { IniWrites.Queue(path, text); IniWrites.Flush(path); } catch (Exception ex) { RynthLog.UI($"PanelLayout: saving {path} at shutdown failed - {ex.Message}"); } } private static bool MaySave(bool explicitSave, out string path) { path = string.Empty; // Nothing applied yet: the context holds no file's windows, and saving it would empty one. if (Volatile.Read(ref _applied) is not { } applied) return false; // The default with the setting on: only read, even by Save Layout Now (2026-11-05). if (!PanelLayoutPolicy.MaySaveLayoutInUse(applied.Switch.To != null, EngineSettings.PanelLayoutPerCharacter)) { if (explicitSave) RynthLog.UI($"PanelLayout: Save Layout Now didn't write {applied.Files.ImGuiIni}: {SaveBlockedReason()}."); return false; } path = applied.Files.ImGuiIni; return true; } private static string SaveBlockedReason() => PanelLayoutPolicy.DefaultReadOnlyText(InWorld); /// /// Save Layout Now (Settings > Display): null when the window places go to the layout in use, else /// why they don't (the PC-wide default with the setting on is only read). Any thread (racy read). /// public static string? SavePlacesProblem() { if (!_initialized || Volatile.Read(ref _applied) is not { } applied) return null; return PanelLayoutPolicy.MaySaveLayoutInUse(applied.Switch.To != null, EngineSettings.PanelLayoutPerCharacter) ? null : SaveBlockedReason(); } // ── /rc layout and Settings ─────────────────────────────────────────── /// /rc layout: the layout in use, its files, and the default's. Any thread (racy reads). public static string[] DescribeLines() { LayoutFiles defaults = FilesFor(null); if (!_initialized) return PanelLayoutPolicy.StatusLines(null, defaults, DefaultReason.NotRunning, defaults, false); bool perCharacter = EngineSettings.PanelLayoutPerCharacter; Loaded? applied = Volatile.Read(ref _applied); LayoutOwner? active = applied?.Switch.To; LayoutFiles files = applied?.Files ?? defaults; DefaultReason why = DefaultReason.None; if (active == null) { if (!perCharacter) why = DefaultReason.SettingOff; else if (!InWorld) why = DefaultReason.NotInWorld; else if (!Session.Decided) why = DefaultReason.WaitingForName; else if (Session.Wanted == null) why = DefaultReason.NoCharacterName; } return PanelLayoutPolicy.StatusLines(active, files, why, defaults, perCharacter); } // Settings > Display's line, built again only when what it shows changed (it draws every frame). private static Loaded? _lineFor; private static int _lineFlags = -1; private static string _line = string.Empty; /// Settings > Display: whose layout this client uses and where its files are. Render thread. public static string SettingsLine() { bool perCharacter = EngineSettings.PanelLayoutPerCharacter; Loaded? applied = Volatile.Read(ref _applied); int flags = (perCharacter ? 1 : 0) | (InWorld ? 2 : 0) | (Session.Decided ? 4 : 0) | (Session.Wanted != null ? 8 : 0) | (_initialized ? 16 : 0); if (ReferenceEquals(applied, _lineFor) && flags == _lineFlags) return _line; _lineFor = applied; _lineFlags = flags; LayoutFiles defaults = FilesFor(null); LayoutOwner? active = applied?.Switch.To; DefaultReason why = DefaultReason.None; if (!_initialized) why = DefaultReason.NotRunning; else if (active == null) { if (!perCharacter) why = DefaultReason.SettingOff; else if (!InWorld) why = DefaultReason.NotInWorld; else if (!Session.Decided) why = DefaultReason.WaitingForName; else if (Session.Wanted == null) why = DefaultReason.NoCharacterName; } _line = PanelLayoutPolicy.SettingsLine(active, applied?.Files ?? defaults, why); return _line; } /// Settings > Display's reset button: there is a character's own layout to reset. public static bool CanResetFromSettings => _initialized && Active != null; /// Settings > Display's reset confirmation text. public static string ResetConfirmText() => PanelLayoutPolicy.ResetConfirmText(Active); /// Settings > Display "Reset this character's layout" (confirmed): /rc layout reset confirm, answered in chat. public static void ResetFromSettings() { static void Chat(string text, bool ok) => AcMainThreadQueue.EnqueueWriteToChat("[RynthCore] " + text, ok ? 1 : 0x1A); string? problem = Request(LayoutCommand.ResetConfirm, Chat); if (problem != null) Chat(problem, false); } /// /// /rc layout default or reset: null when it can run, else why not (with the usage). Reset without /// confirm only answers what it would do (). Any thread. /// public static string? Check(LayoutCommand kind, out string ask) { ask = string.Empty; string verb = kind == LayoutCommand.MakeDefault ? "make a default from this client" : "reset the panel layout"; LayoutOwner? active = Active; string? problem = PanelLayoutPolicy.CharacterLayoutProblem(verb, _initialized, EngineSettings.PanelLayoutPerCharacter, InWorld, active, Session.InFlight); if (problem == null && kind == LayoutCommand.ResetAsk && active != null) ask = PanelLayoutPolicy.ResetAskReply(active, FilesFor(null)); return problem; } /// /// /rc layout default or reset confirm: queued for the render thread; gets the /// result (text, ok) from the UI writer when it is done. Null when queued, else why not. Any thread. /// public static string? Request(LayoutCommand kind, Action reply) { string? problem = Check(kind, out _); if (problem != null) return problem; if (Interlocked.CompareExchange(ref _pendingOp, new PendingOp(kind, reply), null) != null) return PanelLayoutPolicy.BusyReply; return null; } /// Settings > Display "Layout for New Characters": /rc layout default, answered in chat. public static void MakeDefaultFromSettings() { static void Chat(string text, bool ok) => AcMainThreadQueue.EnqueueWriteToChat("[RynthCore] " + text, ok ? 1 : 0x1A); string? problem = Request(LayoutCommand.MakeDefault, Chat); if (problem != null) Chat(problem, false); } /// Runs a queued /rc layout op (render thread, no switch in flight). private static void RunOp(PendingOp op, Func? snapshotIni) { Loaded? applied = Volatile.Read(ref _applied); if (applied?.Switch.To is not { } who) { // The layout changed between the command and this frame (a logout, the setting). op.Reply(PanelLayoutPolicy.CharacterLayoutProblem("change the panel layout", true, EngineSettings.PanelLayoutPerCharacter, InWorld, null, false) ?? PanelLayoutPolicy.FailedReply("change the panel layout", "no character layout is in use"), false); return; } LayoutFiles files = applied.Files; if (op.Kind == LayoutCommand.ResetConfirm) { // The next switch (this frame) copies the default over this character's files and // places the panels from it; it answers when its files are done. _resetReply = op.Reply; Session.WantReseed(); return; } // Make default: this character's windows as they are now (saved to its own file too), then // the panel store's text, both written as the default, in order after every save queued so far. string? ini = snapshotIni?.Invoke(); if (ini != null) { _activeIniText = ini; IniWrites.Queue(files.ImGuiIni, ini); } LayoutFiles defaults = FilesFor(null); UiBackgroundWriter.Enqueue("panel layout default", () => { try { IniWrites.Flush(files.ImGuiIni); PanelLayoutFiles.MakeDefault(files, defaults, PanelStateStore.Snapshot(), ini); RynthLog.UI($"PanelLayout: {PanelLayoutPolicy.Describe(who)}'s layout is now the default ({defaults.PanelState}, {defaults.ImGuiIni})."); op.Reply(PanelLayoutPolicy.MakeDefaultDoneReply(who, defaults), true); } catch (Exception ex) { op.Reply(PanelLayoutPolicy.FailedReply("make the default", ex.Message), false); } }); } }