anvil-sdk 0.1.12

Safe Rust API for plugins of Anvil, the plugin loader for Europa Universalis IV
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
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
//! `anvil-game.game`: the game's main loop, sessions and date, and the events
//! anvil-game publishes on the game's main thread (`anvil_game.h`).
//!
//! Depend on anvil-game in plugin.toml (`[[dependencies]] id = "anvil-game"`)
//! and look the service up with [`Game::get`].

use std::ffi::c_void;
use std::fmt;

use crate::error::{check, Result};
use crate::event::{read_sized, Event, Payload};
use crate::host::Host;
use crate::sys::{
    AnvilGameDate, AnvilGameDateChange, AnvilGameFrame, AnvilGameOosChange, AnvilGamePendingChange,
    AnvilGamePlayerChange, AnvilGameService, AnvilGameSession, AnvilGameWarChange,
    ANVIL_GAME_PENDING_ADDED, ANVIL_GAME_PENDING_REMOVED, ANVIL_GAME_SERVICE_VERSION,
    ANVIL_GAME_SESSION_ENDED, ANVIL_GAME_SESSION_STARTED, ANVIL_GAME_WAR_ENDED,
    ANVIL_GAME_WAR_STARTED,
};
use crate::text::{owned_string, Tag};

/// The service's name.
pub const SERVICE: &std::ffi::CStr = c"anvil-game.game";
/// The service's major version.
pub const VERSION: u32 = ANVIL_GAME_SERVICE_VERSION;

/// How a session changed.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum SessionChange {
    /// A save or a new game is loaded and running.
    Started,
    /// The player left it (to the menu, or by loading another save).
    Ended,
    /// A value this crate does not know.
    Other(u32),
}

/// A game session: `anvil-game.session`'s payload and [`Game::session`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Session {
    /// Started or ended; an end carries the flags the session had.
    pub change: SessionChange,
    /// An ironman game.
    pub ironman: bool,
    /// A multiplayer game.
    pub multiplayer: bool,
    /// 1 for the first session since the game started, then 2, …
    pub number: u64,
}

impl Session {
    fn from_raw(raw: &AnvilGameSession) -> Self {
        let change = match raw.change {
            c if c == ANVIL_GAME_SESSION_STARTED as u32 => SessionChange::Started,
            c if c == ANVIL_GAME_SESSION_ENDED as u32 => SessionChange::Ended,
            other => SessionChange::Other(other),
        };
        Self {
            change,
            ironman: raw.ironman != 0,
            multiplayer: raw.multiplayer != 0,
            number: raw.number,
        }
    }
}

/// An in-game date, as the game's top bar shows it (years have 365 days).
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Date {
    /// E.g. 1444.
    pub year: i32,
    /// 1..12.
    pub month: u32,
    /// 1..31.
    pub day: u32,
    /// The [`Session::number`] it belongs to.
    pub session: u64,
}

impl Date {
    pub(crate) fn from_raw(raw: &AnvilGameDate) -> Self {
        Self {
            year: raw.year,
            month: raw.month,
            day: raw.day,
            session: raw.session,
        }
    }
}

impl fmt::Display for Date {
    /// `1444.11.11`, as the game writes dates.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}.{}.{}", self.year, self.month, self.day)
    }
}

/// `anvil-game.frame`'s payload.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Frame {
    /// The game's own frame counter after this frame.
    pub number: u64,
}

/// Out-of-sync flags (`ANVIL_GAME_OOS_*`).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct OosFlags(pub u32);

impl OosFlags {
    /// This game's checksums differed from the host's (a client only).
    pub const DETECTED: u32 = crate::sys::ANVIL_GAME_OOS_DETECTED;
    /// The game is paused because a machine went out of sync.
    pub const PAUSED: u32 = crate::sys::ANVIL_GAME_OOS_PAUSED;

    /// Whether the game is out of sync.
    pub const fn detected(self) -> bool {
        self.0 & Self::DETECTED != 0
    }
    /// Whether the game is paused for it.
    pub const fn paused(self) -> bool {
        self.0 & Self::PAUSED != 0
    }
}

/// Why the game is paused (`ANVIL_GAME_PAUSE_*`): one reason, the player's
/// pause while it is on (the game may hold one of its own as well), else the
/// game's.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct PauseFlags(pub u32);

