Skip to content

Commit a1fb4a0

Browse files
Bug fix - Method copying was not correctly done for OpCode.Switch
1 parent 7e2a1bb commit a1fb4a0

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

Interception/Cauldron.Interception.Cecilator/Extension.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22
using Mono.Cecil.Cil;
33
using Mono.Cecil.Rocks;
44
using System;
5-
using System.Collections;
6-
using System.Collections.Concurrent;
75
using System.Collections.Generic;
86
using System.Collections.ObjectModel;
97
using System.Linq;
10-
using System.Threading.Tasks;
118

129
namespace Cauldron.Interception.Cecilator
1310
{
@@ -315,6 +312,15 @@ public static IEnumerable<TypeReference> GetNestedTypes(this TypeReference type)
315312

316313
public static bool Implements(this TypeReference type, string interfaceName) => type.GetInterfaces().Any(x => x.FullName == interfaceName);
317314

315+
public static int IndexOf(this Mono.Collections.Generic.Collection<Instruction> instructions, int offset)
316+
{
317+
for (int i = 0; i < instructions.Count; i++)
318+
if (instructions[i].Offset == offset)
319+
return i;
320+
321+
return -1;
322+
}
323+
318324
public static bool IsAssignableFrom(this BuilderType target, BuilderType source) =>
319325
target == source ||
320326
target.typeDefinition == source.typeDefinition ||

Interception/Cauldron.Interception.Cecilator/InstructionsSet.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,11 @@ public Method Copy(Modifiers modifiers, string newName)
143143
var instructionAction = new Func<Instruction, int, Instruction>((item, i) =>
144144
{
145145
var operand = item.Operand as Instruction;
146-
var index = this.method.methodDefinition.Body.Instructions.IndexOf(operand);
146+
var index = this.method.methodDefinition.Body.Instructions.IndexOf(operand?.Offset ?? item.Offset);
147147
jumps.Add(new Tuple<int, int>(i, index));
148148

149+
this.LogInfo($"IL_{item.Offset.ToString("X4")}: {i} {index}");
150+
149151
var instruction = methodProcessor.Create(OpCodes.Nop);
150152
instruction.OpCode = item.OpCode;
151153
return instruction;

0 commit comments

Comments
 (0)