From ed5af082d9988de32c8214f2c721c2f49b71f1f3 Mon Sep 17 00:00:00 2001 From: schwarper <75811921+schwarper@users.noreply.github.com> Date: Thu, 7 Nov 2024 21:43:01 +0300 Subject: [PATCH 1/5] Added GetArgs --- .../Modules/Commands/CommandInfo.cs | 47 +++++++++++++++++-- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/managed/CounterStrikeSharp.API/Modules/Commands/CommandInfo.cs b/managed/CounterStrikeSharp.API/Modules/Commands/CommandInfo.cs index bfb621af5..47b2f8c72 100644 --- a/managed/CounterStrikeSharp.API/Modules/Commands/CommandInfo.cs +++ b/managed/CounterStrikeSharp.API/Modules/Commands/CommandInfo.cs @@ -15,11 +15,12 @@ */ using System; +using System.Text.RegularExpressions; using CounterStrikeSharp.API.Core; namespace CounterStrikeSharp.API.Modules.Commands { - public class CommandInfo + public partial class CommandInfo { public delegate void CommandCallback(CCSPlayerController? player, CommandInfo commandInfo); @@ -47,7 +48,47 @@ internal CommandInfo(IntPtr pointer, CCSPlayerController? player) public string ArgByIndex(int index) => NativeAPI.CommandGetArgByIndex(Handle, index); public string GetArg(int index) => NativeAPI.CommandGetArgByIndex(Handle, index); - + + private static readonly Regex RegexArgs = new(@"(?:""([^""]+)""|\S+)", RegexOptions.Compiled); + + public List GetArgs() + { + MatchCollection matches = RegexArgs.Matches(ArgString); + + var args = new List(); + + foreach (Match match in matches) + { + if (match.Groups[1].Success) + { + args.Add(match.Groups[1].Value); + } + else + { + args.Add(match.Value); + } + } + + return args; + } + + public List GetArgs(int startIndex, int endIndex = -1) + { + var args = GetArgs(); + + if (endIndex == -1) + { + endIndex = args.Count; + } + + if (startIndex < 0 || startIndex >= args.Count || endIndex < 0 || endIndex > args.Count || startIndex > endIndex) + { + throw new ArgumentOutOfRangeException(); + } + + return args.GetRange(startIndex, endIndex - startIndex); + } + /// /// Whether or not the command was sent via Console or Chat. /// @@ -87,4 +128,4 @@ public void ReplyToCommand(string message) } } } -} \ No newline at end of file +} From 9bb6bfa5981c34e84305d2e923b9cfc9a3671249 Mon Sep 17 00:00:00 2001 From: schwarper <75811921+schwarper@users.noreply.github.com> Date: Thu, 7 Nov 2024 21:44:31 +0300 Subject: [PATCH 2/5] Update CommandInfo.cs (no need partial) --- managed/CounterStrikeSharp.API/Modules/Commands/CommandInfo.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/managed/CounterStrikeSharp.API/Modules/Commands/CommandInfo.cs b/managed/CounterStrikeSharp.API/Modules/Commands/CommandInfo.cs index 47b2f8c72..36d4eef75 100644 --- a/managed/CounterStrikeSharp.API/Modules/Commands/CommandInfo.cs +++ b/managed/CounterStrikeSharp.API/Modules/Commands/CommandInfo.cs @@ -20,7 +20,7 @@ namespace CounterStrikeSharp.API.Modules.Commands { - public partial class CommandInfo + public class CommandInfo { public delegate void CommandCallback(CCSPlayerController? player, CommandInfo commandInfo); From 45bee48c39b73b6ee003f1dcba413679b5e55602 Mon Sep 17 00:00:00 2001 From: schwarper <75811921+schwarper@users.noreply.github.com> Date: Fri, 15 Nov 2024 00:08:08 +0300 Subject: [PATCH 3/5] GetArgs update --- .../Modules/Commands/CommandInfo.cs | 43 ++++--------------- 1 file changed, 8 insertions(+), 35 deletions(-) diff --git a/managed/CounterStrikeSharp.API/Modules/Commands/CommandInfo.cs b/managed/CounterStrikeSharp.API/Modules/Commands/CommandInfo.cs index 36d4eef75..68595597f 100644 --- a/managed/CounterStrikeSharp.API/Modules/Commands/CommandInfo.cs +++ b/managed/CounterStrikeSharp.API/Modules/Commands/CommandInfo.cs @@ -49,44 +49,17 @@ internal CommandInfo(IntPtr pointer, CCSPlayerController? player) public string ArgByIndex(int index) => NativeAPI.CommandGetArgByIndex(Handle, index); public string GetArg(int index) => NativeAPI.CommandGetArgByIndex(Handle, index); - private static readonly Regex RegexArgs = new(@"(?:""([^""]+)""|\S+)", RegexOptions.Compiled); - - public List GetArgs() - { - MatchCollection matches = RegexArgs.Matches(ArgString); - - var args = new List(); - - foreach (Match match in matches) - { - if (match.Groups[1].Success) - { - args.Add(match.Groups[1].Value); - } - else - { - args.Add(match.Value); - } - } - - return args; - } - - public List GetArgs(int startIndex, int endIndex = -1) + public IEnumerable GetArgs(int startIndex = 0, int endIndex = -1) { - var args = GetArgs(); + var args = ArgString.Split(' ', StringSplitOptions.RemoveEmptyEntries); + int lastIndex = args.Length - 1; - if (endIndex == -1) - { - endIndex = args.Count; - } - - if (startIndex < 0 || startIndex >= args.Count || endIndex < 0 || endIndex > args.Count || startIndex > endIndex) - { - throw new ArgumentOutOfRangeException(); - } + startIndex = Math.Clamp(startIndex, 0, lastIndex); + endIndex = Math.Clamp(endIndex < 0 ? lastIndex : endIndex, startIndex, lastIndex); - return args.GetRange(startIndex, endIndex - startIndex); + return startIndex == endIndex ? + [args[startIndex]] : + args[startIndex..(endIndex + 1)]; } /// From 62232627337101fe26efe1066bea10f09ea33af4 Mon Sep 17 00:00:00 2001 From: schwarper <75811921+schwarper@users.noreply.github.com> Date: Fri, 15 Nov 2024 00:09:55 +0300 Subject: [PATCH 4/5] Update CommandInfo.cs --- managed/CounterStrikeSharp.API/Modules/Commands/CommandInfo.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/managed/CounterStrikeSharp.API/Modules/Commands/CommandInfo.cs b/managed/CounterStrikeSharp.API/Modules/Commands/CommandInfo.cs index 68595597f..bdd800e84 100644 --- a/managed/CounterStrikeSharp.API/Modules/Commands/CommandInfo.cs +++ b/managed/CounterStrikeSharp.API/Modules/Commands/CommandInfo.cs @@ -15,7 +15,6 @@ */ using System; -using System.Text.RegularExpressions; using CounterStrikeSharp.API.Core; namespace CounterStrikeSharp.API.Modules.Commands From 6e849ab97e76e11e3d7b6a01db4cad6fc4f0e283 Mon Sep 17 00:00:00 2001 From: schwarper <75811921+schwarper@users.noreply.github.com> Date: Wed, 2 Apr 2025 00:07:44 +0300 Subject: [PATCH 5/5] Update GetArgs --- .../Modules/Commands/CommandInfo.cs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/managed/CounterStrikeSharp.API/Modules/Commands/CommandInfo.cs b/managed/CounterStrikeSharp.API/Modules/Commands/CommandInfo.cs index bdd800e84..265fa60e6 100644 --- a/managed/CounterStrikeSharp.API/Modules/Commands/CommandInfo.cs +++ b/managed/CounterStrikeSharp.API/Modules/Commands/CommandInfo.cs @@ -50,15 +50,24 @@ internal CommandInfo(IntPtr pointer, CCSPlayerController? player) public IEnumerable GetArgs(int startIndex = 0, int endIndex = -1) { - var args = ArgString.Split(' ', StringSplitOptions.RemoveEmptyEntries); + if (string.IsNullOrEmpty(ArgString)) + return []; + + string[] args = ArgString.Split(' ', StringSplitOptions.RemoveEmptyEntries); int lastIndex = args.Length - 1; startIndex = Math.Clamp(startIndex, 0, lastIndex); endIndex = Math.Clamp(endIndex < 0 ? lastIndex : endIndex, startIndex, lastIndex); - return startIndex == endIndex ? - [args[startIndex]] : - args[startIndex..(endIndex + 1)]; + string[] selectedArgs = startIndex == endIndex + ? [args[startIndex]] + : args[startIndex..(endIndex + 1)]; + + return selectedArgs.Select(arg => + arg.Length >= 2 && arg[0] == '"' && arg[^1] == '"' + ? arg[1..^1] + : arg + ); } ///