pub struct Method { /* private fields */ }Expand description
A managed method (MethodInfo*).
Implementations§
Source§impl Method
impl Method
pub fn is_instance(&self) -> bool
pub fn parameter_count(&self) -> u32
Sourcepub fn return_type_name(&self) -> String
pub fn return_type_name(&self) -> String
The managed name of the return type, e.g. System.Int32.
Sourcepub fn parameter_type_name(&self, index: u32) -> String
pub fn parameter_type_name(&self, index: u32) -> String
The managed type name of parameter index, e.g. System.Single.
Sourcepub fn parameter_name(&self, index: u32) -> String
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, …
Sourcepub fn parameters(&self) -> impl Iterator<Item = Parameter> + '_
pub fn parameters(&self) -> impl Iterator<Item = Parameter> + '_
The parameters, each with its name and type name.
Sourcepub fn signature(&self) -> String
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.
Sourcepub fn check_types<Args: MethodArgs, R: Il2CppValue>(&self) -> Result<()>
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.
Sourcepub fn hook<Args, R, F: HookCallback<Args, R>>(
&self,
callback: F,
) -> Result<MethodHook>
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))
})?;Sourcepub fn call<Args: MethodArgs, R: Il2CppValue>(&self, args: Args) -> Result<R>
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.
Sourcepub unsafe fn call_unchecked<Args: MethodArgs, R: Il2CppValue>(
&self,
args: Args,
) -> R
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.
Sourcepub fn hook_void_block(&self) -> Result<VoidBlockHook>
pub fn hook_void_block(&self) -> Result<VoidBlockHook>
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-opSource§impl Method
impl Method
Sourcepub fn hook_generic(
&self,
callback: impl Fn(usize, &GenericHookMeta, &mut SavedRegs) -> HookAction + Send + Sync + 'static,
) -> Result<MethodHook>
pub fn hook_generic( &self, callback: impl Fn(usize, &GenericHookMeta, &mut SavedRegs) -> HookAction + Send + Sync + 'static, ) -> Result<MethodHook>
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::HookAction 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::HookAction::Passthrough
})?;Source§impl Method
impl Method
Sourcepub fn as_ptr(&self) -> MethodInfo
pub fn as_ptr(&self) -> MethodInfo
The MethodInfo*, passed as the last argument when calling the method.
pub fn name(&self) -> &'static str
Sourcepub fn token(&self) -> u32
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.
Sourcepub fn code(&self) -> *mut c_void
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.
Sourcepub fn runtime_invoke(&self, this: Object, args: &mut [Ptr]) -> Result<Object>
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).
Sourcepub unsafe fn hook_raw(&self, detour: *mut u8) -> Result<MethodHook>
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.