add EntityList that will decode types and entities and manage them

This commit is contained in:
mykola2312 2023-09-03 01:45:47 +03:00
parent 9b151e509b
commit 28c5bf6989
5 changed files with 52 additions and 47 deletions

View file

@ -1,5 +1,6 @@
mod decoder; mod decoder;
mod entity; mod entity;
mod entitylist;
mod esh; mod esh;
mod fstring; mod fstring;
mod raw; mod raw;

View file

@ -8,9 +8,8 @@ pub trait Decoder: Sized {
fn get_enc_size(&self) -> usize; fn get_enc_size(&self) -> usize;
} }
pub trait DecoderOpt: Sized { pub trait DecoderOpt<Opt>: Sized {
type Opt<'o>; fn decode(raw: &Raw, offset: usize, size: usize, opt: Opt) -> Result<Self>;
fn decode(raw: &Raw, offset: usize, size: usize, opt: Option<Self::Opt<'_>>) -> Result<Self>;
fn encode(&self) -> Result<Raw>; fn encode(&self) -> Result<Raw>;
fn get_enc_size(&self) -> usize; fn get_enc_size(&self) -> usize;
} }

View file

@ -1,56 +1,23 @@
use super::raw::Raw;
use super::tag::Tag;
use super::esh::ESH;
use super::decoder::DecoderOpt; use super::decoder::DecoderOpt;
use super::entitylist::{EntityEncoding, EntityList};
use super::esh::ESH;
use super::fstring::FString; use super::fstring::FString;
use super::raw::Raw;
use super::stream::{ReadStream, WriteStream}; use super::stream::{ReadStream, WriteStream};
use super::tag::Tag;
use anyhow::anyhow; use anyhow::anyhow;
use anyhow::Result; use anyhow::Result;
pub enum EntityEncoding {
File,
World
}
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 { pub struct Entity {
pub flags: u32, pub flags: u32,
pub type_idx: usize, pub type_idx: usize,
pub esh: ESH, pub esh: ESH,
enc_size: usize enc_size: usize,
} }
impl DecoderOpt for Entity { impl DecoderOpt<&EntityList> for Entity {
type Opt<'o> = &'o EntityList; fn decode(raw: &Raw, offset: usize, _: usize, opt: &EntityList) -> Result<Self> {
fn decode<'o>(raw: &Raw, offset: usize, _: usize, opt: Option<Self::Opt<'o>>) -> Result<Self> { let mut rd = ReadStream::new(raw, offset);
let rd = ReadStream::new(raw, offset);
let opt = match opt {
Some(opt) => opt,
None => return Err(anyhow!("no EntityOpt was provided!"))
};
todo!(); todo!();
} }

38
src/fot/entitylist.rs Normal file
View file

@ -0,0 +1,38 @@
use super::decoder::DecoderOpt;
use super::entity::Entity;
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;
pub enum EntityEncoding {
File,
World,
}
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!();
}
}

View file

@ -46,8 +46,8 @@ impl<'a> ReadStream<'a> {
// read_opt - decode with optional paramters. required for complex structure // read_opt - decode with optional paramters. required for complex structure
// with different origins (save / entfile) like entities // with different origins (save / entfile) like entities
pub fn read_opt<'o, T: DecoderOpt>(&mut self, size: usize, opt: T::Opt<'o>) -> Result<T> { pub fn read_opt<T: DecoderOpt<Opt>, Opt>(&mut self, size: usize, opt: Opt) -> Result<T> {
let val = T::decode(&self.raw, self.offset(), size, Some(opt))?; let val = T::decode(&self.raw, self.offset(), size, opt)?;
self.skip(val.get_enc_size()); self.skip(val.get_enc_size());
Ok(val) Ok(val)
} }