11using System ;
22using System . Collections . Generic ;
3- using System . Diagnostics ;
43using System . Diagnostics . CodeAnalysis ;
54using System . Globalization ;
65using System . IO ;
1514using ModuleManager . Threading ;
1615using ModuleManager . Tags ;
1716using ModuleManager . Patches ;
18- using ModuleManager . Progress ;
1917using NodeStack = ModuleManager . Collections . ImmutableStack < ConfigNode > ;
2018
2119using static ModuleManager . FilePathRepository ;
@@ -41,22 +39,25 @@ public class MMPatchLoader : LoadingSystem
4139
4240 private readonly IEnumerable < ModListGenerator . ModAddedByAssembly > modsAddedByAssemblies ;
4341 private readonly IBasicLogger logger ;
42+ private readonly Progress . ProgressCounter counter ;
43+ private readonly Progress . Timings timings ;
4444
4545 public static void AddPostPatchCallback ( ModuleManagerPostPatchCallback callback )
4646 {
4747 PostPatchLoader . AddPostPatchCallback ( callback ) ;
4848 }
4949
50- public MMPatchLoader ( IEnumerable < ModListGenerator . ModAddedByAssembly > modsAddedByAssemblies , IBasicLogger logger )
50+ public MMPatchLoader ( IEnumerable < ModListGenerator . ModAddedByAssembly > modsAddedByAssemblies , IBasicLogger logger , Progress . ProgressCounter counter , Progress . Timings timings )
5151 {
5252 this . modsAddedByAssemblies = modsAddedByAssemblies ?? throw new ArgumentNullException ( nameof ( modsAddedByAssemblies ) ) ;
5353 this . logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
54+ this . counter = counter ;
55+ this . timings = timings ;
5456 }
5557
5658 public IEnumerable < IProtoUrlConfig > Run ( )
5759 {
58- Stopwatch patchSw = new Stopwatch ( ) ;
59- patchSw . Start ( ) ;
60+ this . timings . Patching . Start ( ) ;
6061
6162 status = "Checking Cache" ;
6263 logger . Info ( status ) ;
@@ -79,7 +80,7 @@ public IEnumerable<IProtoUrlConfig> Run()
7980 {
8081 IBasicLogger patchLogger = new PatchLogger ( FilePathRepository . PATCH_LOG_FILENAME ) ;
8182
82- IPatchProgress progress = new PatchProgress ( patchLogger ) ;
83+ Progress . IPatchProgress progress = new Progress . PatchProgress ( patchLogger , this . counter ) ;
8384 status = "Pre patch init" ;
8485 patchLogger . Info ( status ) ;
8586 IEnumerable < string > mods = ModListGenerator . GenerateModList ( modsAddedByAssemblies , progress , patchLogger ) ;
@@ -148,14 +149,14 @@ public IEnumerable<IProtoUrlConfig> Run()
148149
149150 #region Saving Cache
150151
151- foreach ( KeyValuePair < string , int > item in progress . Counter . warningFiles )
152+ foreach ( KeyValuePair < string , int > item in this . counter . warningFiles )
152153 {
153154 patchLogger . Warning ( item . Value + " warning" + ( item . Value > 1 ? "s" : "" ) + " related to GameData/" + item . Key ) ;
154155 }
155156
156- if ( progress . Counter . errors > 0 || progress . Counter . exceptions > 0 )
157+ if ( this . counter . errors > 0 || this . counter . exceptions > 0 )
157158 {
158- foreach ( KeyValuePair < string , int > item in progress . Counter . errorFiles )
159+ foreach ( KeyValuePair < string , int > item in this . counter . errorFiles )
159160 {
160161 errors += item . Value + " error" + ( item . Value > 1 ? "s" : "" ) + " related to GameData/" + item . Key
161162 + "\n " ;
@@ -175,8 +176,8 @@ public IEnumerable<IProtoUrlConfig> Run()
175176 else
176177 {
177178 status = "Saving Cache" ;
178- patchLogger . Info ( status ) ;
179- CreateCache ( databaseConfigs , progress . Counter . patchedNodes ) ;
179+ CreateCache ( databaseConfigs , this . counter . patchedNodes ) ;
180+ patchLogger . Info ( "Cache saved." ) ;
180181 }
181182
182183 patchLogger . Finish ( ) ;
@@ -220,8 +221,8 @@ public IEnumerable<IProtoUrlConfig> Run()
220221
221222 logger . Info ( status ) ;
222223
223- patchSw . Stop ( ) ;
224- logger . Info ( "Ran in " + ( ( float ) patchSw . ElapsedMilliseconds / 1000 ) . ToString ( "F3" ) + "s" ) ;
224+ this . timings . Patching . Stop ( ) ;
225+ logger . Info ( "Ran in " + this . timings . Patching ) ;
225226
226227 return databaseConfigs ;
227228 }
@@ -262,8 +263,7 @@ private void SaveModdedPhysics(IEnumerable<IProtoUrlConfig> databaseConfigs)
262263
263264 private bool IsCacheUpToDate ( )
264265 {
265- Stopwatch sw = new Stopwatch ( ) ;
266- sw . Start ( ) ;
266+ this . timings . ShaCalc . Start ( ) ;
267267
268268 System . Security . Cryptography . SHA256 sha = System . Security . Cryptography . SHA256 . Create ( ) ;
269269 System . Security . Cryptography . SHA256 filesha = System . Security . Cryptography . SHA256 . Create ( ) ;
@@ -314,9 +314,9 @@ private bool IsCacheUpToDate()
314314 sha . Clear ( ) ;
315315 filesha . Clear ( ) ;
316316
317- sw . Stop ( ) ;
317+ this . timings . ShaCalc . Stop ( ) ;
318318
319- logger . Info ( "SHA generated in " + ( ( float ) sw . ElapsedMilliseconds / 1000 ) . ToString ( "F3" ) + "s" ) ;
319+ logger . Info ( "SHA generated in " + this . timings . ShaCalc ) ;
320320 logger . Info ( " SHA = " + configSha ) ;
321321
322322 bool useCache = false ;
@@ -494,7 +494,10 @@ private IEnumerable<IProtoUrlConfig> LoadCache()
494494 ConfigNode cache = CACHE_CONFIG . Load ( ) . Node ;
495495
496496 if ( cache . HasValue ( "patchedNodeCount" ) && int . TryParse ( cache . GetValue ( "patchedNodeCount" ) , out int patchedNodeCount ) )
497+ {
497498 status = "ModuleManager: " + patchedNodeCount + " patch" + ( patchedNodeCount != 1 ? "es" : "" ) + " loaded from cache" ;
499+ this . counter . patchedNodes . Set ( patchedNodeCount ) ;
500+ }
498501
499502 // Create the fake file where we load the physic config cache
500503 UrlDir gameDataDir = GameDatabase . Instance . root . AllDirectories . First ( d => d . path . EndsWith ( "GameData" ) && d . name == "" && d . url == "" ) ;
@@ -524,23 +527,23 @@ private IEnumerable<IProtoUrlConfig> LoadCache()
524527 return databaseConfigs ;
525528 }
526529
527- private void StatusUpdate ( IPatchProgress progress , string activity = null )
530+ private void StatusUpdate ( Progress . IPatchProgress progress , string activity = null )
528531 {
529- status = "ModuleManager: " + progress . Counter . patchedNodes + " patch" + ( progress . Counter . patchedNodes != 1 ? "es" : "" ) + " applied" ;
532+ status = "ModuleManager: " + this . counter . patchedNodes + " patch" + ( this . counter . patchedNodes != 1 ? "es" : "" ) + " applied" ;
530533 if ( progress . ProgressFraction < 1f - float . Epsilon )
531534 status += " (" + progress . ProgressFraction . ToString ( "P0" ) + ")" ;
532535
533536 if ( activity != null )
534537 status += "\n " + activity ;
535538
536- if ( progress . Counter . warnings > 0 )
537- status += ", found <color=yellow>" + progress . Counter . warnings + " warning" + ( progress . Counter . warnings != 1 ? "s" : "" ) + "</color>" ;
539+ if ( this . counter . warnings > 0 )
540+ status += ", found <color=yellow>" + this . counter . warnings + " warning" + ( this . counter . warnings != 1 ? "s" : "" ) + "</color>" ;
538541
539- if ( progress . Counter . errors > 0 )
540- status += ", found <color=orange>" + progress . Counter . errors + " error" + ( progress . Counter . errors != 1 ? "s" : "" ) + "</color>" ;
542+ if ( this . counter . errors > 0 )
543+ status += ", found <color=orange>" + this . counter . errors + " error" + ( this . counter . errors != 1 ? "s" : "" ) + "</color>" ;
541544
542- if ( progress . Counter . exceptions > 0 )
543- status += ", encountered <color=red>" + progress . Counter . exceptions + " exception" + ( progress . Counter . exceptions != 1 ? "s" : "" ) + "</color>" ;
545+ if ( this . counter . exceptions > 0 )
546+ status += ", encountered <color=red>" + this . counter . exceptions + " exception" + ( this . counter . exceptions != 1 ? "s" : "" ) + "</color>" ;
544547 }
545548
546549 #region Applying Patches
0 commit comments