From 19397a89ff6f0d216637e1272b78c7e15c85bf76 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 9 May 2020 09:23:27 -0400 Subject: log detailed error for rewritten patch failures (#711) --- .../RewriteFacades/HarmonyInstanceMethods.cs | 26 ++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'src/SMAPI/Framework/RewriteFacades/HarmonyInstanceMethods.cs') diff --git a/src/SMAPI/Framework/RewriteFacades/HarmonyInstanceMethods.cs b/src/SMAPI/Framework/RewriteFacades/HarmonyInstanceMethods.cs index 0f906f51..bca76981 100644 --- a/src/SMAPI/Framework/RewriteFacades/HarmonyInstanceMethods.cs +++ b/src/SMAPI/Framework/RewriteFacades/HarmonyInstanceMethods.cs @@ -1,3 +1,5 @@ +using System; +using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Reflection.Emit; @@ -24,10 +26,30 @@ namespace StardewModdingAPI.Framework.RewriteFacades return new Harmony(id); } + /// Apply one or more patches to a method. + /// The original method. + /// The prefix to apply. + /// The postfix to apply. + /// The transpiler to apply. public DynamicMethod Patch(MethodBase original, HarmonyMethod prefix = null, HarmonyMethod postfix = null, HarmonyMethod transpiler = null) { - MethodInfo method = base.Patch(original: original, prefix: prefix, postfix: postfix, transpiler: transpiler); - return new DynamicMethod(method.Name, method.Attributes, method.CallingConvention, method.ReturnType, method.GetParameters().Select(p => p.ParameterType).ToArray(), method.Module, true); + try + { + MethodInfo method = base.Patch(original: original, prefix: prefix, postfix: postfix, transpiler: transpiler); + return new DynamicMethod(method.Name, method.Attributes, method.CallingConvention, method.ReturnType, method.GetParameters().Select(p => p.ParameterType).ToArray(), method.Module, true); + } + catch (Exception ex) + { + var patchTypes = new List(); + if (prefix != null) + patchTypes.Add("prefix"); + if (postfix != null) + patchTypes.Add("postfix"); + if (transpiler != null) + patchTypes.Add("transpiler"); + + throw new Exception($"Failed applying {string.Join("/", patchTypes)} to method {original.DeclaringType?.FullName}.{original.Name}", ex); + } } } } -- cgit