From c6c622b191ecbefdcb1ebcad2caf3bf632d49617 Mon Sep 17 00:00:00 2001 From: Dliix66 Date: Mon, 3 Feb 2025 01:06:34 +0100 Subject: [PATCH 01/12] WIP InlinePageOptions --- .../configs/core.example.json | 1 + .../CounterStrikeSharp.API/Core/CoreConfig.cs | 4 ++ .../Modules/Menu/CenterHtmlMenu.cs | 45 +++++++++++++++---- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/configs/addons/counterstrikesharp/configs/core.example.json b/configs/addons/counterstrikesharp/configs/core.example.json index 97dc9bfc3..3d037ac75 100644 --- a/configs/addons/counterstrikesharp/configs/core.example.json +++ b/configs/addons/counterstrikesharp/configs/core.example.json @@ -5,6 +5,7 @@ "PluginHotReloadEnabled": true, "PluginAutoLoadEnabled": true, "ServerLanguage": "en", + "InlinePageOptions": true, "UnlockConCommands": true, "UnlockConVars": true } \ No newline at end of file diff --git a/managed/CounterStrikeSharp.API/Core/CoreConfig.cs b/managed/CounterStrikeSharp.API/Core/CoreConfig.cs index 7771456bf..a730a12e3 100644 --- a/managed/CounterStrikeSharp.API/Core/CoreConfig.cs +++ b/managed/CounterStrikeSharp.API/Core/CoreConfig.cs @@ -55,6 +55,9 @@ internal sealed partial class CoreConfigData [JsonPropertyName("ServerLanguage")] public string ServerLanguage { get; set; } = "en"; + + [JsonPropertyName("InlinePageOptions")] + public bool InlinePageOptions { get; set; } = true; [JsonPropertyName("UnlockConCommands")] public bool UnlockConCommands { get; set; } = true; @@ -110,6 +113,7 @@ public partial class CoreConfig public static bool PluginAutoLoadEnabled => _coreConfig.PluginAutoLoadEnabled; public static string ServerLanguage => _coreConfig.ServerLanguage; + public static bool InlinePageOptions => _coreConfig.InlinePageOptions; public static bool UnlockConCommands => _coreConfig.UnlockConCommands; diff --git a/managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs b/managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs index fcf2390f9..4f6aec2e9 100644 --- a/managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs +++ b/managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs @@ -63,7 +63,33 @@ public class CenterHtmlMenuInstance : BaseMenuInstance { private readonly BasePlugin _plugin; public override int NumPerPage => 5; // one less than the actual number of items per page to avoid truncated options - protected override int MenuItemsPerPage => (Menu.ExitButton ? 0 : 1) + ((HasPrevButton && HasNextButton) ? NumPerPage - 1 : NumPerPage); + public bool InlinePageOptions => CoreConfig.InlinePageOptions; + protected override int MenuItemsPerPage + { + get + { + int count = NumPerPage; + if (InlinePageOptions == false) + { + if (HasExitButton == false) + count++; + + if (HasPrevButton == false) + count++; + + if (HasNextButton == false) + count++; + } + else + { + count += 2; + if (HasExitButton || HasPrevButton || HasNextButton) + count++; + } + + return count; + } + } public CenterHtmlMenuInstance(BasePlugin plugin, CCSPlayerController player, IMenu menu) : base(player, menu) { @@ -98,23 +124,26 @@ public override void Display() builder.Append($"!{keyOffset++} {option.Text}"); builder.AppendLine("
"); } - + if (HasPrevButton) { - builder.AppendFormat($"!7 <- Prev"); - builder.AppendLine("
"); + builder.AppendFormat($"!7 < Prev"); + if (InlinePageOptions == false) + builder.AppendLine("
"); } if (HasNextButton) { - builder.AppendFormat($"!8 -> Next"); - builder.AppendLine("
"); + builder.AppendFormat($"!8 > Next"); + if (InlinePageOptions == false) + builder.AppendLine("
"); } if (centerHtmlMenu.ExitButton) { - builder.AppendFormat($"!9 -> Close"); - builder.AppendLine("
"); + builder.AppendFormat($"!9 X Close"); + if (InlinePageOptions == false) + builder.AppendLine("
"); } var currentPageText = builder.ToString(); From 3a77d1ffa37c0265fda366f4022dd6c15e080c28 Mon Sep 17 00:00:00 2001 From: Joakim <21Joakim@users.noreply.github.com> Date: Mon, 27 Jan 2025 23:10:14 +0100 Subject: [PATCH 02/12] feat: add ReplicateConVar (#751) --- managed/CounterStrikeSharp.API/Core/API.cs | 12 ++++++++++ .../Core/Model/CCSPlayerController.cs | 5 ++++ src/scripting/natives/natives_commands.cpp | 24 +++++++++++++++++++ src/scripting/natives/natives_commands.yaml | 3 ++- 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/managed/CounterStrikeSharp.API/Core/API.cs b/managed/CounterStrikeSharp.API/Core/API.cs index 12fe56a27..94c794624 100644 --- a/managed/CounterStrikeSharp.API/Core/API.cs +++ b/managed/CounterStrikeSharp.API/Core/API.cs @@ -206,6 +206,18 @@ public static void SetFakeClientConvarValue(int clientindex, string convarname, } } + public static void ReplicateConvar(int clientslot, string convarname, string convarvalue){ + lock (ScriptContext.GlobalScriptContext.Lock) { + ScriptContext.GlobalScriptContext.Reset(); + ScriptContext.GlobalScriptContext.Push(clientslot); + ScriptContext.GlobalScriptContext.Push(convarname); + ScriptContext.GlobalScriptContext.Push(convarvalue); + ScriptContext.GlobalScriptContext.SetIdentifier(0xC8728BEC); + ScriptContext.GlobalScriptContext.Invoke(); + ScriptContext.GlobalScriptContext.CheckErrors(); + } + } + public static T DynamicHookGetReturn(IntPtr hook, int datatype){ lock (ScriptContext.GlobalScriptContext.Lock) { ScriptContext.GlobalScriptContext.Reset(); diff --git a/managed/CounterStrikeSharp.API/Core/Model/CCSPlayerController.cs b/managed/CounterStrikeSharp.API/Core/Model/CCSPlayerController.cs index 989c5bb44..d79747856 100644 --- a/managed/CounterStrikeSharp.API/Core/Model/CCSPlayerController.cs +++ b/managed/CounterStrikeSharp.API/Core/Model/CCSPlayerController.cs @@ -347,4 +347,9 @@ public VoiceFlags VoiceFlags { base.Teleport(position, angles, velocity); } + + public void ReplicateConVar(string conVar, string value) + { + NativeAPI.ReplicateConvar(Slot, conVar, value); + } } diff --git a/src/scripting/natives/natives_commands.cpp b/src/scripting/natives/natives_commands.cpp index 141b3e23a..f1e8b4a13 100644 --- a/src/scripting/natives/natives_commands.cpp +++ b/src/scripting/natives/natives_commands.cpp @@ -15,13 +15,17 @@ */ #include +#include #include "scripting/autonative.h" #include "scripting/callback_manager.h" #include "core/managers/con_command_manager.h" #include "core/managers/player_manager.h" +#include "core/recipientfilters.h" +#include "igameeventsystem.h" #include "scripting/script_engine.h" #include "core/log.h" +#include namespace counterstrikesharp { @@ -191,6 +195,25 @@ void SetConVarStringValue(ScriptContext& script_context) pCvar->values = reinterpret_cast((char*)value); } +void ReplicateConVar(ScriptContext& script_context) +{ + auto slot = script_context.GetArgument(0); + auto name = script_context.GetArgument(1); + auto value = script_context.GetArgument(2); + + INetworkMessageInternal* pNetMsg = globals::networkMessages->FindNetworkMessagePartial("SetConVar"); + auto msg = pNetMsg->AllocateMessage()->ToPB(); + + CMsg_CVars_CVar* cvarMsg = msg->mutable_convars()->add_cvars(); + cvarMsg->set_name(name); + cvarMsg->set_value(value); + + CSingleRecipientFilter filter(slot); + globals::gameEventSystem->PostEventAbstract(-1, false, &filter, pNetMsg, msg, 0); + + delete msg; +} + REGISTER_NATIVES(commands, { ScriptEngine::RegisterNativeHandler("ADD_COMMAND", AddCommand); ScriptEngine::RegisterNativeHandler("REMOVE_COMMAND", RemoveCommand); @@ -211,5 +234,6 @@ REGISTER_NATIVES(commands, { IssueClientCommandFromServer); ScriptEngine::RegisterNativeHandler("GET_CLIENT_CONVAR_VALUE", GetClientConVarValue); ScriptEngine::RegisterNativeHandler("SET_FAKE_CLIENT_CONVAR_VALUE", SetFakeClientConVarValue); + ScriptEngine::RegisterNativeHandler("REPLICATE_CONVAR", ReplicateConVar); }) } // namespace counterstrikesharp diff --git a/src/scripting/natives/natives_commands.yaml b/src/scripting/natives/natives_commands.yaml index 3504386ae..b59871a32 100644 --- a/src/scripting/natives/natives_commands.yaml +++ b/src/scripting/natives/natives_commands.yaml @@ -12,4 +12,5 @@ ISSUE_CLIENT_COMMAND_FROM_SERVER: slot:int,command:string -> void FIND_CONVAR: name:string -> pointer SET_CONVAR_STRING_VALUE: convar:pointer,value:string -> void GET_CLIENT_CONVAR_VALUE: clientIndex:int,convarName:string -> string -SET_FAKE_CLIENT_CONVAR_VALUE: clientIndex:int,convarName:string,convarValue:string -> void \ No newline at end of file +SET_FAKE_CLIENT_CONVAR_VALUE: clientIndex:int,convarName:string,convarValue:string -> void +REPLICATE_CONVAR: clientSlot:int,convarName:string,convarValue:string -> void From 1561ff49227396d1a0c6992e73368ac7d23be81e Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 30 Jan 2025 11:22:36 +1000 Subject: [PATCH 03/12] [no ci] chore(deps): bump libraries/Protobufs from `b46090a` to `157162d` (#753) Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- libraries/Protobufs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Protobufs b/libraries/Protobufs index b46090af9..157162d97 160000 --- a/libraries/Protobufs +++ b/libraries/Protobufs @@ -1 +1 @@ -Subproject commit b46090af9cc219763c20eda3cc906675bca4157a +Subproject commit 157162d97b40f0757ab2e44c7afe8dbc272579b5 From 25dcf99b243929552d094b2cbbab432430d6f93c Mon Sep 17 00:00:00 2001 From: qstage <166386817+qstage@users.noreply.github.com> Date: Thu, 30 Jan 2025 14:08:44 +0300 Subject: [PATCH 04/12] fix: errors when reloading auto generated config (#754) --- .../Modules/Extensions/PluginConfigExtensions.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/managed/CounterStrikeSharp.API/Modules/Extensions/PluginConfigExtensions.cs b/managed/CounterStrikeSharp.API/Modules/Extensions/PluginConfigExtensions.cs index cdd666912..c2c3662a9 100644 --- a/managed/CounterStrikeSharp.API/Modules/Extensions/PluginConfigExtensions.cs +++ b/managed/CounterStrikeSharp.API/Modules/Extensions/PluginConfigExtensions.cs @@ -7,7 +7,8 @@ public static class PluginConfigExtensions { private static readonly JsonSerializerOptions _jsonSerializerOptions = new() { - WriteIndented = true + WriteIndented = true, + ReadCommentHandling = JsonCommentHandling.Skip }; public static JsonSerializerOptions JsonSerializerOptions => _jsonSerializerOptions; @@ -62,7 +63,7 @@ public static class PluginConfigExtensions var configContent = File.ReadAllText(configPath); - var newConfig = JsonSerializer.Deserialize(configContent) + var newConfig = JsonSerializer.Deserialize(configContent, JsonSerializerOptions) ?? throw new JsonException($"Deserialization failed for configuration file '{configPath}'."); foreach (var property in typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public)) From 47cc7a659da0e78d9eec64ee69dfcf7816f92616 Mon Sep 17 00:00:00 2001 From: Dliix66 Date: Thu, 6 Feb 2025 22:33:38 +0100 Subject: [PATCH 05/12] Added MaxHtmlMenuTitleLength and MaxHtmlMenuOptionLength in config --- .../configs/core.example.json | 4 +- .../CounterStrikeSharp.API/Core/CoreConfig.cs | 11 ++- .../Modules/Menu/CenterHtmlMenu.cs | 6 +- .../StringExtensions.cs | 80 +++++++++++++++++++ 4 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 managed/CounterStrikeSharp.API/StringExtensions.cs diff --git a/configs/addons/counterstrikesharp/configs/core.example.json b/configs/addons/counterstrikesharp/configs/core.example.json index 3d037ac75..bd20c4a41 100644 --- a/configs/addons/counterstrikesharp/configs/core.example.json +++ b/configs/addons/counterstrikesharp/configs/core.example.json @@ -7,5 +7,7 @@ "ServerLanguage": "en", "InlinePageOptions": true, "UnlockConCommands": true, - "UnlockConVars": true + "UnlockConVars": true, + "MaxHtmlMenuTitleLength": 0, // 0 = unlimited, previous value = 32 + "MaxHtmlMenuOptionLength": 0, // 0 = unlimited, previous value = 26 } \ No newline at end of file diff --git a/managed/CounterStrikeSharp.API/Core/CoreConfig.cs b/managed/CounterStrikeSharp.API/Core/CoreConfig.cs index a730a12e3..a2c9b73a4 100644 --- a/managed/CounterStrikeSharp.API/Core/CoreConfig.cs +++ b/managed/CounterStrikeSharp.API/Core/CoreConfig.cs @@ -64,6 +64,12 @@ internal sealed partial class CoreConfigData [JsonPropertyName("UnlockConVars")] public bool UnlockConVars { get; set; } = true; + + [JsonPropertyName("MaxHtmlMenuTitleLength")] + public int MaxHtmlMenuTitleLength { get; set; } = 0; + + [JsonPropertyName("MaxHtmlMenuOptionLength")] + public int MaxHtmlMenuOptionLength { get; set; } = 0; } /// @@ -118,7 +124,10 @@ public partial class CoreConfig public static bool UnlockConCommands => _coreConfig.UnlockConCommands; public static bool UnlockConVars => _coreConfig.UnlockConVars; - + + public static int MaxHtmlMenuTitleLength => _coreConfig.MaxHtmlMenuTitleLength; + + public static int MaxHtmlMenuOptionLength => _coreConfig.MaxHtmlMenuOptionLength; } public partial class CoreConfig : IStartupService diff --git a/managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs b/managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs index 4f6aec2e9..6f2eeac85 100644 --- a/managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs +++ b/managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs @@ -29,13 +29,13 @@ public class CenterHtmlMenu : BaseMenu public string NextPageColor { get; set; } = "yellow"; public string CloseColor { get; set; } = "red"; - public CenterHtmlMenu(string title, BasePlugin plugin) : base(title) + public CenterHtmlMenu(string title, BasePlugin plugin) : base(title.TruncateHtmlTitle()) { _plugin = plugin; } [Obsolete("Use the constructor that takes a BasePlugin")] - public CenterHtmlMenu(string title) : base(title) + public CenterHtmlMenu(string title) : base(title.TruncateHtmlTitle()) { } @@ -53,7 +53,7 @@ public override void Open(CCSPlayerController player) public override ChatMenuOption AddMenuOption(string display, Action onSelect, bool disabled = false) { - var option = new ChatMenuOption(display, disabled, onSelect); + var option = new ChatMenuOption(display.TruncateHtmlOption(), disabled, onSelect); MenuOptions.Add(option); return option; } diff --git a/managed/CounterStrikeSharp.API/StringExtensions.cs b/managed/CounterStrikeSharp.API/StringExtensions.cs new file mode 100644 index 000000000..2f7285507 --- /dev/null +++ b/managed/CounterStrikeSharp.API/StringExtensions.cs @@ -0,0 +1,80 @@ +using System.Text; +using System.Text.RegularExpressions; + +namespace CounterStrikeSharp.API +{ + public static class StringExtensions + { + private const string HTML_TAG_REGEX_PATTERN = "<[^>]+>"; + private static readonly Regex TagRegex = new(HTML_TAG_REGEX_PATTERN, RegexOptions.Compiled); + + public static string TruncateHtmlTitle(this string msg) + { + int titleMaxLength = CoreConfig.MaxHtmlMenuTitleLength; + return TruncateHtml(msg, titleMaxLength); + } + + public static string TruncateHtmlOption(this string msg) + { + int titleMaxLength = CoreConfig.MaxHtmlMenuOptionLength; + return TruncateHtml(msg, titleMaxLength); + } + + public static string TruncateHtml(this string msg, int maxLength) + { + if (maxLength <= 0) + return msg; + + if (string.IsNullOrEmpty(msg)) + return string.Empty; + + string textOnly = Regex.Replace(msg, HTML_TAG_REGEX_PATTERN, ""); + if (textOnly.Length <= maxLength) + return msg; + + Stack tagStack = new Stack(); + StringBuilder result = new System.Text.StringBuilder(); + int visibleLength = 0, + i = 0; + + while (i < msg.Length && visibleLength < maxLength) + { + if (msg[i] == '<') + { + Match match = TagRegex.Match(msg, i); + if (match.Success && match.Index == i) + { + string tag = match.Value; + result.Append(tag); + i += tag.Length; + + if (!tag.StartsWith("' }, StringSplitOptions.RemoveEmptyEntries)[0].Trim('<'); + if (!tag.EndsWith("/>") && !tagName.StartsWith("!")) + tagStack.Push(tagName); + } + else if (tagStack.Count > 0) + { + tagStack.Pop(); + } + + continue; + } + } + else + { + result.Append(msg[i]); + visibleLength++; + } + + i++; + } + + while (tagStack.Count > 0) + result.Append($""); + + return result.ToString(); + } + } +} From 41b4caacdb1bfc05e4906a98c6c1be6d0c99b133 Mon Sep 17 00:00:00 2001 From: Dliix66 Date: Thu, 6 Feb 2025 22:33:57 +0100 Subject: [PATCH 06/12] Removed comments --- configs/addons/counterstrikesharp/configs/core.example.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/configs/addons/counterstrikesharp/configs/core.example.json b/configs/addons/counterstrikesharp/configs/core.example.json index bd20c4a41..7d9de3516 100644 --- a/configs/addons/counterstrikesharp/configs/core.example.json +++ b/configs/addons/counterstrikesharp/configs/core.example.json @@ -8,6 +8,6 @@ "InlinePageOptions": true, "UnlockConCommands": true, "UnlockConVars": true, - "MaxHtmlMenuTitleLength": 0, // 0 = unlimited, previous value = 32 - "MaxHtmlMenuOptionLength": 0, // 0 = unlimited, previous value = 26 + "MaxHtmlMenuTitleLength": 0, + "MaxHtmlMenuOptionLength": 0 } \ No newline at end of file From 10359b8d299c70e4cc54089ec917d2d5192cc5cc Mon Sep 17 00:00:00 2001 From: Dliix66 Date: Mon, 3 Feb 2025 01:06:34 +0100 Subject: [PATCH 07/12] WIP InlinePageOptions From 0f000cf727e6921fe6afc49017c6a3692ac99bb0 Mon Sep 17 00:00:00 2001 From: Dliix66 Date: Thu, 6 Feb 2025 22:36:25 +0100 Subject: [PATCH 08/12] Working inline options --- .../Modules/Menu/CenterHtmlMenu.cs | 86 +++++++++++++++---- 1 file changed, 67 insertions(+), 19 deletions(-) diff --git a/managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs b/managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs index 6f2eeac85..a4b05bda5 100644 --- a/managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs +++ b/managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs @@ -71,19 +71,16 @@ protected override int MenuItemsPerPage int count = NumPerPage; if (InlinePageOptions == false) { - if (HasExitButton == false) + if (!HasPrevButton) count++; - if (HasPrevButton == false) - count++; - - if (HasNextButton == false) + if (!HasNextButton) count++; } else { - count += 2; - if (HasExitButton || HasPrevButton || HasNextButton) + count++; + if (!HasExitButton && !HasPrevButton && !HasNextButton) count++; } @@ -125,30 +122,81 @@ public override void Display() builder.AppendLine("
"); } + AddPageOptions(centerHtmlMenu, builder); + + var currentPageText = builder.ToString(); + Player.PrintToCenterHtml(currentPageText); + } + + private void AddPageOptions(CenterHtmlMenu centerHtmlMenu, StringBuilder builder) + { + string prevText = $"!7 < Prev"; + string closeText = $"!9 X Close"; + string nextText = $"!8 > Next"; + + if (InlinePageOptions) + AddInlinePageOptions(prevText, closeText, nextText, centerHtmlMenu.ExitButton, builder); + else + AddMultilinePageOptions(prevText, closeText, nextText, centerHtmlMenu.ExitButton, builder); + } + + + private void AddInlinePageOptions(string prevText, string closeText, string nextText, bool hasExitButton, StringBuilder builder) + { + if (HasPrevButton && HasExitButton && HasNextButton) + { + builder.Append($"{prevText} | {closeText} | {nextText}"); + return; + } + + string doubleOptionSplitString = " \u200e \u200e \u200e \u200e | \u200e \u200e \u200e \u200e "; // empty characters that are not trimmed + + int optionsCount = 0; if (HasPrevButton) { - builder.AppendFormat($"!7 < Prev"); - if (InlinePageOptions == false) - builder.AppendLine("
"); + builder.AppendFormat(prevText); + optionsCount++; + } + + if (hasExitButton) + { + if (optionsCount++ > 0) + builder.Append(doubleOptionSplitString); + + builder.AppendFormat(closeText); } if (HasNextButton) { - builder.AppendFormat($"!8 > Next"); - if (InlinePageOptions == false) - builder.AppendLine("
"); + if (optionsCount > 0) + builder.Append(doubleOptionSplitString); + + builder.AppendFormat(nextText); } + } - if (centerHtmlMenu.ExitButton) + private void AddMultilinePageOptions(string prevText, string closeText, string nextText, bool hasExitButton, StringBuilder builder) + { + if (HasPrevButton) { - builder.AppendFormat($"!9 X Close"); - if (InlinePageOptions == false) - builder.AppendLine("
"); + builder.AppendFormat(prevText); + builder.AppendLine("
"); } - var currentPageText = builder.ToString(); - Player.PrintToCenterHtml(currentPageText); + if (hasExitButton) + { + builder.AppendFormat(closeText); + builder.AppendLine("
"); + } + + if (HasNextButton) + { + builder.AppendFormat(nextText); + builder.AppendLine("
"); + } } + + public override void Close() { From e7dd123c215865844487f74429a982e76ce11065 Mon Sep 17 00:00:00 2001 From: Dliix66 Date: Thu, 6 Feb 2025 22:34:35 +0100 Subject: [PATCH 09/12] Removed options from the core config and added them to the CenterHtmlMenu --- .../configs/core.example.json | 5 +---- .../CounterStrikeSharp.API/Core/CoreConfig.cs | 14 ------------- .../Modules/Menu/CenterHtmlMenu.cs | 20 +++++++++++++++---- .../StringExtensions.cs | 14 +------------ 4 files changed, 18 insertions(+), 35 deletions(-) diff --git a/configs/addons/counterstrikesharp/configs/core.example.json b/configs/addons/counterstrikesharp/configs/core.example.json index 7d9de3516..97dc9bfc3 100644 --- a/configs/addons/counterstrikesharp/configs/core.example.json +++ b/configs/addons/counterstrikesharp/configs/core.example.json @@ -5,9 +5,6 @@ "PluginHotReloadEnabled": true, "PluginAutoLoadEnabled": true, "ServerLanguage": "en", - "InlinePageOptions": true, "UnlockConCommands": true, - "UnlockConVars": true, - "MaxHtmlMenuTitleLength": 0, - "MaxHtmlMenuOptionLength": 0 + "UnlockConVars": true } \ No newline at end of file diff --git a/managed/CounterStrikeSharp.API/Core/CoreConfig.cs b/managed/CounterStrikeSharp.API/Core/CoreConfig.cs index a2c9b73a4..1bca56675 100644 --- a/managed/CounterStrikeSharp.API/Core/CoreConfig.cs +++ b/managed/CounterStrikeSharp.API/Core/CoreConfig.cs @@ -55,21 +55,12 @@ internal sealed partial class CoreConfigData [JsonPropertyName("ServerLanguage")] public string ServerLanguage { get; set; } = "en"; - - [JsonPropertyName("InlinePageOptions")] - public bool InlinePageOptions { get; set; } = true; [JsonPropertyName("UnlockConCommands")] public bool UnlockConCommands { get; set; } = true; [JsonPropertyName("UnlockConVars")] public bool UnlockConVars { get; set; } = true; - - [JsonPropertyName("MaxHtmlMenuTitleLength")] - public int MaxHtmlMenuTitleLength { get; set; } = 0; - - [JsonPropertyName("MaxHtmlMenuOptionLength")] - public int MaxHtmlMenuOptionLength { get; set; } = 0; } /// @@ -119,15 +110,10 @@ public partial class CoreConfig public static bool PluginAutoLoadEnabled => _coreConfig.PluginAutoLoadEnabled; public static string ServerLanguage => _coreConfig.ServerLanguage; - public static bool InlinePageOptions => _coreConfig.InlinePageOptions; public static bool UnlockConCommands => _coreConfig.UnlockConCommands; public static bool UnlockConVars => _coreConfig.UnlockConVars; - - public static int MaxHtmlMenuTitleLength => _coreConfig.MaxHtmlMenuTitleLength; - - public static int MaxHtmlMenuOptionLength => _coreConfig.MaxHtmlMenuOptionLength; } public partial class CoreConfig : IStartupService diff --git a/managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs b/managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs index a4b05bda5..a9b1fb1ed 100644 --- a/managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs +++ b/managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs @@ -28,15 +28,24 @@ public class CenterHtmlMenu : BaseMenu public string PrevPageColor { get; set; } = "yellow"; public string NextPageColor { get; set; } = "yellow"; public string CloseColor { get; set; } = "red"; + + public bool InlinePageOptions { get; set; } = true; + public int MaxTitleLength { get; set; } = 0; // defaults to 0 = no limit, if enabled, recommended value is 32 + public int MaxOptionLength { get; set; }= 0; // defaults to 0 = no limit, if enabled, recommended value is 26 - public CenterHtmlMenu(string title, BasePlugin plugin) : base(title.TruncateHtmlTitle()) + public CenterHtmlMenu(string title, BasePlugin plugin, bool inlinePageOptions, int maxTitleLength, int maxOptionLength): base(title) { + Title = title.TruncateHtml(MaxTitleLength); _plugin = plugin; + InlinePageOptions = inlinePageOptions; + MaxTitleLength = maxTitleLength; + MaxOptionLength = maxOptionLength; } [Obsolete("Use the constructor that takes a BasePlugin")] - public CenterHtmlMenu(string title) : base(title.TruncateHtmlTitle()) + public CenterHtmlMenu(string title) : base(title) { + Title = title.TruncateHtml(MaxTitleLength); } public override void Open(CCSPlayerController player) @@ -53,7 +62,7 @@ public override void Open(CCSPlayerController player) public override ChatMenuOption AddMenuOption(string display, Action onSelect, bool disabled = false) { - var option = new ChatMenuOption(display.TruncateHtmlOption(), disabled, onSelect); + var option = new ChatMenuOption(display.TruncateHtml(MaxOptionLength), disabled, onSelect); MenuOptions.Add(option); return option; } @@ -63,7 +72,7 @@ public class CenterHtmlMenuInstance : BaseMenuInstance { private readonly BasePlugin _plugin; public override int NumPerPage => 5; // one less than the actual number of items per page to avoid truncated options - public bool InlinePageOptions => CoreConfig.InlinePageOptions; + public bool InlinePageOptions { get; set; } = true; protected override int MenuItemsPerPage { get @@ -93,6 +102,9 @@ public CenterHtmlMenuInstance(BasePlugin plugin, CCSPlayerController player, IMe _plugin = plugin; RemoveOnTickListener(); plugin.RegisterListener(Display); + + if (menu is CenterHtmlMenu centerHtmlMenu) + InlinePageOptions = centerHtmlMenu.InlinePageOptions; } public override void Display() diff --git a/managed/CounterStrikeSharp.API/StringExtensions.cs b/managed/CounterStrikeSharp.API/StringExtensions.cs index 2f7285507..cfd91003c 100644 --- a/managed/CounterStrikeSharp.API/StringExtensions.cs +++ b/managed/CounterStrikeSharp.API/StringExtensions.cs @@ -7,19 +7,7 @@ public static class StringExtensions { private const string HTML_TAG_REGEX_PATTERN = "<[^>]+>"; private static readonly Regex TagRegex = new(HTML_TAG_REGEX_PATTERN, RegexOptions.Compiled); - - public static string TruncateHtmlTitle(this string msg) - { - int titleMaxLength = CoreConfig.MaxHtmlMenuTitleLength; - return TruncateHtml(msg, titleMaxLength); - } - - public static string TruncateHtmlOption(this string msg) - { - int titleMaxLength = CoreConfig.MaxHtmlMenuOptionLength; - return TruncateHtml(msg, titleMaxLength); - } - + public static string TruncateHtml(this string msg, int maxLength) { if (maxLength <= 0) From 7f21590a9d615f2bc800fb3c1bc293c7014ea0e5 Mon Sep 17 00:00:00 2001 From: Dliix66 Date: Thu, 6 Feb 2025 22:34:39 +0100 Subject: [PATCH 10/12] Default params in contructor --- managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs | 4 ++-- managed/CounterStrikeSharp.API/Modules/Menu/MenuManager.cs | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs b/managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs index a9b1fb1ed..ece21558b 100644 --- a/managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs +++ b/managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs @@ -31,9 +31,9 @@ public class CenterHtmlMenu : BaseMenu public bool InlinePageOptions { get; set; } = true; public int MaxTitleLength { get; set; } = 0; // defaults to 0 = no limit, if enabled, recommended value is 32 - public int MaxOptionLength { get; set; }= 0; // defaults to 0 = no limit, if enabled, recommended value is 26 + public int MaxOptionLength { get; set; } = 0; // defaults to 0 = no limit, if enabled, recommended value is 26 - public CenterHtmlMenu(string title, BasePlugin plugin, bool inlinePageOptions, int maxTitleLength, int maxOptionLength): base(title) + public CenterHtmlMenu(string title, BasePlugin plugin, bool inlinePageOptions = true, int maxTitleLength = 0, int maxOptionLength = 0): base(title) { Title = title.TruncateHtml(MaxTitleLength); _plugin = plugin; diff --git a/managed/CounterStrikeSharp.API/Modules/Menu/MenuManager.cs b/managed/CounterStrikeSharp.API/Modules/Menu/MenuManager.cs index 455413ca7..3a1f085a9 100644 --- a/managed/CounterStrikeSharp.API/Modules/Menu/MenuManager.cs +++ b/managed/CounterStrikeSharp.API/Modules/Menu/MenuManager.cs @@ -52,6 +52,7 @@ public static void OpenChatMenu(CCSPlayerController player, ChatMenu menu) public static void OpenCenterHtmlMenu(BasePlugin plugin, CCSPlayerController player, CenterHtmlMenu menu) { + CloseActiveMenu(player); ActiveMenus[player.Handle] = new CenterHtmlMenuInstance(plugin, player, menu); From d4dcda04071deba2ff07aa66f32259bf5cb99ce6 Mon Sep 17 00:00:00 2001 From: Dliix66 Date: Thu, 6 Feb 2025 22:34:43 +0100 Subject: [PATCH 11/12] Inverted next and close in multiline --- .../Modules/Menu/CenterHtmlMenu.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs b/managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs index ece21558b..98215c350 100644 --- a/managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs +++ b/managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs @@ -195,15 +195,15 @@ private void AddMultilinePageOptions(string prevText, string closeText, string n builder.AppendLine("
"); } - if (hasExitButton) + if (HasNextButton) { - builder.AppendFormat(closeText); + builder.AppendFormat(nextText); builder.AppendLine("
"); } - - if (HasNextButton) + + if (hasExitButton) { - builder.AppendFormat(nextText); + builder.AppendFormat(closeText); builder.AppendLine("
"); } } From 816f54e12457f08c664ff4d860384697a0c6bc60 Mon Sep 17 00:00:00 2001 From: Dliix66 Date: Thu, 6 Feb 2025 22:34:47 +0100 Subject: [PATCH 12/12] Fixed empty next page issue with HTML menus --- managed/CounterStrikeSharp.API/Modules/Menu/BaseMenu.cs | 2 +- managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs | 1 + managed/CounterStrikeSharp.API/Modules/Menu/MenuManager.cs | 1 - 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/managed/CounterStrikeSharp.API/Modules/Menu/BaseMenu.cs b/managed/CounterStrikeSharp.API/Modules/Menu/BaseMenu.cs index fcadaa97a..ef4b4aa16 100644 --- a/managed/CounterStrikeSharp.API/Modules/Menu/BaseMenu.cs +++ b/managed/CounterStrikeSharp.API/Modules/Menu/BaseMenu.cs @@ -89,7 +89,7 @@ protected BaseMenuInstance(CCSPlayerController player, IMenu menu) } protected bool HasPrevButton => Page > 0; - protected bool HasNextButton => Menu.MenuOptions.Count > NumPerPage && CurrentOffset + NumPerPage < Menu.MenuOptions.Count; + protected virtual bool HasNextButton => Menu.MenuOptions.Count > NumPerPage && CurrentOffset + NumPerPage < Menu.MenuOptions.Count; protected bool HasExitButton => Menu.ExitButton; protected virtual int MenuItemsPerPage => NumPerPage; diff --git a/managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs b/managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs index 98215c350..22e1bde6c 100644 --- a/managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs +++ b/managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs @@ -72,6 +72,7 @@ public class CenterHtmlMenuInstance : BaseMenuInstance { private readonly BasePlugin _plugin; public override int NumPerPage => 5; // one less than the actual number of items per page to avoid truncated options + protected override bool HasNextButton => Menu.MenuOptions.Count > NumPerPage + 1 && CurrentOffset + NumPerPage < Menu.MenuOptions.Count; public bool InlinePageOptions { get; set; } = true; protected override int MenuItemsPerPage { diff --git a/managed/CounterStrikeSharp.API/Modules/Menu/MenuManager.cs b/managed/CounterStrikeSharp.API/Modules/Menu/MenuManager.cs index 3a1f085a9..455413ca7 100644 --- a/managed/CounterStrikeSharp.API/Modules/Menu/MenuManager.cs +++ b/managed/CounterStrikeSharp.API/Modules/Menu/MenuManager.cs @@ -52,7 +52,6 @@ public static void OpenChatMenu(CCSPlayerController player, ChatMenu menu) public static void OpenCenterHtmlMenu(BasePlugin plugin, CCSPlayerController player, CenterHtmlMenu menu) { - CloseActiveMenu(player); ActiveMenus[player.Handle] = new CenterHtmlMenuInstance(plugin, player, menu);