implement Entity decoding and encoding
This commit is contained in:
parent
28c5bf6989
commit
122ee7bfc0
3 changed files with 42 additions and 16 deletions
|
|
@ -10,7 +10,7 @@ pub trait Decoder: Sized {
|
||||||
|
|
||||||
pub trait DecoderOpt<Opt>: Sized {
|
pub trait DecoderOpt<Opt>: Sized {
|
||||||
fn decode(raw: &Raw, offset: usize, size: usize, opt: Opt) -> Result<Self>;
|
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;
|
fn get_enc_size(&self) -> usize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,12 @@
|
||||||
use super::decoder::DecoderOpt;
|
use super::decoder::DecoderOpt;
|
||||||
use super::entitylist::{EntityEncoding, EntityList};
|
use super::entitylist::{EntityEncoding, EntityList};
|
||||||
use super::esh::ESH;
|
use super::esh::ESH;
|
||||||
use super::fstring::FString;
|
|
||||||
use super::raw::Raw;
|
use super::raw::Raw;
|
||||||
use super::stream::{ReadStream, WriteStream};
|
use super::stream::{ReadStream, WriteStream};
|
||||||
use super::tag::Tag;
|
|
||||||
use anyhow::anyhow;
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
|
const NO_FLAGS: u32 = 0;
|
||||||
|
|
||||||
pub struct Entity {
|
pub struct Entity {
|
||||||
pub flags: u32,
|
pub flags: u32,
|
||||||
pub type_idx: usize,
|
pub type_idx: usize,
|
||||||
|
|
@ -15,18 +14,45 @@ pub struct Entity {
|
||||||
enc_size: usize,
|
enc_size: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DecoderOpt<&EntityList> for Entity {
|
impl DecoderOpt<&mut EntityList> for Entity {
|
||||||
fn decode(raw: &Raw, offset: usize, _: usize, opt: &EntityList) -> Result<Self> {
|
fn decode(raw: &Raw, offset: usize, _: usize, opt: &mut EntityList) -> Result<Self> {
|
||||||
let mut rd = ReadStream::new(raw, offset);
|
let mut rd = ReadStream::new(raw, offset);
|
||||||
|
Ok(match opt.get_entity_encoding() {
|
||||||
todo!();
|
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> {
|
fn encode(&self, opt: &mut EntityList) -> Result<Raw> {
|
||||||
todo!();
|
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 {
|
fn get_enc_size(&self) -> usize {
|
||||||
todo!();
|
self.enc_size
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,23 +16,23 @@ pub enum EntityEncoding {
|
||||||
pub struct EntityList {}
|
pub struct EntityList {}
|
||||||
|
|
||||||
impl EntityList {
|
impl EntityList {
|
||||||
fn get_entity_encoding(&self) -> EntityEncoding {
|
pub fn get_entity_encoding(&self) -> EntityEncoding {
|
||||||
todo!();
|
todo!();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_entity_tag(&self) -> &Tag {
|
pub fn get_entity_tag(&self) -> &Tag {
|
||||||
todo!();
|
todo!();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_new_type(&mut self, name: FString) -> usize {
|
pub fn add_new_type(&mut self, type_name: FString) -> usize {
|
||||||
todo!();
|
todo!();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_or_get_type(&mut self, name: FString) -> usize {
|
pub fn add_or_get_type(&mut self, type_name: FString) -> usize {
|
||||||
todo!();
|
todo!();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_type_name(&self, type_idx: usize) -> &FString {
|
pub fn get_type_name(&self, type_idx: usize) -> &FString {
|
||||||
todo!();
|
todo!();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue