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
//! Typed loader events, and the [`Kind`] that [`Load::on`](crate::Load::on)
//! takes for them and for the typed [game events](crate::game_events).
//!
//! Events from the server are raised on its game thread: keep handlers short,
//! and leave slow work to a thread of the mod's own. On a client they come
//! from the built-in server while it hosts a game, solo play included; each
//! hosted game starts with [`WorldLoaded`] and ends with [`ShutdownStarted`], so
//! state of one game is reset there.
//!
//! Each event is named for what happened, a subject and a verb in the past
//! tense, and its [`Event`] constant is the same name in capitals:
//! [`PlayerJoined`] is [`Event::PLAYER_JOINED`]. The names are enshwick's:
//! a loader event often joins several game events, whose own names live in
//! [`game_events`](crate::game_events).

use core::ffi::c_void;
use core::time::Duration;

#[cfg(feature = "unstable")]
use crate::abi::PlayerDeathData;
use crate::abi::{
    BuildingData, ChunkData, Event, KnowledgeData, PlayerData, QuestData, TickData, TickHandle,
};

pub(crate) mod sealed {
    pub trait Sealed {}
}

/// An event [`Load::on`](crate::Load::on) subscribes to: a loader event of
/// this module or a typed game event of [`game_events`](crate::game_events);
/// implemented by those types only.
///
/// # Examples
///
/// ```
/// use enshwick::events::{Kind, PlayerJoined};
/// use enshwick::game_events::EntityDiedEvent;
///
/// fn watch<E: Kind>(load: &mut enshwick::Load<'_>) -> Result<(), enshwick::Off> {
///     load.on::<E>(|_| enshwick::debug!("{}", core::any::type_name::<E>()))
/// }
///
/// fn init(load: &mut enshwick::Load<'_>) -> Result<(), enshwick::Off> {
///     watch::<PlayerJoined>(load)?;
///     watch::<EntityDiedEvent>(load)
/// }
/// ```
pub trait Kind: sealed::Sealed + 'static {
    /// What a handler gets: an ABI struct or `()` for a loader event, the
    /// event itself for a game event.
    type Data: 'static;

    /// Resolves what the event needs on this build and subscribes `handler`.
    #[doc(hidden)]
    fn subscribe<F>(load: &mut crate::Load<'_>, handler: F) -> Result<(), crate::Off>
    where
        F: Fn(&Self::Data) + Send + Sync + 'static;
}

/// A loader event, raised under its [`Event`] constant by the loader and by
/// the fake host's `raise`; implemented by the types in this module only.
///
/// # Examples
///
/// ```
/// use enshwick::abi::Event;
/// use enshwick::events::{LoaderEvent, PlayerJoined};
///
/// assert!(PlayerJoined::EVENT == Event::PLAYER_JOINED);
/// ```
pub trait LoaderEvent: Kind<Data: Copy> {
    /// The raw event.
    const EVENT: Event;

    /// The event's data from the raw pointer the loader passes; `None` when
    /// it is missing.
    ///
    /// # Safety
    ///
    /// - `raw` is null or the pointer the loader passed with
    ///   [`LoaderEvent::EVENT`]: a [`Kind::Data`] the loader wrote, readable for the size its header
    ///   states; no alignment is needed.
    /// - No other thread writes that struct during the call.
    unsafe fn data(raw: *const c_void) -> Option<Self::Data>;
}

