add image compatibility mode for PyTK

This commit is contained in:
Jesse Plamondon-Willard 2022-06-10 22:14:28 -04:00
parent 11a497c1f6
commit 0189b282f4
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
2 changed files with 47 additions and 4 deletions

View File

@ -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)
{
IRawTextureData raw = this.LoadRawImageData(file, asRawData);
if (asRawData)
{
IRawTextureData raw = this.LoadRawImageData(file, expectsRawData);
if (expectsRawData)
return (T)raw;
else
{

View File

@ -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