mirror of
https://codeberg.org/icewind/vbsp.git
synced 2026-06-03 10:44:07 +02:00
commit
0d2113f2e6
3 changed files with 19 additions and 22 deletions
|
|
@ -11,23 +11,15 @@ pub struct BspFile<'a> {
|
|||
|
||||
impl<'a> BspFile<'a> {
|
||||
pub fn new(data: &'a [u8]) -> BspResult<Self> {
|
||||
const EXPECTED_HEADER: Header = Header {
|
||||
v: b'V',
|
||||
b: b'B',
|
||||
s: b'S',
|
||||
p: b'P',
|
||||
};
|
||||
// TODO: Use this to decide on the version to parse it as
|
||||
const EXPECTED_VERSION: u32 = 0x14;
|
||||
|
||||
let mut cursor = Cursor::new(data);
|
||||
let header: Header = cursor.read_le()?;
|
||||
let version: u32 = cursor.read_le()?;
|
||||
|
||||
if header != EXPECTED_HEADER || version != EXPECTED_VERSION {
|
||||
return Err(BspError::UnexpectedHeader(header));
|
||||
}
|
||||
|
||||
let header: Header = cursor.read_le().map_err(|err| match err {
|
||||
binrw::Error::BadMagic { .. } => {
|
||||
BspError::UnexpectedHeader(data[..4].try_into().expect(
|
||||
"always enough data because otherwise a different binrw error would be hit",
|
||||
))
|
||||
}
|
||||
error => BspError::MalformedData(error),
|
||||
})?;
|
||||
let directories = cursor.read_le()?;
|
||||
|
||||
Ok(BspFile {
|
||||
|
|
|
|||
|
|
@ -69,13 +69,18 @@ impl Index<LumpType> for Directories {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, BinRead)]
|
||||
#[br(little)]
|
||||
#[brw(repr=u32)]
|
||||
pub enum BspVersion {
|
||||
Version20 = 20,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, BinRead)]
|
||||
#[br(little)]
|
||||
pub struct Header {
|
||||
pub v: u8,
|
||||
pub b: u8,
|
||||
pub s: u8,
|
||||
pub p: u8,
|
||||
#[brw(magic = b"VBSP")]
|
||||
pub version: BspVersion,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, BinRead)]
|
||||
|
|
|
|||
|
|
@ -7,8 +7,8 @@ use zip::result::ZipError;
|
|||
#[non_exhaustive]
|
||||
#[derive(Debug, Error)]
|
||||
pub enum BspError {
|
||||
#[error("unexpected magic numbers or version")]
|
||||
UnexpectedHeader(Header),
|
||||
#[error("unexpected magic numbers or version: {0:?}")]
|
||||
UnexpectedHeader([u8; 4]),
|
||||
#[error("bsp lump is out of bounds of the bsp file")]
|
||||
LumpOutOfBounds(LumpEntry),
|
||||
#[error("bsp game lump is out of bounds of the bsp file")]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue