anvil-sdk 0.4.0

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
//! `anvil-map.modes`: map modes of the plugin's own, each a button on the
//! minimap's list, a colour for each province and lines in the province's
//! tooltip (`anvil_map.h`).
//!
//! Depend on anvil-map in plugin.toml (`[[dependencies]] id = "anvil-map"`,
//! `version = "^1"`) and look the service up with [`MapModes::get`]. A mode
//! changes nothing in the game's state, so it needs no
//! `affects_gameplay = true`.
//!
//! The game paints the map and builds tooltips also in frames drawn inside a
//! game update, where [`World`](crate::world::World) may not be read. So the
//! plugin computes its colours in a step of its own ([`SHOWN`], a
//! [`game`](crate::game) event) and hands them over with
//! [`MapModes::set_colors`]; the map shows the last buffer. Text is UTF-8;
//! anvil-map writes it in the game's encoding and shows it as written.

use std::ffi::{c_char, c_void, CStr};
use std::fmt;
use std::ptr::{self, NonNull};
use std::sync::atomic::AtomicBool;

use crate::error::{check, Error, ErrorCode, Result};
use crate::event::{read_sized, Event, Payload};
use crate::host::Host;
use crate::panic::run_callback;
use crate::sys::{
    AnvilMapLegendEntry, AnvilMapMode, AnvilMapModeChange, AnvilMapModeInfo, AnvilMapModesService,
    ANVIL_MAP_MODES_SERVICE_VERSION, ANVIL_MAP_NO_COLOR, ANVIL_MAP_TAB_1, ANVIL_MAP_TAB_2,
    ANVIL_MAP_TAB_3, ANVIL_MAP_TAB_4, ANVIL_MAP_TAB_DEFAULT,
};
use crate::table::Table;
use crate::text::{c_string, static_str};

/// The service's name.
pub const SERVICE: &CStr = c"anvil-map.modes";
/// The service's major version.
pub const VERSION: u32 = ANVIL_MAP_MODES_SERVICE_VERSION;

/// The colour that leaves a province as the political map mode shows it
/// (sea, wasteland and empty land look as players know them).
pub const NO_COLOR: u32 = ANVIL_MAP_NO_COLOR;

/// The most entries a legend holds ([`MapModes::set_legend`]).
pub const LEGEND_MAX: usize = crate::sys::ANVIL_MAP_LEGEND_MAX as usize;

/// A colour, `0x00RRGGBB`.
pub const fn rgb(r: u8, g: u8, b: u8) -> u32 {
    ((r as u32) << 16) | ((g as u32) << 8) | b as u32
}

/// The tab of the minimap's list where the mode's button goes, named by the
/// game's modes it starts with. The buttons of the tabs stand two by two:
/// political and diplomacy above, trade and terrain below.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum Tab {
    /// anvil-map's choice: [`Tab::Diplomacy`].
    #[default]
    Default,
    /// Tab 1, from the political mode.
    Political,
    /// Tab 2, from the terrain mode.
    Terrain,
    /// Tab 3, from the trade mode.
    Trade,
    /// Tab 4, from the diplomacy mode.
    Diplomacy,
}

impl Tab {
    const fn raw(self) -> u32 {
        (match self {
            Self::Default => ANVIL_MAP_TAB_DEFAULT,
            Self::Political => ANVIL_MAP_TAB_1,
            Self::Terrain => ANVIL_MAP_TAB_2,
            Self::Trade => ANVIL_MAP_TAB_3,
            Self::Diplomacy => ANVIL_MAP_TAB_4,
        }) as u32
    }
}

/// What [`MapModes::register`] takes. The strings are copied.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ModeInfo<'a> {
    /// The tab.
    pub tab: Tab,
    /// `<plugin-id>.<name>`: the plugin's own id, `.`, then `[a-z0-9_-]{1,32}`.
    pub id: &'a str,
    /// The button's name, 1 to 64 bytes.
    pub label: &'a str,
    /// The button's tooltip, at most 512 bytes.
    pub description: Option<&'a str>,
    /// A sprite (`GFX_...`) of the game or of the plugin's mod; `None`: an
    /// empty button frame.
    pub sprite: Option<&'a str>,
}

