99using System . Text ;
1010using System . Threading ;
1111using System . Threading . Tasks ;
12+ using System . Windows ;
1213using System . Windows . Threading ;
1314using NLog ;
1415using Torch . Utils ;
@@ -84,27 +85,21 @@ public bool Initialize(string[] args)
8485 public void Run ( )
8586 {
8687 _server = new TorchServer ( _config ) ;
88+
8789 try
8890 {
89-
90- var initTask = Task . Run ( ( ) => { _server . Init ( ) ; } ) ;
91+ _server . Init ( ) ;
9192 if ( ! _config . NoGui )
9293 {
9394 _server . Init ( ) ;
9495
95- if ( ! _config . NoGui )
96- {
97- var ui = new TorchUI ( _server ) ;
98- if ( _config . Autostart )
99- _server . Start ( ) ;
100- ui . ShowDialog ( ) ;
101- }
102- else
103- _server . Start ( ) ;
96+ if ( _config . Autostart )
97+ Task . Run ( ( ) => _server . Start ( ) ) ;
98+
99+ new TorchUI ( _server ) . ShowDialog ( ) ;
104100 }
105101 else
106102 {
107- initTask . Wait ( ) ;
108103 _server . Start ( ) ;
109104 }
110105 }
@@ -116,101 +111,105 @@ public void Run()
116111 }
117112 }
118113
119- private TorchConfig InitConfig ( )
114+ private TorchConfig InitConfig ( )
115+ {
116+ var configName = "Torch.cfg" ;
117+ var configPath = Path . Combine ( Directory . GetCurrentDirectory ( ) , configName ) ;
118+ if ( File . Exists ( configName ) )
120119 {
121- var configName = "Torch.cfg" ;
122- var configPath = Path . Combine ( Directory . GetCurrentDirectory ( ) , configName ) ;
123- if ( File . Exists ( configName ) )
124- {
125- Log . Info ( $ "Loading config { configPath } ") ;
126- return TorchConfig . LoadFrom ( configPath ) ;
127- }
128- else
129- {
130- Log . Info ( $ "Generating default config at { configPath } ") ;
131- var config = new TorchConfig { InstancePath = Path . GetFullPath ( "Instance" ) } ;
132- config . Save ( configPath ) ;
133- return config ;
134- }
120+ Log . Info ( $ "Loading config { configPath } ") ;
121+ return TorchConfig . LoadFrom ( configPath ) ;
135122 }
136-
137- private static void RunSteamCmd ( )
123+ else
138124 {
139- var log = LogManager . GetLogger ( "SteamCMD" ) ;
125+ Log . Info ( $ "Generating default config at { configPath } ") ;
126+ var config = new TorchConfig { InstancePath = Path . GetFullPath ( "Instance" ) } ;
127+ config . Save ( configPath ) ;
128+ return config ;
129+ }
130+ }
140131
141- if ( ! Directory . Exists ( STEAMCMD_DIR ) )
142- {
143- Directory . CreateDirectory ( STEAMCMD_DIR ) ;
144- }
132+ private static void RunSteamCmd ( )
133+ {
134+ var log = LogManager . GetLogger ( "SteamCMD" ) ;
145135
146- if ( ! File . Exists ( RUNSCRIPT_PATH ) )
147- File . WriteAllText ( RUNSCRIPT_PATH , RUNSCRIPT ) ;
136+ if ( ! Directory . Exists ( STEAMCMD_DIR ) )
137+ {
138+ Directory . CreateDirectory ( STEAMCMD_DIR ) ;
139+ }
140+
141+ if ( ! File . Exists ( RUNSCRIPT_PATH ) )
142+ File . WriteAllText ( RUNSCRIPT_PATH , RUNSCRIPT ) ;
148143
149- if ( ! File . Exists ( STEAMCMD_PATH ) )
144+ if ( ! File . Exists ( STEAMCMD_PATH ) )
145+ {
146+ try
150147 {
151- try
152- {
153- log . Info ( "Downloading SteamCMD." ) ;
154- using ( var client = new WebClient ( ) )
155- client . DownloadFile ( "https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip" , STEAMCMD_ZIP ) ;
148+ log . Info ( "Downloading SteamCMD." ) ;
149+ using ( var client = new WebClient ( ) )
150+ client . DownloadFile ( "https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip" , STEAMCMD_ZIP ) ;
156151
157- ZipFile . ExtractToDirectory ( STEAMCMD_ZIP , STEAMCMD_DIR ) ;
158- File . Delete ( STEAMCMD_ZIP ) ;
159- log . Info ( "SteamCMD downloaded successfully!" ) ;
160- }
161- catch
162- {
163- log . Error ( "Failed to download SteamCMD, unable to update the DS." ) ;
164- return ;
165- }
152+ ZipFile . ExtractToDirectory ( STEAMCMD_ZIP , STEAMCMD_DIR ) ;
153+ File . Delete ( STEAMCMD_ZIP ) ;
154+ log . Info ( "SteamCMD downloaded successfully!" ) ;
166155 }
167-
168- log . Info ( "Checking for DS updates." ) ;
169- var steamCmdProc = new ProcessStartInfo ( STEAMCMD_PATH , "+runscript runscript.txt" )
170- {
171- WorkingDirectory = Path . Combine ( Directory . GetCurrentDirectory ( ) , STEAMCMD_DIR ) ,
172- UseShellExecute = false ,
173- RedirectStandardOutput = true ,
174- StandardOutputEncoding = Encoding . ASCII
175- } ;
176- var cmd = Process . Start ( steamCmdProc ) ;
177-
178- // ReSharper disable once PossibleNullReferenceException
179- while ( ! cmd . HasExited )
156+ catch
180157 {
181- log . Info ( cmd . StandardOutput . ReadLine ( ) ) ;
182- Thread . Sleep ( 100 ) ;
158+ log . Error ( "Failed to download SteamCMD, unable to update the DS." ) ;
159+ return ;
183160 }
184161 }
185162
186- private void LogException ( Exception ex )
163+ log . Info ( "Checking for DS updates." ) ;
164+ var steamCmdProc = new ProcessStartInfo ( STEAMCMD_PATH , "+runscript runscript.txt" )
187165 {
188- if ( ex . InnerException != null )
189- {
190- LogException ( ex . InnerException ) ;
191- }
166+ WorkingDirectory = Path . Combine ( Directory . GetCurrentDirectory ( ) , STEAMCMD_DIR ) ,
167+ UseShellExecute = false ,
168+ RedirectStandardOutput = true ,
169+ StandardOutputEncoding = Encoding . ASCII
170+ } ;
171+ var cmd = Process . Start ( steamCmdProc ) ;
172+
173+ // ReSharper disable once PossibleNullReferenceException
174+ while ( ! cmd . HasExited )
175+ {
176+ log . Info ( cmd . StandardOutput . ReadLine ( ) ) ;
177+ Thread . Sleep ( 100 ) ;
178+ }
179+ }
192180
193- Log . Fatal ( ex ) ;
194- if ( ex is ReflectionTypeLoadException exti )
195- foreach ( Exception exl in exti . LoaderExceptions )
196- LogException ( exl ) ;
181+ private void LogException ( Exception ex )
182+ {
183+ if ( ex . InnerException != null )
184+ {
185+ LogException ( ex . InnerException ) ;
197186 }
198187
199- private void HandleException ( object sender , UnhandledExceptionEventArgs e )
188+ Log . Fatal ( ex ) ;
189+ if ( ex is ReflectionTypeLoadException exti )
190+ foreach ( Exception exl in exti . LoaderExceptions )
191+ LogException ( exl ) ;
192+ }
193+
194+ private void HandleException ( object sender , UnhandledExceptionEventArgs e )
195+ {
196+ var ex = ( Exception ) e . ExceptionObject ;
197+ LogException ( ex ) ;
198+ LogManager . Flush ( ) ;
199+ if ( _config . RestartOnCrash )
200200 {
201- var ex = ( Exception ) e . ExceptionObject ;
202- LogException ( ex ) ;
203- Console . WriteLine ( "Exiting in 5 seconds." ) ;
201+ Console . WriteLine ( "Restarting in 5 seconds." ) ;
204202 Thread . Sleep ( 5000 ) ;
205- LogManager . Flush ( ) ;
206- if ( _config . RestartOnCrash )
207- {
208- var exe = typeof ( Program ) . Assembly . Location ;
209- _config . WaitForPID = Process . GetCurrentProcess ( ) . Id . ToString ( ) ;
210- Process . Start ( exe , _config . ToString ( ) ) ;
211- }
212-
213- Process . GetCurrentProcess ( ) . Kill ( ) ;
203+ var exe = typeof ( Program ) . Assembly . Location ;
204+ _config . WaitForPID = Process . GetCurrentProcess ( ) . Id . ToString ( ) ;
205+ Process . Start ( exe , _config . ToString ( ) ) ;
214206 }
207+ else
208+ {
209+ MessageBox . Show ( "Torch encountered a fatal error and needs to close. Please check the logs for details." ) ;
210+ }
211+
212+ Process . GetCurrentProcess ( ) . Kill ( ) ;
215213 }
216- }
214+ }
215+ }
0 commit comments