IW4M-Admin/Application/Main.cs

206 lines
7.5 KiB
C#
Raw Normal View History

2019-01-27 20:45:35 -05:00
using IW4MAdmin.Application.Migration;
using SharedLibraryCore;
2019-01-27 20:45:35 -05:00
using SharedLibraryCore.Localization;
using System;
using System.IO;
using System.Text;
using System.Threading;
2019-01-27 20:45:35 -05:00
using System.Threading.Tasks;
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
{
2015-08-20 17:54:38 -04:00
static public double Version { get; private set; }
static public ApplicationManager ServerManager;
2018-05-10 01:34:29 -04:00
private static ManualResetEventSlim OnShutdownComplete = new ManualResetEventSlim();
public static void Main(string[] args)
2015-03-08 17:20:10 -04:00
{
AppDomain.CurrentDomain.SetData("DataDirectory", Utilities.OperatingDirectory);
Console.OutputEncoding = Encoding.UTF8;
Console.ForegroundColor = ConsoleColor.Gray;
Version = Utilities.GetVersionAsDouble();
2015-08-17 16:38:42 -04:00
Console.WriteLine("=====================================================");
Console.WriteLine(" IW4M ADMIN");
Console.WriteLine(" by RaidMax ");
Console.WriteLine($" Version {Utilities.GetVersionAsString()}");
Console.WriteLine("=====================================================");
Index loc = null;
try
{
ServerManager = ApplicationManager.GetInstance();
var configuration = ServerManager.GetApplicationSettings().Configuration();
2019-01-27 20:45:35 -05:00
if (configuration != null)
{
Localization.Configure.Initialize(configuration.EnableCustomLocale ? (configuration.CustomLocale ?? "windows-1252") : "windows-1252");
}
2019-01-27 20:45:35 -05:00
else
{
Localization.Configure.Initialize();
}
2019-01-27 20:45:35 -05:00
loc = Utilities.CurrentLocalization.LocalizationIndex;
Console.CancelKeyPress += new ConsoleCancelEventHandler(OnCancelKey);
CheckDirectories();
2018-10-12 22:28:22 -04:00
// do any needed migrations
// todo: move out
ConfigurationMigration.MoveConfigFolder10518(null);
ServerManager.Logger.WriteInfo($"Version is {Version}");
var api = API.Master.Endpoint.Get();
var version = new API.Master.VersionInfo()
{
CurrentVersionStable = 99.99f
};
try
{
version = api.GetVersion().Result;
}
catch (Exception e)
{
ServerManager.Logger.WriteWarning(loc["MANAGER_VERSION_FAIL"]);
while (e.InnerException != null)
{
e = e.InnerException;
}
ServerManager.Logger.WriteDebug(e.Message);
}
if (version.CurrentVersionStable == 99.99f)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(loc["MANAGER_VERSION_FAIL"]);
Console.ForegroundColor = ConsoleColor.Gray;
}
#if !PRERELEASE
else if (version.CurrentVersionStable > Version)
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine($"IW4MAdmin {loc["MANAGER_VERSION_UPDATE"]} [v{version.CurrentVersionStable.ToString("0.0")}]");
Console.WriteLine(loc["MANAGER_VERSION_CURRENT"].FormatExt($"[v{Version.ToString("0.0")}]"));
Console.ForegroundColor = ConsoleColor.Gray;
}
#else
else if (version.CurrentVersionPrerelease > Version)
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine($"IW4MAdmin-Prerelease {loc["MANAGER_VERSION_UPDATE"]} [v{version.CurrentVersionPrerelease.ToString("0.0")}-pr]");
Console.WriteLine(loc["MANAGER_VERSION_CURRENT"].FormatExt($"[v{Version.ToString("0.0")}-pr]"));
Console.ForegroundColor = ConsoleColor.Gray;
}
#endif
else
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(loc["MANAGER_VERSION_SUCCESS"]);
Console.ForegroundColor = ConsoleColor.Gray;
}
ServerManager.Init().Wait();
2018-02-21 20:29:23 -05:00
var consoleTask = Task.Run(async () =>
{
string userInput;
var Origin = Utilities.IW4MAdminClient(ServerManager.Servers[0]);
do
{
userInput = Console.ReadLine();
if (userInput?.ToLower() == "quit")
2019-01-27 20:45:35 -05:00
{
ServerManager.Stop();
2019-01-27 20:45:35 -05:00
}
if (ServerManager.Servers.Count == 0)
{
Console.WriteLine(loc["MANAGER_CONSOLE_NOSERV"]);
continue;
}
if (userInput?.Length > 0)
{
2018-05-10 01:34:29 -04:00
GameEvent E = new GameEvent()
{
Type = GameEvent.EventType.Command,
Data = userInput,
Origin = Origin,
Owner = ServerManager.Servers[0]
};
ServerManager.GetEventHandler().AddEvent(E);
2018-09-06 14:25:58 -04:00
await E.WaitAsync(30 * 1000);
}
Console.Write('>');
} while (ServerManager.Running);
});
}
catch (Exception e)
{
string failMessage = loc == null ? "Failed to initalize IW4MAdmin" : loc["MANAGER_INIT_FAIL"];
string exitMessage = loc == null ? "Press any key to exit..." : loc["MANAGER_EXIT"];
Console.WriteLine(failMessage);
while (e.InnerException != null)
{
e = e.InnerException;
}
Console.WriteLine(e.Message);
Console.WriteLine(exitMessage);
Console.ReadKey();
return;
}
2018-05-10 01:34:29 -04:00
if (ServerManager.GetApplicationSettings().Configuration().EnableWebFront)
{
Task.Run(() => WebfrontCore.Program.Init(ServerManager));
}
2018-05-10 01:34:29 -04:00
OnShutdownComplete.Reset();
ServerManager.Start();
ServerManager.Logger.WriteVerbose(loc["MANAGER_SHUTDOWN_SUCCESS"]);
2018-05-10 01:34:29 -04:00
OnShutdownComplete.Set();
}
private static void OnCancelKey(object sender, ConsoleCancelEventArgs e)
{
ServerManager.Stop();
OnShutdownComplete.Wait();
}
static void CheckDirectories()
{
if (!Directory.Exists(Path.Join(Utilities.OperatingDirectory, "Plugins")))
{
Directory.CreateDirectory(Path.Join(Utilities.OperatingDirectory, "Plugins"));
}
if (!Directory.Exists(Path.Join(Utilities.OperatingDirectory, "Database")))
{
Directory.CreateDirectory(Path.Join(Utilities.OperatingDirectory, "Database"));
}
if (!Directory.Exists(Path.Join(Utilities.OperatingDirectory, "Log")))
{
Directory.CreateDirectory(Path.Join(Utilities.OperatingDirectory, "Log"));
}
2015-08-17 16:38:42 -04:00
}
2015-03-08 17:20:10 -04:00
}
}