Files
RAGECOOP-V/RageCoop.Server/Program.cs

113 lines
3.8 KiB
C#
Raw Permalink Normal View History

2022-09-08 12:41:56 -07:00
using RageCoop.Core;
using System;
using System.Diagnostics;
2021-07-07 13:36:25 +02:00
using System.IO;
using System.Threading;
2022-05-22 15:55:26 +08:00
namespace RageCoop.Server
2021-07-07 13:36:25 +02:00
{
2022-09-08 12:41:56 -07:00
internal class Program
2021-07-07 13:36:25 +02:00
{
2022-06-27 15:35:23 +08:00
private static bool Stopping = false;
2022-09-08 12:41:56 -07:00
private static Logger mainLogger;
private static void Main(string[] args)
2021-07-07 13:36:25 +02:00
{
2022-09-08 12:41:56 -07:00
if (args.Length >= 2 && args[0] == "update")
{
var target = args[1];
2022-09-05 13:02:09 +02:00
int i = 0;
while (i++ < 10)
{
try
{
2022-09-08 12:41:56 -07:00
Console.WriteLine("Applying update to " + target);
2022-08-21 19:56:59 +08:00
CoreUtils.CopyFilesRecursively(new(AppDomain.CurrentDomain.BaseDirectory), new(target));
Process.Start(Path.Combine(target, "RageCoop.Server"));
Environment.Exit(0);
}
2022-09-08 12:41:56 -07:00
catch (Exception ex)
{
2022-08-21 19:30:27 +08:00
Console.WriteLine(ex.ToString());
Thread.Sleep(3000);
}
}
2022-08-21 19:30:27 +08:00
Environment.Exit(i);
}
2022-09-08 12:41:56 -07:00
AppDomain.CurrentDomain.UnhandledException += UnhandledException;
2022-08-15 21:25:42 +08:00
mainLogger = new Logger()
2022-05-31 19:35:01 -08:00
{
2022-09-08 12:41:56 -07:00
LogPath = "RageCoop.Server.log",
UseConsole = true,
Name = "Server"
2022-05-31 19:35:01 -08:00
};
2021-07-07 13:36:25 +02:00
try
{
2022-04-06 02:18:24 +02:00
Console.Title = "RAGECOOP";
2022-08-12 18:10:56 +08:00
var setting = Util.Read<Settings>("Settings.xml");
2022-06-27 15:35:23 +08:00
#if DEBUG
2022-09-08 12:41:56 -07:00
setting.LogLevel = 0;
#endif
2022-06-27 15:35:23 +08:00
var server = new Server(setting, mainLogger);
2022-09-08 12:41:56 -07:00
Console.CancelKeyPress += delegate (object sender, ConsoleCancelEventArgs e)
{
2022-06-27 15:35:23 +08:00
mainLogger.Info("Initiating shutdown sequence...");
mainLogger.Info("Press Ctrl+C again to commence an emergency shutdown.");
if (e.SpecialKey == ConsoleSpecialKey.ControlC)
{
2022-06-27 15:35:23 +08:00
if (!Stopping)
2022-06-22 10:03:45 +08:00
{
e.Cancel = true;
2022-06-27 15:35:23 +08:00
Stopping = true;
server.Stop();
mainLogger.Info("Server stopped.");
mainLogger.Dispose();
2022-08-02 15:55:26 +08:00
Thread.Sleep(1000);
Environment.Exit(0);
2022-06-22 10:03:45 +08:00
}
else
{
2022-06-27 15:35:23 +08:00
mainLogger.Flush();
2022-06-22 10:03:45 +08:00
Environment.Exit(1);
}
}
};
2022-06-27 15:35:23 +08:00
server.Start();
mainLogger?.Info("Please use CTRL + C if you want to stop the server!");
2022-08-01 23:20:22 +08:00
mainLogger?.Info("Type here to send chat messages or execute commands");
mainLogger?.Flush();
while (true)
{
2022-09-08 12:41:56 -07:00
var s = Console.ReadLine();
if (!Stopping && s != null)
{
server.ChatMessageReceived("Server", s, null);
}
2022-08-01 23:20:22 +08:00
Thread.Sleep(20);
}
2021-07-07 13:36:25 +02:00
}
catch (Exception e)
{
2022-08-15 21:25:42 +08:00
Fatal(e);
2021-07-07 13:36:25 +02:00
}
}
2022-08-15 21:25:42 +08:00
private static void UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
2022-09-08 12:41:56 -07:00
mainLogger.Error($"Unhandled exception thrown from user thread", e.ExceptionObject as Exception);
2022-08-15 21:25:42 +08:00
mainLogger.Flush();
}
2022-09-08 12:41:56 -07:00
private static void Fatal(Exception e)
2022-08-15 21:25:42 +08:00
{
mainLogger.Error(e);
mainLogger.Error($"Fatal error occurred, server shutting down.");
mainLogger.Flush();
Thread.Sleep(5000);
Environment.Exit(1);
}
2021-07-07 13:36:25 +02:00
}
}