public AppliedCommand(ICommand command, IEnumerable<ICommand> arguments)
 {
     internCommand = command;
     internArguments = arguments;
 }
		// TODO: test if command does not exist
		public void RemoveCommand(ICommand command) => commands.Remove(commands.FirstOrDefault(kvp => kvp.Value == command).Key);
 public LazyCommand(ICommand innerCommandArg)
 {
     innerCommand = innerCommandArg;
 }
		public void AddCommand(string name, ICommand command) => commands.Add(name, command);
Exemple #5
0
        // TODO: prevent stupid behaviour like:
        // string A(int b)
        // string A(ExecutionInformation i, int b)
        // since the CommandManager can't distinguish these two, when calling
        private void LoadCommand(BotCommand com)         // TODO test
        {
            if (!CommandNamespaceValidator.IsMatch(com.InvokeName))
            {
                throw new InvalidOperationException("BotCommand has an invalid invoke name: " + com.InvokeName);
            }
            if (CommandPaths.Contains(com.FullQualifiedName))
            {
                throw new InvalidOperationException("Command already exists: " + com.InvokeName);
            }
            CommandPaths.Add(com.FullQualifiedName);

            var comPath = com.InvokeName.Split(' ');

            CommandGroup group = CommandSystem.RootCommand;

            // this for loop iterates through the seperate names of
            // the command to be added.
            for (int i = 0; i < comPath.Length - 1; i++)
            {
                ICommand currentCommand = group.GetCommand(comPath[i]);

                // if a group to hold the next level command doesn't exist
                // it will be created here
                if (currentCommand == null)
                {
                    var nextGroup = new CommandGroup();
                    group.AddCommand(comPath[i], nextGroup);
                    group = nextGroup;
                }
                // if the group already exists we can take it.
                else if (currentCommand is CommandGroup)
                {
                    group = (CommandGroup)currentCommand;
                }
                // if the element is anything else, we have to replace it
                // with a group and put the old element back into it.
                else if (currentCommand is FunctionCommand)
                {
                    var subGroup = new CommandGroup();
                    group.RemoveCommand(comPath[i]);
                    group.AddCommand(comPath[i], subGroup);

                    var botCom = currentCommand as BotCommand;
                    if (botCom != null && botCom.NormalParameters > 0)
                    {
                        Log.Write(Log.Level.Warning, "\"{0}\" has at least one parameter and won't be reachable due to an overloading function.", botCom.InvokeName);
                    }
                    subGroup.AddCommand(string.Empty, currentCommand);
                    group = subGroup;
                }
                else
                {
                    throw new InvalidOperationException("An overloaded command cannot be replaced by a CommandGroup: " + com.InvokeName);
                }
            }

            ICommand subCommand = group.GetCommand(comPath.Last());

            // the group we are trying to insert has no element with the current
            // name, so just insert it
            if (subCommand == null)
            {
                group.AddCommand(comPath.Last(), com);
                return;
            }
            // if we have a simple function, we need to create a overlaoder
            // and then add both functions to it
            else if (subCommand is FunctionCommand)
            {
                group.RemoveCommand(comPath.Last());
                var overloader = new OverloadedFunctionCommand();
                overloader.AddCommand((FunctionCommand)subCommand);
                overloader.AddCommand(com);
                group.AddCommand(comPath.Last(), overloader);
            }
            // if we have a overloaded function, we can simply add it
            else if (subCommand is OverloadedFunctionCommand)
            {
                var insertCommand = (OverloadedFunctionCommand)subCommand;
                insertCommand.AddCommand(com);
            }
            // to add a command to CommandGroup will have to treat it as a subcommand
            // with an empty string as a name
            else if (subCommand is CommandGroup)
            {
                var insertCommand  = (CommandGroup)subCommand;
                var noparamCommand = insertCommand.GetCommand(string.Empty);

                if (noparamCommand == null)
                {
                    insertCommand.AddCommand(string.Empty, com);
                    if (com.NormalParameters > 0)
                    {
                        Log.Write(Log.Level.Warning, "parameter of an empty named function under a group will be ignored!!");
                    }
                }
                else
                {
                    throw new InvalidOperationException("An empty named function under a group cannot be overloaded (" + com.InvokeName + ")");
                }
            }
            else
            {
                throw new InvalidOperationException("Unknown insertion error with " + com.FullQualifiedName);
            }
        }