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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//! Reads and writes of game memory at byte offsets, for structs the API does
//! not wrap: a field at an offset from [`Load::field_offset`] or
//! [`Load::constant`], inside a context or component a hook was given.
//!
//! Every read of game memory goes through [`read_at`], [`read_ptr_at`] or
//! [`read_bytes_at`]. A
//! read copies the value unaligned and makes no reference into game memory.
//! Its type must be valid for any bit pattern ([`AnyBitPattern`]), so a
//! `bool` or an enum, where most bit patterns are undefined behaviour, cannot
//! be read: read the integer and compare it.
//!
//! ```
//! use enshwick::raw::read_at;
//!
//! let state = [0u8, 1];
//! // SAFETY: `state` holds two readable bytes, and offset 1 is the second.
//! let open = unsafe { read_at::<u8>(state.as_ptr(), 1) } != 0;
//! assert!(open);
//! ```
//!
//! [`Load::field_offset`]: crate::Load::field_offset
//! [`Load::constant`]: crate::Load::constant
pub use ;
/// Reads a `T` at `offset` bytes past `base`.
///
/// # Safety
///
/// - `base + offset` is readable for `size_of::<T>()` bytes; no alignment is
/// needed.
/// - No other thread writes those bytes during the read.
///
/// # Examples
///
/// ```
/// use enshwick::raw::read_at;
///
/// let record = [0u8, 0, 0, 0, 0x2a, 0, 0, 0];
/// // SAFETY: the eight-byte array holds a u32 at offset 4.
/// let value: u32 = unsafe { read_at(record.as_ptr(), 4) };
/// assert_eq!(value, 42);
/// ```
///
/// A `bool` is refused at compile time, since a byte of 2 is no `bool`:
///
/// ```compile_fail
/// use enshwick::raw::read_at;
///
/// let state = [2u8];
/// // SAFETY: one readable byte.
/// let open: bool = unsafe { read_at(state.as_ptr(), 0) };
/// ```
pub unsafe
/// Reads a pointer to `T` at `offset` bytes past `base`, keeping the
/// provenance the game stored with it.
///
/// # Safety
///
/// - `base + offset` is readable for `size_of::<usize>()` bytes; no
/// alignment is needed.
/// - No other thread writes those bytes during the read.
///
/// # Examples
///
/// ```
/// use enshwick::raw::read_ptr_at;
///
/// let value = 7u32;
/// let slot = [&raw const value];
/// // SAFETY: `slot` holds one pointer at offset 0.
/// let found = unsafe { read_ptr_at::<u32>(slot.as_ptr().cast(), 0) };
/// assert_eq!(found.cast_const(), &raw const value);
/// ```
pub unsafe
/// Writes `value` at `offset` bytes past `base`.
///
/// # Safety
///
/// - `base + offset` is writable for `size_of::<T>()` bytes; no alignment is
/// needed.
/// - Nothing else reads or writes those bytes during the write.
/// - The game expects a `T` there, so its own readers see a valid value.
///
/// # Examples
///
/// ```
/// use enshwick::raw::{read_at, write_at};
///
/// let mut record = [0u8; 8];
/// // SAFETY: the eight-byte array takes a u16 at offset 3.
/// unsafe { write_at(record.as_mut_ptr(), 3, 0x0102_u16) };
/// // SAFETY: the same array, now holding that u16 at offset 3.
/// assert_eq!(unsafe { read_at::<u16>(record.as_ptr(), 3) }, 0x0102);
/// ```
pub unsafe
/// Writes the pointer `value` at `offset` bytes past `base`.
///
/// # Safety
///
/// - `base + offset` is writable for `size_of::<usize>()` bytes; no
/// alignment is needed.
/// - Nothing else reads or writes those bytes during the write.
///
/// # Examples
///
/// ```
/// use enshwick::raw::{read_ptr_at, write_ptr_at};
///
/// let mut value = 7u32;
/// let mut slot = [core::ptr::null_mut::<u32>()];
/// // SAFETY: `slot` takes one pointer at offset 0.
/// unsafe { write_ptr_at(slot.as_mut_ptr().cast(), 0, &raw mut value) };
/// // SAFETY: `slot` holds that pointer at offset 0.
/// assert_eq!(unsafe { read_ptr_at::<u32>(slot.as_ptr().cast(), 0) }, &raw mut value);
/// ```
pub unsafe
/// Loads the `u32` at `offset` bytes past `base` as one atomic read, for a
/// word another game thread may write meanwhile; `None` when it is not
/// aligned to four bytes, where no atomic read is possible.
///
/// # Safety
///
/// - `base + offset` is readable for four bytes.
/// - Any thread that writes those bytes meanwhile writes all four at once.
///
/// # Examples
///
/// ```
/// use enshwick::raw::read_u32_atomic_at;
///
/// let words = [7u32, 9];
/// // SAFETY: the array holds an aligned u32 at offset 4.
/// assert_eq!(unsafe { read_u32_atomic_at(words.as_ptr().cast(), 4) }, Some(9));
/// ```
pub unsafe
/// Copies `out.len()` bytes at `offset` bytes past `base` into `out`, for a
/// span of game memory such as a string or a payload; no reference into game
/// memory is made.
///
/// # Safety
///
/// - `base + offset` is readable for `out.len()` bytes.
/// - No other thread writes those bytes during the copy.
///
/// # Examples
///
/// ```
/// use enshwick::raw::read_bytes_at;
///
/// let record = *b"..name";
/// let mut name = [0u8; 4];
/// // SAFETY: the six-byte array holds four bytes at offset 2.
/// unsafe { read_bytes_at(record.as_ptr(), 2, &mut name) };
/// assert_eq!(&name, b"name");
/// ```
pub unsafe
/// Copies `bytes` to `offset` bytes past `base`, for a span of game memory
/// such as a string or a payload.
///
/// # Safety
///
/// - `base + offset` is writable for `bytes.len()` bytes.
/// - Nothing else reads or writes those bytes during the copy.
///
/// # Examples
///
/// ```
/// use enshwick::raw::write_bytes_at;
///
/// let mut record = [0u8; 6];
/// // SAFETY: the six-byte array takes four bytes at offset 2.
/// unsafe { write_bytes_at(record.as_mut_ptr(), 2, b"name") };
/// assert_eq!(&record, b"\0\0name");
/// ```
pub unsafe