Skip to content

Commit

Permalink
CecilEmitter.Dump: Fixing dumping ILLabels
Browse files Browse the repository at this point in the history
  • Loading branch information
kohanis authored and ManlyMarco committed Jan 10, 2025
1 parent f1a93ec commit c4392ee
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion Harmony/Internal/Util/CecilEmitter.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -96,7 +98,7 @@ public static void Dump(MethodDefinition md, IEnumerable<string> 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
};
Expand Down Expand Up @@ -136,4 +138,14 @@ private static string SanitizeTypeName(string typeName)
.Replace("<", "{")
.Replace(">", "}");
}

private static Instruction LabelFix(ILLabel label, Collection<Instruction> clone, Collection<Instruction> original, string originalName)
{
Debug.Assert(clone.Count == original.Count);
var target = label.Target;
var idx = original.IndexOf(target);
if (idx == -1)
throw new IndexOutOfRangeException($"ILLabel from dump copy of '{originalName}' cannot be relinked. It points to '{target}', which was not found in the original body");
return clone[idx];
}
}

0 comments on commit c4392ee

Please sign in to comment.