macro_rules! without_data {
    ($($(#[doc = $doc:literal])* $name:ident = $event:ident;)*) => {
        /// The script entries of the events without data.
        const SCRIPT_PLAIN: &[crate::script::EventKind] = &[$(
            crate::script::EventKind::new(
                stringify!($name),
                concat!($($doc, "\n"),*),
                Event::$event,
                &crate::script::Ty::Nil,
                |_raw: *const c_void| -> Option<crate::script::Value> { Some(crate::script::Value::Nil) },
            ),
        )*];

        $(
        $(#[doc = $doc])*
        ///
        /// # Examples
        ///
        /// See [`Load::on`](crate::Load::on).
        #[derive(Debug, Clone, Copy)]
        pub struct $name;

        impl sealed::Sealed for $name {}

        impl Kind for $name {
            type Data = ();

            fn subscribe<F>(load: &mut crate::Load<'_>, handler: F) -> Result<(), crate::Off>
            where
                F: Fn(&()) + Send + Sync + 'static,
            {
                load.on_raw_event(Event::$event, move |_raw| handler(&()))
            }
        }

        impl LoaderEvent for $name {
            const EVENT: Event = Event::$event;

            unsafe fn data(_raw: *const c_void) -> Option<()> {
                Some(())
            }
        }
        )*
    };
}

/// The subscription of a loader event with data: every raise, or through
/// a function of its own.
macro_rules! subscribe_with_data {
    ($load:ident, $handler:ident, $name:ident, $event:ident) => {
        $load.on_raw_event(Event::$event, move |raw| {
            // SAFETY: this closure is subscribed under
            // `Event::$event`, this type's `LoaderEvent::EVENT`, and
            // `EventFn` has the loader pass that event's data, null or
            // valid with no other thread writing it for the call.
            if let Some(data) = unsafe { <$name as LoaderEvent>::data(raw) } {
                $handler(&data);
            }
        })
    };
    ($load:ident, $handler:ident, $name:ident, $event:ident, $own:path) => {
        $own($load, $handler)
    };
}

macro_rules! with_data {
    ($($(#[cfg($cfg:meta)])? $(#[doc = $doc:literal])* $name:ident = $event:ident: $data:ty, $born:expr $(, $own:path)?;)*) => {
        /// The script entries of the events with data.
        const SCRIPT_WITH_DATA: &[crate::script::EventKind] = &[$(
            $(#[cfg($cfg)])?
            crate::script::EventKind::new(
                stringify!($name),
                concat!($($doc, "\n"),*),
                Event::$event,
                <$data as crate::script::Typed>::TY,
                |raw: *const c_void| -> Option<crate::script::Value> {
                    // SAFETY: `EventKind::read` passes on the loader's pointer
                    // for this event with no other thread writing its data, by
                    // its `# Safety`, which is what `LoaderEvent::data` asks for.
                    unsafe { <$name as LoaderEvent>::data(raw) }.map(crate::script::IntoScript::into_value)
                },
            ),
        )*];

        $(
        $(#[doc = $doc])*
        ///
        /// # Examples
        ///
        /// See [`Load::on`](crate::Load::on).
        $(#[cfg($cfg)])?
        #[derive(Debug, Clone, Copy)]
        pub struct $name;

        $(#[cfg($cfg)])?
        impl sealed::Sealed for $name {}

        $(#[cfg($cfg)])?
        impl Kind for $name {
            type Data = $data;

            fn subscribe<F>(load: &mut crate::Load<'_>, handler: F) -> Result<(), crate::Off>
            where
                F: Fn(&$data) + Send + Sync + 'static,
            {
                subscribe_with_data!(load, handler, $name, $event $(, $own)?)
            }
        }

        $(#[cfg($cfg)])?
        impl LoaderEvent for $name {
            const EVENT: Event = Event::$event;

            unsafe fn data(raw: *const c_void) -> Option<$data> {
                // SAFETY: the caller guarantees `raw` is null or this event's
                // data, readable for the size its header states with no
                // concurrent writer, which is the contract of `read_headed`.
                unsafe { crate::abi::read_headed(raw.cast::<$data>(), $born) }
            }
        }
        )*
    };
}

without_data! {
    /// [`Event::SETTINGS_CHANGED`], on no fixed thread;
    /// [`Load::on_config_change`](crate::Load::on_config_change) re-reads the
    /// settings for you.
    SettingsChanged = SETTINGS_CHANGED;
    /// [`Event::LOADER_SETTINGS_CHANGED`].
    LoaderSettingsChanged = LOADER_SETTINGS_CHANGED;
    /// [`Event::WORLD_LOADED`].
    WorldLoaded = WORLD_LOADED;
    /// [`Event::WORLD_SAVED`].
    WorldSaved = WORLD_SAVED;
    /// [`Event::SHUTDOWN_STARTED`].
    ShutdownStarted = SHUTDOWN_STARTED;
    /// [`Event::JOIN_REFUSED`]; [`crate::mods::join_missing`] lists the mods.
    JoinRefused = JOIN_REFUSED;
    /// [`Event::ALTARS_CHANGED`]; [`crate::altars::Altars::list`] lists them.
    AltarsChanged = ALTARS_CHANGED;
    /// [`Event::DAY_STARTED`]; [`crate::daytime::Clock::now`] reads the clock.
    DayStarted = DAY_STARTED;
    /// [`Event::NIGHT_STARTED`].
    NightStarted = NIGHT_STARTED;
    /// [`Event::SETTINGS_REPLACED`]; [`Load::on_config_change`](crate::Load::on_config_change)
    /// re-reads the settings for you, the ones that wait for a start too.
    SettingsReplaced = SETTINGS_REPLACED;
}

// Each event names its data and the size that data had in the minor that
// added the event, the part every loader that runs the mod writes.
with_data! {
    /// [`Event::TICKED`], every [`Ticked::DEFAULT_EVERY`] through
    /// [`Load::on`](crate::Load::on); [`Load::on_tick`](crate::Load::on_tick)
    /// picks the period and [`Load::on_every_step`](crate::Load::on_every_step)
    /// runs on every server step.
    Ticked = TICKED: TickData, core::mem::offset_of!(TickData, elapsed_steps), subscribe_ticked;
    /// [`Event::PLAYER_JOINED`].
    PlayerJoined = PLAYER_JOINED: PlayerData, size_of::<PlayerData>();
    /// [`Event::PLAYER_LEFT`].
    PlayerLeft = PLAYER_LEFT: PlayerData, size_of::<PlayerData>();
    /// [`Event::BUILDING_PLACED`].
    BuildingPlaced = BUILDING_PLACED: BuildingData, size_of::<BuildingData>();
    /// [`Event::KNOWLEDGE_CHANGED`]; [`crate::knowledge::Knowledge`] reads and grants.
    KnowledgeChanged = KNOWLEDGE_CHANGED: KnowledgeData, size_of::<KnowledgeData>();
    /// [`Event::QUEST_COMPLETED`].
    QuestCompleted = QUEST_COMPLETED: QuestData, size_of::<QuestData>();
    /// [`Event::CHUNK_LOADED`].
    ChunkLoaded = CHUNK_LOADED: ChunkData, size_of::<ChunkData>();
    /// [`Event::CHUNK_UNLOADED`].
    ChunkUnloaded = CHUNK_UNLOADED: ChunkData, size_of::<ChunkData>();
    /// [`Event::BUILDING_REMOVED`].
    BuildingRemoved = BUILDING_REMOVED: BuildingData, size_of::<BuildingData>();
    #[cfg(feature = "unstable")]
    /// [`Event::PLAYER_DIED`].
    ///
    /// Unstable: needs the `unstable` feature and may change in any release.
    PlayerDied = PLAYER_DIED: PlayerDeathData, core::mem::offset_of!(PlayerDeathData, killer_entity);
}

impl Ticked {
    /// How often a tick handler runs unless it asks otherwise: about every
    /// second step of an empty dedicated server (20 steps a second, build
    /// 399), more steps apart when the server steps faster.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::time::Duration;
    ///
    /// assert_eq!(enshwick::events::Ticked::DEFAULT_EVERY, Duration::from_millis(100));
    /// ```
    pub const DEFAULT_EVERY: Duration = Duration::from_millis(100);
}

/// [`Ticked`] through [`Load::on`](crate::Load::on): on the default period.
fn subscribe_ticked<F>(load: &mut crate::Load<'_>, handler: F) -> Result<(), crate::Off>
where
    F: Fn(&TickData) + Send + Sync + 'static,
{
    load.on_tick(Ticked::DEFAULT_EVERY, handler).map(|_| ())
}

/// A tick handler's period, from [`Load::on_tick`](crate::Load::on_tick);
/// copy it anywhere, the handler itself included, to change the period
/// while the game runs.
///
/// # Examples
///
/// ```
/// use std::time::Duration;
///
/// fn init(load: &mut enshwick::Load<'_>) -> Result<(), enshwick::Off> {
///     let period = load.on_tick(Duration::from_secs(1), |_| enshwick::debug!("a second"))?;
///     period.set_every(Duration::from_millis(250))
/// }
/// ```
#[derive(Clone, Copy, Debug)]
pub struct TickPeriod {
    host: crate::Host,
    handle: TickHandle,
}

impl TickPeriod {
    pub(crate) const fn new(host: crate::Host, handle: TickHandle) -> Self {
        Self { host, handle }
    }

    /// Runs the handler about every `every` from the next step on: its next
    /// call comes `every` after its last, or on the next step when that time
    /// has passed. [`Duration::ZERO`] runs it on every step.
    ///
    /// # Errors
    ///
    /// [`Off`](crate::Off) when the loader refuses: this mod is off.
    ///
    /// # Examples
    ///
    /// See [`TickPeriod`].
    pub fn set_every(self, every: Duration) -> Result<(), crate::Off> {
        let status =
            (self.host.api().set_tick_every)(self.host.handle(), self.handle, nanos(every));
        crate::load::ok(status, (), || "the tick period".to_owned())
    }
}

/// `duration` in nanoseconds, the longest the ABI holds past that.
pub(crate) fn nanos(duration: Duration) -> u64 {
    u64::try_from(duration.as_nanos()).unwrap_or(u64::MAX)
}

/// Every event's script entry, for [`crate::script::events`].
pub(crate) const SCRIPT_EVENTS: [&[crate::script::EventKind]; 2] = [SCRIPT_PLAIN, SCRIPT_WITH_DATA];

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

    #[test]
    fn data_is_read_only_when_it_has_every_field_of_its_minor() {
        let player = PlayerData {
            header: Header::of::<PlayerData>(),
            slot: 2,
            entity: crate::Entity(9),
            key: crate::PlayerKey::new(1),
        };
        let raw = (&raw const player).cast::<c_void>();
        // SAFETY: `player` is a whole `PlayerData` on this stack frame,
        // readable for the size its header states.
        let joined = unsafe { PlayerJoined::data(raw) };
        assert_eq!(joined.map(|p| p.entity), Some(crate::Entity(9)));
        // SAFETY: `data` accepts a null pointer and reads nothing.
        assert!(unsafe { PlayerLeft::data(core::ptr::null()) }.is_none());
        let mut short = player;
        short.header.size = 8;
        // SAFETY: `short` is a whole `PlayerData` on this stack frame, its
        // header stating fewer bytes than it holds.
        assert!(unsafe { PlayerJoined::data((&raw const short).cast()) }.is_none());
        // SAFETY: `data` of an event without data reads nothing.
        assert!(unsafe { WorldSaved::data(core::ptr::null()) }.is_some());
    }

    #[test]
    #[cfg(feature = "unstable")]
    fn a_death_from_minor_60_reads_without_its_killer_entity() {
        let mut death = PlayerDeathData {
            header: Header::of::<PlayerDeathData>(),
            entity: crate::Entity(3),
            cause: PlayerDeathData::IMPACT,
            key: crate::PlayerKey::new(7),
            position: [0.0; 3],
            cause_guid: [1; 16],
            was_deadly_fog: 0,
            drowned: 0,
            reserved: 0,
            killer_entity: crate::Entity(9),
            reserved_tail: 0,
        };
        death.header.size = 56;
        // SAFETY: `death` is a whole `PlayerDeathData` on this stack frame,
        // its header stating fewer bytes than it holds.
        let read = unsafe { PlayerDied::data((&raw const death).cast()) }.unwrap();
        assert_eq!(
            (read.entity, read.killer_entity),
            (crate::Entity(3), crate::Entity(0))
        );
    }

    #[test]
    fn data_from_a_newer_loader_is_read() {
        #[repr(C)]
        struct Newer {
            player: PlayerData,
            appended: u64,
        }
        let mut newer = Newer {
            player: PlayerData {
                header: Header::of::<PlayerData>(),
                slot: 2,
                entity: crate::Entity(9),
                key: crate::PlayerKey::new(1),
            },
            appended: 5,
        };
        newer.player.header.size = u32::try_from(size_of::<Newer>()).unwrap();
        // SAFETY: `newer` is on this stack frame and starts with a whole
        // `PlayerData` whose header states the size of `Newer`, all of it
        // readable.
        let joined = unsafe { PlayerJoined::data((&raw const newer).cast()) }.unwrap();
        assert_eq!(
            (joined.entity, joined.key),
            (crate::Entity(9), crate::PlayerKey::new(1))
        );
        assert_eq!(newer.appended, 5);
    }
}