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 committed Dec 26, 2024
1 parent e89e215 commit c48023c
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 @@ -100,7 +102,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 @@ -156,4 +158,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 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];
}
}

0 comments on commit c48023c

Please sign in to comment.