1212using Chromatics . Interfaces ;
1313using Chromatics . Enums ;
1414using System . Threading ;
15- using Chromatics . Extensions . RGB . NET ;
1615using RGB . NET . Core ;
17- using RGB . NET . Presets . Decorators ;
18- using RGB . NET . Presets . Textures . Gradients ;
19- using RGB . NET . Presets . Textures ;
2016using Chromatics . Extensions . RGB . NET . Decorators ;
21- using static MetroFramework . Drawing . MetroPaint ;
22- using System . Security . Policy ;
23- using Sharlayan . Utilities ;
2417using Sharlayan . Core . Enums ;
2518using Chromatics . Extensions . Sharlayan ;
2619
@@ -30,15 +23,19 @@ namespace Chromatics.Core
3023
3124 public static class GameController
3225 {
26+ private static LayerProcessorFactory _layerProcessorFactory ;
3327 private static MemoryHandler _memoryHandler ;
3428 public static event JobChanged jobChanged ;
3529 private static CustomComparers . LayerComparer comparer = new ( ) ;
3630 private static CancellationTokenSource _GameConnectionCancellationTokenSource = new CancellationTokenSource ( ) ;
3731 private static CancellationTokenSource _GameLoopCancellationTokenSource = new CancellationTokenSource ( ) ;
32+ private static CancellationTokenSource _masterCancellationToken = new CancellationTokenSource ( ) ;
3833 private static Actor . Job _currentJob ;
34+ private static SharlayanConfiguration _configuration ;
3935 private static readonly int _loopInterval = 200 ;
4036 private static readonly int _connectionInterval = 10000 ;
4137 private static int _connectionAttempts = 0 ;
38+ private static int activeProcessId ;
4239 private static bool gameConnected ;
4340 private static bool gameSetup ;
4441 private static bool memoryEfficientLoop ;
@@ -49,30 +46,53 @@ public static void Setup()
4946 {
5047 if ( gameSetup ) return ;
5148
49+ _layerProcessorFactory = LayerProcessorFactory . Instance ;
5250 comparer = new CustomComparers . LayerComparer ( ) ;
5351
52+
5453 if ( ! gameConnected )
5554 {
5655 RGBController . StopEffects ( ) ;
5756 RGBController . RunStartupEffects ( ) ;
58- Task . Run ( ( ) => GameConnectionLoop ( _GameConnectionCancellationTokenSource . Token ) ) ;
57+ Task . Run ( ( ) => GameConnectionLoop ( _GameConnectionCancellationTokenSource . Token ) ) . ContinueWith ( t =>
58+ {
59+ if ( t . IsFaulted )
60+ {
61+ Logger . WriteConsole ( LoggerTypes . Error , $ "GameConnectionLoop task failed: { t . Exception ? . GetBaseException ( ) . Message } ") ;
62+ }
63+ } , _masterCancellationToken . Token ) ;
5964 }
6065
6166 gameSetup = true ;
6267 }
6368
6469 public static void Exit ( )
6570 {
71+ StopGameLoop ( ) ;
6672 _GameConnectionCancellationTokenSource . Cancel ( ) ;
6773 _GameLoopCancellationTokenSource . Cancel ( ) ;
74+ _masterCancellationToken . Cancel ( ) ;
75+ _GameConnectionCancellationTokenSource . Dispose ( ) ;
76+ _GameLoopCancellationTokenSource . Dispose ( ) ;
77+ _masterCancellationToken . Dispose ( ) ;
6878 }
6979
7080 public static void Stop ( bool reconnect = false )
7181 {
7282 RGBController . StopEffects ( ) ;
7383 Logger . WriteConsole ( LoggerTypes . FFXIV , @"Stopping FFXIV Connection.." ) ;
84+
85+ if ( jobChanged != null )
86+ {
87+ foreach ( Delegate d in jobChanged . GetInvocationList ( ) )
88+ {
89+ jobChanged -= ( JobChanged ) d ;
90+ }
91+ }
92+
7493 StopGameLoop ( reconnect ) ;
7594 _GameConnectionCancellationTokenSource . Cancel ( ) ;
95+
7696 }
7797
7898 public static bool IsGameConnected ( )
@@ -117,29 +137,36 @@ private static void StartGameLoop()
117137 {
118138 _GameLoopCancellationTokenSource . Dispose ( ) ;
119139 _GameLoopCancellationTokenSource = new CancellationTokenSource ( ) ;
120- Task . Run ( ( ) => GameLoop ( _GameLoopCancellationTokenSource . Token ) ) ;
140+ Task . Run ( ( ) => GameLoop ( _GameLoopCancellationTokenSource . Token ) , _masterCancellationToken . Token ) ;
121141 }
122142
123143 private static void StopGameLoop ( bool reconnect = false )
124144 {
125145 _GameLoopCancellationTokenSource . Cancel ( ) ;
146+ _memoryHandler ? . Dispose ( ) ;
126147
127- if ( _memoryHandler != null )
148+ if ( activeProcessId != - 1 )
128149 {
129- #if DEBUG
130- Debug . WriteLine ( @"Disposed Memory Handler object." ) ;
131- #endif
132-
133- _memoryHandler . Dispose ( ) ;
150+ SharlayanMemoryManager . Instance . RemoveHandler ( activeProcessId ) ;
151+ activeProcessId = - 1 ;
134152 }
135153
154+ _configuration . ProcessModel . Process ? . Dispose ( ) ;
155+ _configuration = null ;
156+
157+ _masterCancellationToken . Cancel ( ) ;
158+ _masterCancellationToken . Dispose ( ) ;
159+ _masterCancellationToken = new CancellationTokenSource ( ) ;
160+
161+ _layerProcessorFactory . DisposeAll ( ) ;
162+
136163 if ( reconnect )
137164 {
138165 _GameConnectionCancellationTokenSource . Dispose ( ) ;
139166 _GameConnectionCancellationTokenSource = new CancellationTokenSource ( ) ;
140167 RGBController . StopEffects ( ) ;
141168 RGBController . RunStartupEffects ( ) ;
142- Task . Run ( ( ) => GameConnectionLoop ( _GameConnectionCancellationTokenSource . Token ) ) ;
169+ Task . Run ( ( ) => GameConnectionLoop ( _GameConnectionCancellationTokenSource . Token ) , _masterCancellationToken . Token ) ;
143170 }
144171 }
145172
@@ -272,7 +299,7 @@ private static void ConnectFFXIVClient()
272299 Process = process
273300 } ;
274301
275- SharlayanConfiguration configuration = new SharlayanConfiguration
302+ _configuration = new SharlayanConfiguration
276303 {
277304 ProcessModel = processModel ,
278305 GameLanguage = gameLanguage ,
@@ -284,7 +311,7 @@ private static void ConnectFFXIVClient()
284311#if DEBUG
285312 Debug . WriteLine ( $ "Using Local Cache: { AppSettings . GetSettings ( ) . localcache } ") ;
286313#endif
287- _memoryHandler = SharlayanMemoryManager . Instance . AddHandler ( configuration ) ;
314+ _memoryHandler = SharlayanMemoryManager . Instance . AddHandler ( _configuration ) ;
288315
289316 //Load Other Memory Zones
290317 DutyFinderBellExtension . RefreshData ( _memoryHandler ) ;
@@ -293,6 +320,7 @@ private static void ConnectFFXIVClient()
293320 MusicExtension . RefreshData ( _memoryHandler ) ;
294321
295322 gameConnected = true ;
323+ activeProcessId = _configuration . ProcessModel . ProcessID ;
296324
297325 }
298326
@@ -314,6 +342,8 @@ private static void ConnectFFXIVClient()
314342 Debug . WriteLine ( $ "Found { location . Key } . Location: { location . Value . GetAddress ( ) . ToInt64 ( ) : X} ") ;
315343 }
316344#endif
345+
346+ GC . Collect ( ) ;
317347 }
318348 }
319349 catch ( Exception ex )
@@ -392,6 +422,9 @@ private static void GameProcessLayers()
392422 Debug . WriteLine ( @"User on title or character screen" ) ;
393423#endif
394424
425+ _layerProcessorFactory . DisposeAll ( ) ;
426+ GC . Collect ( ) ;
427+
395428 _onTitle = true ;
396429 wasPreviewed = false ;
397430 }
@@ -413,6 +446,7 @@ private static void GameProcessLayers()
413446 RGBController . StopEffects ( ) ;
414447 RGBController . ResetLayerGroups ( ) ;
415448 _onTitle = false ;
449+ GC . Collect ( ) ;
416450 }
417451
418452 }
@@ -443,27 +477,23 @@ private static void GameProcessLayers()
443477 switch ( layer . rootLayerType )
444478 {
445479 case LayerType . BaseLayer :
446-
447- var baseLayerProcessors = BaseLayerProcessorFactory . GetProcessors ( ) ;
448- baseLayerProcessors [ ( BaseLayerType ) layer . layerTypeindex ] . Process ( layer ) ;
480+ var baseProcessor = _layerProcessorFactory . GetProcessor ( ( BaseLayerType ) layer . layerTypeindex ) ;
481+ baseProcessor . Process ( layer ) ;
449482 break ;
450483
451484 case LayerType . DynamicLayer :
452-
453- var dynamicLayerProcessors = DynamicLayerProcessorFactory . GetProcessors ( ) ;
454- dynamicLayerProcessors [ ( DynamicLayerType ) layer . layerTypeindex ] . Process ( layer ) ;
485+ var dynamicProcessor = _layerProcessorFactory . GetProcessor ( ( DynamicLayerType ) layer . layerTypeindex ) ;
486+ dynamicProcessor . Process ( layer ) ;
455487 break ;
456488
457489 case LayerType . EffectLayer :
458- foreach ( var layerProcessor in EffectLayerProcessorFactory . GetProcessors ( ) )
490+ var effectProcessors = EffectLayerProcessorFactory . GetProcessors ( ) ;
491+ foreach ( var effectProcessor in effectProcessors )
459492 {
460- layerProcessor . Value . Process ( layer ) ;
493+ effectProcessor . Value . Process ( layer ) ;
461494 }
462495 break ;
463-
464496 }
465-
466-
467497 }
468498 }
469499 catch ( Exception ex )
0 commit comments