diff --git a/src/RynthCore.Engine/UI/Data/ChatData.cs b/src/RynthCore.Engine/UI/Data/ChatData.cs index b84d41c..e773554 100644 --- a/src/RynthCore.Engine/UI/Data/ChatData.cs +++ b/src/RynthCore.Engine/UI/Data/ChatData.cs @@ -695,13 +695,13 @@ internal static class ChatModel { name = name.Trim(); if (name.Length == 0) return null; - // Every tab, also one hidden because its server is not active (as RenameCustomTab), or a - // second tab of the same name would be made beside it. - string? existing = BuildTabs(null).FirstOrDefault(t => string.Equals(t, name, StringComparison.OrdinalIgnoreCase)); - if (existing == null || !IsBaseTab(existing)) EnsureCustomTab(existing ?? name); - if (select) ActiveChannel = existing ?? name; + // A tab the player sees by that name is that tab; one hidden because its server is not + // active is that server's, so the new tab gets a free name beside it ("Network 2"). + (string tab, bool existing) = ChatServerTabs.ResolveNewTab(name, AllTabs(), BuildTabs(null)); + if (!existing || !IsBaseTab(tab)) EnsureCustomTab(tab); + if (select) ActiveChannel = tab; SaveSettings(); - return existing ?? name; + return tab; } private static void EnsureCustomTab(string name) @@ -1709,9 +1709,13 @@ internal static class ChatModel } catch (Exception ex) { - RynthLog.UI($"ChatModel: settings load failed: {ex.Message}"); - // One copy per run: the retry every 2 s must not leave a copy each time. - if (!_keptUnreadable) { _keptUnreadable = true; KeepUnreadable(); } + // Logged and copied once per run: the retry every 2 s must not repeat either. + if (!_keptUnreadable) + { + _keptUnreadable = true; + RynthLog.UI($"ChatModel: settings load failed (retrying every 2 s, logged once): {ex.Message}"); + KeepUnreadable(); + } return false; } } diff --git a/src/RynthCore.Engine/UI/Data/ChatServerTabs.cs b/src/RynthCore.Engine/UI/Data/ChatServerTabs.cs index 470d2d6..817a2a1 100644 --- a/src/RynthCore.Engine/UI/Data/ChatServerTabs.cs +++ b/src/RynthCore.Engine/UI/Data/ChatServerTabs.cs @@ -168,9 +168,27 @@ internal static class ChatServerTabs private static string FreeName(string name, string clashBase, IReadOnlyCollection tabs) { if (!Has(tabs, name)) return name; - if (!Has(tabs, clashBase)) return clashBase; + return Numbered(clashBase, tabs); + } + + /// if no tab has it, else "name 2", "name 3"... + private static string Numbered(string name, IReadOnlyCollection tabs) + { + if (!Has(tabs, name)) return name; for (int n = 2; ; n++) - if (!Has(tabs, clashBase + " " + n)) return clashBase + " " + n; + if (!Has(tabs, name + " " + n)) return name + " " + n; + } + + /// + /// The tab "New tab: " means (ChatModel.AddCustomTab): a tab the player + /// can see by that name is that tab (Existing); one that exists but is hidden (an inactive + /// server's) is not theirs to fill, so a free "name 2" is made instead; else the name itself. + /// + internal static (string Tab, bool Existing) ResolveNewTab(string name, IReadOnlyCollection visibleTabs, IReadOnlyCollection everyTab) + { + string? shown = visibleTabs.FirstOrDefault(t => string.Equals(t, name, StringComparison.OrdinalIgnoreCase)); + if (shown != null) return (shown, true); + return (Numbered(name, everyTab), false); } /// diff --git a/tools/ChatFilterTests/Program.cs b/tools/ChatFilterTests/Program.cs index f1b5c79..043014f 100644 --- a/tools/ChatFilterTests/Program.cs +++ b/tools/ChatFilterTests/Program.cs @@ -1449,6 +1449,16 @@ internal static class Program Check(!offRoute.ShowsIn("Network", "Chat") && offRoute.ShowsIn("Every", "Chat"), "another server: only the every-server rule runs"); Check(!none.ShowsIn("Network", "Chat") && none.ShowsIn("Every", "Chat"), "no servers known: only the every-server rule runs"); + Case("ResolveNewTab_HiddenServerTabIsAClash"); + // "New tab: Network" on another server, where the DreamWeave Network tab is hidden. + string[] visible = { "All", "Chat", "Mine" }; + string[] everyTab = { "All", "Chat", "Mine", "Network", "Network 2" }; + Check(ChatServerTabs.ResolveNewTab("mine", visible, everyTab) == ("Mine", true), "a visible tab of that name is that tab (its own case)"); + Check(ChatServerTabs.ResolveNewTab("chat", visible, everyTab) == ("Chat", true), "a base tab too"); + Check(ChatServerTabs.ResolveNewTab("Network", visible, everyTab) == ("Network 3", false), "a hidden server tab: a free numbered name, not the server's tab"); + Check(ChatServerTabs.ResolveNewTab("Network", visible, new[] { "All", "Network" }) == ("Network 2", false), "the first free number"); + Check(ChatServerTabs.ResolveNewTab("Fresh", visible, everyTab) == ("Fresh", false), "no match: the name itself, new"); + Case("Apply rules compile"); foreach (RynthChatFilterDto r in new TabSim { Room = "LFG" }.Apply(d).Inserts) {