11using System ;
22using System . Collections . Generic ;
3+ using System . Diagnostics ;
34using System . Globalization ;
45using System . IO ;
56using System . Reflection ;
@@ -45,6 +46,7 @@ private static void Main(string[] args)
4546 } ) ;
4647 }
4748
49+ AppDomain . CurrentDomain . UnhandledException += CurrentDomainOnUnhandledException ;
4850 AppDomain . CurrentDomain . AssemblyResolve += CurrentDomainOnAssemblyResolve ;
4951 AppDomain . CurrentDomain . ReflectionOnlyAssemblyResolve += CurrentDomainOnAssemblyResolve ;
5052
@@ -53,22 +55,10 @@ private static void Main(string[] args)
5355 NitroxServiceLocator . InitializeDependencyContainer ( new SubnauticaServerAutoFacRegistrar ( ) ) ;
5456 NitroxServiceLocator . BeginNewLifetimeScope ( ) ;
5557
56- Server server ;
57- try
58+ Server server = NitroxServiceLocator . LocateService < Server > ( ) ;
59+ if ( ! server . Start ( ) )
5860 {
59- server = NitroxServiceLocator . LocateService < Server > ( ) ;
60-
61- if ( ! server . Start ( ) )
62- {
63- Log . Error ( "Unable to start server." ) ;
64- Console . WriteLine ( "\n Press any key to continue.." ) ;
65- Console . ReadKey ( true ) ;
66- }
67- }
68- catch ( Exception ex )
69- {
70- Log . Error ( ex ) ;
71- return ;
61+ throw new Exception ( "Unable to start server." ) ;
7262 }
7363
7464 CatchExitEvent ( ) ;
@@ -80,6 +70,34 @@ private static void Main(string[] args)
8070 }
8171 }
8272
73+ private static void CurrentDomainOnUnhandledException ( object sender , UnhandledExceptionEventArgs e )
74+ {
75+ if ( e . ExceptionObject is Exception ex )
76+ {
77+ Log . Error ( ex ) ;
78+ }
79+ if ( ! Environment . UserInteractive || Console . In == StreamReader . Null )
80+ {
81+ return ;
82+ }
83+
84+ Console . WriteLine ( "Press L to open log file before closing. Press any other key to close . . ." ) ;
85+ ConsoleKeyInfo key = Console . ReadKey ( true ) ;
86+ if ( key . Key == ConsoleKey . L )
87+ {
88+ Log . Info ( $ "Opening log file at: { Log . FileName } ..") ;
89+ string fileOpenerProgram = Environment . OSVersion . Platform switch
90+ {
91+ PlatformID . MacOSX => "open" ,
92+ PlatformID . Unix => "xdg-open" ,
93+ _ => "explorer"
94+ } ;
95+ Process . Start ( fileOpenerProgram , Log . FileName ) ;
96+ }
97+
98+ Environment . Exit ( 1 ) ;
99+ }
100+
83101 private static Assembly CurrentDomainOnAssemblyResolve ( object sender , ResolveEventArgs args )
84102 {
85103 string dllFileName = args . Name . Split ( ',' ) [ 0 ] ;
@@ -160,7 +178,7 @@ private static void CatchExitEvent()
160178 [ DllImport ( "kernel32.dll" , SetLastError = true ) ]
161179 private static extern bool SetConsoleCtrlHandler ( ConsoleEventDelegate callback , bool add ) ;
162180
163- // Prevents Garbage Collection issue where server closes and an exception occurs for this handle.
181+ // Prevents Garbage Collection freeing this callback's memory. Causing an exception to occur for this handle.
164182 private static readonly ConsoleEventDelegate consoleCtrlCheckDelegate = ConsoleEventCallback ;
165183
166184 private static bool ConsoleEventCallback ( int eventType )
0 commit comments