44using System . IO ;
55using System . Linq ;
66using System . Reflection ;
7- using System . Text ;
87using System . Threading ;
8+ using System . Threading . Tasks ;
99using MultiAdmin . Config ;
1010using MultiAdmin . ConsoleTools ;
1111using MultiAdmin . Features . Attributes ;
@@ -150,7 +150,6 @@ private set
150150 {
151151 MaLogFile = string . IsNullOrEmpty ( LogDirFile ) ? null : string . Format ( LogDirFile , "MA" ) ;
152152 ScpLogFile = string . IsNullOrEmpty ( LogDirFile ) ? null : string . Format ( LogDirFile , "SCP" ) ;
153- ModLogFile = string . IsNullOrEmpty ( LogDirFile ) ? null : string . Format ( LogDirFile , "MODERATOR" ) ;
154153 }
155154 }
156155 }
@@ -164,7 +163,8 @@ private set
164163 public string LogDirFile { get ; private set ; }
165164 public string MaLogFile { get ; private set ; }
166165 public string ScpLogFile { get ; private set ; }
167- public string ModLogFile { get ; private set ; }
166+
167+ private StreamWriter maLogStream ;
168168
169169 public Process GameProcess { get ; private set ; }
170170
@@ -190,17 +190,20 @@ public bool IsGameProcessRunning
190190
191191 private void MainLoop ( )
192192 {
193+ // Creates and starts a timer
193194 Stopwatch timer = new Stopwatch ( ) ;
195+ timer . Restart ( ) ;
196+
194197 while ( IsGameProcessRunning )
195198 {
196- timer . Restart ( ) ;
197-
198199 foreach ( IEventTick tickEvent in tick ) tickEvent . OnTick ( ) ;
199200
200201 timer . Stop ( ) ;
201202
202- // Wait 1 second per tick (calculating how long the tick took and compensating)
203- Thread . Sleep ( Math . Max ( 1000 - timer . Elapsed . Milliseconds , 0 ) ) ;
203+ // Wait the delay per tick (calculating how long the tick took and compensating)
204+ Thread . Sleep ( Math . Max ( ServerConfig . MultiAdminTickDelay . Value - timer . Elapsed . Milliseconds , 0 ) ) ;
205+
206+ timer . Restart ( ) ;
204207
205208 if ( Status == ServerStatus . Restarting && CheckRestartTimeout )
206209 {
@@ -291,6 +294,11 @@ public void StartServer(bool restartOnCrash = true)
291294
292295 try
293296 {
297+ // Set up logging
298+ maLogStream ? . Close ( ) ;
299+ Directory . CreateDirectory ( logDir ) ;
300+ maLogStream = File . AppendText ( MaLogFile ) ;
301+
294302 #region Startup Info Printing & Logging
295303
296304 WriteConfigInformation ( ) ;
@@ -380,15 +388,18 @@ public void StartServer(bool restartOnCrash = true)
380388
381389 Write ( $ "Starting server with the following parameters:\n { scpslExe } { startInfo . Arguments } ") ;
382390
391+ // Reset the supported mod features
392+ supportedModFeatures = ModFeatures . None ;
393+
383394 ForEachHandler < IEventServerPreStart > ( eventPreStart => eventPreStart . OnServerPreStart ( ) ) ;
384395
385396 // Start the input reader
386- Thread inputHandlerThread = null ;
397+ CancellationTokenSource inputHandlerCancellation = new CancellationTokenSource ( ) ;
398+ Task inputHandler = null ;
387399
388400 if ( ! Program . Headless )
389401 {
390- inputHandlerThread = new Thread ( ( ) => InputHandler . Write ( this ) ) ;
391- inputHandlerThread . Start ( ) ;
402+ inputHandler = Task . Run ( ( ) => InputHandler . Write ( this , inputHandlerCancellation . Token ) , inputHandlerCancellation . Token ) ;
392403 }
393404
394405 // Start the output reader
@@ -437,9 +448,19 @@ public void StartServer(bool restartOnCrash = true)
437448 GameProcess = null ;
438449
439450 // Stop the input handler if it's running
440- if ( inputHandlerThread != null && inputHandlerThread . IsAlive )
451+ if ( inputHandler != null )
441452 {
442- inputHandlerThread . Abort ( ) ;
453+ inputHandlerCancellation . Cancel ( ) ;
454+ try
455+ {
456+ inputHandler . Wait ( ) ;
457+ }
458+ catch ( Exception )
459+ {
460+ // Task was cancelled or disposed, this is fine since we're waiting for that
461+ }
462+ inputHandler . Dispose ( ) ;
463+ inputHandlerCancellation . Dispose ( ) ;
443464 }
444465
445466 consoleSocket . Disconnect ( ) ;
@@ -487,6 +508,10 @@ public void StartServer(bool restartOnCrash = true)
487508 }
488509 }
489510 } while ( shouldRestart ) ;
511+
512+ // Finish server instance
513+ maLogStream ? . Close ( ) ;
514+ maLogStream = null ;
490515 }
491516
492517 public void SetStopStatus ( bool killGame = false )
@@ -625,10 +650,10 @@ public void Write(ColoredMessage[] messages, ConsoleColor? timeStampColor = null
625650
626651 ColoredMessage [ ] timeStampedMessage = Utils . TimeStampMessage ( messages , timeStampColor ) ;
627652
628- timeStampedMessage . WriteLine ( ServerConfig . UseNewInputSystem . Value ) ;
653+ timeStampedMessage . WriteLine ( ! ServerConfig . HideInput . Value && ServerConfig . UseNewInputSystem . Value ) ;
629654
630- if ( ServerConfig . UseNewInputSystem . Value )
631- InputHandler . WriteInputAndSetCursor ( ) ;
655+ if ( ! ServerConfig . HideInput . Value && ServerConfig . UseNewInputSystem . Value )
656+ InputHandler . WriteInputAndSetCursor ( ServerConfig ) ;
632657 }
633658 }
634659
@@ -657,14 +682,10 @@ public void Log(string message)
657682
658683 try
659684 {
660- Directory . CreateDirectory ( logDir ) ;
661-
662- using ( StreamWriter sw = File . AppendText ( MaLogFile ) )
663- {
664- message = Utils . TimeStampMessage ( message ) ;
665- sw . Write ( message ) ;
666- if ( ! message . EndsWith ( Environment . NewLine ) ) sw . WriteLine ( ) ;
667- }
685+ message = Utils . TimeStampMessage ( message ) ;
686+ maLogStream . Write ( message ) ;
687+ if ( ! message . EndsWith ( Environment . NewLine ) ) maLogStream . WriteLine ( ) ;
688+ maLogStream . Flush ( ) ;
668689 }
669690 catch ( Exception e )
670691 {
0 commit comments