From 38ed23a5bca101cf5d5c0c8560a73fe255029195 Mon Sep 17 00:00:00 2001 From: spuds <71292624+bananaturtlesandwich@users.noreply.github.com> Date: Fri, 6 Jan 2023 12:40:37 +0000 Subject: [PATCH] trying to figure out why it's not parsing --- examples/read.rs | 5 ++++- src/error.rs | 12 +++++++----- src/ext.rs | 6 +++++- src/footer.rs | 7 +++---- src/pak.rs | 21 ++++++++++----------- 5 files changed, 29 insertions(+), 22 deletions(-) diff --git a/examples/read.rs b/examples/read.rs index 6e6f637..7e37519 100644 --- a/examples/read.rs +++ b/examples/read.rs @@ -1,6 +1,9 @@ fn main() -> Result<(), un_pak::Error> { for version in un_pak::Version::iter().rev() { - match un_pak::Pak::new(version, std::io::Cursor::new(include_bytes!("rando_p.pak"))) { + match un_pak::Pak::new( + version, + std::io::BufReader::new(std::io::Cursor::new(include_bytes!("rando_p.pak"))), + ) { Ok(_) => { println!("parsed successfully!"); return Ok(()); diff --git a/src/error.rs b/src/error.rs index 23a2543..5ea9ee2 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,16 +1,18 @@ #[derive(thiserror::Error, Debug)] pub enum Error { - #[error("expected magic of {} but found {0}", super::MAGIC)] + #[error("expected magic of {} but got {0}", super::MAGIC)] MagicMismatch(u32), #[error("parsed with {0} but pak was {0}")] VersionMismatch(super::Version, super::Version), - #[error("error reading file: {0}")] + #[error("expected 1 or 0 but got {0}")] + BoolConversion(u8), + #[error("reading file: {0}")] IoError(#[from] std::io::Error), - #[error("error converting enum: {0}")] + #[error("converting to enum: {0}")] StrumError(#[from] strum::ParseError), - #[error("error converting to utf8: {0}")] + #[error("converting to utf8: {0}")] Utf8Error(#[from] std::string::FromUtf8Error), - #[error("error converting to utf16: {0}")] + #[error("converting to utf16: {0}")] Utf16Error(#[from] std::string::FromUtf16Error), #[error("{0}")] Other(String), diff --git a/src/ext.rs b/src/ext.rs index 5f9732c..8e4a62f 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -15,7 +15,11 @@ pub trait ReadExt { impl ReadExt for R { fn read_bool(&mut self) -> Result { - Ok(self.read_u8()? != 0) + match self.read_u8()? { + 1 => Ok(true), + 0 => Ok(false), + err => Err(super::Error::BoolConversion(err)), + } } fn read_guid(&mut self) -> Result<[u8; 20], super::Error> { diff --git a/src/footer.rs b/src/footer.rs index d74f484..805e266 100644 --- a/src/footer.rs +++ b/src/footer.rs @@ -30,10 +30,9 @@ impl Footer { frozen: (version == &Version::FrozenIndex).then_some(reader.read_bool()?), compression: (version >= &Version::FNameBasedCompression).then_some({ let mut compression = - Vec::with_capacity(if version == &Version::FNameBasedCompression { - 4 - } else { - 5 + Vec::with_capacity(match version == &Version::FNameBasedCompression { + true => 4, + false => 5, }); for _ in 0..compression.capacity() { compression.push( diff --git a/src/pak.rs b/src/pak.rs index cf6484b..180178b 100644 --- a/src/pak.rs +++ b/src/pak.rs @@ -13,8 +13,7 @@ impl Pak { version: super::Version, mut reader: R, ) -> Result { - reader.seek(io::SeekFrom::End(-(footer_size(&version) as i64)))?; - // parse footer info to get index offset + reader.seek(io::SeekFrom::End(-footer_size(&version)))?; let footer = super::Footer::new(&mut reader, &version)?; reader.seek(io::SeekFrom::Start(footer.offset))?; let index = super::Index::new(&mut reader, &version)?; @@ -26,16 +25,20 @@ impl Pak { } } -fn footer_size(version: &Version) -> u32 { +fn footer_size(version: &Version) -> i64 { // (magic + version): u32 + (offset + size): u64 + hash: [u8; 20] - let mut size = 4 * 2 + 8 * 2 + 20; + let mut size = 4 + 4 + 8 + 8 + 20; + if version >= &Version::EncryptionKeyGuid { + // encryption guid: [u8; 20] + size += 20; + } if version >= &Version::IndexEncryption { // encrypted: bool size += 1; } - if version >= &Version::EncryptionKeyGuid { - // encryption guid: [u8; 20] - size += 10; + if version == &Version::FrozenIndex { + // frozen index: bool + size += 1; } if version >= &Version::FNameBasedCompression { // compression names: [[u8; 32]; 4] @@ -45,9 +48,5 @@ fn footer_size(version: &Version) -> u32 { // extra compression name: [u8; 32] size += 32 } - if version == &Version::FrozenIndex { - // frozen index: bool - size += 1; - } size }