From a564afcf0c5a31e80460223dd1f8d694b858f8c1 Mon Sep 17 00:00:00 2001 From: xen-42 Date: Sun, 14 Jul 2024 21:04:31 -0400 Subject: [PATCH] Ensure inverted slide reels have the same key regardless of if they're from the cache folder or not --- NewHorizons/Utility/Files/ImageUtilities.cs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/NewHorizons/Utility/Files/ImageUtilities.cs b/NewHorizons/Utility/Files/ImageUtilities.cs index 5d0c4d2f..3f9d8497 100644 --- a/NewHorizons/Utility/Files/ImageUtilities.cs +++ b/NewHorizons/Utility/Files/ImageUtilities.cs @@ -108,6 +108,16 @@ namespace NewHorizons.Utility.Files public static Texture2D InvertSlideReel(IModBehaviour mod, Texture2D texture, string originalPath) { var key = $"{texture.name} > invert"; + var cachedPath = ""; + + // If we're going to end up caching the texture we must make sure it will end up using the same key + // Not sure why we check if the originalPath is null but it did that before so + if (!string.IsNullOrEmpty(originalPath)) + { + cachedPath = Path.Combine(mod.ModHelper.Manifest.ModFolderPath, ProjectionBuilder.INVERTED_SLIDE_CACHE_FOLDER, originalPath.Replace(mod.ModHelper.Manifest.ModFolderPath, "")); + key = GetKey(cachedPath); + } + if (_textureCache.TryGetValue(key, out var existingTexture)) return (Texture2D)existingTexture; var pixels = texture.GetPixels(); @@ -143,12 +153,11 @@ namespace NewHorizons.Utility.Files // Since doing this is expensive we cache the results to the disk // Preloading cached values is done in ProjectionBuilder - if (!string.IsNullOrEmpty(originalPath)) + if (!string.IsNullOrEmpty(cachedPath)) { - var path = Path.Combine(mod.ModHelper.Manifest.ModFolderPath, ProjectionBuilder.INVERTED_SLIDE_CACHE_FOLDER, originalPath.Replace(mod.ModHelper.Manifest.ModFolderPath, "")); - NHLogger.LogVerbose($"Caching inverted image to {path}"); - Directory.CreateDirectory(Path.GetDirectoryName(path)); - File.WriteAllBytes(path, newTexture.EncodeToPNG()); + NHLogger.LogVerbose($"Caching inverted image to {cachedPath}"); + Directory.CreateDirectory(Path.GetDirectoryName(cachedPath)); + File.WriteAllBytes(cachedPath, newTexture.EncodeToPNG()); } return newTexture;