boon-deadlock 0.6.0

Boon is a Deadlock demo / replay file parser
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
use std::collections::HashMap;

use crate::error::{Error, Result};
use crate::io::BitReader;

use super::class_info::ClassInfo;

use boon_proto::proto::{CDemoStringTables, CsvcMsgCreateStringTable, CsvcMsgUpdateStringTable};

// String table delta encoding uses a circular buffer of recently-seen
// strings. New strings can reference a history entry by index and copy a
// prefix, then append the remainder. These constants control the buffer.
const HISTORY_SIZE: usize = 32;
const HISTORY_BITMASK: usize = HISTORY_SIZE - 1;

/// Maximum length (in characters) of a string table key.
const MAX_STRING_BITS: usize = 5;
const MAX_STRING_SIZE: usize = 1 << MAX_STRING_BITS;

/// Maximum size (in bytes) of per-entry user data.
const MAX_USERDATA_BITS: usize = 17;
const MAX_USERDATA_SIZE: usize = 1 << MAX_USERDATA_BITS;

/// The `instancebaseline` table stores default field values for each entity class.
pub const INSTANCE_BASELINE_TABLE_NAME: &str = "instancebaseline";

/// A single entry in a string table.
#[derive(Debug, Clone)]
pub struct StringTableEntry {
    pub string: Option<String>,
    pub user_data: Option<Vec<u8>>,
}

/// A string table.
#[derive(Debug)]
pub struct StringTable {
    pub name: String,
    user_data_fixed_size: bool,
    user_data_size: i32,
    user_data_size_bits: i32,
    flags: i32,
    using_varint_bitcounts: bool,
    pub entries: Vec<StringTableEntry>,
    /// Entry indices written since the last [`StringTableContainer::clear_dirty`]
    /// (i.e. since the previous per-tick callback). Lets consumers decode only
    /// what changed this tick instead of rescanning the whole table — see
    /// [`StringTable::dirty_indices`]. May contain duplicates if an index is
    /// touched more than once between callbacks.
    dirty: Vec<usize>,
}

impl StringTable {
    fn new(
        name: &str,
        user_data_fixed_size: bool,
        user_data_size: i32,
        user_data_size_bits: i32,
        flags: i32,
        using_varint_bitcounts: bool,
    ) -> Self {
        Self {
            name: name.to_string(),
            user_data_fixed_size,
            user_data_size,
            user_data_size_bits,
            flags,
            using_varint_bitcounts,
            entries: Vec::new(),
            dirty: Vec::new(),
        }
    }

    /// Entry indices written since the last per-tick callback.
    ///
    /// A consumer that maintains its own per-index state can react to only
    /// these entries rather than iterating (and decoding) every entry every
    /// tick. The list is cleared by the parser after each callback via
    /// [`StringTableContainer::clear_dirty`]. Indices may repeat.
    pub fn dirty_indices(&self) -> &[usize] {
        &self.dirty
    }

