xur_

> module

std::stub::il2cpp

interact with a live Unity Il2Cpp game

use std::stub::il2cpp; feature il2cpp-runtime native only

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

# loaded() -> Result<Runtime, str>

The Il2Cpp runtime handle, attaching the calling thread to Il2Cpp the first time. Fails if Il2Cpp hasn't initialised yet.

# null() -> Object

A null managed object reference, e.g. the object of a static method's invoke().

> types

┌ type Class
# class.field(name: str) -> Result<Field, str>

Look up a field by name.

# class.fields() -> list<Field>

All fields on the class.

# class.method(name: str, param_count: i64) -> Result<Method, str>

Look up a method by name and parameter count.

# class.methods() -> list<Method>

All methods on the class.

# class.name() -> str

The class name.

# class.namespace() -> str

The class namespace.

# class.new_object() -> Object

Allocate a new managed object of this class (does not call a constructor).

┌ enum Error

A recoverable il2cpp error. Match it as `Err(il2cpp::Error::NotLoaded)`.

Error::NotLoadedil2cpp is not loaded yet.
Error::HookFailed(str)A hook operation failed, with its message.
┌ type Field
# field.is_static() -> bool

Whether the field is static.

# field.name() -> str

The field name.

# field.offset() -> i64

The field's byte offset in the object layout.

# field.read(object: Object) -> any

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() -> any

Read this static field's value.

# field.type_name() -> str

The field's type name.

# field.write(object: Object, value: any)

Write value to this instance field on object.

# field.write_static(value: any)

Write a value to this static field.

┌ type HookContext

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.

# hook_context.arg(index_or_name: i64 | str) -> any

An argument, marshalled to a Xur value, by index or parameter name. Raises for an unknown index or name.

# hook_context.args() -> list<any>

All arguments, in order.

# hook_context.call() -> any

Call the original method with the (possibly modified) arguments and return its result. Raises if called more than once per hook invocation.

# hook_context.set_arg(index_or_name: i64 | str, value: any)

Replace an argument. Changes are written back to the saved registers before the original runs (on call() or after the callback returns).

# hook_context.signature() -> str

The hooked method's signature string.

┌ type Image
# image.class_count() -> i64

How many classes the image has, without listing them.

# image.classes() -> list<Class>

All classes in this assembly image.

# image.name() -> str

The assembly image name.

┌ type Method
# method.code_address() -> i64

The native code address of the method.

# 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).

# method.invoke(object?: Object, args?: any) -> any

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.

# method.invoke_on_main(object?: Object, args?: any) -> any

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.

# method.name() -> str

The method name.

# method.signature() -> str

The full method signature string.

┌ type Object
# object.address() -> i64

The raw memory address of the managed object.

# object.class() -> Class

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() -> bool

Whether this is a null object reference.

┌ type Runtime
# runtime.assemblies() -> Result<list<Image>, Error>

All loaded assemblies as Image objects. Fails with Error::NotLoaded if Il2Cpp isn't loaded.

# runtime.class(assembly: str, namespace: str, name: str) -> Result<Class, str>

Look up a managed class by assembly name, namespace, and class name.

# runtime.string(value: str) -> Object

Create a managed System.String from a Xur string. Raises if Il2Cpp isn't loaded.

┌ type VoidBlockHook
# void_block_hook.is_blocked() -> bool

Whether the hook is currently blocking.

# void_block_hook.set_blocked(blocked: bool)

Enable or disable blocking.

> 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 {
    #[default]
    fn default() -> HookState {
        HookState { enabled: cell::new(false) }
    }
}

#[il2cpp_proxy]
impl HealthProxy {
    #[il2cpp_hook(method="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;

#[il2cpp_proxy(asm="Assembly-CSharp", ns="", class="PlayerHealth")]
struct HealthProxy {
    #[il2cpp_field="hp"]
    hp: il2cpp::Field
    #[il2cpp_field="maxHp"]
    max_hp: il2cpp::Field
    #[il2cpp_method(name="DamagePlayer", params=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())
    });
}