Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
7988c36
first pass - no monov2 support
mitchell-merry Apr 19, 2026
f224c66
pull v2 changes over
mitchell-merry May 10, 2026
7c3df3e
Merge branch 'master' into mono-v3-generic-class-field-count-fix
mitchell-merry May 10, 2026
93df9f2
fix mono v2 mac offset
mitchell-merry May 10, 2026
d6d3695
mono v3 pe fix
mitchell-merry May 10, 2026
b8aca86
add supposed offsets for v2 mono 32 bit, remove prints
mitchell-merry May 10, 2026
9569dbd
clean up some comments
mitchell-merry May 10, 2026
8767f40
clean up some more prints
mitchell-merry May 10, 2026
1a5e036
fmt + cleaning stuff
mitchell-merry May 10, 2026
dc14769
further cleaning/commenting
mitchell-merry May 10, 2026
4f63667
another one
mitchell-merry May 10, 2026
6c56fed
Separate the concept of Transform and GameObject; separate the concep…
mitchell-merry May 11, 2026
958fbd8
Add isSelf function, add / update more comments
mitchell-merry May 11, 2026
f575ae4
Create get_class_mono/il2cpp which should fix the klass_name discrepa…
mitchell-merry May 11, 2026
0152784
come on now
mitchell-merry May 11, 2026
25d397b
remove todo comments
mitchell-merry May 11, 2026
d078723
tweak
mitchell-merry May 11, 2026
c974ef2
Revert changes for scene name and path as string
mitchell-merry May 13, 2026
e964365
tmp
mitchell-merry May 14, 2026
5ab8c87
add new sig, add offsets
mitchell-merry May 15, 2026
135f371
update project settings
mitchell-merry May 15, 2026
305c1c5
add cheat table
mitchell-merry May 15, 2026
440ec35
add to todo
mitchell-merry May 15, 2026
25f596a
latest unity version etc
mitchell-merry May 16, 2026
5df5ac0
create mono::Object and clarify everything
mitchell-merry May 16, 2026
822dac3
comments
mitchell-merry May 16, 2026
1b3ed11
update todo
mitchell-merry May 16, 2026
50be285
Start cleaning
mitchell-merry Jun 27, 2026
1cb0786
remove more
mitchell-merry Jun 27, 2026
03646e0
more cleaning + formatting
mitchell-merry Jun 27, 2026
b54ef8e
Allow manually overriding the scene manager offsets
mitchell-merry Jun 27, 2026
8679e33
Merge branch 'master' into transform-refactor-tweaks
mitchell-merry Jun 27, 2026
a24f19e
Add another fixme
mitchell-merry Jun 27, 2026
63776b0
Revert changes to offsets, leave note
mitchell-merry Jun 27, 2026
972bb5e
Add get_component_mono_object to streamline
mitchell-merry Jun 27, 2026
1a29b00
Support older Unity versions by splitting on Mono version when readin…
mitchell-merry Jun 27, 2026
6657b25
Couple doc improvements
mitchell-merry Jun 27, 2026
a05a994
Clean up enum variants + remove todo comment
mitchell-merry Jun 27, 2026
f323316
Some more documentation
mitchell-merry Jun 28, 2026
7f2f828
Fix the PE UnityPlayer.dll sig code
mitchell-merry Jul 2, 2026
c05c8bb
Merge branch 'master' into mono-v3-generic-class-field-count-fix
mitchell-merry Jul 2, 2026
cea4ad0
Merge branch 'mono-v3-generic-class-field-count-fix' into transform-r…
mitchell-merry Jul 2, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
/Cargo.lock
.idea
**/.DS_Store
15 changes: 14 additions & 1 deletion src/game_engine/unity/il2cpp/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,20 @@
}

