diff --git a/src/bot.rs b/src/bot.rs index 3071a21..c5a5c0c 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -1,4 +1,5 @@ pub enum Error{ + InvalidHeader, InvalidSegment, } @@ -53,6 +54,9 @@ pub struct StreamableBot{ timeline:timeline::Timeline, } impl StreamableBot{ + pub fn new(file:crate::file::File)->Result{ + Err(Error::InvalidHeader) + } pub fn load_segment(&mut self,segment_id:u64)->Result{ //load region from disk //parse the models and determine what resources need to be loaded diff --git a/src/demo.rs b/src/demo.rs index ea66dc5..486f861 100644 --- a/src/demo.rs +++ b/src/demo.rs @@ -1,5 +1,5 @@ pub enum Error{ - // + InvalidHeader, } /* @@ -20,4 +20,9 @@ how to do worldstate for deathrun!? pub struct StreamableDemo{ map:Box, bots:Vec, +} +impl StreamableDemo{ + pub fn new(file:crate::file::File)->Result{ + Err(Error::InvalidHeader) + } } \ No newline at end of file diff --git a/src/file.rs b/src/file.rs index 0c2b4b9..d9ebb14 100644 --- a/src/file.rs +++ b/src/file.rs @@ -33,16 +33,16 @@ for block_id in 0..num_blocks{ //each block is compressed with zstd or gz or something */ - -pub enum Magic{ - Map, //"SNFM" - Bot, //"SNFB" - Demo, //"SNFD" +#[derive(Clone,Copy)] +pub(crate) enum FourCC{ + Map, + Bot, + Demo, } -pub struct Header{ +struct Header{ /// Type of file - magic:Magic, + fourcc:FourCC, /// Type version version:u32, /// Minimum data required to know the location of all streamable resources for this specific file @@ -51,11 +51,28 @@ pub struct Header{ resource:u128, } -pub struct BlockLayout{ +pub(crate) struct BlockLayout{ count:u64, location:Vec, } -pub struct File{ - //??? +pub(crate) struct File{ + header:Header, + block_layout:BlockLayout, + //reference to the data +} + +impl File{ + pub(crate) fn new(input:R)->Result{ + Self{ + header:input.read_le()?, + block_layout:input.read_le()?, + } + } + pub(crate) fn read_block(&mut self,block_id:u64)->Result,Error>{ + Err(Error::UnexpectedEOF) + } + pub(crate) fn fourcc(&self)->FourCC{ + self.header.fourcc + } } diff --git a/src/lib.rs b/src/lib.rs index e36ef03..2777453 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ pub mod bot; pub mod demo; pub enum Error{ + UnexpectedFourCC, Header(file::Error), Map(map::Error), Bot(bot::Error), @@ -18,9 +19,34 @@ pub enum SNF{ Demo(demo::StreamableDemo), } -pub fn read(input:R)->Result{ - //let header:file::Header=input.read()?; - Err(Error::Header(file::Error::InvalidMagic)) +pub fn read_snf(input:R)->Result{ + let file=file::File::read(input).map_err(|e|Error::Header(e))?; + Ok(match file.fourcc(){ + file::FourCC::Map=>SNF::Map(map::StreamableMap::new(file).map_err(|e|Error::Map(e))?), + file::FourCC::Bot=>SNF::Bot(bot::StreamableBot::new(file).map_err(|e|Error::Bot(e))?), + file::FourCC::Demo=>SNF::Demo(demo::StreamableDemo::new(file).map_err(|e|Error::Demo(e))?), + }) +} +pub fn read_map(input:R)->Result{ + let file=file::File::read(input).map_err(|e|Error::Header(e))?; + match file.fourcc(){ + file::FourCC::Map=>Ok(map::StreamableMap::new(file).map_err(|e|Error::Map(e))?), + _=>Err(Error::UnexpectedFourCC) + } +} +pub fn read_bot(input:R)->Result{ + let file=file::File::read(input).map_err(|e|Error::Header(e))?; + match file.fourcc(){ + file::FourCC::Bot=>Ok(bot::StreamableBot::new(file).map_err(|e|Error::Bot(e))?), + _=>Err(Error::UnexpectedFourCC) + } +} +pub fn read_demo(input:R)->Result{ + let file=file::File::read(input).map_err(|e|Error::Header(e))?; + match file.fourcc(){ + file::FourCC::Demo=>Ok(demo::StreamableDemo::new(file).map_err(|e|Error::Demo(e))?), + _=>Err(Error::UnexpectedFourCC) + } } #[cfg(test)] diff --git a/src/map.rs b/src/map.rs index 19f7dc6..53e6501 100644 --- a/src/map.rs +++ b/src/map.rs @@ -1,4 +1,5 @@ pub enum Error{ + InvalidHeader, InvalidNode, } @@ -82,6 +83,9 @@ pub struct StreamableMap{ resource_image:std::collections::HashMap, } impl StreamableMap{ + pub fn new(file:crate::file::File)->Result{ + Err(Error::InvalidHeader) + } pub fn load_node(&mut self,node_id:BvhNodeId)->Result,Error>{ //load region from disk //parse the models and determine what resources need to be loaded