physis/sqpack/
db.rs

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
// SPDX-FileCopyrightText: 2025 Joshua Goins <josh@redstrate.com>
// SPDX-License-Identifier: GPL-3.0-or-later

use std::io::Cursor;

use crate::ByteSpan;
use crate::common_file_operations::read_string;
use crate::common_file_operations::write_string;
use crate::sqpack::SqPackHeader;
use binrw::BinRead;
use binrw::binrw;
use binrw::helpers::until_eof;

#[binrw]
#[derive(Debug)]
pub struct SQDBHeader {
    size: u32,
    #[br(pad_after = 1016)] // nothing
    unk: u32,
}

// 264 bytes
#[binrw]
#[derive(Debug)]
pub struct SQDBEntry {
    #[br(pad_before = 4)] // 4 empty bytes
    offset: u32, // TODO: just a guess, offset into what?
    #[br(pad_after = 4)] // 4 more empty bytes
    size: u32, // TODO: also a guess

    // Corresponds to SplitPath hash
    filename_hash: u32,
    path_hash: u32,

    // TODO: this is terrible, just read until string nul terminator
    #[br(count = 240)]
    #[br(map = read_string)]
    #[bw(map = write_string)]
    path: String,
}

#[binrw]
#[derive(Debug)]
#[brw(little)]
pub struct SqPackDatabase {
    sqpack_header: SqPackHeader,

    header: SQDBHeader,

    #[br(parse_with = until_eof)]
    entries: Vec<SQDBEntry>,
}

impl SqPackDatabase {
    /// Reads an existing SQDB file
    pub fn from_existing(buffer: ByteSpan) -> Option<Self> {
        let mut cursor = Cursor::new(buffer);
        Self::read(&mut cursor).ok()
    }
}