enshwick 1.0.0-alpha.11

The Enshwick Loader's modding API for Enshrouded: the one crate mods are written against
Documentation
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
//! Changes of one field of every vanilla item a selection picks, which the
//! loader turns into one change per item from the game's data.

use crate::abi::{ChangeOp, DataChange, DataSelect, Header, ItemData, Str};
use crate::content::Guid;
use crate::content::categories::ItemCategoryMask;
use crate::{Load, Off};

use super::{Change, Field, Value, ValueError, field_and_value};

/// The vanilla items an [`ItemsChange`] reaches at once.
///
/// A stack size reaches only items one slot already holds more than one of
/// and that keep no state of their own, such as durability; container slots
/// reach only items that place a container or are backpacks, and
/// [`Items::Containers`] and [`Items::Backpacks`] pick one kind alone.
///
/// # Examples
///
/// ```
/// use enshwick::content::changes::{Field, ItemTraits, Items};
///
/// let wood = ItemTraits { max_stack: 20, category: 9, flags: 0, slots: None, backpack_slots: None };
/// assert!(Items::Stackable.picks(Field::MaxStackSize, &wood));
/// assert!(!Items::Containers.picks(Field::Slots, &wood));
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Items {
    /// Every item of these categories.
    Category(ItemCategoryMask),
    /// Every item one slot holds more than one of.
    Stackable,
    /// Every item that places a container.
    Containers,
    /// Every backpack: an item worn in the game's `Backpack` equipment slot,
    /// which carries an inventory of its own.
    Backpacks,
}

/// What a selection reads of one vanilla item, from the game's data.
///
/// # Examples
///
/// See [`Items`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ItemTraits {
    /// How many of it one slot holds.
    pub max_stack: u16,
    /// The number of its `ItemCategory`.
    pub category: u8,
    /// Its flags, such as [`ItemData::HAS_PIDE`].
    pub flags: u32,
    /// How many slots the container it places holds; `None` when it places
    /// none.
    pub slots: Option<u8>,
    /// How many slots it holds as a backpack; `None` for an item that is no
    /// backpack.
    pub backpack_slots: Option<u8>,
}

impl Items {
    /// Whether a change of `field` through this selection reaches `item`.
    ///
    /// # Examples
    ///
    /// See [`Items`].
    #[must_use]
    pub const fn picks(self, field: Field, item: &ItemTraits) -> bool {
        let selected = match self {
            Self::Category(mask) => mask.holds(item.category),
            Self::Stackable => item.max_stack > 1,
            Self::Containers => item.slots.is_some(),
            Self::Backpacks => item.backpack_slots.is_some(),
        };
        selected
            && match field {
                Field::MaxStackSize => item.max_stack > 1 && item.flags & ItemData::HAS_PIDE == 0,
                Field::Slots => match self {
                    Self::Containers => item.slots.is_some(),
                    Self::Backpacks => item.backpack_slots.is_some(),
                    _ => item.slots.is_some() || item.backpack_slots.is_some(),
                },
                _ => false,
            }
    }

    /// The selection's number in the ABI and its categories.
    pub(super) const fn to_abi(self) -> (DataSelect, u32) {
        match self {
            Self::Category(mask) => (DataSelect::CATEGORY, mask.0),
            Self::Stackable => (DataSelect::STACKABLE, 0),
            Self::Containers => (DataSelect::CONTAINERS, 0),
            Self::Backpacks => (DataSelect::BACKPACKS, 0),
        }
    }

    /// The selection an ABI record names; `None` for [`DataSelect::ENTRY`]
    /// and a selection this crate does not know.
    const fn from_abi(select: DataSelect, categories: u32) -> Option<Self> {
        match select {
            DataSelect::CATEGORY => Some(Self::Category(ItemCategoryMask(categories))),
            DataSelect::STACKABLE => Some(Self::Stackable),
            DataSelect::CONTAINERS => Some(Self::Containers),
            DataSelect::BACKPACKS => Some(Self::Backpacks),
            _ => None,
        }
    }
}

impl Field {
    /// Whether a selection of items changes this field: a stack size or
    /// container slots.
    ///
    /// # Examples
    ///
    /// ```
    /// use enshwick::content::changes::Field;
    ///
    /// assert!(Field::MaxStackSize.selectable());
    /// assert!(!Field::Output.selectable());
    /// ```
    #[must_use]
    pub const fn selectable(self) -> bool {
        matches!(self, Self::MaxStackSize | Self::Slots)
    }
}

