diff --git a/src/tests.rs b/src/tests.rs index 1e44e2a..d9c1da7 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -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 } diff --git a/src/v1.rs b/src/v1.rs index 6762578..7e8415b 100644 --- a/src/v1.rs +++ b/src/v1.rs @@ -342,6 +342,20 @@ pub struct FileHeader{ #[br(count=num_realtime_events)] pub realtime_blocks_timeline:Vec, } +pub struct BlockInfo{ + start:u32, + length:u32, +} +impl FileHeader{ + pub fn block_info(&self,block_id:BlockId)->Result{ + 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{ pub header:FileHeader, @@ -363,17 +377,12 @@ impl FileData{ fn data_mut(&mut self)->&mut R{ &mut self.data } - fn block_reader(&mut self,header:&FileHeader,block_id:BlockId)->Result,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,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{ - let data=self.block_reader(header,block_id)?; + pub fn read_block_info(&mut self,block_info:BlockInfo)->Result{ + let data=self.block_reader(block_info)?; let block=Block::read(data).map_err(Error::InvalidData)?; Ok(block) }