Only clear asset bundle cache when changing star system

This commit is contained in:
Nick 2023-08-24 13:00:11 -04:00
parent eef5323869
commit 588995bb9b
3 changed files with 28 additions and 11 deletions

View File

@ -164,7 +164,7 @@ namespace NewHorizons.Builder.Body
{ {
if (!string.IsNullOrWhiteSpace(config.ShockEffect.assetBundle) && !string.IsNullOrWhiteSpace(config.ShockEffect.meshPath)) if (!string.IsNullOrWhiteSpace(config.ShockEffect.assetBundle) && !string.IsNullOrWhiteSpace(config.ShockEffect.meshPath))
{ {
var mesh = AssetBundleUtilities.Load<Mesh>(config.ShockEffect.assetBundle, config.ShockEffect.meshPath, mod); var mesh = AssetBundleUtilities.Load<Mesh>(config.ShockEffect.assetBundle, config.ShockEffect.meshPath, mod, config.starSystem);
if (mesh != null) if (mesh != null)
{ {
shockLayer.GetComponent<MeshFilter>().sharedMesh = mesh; shockLayer.GetComponent<MeshFilter>().sharedMesh = mesh;

View File

@ -262,7 +262,7 @@ namespace NewHorizons
SearchUtilities.ClearCache(); SearchUtilities.ClearCache();
ImageUtilities.ClearCache(); ImageUtilities.ClearCache();
AudioUtilities.ClearCache(); AudioUtilities.ClearCache();
AssetBundleUtilities.ClearCache(); AssetBundleUtilities.OnSceneUnloaded();
EnumUtilities.ClearCache(); EnumUtilities.ClearCache();
IsSystemReady = false; IsSystemReady = false;
} }

View File

@ -3,25 +3,42 @@ using OWML.Common;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using UnityEngine; using UnityEngine;
namespace NewHorizons.Utility.Files namespace NewHorizons.Utility.Files
{ {
public static class AssetBundleUtilities public static class AssetBundleUtilities
{ {
public static Dictionary<string, AssetBundle> AssetBundles = new Dictionary<string, AssetBundle>(); public static Dictionary<string, (string starSystem, AssetBundle bundle)> 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"); var (starSystem, bundle) = AssetBundles[key];
else pair.Value.Unload(true); // 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<T>(string assetBundleRelativeDir, string pathInBundle, IModBehaviour mod) where T : UnityEngine.Object public static T Load<T>(string assetBundleRelativeDir, string pathInBundle, IModBehaviour mod) where T : UnityEngine.Object
=> Load<T>(assetBundleRelativeDir, pathInBundle, mod, Main.Instance.CurrentStarSystem);
public static T Load<T>(string assetBundleRelativeDir, string pathInBundle, IModBehaviour mod, string starSystem) where T : UnityEngine.Object
{ {
string key = Path.GetFileName(assetBundleRelativeDir); string key = Path.GetFileName(assetBundleRelativeDir);
T obj; T obj;
@ -32,7 +49,7 @@ namespace NewHorizons.Utility.Files
if (AssetBundles.ContainsKey(key)) if (AssetBundles.ContainsKey(key))
{ {
bundle = AssetBundles[key]; bundle = AssetBundles[key].bundle;
} }
else else
{ {
@ -44,7 +61,7 @@ namespace NewHorizons.Utility.Files
return null; return null;
} }
AssetBundles[key] = bundle; AssetBundles[key] = (starSystem, bundle);
} }
obj = bundle.LoadAsset<T>(pathInBundle); obj = bundle.LoadAsset<T>(pathInBundle);
@ -60,7 +77,7 @@ namespace NewHorizons.Utility.Files
public static GameObject LoadPrefab(string assetBundleRelativeDir, string pathInBundle, IModBehaviour mod) public static GameObject LoadPrefab(string assetBundleRelativeDir, string pathInBundle, IModBehaviour mod)
{ {
var prefab = Load<GameObject>(assetBundleRelativeDir, pathInBundle, mod); var prefab = Load<GameObject>(assetBundleRelativeDir, pathInBundle, mod, Main.Instance.CurrentStarSystem);
prefab.SetActive(false); prefab.SetActive(false);