fix issue with configuration on new install

This commit is contained in:
RaidMax 2022-01-27 13:37:38 -06:00
parent f554536b95
commit 8649b0efe9

View File

@ -41,9 +41,9 @@ namespace IW4MAdmin.Application
public class Program public class Program
{ {
public static BuildNumber Version { get; } = BuildNumber.Parse(Utilities.GetVersionAsString()); public static BuildNumber Version { get; } = BuildNumber.Parse(Utilities.GetVersionAsString());
public static ApplicationManager ServerManager; private static ApplicationManager _serverManager;
private static Task ApplicationTask; private static Task _applicationTask;
private static ServiceProvider serviceProvider; private static ServiceProvider _serviceProvider;
/// <summary> /// <summary>
/// entrypoint of the application /// entrypoint of the application
@ -75,10 +75,10 @@ namespace IW4MAdmin.Application
/// <param name="e"></param> /// <param name="e"></param>
private static async void OnCancelKey(object sender, ConsoleCancelEventArgs e) private static async void OnCancelKey(object sender, ConsoleCancelEventArgs e)
{ {
ServerManager?.Stop(); _serverManager?.Stop();
if (ApplicationTask != null) if (_applicationTask != null)
{ {
await ApplicationTask; await _applicationTask;
} }
} }
@ -92,7 +92,6 @@ namespace IW4MAdmin.Application
ITranslationLookup translationLookup = null; ITranslationLookup translationLookup = null;
var logger = BuildDefaultLogger<Program>(new ApplicationConfiguration()); var logger = BuildDefaultLogger<Program>(new ApplicationConfiguration());
Utilities.DefaultLogger = logger; Utilities.DefaultLogger = logger;
IServiceCollection services = null;
logger.LogInformation("Begin IW4MAdmin startup. Version is {Version} {@Args}", Version, args); logger.LogInformation("Begin IW4MAdmin startup. Version is {Version} {@Args}", Version, args);
try try
@ -102,18 +101,18 @@ namespace IW4MAdmin.Application
ConfigurationMigration.CheckDirectories(); ConfigurationMigration.CheckDirectories();
ConfigurationMigration.RemoveObsoletePlugins20210322(); ConfigurationMigration.RemoveObsoletePlugins20210322();
logger.LogDebug("Configuring services..."); logger.LogDebug("Configuring services...");
services = ConfigureServices(args); var services = await ConfigureServices(args);
serviceProvider = services.BuildServiceProvider(); _serviceProvider = services.BuildServiceProvider();
var versionChecker = serviceProvider.GetRequiredService<IMasterCommunication>(); var versionChecker = _serviceProvider.GetRequiredService<IMasterCommunication>();
ServerManager = (ApplicationManager) serviceProvider.GetRequiredService<IManager>(); _serverManager = (ApplicationManager) _serviceProvider.GetRequiredService<IManager>();
translationLookup = serviceProvider.GetRequiredService<ITranslationLookup>(); translationLookup = _serviceProvider.GetRequiredService<ITranslationLookup>();
ApplicationTask = RunApplicationTasksAsync(logger, services); _applicationTask = RunApplicationTasksAsync(logger, services);
var tasks = new[] var tasks = new[]
{ {
versionChecker.CheckVersion(), versionChecker.CheckVersion(),
ServerManager.Init(), _serverManager.Init(),
ApplicationTask _applicationTask
}; };
await Task.WhenAll(tasks); await Task.WhenAll(tasks);
@ -160,12 +159,12 @@ namespace IW4MAdmin.Application
return; return;
} }
if (ServerManager.IsRestartRequested) if (_serverManager.IsRestartRequested)
{ {
goto restart; goto restart;
} }
await serviceProvider.DisposeAsync(); await _serviceProvider.DisposeAsync();
} }
/// <summary> /// <summary>
@ -174,11 +173,11 @@ namespace IW4MAdmin.Application
/// <returns></returns> /// <returns></returns>
private static async Task RunApplicationTasksAsync(ILogger logger, IServiceCollection services) private static async Task RunApplicationTasksAsync(ILogger logger, IServiceCollection services)
{ {
var webfrontTask = ServerManager.GetApplicationSettings().Configuration().EnableWebFront var webfrontTask = _serverManager.GetApplicationSettings().Configuration().EnableWebFront
? WebfrontCore.Program.Init(ServerManager, serviceProvider, services, ServerManager.CancellationToken) ? WebfrontCore.Program.Init(_serverManager, _serviceProvider, services, _serverManager.CancellationToken)
: Task.CompletedTask; : Task.CompletedTask;
var collectionService = serviceProvider.GetRequiredService<IServerDataCollector>(); var collectionService = _serviceProvider.GetRequiredService<IServerDataCollector>();
// we want to run this one on a manual thread instead of letting the thread pool handle it, // we want to run this one on a manual thread instead of letting the thread pool handle it,
// because we can't exit early from waiting on console input, and it prevents us from restarting // because we can't exit early from waiting on console input, and it prevents us from restarting
@ -190,10 +189,10 @@ namespace IW4MAdmin.Application
var tasks = new[] var tasks = new[]
{ {
webfrontTask, webfrontTask,
ServerManager.Start(), _serverManager.Start(),
serviceProvider.GetRequiredService<IMasterCommunication>() _serviceProvider.GetRequiredService<IMasterCommunication>()
.RunUploadStatus(ServerManager.CancellationToken), .RunUploadStatus(_serverManager.CancellationToken),
collectionService.BeginCollectionAsync(cancellationToken: ServerManager.CancellationToken) collectionService.BeginCollectionAsync(cancellationToken: _serverManager.CancellationToken)
}; };
logger.LogDebug("Starting webfront and input tasks"); logger.LogDebug("Starting webfront and input tasks");
@ -215,14 +214,19 @@ namespace IW4MAdmin.Application
return; return;
} }
string lastCommand;
EFClient origin = null; EFClient origin = null;
try try
{ {
while (!ServerManager.CancellationToken.IsCancellationRequested) while (!_serverManager.CancellationToken.IsCancellationRequested)
{ {
lastCommand = await Console.In.ReadLineAsync(); if (!_serverManager.IsInitialized)
{
await Task.Delay(1000);
continue;
}
var lastCommand = await Console.In.ReadLineAsync();
if (lastCommand == null) if (lastCommand == null)
{ {
@ -238,12 +242,12 @@ namespace IW4MAdmin.Application
{ {
Type = GameEvent.EventType.Command, Type = GameEvent.EventType.Command,
Data = lastCommand, Data = lastCommand,
Origin = origin ??= Utilities.IW4MAdminClient(ServerManager.Servers.FirstOrDefault()), Origin = origin ??= Utilities.IW4MAdminClient(_serverManager.Servers.FirstOrDefault()),
Owner = ServerManager.Servers[0] Owner = _serverManager.Servers[0]
}; };
ServerManager.AddEvent(gameEvent); _serverManager.AddEvent(gameEvent);
await gameEvent.WaitAsync(Utilities.DefaultCommandTimeout, ServerManager.CancellationToken); await gameEvent.WaitAsync(Utilities.DefaultCommandTimeout, _serverManager.CancellationToken);
Console.Write('>'); Console.Write('>');
} }
} }
@ -273,9 +277,9 @@ namespace IW4MAdmin.Application
// register the native commands // register the native commands
foreach (var commandType in typeof(SharedLibraryCore.Commands.QuitCommand).Assembly.GetTypes() foreach (var commandType in typeof(SharedLibraryCore.Commands.QuitCommand).Assembly.GetTypes()
.Concat(typeof(Program).Assembly.GetTypes().Where(type => type.Namespace == "IW4MAdmin.Application.Commands")) .Concat(typeof(Program).Assembly.GetTypes().Where(type => type.Namespace == "IW4MAdmin.Application.Commands"))
.Where(_command => _command.BaseType == typeof(Command))) .Where(command => command.BaseType == typeof(Command)))
{ {
defaultLogger.LogDebug("Registered native command type {name}", commandType.Name); defaultLogger.LogDebug("Registered native command type {Name}", commandType.Name);
serviceCollection.AddSingleton(typeof(IManagerCommand), commandType); serviceCollection.AddSingleton(typeof(IManagerCommand), commandType);
} }
@ -283,23 +287,23 @@ namespace IW4MAdmin.Application
var (plugins, commands, configurations) = pluginImporter.DiscoverAssemblyPluginImplementations(); var (plugins, commands, configurations) = pluginImporter.DiscoverAssemblyPluginImplementations();
foreach (var pluginType in plugins) foreach (var pluginType in plugins)
{ {
defaultLogger.LogDebug("Registered plugin type {name}", pluginType.FullName); defaultLogger.LogDebug("Registered plugin type {Name}", pluginType.FullName);
serviceCollection.AddSingleton(typeof(IPlugin), pluginType); serviceCollection.AddSingleton(typeof(IPlugin), pluginType);
} }
// register the plugin commands // register the plugin commands
foreach (var commandType in commands) foreach (var commandType in commands)
{ {
defaultLogger.LogDebug("Registered plugin command type {name}", commandType.FullName); defaultLogger.LogDebug("Registered plugin command type {Name}", commandType.FullName);
serviceCollection.AddSingleton(typeof(IManagerCommand), commandType); serviceCollection.AddSingleton(typeof(IManagerCommand), commandType);
} }
foreach (var configurationType in configurations) foreach (var configurationType in configurations)
{ {
defaultLogger.LogDebug("Registered plugin config type {name}", configurationType.Name); defaultLogger.LogDebug("Registered plugin config type {Name}", configurationType.Name);
var configInstance = (IBaseConfiguration) Activator.CreateInstance(configurationType); var configInstance = (IBaseConfiguration) Activator.CreateInstance(configurationType);
var handlerType = typeof(BaseConfigurationHandler<>).MakeGenericType(configurationType); var handlerType = typeof(BaseConfigurationHandler<>).MakeGenericType(configurationType);
var handlerInstance = Activator.CreateInstance(handlerType, new[] {configInstance.Name()}); var handlerInstance = Activator.CreateInstance(handlerType, configInstance.Name());
var genericInterfaceType = typeof(IConfigurationHandler<>).MakeGenericType(configurationType); var genericInterfaceType = typeof(IConfigurationHandler<>).MakeGenericType(configurationType);
serviceCollection.AddSingleton(genericInterfaceType, handlerInstance); serviceCollection.AddSingleton(genericInterfaceType, handlerInstance);
@ -313,10 +317,10 @@ namespace IW4MAdmin.Application
// register any eventable types // register any eventable types
foreach (var assemblyType in typeof(Program).Assembly.GetTypes() foreach (var assemblyType in typeof(Program).Assembly.GetTypes()
.Where(_asmType => typeof(IRegisterEvent).IsAssignableFrom(_asmType)) .Where(asmType => typeof(IRegisterEvent).IsAssignableFrom(asmType))
.Union(plugins.SelectMany(_asm => _asm.Assembly.GetTypes()) .Union(plugins.SelectMany(asm => asm.Assembly.GetTypes())
.Distinct() .Distinct()
.Where(_asmType => typeof(IRegisterEvent).IsAssignableFrom(_asmType)))) .Where(asmType => typeof(IRegisterEvent).IsAssignableFrom(asmType))))
{ {
var instance = Activator.CreateInstance(assemblyType) as IRegisterEvent; var instance = Activator.CreateInstance(assemblyType) as IRegisterEvent;
serviceCollection.AddSingleton(instance); serviceCollection.AddSingleton(instance);
@ -329,7 +333,7 @@ namespace IW4MAdmin.Application
/// <summary> /// <summary>
/// Configures the dependency injection services /// Configures the dependency injection services
/// </summary> /// </summary>
private static IServiceCollection ConfigureServices(string[] args) private static async Task<IServiceCollection> ConfigureServices(string[] args)
{ {
// todo: this is a quick fix // todo: this is a quick fix
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
@ -355,7 +359,7 @@ namespace IW4MAdmin.Application
{ {
appConfig = (ApplicationConfiguration) new ApplicationConfiguration().Generate(); appConfig = (ApplicationConfiguration) new ApplicationConfiguration().Generate();
appConfigHandler.Set(appConfig); appConfigHandler.Set(appConfig);
appConfigHandler.Save().RunSynchronously(); await appConfigHandler.Save();
} }
// register override level names // register override level names
@ -373,15 +377,15 @@ namespace IW4MAdmin.Application
serviceCollection serviceCollection
.AddBaseLogger(appConfig) .AddBaseLogger(appConfig)
.AddSingleton(defaultConfig) .AddSingleton(defaultConfig)
.AddSingleton<IServiceCollection>(_serviceProvider => serviceCollection) .AddSingleton<IServiceCollection>(serviceCollection)
.AddSingleton<IConfigurationHandler<DefaultSettings>, BaseConfigurationHandler<DefaultSettings>>() .AddSingleton<IConfigurationHandler<DefaultSettings>, BaseConfigurationHandler<DefaultSettings>>()
.AddSingleton((IConfigurationHandler<ApplicationConfiguration>) appConfigHandler) .AddSingleton((IConfigurationHandler<ApplicationConfiguration>) appConfigHandler)
.AddSingleton( .AddSingleton(
new BaseConfigurationHandler<CommandConfiguration>("CommandConfiguration") as new BaseConfigurationHandler<CommandConfiguration>("CommandConfiguration") as
IConfigurationHandler<CommandConfiguration>) IConfigurationHandler<CommandConfiguration>)
.AddSingleton(appConfig) .AddSingleton(appConfig)
.AddSingleton(_serviceProvider => .AddSingleton(serviceProvider =>
_serviceProvider.GetRequiredService<IConfigurationHandler<CommandConfiguration>>() serviceProvider.GetRequiredService<IConfigurationHandler<CommandConfiguration>>()
.Configuration() ?? new CommandConfiguration()) .Configuration() ?? new CommandConfiguration())
.AddSingleton<IPluginImporter, PluginImporter>() .AddSingleton<IPluginImporter, PluginImporter>()
.AddSingleton<IMiddlewareActionHandler, MiddlewareActionHandler>() .AddSingleton<IMiddlewareActionHandler, MiddlewareActionHandler>()