summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/ModLoading/RewriteFacades
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI/Framework/ModLoading/RewriteFacades')
-rw-r--r--src/SMAPI/Framework/ModLoading/RewriteFacades/AccessToolsFacade.cs44
-rw-r--r--src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyInstanceFacade.cs84
-rw-r--r--src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyMethodFacade.cs47
-rw-r--r--src/SMAPI/Framework/ModLoading/RewriteFacades/SpriteBatchFacade.cs57
4 files changed, 232 insertions, 0 deletions
diff --git a/src/SMAPI/Framework/ModLoading/RewriteFacades/AccessToolsFacade.cs b/src/SMAPI/Framework/ModLoading/RewriteFacades/AccessToolsFacade.cs
new file mode 100644
index 00000000..102f3364
--- /dev/null
+++ b/src/SMAPI/Framework/ModLoading/RewriteFacades/AccessToolsFacade.cs
@@ -0,0 +1,44 @@
+#if HARMONY_2
+using System;
+using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
+using System.Reflection;
+using HarmonyLib;
+
+namespace StardewModdingAPI.Framework.ModLoading.RewriteFacades
+{
+ /// <summary>Maps Harmony 1.x <see cref="AccessTools"/> methods to Harmony 2.x to avoid breaking older mods.</summary>
+ /// <remarks>This is public to support SMAPI rewriting and should not be referenced directly by mods.</remarks>
+ [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Used via assembly rewriting")]
+ [SuppressMessage("ReSharper", "CS1591", Justification = "Documentation not needed for facade classes.")]
+ public class AccessToolsFacade
+ {
+ /*********
+ ** Public methods
+ *********/
+ public static ConstructorInfo DeclaredConstructor(Type type, Type[] parameters = null)
+ {
+ // Harmony 1.x matched both static and instance constructors
+ return
+ AccessTools.DeclaredConstructor(type, parameters, searchForStatic: false)
+ ?? AccessTools.DeclaredConstructor(type, parameters, searchForStatic: true);
+ }
+
+ public static ConstructorInfo Constructor(Type type, Type[] parameters = null)
+ {
+ // Harmony 1.x matched both static and instance constructors
+ return
+ AccessTools.Constructor(type, parameters, searchForStatic: false)
+ ?? AccessTools.Constructor(type, parameters, searchForStatic: true);
+ }
+
+ public static List<ConstructorInfo> GetDeclaredConstructors(Type type)
+ {
+ // Harmony 1.x matched both static and instance constructors
+ return
+ AccessTools.GetDeclaredConstructors(type, searchForStatic: false)
+ ?? AccessTools.GetDeclaredConstructors(type, searchForStatic: true);
+ }
+ }
+}
+#endif
diff --git a/src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyInstanceFacade.cs b/src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyInstanceFacade.cs
new file mode 100644
index 00000000..ad6d5e4f
--- /dev/null
+++ b/src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyInstanceFacade.cs
@@ -0,0 +1,84 @@
+#if HARMONY_2
+using System;
+using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
+using System.Reflection;
+using System.Reflection.Emit;
+using HarmonyLib;
+
+namespace StardewModdingAPI.Framework.ModLoading.RewriteFacades
+{
+ /// <summary>Maps Harmony 1.x <code>HarmonyInstance</code> methods to Harmony 2.x's <see cref="Harmony"/> to avoid breaking older mods.</summary>
+ /// <remarks>This is public to support SMAPI rewriting and should not be referenced directly by mods.</remarks>
+ [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Used via assembly rewriting")]
+ [SuppressMessage("ReSharper", "CS1591", Justification = "Documentation not needed for facade classes.")]
+ public class HarmonyInstanceFacade : Harmony
+ {
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="id">The unique patch identifier.</param>
+ public HarmonyInstanceFacade(string id)
+ : base(id) { }
+
+ public static Harmony Create(string id)
+ {
+ return new Harmony(id);
+ }
+
+ public DynamicMethod Patch(MethodBase original, HarmonyMethod prefix = null, HarmonyMethod postfix = null, HarmonyMethod transpiler = null)
+ {
+ // In Harmony 1.x you could target a virtual method that's not implemented by the
+ // target type, but in Harmony 2.0 you need to target the concrete implementation.
+ // This just resolves the method to the concrete implementation if needed.
+ if (original != null)
+ original = original.GetDeclaredMember();
+
+ // call Harmony 2.0 and show a detailed exception if it fails
+ try
+ {
+ MethodInfo method = base.Patch(original: original, prefix: prefix, postfix: postfix, transpiler: transpiler);
+ return (DynamicMethod)method;
+ }
+ catch (Exception ex)
+ {
+ string patchTypes = this.GetPatchTypesLabel(prefix, postfix, transpiler);
+ string methodLabel = this.GetMethodLabel(original);
+ throw new Exception($"Harmony instance {this.Id} failed applying {patchTypes} to {methodLabel}.", ex);
+ }
+ }
+
+
+ /*********
+ ** Private methods
+ *********/
+ /// <summary>Get a human-readable label for the patch types being applies.</summary>
+ /// <param name="prefix">The prefix method, if any.</param>
+ /// <param name="postfix">The postfix method, if any.</param>
+ /// <param name="transpiler">The transpiler method, if any.</param>
+ private string GetPatchTypesLabel(HarmonyMethod prefix = null, HarmonyMethod postfix = null, HarmonyMethod transpiler = null)
+ {
+ var patchTypes = new List<string>();
+
+ if (prefix != null)
+ patchTypes.Add("prefix");
+ if (postfix != null)
+ patchTypes.Add("postfix");
+ if (transpiler != null)
+ patchTypes.Add("transpiler");
+
+ return string.Join("/", patchTypes);
+ }
+
+ /// <summary>Get a human-readable label for the method being patched.</summary>
+ /// <param name="method">The method being patched.</param>
+ private string GetMethodLabel(MethodBase method)
+ {
+ return method != null
+ ? $"method {method.DeclaringType?.FullName}.{method.Name}"
+ : "null method";
+ }
+ }
+}
+#endif
diff --git a/src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyMethodFacade.cs b/src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyMethodFacade.cs
new file mode 100644
index 00000000..f3975558
--- /dev/null
+++ b/src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyMethodFacade.cs
@@ -0,0 +1,47 @@
+#if HARMONY_2
+using System;
+using System.Diagnostics.CodeAnalysis;
+using System.Reflection;
+using HarmonyLib;
+
+namespace StardewModdingAPI.Framework.ModLoading.RewriteFacades
+{
+ /// <summary>Maps Harmony 1.x <see cref="HarmonyMethod"/> methods to Harmony 2.x to avoid breaking older mods.</summary>
+ /// <remarks>This is public to support SMAPI rewriting and should not be referenced directly by mods.</remarks>
+ [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Used via assembly rewriting")]
+ [SuppressMessage("ReSharper", "CS1591", Justification = "Documentation not needed for facade classes.")]
+ public class HarmonyMethodFacade : HarmonyMethod
+ {
+ /*********
+ ** Public methods
+ *********/
+ public HarmonyMethodFacade(MethodInfo method)
+ {
+ this.ImportMethodImpl(method);
+ }
+
+ public HarmonyMethodFacade(Type type, string name, Type[] parameters = null)
+ {
+ this.ImportMethodImpl(AccessTools.Method(type, name, parameters));
+ }
+
+
+ /*********
+ ** Private methods
+ *********/
+ /// <summary>Import a method directly using the internal HarmonyMethod code.</summary>
+ /// <param name="methodInfo">The method to import.</param>
+ private void ImportMethodImpl(MethodInfo methodInfo)
+ {
+ // A null method is no longer allowed in the constructor with Harmony 2.0, but the
+ // internal code still handles null fine. For backwards compatibility, this bypasses
+ // the new restriction when the mod hasn't been updated for Harmony 2.0 yet.
+
+ MethodInfo importMethod = typeof(HarmonyMethod).GetMethod("ImportMethod", BindingFlags.Instance | BindingFlags.NonPublic);
+ if (importMethod == null)
+ throw new InvalidOperationException("Can't find 'HarmonyMethod.ImportMethod' method");
+ importMethod.Invoke(this, new object[] { methodInfo });
+ }
+ }
+}
+#endif
diff --git a/src/SMAPI/Framework/ModLoading/RewriteFacades/SpriteBatchFacade.cs b/src/SMAPI/Framework/ModLoading/RewriteFacades/SpriteBatchFacade.cs
new file mode 100644
index 00000000..cf71af77
--- /dev/null
+++ b/src/SMAPI/Framework/ModLoading/RewriteFacades/SpriteBatchFacade.cs
@@ -0,0 +1,57 @@
+using System.Diagnostics.CodeAnalysis;
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Graphics;
+
+namespace StardewModdingAPI.Framework.ModLoading.RewriteFacades
+{
+ /// <summary>Provides <see cref="SpriteBatch"/> method signatures that can be injected into mod code for compatibility between Linux/Mac or Windows.</summary>
+ /// <remarks>This is public to support SMAPI rewriting and should not be referenced directly by mods.</remarks>
+ [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Used via assembly rewriting")]
+ [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Linux/Mac.")]
+ [SuppressMessage("ReSharper", "CS1591", Justification = "Documentation not needed for facade classes.")]
+ public class SpriteBatchFacade : SpriteBatch
+ {
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ public SpriteBatchFacade(GraphicsDevice graphicsDevice) : base(graphicsDevice) { }
+
+
+ /****
+ ** MonoGame signatures
+ ****/
+ public new void Begin(SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState, Effect effect, Matrix? matrix)
+ {
+ base.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect, matrix ?? Matrix.Identity);
+ }
+
+ /****
+ ** XNA signatures
+ ****/
+ public new void Begin()
+ {
+ base.Begin();
+ }
+
+ public new void Begin(SpriteSortMode sortMode, BlendState blendState)
+ {
+ base.Begin(sortMode, blendState);
+ }
+
+ public new void Begin(SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState)
+ {
+ base.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState);
+ }
+
+ public new void Begin(SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState, Effect effect)
+ {
+ base.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect);
+ }
+
+ public new void Begin(SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState, Effect effect, Matrix transformMatrix)
+ {
+ base.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect, transformMatrix);
+ }
+ }
+}