aboutsummaryrefslogtreecommitdiff
path: root/stockton-levels/src/q3/header.rs
blob: 61cdab1217573caead2724208ff0f3b010b286f0 (plain)
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
/*
 * Copyright (C) Oscar Shrimpton 2020
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

use crate::types::{ParseError, Result};
use std::convert::TryInto;

const MAGIC_HEADER: &[u8] = &[0x49, 0x42, 0x53, 0x50];
const HEADER_LEN: usize = 4 + 4 + (17 * 4 * 2);

/// The header found at the start of a (Q3) bsp file
#[derive(Clone, Copy, Debug)]
pub struct Header {
    pub version: u32,
    pub dir_entries: [DirEntry; 17],
}

/// A directory entry, pointing to a lump in the file
#[derive(Clone, Copy, Debug)]
pub struct DirEntry {
    /// Offset from beginning of file to start of lump
    pub offset: u32,

    /// Length of lump, multiple of 4.
    pub length: u32,
}

impl Header {
    /// Deserialise from buffer.
    /// # Format
    /// string[4] magic             Magic number. Always "IBSP".
    /// int version                 Version number. 0x2e for the BSP files distributed with Quake 3.
    /// direntry[17] direntries     Lump directory, seventeen entries.
    pub fn from(v: &[u8]) -> Result<Header> {
        if v.len() < HEADER_LEN {
            return Err(ParseError::Invalid);
        }
        let magic = &v[0..4];

        if magic != MAGIC_HEADER {
            return Err(ParseError::Invalid);
        }

        let version: &[u8; 4] = v[4..8].try_into().unwrap();

        let entries: &[u8] = &v[8..144];
        let mut dir_entries: [DirEntry; 17] = [DirEntry {
            offset: 0,
            length: 0,
        }; 17];

        for n in 0..17 {
            let base = &entries[(n * 8)..(n * 8) + 8];
            dir_entries[n] = DirEntry {
                offset: u32::from_le_bytes(base[0..4].try_into().unwrap()),
                length: u32::from_le_bytes(base[4..8].try_into().unwrap()),
            }
        }

        Ok(Header {
            version: u32::from_le_bytes(*version),
            dir_entries,
        })
    }

    /// Get the lump at given index from the buffer, with offset & length based on this directory.
    pub fn get_lump<'l>(&self, buf: &'l [u8], index: usize) -> &'l [u8] {
        let entry = self.dir_entries[index];

        &buf[entry.offset as usize..entry.offset as usize + entry.length as usize]
    }
}