From 0a339435ddd5294d86ee9a811d3663d130c2874c Mon Sep 17 00:00:00 2001 From: Tides Date: Fri, 23 Feb 2024 15:05:31 -0500 Subject: [PATCH 1/4] Set success to true if no args are provided --- Obsidian/Commands/Framework/Entities/Command.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Obsidian/Commands/Framework/Entities/Command.cs b/Obsidian/Commands/Framework/Entities/Command.cs index 140d8085..09b4b263 100644 --- a/Obsidian/Commands/Framework/Entities/Command.cs +++ b/Obsidian/Commands/Framework/Entities/Command.cs @@ -87,6 +87,9 @@ public async Task ExecuteAsync(CommandContext context, string[] args) { executor = exec; + if(args.Length == 0) + success = true;//First executor will work + var methodParams = exec.GetParameters(); for (int i = 0; i < args.Length; i++) { From 2b0721ac887921f1adfae5d7f866b2bba7ee8582 Mon Sep 17 00:00:00 2001 From: Tides Date: Fri, 23 Feb 2024 15:19:17 -0500 Subject: [PATCH 2/4] Just a little cleanup --- .../Commands/Framework/Entities/Command.cs | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/Obsidian/Commands/Framework/Entities/Command.cs b/Obsidian/Commands/Framework/Entities/Command.cs index 09b4b263..942d27a9 100644 --- a/Obsidian/Commands/Framework/Entities/Command.cs +++ b/Obsidian/Commands/Framework/Entities/Command.cs @@ -4,6 +4,7 @@ using Obsidian.Commands.Framework.Exceptions; using Obsidian.Plugins; using Obsidian.Utilities.Interfaces; +using System.Diagnostics.CodeAnalysis; using System.Reflection; namespace Obsidian.Commands.Framework.Entities; @@ -79,17 +80,30 @@ public async Task ExecuteAsync(CommandContext context, string[] args) return; } - - IExecutor executor = default!; - var success = false; + if (!this.TryFindExecutor(executors, args, context, out var executor)) + throw new InvalidOperationException($"Failed to find valid executor for /{this.Name}"); + + await this.ExecuteAsync(executor, context, args); + } + + private bool TryFindExecutor(IEnumerable> executors, string[] args, CommandContext context, + [NotNullWhen(true)]out IExecutor? executor) + { + executor = null; + + var success = args.Length == 0; + + if (success) + { + executor = executors.First(); + return true; + } + foreach (var exec in executors) { executor = exec; - if(args.Length == 0) - success = true;//First executor will work - var methodParams = exec.GetParameters(); for (int i = 0; i < args.Length; i++) { @@ -99,7 +113,8 @@ public async Task ExecuteAsync(CommandContext context, string[] args) if (!CommandHandler.IsValidArgumentType(param.ParameterType)) { success = false; - break; + executor = null; + return false; } var parser = CommandHandler.GetArgumentParser(param.ParameterType); @@ -110,17 +125,14 @@ public async Task ExecuteAsync(CommandContext context, string[] args) } success = false; - break; + return false; } if (success) - break; + return true; } - if (!success) - throw new InvalidOperationException($"Failed to find valid executor for /{this.Name}"); - - await this.ExecuteAsync(executor, context, args); + return false; } private async Task ExecuteAsync(IExecutor commandExecutor, CommandContext context, string[] args) From 9c3bb5b969cdf8c5bd75b7bfd187a0f5498c035e Mon Sep 17 00:00:00 2001 From: Tides Date: Sat, 24 Feb 2024 02:42:36 -0500 Subject: [PATCH 3/4] Update Command.cs --- Obsidian/Commands/Framework/Entities/Command.cs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Obsidian/Commands/Framework/Entities/Command.cs b/Obsidian/Commands/Framework/Entities/Command.cs index 942d27a9..61bd5906 100644 --- a/Obsidian/Commands/Framework/Entities/Command.cs +++ b/Obsidian/Commands/Framework/Entities/Command.cs @@ -88,7 +88,7 @@ public async Task ExecuteAsync(CommandContext context, string[] args) } private bool TryFindExecutor(IEnumerable> executors, string[] args, CommandContext context, - [NotNullWhen(true)]out IExecutor? executor) + [NotNullWhen(true)] out IExecutor? executor) { executor = null; @@ -102,8 +102,6 @@ private bool TryFindExecutor(IEnumerable> executors, s foreach (var exec in executors) { - executor = exec; - var methodParams = exec.GetParameters(); for (int i = 0; i < args.Length; i++) { @@ -111,11 +109,7 @@ private bool TryFindExecutor(IEnumerable> executors, s var arg = args[i]; if (!CommandHandler.IsValidArgumentType(param.ParameterType)) - { - success = false; - executor = null; return false; - } var parser = CommandHandler.GetArgumentParser(param.ParameterType); if (parser.TryParseArgument(arg, context, out _)) @@ -124,12 +118,14 @@ private bool TryFindExecutor(IEnumerable> executors, s continue; } - success = false; return false; } if (success) + { + executor = exec; return true; + } } return false; From f6bba107d634b9486c2f3bf16a8afc4dd5965a86 Mon Sep 17 00:00:00 2001 From: Tides Date: Sat, 24 Feb 2024 14:45:28 -0500 Subject: [PATCH 4/4] Make sure to not return false early --- Obsidian/Commands/Framework/Entities/Command.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Obsidian/Commands/Framework/Entities/Command.cs b/Obsidian/Commands/Framework/Entities/Command.cs index 61bd5906..87adfa56 100644 --- a/Obsidian/Commands/Framework/Entities/Command.cs +++ b/Obsidian/Commands/Framework/Entities/Command.cs @@ -109,7 +109,10 @@ private bool TryFindExecutor(IEnumerable> executors, s var arg = args[i]; if (!CommandHandler.IsValidArgumentType(param.ParameterType)) - return false; + { + success = false; + continue; + } var parser = CommandHandler.GetArgumentParser(param.ParameterType); if (parser.TryParseArgument(arg, context, out _)) @@ -118,7 +121,7 @@ private bool TryFindExecutor(IEnumerable> executors, s continue; } - return false; + success = false; } if (success)