Function add_hook

Source
pub unsafe fn add_hook(
    original: *const (),
    replacement: *const (),
) -> Result<(), Error>
Expand description

Sets up a trampoline to replace the original function with a new one.

§Safety

This is an unsafe function as it manipulates raw pointers and enables function detouring.

  • Both the original and replacement pointers must point to valid functions with matching signatures.
  • Undefined behavior may occur if the functions have different calling conventions or incompatible arguments.

§Errors

This function returns a retour::Error if:

  • The detour cannot be created due to invalid function pointers.
  • Enabling the hook fails.

§Example

use retour::Error;
use commonlibsse_ng::skse::trampoline::add_hook;

fn add5(val: i32) -> i32 {
    val + 5
}

fn add10(val: i32) -> i32 {
    val + 10
}

let original = add5 as *const ();
let replacement = add10 as *const ();

// Verify the original behavior
assert_eq!(add5(5), 10);

// Replace the original function with the new one
unsafe { add_hook(original, replacement) }.unwrap();
assert_eq!(add5(5), 15);