pub struct Hook<F: FnPtr> { /* private fields */ }Expand description
A mod’s link for one game function: its detour and the slot through which it reaches the rest of the chain.
Declared with detour! or #[hook],
registered with
Load::hook.
§Examples
enshwick::detour! {
/// Counts upgrades.
static UPGRADE: unsafe extern "system" fn(ctx: *mut u64) = upgrade;
}
fn upgrade(next: enshwick::Next<unsafe extern "system" fn(*mut u64)>, ctx: *mut u64) {
// SAFETY: the original takes the same context.
unsafe { next.get()(ctx) }
}
assert_eq!(UPGRADE.name(), "UPGRADE");Implementations§
Source§impl<F: FnPtr> Hook<F>
impl<F: FnPtr> Hook<F>
Sourcepub const fn entry(&self) -> F
pub const fn entry(&self) -> F
The entry point the loader links into the chain: the detour with its panic guard, as the game calls it.
§Examples
enshwick::detour! {
static TICK: extern "system" fn() = tick;
}
fn tick(next: enshwick::Next<extern "system" fn()>) {
next.get()();
}
let entry: extern "system" fn() = TICK.entry();Sourcepub fn after_next<R>(&self, work: impl FnOnce() -> R) -> Option<R>
pub fn after_next<R>(&self, work: impl FnOnce() -> R) -> Option<R>
Runs work that a detour does after calling next, so that a panic
in it turns the mod off without running the rest of the chain again.
A panic in the body falls through to the rest of the chain, which is
right before next and wrong after it: the game’s function would run
twice. Work after next belongs in here. A panic in work reports a
fault, as a panic in the body does, and gives None; the detour then
returns what next gave. The guard costs nothing until a panic.
§Examples
use std::sync::atomic::{AtomicU32, Ordering};
static RAISED: AtomicU32 = AtomicU32::new(0);
enshwick::detour! {
static RAISE: extern "system" fn(level: u32) -> u32 = raise;
}
fn raise(next: enshwick::Next<extern "system" fn(u32) -> u32>, level: u32) -> u32 {
let raised = next.get()(level);
let _counted = RAISE.after_next(|| RAISED.fetch_add(raised - level, Ordering::Relaxed));
raised
}Trait Implementations§
Auto Trait Implementations§
impl<F> !Freeze for Hook<F>
impl<F> RefUnwindSafe for Hook<F>where
F: RefUnwindSafe,
impl<F> Send for Hook<F>
impl<F> Sync for Hook<F>
impl<F> Unpin for Hook<F>where
F: Unpin,
impl<F> UnsafeUnpin for Hook<F>where
F: UnsafeUnpin,
impl<F> UnwindSafe for Hook<F>where
F: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more