public CommandProcessor(CommandProcessorConfiguration config) { this.config = config; if (config.AutoSearchForCommands) { CommandSeeker s = new CommandSeeker(); hierarchyRoot = s.GetCommandsFromAttributeAsync(); cachedCommands = hierarchyRoot.GetAllEntitiesOf <Command>(); } if (config.MentionAsPrefix) { if (config.DefaultPrefix != "") { prefixes.Add(new Prefix(config.DefaultPrefix) { Configurable = config.DefaultConfigurable }); } prefixes.Add(new MentionPrefix()); } else { prefixes.Add(new Prefix(config.DefaultPrefix) { Configurable = config.DefaultConfigurable }); } }
public CommandEntity GetCommandsFromAttributeAsync() { CommandEntity commandHierarchyList = new CommandEntity(); commandHierarchyList.Id = "root"; Assembly asm = Assembly.GetEntryAssembly(); var modules = asm.GetTypes() .Where(m => m.GetTypeInfo().GetCustomAttributes <ModuleAttribute>().Any() && !m.IsNested) .ToArray(); int modulesLoaded = 0; int subModulesLoaded = 0; int commandsLoaded = 0; foreach (var m in modules) { CommandEntity entity = GetChildrenFromModule(m); commandHierarchyList.Children.Add(entity); modulesLoaded++; subModulesLoaded += entity.Children.Count(x => (x as Module) != null); commandsLoaded += entity.GetAllEntitiesOf <Command>().Count; } return(commandHierarchyList); }
public CommandEntity ParseMultiCommand(Type attribute) { CommandEntity command = new CommandEntity(); MultiCommandAttribute moduleAttribute = attribute.GetTypeInfo().GetCustomAttribute <MultiCommandAttribute>(); command = new MultiCommand(moduleAttribute.Entity as MultiCommand); object constructedInstance = null; try { constructedInstance = Activator.CreateInstance(Type.GetType(attribute.AssemblyQualifiedName), command); } catch { constructedInstance = Activator.CreateInstance(Type.GetType(attribute.AssemblyQualifiedName)); } List <Type> allChildren = attribute.GetNestedTypes(BindingFlags.NonPublic | BindingFlags.Public) .Where((x) => x.GetTypeInfo().GetCustomAttributes <CommandEntityAttribute>().Any()) .ToList(); foreach (Type t in allChildren) { CommandEntityAttribute entityAttribute = t.GetTypeInfo().GetCustomAttribute <CommandEntityAttribute>(); CommandEntity entity = null; if (entityAttribute is MultiCommandAttribute cAttribute) { entity = ParseMultiCommand(t); } if (entity == null) { continue; } entity.Parent = command; command.Children.Add(entity); } List <MethodInfo> methods = attribute.GetMethods() .Where((x) => x.GetCustomAttributes <CommandAttribute>().Any()) .ToList(); foreach (MethodInfo m in methods) { Command newEvent = CreateCommand(m, command, constructedInstance); if (newEvent.IsDefault && (command as MultiCommand).defaultCommand == null) { (command as MultiCommand).defaultCommand = newEvent; } command.Children.Add(newEvent); } return(command); }
public Command CreateCommand(MethodInfo m, CommandEntity parent, object constructedInstance) { CommandAttribute commandAttribute = m.GetCustomAttribute <CommandAttribute>(); Command newEvent = new Command(commandAttribute.Entity as Command); newEvent.ProcessCommand = async(context) => await(Task) m.Invoke(constructedInstance, new object[] { context }); newEvent.Parent = parent; return(newEvent); }
public CommandEntity GetChildrenFromModule(Type attribute) { CommandEntity module = new CommandEntity(); ModuleAttribute moduleAttribute = attribute.GetTypeInfo().GetCustomAttribute <ModuleAttribute>(); module = new Module(moduleAttribute.Entity as Module); object constructedInstance = null; List <Type> allChildren = attribute.GetNestedTypes(BindingFlags.NonPublic | BindingFlags.Public) .Where((x) => x.GetTypeInfo().GetCustomAttributes <CommandEntityAttribute>().Any()) .ToList(); try { constructedInstance = Activator.CreateInstance(Type.GetType(attribute.AssemblyQualifiedName), module); } catch { constructedInstance = Activator.CreateInstance(Type.GetType(attribute.AssemblyQualifiedName)); } foreach (Type t in allChildren) { CommandEntityAttribute entityAttribute = t.GetTypeInfo().GetCustomAttribute <CommandEntityAttribute>(); CommandEntity entity = null; if (entityAttribute is ModuleAttribute mAttribute) { entity = GetChildrenFromModule(t); } else if (entityAttribute is MultiCommandAttribute cAttribute) { entity = ParseMultiCommand(t); } if (entity == null) { continue; } entity.Parent = module; module.Children.Add(entity); } List <MethodInfo> methods = attribute.GetMethods() .Where((x) => x.GetCustomAttributes <CommandAttribute>().Any()) .ToList(); foreach (MethodInfo m in methods) { module.Children.Add(CreateCommand(m, module, constructedInstance)); } return(module); }
public CommandProcessor(CommandProcessorConfiguration config) { _prefixes.Add(new Prefix(config.DefaultPrefix)); if (config.AutoSearchForCommands) { CommandSeeker s = new CommandSeeker(); hierarchyRoot = s.GetCommandsFromAttributeAsync(); cachedCommands = hierarchyRoot.GetAllEntitiesOf <Command>(); } }
public CommandEntity GetChildrenFromModule(Type attribute) { CommandEntity module = new CommandEntity(); ModuleAttribute moduleAttribute = attribute.GetTypeInfo().GetCustomAttribute <ModuleAttribute>(); module = new Module(moduleAttribute.Entity as Module); object constructedInstance = null; List <Type> allChildren = attribute.GetNestedTypes(BindingFlags.NonPublic | BindingFlags.Public) .Where((x) => x.GetTypeInfo().GetCustomAttributes <CommandEntityAttribute>().Any()) .ToList(); try { constructedInstance = Activator.CreateInstance(Type.GetType(attribute.AssemblyQualifiedName), module); } catch { constructedInstance = Activator.CreateInstance(Type.GetType(attribute.AssemblyQualifiedName)); } foreach (Type t in allChildren) { CommandEntityAttribute entityAttribute = t.GetTypeInfo().GetCustomAttribute <CommandEntityAttribute>(); if (entityAttribute is ModuleAttribute mAttribute) { CommandEntity entity = GetChildrenFromModule(t); module = new Module(mAttribute.Entity as Module); entity.Parent = module; module.Children.Add(entity); } } List <MethodInfo> methods = attribute.GetMethods() .Where((x) => x.GetCustomAttributes <CommandAttribute>().Any()) .ToList(); foreach (MethodInfo m in methods) { CommandAttribute commandAttribute = m.GetCustomAttribute <CommandAttribute>(); Command newEvent = new Command(commandAttribute.Entity as Command); newEvent.ProcessCommand = async(context) => await(Task) m.Invoke(constructedInstance, new object[] { context }); newEvent.Parent = module; module.Children.Add(newEvent); } return(module); }
private CommandEntity GetCommand(CommandEntity root, string query) { CommandEntity entity = root.Children .FirstOrDefault(x => query.StartsWith(x.Id)); if (entity is Command command) { return(command); } int dot = query.IndexOf('.'); string newQuery = query.Substring(dot == -1 ? 0 : dot) .TrimStart('.'); if (entity == null) { return(null); } return(GetCommand(entity, newQuery)); }
public Command(CommandEntity cmd) : base(cmd) { }
public CommandEntity(CommandEntity entity) { Id = entity.Id; Parent = entity.Parent; Children = entity.Children; }
public Module(CommandEntity entity) : base(entity) { }