2021-07-07 13:36:25 +02:00
|
|
|
|
using System;
|
2021-12-10 11:31:36 +01:00
|
|
|
|
using System.Diagnostics;
|
2021-07-07 13:36:25 +02:00
|
|
|
|
using System.IO;
|
2021-12-10 11:31:36 +01:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2022-07-10 16:13:08 +08:00
|
|
|
|
using RageCoop.Core;
|
2022-08-12 18:10:56 +08:00
|
|
|
|
using Newtonsoft.Json;
|
2022-05-22 15:55:26 +08:00
|
|
|
|
namespace RageCoop.Server
|
2021-07-07 13:36:25 +02:00
|
|
|
|
{
|
|
|
|
|
class Program
|
|
|
|
|
{
|
2022-06-27 15:35:23 +08:00
|
|
|
|
private static bool Stopping = false;
|
2022-08-15 21:25:42 +08:00
|
|
|
|
static Logger mainLogger;
|
2021-07-07 13:36:25 +02:00
|
|
|
|
static void Main(string[] args)
|
|
|
|
|
{
|
2022-08-15 21:25:42 +08:00
|
|
|
|
AppDomain.CurrentDomain.UnhandledException+=UnhandledException;
|
|
|
|
|
mainLogger = new Logger()
|
2022-05-31 19:35:01 -08:00
|
|
|
|
{
|
|
|
|
|
LogPath="RageCoop.Server.log",
|
|
|
|
|
UseConsole=true,
|
2022-07-10 16:13:08 +08:00
|
|
|
|
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
|
|
|
|
|
setting.LogLevel=0;
|
2021-12-10 11:31:36 +01:00
|
|
|
|
#endif
|
2022-06-27 15:35:23 +08:00
|
|
|
|
var server = new Server(setting, mainLogger);
|
2021-12-08 13:45:00 +01: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.");
|
2021-12-08 13:45:00 +01:00
|
|
|
|
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);
|
2022-07-22 19:43:48 +08:00
|
|
|
|
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);
|
|
|
|
|
}
|
2021-12-08 13:45:00 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
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();
|
2022-07-22 19:43:48 +08:00
|
|
|
|
while (true)
|
|
|
|
|
{
|
2022-08-01 23:20:22 +08:00
|
|
|
|
|
2022-07-22 19:43:48 +08: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);
|
2022-07-22 19:43:48 +08:00
|
|
|
|
}
|
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)
|
|
|
|
|
{
|
|
|
|
|
mainLogger.Error($"Unhandled exception thrown from user thread:",e.ExceptionObject as Exception);
|
|
|
|
|
mainLogger.Flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void Fatal(Exception e)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|