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); } } } }
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."); }
private void RegisterModuleEventHandling(TamaChanModule module) { EventSystem eventSystem = TamaChan.Instance.EventSystem; if (module is IMessageReceiver) { eventSystem.messageReceivedEvent += (module as IMessageReceiver).OnMessageReceived; } if (module is IConnectionStatusReceiver) { eventSystem.onConnectedEvent += (module as IConnectionStatusReceiver).OnConnected; eventSystem.onDisconnectedEvent += (module as IConnectionStatusReceiver).OnDisconnected; } }
private void RegisterCommand(TamaChanModule module, string commandName, Command command) { try { ModuleAttribute attribute = module.GetType().GetCustomAttribute <ModuleAttribute>(); if (!commandName.IsRegistryValid()) { throw new ArgumentException($"Command from module {attribute.moduleName} was given an invalid name: \"{commandName}\". Command names may only contain alphabetic characters and periods."); } TamaChan.Instance.Logger.LogDebug($"Registering \"{attribute.moduleName} command \"{commandName.ToLower()}\"..."); registry.Add(commandName.ToLower(), command); TamaChan.Instance.Logger.LogDebug($"Command \"{commandName.ToLower()}\" registered."); } catch (Exception ex) { TamaChan.Instance.Logger.LogError("An error occured while registering a command: " + ex.ToString()); } }