Exemplo n.º 1
0
        private NodeContainer LoadModule(Type t, NodeContainer parent)
        {
            var moduleAttrib = t.GetCustomAttribute <ModuleAttribute>();

            if (moduleAttrib == null)
            {
                throw new InvalidOperationException("Modules must have a valid ModuleAttribute.");
            }

            NodeContainer module = new NodeModule(moduleAttrib.Name, parent, services, t);

            OnContainerLoaded?.Invoke(module, services);

            var allCommands = t.GetNestedTypes(BindingFlags.NonPublic | BindingFlags.Public)
                              .Where(x => x.GetCustomAttribute <CommandAttribute>() != null);

            foreach (var c in allCommands)
            {
                module.Children.Add(LoadCommand(c, module));
            }

            var allSingleCommands = t.GetMethods()
                                    .Where(x => x.GetCustomAttribute <CommandAttribute>() != null);

            foreach (var c in allSingleCommands)
            {
                module.Children.Add(LoadCommand(c, module));
            }

            return(module);
        }
Exemplo n.º 2
0
        private Node LoadCommand(Type t, NodeContainer parent)
        {
            var commandAttrib = t.GetCustomAttribute <CommandAttribute>();

            if (commandAttrib == null)
            {
                throw new InvalidOperationException(
                          $"Multi command of type '{t}' must have a valid CommandAttribute.");
            }

            if (commandAttrib.Aliases?.Count() == 0)
            {
                throw new InvalidOperationException(
                          $"Multi commands cannot have an invalid name.");
            }

            var multiCommand = new NodeNestedExecutable(commandAttrib.AsMetadata(), parent, services, t);

            OnContainerLoaded?.Invoke(multiCommand, services);

            var allCommands = t.GetNestedTypes()
                              .Where(x => x.GetCustomAttribute <CommandAttribute>() != null);

            foreach (var c in allCommands)
            {
                multiCommand.Children.Add(LoadCommand(c, multiCommand));
            }

            var allSingleCommands = t.GetMethods()
                                    .Where(x => x.GetCustomAttribute <CommandAttribute>() != null);

            foreach (var c in allSingleCommands)
            {
                var attrib = c.GetCustomAttribute <CommandAttribute>();
                if (attrib.Aliases == null ||
                    attrib.Aliases.Count() == 0)
                {
                    var node = LoadCommand(c, multiCommand);
                    if (node is IExecutable execNode)
                    {
                        multiCommand.SetDefaultExecution(async(e)
                                                         => await execNode.ExecuteAsync(e));
                    }
                }
                else
                {
                    multiCommand.Children.Add(LoadCommand(c, multiCommand));
                }
            }
            return(multiCommand);
        }