impl PauseFlags {
    /// The player's pause, the one the space bar and the pause button toggle.
    pub const PLAYER: u32 = crate::sys::ANVIL_GAME_PAUSE_PLAYER;
    /// In single player, the game's own list of pauses is not empty and the
    /// player's pause is off.
    pub const GAME: u32 = crate::sys::ANVIL_GAME_PAUSE_GAME;

    /// Whether the player paused the game.
    pub const fn player(self) -> bool {
        self.0 & Self::PLAYER != 0
    }
    /// Whether the game holds a pause of its own.
    pub const fn game(self) -> bool {
        self.0 & Self::GAME != 0
    }
}

/// What of the game's GUI takes the keyboard (`ANVIL_GAME_KEYBOARD_*`).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct KeyboardFlags(pub u32);

impl KeyboardFlags {
    /// A text field of the game has focus.
    pub const TEXT: u32 = crate::sys::ANVIL_GAME_KEYBOARD_TEXT;
    /// The shortcut editor waits for a key.
    pub const SHORTCUT: u32 = crate::sys::ANVIL_GAME_KEYBOARD_SHORTCUT;

    /// Whether a text field has focus.
    pub const fn text(self) -> bool {
        self.0 & Self::TEXT != 0
    }
    /// Whether the shortcut editor waits.
    pub const fn shortcut(self) -> bool {
        self.0 & Self::SHORTCUT != 0
    }
}

/// `anvil-game.oos`'s payload.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct OosChange {
    /// The flags now.
    pub flags: OosFlags,
    /// The flags before.
    pub previous: OosFlags,
    /// The [`Session::number`].
    pub session: u64,
}

/// The payload of `anvil-game.day`, `.month` and `.year`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct DateChange {
    /// How many days the step covers: 1 in a single-player game, perhaps more
    /// on a multiplayer client.
    pub days: u32,
    /// The date now.
    pub date: Date,
    /// The date before.
    pub previous: Date,
}

/// `anvil-game.player`'s payload: the player's country changed within a session.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct PlayerChange {
    /// The country now; `None` for none.
    pub tag: Option<Tag>,
    /// The country before.
    pub previous: Option<Tag>,
    /// The [`Session::number`].
    pub session: u64,
}

/// How a war changed.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum WarChangeKind {
    /// The war started.
    Started,
    /// The war ended.
    Ended,
    /// A value this crate does not know.
    Other(u32),
}

/// `anvil-game.war`'s payload.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WarChange {
    /// Started or ended.
    pub change: WarChangeKind,
    /// The war's id (no longer valid for an end).
    pub war: u64,
    /// Who started it.
    pub attacker: Option<Tag>,
    /// Whom it was declared on.
    pub defender: Option<Tag>,
    /// The war's name as the game shows it.
    pub name: String,
    /// The [`Session::number`].
    pub session: u64,
}

/// How a pending event of the player changed (anvil-game 1.21.0).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PendingChangeKind {
    /// It came: the player can choose.
    Added,
    /// It left: chosen, by the player, a plugin or the game's auto-select.
    Removed,
    /// A value this crate does not know.
    Other(u32),
}

/// `anvil-game.pending_event`'s payload.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct PendingChange {
    /// Added or removed.
    pub change: PendingChangeKind,
    /// The pending id ([`World::pending_event`](crate::world::World::pending_event));
    /// no longer valid for a removal.
    pub pending_id: i32,
    /// The player's country.
    pub country: Option<Tag>,
    /// The [`Session::number`].
    pub session: u64,
}

// SAFETY: anvil-game.frame publishes a const AnvilGameFrame*.
unsafe impl Payload for Frame {
    unsafe fn read(raw: *const c_void) -> Option<Self> {
        // SAFETY: the event's payload type.
        unsafe { read_sized::<AnvilGameFrame>(raw) }.map(|f| Self { number: f.number })
    }
}

// SAFETY: anvil-game.session publishes a const AnvilGameSession*.
unsafe impl Payload for Session {
    unsafe fn read(raw: *const c_void) -> Option<Self> {
        // SAFETY: the event's payload type.
        unsafe { read_sized::<AnvilGameSession>(raw) }.map(|s| Self::from_raw(&s))
    }
}

// SAFETY: anvil-game.oos publishes a const AnvilGameOosChange*.
unsafe impl Payload for OosChange {
    unsafe fn read(raw: *const c_void) -> Option<Self> {
        // SAFETY: the event's payload type.
        unsafe { read_sized::<AnvilGameOosChange>(raw) }.map(|c| Self {
            flags: OosFlags(c.flags),
            previous: OosFlags(c.previous),
            session: c.session,
        })
    }
}

