Ensure inverted slide reels have the same key regardless of if they're from the cache folder or not

This commit is contained in:
xen-42 2024-07-14 21:04:31 -04:00
parent 63cb8e6f5d
commit a564afcf0c

View File

@ -108,6 +108,16 @@ namespace NewHorizons.Utility.Files
public static Texture2D InvertSlideReel(IModBehaviour mod, Texture2D texture, string originalPath) public static Texture2D InvertSlideReel(IModBehaviour mod, Texture2D texture, string originalPath)
{ {
var key = $"{texture.name} > invert"; 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; if (_textureCache.TryGetValue(key, out var existingTexture)) return (Texture2D)existingTexture;
var pixels = texture.GetPixels(); var pixels = texture.GetPixels();
@ -143,12 +153,11 @@ namespace NewHorizons.Utility.Files
// Since doing this is expensive we cache the results to the disk // Since doing this is expensive we cache the results to the disk
// Preloading cached values is done in ProjectionBuilder // 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 {cachedPath}");
NHLogger.LogVerbose($"Caching inverted image to {path}"); Directory.CreateDirectory(Path.GetDirectoryName(cachedPath));
Directory.CreateDirectory(Path.GetDirectoryName(path)); File.WriteAllBytes(cachedPath, newTexture.EncodeToPNG());
File.WriteAllBytes(path, newTexture.EncodeToPNG());
} }
return newTexture; return newTexture;