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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//! Content: items, archives, resources, texts and what the mod serves.
use crate::abi::Header;
use crate::abi::ItemData;
use crate::abi::ResourceKey;
use crate::abi::Status;
use super::FakeHost;
use super::Installed;
use super::table;
impl FakeHost {
/// The static data of item `item`, for `item_info`.
///
/// # Examples
///
/// ```
/// use enshwick::abi::ItemData;
///
/// let loader = enshwick::test::FakeHost::new().item(enshwick::ItemId::new(0x5e01), 1, ItemData::HAS_PIDE).install();
/// # let _ = loader;
/// ```
#[must_use]
pub fn item(mut self, item: crate::ItemId, max_stack: u16, flags: u32) -> Self {
self.items.insert(
item.get(),
ItemData {
header: Header::of::<ItemData>(),
id: item,
max_stack,
category: 0,
rarity: 0,
flags,
placed_entity: [0; 16],
},
);
self
}
/// Puts item `item`, set with [`FakeHost::item`], in the game's
/// `ItemCategory` numbered `category`.
///
/// # Examples
///
/// ```
/// let host = enshwick::test::FakeHost::new().item(enshwick::ItemId::new(0x5e01), 1, 0).item_category(enshwick::ItemId::new(0x5e01), 1);
/// let _loader = host.install();
/// ```
#[must_use]
pub fn item_category(mut self, item: crate::ItemId, category: u8) -> Self {
if let Some(data) = self.items.get_mut(&item.get()) {
data.category = category;
}
self
}
/// Makes item `item`, set with [`FakeHost::item`], one a player places
/// as the template `template`, by its GUID in file order.
///
/// # Examples
///
/// ```
/// let host = enshwick::test::FakeHost::new().item(enshwick::ItemId::new(0x5e01), 1, 0).item_places(enshwick::ItemId::new(0x5e01), [0x3a; 16]);
/// let _loader = host.install();
/// ```
#[must_use]
pub fn item_places(mut self, item: crate::ItemId, template: [u8; 16]) -> Self {
if let Some(data) = self.items.get_mut(&item.get()) {
data.placed_entity = template;
}
self
}
/// Plays a client that shows served UI images: `icons_ready` answers
/// [`Status::OK`].
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().icons_ready().install();
/// # let _ = loader;
/// ```
#[must_use]
pub fn icons_ready(mut self) -> Self {
self.icons_ready = true;
self
}
/// Puts content `bytes` under `hash` in the game's archive, for
/// `content_read`.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().archive([4, 1, 2, 3], vec![1, 2, 3, 4]).install();
/// # let _ = loader;
/// ```
#[must_use]
pub fn archive(mut self, hash: [u32; 4], bytes: Vec<u8>) -> Self {
self.archive.insert(hash, bytes);
self
}
/// Puts resource `bytes` under `key` in the game's archive, for
/// `resource_read`.
///
/// # Examples
///
/// ```
/// use enshwick::content::Guid;
///
/// let key = Guid::parse("2f04a05f-697d-489b-a32f-76bb3439f5bc").key_by_hash(1);
/// let loader = enshwick::test::FakeHost::new().resource(key, vec![1, 2, 3]).install();
/// # let _ = loader;
/// ```
#[must_use]
pub fn resource(mut self, key: ResourceKey, bytes: Vec<u8>) -> Self {
self.resources
.insert((key.guid, key.type_hash, key.part), bytes);
self
}
/// Gives the game the text `text` for tag `id` in `language`, for
/// `game_text`; a tag without a text in a language falls back to
/// [`Language::EN_US`](crate::i18n::Language::EN_US) as the game's does.
///
/// # Examples
///
/// ```
/// use enshwick::i18n::Language;
///
/// let loader = enshwick::test::FakeHost::new()
/// .game_text(7, Language::UK_UA, "Бальтазар")
/// .install();
/// # let _ = loader;
/// ```
#[must_use]
pub fn game_text(
mut self,
id: u32,
language: crate::i18n::Language,
text: &'static str,
) -> Self {
self.game_texts.insert((id, language.0), text);
self
}
/// Plays a game whose recipe `recipe` is disabled, as
/// [`recipes::is_disabled`](crate::content::recipes::is_disabled) asks.
///
/// # Examples
///
/// ```
/// use enshwick::content::Guid;
///
/// let torch = Guid::parse("7fa62007-a38e-4790-8591-873055fc3f34");
/// let loader = enshwick::test::FakeHost::new().disabled_recipe(torch).install();
/// # let _ = loader;
/// ```
#[must_use]
pub fn disabled_recipe(mut self, recipe: crate::content::Guid) -> Self {
self.disabled_recipes.insert(recipe.fnv1a());
self
}
/// Plays a game that has loaded the item `item` as one of `kind`, as
/// [`RecipeView::kind`](crate::content::recipes::RecipeView::kind) asks;
/// every other item reads as not loaded yet.
///
/// # Examples
///
/// ```
/// use enshwick::content::Guid;
/// use enshwick::content::recipes::RecipeKind;
///
/// let plank = Guid::parse("0b6c5a52-1df4-4a3b-bd0e-6c1f7e2a9c44");
/// let loader = enshwick::test::FakeHost::new()
/// .recipe_kind(plank, RecipeKind::BuildingBlock)
/// .install();
/// # let _ = loader;
/// ```
#[must_use]
pub fn recipe_kind(
mut self,
item: crate::content::Guid,
kind: crate::content::recipes::RecipeKind,
) -> Self {
self.recipe_kinds.insert(item.fnv1a(), kind.into());
self
}
}
#[expect(
clippy::unused_self,
reason = "the fake's state is process-wide; `self` proves it is installed"
)]
impl Installed {
/// The resources the mod serves, in order.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// assert!(loader.resources().is_empty());
/// ```
#[must_use]
pub fn resources(&self) -> Vec<ServedResource> {
table::with(Vec::new(), |s| {
let mut keys: Vec<ServedResource> = Vec::new();
for served in &s.served {
if !keys.iter().any(|k| k.key == served.0.key) {
keys.push(served.0);
}
}
keys
})
}
/// The changes of vanilla values the mod declared, in order, each with
/// its source.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// assert!(loader.changes().is_empty());
/// ```
#[must_use]
pub fn changes(&self) -> Vec<(crate::content::changes::Change, String)> {
table::with(Vec::new(), |s| s.changes.one.clone())
}
/// The changes of every item a selection picks the mod declared, in
/// order, each with its source.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// assert!(loader.item_changes().is_empty());
/// ```
#[must_use]
pub fn item_changes(&self) -> Vec<(crate::content::changes::ItemsChange, String)> {
table::with(Vec::new(), |s| s.changes.items.clone())
}
/// The changes of every recipe a selection picks the mod declared, in
/// order, each with its source.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// assert!(loader.recipe_changes().is_empty());
/// ```
#[must_use]
pub fn recipe_changes(&self) -> Vec<(crate::content::changes::RecipesChange, String)> {
table::with(Vec::new(), |s| s.changes.recipes.clone())
}
/// The changes the mod declared to set later, in order, each with its
/// value in force: `None` until set or after a reset.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// assert!(loader.live_changes().is_empty());
/// ```
#[must_use]
pub fn live_changes(&self) -> Vec<LiveChange> {
table::with(Vec::new(), |s| s.changes.live.clone())
}
/// The mod's recipe rules in force, from
/// [`recipes::disable`](crate::content::recipes::disable) less those
/// [`recipes::enable`](crate::content::recipes::enable) took back: kind
/// and name.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// assert!(loader.recipe_rules().is_empty());
/// ```
#[must_use]
pub fn recipe_rules(&self) -> Vec<(crate::abi::RecipeRuleKind, String)> {
table::with(Vec::new(), |s| s.recipe_rules.clone())
}
/// The served resources the mod asked to rebuild, in order: each
/// [`Load::allow_rebuild`](crate::Load::allow_rebuild) at init and each
/// [`rebuild`](crate::content::rebuild) after.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// assert!(loader.rebuilds().is_empty());
/// ```
#[must_use]
pub fn rebuilds(&self) -> Vec<ResourceKey> {
table::with(Vec::new(), |s| s.rebuilds.clone())
}
/// Builds the served resource `key` from `base` with the mod's patches, in order, as
/// the game's first read of it would.
///
/// # Errors
///
/// [`Status::MISSING`] when the mod serves no `key`, or the patch's own
/// status when it fails.
///
/// # Examples
///
/// ```
/// use enshwick::abi::{ResourceKey, Status};
///
/// let loader = enshwick::test::FakeHost::new().install();
/// let key = ResourceKey { guid: [0x3a; 16], type_hash: 0xb5ce_8765, part: 0 };
/// assert_eq!(loader.build_resource(&key, &[]), Err(Status::MISSING));
/// ```
pub fn build_resource(&self, key: &ResourceKey, base: &[u8]) -> Result<Vec<u8>, Status> {
let patches: Vec<_> = table::with(Vec::new(), |s| {
s.served
.iter()
.filter(|s| s.0.key == *key)
.map(|s| (s.1, s.2))
.collect()
});
if patches.is_empty() {
return Err(Status::MISSING);
}
let mut bytes = base.to_vec();
for (patch, user) in patches {
let mut out = core::ptr::null();
let mut out_len = 0;
// SAFETY: `bytes` is readable for the call, the outs are valid,
// and `user` is the pointer the mod served with.
let status = unsafe {
patch(
bytes.as_ptr(),
bytes.len(),
core::ptr::with_exposed_provenance_mut(user),
&raw mut out,
&raw mut out_len,
)
};
if !status.is_ok() {
return Err(status);
}
bytes = if out.is_null() {
Vec::new()
} else {
// SAFETY: on success the patch wrote bytes that live for the
// process.
unsafe { core::slice::from_raw_parts(out, out_len) }.to_vec()
};
}
Ok(bytes)
}
/// The texts the mod added, as `(tag, language, text)`, in order.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// assert!(loader.texts().is_empty());
/// ```
#[must_use]
pub fn texts(&self) -> Vec<(u32, u32, String)> {
table::with(Vec::new(), |s| s.texts.clone())
}
/// The item data kinds the mod opened, by full id, and every value set:
/// `(kind id, entity, item, bytes)`.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// assert!(loader.item_data().is_empty());
/// ```
#[must_use]
pub fn item_data(&self) -> Vec<(String, u32, u32, Vec<u8>)> {
table::with(Vec::new(), |s| {
s.item_values
.iter()
.map(|(&(kind, pide), (item, bytes))| {
let id = s.item_kinds.get(kind as usize).cloned().unwrap_or_default();
(id, pide, *item, bytes.clone())
})
.collect()
})
}
/// The content the mod served, by hash.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// assert!(loader.served_content().is_empty());
/// ```
#[must_use]
pub fn served_content(&self) -> Vec<([u32; 4], Vec<u8>)> {
table::with(Vec::new(), |s| {
s.content.iter().map(|(h, b)| (*h, b.clone())).collect()
})
}
/// The texts the mod handed the loader last, as TOML; `None` before it
/// read any.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// assert_eq!(loader.published_texts(), None);
/// ```
#[must_use]
pub fn published_texts(&self) -> Option<String> {
table::with(None, |s| s.published_texts.clone())
}
/// The services the mod published, as `(name, major)`, in order.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// assert!(loader.published().is_empty());
/// ```
#[must_use]
pub fn published(&self) -> Vec<(String, u32)> {
table::with(Vec::new(), |s| {
s.published
.iter()
.map(|p| (p.name.clone(), p.major))
.collect()
})
}
/// The content ids the mod declared, `<mod>:<name>`, in order.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// assert!(loader.content_ids().is_empty());
/// ```
#[must_use]
pub fn content_ids(&self) -> Vec<String> {
table::with(Vec::new(), |s| s.content_ids.clone())
}
}
/// A resource the mod serves, as the loader received it.
///
/// # Examples
///
/// See [`Installed::resources`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ServedResource {
/// The resource served.
pub key: ResourceKey,
/// The resource it is built from.
pub base: ResourceKey,
}
/// A change a mod declared to set later, as the fake loader keeps it.
///
/// # Examples
///
/// See [`Installed::live_changes`](crate::test::Installed::live_changes).
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct LiveChange {
/// What it reaches.
pub target: crate::content::changes::Target,
/// What it changes.
pub field: crate::content::changes::Field,
/// Its source.
pub source: String,
/// The value in force; `None` until set or after a reset.
pub value: Option<crate::content::changes::Value>,
}