Skip to main content

dobby_rs_framework/
hooks.rs

1use crate::Result;
2use crate::hook_utils;
3use core::ffi::c_void;
4
5pub use crate::hook_utils::{StaticHook, TypedHookHandle};
6
7pub struct ReplaceHandle<F: Copy> {
8    handle: TypedHookHandle<F>,
9    original: F,
10}
11impl<F: Copy> ReplaceHandle<F> {
12    pub fn original(&self) -> F {
13        self.original
14    }
15    pub unsafe fn unreplace(self) -> Result<()> {
16        self.handle.unhook()
17    }
18}
19
20pub unsafe fn install<F: Copy>(target: F, detour: F) -> Result<TypedHookHandle<F>> {
21    hook_utils::install(target, detour)
22}
23pub unsafe fn install_with<F: Copy, B, A>(
24    target: F,
25    detour: F,
26    before: Option<B>,
27    after: Option<A>,
28) -> Result<TypedHookHandle<F>>
29where
30    B: Fn() + Send + Sync + 'static,
31    A: Fn() + Send + Sync + 'static,
32{
33    hook_utils::hook_fn_with(target, detour, before, after)
34}
35pub unsafe fn install_addr(
36    target: *mut c_void,
37    detour: *mut c_void,
38) -> Result<hook_utils::HookHandle> {
39    hook_utils::install_addr(target, detour)
40}
41pub unsafe fn replace<F: Copy>(target: F, replacement: F) -> Result<ReplaceHandle<F>> {
42    let h = install(target, replacement)?;
43    let o = h.original();
44    Ok(ReplaceHandle {
45        handle: h,
46        original: o,
47    })
48}