From 6c2fe4dfb7032b88e0cbc8a27dc20db94a02da50 Mon Sep 17 00:00:00 2001 From: spuds <71292624+bananaturtlesandwich@users.noreply.github.com> Date: Sun, 15 Jan 2023 16:26:23 +0000 Subject: [PATCH] more general reading --- examples/subcommands/unpack.rs | 24 +++++++----------------- src/entry.rs | 16 ++++++---------- src/pak.rs | 15 +++++++++++---- 3 files changed, 24 insertions(+), 31 deletions(-) diff --git a/examples/subcommands/unpack.rs b/examples/subcommands/unpack.rs index 61d3526..f85f492 100644 --- a/examples/subcommands/unpack.rs +++ b/examples/subcommands/unpack.rs @@ -6,22 +6,12 @@ pub fn unpack(path: String, key: String) -> Result<(), unpak::Error> { .unwrap_or_default(), ); let mut pak = super::load_pak(path.clone(), key)?; - std::thread::scope(|scope| -> Result<(), unpak::Error> { - for file in pak.files() { - match pak.get(&file).expect("file should be in pak") { - Ok(data) => { - scope.spawn(move || -> Result<(), unpak::Error> { - std::fs::create_dir_all( - folder.join(&file).parent().expect("will be a file"), - )?; - println!("{file}"); - std::fs::write(folder.join(&file), data)?; - Ok(()) - }); - } - Err(e) => eprintln!("{e}"), - } + for file in pak.files() { + std::fs::create_dir_all(folder.join(&file).parent().expect("will be a file"))?; + match pak.read(&file, &mut std::fs::File::create(folder.join(&file))?) { + Ok(_) => println!("{file}"), + Err(e) => eprintln!("{e}"), } - Ok(()) - }) + } + Ok(()) } diff --git a/src/entry.rs b/src/entry.rs index 86f2371..f91a0d1 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -64,13 +64,13 @@ impl Entry { }) } - pub fn read( + pub fn read( &self, reader: &mut R, version: super::Version, key: Option<&aes::Aes256Dec>, - ) -> Result, super::Error> { - let mut buf = io::BufWriter::new(Vec::with_capacity(self.uncompressed as usize)); + buf: &mut W, + ) -> Result<(), super::Error> { reader.seek(io::SeekFrom::Start(self.offset))?; Entry::new(reader, version)?; let data_offset = reader.stream_position()?; @@ -89,7 +89,6 @@ impl Entry { } data.truncate(self.compressed as usize); } - use io::Write; macro_rules! decompress { ($decompressor: ty) => { match &self.blocks { @@ -108,15 +107,12 @@ impl Entry { } }], ), - &mut buf, + buf, )?; } } None => { - io::copy( - &mut flate2::read::ZlibDecoder::new(data.as_slice()), - &mut buf, - )?; + io::copy(&mut flate2::read::ZlibDecoder::new(data.as_slice()), buf)?; } } }; @@ -128,6 +124,6 @@ impl Entry { Compression::Oodle => todo!(), } buf.flush()?; - Ok(buf.into_inner()?) + Ok(()) } } diff --git a/src/pak.rs b/src/pak.rs index dce36db..b4d5096 100644 --- a/src/pak.rs +++ b/src/pak.rs @@ -66,10 +66,17 @@ impl Pak { &self.mount_point } - pub fn get(&mut self, path: &str) -> Option, super::Error>> { - self.entries - .get(path) - .map(|entry| entry.read(&mut self.reader, self.version, self.key.as_ref())) + pub fn get(&mut self, path: &str) -> Result, super::Error> { + let mut data = Vec::new(); + self.read(path, &mut data)?; + Ok(data) + } + + pub fn read(&mut self, path: &str, writer: &mut W) -> Result<(), super::Error> { + match self.entries.get(path) { + Some(entry) => entry.read(&mut self.reader, self.version, self.key.as_ref(), writer), + None => Err(super::Error::Other("no file found at given path")), + } } pub fn files(&self) -> std::vec::IntoIter {