Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from 0x0ade/master
Make MethodInvoker.Handler operate on stack instead of locals
- Loading branch information
Showing
4 changed files
with
160 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Harmony; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace HarmonyTests.Assets | ||
{ | ||
public class TestMethodInvokerObject | ||
{ | ||
public int Value; | ||
public void Method1(int a) | ||
{ | ||
Value += a; | ||
} | ||
} | ||
public struct TestMethodInvokerStruct | ||
{ | ||
public int Value; | ||
} | ||
public static class MethodInvokerClass | ||
{ | ||
|
||
public static void Method1(int a, ref int b, out int c, out TestMethodInvokerObject d, ref TestMethodInvokerStruct e) | ||
{ | ||
b = b + 1; | ||
c = b * 2; | ||
d = new TestMethodInvokerObject(); | ||
d.Value = a; | ||
e.Value = a; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using Harmony; | ||
using Harmony.ILCopying; | ||
using HarmonyTests.Assets; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace HarmonyTests | ||
{ | ||
[TestClass] | ||
public class TestMethodInvoker | ||
{ | ||
|
||
[TestMethod] | ||
public void TestMethodInvokerGeneral() | ||
{ | ||
var type = typeof(MethodInvokerClass); | ||
Assert.IsNotNull(type); | ||
var method = type.GetMethod("Method1"); | ||
Assert.IsNotNull(method); | ||
|
||
var handler = MethodInvoker.GetHandler(method); | ||
Assert.IsNotNull(handler); | ||
|
||
object[] args = new object[] { 1, 0, 0, /*out*/ null, /*ref*/ new TestMethodInvokerStruct() }; | ||
handler(null, args); | ||
Assert.AreEqual(args[0], 1); | ||
Assert.AreEqual(args[1], 1); | ||
Assert.AreEqual(args[2], 2); | ||
Assert.AreEqual(((TestMethodInvokerObject) args[3])?.Value, 1); | ||
Assert.AreEqual(((TestMethodInvokerStruct) args[4]).Value, 1); | ||
} | ||
|
||
[TestMethod] | ||
public void TestMethodInvokerSelfObject() | ||
{ | ||
var type = typeof(TestMethodInvokerObject); | ||
Assert.IsNotNull(type); | ||
var method = type.GetMethod("Method1"); | ||
Assert.IsNotNull(method); | ||
|
||
var handler = MethodInvoker.GetHandler(method); | ||
Assert.IsNotNull(handler); | ||
|
||
var instance = new TestMethodInvokerObject(); | ||
instance.Value = 1; | ||
|
||
object[] args = new object[] { 2 }; | ||
handler(instance, args); | ||
Assert.AreEqual(instance.Value, 3); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters