Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Unity3D GetFunctionPointer #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions RuntimeDetour/Platforms/Runtime/DetourRuntimeILPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,48 @@ private static unsafe void _SelftestGetStructHook(IntPtr a, IntPtr b, IntPtr c,

#endregion

#if UNITY_5_3_OR_NEWER //source https://github.com/Misaka-Mikoto-Tech/MonoHook
[StructLayout(LayoutKind.Sequential, Pack = 1)] // 好像在 IL2CPP 里无效
private struct __ForCopy
{
public long __dummy;
public MethodBase method;
}
protected virtual unsafe IntPtr GetFunctionPointer(MethodBase method,RuntimeMethodHandle handle)
{
// if (!LDasm.IsIL2CPP())
// return method.MethodHandle.GetFunctionPointer();
// else
__ForCopy __forCopy = new __ForCopy() {method = method};

long* ptr = &__forCopy.__dummy;
ptr++; // addr of _forCopy.method

IntPtr methodAddr = IntPtr.Zero;
if (sizeof(IntPtr) == 8)
{
long methodDataAddr = *(long*) ptr;
byte* ptrData = (byte*) methodDataAddr + sizeof(IntPtr) * 2; // offset of Il2CppReflectionMethod::const MethodInfo *method;

long methodPtr = 0;
methodPtr = *(long*) ptrData;
methodAddr = new IntPtr(*(long*) methodPtr); // MethodInfo::Il2CppMethodPointer methodPointer;
}
else
{
int methodDataAddr = *(int*) ptr;
byte* ptrData = (byte*) methodDataAddr + sizeof(IntPtr) * 2; // offset of Il2CppReflectionMethod::const MethodInfo *method;

int methodPtr = 0;
methodPtr = *(int*) ptrData;
methodAddr = new IntPtr(*(int*) methodPtr);
}
return methodAddr;
}
#else
protected virtual IntPtr GetFunctionPointer(MethodBase method, RuntimeMethodHandle handle)
=> handle.GetFunctionPointer();
#endif

protected virtual void PrepareMethod(MethodBase method, RuntimeMethodHandle handle)
=> RuntimeHelpers.PrepareMethod(handle);
Expand Down