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:
parent
aa4a7da90c
commit
1620098672
9 changed files with 35 additions and 23 deletions
|
|
@ -3,14 +3,14 @@ use anyhow::Result;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
pub trait Decoder: Sized {
|
pub trait Decoder: Sized {
|
||||||
type Opt;
|
type Opt<'o>;
|
||||||
fn decode(raw: &Raw, offset: usize, size: usize, opt: Option<Self::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;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Decoder for String {
|
impl Decoder for String {
|
||||||
type Opt = ();
|
type Opt<'o> = ();
|
||||||
fn decode(raw: &Raw, offset: usize, size: usize, _: Option<()>) -> Result<Self> {
|
fn decode(raw: &Raw, offset: usize, size: usize, _: Option<()>) -> Result<Self> {
|
||||||
let str = &raw.mem[offset..];
|
let str = &raw.mem[offset..];
|
||||||
match str.iter().position(|&c| c == 0) {
|
match str.iter().position(|&c| c == 0) {
|
||||||
|
|
|
||||||
|
|
@ -12,11 +12,28 @@ pub enum EntityEncoding {
|
||||||
World
|
World
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait EntityOwner<'a> {
|
pub struct EntityList {}
|
||||||
fn get_entity_encoding(&self) -> EntityEncoding;
|
|
||||||
fn add_new_type(name: FString) -> usize;
|
impl EntityList {
|
||||||
fn add_or_get_type(name: FString) -> usize;
|
fn get_entity_encoding(&self) -> EntityEncoding {
|
||||||
fn get_type_name(type_idx: usize) -> &'a FString;
|
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 {
|
||||||
|
|
@ -26,14 +43,9 @@ pub struct Entity {
|
||||||
enc_size: usize
|
enc_size: usize
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct EntityOpt {
|
|
||||||
//pub origin: EntityOrigin,
|
|
||||||
pub type_idx: usize
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Decoder for Entity {
|
impl Decoder for Entity {
|
||||||
type Opt = EntityOpt;
|
type Opt<'o> = &'o EntityList;
|
||||||
fn decode(raw: &Raw, offset: usize, _: usize, opt: Option<Self::Opt>) -> Result<Self> {
|
fn decode<'o>(raw: &Raw, offset: usize, _: usize, opt: Option<Self::Opt<'o>>) -> Result<Self> {
|
||||||
let rd = ReadStream::new(raw, offset);
|
let rd = ReadStream::new(raw, offset);
|
||||||
let opt = match opt {
|
let opt = match opt {
|
||||||
Some(opt) => opt,
|
Some(opt) => opt,
|
||||||
|
|
|
||||||
|
|
@ -76,7 +76,7 @@ impl ESHValue {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Decoder for ESHValue {
|
impl Decoder for ESHValue {
|
||||||
type Opt = ();
|
type Opt<'o> = ();
|
||||||
fn decode(raw: &Raw, offset: usize, _: usize, _: Option<()>) -> Result<Self> {
|
fn decode(raw: &Raw, offset: usize, _: usize, _: Option<()>) -> Result<Self> {
|
||||||
let mut rd = ReadStream::new(raw, offset);
|
let mut rd = ReadStream::new(raw, offset);
|
||||||
let data_type = rd.read_u32()?;
|
let data_type = rd.read_u32()?;
|
||||||
|
|
@ -249,7 +249,7 @@ pub struct ESH {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Decoder for ESH {
|
impl Decoder for ESH {
|
||||||
type Opt = ();
|
type Opt<'o> = ();
|
||||||
fn decode(raw: &Raw, offset: usize, _: usize, _: Option<()>) -> Result<Self> {
|
fn decode(raw: &Raw, offset: usize, _: usize, _: Option<()>) -> Result<Self> {
|
||||||
let mut rd = ReadStream::new(raw, offset);
|
let mut rd = ReadStream::new(raw, offset);
|
||||||
let tag: Tag = rd.read(0)?;
|
let tag: Tag = rd.read(0)?;
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ pub struct FString {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Decoder for FString {
|
impl Decoder for FString {
|
||||||
type Opt = ();
|
type Opt<'o> = ();
|
||||||
fn decode(raw: &Raw, offset: usize, _: usize, _: Option<()>) -> Result<Self> {
|
fn decode(raw: &Raw, offset: usize, _: usize, _: Option<()>) -> Result<Self> {
|
||||||
let mut rdr = Cursor::new(&raw.mem[offset..]);
|
let mut rdr = Cursor::new(&raw.mem[offset..]);
|
||||||
let flen = rdr.read_u32::<LittleEndian>()? as usize;
|
let flen = rdr.read_u32::<LittleEndian>()? as usize;
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ pub struct SGD {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Decoder for SGD {
|
impl Decoder for SGD {
|
||||||
type Opt = ();
|
type Opt<'o> = ();
|
||||||
fn decode(raw: &Raw, offset: usize, _: usize, _: Option<()>) -> Result<Self> {
|
fn decode(raw: &Raw, offset: usize, _: usize, _: Option<()>) -> Result<Self> {
|
||||||
let mut rd = ReadStream::new(raw, offset);
|
let mut rd = ReadStream::new(raw, offset);
|
||||||
let tag: Tag = rd.read(0)?;
|
let tag: Tag = rd.read(0)?;
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ pub struct SSG {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Decoder for SSG {
|
impl Decoder for SSG {
|
||||||
type Opt = ();
|
type Opt<'o> = ();
|
||||||
fn decode(raw: &Raw, offset: usize, _: usize, _: Option<()>) -> Result<Self> {
|
fn decode(raw: &Raw, offset: usize, _: usize, _: Option<()>) -> Result<Self> {
|
||||||
let mut rd = ReadStream::new(raw, offset);
|
let mut rd = ReadStream::new(raw, offset);
|
||||||
let tag: Tag = rd.read(0)?;
|
let tag: Tag = rd.read(0)?;
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@ 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<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))?;
|
let val = T::decode(&self.raw, self.offset(), size, Some(opt))?;
|
||||||
self.skip(val.get_enc_size());
|
self.skip(val.get_enc_size());
|
||||||
Ok(val)
|
Ok(val)
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ pub struct Tag {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Decoder for Tag {
|
impl Decoder for Tag {
|
||||||
type Opt = ();
|
type Opt<'o> = ();
|
||||||
fn decode(raw: &Raw, offset: usize, size: usize, _: Option<()>) -> Result<Self> {
|
fn decode(raw: &Raw, offset: usize, size: usize, _: Option<()>) -> Result<Self> {
|
||||||
let mut rd = ReadStream::new(raw, offset);
|
let mut rd = ReadStream::new(raw, offset);
|
||||||
let name: String = rd.read(0)?;
|
let name: String = rd.read(0)?;
|
||||||
|
|
|
||||||
|
|
@ -67,7 +67,7 @@ impl World {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Decoder for World {
|
impl Decoder for World {
|
||||||
type Opt = ();
|
type Opt<'o> = ();
|
||||||
fn decode(raw: &Raw, offset: usize, size: usize, _: Option<()>) -> Result<Self> {
|
fn decode(raw: &Raw, offset: usize, size: usize, _: Option<()>) -> Result<Self> {
|
||||||
let mut enc = ReadStream::new(raw, offset);
|
let mut enc = ReadStream::new(raw, offset);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue