Skip to content

Commit

Permalink
Differentiate between MethodReplacer and ConstructorReplacer
Browse files Browse the repository at this point in the history
  • Loading branch information
pardeike committed Jul 21, 2018
1 parent 3b5f6da commit a1cf7fc
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions Harmony/Transpilers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Harmony
{
public static class Transpilers
{
public static IEnumerable<CodeInstruction> MethodReplacer(this IEnumerable<CodeInstruction> instructions, MethodBase from, MethodBase to)
private static IEnumerable<CodeInstruction> Replacer(IEnumerable<CodeInstruction> instructions, MethodBase from, MethodBase to, OpCode opcode)
{
if (from == null)
throw new ArgumentException("Unexpected null argument", nameof(from));
Expand All @@ -19,13 +19,23 @@ public static IEnumerable<CodeInstruction> MethodReplacer(this IEnumerable<CodeI
var method = instruction.operand as MethodBase;
if (method == from)
{
instruction.opcode = OpCodes.Call;
instruction.opcode = opcode;
instruction.operand = to;
}
yield return instruction;
}
}

public static IEnumerable<CodeInstruction> MethodReplacer(this IEnumerable<CodeInstruction> instructions, MethodBase from, MethodBase to)
{
return Replacer(instructions, from, to, OpCodes.Call);
}

public static IEnumerable<CodeInstruction> ConstructorReplacer(this IEnumerable<CodeInstruction> instructions, MethodBase from, MethodBase to)
{
return Replacer(instructions, from, to, OpCodes.Newobj);
}

public static IEnumerable<CodeInstruction> DebugLogger(this IEnumerable<CodeInstruction> instructions, string text)
{
yield return new CodeInstruction(OpCodes.Ldstr, text);
Expand Down

0 comments on commit a1cf7fc

Please sign in to comment.