split PyTK raw-image-load check into a separate method so it can be patched by mods like SpriteMaster

This commit is contained in:
Jesse Plamondon-Willard 2022-10-09 17:56:33 -04:00
parent 037d7e357b
commit b78b269cf5
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
1 changed files with 32 additions and 19 deletions

View File

@ -204,31 +204,22 @@ namespace StardewModdingAPI.Framework.ContentManagers
private T LoadImageFile<T>(IAssetName assetName, FileInfo file) private T LoadImageFile<T>(IAssetName assetName, FileInfo file)
{ {
this.AssertValidType<T>(assetName, file, typeof(Texture2D), typeof(IRawTextureData)); this.AssertValidType<T>(assetName, file, typeof(Texture2D), typeof(IRawTextureData));
bool expectsRawData = typeof(T).IsAssignableTo(typeof(IRawTextureData)); bool returnRawData = typeof(T).IsAssignableTo(typeof(IRawTextureData));
bool asRawData = expectsRawData || this.UseRawImageLoading; bool loadRawData =
returnRawData
|| (
this.UseRawImageLoading
#if SMAPI_DEPRECATED #if SMAPI_DEPRECATED
// disable raw data if PyTK will rescale the image (until it supports raw data) && !this.ShouldDisableIntermediateRawDataLoad<T>(assetName, file)
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);
}
}
#endif #endif
);
// load // load
if (asRawData) if (loadRawData)
{ {
IRawTextureData raw = this.LoadRawImageData(file, expectsRawData); IRawTextureData raw = this.LoadRawImageData(file, returnRawData);
if (expectsRawData) if (returnRawData)
return (T)raw; return (T)raw;
else 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> /// <summary>Load the raw image data from a file on disk.</summary>
/// <param name="file">The file whose data to load.</param> /// <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> /// <param name="forRawData">Whether the data is being loaded for an <see cref="IRawTextureData"/> (true) or <see cref="Texture2D"/> (false) instance.</param>