This commit is contained in:
mykola2312 2023-08-22 12:44:43 +03:00
parent 6b202eb725
commit 6f441eaf39
5 changed files with 43 additions and 3 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
/target /target
*.bin *.bin
*.sav

10
Cargo.lock generated
View file

@ -14,11 +14,21 @@ version = "1.0.75"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6"
[[package]]
name = "deflate"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c86f7e25f518f4b81808a2cf1c50996a61f5c2eb394b2393bd87f2a4780a432f"
dependencies = [
"adler32",
]
[[package]] [[package]]
name = "fot-save-edit" name = "fot-save-edit"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"deflate",
"inflate", "inflate",
] ]

View file

@ -7,4 +7,5 @@ edition = "2021"
[dependencies] [dependencies]
anyhow = "1.0.75" anyhow = "1.0.75"
deflate = "1.0.0"
inflate = "0.4.5" inflate = "0.4.5"

View file

@ -12,7 +12,9 @@ fn main() {
};*/ };*/
let save = Save::load(Path::new(save_path)).expect("load save"); 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); println!("World {:x} size {}", w.offset, w.size);
} }
save.save(Path::new("out.sav")).expect("failed to save");
} }

View file

@ -1,9 +1,13 @@
use std::fs; use std::io::Write;
use std::str; use std::str;
use std::fs;
use std::fs::File;
use std::fs::OpenOptions;
use std::path::Path; use std::path::Path;
use anyhow::anyhow; use anyhow::anyhow;
use anyhow::Result; use anyhow::Result;
use inflate::inflate_bytes_zlib; use inflate::inflate_bytes_zlib;
use deflate::deflate_bytes_zlib;
#[derive(Debug)] #[derive(Debug)]
pub struct World { pub struct World {
@ -24,6 +28,10 @@ impl World {
Ok(Self { offset, size, data }) Ok(Self { offset, size, data })
} }
fn encode(&self) -> Vec<u8> {
deflate_bytes_zlib(&self.data)
}
pub fn dump(&self, path: &Path) -> Result<()> { pub fn dump(&self, path: &Path) -> Result<()> {
Ok(fs::write(path, &self.data)?) Ok(fs::write(path, &self.data)?)
} }
@ -69,4 +77,22 @@ impl Save {
Ok(Self { raw, worlds }) 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(())
}
} }