From 0705e879db99a830dc2e69b0d572ebc7acd440cb Mon Sep 17 00:00:00 2001
From: Quaternions <krakow20@gmail.com>
Date: Fri, 26 Jul 2024 12:19:09 -0700
Subject: [PATCH] fix block_location

---
 src/map.rs | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/src/map.rs b/src/map.rs
index 305cabe4f..1903d1508 100644
--- a/src/map.rs
+++ b/src/map.rs
@@ -346,7 +346,12 @@ pub fn write_map<W:BinWriterExt>(mut writer:W,map:strafesnet_common::map::Comple
 	}).collect::<Result<Vec<_>,_>>()?;
 	let bvh=strafesnet_common::bvh::generate_bvh(boxen).weigh_contents(&|_|std::mem::size_of::<newtypes::model::Model>());
 	//build blocks
-	let mut block_location=vec![0];//for file header
+	//block location is initialized with two values
+	//the first value represents the location of the first byte after the file header
+	//the second value represents the first byte of the second data block
+	//the first data block is always the map header, so the difference is the map header size.
+	//this information is filled in later after the sizes are known.
+	let mut block_location=vec![0,0];//for file header
 	let mut spacial_blocks=Vec::new();//for map header
 	let mut sequential_block_data=Vec::new();
 	let mut cursor_to_data=std::io::Cursor::new(&mut sequential_block_data);
@@ -389,28 +394,33 @@ pub fn write_map<W:BinWriterExt>(mut writer:W,map:strafesnet_common::map::Comple
 		attributes:map.attributes.into_iter().map(Into::into).collect(),
 		render_configs:map.render_configs.into_iter().map(Into::into).collect(),
 	};
+	//the map header is a block!
+	block_count+=1;
+	assert_eq!(block_count as usize+1,block_location.len());
 	let mut file_header=crate::file::Header{
 		fourcc:crate::file::FourCC::Map,
 		version:0,
-		priming:0,//TODO
+		priming:0,
 		resource:0,
 		block_count,
 		block_location,
 	};
-	//probe header lengths
+	//probe header length
 	let mut file_header_data=Vec::new();
 	binrw::BinWrite::write_le(&file_header,&mut std::io::Cursor::new(&mut file_header_data)).map_err(Error::InvalidData)?;
 	let mut map_header_data=Vec::new();
 	binrw::BinWrite::write_le(&map_header,&mut std::io::Cursor::new(&mut map_header_data)).map_err(Error::InvalidData)?;
 
 	//update file header according to probe data
-	let offset=file_header_data.len() as u64+map_header_data.len() as u64;
+	let mut offset=file_header_data.len() as u64;
 	file_header.priming=offset;
-	for position in &mut file_header.block_location{
+	file_header.block_location[0]=offset;
+	offset+=map_header_data.len() as u64;
+	for position in &mut file_header.block_location[1..]{
 		*position+=offset;
 	}
 
-	//write file header
+	//write (updated) file header
 	writer.write_le(&file_header).map_err(Error::InvalidData)?;
 	//write map header
 	writer.write(&map_header_data).map_err(Error::IO)?;