trying to figure out why it's not parsing

This commit is contained in:
spuds 2023-01-06 12:40:37 +00:00
parent 5104b41878
commit 38ed23a5bc
No known key found for this signature in database
GPG key ID: 0B6CA6068E827C8F
5 changed files with 29 additions and 22 deletions

View file

@ -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(());

View file

@ -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),

View file

@ -15,7 +15,11 @@ pub trait ReadExt {
impl<R: std::io::Read> ReadExt for R {
fn read_bool(&mut self) -> Result<bool, super::Error> {
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> {

View file

@ -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(

View file

@ -13,8 +13,7 @@ impl Pak {
version: super::Version,
mut reader: R,
) -> Result<Self, super::Error> {
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
}