Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
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-wasip1, 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-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, 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, stable)

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.

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-wasip1, 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-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, 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, stable)

missing documentation for a method
&self,
process: &Process,
module: &Module,
Expand Down
17 changes: 16 additions & 1 deletion src/game_engine/unity/mono/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,22 @@
}

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/mono/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/mono/class.rs

View workflow job for this annotation

GitHub Actions / build (wasm32-wasip1, nightly)

missing documentation for an associated function

Check warning on line 16 in src/game_engine/unity/mono/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/mono/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/mono/class.rs

View workflow job for this annotation

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

missing documentation for an associated function
process: &Process,
module: &Module,
component: Address,
) -> Result<Self, Error> {
process
.read_pointer(component, module.pointer_size)
.ok()
.filter(|val| !val.is_null())
.and_then(|addr| process.read_pointer(addr, 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 31 in src/game_engine/unity/mono/class.rs

View workflow job for this annotation

GitHub Actions / Test (Host)

missing documentation for a method

Check warning on line 31 in src/game_engine/unity/mono/class.rs

View workflow job for this annotation

GitHub Actions / build (wasm32-wasip1, nightly)

missing documentation for a method

Check warning on line 31 in src/game_engine/unity/mono/class.rs

View workflow job for this annotation

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

missing documentation for a method

Check warning on line 31 in src/game_engine/unity/mono/class.rs

View workflow job for this annotation

GitHub Actions / build (wasm32-wasip1, stable)

missing documentation for a method

Check warning on line 31 in src/game_engine/unity/mono/class.rs

View workflow job for this annotation

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

missing documentation for a method
&self,
process: &Process,
module: &Module,
Expand Down
164 changes: 164 additions & 0 deletions src/game_engine/unity/scene_manager/game_object.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
use super::{SceneManager, CSTR};
use crate::game_engine::unity::{il2cpp, mono};
use crate::string::ArrayCString;
use crate::{Address, Address32, Address64, Error, PointerSize, Process};
use core::array;
use core::mem::MaybeUninit;

/// Representing a GameObject. From a GameObject, you can get the attached components (includes the
/// C# scripts).
#[derive(Clone, Debug)]
pub struct GameObject {
pub(super) address: Address,
}

impl GameObject {
/// Get the name of the GameObject.
pub fn get_name<const N: usize>(
&self,
process: &Process,
scene_manager: &SceneManager,
) -> Result<ArrayCString<N>, Error> {
process.read_pointer_path(
self.address,
scene_manager.pointer_size,
&[scene_manager.offsets.game_object_name as u64, 0x0],
)
}

/// Traverse the classes associated with the Components attached to this game object.
pub fn classes<'a>(
&'a self,
process: &'a Process,
scene_manager: &'a SceneManager,
) -> Result<impl Iterator<Item = Address> + 'a, Error> {
let (number_of_components, component_pair_array): (usize, Address) =
match scene_manager.pointer_size {
PointerSize::Bit64 => {
let array = process
.read::<[Address64; 3]>(self.address + scene_manager.offsets.game_object)?;
(array[2].value() as usize, array[0].into())
}
_ => {
let array = process
.read::<[Address32; 3]>(self.address + scene_manager.offsets.game_object)?;
(array[2].value() as usize, array[0].into())
}
};

if number_of_components == 0 {
return Err(Error {});
}

const ARRAY_SIZE: usize = 128;

let components: [Address; ARRAY_SIZE] = match scene_manager.pointer_size {
PointerSize::Bit64 => {
let mut buf = [MaybeUninit::<[Address64; 2]>::uninit(); ARRAY_SIZE];
let slice = process.read_into_uninit_slice(
component_pair_array,
&mut buf[..number_of_components],
)?;

let mut iter = slice.iter_mut();
array::from_fn(|_| {
iter.next()
.map(|&mut [_, second]| second.into())
.unwrap_or_default()
})
}
_ => {
let mut buf = [MaybeUninit::<[Address32; 2]>::uninit(); ARRAY_SIZE];
let slice = process.read_into_uninit_slice(
component_pair_array,
&mut buf[..number_of_components],
)?;

let mut iter = slice.iter_mut();
array::from_fn(|_| {
iter.next()
.map(|&mut [_, second]| second.into())
.unwrap_or_default()
})
}
};

Ok((1..number_of_components).filter_map(move |m| {
process
.read_pointer(
components[m] + scene_manager.offsets.klass,
scene_manager.pointer_size,
)
.ok()
.filter(|val| !val.is_null())
}))
}

/// Tries to find the base address of a class in the current `GameObject` by name.
///
/// Mono only.
pub fn get_class_mono(
&self,
process: &Process,
scene_manager: &SceneManager,
module: &mono::Module,
name: &str,
) -> Result<Address, Error> {
if scene_manager.is_il2cpp {
return Err(Error {});
}

self.classes(process, scene_manager)?
.find(|&addr| {
let val = mono::Class::get_from_component(process, module, addr)
.and_then(|c| c.get_name::<CSTR>(process, module));

val.is_ok_and(|class_name| class_name.matches(name))
})
.ok_or(Error {})
}

/// Tries to find the base address of a class in the current `GameObject` by name.
///
/// IL2CPP only.
pub fn get_class_il2cpp(
&self,
process: &Process,
scene_manager: &SceneManager,
module: &il2cpp::Module,
name: &str,
) -> Result<Address, Error> {
if !scene_manager.is_il2cpp {
return Err(Error {});
}

self.classes(process, scene_manager)?
.find(|&addr| {
let val = il2cpp::Class::get_from_component(process, module, addr)
.and_then(|c| c.get_name::<CSTR>(process, module));

val.is_ok_and(|class_name| class_name.matches(name))
})
.ok_or(Error {})
}

/// Returns whether the game object is considered "active" by the scene (if it or any of its
/// parents are inactive, then the game object is inactive)
pub fn is_active_in_hierarchy(
&self,
process: &Process,
scene_manager: &SceneManager,
) -> Result<bool, Error> {
process.read::<bool>(self.address + scene_manager.offsets.game_object_activeinhierarchy)
}

/// Returns whether the game object is considered "active" by itself (irrespective of any of its
/// parents)
pub fn is_active_self(
&self,
process: &Process,
scene_manager: &SceneManager,
) -> Result<bool, Error> {
process.read::<bool>(self.address + scene_manager.offsets.game_object_activeself)
}
}
80 changes: 0 additions & 80 deletions src/game_engine/unity/scene_manager/game_objects.rs

This file was deleted.

6 changes: 4 additions & 2 deletions src/game_engine/unity/scene_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ use crate::{
Address, Address32, Error, PointerSize, Process,
};

mod game_objects;

mod offsets;

mod transform;
pub use transform::Transform;

use offsets::Offsets;

mod game_object;
pub use game_object::GameObject;

mod scene;

pub use scene::Scene;

use super::{BinaryFormat, CSTR};
Expand Down
9 changes: 6 additions & 3 deletions src/game_engine/unity/scene_manager/offsets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ pub(super) struct Offsets {
pub(super) root_storage_container: u8,
pub(super) game_object: u8,
pub(super) game_object_name: u8,
pub(super) game_object_activeself: u8,
pub(super) game_object_activeinhierarchy: u8,
pub(super) klass: u8,
pub(super) klass_name: u8,
pub(super) children_pointer: u8,
}

Expand All @@ -26,8 +27,9 @@ impl Offsets {
root_storage_container: 0xB0,
game_object: 0x30,
game_object_name: 0x60,
game_object_activeself: 0x5E,
game_object_activeinhierarchy: 0x5F,
klass: 0x28,
klass_name: 0x48,
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.

this changes depending on the mono version, which, of course it does.

Code updated to use the mono/il2cpp class name offsets

children_pointer: 0x70,
}),
PointerSize::Bit32 => Some(&Self {
Expand All @@ -39,8 +41,9 @@ impl Offsets {
root_storage_container: 0x88,
game_object: 0x1C,
game_object_name: 0x3C,
game_object_activeself: 0x32,
game_object_activeinhierarchy: 0x33,
klass: 0x18,
klass_name: 0x2C,
children_pointer: 0x50,
}),
_ => None,
Expand Down
Loading
Loading