From 91a1b3d65e2a5b929724433d503d8a9d4267ee32 Mon Sep 17 00:00:00 2001
From: Quaternions <krakow20@gmail.com>
Date: Mon, 20 Jan 2025 09:11:23 -0800
Subject: [PATCH] read entire file

---
 strafe-client/src/file.rs | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/strafe-client/src/file.rs b/strafe-client/src/file.rs
index 229f818b..2d0d3b99 100644
--- a/strafe-client/src/file.rs
+++ b/strafe-client/src/file.rs
@@ -35,20 +35,24 @@ pub enum ReadFormat{
 
 pub fn read<R:Read+std::io::Seek>(input:R)->Result<ReadFormat,ReadError>{
 	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"<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")]
-		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),