Skip to content

Commit 366f359

Browse files
committed
Fix GameAssembly base address and memory size on macOS
1 parent 88c1f91 commit 366f359

5 files changed

Lines changed: 144 additions & 135 deletions

File tree

Il2CppInterop.Common/XrefScans/XrefScanMethodDb.cs

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,19 @@ static XrefScanMethodDb()
2222

2323
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
2424
{
25-
IntPtr errorPtr;
25+
var errorPtr = IntPtr.Zero;
2626
var libHandle = dlopen("GameAssembly.dylib", 2);
2727

28-
if (libHandle == IntPtr.Zero)
28+
if (libHandle == IntPtr.Zero
29+
&& Process.GetCurrentProcess().MainModule?.FileName is { } procPath
30+
&& Directory.GetParent(procPath)?.Parent?.FullName is { } appContentsPath
31+
&& Path.GetFileName(appContentsPath) == "Contents")
2932
{
30-
var currentProcessPath = Process.GetCurrentProcess().MainModule?.FileName;
33+
var gameAssemblyPath = Path.Combine(appContentsPath, "Frameworks", "GameAssembly.dylib");
3134

32-
if (!string.IsNullOrEmpty(currentProcessPath))
35+
if (File.Exists(gameAssemblyPath))
3336
{
34-
var appContentsPath = Path.GetDirectoryName(Path.GetDirectoryName(currentProcessPath));
35-
36-
if (!string.IsNullOrEmpty(appContentsPath) && Path.GetFileName(appContentsPath) == "Contents")
37-
{
38-
var gameAssemblyPath = Path.Combine(appContentsPath, "Frameworks", "GameAssembly.dylib");
39-
40-
if (File.Exists(gameAssemblyPath))
41-
{
42-
libHandle = dlopen(gameAssemblyPath, 2);
43-
}
44-
}
37+
libHandle = dlopen(gameAssemblyPath, 2);
4538
}
4639
}
4740

@@ -54,7 +47,7 @@ static XrefScanMethodDb()
5447
: "Unknown dlopen failure";
5548

5649
throw new DllNotFoundException(
57-
$"Failed to load GameAssembly.dylib with error message: {errorMessage}");
50+
$"Failed to load \"GameAssembly.dylib\" with error message: {errorMessage}");
5851
}
5952

6053
// Clear any previous error state.
@@ -134,19 +127,17 @@ private struct DlInfo
134127
public IntPtr dli_saddr;
135128
}
136129

137-
[DllImport("libSystem.dylib", EntryPoint = "dlopen",
138-
CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
130+
[DllImport("libSystem.dylib", EntryPoint = "dlopen", CallingConvention = CallingConvention.Cdecl,
131+
CharSet = CharSet.Ansi)]
139132
private static extern IntPtr dlopen(string filename, int flags);
140133

141-
[DllImport("libSystem.dylib", EntryPoint = "dlerror",
142-
CallingConvention = CallingConvention.Cdecl)]
134+
[DllImport("libSystem.dylib", EntryPoint = "dlerror", CallingConvention = CallingConvention.Cdecl)]
143135
private static extern IntPtr dlerror();
144136

145-
[DllImport("libSystem.dylib", EntryPoint = "dlsym",
146-
CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
137+
[DllImport("libSystem.dylib", EntryPoint = "dlsym", CallingConvention = CallingConvention.Cdecl,
138+
CharSet = CharSet.Ansi)]
147139
private static extern IntPtr dlsym(IntPtr handle, string symbol);
148140

149-
[DllImport("libSystem.dylib", EntryPoint = "dladdr",
150-
CallingConvention = CallingConvention.Cdecl)]
141+
[DllImport("libSystem.dylib", EntryPoint = "dladdr", CallingConvention = CallingConvention.Cdecl)]
151142
private static extern int dladdr(IntPtr addr, out DlInfo info);
152143
}

Il2CppInterop.Runtime/Injection/Hooks/Class_GetFieldDefaultValue_Hook.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Linq;
33
using System.Runtime.InteropServices;
44
using Il2CppInterop.Common;
@@ -141,7 +141,7 @@ public override IntPtr FindTargetMethod()
141141
// inlined but we'll treat it the same even though it doesn't receive the type parameter the RDX register
142142
// doesn't get cleared so we still get the same parameters
143143
var classGetDefaultFieldValue = s_Signatures
144-
.Select(s => MemoryUtils.FindSignatureInModule(InjectorHelpers.Il2CppModule, s))
144+
.Select(s => MemoryUtils.FindSignatureInBlock(InjectorHelpers.GameAssemblyBaseAddress, InjectorHelpers.GameAssemblyMemorySize, s))
145145
.FirstOrDefault(p => p != 0);
146146

147147
if (classGetDefaultFieldValue == 0)

Il2CppInterop.Runtime/Injection/Hooks/GenericMethod_GetMethod_Hook.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ internal unsafe class GenericMethod_GetMethod_Hook : Hook<GenericMethod_GetMetho
6060
public override IntPtr FindTargetMethod()
6161
{
6262
var genericMethodGetMethod = s_Signatures
63-
.Select(s => MemoryUtils.FindSignatureInModule(InjectorHelpers.Il2CppModule, s))
63+
.Select(s => MemoryUtils.FindSignatureInBlock(InjectorHelpers.GameAssemblyBaseAddress, InjectorHelpers.GameAssemblyMemorySize, s))
6464
.FirstOrDefault(p => p != 0);
6565

6666
if (genericMethodGetMethod == 0)

0 commit comments

Comments
 (0)