some progress in decoding. need to decode all worlds

This commit is contained in:
mykola2312 2023-08-22 10:51:14 +03:00
parent 6edd70ecf5
commit 16f0779f29
2 changed files with 30 additions and 14 deletions

View file

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

View file

@ -7,17 +7,18 @@ use inflate::inflate_bytes_zlib;
#[derive(Debug)]
pub struct World {
offset: usize,
size: usize,
data: Vec<u8>
pub offset: usize,
pub size: usize,
pub data: Vec<u8>
}
impl World {
const DATA_OFFSET: usize = 0x13;
fn decode(raw: &[u8], offset: usize, size: usize) -> Result<Self> {
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<u8>,
worlds: Vec<World>
pub raw: Vec<u8>,
pub worlds: Vec<World>
//world: World
}
impl Save {
const WORLD_HDR: &str = "<world>";
const WORLD_HDR_LEN: usize = Self::WORLD_HDR.len();
pub fn load(path: &Path) -> Result<Self> {
let raw = fs::read(path)?;
let file_end = raw.len() - 1;
let file_end = raw.len();
let mut offsets: Vec<usize> = 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 == "<world>" {
if keyword == Self::WORLD_HDR {
offsets.push(i);
}
}
@ -53,13 +58,22 @@ impl Save {
let mut worlds: Vec<World> = 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 })*/
}
}