anvil/fnptr.rs
1use std::ffi::c_void;
2use std::marker::PhantomData;
3
4use crate::sys::AnvilSymbol;
5
6/// A function pointer type the game can call: `unsafe extern "C" fn(…) -> R`
7/// with up to 12 arguments, as `anvil gen --lang rust` writes them
8/// (`sym::<Name>Fn`).
9///
10/// Only `unsafe` function pointers implement it, so calling a hook's
11/// [`next`](crate::FunctionHook::next) or a game function is always an
12/// `unsafe` call. Variadic functions are not covered.
13///
14/// # Safety
15/// An implementation must be a function pointer type, so that the raw
16/// conversions keep its value.
17pub unsafe trait FnPtr: Copy + Send + Sync + 'static {
18 /// The pointer as Anvil takes it.
19 fn into_raw(self) -> *mut c_void;
20
21 /// The function pointer back from Anvil's pointer.
22 ///
23 /// # Safety
24 /// `raw` is not null and points to a function of this type.
25 unsafe fn from_raw(raw: *mut c_void) -> Self;
26}
27
28macro_rules! fn_ptr {
29 ($($arg:ident),*) => {
30 // SAFETY: a function pointer type.
31 unsafe impl<R: 'static, $($arg: 'static),*> FnPtr for unsafe extern "C" fn($($arg),*) -> R {
32 fn into_raw(self) -> *mut c_void {
33 self as *const () as *mut c_void
34 }
35
36 unsafe fn from_raw(raw: *mut c_void) -> Self {
37 // SAFETY: the caller's contract; both are one pointer wide.
38 unsafe { std::mem::transmute_copy::<*mut c_void, Self>(&raw) }
39 }
40 }
41 };
42}
43
44fn_ptr!();
45fn_ptr!(A);
46fn_ptr!(A, B);
47fn_ptr!(A, B, C);
48fn_ptr!(A, B, C, D);
49fn_ptr!(A, B, C, D, E);
50fn_ptr!(A, B, C, D, E, F);
51fn_ptr!(A, B, C, D, E, F, G);
52fn_ptr!(A, B, C, D, E, F, G, H);
53fn_ptr!(A, B, C, D, E, F, G, H, I);
54fn_ptr!(A, B, C, D, E, F, G, H, I, J);
55fn_ptr!(A, B, C, D, E, F, G, H, I, J, K);
56fn_ptr!(A, B, C, D, E, F, G, H, I, J, K, L);
57
58/// A function symbol together with its declared type: what
59/// `anvil gen --lang rust-sdk` writes as `sym::typed::<NAME>` for each
60/// function symbol of `symbols.toml`, from the same signature as
61/// `sym::<Name>Fn`. [`Host::hook_symbol`](crate::Host::hook_symbol) takes it
62/// with a hook and a detour of that type, so a detour of another shape does
63/// not compile and the call needs no `unsafe`.
64///
65/// ```no_run
66/// use std::ffi::c_void;
67/// use anvil::{FnSymbol, FunctionHook, Host};
68///
69/// mod sym {
70/// // What anvil gen --lang rust-sdk writes for `void* (void* session)`.
71/// pub type SpeedObjectFn = unsafe extern "C" fn(session: *mut std::ffi::c_void) -> *mut std::ffi::c_void;
72/// pub const SPEED_OBJECT: u32 = 0;
73/// pub mod typed {
74/// // SAFETY: the type of the signature above.
75/// pub const SPEED_OBJECT: anvil::FnSymbol<super::SpeedObjectFn> =
76/// unsafe { anvil::FnSymbol::new(super::SPEED_OBJECT) };
77/// }
78/// }
79///
80/// static HOOK: FunctionHook<sym::SpeedObjectFn> = FunctionHook::new();
81///
82/// unsafe extern "C" fn detour(session: *mut c_void) -> *mut c_void {
83/// // SAFETY: the game's function, with the game's argument.
84/// HOOK.next().map_or(std::ptr::null_mut(), |next| unsafe { next(session) })
85/// }
86///
87/// fn load(host: &Host) -> anvil::Result<()> {
88/// host.hook_symbol(&HOOK, sym::typed::SPEED_OBJECT, detour)
89/// }
90/// ```
91///
92/// A detour of another type is a compile error:
93///
94/// ```compile_fail
95/// use std::ffi::c_void;
96/// use anvil::{FnSymbol, FunctionHook, Host};
97///
98/// type SpeedObjectFn = unsafe extern "C" fn(session: *mut c_void) -> *mut c_void;
99/// // SAFETY: the type of the symbol's signature.
100/// const SPEED_OBJECT: FnSymbol<SpeedObjectFn> = unsafe { FnSymbol::new(0) };
101/// static HOOK: FunctionHook<SpeedObjectFn> = FunctionHook::new();
102///
103/// unsafe extern "C" fn detour(_speed: i32) -> i32 {
104/// 0
105/// }
106///
107/// fn load(host: &Host) -> anvil::Result<()> {
108/// host.hook_symbol(&HOOK, SPEED_OBJECT, detour)
109/// }
110/// ```
111pub struct FnSymbol<F: FnPtr> {
112 id: AnvilSymbol,
113 function: PhantomData<F>,
114}
115
116impl<F: FnPtr> FnSymbol<F> {
117 /// The symbol `id` as a function of type `F`.
118 ///
119 /// # Safety
120 /// `id` is a function symbol of this plugin's `symbols.toml` whose declared
121 /// signature is exactly `F` (as `anvil gen` translates it): the game calls
122 /// a detour of `F` with the arguments of that signature.
123 pub const unsafe fn new(id: AnvilSymbol) -> Self {
124 FnSymbol {
125 id,
126 function: PhantomData,
127 }
128 }
129
130 /// The symbol's id, `sym::<NAME>`.
131 pub const fn id(self) -> AnvilSymbol {
132 self.id
133 }
134}
135
136impl<F: FnPtr> Clone for FnSymbol<F> {
137 fn clone(&self) -> Self {
138 *self
139 }
140}
141
142impl<F: FnPtr> Copy for FnSymbol<F> {}
143
144impl<F: FnPtr> std::fmt::Debug for FnSymbol<F> {
145 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
146 f.debug_tuple("FnSymbol").field(&self.id).finish()
147 }
148}