implement Entity decoding and encoding

This commit is contained in:
mykola2312 2023-09-03 02:20:59 +03:00
parent 28c5bf6989
commit 122ee7bfc0
3 changed files with 42 additions and 16 deletions

View file

@ -10,7 +10,7 @@ pub trait Decoder: Sized {
pub trait DecoderOpt<Opt>: Sized {
fn decode(raw: &Raw, offset: usize, size: usize, opt: Opt) -> Result<Self>;
fn encode(&self) -> Result<Raw>;
fn encode(&self, opt: Opt) -> Result<Raw>;
fn get_enc_size(&self) -> usize;
}

View file

@ -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<Self> {
impl DecoderOpt<&mut EntityList> for Entity {
fn decode(raw: &Raw, offset: usize, _: usize, opt: &mut EntityList) -> Result<Self> {
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<Raw> {
todo!();
fn encode(&self, opt: &mut EntityList) -> Result<Raw> {
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
}
}

View file

@ -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!();
}
}