jsgr-mmo/src/Core/ModuleManager.cs
2023-09-02 22:52:48 +02:00

50 lines
2.1 KiB
C#

using System.Reflection;
using sodoffmmo.Attributes;
namespace sodoffmmo.Core;
class ModuleManager {
Dictionary<int, Type> handlers = new();
Dictionary<string, Type> extHandlers = new();
public void RegisterModules() {
RegisterHandlers();
RegisterExtensionHandlers();
}
public ICommandHandler GetCommandHandler(int id) {
if (handlers.TryGetValue(id, out Type? handler))
return (ICommandHandler)Activator.CreateInstance(handler)!;
throw new Exception($"Command handler with ID {id} not found!");
}
public ICommandHandler GetCommandHandler(string name) {
if (extHandlers.TryGetValue(name, out Type? handler))
return (ICommandHandler)Activator.CreateInstance(handler)!;
throw new Exception($"Command handler with name \"{name}\" not found!");
}
private void RegisterHandlers() {
handlers.Clear();
var handlerTypes = Assembly.GetExecutingAssembly().GetTypes()
.Where(type => typeof(ICommandHandler).IsAssignableFrom(type))
.Where(type => type.GetCustomAttribute<CommandHandlerAttribute>() != null);
foreach (var handlerType in handlerTypes) {
CommandHandlerAttribute attrib = (handlerType.GetCustomAttribute(typeof(CommandHandlerAttribute)) as CommandHandlerAttribute)!;
handlers[attrib.ID] = handlerType;
}
}
private void RegisterExtensionHandlers() {
extHandlers.Clear();
var extHandlerTypes = Assembly.GetExecutingAssembly().GetTypes()
.Where(type => typeof(ICommandHandler).IsAssignableFrom(type))
.Where(type => type.GetCustomAttribute<ExtensionCommandHandlerAttribute>() != null);
foreach (var extHandlerType in extHandlerTypes) {
ExtensionCommandHandlerAttribute attrib = (extHandlerType.GetCustomAttribute(typeof(ExtensionCommandHandlerAttribute)) as ExtensionCommandHandlerAttribute)!;
extHandlers[attrib.Name] = extHandlerType;
}
}
}