]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Add chat.max_announcement_length cvar (#23571)
authorKot <1192090+koteq@users.noreply.github.com>
Sun, 21 Jan 2024 09:14:01 +0000 (13:14 +0400)
committerGitHub <noreply@github.com>
Sun, 21 Jan 2024 09:14:01 +0000 (20:14 +1100)
* Add announce message length to UI and make a cvar for it

* Update comm console server-side trim to use the cvar

* Rely on the new OnTextChanged event

Because OnKeyBindUp only works for keys that have binds

* Add a similar indicator to nukies' war declaration UI

* Remove message length indicators for now cuz it requires the engine update

* Rename cvar slightly

* Refactor duplicated code to a helper method

* Remove message trimming from *Window class as it's better to live in the BoundUserInterface where the other message handling happens

* Rename to chat.max_announcement_length

Content.Client/Communications/UI/CommunicationsConsoleBoundUserInterface.cs
Content.Client/Communications/UI/CommunicationsConsoleMenu.xaml.cs
Content.Client/NukeOps/WarDeclaratorBoundUserInterface.cs
Content.Client/NukeOps/WarDeclaratorWindow.xaml.cs
Content.Server/Communications/CommunicationsConsoleSystem.cs
Content.Server/NukeOps/WarDeclaratorComponent.cs
Content.Server/NukeOps/WarDeclaratorSystem.cs
Content.Shared/CCVar/CCVars.cs
Content.Shared/Chat/SharedChatSystem.cs

index dc7448aab1e5e2da7ec8ae7b4db93dd4ea6212dd..07492b310f3d70090a25ad461f62ee67070017fd 100644 (file)
@@ -1,5 +1,7 @@
-using Content.Shared.Communications;
-using Robust.Client.GameObjects;
+using Content.Shared.CCVar;
+using Content.Shared.Chat;
+using Content.Shared.Communications;
+using Robust.Shared.Configuration;
 using Robust.Shared.Timing;
 
 namespace Content.Client.Communications.UI
@@ -7,6 +9,7 @@ namespace Content.Client.Communications.UI
     public sealed class CommunicationsConsoleBoundUserInterface : BoundUserInterface
     {
         [Dependency] private readonly IGameTiming _gameTiming = default!;
+        [Dependency] private readonly IConfigurationManager _cfg = default!;
 
         [ViewVariables]
         private CommunicationsConsoleMenu? _menu;
@@ -63,22 +66,9 @@ namespace Content.Client.Communications.UI
 
         public void AnnounceButtonPressed(string message)
         {
-            var msg = (message.Length <= 256 ? message.Trim() : $"{message.Trim().Substring(0, 256)}...").ToCharArray();
-
-            // No more than 2 newlines, other replaced to spaces
-            var newlines = 0;
-            for (var i = 0; i < msg.Length; i++)
-            {
-                if (msg[i] != '\n')
-                    continue;
-
-                if (newlines >= 2)
-                    msg[i] = ' ';
-
-                newlines++;
-            }
-
-            SendMessage(new CommunicationsConsoleAnnounceMessage(new string(msg)));
+            var maxLength = _cfg.GetCVar(CCVars.ChatMaxAnnouncementLength);
+            var msg = SharedChatSystem.SanitizeAnnouncement(message, maxLength);
+            SendMessage(new CommunicationsConsoleAnnounceMessage(msg));
         }
 
         public void CallShuttle()
index 8ab444f9bafcb62667a42ad8bc710e6ebb22d1b9..37fcdd5e29c62cae9ae2285877a2299d061557b7 100644 (file)
@@ -23,7 +23,7 @@ namespace Content.Client.Communications.UI
             var loc = IoCManager.Resolve<ILocalizationManager>();
             MessageInput.Placeholder = new Rope.Leaf(loc.GetString("comms-console-menu-announcement-placeholder"));
 
-            AnnounceButton.OnPressed += (_) => Owner.AnnounceButtonPressed(Rope.Collapse(MessageInput.TextRope).Trim());
+            AnnounceButton.OnPressed += (_) => Owner.AnnounceButtonPressed(Rope.Collapse(MessageInput.TextRope));
             AnnounceButton.Disabled = !owner.CanAnnounce;
 
             AlertLevelButton.OnItemSelected += args =>
index 7394e27043a5c193ce7ceb816fef9fcb2c262a11..20103a97432ddcdf859bcf3fe7b6379a00191aae 100644 (file)
@@ -1,13 +1,16 @@
-using Content.Shared.NukeOps;
+using Content.Shared.CCVar;
+using Content.Shared.Chat;
+using Content.Shared.NukeOps;
 using JetBrains.Annotations;
-using Robust.Client.GameObjects;
-using Robust.Shared.Timing;
+using Robust.Shared.Configuration;
 
 namespace Content.Client.NukeOps;
 
 [UsedImplicitly]
 public sealed class WarDeclaratorBoundUserInterface : BoundUserInterface
 {
+    [Dependency] private readonly IConfigurationManager _cfg = default!;
+
     [ViewVariables]
     private WarDeclaratorWindow? _window;
 
@@ -44,6 +47,8 @@ public sealed class WarDeclaratorBoundUserInterface : BoundUserInterface
 
     private void OnWarDeclaratorActivated(string message)
     {
-        SendMessage(new WarDeclaratorActivateMessage(message));
+        var maxLength = _cfg.GetCVar(CCVars.ChatMaxAnnouncementLength);
+        var msg = SharedChatSystem.SanitizeAnnouncement(message, maxLength);
+        SendMessage(new WarDeclaratorActivateMessage(msg));
     }
 }
index 8fb10b8215d84b7c42d7ab95701b10be5b85b286..104e776daa48d3811a1e7a6d29fcd1de3d448a87 100644 (file)
@@ -2,7 +2,6 @@
 using Content.Shared.NukeOps;
 using Robust.Client.AutoGenerated;
 using Robust.Client.Graphics;
-using Robust.Client.UserInterface.Controls;
 using Robust.Client.UserInterface.CustomControls;
 using Robust.Client.UserInterface.XAML;
 using Robust.Shared.Timing;
@@ -27,7 +26,7 @@ public sealed partial class WarDeclaratorWindow : DefaultWindow
 
         _gameTiming = IoCManager.Resolve<IGameTiming>();
 
-        WarButton.OnPressed += ActivateWarDeclarator;
+        WarButton.OnPressed += (_) => OnActivated?.Invoke(Rope.Collapse(MessageEdit.TextRope));
 
         var loc = IoCManager.Resolve<ILocalizationManager>();
         MessageEdit.Placeholder = new Rope.Leaf(loc.GetString("war-declarator-message-placeholder"));
@@ -129,10 +128,4 @@ public sealed partial class WarDeclaratorWindow : DefaultWindow
                 return;
         }
     }
