/// <summary> /// load all handlers in TCurrentClass' assembly /// </summary> /// <typeparam name="TCurrentClass"></typeparam> /// <returns>amount of handlers loaded, or -1 (if an error occurs)</returns> public int LoadHandlers <TCurrentClass>(string identifier) { try { List <Type> handlers = typeof(TCurrentClass).Assembly.GetTypes() .Where(x => x.IsClass && !x.IsAbstract && IsAssignableToGenericType(x, typeof(ICommandHandler <>)) && x.GetCustomAttributes <AutoDiscoverAttribute>().Count() == 1 && x.GetCustomAttribute <AutoDiscoverAttribute>().Identifier == identifier) .ToList(); handlers.ForEach(x => { short id = CommandLookup.CreateInstance(x.GetInterfaces()[0].GetGenericArguments()[0], _logger).ID; if (_lookup.ContainsKey(id)) { _logger?.LogWarning($"Cannot add handler '{x.GetType().Name}' for '{id}' " + $"as identifier, because there is already a handler for this identifier!"); return; } try { _lookup.Add(id, Delegate.CreateDelegate(typeof(Action <,>) .MakeGenericType(typeof(IClient), x.GetInterfaces()[0].GetGenericArguments()[0]), x.CreateInstance(), x.GetMethod("Execute"))); } catch (Exception e) { _logger?.LogError(e); } }); return(handlers.Count); } catch (Exception e) { _logger?.LogError(e); } return(-1); }
public ICommandLookup BuildCommandLookup(IGameLogger logger) { if (_commandLookup == null) { _commandLookup = new CommandLookup(logger); int loaded = _commandLookup.LoadCommands <NettyLookupBuilder>("10.0.6435"); if (loaded < 0) { throw logger.LogError(new InvalidOperationException("Failed to fill commandLookup!")); } logger.LogInformation($"CommandLookup loaded with {loaded} commands!"); } return(_commandLookup); }