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(),
);
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(())
}

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,
reader: &mut R,
version: super::Version,
key: Option<&aes::Aes256Dec>,
) -> Result<Vec<u8>, 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(())
}
}

View file

@ -66,10 +66,17 @@ impl<R: io::Read + io::Seek> Pak<R> {
&self.mount_point
}
pub fn get(&mut self, path: &str) -> Option<Result<Vec<u8>, 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<Vec<u8>, super::Error> {
let mut data = Vec::new();
self.read(path, &mut data)?;
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> {