-
-    private void ActivateWarDeclarator(BaseButton.ButtonEventArgs obj)
-    {
-        var message = Rope.Collapse(MessageEdit.TextRope);
-        OnActivated?.Invoke(message);
-    }
 }
index ea27450956cd2ab5d69ca76ac18d4f41140a4cdf..6a4cd23ba12b4e6ee54b57ee7e4db94328418059 100644 (file)
@@ -11,6 +11,7 @@ using Content.Server.Station.Systems;
 using Content.Shared.Access.Components;
 using Content.Shared.Access.Systems;
 using Content.Shared.CCVar;
+using Content.Shared.Chat;
 using Content.Shared.Communications;
 using Content.Shared.Database;
 using Content.Shared.Emag.Components;
@@ -35,8 +36,6 @@ namespace Content.Server.Communications
         [Dependency] private readonly IConfigurationManager _cfg = default!;
         [Dependency] private readonly IAdminLogManager _adminLogger = default!;
 
-        private const int MaxMessageLength = 256;
-        private const int MaxMessageNewlines = 2;
         private const float UIUpdateInterval = 5.0f;
 
         public override void Initialize()
@@ -231,22 +230,8 @@ namespace Content.Server.Communications
         private void OnAnnounceMessage(EntityUid uid, CommunicationsConsoleComponent comp,
             CommunicationsConsoleAnnounceMessage message)
         {
-            var msgWords = message.Message.Trim();
-            var msgChars = (msgWords.Length <= MaxMessageLength ? msgWords : $"{msgWords[0..MaxMessageLength]}...").ToCharArray();
-
-            var newlines = 0;
-            for (var i = 0; i < msgChars.Length; i++)
-            {
-                if (msgChars[i] != '\n')
-                    continue;
-
-                if (newlines >= MaxMessageNewlines)
-                    msgChars[i] = ' ';
-
-                newlines++;
-            }
-
-            var msg = new string(msgChars);
+            var maxLength = _cfg.GetCVar(CCVars.ChatMaxAnnouncementLength);
+            var msg = SharedChatSystem.SanitizeAnnouncement(message.Message, maxLength);
             var author = Loc.GetString("comms-console-announcement-unknown-sender");
             if (message.Session.AttachedEntity is { Valid: true } mob)
             {
index 15279ee13cabcbfdf053923dddda2990d474d55b..ef6a3db5af63b790e217e4206a81499c6b51f3aa 100644 (file)
@@ -22,10 +22,6 @@ public sealed partial class WarDeclaratorComponent : Component
     [DataField]
     public bool AllowEditingMessage = true;
 
-    [ViewVariables(VVAccess.ReadWrite)]
-    [DataField]
-    public int MaxMessageLength = 512;
-
     /// <summary>
     /// War declarement text color
     /// </summary>
index dcf6c28d4345a7b7fa3c5d605cc4d55f854aefcc..328990738e7da0bb9b76ca042553d36b2709537f 100644 (file)
@@ -3,9 +3,12 @@ using Content.Server.GameTicking.Rules;
 using Content.Server.GameTicking.Rules.Components;
 using Content.Server.Popups;
 using Content.Server.UserInterface;
+using Content.Shared.CCVar;
+using Content.Shared.Chat;
 using Content.Shared.Database;
 using Content.Shared.NukeOps;
 using Robust.Server.GameObjects;
+using Robust.Shared.Configuration;
 
 namespace Content.Server.NukeOps;
 
@@ -18,6 +21,7 @@ public sealed class WarDeclaratorSystem : EntitySystem
     [Dependency] private readonly IAdminLogManager _adminLogger = default!;
     [Dependency] private readonly NukeopsRuleSystem _nukeopsRuleSystem = default!;
     [Dependency] private readonly PopupSystem _popupSystem = default!;
+    [Dependency] private readonly IConfigurationManager _cfg = default!;
 
     public override void Initialize()
     {
@@ -52,22 +56,8 @@ public sealed class WarDeclaratorSystem : EntitySystem
             return;
         }
 
-        var text = (args.Message.Length <= component.MaxMessageLength ? args.Message.Trim() : $"{args.Message.Trim().Substring(0, 256)}...").ToCharArray();
-
-        // No more than 2 newlines, other replaced to spaces
-        var newlines = 0;
-        for (var i = 0; i < text.Length; i++)
-        {
-            if (text[i] != '\n')
-                continue;
-
-            if (newlines >= 2)
-                text[i] = ' ';
-
-            newlines++;
-        }
-
-        string message = new string(text);
+        var maxLength = _cfg.GetCVar(CCVars.ChatMaxAnnouncementLength);
+        var message = SharedChatSystem.SanitizeAnnouncement(args.Message, maxLength);
         if (component.AllowEditingMessage && message != string.Empty)
         {
             component.Message = message;
index a3b875feef0d5f83e3316298f9fcda7020efa957..f0b6c2f9232ed7ebabeb47ca69f2e91a1534add1 100644 (file)
@@ -1598,6 +1598,9 @@ namespace Content.Shared.CCVar
         public static readonly CVarDef<int> ChatMaxMessageLength =
             CVarDef.Create("chat.max_message_length", 1000, CVar.SERVER | CVar.REPLICATED);
 
+        public static readonly CVarDef<int> ChatMaxAnnouncementLength =
+            CVarDef.Create("chat.max_announcement_length", 256, CVar.SERVER | CVar.REPLICATED);
+
         public static readonly CVarDef<bool> ChatSanitizerEnabled =
             CVarDef.Create("chat.chat_sanitizer_enabled", true, CVar.SERVERONLY);
 
index 69918f8098b4e9da007c280cae10a65ac1282ecd..931bfb37fcbc4a06c4552fcf6ebd17b1134484c5 100644 (file)
@@ -184,4 +184,34 @@ public abstract class SharedChatSystem : EntitySystem
 
         return message;
     }
+
+    public static string SanitizeAnnouncement(string message, int maxLength = 0, int maxNewlines = 2)
+    {
+        var trimmed = message.Trim();
+        if (maxLength > 0 && trimmed.Length > maxLength)
+        {
+            trimmed = $"{message[..maxLength]}...";
+        }
+
+        // No more than max newlines, other replaced to spaces
+        if (maxNewlines > 0)
+        {
+            var chars = trimmed.ToCharArray();
+            var newlines = 0;
+            for (var i = 0; i < chars.Length; i++)
+            {
+                if (chars[i] != '\n')
+                    continue;
+
+                if (newlines >= maxNewlines)
+                    chars[i] = ' ';
+
+                newlines++;
+            }
+
+            return new string(chars);
+        }
+
+        return trimmed;
+    }
 }