From 726e0287f25b2e5087b060e6ad8eb08c7e9c8995 Mon Sep 17 00:00:00 2001 From: Daniel Wallace Date: Sun, 15 Nov 2026 10:22:06 -0800 Subject: [PATCH] Network tab sends ChatNetwork.Rewrite sends a plain line typed on a network tab as / global while on the network (not sent off it); commands are unchanged. ChatModel.OnSend applies it only on a tab owned by an active chatTabs entry that declares network. On is unknown (treated as on) until the join status exists. The input placeholder follows the state. Co-Authored-By: Claude Opus 5.5 --- src/RynthCore.Engine/ImGui/Panels/ChatFace.cs | 2 +- src/RynthCore.Engine/UI/Data/ChatData.cs | 37 ++++++++++++++++ src/RynthCore.Engine/UI/Data/ChatNetwork.cs | 44 +++++++++++++++++++ tools/ChatFilterTests/ChatFilterTests.csproj | 2 + tools/ChatFilterTests/Program.cs | 32 ++++++++++++++ 5 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 src/RynthCore.Engine/UI/Data/ChatNetwork.cs diff --git a/src/RynthCore.Engine/ImGui/Panels/ChatFace.cs b/src/RynthCore.Engine/ImGui/Panels/ChatFace.cs index ca483cd..f304320 100644 --- a/src/RynthCore.Engine/ImGui/Panels/ChatFace.cs +++ b/src/RynthCore.Engine/ImGui/Panels/ChatFace.cs @@ -431,7 +431,7 @@ internal sealed class ChatFace : IImGuiPanel if (text.Length == 0 && !typing) { _inputScroll = 0; - dl.AddText(new Vector2(p.X + 5, ty), Faded(Mute), ChatModel.InputHint ?? "Press Enter to chat…"); + dl.AddText(new Vector2(p.X + 5, ty), Faded(Mute), ChatModel.InputPlaceholder ?? "Press Enter to chat…"); } else { diff --git a/src/RynthCore.Engine/UI/Data/ChatData.cs b/src/RynthCore.Engine/UI/Data/ChatData.cs index 52576a4..e1b4607 100644 --- a/src/RynthCore.Engine/UI/Data/ChatData.cs +++ b/src/RynthCore.Engine/UI/Data/ChatData.cs @@ -1278,6 +1278,10 @@ internal static class ChatModel public static int InputCursor => Volatile.Read(ref _input).Cursor; /// Shown in the empty input box instead of the usual hint (e.g. "select an NPC first"). public static string? InputHint => Volatile.Read(ref _input).Hint; + /// What the empty input box shows: the hint if one is set, else the network tab's line, else null (the face's usual one). + public static string? InputPlaceholder => + InputHint ?? (CurrentNetwork() == null ? null + : ChatNetwork.On == false ? "Join the network to chat here" : "Press Enter to chat in Global (Dereth Network)"); public static long InputVersion => Interlocked.Read(ref _inputVersion); /// Raised on AC's main thread after the input line changes. @@ -1455,6 +1459,14 @@ internal static class ChatModel SetInput("", 0, null); IntPtr send = SendLinePtr; if (text.Length == 0 || send == IntPtr.Zero) return; + // On a network tab (CurrentNetwork) a plain line goes through the network's command; off + // the network it is not sent (the placeholder says so, InputHint). Commands go as typed. + if (CurrentNetwork() is { } network) + { + string? rewritten = ChatNetwork.Rewrite(text, network.Command, ChatNetwork.OnOrUnknown); + if (rewritten == null) return; + text = rewritten; + } IntPtr ptr = Marshal.StringToHGlobalAnsi(text); // RynthChat sends the line through Host.InvokeChatParser, on this thread: an engine /rc // command in it is the player's, so it answers in chat (RynthCoreChatCommands.PlayerLine). @@ -1466,6 +1478,31 @@ internal static class ChatModel finally { Marshal.FreeHGlobal(ptr); } } + /// + /// The network declaration of the current tab, or null when the tab is not a network tab: the + /// tab is a server's seeded tab (tabServers), one of that server's active entries owns a rule + /// that fills it, and the entry declares "network". Any thread. + /// + public static SectionNetworkDecl? CurrentNetwork() => NetworkOfTab(CurrentTab()); + + /// The network declaration of (see CurrentNetwork), or null. + public static SectionNetworkDecl? NetworkOfTab(string tab) + { + if (!Volatile.Read(ref _tabServers).TryGetValue(tab, out string? server)) return null; + string[] active = _activeServers; + if (!ChatRouter.ServerIn(active, server)) return null; + foreach (SectionChatTabDecl d in _tabDecls) + { + if (d.Network == null || !string.Equals(d.Server, server, StringComparison.OrdinalIgnoreCase)) continue; + string key = ChatServerTabs.OwnerKey(d); + foreach (ChatFilterRule rule in Filters) + if (string.Equals(rule.Owner, key, StringComparison.OrdinalIgnoreCase) + && string.Equals(rule.Tab, tab, StringComparison.OrdinalIgnoreCase)) + return d.Network; + } + return null; + } + /// The creature selected in the game (an NPC or another player), for /tell. /// AC's main thread only. Never yourself, never an item. private static bool TryGetSelectedTellName(out string name) diff --git a/src/RynthCore.Engine/UI/Data/ChatNetwork.cs b/src/RynthCore.Engine/UI/Data/ChatNetwork.cs new file mode 100644 index 0000000..bbed857 --- /dev/null +++ b/src/RynthCore.Engine/UI/Data/ChatNetwork.cs @@ -0,0 +1,44 @@ +// ============================================================================ +// RynthCore.Engine - UI/Data/ChatNetwork.cs +// Chat network tabs (2026-11-15): a chatTabs entry with "network" is a tab for a chat +// network (the Dereth Network). While that tab is current, a typed line that is not a +// command is sent through the network's own command ("/ global "), and +// only while the player is on the network. Pure and engine-free: ChatModel.OnSend +// calls Rewrite; tools/ChatFilterTests compiles this file as is. +// ============================================================================ + +using System.Threading; + +namespace RynthCore.Engine.UI.Data; + +internal static class ChatNetwork +{ + // -1 unknown, 0 off the network, 1 on it. An int so any thread can read and write it whole. + private static int _on = -1; + + /// + /// Is the player on the network this session? null = not known yet (Task 11 fills it; until then + /// a line is sent as if on). Any thread. + /// + public static bool? On + { + get => Volatile.Read(ref _on) switch { 0 => false, 1 => true, _ => null }; + set => Volatile.Write(ref _on, value == null ? -1 : value.Value ? 1 : 0); + } + + /// Is a line to be sent as if on the network? Unknown counts as on. + public static bool OnOrUnknown => On != false; + + /// + /// What to send for a line typed on a network tab, or null when it must not be sent. The line is + /// trimmed; empty is null; a line starting with / or @ is a command and goes unchanged; any other + /// goes as "/<command> global <text>" while on the network, and is not sent off it. + /// + internal static string? Rewrite(string line, string command, bool onNetwork) + { + string text = (line ?? string.Empty).Trim(); + if (text.Length == 0) return null; + if (text[0] == '/' || text[0] == '@') return text; + return onNetwork ? "/" + command + " global " + text : null; + } +} diff --git a/tools/ChatFilterTests/ChatFilterTests.csproj b/tools/ChatFilterTests/ChatFilterTests.csproj index 353492c..3e352cd 100644 --- a/tools/ChatFilterTests/ChatFilterTests.csproj +++ b/tools/ChatFilterTests/ChatFilterTests.csproj @@ -41,6 +41,8 @@ + + diff --git a/tools/ChatFilterTests/Program.cs b/tools/ChatFilterTests/Program.cs index 179af1b..4f9e23f 100644 --- a/tools/ChatFilterTests/Program.cs +++ b/tools/ChatFilterTests/Program.cs @@ -58,6 +58,7 @@ internal static class Program ServerTabDiscovery(); ServerTabPlans(); PinBookkeeping(); + NetworkRewrite(); SharedFileReload(); LineColorTags(); LineColorKinds(); @@ -1606,6 +1607,37 @@ internal static class Program } // 2026-11-15: pinned tab bookkeeping (ChatPins), pure. + private static void NetworkRewrite() + { + Case("Rewrite_PlainText"); + Check(ChatNetwork.Rewrite("hello all", "derethnetwork", true) == "/derethnetwork global hello all", "on the network: sent through the command"); + Check(ChatNetwork.Rewrite("hello all", "derethnetwork", false) == null, "off the network: not sent"); + + Case("Rewrite_CommandsPassThrough"); + Check(ChatNetwork.Rewrite("/tell Bob@coldeve, hi", "dn", true) == "/tell Bob@coldeve, hi", "/tell unchanged on"); + Check(ChatNetwork.Rewrite("/tell Bob@coldeve, hi", "dn", false) == "/tell Bob@coldeve, hi", "/tell unchanged off"); + Check(ChatNetwork.Rewrite("@derethnetwork on", "dn", true) == "@derethnetwork on", "@command unchanged on"); + Check(ChatNetwork.Rewrite("@derethnetwork on", "dn", false) == "@derethnetwork on", "@command unchanged off"); + + Case("Rewrite_Trimmed"); + Check(ChatNetwork.Rewrite(" hi there ", "dn", true) == "/dn global hi there", "leading and trailing spaces trimmed"); + Check(ChatNetwork.Rewrite(" /tell Bob, hi ", "dn", true) == "/tell Bob, hi", "a command after spaces is still a command"); + Check(ChatNetwork.Rewrite(" @dn on", "dn", false) == "@dn on", "an @command after spaces is still a command"); + + Case("Rewrite_Empty"); + Check(ChatNetwork.Rewrite("", "dn", true) == null && ChatNetwork.Rewrite("", "dn", false) == null, "empty is never sent"); + Check(ChatNetwork.Rewrite(" \t ", "dn", true) == null && ChatNetwork.Rewrite(" ", "dn", false) == null, "whitespace only is never sent"); + + Case("NetworkState_DefaultsUnknown"); + Check(ChatNetwork.On == null && ChatNetwork.OnOrUnknown, "unknown by default and treated as on"); + ChatNetwork.On = false; + Check(ChatNetwork.On == false && !ChatNetwork.OnOrUnknown, "off"); + ChatNetwork.On = true; + Check(ChatNetwork.On == true && ChatNetwork.OnOrUnknown, "on"); + ChatNetwork.On = null; + Check(ChatNetwork.On == null, "reset to unknown"); + } + private static void PinBookkeeping() { Case("Toggle_AddsAtEndAndRemoves");