bdat 0.6.0

(De)serialization library for Monolithsoft's BDAT file format
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
use std::borrow::Cow;
use std::collections::HashSet;
use std::str::Utf8Error;
use std::{
    convert::TryFrom,
    io::{Cursor, Read, Seek, SeekFrom},
    marker::PhantomData,
};

use byteorder::{ByteOrder, ReadBytesExt};

use crate::error::ReadError;
use crate::io::read::{BdatReader, BdatSlice};
use crate::io::BDAT_MAGIC;
use crate::legacy::float::BdatReal;
use crate::modern::{ModernColumn, ModernRow, ModernTable, ModernTableBuilder};
use crate::{
    error::{BdatError, Result},
    BdatFile, Label, Utf, Value, ValueType,
};

use super::FileHeader;

const LEN_COLUMN_DEF_V2: usize = 3;
const LEN_HASH_DEF_V2: usize = 8;

#[doc(hidden)]
pub struct FileReader<R, E> {
    tables: TableReader<R, E>,
    header: FileHeader,
    _endianness: PhantomData<E>,
}

struct TableData<'r> {
    table_offset: usize,
    data: Cow<'r, [u8]>,
    string_table_offset: usize,
}

pub trait ModernRead<'b> {
    /// Read a single 32-bit unsigned integer at the current position.
    fn read_u32(&mut self) -> Result<u32>;

    /// Get a slice (or buffer) to the limited binary stream from the current position.
    fn read_data(&mut self, length: usize) -> Result<Cow<'b, [u8]>>;

    /// Get a slice (or buffer) to the full binary stream for a single table.
    fn read_table_data(&mut self, length: usize) -> Result<Cow<'b, [u8]>>;

    /// Seek the current position to the next table at the given offset.
    fn seek_table(&mut self, offset: usize) -> Result<()>;

    /// Returns the current position of the cursor, relative to the start of the file.
    fn file_pos(&mut self) -> u64;
}

struct HeaderReader<R, E> {
    reader: R,
    _endianness: PhantomData<E>,
}

struct TableReader<R, E> {
    reader: R,
    _endianness: PhantomData<E>,
}

struct TableHeader {
    columns: usize,
    rows: usize,
    base_id: u32,
    offset_col: usize,
    offset_hash: usize,
    offset_row: usize,
    row_length: usize,
    offset_string: usize,
    str_length: usize,
}

impl<'b, R, E> FileReader<R, E>
where
    R: ModernRead<'b>,
    E: ByteOrder,
{
    pub(crate) fn read_file(mut reader: R) -> Result<Self> {
        let magic = reader.read_u32()?;
        if magic == u32::from_le_bytes(BDAT_MAGIC) {
            let version = reader.read_u32()?;
            if version != 0x01_00_10_04 {
                return Err(BdatError::new_read(
                    reader.file_pos() - 8,
                    ReadError::UnsupportedVersion(version),
                ));
            }
            Self::new_with_header(reader)
        } else {
            return Err(BdatError::new_read(
                reader.file_pos() - 4,
                ReadError::InvalidMagic(u32::to_le_bytes(magic)),
            ));
        }
    }

    /// Tries to extract unhashed names from the table, if the table contains some.
    ///
    /// This is the case for XCXDE message tables, and some tables in XC3 1.3.0.
    pub fn extract_hashes(&mut self) -> Result<Vec<Utf<'b>>> {
        let mut strings: HashSet<Utf<'b>> = HashSet::default();
        for i in 0..self.header.table_count {
            self.tables
                .reader
                .seek_table(self.header.table_offsets[i])?;
            let header = self.tables.read_header(self.header.table_offsets[i])?;
            if header.offset_col == 0x30 {
                // No debug string table
                continue;
            }
            // Debug string table between header and columns. 2 known sections: 1=row IDs, 2=table
            // and column names
            let mut total_size = header.offset_col.saturating_sub(0x30);
            for _ in 0..2 {
                if total_size == 0 {
                    break;
                }
                let ty = self.tables.reader.read_u32()?;
                let size = self.tables.reader.read_u32()? as usize;
                let mut buf = Cursor::new(self.tables.reader.read_data(size - 8)?);
                let offset_str = if ty == 1 { buf.read_u32::<E>()? } else { 0 } as usize;
                let found: Vec<std::result::Result<Utf<'b>, Utf8Error>> = match buf.into_inner() {
                    Cow::Borrowed(buf) => buf[offset_str..]
                        .split(|&b| b == b'\0')
                        .map(|sub| std::str::from_utf8(sub).map(Cow::Borrowed))
                        .collect(),
                    Cow::Owned(buf) => buf[offset_str..]
                        .split(|&b| b == b'\0')
                        .map(|sub| std::str::from_utf8(sub).map(|s| Cow::Owned(s.to_string())))
                        .collect(),
                };
                for string in found {
                    // Handle utf error
                    strings.insert(string.map_err(|e| {
                        BdatError::new_read(self.tables.reader.file_pos(), e.into())
                    })?);
                }
                total_size = total_size.saturating_sub(size);
            }
        }
        Ok(strings.into_iter().collect())
    }

    fn read_table(&mut self, offset: usize) -> Result<ModernTable<'b>> {
        self.tables.read_table_v2(offset)
    }

    fn new_with_header(reader: R) -> Result<Self> {
        let mut header_reader = HeaderReader::<R, E>::new(reader);
        let header = header_reader.read_header()?;
        Ok(Self {
            tables: TableReader::new(header_reader.reader),
            header,
            _endianness: PhantomData,
        })
    }
}

