using System; using System.Runtime.InteropServices; using System.Security.Permissions; using Hang.Core; using Hang; namespace Hang { internal class Program { private static bool ProgramLoaded = false; [DllImport("Kernel32")] private static extern bool SetConsoleCtrlHandler(EventHandler handler, bool add); private delegate bool EventHandler(CtrlType sig); static EventHandler _handler; enum CtrlType { CTRL_C_EVENT = 0, CTRL_BREAK_EVENT = 1, CTRL_CLOSE_EVENT = 2, CTRL_LOGOFF_EVENT = 5, CTRL_SHUTDOWN_EVENT = 6 } [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)] internal static void Main() { Console.CursorVisible = false; AppDomain currentDomain = AppDomain.CurrentDomain; currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler); _handler += new EventHandler(Shutdown); SetConsoleCtrlHandler(_handler, true); try { HangEnvironment.Initialize(); ProgramLoaded = true; } catch (Exception e) { Console.Write(e.ToString()); } while (true) Console.ReadKey(); } static void MyHandler(object sender, UnhandledExceptionEventArgs args) { Logging.DisablePrimaryWriting(); Exception e = (Exception)args.ExceptionObject; Logging.LogCriticalException(e.ToString()); HangEnvironment.SendMassMessage("A fatal error crashed the server, server shutting down."); HangEnvironment.PreformShutDown(true); } private static bool Shutdown(CtrlType sig) { if (ProgramLoaded) { Logging.LogMessage("Server close " + DateTime.Now); Logging.DisablePrimaryWriting(); Console.WriteLine("The server is saving users furniture, rooms, etc. WAIT FOR THE SERVER TO CLOSE, DO NOT EXIT THE PROCESS IN TASK MANAGER!!"); HangEnvironment.PreformShutDown(true); } return true; } } }