> module
std::stub::il2cpp
interact with a live Unity Il2Cpp game
std::stub::il2cpp provides runtime access to a Unity Il2Cpp game's types, fields, methods, and objects from inside the injected process. It is the primary module for writing game mods and trainers.
Fields can be read/written, methods can be invoked, and methods can be hooked with closures that intercept calls at runtime.
> functions
> types
Look up a method by name and parameter count.
A recoverable il2cpp error. Match it as `Err(il2cpp::Error::NotLoaded)`.
| Error::NotLoaded | il2cpp is not loaded yet. |
| Error::HookFailed(str) | A hook operation failed, with its message. |
field.is_static() -> boolWhether the field is static.
Read this instance field from object. The value's type follows the field's element type: a bool, an integer, a float, or an Object for references.
field.read_static() -> anyRead this static field's value.
field.write_static(value: any)Write a value to this static field.
Available inside a hook() callback:
The context object passed to a `method.hook()` callback in script. Created per-call. Arg values are cloned; the register pointers are only valid for the duration of the callback.
An argument, marshalled to a Xur value, by index or parameter name. Raises for an unknown index or name.
hook_context.call() -> anyCall the original method with the (possibly modified) arguments and return its result. Raises if called more than once per hook invocation.
Replace an argument. Changes are written back to the saved registers before the original runs (on call() or after the callback returns).
method.hook(callback: fn(HookContext) -> Option<any>)Hook this method (x86_64 only). The callback receives a HookContext with the call's arguments, and returns None to run the original or Some(value) to skip it and return value instead. For a void method, return Some(hooking::Skip()) to skip the original without a return value. Arguments changed with set_arg() are written back before the original runs. A callback error is logged and the original runs.
method.hook_void_block() -> Result<VoidBlockHook, Error>Install a simple block/unblock hook. While blocked, the method returns immediately without running. Useful for void methods you just want to stop. Fails with Error::HookFailed(message).
Call the method through il2cpp_runtime_invoke. object is the instance, or null() (or left out) for a static method. The remaining arguments are boxed and passed to the method; the result is unboxed by the return type.
Like invoke() but runs the call on Unity's main thread and blocks until it completes. Use this for methods that instantiate GameObjects or fire events that touch the Unity API, which is only safe from the main thread. The call runs during the next frame's update.
The object's runtime class. Useful for calling methods on objects whose type wasn't known at proxy time, such as generic collections. Raises for a null object.
object.is_null() -> boolWhether this is a null object reference.
All loaded assemblies as Image objects. Fails with Error::NotLoaded if Il2Cpp isn't loaded.
Look up a managed class by assembly name, namespace, and class name.
> il2cpp proxy macros
The #[il2cpp_proxy] attribute macro generates constructors for structs that map to IL2CPP classes, eliminating manual class/field/method lookup boilerplate.
#[il2cpp_proxy] on an impl block generates a setup_hooks() method. Functions annotated with #[il2cpp_hook(method="field")] are wired up as self.field.hook(Self::fn).
Extra parameters beyond ctx on a hook function become Option<Type> parameters on the generated setup_hooks(). The caller passes None to use Type::default(), or Some(value) to provide pre-configured state.
Hooks in the same impl block that share a param name and type share one instance automatically. This makes it easy to wire multiple hooks to the same state within a proxy.
For cross-class sharing (e.g. a NoMaxLevelState shared across weapon, tome, and utility proxies), the caller creates one instance and passes Some(state) to each proxy's setup_hooks().
The data type must have a static default() function (conventionally marked with #[default]) that returns an instance. Fields using cell::Cell share identity through Arc when captured, so both the hook closure and the returned record see the same mutable state.
struct HookState {
enabled: cell::Cell<bool>
}
impl HookState {
fn default() -> HookState {
HookState { enabled: cell::new(false) }
}
}
2
impl HealthProxy {
2"damage"
fn on_damage(ctx: il2cpp::HookContext, state: HookState) -> Option<hooking::Skip> {
if state.enabled.get() { Some(hooking::Skip()) } else { None }
}
}
// None = use HookState::default()
let hooks = proxy.setup_hooks(None);
hooks.state.enabled.set(true);
// Or provide pre-configured state:
let my_state = HookState { enabled: cell::new(true) };
let hooks = proxy.setup_hooks(Some(my_state));The macro lives in the std::stub::il2cpp module. Import it before use:
use std::stub::il2cpp::il2cpp_proxy;Or use the full path directly: #[std::stub::il2cpp::il2cpp_proxy(...)]
use std::stub::il2cpp::il2cpp_proxy;
2"Assembly-CSharp" "" "PlayerHealth"
struct HealthProxy {
2"hp"
hp: il2cpp::Field
2"maxHp"
max_hp: il2cpp::Field
2"DamagePlayer" 3
damage: il2cpp::Method
}
fn main() -> Result<()> {
let rt = il2cpp::loaded()?;
let proxy = HealthProxy::from_runtime(rt)?;
// proxy.hp, proxy.damage etc. are ready to use
Ok(())
}> examples
use std::stub::hooking;
use std::stub::il2cpp;
use std::log;
fn ctor() {
log::subscriber().to_file("/tmp/mod.log").init();
}
fn stub_main() {
let rt = il2cpp::loaded().unwrap();
let cls = rt.class("Assembly-CSharp", "", "PlayerHealth").unwrap();
let hp = cls.field("hp").unwrap();
let damage = cls.method("DamagePlayer", 3).unwrap();
// Godmode: intercept damage calls
damage.hook(|ctx| {
log::info("blocked damage");
Some(hooking::Skip())
});
}