Skip to main content

Crate hao_il2cpp_runtime

Crate hao_il2cpp_runtime 

Source
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(())
});

Methods are called the same way, with a tuple of arguments checked against the signature. Outside a game, Il2Cpp::load and Il2Cpp::init_standalone run a game’s plain .NET code in an ordinary process:

use hao_il2cpp_runtime::{Il2Cpp, Il2CppString};

let il2cpp = Il2Cpp::load("Game/GameAssembly.so")?;
il2cpp.init_standalone("Game/Game_Data/il2cpp_data")?;

let concat = il2cpp
    .class("mscorlib", "System", "String")?
    .methods()
    .find(|m| m.signature() == "static System.String Concat(System.String, System.String)")
    .expect("String.Concat(string, string)");
let joined: Il2CppString = concat.call((il2cpp.string("Kia ")?, il2cpp.string("ora")?))?;
assert_eq!(joined.to_rust().as_deref(), Some("Kia ora"));

Modules§

element
Managed element types (Il2CppTypeEnum).
reflectreflect
Reflection roots for the reflect feature. See hao::reflect for 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.
GenericHookMetax86-64
Metadata about a generically-hooked method, built once at install time.
GenericParamx86-64
One parameter of a hooked method, mapped to its ABI slot at install time.
Il2Cpp
The game’s Il2Cpp runtime.
Il2CppString
A managed string (Il2CppString*), possibly null.
Image
A loaded assembly image, containing classes.
Method
A managed method (MethodInfo*).
MethodHook
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.
TypeInfo
The managed type of a parameter or return value.
ValueCheck
How a Rust type is checked against a managed parameter or return type.
VoidBlockHook
A hook that conditionally blocks a void method. Created by Method::hook_void_block.

Enums§

ElementType
The managed element type of a field, parameter, or return value.
Error
What went wrong loading the game binary, finding things in it, or calling and hooking its methods.
ParamSlotx86-64
How a method parameter maps to the System V AMD64 ABI.

Traits§

HookCallback
A closure usable as a hook for a method with parameters Args returning R. Implemented for Fn(args..., Original<(args...), R>) -> R.
Il2CppValue
A Rust type that has the same representation as some managed types when passed to and from compiled Il2Cpp code.
MethodArgs
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 path is a game binary: GameAssembly.so, GameAssembly.dll or any other GameAssembly.*. Both / and \\ separate directories, as Windows paths can show up on Linux under Wine.
loaded_game_assemblyUnix
The game binary mapped into this process, if any.
on_initUnix
Runs callback once il2cpp_init has finished, or straight away if it already has.
read_field⚠
Reads a managed object’s field at offset (from Class::field_offset).
write_field⚠
Writes a managed object’s field at offset. Only for fields that don’t hold references.

Type Aliases§

MethodInfo
The hidden last argument of every Il2Cpp method (const MethodInfo*).
Ptr
An untyped pointer into the Il2Cpp runtime: a domain, image, class, method, field, type or object, depending on the function.
Result
A result whose error is Error.