add GAT to Decoder Opt (so there will be lifetime), introducce EntityList which will control behavior of entity decoding/encoding and management of entities

This commit is contained in:
mykola2312 2023-09-03 00:52:29 +03:00
parent aa4a7da90c
commit 1620098672
9 changed files with 35 additions and 23 deletions

View file

@ -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<Self::Opt>) -> Result<Self>;
type Opt<'o>;
fn decode(raw: &Raw, offset: usize, size: usize, opt: Option<Self::Opt<'_>>) -> Result<Self>;
fn encode(&self) -> Result<Raw>;
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<Self> {
let str = &raw.mem[offset..];
match str.iter().position(|&c| c == 0) {

View file

@ -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<Self::Opt>) -> Result<Self> {
type Opt<'o> = &'o EntityList;
fn decode<'o>(raw: &Raw, offset: usize, _: usize, opt: Option<Self::Opt<'o>>) -> Result<Self> {
let rd = ReadStream::new(raw, offset);
let opt = match opt {
Some(opt) => opt,

View file

@ -76,7 +76,7 @@ impl ESHValue {
}
impl Decoder for ESHValue {
type Opt = ();
type Opt<'o> = ();
fn decode(raw: &Raw, offset: usize, _: usize, _: Option<()>) -> Result<Self> {
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<Self> {
let mut rd = ReadStream::new(raw, offset);
let tag: Tag = rd.read(0)?;

View file

@ -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<Self> {
let mut rdr = Cursor::new(&raw.mem[offset..]);
let flen = rdr.read_u32::<LittleEndian>()? as usize;

View file

@ -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<Self> {
let mut rd = ReadStream::new(raw, offset);
let tag: Tag = rd.read(0)?;

View file

@ -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<Self> {
let mut rd = ReadStream::new(raw, offset);
let tag: Tag = rd.read(0)?;

View file

@ -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<T: Decoder>(&mut self, size: usize, opt: T::Opt) -> Result<T> {
pub fn read_opt<'o, T: Decoder>(&mut self, size: usize, opt: T::Opt<'o>) -> Result<T> {
let val = T::decode(&self.raw, self.offset(), size, Some(opt))?;
self.skip(val.get_enc_size());
Ok(val)

View file

@ -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<Self> {
let mut rd = ReadStream::new(raw, offset);
let name: String = rd.read(0)?;

View file

@ -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<Self> {
let mut enc = ReadStream::new(raw, offset);