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
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
//! The fake loader's extension points: the points the mod defines, with the
//! links a test attaches as other mods would, and the links the mod
//! attaches to other mods' points, which a test runs as their owner would.

use core::ffi::c_void;
use core::sync::atomic::{AtomicU32, Ordering};

use crate::abi::{
    ModHandle, PointFn, PointHandle, PointLink, PointSpec, PointVerdict, Status, Str,
};
use crate::points::{Decision, LinkPlace, Point, Verdict, order};

use super::Installed;
use super::table::{put, view, with};

/// A point as declared: name, input and answer layouts, whether links
/// change the answer and whether they may refuse.
#[derive(Clone)]
struct Declared {
    name: String,
    input: Vec<(String, u64)>,
    output: Vec<(String, u64)>,
    mode: crate::abi::PointMode,
    refuse: bool,
    input_size: u32,
    output_size: u32,
}

impl Declared {
    fn of<P: Point>() -> Self {
        let fields = |layout: &[crate::abi::LayoutField]| {
            layout
                .iter()
                // SAFETY: the declaration's names are static strings.
                .filter_map(|f| unsafe { f.name.as_str() }.map(|n| (n.to_owned(), f.hash)))
                .collect()
        };
        Self {
            name: P::NAME.to_owned(),
            input: fields(P::LAYOUT),
            output: fields(P::OUTPUT),
            mode: P::MODE.to_abi(),
            refuse: P::REFUSE,
            input_size: u32::try_from(size_of::<P>()).unwrap_or(u32::MAX),
            output_size: u32::try_from(size_of::<P::Output>()).unwrap_or(u32::MAX),
        }
    }

    /// Whether a link declared as `link` serves this point.
    fn serves(&self, link: &Self) -> bool {
        self.name == link.name
            && crate::services::compare(&view(&link.input), &view(&self.input)).is_ok()
            && self.output == link.output
            && (self.mode, self.refuse) == (link.mode, link.refuse)
            && link.input_size <= self.input_size
            && link.output_size == self.output_size
    }
}

/// A link: its handler and pointer, and whether it is still on.
#[derive(Clone, Copy)]
struct Link {
    callback: PointFn,
    user: usize,
    on: bool,
}

/// A point the mod defined, with the links tests attached.
struct Defined {
    declared: Declared,
    live: &'static AtomicU32,
    links: Vec<Link>,
}

/// A link the mod attached to another mod's point; every one has the
/// mod's id, so `id` is empty.
struct Attached {
    declared: Declared,
    id: String,
    priority: i32,
    before: Vec<String>,
    after: Vec<String>,
    link: Link,
}

/// The fake's points.
#[derive(Default)]
pub(crate) struct FakePoints {
    defined: Vec<Defined>,
    attached: Vec<Attached>,
}

/// The declaration a spec carries; `None` for a bad one.
fn declared(spec: *const PointSpec) -> Option<Declared> {
    // SAFETY: the safe layer passes a valid spec.
    let spec = unsafe { spec.as_ref() }?;
    // SAFETY: the safe layer passes the spec's name borrowed and unchanged
    // for the call.
    let name = unsafe { spec.name.as_str() }?.to_owned();
    // SAFETY: the safe layer passes the input layout of a declared type, its
    // fields and their names borrowed and unchanged for the call.
    let input = unsafe { crate::services::read_layout(&spec.input) }?;
    // SAFETY: the safe layer passes the output layout of a declared type, its
    // fields and their names borrowed and unchanged for the call.
    let output = unsafe { crate::services::read_layout(&spec.output) }?;
    Some(Declared {
        name,
        input,
        output,
        mode: spec.mode,
        refuse: spec.flags.contains(crate::abi::PointFlags::REFUSE),
        input_size: spec.input.size,
        output_size: spec.output.size,
    })
}

/// The ids a link names, as owned strings.
fn ids(raw: *const Str, count: u32) -> Vec<String> {
    if raw.is_null() || count == 0 {
        return Vec::new();
    }
    // SAFETY: the safe layer passes `count` strings, borrowed for the call.
    let ids = unsafe { core::slice::from_raw_parts(raw, count as usize) };
    ids.iter()
        // SAFETY: each id is one of the `count` strings the safe layer passes
        // borrowed and unchanged for the call.
        .filter_map(|id| unsafe { id.as_str() }.map(str::to_owned))
        .collect()
}

