1111using System . Text . RegularExpressions ;
1212using UnityEngine ;
1313using Debug = UnityEngine . Debug ;
14+ using KSPe ;
1415
1516using ModuleManager . Logging ;
1617using ModuleManager . Extensions ;
@@ -27,6 +28,13 @@ namespace ModuleManager
2728 [ SuppressMessage ( "ReSharper" , "StringIndexOfIsCultureSpecific.1" ) ]
2829 public class MMPatchLoader : LoadingSystem
2930 {
31+ private static readonly PluginConfig SHA_CONFIG = PluginConfig . ForType < ModuleManager > ( "ConfigSHA" ) ;
32+ private static readonly PluginConfig CACHE_CONFIG = PluginConfig . ForType < ModuleManager > ( "ConfigCache" ) ;
33+ internal static readonly PluginConfig TECHTREE_CONFIG = PluginConfig . ForType < ModuleManager > ( "TechTree" ) ;
34+ internal static readonly PluginConfig PHYSICS_CONFIG = PluginConfig . ForType < ModuleManager > ( "Physics" ) ;
35+ internal static readonly KspConfig PHYSICS_DEFAULT = new KspConfig ( "Physics" ) ;
36+ internal static readonly KspConfig PART_DATABASE = new KspConfig ( "PartDatabase" ) ;
37+
3038 public string status = "" ;
3139
3240 public string errors = "" ;
@@ -37,19 +45,6 @@ public class MMPatchLoader : LoadingSystem
3745
3846 private static readonly Dictionary < string , Regex > regexCache = new Dictionary < string , Regex > ( ) ;
3947
40- private static string cachePath ;
41-
42- internal static string techTreeFile ;
43- internal static string techTreePath ;
44-
45- internal static string physicsFile ;
46- internal static string physicsPath ;
47- private static string defaultPhysicsPath ;
48-
49- internal static string partDatabasePath ;
50-
51- private static string shaPath ;
52-
5348 private UrlDir . UrlFile physicsUrlFile ;
5449
5550 private string configSha ;
@@ -79,15 +74,6 @@ private void Awake()
7974 Instance = this ;
8075 DontDestroyOnLoad ( gameObject ) ;
8176
82- cachePath = Path . Combine ( Path . Combine ( KSPUtil . ApplicationRootPath , "GameData" ) , "ModuleManager.ConfigCache" ) ;
83- techTreeFile = Path . Combine ( "GameData" , "ModuleManager.TechTree" ) ;
84- techTreePath = Path . Combine ( KSPUtil . ApplicationRootPath , techTreeFile ) ;
85- physicsFile = Path . Combine ( "GameData" , "ModuleManager.Physics" ) ;
86- physicsPath = Path . Combine ( KSPUtil . ApplicationRootPath , physicsFile ) ;
87- defaultPhysicsPath = Path . Combine ( KSPUtil . ApplicationRootPath , "Physics.cfg" ) ;
88- partDatabasePath = Path . Combine ( KSPUtil . ApplicationRootPath , "PartDatabase.cfg" ) ;
89- shaPath = Path . Combine ( Path . Combine ( KSPUtil . ApplicationRootPath , "GameData" ) , "ModuleManager.ConfigSHA" ) ;
90-
9177 logger = new ModLogger ( "ModuleManager" , new UnityLogger ( Debug . unityLogger ) ) ;
9278 }
9379
@@ -161,8 +147,8 @@ private IEnumerator ProcessPatch()
161147 yield return null ;
162148
163149 // If we don't use the cache then it is best to clean the PartDatabase.cfg
164- if ( ! keepPartDB && File . Exists ( partDatabasePath ) )
165- File . Delete ( partDatabasePath ) ;
150+ if ( ! keepPartDB && PART_DATABASE . IsLoadable )
151+ File . Delete ( PART_DATABASE . Path ) ;
166152
167153 LoadPhysicsConfig ( ) ;
168154
@@ -270,10 +256,8 @@ float updateTimeRemaining()
270256 logger . Warning ( "Errors in patch prevents the creation of the cache" ) ;
271257 try
272258 {
273- if ( File . Exists ( cachePath ) )
274- File . Delete ( cachePath ) ;
275- if ( File . Exists ( shaPath ) )
276- File . Delete ( shaPath ) ;
259+ CACHE_CONFIG . Destroy ( ) ;
260+ SHA_CONFIG . Destroy ( ) ;
277261 }
278262 catch ( Exception e )
279263 {
@@ -398,11 +382,11 @@ private void LoadPhysicsConfig()
398382 logger . Info ( "Loading Physics.cfg" ) ;
399383 UrlDir gameDataDir = GameDatabase . Instance . root . AllDirectories . First ( d => d . path . EndsWith ( "GameData" ) && d . name == "" && d . url == "" ) ;
400384 // need to use a file with a cfg extension to get the right fileType or you can't AddConfig on it
401- physicsUrlFile = new UrlDir . UrlFile ( gameDataDir , new FileInfo ( defaultPhysicsPath ) ) ;
385+ physicsUrlFile = new UrlDir . UrlFile ( gameDataDir , new FileInfo ( PHYSICS_DEFAULT . Path ) ) ;
402386 // Since it loaded the default config badly (sub node only) we clear it first
403387 physicsUrlFile . configs . Clear ( ) ;
404388 // And reload it properly
405- ConfigNode physicsContent = ConfigNode . Load ( defaultPhysicsPath ) ;
389+ ConfigNode physicsContent = ConfigNode . Load ( PHYSICS_DEFAULT . Path ) ;
406390 physicsContent . name = "PHYSICSGLOBALS" ;
407391 physicsUrlFile . AddConfig ( physicsContent ) ;
408392 gameDataDir . files . Add ( physicsUrlFile ) ;
@@ -424,7 +408,7 @@ private void SaveModdedPhysics()
424408 logger . Info ( configs . Count + " PHYSICSGLOBALS node found. A patch may be wrong. Using the first one" ) ;
425409 }
426410
427- configs [ 0 ] . config . Save ( physicsPath ) ;
411+ configs [ 0 ] . config . Save ( PHYSICS_CONFIG . Path ) ;
428412 }
429413
430414 private void IsCacheUpToDate ( )
@@ -477,26 +461,41 @@ private void IsCacheUpToDate()
477461 logger . Info ( " SHA = " + configSha ) ;
478462
479463 useCache = false ;
480- if ( File . Exists ( shaPath ) )
464+ try
481465 {
482- ConfigNode shaConfigNode = ConfigNode . Load ( shaPath ) ;
483- if ( shaConfigNode != null && shaConfigNode . HasValue ( "SHA" ) && shaConfigNode . HasValue ( "version" ) && shaConfigNode . HasValue ( "KSPVersion" ) )
466+ SHA_CONFIG . Load ( ) ;
467+ logger . Info ( "ConfigSHA loaded" ) ;
468+ if ( SHA_CONFIG . Node . HasValue ( "SHA" ) && SHA_CONFIG . Node . HasValue ( "version" ) && SHA_CONFIG . Node . HasValue ( "KSPVersion" ) )
484469 {
485- string storedSHA = shaConfigNode . GetValue ( "SHA" ) ;
486- string version = shaConfigNode . GetValue ( "version" ) ;
487- string kspVersion = shaConfigNode . GetValue ( "KSPVersion" ) ;
488- ConfigNode filesShaNode = shaConfigNode . GetNode ( "FilesSHA" ) ;
470+ string storedSHA = SHA_CONFIG . Node . GetValue ( "SHA" ) ;
471+ string version = SHA_CONFIG . Node . GetValue ( "version" ) ;
472+ string kspVersion = SHA_CONFIG . Node . GetValue ( "KSPVersion" ) ;
473+ ConfigNode filesShaNode = SHA_CONFIG . Node . GetNode ( "FilesSHA" ) ;
489474 useCache = CheckFilesChange ( files , filesShaNode ) ;
490475 useCache = useCache && storedSHA . Equals ( configSha ) ;
491476 useCache = useCache && version . Equals ( Assembly . GetExecutingAssembly ( ) . GetName ( ) . Version . ToString ( ) ) ;
492477 useCache = useCache && kspVersion . Equals ( Versioning . version_major + "." + Versioning . version_minor + "." + Versioning . Revision + "." + Versioning . BuildID ) ;
493- useCache = useCache && File . Exists ( cachePath ) ;
494- useCache = useCache && File . Exists ( physicsPath ) ;
495- useCache = useCache && File . Exists ( techTreePath ) ;
478+ useCache = useCache && CACHE_CONFIG . IsLoadable ;
479+ useCache = useCache && PHYSICS_CONFIG . IsLoadable ;
480+ useCache = useCache && TECHTREE_CONFIG . IsLoadable ;
496481 logger . Info ( "Cache SHA = " + storedSHA ) ;
497- logger . Info ( "useCache = " + useCache ) ;
498482 }
499483 }
484+ catch ( Exception e )
485+ {
486+ logger . Error ( "Error while checking cache: " + e . ToString ( ) ) ;
487+ useCache = false ;
488+ }
489+ #if false
490+ // Check is this is really a good idea. Would make debugging very difficult on field...
491+ if ( ! useCache ) {
492+ SHA_CONFIG . Destroy ( ) ;
493+ CACHE_CONFIG . Destroy ( ) ;
494+ PHYSICS_CONFIG . Destroy ( ) ;
495+ TECHTREE_CONFIG . Destroy ( ) ;
496+ }
497+ #endif
498+ logger . Info ( "useCache = " + useCache ) ;
500499 }
501500
502501 private bool CheckFilesChange ( UrlDir . UrlFile [ ] files , ConfigNode shaConfigNode )
@@ -590,15 +589,15 @@ private void CreateCache(int patchedNodeCount)
590589
591590 try
592591 {
593- shaConfigNode . Save ( shaPath ) ;
592+ SHA_CONFIG . Save ( shaConfigNode ) ;
594593 }
595594 catch ( Exception e )
596595 {
597596 logger . Exception ( "Exception while saving the sha" , e ) ;
598597 }
599598 try
600599 {
601- cache . Save ( cachePath ) ;
600+ CACHE_CONFIG . Save ( cache ) ;
602601 return ;
603602 }
604603 catch ( NullReferenceException e )
@@ -613,10 +612,8 @@ private void CreateCache(int patchedNodeCount)
613612 try
614613 {
615614 logger . Error ( "An error occured while creating the cache. Deleting the cache files to avoid keeping a bad cache" ) ;
616- if ( File . Exists ( cachePath ) )
617- File . Delete ( cachePath ) ;
618- if ( File . Exists ( shaPath ) )
619- File . Delete ( shaPath ) ;
615+ CACHE_CONFIG . Destroy ( ) ;
616+ SHA_CONFIG . Destroy ( ) ;
620617 }
621618 catch ( Exception e )
622619 {
@@ -641,7 +638,7 @@ private void SaveModdedTechTree()
641638
642639 ConfigNode techNode = new ConfigNode ( "TechTree" ) ;
643640 techNode . AddNode ( configs [ 0 ] . config ) ;
644- techNode . Save ( techTreePath ) ;
641+ techNode . Save ( TECHTREE_CONFIG . Path ) ;
645642 }
646643
647644 private void LoadCache ( )
@@ -653,18 +650,18 @@ private void LoadCache()
653650 }
654651
655652 // And then load all the cached configs
656- ConfigNode cache = ConfigNode . Load ( cachePath ) ;
657-
658- if ( cache . HasValue ( "patchedNodeCount" ) && int . TryParse ( cache . GetValue ( "patchedNodeCount" ) , out int patchedNodeCount ) )
653+ CACHE_CONFIG . Load ( ) ;
654+
655+ if ( CACHE_CONFIG . Node . HasValue ( "patchedNodeCount" ) && int . TryParse ( CACHE_CONFIG . Node . GetValue ( "patchedNodeCount" ) , out int patchedNodeCount ) )
659656 status = "ModuleManager: " + patchedNodeCount + " patch" + ( patchedNodeCount != 1 ? "es" : "" ) + " loaded from cache" ;
660657
661658 // Create the fake file where we load the physic config cache
662659 UrlDir gameDataDir = GameDatabase . Instance . root . AllDirectories . First ( d => d . path . EndsWith ( "GameData" ) && d . name == "" && d . url == "" ) ;
663660 // need to use a file with a cfg extension to get the right fileType or you can't AddConfig on it
664- physicsUrlFile = new UrlDir . UrlFile ( gameDataDir , new FileInfo ( defaultPhysicsPath ) ) ;
661+ physicsUrlFile = new UrlDir . UrlFile ( gameDataDir , new FileInfo ( PHYSICS_DEFAULT . Path ) ) ;
665662 gameDataDir . files . Add ( physicsUrlFile ) ;
666663
667- foreach ( ConfigNode node in cache . nodes )
664+ foreach ( ConfigNode node in CACHE_CONFIG . Node . nodes )
668665 {
669666 string name = node . GetValue ( "name" ) ;
670667 string type = node . GetValue ( "type" ) ;
0 commit comments