diff --git a/strafe-client/src/file.rs b/strafe-client/src/file.rs index 229f818..2d0d3b9 100644 --- a/strafe-client/src/file.rs +++ b/strafe-client/src/file.rs @@ -35,20 +35,24 @@ pub enum ReadFormat{ pub fn read(input:R)->Result{ let mut buf=std::io::BufReader::new(input); - let peek=std::io::BufRead::fill_buf(&mut buf).map_err(ReadError::Io)?; - match &peek[0..4]{ + let peek=std::io::BufRead::fill_buf(&mut buf).map_err(ReadError::Io)?[0..4].to_owned(); + // 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")] - b"Ok(ReadFormat::Roblox(strafesnet_rbx_loader::read(buf).map_err(ReadError::Roblox)?)), + b"Ok(ReadFormat::Roblox(strafesnet_rbx_loader::read(cursor).map_err(ReadError::Roblox)?)), #[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")] 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)? )), #[cfg(feature="snf")] 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)? )), _=>Err(ReadError::UnknownFileFormat),