mirror of
https://github.com/Outer-Wilds-New-Horizons/new-horizons.git
synced 2025-12-11 20:15:44 +01:00
added a cache to new horizons bodies that get loaded on body creation and then released
This commit is contained in:
parent
b9d4eda6c6
commit
c9e46050d2
@ -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<NHAstroObject>();
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
30
NewHorizons/Utility/Cache.cs
Normal file
30
NewHorizons/Utility/Cache.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user