mod common;
use core::ffi::c_void;
use dobby_rs_framework::hook_utils;
use dobby_rs_framework::prelude::*;
#[inline(never)]
fn detour_mul(x: i32) -> i32 {
let original = unsafe {
hook_utils::original::<fn(i32) -> i32>(detour_mul as *const () as *mut c_void)
.expect("original not found")
};
original(x) + 1
}
#[inline(never)]
fn replacement_sub(_x: i32) -> i32 {
123
}
#[inline(never)]
fn target_with_callbacks(x: i32) -> i32 {
x + 10
}
#[inline(never)]
fn detour_with_callbacks(x: i32) -> i32 {
hook_utils::call_before(detour_with_callbacks as *const () as *mut c_void);
let original = unsafe {
hook_utils::original::<fn(i32) -> i32>(detour_with_callbacks as *const () as *mut c_void)
.expect("original not found")
};
let out = original(x);
hook_utils::call_after(detour_with_callbacks as *const () as *mut c_void);
out
}
fn main() -> dobby_rs_framework::Result<()> {
common::init_example_logging();
let h1 = unsafe {
install(
common::target_mul as fn(i32) -> i32,
detour_mul as fn(i32) -> i32,
)?
};
println!("target_mul(3) after install = {}", common::target_mul(3));
unsafe { h1.unhook()? };
let h1_addr = unsafe {
install_addr(
common::target_mul as *const () as *mut c_void,
detour_mul as *const () as *mut c_void,
)?
};
println!(
"target_mul(3) after install_addr = {}",
common::target_mul(3)
);
unsafe { h1_addr.unhook()? };
let r = unsafe {
replace(
common::target_sub as fn(i32) -> i32,
replacement_sub as fn(i32) -> i32,
)?
};
println!("target_sub(10) after replace = {}", common::target_sub(10));
println!("original target_sub(10) = {}", (r.original())(10));
unsafe { r.unreplace()? };
let h2 = unsafe {
install_with(
target_with_callbacks as fn(i32) -> i32,
detour_with_callbacks as fn(i32) -> i32,
Some(|| log::info!("before (install_with)")),
Some(|| log::info!("after (install_with)")),
)?
};
println!(
"target_with_callbacks(1) after install_with = {}",
target_with_callbacks(1)
);
unsafe { h2.unhook()? };
Ok(())
}