diff --git a/NewHorizons/Builder/Props/ProjectionBuilder.cs b/NewHorizons/Builder/Props/ProjectionBuilder.cs index ca612763..1e12854f 100644 --- a/NewHorizons/Builder/Props/ProjectionBuilder.cs +++ b/NewHorizons/Builder/Props/ProjectionBuilder.cs @@ -156,11 +156,11 @@ namespace NewHorizons.Builder.Props // We can fit 16 slides max into an atlas var textures = new Texture2D[slidesCount > 16 ? 16 : slidesCount]; - // Don't load inverted images if the cache exists, in this case we only load when actively displaying stuff - var (invImageLoader, atlasImageLoader, imageLoader) = StartAsyncLoader(mod, info.slides, ref slideCollection, !CacheExists(mod), true, false); + // Slide reels dynamically load the inverted cached images when needed. We only need to load raw images to generate the cache or atlases + var (_, atlasImageLoader, imageLoader) = StartAsyncLoader(mod, info.slides, ref slideCollection, false, true, false); // If the cache doesn't exist it will be created here, slide reels only use the base image loader for cache creation so delete the images after to free memory - imageLoader.deleteTexturesWhenDone = !CacheExists(mod); + imageLoader.deleteTexturesWhenDone = true; var key = GetUniqueSlideReelID(mod, info.slides); @@ -397,9 +397,9 @@ namespace NewHorizons.Builder.Props if (_visionTorchDetectorPrefab == null) return null; // spawn a trigger for the vision torch - var g = DetailBuilder.Make(planetGO, sector, mod, _visionTorchDetectorPrefab, new DetailInfo(info) { scale = 2, rename = !string.IsNullOrEmpty(info.rename) ? info.rename : "VisionStaffDetector" }); + var visionTorchTargetGO = DetailBuilder.Make(planetGO, sector, mod, _visionTorchDetectorPrefab, new DetailInfo(info) { scale = 2, rename = !string.IsNullOrEmpty(info.rename) ? info.rename : "VisionStaffDetector" }); - if (g == null) + if (visionTorchTargetGO == null) { NHLogger.LogWarning($"Tried to make a vision torch target but couldn't. Do you have the DLC installed?"); return null; @@ -420,17 +420,18 @@ namespace NewHorizons.Builder.Props }); // attach a component to store all the data for the slides that play when a vision torch scans this target - var target = g.AddComponent(); - var slideCollectionContainer = g.AddComponent(); + var target = visionTorchTargetGO.AddComponent(); + var slideCollectionContainer = visionTorchTargetGO.AddComponent(); + slideCollectionContainer.doAsyncLoading = false; slideCollectionContainer.slideCollection = slideCollection; - target.slideCollection = g.AddComponent(); + target.slideCollection = visionTorchTargetGO.AddComponent(); target.slideCollection._slideCollectionContainer = slideCollectionContainer; LinkShipLogFacts(info, slideCollectionContainer); - g.SetActive(true); + visionTorchTargetGO.SetActive(true); - return g; + return visionTorchTargetGO; } public static GameObject MakeStandingVisionTorch(GameObject planetGO, Sector sector, ProjectionInfo info, IModBehaviour mod) @@ -489,6 +490,7 @@ namespace NewHorizons.Builder.Props // Set up the containers for the slides var slideCollectionContainer = standingTorch.AddComponent(); + slideCollectionContainer.doAsyncLoading = false; slideCollectionContainer.slideCollection = slideCollection; var mindSlideCollection = standingTorch.AddComponent(); @@ -550,7 +552,7 @@ namespace NewHorizons.Builder.Props // Load the inverted images used when displaying slide reels to a screen invertedImageLoader.PathsToLoad.Add((i, Path.Combine(Main.Instance.ModHelper.Manifest.ModFolderPath, "Assets/textures/inverted_blank_slide_reel.png"))); } - else + else if (!cacheExists || loadRawImages) { // Used to then make cached stuff imageLoader.PathsToLoad.Add((i, Path.Combine(Main.Instance.ModHelper.Manifest.ModFolderPath, "Assets/textures/blank_slide_reel.png"))); @@ -587,7 +589,7 @@ namespace NewHorizons.Builder.Props { invertedImageLoader.Start(true, false); } - else + if (loadRawImages) { imageLoader.Start(true, false); } diff --git a/NewHorizons/Components/EOTE/NHSlideCollectionContainer.cs b/NewHorizons/Components/EOTE/NHSlideCollectionContainer.cs index ff627456..5fded2a3 100644 --- a/NewHorizons/Components/EOTE/NHSlideCollectionContainer.cs +++ b/NewHorizons/Components/EOTE/NHSlideCollectionContainer.cs @@ -10,6 +10,7 @@ public class NHSlideCollectionContainer : SlideCollectionContainer { public string[] conditionsToSet; public string[] persistentConditionsToSet; + public bool doAsyncLoading = true; [HarmonyPrefix] [HarmonyPatch(typeof(SlideCollectionContainer), nameof(SlideCollectionContainer.Initialize))] @@ -68,7 +69,7 @@ public class NHSlideCollectionContainer : SlideCollectionContainer [HarmonyPatch(typeof(SlideCollectionContainer), nameof(SlideCollectionContainer.NextSlideAvailable))] public static bool SlideCollectionContainer_NextSlideAvailable(SlideCollectionContainer __instance, ref bool __result) { - if (__instance is NHSlideCollectionContainer container) + if (__instance is NHSlideCollectionContainer container && container.doAsyncLoading) { __result = (container.slideCollection as NHSlideCollection).IsSlideLoaded(container.slideIndex + 1); return false; @@ -84,7 +85,7 @@ public class NHSlideCollectionContainer : SlideCollectionContainer [HarmonyPatch(typeof(SlideCollectionContainer), nameof(SlideCollectionContainer.PrevSlideAvailable))] public static bool SlideCollectionContainer_PrevSlideAvailable(SlideCollectionContainer __instance, ref bool __result) { - if (__instance is NHSlideCollectionContainer container) + if (__instance is NHSlideCollectionContainer container && container.doAsyncLoading) { __result = (container.slideCollection as NHSlideCollection).IsSlideLoaded(container.slideIndex - 1); return false; @@ -99,7 +100,7 @@ public class NHSlideCollectionContainer : SlideCollectionContainer [HarmonyPatch(typeof(SlideCollectionContainer), nameof(SlideCollectionContainer.UnloadStreamingTextures))] public static bool SlideCollectionContainer_UnloadStreamingTextures(SlideCollectionContainer __instance) { - if (__instance is NHSlideCollectionContainer container) + if (__instance is NHSlideCollectionContainer container && container.doAsyncLoading) { for (int i = 0; i < (container.slideCollection as NHSlideCollection).slidePaths.Length; i++) { @@ -117,7 +118,7 @@ public class NHSlideCollectionContainer : SlideCollectionContainer [HarmonyPatch(typeof(SlideCollectionContainer), nameof(SlideCollectionContainer.GetStreamingTexture))] public static bool SlideCollectionContainer_GetStreamingTexture(SlideCollectionContainer __instance, int id, ref Texture __result) { - if (__instance is NHSlideCollectionContainer container) + if (__instance is NHSlideCollectionContainer container && container.doAsyncLoading) { __result = (container.slideCollection as NHSlideCollection).LoadSlide(id); return false; @@ -132,7 +133,7 @@ public class NHSlideCollectionContainer : SlideCollectionContainer [HarmonyPatch(typeof(SlideCollectionContainer), nameof(SlideCollectionContainer.RequestManualStreamSlides))] public static bool SlideCollectionContainer_RequestManualStreamSlides(SlideCollectionContainer __instance) { - if (__instance is NHSlideCollectionContainer container) + if (__instance is NHSlideCollectionContainer container && container.doAsyncLoading) { (container.slideCollection as NHSlideCollection).LoadSlide(__instance._currentSlideIndex); return false; @@ -147,7 +148,7 @@ public class NHSlideCollectionContainer : SlideCollectionContainer [HarmonyPatch(typeof(SlideCollectionContainer), nameof(SlideCollectionContainer.streamingTexturesAvailable), MethodType.Getter)] public static bool SlideCollectionContainer_streamingTexturesAvailable(SlideCollectionContainer __instance, ref bool __result) { - if (__instance is NHSlideCollectionContainer container) + if (__instance is NHSlideCollectionContainer container && container.doAsyncLoading) { __result = (container.slideCollection as NHSlideCollection).slidePaths != null && (container.slideCollection as NHSlideCollection).slidePaths.Any(); return false; diff --git a/NewHorizons/Patches/ShipLogPatches/ShipLogSlideReelPatches.cs b/NewHorizons/Patches/ShipLogPatches/ShipLogSlideReelPatches.cs index 390a4e79..1da6531e 100644 --- a/NewHorizons/Patches/ShipLogPatches/ShipLogSlideReelPatches.cs +++ b/NewHorizons/Patches/ShipLogPatches/ShipLogSlideReelPatches.cs @@ -18,4 +18,20 @@ public static class ShipLogSlideReelPatches } return true; } + + [HarmonyPrefix] + [HarmonyPatch(typeof(ShipLogSlideProjector), nameof(ShipLogSlideProjector.UnloadCurrentStreamingTextures))] + public static bool ShipLogSlideProjector_UnloadCurrentStreamingTextures(ShipLogSlideProjector __instance) + { + if (__instance._collectionIndex >= 0 && __instance._collectionIndex < __instance._slideCollections.Count && + __instance._slideCollections[__instance._collectionIndex] is NHSlideCollection collection) + { + for (int i = 0; i < collection.slides.Length; i++) + { + collection.UnloadSlide(i); + } + return false; + } + return true; + } } diff --git a/NewHorizons/Utility/Files/ImageUtilities.cs b/NewHorizons/Utility/Files/ImageUtilities.cs index cc258967..a0f02204 100644 --- a/NewHorizons/Utility/Files/ImageUtilities.cs +++ b/NewHorizons/Utility/Files/ImageUtilities.cs @@ -47,7 +47,7 @@ namespace NewHorizons.Utility.Files var key = GetKey(path); if (_textureCache.TryGetValue(key, out var existingTexture)) { - NHLogger.LogVerbose($"Already loaded image at path: {path}"); + //NHLogger.LogVerbose($"Already loaded image at path: {path}"); return (Texture2D)existingTexture; }