internal MethodCommand(Func <ICommandSource, ICommandArgs, ICommand, CommandResult> methodFunc) : base(Preconditions.NotNull(ReflectUtil.GetAttributeFrom <CommandInfo>(methodFunc.Method), "methodFunc must have 'CommandInfo' attribute.")) { _methodFuncWithCommand = methodFunc; Init(true, methodFunc.Method); }
/// <summary> /// Constructor :D /// </summary> protected EssModule() { Info = ReflectUtil.GetAttributeFrom <ModuleInfo>(this); Preconditions.NotNull(Info, "Module must have 'ModuleInfo' attribute"); Preconditions.NotNull(Info.Name, "Module name cannot be null"); Logger = new ConsoleLogger($"[{Info.Name}] "); Folder = UEssentials.ModulesFolder + Info.Name + "/"; if (!Directory.Exists(Folder)) { Directory.CreateDirectory(Folder); } }
protected EssCommand() { Info = ReflectUtil.GetAttributeFrom <CommandInfo>(this); if (Info == null) { throw new ArgumentException("EssCommand must have 'CommandInfo' attribute."); } Permission = GetType().Assembly.Equals(typeof(EssCore).Assembly) ? $"essentials.command.{Name}" : Info.Permission; if (string.IsNullOrEmpty(Permission)) { Permission = Name; } }
private void RegisterAllWhere(Assembly asm, Predicate <Type> filter) { // Register classes that represents commands ( from type in asm.GetTypes() where !type.IsAbstract && typeof(ICommand).IsAssignableFrom(type) && type != typeof(MethodCommand) where filter(type) select(ICommand) EssCore.Instance.CommonInstancePool.GetOrCreate(type) ).ForEach(Register); // Register methods that represents commands T createDelegate <T>(object obj, MethodInfo method) where T : class { return(obj == null ? Delegate.CreateDelegate(typeof(T), method) as T : Delegate.CreateDelegate(typeof(T), obj, method.Name) as T); }; ( from type in asm.GetTypes() where filter(type) from method in type.GetMethods(ReflectUtil.STATIC_INSTANCE_FLAGS) where ReflectUtil.GetAttributeFrom <CommandInfo>(method) != null select method ).ForEach(method => { var inst = method.IsStatic ? null : EssCore.Instance.CommonInstancePool.GetOrCreate(method.DeclaringType); var methodParams = method.GetParameters(); if (method.ReturnType != typeof(CommandResult)) { UEssentials.Logger.LogError($"Invalid method signature in '{method}'. " + "Expected CommandResult as return type"); return; } // CommandResult methodName(ICommandSource, ICommandArgs) if ( methodParams.Length == 2 && methodParams[0].ParameterType == typeof(ICommandSource) && methodParams[1].ParameterType == typeof(ICommandArgs) ) { Register(createDelegate <Func <ICommandSource, ICommandArgs, CommandResult> >(inst, method)); return; } // CommandResult methodName(ICommandSource, ICommandArgs, ICommand) if ( methodParams.Length == 3 && methodParams[0].ParameterType == typeof(ICommandSource) && methodParams[1].ParameterType == typeof(ICommandArgs) && methodParams[2].ParameterType == typeof(ICommand) ) { Register(createDelegate <Func <ICommandSource, ICommandArgs, ICommand, CommandResult> >(inst, method)); return; } UEssentials.Logger.LogError($"Invalid method signature in '{method}'. " + "Expected parameters (ICommandSource, ICommandArgs) or (ICommandSource, ICommandArgs, ICommand)"); }); }
private void RegisterAllWhere(Assembly asm, Predicate <Type> filter) { Predicate <Type> defaultPredicate = type => { return(typeof(ICommand).IsAssignableFrom(type) && !type.IsAbstract && type != typeof(MethodCommand)); }; var commonInstPoll = EssCore.Instance.CommonInstancePool; /* * Register classes */ ( from type in asm.GetTypes() where defaultPredicate(type) where filter(type) select(ICommand) commonInstPoll.GetOrCreate(type) ).ForEach(Register); /* * Register methods */ Func <Type, object, MethodInfo, Delegate> createDelegate = (type, obj, method) => { return(obj == null ? Delegate.CreateDelegate(type, method) : Delegate.CreateDelegate(type, obj, method.Name)); }; foreach (var type in asm.GetTypes().Where(filter.Invoke)) { foreach (var method in type.GetMethods((BindingFlags)0x3C)) { if (ReflectUtil.GetAttributeFrom <CommandInfo>(method) == null) { continue; } var inst = method.IsStatic ? null : EssCore.Instance.CommonInstancePool.GetOrCreate(type); var paramz = method.GetParameters(); if (method.ReturnType != typeof(CommandResult)) { UEssentials.Logger.LogError($"Invalid method signature in '{method}'. " + "Expected 'CommandResult methodName(ICommandSource, ICommandArgs)'"); continue; } if (paramz.Length == 2 && paramz[0].ParameterType == typeof(ICommandSource) && paramz[1].ParameterType == typeof(ICommandArgs)) { Register((Func <ICommandSource, ICommandArgs, CommandResult>)createDelegate( typeof(Func <ICommandSource, ICommandArgs, CommandResult>), inst, method)); } else if (paramz.Length == 3 && paramz[0].ParameterType == typeof(ICommandSource) && paramz[1].ParameterType == typeof(ICommandArgs) && paramz[2].ParameterType == typeof(ICommand)) { Register((Func <ICommandSource, ICommandArgs, ICommand, CommandResult>)createDelegate( typeof(Func <ICommandSource, ICommandArgs, ICommand, CommandResult>), inst, method)); } else { UEssentials.Logger.LogError($"Invalid method signature in '{method}'. " + "Expected 'CommandResult methodName(ICommandSource, ICommandArgs)'"); } } } }