Exemplo n.º 1
0
        private void UnloadICommand(ICommand com, string path)
        {
            var comPath = path.Split(' ');

            var node = new CommandUnloadNode
            {
                ParentNode = null,
                Self       = RootGroup,
            };

            // build up the list to our desired node
            for (int i = 0; i < comPath.Length - 1; i++)
            {
                if (!(node.Self.GetCommand(comPath[i]) is CommandGroup nextGroup))
                {
                    break;
                }

                node = new CommandUnloadNode
                {
                    ParentNode = node,
                    Self       = nextGroup,
                };
            }

            var subGroup = node.Self.GetCommand(comPath.Last());

            switch (subGroup)
            {
            // nothing to remove
            case null:
                return;

            // if the subnode is a plain FunctionCommand then we found our command to delete
            case FunctionCommand _:
            case AliasCommand _:
                node.Self.RemoveCommand(com);
                break;

            // here we can delete our command from the overloader
            case OverloadedFunctionCommand subOverloadGroup:
                if (com is FunctionCommand funcCom)
                {
                    subOverloadGroup.RemoveCommand(funcCom);
                }
                else
                {
                    return;
                }
                break;

            // now to the special case when a command gets inserted with an empty string
            case CommandGroup insertGroup:
                // since we check precisely that only one command and only a simple FunctionCommand
                // can be added with an empty string, wen can delete it safely this way
                insertGroup.RemoveCommand(string.Empty);
                // add the node for cleanup
                node = new CommandUnloadNode
                {
                    ParentNode = node,
                    Self       = insertGroup,
                };
                break;
            }

            // and finally clean all empty nodes up
            while (node != null)
            {
                if (node.Self.IsEmpty)
                {
                    node.ParentNode?.Self.RemoveCommand(node.Self);
                }
                node = node.ParentNode;
            }
        }
Exemplo n.º 2
0
        private void UnloadCommand(BotCommand com)
        {
            if (!commandPaths.Remove(com.FullQualifiedName))
            {
                return;
            }

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

            var node = new CommandUnloadNode
            {
                ParentNode = null,
                Self       = CommandSystem.RootCommand,
            };

            // build up the list to our desired node
            for (int i = 0; i < comPath.Length - 1; i++)
            {
                if (!(node.Self.GetCommand(comPath[i]) is CommandGroup nextGroup))
                {
                    break;
                }

                node = new CommandUnloadNode
                {
                    ParentNode = node,
                    Self       = nextGroup,
                };
            }
            var subGroup = node.Self.GetCommand(comPath.Last());

            // nothing to remove
            if (subGroup is null)
            {
                return;
            }
            // if the subnode is a plain FunctionCommand then we found our command to delete
            else if (subGroup is FunctionCommand)
            {
                node.Self.RemoveCommand(com);
            }
            // here we can delete our command from the overloader
            else if (subGroup is OverloadedFunctionCommand subOverloadGroup)
            {
                subOverloadGroup.RemoveCommand(com);
            }
            // now to the special case when a command gets inserted with an empty string
            else if (subGroup is CommandGroup insertGroup)
            {
                // since we check precisely that only one command and only a simple FunctionCommand
                // can be added with an empty string, wen can delete it safely this way
                insertGroup.RemoveCommand(string.Empty);
                // add the node for cleanup
                node = new CommandUnloadNode
                {
                    ParentNode = node,
                    Self       = insertGroup,
                };
            }

            // and finally clean all empty nodes up
            while (node != null)
            {
                if (node.Self.IsEmpty)
                {
                    node.ParentNode?.Self.RemoveCommand(node.Self);
                }
                node = node.ParentNode;
            }
        }
Exemplo n.º 3
0
        private void UnloadCommand(BotCommand com)
        {
            if (!CommandPaths.Contains(com.FullQualifiedName))
                return;
            CommandPaths.Remove(com.FullQualifiedName);

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

            CommandUnloadNode node = new CommandUnloadNode
            {
                parentNode = null,
                self = CommandSystem.RootCommand,
            };

            // build up the list to our desired node
            for (int i = 0; i < comPath.Length - 1; i++)
            {
                var nextGroup = node.self.GetCommand(comPath[i]) as CommandGroup;
                if (nextGroup == null)
                    break;

                node = new CommandUnloadNode
                {
                    parentNode = node,
                    self = nextGroup,
                };
            }
            var subGroup = node.self.GetCommand(comPath.Last());
            // nothing to remove
            if (subGroup == null)
                return;
            // if the subnode is a plain FunctionCommand then we found our command to delete
            else if (subGroup is FunctionCommand)
            {
                node.self.RemoveCommand(com);
            }
            // here we can delete our command from the overloader
            else if (subGroup is OverloadedFunctionCommand)
            {
                ((OverloadedFunctionCommand)subGroup).RemoveCommand(com);
            }
            // now to the special case when a command gets inserted with an empty string
            else if (subGroup is CommandGroup)
            {
                var insertGroup = (CommandGroup)subGroup;
                // since we check precisely that only one command and only a simple FunctionCommand
                // can be added with an empty string, wen can delte it safely this way
                insertGroup.RemoveCommand(string.Empty);
                // add the node for cleanup
                node = new CommandUnloadNode
                {
                    parentNode = node,
                    self = insertGroup,
                };
            }

            // and finally clean all empty nodes up
            while (node != null)
            {
                if (node.self.IsEmpty && node.parentNode != null)
                    node.parentNode.self.RemoveCommand(node.self);
                node = node.parentNode;
            }
        }