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;
|
2022-06-11 12:34:00 -04:00
|
|
|
|
using IW4MAdmin.Application.Alerts;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using IW4MAdmin.Application.Extensions;
|
2023-04-04 22:44:08 -04:00
|
|
|
|
using IW4MAdmin.Application.IO;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using IW4MAdmin.Application.Localization;
|
2023-04-04 19:24:13 -04:00
|
|
|
|
using IW4MAdmin.Application.Plugin;
|
|
|
|
|
using IW4MAdmin.Application.Plugin.Script;
|
2023-01-23 17:38:16 -05:00
|
|
|
|
using IW4MAdmin.Application.QueryHelpers;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
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;
|
2023-04-21 21:40:20 -04:00
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
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;
|
2023-01-23 17:38:16 -05:00
|
|
|
|
using WebfrontCore.QueryHelpers.Models;
|
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;
|
2023-04-05 10:54:57 -04:00
|
|
|
|
private static IServiceProvider _serviceProvider;
|
2019-05-08 21:34:17 -04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// entrypoint of the application
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2023-05-30 15:58:17 -04:00
|
|
|
|
public static async Task Main(bool noConfirm = false, int? maxConcurrentRequests = 25, int? requestQueueLimit = 25)
|
2015-03-08 17:20:10 -04:00
|
|
|
|
{
|
2018-12-31 21:52:19 -05:00
|
|
|
|
AppDomain.CurrentDomain.SetData("DataDirectory", Utilities.OperatingDirectory);
|
2023-05-27 15:01:16 -04:00
|
|
|
|
AppDomain.CurrentDomain.AssemblyResolve += (sender, eventArgs) =>
|
|
|
|
|
{
|
|
|
|
|
var libraryName = eventArgs.Name.Split(",").First();
|
|
|
|
|
|
|
|
|
|
var overrides = new[] { nameof(SharedLibraryCore), nameof(Stats) };
|
|
|
|
|
if (!overrides.Contains(libraryName))
|
|
|
|
|
{
|
2023-05-27 15:09:57 -04:00
|
|
|
|
return AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(asm => asm.FullName == eventArgs.Name);
|
2023-05-27 15:01:16 -04:00
|
|
|
|
}
|
|
|
|
|
// added to be a bit more permissive with plugin references
|
|
|
|
|
return AppDomain.CurrentDomain.GetAssemblies()
|
|
|
|
|
.FirstOrDefault(asm => asm.FullName?.StartsWith(libraryName) ?? false);
|
2023-05-30 15:58:17 -04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (noConfirm)
|
|
|
|
|
{
|
|
|
|
|
AppContext.SetSwitch("NoConfirmPrompt", true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Environment.SetEnvironmentVariable("MaxConcurrentRequests", (maxConcurrentRequests * Environment.ProcessorCount).ToString());
|
|
|
|
|
Environment.SetEnvironmentVariable("RequestQueueLimit", requestQueueLimit.ToString());
|
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
|
|
|
|
|
2023-05-30 15:58:17 -04:00
|
|
|
|
await LaunchAsync();
|
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>
|
2023-05-30 15:58:17 -04:00
|
|
|
|
private static async Task LaunchAsync()
|
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;
|
2023-05-30 15:58:17 -04:00
|
|
|
|
logger.LogInformation("Begin IW4MAdmin startup. Version is {Version}", Version);
|
2022-01-26 15:42:23 -05:00
|
|
|
|
|
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();
|
2023-04-05 10:54:57 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
logger.LogDebug("Configuring services...");
|
2020-01-31 21:15:07 -05:00
|
|
|
|
|
2023-04-05 10:54:57 -04:00
|
|
|
|
var configHandler = new BaseConfigurationHandler<ApplicationConfiguration>("IW4MAdminSettings");
|
|
|
|
|
await configHandler.BuildAsync();
|
|
|
|
|
_serviceProvider = WebfrontCore.Program.InitializeServices(ConfigureServices,
|
|
|
|
|
(configHandler.Configuration() ?? new ApplicationConfiguration()).WebfrontBindUrl);
|
|
|
|
|
|
|
|
|
|
_serverManager = (ApplicationManager)_serviceProvider.GetRequiredService<IManager>();
|
|
|
|
|
translationLookup = _serviceProvider.GetRequiredService<ITranslationLookup>();
|
2022-01-26 15:42:23 -05:00
|
|
|
|
|
2022-02-15 21:23:16 -05:00
|
|
|
|
await _serverManager.Init();
|
|
|
|
|
|
2023-04-21 21:40:20 -04:00
|
|
|
|
_applicationTask = RunApplicationTasksAsync(logger, _serverManager, _serviceProvider);
|
2023-04-05 10:54:57 -04:00
|
|
|
|
|
|
|
|
|
await _applicationTask;
|
|
|
|
|
logger.LogInformation("Shutdown completed successfully");
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
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>
|
2023-04-21 21:40:20 -04:00
|
|
|
|
private static Task RunApplicationTasksAsync(ILogger logger, ApplicationManager applicationManager,
|
|
|
|
|
IServiceProvider serviceProvider)
|
2019-05-08 21:34:17 -04:00
|
|
|
|
{
|
2023-04-21 21:40:20 -04:00
|
|
|
|
var collectionService = serviceProvider.GetRequiredService<IServerDataCollector>();
|
|
|
|
|
var versionChecker = serviceProvider.GetRequiredService<IMasterCommunication>();
|
|
|
|
|
var masterCommunicator = serviceProvider.GetRequiredService<IMasterCommunication>();
|
|
|
|
|
var webfrontLifetime = serviceProvider.GetRequiredService<IHostApplicationLifetime>();
|
|
|
|
|
using var onWebfrontErrored = new ManualResetEventSlim();
|
|
|
|
|
|
2022-01-27 14:37:38 -05:00
|
|
|
|
var webfrontTask = _serverManager.GetApplicationSettings().Configuration().EnableWebFront
|
2023-04-21 21:40:20 -04:00
|
|
|
|
? WebfrontCore.Program.GetWebHostTask(_serverManager.CancellationToken).ContinueWith(continuation =>
|
|
|
|
|
{
|
|
|
|
|
if (!continuation.IsFaulted)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.LogCritical("Unable to start webfront task. {Message}",
|
|
|
|
|
continuation.Exception?.InnerException?.Message);
|
|
|
|
|
|
|
|
|
|
logger.LogDebug(continuation.Exception, "Unable to start webfront task");
|
|
|
|
|
|
|
|
|
|
onWebfrontErrored.Set();
|
|
|
|
|
|
|
|
|
|
})
|
2021-03-22 12:09:25 -04:00
|
|
|
|
: Task.CompletedTask;
|
2018-02-21 20:29:23 -05:00
|
|
|
|
|
2023-04-21 21:40:20 -04:00
|
|
|
|
if (_serverManager.GetApplicationSettings().Configuration().EnableWebFront)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
onWebfrontErrored.Wait(webfrontLifetime.ApplicationStarted);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignored when webfront successfully starts
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (onWebfrontErrored.IsSet)
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
}
|
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[]
|
|
|
|
|
{
|
2023-04-21 21:40:20 -04:00
|
|
|
|
applicationManager.Start(),
|
2023-04-05 10:54:57 -04:00
|
|
|
|
versionChecker.CheckVersion(),
|
2019-05-08 21:34:17 -04:00
|
|
|
|
webfrontTask,
|
2023-04-21 21:40:20 -04:00
|
|
|
|
masterCommunicator.RunUploadStatus(_serverManager.CancellationToken),
|
2022-01-27 14:37:38 -05:00
|
|
|
|
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");
|
2023-04-05 10:54:57 -04:00
|
|
|
|
return Task.WhenAll(tasks);
|
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
|
|
|
|
{
|
2023-04-05 10:54:57 -04:00
|
|
|
|
var isV2 = pluginType.GetInterface(nameof(IPluginV2), false) != null;
|
|
|
|
|
|
|
|
|
|
defaultLogger.LogDebug("Registering plugin type {Name}", pluginType.FullName);
|
|
|
|
|
|
|
|
|
|
serviceCollection.AddSingleton(!isV2 ? typeof(IPlugin) : typeof(IPluginV2), pluginType);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var registrationMethod = pluginType.GetMethod(nameof(IPluginV2.RegisterDependencies));
|
|
|
|
|
registrationMethod?.Invoke(null, new object[] { serviceCollection });
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
defaultLogger.LogError(ex, "Could not register plugin of type {Type}", pluginType.Name);
|
|
|
|
|
}
|
2020-11-11 18:31:26 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-04 19:24:13 -04:00
|
|
|
|
var scriptPlugins = pluginImporter.DiscoverScriptPlugins();
|
|
|
|
|
|
|
|
|
|
foreach (var scriptPlugin in scriptPlugins)
|
2022-02-07 23:02:50 -05:00
|
|
|
|
{
|
2023-04-04 19:24:13 -04:00
|
|
|
|
serviceCollection.AddSingleton(scriptPlugin.Item1, sp =>
|
|
|
|
|
sp.GetRequiredService<IScriptPluginFactory>()
|
|
|
|
|
.CreateScriptPlugin(scriptPlugin.Item1, scriptPlugin.Item2));
|
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>
|
2023-04-05 10:54:57 -04:00
|
|
|
|
private static void ConfigureServices(IServiceCollection serviceCollection)
|
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);
|
|
|
|
|
|
2023-02-11 22:09:02 -05:00
|
|
|
|
serviceCollection.AddConfiguration<ApplicationConfiguration>("IW4MAdminSettings")
|
|
|
|
|
.AddConfiguration<DefaultSettings>()
|
|
|
|
|
.AddConfiguration<CommandConfiguration>()
|
|
|
|
|
.AddConfiguration<StatsConfiguration>("StatsPluginSettings");
|
|
|
|
|
|
|
|
|
|
// for legacy purposes. update at some point
|
2020-10-24 16:02:38 -04:00
|
|
|
|
var appConfigHandler = new BaseConfigurationHandler<ApplicationConfiguration>("IW4MAdminSettings");
|
2023-04-05 10:54:57 -04:00
|
|
|
|
appConfigHandler.BuildAsync().GetAwaiter().GetResult();
|
2022-01-28 10:35:01 -05:00
|
|
|
|
var commandConfigHandler = new BaseConfigurationHandler<CommandConfiguration>("CommandConfiguration");
|
2023-04-05 10:54:57 -04:00
|
|
|
|
commandConfigHandler.BuildAsync().GetAwaiter().GetResult();
|
|
|
|
|
|
2020-10-24 16:02:38 -04:00
|
|
|
|
var appConfig = appConfigHandler.Configuration();
|
2023-05-30 15:58:17 -04:00
|
|
|
|
var masterUri = Utilities.IsDevelopment
|
2020-11-11 18:31:26 -05:00
|
|
|
|
? new Uri("http://127.0.0.1:8080")
|
2023-05-30 15:58:17 -04:00
|
|
|
|
: 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);
|
2023-04-09 10:59:10 -04:00
|
|
|
|
|
|
|
|
|
if (appConfig == null)
|
|
|
|
|
{
|
|
|
|
|
appConfig = (ApplicationConfiguration) new ApplicationConfiguration().Generate();
|
|
|
|
|
appConfigHandler.Set(appConfig);
|
|
|
|
|
appConfigHandler.Save().GetAwaiter().GetResult();
|
|
|
|
|
}
|
2020-02-11 17:44:06 -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
|
|
|
|
|
serviceCollection
|
|
|
|
|
.AddBaseLogger(appConfig)
|
|
|
|
|
.AddSingleton((IConfigurationHandler<ApplicationConfiguration>) appConfigHandler)
|
2022-01-28 10:35:01 -05:00
|
|
|
|
.AddSingleton<IConfigurationHandler<CommandConfiguration>>(commandConfigHandler)
|
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>()
|
2023-01-23 17:38:16 -05:00
|
|
|
|
.AddSingleton<IResourceQueryHelper<ClientResourceRequest, ClientResourceResponse>, ClientResourceQueryHelper>()
|
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>()
|
2022-04-19 19:43:58 -04:00
|
|
|
|
.AddSingleton<IGeoLocationService>(new GeoLocationService(Path.Join(".", "Resources", "GeoLite2-Country.mmdb")))
|
2022-06-11 12:34:00 -04:00
|
|
|
|
.AddSingleton<IAlertManager, AlertManager>()
|
2023-04-05 10:54:57 -04:00
|
|
|
|
#pragma warning disable CS0618
|
2022-02-07 19:43:36 -05:00
|
|
|
|
.AddTransient<IScriptPluginTimerHelper, ScriptPluginTimerHelper>()
|
2023-04-05 10:54:57 -04:00
|
|
|
|
#pragma warning restore CS0618
|
2022-09-08 16:03:38 -04:00
|
|
|
|
.AddSingleton<IInteractionRegistration, InteractionRegistration>()
|
2022-10-12 22:06:18 -04:00
|
|
|
|
.AddSingleton<IRemoteCommandService, RemoteCommandService>()
|
2023-02-11 22:09:02 -05:00
|
|
|
|
.AddSingleton(new ConfigurationWatcher())
|
|
|
|
|
.AddSingleton(typeof(IConfigurationHandlerV2<>), typeof(BaseConfigurationHandlerV2<>))
|
|
|
|
|
.AddSingleton<IScriptPluginFactory, ScriptPluginFactory>()
|
2020-11-27 22:52:52 -05:00
|
|
|
|
.AddSingleton(translationLookup)
|
2020-11-29 17:01:52 -05:00
|
|
|
|
.AddDatabaseContextOptions(appConfig);
|
2023-04-05 10:54:57 -04:00
|
|
|
|
|
|
|
|
|
serviceCollection.AddSingleton<ICoreEventHandler, CoreEventHandler>();
|
2021-06-03 11:51:03 -04:00
|
|
|
|
serviceCollection.AddSource();
|
2023-04-05 10:54:57 -04:00
|
|
|
|
HandlePluginRegistration(appConfig, serviceCollection, masterRestClient);
|
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
|
|
|
|
}
|