From 16f0779f29cf02fbb2e798c0416b11c34be6e65b Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Tue, 22 Aug 2023 10:51:14 +0300 Subject: [PATCH] some progress in decoding. need to decode all worlds --- src/main.rs | 4 +++- src/save.rs | 40 +++++++++++++++++++++++++++------------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index a87c3fd..e2102a2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,5 +12,7 @@ fn main() { };*/ let save = Save::load(Path::new(save_path)).expect("load save"); - dbg!(save); + for w in save.worlds { + println!("World {:x} size {}", w.offset, w.size); + } } \ No newline at end of file diff --git a/src/save.rs b/src/save.rs index 784ede7..0945174 100644 --- a/src/save.rs +++ b/src/save.rs @@ -7,17 +7,18 @@ use inflate::inflate_bytes_zlib; #[derive(Debug)] pub struct World { - offset: usize, - size: usize, - data: Vec + pub offset: usize, + pub size: usize, + pub data: Vec } impl World { const DATA_OFFSET: usize = 0x13; fn decode(raw: &[u8], offset: usize, size: usize) -> Result { - let data_offset = offset + World::DATA_OFFSET; - let data = inflate_bytes_zlib(&raw[data_offset..data_offset+size]) + let data_start = offset + World::DATA_OFFSET; + let data_end = offset + size; + let data = inflate_bytes_zlib(&raw[data_start..data_end]) .map_err(|e| anyhow!(e))?; Ok(Self { offset, size, data }) @@ -30,22 +31,26 @@ impl World { #[derive(Debug)] pub struct Save { - raw: Vec, - worlds: Vec + pub raw: Vec, + pub worlds: Vec + //world: World } impl Save { + const WORLD_HDR: &str = ""; + const WORLD_HDR_LEN: usize = Self::WORLD_HDR.len(); + pub fn load(path: &Path) -> Result { let raw = fs::read(path)?; - let file_end = raw.len() - 1; + let file_end = raw.len(); let mut offsets: Vec = Vec::new(); - for i in 0..raw.len()-7 { - let keyword = match str::from_utf8(&raw[i..i+7]) { + for i in 0..raw.len()-Self::WORLD_HDR_LEN { + let keyword = match str::from_utf8(&raw[i..i+Self::WORLD_HDR_LEN]) { Ok(keyword) => keyword, Err(_) => continue }; - if keyword == "" { + if keyword == Self::WORLD_HDR { offsets.push(i); } } @@ -53,13 +58,22 @@ impl Save { let mut worlds: Vec = Vec::new(); for i in offsets.chunks(2) { let offset = i[0]; - let size = i.get(1).unwrap_or(&file_end); - match World::decode(&raw, offset, *size) { + let end = i.get(1).unwrap_or(&file_end); + match World::decode(&raw, offset, *end - offset) { Ok(world) => worlds.push(world), Err(e) => println!("world 0x{:x} decode error {}", offset, e) }; } Ok(Self { raw, worlds }) + + /*let offset = match offsets.last() { + Some(offset) => *offset, + None => return Err(anyhow!("no world offset found")) + }; + let size = raw.len() - offset; + let world = World::decode(&raw, offset, size)?; + + Ok(Self { raw, world })*/ } } \ No newline at end of file