diff --git a/src/fot/decoder.rs b/src/fot/decoder.rs index 7800edf..b4ebac2 100644 --- a/src/fot/decoder.rs +++ b/src/fot/decoder.rs @@ -10,7 +10,7 @@ pub trait Decoder: Sized { pub trait DecoderOpt: Sized { fn decode(raw: &Raw, offset: usize, size: usize, opt: Opt) -> Result; - fn encode(&self) -> Result; + fn encode(&self, opt: Opt) -> Result; fn get_enc_size(&self) -> usize; } diff --git a/src/fot/entity.rs b/src/fot/entity.rs index 3c0562f..3f007b5 100644 --- a/src/fot/entity.rs +++ b/src/fot/entity.rs @@ -1,13 +1,12 @@ use super::decoder::DecoderOpt; use super::entitylist::{EntityEncoding, EntityList}; use super::esh::ESH; -use super::fstring::FString; use super::raw::Raw; use super::stream::{ReadStream, WriteStream}; -use super::tag::Tag; -use anyhow::anyhow; use anyhow::Result; +const NO_FLAGS: u32 = 0; + pub struct Entity { pub flags: u32, pub type_idx: usize, @@ -15,18 +14,45 @@ pub struct Entity { enc_size: usize, } -impl DecoderOpt<&EntityList> for Entity { - fn decode(raw: &Raw, offset: usize, _: usize, opt: &EntityList) -> Result { +impl DecoderOpt<&mut EntityList> for Entity { + fn decode(raw: &Raw, offset: usize, _: usize, opt: &mut EntityList) -> Result { let mut rd = ReadStream::new(raw, offset); - - todo!(); + Ok(match opt.get_entity_encoding() { + EntityEncoding::File => { + let flags = NO_FLAGS; + let type_idx = opt.add_or_get_type(rd.read(0)?); + let esh: ESH = rd.read(0)?; + let enc_size = rd.offset() - offset; + Entity { flags, type_idx, esh, enc_size } + }, + EntityEncoding::World => { + let flags = rd.read_u32()?; + let type_idx = rd.read_u16()? as usize; + let esh: ESH = rd.read(0)?; + let enc_size = rd.offset() - offset; + Entity { flags, type_idx, esh, enc_size } + } + }) } - fn encode(&self) -> Result { - todo!(); + fn encode(&self, opt: &mut EntityList) -> Result { + let mut wd = WriteStream::new(self.get_enc_size()); + match opt.get_entity_encoding() { + EntityEncoding::File => { + wd.write(opt.get_entity_tag())?; + wd.write(opt.get_type_name(self.type_idx))?; + wd.write(&self.esh)?; + }, + EntityEncoding::World => { + wd.write_u32(self.flags)?; + wd.write_u16(self.type_idx as u16)?; + wd.write(&self.esh)?; + } + } + Ok(wd.into_raw(0, 0)) } fn get_enc_size(&self) -> usize { - todo!(); + self.enc_size } } diff --git a/src/fot/entitylist.rs b/src/fot/entitylist.rs index 85bed0a..295e076 100644 --- a/src/fot/entitylist.rs +++ b/src/fot/entitylist.rs @@ -16,23 +16,23 @@ pub enum EntityEncoding { pub struct EntityList {} impl EntityList { - fn get_entity_encoding(&self) -> EntityEncoding { + pub fn get_entity_encoding(&self) -> EntityEncoding { todo!(); } - fn get_entity_tag(&self) -> &Tag { + pub fn get_entity_tag(&self) -> &Tag { todo!(); } - fn add_new_type(&mut self, name: FString) -> usize { + pub fn add_new_type(&mut self, type_name: FString) -> usize { todo!(); } - fn add_or_get_type(&mut self, name: FString) -> usize { + pub fn add_or_get_type(&mut self, type_name: FString) -> usize { todo!(); } - fn get_type_name(&self, type_idx: usize) -> &FString { + pub fn get_type_name(&self, type_idx: usize) -> &FString { todo!(); } }