mirror of
https://codeberg.org/icewind/vbsp.git
synced 2026-06-03 10:44:07 +02:00
include Lump version in LumpReader
This commit is contained in:
parent
75728cfab6
commit
8d2ea3d6fb
4 changed files with 27 additions and 11 deletions
|
|
@ -34,12 +34,16 @@ impl<'a> BspFile<'a> {
|
|||
}
|
||||
|
||||
pub fn lump_reader(&self, lump: LumpType) -> BspResult<LumpReader<Cursor<Cow<[u8]>>>> {
|
||||
let data = self.get_lump(lump)?;
|
||||
Ok(LumpReader::new(data, lump))
|
||||
let entry = self.get_lump_entry(lump);
|
||||
let data = self.get_lump(entry)?;
|
||||
Ok(LumpReader::new(data, lump, entry.version))
|
||||
}
|
||||
|
||||
pub fn get_lump(&self, lump: LumpType) -> BspResult<Cow<[u8]>> {
|
||||
let lump = &self.directories[lump];
|
||||
pub fn get_lump_entry(&self, lump: LumpType) -> &LumpEntry {
|
||||
&self.directories[lump]
|
||||
}
|
||||
|
||||
pub fn get_lump(&self, lump: &LumpEntry) -> BspResult<Cow<[u8]>> {
|
||||
let raw_data = self
|
||||
.data
|
||||
.get(lump.offset as usize..lump.offset as usize + lump.length as usize)
|
||||
|
|
|
|||
|
|
@ -85,6 +85,12 @@ pub struct Header {
|
|||
pub version: BspVersion,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
pub struct LumpArgs {
|
||||
pub length: usize,
|
||||
pub version: u32,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, BinRead)]
|
||||
#[br(little)]
|
||||
pub struct LumpEntry {
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@ impl Bsp {
|
|||
.read_vec(|r| r.read())?;
|
||||
let texture_string_data = String::from_utf8(
|
||||
bsp_file
|
||||
.get_lump(LumpType::TextureDataStringData)?
|
||||
.get_lump(bsp_file.get_lump_entry(LumpType::TextureDataStringData))?
|
||||
.into_owned(),
|
||||
)
|
||||
.map_err(|e| BspError::String(StringError::NonUTF8(e.utf8_error())))?;
|
||||
|
|
|
|||
|
|
@ -8,16 +8,18 @@ pub struct LumpReader<R> {
|
|||
inner: R,
|
||||
length: usize,
|
||||
lump: LumpType,
|
||||
version: u32,
|
||||
}
|
||||
|
||||
impl<'a> LumpReader<Cursor<Cow<'a, [u8]>>> {
|
||||
pub fn new(data: Cow<'a, [u8]>, lump: LumpType) -> Self {
|
||||
pub fn new(data: Cow<'a, [u8]>, lump: LumpType, version: u32) -> Self {
|
||||
let length = data.len();
|
||||
let reader = Cursor::new(data);
|
||||
LumpReader {
|
||||
inner: reader,
|
||||
length,
|
||||
lump,
|
||||
version,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -58,11 +60,7 @@ impl<R: BinReaderExt + Read> LumpReader<R> {
|
|||
Ok(entries)
|
||||
}
|
||||
|
||||
pub fn read<T: BinRead + Debug>(&mut self) -> BspResult<T>
|
||||
where
|
||||
T::Args<'static>: Default,
|
||||
<T as BinRead>::Args<'static>: Clone,
|
||||
{
|
||||
pub fn read<'a, T: BinRead<Args<'a> = ()> + Debug>(&mut self) -> BspResult<T> {
|
||||
// let start = self.inner.stream_position().unwrap() as usize;
|
||||
let result = self.inner.read_le()?;
|
||||
// let end = self.inner.stream_position().unwrap() as usize;
|
||||
|
|
@ -76,6 +74,14 @@ impl<R: BinReaderExt + Read> LumpReader<R> {
|
|||
// );
|
||||
Ok(result)
|
||||
}
|
||||
pub fn read_args<'a, T: BinRead<Args<'a> = LumpArgs> + Debug>(&mut self) -> BspResult<T> {
|
||||
let args = LumpArgs {
|
||||
length: self.length,
|
||||
version: self.version,
|
||||
};
|
||||
let result = T::read_options(&mut self.inner, binrw::Endian::Little, args)?;
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn read_visdata(&mut self) -> BspResult<VisData> {
|
||||
if self.length < size_of::<u32>() * 2 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue