Skip to content

Commit 594f09b

Browse files
committed
Moving data files into <KSP_ROOT>/PluginData where God inteded them to be.
Adding hard dependency to KSPe/L
1 parent 4532370 commit 594f09b

5 files changed

Lines changed: 62 additions & 62 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ ModuleManager is mod that let you write patch file that edit other part at load
1515

1616
ModuleManager is mod that let you write patch file that edit other part at load time. With is you can edit squad (and other mod) part without overwriting their file.
1717

18-
### Installation
18+
## Installation
1919

2020
To install, place the GameData folder inside your Kerbal Space Program folder. If asked to overwrite files, please do so.
2121

2222
**REMOVE ANY OLD VERSIONS OF THE DLL BEFORE INSTALLING**.
2323

24-
#### Dependencies
24+
### Dependencies
2525

2626
<!-- * [KSP API Extensions/L](https://github.com/net-lisias-ksp/KSPAPIExtensions) -->
2727
None at the moment.

Source/ModuleManager/CustomConfigsManager.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,16 @@ public class CustomConfigsManager : MonoBehaviour
99
{
1010
internal void Start()
1111
{
12-
if (HighLogic.CurrentGame.Parameters.Career.TechTreeUrl != MMPatchLoader.techTreeFile && File.Exists(MMPatchLoader.techTreePath))
12+
if (HighLogic.CurrentGame.Parameters.Career.TechTreeUrl != MMPatchLoader.TECHTREE_CONFIG.Path && MMPatchLoader.TECHTREE_CONFIG.IsLoadable)
1313
{
1414
Log("Setting modded tech tree as the active one");
15-
HighLogic.CurrentGame.Parameters.Career.TechTreeUrl = MMPatchLoader.techTreeFile;
15+
HighLogic.CurrentGame.Parameters.Career.TechTreeUrl = MMPatchLoader.TECHTREE_CONFIG.Path;
1616
}
1717

18-
if (PhysicsGlobals.PhysicsDatabaseFilename != MMPatchLoader.physicsFile && File.Exists(MMPatchLoader.physicsPath))
18+
if (PhysicsGlobals.PhysicsDatabaseFilename != MMPatchLoader.PHYSICS_CONFIG.Path && MMPatchLoader.PHYSICS_CONFIG.IsLoadable)
1919
{
2020
Log("Setting modded physics as the active one");
21-
22-
PhysicsGlobals.PhysicsDatabaseFilename = MMPatchLoader.physicsFile;
23-
21+
PhysicsGlobals.PhysicsDatabaseFilename = MMPatchLoader.PHYSICS_CONFIG.Path;
2422
if (!PhysicsGlobals.Instance.LoadDatabase())
2523
Log("Something went wrong while setting the active physics config.");
2624
}

Source/ModuleManager/MMPatchLoader.cs

Lines changed: 51 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Text.RegularExpressions;
1212
using UnityEngine;
1313
using Debug = UnityEngine.Debug;
14+
using KSPe;
1415

1516
using ModuleManager.Logging;
1617
using 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");

Source/ModuleManager/ModuleManager.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@
9292
<Reference Include="UnityEngine">
9393
<HintPath>..\..\..\..\..\LIB\managed\1.4.1\UnityEngine.dll</HintPath>
9494
</Reference>
95+
<Reference Include="KSPe">
96+
<HintPath>..\..\..\..\..\LIB\plugins\KSPe.dll</HintPath>
97+
</Reference>
9598
</ItemGroup>
9699
<ItemGroup>
97100
<EmbeddedResource Include="Properties\Resources.resx" />

Source/ModuleManager/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@
2626
//[assembly: AssemblyDelaySign(false)]
2727
//[assembly: AssemblyKeyFile("")]
2828

29+
[assembly: KSPAssemblyDependency("KSPe", 2, 0)]
30+

0 commit comments

Comments
 (0)