more general reading

This commit is contained in:
spuds 2023-01-15 16:26:23 +00:00
parent 7dda23967d
commit 6c2fe4dfb7
No known key found for this signature in database
GPG key ID: 0B6CA6068E827C8F
3 changed files with 24 additions and 31 deletions

View file

@ -6,22 +6,12 @@ pub fn unpack(path: String, key: String) -> Result<(), unpak::Error> {
.unwrap_or_default(), .unwrap_or_default(),
); );
let mut pak = super::load_pak(path.clone(), key)?; let mut pak = super::load_pak(path.clone(), key)?;
std::thread::scope(|scope| -> Result<(), unpak::Error> { for file in pak.files() {
for file in pak.files() { std::fs::create_dir_all(folder.join(&file).parent().expect("will be a file"))?;
match pak.get(&file).expect("file should be in pak") { match pak.read(&file, &mut std::fs::File::create(folder.join(&file))?) {
Ok(data) => { Ok(_) => println!("{file}"),
scope.spawn(move || -> Result<(), unpak::Error> { Err(e) => eprintln!("{e}"),
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}"),
}
} }
Ok(()) }
}) Ok(())
} }

View file

@ -64,13 +64,13 @@ impl Entry {
}) })
} }
pub fn read<R: io::Read + io::Seek>( pub fn read<R: io::Read + io::Seek, W: io::Write>(
&self, &self,
reader: &mut R, reader: &mut R,
version: super::Version, version: super::Version,
key: Option<&aes::Aes256Dec>, key: Option<&aes::Aes256Dec>,
) -> Result<Vec<u8>, super::Error> { buf: &mut W,
let mut buf = io::BufWriter::new(Vec::with_capacity(self.uncompressed as usize)); ) -> Result<(), super::Error> {
reader.seek(io::SeekFrom::Start(self.offset))?; reader.seek(io::SeekFrom::Start(self.offset))?;
Entry::new(reader, version)?; Entry::new(reader, version)?;
let data_offset = reader.stream_position()?; let data_offset = reader.stream_position()?;
@ -89,7 +89,6 @@ impl Entry {
} }
data.truncate(self.compressed as usize); data.truncate(self.compressed as usize);
} }
use io::Write;
macro_rules! decompress { macro_rules! decompress {
($decompressor: ty) => { ($decompressor: ty) => {
match &self.blocks { match &self.blocks {
@ -108,15 +107,12 @@ impl Entry {
} }
}], }],
), ),
&mut buf, buf,
)?; )?;
} }
} }
None => { None => {
io::copy( io::copy(&mut flate2::read::ZlibDecoder::new(data.as_slice()), buf)?;
&mut flate2::read::ZlibDecoder::new(data.as_slice()),
&mut buf,
)?;
} }
} }
}; };
@ -128,6 +124,6 @@ impl Entry {
Compression::Oodle => todo!(), Compression::Oodle => todo!(),
} }
buf.flush()?; buf.flush()?;
Ok(buf.into_inner()?) Ok(())
} }
} }

View file

@ -66,10 +66,17 @@ impl<R: io::Read + io::Seek> Pak<R> {
&self.mount_point &self.mount_point
} }
pub fn get(&mut self, path: &str) -> Option<Result<Vec<u8>, super::Error>> { pub fn get(&mut self, path: &str) -> Result<Vec<u8>, super::Error> {
self.entries let mut data = Vec::new();
.get(path) self.read(path, &mut data)?;
.map(|entry| entry.read(&mut self.reader, self.version, self.key.as_ref())) Ok(data)
}
pub fn read<W: io::Write>(&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<String> { pub fn files(&self) -> std::vec::IntoIter<String> {