// ============================================================================ // RynthCore.Engine - UI/ServerMenus.cs // The servers and sections, as menus: the bottom half of the RynthAi // dashboard's Char menu and of the Mini Remote's right-click menu (the top // half, "Character", is the windows every server has), and the "servermenu" // push that gives RynthAi the same list for its own menus. // // Everything comes from UI/Sections/SectionRegistry: the servers (built in or // declared by a plugin), whether each is active (detected or forced for this // character), and what the engine and the installed plugins add for each. A // server's group shows only while it is active and has items. // // Items run "/rc" commands through the engine's dispatcher (any other command // goes through the chat dispatcher, as if typed); a plugin surface opens its // section in the Mini Remote ("/rc miniremote section "). // // Push (Tick: when it changes, and every ResendMs in case RynthAi missed one), // tab-separated lines: // V id name active 0/1 force auto|on|off detected 0/1 reason every server // X id label every section, rail order // S id name active 0/1 force command a server with items // I label command tooltip feature its items // N id pattern default|engine.json a server's world names // RynthAi 0.5.54 and older read only S and I (unknown kinds and extra fields are // skipped); a "section:" feature is a surface it may show in its own Mini Remote. // RynthILT (0.1.1+) gets the same push for its copy of the ILT settings (V and N), // and so does every server plugin whose sections manifest declares a server // (RynthColdeve, RynthAelrynth, ...: their server card's verdict and override). // ============================================================================ using System; using System.Collections.Generic; using System.Text; using RynthCore.Engine.UI.Data; using RynthCore.Engine.UI.Sections; namespace RynthCore.Engine.UI; internal static class ServerMenus { /// How often RynthAi gets the list again even when nothing changed. private const long ResendMs = 30_000; /// How often the payload is rebuilt to look for a change. private const long CheckMs = 1_000; /// Feature prefix of an item that is a plugin surface (its section in the Mini Remote). public const string SectionFeaturePrefix = "section:"; /// How often RynthILT gets the list again: it may load (or hot-reload) after the last change. private const long IltResendMs = 10_000; /// The plugin that keeps a copy of the ILT settings. private const string RynthIltPlugin = "RynthILT"; private static long _nextCheckMs; private static long _lastPushMs; private static string _lastPayload = string.Empty; private static long _lastIltPushMs; private static string _lastIltPayload = string.Empty; // Server plugins (sections manifests that declare a server): the last payload each took, and when. private static readonly Dictionary _serverPluginPushes = new(StringComparer.OrdinalIgnoreCase); /// RynthAi is installed: Tick pushes it the list. public static bool PushToRynthAi { get; set; } /// RynthILT is installed: Tick pushes it the list too (its ILT settings view). public static bool PushToRynthIlt { get; set; } /// One menu item: a command, with the section it belongs to. internal sealed record MenuItem(string Label, string Command, string Tooltip, string Feature, bool Enabled, string Note); /// A server's items for the menus: every section's, surfaces as "open the section". internal sealed record MenuGroup(ServerState Server, MenuItem[] Items); /// /// Runs an item's command: an engine "/rc" command directly, anything else through the /// chat dispatcher (plugins' commands, as if typed). Render thread. /// public static void Run(string command) { string cmd = SectionManifestParser.Command(command); if (cmd.Length == 0) return; if (Compatibility.RynthCoreChatCommands.TryHandle(cmd)) return; if (cmd.StartsWith("/rc ", StringComparison.OrdinalIgnoreCase)) { RynthLog.UI($"ServerMenus: '{cmd}' is not an engine command."); return; } Compatibility.ChatCommandDispatcher.Dispatch(cmd); } /// The command that opens section in the Mini Remote. public static string OpenSectionCommand(string sectionId) => "/rc miniremote section " + sectionId; /// The command that forces on for this character. public static string ForceCommand(string serverId) => "/rc server " + serverId + " on"; /// Every server that has items (active or not), with them. Any thread. public static List Groups(SectionRegistry.Snapshot snap) { var byServer = new Dictionary>(StringComparer.OrdinalIgnoreCase); foreach (SectionManifest m in snap.Manifests) foreach (SectionContribution c in m.Contributions) { if (c.IsUniversal) continue; if (!byServer.TryGetValue(c.Server, out var list)) byServer[c.Server] = list = new List(); list.Add(c.IsSurface ? new MenuItem(c.Label, OpenSectionCommand(c.Section), c.Tooltip, SectionFeaturePrefix + c.Section, c.Enabled, c.Note) : new MenuItem(c.Label, c.Command, c.Tooltip, string.Empty, c.Enabled, c.Note)); } var groups = new List(); foreach (ServerState s in snap.Servers) if (byServer.TryGetValue(s.Id, out var items)) groups.Add(new MenuGroup(s, items.ToArray())); return groups; } // ── RynthAi push ─────────────────────────────────────────────────── /// /// Render thread, every frame: sends RynthAi the list when it changed (a server /// detected or forced, or not any more; a plugin's sections) and every . /// public static void Tick() { SectionRegistry.Tick(); SectionRegistry.Snapshot snap = SectionRegistry.Current; List serverPlugins = ServerPluginTargets(snap); if (!PushToRynthAi && !PushToRynthIlt && serverPlugins.Count == 0) return; long now = Environment.TickCount64; if (now < _nextCheckMs) return; _nextCheckMs = now + CheckMs; string payload = BuildPayload(snap, IltWorldNames()); // Same resend rule as RynthILT's: a plugin may (re)load after the last change. foreach (string plugin in serverPlugins) { bool due = !_serverPluginPushes.TryGetValue(plugin, out var last) || last.Payload != payload || now - last.AtMs >= IltResendMs; if (due && Plugins.PluginManager.SendPluginCommand(plugin, "servermenu", payload)) _serverPluginPushes[plugin] = (payload, now); } if (PushToRynthAi && (payload != _lastPayload || now - _lastPushMs >= ResendMs)) { _lastPayload = payload; _lastPushMs = now; RynthAiCommands.ApplyRemoteCommand("servermenu", payload); } // Not delivered (RynthILT not loaded yet, or an older one without the export): try again next check. if (PushToRynthIlt && (payload != _lastIltPayload || now - _lastIltPushMs >= IltResendMs) && Plugins.PluginManager.SendPluginCommand(RynthIltPlugin, "servermenu", payload)) { _lastIltPayload = payload; _lastIltPushMs = now; } } /// /// The plugins to push to besides RynthAi and RynthILT (they have their own paths): each one /// whose sections manifest declares a server. Pure apart from reading . /// internal static List ServerPluginTargets(SectionRegistry.Snapshot snap) { var list = new List(); foreach (SectionManifest m in snap.Manifests) { if (m.Servers.Length == 0 || m.Plugin.Length == 0) continue; if (m.Plugin.Equals("RynthAi", StringComparison.OrdinalIgnoreCase) || (PushToRynthIlt && m.Plugin.Equals(RynthIltPlugin, StringComparison.OrdinalIgnoreCase))) continue; if (!list.Exists(p => p.Equals(m.Plugin, StringComparison.OrdinalIgnoreCase))) list.Add(m.Plugin); } return list; } /// The ILT world-name patterns in force, and whether they are the built-in defaults. private static (string Server, string Pattern, bool Default)[] IltWorldNames() { IReadOnlyList? names = Plugins.EngineSettings.IltServerNames; IReadOnlyList list = names ?? Compatibility.IltDetect.DefaultServerNames; var result = new (string, string, bool)[list.Count]; for (int i = 0; i < list.Count; i++) result[i] = ("ilt", list[i], names == null); return result; } /// /// The push text for and the servers' . /// Pure apart from reading them. /// internal static string BuildPayload(SectionRegistry.Snapshot snap, IReadOnlyList<(string Server, string Pattern, bool Default)>? worldNames = null) { var sb = new StringBuilder(512); foreach (ServerState s in snap.Servers) sb.Append('V').Append('\t').Append(Clean(s.Id)).Append('\t').Append(Clean(s.Name)).Append('\t') .Append(s.Active ? '1' : '0').Append('\t').Append(ServerForceText.Name(s.Force)).Append('\t') .Append(s.Detected ? '1' : '0').Append('\t').Append(Clean(s.Reason ?? string.Empty)).Append('\n'); if (worldNames != null) foreach (var n in worldNames) sb.Append('N').Append('\t').Append(Clean(n.Server)).Append('\t').Append(Clean(n.Pattern)).Append('\t') .Append(n.Default ? "default" : "engine.json").Append('\n'); foreach (SectionView v in snap.Sections) sb.Append('X').Append('\t').Append(Clean(v.Id)).Append('\t').Append(Clean(v.Label)).Append('\n'); foreach (MenuGroup g in Groups(snap)) { sb.Append('S').Append('\t').Append(Clean(g.Server.Id)).Append('\t').Append(Clean(g.Server.Name)).Append('\t') .Append(g.Server.Active ? '1' : '0').Append('\t').Append(Clean(ForceCommand(g.Server.Id))).Append('\n'); foreach (MenuItem i in g.Items) sb.Append('I').Append('\t').Append(Clean(i.Label)).Append('\t').Append(Clean(i.Command)).Append('\t') .Append(Clean(i.Note.Length > 0 && !i.Enabled ? i.Note : i.Tooltip)).Append('\t').Append(Clean(i.Feature)).Append('\n'); } return sb.ToString(); } /// Field text without the payload's separators (the push is ANSI, so ASCII only). private static string Clean(string text) { var sb = new StringBuilder(text.Length); foreach (char c in text) sb.Append(c is '\t' or '\n' or '\r' ? ' ' : c < 128 ? c : '?'); return sb.ToString(); } // ── ImGui menus ──────────────────────────────────────────────────── /// /// The server half of an ImGui popup menu: each active server's name and items. With /// servers that have items but none active, a note and one "Treat this world as ..." item /// each (saved for this character). Draws nothing (not even 's /// separator) when no server has items. Returns true when it drew something. /// public static bool DrawImGuiMenu(bool separatorFirst = false) { List groups = Groups(SectionRegistry.Current); if (groups.Count == 0) return false; if (separatorFirst) ImGuiNET.ImGui.Separator(); bool any = false; foreach (MenuGroup g in groups) { if (!g.Server.Active) continue; any = true; SafeImGui.TextDisabled("Server: " + g.Server.Name); foreach (MenuItem item in g.Items) { if (ImGuiNET.ImGui.MenuItem(item.Label + "##srv_" + g.Server.Id + "_" + item.Command, "", false, item.Enabled)) Run(item.Command); string tip = item.Enabled || item.Note.Length == 0 ? item.Tooltip : item.Note; if (tip.Length > 0) SafeImGui.SetItemTooltip(tip); } } if (any) return true; ImGuiNET.ImGui.TextDisabled("Server: no installed server plugin matches this world"); foreach (MenuGroup g in groups) { if (ImGuiNET.ImGui.MenuItem("Treat this world as " + g.Server.Name + "##srvforce_" + g.Server.Id)) Run(ForceCommand(g.Server.Id)); ImGuiNET.ImGui.SetItemTooltip("Saved for this character. Undo with /rc server " + g.Server.Id + " auto, or in the Mini Remote's Char section."); } return true; } /// /// A "Sections" submenu: every Mini Remote section, opening it there. Returns true when /// one was picked. /// public static bool DrawSectionsMenu(string label = "Sections") { SectionRegistry.Snapshot snap = SectionRegistry.Current; if (snap.Sections.Count == 0 || !ImGuiNET.ImGui.BeginMenu(label)) return false; bool picked = false; foreach (SectionView v in snap.Sections) { if (ImGuiNET.ImGui.MenuItem(v.Label + "##sec_" + v.Id, "", false, v.HasContent || v.Core)) { Run(OpenSectionCommand(v.Id)); picked = true; } } ImGuiNET.ImGui.EndMenu(); return picked; } }