From 6f441eaf3978393c35d0cc53585bd6b11f12e7fe Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Tue, 22 Aug 2023 12:44:43 +0300 Subject: [PATCH] push it --- .gitignore | 3 ++- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + src/main.rs | 4 +++- src/save.rs | 28 +++++++++++++++++++++++++++- 5 files changed, 43 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 5a48dd5..e604ba8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target -*.bin \ No newline at end of file +*.bin +*.sav \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 30169a5..5dd5c00 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,11 +14,21 @@ version = "1.0.75" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" +[[package]] +name = "deflate" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c86f7e25f518f4b81808a2cf1c50996a61f5c2eb394b2393bd87f2a4780a432f" +dependencies = [ + "adler32", +] + [[package]] name = "fot-save-edit" version = "0.1.0" dependencies = [ "anyhow", + "deflate", "inflate", ] diff --git a/Cargo.toml b/Cargo.toml index 547eaf9..a6d03bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,5 @@ edition = "2021" [dependencies] anyhow = "1.0.75" +deflate = "1.0.0" inflate = "0.4.5" diff --git a/src/main.rs b/src/main.rs index e2102a2..5df7bd6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,9 @@ fn main() { };*/ let save = Save::load(Path::new(save_path)).expect("load save"); - for w in save.worlds { + for w in save.worlds.iter() { println!("World {:x} size {}", w.offset, w.size); } + + save.save(Path::new("out.sav")).expect("failed to save"); } \ No newline at end of file diff --git a/src/save.rs b/src/save.rs index 729996f..c4d0550 100644 --- a/src/save.rs +++ b/src/save.rs @@ -1,9 +1,13 @@ -use std::fs; +use std::io::Write; use std::str; +use std::fs; +use std::fs::File; +use std::fs::OpenOptions; use std::path::Path; use anyhow::anyhow; use anyhow::Result; use inflate::inflate_bytes_zlib; +use deflate::deflate_bytes_zlib; #[derive(Debug)] pub struct World { @@ -24,6 +28,10 @@ impl World { Ok(Self { offset, size, data }) } + fn encode(&self) -> Vec { + deflate_bytes_zlib(&self.data) + } + pub fn dump(&self, path: &Path) -> Result<()> { Ok(fs::write(path, &self.data)?) } @@ -69,4 +77,22 @@ impl Save { Ok(Self { raw, worlds }) } + + pub fn save(&self, path: &Path) -> Result<()> { + let mut file = OpenOptions::new() + .create(true).write(true).open(path)?; + + // write unknown block + let unk_size = match self.worlds.first() { + Some(w) => w.offset - 1, + None => return Err(anyhow!("no offsets found")) + }; + file.write(&self.raw[..unk_size])?; + + for world in self.worlds.iter() { + file.write(&world.encode())?; + } + + Ok(()) + } } \ No newline at end of file