dobby_rs_framework/hook_utils/
handle.rs1use super::registry;
2use crate::{Result, destroy};
3use core::ffi::c_void;
4use core::marker::PhantomData;
5use log::info;
6
7pub struct HookHandle {
8 pub(crate) target: usize,
9 pub(crate) detour: usize,
10 pub(crate) original: usize,
11}
12
13impl HookHandle {
14 pub fn target_ptr(&self) -> *mut c_void {
15 self.target as *mut c_void
16 }
17 pub fn detour_ptr(&self) -> *mut c_void {
18 self.detour as *mut c_void
19 }
20 pub fn original_ptr(&self) -> *mut c_void {
21 self.original as *mut c_void
22 }
23 pub unsafe fn original<T: Copy>(&self) -> T {
24 debug_assert_eq!(core::mem::size_of::<T>(), core::mem::size_of::<usize>());
25 core::mem::transmute_copy(&self.original)
26 }
27 pub unsafe fn unhook(self) -> Result<()> {
28 info!(
29 "uninstalling hook target={:p} detour={:p}",
30 self.target_ptr(),
31 self.detour_ptr()
32 );
33 destroy(self.target_ptr())?;
34 registry::remove(self.target, self.detour);
35 Ok(())
36 }
37}
38
39pub struct TypedHookHandle<F> {
40 pub(crate) inner: HookHandle,
41 pub(crate) _marker: PhantomData<F>,
42}
43impl<F: Copy> TypedHookHandle<F> {
44 pub fn original(&self) -> F {
45 unsafe { self.inner.original() }
46 }
47 pub unsafe fn unhook(self) -> Result<()> {
48 self.inner.unhook()
49 }
50}