World encoding now fully on WriteStream to write all data to construct new save file

This commit is contained in:
mykola2312 2023-09-07 14:45:48 +03:00
parent 676b7b598f
commit d060e5ca63
2 changed files with 40 additions and 35 deletions

View file

@ -4,7 +4,7 @@ use super::fstring::FString;
use super::raw::Raw; use super::raw::Raw;
use super::sgd::SGD; use super::sgd::SGD;
use super::ssg::SSG; use super::ssg::SSG;
use super::stream::ReadStream; use super::stream::{ReadStream, WriteStream};
use super::tag::Tag; use super::tag::Tag;
use anyhow::anyhow; use anyhow::anyhow;
use anyhow::Result; use anyhow::Result;
@ -16,16 +16,20 @@ use std::io::Cursor;
use std::path::Path; use std::path::Path;
pub struct World { pub struct World {
pub offset: usize,
pub size: usize,
pub tag: Tag, pub tag: Tag,
pub uncompressed_size: u32, pub uncompressed_size: u32,
//pub data: Raw,
pub data: Raw,
pub mission: FString, pub mission: FString,
pub sgd: SGD, pub sgd: SGD,
pub ssg: SSG, pub ssg: SSG,
pub entlist: EntityList, pub entlist: EntityList,
pub unparsed: Vec<u8>
} }
impl World { impl World {
@ -34,10 +38,11 @@ impl World {
pub fn test(&mut self) -> Result<()> { pub fn test(&mut self) -> Result<()> {
let actor_type = self.entlist.get_type_idx("Actor").unwrap(); let actor_type = self.entlist.get_type_idx("Actor").unwrap();
println!("Actor type {}", actor_type); for (id, ent) in &self.entlist {
//for (id, ent) in &self.entlist { if ent.type_idx == actor_type {
// println!("{} {}", id, ent.type_idx); println!("Actor {}", id);
//} }
}
Ok(()) Ok(())
} }
@ -67,46 +72,46 @@ impl Decoder for World {
let entlist: EntityList = rd.read_opt(0, EntityEncoding::World)?; let entlist: EntityList = rd.read_opt(0, EntityEncoding::World)?;
let unparsed = rd.read_bytes(data.mem.len() - rd.offset())?;
Ok(World { Ok(World {
offset,
size,
tag, tag,
uncompressed_size, uncompressed_size,
data, //data,
mission, mission,
sgd, sgd,
ssg, ssg,
entlist, entlist,
unparsed,
}) })
} }
fn encode(&self) -> Result<Raw> { fn encode(&self) -> Result<Raw> {
let mut hdr = [0u8; 8]; let data = {
{ let mut wd = WriteStream::new(self.uncompressed_size as usize);
let mut wdr = Cursor::new(&mut hdr[..]);
let _ = wdr.write_u32::<LittleEndian>(self.uncompressed_size); wd.write(&self.mission)?;
let _ = wdr.write_u32::<LittleEndian>(self.uncompressed_size); wd.write(&self.sgd)?;
} wd.write(&self.ssg)?;
let data = deflate_bytes_zlib(&self.data.mem); wd.write_opt(&self.entlist, EntityEncoding::World)?;
wd.write_bytes(&self.unparsed);
Ok(Raw::join( let raw = wd.into_raw(0, 0);
self.data.offset, deflate_bytes_zlib(&raw.mem)
self.data.size, };
&mut [
self.tag.encode()?, let mut wd = WriteStream::new(self.get_enc_size());
Raw { wd.write(&self.tag)?;
offset: Self::WORLD_TAG_LEN, wd.write_u32(self.uncompressed_size)?;
size: 8, wd.write_u32(self.uncompressed_size)?;
mem: hdr.to_vec(), wd.write_bytes(&data);
},
Raw { Ok(wd.into_raw(self.offset, self.size))
offset: Self::WORLD_HDR_LEN,
size: data.len(),
mem: data,
},
],
))
} }
fn get_enc_size(&self) -> usize { fn get_enc_size(&self) -> usize {
Self::WORLD_HDR_LEN + self.data.mem.len() Self::WORLD_HDR_LEN + self.size
} }
} }

View file

@ -14,6 +14,6 @@ fn main() {
};*/ };*/
let mut save = Save::load(Path::new(save_path)).expect("load save"); let mut save = Save::load(Path::new(save_path)).expect("load save");
save.world.test().expect("test"); //save.world.test().expect("test");
//save.save(Path::new("out.sav")).expect("failed to save"); save.save(Path::new("out.sav")).expect("failed to save");
} }