2020-06-30 17:39:32 -04:00
|
|
|
|
using IW4MAdmin.Application.API.Master;
|
|
|
|
|
using IW4MAdmin.Application.EventParsers;
|
2020-04-01 15:11:56 -04:00
|
|
|
|
using IW4MAdmin.Application.Factories;
|
2020-08-17 22:21:11 -04:00
|
|
|
|
using IW4MAdmin.Application.Meta;
|
2020-01-31 21:15:07 -05:00
|
|
|
|
using IW4MAdmin.Application.Migration;
|
2020-01-13 21:06:57 -05:00
|
|
|
|
using IW4MAdmin.Application.Misc;
|
2019-12-02 16:52:36 -05:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-06-30 17:39:32 -04:00
|
|
|
|
using RestEase;
|
2018-04-09 15:17:10 -04:00
|
|
|
|
using SharedLibraryCore;
|
2020-01-26 19:06:50 -05:00
|
|
|
|
using SharedLibraryCore.Configuration;
|
2020-08-17 22:21:11 -04:00
|
|
|
|
using SharedLibraryCore.Database.Models;
|
|
|
|
|
using SharedLibraryCore.Dtos.Meta.Responses;
|
2020-01-17 18:31:53 -05:00
|
|
|
|
using SharedLibraryCore.Exceptions;
|
2020-01-11 21:32:27 -05:00
|
|
|
|
using SharedLibraryCore.Helpers;
|
2019-12-02 16:52:36 -05:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2020-08-17 22:21:11 -04:00
|
|
|
|
using SharedLibraryCore.QueryHelper;
|
2020-04-28 17:48:06 -04:00
|
|
|
|
using SharedLibraryCore.Repositories;
|
2020-08-17 22:21:11 -04:00
|
|
|
|
using SharedLibraryCore.Services;
|
2020-08-20 11:38:11 -04:00
|
|
|
|
using Stats.Dtos;
|
2019-01-27 20:45:35 -05:00
|
|
|
|
using System;
|
2022-02-15 21:16:21 -05:00
|
|
|
|
using System.IO;
|
2020-01-26 19:06:50 -05:00
|
|
|
|
using System.Linq;
|
2021-10-30 20:42:07 -04:00
|
|
|
|
using System.Net.Http;
|
2018-04-23 01:43:48 -04:00
|
|
|
|
using System.Text;
|
2019-05-17 10:02:09 -04:00
|
|
|
|
using System.Threading;
|
2019-01-27 20:45:35 -05:00
|
|
|
|
using System.Threading.Tasks;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using Data.Abstractions;
|
|
|
|
|
using Data.Helpers;
|
2021-06-03 11:51:03 -04:00
|
|
|
|
using Integrations.Source.Extensions;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using IW4MAdmin.Application.Extensions;
|
|
|
|
|
using IW4MAdmin.Application.Localization;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using IW4MAdmin.Plugins.Stats.Client.Abstractions;
|
|
|
|
|
using IW4MAdmin.Plugins.Stats.Client;
|
|
|
|
|
using Stats.Client.Abstractions;
|
|
|
|
|
using Stats.Client;
|
2022-01-28 18:28:49 -05:00
|
|
|
|
using Stats.Config;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using Stats.Helpers;
|
2018-04-09 15:17:10 -04:00
|
|
|
|
|
2018-04-08 14:48:40 -04:00
|
|
|
|
namespace IW4MAdmin.Application
|
2015-03-08 17:20:10 -04:00
|
|
|
|
{
|
2018-02-21 20:29:23 -05:00
|
|
|
|
public class Program
|
2015-03-08 17:20:10 -04:00
|
|
|
|
{
|
2022-01-26 15:42:23 -05:00
|
|
|
|
public static BuildNumber Version { get; } = BuildNumber.Parse(Utilities.GetVersionAsString());
|
2022-01-27 14:37:38 -05:00
|
|
|
|
private static ApplicationManager _serverManager;
|
|
|
|
|
private static Task _applicationTask;
|
|
|
|
|
private static ServiceProvider _serviceProvider;
|
2019-05-08 21:34:17 -04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// entrypoint of the application
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2020-05-04 17:50:02 -04:00
|
|
|
|
public static async Task Main(string[] args)
|
2015-03-08 17:20:10 -04:00
|
|
|
|
{
|
2018-12-31 21:52:19 -05:00
|
|
|
|
AppDomain.CurrentDomain.SetData("DataDirectory", Utilities.OperatingDirectory);
|
2019-05-08 21:34:17 -04:00
|
|
|
|
|
2018-04-23 01:43:48 -04:00
|
|
|
|
Console.OutputEncoding = Encoding.UTF8;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
Console.ForegroundColor = ConsoleColor.Gray;
|
2017-11-19 01:44:11 -05:00
|
|
|
|
|
2022-01-26 15:42:23 -05:00
|
|
|
|
Console.CancelKeyPress += OnCancelKey;
|
2015-08-17 16:38:42 -04:00
|
|
|
|
|
2015-03-09 00:28:57 -04:00
|
|
|
|
Console.WriteLine("=====================================================");
|
2019-05-08 21:34:17 -04:00
|
|
|
|
Console.WriteLine(" IW4MAdmin");
|
2015-03-09 00:28:57 -04:00
|
|
|
|
Console.WriteLine(" by RaidMax ");
|
2018-09-13 15:34:42 -04:00
|
|
|
|
Console.WriteLine($" Version {Utilities.GetVersionAsString()}");
|
2015-03-09 00:28:57 -04:00
|
|
|
|
Console.WriteLine("=====================================================");
|
2015-07-06 15:51:08 -04:00
|
|
|
|
|
2020-05-04 17:50:02 -04:00
|
|
|
|
await LaunchAsync(args);
|
2019-05-08 21:34:17 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// event callback executed when the control + c combination is detected
|
|
|
|
|
/// gracefully stops the server manager and waits for all tasks to finish
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private static async void OnCancelKey(object sender, ConsoleCancelEventArgs e)
|
|
|
|
|
{
|
2022-03-24 12:34:32 -04:00
|
|
|
|
if (_serverManager is not null)
|
|
|
|
|
{
|
|
|
|
|
await _serverManager.Stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_applicationTask is not null)
|
2020-12-16 14:11:30 -05:00
|
|
|
|
{
|
2022-01-27 14:37:38 -05:00
|
|
|
|
await _applicationTask;
|
2020-12-16 14:11:30 -05:00
|
|
|
|
}
|
2019-05-08 21:34:17 -04:00
|
|
|
|
}
|
2018-05-05 16:36:26 -04:00
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// task that initializes application and starts the application monitoring and runtime tasks
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2020-05-04 17:50:02 -04:00
|
|
|
|
private static async Task LaunchAsync(string[] args)
|
2019-05-08 21:34:17 -04:00
|
|
|
|
{
|
2021-03-22 12:09:25 -04:00
|
|
|
|
restart:
|
2020-01-31 21:15:07 -05:00
|
|
|
|
ITranslationLookup translationLookup = null;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
var logger = BuildDefaultLogger<Program>(new ApplicationConfiguration());
|
|
|
|
|
Utilities.DefaultLogger = logger;
|
2022-01-26 15:42:23 -05:00
|
|
|
|
logger.LogInformation("Begin IW4MAdmin startup. Version is {Version} {@Args}", Version, args);
|
|
|
|
|
|
2017-05-26 18:49:27 -04:00
|
|
|
|
try
|
2015-07-06 15:51:08 -04:00
|
|
|
|
{
|
2020-02-03 09:21:42 -05:00
|
|
|
|
// do any needed housekeeping file/folder migrations
|
|
|
|
|
ConfigurationMigration.MoveConfigFolder10518(null);
|
|
|
|
|
ConfigurationMigration.CheckDirectories();
|
2021-03-22 12:46:32 -04:00
|
|
|
|
ConfigurationMigration.RemoveObsoletePlugins20210322();
|
2020-11-11 18:31:26 -05:00
|
|
|
|
logger.LogDebug("Configuring services...");
|
2022-01-27 14:37:38 -05:00
|
|
|
|
var services = await ConfigureServices(args);
|
|
|
|
|
_serviceProvider = services.BuildServiceProvider();
|
|
|
|
|
var versionChecker = _serviceProvider.GetRequiredService<IMasterCommunication>();
|
|
|
|
|
_serverManager = (ApplicationManager) _serviceProvider.GetRequiredService<IManager>();
|
|
|
|
|
translationLookup = _serviceProvider.GetRequiredService<ITranslationLookup>();
|
2020-01-31 21:15:07 -05:00
|
|
|
|
|
2022-01-27 14:37:38 -05:00
|
|
|
|
_applicationTask = RunApplicationTasksAsync(logger, services);
|
2022-01-26 15:42:23 -05:00
|
|
|
|
var tasks = new[]
|
|
|
|
|
{
|
|
|
|
|
versionChecker.CheckVersion(),
|
2022-01-27 14:37:38 -05:00
|
|
|
|
_applicationTask
|
2022-01-26 15:42:23 -05:00
|
|
|
|
};
|
|
|
|
|
|
2022-02-15 21:23:16 -05:00
|
|
|
|
await _serverManager.Init();
|
|
|
|
|
|
2022-01-26 15:42:23 -05:00
|
|
|
|
await Task.WhenAll(tasks);
|
2019-05-08 21:34:17 -04:00
|
|
|
|
}
|
2018-05-08 00:58:46 -04:00
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2022-01-26 15:42:23 -05:00
|
|
|
|
var failMessage = translationLookup == null
|
2021-03-22 12:09:25 -04:00
|
|
|
|
? "Failed to initialize IW4MAdmin"
|
|
|
|
|
: translationLookup["MANAGER_INIT_FAIL"];
|
2022-01-26 15:42:23 -05:00
|
|
|
|
var exitMessage = translationLookup == null
|
2021-03-22 12:09:25 -04:00
|
|
|
|
? "Press enter to exit..."
|
|
|
|
|
: translationLookup["MANAGER_EXIT"];
|
2018-04-19 01:48:14 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
logger.LogCritical(e, "Failed to initialize IW4MAdmin");
|
2019-05-08 21:34:17 -04:00
|
|
|
|
Console.WriteLine(failMessage);
|
|
|
|
|
|
|
|
|
|
while (e.InnerException != null)
|
2018-04-19 01:48:14 -04:00
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
e = e.InnerException;
|
2018-04-19 01:48:14 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-31 21:15:07 -05:00
|
|
|
|
if (e is ConfigurationException configException)
|
2020-01-17 18:31:53 -05:00
|
|
|
|
{
|
2022-02-15 21:16:21 -05:00
|
|
|
|
Console.WriteLine("{{fileName}} contains an error."
|
|
|
|
|
.FormatExt(Path.GetFileName(configException.ConfigurationFileName)));
|
2020-01-31 21:15:07 -05:00
|
|
|
|
|
2022-01-26 15:42:23 -05:00
|
|
|
|
foreach (var error in configException.Errors)
|
2020-01-17 18:31:53 -05:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-31 21:15:07 -05:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(e.Message);
|
|
|
|
|
}
|
2022-03-24 12:34:32 -04:00
|
|
|
|
|
|
|
|
|
if (_serverManager is not null)
|
|
|
|
|
{
|
|
|
|
|
await _serverManager?.Stop();
|
|
|
|
|
}
|
2020-01-31 21:15:07 -05:00
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
Console.WriteLine(exitMessage);
|
2020-04-01 15:11:56 -04:00
|
|
|
|
await Console.In.ReadAsync(new char[1], 0, 1);
|
2019-05-09 21:00:00 -04:00
|
|
|
|
return;
|
2019-05-08 21:34:17 -04:00
|
|
|
|
}
|
2018-04-19 01:48:14 -04:00
|
|
|
|
|
2022-01-27 14:37:38 -05:00
|
|
|
|
if (_serverManager.IsRestartRequested)
|
2019-05-08 21:34:17 -04:00
|
|
|
|
{
|
|
|
|
|
goto restart;
|
|
|
|
|
}
|
2020-01-31 21:15:07 -05:00
|
|
|
|
|
2022-01-27 14:37:38 -05:00
|
|
|
|
await _serviceProvider.DisposeAsync();
|
2019-05-08 21:34:17 -04:00
|
|
|
|
}
|
2018-04-19 01:48:14 -04:00
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// runs the core application tasks
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2020-11-27 22:52:52 -05:00
|
|
|
|
private static async Task RunApplicationTasksAsync(ILogger logger, IServiceCollection services)
|
2019-05-08 21:34:17 -04:00
|
|
|
|
{
|
2022-01-27 14:37:38 -05:00
|
|
|
|
var webfrontTask = _serverManager.GetApplicationSettings().Configuration().EnableWebFront
|
|
|
|
|
? WebfrontCore.Program.Init(_serverManager, _serviceProvider, services, _serverManager.CancellationToken)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
: Task.CompletedTask;
|
2018-02-21 20:29:23 -05:00
|
|
|
|
|
2022-01-27 14:37:38 -05:00
|
|
|
|
var collectionService = _serviceProvider.GetRequiredService<IServerDataCollector>();
|
2021-08-26 18:35:05 -04:00
|
|
|
|
|
2019-05-17 10:02:09 -04:00
|
|
|
|
// 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
|
2022-01-26 15:42:23 -05:00
|
|
|
|
async void ReadInput() => await ReadConsoleInput(logger);
|
|
|
|
|
|
|
|
|
|
var inputThread = new Thread(ReadInput);
|
2019-05-17 10:02:09 -04:00
|
|
|
|
inputThread.Start();
|
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
var tasks = new[]
|
|
|
|
|
{
|
|
|
|
|
webfrontTask,
|
2022-01-27 14:37:38 -05:00
|
|
|
|
_serverManager.Start(),
|
|
|
|
|
_serviceProvider.GetRequiredService<IMasterCommunication>()
|
|
|
|
|
.RunUploadStatus(_serverManager.CancellationToken),
|
|
|
|
|
collectionService.BeginCollectionAsync(cancellationToken: _serverManager.CancellationToken)
|
2019-05-08 21:34:17 -04:00
|
|
|
|
};
|
2015-08-22 02:04:30 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
logger.LogDebug("Starting webfront and input tasks");
|
2019-05-08 21:34:17 -04:00
|
|
|
|
await Task.WhenAll(tasks);
|
2017-06-12 13:50:00 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
logger.LogInformation("Shutdown completed successfully");
|
2021-01-24 12:47:19 -05:00
|
|
|
|
Console.WriteLine(Utilities.CurrentLocalization.LocalizationIndex["MANAGER_SHUTDOWN_SUCCESS"]);
|
2019-05-08 21:34:17 -04:00
|
|
|
|
}
|
2022-01-26 15:42:23 -05:00
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// reads input from the console and executes entered commands on the default server
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2020-11-11 18:31:26 -05:00
|
|
|
|
private static async Task ReadConsoleInput(ILogger logger)
|
2019-05-08 21:34:17 -04:00
|
|
|
|
{
|
2020-06-03 20:45:06 -04:00
|
|
|
|
if (Console.IsInputRedirected)
|
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
logger.LogInformation("Disabling console input as it has been redirected");
|
2020-06-03 20:45:06 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-26 15:42:23 -05:00
|
|
|
|
EFClient origin = null;
|
2018-10-05 23:10:39 -04:00
|
|
|
|
|
2019-05-31 11:17:01 -04:00
|
|
|
|
try
|
2018-10-05 23:10:39 -04:00
|
|
|
|
{
|
2022-01-27 14:37:38 -05:00
|
|
|
|
while (!_serverManager.CancellationToken.IsCancellationRequested)
|
2019-05-08 21:34:17 -04:00
|
|
|
|
{
|
2022-01-27 14:37:38 -05:00
|
|
|
|
if (!_serverManager.IsInitialized)
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(1000);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var lastCommand = await Console.In.ReadLineAsync();
|
2019-05-31 11:17:01 -04:00
|
|
|
|
|
2022-01-26 15:42:23 -05:00
|
|
|
|
if (lastCommand == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!lastCommand.Any())
|
2019-05-08 21:34:17 -04:00
|
|
|
|
{
|
2022-01-26 15:42:23 -05:00
|
|
|
|
continue;
|
2019-05-08 21:34:17 -04:00
|
|
|
|
}
|
2022-01-26 15:42:23 -05:00
|
|
|
|
|
|
|
|
|
var gameEvent = new GameEvent
|
|
|
|
|
{
|
|
|
|
|
Type = GameEvent.EventType.Command,
|
|
|
|
|
Data = lastCommand,
|
2022-01-27 14:37:38 -05:00
|
|
|
|
Origin = origin ??= Utilities.IW4MAdminClient(_serverManager.Servers.FirstOrDefault()),
|
|
|
|
|
Owner = _serverManager.Servers[0]
|
2022-01-26 15:42:23 -05:00
|
|
|
|
};
|
|
|
|
|
|
2022-01-27 14:37:38 -05:00
|
|
|
|
_serverManager.AddEvent(gameEvent);
|
|
|
|
|
await gameEvent.WaitAsync(Utilities.DefaultCommandTimeout, _serverManager.CancellationToken);
|
2022-01-26 15:42:23 -05:00
|
|
|
|
Console.Write('>');
|
2019-05-08 21:34:17 -04:00
|
|
|
|
}
|
2018-10-05 23:10:39 -04:00
|
|
|
|
}
|
2019-05-31 11:17:01 -04:00
|
|
|
|
catch (OperationCanceledException)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
|
|
|
|
}
|
2015-08-17 16:38:42 -04:00
|
|
|
|
}
|
2019-12-02 16:52:36 -05:00
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
private static IServiceCollection HandlePluginRegistration(ApplicationConfiguration appConfig,
|
|
|
|
|
IServiceCollection serviceCollection,
|
2020-11-11 18:31:26 -05:00
|
|
|
|
IMasterApi masterApi)
|
|
|
|
|
{
|
|
|
|
|
var defaultLogger = BuildDefaultLogger<Program>(appConfig);
|
|
|
|
|
var pluginServiceProvider = new ServiceCollection()
|
|
|
|
|
.AddBaseLogger(appConfig)
|
|
|
|
|
.AddSingleton(appConfig)
|
|
|
|
|
.AddSingleton(masterApi)
|
|
|
|
|
.AddSingleton<IRemoteAssemblyHandler, RemoteAssemblyHandler>()
|
|
|
|
|
.AddSingleton<IPluginImporter, PluginImporter>()
|
|
|
|
|
.BuildServiceProvider();
|
|
|
|
|
|
|
|
|
|
var pluginImporter = pluginServiceProvider.GetRequiredService<IPluginImporter>();
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
// we need to register the rest client with regular collection
|
|
|
|
|
serviceCollection.AddSingleton(masterApi);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
// register the native commands
|
|
|
|
|
foreach (var commandType in typeof(SharedLibraryCore.Commands.QuitCommand).Assembly.GetTypes()
|
2022-03-23 09:43:57 -04:00
|
|
|
|
.Concat(typeof(Program).Assembly.GetTypes().Where(type => type.Namespace?.StartsWith("IW4MAdmin.Application.Commands") ?? false))
|
2022-01-27 14:37:38 -05:00
|
|
|
|
.Where(command => command.BaseType == typeof(Command)))
|
2020-11-11 18:31:26 -05:00
|
|
|
|
{
|
2022-01-27 14:37:38 -05:00
|
|
|
|
defaultLogger.LogDebug("Registered native command type {Name}", commandType.Name);
|
2020-11-11 18:31:26 -05:00
|
|
|
|
serviceCollection.AddSingleton(typeof(IManagerCommand), commandType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// register the plugin implementations
|
2021-03-22 12:09:25 -04:00
|
|
|
|
var (plugins, commands, configurations) = pluginImporter.DiscoverAssemblyPluginImplementations();
|
|
|
|
|
foreach (var pluginType in plugins)
|
2020-11-11 18:31:26 -05:00
|
|
|
|
{
|
2022-01-27 14:37:38 -05:00
|
|
|
|
defaultLogger.LogDebug("Registered plugin type {Name}", pluginType.FullName);
|
2020-11-11 18:31:26 -05:00
|
|
|
|
serviceCollection.AddSingleton(typeof(IPlugin), pluginType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// register the plugin commands
|
2021-03-22 12:09:25 -04:00
|
|
|
|
foreach (var commandType in commands)
|
2020-11-11 18:31:26 -05:00
|
|
|
|
{
|
2022-01-27 14:37:38 -05:00
|
|
|
|
defaultLogger.LogDebug("Registered plugin command type {Name}", commandType.FullName);
|
2020-11-11 18:31:26 -05:00
|
|
|
|
serviceCollection.AddSingleton(typeof(IManagerCommand), commandType);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
foreach (var configurationType in configurations)
|
|
|
|
|
{
|
2022-01-27 14:37:38 -05:00
|
|
|
|
defaultLogger.LogDebug("Registered plugin config type {Name}", configurationType.Name);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
var configInstance = (IBaseConfiguration) Activator.CreateInstance(configurationType);
|
|
|
|
|
var handlerType = typeof(BaseConfigurationHandler<>).MakeGenericType(configurationType);
|
2022-01-27 14:37:38 -05:00
|
|
|
|
var handlerInstance = Activator.CreateInstance(handlerType, configInstance.Name());
|
2021-03-22 12:09:25 -04:00
|
|
|
|
var genericInterfaceType = typeof(IConfigurationHandler<>).MakeGenericType(configurationType);
|
|
|
|
|
|
|
|
|
|
serviceCollection.AddSingleton(genericInterfaceType, handlerInstance);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
// register any script plugins
|
2022-02-09 15:45:28 -05:00
|
|
|
|
foreach (var plugin in pluginImporter.DiscoverScriptPlugins())
|
2022-02-07 23:02:50 -05:00
|
|
|
|
{
|
2022-02-09 15:45:28 -05:00
|
|
|
|
serviceCollection.AddSingleton(plugin);
|
2022-02-07 23:02:50 -05:00
|
|
|
|
}
|
2020-11-11 18:31:26 -05:00
|
|
|
|
|
|
|
|
|
// register any eventable types
|
|
|
|
|
foreach (var assemblyType in typeof(Program).Assembly.GetTypes()
|
2022-01-27 14:37:38 -05:00
|
|
|
|
.Where(asmType => typeof(IRegisterEvent).IsAssignableFrom(asmType))
|
|
|
|
|
.Union(plugins.SelectMany(asm => asm.Assembly.GetTypes())
|
2021-03-22 12:09:25 -04:00
|
|
|
|
.Distinct()
|
2022-01-27 14:37:38 -05:00
|
|
|
|
.Where(asmType => typeof(IRegisterEvent).IsAssignableFrom(asmType))))
|
2020-11-11 18:31:26 -05:00
|
|
|
|
{
|
|
|
|
|
var instance = Activator.CreateInstance(assemblyType) as IRegisterEvent;
|
|
|
|
|
serviceCollection.AddSingleton(instance);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return serviceCollection;
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
|
2020-01-13 21:06:57 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Configures the dependency injection services
|
|
|
|
|
/// </summary>
|
2022-01-27 14:37:38 -05:00
|
|
|
|
private static async Task<IServiceCollection> ConfigureServices(string[] args)
|
2019-12-02 16:52:36 -05:00
|
|
|
|
{
|
2022-01-27 11:14:38 -05:00
|
|
|
|
// todo: this is a quick fix
|
|
|
|
|
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
|
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
// setup the static resources (config/master api/translations)
|
|
|
|
|
var serviceCollection = new ServiceCollection();
|
2020-10-24 16:02:38 -04:00
|
|
|
|
var appConfigHandler = new BaseConfigurationHandler<ApplicationConfiguration>("IW4MAdminSettings");
|
2022-01-28 10:35:01 -05:00
|
|
|
|
await appConfigHandler.BuildAsync();
|
2021-07-09 17:50:33 -04:00
|
|
|
|
var defaultConfigHandler = new BaseConfigurationHandler<DefaultSettings>("DefaultSettings");
|
2022-01-28 10:35:01 -05:00
|
|
|
|
await defaultConfigHandler.BuildAsync();
|
|
|
|
|
var commandConfigHandler = new BaseConfigurationHandler<CommandConfiguration>("CommandConfiguration");
|
|
|
|
|
await commandConfigHandler.BuildAsync();
|
2022-03-08 12:24:59 -05:00
|
|
|
|
var statsCommandHandler = new BaseConfigurationHandler<StatsConfiguration>("StatsPluginSettings");
|
2022-01-28 18:28:49 -05:00
|
|
|
|
await statsCommandHandler.BuildAsync();
|
2021-07-09 17:50:33 -04:00
|
|
|
|
var defaultConfig = defaultConfigHandler.Configuration();
|
2020-10-24 16:02:38 -04:00
|
|
|
|
var appConfig = appConfigHandler.Configuration();
|
2020-11-11 18:31:26 -05:00
|
|
|
|
var masterUri = Utilities.IsDevelopment
|
|
|
|
|
? new Uri("http://127.0.0.1:8080")
|
|
|
|
|
: appConfig?.MasterUrl ?? new ApplicationConfiguration().MasterUrl;
|
2021-10-30 20:42:07 -04:00
|
|
|
|
var httpClient = new HttpClient
|
|
|
|
|
{
|
|
|
|
|
BaseAddress = masterUri,
|
|
|
|
|
Timeout = TimeSpan.FromSeconds(15)
|
|
|
|
|
};
|
|
|
|
|
var masterRestClient = RestClient.For<IMasterApi>(httpClient);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
var translationLookup = Configure.Initialize(Utilities.DefaultLogger, masterRestClient, appConfig);
|
2020-02-11 17:44:06 -05:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
if (appConfig == null)
|
|
|
|
|
{
|
|
|
|
|
appConfig = (ApplicationConfiguration) new ApplicationConfiguration().Generate();
|
|
|
|
|
appConfigHandler.Set(appConfig);
|
2022-01-27 14:37:38 -05:00
|
|
|
|
await appConfigHandler.Save();
|
2020-11-11 18:31:26 -05:00
|
|
|
|
}
|
2021-01-24 12:47:19 -05:00
|
|
|
|
|
|
|
|
|
// register override level names
|
|
|
|
|
foreach (var (key, value) in appConfig.OverridePermissionLevelNames)
|
|
|
|
|
{
|
|
|
|
|
if (!Utilities.PermissionLevelOverrides.ContainsKey(key))
|
|
|
|
|
{
|
|
|
|
|
Utilities.PermissionLevelOverrides.Add(key, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
// build the dependency list
|
|
|
|
|
HandlePluginRegistration(appConfig, serviceCollection, masterRestClient);
|
|
|
|
|
|
|
|
|
|
serviceCollection
|
|
|
|
|
.AddBaseLogger(appConfig)
|
2021-07-09 17:50:33 -04:00
|
|
|
|
.AddSingleton(defaultConfig)
|
2022-01-27 14:37:38 -05:00
|
|
|
|
.AddSingleton<IServiceCollection>(serviceCollection)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
.AddSingleton<IConfigurationHandler<DefaultSettings>, BaseConfigurationHandler<DefaultSettings>>()
|
2020-11-11 18:31:26 -05:00
|
|
|
|
.AddSingleton((IConfigurationHandler<ApplicationConfiguration>) appConfigHandler)
|
2022-01-28 10:35:01 -05:00
|
|
|
|
.AddSingleton<IConfigurationHandler<CommandConfiguration>>(commandConfigHandler)
|
2020-11-11 18:31:26 -05:00
|
|
|
|
.AddSingleton(appConfig)
|
2022-01-28 18:28:49 -05:00
|
|
|
|
.AddSingleton(statsCommandHandler.Configuration() ?? new StatsConfiguration())
|
2022-01-27 14:37:38 -05:00
|
|
|
|
.AddSingleton(serviceProvider =>
|
|
|
|
|
serviceProvider.GetRequiredService<IConfigurationHandler<CommandConfiguration>>()
|
2020-11-27 22:52:52 -05:00
|
|
|
|
.Configuration() ?? new CommandConfiguration())
|
2020-01-31 21:15:07 -05:00
|
|
|
|
.AddSingleton<IPluginImporter, PluginImporter>()
|
2020-01-26 19:06:50 -05:00
|
|
|
|
.AddSingleton<IMiddlewareActionHandler, MiddlewareActionHandler>()
|
2020-02-11 17:44:06 -05:00
|
|
|
|
.AddSingleton<IRConConnectionFactory, RConConnectionFactory>()
|
|
|
|
|
.AddSingleton<IGameServerInstanceFactory, GameServerInstanceFactory>()
|
|
|
|
|
.AddSingleton<IConfigurationHandlerFactory, ConfigurationHandlerFactory>()
|
2020-04-01 15:11:56 -04:00
|
|
|
|
.AddSingleton<IParserRegexFactory, ParserRegexFactory>()
|
2020-04-25 20:01:26 -04:00
|
|
|
|
.AddSingleton<IDatabaseContextFactory, DatabaseContextFactory>()
|
2020-05-04 17:50:02 -04:00
|
|
|
|
.AddSingleton<IGameLogReaderFactory, GameLogReaderFactory>()
|
2020-05-11 17:10:43 -04:00
|
|
|
|
.AddSingleton<IScriptCommandFactory, ScriptCommandFactory>()
|
2020-04-28 17:48:06 -04:00
|
|
|
|
.AddSingleton<IAuditInformationRepository, AuditInformationRepository>()
|
2020-08-17 22:21:11 -04:00
|
|
|
|
.AddSingleton<IEntityService<EFClient>, ClientService>()
|
2022-03-24 09:40:42 -04:00
|
|
|
|
#pragma warning disable CS0618
|
2020-08-17 22:21:11 -04:00
|
|
|
|
.AddSingleton<IMetaService, MetaService>()
|
2022-03-24 09:40:42 -04:00
|
|
|
|
#pragma warning restore CS0618
|
2022-03-23 09:43:57 -04:00
|
|
|
|
.AddSingleton<IMetaServiceV2, MetaServiceV2>()
|
2020-11-11 18:31:26 -05:00
|
|
|
|
.AddSingleton<ClientService>()
|
2020-11-27 22:52:52 -05:00
|
|
|
|
.AddSingleton<PenaltyService>()
|
2020-11-11 18:31:26 -05:00
|
|
|
|
.AddSingleton<ChangeHistoryService>()
|
2020-08-17 22:21:11 -04:00
|
|
|
|
.AddSingleton<IMetaRegistration, MetaRegistration>()
|
2020-09-26 19:13:56 -04:00
|
|
|
|
.AddSingleton<IScriptPluginServiceResolver, ScriptPluginServiceResolver>()
|
2020-11-27 22:52:52 -05:00
|
|
|
|
.AddSingleton<IResourceQueryHelper<ClientPaginationRequest, ReceivedPenaltyResponse>,
|
|
|
|
|
ReceivedPenaltyResourceQueryHelper>()
|
|
|
|
|
.AddSingleton<IResourceQueryHelper<ClientPaginationRequest, AdministeredPenaltyResponse>,
|
|
|
|
|
AdministeredPenaltyResourceQueryHelper>()
|
|
|
|
|
.AddSingleton<IResourceQueryHelper<ClientPaginationRequest, UpdatedAliasResponse>,
|
|
|
|
|
UpdatedAliasResourceQueryHelper>()
|
2020-08-20 11:38:11 -04:00
|
|
|
|
.AddSingleton<IResourceQueryHelper<ChatSearchQuery, MessageResponse>, ChatResourceQueryHelper>()
|
2021-08-31 19:21:40 -04:00
|
|
|
|
.AddSingleton<IResourceQueryHelper<ClientPaginationRequest, ConnectionHistoryResponse>, ConnectionsResourceQueryHelper>()
|
2022-02-23 13:47:00 -05:00
|
|
|
|
.AddSingleton<IResourceQueryHelper<ClientPaginationRequest, PermissionLevelChangedResponse>, PermissionLevelChangedResourceQueryHelper>()
|
2020-04-01 15:11:56 -04:00
|
|
|
|
.AddTransient<IParserPatternMatcher, ParserPatternMatcher>()
|
2020-10-24 16:02:38 -04:00
|
|
|
|
.AddSingleton<IRemoteAssemblyHandler, RemoteAssemblyHandler>()
|
|
|
|
|
.AddSingleton<IMasterCommunication, MasterCommunication>()
|
|
|
|
|
.AddSingleton<IManager, ApplicationManager>()
|
2022-01-26 11:32:16 -05:00
|
|
|
|
#pragma warning disable CS0612
|
2020-11-11 18:31:26 -05:00
|
|
|
|
.AddSingleton<SharedLibraryCore.Interfaces.ILogger, Logger>()
|
2022-01-26 11:32:16 -05:00
|
|
|
|
#pragma warning restore CS0612
|
2020-11-17 19:24:54 -05:00
|
|
|
|
.AddSingleton<IClientNoticeMessageFormatter, ClientNoticeMessageFormatter>()
|
2021-03-22 12:09:25 -04:00
|
|
|
|
.AddSingleton<IClientStatisticCalculator, HitCalculator>()
|
|
|
|
|
.AddSingleton<IServerDistributionCalculator, ServerDistributionCalculator>()
|
|
|
|
|
.AddSingleton<IWeaponNameParser, WeaponNameParser>()
|
|
|
|
|
.AddSingleton<IHitInfoBuilder, HitInfoBuilder>()
|
|
|
|
|
.AddSingleton(typeof(ILookupCache<>), typeof(LookupCache<>))
|
|
|
|
|
.AddSingleton(typeof(IDataValueCache<,>), typeof(DataValueCache<,>))
|
2021-08-26 18:35:05 -04:00
|
|
|
|
.AddSingleton<IServerDataViewer, ServerDataViewer>()
|
|
|
|
|
.AddSingleton<IServerDataCollector, ServerDataCollector>()
|
2021-08-31 19:21:40 -04:00
|
|
|
|
.AddSingleton<IEventPublisher, EventPublisher>()
|
2022-02-07 19:43:36 -05:00
|
|
|
|
.AddTransient<IScriptPluginTimerHelper, ScriptPluginTimerHelper>()
|
2020-11-27 22:52:52 -05:00
|
|
|
|
.AddSingleton(translationLookup)
|
2020-11-29 17:01:52 -05:00
|
|
|
|
.AddDatabaseContextOptions(appConfig);
|
2020-01-26 19:06:50 -05:00
|
|
|
|
|
2020-05-04 17:50:02 -04:00
|
|
|
|
if (args.Contains("serialevents"))
|
|
|
|
|
{
|
|
|
|
|
serviceCollection.AddSingleton<IEventHandler, SerialGameEventHandler>();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
serviceCollection.AddSingleton<IEventHandler, GameEventHandler>();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-03 11:51:03 -04:00
|
|
|
|
serviceCollection.AddSource();
|
|
|
|
|
|
2020-02-01 13:27:14 -05:00
|
|
|
|
return serviceCollection;
|
2019-12-02 16:52:36 -05:00
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
private static ILogger BuildDefaultLogger<T>(ApplicationConfiguration appConfig)
|
|
|
|
|
{
|
|
|
|
|
var collection = new ServiceCollection()
|
|
|
|
|
.AddBaseLogger(appConfig)
|
|
|
|
|
.BuildServiceProvider();
|
|
|
|
|
|
|
|
|
|
return collection.GetRequiredService<ILogger<T>>();
|
|
|
|
|
}
|
2015-03-08 17:20:10 -04:00
|
|
|
|
}
|
2022-01-26 11:32:16 -05:00
|
|
|
|
}
|