Пример #1
0
        /// <summary>
        /// Registers a console command.
        /// </summary>
        /// <param name="info">The command definition.</param>
        /// <param name="force">If set to <c>true</c>, force register the command (overwriting existing commands).</param>
        /// <exception cref="System.ArgumentException">Tried to register existing command.</exception>
        public void RegisterCommand(CommandInfo info, bool force = false)
        {
            if (this.Commands.ContainsKey(info.Name))
            {
                if (force)
                {
                    this.Commands.Remove(info.Name);
                }
                else
                {
                    throw new ArgumentException(string.Format("Tried to register existing command \"{0}\"", info.Name));
                }
            }

            this.Commands.Add(info.Name, info);
        }
Пример #2
0
        /// <summary>
        /// Loads any console-related types from an assembly.
        /// </summary>
        /// <param name="assembly">The assembly.</param>
        public void ImportAssembly(Assembly assembly)
        {
            foreach (Type type in assembly.GetTypes())
            {
                foreach (MethodInfo method in type.GetMethods())
                {
                    foreach (Attributes.CommandDef attribute in method.GetCustomAttributes<Attributes.CommandDef>(false))
                    {
                        try
                        {
                            CommandInfo info = new CommandInfo()
                            {
                                Command = (ConsoleCommand)ConsoleCommand.CreateDelegate(typeof(ConsoleCommand), method, true),
                                Name = attribute.Name,
                                Usage = attribute.Usage,
                                Help = attribute.Help
                            };

                            this.RegisterCommand(info);
                        }
                        catch (Exception e)
                        {
                            Log.Warn(string.Format("Unable to register command \"{0}\"", attribute.Name), e);
                        }
                    }
                }
            }
        }