diff --git a/lib/snf/src/file.rs b/lib/snf/src/file.rs index fb04c3e..24e8b73 100644 --- a/lib/snf/src/file.rs +++ b/lib/snf/src/file.rs @@ -73,6 +73,16 @@ pub struct Header{ #[br(count=block_count+1)] pub block_location:Vec, } +impl Header{ + pub const fn calculate_size(block_count:u32)->usize{ + 4 // fourcc + +4 // version + +8 // priming + +16 // resource + +4 // block_count + +(block_count as usize+1)*8 // block_location + } +} #[binrw] #[brw(little)] diff --git a/lib/snf/src/map.rs b/lib/snf/src/map.rs index 69c8b72..d8987c9 100644 --- a/lib/snf/src/map.rs +++ b/lib/snf/src/map.rs @@ -412,28 +412,26 @@ pub fn write_map(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(), }; - let mut file_header=crate::file::Header{ + //probe header length + 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)?; + + // calculate final file header + let mut offset=crate::file::Header::calculate_size(block_count) as u64; + offset+=map_header_data.len() as u64; + // priming includes map header + let priming=offset; + for position in &mut block_location{ + *position+=offset; + } + let file_header=crate::file::Header{ fourcc:crate::file::FourCC::Map, version:0, - priming:0, + priming, resource:0, block_count, block_location, }; - //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 mut offset=file_header_data.len() as u64; - file_header.priming=offset; - 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 (updated) file header writer.write_le(&file_header).map_err(Error::InvalidData)?;