fmt + clippy

This commit is contained in:
Truman Kilen 2023-01-20 13:07:28 -06:00
parent 529bdcac4e
commit de69a98d34
5 changed files with 38 additions and 30 deletions

View file

@ -21,5 +21,8 @@ fn load_pak(
}) })
.transpose()?; .transpose()?;
unpak::PakReader::new_any( std::io::BufReader::new(std::fs::OpenOptions::new().read(true).open(&path)?), key) unpak::PakReader::new_any(
std::io::BufReader::new(std::fs::OpenOptions::new().read(true).open(&path)?),
key,
)
} }

View file

@ -46,7 +46,7 @@ impl Entry {
size += 8; // compressed size += 8; // compressed
size += 8; // uncompressed size += 8; // uncompressed
size += match version != Version::V8A { size += match version != Version::V8A {
true => 4, // 32 bit compression true => 4, // 32 bit compression
false => 1, // 8 bit compression false => 1, // 8 bit compression
}; };
size += match version.version_major() == VersionMajor::Initial { size += match version.version_major() == VersionMajor::Initial {
@ -71,7 +71,11 @@ impl Entry {
let offset = reader.read_u64::<LE>()?; let offset = reader.read_u64::<LE>()?;
let compressed = reader.read_u64::<LE>()?; let compressed = reader.read_u64::<LE>()?;
let uncompressed = reader.read_u64::<LE>()?; let uncompressed = reader.read_u64::<LE>()?;
let compression = match if version == Version::V8A { reader.read_u8()? as u32 } else { reader.read_u32::<LE>()? } { let compression = match if version == Version::V8A {
reader.read_u8()? as u32
} else {
reader.read_u32::<LE>()?
} {
0x01 | 0x10 | 0x20 => Compression::Zlib, 0x01 | 0x10 | 0x20 => Compression::Zlib,
_ => Compression::None, _ => Compression::None,
}; };
@ -91,8 +95,10 @@ impl Entry {
true => Some(reader.read_array(Block::new)?), true => Some(reader.read_array(Block::new)?),
false => None, false => None,
}, },
encrypted: version.version_major() >= VersionMajor::CompressionEncryption && reader.read_bool()?, encrypted: version.version_major() >= VersionMajor::CompressionEncryption
block_uncompressed: match version.version_major() >= VersionMajor::CompressionEncryption { && reader.read_bool()?,
block_uncompressed: match version.version_major() >= VersionMajor::CompressionEncryption
{
true => Some(reader.read_u32::<LE>()?), true => Some(reader.read_u32::<LE>()?),
false => None, false => None,
}, },
@ -116,7 +122,7 @@ impl Entry {
if block_uncompressed == 0x3f { if block_uncompressed == 0x3f {
block_uncompressed = reader.read_u32::<LE>()?; block_uncompressed = reader.read_u32::<LE>()?;
} else { } else {
block_uncompressed = block_uncompressed << 11; block_uncompressed <<= 11;
} }
let mut var_int = |bit: u32| -> Result<_, super::Error> { let mut var_int = |bit: u32| -> Result<_, super::Error> {
@ -134,7 +140,7 @@ impl Entry {
_ => var_int(29)?, _ => var_int(29)?,
}; };
block_uncompressed = if compression_block_count <= 0 { block_uncompressed = if compression_block_count == 0 {
0 0
} else if uncompressed < block_uncompressed.into() { } else if uncompressed < block_uncompressed.into() {
uncompressed.try_into().unwrap() uncompressed.try_into().unwrap()
@ -157,7 +163,6 @@ impl Entry {
let mut index = offset_base; let mut index = offset_base;
Some( Some(
(0..compression_block_count) (0..compression_block_count)
.into_iter()
.map(|_| { .map(|_| {
let mut block_size = reader.read_u32::<LE>()? as u64; let mut block_size = reader.read_u32::<LE>()? as u64;
let block = Block { let block = Block {
@ -220,7 +225,9 @@ impl Entry {
for block in blocks { for block in blocks {
io::copy( io::copy(
&mut <$decompressor>::new( &mut <$decompressor>::new(
&data[match version.version_major() >= VersionMajor::RelativeChunkOffsets { &data[match version.version_major()
>= VersionMajor::RelativeChunkOffsets
{
true => { true => {
(block.start - (data_offset - self.offset)) as usize (block.start - (data_offset - self.offset)) as usize
..(block.end - (data_offset - self.offset)) as usize ..(block.end - (data_offset - self.offset)) as usize

View file

@ -23,10 +23,12 @@ impl Footer {
true => Some(reader.read_u128::<LE>()?), true => Some(reader.read_u128::<LE>()?),
false => None, false => None,
}, },
encrypted: version.version_major() >= VersionMajor::IndexEncryption && reader.read_bool()?, encrypted: version.version_major() >= VersionMajor::IndexEncryption
&& reader.read_bool()?,
magic: reader.read_u32::<LE>()?, magic: reader.read_u32::<LE>()?,
version, version,
version_major: VersionMajor::from_repr(reader.read_u32::<LE>()?).unwrap_or(version.version_major()), version_major: VersionMajor::from_repr(reader.read_u32::<LE>()?)
.unwrap_or(version.version_major()),
index_offset: reader.read_u64::<LE>()?, index_offset: reader.read_u64::<LE>()?,
index_size: reader.read_u64::<LE>()?, index_size: reader.read_u64::<LE>()?,
hash: reader.read_guid()?, hash: reader.read_guid()?,

View file

@ -70,12 +70,12 @@ impl Version {
size += 1; size += 1;
} }
if self >= Version::V8A { if self >= Version::V8A {
// compression names: [[u8; 32]; 5] // compression names: [[u8; 32]; 4]
size += 32 * 4; size += 32 * 4;
} }
if self >= Version::V8B { if self >= Version::V8B {
// compression names: [[u8; 32]; 5] // additional compression name
size += 32 * 1; size += 32;
} }
size size
} }

View file

@ -57,21 +57,11 @@ fn decrypt(key: &Option<aes::Aes256Dec>, bytes: &mut [u8]) -> Result<(), super::
} }
impl<R: io::Read + io::Seek> PakReader<R> { impl<R: io::Read + io::Seek> PakReader<R> {
pub fn new_any( pub fn new_any(mut reader: R, key: Option<aes::Aes256Dec>) -> Result<Self, super::Error> {
mut reader: R,
key: Option<aes::Aes256Dec>,
) -> Result<Self, super::Error> {
for ver in Version::iter() { for ver in Version::iter() {
match PakReader::new( match PakReader::new(&mut reader, ver, key.clone()) {
&mut reader,
ver,
key.clone(),
) {
Ok(pak) => { Ok(pak) => {
return Ok(PakReader { return Ok(PakReader { pak, reader });
pak,
reader,
});
} }
_ => continue, _ => continue,
} }
@ -172,7 +162,7 @@ impl<R: io::Read + io::Seek> PakReader<R> {
// concat directory with file name to match IndexV1 but should provide a more direct access method // concat directory with file name to match IndexV1 but should provide a more direct access method
let path = format!( let path = format!(
"{}{}", "{}{}",
dir_name.strip_prefix("/").unwrap_or(dir_name), dir_name.strip_prefix('/').unwrap_or(dir_name),
file_name file_name
); );
entries_by_path.insert(path, entry); entries_by_path.insert(path, entry);
@ -224,13 +214,19 @@ impl<R: io::Read + io::Seek> PakReader<R> {
pub fn read<W: io::Write>(&mut self, path: &str, writer: &mut W) -> Result<(), super::Error> { pub fn read<W: io::Write>(&mut self, path: &str, writer: &mut W) -> Result<(), super::Error> {
match self.pak.index.entries().get(path) { match self.pak.index.entries().get(path) {
Some(entry) => entry.read(&mut self.reader, self.pak.version, self.pak.key.as_ref(), writer), Some(entry) => entry.read(
&mut self.reader,
self.pak.version,
self.pak.key.as_ref(),
writer,
),
None => Err(super::Error::Other("no file found at given path")), 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> {
self.pak.index self.pak
.index
.entries() .entries()
.keys() .keys()
.cloned() .cloned()