// SAFETY: anvil-game.day, .month and .year publish a const AnvilGameDateChange*.
unsafe impl Payload for DateChange {
    unsafe fn read(raw: *const c_void) -> Option<Self> {
        // SAFETY: the event's payload type.
        unsafe { read_sized::<AnvilGameDateChange>(raw) }.map(|c| Self {
            days: c.days,
            date: Date::from_raw(&c.date),
            previous: Date::from_raw(&c.previous),
        })
    }
}

// SAFETY: anvil-game.player publishes a const AnvilGamePlayerChange*.
unsafe impl Payload for PlayerChange {
    unsafe fn read(raw: *const c_void) -> Option<Self> {
        // SAFETY: the event's payload type.
        unsafe { read_sized::<AnvilGamePlayerChange>(raw) }.map(|c| Self {
            tag: Tag::from_raw(&c.tag),
            previous: Tag::from_raw(&c.previous),
            session: c.session,
        })
    }
}

// SAFETY: anvil-game.war publishes a const AnvilGameWarChange* with its name.
unsafe impl Payload for WarChange {
    unsafe fn read(raw: *const c_void) -> Option<Self> {
        // SAFETY: the event's payload type; a zeroed name (an older publisher) is null.
        let c = unsafe { read_sized::<AnvilGameWarChange>(raw) }?;
        let change = match c.change {
            k if k == ANVIL_GAME_WAR_STARTED as u32 => WarChangeKind::Started,
            k if k == ANVIL_GAME_WAR_ENDED as u32 => WarChangeKind::Ended,
            other => WarChangeKind::Other(other),
        };
        Some(Self {
            change,
            war: c.war,
            attacker: Tag::from_raw(&c.attacker),
            defender: Tag::from_raw(&c.defender),
            // SAFETY: the name is valid during the handler.
            name: unsafe { owned_string(c.name) },
            session: c.session,
        })
    }
}

// SAFETY: anvil-game.pending_event publishes a const AnvilGamePendingChange*.
unsafe impl Payload for PendingChange {
    unsafe fn read(raw: *const c_void) -> Option<Self> {
        // SAFETY: the event's payload type.
        let c = unsafe { read_sized::<AnvilGamePendingChange>(raw) }?;
        let change = match c.change {
            k if k == ANVIL_GAME_PENDING_ADDED as u32 => PendingChangeKind::Added,
            k if k == ANVIL_GAME_PENDING_REMOVED as u32 => PendingChangeKind::Removed,
            other => PendingChangeKind::Other(other),
        };
        Some(Self {
            change,
            pending_id: c.pending_id,
            country: Tag::from_raw(&c.country),
            session: c.session,
        })
    }
}

/// Once, when the game has finished initializing (the main menu is up).
// SAFETY: anvil_game.h names the payload type, which this one reads.
pub const READY: Event<()> = unsafe { Event::new(c"anvil-game.ready") };
/// After every frame of the main loop. Keep the handler short.
// SAFETY: anvil_game.h names the payload type, which this one reads.
pub const FRAME: Event<Frame> = unsafe { Event::new(c"anvil-game.frame") };
/// A session started or ended.
// SAFETY: anvil_game.h names the payload type, which this one reads.
pub const SESSION: Event<Session> = unsafe { Event::new(c"anvil-game.session") };
/// The main menu came up or went away (anvil-game 1.8.0); ask [`Game::main_menu`].
// SAFETY: anvil_game.h names the payload type, which this one reads.
pub const MAIN_MENU: Event<()> = unsafe { Event::new(c"anvil-game.main_menu") };
/// The out-of-sync flags changed (anvil-game 1.14.0).
// SAFETY: anvil_game.h names the payload type, which this one reads.
pub const OOS: Event<OosChange> = unsafe { Event::new(c"anvil-game.oos") };
/// The date moved (anvil-game 1.11.0).
// SAFETY: anvil_game.h names the payload type, which this one reads.
pub const DAY: Event<DateChange> = unsafe { Event::new(c"anvil-game.day") };
/// A new month began (anvil-game 1.11.0), after the game's monthly update.
// SAFETY: anvil_game.h names the payload type, which this one reads.
pub const MONTH: Event<DateChange> = unsafe { Event::new(c"anvil-game.month") };
/// A new year began (anvil-game 1.11.0), after the game's yearly update.
// SAFETY: anvil_game.h names the payload type, which this one reads.
pub const YEAR: Event<DateChange> = unsafe { Event::new(c"anvil-game.year") };
/// The player's country changed within a session (anvil-game 1.11.0).
// SAFETY: anvil_game.h names the payload type, which this one reads.
pub const PLAYER: Event<PlayerChange> = unsafe { Event::new(c"anvil-game.player") };
/// A war started or ended (anvil-game 1.11.0).
// SAFETY: anvil_game.h names the payload type, which this one reads.
pub const WAR: Event<WarChange> = unsafe { Event::new(c"anvil-game.war") };
/// A pending event of the player's own country came or left (anvil-game 1.21.0).
// SAFETY: anvil_game.h names the payload type, which this one reads.
pub const PENDING_EVENT: Event<PendingChange> = unsafe { Event::new(c"anvil-game.pending_event") };

