forked from LavaGang/MelonLoader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnhollowerSupport.cs
46 lines (41 loc) · 2.66 KB
/
UnhollowerSupport.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Reflection;
namespace MelonLoader
{
public class UnhollowerSupport
{
private static Assembly UnhollowerBaseLib = null;
private static Type Il2CppObjectBaseType = null;
internal static Type Il2CppMethodInfoType = null;
internal static MethodInfo Il2CppObjectBaseToPtrMethod = null;
internal static MethodInfo Il2CppStringToManagedMethod = null;
internal static MethodInfo ManagedStringToIl2CppMethod = null;
internal static MethodInfo GetIl2CppMethodInfoPointerFieldForGeneratedMethod = null;
internal static void Initialize()
{
UnhollowerBaseLib = Assembly.Load("UnhollowerBaseLib");
Il2CppObjectBaseType = UnhollowerBaseLib.GetType("UnhollowerBaseLib.Il2CppObjectBase");
Il2CppMethodInfoType = UnhollowerBaseLib.GetType("UnhollowerBaseLib.Runtime.Il2CppMethodInfo");
Il2CppObjectBaseToPtrMethod = UnhollowerBaseLib.GetType("UnhollowerBaseLib.IL2CPP").GetMethod("Il2CppObjectBaseToPtr");
Il2CppStringToManagedMethod = UnhollowerBaseLib.GetType("UnhollowerBaseLib.IL2CPP").GetMethod("Il2CppStringToManaged");
ManagedStringToIl2CppMethod = UnhollowerBaseLib.GetType("UnhollowerBaseLib.IL2CPP").GetMethod("ManagedStringToIl2Cpp");
GetIl2CppMethodInfoPointerFieldForGeneratedMethod = UnhollowerBaseLib.GetType("UnhollowerBaseLib.UnhollowerUtils").GetMethod("GetIl2CppMethodInfoPointerFieldForGeneratedMethod");
}
public static bool IsGeneratedAssemblyType(Type type) => (Imports.IsIl2CppGame() && !Il2CppObjectBaseType.Equals(null) && !type.Equals(null) && type.IsSubclassOf(Il2CppObjectBaseType));
public static IntPtr MethodBaseToIl2CppMethodInfoPointer(MethodBase method)
{
FieldInfo methodPtr = (FieldInfo)GetIl2CppMethodInfoPointerFieldForGeneratedMethod.Invoke(null, new object[] { method });
if (methodPtr == null)
throw new NotSupportedException($"Cannot get IntPtr for {method.Name} as there is no corresponding IL2CPP method");
return (IntPtr)methodPtr.GetValue(null);
}
public static T Il2CppObjectPtrToIl2CppObject<T>(IntPtr ptr)
{
if (ptr == IntPtr.Zero)
throw new NullReferenceException("The ptr cannot be IntPtr.Zero.");
if (!IsGeneratedAssemblyType(typeof(T)))
throw new NullReferenceException("The type must be a Generated Assembly Type.");
return (T)typeof(T).GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(IntPtr) }, new ParameterModifier[0]).Invoke(new object[] { ptr });
}
}
}