From 0189b282f4db300c70eeef5e9f714aa450deafde Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 10 Jun 2022 22:14:28 -0400 Subject: [PATCH] add image compatibility mode for PyTK --- .../ContentManagers/ModContentManager.cs | 45 +++++++++++++++++-- src/SMAPI/Framework/SCore.cs | 6 +++ 2 files changed, 47 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 /// If a map tilesheet's image source has no file extensions, the file extensions to check for in the local mod folder. private static readonly string[] LocalTilesheetExtensions = { ".png", ".xnb" }; + /// A lookup of image file paths to whether they have PyTK scaling information. + private static readonly Dictionary IsPyTkScaled = new(StringComparer.OrdinalIgnoreCase); + + + /********* + ** Accessors + *********/ + /// Whether to enable legacy compatibility mode for PyTK scale-up textures. + internal static bool EnablePyTkLegacyMode; + /********* ** Public methods @@ -192,14 +203,40 @@ namespace StardewModdingAPI.Framework.ContentManagers private T LoadImageFile(IAssetName assetName, FileInfo file) { this.AssertValidType(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 { diff --git a/src/SMAPI/Framework/SCore.cs b/src/SMAPI/Framework/SCore.cs index fa3f8778..7792c64a 100644 --- a/src/SMAPI/Framework/SCore.cs +++ b/src/SMAPI/Framework/SCore.cs @@ -1636,6 +1636,12 @@ namespace StardewModdingAPI.Framework // initialize translations this.ReloadTranslations(loaded); + // set temporary PyTK compatibility mode + { + IModInfo? pyTk = this.ModRegistry.Get("Platonymous.Toolkit"); + ModContentManager.EnablePyTkLegacyMode = pyTk is not null && pyTk.Manifest.Version.IsOlderThan("1.23.1"); + } + // initialize loaded non-content-pack mods this.Monitor.Log("Launching mods...", LogLevel.Debug); #pragma warning disable CS0612, CS0618 // deprecated code