pub(crate) extern "system" fn define_point(
    _me: ModHandle,
    spec: *const PointSpec,
    out: *mut PointHandle,
) -> Status {
    let Some(declared) = declared(spec) else {
        return Status::BAD_ARGS;
    };
    let id = with(None, |s| {
        let points = &mut s.points;
        if points
            .defined
            .iter()
            .any(|d| d.declared.name == declared.name)
        {
            return None;
        }
        let live: &'static AtomicU32 = Box::leak(Box::new(AtomicU32::new(0)));
        points.defined.push(Defined {
            declared,
            live,
            links: Vec::new(),
        });
        u32::try_from(points.defined.len() - 1)
            .ok()
            .map(|id| (id, live))
    });
    match id {
        Some((id, live)) => put(
            out,
            PointHandle {
                header: crate::abi::Header::of::<PointHandle>(),
                id,
                live,
            },
        ),
        None => Status::REFUSED,
    }
}

pub(crate) extern "system" fn attach_point(
    _me: ModHandle,
    link: *const PointLink,
    callback: PointFn,
    user: *mut c_void,
) -> Status {
    // SAFETY: the safe layer passes a valid link.
    let Some(link) = (unsafe { link.as_ref() }) else {
        return Status::BAD_ARGS;
    };
    let Some(declared) = declared(link.point) else {
        return Status::BAD_ARGS;
    };
    let attached = Attached {
        declared,
        id: String::new(),
        priority: link.priority,
        before: ids(link.before, link.before_count),
        after: ids(link.after, link.after_count),
        link: Link {
            callback,
            user: user.expose_provenance(),
            on: true,
        },
    };
    with(Status::MISSING, |s| {
        let attached_already = s
            .points
            .attached
            .iter()
            .any(|a| a.declared.name == attached.declared.name);
        if attached_already {
            return Status::REFUSED;
        }
        s.points.attached.push(attached);
        Status::OK
    })
}

/// Runs `links` in turn over `value`; the positions of links that panicked
/// come back with the verdict.
fn run(
    links: &[Link],
    changes: bool,
    refuse: bool,
    input: *const c_void,
    value: *mut c_void,
) -> (PointVerdict, Vec<usize>) {
    let mut verdict = PointVerdict::PASS;
    let mut panicked = Vec::new();
    for (i, link) in links.iter().enumerate().filter(|(_, l)| l.on) {
        // SAFETY: the layouts match, input and value live for the call, and
        // `user` is the pointer the link was attached with.
        let answer = unsafe {
            (link.callback)(
                input,
                value,
                core::ptr::with_exposed_provenance_mut(link.user),
            )
        };
        match answer {
            PointVerdict::SET if changes => verdict = PointVerdict::SET,
            PointVerdict::REFUSE if changes && refuse => return (PointVerdict::REFUSE, panicked),
            PointVerdict::PANICKED => panicked.push(i),
            _ => {}
        }
    }
    (verdict, panicked)
}

pub(crate) extern "system" fn run_point(
    _me: ModHandle,
    point: u32,
    input: *const c_void,
    value: *mut c_void,
    verdict: *mut PointVerdict,
) -> Status {
    let Some((links, changes, refuse)) = with(None, |s| {
        s.points.defined.get(point as usize).map(|d| {
            (
                d.links.clone(),
                d.declared.mode != crate::abi::PointMode::OBSERVE,
                d.declared.refuse,
            )
        })
    }) else {
        return Status::BAD_ARGS;
    };
    let (answer, panicked) = run(&links, changes, refuse, input, value);
    if !panicked.is_empty() {
        with((), |s| {
            if let Some(defined) = s.points.defined.get_mut(point as usize) {
                for &i in &panicked {
                    if let Some(link) = defined.links.get_mut(i) {
                        link.on = false;
                        defined.live.fetch_sub(1, Ordering::Relaxed);
                    }
                }
            }
        });
    }
    put(verdict, answer)
}

