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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//! The world: components, entities, daytime, game settings, weather and terrain.
use super::FakeHost;
use super::Installed;
use super::table;
use crate::abi::EntityPlace;
impl FakeHost {
/// The component of type `type_hash` that `entity` has, holding a copy
/// of `bytes` that the fake keeps, 16-byte aligned, for the rest of the
/// process; the mod's writes land in that copy.
///
/// # Examples
///
/// ```
/// let health = [80.0f32.to_le_bytes(), 100.0f32.to_le_bytes()].concat();
/// let loader = enshwick::test::FakeHost::new()
/// .component(4096, 0x027a_c564, &health)
/// .template(4096, [0x3a; 16])
/// .install();
/// # let _ = loader;
/// ```
#[must_use]
pub fn component(mut self, entity: u32, type_hash: u32, bytes: &[u8]) -> Self {
let mut words = vec![0u128; bytes.len().div_ceil(16).max(1)].into_boxed_slice();
for (word, chunk) in words.iter_mut().zip(bytes.chunks(16)) {
let mut le = [0u8; 16];
le[..chunk.len()].copy_from_slice(chunk);
*word = u128::from_le_bytes(le);
}
let data = Box::leak(words).as_mut_ptr();
self.components
.insert((entity, type_hash), (data.expose_provenance(), bytes.len()));
self
}
/// Refuses component writes of type `type_hash`, as the loader refuses
/// the copies the game keeps for clients.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().refuse_write(0x216e_b5f9).install();
/// # let _ = loader;
/// ```
#[must_use]
pub fn refuse_write(mut self, type_hash: u32) -> Self {
self.refused_writes.insert(type_hash);
self
}
/// An entity in the world with a component of type `type_hash`, at
/// `position` in metres, for [`World::entities`](crate::world::World::entities)
/// and [`World::near`](crate::world::World::near).
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new()
/// .placed(0x027a_c564, 4096, [1.0, 2.0, 3.0])
/// .install();
/// # let _ = loader;
/// ```
#[must_use]
pub fn placed(mut self, type_hash: u32, entity: u32, position: [f64; 3]) -> Self {
self.placed.push((
type_hash,
EntityPlace {
entity,
reserved: 0,
position: crate::ecs::fixed(position),
},
));
self
}
/// The template GUID of `entity`, in file order.
///
/// # Examples
///
/// See [`FakeHost::component`].
#[must_use]
pub fn template(mut self, entity: u32, guid: [u8; 16]) -> Self {
self.templates.insert(entity, guid);
self
}
}
#[expect(
clippy::unused_self,
reason = "the fake's state is process-wide; `self` proves it is installed"
)]
impl Installed {
/// Sets the clock the fake's `time_of_day` reports; until then the
/// clock is missing, as before a world runs.
///
/// # Examples
///
/// ```
/// use enshwick::abi::{DaytimeInfo, Header};
///
/// let loader = enshwick::test::FakeHost::new().install();
/// loader.set_daytime(DaytimeInfo {
/// header: Header::of::<DaytimeInfo>(),
/// time: 0,
/// day_start: 6 * 3_600_000_000_000,
/// day_end: 18 * 3_600_000_000_000,
/// day_length: 1_800_000_000_000,
/// night_length: 720_000_000_000,
/// speed: 1.0,
/// flags: 0,
/// });
/// assert_eq!(loader.daytime().map(|info| info.time), Some(0));
/// ```
pub fn set_daytime(&self, info: crate::abi::DaytimeInfo) {
table::with((), |s| s.daytime = Some(info));
}
/// The clock as the mod left it; a time it set moves the clock and its
/// day flag.
///
/// # Examples
///
/// See [`Installed::set_daytime`].
#[must_use]
pub fn daytime(&self) -> Option<crate::abi::DaytimeInfo> {
table::with(None, |s| s.daytime)
}
/// The mod's factor on the clock's speed, once it asked to change it.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// assert_eq!(loader.clock_speed(), None);
/// ```
#[must_use]
pub fn clock_speed(&self) -> Option<f32> {
table::with(None, |s| s.clock_speed)
}
/// Sets the game settings the fake reports, `keen::ecs::GameSettings`
/// as bytes of its reflected size, and their preset; until then no
/// world runs. A change the mod makes replaces them and makes the
/// preset custom (4).
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// loader.set_game_settings(vec![0; 144], 0);
/// assert_eq!(loader.game_settings().map(|(bytes, _)| bytes.len()), Some(144));
/// ```
pub fn set_game_settings(&self, bytes: Vec<u8>, preset: u32) {
table::with((), |s| s.game_settings = Some((bytes, preset)));
}
/// The game settings' bytes and preset as the mod left them.
///
/// # Examples
///
/// See [`Installed::set_game_settings`].
#[must_use]
pub fn game_settings(&self) -> Option<(Vec<u8>, u32)> {
table::with(None, |s| s.game_settings.clone())
}
/// The game settings' bytes the mod kept with the world, by offset:
/// `None` for a byte no kept field covers; empty before any is kept or
/// after the mod forgot them.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// assert!(loader.kept_game_settings().is_empty());
/// ```
#[must_use]
pub fn kept_game_settings(&self) -> Vec<Option<u8>> {
table::with(Vec::new(), |s| s.kept_settings.clone())
}
/// Sets the weather the fake reports: the weights everywhere (clear,
/// rain, snow, blizzard) and the fronts; until then no world runs.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// loader.set_weather([1.0, 0.0, 0.0, 0.0], &[]);
/// assert!(loader.forecasts().is_empty());
/// ```
pub fn set_weather(&self, weights: [f32; 4], fronts: &[crate::abi::WeatherFront]) {
table::with((), |s| s.weather = Some((weights, fronts.to_vec())));
}
/// The weather the mod started: effect (`u32::MAX` for the game's
/// choice) and duration in game nanoseconds.
///
/// # Examples
///
/// See [`Installed::set_weather`].
#[must_use]
pub fn forecasts(&self) -> Vec<(u32, i64)> {
table::with(Vec::new(), |s| s.forecasts.clone())
}
/// Sets the terrain materials the fake lists; until then no world runs.
/// Every edit the mod asks for is recorded and moves one cubic metre.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// loader.set_terrain(&[]);
/// assert!(loader.terrain_edits().is_empty());
/// ```
pub fn set_terrain(&self, materials: &[crate::abi::TerrainMaterialInfo]) {
table::with((), |s| s.terrain = Some(materials.to_vec()));
}
/// The terrain edits the mod asked for, in order.
///
/// # Examples
///
/// See [`Installed::set_terrain`].
#[must_use]
pub fn terrain_edits(&self) -> Vec<crate::abi::TerrainEdit> {
table::with(Vec::new(), |s| s.terrain_edits.clone())
}
/// The labels the mod set, as `(entity, text)`, in order.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// assert!(loader.labels().is_empty());
/// ```
#[must_use]
pub fn labels(&self) -> Vec<(u32, String)> {
table::with(Vec::new(), |s| s.labels.clone())
}
/// The entities the mod asked the world to destroy, in order.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// assert!(loader.destroyed().is_empty());
/// ```
#[must_use]
pub fn destroyed(&self) -> Vec<u32> {
table::with(Vec::new(), |s| s.destroyed.clone())
}
/// The components the mod asked to write, `(entity, type hash)`, in
/// order; the fake hands out the data [`FakeHost::component`] gave.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// assert!(loader.writes().is_empty());
/// ```
#[must_use]
pub fn writes(&self) -> Vec<(u32, u32)> {
table::with(Vec::new(), |s| s.writes.clone())
}
/// The bytes of the component [`FakeHost::component`] gave `entity`, as
/// the mod's writes left them.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().component(7, 1, &[4, 2]).install();
/// assert_eq!(loader.component_bytes(7, 1), Some(vec![4, 2]));
/// assert_eq!(loader.component_bytes(7, 2), None);
/// ```
#[must_use]
pub fn component_bytes(&self, entity: u32, type_hash: u32) -> Option<Vec<u8>> {
let (address, len) = table::with(None, |s| {
s.host.components.get(&(entity, type_hash)).copied()
})?;
let data: *const u8 = core::ptr::with_exposed_provenance(address);
// SAFETY: `component` leaked `len` initialised bytes at `address`,
// and no write of the mod's runs while the test reads.
Some(unsafe { core::slice::from_raw_parts(data, len) }.to_vec())
}
/// The game events the mod raised, `(type hash, bytes)`, in order.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// assert!(loader.emits().is_empty());
/// ```
#[must_use]
pub fn emits(&self) -> Vec<(u32, Vec<u8>)> {
table::with(Vec::new(), |s| s.emits.clone())
}
/// The teleports the mod asked for, in order; an entity the fake does
/// not know is refused.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// assert!(loader.teleports().is_empty());
/// ```
#[must_use]
pub fn teleports(&self) -> Vec<crate::abi::TeleportRequest> {
table::with(Vec::new(), |s| s.teleports.clone())
}
/// The spawns the mod asked for, with the entity ids the fake handed
/// out, in order.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// assert!(loader.spawns().is_empty());
/// ```
#[must_use]
pub fn spawns(&self) -> Vec<(u32, crate::abi::SpawnRequest)> {
table::with(Vec::new(), |s| s.spawns.clone())
}
/// How many saves the mod asked for.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// assert_eq!(loader.saves(), 0);
/// ```
#[must_use]
pub fn saves(&self) -> usize {
table::with(0, |s| s.saves)
}
}