1
0
Fork 0
mirror of https://codeberg.org/icewind/vbsp.git synced 2026-06-03 18:54:05 +02:00

binread -> binrw

This commit is contained in:
Robin Appelman 2022-02-17 21:47:08 +01:00
commit a785884bc1
5 changed files with 21 additions and 36 deletions

View file

@ -16,13 +16,15 @@ bv = "0.11.1"
itertools = "0.10.3" itertools = "0.10.3"
thiserror = "1.0.30" thiserror = "1.0.30"
lzma-rs = "0.2.0" lzma-rs = "0.2.0"
binread = "2.2.0" binrw = "0.8.0"
parse-display = "0.5.3" parse-display = "0.5.3"
static_assertions = "1.1.0" static_assertions = "1.1.0"
miette = "3.2.0"
[dev-dependencies] [dev-dependencies]
obj = "0.10" obj = "0.10"
main_error = "0.1.0" main_error = "0.1.0"
miette = { version = "3.2.0", features = ["fancy"] }
[features] [features]
bench = [] bench = []

View file

@ -1,6 +1,6 @@
use crate::*; use crate::*;
use binread::io::Cursor; use binrw::io::Cursor;
use binread::BinReaderExt; use binrw::BinReaderExt;
use lzma_rs::decompress::{Options, UnpackedSize}; use lzma_rs::decompress::{Options, UnpackedSize};
use std::borrow::Cow; use std::borrow::Cow;

View file

@ -1,7 +1,7 @@
use crate::bspfile::LumpType; use crate::bspfile::LumpType;
use arrayvec::ArrayString; use arrayvec::ArrayString;
use binread::io::SeekFrom; use binrw::io::SeekFrom;
use binread::{BinRead, BinResult, ReadOptions}; use binrw::{BinRead, BinResult, ReadOptions};
use bitflags::bitflags; use bitflags::bitflags;
use bv::BitVec; use bv::BitVec;
use parse_display::Display; use parse_display::Display;
@ -11,28 +11,11 @@ use std::iter::once;
use std::mem::size_of; use std::mem::size_of;
use std::ops::{Add, Index}; use std::ops::{Add, Index};
#[derive(Clone)] #[derive(Clone, BinRead)]
pub struct Directories { pub struct Directories {
entries: [LumpEntry; 64], entries: [LumpEntry; 64],
} }
impl BinRead for Directories {
type Args = <LumpEntry as BinRead>::Args;
fn read_options<R: binread::io::Read + binread::io::Seek>(
reader: &mut R,
options: &ReadOptions,
args: Self::Args,
) -> BinResult<Self> {
let mut entries = [LumpEntry::default(); 64];
for i in 0..64 {
entries[i] = LumpEntry::read_options(reader, options, args)?;
}
Ok(Directories { entries })
}
}
impl Index<LumpType> for Directories { impl Index<LumpType> for Directories {
type Output = LumpEntry; type Output = LumpEntry;
@ -184,24 +167,20 @@ pub struct Name(ArrayString<64>);
impl BinRead for Name { impl BinRead for Name {
type Args = (); type Args = ();
fn read_options<R: binread::io::Read + binread::io::Seek>( fn read_options<R: binrw::io::Read + binrw::io::Seek>(
reader: &mut R, reader: &mut R,
options: &ReadOptions, options: &ReadOptions,
args: Self::Args, args: Self::Args,
) -> BinResult<Self> { ) -> BinResult<Self> {
use std::str; use std::str;
let mut name_buf: [u8; 64] = [0; 64]; let name_buf = <[u8; 64]>::read_options(reader, options, args)?;
for i in 0..64 {
name_buf[i] = u8::read_options(reader, options, args)?;
}
let zero_pos = let zero_pos =
name_buf name_buf
.iter() .iter()
.position(|c| *c == 0) .position(|c| *c == 0)
.ok_or_else(|| binread::Error::AssertFail { .ok_or_else(|| binrw::Error::AssertFail {
pos: reader.seek(SeekFrom::Current(0)).unwrap(), pos: reader.seek(SeekFrom::Current(0)).unwrap(),
message: "Name not null terminated".to_string(), message: "Name not null terminated".to_string(),
})?; })?;

View file

@ -6,15 +6,16 @@ use crate::bspfile::LumpType;
pub use crate::data::TextureFlags; pub use crate::data::TextureFlags;
pub use crate::data::Vector; pub use crate::data::Vector;
use crate::data::*; use crate::data::*;
use binread::io::Cursor; use binrw::io::Cursor;
use binread::BinRead; use binrw::BinRead;
use bspfile::BspFile; use bspfile::BspFile;
use itertools::{GroupBy, Itertools}; use itertools::{GroupBy, Itertools};
use miette::Diagnostic;
use reader::LumpReader; use reader::LumpReader;
use std::{io::Read, ops::Deref}; use std::{io::Read, ops::Deref};
use thiserror::Error; use thiserror::Error;
#[derive(Debug, Error)] #[derive(Debug, Error, Diagnostic)]
pub enum BspError { pub enum BspError {
#[error("unexpected magic numbers or version, is this a valve bsp?")] #[error("unexpected magic numbers or version, is this a valve bsp?")]
UnexpectedHeader(Header), UnexpectedHeader(Header),
@ -41,7 +42,7 @@ pub enum BspError {
#[error("error while reading data: {0}")] #[error("error while reading data: {0}")]
ReadError(#[from] std::io::Error), ReadError(#[from] std::io::Error),
#[error("error while reading data: {0}")] #[error("error while reading data: {0}")]
BinReadError(#[from] binread::Error), BinReadError(#[from] binrw::Error),
} }
pub type BspResult<T> = Result<T, BspError>; pub type BspResult<T> = Result<T, BspError>;

View file

@ -1,5 +1,5 @@
use crate::*; use crate::*;
use binread::BinReaderExt; use binrw::BinReaderExt;
use std::borrow::Cow; use std::borrow::Cow;
use std::mem::size_of; use std::mem::size_of;
@ -40,7 +40,10 @@ impl<R: BinReaderExt + Read> LumpReader<R> {
Ok(entries) Ok(entries)
} }
pub fn read<T: BinRead>(&mut self) -> BspResult<T> { pub fn read<T: BinRead>(&mut self) -> BspResult<T>
where
T::Args: Default,
{
Ok(self.inner.read_le()?) Ok(self.inner.read_le()?)
} }