streaming unloading (#471)

## Improvements
- leaving sectors now unloads streamed things
This commit is contained in:
Will Corby 2022-12-28 13:46:50 -08:00 committed by GitHub
commit e4a68a3634
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 17 deletions

View File

@ -385,6 +385,8 @@ namespace NewHorizons.Builder.Props
}
}
StreamingHandler.SetUpStreaming(brambleNode, sector);
// Done!
brambleNode.SetActive(true);
return brambleNode;

View File

@ -614,7 +614,7 @@ namespace NewHorizons.Builder.Props
nomaiWallText.SetTextAsset(text);
// #433 fuzzy stranger text
if (info.arcInfo.Any(x => x.type == PropModule.NomaiTextArcInfo.NomaiTextArcType.Stranger))
if (info.arcInfo != null && info.arcInfo.Any(x => x.type == PropModule.NomaiTextArcInfo.NomaiTextArcType.Stranger))
{
StreamingHandler.SetUpStreaming(AstroObject.Name.RingWorld, sector);
}

View File

@ -24,16 +24,23 @@ namespace NewHorizons.Handlers
{
var group = GetStreamingGroup(name);
sector.OnOccupantEnterSector += _ =>
if (sector)
{
if (sector.ContainsAnyOccupants(DynamicOccupant.Player | DynamicOccupant.Probe))
group.RequestGeneralAssets();
};
sector.OnOccupantExitSector += _ =>
sector.OnOccupantEnterSector += _ =>
{
if (sector.ContainsAnyOccupants(DynamicOccupant.Player | DynamicOccupant.Probe))
group.RequestGeneralAssets();
};
sector.OnOccupantExitSector += _ =>
{
if (!sector.ContainsAnyOccupants(DynamicOccupant.Player | DynamicOccupant.Probe))
group.ReleaseGeneralAssets();
};
}
else
{
if (!sector.ContainsAnyOccupants(DynamicOccupant.Player | DynamicOccupant.Probe))
group.ReleaseGeneralAssets();
};
group.RequestGeneralAssets();
}
}
/// <summary>
@ -87,8 +94,6 @@ namespace NewHorizons.Handlers
foreach (var assetBundle in assetBundles)
{
StreamingManager.LoadStreamingAssets(assetBundle);
// Track the sector even if its null. null means stay loaded forever
if (!_sectorCache.TryGetValue(assetBundle, out var sectors))
{
@ -106,15 +111,17 @@ namespace NewHorizons.Handlers
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);
};
*/
}
else
{
foreach (var assetBundle in assetBundles)
StreamingManager.LoadStreamingAssets(assetBundle);
}
}
@ -136,7 +143,7 @@ namespace NewHorizons.Handlers
}
else
{
return Locator.GetAstroObject(name)?.GetComponentInChildren<StreamingGroup>();
return Locator.GetAstroObject(name).GetComponentInChildren<StreamingGroup>();
}
}
}

View File

@ -1,10 +1,11 @@
using HarmonyLib;
using NewHorizons.Handlers;
using OWML.Logging;
namespace NewHorizons.Patches
{
[HarmonyPatch]
public static class StreamingManagerPatches
public static class StreamingPatches
{
[HarmonyPrefix]
[HarmonyPatch(typeof(StreamingManager), nameof(StreamingManager.UnloadStreamingAssets))]
@ -12,6 +13,14 @@ namespace NewHorizons.Patches
{
// Only let it unload stuff that isn't being used
return !StreamingHandler.IsBundleInUse(assetBundleName);
}
}
[HarmonyPrefix]
[HarmonyPatch(typeof(UnityLogger), "OnLogMessageReceived")]
public static bool UnityLogger_OnLogMessageReceived(string message)
{
// Filter out goofy error that doesn't actually break anything
return !message.EndsWith(") is out of bounds (size=0)");
}
}
}