// ServerMenuModel.cs - The servers and sections RynthCore pushes for the Mini Remote's menus. // // RynthCore decides which servers the client is on (one world check for the engine and every // plugin, with a per-character Auto / On / Off override) and what the engine and the installed // plugins add for each (UI/ServerMenus.cs and UI/Sections in RynthCore). It sends that list as // the "servermenu" remote command when it changes, and again every 30 s. Tab-separated lines: // V id name active 0/1 force auto|on|off detected 0/1 every server (RynthCore 2026.10.5.46+) // X id label every Mini Remote section, rail order (.46+) // S id name active 0/1 force command a server with items // I label command tooltip feature its items // A "tab:" feature is one of RynthAi's own Mini Remote tabs (older RynthCore); "section:" // opens that section in RynthCore's Mini Remote (its command does that). // // Groups stays null until the first push: an older RynthCore, or the ImGui shell off. The menu // then falls back to RynthILT's own ILT entries (RynthILT.Hub section "server-menu"). Servers stays // empty until a RynthCore that sends V lines: the world check is then RynthILT's own. using System; using System.Collections.Generic; using System.Threading; namespace RynthCore.Plugin.RynthAi.Huds; /// A server section item: an engine "/rc" command, or a Mini Remote tab ("tab:<id>" feature). internal sealed record ServerMenuEntry(string Label, string Command, string Tooltip, string Feature); /// One server's section: shown while (detected or forced in RynthCore). internal sealed record ServerMenuGroup(string Id, string Name, bool Active, string ForceCommand, ServerMenuEntry[] Items); /// RynthCore's verdict for one server: active now, its saved override ("auto", "on", "off"), what detection said. internal sealed record ServerVerdictLine(string Id, string Name, bool Active, string Force, bool Detected); /// One of RynthCore's Mini Remote sections. internal sealed record SectionLine(string Id, string Label); /// Everything one push carries. internal sealed record ServerMenuPayload(ServerMenuGroup[] Groups, ServerVerdictLine[] Servers, SectionLine[] Sections); internal static class ServerMenuModel { /// Feature prefix for items that are Mini Remote tabs (drawn by RynthAi itself). public const string TabFeaturePrefix = "tab:"; /// Feature prefix for items that open a section of RynthCore's Mini Remote. public const string SectionFeaturePrefix = "section:"; private static ServerMenuGroup[]? _groups; private static ServerVerdictLine[] _servers = Array.Empty(); private static SectionLine[] _sections = Array.Empty(); /// The latest sections from RynthCore; null until it sends any. Any thread. public static ServerMenuGroup[]? Groups => Volatile.Read(ref _groups); /// The latest server verdicts (empty from a RynthCore older than 2026.10.5.46). Any thread. public static ServerVerdictLine[] Servers => Volatile.Read(ref _servers); /// RynthCore's Mini Remote sections, rail order (empty from an older RynthCore). Any thread. public static SectionLine[] Sections => Volatile.Read(ref _sections); /// Pump thread: stores a "servermenu" push. A malformed line is skipped, never thrown. public static void Apply(string payload) { ServerMenuPayload p = ParsePayload(payload); Volatile.Write(ref _servers, p.Servers); Volatile.Write(ref _sections, p.Sections); Volatile.Write(ref _groups, p.Groups); } /// /// RynthCore's verdict for server : true / false once it sends V lines, /// null before (an older RynthCore, or no push yet). Any thread. /// public static bool? ServerActive(string id) { foreach (ServerVerdictLine s in Servers) if (s.Id.Equals(id, StringComparison.OrdinalIgnoreCase)) return s.Active; return null; } /// RynthCore's line for server , or null. public static ServerVerdictLine? Server(string id) { foreach (ServerVerdictLine s in Servers) if (s.Id.Equals(id, StringComparison.OrdinalIgnoreCase)) return s; return null; } /// /// Parses a payload's server groups. Items before the first section line, short lines and /// unknown line kinds are skipped. /// public static ServerMenuGroup[] Parse(string? payload) => ParsePayload(payload).Groups; /// Parses a whole payload (groups, server verdicts, sections). Never throws. public static ServerMenuPayload ParsePayload(string? payload) { var groups = new List(); var servers = new List(); var sections = new List(); if (string.IsNullOrEmpty(payload)) return new ServerMenuPayload(groups.ToArray(), servers.ToArray(), sections.ToArray()); string id = "", name = "", force = ""; bool active = false, open = false; var items = new List(); void Close() { if (open) groups.Add(new ServerMenuGroup(id, name, active, force, items.ToArray())); items.Clear(); open = false; } foreach (string raw in payload.Split('\n')) { string line = raw.TrimEnd('\r'); if (line.Length == 0) continue; string[] f = line.Split('\t'); if (f[0] == "S" && f.Length >= 5) { Close(); id = f[1]; name = f[2]; active = f[3] == "1"; force = f[4]; open = id.Length > 0 && name.Length > 0; } else if (f[0] == "I" && f.Length >= 5 && open && f[1].Length > 0) { items.Add(new ServerMenuEntry(f[1], f[2], f[3], f[4])); } else if (f[0] == "V" && f.Length >= 6 && f[1].Length > 0) { servers.Add(new ServerVerdictLine(f[1], f[2].Length > 0 ? f[2] : f[1], f[3] == "1", f[4], f[5] == "1")); } else if (f[0] == "X" && f.Length >= 3 && f[1].Length > 0 && f[2].Length > 0) { sections.Add(new SectionLine(f[1], f[2])); } } Close(); return new ServerMenuPayload(groups.ToArray(), servers.ToArray(), sections.ToArray()); } /// The tab id of a "tab:<id>" feature, else null. public static string? TabOf(string feature) => feature.StartsWith(TabFeaturePrefix, StringComparison.OrdinalIgnoreCase) && feature.Length > TabFeaturePrefix.Length ? feature.Substring(TabFeaturePrefix.Length) : null; /// The section id of a "section:<id>" feature, else null. public static string? SectionOf(string feature) => feature.StartsWith(SectionFeaturePrefix, StringComparison.OrdinalIgnoreCase) && feature.Length > SectionFeaturePrefix.Length ? feature.Substring(SectionFeaturePrefix.Length) : null; }