Skip to main content

unbanished_formats/savegame/
fields.rs

1//! An object's or block's values: the field list and its path lookups.
2
3use super::*;
4
5/// A decoded value.
6#[derive(Debug, Clone, PartialEq)]
7pub enum Value {
8    /// A bool.
9    Bool(bool),
10    /// An `i8`.
11    I8(i8),
12    /// An `i16`.
13    I16(i16),
14    /// A `u16`.
15    U16(u16),
16    /// An `i32`.
17    I32(i32),
18    /// A `u32`.
19    U32(u32),
20    /// An `i64`.
21    I64(i64),
22    /// An `f32`.
23    F32(f32),
24    /// A list of `f32`.
25    F32s(Vec<f32>),
26    /// A list of `i64`.
27    I64s(Vec<i64>),
28    /// A list of `u32`.
29    U32s(Vec<u32>),
30    /// A string.
31    Str(String),
32    /// An external reference id (`u32::MAX` for none).
33    Ext(u32),
34    /// An internal pointer to an object (none for null).
35    Ptr(Option<ObjId>),
36    /// A list of records.
37    List(Vec<Fields>),
38    /// A nested block: its version and fields.
39    Block(u32, Fields),
40}
41
42/// Named values in serialization order.
43#[derive(Debug, Clone, Default, PartialEq)]
44pub struct Fields(pub Vec<(&'static str, Value)>);
45
46impl Fields {
47    /// A field of this block by name.
48    pub fn get(&self, name: &str) -> Option<&Value> {
49        self.0.iter().find(|(n, _)| *n == name).map(|(_, v)| v)
50    }
51
52    /// A field inside nested blocks: `path` is `"block.field"`.
53    pub fn at(&self, path: &str) -> Option<&Value> {
54        let mut fields = self;
55        let mut parts = path.split('.').peekable();
56        while let Some(p) = parts.next() {
57            let v = fields.get(p)?;
58            if parts.peek().is_none() {
59                return Some(v);
60            }
61            match v {
62                Value::Block(_, f) => fields = f,
63                _ => return None,
64            }
65        }
66        None
67    }
68
69    /// [`Fields::at`] for writing.
70    pub fn at_mut(&mut self, path: &str) -> Option<&mut Value> {
71        let (head, rest) = match path.split_once('.') {
72            Some((h, r)) => (h, Some(r)),
73            None => (path, None),
74        };
75        let v = self
76            .0
77            .iter_mut()
78            .find(|(n, _)| *n == head)
79            .map(|(_, v)| v)?;
80        match rest {
81            None => Some(v),
82            Some(rest) => match v {
83                Value::Block(_, f) => f.at_mut(rest),
84                _ => None,
85            },
86        }
87    }
88
89    /// Replace the value at `path`, keeping its kind (an `I32` stays an `I32`, a `Bool` that
90    /// holds a raw byte takes the new truth value as 0 or 1 only if it changed). Returns whether
91    /// the field exists.
92    pub fn set(&mut self, path: &str, value: Value) -> bool {
93        let Some(slot) = self.at_mut(path) else {
94            return false;
95        };
96        // A raw non-0/1 bool byte stays as written while its truth value is unchanged.
97        if let (Value::I8(b), Value::Bool(v)) = (&*slot, &value) {
98            if (*b != 0) == *v {
99                return true;
100            }
101        }
102        *slot = value;
103        true
104    }
105
106    /// Set an `i32` field inside nested blocks (`"block.field"`). Returns whether it exists.
107    pub fn set_i32(&mut self, path: &str, v: i32) -> bool {
108        match self.at_mut(path) {
109            Some(Value::I32(x)) => *x = v,
110            Some(Value::U32(x)) | Some(Value::Ext(x)) => *x = v as u32,
111            Some(Value::I16(x)) => *x = v as i16,
112            Some(Value::I8(x)) => *x = v as i8,
113            Some(Value::U16(x)) => *x = v as u16,
114            _ => return false,
115        }
116        true
117    }
118
119    /// Set an `f32` field inside nested blocks. Returns whether it exists.
120    pub fn set_f32(&mut self, path: &str, v: f32) -> bool {
121        match self.at_mut(path) {
122            Some(Value::F32(x)) => *x = v,
123            _ => return false,
124        }
125        true
126    }
127
128    /// Set a time field (microseconds) inside nested blocks. Returns whether it exists.
129    pub fn set_time(&mut self, path: &str, v: i64) -> bool {
130        match self.at_mut(path) {
131            Some(Value::I64(x)) => *x = v,
132            _ => return false,
133        }
134        true
135    }
136
137    /// Set a `bool` field inside nested blocks. Returns whether it exists.
138    pub fn set_bool(&mut self, path: &str, v: bool) -> bool {
139        self.set(path, Value::Bool(v))
140    }
141
142    /// An `i32` field inside nested blocks.
143    pub fn i32(&self, path: &str) -> Option<i32> {
144        match self.at(path)? {
145            Value::I32(v) => Some(*v),
146            Value::I16(v) => Some(*v as i32),
147            Value::I8(v) => Some(*v as i32),
148            Value::U32(v) | Value::Ext(v) => Some(*v as i32),
149            _ => None,
150        }
151    }
152
153    /// An `f32` field inside nested blocks.
154    pub fn f32(&self, path: &str) -> Option<f32> {
155        match self.at(path)? {
156            Value::F32(v) => Some(*v),
157            _ => None,
158        }
159    }
160
161    /// An object pointer field inside nested blocks.
162    pub fn ptr(&self, path: &str) -> Option<ObjId> {
163        match self.at(path)? {
164            Value::Ptr(p) => *p,
165            _ => None,
166        }
167    }
168}