2019-01-27 20:45:35 -05:00
|
|
|
|
using IW4MAdmin.Application.Migration;
|
2019-12-02 16:52:36 -05:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2018-04-09 15:17:10 -04:00
|
|
|
|
using SharedLibraryCore;
|
2020-01-11 21:32:27 -05:00
|
|
|
|
using SharedLibraryCore.Helpers;
|
2019-12-02 16:52:36 -05:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2019-01-27 20:45:35 -05:00
|
|
|
|
using System;
|
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;
|
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
|
|
|
|
{
|
2020-01-11 21:32:27 -05:00
|
|
|
|
public static BuildNumber Version { get; private set; } = BuildNumber.Parse(Utilities.GetVersionAsString());
|
2019-05-08 21:34:17 -04:00
|
|
|
|
public static ApplicationManager ServerManager;
|
|
|
|
|
private static Task ApplicationTask;
|
2020-01-11 21:32:27 -05:00
|
|
|
|
private static readonly BuildNumber _fallbackVersion = BuildNumber.Parse("99.99.99.99");
|
2019-05-08 21:34:17 -04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// entrypoint of the application
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static async Task Main()
|
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
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
Console.CancelKeyPress += new ConsoleCancelEventHandler(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
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
await LaunchAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
|
|
|
|
ServerManager?.Stop();
|
|
|
|
|
await ApplicationTask;
|
|
|
|
|
}
|
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>
|
|
|
|
|
private static async Task LaunchAsync()
|
|
|
|
|
{
|
|
|
|
|
restart:
|
2017-05-26 18:49:27 -04:00
|
|
|
|
try
|
2015-07-06 15:51:08 -04:00
|
|
|
|
{
|
2018-10-13 19:49:08 -04:00
|
|
|
|
ServerManager = ApplicationManager.GetInstance();
|
2019-04-21 17:28:13 -04:00
|
|
|
|
var configuration = ServerManager.GetApplicationSettings().Configuration();
|
2019-06-28 17:57:01 -04:00
|
|
|
|
Localization.Configure.Initialize(configuration?.EnableCustomLocale ?? false ? (configuration.CustomLocale ?? "en-US") : "en-US");
|
2019-01-27 20:45:35 -05:00
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
// do any needed housekeeping file/folder migrations
|
2018-10-12 22:28:22 -04:00
|
|
|
|
ConfigurationMigration.MoveConfigFolder10518(null);
|
2019-05-08 21:34:17 -04:00
|
|
|
|
ConfigurationMigration.CheckDirectories();
|
2015-03-09 00:28:57 -04:00
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
ServerManager.Logger.WriteInfo(Utilities.CurrentLocalization.LocalizationIndex["MANAGER_VERSION"].FormatExt(Version));
|
2018-09-02 17:59:27 -04:00
|
|
|
|
|
2019-12-02 16:52:36 -05:00
|
|
|
|
ConfigureServices();
|
2019-05-08 21:34:17 -04:00
|
|
|
|
await CheckVersion();
|
|
|
|
|
await ServerManager.Init();
|
|
|
|
|
}
|
2018-05-08 00:58:46 -04:00
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
var loc = Utilities.CurrentLocalization.LocalizationIndex;
|
|
|
|
|
string failMessage = loc == null ? "Failed to initalize IW4MAdmin" : loc["MANAGER_INIT_FAIL"];
|
|
|
|
|
string exitMessage = loc == null ? "Press any key to exit..." : loc["MANAGER_EXIT"];
|
2018-04-19 01:48:14 -04:00
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
Console.WriteLine(e.Message);
|
|
|
|
|
Console.WriteLine(exitMessage);
|
|
|
|
|
Console.ReadKey();
|
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
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ApplicationTask = RunApplicationTasksAsync();
|
|
|
|
|
await ApplicationTask;
|
|
|
|
|
}
|
2018-04-19 01:48:14 -04:00
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
catch { }
|
2018-04-19 01:48:14 -04:00
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
if (ServerManager.IsRestartRequested)
|
|
|
|
|
{
|
|
|
|
|
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>
|
|
|
|
|
private static async Task RunApplicationTasksAsync()
|
|
|
|
|
{
|
|
|
|
|
var webfrontTask = ServerManager.GetApplicationSettings().Configuration().EnableWebFront ?
|
|
|
|
|
WebfrontCore.Program.Init(ServerManager, ServerManager.CancellationToken) :
|
|
|
|
|
Task.CompletedTask;
|
2018-02-21 20:29:23 -05: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
|
|
|
|
|
var inputThread = new Thread(async () => await ReadConsoleInput());
|
|
|
|
|
inputThread.Start();
|
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
var tasks = new[]
|
|
|
|
|
{
|
2019-05-09 21:00:00 -04:00
|
|
|
|
ServerManager.Start(),
|
2019-05-08 21:34:17 -04:00
|
|
|
|
webfrontTask,
|
|
|
|
|
};
|
2015-08-22 02:04:30 -04:00
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
await Task.WhenAll(tasks);
|
2019-05-17 10:02:09 -04:00
|
|
|
|
inputThread.Abort();
|
2017-06-12 13:50:00 -04:00
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
ServerManager.Logger.WriteVerbose(Utilities.CurrentLocalization.LocalizationIndex["MANAGER_SHUTDOWN_SUCCESS"]);
|
|
|
|
|
}
|
2017-05-26 18:49:27 -04:00
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// checks for latest version of the application
|
|
|
|
|
/// notifies user if an update is available
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private static async Task CheckVersion()
|
|
|
|
|
{
|
|
|
|
|
var api = API.Master.Endpoint.Get();
|
|
|
|
|
var loc = Utilities.CurrentLocalization.LocalizationIndex;
|
2015-08-22 02:04:30 -04:00
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
var version = new API.Master.VersionInfo()
|
|
|
|
|
{
|
2020-01-11 21:32:27 -05:00
|
|
|
|
CurrentVersionStable = _fallbackVersion
|
2019-05-08 21:34:17 -04:00
|
|
|
|
};
|
2017-05-26 18:49:27 -04:00
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
2020-01-11 21:32:27 -05:00
|
|
|
|
version = await api.GetVersion(1);
|
2017-05-26 18:49:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-25 20:29:58 -05:00
|
|
|
|
catch (Exception e)
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
ServerManager.Logger.WriteWarning(loc["MANAGER_VERSION_FAIL"]);
|
2018-04-09 15:17:10 -04:00
|
|
|
|
while (e.InnerException != null)
|
2018-03-13 20:12:24 -04:00
|
|
|
|
{
|
|
|
|
|
e = e.InnerException;
|
|
|
|
|
}
|
2019-05-08 21:34:17 -04:00
|
|
|
|
|
|
|
|
|
ServerManager.Logger.WriteDebug(e.Message);
|
2017-05-26 18:49:27 -04:00
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2020-01-11 21:32:27 -05:00
|
|
|
|
if (version.CurrentVersionStable == _fallbackVersion)
|
2018-05-10 01:34:29 -04:00
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
|
|
|
Console.WriteLine(loc["MANAGER_VERSION_FAIL"]);
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Gray;
|
2018-05-10 01:34:29 -04:00
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
#if !PRERELEASE
|
|
|
|
|
else if (version.CurrentVersionStable > Version)
|
2018-10-05 23:10:39 -04:00
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
Console.ForegroundColor = ConsoleColor.DarkYellow;
|
2020-01-11 21:32:27 -05:00
|
|
|
|
Console.WriteLine($"IW4MAdmin {loc["MANAGER_VERSION_UPDATE"]} [v{version.CurrentVersionStable.ToString()}]");
|
|
|
|
|
Console.WriteLine(loc["MANAGER_VERSION_CURRENT"].FormatExt($"[v{Version.ToString()}]"));
|
2019-05-08 21:34:17 -04:00
|
|
|
|
Console.ForegroundColor = ConsoleColor.Gray;
|
2018-10-05 23:10:39 -04:00
|
|
|
|
}
|
2019-05-08 21:34:17 -04:00
|
|
|
|
#else
|
|
|
|
|
else if (version.CurrentVersionPrerelease > Version)
|
|
|
|
|
{
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.DarkYellow;
|
2020-01-11 21:32:27 -05:00
|
|
|
|
Console.WriteLine($"IW4MAdmin-Prerelease {loc["MANAGER_VERSION_UPDATE"]} [v{version.CurrentVersionPrerelease.ToString()}-pr]");
|
|
|
|
|
Console.WriteLine(loc["MANAGER_VERSION_CURRENT"].FormatExt($"[v{Version.ToString()}-pr]"));
|
2019-05-08 21:34:17 -04:00
|
|
|
|
Console.ForegroundColor = ConsoleColor.Gray;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
else
|
2018-10-05 23:10:39 -04:00
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
Console.ForegroundColor = ConsoleColor.Green;
|
|
|
|
|
Console.WriteLine(loc["MANAGER_VERSION_SUCCESS"]);
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Gray;
|
2018-10-05 23:10:39 -04: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>
|
|
|
|
|
private static async Task ReadConsoleInput()
|
|
|
|
|
{
|
|
|
|
|
string lastCommand;
|
|
|
|
|
var Origin = Utilities.IW4MAdminClient(ServerManager.Servers[0]);
|
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
|
|
|
|
{
|
2019-05-31 11:17:01 -04:00
|
|
|
|
while (!ServerManager.CancellationToken.IsCancellationRequested)
|
2019-05-08 21:34:17 -04:00
|
|
|
|
{
|
2019-05-31 11:17:01 -04:00
|
|
|
|
lastCommand = Console.ReadLine();
|
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
if (lastCommand?.Length > 0)
|
|
|
|
|
{
|
2019-05-31 11:17:01 -04:00
|
|
|
|
if (lastCommand?.Length > 0)
|
2019-05-08 21:34:17 -04:00
|
|
|
|
{
|
2019-05-31 11:17:01 -04:00
|
|
|
|
GameEvent E = new GameEvent()
|
|
|
|
|
{
|
|
|
|
|
Type = GameEvent.EventType.Command,
|
|
|
|
|
Data = lastCommand,
|
|
|
|
|
Origin = Origin,
|
|
|
|
|
Owner = ServerManager.Servers[0]
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ServerManager.GetEventHandler().AddEvent(E);
|
|
|
|
|
await E.WaitAsync(Utilities.DefaultCommandTimeout, ServerManager.CancellationToken);
|
|
|
|
|
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)
|
|
|
|
|
{ }
|
2015-08-17 16:38:42 -04:00
|
|
|
|
}
|
2019-12-02 16:52:36 -05:00
|
|
|
|
|
|
|
|
|
private static void ConfigureServices()
|
|
|
|
|
{
|
2019-12-27 13:10:20 -05:00
|
|
|
|
var serviceProvider = new ServiceCollection();
|
|
|
|
|
serviceProvider.AddSingleton<IManager>(ServerManager);
|
|
|
|
|
var builder = serviceProvider.BuildServiceProvider();
|
|
|
|
|
builder.Dispose();
|
2019-12-02 16:52:36 -05:00
|
|
|
|
}
|
2015-03-08 17:20:10 -04:00
|
|
|
|
}
|
|
|
|
|
}
|