From b78b269cf53529bcb73fa99538a3e9eb23e283b0 Mon Sep 17 00:00:00 2001
From: Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com>
Date: Sun, 9 Oct 2022 17:56:33 -0400
Subject: split PyTK raw-image-load check into a separate method so it can be
 patched by mods like SpriteMaster

---
 .../Framework/ContentManagers/ModContentManager.cs | 51 ++++++++++++++--------
 1 file changed, 32 insertions(+), 19 deletions(-)

(limited to 'src/SMAPI/Framework')

diff --git a/src/SMAPI/Framework/ContentManagers/ModContentManager.cs b/src/SMAPI/Framework/ContentManagers/ModContentManager.cs
index 30dd4215..ddb6f6f5 100644
--- a/src/SMAPI/Framework/ContentManagers/ModContentManager.cs
+++ b/src/SMAPI/Framework/ContentManagers/ModContentManager.cs
@@ -204,31 +204,22 @@ namespace StardewModdingAPI.Framework.ContentManagers
         private T LoadImageFile<T>(IAssetName assetName, FileInfo file)
         {
             this.AssertValidType<T>(assetName, file, typeof(Texture2D), typeof(IRawTextureData));
-            bool expectsRawData = typeof(T).IsAssignableTo(typeof(IRawTextureData));
-            bool asRawData = expectsRawData || this.UseRawImageLoading;
-
+            bool returnRawData = typeof(T).IsAssignableTo(typeof(IRawTextureData));
+            bool loadRawData =
+                returnRawData
+                || (
+                    this.UseRawImageLoading
 #if SMAPI_DEPRECATED
-            // disable raw data if PyTK will rescale the image (until it supports raw data)
-            if (asRawData && !expectsRawData)
-            {
-                if (ModContentManager.EnablePyTkLegacyMode)
-                {
-                    // PyTK intercepts Texture2D file loads to rescale them (e.g. for HD portraits),
-                    // but doesn't support IRawTextureData loads yet. We can't just check if the
-                    // current file has a '.pytk.json' rescale file though, since PyTK may still
-                    // rescale it if the original asset or another edit gets rescaled.
-                    asRawData = false;
-                    this.Monitor.LogOnce("Enabled compatibility mode for PyTK 1.23.* or earlier. This won't cause any issues, but may impact performance. This will no longer be supported in the upcoming SMAPI 4.0.0.", LogLevel.Warn);
-                }
-            }
+                    && !this.ShouldDisableIntermediateRawDataLoad<T>(assetName, file)
 #endif
+                );
 
             // load
-            if (asRawData)
+            if (loadRawData)
             {
-                IRawTextureData raw = this.LoadRawImageData(file, expectsRawData);
+                IRawTextureData raw = this.LoadRawImageData(file, returnRawData);
 
-                if (expectsRawData)
+                if (returnRawData)
                     return (T)raw;
                 else
                 {
@@ -246,6 +237,28 @@ namespace StardewModdingAPI.Framework.ContentManagers
             }
         }
 
+#if SMAPI_DEPRECATED
+        /// <summary>Get whether to disable loading an image as <see cref="IRawTextureData"/> before building a <see cref="Texture2D"/> instance. This isn't called if the mod requested <see cref="IRawTextureData"/> directly.</summary>
+        /// <typeparam name="T">The type of asset being loaded.</typeparam>
+        /// <param name="assetName">The asset name relative to the loader root directory.</param>
+        /// <param name="file">The file being loaded.</param>
+        private bool ShouldDisableIntermediateRawDataLoad<T>(IAssetName assetName, FileInfo file)
+        {
+            // disable raw data if PyTK will rescale the image (until it supports raw data)
+            if (ModContentManager.EnablePyTkLegacyMode)
+            {
+                // PyTK intercepts Texture2D file loads to rescale them (e.g. for HD portraits),
+                // but doesn't support IRawTextureData loads yet. We can't just check if the
+                // current file has a '.pytk.json' rescale file though, since PyTK may still
+                // rescale it if the original asset or another edit gets rescaled.
+                this.Monitor.LogOnce("Enabled compatibility mode for PyTK 1.23.* or earlier. This won't cause any issues, but may impact performance. This will no longer be supported in the upcoming SMAPI 4.0.0.", LogLevel.Warn);
+                return true;
+            }
+
+            return false;
+        }
+#endif
+
         /// <summary>Load the raw image data from a file on disk.</summary>
         /// <param name="file">The file whose data to load.</param>
         /// <param name="forRawData">Whether the data is being loaded for an <see cref="IRawTextureData"/> (true) or <see cref="Texture2D"/> (false) instance.</param>
-- 
cgit