diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2022-06-10 22:14:28 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2022-06-10 22:14:28 -0400 |
commit | 0189b282f4db300c70eeef5e9f714aa450deafde (patch) | |
tree | e75b434b0c88ef700e6ca9554a02266274fba99f /src/SMAPI/Framework/ContentManagers/ModContentManager.cs | |
parent | 11a497c1f6f62c0956d6d7265883f58504cee653 (diff) | |
download | SMAPI-0189b282f4db300c70eeef5e9f714aa450deafde.tar.gz SMAPI-0189b282f4db300c70eeef5e9f714aa450deafde.tar.bz2 SMAPI-0189b282f4db300c70eeef5e9f714aa450deafde.zip |
add image compatibility mode for PyTK
Diffstat (limited to 'src/SMAPI/Framework/ContentManagers/ModContentManager.cs')
-rw-r--r-- | src/SMAPI/Framework/ContentManagers/ModContentManager.cs | 45 |
1 files changed, 41 insertions, 4 deletions
diff --git a/src/SMAPI/Framework/ContentManagers/ModContentManager.cs b/src/SMAPI/Framework/ContentManagers/ModContentManager.cs index 8e2d58a6..8c5d0f84 100644 --- a/src/SMAPI/Framework/ContentManagers/ModContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/ModContentManager.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Globalization; @@ -46,6 +47,16 @@ namespace StardewModdingAPI.Framework.ContentManagers /// <summary>If a map tilesheet's image source has no file extensions, the file extensions to check for in the local mod folder.</summary> private static readonly string[] LocalTilesheetExtensions = { ".png", ".xnb" }; + /// <summary>A lookup of image file paths to whether they have PyTK scaling information.</summary> + private static readonly Dictionary<string, bool> IsPyTkScaled = new(StringComparer.OrdinalIgnoreCase); + + + /********* + ** Accessors + *********/ + /// <summary>Whether to enable legacy compatibility mode for PyTK scale-up textures.</summary> + internal static bool EnablePyTkLegacyMode; + /********* ** Public methods @@ -192,14 +203,40 @@ namespace StardewModdingAPI.Framework.ContentManagers private T LoadImageFile<T>(IAssetName assetName, FileInfo file) { this.AssertValidType<T>(assetName, file, typeof(Texture2D), typeof(IRawTextureData)); - bool asRawData = typeof(T).IsAssignableTo(typeof(IRawTextureData)); + bool expectsRawData = typeof(T).IsAssignableTo(typeof(IRawTextureData)); + bool asRawData = expectsRawData || this.UseRawImageLoading; + + // disable raw data if PyTK will rescale the image (until it supports raw data) + if (asRawData && !expectsRawData) + { + if (ModContentManager.EnablePyTkLegacyMode) + { + if (!ModContentManager.IsPyTkScaled.TryGetValue(file.FullName, out bool isScaled)) + { + string? dirPath = file.DirectoryName; + string fileName = $"{Path.GetFileNameWithoutExtension(file.Name)}.pytk.json"; + + string path = dirPath is not null + ? Path.Combine(dirPath, fileName) + : fileName; + + ModContentManager.IsPyTkScaled[file.FullName] = isScaled = File.Exists(path); + } + + asRawData = !isScaled; + if (!asRawData) + this.Monitor.LogOnce("Enabled compatibility mode for PyTK scaled textures. This won't cause any issues, but may impact performance.", LogLevel.Warn); + } + else + asRawData = true; + } // load - if (asRawData || this.UseRawImageLoading) + if (asRawData) { - IRawTextureData raw = this.LoadRawImageData(file, asRawData); + IRawTextureData raw = this.LoadRawImageData(file, expectsRawData); - if (asRawData) + if (expectsRawData) return (T)raw; else { |