public bool Execute(Tag tag, int permissionLevel, string name, params object[] args) { lock (CommandsLock) { foreach (Command command in Commands) { if (!CommandAPI.NameEquals(name, command.Name, IgnoreCase)) { continue; } int argsLength = command.Parameters.Length - 1; if (args.Length < argsLength) { continue; } if (argsLength == 0 && args.Length > argsLength) { continue; } try { for (int i = 0; i < command.Parameters.Length; i++) { if (i == 0) { command.Parameters[i] = tag; continue; } Type parameterType = command.ParameterInfos[i].ParameterType; if (i == command.Parameters.Length - 1 && args.Length >= argsLength && parameterType.IsArray) { Type elementType = parameterType.GetElementType(); Array array = Array.CreateInstance(elementType, args.Length - argsLength + 1); for (int n = 0; n < array.Length; n++) { array.SetValue(Convert.ChangeType(args[argsLength + n - 1], elementType), n); } command.Parameters[i] = array; continue; } command.Parameters[i] = Convert.ChangeType(args[i - 1], parameterType); } } catch { continue; } if (permissionLevel < command.PermissionLevel) { NoPermission?.Invoke(tag); continue; } command.Invoke(); return(true); } return(false); } }
public void Register <T> () where T : class { lock (CommandsLock) { foreach (MethodInfo methodInfo in typeof(T).GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)) { Command command = CommandAPI.GetCustomAttribute <Command> (methodInfo); if (command is null) { continue; } command.MethodInfo = methodInfo; Commands.Add(command); } } }