read entire file

This commit is contained in:
Quaternions 2025-01-20 09:11:23 -08:00
parent e1d51ff2da
commit 91a1b3d65e

View File

@ -35,20 +35,24 @@ pub enum ReadFormat{
pub fn read<R:Read+std::io::Seek>(input:R)->Result<ReadFormat,ReadError>{ pub fn read<R:Read+std::io::Seek>(input:R)->Result<ReadFormat,ReadError>{
let mut buf=std::io::BufReader::new(input); let mut buf=std::io::BufReader::new(input);
let peek=std::io::BufRead::fill_buf(&mut buf).map_err(ReadError::Io)?; let peek=std::io::BufRead::fill_buf(&mut buf).map_err(ReadError::Io)?[0..4].to_owned();
match &peek[0..4]{ // reading the entire file is way faster than round tripping the disk constantly
let mut entire_file=Vec::new();
buf.read_to_end(&mut entire_file).map_err(ReadError::Io)?;
let cursor=std::io::Cursor::new(entire_file);
match peek.as_slice(){
#[cfg(feature="roblox")] #[cfg(feature="roblox")]
b"<rob"=>Ok(ReadFormat::Roblox(strafesnet_rbx_loader::read(buf).map_err(ReadError::Roblox)?)), b"<rob"=>Ok(ReadFormat::Roblox(strafesnet_rbx_loader::read(cursor).map_err(ReadError::Roblox)?)),
#[cfg(feature="source")] #[cfg(feature="source")]
b"VBSP"=>Ok(ReadFormat::Source(strafesnet_bsp_loader::read(buf).map_err(ReadError::Source)?)), b"VBSP"=>Ok(ReadFormat::Source(strafesnet_bsp_loader::read(cursor).map_err(ReadError::Source)?)),
#[cfg(feature="snf")] #[cfg(feature="snf")]
b"SNFM"=>Ok(ReadFormat::SNFM( b"SNFM"=>Ok(ReadFormat::SNFM(
strafesnet_snf::read_map(buf).map_err(ReadError::StrafesNET)? strafesnet_snf::read_map(cursor).map_err(ReadError::StrafesNET)?
.into_complete_map().map_err(ReadError::StrafesNETMap)? .into_complete_map().map_err(ReadError::StrafesNETMap)?
)), )),
#[cfg(feature="snf")] #[cfg(feature="snf")]
b"SNFB"=>Ok(ReadFormat::SNFB( b"SNFB"=>Ok(ReadFormat::SNFB(
strafesnet_snf::read_bot(buf).map_err(ReadError::StrafesNET)? strafesnet_snf::read_bot(cursor).map_err(ReadError::StrafesNET)?
.read_all().map_err(ReadError::StrafesNETBot)? .read_all().map_err(ReadError::StrafesNETBot)?
)), )),
_=>Err(ReadError::UnknownFileFormat), _=>Err(ReadError::UnknownFileFormat),