diff --git a/NewHorizons/Handlers/PlanetCreationHandler.cs b/NewHorizons/Handlers/PlanetCreationHandler.cs index e0912246..db5d7430 100644 --- a/NewHorizons/Handlers/PlanetCreationHandler.cs +++ b/NewHorizons/Handlers/PlanetCreationHandler.cs @@ -136,6 +136,8 @@ namespace NewHorizons.Handlers public static bool LoadBody(NewHorizonsBody body, bool defaultPrimaryToSun = false) { + body.LoadCache(); + // I don't remember doing this why is it exceptions what am I doing GameObject existingPlanet = null; try @@ -202,6 +204,7 @@ namespace NewHorizons.Handlers catch (Exception ex) { Logger.LogError($"Couldn't make quantum state for [{body.Config.name}]:\n{ex}"); + body.UnloadCache(); return false; } } @@ -217,6 +220,7 @@ namespace NewHorizons.Handlers catch (Exception e) { Logger.LogError($"Couldn't update body {body.Config?.name}:\n{e}"); + body.UnloadCache(); return false; } } @@ -237,8 +241,12 @@ namespace NewHorizons.Handlers { Logger.Log($"Creating [{body.Config.name}]"); var planetObject = GenerateBody(body, defaultPrimaryToSun); - if (planetObject == null) return false; - planetObject.SetActive(true); + planetObject?.SetActive(true); + if (planetObject == null) + { + body.UnloadCache(); + return false; + } var ao = planetObject.GetComponent(); @@ -250,6 +258,7 @@ namespace NewHorizons.Handlers catch (Exception e) { Logger.LogError($"Couldn't generate body {body.Config?.name}:\n{e}"); + body.UnloadCache(); return false; } } @@ -264,6 +273,7 @@ namespace NewHorizons.Handlers Logger.LogError($"Error in event handler for OnPlanetLoaded on body {body.Config.name}: {e}"); } + body.UnloadCache(); return true; } diff --git a/NewHorizons/Utility/Cache.cs b/NewHorizons/Utility/Cache.cs new file mode 100644 index 00000000..8113575e --- /dev/null +++ b/NewHorizons/Utility/Cache.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; + +namespace NewHorizons.Utility +{ + public class Cache : Dictionary + { + [NonSerialized] string filepath; + + public Cache(string cacheFilePath) + { + filepath = cacheFilePath; + var existingEntries = NewHorizons.Main.Instance.ModHelper.Storage.Load>(filepath); + + if (existingEntries == null) return; + + foreach(var entry in existingEntries) + { + this[entry.Key] = entry.Value; + } + } + + public void WriteToFile() + { + NewHorizons.Main.Instance.ModHelper.Storage.Save>(this, filepath); + } + } +} diff --git a/NewHorizons/Utility/NewHorizonBody.cs b/NewHorizons/Utility/NewHorizonBody.cs index fc9f53bd..074ccf71 100644 --- a/NewHorizons/Utility/NewHorizonBody.cs +++ b/NewHorizons/Utility/NewHorizonBody.cs @@ -17,10 +17,23 @@ namespace NewHorizons.Utility public PlanetConfig Config; public IModBehaviour Mod; + public Cache Cache; public string RelativePath; public GameObject Object; + #region Cache + public void LoadCache() + { + if (RelativePath != null) Cache = new Cache(RelativePath+".nhcache"); + } + + public void UnloadCache() + { + Cache = null; // garbage collection will take care of it + } + #endregion Cache + #region Migration private static readonly string[] _keepLoadedModsList = new string[] { @@ -45,6 +58,7 @@ namespace NewHorizons.Utility } } } + #endregion } }