summaryrefslogtreecommitdiff
path: root/src/SMAPI.Mods.ErrorHandler
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2022-06-12 12:26:51 -0400
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2022-06-12 12:26:51 -0400
commit15f829e48c1ee9731a812798841ee102e024775d (patch)
tree2fbc66e78a802445389c54a39bb9f0912f567abf /src/SMAPI.Mods.ErrorHandler
parent0189b282f4db300c70eeef5e9f714aa450deafde (diff)
downloadSMAPI-15f829e48c1ee9731a812798841ee102e024775d.tar.gz
SMAPI-15f829e48c1ee9731a812798841ee102e024775d.tar.bz2
SMAPI-15f829e48c1ee9731a812798841ee102e024775d.zip
patch PyTK temporarily to fix scale-up compatibility in SMAPI 3.15
Diffstat (limited to 'src/SMAPI.Mods.ErrorHandler')
-rw-r--r--src/SMAPI.Mods.ErrorHandler/ModEntry.cs7
-rw-r--r--src/SMAPI.Mods.ErrorHandler/ModPatches/PyTkPatcher.cs79
-rw-r--r--src/SMAPI.Mods.ErrorHandler/SMAPI.Mods.ErrorHandler.csproj3
3 files changed, 85 insertions, 4 deletions
diff --git a/src/SMAPI.Mods.ErrorHandler/ModEntry.cs b/src/SMAPI.Mods.ErrorHandler/ModEntry.cs
index bfbfd2dc..22e68421 100644
--- a/src/SMAPI.Mods.ErrorHandler/ModEntry.cs
+++ b/src/SMAPI.Mods.ErrorHandler/ModEntry.cs
@@ -2,6 +2,7 @@ using System;
using System.Reflection;
using StardewModdingAPI.Events;
using StardewModdingAPI.Internal.Patching;
+using StardewModdingAPI.Mods.ErrorHandler.ModPatches;
using StardewModdingAPI.Mods.ErrorHandler.Patches;
using StardewValley;
@@ -29,6 +30,7 @@ namespace StardewModdingAPI.Mods.ErrorHandler
// apply patches
HarmonyPatcher.Apply(this.ModManifest.UniqueID, this.Monitor,
+ // game patches
new DialoguePatcher(monitorForGame, this.Helper.Reflection),
new EventPatcher(monitorForGame),
new GameLocationPatcher(monitorForGame),
@@ -37,7 +39,10 @@ namespace StardewModdingAPI.Mods.ErrorHandler
new ObjectPatcher(),
new SaveGamePatcher(this.Monitor, this.OnSaveContentRemoved),
new SpriteBatchPatcher(),
- new UtilityPatcher()
+ new UtilityPatcher(),
+
+ // mod patches
+ new PyTkPatcher(helper.ModRegistry)
);
// hook events
diff --git a/src/SMAPI.Mods.ErrorHandler/ModPatches/PyTkPatcher.cs b/src/SMAPI.Mods.ErrorHandler/ModPatches/PyTkPatcher.cs
new file mode 100644
index 00000000..9ee864db
--- /dev/null
+++ b/src/SMAPI.Mods.ErrorHandler/ModPatches/PyTkPatcher.cs
@@ -0,0 +1,79 @@
+using System;
+using System.Diagnostics.CodeAnalysis;
+using System.Reflection;
+using HarmonyLib;
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Graphics;
+using StardewModdingAPI.Framework;
+using StardewModdingAPI.Framework.Content;
+using StardewModdingAPI.Internal;
+using StardewModdingAPI.Internal.Patching;
+
+//
+// This is part of a three-part fix for PyTK 1.23.0 and earlier. When removing this, search
+// 'Platonymous.Toolkit' to find the other part in SMAPI and Content Patcher.
+//
+
+namespace StardewModdingAPI.Mods.ErrorHandler.ModPatches
+{
+ /// <summary>Harmony patches for the PyTK mod for compatibility with newer SMAPI versions.</summary>
+ /// <remarks>Patch methods must be static for Harmony to work correctly. See the Harmony documentation before renaming patch arguments.</remarks>
+ [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony and methods are named for clarity.")]
+ [SuppressMessage("ReSharper", "IdentifierTypo", Justification = "Argument names are defined by Harmony and methods are named for clarity.")]
+ [SuppressMessage("ReSharper", "StringLiteralTypo", Justification = "'Platonymous' is part of the mod ID.")]
+ internal class PyTkPatcher : BasePatcher
+ {
+ /*********
+ ** Fields
+ *********/
+ /// <summary>The PyTK mod metadata, if it's installed.</summary>
+ private static IModMetadata? PyTk;
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="modRegistry">The mod registry from which to read PyTK metadata.</param>
+ public PyTkPatcher(IModRegistry modRegistry)
+ {
+ IModMetadata? pyTk = (IModMetadata?)modRegistry.Get(@"Platonymous.Toolkit");
+ if (pyTk is not null && !pyTk.Manifest.Version.IsNewerThan("1.23.0"))
+ PyTkPatcher.PyTk = pyTk;
+ }
+
+ /// <inheritdoc />
+ public override void Apply(Harmony harmony, IMonitor monitor)
+ {
+ try
+ {
+ // get mod info
+ IModMetadata? pyTk = PyTkPatcher.PyTk;
+ if (pyTk is null)
+ return;
+
+ // get patch method
+ const string patchMethodName = "PatchImage";
+ MethodInfo? patch = AccessTools.Method(pyTk.Mod!.GetType(), patchMethodName);
+ if (patch is null)
+ {
+ monitor.Log("Failed applying compatibility patch for PyTK. Its image scaling feature may not work correctly.", LogLevel.Warn);
+ monitor.Log($"Couldn't find patch method '{pyTk.Mod.GetType().FullName}.{patchMethodName}'.");
+ return;
+ }
+
+ // apply patch
+ harmony = new($"{harmony.Id}.compatibility-patches.PyTK");
+ harmony.Patch(
+ original: AccessTools.Method(typeof(AssetDataForImage), nameof(AssetDataForImage.PatchImage), new[] { typeof(Texture2D), typeof(Rectangle), typeof(Rectangle), typeof(PatchMode) }),
+ prefix: new HarmonyMethod(patch)
+ );
+ }
+ catch (Exception ex)
+ {
+ monitor.Log("Failed applying compatibility patch for PyTK. Its image scaling feature may not work correctly.", LogLevel.Warn);
+ monitor.Log(ex.GetLogSummary());
+ }
+ }
+ }
+}
diff --git a/src/SMAPI.Mods.ErrorHandler/SMAPI.Mods.ErrorHandler.csproj b/src/SMAPI.Mods.ErrorHandler/SMAPI.Mods.ErrorHandler.csproj
index 78cdb315..53c37e97 100644
--- a/src/SMAPI.Mods.ErrorHandler/SMAPI.Mods.ErrorHandler.csproj
+++ b/src/SMAPI.Mods.ErrorHandler/SMAPI.Mods.ErrorHandler.csproj
@@ -24,7 +24,4 @@
<None Update="i18n\*.json" CopyToOutputDirectory="PreserveNewest" />
<None Update="manifest.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
-
- <Import Project="..\SMAPI.Internal\SMAPI.Internal.projitems" Label="Shared" />
- <Import Project="..\SMAPI.Internal.Patching\SMAPI.Internal.Patching.projitems" Label="Shared" />
</Project>