impl<'b, R: ModernRead<'b>, E: ByteOrder> HeaderReader<R, E> {
    fn new(reader: R) -> Self {
        Self {
            reader,
            _endianness: PhantomData,
        }
    }

    fn read_header(&mut self) -> Result<FileHeader> {
        let table_count = self.reader.read_u32()? as usize;
        let mut table_offsets = Vec::with_capacity(table_count);

        self.reader.read_u32()?; // File size

        for _ in 0..table_count {
            table_offsets.push(self.reader.read_u32()? as usize);
        }

        Ok(FileHeader {
            table_count,
            table_offsets,
        })
    }
}

impl<'b, R: ModernRead<'b>, E: ByteOrder> TableReader<R, E> {
    fn new(reader: R) -> Self {
        Self {
            reader,
            _endianness: PhantomData,
        }
    }

    fn read_header(&mut self, table_offset: usize) -> Result<TableHeader> {
        let magic = self.reader.read_u32()?;
        if magic != u32::from_le_bytes(BDAT_MAGIC) {
            return Err(BdatError::new_read(
                table_offset as u64,
                ReadError::InvalidMagic(magic.to_be_bytes()),
            ));
        }
        let version = self.reader.read_u32()?;
        if version != 0x3004 {
            return Err(BdatError::new_read(
                table_offset as u64 + 4,
                ReadError::UnsupportedVersion(version),
            ));
        }

        let columns = self.reader.read_u32()? as usize;
        let rows = self.reader.read_u32()? as usize;
        let base_id = self.reader.read_u32()?;
        let unk = self.reader.read_u32()?;
        if unk != 0 {
            return Err(BdatError::new_read(
                self.reader.file_pos() - 4,
                ReadError::UnexpectedUnknown(unk),
            ));
        }

        let offset_col = self.reader.read_u32()? as usize;
        let offset_hash = self.reader.read_u32()? as usize;
        let offset_row = self.reader.read_u32()? as usize;
        let offset_string;

        let row_length = self.reader.read_u32()? as usize;
        offset_string = self.reader.read_u32()? as usize;
        let str_length = self.reader.read_u32()? as usize;
        Ok(TableHeader {
            columns,
            rows,
            base_id,
            offset_col,
            offset_hash,
            offset_row,
            row_length,
            offset_string,
            str_length,
        })
    }

    fn read_table_v2(&mut self, table_offset: usize) -> Result<ModernTable<'b>> {
        let hdr = self.read_header(table_offset)?;
        let lengths = [
            hdr.offset_col + LEN_COLUMN_DEF_V2 * hdr.columns,
            hdr.offset_hash + LEN_HASH_DEF_V2 * hdr.rows,
            hdr.offset_row + hdr.row_length * hdr.rows,
            hdr.offset_string + hdr.str_length,
        ];
        let table_len = lengths
            .iter()
            .max_by_key(|&i| i)
            .expect("could not determine table length");
        let table_raw = self.reader.read_table_data(*table_len)?;
        let table_data = TableData::new(table_raw, table_offset, hdr.offset_string);

        let name = table_data.get_name::<E>()?;
        let mut col_data = Vec::with_capacity(hdr.columns);
        let mut row_data = Vec::with_capacity(hdr.rows);

        for i in 0..hdr.columns {
            let col_offset = hdr.offset_col + i * LEN_COLUMN_DEF_V2;
            let col = &table_data.data[col_offset..];
            let ty = ValueType::try_from(col[0]).map_err(|_| {
                BdatError::new_read(col_offset as u64, ReadError::UnknownValueType(col[0]))
            })?;
            let name_offset = (&col[1..]).read_u16::<E>()?;
            let label = table_data.get_label::<E>(name_offset as usize)?;

            col_data.push(ModernColumn::new(ty, label));
        }

