46 lines
1.5 KiB
C#
46 lines
1.5 KiB
C#
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using Vintagestory.API.Common;
|
|
|
|
namespace ClaimLink;
|
|
|
|
internal delegate TextCommandResult? HookBeforeDelegate(TextCommandCallingArgs args);
|
|
|
|
internal delegate void HookAfterDelegate(TextCommandCallingArgs args, TextCommandResult result);
|
|
|
|
internal class CommandHookListenerProxy : DispatchProxy
|
|
{
|
|
private string modId = null!;
|
|
private IReadOnlyList<string> commands = null!;
|
|
private HookBeforeDelegate? before;
|
|
private HookAfterDelegate? after;
|
|
|
|
internal static CommandHook.ICommandHookListener Create(
|
|
string modId,
|
|
IReadOnlyList<string> commands,
|
|
HookBeforeDelegate? before,
|
|
HookAfterDelegate? after
|
|
)
|
|
{
|
|
object proxy = Create<CommandHook.ICommandHookListener, CommandHookListenerProxy>()!;
|
|
CommandHookListenerProxy p = (CommandHookListenerProxy)proxy;
|
|
p.modId = modId;
|
|
p.commands = commands;
|
|
p.before = before;
|
|
p.after = after;
|
|
return (CommandHook.ICommandHookListener)proxy;
|
|
}
|
|
|
|
protected override object? Invoke(MethodInfo? targetMethod, object?[]? args) =>
|
|
targetMethod!.Name switch
|
|
{
|
|
"get_ModId" => modId,
|
|
"get_Commands" => commands,
|
|
"get_Registration" => new CommandHook.CommandRegistration(
|
|
before == null ? null : new CommandHook.CommandBeforeDelegate(before),
|
|
after == null ? null : new CommandHook.CommandAfterDelegate(after)
|
|
),
|
|
_ => null,
|
|
};
|
|
}
|