Skip to content

Commit 3e16aae

Browse files
committed
Offsets for 64-bit MachO V1, V2
1 parent 982ea34 commit 3e16aae

File tree

1 file changed

+60
-5
lines changed

1 file changed

+60
-5
lines changed

src/game_engine/unity/mono.rs

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl Module {
4848
.find_map(|&name| process.get_module_address(name).ok())?;
4949

5050
let is_64_bit = pe::MachineType::read(process, module)? == pe::MachineType::X86_64;
51-
let offsets = Offsets::new(version, is_64_bit);
51+
let offsets = Offsets::new(version, is_64_bit, BinaryFormat::PE);
5252

5353
let root_domain_function_address = pe::symbols(process, module)
5454
.find(|symbol| {
@@ -666,6 +666,13 @@ impl<const CAP: usize> UnityPointer<CAP> {
666666
}
667667
}
668668

669+
#[derive(Copy, Clone, PartialEq, Hash, Debug)]
670+
enum BinaryFormat {
671+
PE,
672+
#[cfg(feature = "std")]
673+
MachO,
674+
}
675+
669676
struct Offsets {
670677
monoassembly_aname: u8,
671678
monoassembly_image: u8,
@@ -688,9 +695,9 @@ struct Offsets {
688695
}
689696

690697
impl Offsets {
691-
const fn new(version: Version, is_64_bit: bool) -> &'static Self {
692-
match is_64_bit {
693-
true => match version {
698+
const fn new(version: Version, is_64_bit: bool, format: BinaryFormat) -> &'static Self {
699+
match (is_64_bit, format) {
700+
(true, BinaryFormat::PE) => match version {
694701
Version::V1 => &Self {
695702
monoassembly_aname: 0x10,
696703
monoassembly_image: 0x58,
@@ -754,7 +761,7 @@ impl Offsets {
754761
monoclassfieldalignment: 0x20,
755762
},
756763
},
757-
false => match version {
764+
(false, BinaryFormat::PE) => match version {
758765
Version::V1 => &Self {
759766
monoassembly_aname: 0x8,
760767
monoassembly_image: 0x40,
@@ -818,6 +825,54 @@ impl Offsets {
818825
monoclassfieldalignment: 0x10,
819826
},
820827
},
828+
#[cfg(feature = "std")]
829+
(true, BinaryFormat::MachO) => match version {
830+
Version::V1 => &Self {
831+
monoassembly_aname: 0x10,
832+
monoassembly_image: 0x58, // matches 64-bit PE V1
833+
monoimage_class_cache: 0x3D0, // matches 64-bit PE V1
834+
monointernalhashtable_table: 0x20,
835+
monointernalhashtable_size: 0x18,
836+
monoclassdef_next_class_cache: 0xF8, // 0x8 less than 64-bit PE V1
837+
monoclassdef_klass: 0x0,
838+
monoclass_name: 0x40, // 0x8 less than 64-bit PE V1
839+
monoclass_fields: 0xA0, // 0x8 less than 64-bit PE V1
840+
monoclassdef_field_count: 0x8C, // 0x8 less than 64-bit PE V1
841+
monoclass_runtime_info: 0xF0, // 0x8 less than 64-bit PE V1
842+
monoclass_vtable_size: 0x18, // MonoVtable.data
843+
monoclass_parent: 0x28, // 0x8 less than 64-bit PE V1
844+
monoclassfield_name: 0x8,
845+
monoclassfield_offset: 0x18,
846+
monoclassruntimeinfo_domain_vtables: 0x8,
847+
monovtable_vtable: 0x0, // UNUSED for V1
848+
monoclassfieldalignment: 0x20,
849+
},
850+
// 64-bit MachO V2 matches Unity2019_4_2020_3_x64_MachO_Offsets from
851+
// https://github.com/hackf5/unityspy/blob/master/src/HackF5.UnitySpy/Offsets/MonoLibraryOffsets.cs#L86
852+
Version::V2 => &Self {
853+
monoassembly_aname: 0x10,
854+
monoassembly_image: 0x60, // AssemblyImage = 0x44 + 0x1c
855+
monoimage_class_cache: 0x4C0, // ImageClassCache = 0x354 + 0x16c
856+
monointernalhashtable_table: 0x20, // HashTableTable = 0x14 + 0xc
857+
monointernalhashtable_size: 0x18, // HashTableSize = 0xc + 0xc
858+
monoclassdef_next_class_cache: 0x100, // TypeDefinitionNextClassCache = 0xa8 + 0x34 + 0x10 + 0x18 + 0x4 - 0x8
859+
monoclassdef_klass: 0x0,
860+
monoclass_name: 0x40, // TypeDefinitionName = 0x2c + 0x1c - 0x8
861+
monoclass_fields: 0x90, // TypeDefinitionFields = 0x60 + 0x20 + 0x18 - 0x8
862+
monoclassdef_field_count: 0xF8, // TypeDefinitionFieldCount = 0xa4 + 0x34 + 0x10 + 0x18 - 0x8
863+
monoclass_runtime_info: 0xC8, // TypeDefinitionRuntimeInfo = 0x84 + 0x34 + 0x18 - 0x8
864+
monoclass_vtable_size: 0x54, // TypeDefinitionVTableSize = 0x38 + 0x24 - 0x8
865+
monoclass_parent: 0x28, // TypeDefinitionParent = 0x20 + 0x10 - 0x8
866+
monoclassfield_name: 0x8,
867+
monoclassfield_offset: 0x18,
868+
monoclassruntimeinfo_domain_vtables: 0x8, // TypeDefinitionRuntimeInfoDomainVTables = 0x4 + 0x4
869+
monovtable_vtable: 0x40, // VTable = 0x28 + 0x18
870+
monoclassfieldalignment: 0x20,
871+
},
872+
Version::V3 => panic!("MachO V3 not supported"),
873+
},
874+
#[cfg(feature = "std")]
875+
(false, BinaryFormat::MachO) => panic!("32-bit MachO format not supported"),
821876
}
822877
}
823878
}

0 commit comments

Comments
 (0)