Skip to content

Commit c5bd38a

Browse files
committed
Support Mac SceneManager
1 parent 6294a11 commit c5bd38a

File tree

1 file changed

+46
-18
lines changed

1 file changed

+46
-18
lines changed

src/game_engine/unity/scene.rs

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
use core::{array, iter, mem::MaybeUninit};
1010

1111
use crate::{
12-
file_format::pe, future::retry, signature::Signature, string::ArrayCString, Address, Address32,
13-
Address64, Error, PointerSize, Process,
12+
file_format::{macho, pe},
13+
future::retry,
14+
signature::Signature,
15+
string::ArrayCString,
16+
Address, Address32, Address64, Error, PointerSize, Process,
1417
};
1518

1619
const CSTR: usize = 128;
@@ -30,33 +33,52 @@ pub struct SceneManager {
3033
impl SceneManager {
3134
/// Attaches to the scene manager in the given process.
3235
pub fn attach(process: &Process) -> Option<Self> {
33-
const SIG_64_BIT: Signature<13> = Signature::new("48 83 EC 20 4C 8B ?5 ???????? 33 F6");
36+
const SIG_64_BIT_PE: Signature<13> = Signature::new("48 83 EC 20 4C 8B ?5 ???????? 33 F6");
37+
const SIG_64_BIT_MACHO: Signature<13> =
38+
Signature::new("41 54 53 50 4C 8B ?5 ???????? 41 83");
3439
const SIG_32_1: Signature<12> = Signature::new("55 8B EC 51 A1 ???????? 53 33 DB");
3540
const SIG_32_2: Signature<6> = Signature::new("53 8D 41 ?? 33 DB");
3641
const SIG_32_3: Signature<14> = Signature::new("55 8B EC 83 EC 18 A1 ???????? 33 C9 53");
3742

38-
let unity_player = process.get_module_range("UnityPlayer.dll").ok()?;
43+
let (unity_player, format) = [
44+
("UnityPlayer.dll", BinaryFormat::PE),
45+
("UnityPlayer.dylib", BinaryFormat::MachO),
46+
]
47+
.into_iter()
48+
.find_map(|(name, format)| Some((process.get_module_range(name).ok()?, format)))?;
3949

40-
let pointer_size = match pe::MachineType::read(process, unity_player.0)? {
41-
pe::MachineType::X86_64 => PointerSize::Bit64,
42-
_ => PointerSize::Bit32,
50+
let pointer_size = match format {
51+
BinaryFormat::PE => pe::MachineType::read(process, unity_player.0)?.pointer_size()?,
52+
BinaryFormat::MachO => macho::pointer_size(process, unity_player)?,
4353
};
4454

4555
let is_il2cpp = process.get_module_address("GameAssembly.dll").is_ok();
4656

4757
// There are multiple signatures that can be used, depending on the version of Unity
4858
// used in the target game.
49-
let base_address: Address = if pointer_size == PointerSize::Bit64 {
50-
let addr = SIG_64_BIT.scan_process_range(process, unity_player)? + 7;
51-
addr + 0x4 + process.read::<i32>(addr).ok()?
52-
} else if let Some(addr) = SIG_32_1.scan_process_range(process, unity_player) {
53-
process.read::<Address32>(addr + 5).ok()?.into()
54-
} else if let Some(addr) = SIG_32_2.scan_process_range(process, unity_player) {
55-
process.read::<Address32>(addr.add_signed(-4)).ok()?.into()
56-
} else if let Some(addr) = SIG_32_3.scan_process_range(process, unity_player) {
57-
process.read::<Address32>(addr + 7).ok()?.into()
58-
} else {
59-
return None;
59+
let base_address: Address = match (pointer_size, format) {
60+
(PointerSize::Bit64, BinaryFormat::PE) => {
61+
let addr = SIG_64_BIT_PE.scan_process_range(process, unity_player)? + 7;
62+
addr + 0x4 + process.read::<i32>(addr).ok()?
63+
}
64+
(PointerSize::Bit64, BinaryFormat::MachO) => {
65+
let addr = SIG_64_BIT_MACHO.scan_process_range(process, unity_player)? + 7;
66+
addr + 0x4 + process.read::<i32>(addr).ok()?
67+
}
68+
(PointerSize::Bit32, BinaryFormat::PE) => {
69+
if let Some(addr) = SIG_32_1.scan_process_range(process, unity_player) {
70+
process.read::<Address32>(addr + 5).ok()?.into()
71+
} else if let Some(addr) = SIG_32_2.scan_process_range(process, unity_player) {
72+
process.read::<Address32>(addr.add_signed(-4)).ok()?.into()
73+
} else if let Some(addr) = SIG_32_3.scan_process_range(process, unity_player) {
74+
process.read::<Address32>(addr + 7).ok()?.into()
75+
} else {
76+
return None;
77+
}
78+
}
79+
_ => {
80+
return None;
81+
}
6082
};
6183

6284
let offsets = Offsets::new(pointer_size);
@@ -429,6 +451,12 @@ impl Transform {
429451
}
430452
}
431453

454+
#[derive(Copy, Clone, PartialEq, Hash, Debug)]
455+
enum BinaryFormat {
456+
PE,
457+
MachO,
458+
}
459+
432460
struct Offsets {
433461
scene_count: u8,
434462
active_scene: u8,

0 commit comments

Comments
 (0)