-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathAPI_il2cpp_domain_get_assemblies_hook.cs
More file actions
67 lines (51 loc) · 2.21 KB
/
API_il2cpp_domain_get_assemblies_hook.cs
File metadata and controls
67 lines (51 loc) · 2.21 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Runtime.InteropServices;
using Il2CppInterop.Runtime.Runtime;
namespace Il2CppInterop.Runtime.Injection.Hooks
{
internal unsafe class API_il2cpp_domain_get_assemblies_hook : Hook<API_il2cpp_domain_get_assemblies_hook.MethodDelegate>
{
public override MethodDelegate GetDetour() => Hook;
public override string TargetMethodName => "il2cpp_domain_get_assemblies";
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate IntPtr MethodDelegate(IntPtr domain, long* size);
private IntPtr currentDataPtr = IntPtr.Zero;
private int lastAssemblyListSize = -1;
private IntPtr Hook(IntPtr domain, long* size)
{
IntPtr assemblyArrayPtr = Original(domain, size);
if (InjectorHelpers.InjectedImages.Count > 0)
{
CreateCustomAssemblyList((int)*size, assemblyArrayPtr);
*size = lastAssemblyListSize;
return currentDataPtr;
}
return assemblyArrayPtr;
}
private void CreateCustomAssemblyList(int origSize, IntPtr assemblyArrayPtr)
{
var newSize = origSize + InjectorHelpers.InjectedImages.Count;
if (lastAssemblyListSize == newSize) return;
Il2CppAssembly** oldArray = (Il2CppAssembly**)assemblyArrayPtr;
if (currentDataPtr != IntPtr.Zero)
Marshal.FreeHGlobal(currentDataPtr);
currentDataPtr = Marshal.AllocHGlobal(newSize * sizeof(Il2CppSystem.IntPtr));
Il2CppAssembly** newArray = (Il2CppAssembly**)currentDataPtr;
int i;
for (i = 0; i < origSize; i++)
newArray[i] = oldArray[i];
i = origSize;
foreach (IntPtr imagePtr in InjectorHelpers.InjectedImages.Values)
{
var image = UnityVersionHandler.Wrap((Il2CppImage*)imagePtr);
newArray[i] = image.Assembly;
i++;
}
lastAssemblyListSize = newSize;
}
public override IntPtr FindTargetMethod()
{
return InjectorHelpers.GetIl2CppExport(nameof(IL2CPP.il2cpp_domain_get_assemblies));
}
}
}