Skip to content

Commit

Permalink
Fixed ReversePatching, added GetOriginalInstructions
Browse files Browse the repository at this point in the history
  • Loading branch information
pardeike committed Jan 25, 2020
1 parent a2c0eeb commit 8c2cf47
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 23 deletions.
5 changes: 2 additions & 3 deletions Harmony/Internal/MethodPatcher.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using MonoMod.RuntimeDetour;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using MonoMod.RuntimeDetour;
using MonoMod.Utils;

namespace HarmonyLib
{
Expand All @@ -20,7 +19,7 @@ internal static class MethodPatcher
public static string PARAM_INDEX_PREFIX = "__";
public static string INSTANCE_FIELD_PREFIX = "___";

public static MethodInfo CreatePatchedMethod(MethodBase original, MethodBase source, string harmonyInstanceID, List<MethodInfo> prefixes, List<MethodInfo> postfixes, List<MethodInfo> transpilers, List<MethodInfo> finalizers)
internal static MethodInfo CreatePatchedMethod(MethodBase original, MethodBase source, string harmonyInstanceID, List<MethodInfo> prefixes, List<MethodInfo> postfixes, List<MethodInfo> transpilers, List<MethodInfo> finalizers)
{
try
{
Expand Down
33 changes: 15 additions & 18 deletions Harmony/Internal/PatchFunctions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;

Expand Down Expand Up @@ -164,28 +165,24 @@ internal static MethodInfo ReversePatch(HarmonyMethod standin, MethodBase origin
if (standin.method == null)
throw new ArgumentNullException($"{nameof(standin)}.{nameof(standin.method)}");

var processor = instance.CreateProcessor(standin.method);
var transpilers = new List<MethodInfo>();
if (standin.reversePatchType == HarmonyReversePatchType.Snapshot)
{
var info = Harmony.GetPatchInfo(original);
foreach (var tr in info.Transpilers)
{
var transpiler = new HarmonyMethod(tr.GetMethod(original))
{
before = tr.before,
after = tr.after,

priority = tr.priority
};
_ = processor.AddTranspiler(transpiler);
}
transpilers.AddRange(GetSortedPatchMethods(original, info.Transpilers.ToArray()));
}
if (postTranspiler != null)
{
var transpiler = new HarmonyMethod(postTranspiler) { priority = int.MinValue };
_ = processor.AddTranspiler(transpiler);
}
return processor.Patch();
if (postTranspiler != null) transpilers.Add(postTranspiler);

var empty = new List<MethodInfo>();
var replacement = MethodPatcher.CreatePatchedMethod(standin.method, original, instance.Id, empty, empty, transpilers, empty);
if (replacement == null) throw new MissingMethodException($"Cannot create dynamic replacement for {standin.method.FullDescription()}");

var errorString = Memory.DetourMethod(standin.method, replacement);
if (errorString != null)
throw new FormatException($"Method {standin.method.FullDescription()} cannot be patched. Reason: {errorString}");

PatchTools.RememberObject(standin.method, replacement);
return replacement;
}
}
}
13 changes: 13 additions & 0 deletions Harmony/Public/PatchProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;

namespace HarmonyLib
{
Expand Down Expand Up @@ -236,5 +237,17 @@ public static Patches GetPatchInfo(MethodBase method)
});
return result;
}

/// <summary>Returns the methods unmodified list of CodeInstructions</summary>
/// <param name="original">The original method</param>
/// <param name="generator">The generator that now contains all local variables and labels contained in the result</param>
/// <returns>A list containing all the original CodeInstructions</returns>
public static List<CodeInstruction> GetOriginalInstructions(MethodBase original, out ILGenerator generator)
{
var patch = DynamicTools.CreateDynamicMethod(original, $"_Copy{Guid.NewGuid()}");
generator = patch.GetILGenerator();
var reader = MethodBodyReader.GetInstructions(generator, original);
return reader.Select(ins => ins.GetCodeInstruction()).ToList();
}
}
}
2 changes: 1 addition & 1 deletion HarmonyTests/ReversePatching/AttributeReversePatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace HarmonyLibTests
[TestFixture]
public class AttributeReversePatches
{
//[Test]
[Test]
public void Test_ReversePatchingWithAttributes()
{
var test = new Class1Reverse();
Expand Down
2 changes: 1 addition & 1 deletion HarmonyTests/ReversePatching/ReverseTranspiling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace HarmonyLibTests
[TestFixture]
public class ReverseTranspiling
{
//[Test]
[Test]
public void Test_ReverseTranspilerPatching()
{
var class0 = new Class0Reverse();
Expand Down

0 comments on commit 8c2cf47

Please sign in to comment.