store an array in the cache just cuz

This commit is contained in:
JohnCorby 2022-07-09 17:36:15 -07:00
parent 2407ac9fba
commit 34118082d2

View File

@ -1,6 +1,8 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using UnityEngine; using UnityEngine;
using Logger = NewHorizons.Utility.Logger;
namespace NewHorizons.Handlers namespace NewHorizons.Handlers
{ {
/// <summary> /// <summary>
@ -8,13 +10,13 @@ namespace NewHorizons.Handlers
/// </summary> /// </summary>
public static class StreamingHandler public static class StreamingHandler
{ {
private static Dictionary<Material, string> _materialCache; private static readonly Dictionary<Material, string> _materialCache = new();
private static Dictionary<GameObject, List<string>> _objectCache; private static readonly Dictionary<GameObject, string[]> _objectCache = new();
public static void Init() public static void Init()
{ {
_materialCache = new Dictionary<Material, string>(); _materialCache.Clear();
_objectCache = new Dictionary<GameObject, List<string>>(); _objectCache.Clear();
} }
/// <summary> /// <summary>
@ -23,23 +25,17 @@ namespace NewHorizons.Handlers
public static void SetUpStreaming(GameObject obj, Sector sector) public static void SetUpStreaming(GameObject obj, Sector sector)
{ {
// find the asset bundles to load // find the asset bundles to load
List<string> assetBundles; // tries the cache first, then builds
if (_objectCache.ContainsKey(obj)) if (!_objectCache.TryGetValue(obj, out var assetBundles))
{ {
assetBundles = _objectCache[obj]; var assetBundlesList = new List<string>();
}
else
{
assetBundles = new List<string>();
var tables = Resources.FindObjectsOfTypeAll<StreamingMaterialTable>(); var tables = Resources.FindObjectsOfTypeAll<StreamingMaterialTable>();
foreach (var streamingHandle in obj.GetComponentsInChildren<StreamingMeshHandle>()) foreach (var streamingHandle in obj.GetComponentsInChildren<StreamingMeshHandle>())
{ {
var assetBundle = streamingHandle.assetBundle; var assetBundle = streamingHandle.assetBundle;
if (!assetBundles.Contains(assetBundle)) assetBundlesList.SafeAdd(assetBundle);
{
assetBundles.Add(assetBundle);
}
if (streamingHandle is StreamingRenderMeshHandle or StreamingSkinnedMeshHandle) if (streamingHandle is StreamingRenderMeshHandle or StreamingSkinnedMeshHandle)
{ {
var materials = streamingHandle.GetComponent<Renderer>().sharedMaterials; var materials = streamingHandle.GetComponent<Renderer>().sharedMaterials;
@ -49,7 +45,7 @@ namespace NewHorizons.Handlers
// Gonna assume that if theres more than one material its probably in the same asset bundle anyway right // Gonna assume that if theres more than one material its probably in the same asset bundle anyway right
if (_materialCache.TryGetValue(materials[0], out assetBundle)) if (_materialCache.TryGetValue(materials[0], out assetBundle))
{ {
assetBundles.Add(assetBundle); assetBundlesList.SafeAdd(assetBundle);
} }
else else
{ {
@ -60,13 +56,15 @@ namespace NewHorizons.Handlers
if (materials.Contains(x.material)) if (materials.Contains(x.material))
{ {
_materialCache.SafeAdd(x.material, table.assetBundle); _materialCache.SafeAdd(x.material, table.assetBundle);
assetBundles.SafeAdd(table.assetBundle); assetBundlesList.SafeAdd(table.assetBundle);
} }
} }
} }
} }
} }
} }
assetBundles = assetBundlesList.ToArray();
_objectCache[obj] = assetBundles; _objectCache[obj] = assetBundles;
} }
@ -77,6 +75,7 @@ namespace NewHorizons.Handlers
{ {
foreach (var assetBundle in assetBundles) foreach (var assetBundle in assetBundles)
{ {
Logger.LogVerbose($"loading bundle {assetBundle}");
StreamingManager.LoadStreamingAssets(assetBundle); StreamingManager.LoadStreamingAssets(assetBundle);
} }
} }
@ -87,6 +86,7 @@ namespace NewHorizons.Handlers
{ {
foreach (var assetBundle in assetBundles) foreach (var assetBundle in assetBundles)
{ {
Logger.LogVerbose($"loading bundle {assetBundle}");
StreamingManager.LoadStreamingAssets(assetBundle); StreamingManager.LoadStreamingAssets(assetBundle);
} }
} }
@ -94,6 +94,7 @@ namespace NewHorizons.Handlers
{ {
foreach (var assetBundle in assetBundles) foreach (var assetBundle in assetBundles)
{ {
Logger.LogVerbose($"UNloading bundle {assetBundle}");
StreamingManager.UnloadStreamingAssets(assetBundle); StreamingManager.UnloadStreamingAssets(assetBundle);
} }
} }
@ -104,9 +105,10 @@ namespace NewHorizons.Handlers
// just load it immediately and hope for the best // just load it immediately and hope for the best
foreach (var assetBundle in assetBundles) foreach (var assetBundle in assetBundles)
{ {
Logger.LogVerbose($"loading bundle {assetBundle}");
StreamingManager.LoadStreamingAssets(assetBundle); StreamingManager.LoadStreamingAssets(assetBundle);
} }
} }
} }
} }
} }