impl<'a> ModeInfo<'a> {
    /// A mode on the default tab with no description and no sprite.
    pub const fn new(id: &'a str, label: &'a str) -> Self {
        Self {
            tab: Tab::Default,
            id,
            label,
            description: None,
            sprite: None,
        }
    }
}

/// A registered mode; anvil-map keeps it until the process ends.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Mode {
    raw: NonNull<AnvilMapMode>,
}

// SAFETY: a mode is never freed, and every service call may come from any
// thread (anvil_map.h).
unsafe impl Send for Mode {}
// SAFETY: as above.
unsafe impl Sync for Mode {}

impl fmt::Debug for Mode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("Mode").field(&self.raw).finish()
    }
}

impl Mode {
    fn from_raw(raw: *const AnvilMapMode) -> Option<Self> {
        NonNull::new(raw.cast_mut()).map(|raw| Self { raw })
    }

    /// The raw mode, for the C table.
    pub fn as_raw(&self) -> *const AnvilMapMode {
        self.raw.as_ptr()
    }
}

/// The payload of [`SHOWN`] and [`HIDDEN`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ModeChange {
    /// The mode.
    pub mode: Option<Mode>,
    /// Its id.
    pub id: Option<&'static str>,
}

// SAFETY: anvil-map publishes both events with an AnvilMapModeChange whose
// id lives for the process.
unsafe impl Payload for ModeChange {
    unsafe fn read(raw: *const c_void) -> Option<Self> {
        // SAFETY: the payload of anvil-map.shown or anvil-map.hidden.
        let change = unsafe { read_sized::<AnvilMapModeChange>(raw) }?;
        Some(Self {
            mode: Mode::from_raw(change.mode),
            // SAFETY: the mode's id, kept with the mode.
            id: unsafe { static_str(change.id) },
        })
    }
}

/// `anvil-map.shown`: a plugin mode became the map mode shown. On the main
/// thread after a frame of the main loop, where `World` may be read.
// SAFETY: anvil-map publishes it with an AnvilMapModeChange.
pub const SHOWN: Event<ModeChange> = unsafe { Event::new(c"anvil-map.shown") };
/// `anvil-map.hidden`: a plugin mode stopped being the map mode shown (also at
/// the end of a session). Published before `shown` of the next one.
// SAFETY: as above.
pub const HIDDEN: Event<ModeChange> = unsafe { Event::new(c"anvil-map.hidden") };

/// The `anvil-map.modes` service.
#[derive(Clone, Copy)]
pub struct MapModes {
    table: Table<AnvilMapModesService>,
}

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

struct DescribeAdapter<F> {
    handler: F,
    panicked: AtomicBool,
}

/// `lines` into the C buffer: at most `capacity - 1` bytes, cut at a whole
/// character and at a NUL, then a terminating zero. Returns the length.
///
/// # Safety
/// `text` has room for `capacity` bytes, `capacity > 0`.
unsafe fn write_lines(lines: &str, text: *mut c_char, capacity: usize) -> usize {
    let end = lines.find('\0').unwrap_or(lines.len());
    let mut length = end.min(capacity - 1);
    while !lines.is_char_boundary(length) {
        length -= 1;
    }
    // SAFETY: `length` < `capacity` bytes into the caller's buffer.
    unsafe {
        ptr::copy_nonoverlapping(lines.as_ptr(), text.cast::<u8>(), length);
        *text.add(length) = 0;
    }
    length
}

