Only unload stuff that isn't in use

This commit is contained in:
Nick 2022-08-05 19:49:55 -04:00
parent a2da11a64a
commit 7556d911bf
2 changed files with 49 additions and 13 deletions

View File

@ -1,4 +1,4 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using UnityEngine; using UnityEngine;
using Logger = NewHorizons.Utility.Logger; using Logger = NewHorizons.Utility.Logger;
@ -12,11 +12,13 @@ namespace NewHorizons.Handlers
{ {
private static readonly Dictionary<Material, string> _materialCache = new(); private static readonly Dictionary<Material, string> _materialCache = new();
private static readonly Dictionary<GameObject, string[]> _objectCache = new(); private static readonly Dictionary<GameObject, string[]> _objectCache = new();
private static readonly Dictionary<string, List<Sector>> _sectorCache = new();
public static void Init() public static void Init()
{ {
_materialCache.Clear(); _materialCache.Clear();
_objectCache.Clear(); _objectCache.Clear();
_sectorCache.Clear();
} }
/// <summary> /// <summary>
@ -71,22 +73,39 @@ namespace NewHorizons.Handlers
foreach (var assetBundle in assetBundles) foreach (var assetBundle in assetBundles)
{ {
StreamingManager.LoadStreamingAssets(assetBundle); StreamingManager.LoadStreamingAssets(assetBundle);
// Track the sector even if its null. null means stay loaded forever
if (!_sectorCache.ContainsKey(assetBundle)) _sectorCache.Add(assetBundle, new List<Sector>());
if (!_sectorCache[assetBundle].Contains(sector)) _sectorCache[assetBundle].Add(sector);
} }
if (!sector) if (sector)
{ {
Logger.LogWarning($"StreamingHandler for {obj} has null sector. " + sector.OnOccupantEnterSector += _ =>
"This can lead to the thing being unloaded permanently.");
return;
}
sector.OnOccupantEnterSector += _ =>
{
foreach (var assetBundle in assetBundles)
{ {
StreamingManager.LoadStreamingAssets(assetBundle); foreach (var assetBundle in assetBundles)
} StreamingManager.LoadStreamingAssets(assetBundle);
}; };
/*
sector.OnOccupantExitSector += _ =>
{
// UnloadStreamingAssets is patched to check IsBundleInUse first before unloading
if (!sector.ContainsAnyOccupants(DynamicOccupant.Player | DynamicOccupant.Probe))
foreach (var assetBundle in assetBundles)
StreamingManager.UnloadStreamingAssets(assetBundle);
};
*/
}
}
public static bool IsBundleInUse(string assetBundle)
{
// If a sector in the list is null then it is always in use
if(_sectorCache.TryGetValue(assetBundle, out var sectors))
foreach (var sector in sectors)
if (sector == null || sector.ContainsAnyOccupants(DynamicOccupant.Player | DynamicOccupant.Probe))
return true;
return false;
} }
} }
} }

View File

@ -0,0 +1,17 @@
using HarmonyLib;
using NewHorizons.Handlers;
namespace NewHorizons.Patches
{
[HarmonyPatch]
public static class StreamingManagerPatches
{
[HarmonyPrefix]
[HarmonyPatch(typeof(StreamingManager), nameof(StreamingManager.UnloadStreamingAssets))]
public static bool StreamingManager_UnloadStreamingAssets(string assetBundleName)
{
// Only let it unload stuff that isn't being used
return !StreamingHandler.IsBundleInUse(assetBundleName);
}
}
}