This commit is contained in:
Quaternions 2024-07-26 15:25:00 -07:00
parent b6fede20bf
commit 943d78a9fb
2 changed files with 157 additions and 1 deletions

View File

@ -101,6 +101,7 @@ impl<R:BinReaderExt> File<R>{
}
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))
}

View File

@ -163,6 +163,102 @@ struct MapHeader{
render_configs:Vec<newtypes::model::RenderConfig>,
}
fn read_vec<'a,R:BinReaderExt,T:binrw::BinRead>(len:usize,input:&mut R)->Result<Vec<T>,binrw::Error>
where
<T as binrw::BinRead>::Args<'a>: Default
{
let mut vec=Vec::with_capacity(len);
for _ in 0..len{
vec.push(input.read_le()?);
}
Ok(vec)
}
fn read_stage<R:BinReaderExt>(input:&mut R)->Result<newtypes::gameplay_modes::Stage,binrw::Error>{
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<R:BinReaderExt>(len:usize,input:&mut R)->Result<Vec<newtypes::gameplay_modes::Stage>,binrw::Error>{
let mut vec=Vec::with_capacity(len);
for _ in 0..len{
vec.push(read_stage(input)?);
}
Ok(vec)
}
fn read_mode<R:BinReaderExt>(input:&mut R)->Result<newtypes::gameplay_modes::Mode,binrw::Error>{
//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<R:BinReaderExt>(len:usize,input:&mut R)->Result<Vec<newtypes::gameplay_modes::Mode>,binrw::Error>{
let mut vec=Vec::with_capacity(len);
for _ in 0..len{
vec.push(read_mode(input)?);
}
Ok(vec)
}
fn read_map_header<R:BinReaderExt>(input:&mut R)->Result<MapHeader,binrw::Error>{
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<SpacialBlockHeader>=read_vec(num_spacial_blocks as usize,input)?;
let resource_blocks:Vec<ResourceBlockHeader>=read_vec(num_resource_blocks as usize,input)?;
let external_resources:Vec<ResourceExternalHeader>=read_vec(num_resources_external as usize,input)?;
let modes:Vec<newtypes::gameplay_modes::Mode>=read_modes(num_modes as usize,input)?;
let attributes:Vec<newtypes::gameplay_attributes::CollisionAttributes>=read_vec(num_attributes as usize,input)?;
let render_configs:Vec<newtypes::model::RenderConfig>=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<R:BinReaderExt>{
impl<R:BinReaderExt> StreamableMap<R>{
pub(crate) fn new(mut file:crate::file::File<R>)->Result<Self,Error>{
//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<R:BinReaderExt> StreamableMap<R>{
}
}
use binrw::BinWrite;
fn write_mode<W:std::io::Seek+std::io::Write>(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<W:std::io::Seek+std::io::Write>(modes:&Vec<newtypes::gameplay_modes::Mode>,output:&mut W)->Result<(),binrw::Error>{
for mode in modes{
write_mode(mode,output)?;
}
Ok(())
}
fn write_map_header<W:std::io::Seek+std::io::Write>(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<u64>,
@ -373,6 +512,7 @@ pub fn write_map<W:BinWriterExt>(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<W:BinWriterExt>(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<W:BinWriterExt>(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<W:BinWriterExt>(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<W:BinWriterExt>(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