From 588995bb9b3e23f1d4a7a548bf3cada7c36e3c81 Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 24 Aug 2023 13:00:11 -0400 Subject: [PATCH] Only clear asset bundle cache when changing star system --- .../Builder/Body/SupernovaEffectBuilder.cs | 2 +- NewHorizons/Main.cs | 2 +- .../Utility/Files/AssetBundleUtilities.cs | 35 ++++++++++++++----- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/NewHorizons/Builder/Body/SupernovaEffectBuilder.cs b/NewHorizons/Builder/Body/SupernovaEffectBuilder.cs index 32086640..9b4c18ec 100644 --- a/NewHorizons/Builder/Body/SupernovaEffectBuilder.cs +++ b/NewHorizons/Builder/Body/SupernovaEffectBuilder.cs @@ -164,7 +164,7 @@ namespace NewHorizons.Builder.Body { if (!string.IsNullOrWhiteSpace(config.ShockEffect.assetBundle) && !string.IsNullOrWhiteSpace(config.ShockEffect.meshPath)) { - var mesh = AssetBundleUtilities.Load(config.ShockEffect.assetBundle, config.ShockEffect.meshPath, mod); + var mesh = AssetBundleUtilities.Load(config.ShockEffect.assetBundle, config.ShockEffect.meshPath, mod, config.starSystem); if (mesh != null) { shockLayer.GetComponent().sharedMesh = mesh; diff --git a/NewHorizons/Main.cs b/NewHorizons/Main.cs index e056b4c3..a3ee3d60 100644 --- a/NewHorizons/Main.cs +++ b/NewHorizons/Main.cs @@ -262,7 +262,7 @@ namespace NewHorizons SearchUtilities.ClearCache(); ImageUtilities.ClearCache(); AudioUtilities.ClearCache(); - AssetBundleUtilities.ClearCache(); + AssetBundleUtilities.OnSceneUnloaded(); EnumUtilities.ClearCache(); IsSystemReady = false; } diff --git a/NewHorizons/Utility/Files/AssetBundleUtilities.cs b/NewHorizons/Utility/Files/AssetBundleUtilities.cs index 60fdce56..678f3bc8 100644 --- a/NewHorizons/Utility/Files/AssetBundleUtilities.cs +++ b/NewHorizons/Utility/Files/AssetBundleUtilities.cs @@ -3,25 +3,42 @@ using OWML.Common; using System; using System.Collections.Generic; using System.IO; +using System.Linq; using UnityEngine; namespace NewHorizons.Utility.Files { public static class AssetBundleUtilities { - public static Dictionary AssetBundles = new Dictionary(); + public static Dictionary AssetBundles = new(); - public static void ClearCache() + public static void OnSceneUnloaded() { - foreach (var pair in AssetBundles) + var bundleKeys = AssetBundles.Keys.ToArray(); + + foreach (var key in bundleKeys) { - if (pair.Value == null) NHLogger.LogError($"The asset bundle for {pair.Key} was null when trying to unload"); - else pair.Value.Unload(true); + var (starSystem, bundle) = AssetBundles[key]; + // If the star system is null/empty keep loaded forever, else only unload when we leave the system + if (!string.IsNullOrEmpty(starSystem) && starSystem != Main.Instance.CurrentStarSystem) + { + if (bundle == null) NHLogger.LogError($"The asset bundle for [{key}] was null when trying to unload"); + else bundle.Unload(true); + + AssetBundles.Remove(key); + } + else + { + NHLogger.LogVerbose($"Not unloading bundle [{key}] because it is still in use for system [{starSystem}]"); + } } - AssetBundles.Clear(); } + // On the off chance this was being called from another mod public static T Load(string assetBundleRelativeDir, string pathInBundle, IModBehaviour mod) where T : UnityEngine.Object + => Load(assetBundleRelativeDir, pathInBundle, mod, Main.Instance.CurrentStarSystem); + + public static T Load(string assetBundleRelativeDir, string pathInBundle, IModBehaviour mod, string starSystem) where T : UnityEngine.Object { string key = Path.GetFileName(assetBundleRelativeDir); T obj; @@ -32,7 +49,7 @@ namespace NewHorizons.Utility.Files if (AssetBundles.ContainsKey(key)) { - bundle = AssetBundles[key]; + bundle = AssetBundles[key].bundle; } else { @@ -44,7 +61,7 @@ namespace NewHorizons.Utility.Files return null; } - AssetBundles[key] = bundle; + AssetBundles[key] = (starSystem, bundle); } obj = bundle.LoadAsset(pathInBundle); @@ -60,7 +77,7 @@ namespace NewHorizons.Utility.Files public static GameObject LoadPrefab(string assetBundleRelativeDir, string pathInBundle, IModBehaviour mod) { - var prefab = Load(assetBundleRelativeDir, pathInBundle, mod); + var prefab = Load(assetBundleRelativeDir, pathInBundle, mod, Main.Instance.CurrentStarSystem); prefab.SetActive(false);