diff --git a/NewHorizons/Handlers/EyeDetailCacher.cs b/NewHorizons/Handlers/EyeDetailCacher.cs new file mode 100644 index 00000000..6e9faef6 --- /dev/null +++ b/NewHorizons/Handlers/EyeDetailCacher.cs @@ -0,0 +1,39 @@ +using NewHorizons.Utility; +using NewHorizons.Utility.OWML; +using System.Linq; + +namespace NewHorizons.Handlers; + +public static class EyeDetailCacher +{ + public static void Init() + { + foreach (var body in Main.BodyDict["EyeOfTheUniverse"]) + { + NHLogger.LogVerbose($"{nameof(EyeDetailCacher)}: {body.Config.name}"); + if (body.Config?.Props?.details == null) continue; + + foreach (var detail in body.Config.Props.details) + { + NHLogger.LogVerbose($"{nameof(EyeDetailCacher)}: {detail.path}"); + + + if (string.IsNullOrEmpty(detail.path)) continue; + + var planet = detail.path.Contains('/') ? detail.path.Split('/').First() : string.Empty; + + // TODO: what other root paths can we ignore + if (planet != "EyeOfTheUniverse_Body" && planet != "Vessel_Body") + { + NHLogger.LogVerbose($"{nameof(EyeDetailCacher)}: Looking for {detail.path}"); + var obj = SearchUtilities.Find(detail.path); + if (obj != null) + { + NHLogger.LogVerbose($"{nameof(EyeDetailCacher)}: Added solar system asset to dont destroy on load cache for eye: {detail.path}"); + SearchUtilities.AddToDontDestroyOnLoadCache(detail.path, obj); + } + } + } + } + } +} diff --git a/NewHorizons/Main.cs b/NewHorizons/Main.cs index 3b38bd35..ec0325ea 100644 --- a/NewHorizons/Main.cs +++ b/NewHorizons/Main.cs @@ -341,6 +341,8 @@ namespace NewHorizons { try { + EyeDetailCacher.Init(); + AtmosphereBuilder.InitPrefabs(); BrambleDimensionBuilder.InitPrefabs(); BrambleNodeBuilder.InitPrefabs(); diff --git a/NewHorizons/Utility/SearchUtilities.cs b/NewHorizons/Utility/SearchUtilities.cs index 9ed77826..4bd0ca44 100644 --- a/NewHorizons/Utility/SearchUtilities.cs +++ b/NewHorizons/Utility/SearchUtilities.cs @@ -9,9 +9,15 @@ namespace NewHorizons.Utility { public static class SearchUtilities { + private static readonly Dictionary DontDestroyOnLoadCachedGameObjects = new Dictionary(); private static readonly Dictionary CachedGameObjects = new Dictionary(); private static readonly Dictionary CachedRootGameObjects = new Dictionary(); + public static void AddToDontDestroyOnLoadCache(string path, GameObject go) + { + DontDestroyOnLoadCachedGameObjects[path] = go.InstantiateInactive().DontDestroyOnLoad(); + } + public static void ClearCache() { NHLogger.LogVerbose("Clearing search cache"); @@ -96,6 +102,8 @@ namespace NewHorizons.Utility /// public static GameObject Find(string path, bool warn = true) { + if (DontDestroyOnLoadCachedGameObjects.TryGetValue(path, out var gameObject)) return gameObject; + if (CachedGameObjects.TryGetValue(path, out var go)) return go; // 1: normal find