From 943d78a9fb4efe83ad30e6bc0d23c7e0f1f00548 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Fri, 26 Jul 2024 15:25:00 -0700 Subject: [PATCH] debug --- src/file.rs | 1 + src/map.rs | 157 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 157 insertions(+), 1 deletion(-) diff --git a/src/file.rs b/src/file.rs index 1cc2d8e..c379884 100644 --- a/src/file.rs +++ b/src/file.rs @@ -101,6 +101,7 @@ impl File{ } let block_start=self.header.block_location[block_id.get() as usize]; let block_end=self.header.block_location[block_id.get() as usize+1]; + //println!("reading from {} to {}",block_start,block_end); self.data.seek(std::io::SeekFrom::Start(block_start)).map_err(Error::Seek)?; Ok(self.as_mut().take_seek(block_end-block_start)) } diff --git a/src/map.rs b/src/map.rs index 55313f9..c3c20f7 100644 --- a/src/map.rs +++ b/src/map.rs @@ -163,6 +163,102 @@ struct MapHeader{ render_configs:Vec, } +fn read_vec<'a,R:BinReaderExt,T:binrw::BinRead>(len:usize,input:&mut R)->Result,binrw::Error> +where + ::Args<'a>: Default +{ + let mut vec=Vec::with_capacity(len); + for _ in 0..len{ + vec.push(input.read_le()?); + } + Ok(vec) +} + +fn read_stage(input:&mut R)->Result{ + let spawn=input.read_le()?; + let ordered_checkpoints_count=input.read_le()?; + let unordered_checkpoints_count=input.read_le()?; + //println!("ordered_checkpoints_count={ordered_checkpoints_count}"); + //println!("unordered_checkpoints_count={unordered_checkpoints_count}"); + let ordered_checkpoints=read_vec(ordered_checkpoints_count as usize,input)?; + let unordered_checkpoints=read_vec(unordered_checkpoints_count as usize,input)?; + Ok(newtypes::gameplay_modes::Stage{ + spawn, + ordered_checkpoints_count, + unordered_checkpoints_count, + ordered_checkpoints, + unordered_checkpoints, + }) +} + +fn read_stages(len:usize,input:&mut R)->Result,binrw::Error>{ + let mut vec=Vec::with_capacity(len); + for _ in 0..len{ + vec.push(read_stage(input)?); + } + Ok(vec) +} + +fn read_mode(input:&mut R)->Result{ + //println!("read_mode pos={}",input.stream_position().unwrap()); + let header:newtypes::gameplay_modes::ModeHeader=input.read_le()?; + //println!("mode.header pos={}",input.stream_position().unwrap()); + let style=input.read_le()?; + //println!("mode.style pos={}",input.stream_position().unwrap()); + let start=input.read_le()?; + //println!("mode.start pos={}",input.stream_position().unwrap()); + let zones=read_vec(header.zones as usize,input)?; + //println!("mode.zones pos={}",input.stream_position().unwrap()); + let stages=read_stages(header.stages as usize,input)?; + //println!("mode.stages pos={}",input.stream_position().unwrap()); + let elements=read_vec(header.elements as usize,input)?; + Ok(newtypes::gameplay_modes::Mode{ + header, + style, + start, + zones, + stages, + elements, + }) +} + +fn read_modes(len:usize,input:&mut R)->Result,binrw::Error>{ + let mut vec=Vec::with_capacity(len); + for _ in 0..len{ + vec.push(read_mode(input)?); + } + Ok(vec) +} + +fn read_map_header(input:&mut R)->Result{ + let num_spacial_blocks:u32=input.read_le()?; + let num_resource_blocks:u32=input.read_le()?; + let num_resources_external:u32=input.read_le()?; + let num_modes:u32=input.read_le()?; + let num_attributes:u32=input.read_le()?; + let num_render_configs:u32=input.read_le()?; + let spacial_blocks:Vec=read_vec(num_spacial_blocks as usize,input)?; + let resource_blocks:Vec=read_vec(num_resource_blocks as usize,input)?; + let external_resources:Vec=read_vec(num_resources_external as usize,input)?; + let modes:Vec=read_modes(num_modes as usize,input)?; + let attributes:Vec=read_vec(num_attributes as usize,input)?; + let render_configs:Vec=read_vec(num_render_configs as usize,input)?; + Ok(MapHeader{ + num_spacial_blocks, + num_resource_blocks, + num_resources_external, + num_modes, + num_attributes, + num_render_configs, + spacial_blocks, + resource_blocks, + external_resources, + modes, + attributes, + render_configs, + }) +} + #[binrw] #[brw(little)] struct Region{ @@ -213,6 +309,8 @@ pub struct StreamableMap{ impl StreamableMap{ pub(crate) fn new(mut file:crate::file::File)->Result{ //assume the file seek is in the right place to start reading a map header + //let mut f=file.block_reader(BlockId::new(0)).map_err(Error::File)?; + //println!("position={}",f.stream_position().unwrap()); let header:MapHeader=file.as_mut().read_le().map_err(Error::InvalidHeader)?; let modes=header.modes.into_iter().map(Into::into).collect(); let attributes=header.attributes.into_iter().map(Into::into).collect(); @@ -305,6 +403,47 @@ impl StreamableMap{ } } +use binrw::BinWrite; + +fn write_mode(mode:&newtypes::gameplay_modes::Mode,output:&mut W)->Result<(),binrw::Error>{ + println!("write_mode pos={}",output.stream_position().unwrap()); + mode.header.write_le(output)?; + println!("mode.header pos={}",output.stream_position().unwrap()); + mode.style.write_le(output)?; + println!("mode.style pos={}",output.stream_position().unwrap()); + mode.start.write_le(output)?; + println!("mode.start pos={}",output.stream_position().unwrap()); + mode.zones.write_le(output)?; + println!("mode.zones pos={}",output.stream_position().unwrap()); + mode.stages.write_le(output)?; + println!("mode.stages pos={}",output.stream_position().unwrap()); + mode.elements.write_le(output)?; + Ok(()) +} + +fn write_modes(modes:&Vec,output:&mut W)->Result<(),binrw::Error>{ + for mode in modes{ + write_mode(mode,output)?; + } + Ok(()) +} + +fn write_map_header(map_header:&MapHeader,output:&mut W)->Result<(),binrw::Error>{ + map_header.num_spacial_blocks.write_le(output)?; + map_header.num_resource_blocks.write_le(output)?; + map_header.num_resources_external.write_le(output)?; + map_header.num_modes.write_le(output)?; + map_header.num_attributes.write_le(output)?; + map_header.num_render_configs.write_le(output)?; + map_header.spacial_blocks.write_le(output)?; + map_header.resource_blocks.write_le(output)?; + map_header.external_resources.write_le(output)?; + write_modes(&map_header.modes,output)?; + map_header.attributes.write_le(output)?; + map_header.render_configs.write_le(output)?; + Ok(()) +} + const BVH_NODE_MAX_WEIGHT:usize=64*1024;//64 kB fn collect_spacial_blocks( block_location:&mut Vec, @@ -373,6 +512,7 @@ pub fn write_map(mut writer:W,map:strafesnet_common::map::Comple let mut cursor_to_data=std::io::Cursor::new(&mut sequential_block_data); collect_spacial_blocks(&mut block_location,&mut spacial_blocks,&mut cursor_to_data,bvh)?; let mut block_count=spacial_blocks.len() as u32+1;//continue block id + println!("spacial_blocks={}",block_count); let mut resource_blocks=Vec::new();//for map header //meshes for mesh in map.meshes.into_iter(){ @@ -385,6 +525,8 @@ pub fn write_map(mut writer:W,map:strafesnet_common::map::Comple binrw::BinWrite::write_le(&serializable_mesh,&mut cursor_to_data).map_err(Error::InvalidData)?; block_location.push(cursor_to_data.position()); } + let aaa=block_count; + println!("mesh_blocks={}",block_count-(spacial_blocks.len() as u32)); //textures for mut texture in map.textures.into_iter(){ resource_blocks.push(ResourceBlockHeader{ @@ -395,6 +537,7 @@ pub fn write_map(mut writer:W,map:strafesnet_common::map::Comple sequential_block_data.append(&mut texture); block_location.push(sequential_block_data.len() as u64); } + println!("texture_blocks={}",block_count-aaa); //build header let map_header=MapHeader{ num_spacial_blocks:spacial_blocks.len() as u32, @@ -422,7 +565,7 @@ pub fn write_map(mut writer:W,map:strafesnet_common::map::Comple 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)?; + write_map_header(&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; @@ -433,6 +576,18 @@ pub fn write_map(mut writer:W,map:strafesnet_common::map::Comple *position+=offset; } + //debug + println!("block_count={}",file_header.block_count); + println!("block_locations[0..10]={:?}",&file_header.block_location[0..10]); + println!("file header len={}",file_header_data.len()); + println!("map header len={}",map_header_data.len()); + println!("num_spacial_blocks={}",map_header.num_spacial_blocks); + println!("num_resource_blocks={}",map_header.num_resource_blocks); + println!("num_resources_external={}",map_header.num_resources_external); + println!("num_modes={}",map_header.num_modes); + println!("num_attributes={}",map_header.num_attributes); + println!("num_render_configs={}",map_header.num_render_configs); + //write (updated) file header writer.write_le(&file_header).map_err(Error::InvalidData)?; //write map header