From f2d7f0464ccdf0dd1284f7b31ef1768f36ebd4bc Mon Sep 17 00:00:00 2001 From: Daniel Wallace Date: Sun, 15 Nov 2026 08:22:56 -0800 Subject: [PATCH] Sections manifest: parse chatTabs (server default chat tabs) Co-Authored-By: Claude Opus 5.5 --- .../UI/Sections/SectionManifest.cs | 67 +++++++++++++++++++ tools/SectionsTests/Program.cs | 53 +++++++++++++++ 2 files changed, 120 insertions(+) diff --git a/src/RynthCore.Engine/UI/Sections/SectionManifest.cs b/src/RynthCore.Engine/UI/Sections/SectionManifest.cs index 08f5de2..95942d5 100644 --- a/src/RynthCore.Engine/UI/Sections/SectionManifest.cs +++ b/src/RynthCore.Engine/UI/Sections/SectionManifest.cs @@ -61,6 +61,17 @@ // command, or with a duplicate id, is dropped; at most MaxBarMenuItems (6) // are kept, and a "barMenu" that is not an array is ignored (the window is // kept). Engines before v95 ignore the key. +// chatTabs (2026-11-15) default RynthChat tabs a server's plugin declares: +// [ { "server": "dreamweave", "id": "dereth-network", "name": "Network", +// "pinned": true, "globalRoomFallback": "Roleplay", +// "rules": [ { "action": "copy", "when": "type:3", "mode": "", +// "pattern": "...", "globalRoom": false } ] } ]. +// "server" and "id" are ids (the owner key is "server/id"); "action" is copy +// or move; "globalRoomFallback" is General, Trade, LFG or Roleplay (the +// room a "globalRoom": true rule matches until chat shows another). An +// entry with a bad field is dropped and reported in the parse error (the +// other keys still parse); at most MaxChatTabs entries of MaxChatRules +// rules each. Engines before this ignore the key. // // Everything from a plugin is checked here: ids are short lower-case words, // text is capped, a command must be a single "/..." chat command, and the lists @@ -145,6 +156,12 @@ internal sealed record SectionWindowDecl( public SectionBarMenuItem[] Menu => BarMenu ?? System.Array.Empty(); } +/// One rule of a plugin-declared chat tab ("chatTabs"); a GlobalRoom rule's pattern follows the room discovered from chat. +internal sealed record SectionChatRuleDecl(string Action, string When, string Mode, string Pattern, bool GlobalRoom); + +/// A default chat tab a server's plugin declares ("chatTabs", 2026-11-15); owner key = Server + "/" + Id. +internal sealed record SectionChatTabDecl(string Server, string Id, string Name, bool Pinned, string GlobalRoomFallback, SectionChatRuleDecl[] Rules); + /// One plugin's parsed manifest. internal sealed record SectionManifest( string Plugin, @@ -157,6 +174,9 @@ internal sealed record SectionManifest( /// The windows the plugin draws (plugin API v89 engines host them; older ones ignore the key). public SectionWindowDecl[] Windows { get; init; } = Array.Empty(); + + /// The default chat tabs the plugin declares for its server (2026-11-15; older engines ignore the key). + public SectionChatTabDecl[] ChatTabs { get; init; } = Array.Empty(); } internal static class SectionManifestParser @@ -178,6 +198,9 @@ internal static class SectionManifestParser public const int MaxWindowTitleLength = 32; /// Most items kept in one window's bar-icon dropdown (v95). public const int MaxBarMenuItems = 6; + /// Most chat tab entries kept per manifest, and rules per entry (2026-11-15). + public const int MaxChatTabs = 8; + public const int MaxChatRules = 16; public const float DefaultWindowWidth = 420f, DefaultWindowHeight = 300f, MinWindowSize = 120f, MaxWindowSize = 2000f; /// @@ -278,9 +301,20 @@ internal static class SectionManifestParser BarMenu: barMenu.Count > 0 ? barMenu.ToArray() : null)); } + var chatTabs = new List(); + foreach (JsonElement e in Array(root, "chatTabs")) + { + if (chatTabs.Count >= MaxChatTabs) { Note(ref error, $"chatTabs: more than {MaxChatTabs} entries, the rest dropped"); break; } + SectionChatTabDecl? decl = ChatTab(e, out string why); + if (decl == null) { Note(ref error, "chatTabs: entry dropped, " + why); continue; } + if (chatTabs.Exists(c => c.Server == decl.Server && c.Id == decl.Id)) { Note(ref error, "chatTabs: duplicate " + decl.Server + "/" + decl.Id); continue; } + chatTabs.Add(decl); + } + return new SectionManifest(plugin, servers.ToArray(), sections.ToArray(), contributions.ToArray()) { Windows = windows.ToArray(), + ChatTabs = chatTabs.ToArray(), }; } catch (JsonException ex) @@ -290,6 +324,39 @@ internal static class SectionManifestParser } } + /// Keeps the first note; a dropped chatTabs entry is reported without discarding the rest of the manifest. + private static void Note(ref string error, string note) + { + if (error.Length == 0) error = note; + } + + /// One chatTabs entry, or null (with the reason) when a field is bad. + private static SectionChatTabDecl? ChatTab(JsonElement e, out string why) + { + why = string.Empty; + string server = Id(Str(e, "server")); + if (server.Length == 0) { why = "missing or bad server"; return null; } + string id = Id(Str(e, "id")); + if (id.Length == 0) { why = "missing or bad id"; return null; } + string name = Text(Str(e, "name"), MaxLabelLength); + if (name.Length == 0) { why = "missing name"; return null; } + string fallback = Str(e, "globalRoomFallback").Trim(); + string room = fallback.Length == 0 ? "General" : GlobalRooms.FirstOrDefault(r => r.Equals(fallback, StringComparison.OrdinalIgnoreCase)) ?? string.Empty; + if (room.Length == 0) { why = "globalRoomFallback is not General, Trade, LFG or Roleplay"; return null; } + var rules = new List(); + foreach (JsonElement r in Array(e, "rules")) + { + if (rules.Count >= MaxChatRules) { why = $"more than {MaxChatRules} rules"; return null; } + string action = Str(r, "action").Trim().ToLowerInvariant(); + if (action is not ("copy" or "move")) { why = "rule action is not copy or move"; return null; } + rules.Add(new SectionChatRuleDecl(action, Text(Str(r, "when"), MaxLabelLength), Text(Str(r, "mode"), MaxLabelLength), + Text(Str(r, "pattern"), MaxTextLength), Bool(r, "globalRoom", false))); + } + return new SectionChatTabDecl(server, id, name, Bool(e, "pinned", false), room, rules.ToArray()); + } + + private static readonly string[] GlobalRooms = { "General", "Trade", "LFG", "Roleplay" }; + // ── Field rules ───────────────────────────────────────────────────── /// A lower-case id of letters, digits, '-' and '_' (at most MaxIdLength), else "". diff --git a/tools/SectionsTests/Program.cs b/tools/SectionsTests/Program.cs index bfdeb98..0d66f61 100644 --- a/tools/SectionsTests/Program.cs +++ b/tools/SectionsTests/Program.cs @@ -34,6 +34,7 @@ internal static class Program WindowTakeOver(); WindowFrameless(); ManifestBarMenu(); + ManifestChatTabs(); BarMenuHoverTiming(); BarMenuReload(); StackCheckAfterPluginDraw(); @@ -594,6 +595,58 @@ internal static class Program "{ \"version\": 1, \"windows\": [ { \"id\": \"server\", \"title\": \"DreamWeave\", \"surface\": \"window:server\", \"bar\": true" + windowExtra + " } ] }"; + private static void ManifestChatTabs() + { + Case("manifest: chatTabs (2026-11-15)"); + const string good = """ + { "chatTabs": [ { "server": "dreamweave", "id": "dereth-network", "name": "Network", "pinned": true, + "globalRoomFallback": "Roleplay", + "rules": [ { "action": "copy", "when": "type:3", "mode": "", "pattern": "^x", "globalRoom": false }, + { "action": "copy", "when": "", "mode": "startswith", "pattern": "", "globalRoom": true } ] } ] } + """; + SectionManifest m = SectionManifestParser.Parse("DW", good, out string err); + Check(err.Length == 0 && m.ChatTabs.Length == 1, "one entry parses: " + err); + SectionChatTabDecl t = m.ChatTabs[0]; + Check(t.Server == "dreamweave" && t.Id == "dereth-network" && t.Name == "Network" && t.Pinned && t.GlobalRoomFallback == "Roleplay", + "entry fields"); + Check(t.Rules.Length == 2 && t.Rules[1].GlobalRoom && t.Rules[1].Pattern == "", "rules: global-room rule with an empty pattern"); + Check(t.Rules[0].Action == "copy" && t.Rules[0].When == "type:3" && t.Rules[0].Pattern == "^x" && !t.Rules[0].GlobalRoom, "rule 0 fields"); + + Check(SectionManifestParser.Parse("DW", """{ "contributions": [] }""", out _).ChatTabs.Length == 0, "no chatTabs key = empty array"); + + // Each reject drops the entry, reports it in error, and still parses the other keys. + string section = """ "sections": [ { "id": "quests", "label": "Quests", "order": 150 } ] """; + string[] bad = + { + """{ "id": "a", "name": "A", "globalRoomFallback": "General", "rules": [] }""", + """{ "server": "dw", "name": "A", "globalRoomFallback": "General", "rules": [] }""", + """{ "server": "dw", "id": "Bad Id", "name": "A", "globalRoomFallback": "General", "rules": [] }""", + """{ "server": "dw", "id": "a", "name": "A", "globalRoomFallback": "General", "rules": [ { "action": "delete", "when": "", "mode": "", "pattern": "x" } ] }""", + """{ "server": "dw", "id": "a", "name": "A", "globalRoomFallback": "Sports", "rules": [] }""", + }; + string[] why = { "missing server", "missing id", "bad id", "bad action", "bad fallback" }; + for (int i = 0; i < bad.Length; i++) + { + m = SectionManifestParser.Parse("DW", "{ " + section + ", \"chatTabs\": [" + bad[i] + "] }", out err); + Check(m.ChatTabs.Length == 0 && err.Length > 0 && m.Sections.Length == 1, "dropped, error set, other keys kept: " + why[i]); + } + + var entries = new System.Text.StringBuilder(); + for (int i = 0; i < 9; i++) + entries.Append(i > 0 ? "," : "").Append($"{{ \"server\": \"dw\", \"id\": \"t{i}\", \"name\": \"T{i}\", \"globalRoomFallback\": \"General\", \"rules\": [] }}"); + m = SectionManifestParser.Parse("DW", "{ \"chatTabs\": [" + entries + "] }", out err); + Check(m.ChatTabs.Length == SectionManifestParser.MaxChatTabs && SectionManifestParser.MaxChatTabs == 8 && err.Length > 0, + "more than 8 entries: the cap holds and error is set: " + m.ChatTabs.Length); + + var rules = new System.Text.StringBuilder(); + for (int i = 0; i < 17; i++) + rules.Append(i > 0 ? "," : "").Append($"{{ \"action\": \"copy\", \"when\": \"\", \"mode\": \"\", \"pattern\": \"r{i}\" }}"); + m = SectionManifestParser.Parse("DW", "{ " + section + ", \"chatTabs\": [ { \"server\": \"dw\", \"id\": \"a\", \"name\": \"A\", \"globalRoomFallback\": \"General\", \"rules\": [" + + rules + "] } ] }", out err); + Check(SectionManifestParser.MaxChatRules == 16 && m.ChatTabs.Length == 0 && err.Length > 0 && m.Sections.Length == 1, + "17 rules: entry dropped, error set"); + } + private static void ManifestBarMenu() { Case("plugin windows: barMenu (v95)");