diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-05-18 22:44:06 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-05-18 22:44:06 -0400 |
commit | d1bf3d52352df4bb720cf0fa87dcd6a64f35446a (patch) | |
tree | fe48d4e07815a6e183e4773492ed8cda1b7e239c /src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyInstanceMethods.cs | |
parent | 21303a4e987e4169f3bf0c55c7099d0d07536ca5 (diff) | |
download | SMAPI-d1bf3d52352df4bb720cf0fa87dcd6a64f35446a.tar.gz SMAPI-d1bf3d52352df4bb720cf0fa87dcd6a64f35446a.tar.bz2 SMAPI-d1bf3d52352df4bb720cf0fa87dcd6a64f35446a.zip |
move facade namespace (#711)
Diffstat (limited to 'src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyInstanceMethods.cs')
-rw-r--r-- | src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyInstanceMethods.cs | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyInstanceMethods.cs b/src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyInstanceMethods.cs new file mode 100644 index 00000000..78cf25f8 --- /dev/null +++ b/src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyInstanceMethods.cs @@ -0,0 +1,49 @@ +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", "CS1591", Justification = "Documentation not needed for facade classes.")] + public class HarmonyInstanceMethods : Harmony + { + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="id">The unique patch identifier.</param> + public HarmonyInstanceMethods(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) + { + try + { + MethodInfo method = base.Patch(original: original, prefix: prefix, postfix: postfix, transpiler: transpiler); + return (DynamicMethod)method; + } + catch (Exception ex) + { + var patchTypes = new List<string>(); + if (prefix != null) + patchTypes.Add("prefix"); + if (postfix != null) + patchTypes.Add("postfix"); + if (transpiler != null) + patchTypes.Add("transpiler"); + + throw new Exception($"Harmony instance {this.Id} failed applying {string.Join("/", patchTypes)} to method {original.DeclaringType?.FullName}.{original.Name}.", ex); + } + } + } +} |