        for i in 0..hdr.rows {
            let row = &table_data.data[hdr.offset_row + i * hdr.row_length..];
            let mut values = Vec::with_capacity(col_data.len());
            let mut cursor = Cursor::new(row);
            for col in &col_data {
                let value = Self::read_value(&table_data, &mut cursor, col.value_type)?;
                values.push(value);
            }
            row_data.push(ModernRow::new(values));
        }

        // Read hash table. In XC3, there is an entry for each row. In XCXDE, the hash table ends
        // where rows start.
        let row_hash_table = {
            let hashes_len = hdr
                .row_length
                .min(hdr.offset_row.saturating_sub(hdr.offset_hash) / 8);
            let mut row_hash_table = Vec::with_capacity(hashes_len);
            let mut reader = Cursor::new(&table_data.data[hdr.offset_hash..]);

            for _ in 0..hashes_len {
                let hash = reader.read_u32::<E>()?;
                let index = reader.read_u32::<E>()?;
                if let Some((prev_hash, _)) = row_hash_table.last().copied() {
                    if hash == prev_hash {
                        // The issue with XCXDE is that some rows have duplicate IDs. However,
                        // those rows only have one entry in the hash table, so mark it as an error
                        // if the hash table has duplicate entries.
                        return Err(BdatError::new_read(
                            reader.position() + table_offset as u64 - 8,
                            ReadError::NameTableDuplicate(hash),
                        ));
                    }
                    if hash < prev_hash {
                        return Err(BdatError::new_read(
                            reader.position() + table_offset as u64 - 8,
                            ReadError::NameTableOrder(prev_hash, hash),
                        ));
                    }
                }
                row_hash_table.push((hash, index));
            }

            row_hash_table
        };

        Ok(ModernTableBuilder::with_name(name)
            .set_base_id(hdr.base_id)
            .set_columns(col_data)
            .set_rows(row_data)
            .build_with_row_map(row_hash_table))
    }

    fn read_value(
        table_data: &TableData<'b>,
        mut buf: impl Read,
        col_type: ValueType,
    ) -> Result<Value<'b>> {
        Ok(match col_type {
            ValueType::Unknown => Value::Unknown,
            ValueType::UnsignedByte => Value::UnsignedByte(buf.read_u8()?),
            ValueType::UnsignedShort => Value::UnsignedShort(buf.read_u16::<E>()?),
            ValueType::UnsignedInt => Value::UnsignedInt(buf.read_u32::<E>()?),
            ValueType::SignedByte => Value::SignedByte(buf.read_i8()?),
            ValueType::SignedShort => Value::SignedShort(buf.read_i16::<E>()?),
            ValueType::SignedInt => Value::SignedInt(buf.read_i32::<E>()?),
            ValueType::String => {
                Value::String(table_data.get_string(buf.read_u32::<E>()? as usize, usize::MAX)?)
            }
            ValueType::Float => Value::Float(BdatReal::Floating(buf.read_f32::<E>()?.into())),
            ValueType::Percent => Value::Percent(buf.read_u8()?),
            ValueType::HashRef => Value::HashRef(buf.read_u32::<E>()?),
            ValueType::DebugString => Value::DebugString(
                table_data.get_string(buf.read_u32::<E>()? as usize, usize::MAX)?,
            ),
            ValueType::Unknown12 => Value::Unknown12(buf.read_u8()?),
            ValueType::MessageId => Value::MessageId(buf.read_u16::<E>()?),
        })
    }
}

impl<'r> TableData<'r> {
    fn new(data: Cow<'r, [u8]>, table_offset: usize, strings_offset: usize) -> TableData<'r> {
        Self {
            table_offset,
            data,
            string_table_offset: strings_offset,
        }
    }

    /// Returns the table's hashed name, or [`None`] if it could not be found.
    fn get_name<E>(&self) -> Result<Label<'r>>
    where
        E: ByteOrder,
    {
        // First byte = 0 => labels are hashed. Otherwise, the string starts from the first byte
        let offset = if self.are_labels_hashed() { 1 } else { 0 };
        self.get_label::<E>(offset)
    }

    /// Reads a null-terminated UTF-8 encoded string from the string table at the given offset
    fn get_string(&self, offset: usize, limit: usize) -> Result<Utf<'r>> {
        let str_ptr = self.string_table_offset + offset;
        let len = self.data[str_ptr..]
            .split(|&b| b == 0)
            .take(1)
            .flatten()
            .take(limit)
            .count();
        let str = match &self.data {
            Cow::Borrowed(data) => Cow::Borrowed(
                std::str::from_utf8(&data[str_ptr..str_ptr + len]).map_err(|e| {
                    BdatError::new_read((self.table_offset + str_ptr) as u64, e.into())
                })?,
            ),
            Cow::Owned(data) => Cow::Owned(
                std::str::from_utf8(&data[str_ptr..str_ptr + len])
                    .map_err(|e| {
                        BdatError::new_read((self.table_offset + str_ptr) as u64, e.into())
                    })?
                    .to_string(),
            ),
        };
        Ok(str)
    }

