slightly different api

This commit is contained in:
Quaternions 2024-12-31 03:46:11 -08:00
parent 3ddd31a489
commit 8b976c8e7d
2 changed files with 23 additions and 12 deletions

View File

@ -8,13 +8,15 @@ fn _1()->Result<(),crate::v1::Error>{
println!("header={:?}",bot_file.header);
for &TimedBlockId{time,block_id} in &bot_file.header.offline_blocks_timeline{
println!("offline time={} block_id={:?}",time,block_id);
let block=bot_file.data.read_block(&bot_file.header,block_id)?;
let block_info=bot_file.header.block_info(block_id)?;
let block=bot_file.data.read_block_info(block_info)?;
// offline blocks include the following event types:
// World, Gravity, Run, Camera, Setting
}
for &TimedBlockId{time,block_id} in &bot_file.header.realtime_blocks_timeline{
println!("realtime time={} block_id={:?}",time,block_id);
let block=bot_file.data.read_block(&bot_file.header,block_id)?;
let block_info=bot_file.header.block_info(block_id)?;
let block=bot_file.data.read_block_info(block_info)?;
// realtime blocks include the following event types:
// Input, Output, Sound
}

View File

@ -342,6 +342,20 @@ pub struct FileHeader{
#[br(count=num_realtime_events)]
pub realtime_blocks_timeline:Vec<TimedBlockId>,
}
pub struct BlockInfo{
start:u32,
length:u32,
}
impl FileHeader{
pub fn block_info(&self,block_id:BlockId)->Result<BlockInfo,Error>{
if self.block_positions.len() as u32<=block_id.0{
return Err(Error::InvalidBlockId(block_id));
}
let start=self.block_positions[block_id.0 as usize];
let end=self.block_positions[block_id.0 as usize+1];
Ok(BlockInfo{start,length:end-start})
}
}
pub struct File<R:BinReaderExt>{
pub header:FileHeader,
@ -363,17 +377,12 @@ impl<R:BinReaderExt> FileData<R>{
fn data_mut(&mut self)->&mut R{
&mut self.data
}
fn block_reader(&mut self,header:&FileHeader,block_id:BlockId)->Result<binrw::io::TakeSeek<&mut R>,Error>{
if header.block_positions.len() as u32<=block_id.0{
return Err(Error::InvalidBlockId(block_id))
}
let block_start=header.block_positions[block_id.0 as usize] as u64;
let block_end=header.block_positions[block_id.0 as usize+1] as u64;
self.data.seek(std::io::SeekFrom::Start(block_start)).map_err(Error::Seek)?;
Ok(self.data_mut().take_seek(block_end-block_start))
fn block_reader(&mut self,block_info:BlockInfo)->Result<binrw::io::TakeSeek<&mut R>,Error>{
self.data.seek(std::io::SeekFrom::Start(block_info.start as u64)).map_err(Error::Seek)?;
Ok(self.data_mut().take_seek(block_info.length as u64))
}
pub fn read_block(&mut self,header:&FileHeader,block_id:BlockId)->Result<Block,Error>{
let data=self.block_reader(header,block_id)?;
pub fn read_block_info(&mut self,block_info:BlockInfo)->Result<Block,Error>{
let data=self.block_reader(block_info)?;
let block=Block::read(data).map_err(Error::InvalidData)?;
Ok(block)
}