Beispiel #1
0
        public void SetParentCommand(SubCommandContainer command)
        {
            if (ParentCommand != null)
            {
                throw new Exception("Parent command already set");
            }

            ParentCommand = command;
        }
        private void RegisterSubCommand(Type commandType)
        {
            SubCommand subcommand = Activator.CreateInstance(commandType) as SubCommand;

            if (subcommand == null)
            {
                throw new Exception(string.Format("Cannot create a new instance of {0}", commandType));
            }
            if (subcommand.Aliases == null)
            {
                this.logger.Error("Cannot register subcommand {0}, Aliases is null", commandType.Name);
            }
            else
            {
                if (subcommand.RequiredRole == RoleEnum.None)
                {
                    this.logger.Error("Cannot register subcommand : {0}. RequiredRole is incorrect", commandType.Name);
                }
                else
                {
                    if (subcommand.ParentCommand == null)
                    {
                        this.logger.Error <Type>("The subcommand {0} has no parent command and cannot be registered", commandType);
                    }
                    else
                    {
                        if (!this.IsCommandRegister(subcommand.ParentCommand))
                        {
                            this.RegisterCommand(subcommand.ParentCommand);
                        }
                        SubCommandContainer subCommandContainer = (
                            from entry in this.AvailableCommands
                            where entry.GetType() == subcommand.ParentCommand
                            select entry).SingleOrDefault <CommandBase>() as SubCommandContainer;
                        if (subCommandContainer == null)
                        {
                            throw new Exception(string.Format("Cannot found declaration of command '{0}'", subcommand.ParentCommand));
                        }
                        subCommandContainer.AddSubCommand(subcommand);
                        this.m_registeredCommands.Add(subcommand);
                        this.m_commandsInfos.Add(new CommandInfo(subcommand));
                        this.m_registeredTypes.Add(commandType);
                    }
                }
            }
        }
        private void RegisterSubCommandContainer(Type commandType)
        {
            SubCommandContainer subCommandContainer = Activator.CreateInstance(commandType) as SubCommandContainer;

            if (subCommandContainer == null)
            {
                throw new Exception(string.Format("Cannot create a new instance of {0}", commandType));
            }
            if (subCommandContainer.Aliases == null)
            {
                this.logger.Error("Cannot register Command {0}, Aliases is null", commandType.Name);
            }
            else
            {
                if (subCommandContainer.RequiredRole == RoleEnum.None)
                {
                    this.logger.Error("Cannot register Command : {0}. RequiredRole is incorrect", commandType.Name);
                }
                else
                {
                    this.m_registeredCommands.Add(subCommandContainer);
                    this.m_commandsInfos.Add(new CommandInfo(subCommandContainer));
                    this.m_registeredTypes.Add(commandType);
                    string[] aliases = subCommandContainer.Aliases;
                    for (int i = 0; i < aliases.Length; i++)
                    {
                        string      text = aliases[i];
                        CommandBase argument;
                        if (!this.m_commandsByAlias.TryGetValue(text, out argument))
                        {
                            this.m_commandsByAlias[CommandBase.IgnoreCommandCase ? text.ToLower() : text] = subCommandContainer;
                        }
                        else
                        {
                            this.logger.Error <string, CommandBase, SubCommandContainer>("Found two Commands with Alias \"{0}\": {1} and {2}", text, argument, subCommandContainer);
                        }
                    }
                }
            }
        }