1
0
mirror of https://github.com/oliverbooth/VpSharp synced 2024-11-22 19:18:47 +00:00

style: reduce complexity of command arg parsing

This commit is contained in:
Oliver Booth 2023-05-08 15:59:39 +01:00
parent 268c359f3a
commit 7084fc3f4a
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025

View File

@ -211,45 +211,7 @@ public sealed class CommandsExtension : VirtualParadiseClientExtension
} }
object?[] arguments = {context}; object?[] arguments = {context};
if (rawArguments.Length > 0) arguments = ParseArguments(rawArguments, arguments, command);
{
spaceIndex = rawArguments.IndexOf(' ');
if (spaceIndex == -1)
{
Array.Resize(ref arguments, 2);
arguments[1] = rawArguments.ToString();
}
else
{
var appendLast = true;
for (var argumentIndex = 1; spaceIndex > -1; argumentIndex++)
{
Array.Resize(ref arguments, argumentIndex + 1);
if (argumentIndex == command.Parameters.Length)
{
if (command.Parameters[argumentIndex - 1].GetCustomAttribute<RemainderAttribute>() is not null)
{
appendLast = false;
arguments[argumentIndex] = rawArguments.ToString();
break;
}
}
arguments[argumentIndex] = rawArguments[..spaceIndex].ToString();
rawArguments = rawArguments[(spaceIndex + 1)..];
spaceIndex = rawArguments.IndexOf(' ');
argumentIndex++;
}
if (appendLast)
{
Array.Resize(ref arguments, arguments.Length + 1);
arguments[^1] = rawArguments.ToString();
}
}
}
ParameterInfo[] parameters = commandMethod.GetParameters(); ParameterInfo[] parameters = commandMethod.GetParameters();
if (parameters.Length != arguments.Length || parameters[arguments.Length..].Any(p => !p.IsOptional)) if (parameters.Length != arguments.Length || parameters[arguments.Length..].Any(p => !p.IsOptional))
@ -302,6 +264,51 @@ public sealed class CommandsExtension : VirtualParadiseClientExtension
return base.OnMessageReceived(message); return base.OnMessageReceived(message);
} }
private static object?[] ParseArguments(ReadOnlySpan<char> rawArguments, object?[] arguments, Command command)
{
if (rawArguments.Length <= 0)
{
return arguments;
}
int spaceIndex = rawArguments.IndexOf(' ');
if (spaceIndex == -1)
{
Array.Resize(ref arguments, 2);
arguments[1] = rawArguments.ToString();
return arguments;
}
var appendLast = true;
for (var argumentIndex = 1; spaceIndex > -1; argumentIndex++)
{
Array.Resize(ref arguments, argumentIndex + 1);
if (argumentIndex == command.Parameters.Length)
{
if (command.Parameters[argumentIndex - 1].GetCustomAttribute<RemainderAttribute>() is not null)
{
appendLast = false;
arguments[argumentIndex] = rawArguments.ToString();
break;
}
}
arguments[argumentIndex] = rawArguments[..spaceIndex].ToString();
rawArguments = rawArguments[(spaceIndex + 1)..];
spaceIndex = rawArguments.IndexOf(' ');
argumentIndex++;
}
if (appendLast)
{
Array.Resize(ref arguments, arguments.Length + 1);
arguments[^1] = rawArguments.ToString();
}
return arguments;
}
private void RegisterCommandMethod(CommandModule module, MethodInfo methodInfo) private void RegisterCommandMethod(CommandModule module, MethodInfo methodInfo)
{ {
var commandAttribute = methodInfo.GetCustomAttribute<CommandAttribute>(); var commandAttribute = methodInfo.GetCustomAttribute<CommandAttribute>();