impl Class {
pub(super) fn get_name<const N: usize>(
pub fn get_from_component(

Check warning on line 16 in src/game_engine/unity/il2cpp/class.rs

View workflow job for this annotation

GitHub Actions / Test (Host)

missing documentation for an associated function

Check warning on line 16 in src/game_engine/unity/il2cpp/class.rs

View workflow job for this annotation

GitHub Actions / build (wasm32-unknown-unknown, stable)

missing documentation for an associated function

Check warning on line 16 in src/game_engine/unity/il2cpp/class.rs

View workflow job for this annotation

GitHub Actions / build (wasm32-wasip1, stable)

missing documentation for an associated function

Check warning on line 16 in src/game_engine/unity/il2cpp/class.rs

View workflow job for this annotation

GitHub Actions / build (wasm32-unknown-unknown, nightly)

missing documentation for an associated function

Check warning on line 16 in src/game_engine/unity/il2cpp/class.rs

View workflow job for this annotation

GitHub Actions / build (wasm32-wasip1, nightly)

missing documentation for an associated function

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"component" isn't accurate here, but there's something in between the component and the class. not exactly sure what it is.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[ ] TODO: figure out what it is

process: &Process,
module: &Module,
component: Address,
) -> Result<Self, Error> {
process
.read_pointer(component, module.pointer_size)
.ok()
.filter(|val| !val.is_null())
.map(|class| Class { class })
.ok_or(Error {})
}

pub fn get_name<const N: usize>(

Check warning on line 29 in src/game_engine/unity/il2cpp/class.rs

View workflow job for this annotation

GitHub Actions / Test (Host)

missing documentation for a method

Check warning on line 29 in src/game_engine/unity/il2cpp/class.rs

View workflow job for this annotation

GitHub Actions / build (wasm32-unknown-unknown, stable)

missing documentation for a method

Check warning on line 29 in src/game_engine/unity/il2cpp/class.rs

View workflow job for this annotation

GitHub Actions / build (wasm32-wasip1, stable)

missing documentation for a method

Check warning on line 29 in src/game_engine/unity/il2cpp/class.rs

View workflow job for this annotation

GitHub Actions / build (wasm32-unknown-unknown, nightly)

missing documentation for a method

Check warning on line 29 in src/game_engine/unity/il2cpp/class.rs

View workflow job for this annotation

GitHub Actions / build (wasm32-wasip1, nightly)

missing documentation for a method
&self,
process: &Process,
module: &Module,
Expand Down
28 changes: 28 additions & 0 deletions src/game_engine/unity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@
// https://github.com/Unity-Technologies/mono
// https://github.com/CryZe/lunistice-auto-splitter/blob/b8c01031991783f7b41044099ee69edd54514dba/asr-dotnet/src/lib.rs

use crate::file_format::{elf, macho, pe};
use crate::{Error, PointerSize, Process};

pub mod il2cpp;
pub mod mono;
pub mod scene_manager;
Expand All @@ -91,9 +94,15 @@ const CSTR: usize = 128;

#[derive(Copy, Clone, PartialEq, Hash, Debug)]
#[non_exhaustive]
/// The file format of a given executable / binary - different per operating system.
/// See https://en.wikipedia.org/wiki/Comparison_of_executable_file_formats
enum BinaryFormat {
/// [Portable Executable](https://en.wikipedia.org/wiki/Portable_Executable), for Windows.
PE,
/// [Executable and Linkable Format](https://en.wikipedia.org/wiki/Executable_and_Linkable_Format),
/// for Linux.
ELF,
/// [Mach-O](https://en.wikipedia.org/wiki/Mach-O) (Mach object), for Mac.
MachO,
}

Expand All @@ -103,3 +112,22 @@ fn get_backing_name(name: &str) -> Option<&str> {
let end = name[start + 1..].find('>')?;
Some(&name[start + 1..start + 1 + end])
}

impl Process {
/// Get the [BinaryFormat](BinaryFormat) and pointer size of the main module of the Process
// FIXME: BinaryFormat and this function should probably be widely available (rather
// than gated by Unity)
fn get_format_and_pointer_size(&self) -> Result<(BinaryFormat, PointerSize), Error> {
let main_module = self.get_main_module_range()?;

if let Some(x) = pe::MachineType::read(self, main_module.0) {
Ok((BinaryFormat::PE, x.pointer_size().ok_or(Error {})?))
} else if let Some(pointer_size) = elf::pointer_size(self, main_module.0) {
Ok((BinaryFormat::ELF, pointer_size))
} else if let Some(pointer_size) = macho::pointer_size(self, main_module) {
Ok((BinaryFormat::MachO, pointer_size))
} else {
Err(Error {})
}
}
}
122 changes: 117 additions & 5 deletions src/game_engine/unity/mono/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,43 @@ use crate::{future::retry, string::ArrayCString, Address, Error, Process};

#[cfg(feature = "derive")]
pub use asr_derive::MonoClass as Class;
use bytemuck::CheckedBitPattern;

/// The kind of MonoClass.
/// See https://github.com/mono/mono/blob/0f53e9e151d92944cacab3e24ac359410c606df6/mono/metadata/class-internals.h#L267
#[derive(CheckedBitPattern, Copy, Clone, Debug, Default, PartialEq, Eq)]
#[repr(u8)]
#[allow(unused)]
enum MonoTypeKind {
/// Non-generic type
Def = 1,
/// Generic type definition
Gtd = 2,
/// Generic instantiation
GInst = 3,
/// Generic parameter
GParam = 4,
/// vector or array
Array = 5,
/// pointer or function pointer
Pointer = 6,
/// Not a real class kind, internal to Mono
GcFiller = 0xAC,

#[default]
Unknown,
}

/// A .NET class that is part of an [`Image`](Image).
#[derive(Copy, Clone)]
#[derive(Copy, Clone, Debug)]
pub struct Class {
pub(super) class: Address,
/// The address of the class
pub class: Address,
}

impl Class {
pub(super) fn get_name<const N: usize>(
/// Get the name of the class
pub fn get_name<const N: usize>(
&self,
process: &Process,
module: &Module,
Expand All @@ -36,6 +64,61 @@ impl Class {
.and_then(|addr| process.read(addr))
}

fn class_kind(&self, process: &Process, module: &Module) -> Result<MonoTypeKind, Error> {
match module.version {
// See https://github.com/mono/mono/blob/337052f86112fc0dc8435c5c4a2de43b399a14bb/mono/metadata/class-internals.h#L327
Version::V2 => {
let byte =
process.read::<u8>(self.class + module.offsets.class.class_kind)? & 0x7u8;

if !MonoTypeKind::is_valid_bit_pattern(&byte) {
return Err(Error {});
}

// SAFETY: We just checked if this was valid
let kind: MonoTypeKind = unsafe { (&raw const byte).cast::<MonoTypeKind>().read() };

Ok(kind)
}
// See https://github.com/mono/mono/blob/0f53e9e151d92944cacab3e24ac359410c606df6/mono/metadata/class-private-definition.h#L28
Version::V3 => {
process.read::<MonoTypeKind>(self.class + module.offsets.class.class_kind)
}
_ => Err(Error {}),
}
}

fn field_count(&self, process: &Process, module: &Module) -> Result<i32, Error> {
match module.version {
Version::V1 | Version::V1Cattrs => {
process.read::<i32>(self.class + module.offsets.class.field_count)
}
Version::V2 | Version::V3 => {
let class_kind = self.class_kind(process, module)?;

// See https://github.com/mono/mono/blob/0f53e9e151d92944cacab3e24ac359410c606df6/mono/metadata/class-accessors.c#L216
match class_kind {
MonoTypeKind::Def | MonoTypeKind::Gtd => {
process.read::<i32>(self.class + module.offsets.class.field_count)
}
MonoTypeKind::GInst => {
let generic_class = process.read_pointer(
self.class + module.offsets.class.generic_class,
module.get_pointer_size(),
)?;
let container_class = Class {
class: process
.read_pointer(generic_class + 0x0, module.get_pointer_size())?,
};

container_class.field_count(process, module)
}
_ => Ok(0),
}
}
}
}

fn fields<'a>(
&'a self,
process: &'a Process,
Expand All @@ -61,8 +144,8 @@ impl Class {
// Prepare for next iteration
this_class = class.get_parent(process, module);

let field_count = process
.read::<i32>(class.class + module.offsets.class.field_count)
let field_count = class
.field_count(process, module)
.ok()
.filter(|&val| val > 0)
.unwrap_or_default();
Expand Down Expand Up @@ -215,3 +298,32 @@ impl Class {
retry(|| self.get_parent(process, module)).await
}
}

/** Represents a MonoObject, which is an instantiation of a MonoVTable (and therefore MonoClass). It
* is where the actual fields of the class live.
*
* See https://github.com/mono/mono/blob/0f53e9e151d92944cacab3e24ac359410c606df6/mono/metadata/object.h#L31.
*/
#[derive(Debug, Clone)]
pub struct Object {
/// The address of the Mono object.
pub address: Address,
}

impl Object {
/** Get the Mono [class](Class) of this object. */
pub fn get_class(&self, process: &Process, module: &Module) -> Result<Class, Error> {
process
// MonoVTable *vtable
// See https://github.com/mono/mono/blob/0f53e9e151d92944cacab3e24ac359410c606df6/mono/metadata/object.h#L32
.read_pointer(self.address, module.pointer_size)
.ok()
.filter(|val| !val.is_null())
// MonoClass *klass
// See https://github.com/mono/mono/blob/0f53e9e151d92944cacab3e24ac359410c606df6/mono/metadata/class-internals.h#L360
.and_then(|addr| process.read_pointer(addr, module.pointer_size).ok())
.filter(|val| !val.is_null())
.map(|class| Class { class })
.ok_or(Error {})
}
}
7 changes: 4 additions & 3 deletions src/game_engine/unity/mono/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use assembly::Assembly;
mod image;
pub use image::Image;
mod class;
pub use class::Class;
pub use class::{Class, Object};
mod field;
use field::Field;
mod version;
Expand Down Expand Up @@ -146,6 +146,7 @@ impl Module {
check_pos: Some(7),
check_byte: 0xF9,
};

if let Some(scan_address) = SIG_MONO_X86_64_MACHO
.scan_process_range(process, (root_domain_function_address, 0x100))
.map(|a| a + 3)
Expand Down Expand Up @@ -193,12 +194,12 @@ impl Module {
}

/// Retrieve the [Mono version](Version) of the module.
pub fn get_version(&self) -> Version {
pub const fn get_version(&self) -> Version {
self.version
}

/// Retrieve the [pointer size](PointerSize) of the process/module.
pub fn get_pointer_size(&self) -> PointerSize {
pub const fn get_pointer_size(&self) -> PointerSize {
self.pointer_size
}

Expand Down
Loading
Loading