hao-il2cpp-runtime
Hook and call into a Unity Il2Cpp game from inside its process, the way MelonLoader mods do.
A mod is a cdylib that gets loaded into the game. on_init runs once the game's Il2Cpp
runtime has started; from there, classes and methods are found by name through the game's own
il2cpp_* API, and methods are hooked with hooking.
use ;
// Runs `load` when the game loads the library.
static LOAD: extern "C" fn = load;
extern "C"
The closure's types are checked against the method's real signature when the hook is installed,
and a mismatch is an Error::Signature rather than a crash. Arguments and return values are
Rust primitives for the matching .NET ones, Object for references, Il2CppString for strings
and *mut c_void for pointers and ref parameters. Method::call calls a method the same way,
and Class::methods lists every overload with its Method::signature. Managed exceptions pass
through hooks.
A method's signature is also available in parts, not only as the string: Method::name,
is_instance, return_type_name, and parameters (each a Parameter with its name and
type_name, or parameter_type_name(i) / parameter_name(i)). signature is built from these.
Methods are usually found by name, but Il2Cpp::method(image, token) resolves one by the
metadata token hao-il2cpp emits — the identity carried by its generated
assemblies, and what Binary::method_pointer maps to a static address offline. It's the online
mirror, so a token read from those assemblies (or shown in ILSpy) calls live:
// `token` is the MethodDef token hao-il2cpp emits for the method.
let heal = il2cpp.method?;
let healed: i32 = heal.call?; // instance first, typed + checked
This crate is the engine-agnostic layer. hao-il2cpp-unity builds typed
wrappers for Unity on top of it (game objects, UI, scenes, logging) and has the example mods.
Examples
Both run a game's Il2Cpp runtime in a normal process, without Unity:
call_methods: lists a class's methods with their signatures, picks an overload and calls it.hook_methods: hooksString.Concatwith closures that change its arguments or replace its result, then removes the hooks.
Mods are loaded by adding LD_PRELOAD=/path/to/libmy_mod.so %command% to the game's Steam launch
options. Class, method and field names come from the game's metadata, which
hao-il2cpp's game_info example shows.
Managed structs are passed as #[repr(C)] Rust structs that implement Il2CppValue:
unsafe
Enums are passed as their underlying integer. The garbage collector doesn't scan Rust memory, so
objects kept between calls should be held through a GcHandle.
Method::hook_rawtakes anextern "C"detour instead, with Il2Cpp's hiddenMethodInfo*as the last parameter.original!gets the original function and must be the first thing the detour does: it reads the trampoline address from registers the hook sets.Class::fieldsandClass::fieldgive each field with itsname,type_name,offsetandis_static;Class::field_offsetwithread_field/write_fieldreads and writes instance fields of game objects.on_initalso runs straight away if the mod is injected after the game's Il2Cpp runtime has already started (it attaches the injecting thread first), not only when loaded before it.Il2Cpp::load+init_standalonerun a game's Il2Cpp runtime without Unity, which is how the tests work. Only code that doesn't call into Unity can run that way.
The game binary is found at runtime, whatever it's called (GameAssembly.so, GameAssembly.dll):
find_game_assembly(dir) in a game folder, loaded_game_assembly() in the running process.
Status: native Linux games with LD_PRELOAD so far. A Windows build of a game running under
Proton is detected and reported as unsupported for now: its GameAssembly.dll is loaded by
Wine rather than dlopen, and uses the Windows calling convention.
Logging
Errors, debug and trace messages (the game binary being found, il2cpp_init, hooks being
installed and removed, lookups) go through tracing. Nothing
is printed unless the mod installs a subscriber, e.g. with tracing-subscriber:
let filter = try_from_default_env.unwrap_or_else;
let _ = fmt.with_env_filter.with_writer.try_init;
Then RUST_LOG=hao_il2cpp_runtime=debug in the game's launch options shows what hao is doing.
Documentation
The API docs are on docs.rs: the crate page covers hooking and
calling, with Api for the raw il2cpp_*
exports. The reflect feature adds facet reflection roots, as in hao.
Tests
HAO_IL2CPP_GAME=/path/to/game cargo test -p hao-il2cpp-runtime loads the game's Il2Cpp runtime
into the test process, hooks String.Concat and Int32.Parse with raw and typed hooks, and checks
Unity struct and enum types. Without the variable the tests skip.