Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
using System.Reflection;
using SwiftlyS2.Core.Commands;
using SwiftlyS2.Shared.Commands;

namespace SwiftlyS2.Core.AttributeParsers;

internal static class CommandAttributeParser {
public static void ParseFromObject(this ICommandService self, object instance) {
var type = instance.GetType();
var methods = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
foreach (var method in methods)
internal static class CommandAttributeParser
{
public static void ParseFromObject( this ICommandService self, object instance )
{
var commandAttribute = method.GetCustomAttribute<Command>();
var clientCommandHookHandlerAttribute = method.GetCustomAttribute<ClientCommandHookHandler>();
var clientChatHookHandlerAttribute = method.GetCustomAttribute<ClientChatHookHandler>();
if (commandAttribute != null)
{
var commandAliasAttributes = method.GetCustomAttributes<CommandAlias>();
var commandName = commandAttribute.Name;
var commandAlias = commandAliasAttributes.Select(a => a.Alias).ToArray();
var registerRaw = commandAttribute.RegisterRaw;
var permission = commandAttribute.Permission;
self.RegisterCommand(commandName, method.CreateDelegate<ICommandService.CommandListener>(instance), registerRaw, permission);
foreach (var alias in commandAlias)
var type = instance.GetType();
var methods = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
foreach (var method in methods)
{
self.RegisterCommandAlias(commandName, alias, registerRaw);
}
}
var commandAttribute = method.GetCustomAttribute<Command>();
var clientCommandHookHandlerAttribute = method.GetCustomAttribute<ClientCommandHookHandler>();
var clientChatHookHandlerAttribute = method.GetCustomAttribute<ClientChatHookHandler>();
if (commandAttribute != null)
{
var commandAliasAttributes = method.GetCustomAttributes<CommandAlias>();
var commandName = commandAttribute.Name;
var registerRaw = commandAttribute.RegisterRaw;
var permission = commandAttribute.Permission;

var cmdGuid = self.RegisterCommand(commandName, method.CreateDelegate<ICommandService.CommandListener>(instance), registerRaw, permission);
foreach (var aliasAttr in commandAliasAttributes)
{
self.RegisterCommandAlias(commandName, aliasAttr.Alias, aliasAttr.RegisterRaw);
}
}

if (clientCommandHookHandlerAttribute != null)
{
self.HookClientCommand(method.CreateDelegate<ICommandService.ClientCommandHandler>(instance));
}
if (clientCommandHookHandlerAttribute != null)
{
_ = self.HookClientCommand(method.CreateDelegate<ICommandService.ClientCommandHandler>(instance));
}

if (clientChatHookHandlerAttribute != null)
{
self.HookClientChat(method.CreateDelegate<ICommandService.ClientChatHandler>(instance));
}
if (clientChatHookHandlerAttribute != null)
{
_ = self.HookClientChat(method.CreateDelegate<ICommandService.ClientChatHandler>(instance));
}
}
}
}
}
Loading
Loading