Exemplo n.º 1
0
        /// <summary>
        /// Configures the dependency injection services
        /// </summary>
        private static IServiceCollection ConfigureServices(string[] args)
        {
            var appConfigHandler = new BaseConfigurationHandler <ApplicationConfiguration>("IW4MAdminSettings");
            var appConfig        = appConfigHandler.Configuration();
            var defaultLogger    = new Logger("IW4MAdmin-Manager");

            var masterUri      = Utilities.IsDevelopment ? new Uri("http://127.0.0.1:8080") : appConfig?.MasterUrl ?? new ApplicationConfiguration().MasterUrl;
            var apiClient      = RestClient.For <IMasterApi>(masterUri);
            var pluginImporter = new PluginImporter(defaultLogger, appConfig, apiClient, new RemoteAssemblyHandler(defaultLogger, appConfig));

            var serviceCollection = new ServiceCollection();

            serviceCollection.AddSingleton <IServiceCollection>(_serviceProvider => serviceCollection)
            .AddSingleton(appConfigHandler as IConfigurationHandler <ApplicationConfiguration>)
            .AddSingleton(new BaseConfigurationHandler <CommandConfiguration>("CommandConfiguration") as IConfigurationHandler <CommandConfiguration>)
            .AddSingleton(_serviceProvider => _serviceProvider.GetRequiredService <IConfigurationHandler <ApplicationConfiguration> >().Configuration() ?? new ApplicationConfiguration())
            .AddSingleton(_serviceProvider => _serviceProvider.GetRequiredService <IConfigurationHandler <CommandConfiguration> >().Configuration() ?? new CommandConfiguration())
            .AddSingleton <ILogger>(_serviceProvider => defaultLogger)
            .AddSingleton <IPluginImporter, PluginImporter>()
            .AddSingleton <IMiddlewareActionHandler, MiddlewareActionHandler>()
            .AddSingleton <IRConConnectionFactory, RConConnectionFactory>()
            .AddSingleton <IGameServerInstanceFactory, GameServerInstanceFactory>()
            .AddSingleton <IConfigurationHandlerFactory, ConfigurationHandlerFactory>()
            .AddSingleton <IParserRegexFactory, ParserRegexFactory>()
            .AddSingleton <IDatabaseContextFactory, DatabaseContextFactory>()
            .AddSingleton <IGameLogReaderFactory, GameLogReaderFactory>()
            .AddSingleton <IScriptCommandFactory, ScriptCommandFactory>()
            .AddSingleton <IAuditInformationRepository, AuditInformationRepository>()
            .AddSingleton <IEntityService <EFClient>, ClientService>()
            .AddSingleton <IMetaService, MetaService>()
            .AddSingleton <IMetaRegistration, MetaRegistration>()
            .AddSingleton <IScriptPluginServiceResolver, ScriptPluginServiceResolver>()
            .AddSingleton <IResourceQueryHelper <ClientPaginationRequest, ReceivedPenaltyResponse>, ReceivedPenaltyResourceQueryHelper>()
            .AddSingleton <IResourceQueryHelper <ClientPaginationRequest, AdministeredPenaltyResponse>, AdministeredPenaltyResourceQueryHelper>()
            .AddSingleton <IResourceQueryHelper <ClientPaginationRequest, UpdatedAliasResponse>, UpdatedAliasResourceQueryHelper>()
            .AddSingleton <IResourceQueryHelper <ChatSearchQuery, MessageResponse>, ChatResourceQueryHelper>()
            .AddTransient <IParserPatternMatcher, ParserPatternMatcher>()
            .AddSingleton <IRemoteAssemblyHandler, RemoteAssemblyHandler>()
            .AddSingleton <IMasterCommunication, MasterCommunication>()
            .AddSingleton <IManager, ApplicationManager>()
            .AddSingleton(apiClient)
            .AddSingleton(_serviceProvider =>
            {
                var config = _serviceProvider.GetRequiredService <IConfigurationHandler <ApplicationConfiguration> >().Configuration();
                return(Localization.Configure.Initialize(useLocalTranslation: config?.UseLocalTranslations ?? false,
                                                         apiInstance: _serviceProvider.GetRequiredService <IMasterApi>(),
                                                         customLocale: config?.EnableCustomLocale ?? false ? (config.CustomLocale ?? "en-US") : "en-US"));
            });

            if (args.Contains("serialevents"))
            {
                serviceCollection.AddSingleton <IEventHandler, SerialGameEventHandler>();
            }
            else
            {
                serviceCollection.AddSingleton <IEventHandler, GameEventHandler>();
            }

            // register the native commands
            foreach (var commandType in typeof(SharedLibraryCore.Commands.QuitCommand).Assembly.GetTypes()
                     .Where(_command => _command.BaseType == typeof(Command)))
            {
                defaultLogger.WriteInfo($"Registered native command type {commandType.Name}");
                serviceCollection.AddSingleton(typeof(IManagerCommand), commandType);
            }

            // register the plugin implementations
            var pluginImplementations = pluginImporter.DiscoverAssemblyPluginImplementations();

            foreach (var pluginType in pluginImplementations.Item1)
            {
                defaultLogger.WriteInfo($"Registered plugin type {pluginType.FullName}");
                serviceCollection.AddSingleton(typeof(IPlugin), pluginType);
            }

            // register the plugin commands
            foreach (var commandType in pluginImplementations.Item2)
            {
                defaultLogger.WriteInfo($"Registered plugin command type {commandType.FullName}");
                serviceCollection.AddSingleton(typeof(IManagerCommand), commandType);
            }

            // register any script plugins
            foreach (var scriptPlugin in pluginImporter.DiscoverScriptPlugins())
            {
                serviceCollection.AddSingleton(scriptPlugin);
            }

            // register any eventable types
            foreach (var assemblyType in typeof(Program).Assembly.GetTypes()
                     .Where(_asmType => typeof(IRegisterEvent).IsAssignableFrom(_asmType))
                     .Union(pluginImplementations
                            .Item1.SelectMany(_asm => _asm.Assembly.GetTypes())
                            .Distinct()
                            .Where(_asmType => typeof(IRegisterEvent).IsAssignableFrom(_asmType))))
            {
                var instance = Activator.CreateInstance(assemblyType) as IRegisterEvent;
                serviceCollection.AddSingleton(instance);
            }

            return(serviceCollection);
        }