unsafe extern "C" fn describe_adapter<F>(
    context: *mut c_void,
    mode: *const AnvilMapMode,
    province: i32,
    text: *mut c_char,
    capacity: usize,
) -> usize
where
    F: Fn(Mode, i32) -> Option<String> + Send + Sync + 'static,
{
    // SAFETY: the adapter register_with_tooltip leaked, never freed.
    let adapter = unsafe { &*context.cast::<DescribeAdapter<F>>() };
    let Some(mode) = Mode::from_raw(mode) else {
        return 0;
    };
    if text.is_null() || capacity == 0 {
        return 0;
    }
    run_callback("map mode tooltip", &adapter.panicked, 0, || {
        match (adapter.handler)(mode, province) {
            // SAFETY: anvil-map's buffer of `capacity` bytes.
            Some(lines) => unsafe { write_lines(&lines, text, capacity) },
            None => 0,
        }
    })
}

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

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

    /// Registers a mode with no lines of its own in the tooltip, which then
    /// shows the province's name and owner only. A pool of 13 numbers is lent
    /// in the order modes register: a fourteenth is `Unsupported`, and so is
    /// any mode when another loader of map modes runs. A taken id is
    /// `AlreadyExists`; a bad id, label, description or sprite is
    /// `InvalidArgument`.
    pub fn register(&self, info: &ModeInfo<'_>) -> Result<Mode> {
        self.register_raw(info, None, ptr::null_mut())
    }

    /// Registers a mode whose tooltip lines `describe` gives: for a province
    /// ID, the lines (separated by `\n`), or `None` for none. It runs on the
    /// main thread after a frame of the main loop, where `World` may be read,
    /// for the province under the cursor while the mode is shown; keep it
    /// short. Text past the buffer anvil-map gives is cut at a whole
    /// character.
    pub fn register_with_tooltip<F>(&self, info: &ModeInfo<'_>, describe: F) -> Result<Mode>
    where
        F: Fn(Mode, i32) -> Option<String> + Send + Sync + 'static,
    {
        // Never freed: anvil-map keeps the mode, and may call it, for the process.
        let adapter: &'static DescribeAdapter<F> = Box::leak(Box::new(DescribeAdapter {
            handler: describe,
            panicked: AtomicBool::new(false),
        }));
        self.register_raw(
            info,
            Some(describe_adapter::<F>),
            ptr::from_ref(adapter).cast_mut().cast(),
        )
    }

    fn register_raw(
        &self,
        info: &ModeInfo<'_>,
        describe: crate::sys::AnvilMapDescribe,
        context: *mut c_void,
    ) -> Result<Mode> {
        let f = function!(self.table, AnvilMapModesService, register_mode)?;
        let id = c_string(info.id);
        let label = c_string(info.label);
        let description = info.description.map(c_string);
        let sprite = info.sprite.map(c_string);
        let raw_info = AnvilMapModeInfo {
            size: std::mem::size_of::<AnvilMapModeInfo>() as u32,
            tab: info.tab.raw(),
            id: id.as_ptr(),
            label: label.as_ptr(),
            description: description
                .as_ref()
                .map_or(ptr::null(), |text| text.as_ptr()),
            sprite: sprite.as_ref().map_or(ptr::null(), |text| text.as_ptr()),
            describe,
            context,
        };
        let mut out: *const AnvilMapMode = ptr::null();
        // SAFETY: an info whose strings live until the call returns, and an
        // out pointer; anvil-map copies the strings.
        check(unsafe { f(&raw_info, &mut out) }, || {
            format!("map mode register {:?}", info.id)
        })?;
        Mode::from_raw(out).ok_or_else(|| {
            Error::new(
                ErrorCode::Internal,
                format!("map mode register {:?}: no mode", info.id),
            )
        })
    }

    /// Hands over the colours of `mode`: `colors[i]` for the province with
    /// ID `i` (see [`rgb`] and [`NO_COLOR`]); provinces past the slice and
    /// province 0 keep the political colour. The slice is copied. Only while
    /// a session runs (`WrongPhase` otherwise, also when it ends during the
    /// call: the slice is dropped); more entries than the
    /// session's province limit, or a colour with a top byte other than 0 or
    /// 0xFF, is `InvalidArgument`.
    pub fn set_colors(&self, mode: Mode, colors: &[u32]) -> Result<()> {
        let f = function!(self.table, AnvilMapModesService, set_colors)?;
        let count = u32::try_from(colors.len()).map_err(|_| {
            Error::new(
                ErrorCode::InvalidArgument,
                "map mode set_colors: too many colours",
            )
        })?;
        // SAFETY: the mode and `count` colours valid during the call.
        check(unsafe { f(mode.as_raw(), colors.as_ptr(), count) }, || {
            "map mode set_colors"
        })
    }

    /// The id of the plugin mode that borrowed `number`, a value of the
    /// game's map mode enum; `None` for the game's own modes.
    pub fn mode_id(&self, number: i32) -> Option<&'static str> {
        let f = function!(self.table, AnvilMapModesService, mode_id).ok()?;
        // SAFETY: the service's function; the id lives for the process.
        unsafe { static_str(f(number)) }
    }

    /// The plugin mode shown on the map as of anvil-map's last step.
    pub fn shown(&self) -> Option<Mode> {
        let f = function!(self.table, AnvilMapModesService, shown).ok()?;
        // SAFETY: the service's function.
        Mode::from_raw(unsafe { f() })
    }

    /// The id of `mode`.
    pub fn id(&self, mode: Mode) -> Option<&'static str> {
        let f = function!(self.table, AnvilMapModesService, id).ok()?;
        // SAFETY: the service's function with its mode; the id lives for the process.
        unsafe { static_str(f(mode.as_raw())) }
    }

    /// Shows `mode` on the map, as its button does: anvil-map sets it in its
    /// next step, and `anvil-map.shown` follows there. Any thread while a
    /// session runs (`WrongPhase` otherwise); `Unsupported` with an older
    /// anvil-map or without its hooks.
    pub fn show(&self, mode: Mode) -> Result<()> {
        let f = function!(self.table, AnvilMapModesService, show)?;
        // SAFETY: the service's function with its mode.
        check(unsafe { f(mode.as_raw()) }, || "map mode show")
    }

    /// Gives `mode` a legend: each colour (see [`rgb`]) and what it means,
    /// in order, at most [`LEGEND_MAX`]. anvil-map shows it in a window of
    /// Anvil's overlay while the mode is shown; an empty slice removes it.
    /// Any thread, any time; it lasts across sessions. `InvalidArgument` for
    /// too many entries, [`NO_COLOR`], or a label that is empty or longer
    /// than 64 bytes; `Unsupported` with an anvil-map before 1.26.0.
    pub fn set_legend(&self, mode: Mode, entries: &[(u32, &str)]) -> Result<()> {
        let f = function!(self.table, AnvilMapModesService, set_legend)?;
        let count = u32::try_from(entries.len()).map_err(|_| {
            Error::new(
                ErrorCode::InvalidArgument,
                "map mode set_legend: too many entries",
            )
        })?;
        // A NUL inside a label is dropped, as in every text of the SDK.
        let labels: Vec<_> = entries.iter().map(|(_, label)| c_string(label)).collect();
        let raw: Vec<AnvilMapLegendEntry> = entries
            .iter()
            .zip(&labels)
            .map(|((color, _), label)| AnvilMapLegendEntry {
                color: *color,
                reserved: 0,
                label: label.as_ptr(),
            })
            .collect();
        // SAFETY: `count` entries whose labels live until the call returns;
        // anvil-map copies them.
        check(
            unsafe {
                f(
                    mode.as_raw(),
                    raw.as_ptr(),
                    count,
                    std::mem::size_of::<AnvilMapLegendEntry>() as u32,
                )
            },
            || "map mode set_legend",
        )
    }
}