added a cache to new horizons bodies that get loaded on body creation and then released

This commit is contained in:
FreezeDriedMangoes 2023-01-30 22:51:04 -05:00
parent b9d4eda6c6
commit c9e46050d2
3 changed files with 56 additions and 2 deletions

View File

@ -136,6 +136,8 @@ namespace NewHorizons.Handlers
public static bool LoadBody(NewHorizonsBody body, bool defaultPrimaryToSun = false) 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 // I don't remember doing this why is it exceptions what am I doing
GameObject existingPlanet = null; GameObject existingPlanet = null;
try try
@ -202,6 +204,7 @@ namespace NewHorizons.Handlers
catch (Exception ex) catch (Exception ex)
{ {
Logger.LogError($"Couldn't make quantum state for [{body.Config.name}]:\n{ex}"); Logger.LogError($"Couldn't make quantum state for [{body.Config.name}]:\n{ex}");
body.UnloadCache();
return false; return false;
} }
} }
@ -217,6 +220,7 @@ namespace NewHorizons.Handlers
catch (Exception e) catch (Exception e)
{ {
Logger.LogError($"Couldn't update body {body.Config?.name}:\n{e}"); Logger.LogError($"Couldn't update body {body.Config?.name}:\n{e}");
body.UnloadCache();
return false; return false;
} }
} }
@ -237,8 +241,12 @@ namespace NewHorizons.Handlers
{ {
Logger.Log($"Creating [{body.Config.name}]"); Logger.Log($"Creating [{body.Config.name}]");
var planetObject = GenerateBody(body, defaultPrimaryToSun); 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<NHAstroObject>(); var ao = planetObject.GetComponent<NHAstroObject>();
@ -250,6 +258,7 @@ namespace NewHorizons.Handlers
catch (Exception e) catch (Exception e)
{ {
Logger.LogError($"Couldn't generate body {body.Config?.name}:\n{e}"); Logger.LogError($"Couldn't generate body {body.Config?.name}:\n{e}");
body.UnloadCache();
return false; return false;
} }
} }
@ -264,6 +273,7 @@ namespace NewHorizons.Handlers
Logger.LogError($"Error in event handler for OnPlanetLoaded on body {body.Config.name}: {e}"); Logger.LogError($"Error in event handler for OnPlanetLoaded on body {body.Config.name}: {e}");
} }
body.UnloadCache();
return true; return true;
} }

View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
namespace NewHorizons.Utility
{
public class Cache : Dictionary<string, ISerializable>
{
[NonSerialized] string filepath;
public Cache(string cacheFilePath)
{
filepath = cacheFilePath;
var existingEntries = NewHorizons.Main.Instance.ModHelper.Storage.Load<Dictionary<string, ISerializable>>(filepath);
if (existingEntries == null) return;
foreach(var entry in existingEntries)
{
this[entry.Key] = entry.Value;
}
}
public void WriteToFile()
{
NewHorizons.Main.Instance.ModHelper.Storage.Save<Dictionary<string, ISerializable>>(this, filepath);
}
}
}

View File

@ -17,10 +17,23 @@ namespace NewHorizons.Utility
public PlanetConfig Config; public PlanetConfig Config;
public IModBehaviour Mod; public IModBehaviour Mod;
public Cache Cache;
public string RelativePath; public string RelativePath;
public GameObject Object; 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 #region Migration
private static readonly string[] _keepLoadedModsList = new string[] private static readonly string[] _keepLoadedModsList = new string[]
{ {
@ -45,6 +58,7 @@ namespace NewHorizons.Utility
} }
} }
} }
#endregion #endregion
} }
} }