diff --git a/examples/read.rs b/examples/read.rs index 7e37519..adc695b 100644 --- a/examples/read.rs +++ b/examples/read.rs @@ -1,15 +1,18 @@ fn main() -> Result<(), un_pak::Error> { - for version in un_pak::Version::iter().rev() { + let path = std::env::args().nth(1).unwrap_or_default(); + for version in un_pak::Version::iter() { + print!("{version} - "); match un_pak::Pak::new( version, - std::io::BufReader::new(std::io::Cursor::new(include_bytes!("rando_p.pak"))), + std::io::BufReader::new(std::fs::OpenOptions::new().read(true).open(&path)?), ) { - Ok(_) => { + Ok(pak) => { + print!("{:#?}", pak); println!("parsed successfully!"); - return Ok(()); } Err(e) => println!("{e}"), } } - Err(un_pak::Error::Other("no version can parse".to_string())) + std::thread::sleep_ms(10000); + Ok(()) } diff --git a/src/entry.rs b/src/entry.rs index 4f66fa8..1507110 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -2,6 +2,7 @@ use byteorder::{ReadBytesExt, LE}; use super::{Compression, ReadExt, Version}; +#[derive(Debug)] pub struct Entry { pub name: String, pub offset: u64, @@ -43,6 +44,7 @@ impl Entry { } } +#[derive(Debug)] pub struct Block { /// start offset relative to the start of the entry header pub offset: u64, diff --git a/src/error.rs b/src/error.rs index 5ea9ee2..320bce8 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,19 +1,19 @@ #[derive(thiserror::Error, Debug)] pub enum Error { - #[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("expected 1 or 0 but got {0}")] - BoolConversion(u8), - #[error("reading file: {0}")] + #[error("illegal file magic of {0}")] + WrongMagic(u32), + #[error("used version {0} but pak is version {1}")] + WrongVersion(super::Version, super::Version), + #[error("couldn't convert {0} to boolean")] + BoolConv(u8), + #[error("io error: {0}")] IoError(#[from] std::io::Error), - #[error("converting to enum: {0}")] - StrumError(#[from] strum::ParseError), - #[error("converting to utf8: {0}")] - Utf8Error(#[from] std::string::FromUtf8Error), - #[error("converting to utf16: {0}")] - Utf16Error(#[from] std::string::FromUtf16Error), + #[error("enum conversion: {0}")] + StrumConv(#[from] strum::ParseError), + #[error("utf8 conversion: {0}")] + Utf8Conv(#[from] std::string::FromUtf8Error), + #[error("utf16 conversion: {0}")] + Utf16Conv(#[from] std::string::FromUtf16Error), #[error("{0}")] Other(String), } diff --git a/src/ext.rs b/src/ext.rs index 8e4a62f..c0589e1 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -1,7 +1,5 @@ use byteorder::{ReadBytesExt, LE}; -type R = dyn std::io::Read; - pub trait ReadExt { fn read_bool(&mut self) -> Result; fn read_guid(&mut self) -> Result<[u8; 20], super::Error>; @@ -18,7 +16,7 @@ impl ReadExt for R { match self.read_u8()? { 1 => Ok(true), 0 => Ok(false), - err => Err(super::Error::BoolConversion(err)), + err => Err(super::Error::BoolConv(err)), } } @@ -43,8 +41,7 @@ impl ReadExt for R { Ok(match self.read_i32::()? { size if size.is_positive() => String::from_utf8(self.read_len(size as usize)?)?, size if size.is_negative() => { - let size = 2 * -size; - let mut buf = Vec::with_capacity(size as usize / 2); + let mut buf = Vec::with_capacity(-size as usize); for _ in 0..buf.capacity() { buf.push(self.read_u16::()?); } diff --git a/src/footer.rs b/src/footer.rs index 805e266..efe2ce9 100644 --- a/src/footer.rs +++ b/src/footer.rs @@ -4,8 +4,9 @@ use byteorder::{ReadBytesExt, LE}; use super::{Compression, ReadExt, Version}; +#[derive(Debug)] pub struct Footer { - pub encryption_guid: Option<[u8; 20]>, + pub encryption_uuid: Option, pub encrypted: Option, pub magic: u32, pub version: Version, @@ -19,8 +20,8 @@ pub struct Footer { impl Footer { pub fn new(reader: &mut R, version: &Version) -> Result { let footer = Footer { - encryption_guid: (version >= &Version::EncryptionKeyGuid) - .then_some(reader.read_guid()?), + encryption_uuid: (version >= &Version::EncryptionKeyGuid) + .then_some(reader.read_u128::()?), encrypted: (version >= &Version::CompressionEncryption).then_some(reader.read_bool()?), magic: reader.read_u32::()?, version: Version::from_repr(reader.read_u32::()?).unwrap_or_default(), @@ -51,10 +52,10 @@ impl Footer { }), }; if super::MAGIC != footer.magic { - return Err(super::Error::MagicMismatch(footer.magic)); + return Err(super::Error::WrongMagic(footer.magic)); } if version != &footer.version { - return Err(super::Error::VersionMismatch(*version, footer.version)); + return Err(super::Error::WrongVersion(*version, footer.version)); } Ok(footer) } diff --git a/src/index.rs b/src/index.rs index 1f7e6fd..92f2ffe 100644 --- a/src/index.rs +++ b/src/index.rs @@ -1,19 +1,21 @@ use super::{ReadExt, Version}; +#[derive(Debug)] pub enum Index { WithoutPathHash(IndexV1), - WithPathHash(IndexV2), + WithPathHash, } impl Index { pub fn new(reader: &mut R, version: &Version) -> Result { Ok(match version < &Version::PathHashIndex { true => Index::WithoutPathHash(IndexV1::new(reader, version)?), - false => Index::WithPathHash(todo!()), + false => Index::WithPathHash, }) } } +#[derive(Debug)] pub struct IndexV1 { pub mount_point: String, pub entries: Vec, diff --git a/src/pak.rs b/src/pak.rs index 180178b..62d6ac0 100644 --- a/src/pak.rs +++ b/src/pak.rs @@ -2,6 +2,7 @@ use std::io; use super::Version; +#[derive(Debug)] pub struct Pak { pub version: Version, pub footer: super::Footer, @@ -29,8 +30,8 @@ fn footer_size(version: &Version) -> i64 { // (magic + version): u32 + (offset + size): u64 + hash: [u8; 20] let mut size = 4 + 4 + 8 + 8 + 20; if version >= &Version::EncryptionKeyGuid { - // encryption guid: [u8; 20] - size += 20; + // encryption uuid: u128 + size += 16; } if version >= &Version::IndexEncryption { // encrypted: bool