/// One change of a field of every vanilla item a selection picks.
///
/// # Examples
///
/// ```
/// use enshwick::content::changes::{Field, Items, ItemsChange, Value};
///
/// fn init(load: &mut enshwick::Load<'_>) -> Result<(), enshwick::Off> {
///     let stacks = ItemsChange::new(Items::Stackable, Field::MaxStackSize, Value::Times(4.0));
///     load.change_items(&stacks, "my-mod")
/// }
/// ```
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ItemsChange {
    /// The items.
    pub items: Items,
    /// What changes.
    pub field: Field,
    /// How.
    pub value: Value,
}

impl ItemsChange {
    /// The change of `field` of every item `items` picks by `value`.
    ///
    /// # Examples
    ///
    /// See [`ItemsChange`].
    #[must_use]
    pub const fn new(items: Items, field: Field, value: Value) -> Self {
        Self {
            items,
            field,
            value,
        }
    }

    /// Whether the loader takes the change: a field a selection changes,
    /// and a value [`Field::check`] takes.
    ///
    /// # Errors
    ///
    /// [`ValueError`] saying why not.
    ///
    /// # Examples
    ///
    /// ```
    /// use enshwick::content::changes::{Field, Items, ItemsChange, Value};
    ///
    /// let output = ItemsChange::new(Items::Stackable, Field::Output, Value::Set(2.0));
    /// assert_eq!(
    ///     output.check().unwrap_err().to_string(),
    ///     "output_count is no field a selection of items changes"
    /// );
    /// ```
    pub fn check(&self) -> Result<(), ValueError> {
        if !self.field.selectable() {
            return Err(ValueError::NotSelectable { field: self.field });
        }
        self.field.check(self.value)
    }

    /// The same change of the one item `item`.
    ///
    /// # Examples
    ///
    /// ```
    /// use enshwick::content::Guid;
    /// use enshwick::content::changes::{Field, Items, ItemsChange, Value};
    ///
    /// let stacks = ItemsChange::new(Items::Stackable, Field::MaxStackSize, Value::Times(2.0));
    /// assert_eq!(stacks.of(Guid([7; 16])).entry, Guid([7; 16]));
    /// ```
    #[must_use]
    pub const fn of(&self, item: Guid) -> Change {
        Change::new(item, self.field, self.value)
    }

    /// The change an ABI record describes, checked as
    /// [`ItemsChange::check`] checks it; `None` for a record that names one
    /// entry, or a selection, field or operation this crate does not know.
    ///
    /// # Errors
    ///
    /// [`ValueError`] for a field or value the selection does not take.
    ///
    /// # Examples
    ///
    /// ```
    /// use enshwick::abi::{ChangeOp, DataChange, DataField, DataSelect, Header, Str};
    /// use enshwick::content::changes::{Items, ItemsChange, Value};
    ///
    /// let raw = DataChange {
    ///     header: Header::of::<DataChange>(),
    ///     entry: [0; 16],
    ///     field: DataField::MAX_STACK,
    ///     op: ChangeOp::TIMES,
    ///     value: 4.0,
    ///     source: Str::EMPTY,
    ///     item: [0; 16],
    ///     select: DataSelect::STACKABLE,
    ///     categories: 0,
    /// };
    /// let change = ItemsChange::from_abi(&raw)?.ok_or("unknown")?;
    /// assert_eq!((change.items, change.value), (Items::Stackable, Value::Times(4.0)));
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn from_abi(raw: &DataChange) -> Result<Option<Self>, ValueError> {
        let Some(items) = Items::from_abi(raw.select, raw.categories) else {
            return Ok(None);
        };
        let Some((field, value)) = field_and_value(raw) else {
            return Ok(None);
        };
        let change = Self::new(items, field, value);
        change.check()?;
        Ok(Some(change))
    }

    /// The ABI record of the change, with `source` borrowed into it.
    fn to_abi(self, source: &str) -> DataChange {
        let (op, value) = match self.value {
            Value::Set(v) => (ChangeOp::SET, v),
            Value::Times(v) => (ChangeOp::TIMES, v),
        };
        let (select, categories) = self.items.to_abi();
        DataChange {
            header: Header::of::<DataChange>(),
            entry: [0; 16],
            field: self.field.to_abi(),
            op,
            value,
            source: Str::new(source),
            item: [0; 16],
            select,
            categories,
        }
    }
}

