Skip to main content

unbanished_formats/save/
events.rs

1//! The event log of a save: its entries and the reader of its block.
2
3use super::*;
4
5// Game +0x138
6/// One saved event-log entry (`Game`, 0x28 bytes in memory).
7#[derive(Debug, Clone, PartialEq)]
8pub struct SavedEvent {
9    /// The colour of the entry, `0xAARRGGBB`.
10    pub color: u32,
11    /// The text.
12    pub text: String,
13    /// The kind of event.
14    pub kind: i32,
15    /// How serious the event is.
16    pub severity: i32,
17    /// The map cell it happened at (-1 for none).
18    pub cell: i32,
19    /// The category the log filters by.
20    pub category: i32,
21    /// The icon.
22    pub icon: i32,
23}
24
25// +0x10..+0x15
26/// The event log block: six notify flags, then the entries.
27#[derive(Debug, Clone)]
28pub struct SavedEventLog {
29    /// Which categories show a notification.
30    pub notify: [bool; 6],
31    /// The entries, oldest first.
32    pub entries: Vec<SavedEvent>,
33}
34
35pub(super) fn skip_rest(r: &mut Reader) {
36    let rest = r.remaining();
37    let _ = r.bytes(rest);
38}
39
40pub(super) fn parse_event_block(b: &[u8]) -> Option<SavedEventLog> {
41    let u32_at = |o: usize| {
42        b.get(o..o + 4)
43            .map(|s| u32::from_le_bytes(s.try_into().unwrap()))
44    };
45    // `aaaaaaaa | size | body | bbbbbbbb`; the body starts with the version.
46    let size = u32_at(4)? as usize;
47    if size < 14 || u32_at(8 + size)? != 0xbbbb_bbbb || u32_at(8)? != 1 {
48        return None;
49    }
50    let body = &b[8..8 + size];
51    if body[4..10].iter().any(|&v| v > 1) {
52        return None;
53    }
54    let notify: [bool; 6] = std::array::from_fn(|i| body[4 + i] == 1);
55    let mut o = 10;
56    let rd = |o: &mut usize| -> Option<u32> {
57        let v = body.get(*o..*o + 4)?;
58        *o += 4;
59        Some(u32::from_le_bytes(v.try_into().unwrap()))
60    };
61    let count = rd(&mut o)? as usize;
62    if count > 100_000 {
63        return None;
64    }
65    let mut entries = Vec::with_capacity(count);
66    for _ in 0..count {
67        let color = rd(&mut o)?;
68        let len = rd(&mut o)?;
69        // The length counts the terminating NUL; without the 8-bit flag the text is UTF-16
70        // (2015 builds).
71        let text = if len & 0x8000_0000 != 0 {
72            let n = (len & 0x7fff_ffff) as usize;
73            let text = body.get(o..o + n.checked_sub(1)?)?;
74            o += n;
75            String::from_utf8_lossy(text).into_owned()
76        } else {
77            let n = len as usize;
78            let units: Vec<u16> = body
79                .get(o..o + 2 * n.checked_sub(1)?)?
80                .chunks_exact(2)
81                .map(|c| u16::from_le_bytes([c[0], c[1]]))
82                .collect();
83            o += 2 * n;
84            String::from_utf16_lossy(&units)
85        };
86        let mut i = || rd(&mut o).map(|v| v as i32);
87        entries.push(SavedEvent {
88            color,
89            text,
90            kind: i()?,
91            severity: i()?,
92            cell: i()?,
93            category: i()?,
94            icon: i()?,
95        });
96    }
97    (o == body.len()).then_some(SavedEventLog { notify, entries })
98}