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