impl Load<'_> {
    /// Declares `change` during `init`, from `source`, as
    /// [`Load::change`] declares a change of one entry; the loader turns it
    /// into one change of each vanilla item the selection picks, read from
    /// the game's data, in this mod's place in load order. Items of mods
    /// are not picked.
    ///
    /// # Errors
    ///
    /// [`Off`] for a field a selection does not change, a value the field
    /// does not take, after init, and when the running build does not
    /// reflect the field.
    ///
    /// # Examples
    ///
    /// See [`ItemsChange`].
    pub fn change_items(&mut self, change: &ItemsChange, source: &str) -> Result<(), Off> {
        change.field.unstable_gate()?;
        change
            .check()
            .map_err(|why| Off::new(format!("change of {:?}: {why}", change.items)))?;
        let raw = change.to_abi(source);
        let api = self.host().api();
        let status = (api.change_data)(self.host().handle(), &raw const raw);
        crate::load::ok(status, (), || {
            format!("change {} of {:?}", change.field, change.items)
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const WOOD: ItemTraits = ItemTraits {
        max_stack: 20,
        category: 9,
        flags: 0,
        slots: None,
        backpack_slots: None,
    };
    const SWORD: ItemTraits = ItemTraits {
        max_stack: 1,
        category: 2,
        flags: ItemData::HAS_PIDE,
        slots: None,
        backpack_slots: None,
    };
    const CHEST: ItemTraits = ItemTraits {
        max_stack: 1,
        category: 6,
        flags: 0,
        slots: Some(24),
        backpack_slots: None,
    };
    const BACKPACK: ItemTraits = ItemTraits {
        max_stack: 1,
        category: 1,
        flags: ItemData::HAS_PIDE,
        slots: None,
        backpack_slots: Some(16),
    };

    #[test]
    fn backpack_slots_reach_backpacks_alone() {
        assert!(Items::Backpacks.picks(Field::Slots, &BACKPACK));
        assert!(!Items::Backpacks.picks(Field::Slots, &CHEST));
        assert!(!Items::Containers.picks(Field::Slots, &BACKPACK));
        assert!(!Items::Backpacks.picks(Field::MaxStackSize, &BACKPACK));
        assert!(Items::Category(ItemCategoryMask::EQUIPMENT).picks(Field::Slots, &BACKPACK));
    }

    #[test]
    fn a_stack_size_reaches_only_items_that_stack() {
        let every = Items::Category(ItemCategoryMask(u32::MAX));
        assert!(every.picks(Field::MaxStackSize, &WOOD));
        assert!(!every.picks(Field::MaxStackSize, &SWORD));
        assert!(!every.picks(Field::MaxStackSize, &CHEST));
        let held = ItemTraits {
            max_stack: 5,
            flags: ItemData::HAS_PIDE,
            ..WOOD
        };
        assert!(!Items::Stackable.picks(Field::MaxStackSize, &held));
    }

    #[test]
    fn container_slots_reach_only_items_that_place_one() {
        assert!(Items::Containers.picks(Field::Slots, &CHEST));
        assert!(!Items::Containers.picks(Field::Slots, &WOOD));
        assert!(Items::Category(ItemCategoryMask::BUILD_TOOLS).picks(Field::Slots, &CHEST));
        assert!(!Items::Containers.picks(Field::MaxStackSize, &CHEST));
    }

    #[test]
    fn a_category_picks_its_items_alone() {
        let materials = Items::Category(ItemCategoryMask::MATERIALS);
        assert!(materials.picks(Field::MaxStackSize, &WOOD));
        let ammo = ItemTraits {
            category: 8,
            ..WOOD
        };
        assert!(!materials.picks(Field::MaxStackSize, &ammo));
    }

    #[test]
    fn a_selection_goes_through_the_abi_and_back() {
        for items in [
            Items::Category(ItemCategoryMask::MATERIALS | ItemCategoryMask::AMMUNITION),
            Items::Stackable,
            Items::Containers,
            Items::Backpacks,
        ] {
            let field = if matches!(items, Items::Containers | Items::Backpacks) {
                Field::Slots
            } else {
                Field::MaxStackSize
            };
            let change = ItemsChange::new(items, field, Value::Times(2.0));
            let raw = change.to_abi("my-mod");
            assert_eq!(ItemsChange::from_abi(&raw), Ok(Some(change)));
            assert_eq!(Change::from_abi(&raw), Ok(None));
        }
    }

    #[test]
    fn a_record_of_one_entry_is_no_selection() {
        let one = Change::new(Guid([7; 16]), Field::MaxStackSize, Value::Set(50.0));
        assert_eq!(ItemsChange::from_abi(&one.to_abi("x")), Ok(None));
    }

    #[test]
    fn a_selection_refuses_fields_it_does_not_reach() {
        let damage = ItemsChange::new(Items::Stackable, Field::DamageMod, Value::Times(2.0));
        assert!(damage.check().is_err());
        let shrink = ItemsChange::new(Items::Containers, Field::Slots, Value::Times(0.5));
        assert!(shrink.check().is_err());
    }
}