Skip to main content

HookManager

Struct HookManager 

pub struct HookManager { /* private fields */ }
Expand description

Manager for registering and executing hooks.

The HookManager maintains a collection of hooks sorted by priority (highest first). When a method call is intercepted, the manager finds the first matching hook and executes its handlers.

§Hook Resolution

When looking for a matching hook:

  1. Hooks are checked in priority order (highest first)
  2. The first hook whose matchers all match is selected
  3. Only one hook is executed per method call

§Examples

use dotscope::emulation::{HookManager, Hook, HookPriority, PreHookResult, EmValue};

let mut manager = HookManager::new();

// Register hooks (order doesn't matter, they're sorted by priority)
manager.register(
    Hook::new("low-priority")
        .with_priority(HookPriority::LOW)
        .match_method_name("Decrypt")
        .pre(|ctx, thread| PreHookResult::Continue)
);

manager.register(
    Hook::new("high-priority")
        .with_priority(HookPriority::HIGH)
        .match_method_name("Decrypt")
        .pre(|ctx, thread| PreHookResult::Bypass(Some(EmValue::I32(42))))
);

// When "Decrypt" is called, "high-priority" matches first

Implementations§

§

impl HookManager

pub fn new() -> Self

Creates a new, empty hook manager.

pub fn register(&mut self, hook: Hook)

Registers a hook.

Hooks are automatically sorted by priority after insertion. Higher priority hooks are checked first when matching.

§Arguments
  • hook - The hook to register
§Examples
let mut manager = HookManager::new();
manager.register(Hook::new("my-hook").match_method_name("Test"));

pub fn find_matching<'a>( &'a self, context: &HookContext<'_>, thread: &EmulationThread, ) -> Option<&'a Hook>

Finds the first matching hook for the given context.

Hooks are checked in priority order (highest first). The first hook whose matchers all match is returned.

§Arguments
  • context - The hook context containing method call information
  • thread - The emulation thread for runtime data inspection
§Returns

The first matching hook, or None if no hook matches.

pub fn execute<F>( &self, context: &HookContext<'_>, thread: &mut EmulationThread, execute_original: F, ) -> Result<HookOutcome>
where F: FnOnce(&mut EmulationThread) -> Option<EmValue>,

Executes a method call through the hook system.

This is the primary entry point for hook execution. It handles the complete hook lifecycle:

  1. Find a matching hook (by priority order)
  2. Execute the pre-hook
  3. If pre-hook returns Continue, execute the original method via callback
  4. Execute the post-hook on the result
  5. Return the final outcome
§Arguments
  • context - The hook context containing method call information
  • thread - The emulation thread
  • execute_original - Callback to execute the original method. Only called if a hook matches and returns Continue.
§Returns
  • Ok(HookOutcome::NoMatch) - No hook matched; caller should execute normally
  • Ok(HookOutcome::Handled(value)) - Hook handled the call; use this result
  • Err(...) - Hook execution failed
§Errors

Returns an error if a pre-hook or post-hook returns Error.

§Examples
use dotscope::emulation::{HookContext, HookOutcome};

let context = HookContext::new(
    method_token,
    "System", "String", "Concat",
);

let outcome = manager.execute(&context, &mut thread, |thread| {
    // Execute the original method
    Some(EmValue::String(...))
})?;

match outcome {
    HookOutcome::NoMatch => { /* execute normally */ }
    HookOutcome::Handled(value) => { /* use value */ }
}

pub fn len(&self) -> usize

Returns the number of registered hooks.

pub fn is_empty(&self) -> bool

Returns true if no hooks are registered.

pub fn iter(&self) -> impl Iterator<Item = &Hook>

Returns an iterator over all registered hooks.

Hooks are yielded in priority order (highest first).

Trait Implementations§

§

impl Debug for HookManager

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
§

impl Default for HookManager

§

fn default() -> HookManager

Returns the “default value” for a type. Read more

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, A> IntoAst<A> for T
where T: Into<A>, A: Ast,

Source§

fn into_ast(self, _a: &A) -> A

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.