Exemplo n.º 1
0
        public void RegisterModuleCommands(TamaChanModule module, HelpFileGenerator helpFileGenerator)
        {
            MethodInfo[] methods = module.GetType().GetMethods();

            foreach (MethodInfo m in methods)
            {
                CommandAttribute attribute = m.GetCustomAttribute <CommandAttribute>();
                if (attribute != null)
                {
                    Command command = new Command(attribute.commandName, m, attribute.PermissionFlag, attribute.BotOwnerOnly, attribute.IsNSFW, attribute.ParameterExample, module);
                    RegisterCommand(module, attribute.commandName, command);

                    if (registry.ContainsKey(attribute.commandName.ToLower()))
                    {
                        IEnumerable <AltCommandAttribute> altCommandAttributes = m.GetCustomAttributes <AltCommandAttribute>();
                        foreach (AltCommandAttribute alt in altCommandAttributes)
                        {
                            if (!alt.commandName.IsRegistryValid())
                            {
                                TamaChan.Instance.Logger.LogWarning($"Alt command \"{alt.commandName}\" for \"{attribute.commandName}\" is not valid. Ignored.");
                            }
                            else
                            {
                                altCommandRegistry.Add(alt.commandName.ToLower(), attribute.commandName.ToLower());
                            }
                        }
                        command.Description = attribute.Description;
                        helpFileGenerator.commands.Add(command);
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void RegisterModule(Type moduleType, ModuleAttribute attribute, HelpFileGenerator helpFileGenerator)
        {
            if (attribute.moduleName == string.Empty || attribute.moduleName == null)
            {
                throw new ArgumentException("Module name can not be null or an empty string.");
            }
            if (registry.ContainsKey(attribute.moduleName))
            {
                throw new ArgumentException($"Module registry already contains a module with the name \"{attribute.moduleName}\".");
            }

            string logString = $"Registering Module \"{attribute.moduleName}\"";

            if (attribute.Version != null && !attribute.Version.Equals(string.Empty))
            {
                logString += $" version \"{attribute.Version}\"";
            }
            TamaChan.Instance.Logger.LogInfo(logString + "...");

            TamaChanModule module = Activator.CreateInstance(moduleType) as TamaChanModule;

            TamaChan.Instance.CommandRegistry.RegisterModuleCommands(module, helpFileGenerator);
            registry.Add(attribute.moduleName, module);
            RegisterModuleEventHandling(module);
            module.RegistryName = attribute.moduleName;
            module.Initialize();
            TamaChan.Instance.Logger.LogInfo($"Module \"{attribute.moduleName}\" registered.");
        }
Exemplo n.º 3
0
        internal void RegisterModules()
        {
            if (!Directory.Exists(MODULE_LIBRARY_PATH))
            {
                Directory.CreateDirectory(MODULE_LIBRARY_PATH);
                throw new DirectoryNotFoundException($"Directory \"{MODULE_LIBRARY_PATH}\" did not exist. Created it instead. Please add in modules there.");
            }
            TamaChan.Instance.Logger.LogInfo("Registering modules...");
            HelpFileGenerator helpFileGenerator = new HelpFileGenerator();

            Assembly[] assemblies = LoadAssemblies();
            RegisterModules(assemblies, helpFileGenerator);
            helpFileGenerator.Generate();
        }
Exemplo n.º 4
0
 private void RegisterModules(Assembly[] assemblies, HelpFileGenerator helpFileGenerator)
 {
     foreach (Assembly assembly in assemblies)
     {
         if (!assembly.FullName.StartsWith("Discord.Net"))
         {
             Type[] assemblyTypes = assembly.GetTypes();
             foreach (Type type in assemblyTypes)
             {
                 if (!type.IsAbstract && type.IsSubclassOf(typeof(TamaChanModule)))
                 {
                     ModuleAttribute moduleAttribute = type.GetCustomAttribute <ModuleAttribute>();
                     if (moduleAttribute == null)
                     {
                         TamaChan.Instance.Logger.LogWarning($"Class {type.FullName} from {assembly.FullName} inherits TamaChanModule, but does not contain the Module attribute. Module will be ignored.");
                     }
                     else if (moduleAttribute.moduleName.IsRegistryValid())
                     {
                         try
                         {
                             RegisterModule(type, moduleAttribute, helpFileGenerator);
                         }
                         catch (Exception ex)
                         {
                             TamaChan.Instance.Logger.LogError($"Could not instantiate and register module {type.FullName} from {assembly.FullName}: " + ex.ToString());
                         }
                     }
                     else
                     {
                         TamaChan.Instance.Logger.LogError($"Module class {type.FullName} from {assembly.FullName} has invalid characters in its name: \"{moduleAttribute.moduleName}\". May only contain alphabetic characters and periods.");
                     }
                 }
             }
         }
     }
 }