Exemple #1
0
 /// <summary>
 /// Loads all methods marked with <see cref="CommandHandlerAttribute"/>s in an assembly.
 /// </summary>
 /// <param name="assembly">The assembly to load from.</param>
 public void RegisterCommands(Assembly assembly)
 {
     foreach (Type type in assembly.GetTypes())
     {
         foreach (MethodInfo method in type.GetMethods())
         {
             try
             {
                 CommandHandlerAttribute attribute = method.GetCustomAttribute <CommandHandlerAttribute>();
                 if (attribute != null)
                 {
                     Func <BotCommand, Task> func = (Func <BotCommand, Task>)method.CreateDelegate(typeof(Func <BotCommand, Task>));
                     foreach (string alias in attribute.Aliases)
                     {
                         Commands.Add(alias.ToLower(), func);
                     }
                 }
             }
             catch (Exception e)
             {
                 Console.WriteLine("Failed to register command: " + e.Message);
                 Console.WriteLine(e.StackTrace);
             }
         }
     }
 }
Exemple #2
0
        public void CanCreateAttribute()
        {
            CommandHandlerAttribute attr = new CommandHandlerAttribute("TestCommand");

            Assert.IsNotNull(attr);
            Assert.AreEqual("TestCommand", attr.CommandName);
        }
Exemple #3
0
        /// <inheritdoc/>
        public virtual void OnRegisteringCommands()
        {
            Log.Debug($"Registering commands...");

            foreach (Type type in Assembly.GetTypes())
            {
                if (type.GetInterface("ICommand") != typeof(ICommand))
                {
                    continue;
                }

                if (!Attribute.IsDefined(type, typeof(CommandHandlerAttribute)))
                {
                    continue;
                }

                // Useless for now.
                CommandHandlerAttribute commandHandlerAttribute = type.GetCustomAttribute <CommandHandlerAttribute>();

                // Since I cannot determine the type of the command from the CommandHandlerAttribute, because it's not being saved anywhere inside that class,
                // I just register commands for both RemoteAdmin and GameConsole.
                try
                {
                    // Every command will be registered as RemoteAdminCommandHandler, so I'll just do one check.
                    if (!Commands[typeof(RemoteAdminCommandHandler)].TryGetValue(type, out ICommand command))
                    {
                        command = (ICommand)Activator.CreateInstance(type);
                    }

                    CommandProcessor.RemoteAdminCommandHandler.RegisterCommand(command);
                    GameCore.Console.singleton.ConsoleCommandHandler.RegisterCommand(command);

                    Commands[typeof(ClientCommandHandler)][type]      = command;
                    Commands[typeof(RemoteAdminCommandHandler)][type] = command;
                    Commands[typeof(GameConsoleCommandHandler)][type] = command;

                    Log.Debug($"Successfully registered command {command.Command}");
                }
                catch (Exception exception)
                {
                    Log.Error($"An error has occurred while registering a command: {exception}");
                }
            }

            Log.Debug($"Commands have been registered successfully!");
        }
Exemple #4
0
        public static void Init()
        {
            Type[] tmpTypes = HotFixHelper.GetHotfixTypes();

            Type tmpCommandAttributeType        = typeof(CommandAttribute);
            Type tmpCommandHandlerAttributeType = typeof(CommandHandlerAttribute);
            Type tmpCommandHandleAttributeType  = typeof(CommandHandleAttribute);

            foreach (Type typeElem in tmpTypes)
            {
                //协议信息
                var tmpCommandAttributes    = typeElem.GetCustomAttributes(tmpCommandAttributeType, false);
                CommandAttribute tmpCommand = tmpCommandAttributes.Length > 0 ? tmpCommandAttributes[0] as CommandAttribute : null;

                if (null != tmpCommand)
                {
                    if (!sCommandOpcode2TypeDict.ContainsKey(tmpCommand.Opcode))
                    {
                        sCommandOpcode2TypeDict.Add(tmpCommand.Opcode, typeElem);
                    }

                    if (!sCommandType2FieldInfoDict.ContainsKey(typeElem))
                    {
                        FieldInfo tmpFieldInfo = typeElem.GetField("Data");
                        if (null == tmpFieldInfo)
                        {
                            Logger.LogError("消息中不包含data数据");
                        }
                        else
                        {
                            sCommandType2FieldInfoDict.Add(typeElem, tmpFieldInfo);
                        }
                    }
                }

                //协议观察者信息
                var tmpCommandHandlerAttris = typeElem.GetCustomAttributes(tmpCommandHandlerAttributeType, false);
                CommandHandlerAttribute tmpCommandHandler = null;

                if (tmpCommandHandlerAttris.Length > 0)
                {
                    tmpCommandHandler = tmpCommandHandlerAttris[0] as CommandHandlerAttribute;
                }

                if (null == tmpCommandHandler)
                {
                    continue;
                }

                MethodInfo[] tmpMethods = typeElem.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);

                foreach (MethodInfo methodInfoElem in tmpMethods)
                {
                    var tmpCommandHandleAttris = methodInfoElem.GetCustomAttributes(tmpCommandHandleAttributeType, false);
                    CommandHandleAttribute tmpCommandHandle = null;

                    if (tmpCommandHandleAttris.Length > 0)
                    {
                        tmpCommandHandle = tmpCommandHandleAttris[0] as CommandHandleAttribute;
                    }

                    if (null == tmpCommandHandle)
                    {
                        continue;
                    }
                    //ILRT暂不支持GetParameters
                    //ParameterInfo[] tmpParameters = methodInfoElem.GetParameters();

                    //if (tmpParameters.Length > 0 && tmpParameters[0].ParameterType != tmpCommandHandle.CommandType)
                    //{
                    //    Logger.LogError("消息处理方法形参类型错误 " + typeElem + " -> " + methodInfoElem.Name);
                    //    continue;
                    //}

                    List <MethodInfo> tmpMethodInfoList = null;
                    if (!sOpcode2MethodInfosDict.TryGetValue(tmpCommandHandle.Opcode, out tmpMethodInfoList))
                    {
                        tmpMethodInfoList = new List <MethodInfo>();
                        sOpcode2MethodInfosDict.Add(tmpCommandHandle.Opcode, tmpMethodInfoList);
                    }

                    tmpMethodInfoList.Add(methodInfoElem);
                }
            }
        }