Skip to main content

FunctionHook

Struct FunctionHook 

Source
pub struct FunctionHook<F: FnPtr> { /* private fields */ }
Expand description

A function hook, call redirect or import replacement, and the rest of its chain. It lives in a static, so the detour (a plain function the game calls) reaches next, and Anvil can write next into it before the change is enabled (on a hook reused after remove, only on an Anvil newer than 1.25.0):

ⓘ
static SPEED_OBJECT: anvil::FunctionHook<sym::SpeedObjectFn> = anvil::FunctionHook::new();

unsafe extern "C" fn on_speed_object(session: *mut c_void) -> *mut c_void {
    let Some(next) = SPEED_OBJECT.next() else { return std::ptr::null_mut() };
    // SAFETY: the game's own function, called as it declares.
    unsafe { next(session) }
}

A detour is called by the game directly, with no adapter between: it must not panic (a panic out of an extern "C" function aborts the process). Wrap fallible work in catch_panic.

Implementations§

Source§

impl<F: FnPtr> FunctionHook<F>

Source

pub const fn new() -> Self

An unused hook.

Source

pub fn next(&self) -> Option<F>

The rest of the chain: the next plugin’s detour or the game’s code; None before Anvil wrote it. It stays valid after the change is removed, for a thread still inside the detour.

Source

pub fn is_active(&self) -> bool

Whether the hook holds a change (or another thread is installing one).

Source

pub fn remove(&self) -> Result<()>

Removes the change; nothing when there is none. The hook can then take a new change; until it does, next keeps the removed change’s chain for a thread still inside the detour.

Trait Implementations§

Source§

impl<F: FnPtr> Debug for FunctionHook<F>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<F: FnPtr> Default for FunctionHook<F>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<F> !Freeze for FunctionHook<F>

§

impl<F> Forget for FunctionHook<F>

§

impl<F> Move for FunctionHook<F>

§

impl<F> RefUnwindSafe for FunctionHook<F>

§

impl<F> Send for FunctionHook<F>
where PhantomData<F>: Send,

§

impl<F> Sync for FunctionHook<F>
where PhantomData<F>: Sync,

§

impl<F> Unpin for FunctionHook<F>
where PhantomData<F>: Unpin,

§

impl<F> UnsafeUnpin for FunctionHook<F>

§

impl<F> UnwindSafe for FunctionHook<F>

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, 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.