diff --git a/Harmony/Internal/Util/CecilEmitter.cs b/Harmony/Internal/Util/CecilEmitter.cs index dc8495fa..8c79e613 100644 --- a/Harmony/Internal/Util/CecilEmitter.cs +++ b/Harmony/Internal/Util/CecilEmitter.cs @@ -1,10 +1,12 @@ using HarmonyLib.Tools; using Mono.Cecil; using Mono.Cecil.Cil; +using Mono.Collections.Generic; using MonoMod.Cil; using MonoMod.Utils; using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; @@ -100,7 +102,7 @@ public static void Dump(MethodDefinition md, IEnumerable dumpPaths, Meth operand = operand switch { ParameterDefinition param => clone.Parameters[param.Index], - ILLabel label => label.Target, + ILLabel label => LabelFix(label, body.Instructions, md.Body.Instructions, originalName), IMetadataTokenProvider mtp => mtp.Relink(relinker, clone), _ => operand }; @@ -156,4 +158,14 @@ private static string SanitizeTypeName(string typeName) .Replace("<", "{") .Replace(">", "}"); } + + private static Instruction LabelFix(ILLabel label, Collection clone, Collection original, string originalName) + { + Debug.Assert(clone.Count == original.Count); + var target = label.Target; + var idx = original.IndexOf(target); + if (idx == -1) + throw new NullReferenceException($"ILLabel from dump copy of '{originalName}' cannot be relinked. It points to '{target}', which was not found in the original body"); + return clone[idx]; + } }