mirror of
https://github.com/Outer-Wilds-New-Horizons/new-horizons.git
synced 2025-12-11 20:15:44 +01:00
Only unload stuff that isn't in use
This commit is contained in:
parent
a2da11a64a
commit
7556d911bf
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
17
NewHorizons/Patches/StreamingManagerPatches.cs
Normal file
17
NewHorizons/Patches/StreamingManagerPatches.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user