    /// Reads a column label (either a string or a hash) from the string table at the given offset
    fn get_label<E>(&self, offset: usize) -> Result<Label<'r>>
    where
        E: ByteOrder,
    {
        if self.are_labels_hashed() {
            Ok(Label::Hash(
                (&self.data[self.string_table_offset + offset..]).read_u32::<E>()?,
            ))
        } else {
            Ok(Label::String(self.get_string(offset, usize::MAX)?))
        }
    }

    fn are_labels_hashed(&self) -> bool {
        self.data[self.string_table_offset] == 0
    }
}

impl<'b, E> ModernRead<'b> for BdatSlice<'b, E>
where
    E: ByteOrder,
{
    fn read_data(&mut self, length: usize) -> Result<Cow<'b, [u8]>> {
        let pos = self.data.position() as usize;
        if pos
            .checked_add(length)
            .is_none_or(|l| l > self.data.get_ref().len())
        {
            return Err(BdatError::Io(std::io::Error::new(
                std::io::ErrorKind::UnexpectedEof,
                format!(
                    "failed to fill whole buffer at={pos} len={length} max={}",
                    self.data.get_ref().len()
                ),
            )));
        }
        let res = Cow::Borrowed(&self.data.clone().into_inner()[pos..pos + length]);
        self.data.set_position((pos + length) as u64);
        Ok(res)
    }

    fn read_table_data(&mut self, length: usize) -> Result<Cow<'b, [u8]>> {
        if self
            .table_offset
            .checked_add(length)
            .is_none_or(|l| l > self.data.get_ref().len())
        {
            return Err(BdatError::Io(std::io::Error::new(
                std::io::ErrorKind::UnexpectedEof,
                format!(
                    "failed to fill whole buffer at={} len={length} max={}",
                    self.table_offset,
                    self.data.get_ref().len()
                ),
            )));
        }
        Ok(Cow::Borrowed(
            &self.data.clone().into_inner()[self.table_offset..self.table_offset + length],
        ))
    }

    #[inline]
    fn read_u32(&mut self) -> Result<u32> {
        Ok(self.data.read_u32::<E>()?)
    }

    fn seek_table(&mut self, offset: usize) -> Result<()> {
        self.data.seek(SeekFrom::Start(offset as u64))?;
        self.table_offset = offset;
        Ok(())
    }

    fn file_pos(&mut self) -> u64 {
        self.data.position()
    }
}

impl<'b, R, E> ModernRead<'b> for BdatReader<R, E>
where
    R: Read + Seek,
    E: ByteOrder,
{
    fn read_data(&mut self, length: usize) -> Result<Cow<'b, [u8]>> {
        let mut buf = vec![0u8; length];
        self.stream.read_exact(&mut buf)?;
        Ok(buf.into())
    }

    fn read_table_data(&mut self, length: usize) -> Result<Cow<'b, [u8]>> {
        let mut table_raw = vec![0u8; length];
        self.stream
            .seek(SeekFrom::Start(self.table_offset as u64))?;
        self.stream.read_exact(&mut table_raw)?;
        Ok(table_raw.into())
    }

    #[inline]
    fn read_u32(&mut self) -> Result<u32> {
        Ok(self.stream.read_u32::<E>()?)
    }

    fn seek_table(&mut self, offset: usize) -> Result<()> {
        self.stream.seek(SeekFrom::Start(offset as u64))?;
        self.table_offset = offset;
        Ok(())
    }

    fn file_pos(&mut self) -> u64 {
        self.stream.stream_position().unwrap()
    }
}

impl<'b, R, E> BdatFile<'b> for FileReader<R, E>
where
    R: ModernRead<'b>,
    E: ByteOrder,
{
    type TableOut = ModernTable<'b>;

    /// Reads all tables from the BDAT source.
    fn get_tables(&mut self) -> Result<Vec<ModernTable<'b>>> {
        let mut tables = Vec::with_capacity(self.header.table_count);

        for i in 0..self.header.table_count {
            self.tables
                .reader
                .seek_table(self.header.table_offsets[i])?;
            let table = self.read_table(self.header.table_offsets[i])?;
            tables.push(table);
        }

        Ok(tables)
    }

    /// Returns the number of tables in the BDAT file.
    fn table_count(&self) -> usize {
        self.header.table_count
    }
}