From 34118082d2e2b06d5209b7004ca0894539c88dad Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Sat, 9 Jul 2022 17:36:15 -0700 Subject: [PATCH] store an array in the cache just cuz --- NewHorizons/Handlers/StreamingHandler.cs | 38 +++++++++++++----------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/NewHorizons/Handlers/StreamingHandler.cs b/NewHorizons/Handlers/StreamingHandler.cs index 19d66991..348f6fff 100644 --- a/NewHorizons/Handlers/StreamingHandler.cs +++ b/NewHorizons/Handlers/StreamingHandler.cs @@ -1,6 +1,8 @@ using System.Collections.Generic; using System.Linq; using UnityEngine; +using Logger = NewHorizons.Utility.Logger; + namespace NewHorizons.Handlers { /// @@ -8,13 +10,13 @@ namespace NewHorizons.Handlers /// public static class StreamingHandler { - private static Dictionary _materialCache; - private static Dictionary> _objectCache; + private static readonly Dictionary _materialCache = new(); + private static readonly Dictionary _objectCache = new(); public static void Init() { - _materialCache = new Dictionary(); - _objectCache = new Dictionary>(); + _materialCache.Clear(); + _objectCache.Clear(); } /// @@ -23,23 +25,17 @@ namespace NewHorizons.Handlers public static void SetUpStreaming(GameObject obj, Sector sector) { // find the asset bundles to load - List assetBundles; - if (_objectCache.ContainsKey(obj)) + // tries the cache first, then builds + if (!_objectCache.TryGetValue(obj, out var assetBundles)) { - assetBundles = _objectCache[obj]; - } - else - { - assetBundles = new List(); + var assetBundlesList = new List(); var tables = Resources.FindObjectsOfTypeAll(); foreach (var streamingHandle in obj.GetComponentsInChildren()) { var assetBundle = streamingHandle.assetBundle; - if (!assetBundles.Contains(assetBundle)) - { - assetBundles.Add(assetBundle); - } + assetBundlesList.SafeAdd(assetBundle); + if (streamingHandle is StreamingRenderMeshHandle or StreamingSkinnedMeshHandle) { var materials = streamingHandle.GetComponent().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 if (_materialCache.TryGetValue(materials[0], out assetBundle)) { - assetBundles.Add(assetBundle); + assetBundlesList.SafeAdd(assetBundle); } else { @@ -60,13 +56,15 @@ namespace NewHorizons.Handlers if (materials.Contains(x.material)) { _materialCache.SafeAdd(x.material, table.assetBundle); - assetBundles.SafeAdd(table.assetBundle); + assetBundlesList.SafeAdd(table.assetBundle); } } } } } } + + assetBundles = assetBundlesList.ToArray(); _objectCache[obj] = assetBundles; } @@ -77,6 +75,7 @@ namespace NewHorizons.Handlers { foreach (var assetBundle in assetBundles) { + Logger.LogVerbose($"loading bundle {assetBundle}"); StreamingManager.LoadStreamingAssets(assetBundle); } } @@ -87,6 +86,7 @@ namespace NewHorizons.Handlers { foreach (var assetBundle in assetBundles) { + Logger.LogVerbose($"loading bundle {assetBundle}"); StreamingManager.LoadStreamingAssets(assetBundle); } } @@ -94,6 +94,7 @@ namespace NewHorizons.Handlers { foreach (var assetBundle in assetBundles) { + Logger.LogVerbose($"UNloading bundle {assetBundle}"); StreamingManager.UnloadStreamingAssets(assetBundle); } } @@ -104,9 +105,10 @@ namespace NewHorizons.Handlers // just load it immediately and hope for the best foreach (var assetBundle in assetBundles) { + Logger.LogVerbose($"loading bundle {assetBundle}"); StreamingManager.LoadStreamingAssets(assetBundle); } } } } -} +} \ No newline at end of file