Expand description
Hook and call into a Unity Il2Cpp game from inside its process, the way MelonLoader mods do.
A mod is a cdylib loaded into the game (on Linux with LD_PRELOAD).
on_init runs once the game’s Il2Cpp runtime has started, and from there
methods are found by name and hooked with ordinary closures. The closure’s
types are checked against the method’s real signature when the hook is
installed:
use hao_il2cpp_runtime::{Il2Cpp, Object, Original};
hao_il2cpp_runtime::on_init(|il2cpp: &'static Il2Cpp| {
let health = il2cpp.class("Assembly-CSharp", "Assets.Scripts.Inventory__Items__Pickups", "PlayerHealth")?;
// `int Heal(float amount, bool allowOverheal)`: the instance comes first.
health.method("Heal", 2)?.hook(|this: Object, amount: f32, overheal: bool, original: Original<_, i32>| {
original.call((this, amount * 2.0, overheal))
})?;
Ok(())
});Modules§
- element
- Managed element types (
Il2CppTypeEnum). - reflect
- Reflection roots for the
reflectfeature. Seehao::reflectfor the pattern. Only the plain-data types reflect; the runtime handles (raw pointers, GC handles, hooks) do not.
Macros§
- original
- Inside a detour from
Method::hook_raw, gets the function that calls the original method. Must be the first thing the detour does.
Structs§
- Api
- Function pointers into the game’s Il2Cpp runtime.
- Class
- A managed class (
Il2CppClass*). - Field
- A managed field (
FieldInfo*). - GcHandle
- Keeps an object alive and findable after the garbage collector moves or would free it. The collector doesn’t scan Rust memory, so objects kept between calls (in statics or closures) need one. Freed on drop.
- Generic
Hook Meta - Metadata about a generically-hooked method, built once at install time.
- Generic
Param - One parameter of a hooked method, mapped to its ABI slot at install time.
- Il2Cpp
- The game’s Il2Cpp runtime.
- Il2Cpp
String - A managed string (
Il2CppString*), possibly null. - Image
- A loaded assembly image, containing classes.
- Method
- A managed method (
MethodInfo*). - Method
Hook - An applied method hook. Dropping it leaves the hook in place.
- Object
- A managed object reference (
Il2CppObject*), possibly null. - Original
- Calls the original code of a hooked method.
- Parameter
- A method parameter’s name and managed type name, from
Method::parameters. - Signature
- The Rust types of a hook or call, checked against the method before use.
- Type
Info - The managed type of a parameter or return value.
- Value
Check - How a Rust type is checked against a managed parameter or return type.
- Void
Block Hook - A hook that conditionally blocks a void method. Created by
Method::hook_void_block.
Enums§
- Element
Type - The managed element type of a field, parameter, or return value.
- Error
- Param
Slot - How a method parameter maps to the System V AMD64 ABI.
Traits§
- Hook
Callback - A closure usable as a hook for a method with parameters
ArgsreturningR. Implemented forFn(args..., Original<(args...), R>) -> R. - Il2Cpp
Value - A Rust type that has the same representation as some managed types when passed to and from compiled Il2Cpp code.
- Method
Args - Argument tuples for
Method::call.
Functions§
- find_
game_ assembly - The game binary in a game folder, whichever platform the game was built for.
- is_
game_ assembly - Whether
pathis a game binary:GameAssembly.so,GameAssembly.dllor any otherGameAssembly.*. Both/and\\separate directories, as Windows paths can show up on Linux under Wine. - loaded_
game_ assembly - The game binary mapped into this process, if any.
- on_init
- Runs
callbackonceil2cpp_inithas finished, or straight away if it already has. - read_
field ⚠ - Reads a managed object’s field at
offset(fromClass::field_offset). - write_
field ⚠ - Writes a managed object’s field at
offset. Only for fields that don’t hold references.
Type Aliases§
- Method
Info - The hidden last argument of every Il2Cpp method (
const MethodInfo*). - Ptr
- Result