From 20a00ab51c7434fb34e985c9c3c8351f5bf95b19 Mon Sep 17 00:00:00 2001 From: Amir Halloul Date: Thu, 4 Jun 2020 22:20:48 +0100 Subject: [PATCH] Added alt commands and ignore case option for commands --- .../SAMP/Commands/CommandInfo.cs | 24 ++++++++++--- .../SAMP/Commands/CommandServiceBase.cs | 36 +++++++++++++------ .../SAMP/Commands/ICommandMethodInfo.cs | 9 +++-- .../SAMP/Commands/PlayerCommandAttribute.cs | 19 ++++++++-- .../SAMP/Commands/PlayerCommandService.cs | 4 +-- .../SAMP/Commands/RconCommandAttribute.cs | 18 ++++++++-- 6 files changed, 84 insertions(+), 26 deletions(-) diff --git a/src/SampSharp.Entities/SAMP/Commands/CommandInfo.cs b/src/SampSharp.Entities/SAMP/Commands/CommandInfo.cs index 24b6b11a..bbc27abb 100644 --- a/src/SampSharp.Entities/SAMP/Commands/CommandInfo.cs +++ b/src/SampSharp.Entities/SAMP/Commands/CommandInfo.cs @@ -13,6 +13,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +using System.Linq; + namespace SampSharp.Entities.SAMP.Commands { /// @@ -23,18 +25,30 @@ public class CommandInfo /// /// Initializes a new instance of the class. /// - /// The name of the command. + /// The names of the command. /// The parameters of the command. - public CommandInfo(string name, CommandParameterInfo[] parameters) + /// Ignore the names case + public CommandInfo(string[] names, CommandParameterInfo[] parameters, bool ignoreCase) { - Name = name; + Names = names; Parameters = parameters; + IgnoreCase = ignoreCase; } /// - /// Gets the name of this command. + /// Gets the names of this command. + /// + public string[] Names { get; } + + /// + /// Gets whether the command names case are ignored. + /// + public bool IgnoreCase { get; } + + /// + /// Gets the display name of this command. /// - public string Name { get; } + public string DisplayName { get => Names.OrderByDescending(n => n.Length).First(); } /// /// Gets the parameters of this command. diff --git a/src/SampSharp.Entities/SAMP/Commands/CommandServiceBase.cs b/src/SampSharp.Entities/SAMP/Commands/CommandServiceBase.cs index 49e46c43..ebcacb0f 100644 --- a/src/SampSharp.Entities/SAMP/Commands/CommandServiceBase.cs +++ b/src/SampSharp.Entities/SAMP/Commands/CommandServiceBase.cs @@ -85,11 +85,22 @@ protected InvokeResult Invoke(IServiceProvider services, object[] prefix, string inputText = inputText.Substring(name.Length); // Find commands with the name - if (!_commands.TryGetValue(name, out var commands)) + if (!_commands.TryGetValue(name.ToLower(), out var commands)) return InvokeResult.CommandNotFound; foreach (var command in commands) { + if (!command.Info.IgnoreCase) + { + for(int i = 0; i < command.Info.Names.Length; i++) + { + if (command.Info.Names[i] == name) + break; + if( i == command.Info.Names.Length - 1) + return InvokeResult.CommandNotFound; + } + } + var cmdInput = inputText; // Parse the command arguments using the parsers provided by the command @@ -171,8 +182,8 @@ protected InvokeResult Invoke(IServiceProvider services, object[] prefix, string private static string CommandText(CommandInfo command) { return command.Parameters.Length == 0 - ? $"Usage: {command.Name}" - : $"Usage: {command.Name} " + string.Join(" ", + ? $"Usage: {command.DisplayName}" + : $"Usage: {command.DisplayName} " + string.Join(" ", command.Parameters.Select(arg => arg.IsRequired ? $"[{arg.Name}]" : $"<{arg.Name}>")); } @@ -307,9 +318,9 @@ private void CreateCommandsFromAssemblies(int prefixParameters) foreach (var (method, commandInfo) in methods) { - // Determine command name. - var name = commandInfo.Name ?? GetCommandName(method); - if (name == null) + // Determine command names. + var names = commandInfo.Names ?? new[] { GetCommandName(method) }; + if (names == null) continue; // Validate acceptable return type. @@ -323,7 +334,7 @@ private void CreateCommandsFromAssemblies(int prefixParameters) if (!TryCollectParameters(methodParameters, prefixParameters, out var parameters)) continue; - var info = new CommandInfo(name, parameters); + var info = new CommandInfo(names, parameters, commandInfo.IgnoreCase); var argsPtr = 0; // The current pointer in the event arguments array. var parameterSources = methodParameters @@ -362,10 +373,13 @@ private void CreateCommandsFromAssemblies(int prefixParameters) SystemType = method.DeclaringType }; - if (!_commands.TryGetValue(info.Name, out var lst)) - lst = _commands[info.Name] = new List(); - - lst.Add(data); + foreach(string name in info.Names) + { + if (!_commands.TryGetValue(name.ToLower(), out var lst)) + lst = _commands[name.ToLower()] = new List(); + + lst.Add(data); + } } } diff --git a/src/SampSharp.Entities/SAMP/Commands/ICommandMethodInfo.cs b/src/SampSharp.Entities/SAMP/Commands/ICommandMethodInfo.cs index b60de12a..39e28e4c 100644 --- a/src/SampSharp.Entities/SAMP/Commands/ICommandMethodInfo.cs +++ b/src/SampSharp.Entities/SAMP/Commands/ICommandMethodInfo.cs @@ -21,8 +21,13 @@ namespace SampSharp.Entities.SAMP.Commands public interface ICommandMethodInfo { /// - /// Gets the overriden name of the command. + /// Gets the overriden names of the command. /// - string Name { get; } + string[] Names { get; } + + /// + /// Gets whether the case is ignored or not. + /// + bool IgnoreCase { get; } } } \ No newline at end of file diff --git a/src/SampSharp.Entities/SAMP/Commands/PlayerCommandAttribute.cs b/src/SampSharp.Entities/SAMP/Commands/PlayerCommandAttribute.cs index 4fe287a0..9447b791 100644 --- a/src/SampSharp.Entities/SAMP/Commands/PlayerCommandAttribute.cs +++ b/src/SampSharp.Entities/SAMP/Commands/PlayerCommandAttribute.cs @@ -21,12 +21,25 @@ public PlayerCommandAttribute() /// Initializes a new instance of the class. /// /// The overridden name of the command. - public PlayerCommandAttribute(string name) + public PlayerCommandAttribute(string name) : this(new[] { name }) { - Name = name; + } + /// + /// Initializes a new instance of the class. + /// + /// The overridden names of the command. + public PlayerCommandAttribute(params string[] names) + { + Names = names; + IgnoreCase = true; + } + + /// + public string[] Names { get; set; } + /// - public string Name { get; set; } + public bool IgnoreCase { get; set; } } } \ No newline at end of file diff --git a/src/SampSharp.Entities/SAMP/Commands/PlayerCommandService.cs b/src/SampSharp.Entities/SAMP/Commands/PlayerCommandService.cs index b8b023da..bbf627fc 100644 --- a/src/SampSharp.Entities/SAMP/Commands/PlayerCommandService.cs +++ b/src/SampSharp.Entities/SAMP/Commands/PlayerCommandService.cs @@ -87,8 +87,8 @@ protected override bool ValidateInputText(ref string input) private static string CommandText(CommandInfo command) { return command.Parameters.Length == 0 - ? $"Usage: /{command.Name}" - : $"Usage: /{command.Name} " + string.Join(" ", + ? $"Usage: /{command.DisplayName}" + : $"Usage: /{command.DisplayName} " + string.Join(" ", command.Parameters.Select(arg => arg.IsRequired ? $"[{arg.Name}]" : $"<{arg.Name}>")); } diff --git a/src/SampSharp.Entities/SAMP/Commands/RconCommandAttribute.cs b/src/SampSharp.Entities/SAMP/Commands/RconCommandAttribute.cs index 0392408a..9219a2d9 100644 --- a/src/SampSharp.Entities/SAMP/Commands/RconCommandAttribute.cs +++ b/src/SampSharp.Entities/SAMP/Commands/RconCommandAttribute.cs @@ -36,12 +36,24 @@ public RconCommandAttribute() /// Initializes a new instance of the class. /// /// The overridden name of the command. - public RconCommandAttribute(string name) + public RconCommandAttribute(string name) : this(new[] { name }) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The overridden name of the command. + public RconCommandAttribute(params string[] names) { - Name = name; + Names = names; + IgnoreCase = true; } /// - public string Name { get; set; } + public string[] Names { get; set; } + + /// + public bool IgnoreCase { get; set; } } } \ No newline at end of file