mirror of
https://codeberg.org/icewind/vbsp.git
synced 2026-06-03 18:54:05 +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> {
|
impl<'a> BspFile<'a> {
|
||||||
pub fn new(data: &'a [u8]) -> BspResult<Self> {
|
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 mut cursor = Cursor::new(data);
|
||||||
let header: Header = cursor.read_le()?;
|
let header: Header = cursor.read_le().map_err(|err| match err {
|
||||||
let version: u32 = cursor.read_le()?;
|
binrw::Error::BadMagic { .. } => {
|
||||||
|
BspError::UnexpectedHeader(data[..4].try_into().expect(
|
||||||
if header != EXPECTED_HEADER || version != EXPECTED_VERSION {
|
"always enough data because otherwise a different binrw error would be hit",
|
||||||
return Err(BspError::UnexpectedHeader(header));
|
))
|
||||||
}
|
}
|
||||||
|
error => BspError::MalformedData(error),
|
||||||
|
})?;
|
||||||
let directories = cursor.read_le()?;
|
let directories = cursor.read_le()?;
|
||||||
|
|
||||||
Ok(BspFile {
|
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)]
|
#[derive(Debug, Clone, PartialEq, Eq, BinRead)]
|
||||||
#[br(little)]
|
#[br(little)]
|
||||||
pub struct Header {
|
pub struct Header {
|
||||||
pub v: u8,
|
#[brw(magic = b"VBSP")]
|
||||||
pub b: u8,
|
pub version: BspVersion,
|
||||||
pub s: u8,
|
|
||||||
pub p: u8,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Default, BinRead)]
|
#[derive(Clone, Copy, Debug, Default, BinRead)]
|
||||||
|
|
|
||||||
|
|
@ -7,8 +7,8 @@ use zip::result::ZipError;
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
#[derive(Debug, Error)]
|
#[derive(Debug, Error)]
|
||||||
pub enum BspError {
|
pub enum BspError {
|
||||||
#[error("unexpected magic numbers or version")]
|
#[error("unexpected magic numbers or version: {0:?}")]
|
||||||
UnexpectedHeader(Header),
|
UnexpectedHeader([u8; 4]),
|
||||||
#[error("bsp lump is out of bounds of the bsp file")]
|
#[error("bsp lump is out of bounds of the bsp file")]
|
||||||
LumpOutOfBounds(LumpEntry),
|
LumpOutOfBounds(LumpEntry),
|
||||||
#[error("bsp game lump is out of bounds of the bsp file")]
|
#[error("bsp game lump is out of bounds of the bsp file")]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue