Skip to content

Commit 96a84ba

Browse files
committed
For some reason the constructed delegate fails with high parameter cound commands
1 parent 86a8d40 commit 96a84ba

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

LabExtended/Commands/CommandOverload.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ public class CommandOverload
3737
/// </summary>
3838
public Dictionary<string, CommandParameterBuilder> ParameterBuilders { get; } = new();
3939

40-
/// <summary>
41-
/// Gets the compiled method delegate.
42-
/// </summary>
43-
public Func<object, object[], object> Method { get; }
44-
4540
/// <summary>
4641
/// Gets the empty buffer.
4742
/// </summary>
@@ -120,8 +115,6 @@ public CommandOverload(MethodInfo target)
120115
IsEmpty = parameters?.Length == 0;
121116
IsAsync = target.ReturnType == typeof(Task);
122117
IsCoroutine = target.ReturnType == typeof(IEnumerator<float>);
123-
124-
Method = FastReflection.ForMethod(Target);
125118

126119
if (target.DeclaringType != null && target.DeclaringType.InheritsType<ContinuableCommandBase>())
127120
{
@@ -143,4 +136,30 @@ public CommandOverload(MethodInfo target)
143136
if (!IsEmpty)
144137
Buffer = new(new object[ParameterCount], () => new object[ParameterCount]);
145138
}
139+
140+
/// <summary>
141+
/// Invokes the underlying command or method with the specified context and arguments.
142+
/// </summary>
143+
/// <param name="ctx">The command context that provides the target instance and execution environment. Cannot be null.</param>
144+
/// <param name="args">An array of arguments to pass to the command or method. The number of elements must match the expected parameter
145+
/// count. If the command takes no parameters, this can be null or an empty array.</param>
146+
/// <returns>The result returned by the invoked command or method. The type and meaning of the result depend on the specific
147+
/// command implementation.</returns>
148+
/// <exception cref="ArgumentNullException">Thrown if ctx is null, or if args is null when the command expects one or more parameters.</exception>
149+
/// <exception cref="ArgumentException">Thrown if the number of elements in args does not match the expected parameter count.</exception>
150+
public object Invoke(CommandContext ctx, object[] args)
151+
{
152+
if (ctx is null)
153+
throw new ArgumentNullException(nameof(ctx));
154+
155+
if (args is null && !IsEmpty)
156+
throw new ArgumentNullException(nameof(args));
157+
158+
args ??= Array.Empty<object>();
159+
160+
if (args.Length != ParameterCount)
161+
throw new ArgumentException($"Expected {ParameterCount} arguments, but got {args.Length}.");
162+
163+
return Target.Invoke(ctx.Instance, args);
164+
}
146165
}

LabExtended/Commands/Runners/ContinuableCommandRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void FinishCommand()
105105

106106
try
107107
{
108-
var result = ctx.Overload.Method(ctx.Instance, buffer);
108+
var result = ctx.Overload.Invoke(ctx, buffer);
109109

110110
if (result is IEnumerator<float> coroutine)
111111
{

LabExtended/Commands/Runners/RegularCommandRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void FinishCommand()
5555

5656
try
5757
{
58-
var result = ctx.Overload.Method(ctx.Instance, buffer);
58+
var result = ctx.Overload.Invoke(ctx, buffer);
5959

6060
if (result is IEnumerator<float> coroutine)
6161
{

0 commit comments

Comments
 (0)