Skip to main content

Method

Struct Method 

Source
pub struct Method { /* private fields */ }
Expand description

A managed method (MethodInfo*).

Implementations§

Source§

impl Method

Source

pub fn is_instance(&self) -> bool

Whether the method takes an instance (this) as its first argument, i.e. isn’t static.

Source

pub fn parameter_count(&self) -> u32

The number of declared parameters, not counting this or the trailing MethodInfo*.

Source

pub fn return_type_name(&self) -> String

The managed name of the return type, e.g. System.Int32.

Source

pub fn parameter_type_name(&self, index: u32) -> String

The managed type name of parameter index, e.g. System.Single.

Source

pub fn parameter_name(&self, index: u32) -> String

The declared name of parameter index, e.g. amount. Release builds often strip these to arg0, arg1, …

Source

pub fn parameters(&self) -> impl Iterator<Item = Parameter> + '_

The parameters, each with its name and type name.

Source

pub fn signature(&self) -> String

The managed signature, e.g. System.Int32 Heal(System.Single, System.Boolean), built from Method::return_type_name, Method::name and Method::parameters.

Source

pub fn check_types<Args: MethodArgs, R: Il2CppValue>(&self) -> Result<()>

Checks argument types Args (a tuple, with the instance first for instance methods) and return type R against the method’s signature, as Method::hook and Method::call do.

Source

pub fn hook<Args, R, F: HookCallback<Args, R>>( &self, callback: F, ) -> Result<MethodHook>

Redirects calls of this method to callback, after checking its types against the method’s signature. The callback gets the arguments (the instance first for instance methods) and an Original to call the original code with.

let heal = il2cpp.class("Assembly-CSharp", "Game", "PlayerHealth")?.method("Heal", 2)?;
heal.hook(|this: Object, amount: f32, overheal: bool, original: Original<_, i32>| {
    original.call((this, amount * 2.0, overheal))
})?;
Source

pub fn call<Args: MethodArgs, R: Il2CppValue>(&self, args: Args) -> Result<R>

Calls the method’s compiled code after checking the argument and return types. args is a tuple, with the instance first for instance methods.

Managed exceptions thrown by the method unwind through the caller.

Source

pub unsafe fn call_unchecked<Args: MethodArgs, R: Il2CppValue>( &self, args: Args, ) -> R

Method::call without the type check, for calls made often after Method::check_types has passed once.

§Safety

Args and R must pass Method::check_types for this method.

Source

pub fn hook_void_block(&self) -> Result<VoidBlockHook>

Available on x86-64 only.

Hooks a void method with a conditional block: when VoidBlockHook::set_blocked(true) is active, calls to the method return immediately without running the original code.

Works for any void method regardless of its parameter types — the trampoline preserves all argument registers and either tail-calls the original or returns.

let damage = il2cpp
    .class("Assembly-CSharp", "Actors", "PlayerHealth")?
    .method("DamagePlayer", 3)?;
let hook = damage.hook_void_block()?;
hook.set_blocked(true); // godmode — DamagePlayer is now a no-op
Source§

impl Method

Source

pub fn hook_generic( &self, callback: impl Fn(usize, &GenericHookMeta, &SavedRegs) -> DispatchResult + Send + Sync + 'static, ) -> Result<MethodHook>

Available on x86-64 only.

Hooks this method with a generic callback that receives the saved argument registers and metadata about each parameter’s type and ABI slot. The callback inspects or modifies arguments through hooking::SavedRegs and returns a hooking::DispatchResult to pass through to the original or override the return value.

Unlike Method::hook, the callback doesn’t need to declare the method’s parameter types at compile time — they are mapped to ABI registers at install time using Il2Cpp metadata.

let heal = il2cpp
    .class("Assembly-CSharp", "Game", "PlayerHealth")?
    .method("Heal", 2)?;
heal.hook_generic(|original, meta, regs| {
    // Read the second declared parameter (index 1 if instance method,
    // since index 0 is `this`).
    let amount = regs.read_int(1);
    // Double it and pass through to the original.
    regs.write_int(1, amount * 2);
    hooking::DispatchResult::Passthrough
})?;
Source§

impl Method

Source

pub fn as_ptr(&self) -> MethodInfo

The MethodInfo*, passed as the last argument when calling the method.

Source

pub fn name(&self) -> &'static str

The method’s name, e.g. Heal, .ctor or get_Health.

Source

pub fn token(&self) -> u32

This method’s metadata token: the MethodDef token (0x06-prefixed) that hao-il2cpp emits as a proxy’s identity and Binary::method_pointer maps to a static code address. Resolve a token back to a live method with Il2Cpp::method.

Source

pub fn code(&self) -> *mut c_void

The method’s compiled code. Call it with the instance first (for instance methods), then the arguments, then Method::as_ptr.

Source

pub fn runtime_invoke(&self, this: Object, args: &mut [Ptr]) -> Result<Object>

Calls the method through il2cpp_runtime_invoke, which catches managed exceptions. Each element of args is a pointer to the argument value (for value types) or the object pointer itself (for reference types).

Source

pub unsafe fn hook_raw(&self, detour: *mut u8) -> Result<MethodHook>

Redirects every call of the method to detour, which has the method’s signature as described on Method::code. Inside the detour, original! gets a function that calls the original code. Method::hook is the safe, typed version of this.

The hook stays in place when the returned value is dropped.

§Safety

detour must be an extern "C" function with exactly the method’s signature.

Trait Implementations§

Source§

impl Clone for Method

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Method

Source§

impl Send for Method

Source§

impl Sync for Method

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more