#[expect(
    clippy::unused_self,
    reason = "the fake's state is process-wide; `self` proves it is installed"
)]
impl Installed {
    /// Attaches `link` to the mod's point `P`, as another mod would; it runs
    /// after the links attached before it. Returns false when the mod
    /// defines no point that serves `P`. A panicking link turns off, as
    /// under the loader.
    ///
    /// # Examples
    ///
    /// ```
    /// use enshwick::points::Verdict;
    ///
    /// #[enshwick::point(name = "you.my-mod:limit", mode = Override, output = u32)]
    /// #[derive(Clone, Copy, Debug)]
    /// pub struct Limit {
    ///     pub level: u32,
    /// }
    ///
    /// let loader = enshwick::test::FakeHost::new().install();
    /// assert!(!loader.extend_point(|_: &Limit, _: &u32| Verdict::Set(3)));
    /// ```
    #[must_use = "false means the mod defines no such point"]
    pub fn extend_point<P: Point, F>(&self, link: F) -> bool
    where
        F: Fn(&P, &P::Output) -> Verdict<P::Output> + Send + Sync + 'static,
    {
        let wanted = Declared::of::<P>();
        let user = Box::into_raw(Box::new(link));
        let attached = with(false, |s| {
            let Some(defined) = s
                .points
                .defined
                .iter_mut()
                .find(|d| d.declared.serves(&wanted))
            else {
                return false;
            };
            defined.links.push(Link {
                callback: crate::points::run_link::<P, F>,
                user: user.expose_provenance(),
                on: true,
            });
            defined.live.fetch_add(1, Ordering::Relaxed);
            true
        });
        if !attached {
            // SAFETY: `user` came from `Box::into_raw` just before, and the
            // closure returned `false` before pushing a link, so no link holds
            // it and this is its only release.
            drop(unsafe { Box::from_raw(user) });
        }
        attached
    }

    /// The points the mod defined, by name, in order.
    ///
    /// # Examples
    ///
    /// ```
    /// let loader = enshwick::test::FakeHost::new().install();
    /// assert!(loader.defined_points().is_empty());
    /// ```
    #[must_use]
    pub fn defined_points(&self) -> Vec<String> {
        with(Vec::new(), |s| {
            s.points
                .defined
                .iter()
                .map(|d| d.declared.name.clone())
                .collect()
        })
    }

    /// The points the mod attached links to, by name, in order.
    ///
    /// # Examples
    ///
    /// ```
    /// let loader = enshwick::test::FakeHost::new().install();
    /// assert!(loader.attached_points().is_empty());
    /// ```
    #[must_use]
    pub fn attached_points(&self) -> Vec<String> {
        with(Vec::new(), |s| {
            s.points
                .attached
                .iter()
                .map(|a| a.declared.name.clone())
                .collect()
        })
    }

    /// Asks the point `P` as its owner would, with `answer` as the owner's
    /// own, running the mod's links that serve `P` in the loader's order;
    /// [`Decision::Kept`] when none does.
    ///
    /// # Examples
    ///
    /// ```
    /// use enshwick::points::Decision;
    ///
    /// #[enshwick::point(name = "you.my-mod:limit", mode = Override, output = u32)]
    /// #[derive(Clone, Copy, Debug)]
    /// pub struct Limit {
    ///     pub level: u32,
    /// }
    ///
    /// let loader = enshwick::test::FakeHost::new().install();
    /// assert_eq!(loader.ask_point(&Limit { level: 1 }, 4), Decision::Kept(4));
    /// ```
    #[must_use]
    pub fn ask_point<P: Point>(&self, input: &P, answer: P::Output) -> Decision<P::Output> {
        let owner = Declared::of::<P>();
        let (links, positions) = with((Vec::new(), Vec::new()), |s| {
            let serving: Vec<(usize, &Attached)> = s
                .points
                .attached
                .iter()
                .enumerate()
                .filter(|(_, a)| owner.serves(&a.declared))
                .collect();
            let before: Vec<Vec<&str>> = serving
                .iter()
                .map(|(_, a)| a.before.iter().map(String::as_str).collect())
                .collect();
            let after: Vec<Vec<&str>> = serving
                .iter()
                .map(|(_, a)| a.after.iter().map(String::as_str).collect())
                .collect();
            let places: Vec<LinkPlace<'_>> = serving
                .iter()
                .zip(before.iter().zip(&after))
                .map(|((_, a), (before, after))| LinkPlace {
                    id: &a.id,
                    priority: a.priority,
                    before,
                    after,
                })
                .collect();
            let (running, _) = order(&places);
            let links: Vec<Link> = running.iter().map(|&i| serving[i].1.link).collect();
            let positions: Vec<usize> = running.iter().map(|&i| serving[i].0).collect();
            (links, positions)
        });
        let mut value = answer;
        let (verdict, panicked) = run(
            &links,
            owner.mode != crate::abi::PointMode::OBSERVE,
            owner.refuse,
            core::ptr::from_ref(input).cast(),
            (&raw mut value).cast(),
        );
        with((), |s| {
            for i in panicked {
                if let Some(attached) = positions.get(i).and_then(|&p| s.points.attached.get_mut(p))
                {
                    attached.link.on = false;
                }
            }
        });
        match verdict {
            PointVerdict::SET => Decision::Changed(value),
            PointVerdict::REFUSE => Decision::Refused,
            _ => Decision::Kept(answer),
        }
    }
}