add CTag for const tag definitions. + cargo fmt

This commit is contained in:
mykola2312 2023-09-04 09:58:19 +03:00
parent 43c3bd11f6
commit 41c513f5e9
5 changed files with 51 additions and 16 deletions

View file

@ -62,7 +62,7 @@ impl DecoderCtx<&mut EntityList, &EntityList> for Entity {
wd.write_u16(self.type_idx as u16)?; wd.write_u16(self.type_idx as u16)?;
match self.esh.as_ref() { match self.esh.as_ref() {
Some(esh) => wd.write(esh)?, Some(esh) => wd.write(esh)?,
None => () None => (),
} }
} }
} }

View file

@ -4,16 +4,21 @@ use super::esh::ESH;
use super::fstring::FString; 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 super::tag::{CTag, Tag};
use anyhow::anyhow; use anyhow::anyhow;
use anyhow::Result; use anyhow::Result;
#[derive(Clone, Copy)] #[derive(Clone, Copy, PartialEq)]
pub enum EntityEncoding { pub enum EntityEncoding {
File, File,
World, World,
} }
const DEFAULT_ENTITY_TAG: CTag<'static> = CTag {
name: "<entity>",
version: "2",
};
pub struct EntityList { pub struct EntityList {
encoding: EntityEncoding, encoding: EntityEncoding,
entity_file_tag: Option<Tag>, entity_file_tag: Option<Tag>,
@ -22,7 +27,7 @@ pub struct EntityList {
enc_size: usize, enc_size: usize,
pub types: Vec<FString>, pub types: Vec<FString>,
pub entities: Vec<Entity> pub entities: Vec<Entity>,
} }
impl EntityList { impl EntityList {
@ -42,16 +47,23 @@ impl EntityList {
pub fn add_or_get_type(&mut self, type_name: FString) -> usize { pub fn add_or_get_type(&mut self, type_name: FString) -> usize {
match self.types.iter().position(|f| f.eq(&type_name)) { match self.types.iter().position(|f| f.eq(&type_name)) {
Some(idx) => idx, Some(idx) => idx,
None => self.add_new_type(type_name) None => self.add_new_type(type_name),
} }
} }
pub fn get_type_name(&self, type_idx: usize) -> &FString { pub fn get_type_name(&self, type_idx: usize) -> &FString {
&self.types[type_idx] &self.types[type_idx]
} }
pub fn convert(&mut self, new: EntityEncoding) {
use EntityEncoding as EE;
if self.encoding == EE::World && new == EE::File {
self.entity_tag = Some(DEFAULT_ENTITY_TAG.to_tag());
}
}
} }
impl DecoderCtx<EntityEncoding,EntityEncoding> for EntityList { impl DecoderCtx<EntityEncoding, EntityEncoding> for EntityList {
fn decode(raw: &Raw, offset: usize, size: usize, ctx: EntityEncoding) -> Result<Self> { fn decode(raw: &Raw, offset: usize, size: usize, ctx: EntityEncoding) -> Result<Self> {
let mut rd = ReadStream::new(raw, offset); let mut rd = ReadStream::new(raw, offset);
let mut ent_list = EntityList { let mut ent_list = EntityList {
@ -61,7 +73,7 @@ impl DecoderCtx<EntityEncoding,EntityEncoding> for EntityList {
unk1: 0, unk1: 0,
enc_size: 0, enc_size: 0,
types: Vec::new(), types: Vec::new(),
entities: Vec::new() entities: Vec::new(),
}; };
Ok(match ctx { Ok(match ctx {
@ -80,7 +92,7 @@ impl DecoderCtx<EntityEncoding,EntityEncoding> for EntityList {
ent_list.enc_size = rd.offset() - offset; ent_list.enc_size = rd.offset() - offset;
ent_list ent_list
}, }
EntityEncoding::World => { EntityEncoding::World => {
ent_list.entity_file_tag = Some(rd.read(0)?); ent_list.entity_file_tag = Some(rd.read(0)?);
@ -110,7 +122,7 @@ impl DecoderCtx<EntityEncoding,EntityEncoding> for EntityList {
wd.write(self.get_entity_tag())?; wd.write(self.get_entity_tag())?;
wd.write_opt(ent, &self)?; wd.write_opt(ent, &self)?;
} }
}, }
EntityEncoding::World => { EntityEncoding::World => {
wd.write(self.entity_file_tag.as_ref().unwrap())?; wd.write(self.entity_file_tag.as_ref().unwrap())?;
wd.write_u32(self.types.len() as u32)?; wd.write_u32(self.types.len() as u32)?;

View file

@ -46,7 +46,11 @@ 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: DecoderCtx<DCtx, ECtx>, DCtx, ECtx>(&mut self, size: usize, ctx: DCtx) -> Result<T> { pub fn read_opt<T: DecoderCtx<DCtx, ECtx>, DCtx, ECtx>(
&mut self,
size: usize,
ctx: DCtx,
) -> Result<T> {
let val = T::decode(&self.raw, self.offset(), size, ctx)?; let val = T::decode(&self.raw, self.offset(), size, ctx)?;
self.skip(val.get_enc_size()); self.skip(val.get_enc_size());
Ok(val) Ok(val)
@ -112,7 +116,11 @@ impl WriteStream {
self.buf.get_mut().extend(bytes.iter()); self.buf.get_mut().extend(bytes.iter());
} }
pub fn write_opt<T: DecoderCtx<DCtx, ECtx>, DCtx, ECtx>(&mut self, val: &T, ctx: ECtx) -> Result<()> { pub fn write_opt<T: DecoderCtx<DCtx, ECtx>, DCtx, ECtx>(
&mut self,
val: &T,
ctx: ECtx,
) -> Result<()> {
let mut raw = val.encode(ctx)?; let mut raw = val.encode(ctx)?;
self.skip(raw.mem.len()); self.skip(raw.mem.len());
self.buf.get_mut().append(&mut raw.mem); self.buf.get_mut().append(&mut raw.mem);

View file

@ -28,3 +28,18 @@ impl Decoder for Tag {
self.name.get_enc_size() + self.version.get_enc_size() self.name.get_enc_size() + self.version.get_enc_size()
} }
} }
// struct for Tag consts
pub struct CTag<'a> {
pub name: &'a str,
pub version: &'a str,
}
impl<'a> CTag<'a> {
pub fn to_tag(&self) -> Tag {
Tag {
name: self.name.to_string(),
version: self.version.to_string(),
}
}
}

View file

@ -1,6 +1,6 @@
use super::decoder::{Decoder, DecoderCtx}; use super::decoder::{Decoder, DecoderCtx};
use super::entitylist::{EntityEncoding, EntityList};
use super::fstring::FString; use super::fstring::FString;
use super::entitylist::{EntityList, EntityEncoding};
use super::raw::Raw; use super::raw::Raw;
use super::sgd::SGD; use super::sgd::SGD;
use super::ssg::SSG; use super::ssg::SSG;
@ -25,7 +25,7 @@ pub struct World {
pub sgd: SGD, pub sgd: SGD,
pub ssg: SSG, pub ssg: SSG,
pub ents: EntityList pub ents: EntityList,
} }
impl World { impl World {
@ -37,7 +37,7 @@ impl World {
let raw1 = Raw { let raw1 = Raw {
offset: 0, offset: 0,
size: self.ents.get_enc_size(), size: self.ents.get_enc_size(),
mem: self.data.mem[entfile_start..entfile_start+self.ents.get_enc_size()].to_vec() mem: self.data.mem[entfile_start..entfile_start + self.ents.get_enc_size()].to_vec(),
}; };
raw1.dump(Path::new("entfile1.bin"))?; raw1.dump(Path::new("entfile1.bin"))?;
@ -79,7 +79,7 @@ impl Decoder for World {
mission, mission,
sgd, sgd,
ssg, ssg,
ents ents,
}) })
} }