    /// Parse a string table update from a bit reader.
    pub fn parse_update(&mut self, br: &mut BitReader, num_entries: i32) -> Result<()> {
        let mut entry_index: i32 = -1;
        let mut history: Vec<[u8; MAX_STRING_SIZE]> = vec![[0u8; MAX_STRING_SIZE]; HISTORY_SIZE];
        let mut history_delta_index: usize = 0;
        let mut string_buf = vec![0u8; 1024];
        let mut user_data_buf = vec![0u8; MAX_USERDATA_SIZE];
        let mut user_data_uncompressed_buf = vec![0u8; MAX_USERDATA_SIZE];

        for _ in 0..num_entries as usize {
            // Read index
            entry_index = if br.read_bool()? {
                entry_index + 1
            } else {
                br.read_uvarint32()? as i32 + 1
            };

            // Read string
            let has_string = br.read_bool()?;
            let string = if has_string {
                let mut size: usize = 0;

                if br.read_bool()? {
                    // Uses history reference
                    let mut history_delta_zero = 0;
                    if history_delta_index > HISTORY_SIZE {
                        history_delta_zero = history_delta_index & HISTORY_BITMASK;
                    }

                    let index = (history_delta_zero + br.read_bits(5)? as usize) & HISTORY_BITMASK;
                    let bytes_to_copy = br.read_bits(MAX_STRING_BITS)? as usize;
                    size += bytes_to_copy;

                    string_buf[..bytes_to_copy].copy_from_slice(&history[index][..bytes_to_copy]);
                    size += br.read_string_into(&mut string_buf[bytes_to_copy..])?;
                } else {
                    size += br.read_string_into(&mut string_buf)?;
                }

                // Update history
                let mut she = [0u8; MAX_STRING_SIZE];
                let copy_len = size.min(MAX_STRING_SIZE);
                she[..copy_len].copy_from_slice(&string_buf[..copy_len]);
                history[history_delta_index & HISTORY_BITMASK] = she;
                history_delta_index += 1;

                Some(String::from_utf8_lossy(&string_buf[..size]).into_owned())
            } else {
                None
            };

            // Read user data
            let has_user_data = br.read_bool()?;
            let user_data = if has_user_data {
                if self.user_data_fixed_size {
                    let size = self.user_data_size as usize;
                    let size_bits = self.user_data_size_bits as usize;
                    if size > user_data_buf.len() || size_bits.div_ceil(8) > user_data_buf.len() {
                        return Err(Error::Parse {
                            context: format!(
                                "string-table fixed user_data size ({size} bytes / {size_bits} bits) exceeds {} bytes",
                                user_data_buf.len()
                            ),
                        });
                    }
                    br.read_bits_to_bytes(&mut user_data_buf, size_bits)?;
                    Some(user_data_buf[..size].to_vec())
                } else {
                    let mut is_compressed = false;
                    if (self.flags & 0x1) != 0 {
                        is_compressed = br.read_bool()?;
                    }

                    let size = if self.using_varint_bitcounts {
                        br.read_ubitvar()? as usize
                    } else {
                        br.read_bits(MAX_USERDATA_BITS)? as usize
                    };
                    if size > user_data_buf.len() {
                        return Err(Error::Parse {
                            context: format!(
                                "string-table user_data size {size} exceeds {} bytes",
                                user_data_buf.len()
                            ),
                        });
                    }

                    br.read_bytes(&mut user_data_buf[..size])?;

                    if is_compressed {
                        let decomp_len = snap::raw::decompress_len(&user_data_buf[..size])
                            .map_err(|e| Error::Decompress(e.to_string()))?;
                        user_data_uncompressed_buf.resize(decomp_len, 0);
                        snap::raw::Decoder::new()
                            .decompress(&user_data_buf[..size], &mut user_data_uncompressed_buf)
                            .map_err(|e| Error::Decompress(e.to_string()))?;
                        Some(user_data_uncompressed_buf[..decomp_len].to_vec())
                    } else {
                        Some(user_data_buf[..size].to_vec())
                    }
                }
            } else {
                None
            };

            // Insert or update
            let idx = entry_index as usize;
            if idx < self.entries.len() {
                if let Some(ud) = user_data {
                    self.entries[idx].user_data = Some(ud);
                }
                if let Some(s) = string {
                    self.entries[idx].string = Some(s);
                }
            } else {
                // Extend entries to reach idx
                while self.entries.len() < idx {
                    self.entries.push(StringTableEntry {
                        string: None,
                        user_data: None,
                    });
                }
                self.entries.push(StringTableEntry { string, user_data });
            }
            self.dirty.push(idx);
        }

        Ok(())
    }
}

/// Container for all string tables.
#[derive(Default)]
pub struct StringTableContainer {
    tables: Vec<StringTable>,
    /// Cached instance baselines: class_id -> baseline data.
    pub instance_baselines: HashMap<i32, Vec<u8>>,
}

impl StringTableContainer {
    pub fn new() -> Self {
        Self::default()
    }

    /// Handle CSVCMsg_CreateStringTable. Returns `true` if the created table
    /// is `instancebaseline` (caller should refresh baselines).
    pub fn handle_create(&mut self, msg: CsvcMsgCreateStringTable) -> Result<bool> {
        let name = msg.name.as_deref().unwrap_or("");
        let is_baseline = name == INSTANCE_BASELINE_TABLE_NAME;
        let mut table = StringTable::new(
            name,
            msg.user_data_fixed_size.unwrap_or(false),
            msg.user_data_size.unwrap_or(0),
            msg.user_data_size_bits.unwrap_or(0),
            msg.flags.unwrap_or(0),
            msg.using_varint_bitcounts.unwrap_or(false),
        );

        let string_data = if msg.data_compressed.unwrap_or(false) {
            let sd = msg.string_data.as_deref().unwrap_or(&[]);
            let decomp_len =
                snap::raw::decompress_len(sd).map_err(|e| Error::Decompress(e.to_string()))?;
            let mut buf = vec![0u8; decomp_len];
            snap::raw::Decoder::new()
                .decompress(sd, &mut buf)
                .map_err(|e| Error::Decompress(e.to_string()))?;
            buf
        } else {
            msg.string_data.unwrap_or_default()
        };

        let mut br = BitReader::new(&string_data);
        table.parse_update(&mut br, msg.num_entries.unwrap_or(0))?;

        self.tables.push(table);
        Ok(is_baseline)
    }

    /// Handle CSVCMsg_UpdateStringTable. Returns `true` if the updated table
    /// is `instancebaseline` (caller should refresh baselines).
    pub fn handle_update(&mut self, msg: CsvcMsgUpdateStringTable) -> Result<bool> {
        let table_id = msg.table_id.unwrap_or(0) as usize;
        if table_id >= self.tables.len() {
            return Err(Error::Parse {
                context: format!("string table update for non-existent table {}", table_id),
            });
        }

        let is_baseline = self.tables[table_id].name == INSTANCE_BASELINE_TABLE_NAME;

        let string_data = msg.string_data.unwrap_or_default();
        let mut br = BitReader::new(&string_data);
        self.tables[table_id].parse_update(&mut br, msg.num_changed_entries.unwrap_or(0))?;

        Ok(is_baseline)
    }

    /// Do a full update from CDemoStringTables (used in full packets).
    pub fn do_full_update(&mut self, cmd: CDemoStringTables) {
        for incoming in &cmd.tables {
            let table_name = incoming.table_name.as_deref().unwrap_or("");
            if let Some(table) = self.tables.iter_mut().find(|t| t.name == table_name) {
                for (i, item) in incoming.items.iter().enumerate() {
                    let entry = StringTableEntry {
                        string: item.str.clone(),
                        user_data: item.data.clone(),
                    };
                    if i < table.entries.len() {
                        if entry.user_data.is_some() {
                            table.entries[i].user_data = entry.user_data;
                            table.dirty.push(i);
                        }
                    } else {
                        while table.entries.len() < i {
                            table.entries.push(StringTableEntry {
                                string: None,
                                user_data: None,
                            });
                        }
                        table.entries.push(entry);
                        table.dirty.push(i);
                    }
                }
            }
        }
    }

    /// Clear every table's dirty-index list.
    ///
    /// The parser calls this after each per-tick callback so that
    /// [`StringTable::dirty_indices`] reflects only the entries written since
    /// the previous callback.
    pub fn clear_dirty(&mut self) {
        for table in &mut self.tables {
            table.dirty.clear();
        }
    }

    /// Update instance baselines from the instancebaseline string table.
    pub fn update_instance_baselines(&mut self, _class_info: &ClassInfo) {
        if let Some(table) = self
            .tables
            .iter()
            .find(|t| t.name == INSTANCE_BASELINE_TABLE_NAME)
        {
            for entry in &table.entries {
                if let (Some(s), Some(data)) = (&entry.string, &entry.user_data)
                    && let Ok(class_id) = s.parse::<i32>()
                {
                    // Only clone if new or changed
                    if self.instance_baselines.get(&class_id) != Some(data) {
                        self.instance_baselines.insert(class_id, data.clone());
                    }
                }
            }
        }
    }

    /// Look up a string table by name.
    pub fn find_table(&self, name: &str) -> Option<&StringTable> {
        self.tables.iter().find(|t| t.name == name)
    }

    /// Returns a slice of all string tables.
    pub fn tables(&self) -> &[StringTable] {
        &self.tables
    }
}

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

    #[test]
    fn container_new_is_empty() {
        let c = StringTableContainer::new();
        assert!(c.tables().is_empty());
        assert!(c.instance_baselines.is_empty());
    }

    #[test]
    fn find_table_missing() {
        let c = StringTableContainer::new();
        assert!(c.find_table("nonexistent").is_none());
    }

    #[test]
    fn update_instance_baselines_on_empty_is_noop() {
        let mut c = StringTableContainer::new();
        let ci = ClassInfo::empty();
        c.update_instance_baselines(&ci);
        assert!(c.instance_baselines.is_empty());
    }

    #[test]
    fn handle_update_invalid_table_id() {
        let mut c = StringTableContainer::new();
        let msg = CsvcMsgUpdateStringTable {
            table_id: Some(99),
            num_changed_entries: Some(0),
            string_data: None,
        };
        let result = c.handle_update(msg);
        assert!(result.is_err());
    }

    #[test]
    fn handle_create_empty_uncompressed() {
        let mut c = StringTableContainer::new();
        let msg = CsvcMsgCreateStringTable {
            name: Some("test".to_string()),
            num_entries: Some(0),
            user_data_fixed_size: Some(false),
            user_data_size: Some(0),
            user_data_size_bits: Some(0),
            flags: Some(0),
            string_data: Some(vec![]),
            data_compressed: Some(false),
            using_varint_bitcounts: Some(false),
            ..Default::default()
        };
        c.handle_create(msg).unwrap();
        assert_eq!(c.tables().len(), 1);
        assert!(c.find_table("test").is_some());
    }
}