diff --git a/src/tests.rs b/src/tests.rs index bcaa243..1e44e2a 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -6,15 +6,15 @@ fn _1()->Result<(),crate::v1::Error>{ let input=std::io::BufReader::new(file); let mut bot_file=crate::v1::File::new(input).unwrap(); println!("header={:?}",bot_file.header); - for TimedBlockId{time,block_id} in bot_file.header.offline_blocks_timeline.clone(){ + for &TimedBlockId{time,block_id} in &bot_file.header.offline_blocks_timeline{ println!("offline time={} block_id={:?}",time,block_id); - let block=bot_file.read_block(block_id)?; + let block=bot_file.data.read_block(&bot_file.header,block_id)?; // offline blocks include the following event types: // World, Gravity, Run, Camera, Setting } - for TimedBlockId{time,block_id} in bot_file.header.realtime_blocks_timeline.clone(){ + for &TimedBlockId{time,block_id} in &bot_file.header.realtime_blocks_timeline{ println!("realtime time={} block_id={:?}",time,block_id); - let block=bot_file.read_block(block_id)?; + let block=bot_file.data.read_block(&bot_file.header,block_id)?; // realtime blocks include the following event types: // Input, Output, Sound } diff --git a/src/v1.rs b/src/v1.rs index a15d53c..6762578 100644 --- a/src/v1.rs +++ b/src/v1.rs @@ -345,31 +345,35 @@ pub struct FileHeader{ pub struct File{ pub header:FileHeader, - //reference to the data - data:R, + pub data:FileData, } - impl File{ - pub fn new(mut input:R)->Result,binrw::Error>{ + pub fn new(mut data:R)->Result,binrw::Error>{ Ok(File{ - header:input.read_le()?, - data:input, + header:data.read_le()?, + data:FileData{data}, }) } +} + +pub struct FileData{ + data:R, +} +impl FileData{ fn data_mut(&mut self)->&mut R{ &mut self.data } - fn block_reader(&mut self,block_id:BlockId)->Result,Error>{ - if self.header.block_positions.len() as u32<=block_id.0{ + 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=self.header.block_positions[block_id.0 as usize] as u64; - let block_end=self.header.block_positions[block_id.0 as usize+1] as u64; + 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)) } - pub fn read_block(&mut self,block_id:BlockId)->Result{ - let data=self.block_reader(block_id)?; + pub fn read_block(&mut self,header:&FileHeader,block_id:BlockId)->Result{ + let data=self.block_reader(header,block_id)?; let block=Block::read(data).map_err(Error::InvalidData)?; Ok(block) }