mirror of
https://codeberg.org/icewind/vbsp.git
synced 2026-06-03 18:54:05 +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]>>>> {
|
pub fn lump_reader(&self, lump: LumpType) -> BspResult<LumpReader<Cursor<Cow<[u8]>>>> {
|
||||||
let data = self.get_lump(lump)?;
|
let entry = self.get_lump_entry(lump);
|
||||||
Ok(LumpReader::new(data, lump))
|
let data = self.get_lump(entry)?;
|
||||||
|
Ok(LumpReader::new(data, lump, entry.version))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_lump(&self, lump: LumpType) -> BspResult<Cow<[u8]>> {
|
pub fn get_lump_entry(&self, lump: LumpType) -> &LumpEntry {
|
||||||
let lump = &self.directories[lump];
|
&self.directories[lump]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_lump(&self, lump: &LumpEntry) -> BspResult<Cow<[u8]>> {
|
||||||
let raw_data = self
|
let raw_data = self
|
||||||
.data
|
.data
|
||||||
.get(lump.offset as usize..lump.offset as usize + lump.length as usize)
|
.get(lump.offset as usize..lump.offset as usize + lump.length as usize)
|
||||||
|
|
|
||||||
|
|
@ -85,6 +85,12 @@ pub struct Header {
|
||||||
pub version: BspVersion,
|
pub version: BspVersion,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, Default)]
|
||||||
|
pub struct LumpArgs {
|
||||||
|
pub length: usize,
|
||||||
|
pub version: u32,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Default, BinRead)]
|
#[derive(Clone, Copy, Debug, Default, BinRead)]
|
||||||
#[br(little)]
|
#[br(little)]
|
||||||
pub struct LumpEntry {
|
pub struct LumpEntry {
|
||||||
|
|
|
||||||
|
|
@ -71,7 +71,7 @@ impl Bsp {
|
||||||
.read_vec(|r| r.read())?;
|
.read_vec(|r| r.read())?;
|
||||||
let texture_string_data = String::from_utf8(
|
let texture_string_data = String::from_utf8(
|
||||||
bsp_file
|
bsp_file
|
||||||
.get_lump(LumpType::TextureDataStringData)?
|
.get_lump(bsp_file.get_lump_entry(LumpType::TextureDataStringData))?
|
||||||
.into_owned(),
|
.into_owned(),
|
||||||
)
|
)
|
||||||
.map_err(|e| BspError::String(StringError::NonUTF8(e.utf8_error())))?;
|
.map_err(|e| BspError::String(StringError::NonUTF8(e.utf8_error())))?;
|
||||||
|
|
|
||||||
|
|
@ -8,16 +8,18 @@ pub struct LumpReader<R> {
|
||||||
inner: R,
|
inner: R,
|
||||||
length: usize,
|
length: usize,
|
||||||
lump: LumpType,
|
lump: LumpType,
|
||||||
|
version: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> LumpReader<Cursor<Cow<'a, [u8]>>> {
|
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 length = data.len();
|
||||||
let reader = Cursor::new(data);
|
let reader = Cursor::new(data);
|
||||||
LumpReader {
|
LumpReader {
|
||||||
inner: reader,
|
inner: reader,
|
||||||
length,
|
length,
|
||||||
lump,
|
lump,
|
||||||
|
version,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -58,11 +60,7 @@ impl<R: BinReaderExt + Read> LumpReader<R> {
|
||||||
Ok(entries)
|
Ok(entries)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read<T: BinRead + Debug>(&mut self) -> BspResult<T>
|
pub fn read<'a, T: BinRead<Args<'a> = ()> + Debug>(&mut self) -> BspResult<T> {
|
||||||
where
|
|
||||||
T::Args<'static>: Default,
|
|
||||||
<T as BinRead>::Args<'static>: Clone,
|
|
||||||
{
|
|
||||||
// let start = self.inner.stream_position().unwrap() as usize;
|
// let start = self.inner.stream_position().unwrap() as usize;
|
||||||
let result = self.inner.read_le()?;
|
let result = self.inner.read_le()?;
|
||||||
// let end = self.inner.stream_position().unwrap() as usize;
|
// let end = self.inner.stream_position().unwrap() as usize;
|
||||||
|
|
@ -76,6 +74,14 @@ impl<R: BinReaderExt + Read> LumpReader<R> {
|
||||||
// );
|
// );
|
||||||
Ok(result)
|
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> {
|
pub fn read_visdata(&mut self) -> BspResult<VisData> {
|
||||||
if self.length < size_of::<u32>() * 2 {
|
if self.length < size_of::<u32>() * 2 {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue