refactor - will decompress soon

This commit is contained in:
spuds 2023-01-10 22:29:20 +00:00
parent 778b87570a
commit c571865d94
6 changed files with 37 additions and 63 deletions

View file

@ -11,6 +11,7 @@ edition = "2021"
[dependencies] [dependencies]
byteorder = "*" byteorder = "*"
strum = { version = "*", features = ["derive"] } strum = { version = "*", features = ["derive"] }
hashbrown = "*"
thiserror = "*" thiserror = "*"
[profile.release] [profile.release]

View file

@ -4,7 +4,6 @@ use super::{Compression, ReadExt, Version};
#[derive(Debug)] #[derive(Debug)]
pub struct Entry { pub struct Entry {
pub name: String,
pub offset: u64, pub offset: u64,
pub compressed: u64, pub compressed: u64,
pub uncompressed: u64, pub uncompressed: u64,
@ -12,6 +11,8 @@ pub struct Entry {
pub timestamp: Option<u64>, pub timestamp: Option<u64>,
pub hash: [u8; 20], pub hash: [u8; 20],
pub compression_blocks: Option<Vec<Block>>, pub compression_blocks: Option<Vec<Block>>,
pub encrypted: bool,
pub block_uncompressed: Option<u32>,
} }
impl Entry { impl Entry {
@ -19,27 +20,34 @@ impl Entry {
reader: &mut R, reader: &mut R,
version: super::Version, version: super::Version,
) -> Result<Self, super::Error> { ) -> Result<Self, super::Error> {
let name = reader.read_string()?;
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_method = match reader.read_u32::<LE>()? { let compression_method = match reader.read_u32::<LE>()? {
0x01 => Compression::Zlib, 0x01 | 0x10 | 0x20 => Compression::Zlib,
0x10 => Compression::ZlibBiasMemory,
0x20 => Compression::ZlibBiasMemory,
_ => Compression::None, _ => Compression::None,
}; };
Ok(Self { Ok(Self {
name,
offset, offset,
compressed, compressed,
uncompressed, uncompressed,
compression_method, compression_method,
timestamp: (version == Version::Initial).then_some(reader.read_u64::<LE>()?), timestamp: match version == Version::Initial {
true => Some(reader.read_u64::<LE>()?),
false => None,
},
hash: reader.read_guid()?, hash: reader.read_guid()?,
compression_blocks: (version >= Version::CompressionEncryption compression_blocks: match version >= Version::CompressionEncryption
&& compression_method != Compression::None) && compression_method != Compression::None
.then_some(reader.read_array(Block::new)?), {
true => Some(reader.read_array(Block::new)?),
false => None,
},
encrypted: version >= Version::CompressionEncryption && reader.read_bool()?,
block_uncompressed: match version >= Version::CompressionEncryption {
true => Some(reader.read_u32::<LE>()?),
false => None,
},
}) })
} }
} }

View file

@ -7,13 +7,13 @@ use super::{Compression, ReadExt, Version};
#[derive(Debug)] #[derive(Debug)]
pub struct Footer { pub struct Footer {
pub encryption_uuid: Option<u128>, pub encryption_uuid: Option<u128>,
pub encrypted: Option<bool>, pub encrypted: bool,
pub magic: u32, pub magic: u32,
pub version: Version, pub version: Version,
pub offset: u64, pub offset: u64,
pub size: u64, pub size: u64,
pub hash: [u8; 20], pub hash: [u8; 20],
pub frozen: Option<bool>, pub frozen: bool,
pub compression: Option<Vec<Compression>>, pub compression: Option<Vec<Compression>>,
} }
@ -24,19 +24,13 @@ impl Footer {
true => Some(reader.read_u128::<LE>()?), true => Some(reader.read_u128::<LE>()?),
false => None, false => None,
}, },
encrypted: match version >= Version::IndexEncryption { encrypted: version >= Version::IndexEncryption && reader.read_bool()?,
true => Some(reader.read_bool()?),
false => None,
},
magic: reader.read_u32::<LE>()?, magic: reader.read_u32::<LE>()?,
version: Version::from_repr(reader.read_u32::<LE>()?).unwrap_or(version), version: Version::from_repr(reader.read_u32::<LE>()?).unwrap_or(version),
offset: reader.read_u64::<LE>()?, offset: reader.read_u64::<LE>()?,
size: reader.read_u64::<LE>()?, size: reader.read_u64::<LE>()?,
hash: reader.read_guid()?, hash: reader.read_guid()?,
frozen: match version == Version::FrozenIndex { frozen: version == Version::FrozenIndex && reader.read_bool()?,
true => Some(reader.read_bool()?),
false => None,
},
compression: match version >= Version::FNameBasedCompression { compression: match version >= Version::FNameBasedCompression {
true => { true => {
let mut compression = let mut compression =

View file

@ -1,36 +0,0 @@
use super::{ReadExt, Version};
#[derive(Debug)]
pub enum Index {
WithoutPathHash(IndexV1),
WithPathHash,
}
impl Index {
pub fn new<R: std::io::Read>(reader: &mut R, version: Version) -> Result<Self, super::Error> {
Ok(match version < Version::PathHashIndex {
true => Index::WithoutPathHash(IndexV1::new(reader, version)?),
false => Index::WithPathHash,
})
}
}
#[derive(Debug)]
pub struct IndexV1 {
pub mount_point: String,
pub entries: Vec<super::Entry>,
}
impl IndexV1 {
pub fn new<R: std::io::Read>(
reader: &mut R,
version: super::Version,
) -> Result<Self, super::Error> {
Ok(Self {
mount_point: reader.read_string()?,
entries: reader.read_array(|reader| super::Entry::new(reader, version))?,
})
}
}
pub struct IndexV2 {}

View file

@ -3,10 +3,9 @@ mod entry;
mod error; mod error;
mod ext; mod ext;
mod footer; mod footer;
mod index;
mod pak; mod pak;
pub use {entry::*, error::*, ext::*, footer::*, index::*, pak::*}; pub use {entry::*, error::*, ext::*, footer::*, pak::*};
pub const MAGIC: u32 = 0x5A6F12E1; pub const MAGIC: u32 = 0x5A6F12E1;
@ -40,8 +39,6 @@ pub enum Compression {
#[default] #[default]
None, None,
Zlib, Zlib,
ZlibBiasMemory,
ZlibBiasSpeed,
Gzip, Gzip,
Oodle, Oodle,
} }

View file

@ -1,12 +1,15 @@
use std::io; use std::io;
use super::ReadExt;
use byteorder::{ReadBytesExt, LE};
use super::Version; use super::Version;
#[derive(Debug)] #[derive(Debug)]
pub struct Pak { pub struct Pak {
pub version: Version, pub version: Version,
pub footer: super::Footer, pub footer: super::Footer,
pub index: super::Index, pub mount_point: String,
} }
impl Pak { impl Pak {
@ -17,11 +20,18 @@ impl Pak {
reader.seek(io::SeekFrom::End(-footer_size(&version)))?; reader.seek(io::SeekFrom::End(-footer_size(&version)))?;
let footer = super::Footer::new(&mut reader, version)?; let footer = super::Footer::new(&mut reader, version)?;
reader.seek(io::SeekFrom::Start(footer.offset))?; reader.seek(io::SeekFrom::Start(footer.offset))?;
let index = super::Index::new(&mut reader, version)?; let mount_point = reader.read_string()?;
let mut entries = hashbrown::HashMap::with_capacity(reader.read_u32::<LE>()? as usize);
for _ in 0..entries.capacity() {
entries.insert(
reader.read_string()?,
super::Entry::new(&mut reader, version)?,
);
}
Ok(Self { Ok(Self {
version, version,
footer, footer,
index, mount_point,
}) })
} }
} }