/// The `anvil-game.game` service.
#[derive(Clone, Copy)]
pub struct Game {
    table: &'static AnvilGameService,
}

impl fmt::Debug for Game {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Game")
            .field("size", &self.table.size)
            .finish()
    }
}

struct Work<F> {
    work: F,
}

unsafe extern "C" fn run_work<F: FnOnce() + Send + 'static>(context: *mut c_void) {
    // SAFETY: the box run_on_main_thread queued, run once.
    let work = unsafe { Box::from_raw(context.cast::<Work<F>>()) };
    let logged = std::sync::atomic::AtomicBool::new(false);
    crate::panic::run_callback("run_on_main_thread work", &logged, (), move || {
        (work.work)()
    });
}

impl Game {
    /// The service; `None` without anvil-game.
    pub fn get(host: &Host) -> Option<Self> {
        // SAFETY: the table type of anvil-game.game version 1.
        let table = unsafe { host.service::<AnvilGameService>(SERVICE, VERSION) }?;
        Some(Self { table })
    }

    /// The C table, for what this crate does not wrap.
    pub fn c_api(&self) -> &'static AnvilGameService {
        self.table
    }

    /// Whether `anvil-game.ready` was published.
    pub fn ready(&self) -> bool {
        // SAFETY: the service's function.
        function!(self.table, AnvilGameService, ready).is_ok_and(|f| unsafe { f() } != 0)
    }

    /// The game's main thread; 0 before its first frame.
    pub fn main_thread_id(&self) -> u32 {
        // SAFETY: as above.
        function!(self.table, AnvilGameService, main_thread_id).map_or(0, |f| unsafe { f() })
    }

    /// The frame number of the last `anvil-game.frame`; 0 before it.
    pub fn frame_number(&self) -> u64 {
        // SAFETY: as above.
        function!(self.table, AnvilGameService, frame_number).map_or(0, |f| unsafe { f() })
    }

    /// Queues `work` to run on the main thread after the next frame of the
    /// main loop, in the order queued. Callable from any thread. Work still
    /// queued when the game quits never runs, and frames stop while a save
    /// loads: never wait for it without a limit.
    pub fn run_on_main_thread(&self, work: impl FnOnce() + Send + 'static) -> Result<()> {
        self.queue(work)
    }

    fn queue<F: FnOnce() + Send + 'static>(&self, work: F) -> Result<()> {
        let call = function!(self.table, AnvilGameService, run_on_main_thread)?;
        let context = Box::into_raw(Box::new(Work { work }));
        // SAFETY: the adapter never unwinds and frees the box when it runs.
        let result = unsafe { call(Some(run_work::<F>), context.cast()) };
        check(result, || "run_on_main_thread").inspect_err(|_| {
            // SAFETY: refused, so it never runs: the box is still ours.
            drop(unsafe { Box::from_raw(context) });
        })
    }

    /// The running session, as of the last frame of the main loop.
    pub fn session(&self) -> Option<Session> {
        let f = function!(self.table, AnvilGameService, session).ok()?;
        // SAFETY: zeroed plain data with its size set, filled by the service.
        let mut raw: AnvilGameSession = unsafe { std::mem::zeroed() };
        raw.size = std::mem::size_of::<AnvilGameSession>() as u32;
        // SAFETY: the service's function with a valid out pointer.
        (unsafe { f(&mut raw) } != 0).then(|| Session::from_raw(&raw))
    }

    /// The current date while a session runs (anvil-game 1.6.0); `None` in
    /// the menu, while a save loads from it, or with an older anvil-game.
    pub fn date(&self) -> Option<Date> {
        let f = function!(self.table, AnvilGameService, date).ok()?;
        // SAFETY: as in session().
        let mut raw: AnvilGameDate = unsafe { std::mem::zeroed() };
        raw.size = std::mem::size_of::<AnvilGameDate>() as u32;
        // SAFETY: as in session().
        (unsafe { f(&mut raw) } != 0).then(|| Date::from_raw(&raw))
    }

    /// Whether the main menu is up (anvil-game 1.8.0); `None` with an older one.
    pub fn main_menu(&self) -> Option<bool> {
        // SAFETY: the service's function.
        function!(self.table, AnvilGameService, main_menu)
            .ok()
            .map(|f| unsafe { f() } != 0)
    }

    /// The session's out-of-sync flags (anvil-game 1.14.0); `None` with an older one.
    pub fn out_of_sync(&self) -> Option<OosFlags> {
        // SAFETY: the service's function.
        function!(self.table, AnvilGameService, out_of_sync)
            .ok()
            .map(|f| OosFlags(unsafe { f() }))
    }

    /// Whether the game's GUI takes the keyboard for itself (anvil-game
    /// 1.18.0), read live: ask on the main thread. `None` with an older
    /// anvil-game or on a build that declares its symbols absent.
    pub fn keyboard(&self) -> Option<KeyboardFlags> {
        let f = function!(self.table, AnvilGameService, keyboard).ok()?;
        let mut flags = 0u32;
        // SAFETY: the service's function with a valid out pointer.
        (unsafe { f(&mut flags) } != 0).then_some(KeyboardFlags(flags))
    }

    /// Asks the game for its own restart (anvil-game 1.23.0, Anvil's ADR 0012),
    /// on the player's request only. `close_campaign` is the caller's word that
    /// the player accepted closing a running single-player campaign (ironman
    /// is saved, anything else loses its progress since the last save).
    /// `WrongPhase` before the game is ready, in a multiplayer session, or in
    /// a campaign without `close_campaign`; `Unsupported` with an older
    /// anvil-game or a build without its symbols. `host` is this plugin's, for
    /// the log.
    pub fn restart(&self, host: &Host, close_campaign: bool) -> Result<()> {
        let f = function!(self.table, AnvilGameService, restart)?;
        let flags = if close_campaign {
            crate::sys::ANVIL_GAME_RESTART_CLOSE_CAMPAIGN
        } else {
            0
        };
        // SAFETY: the service's function with this plugin's handle.
        check(unsafe { f(host.raw_plugin(), flags) }, || "restart")
    }

    /// The game speed as the speed bar shows it, 1 to 5, as of the last frame
    /// (anvil-game 1.24.0); it keeps its value while paused. `None` without a
    /// session, with an older anvil-game or on a build without its symbols.
    pub fn speed(&self) -> Option<i32> {
        let f = function!(self.table, AnvilGameService, speed).ok()?;
        let mut speed = 0i32;
        // SAFETY: the service's function with a valid out pointer.
        (unsafe { f(&mut speed) } != 0).then_some(speed)
    }

    /// Why the game is paused, as of the last frame (anvil-game 1.24.0):
    /// `Some` while a session runs and is paused, `None` while it runs on,
    /// without a session, with an older anvil-game or on a build without its
    /// symbols. A pause for going out of sync is [`Game::out_of_sync`]'s.
    pub fn paused(&self) -> Option<PauseFlags> {
        let f = function!(self.table, AnvilGameService, paused).ok()?;
        let mut flags = 0u32;
        // SAFETY: the service's function with a valid out pointer.
        (unsafe { f(&mut flags) } != 0).then_some(PauseFlags(flags))
    }

    /// The game's own write folder, where it keeps `save games`, `logs` and
    /// `settings.txt` (by default `Documents\Paradox Interactive\Europa
    /// Universalis IV`, or what `-userdir` and `userdir.txt` choose). `None`
    /// until the game set it at start-up (it has by `anvil-game.ready`), with
    /// an older anvil-game or on a build without its symbol. Any thread.
    pub fn user_dir(&self) -> Option<std::path::PathBuf> {
        let f = function!(self.table, AnvilGameService, user_dir).ok()?;
        // SAFETY: the service's function; it answers NULL or a string anvil-game keeps.
        let dir = unsafe { f() };
        (!dir.is_null()).then(|| crate::host::wide_path(dir))
    }
}