From 25fd6cd7617fbbf965542ad194c5026f8b501ec9 Mon Sep 17 00:00:00 2001 From: xen-42 Date: Thu, 13 Feb 2025 15:05:02 -0500 Subject: [PATCH] Make it async --- .../EOTE/NHSlideCollectionContainer.cs | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/NewHorizons/Components/EOTE/NHSlideCollectionContainer.cs b/NewHorizons/Components/EOTE/NHSlideCollectionContainer.cs index edc954ca..8b0fb6d7 100644 --- a/NewHorizons/Components/EOTE/NHSlideCollectionContainer.cs +++ b/NewHorizons/Components/EOTE/NHSlideCollectionContainer.cs @@ -21,6 +21,8 @@ public class NHSlideCollectionContainer : SlideCollectionContainer public string[] slidePaths; public IModBehaviour mod; + private HashSet _pathsBeingLoaded = new(); + public static Dictionary> _slidesRequiringPath = new(); static NHSlideCollectionContainer() { @@ -196,9 +198,30 @@ public class NHSlideCollectionContainer : SlideCollectionContainer _slidesRequiringPath[key].Add(this); } - var texture = ImageUtilities.GetTexture(mod, path); - this.slideCollection.slides[wrappedIndex]._image = texture; - return texture; + if (ImageUtilities.IsTextureLoaded(mod, path)) + { + var texture = ImageUtilities.GetTexture(mod, path); + this.slideCollection.slides[wrappedIndex]._image = texture; + return texture; + } + else if (!_pathsBeingLoaded.Contains(path)) + { + var loader = new SlideReelAsyncImageLoader(); + loader.PathsToLoad.Add((wrappedIndex, path)); + loader.Start(true, false); + loader.imageLoadedEvent.AddListener((Texture2D tex, int index, string originalPath) => + { + slideCollection.slides[wrappedIndex]._image = tex; + _pathsBeingLoaded.Remove(path); + }); + _pathsBeingLoaded.Add(path); + return null; + } + else + { + // It is being loaded so we just wait + return null; + } } var texture = LoadSlideInt(index); LoadSlideInt(index - 1);