From 162009867289d9a14ec6064d4bc0890bfaf02e57 Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Sun, 3 Sep 2023 00:52:29 +0300 Subject: [PATCH] add GAT to Decoder Opt (so there will be lifetime), introducce EntityList which will control behavior of entity decoding/encoding and management of entities --- src/fot/decoder.rs | 6 +++--- src/fot/entity.rs | 36 ++++++++++++++++++++++++------------ src/fot/esh.rs | 4 ++-- src/fot/fstring.rs | 2 +- src/fot/sgd.rs | 2 +- src/fot/ssg.rs | 2 +- src/fot/stream.rs | 2 +- src/fot/tag.rs | 2 +- src/fot/world.rs | 2 +- 9 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/fot/decoder.rs b/src/fot/decoder.rs index b91806d..8eb9b59 100644 --- a/src/fot/decoder.rs +++ b/src/fot/decoder.rs @@ -3,14 +3,14 @@ use anyhow::Result; use std::str; pub trait Decoder: Sized { - type Opt; - fn decode(raw: &Raw, offset: usize, size: usize, opt: Option) -> Result; + type Opt<'o>; + fn decode(raw: &Raw, offset: usize, size: usize, opt: Option>) -> Result; fn encode(&self) -> Result; fn get_enc_size(&self) -> usize; } impl Decoder for String { - type Opt = (); + type Opt<'o> = (); fn decode(raw: &Raw, offset: usize, size: usize, _: Option<()>) -> Result { let str = &raw.mem[offset..]; match str.iter().position(|&c| c == 0) { diff --git a/src/fot/entity.rs b/src/fot/entity.rs index 63bf7dc..8311a8d 100644 --- a/src/fot/entity.rs +++ b/src/fot/entity.rs @@ -12,11 +12,28 @@ pub enum EntityEncoding { World } -pub trait EntityOwner<'a> { - fn get_entity_encoding(&self) -> EntityEncoding; - fn add_new_type(name: FString) -> usize; - fn add_or_get_type(name: FString) -> usize; - fn get_type_name(type_idx: usize) -> &'a FString; +pub struct EntityList {} + +impl EntityList { + fn get_entity_encoding(&self) -> EntityEncoding { + todo!(); + } + + fn get_entity_tag(&self) -> &Tag { + todo!(); + } + + fn add_new_type(&mut self, name: FString) -> usize { + todo!(); + } + + fn add_or_get_type(&mut self, name: FString) -> usize { + todo!(); + } + + fn get_type_name(&self, type_idx: usize) -> &FString { + todo!(); + } } pub struct Entity { @@ -26,14 +43,9 @@ pub struct Entity { enc_size: usize } -pub struct EntityOpt { - //pub origin: EntityOrigin, - pub type_idx: usize -} - impl Decoder for Entity { - type Opt = EntityOpt; - fn decode(raw: &Raw, offset: usize, _: usize, opt: Option) -> Result { + type Opt<'o> = &'o EntityList; + fn decode<'o>(raw: &Raw, offset: usize, _: usize, opt: Option>) -> Result { let rd = ReadStream::new(raw, offset); let opt = match opt { Some(opt) => opt, diff --git a/src/fot/esh.rs b/src/fot/esh.rs index afda8dc..a3b60a4 100644 --- a/src/fot/esh.rs +++ b/src/fot/esh.rs @@ -76,7 +76,7 @@ impl ESHValue { } impl Decoder for ESHValue { - type Opt = (); + type Opt<'o> = (); fn decode(raw: &Raw, offset: usize, _: usize, _: Option<()>) -> Result { let mut rd = ReadStream::new(raw, offset); let data_type = rd.read_u32()?; @@ -249,7 +249,7 @@ pub struct ESH { } impl Decoder for ESH { - type Opt = (); + type Opt<'o> = (); fn decode(raw: &Raw, offset: usize, _: usize, _: Option<()>) -> Result { let mut rd = ReadStream::new(raw, offset); let tag: Tag = rd.read(0)?; diff --git a/src/fot/fstring.rs b/src/fot/fstring.rs index 25ffc57..93b147a 100644 --- a/src/fot/fstring.rs +++ b/src/fot/fstring.rs @@ -24,7 +24,7 @@ pub struct FString { } impl Decoder for FString { - type Opt = (); + type Opt<'o> = (); fn decode(raw: &Raw, offset: usize, _: usize, _: Option<()>) -> Result { let mut rdr = Cursor::new(&raw.mem[offset..]); let flen = rdr.read_u32::()? as usize; diff --git a/src/fot/sgd.rs b/src/fot/sgd.rs index 8f27b7f..dd0ae95 100644 --- a/src/fot/sgd.rs +++ b/src/fot/sgd.rs @@ -15,7 +15,7 @@ pub struct SGD { } impl Decoder for SGD { - type Opt = (); + type Opt<'o> = (); fn decode(raw: &Raw, offset: usize, _: usize, _: Option<()>) -> Result { let mut rd = ReadStream::new(raw, offset); let tag: Tag = rd.read(0)?; diff --git a/src/fot/ssg.rs b/src/fot/ssg.rs index e66bb94..5634e37 100644 --- a/src/fot/ssg.rs +++ b/src/fot/ssg.rs @@ -11,7 +11,7 @@ pub struct SSG { } impl Decoder for SSG { - type Opt = (); + type Opt<'o> = (); fn decode(raw: &Raw, offset: usize, _: usize, _: Option<()>) -> Result { let mut rd = ReadStream::new(raw, offset); let tag: Tag = rd.read(0)?; diff --git a/src/fot/stream.rs b/src/fot/stream.rs index 1f8c799..365a9e7 100644 --- a/src/fot/stream.rs +++ b/src/fot/stream.rs @@ -46,7 +46,7 @@ impl<'a> ReadStream<'a> { // read_opt - decode with optional paramters. required for complex structure // with different origins (save / entfile) like entities - pub fn read_opt(&mut self, size: usize, opt: T::Opt) -> Result { + pub fn read_opt<'o, T: Decoder>(&mut self, size: usize, opt: T::Opt<'o>) -> Result { let val = T::decode(&self.raw, self.offset(), size, Some(opt))?; self.skip(val.get_enc_size()); Ok(val) diff --git a/src/fot/tag.rs b/src/fot/tag.rs index 1e9fea9..d964491 100644 --- a/src/fot/tag.rs +++ b/src/fot/tag.rs @@ -10,7 +10,7 @@ pub struct Tag { } impl Decoder for Tag { - type Opt = (); + type Opt<'o> = (); fn decode(raw: &Raw, offset: usize, size: usize, _: Option<()>) -> Result { let mut rd = ReadStream::new(raw, offset); let name: String = rd.read(0)?; diff --git a/src/fot/world.rs b/src/fot/world.rs index b5fb00d..9f74d8a 100644 --- a/src/fot/world.rs +++ b/src/fot/world.rs @@ -67,7 +67,7 @@ impl World { } impl Decoder for World { - type Opt = (); + type Opt<'o> = (); fn decode(raw: &Raw, offset: usize, size: usize, _: Option<()>) -> Result { let mut enc = ReadStream::new(raw, offset);