Skip to content

Commit

Permalink
Just a little cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Tides committed Feb 23, 2024
1 parent 0a33943 commit 2b0721a
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions Obsidian/Commands/Framework/Entities/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,17 +80,30 @@ public async Task ExecuteAsync(CommandContext context, string[] args)

return;
}

IExecutor<CommandContext> 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<IExecutor<CommandContext>> executors, string[] args, CommandContext context,
[NotNullWhen(true)]out IExecutor<CommandContext>? 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++)
{
Expand All @@ -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);
Expand All @@ -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<CommandContext> commandExecutor, CommandContext context, string[] args)
Expand Down

0 comments on commit 2b0721a

Please sign in to comment.