Skip to content

Commit

Permalink
GetMethodFromStackframe fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kohanis committed Mar 7, 2024
1 parent 679d2d7 commit 8cb43cb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
6 changes: 5 additions & 1 deletion Harmony/Internal/HarmonySharedState.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mono.Cecil;
using MonoMod.Core.Platforms;
using MonoMod.Utils;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -127,7 +128,10 @@ internal static void UpdatePatchInfo(MethodBase original, MethodInfo replacement

internal static MethodBase GetOriginal(MethodInfo replacement)
{
lock (originals) return originals.GetValueSafe(replacement);
// The runtime can return several different MethodInfo's that point to the same method. Use the correct one
replacement = PlatformTriple.Current.GetIdentifiable(replacement) as MethodInfo;
lock (originals)
return originals.GetValueSafe(replacement);
}

internal static MethodBase FindReplacement(StackFrame frame)
Expand Down
5 changes: 1 addition & 4 deletions Harmony/Public/Harmony.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using MonoMod.Core.Platforms;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand Down Expand Up @@ -258,9 +257,7 @@ public IEnumerable<MethodBase> GetPatchedMethods()
public static MethodBase GetOriginalMethod(MethodInfo replacement)
{
if (replacement == null) throw new ArgumentNullException(nameof(replacement));
// The runtime can return several different MethodInfo's that point to the same method. Use the correct one
var identifiableReplacement = PlatformTriple.Current.GetIdentifiable(replacement) as MethodInfo;
return HarmonySharedState.GetOriginal(identifiableReplacement);
return HarmonySharedState.GetOriginal(replacement);
}

/// <summary>Tries to get the method from a stackframe including dynamic replacement methods</summary>
Expand Down
32 changes: 16 additions & 16 deletions HarmonyTests/Extras/RetrieveOriginalMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace HarmonyLibTests.Extras
{
Expand All @@ -14,20 +15,20 @@ private static void CheckStackTraceFor(MethodBase expectedMethod)
Assert.NotNull(expectedMethod);

var st = new StackTrace(1, false);
var method = Harmony.GetMethodFromStackframe(st.GetFrame(0));

Assert.NotNull(method);

if (method is MethodInfo replacement)
{
var original = Harmony.GetOriginalMethod(replacement);
Assert.NotNull(original);
Assert.AreEqual(original, expectedMethod);
}
var frame = st.GetFrame(0);
Assert.NotNull(frame);

var methodFromStackframe = Harmony.GetMethodFromStackframe(frame);
Assert.NotNull(methodFromStackframe);
Assert.AreEqual(expectedMethod, methodFromStackframe);

var replacement = frame.GetMethod() as MethodInfo;
Assert.NotNull(replacement);
var original = Harmony.GetOriginalMethod(replacement);
Assert.NotNull(original);
Assert.AreEqual(expectedMethod, original);
}

/* TODO
*
[Test]
public void TestRegularMethod()
{
Expand All @@ -37,7 +38,7 @@ public void TestRegularMethod()
_ = harmony.Patch(originalMethod, new HarmonyMethod(dummyPrefix));
PatchTarget();
}

[Test]
public void TestConstructor()
{
Expand All @@ -48,7 +49,6 @@ public void TestConstructor()
var inst = new NestedClass(5);
_ = inst.index;
}
*/

internal static void PatchTarget()
{
Expand All @@ -60,7 +60,7 @@ internal static void PatchTarget()
}
}

// [MethodImpl(MethodImplOptions.NoInlining)]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void DummyPrefix()
{
}
Expand All @@ -69,7 +69,7 @@ class NestedClass {
public NestedClass(int i)
{
try {
CheckStackTraceFor(AccessTools.Constructor(typeof(NestedClass), [typeof(int)]));
CheckStackTraceFor(AccessTools.Constructor(typeof(NestedClass), [typeof(int)]));
throw new Exception();
} catch (Exception e)
{
Expand Down

0 comments on commit 8cb43cb

Please sign in to comment.