From 907ea77b4be3d34b8d53affbbc37534a18c34b00 Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Mon, 21 Aug 2023 23:15:04 +0300 Subject: [PATCH] push it --- Cargo.lock | 63 ++++++----------------------------------------------- Cargo.toml | 2 +- src/save.rs | 48 +++++++++++++++++++--------------------- 3 files changed, 30 insertions(+), 83 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ecb353f..30169a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,12 +8,18 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234" +[[package]] +name = "anyhow" +version = "1.0.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" + [[package]] name = "fot-save-edit" version = "0.1.0" dependencies = [ + "anyhow", "inflate", - "thiserror", ] [[package]] @@ -24,58 +30,3 @@ checksum = "1cdb29978cc5797bd8dcc8e5bf7de604891df2a8dc576973d71a281e916db2ff" dependencies = [ "adler32", ] - -[[package]] -name = "proc-macro2" -version = "1.0.66" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "quote" -version = "1.0.33" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "syn" -version = "2.0.29" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c324c494eba9d92503e6f1ef2e6df781e78f6a7705a0202d9801b198807d518a" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "thiserror" -version = "1.0.47" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97a802ec30afc17eee47b2855fc72e0c4cd62be9b4efe6591edde0ec5bd68d8f" -dependencies = [ - "thiserror-impl", -] - -[[package]] -name = "thiserror-impl" -version = "1.0.47" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6bb623b56e39ab7dcd4b1b98bb6c8f8d907ed255b18de254088016b27a8ee19b" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "unicode-ident" -version = "1.0.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" diff --git a/Cargo.toml b/Cargo.toml index 07468f9..547eaf9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,5 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +anyhow = "1.0.75" inflate = "0.4.5" -thiserror = "1.0.47" diff --git a/src/save.rs b/src/save.rs index 8cd33e1..75fb806 100644 --- a/src/save.rs +++ b/src/save.rs @@ -1,7 +1,8 @@ use std::fs; +use std::str; use std::path::Path; -use std::path::PathBuf; -use thiserror::Error; +use anyhow::anyhow; +use anyhow::Result; use inflate::inflate_bytes_zlib; struct World { @@ -10,40 +11,35 @@ struct World { data: Vec } -#[derive(Error, Debug)] -enum WorldDecodeError { - #[error("inflate error")] - InflateError(String) -} - impl World { - fn decode(raw: &[u8], offset: usize, size: usize) -> Result { - let data = match inflate_bytes_zlib(&raw[offset..offset+size]) { - Ok(data) => data, - Err(e) => return Err(WorldDecodeError::InflateError((e))) - }; + fn decode(raw: &[u8], offset: usize, size: usize) -> Result { + let data = inflate_bytes_zlib(&raw[offset..offset+size]) + .map_err(|e| anyhow!(e))?; Ok(Self { offset, size, data }) } } struct Save { - raw: Vec -} - -#[derive(Error, Debug)] -enum SaveLoadError { - #[error("file error")] - FileError(std::io::Error) + raw: Vec, + worlds: Vec } impl Save { - fn load(path: &Path) -> Result { - let raw = match fs::read(path) { - Ok(raw) => raw, - Err(e) => return Err(SaveLoadError::FileError(e)) - }; + fn load(path: &Path) -> Result { + let raw = fs::read(path)?; + let mut offsets: Vec = Vec::new(); + for i in 0..raw.len()-7 { + let keyword = match str::from_utf8(&raw[i..i+7]) { + Ok(keyword) => keyword, + Err(_) => continue + }; - Ok(Self { raw }) + if keyword == "" { + offsets.push(i); + } + } + + Ok(Self { raw, worlds: Vec::new() }) } } \ No newline at end of file