Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for reading wad v3.4 #11

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/league-toolkit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ miette = "7.2.0"
enum_dispatch = "0.3.13"

[dev-dependencies]
league-toolkit = {path = ".", features = ["serde"]}
league-toolkit = { path = ".", features = ["serde"] }
approx = "0.5.1"
insta = { version = "1.39.0", features = ["ron"] }
serde = { version = "*", features = ["derive"] }
Expand Down
19 changes: 15 additions & 4 deletions crates/league-toolkit/src/core/wad/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::io::{BufReader, Read};
use byteorder::{ReadBytesExt as _, LE};
use num_enum::{IntoPrimitive, TryFromPrimitive};

use crate::util::{int, u24};

use super::WadError;

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand All @@ -26,12 +28,15 @@ pub struct WadChunk {
pub compression_type: WadChunkCompression,
pub is_duplicated: bool,
pub frame_count: u8,
pub start_frame: u16,
pub start_frame: u32,
pub checksum: u64,
}

impl WadChunk {
pub(crate) fn read<R: Read>(reader: &mut BufReader<R>) -> Result<WadChunk, WadError> {
pub(crate) fn read<R: Read>(
reader: &mut BufReader<R>,
minor: u8,
) -> Result<WadChunk, WadError> {
let path_hash = reader.read_u64::<LE>()?;
let data_offset = reader.read_u32::<LE>()? as usize;
let compressed_size = reader.read_i32::<LE>()? as usize;
Expand All @@ -42,8 +47,14 @@ impl WadChunk {
let compression_type = WadChunkCompression::try_from_primitive(type_frame_count & 0xF)
.expect("failed to read chunk compression");

let is_duplicated = reader.read_u8()? == 1;
let start_frame = reader.read_u16::<LE>()?;
let is_duplicated = match minor {
..4 => false,
4.. => reader.read_u8()? == 1,
};
let start_frame = match minor {
..4 => reader.read_u16::<LE>()? as u32,
4.. => reader.read_u24::<LE>()?,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a mixed endian in order from first byte to last: hi, lo, mi

};
let checksum = reader.read_u64::<LE>()?;

Ok(WadChunk {
Expand Down
2 changes: 1 addition & 1 deletion crates/league-toolkit/src/core/wad/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<TSource: Read + Seek> Wad<TSource> {
let chunk_count = reader.read_i32::<LE>()? as usize;
let mut chunks = HashMap::<u64, WadChunk>::with_capacity(chunk_count);
for _ in 0..chunk_count {
let chunk = WadChunk::read(&mut reader)?;
let chunk = WadChunk::read(&mut reader, minor)?;
chunks
.insert(chunk.path_hash(), chunk)
.map_or(Ok(()), |chunk| {
Expand Down
57 changes: 57 additions & 0 deletions crates/league-toolkit/src/util/int.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[allow(non_camel_case_types)]
pub struct u24 {
lo: u8,
mi: u8,
hi: u8,
}

impl u24 {
pub fn new(value: u32) -> Self {
Self {
lo: (value & 0xFF) as u8,
mi: ((value >> 8) & 0xFF) as u8,
hi: ((value >> 16) & 0xFF) as u8,
}
}
}

impl From<u32> for u24 {
fn from(value: u32) -> Self {
Self::new(value)
}
}

impl From<u24> for u32 {
fn from(value: u24) -> Self {
(value.hi as u32) << 16 | (value.mi as u32) << 8 | (value.lo as u32)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_u24_new() {
let value = u24::new(0x123456);
assert_eq!(value.hi, 0x12);
assert_eq!(value.mi, 0x34);
assert_eq!(value.lo, 0x56);
}

#[test]
fn test_u24_from_u32() {
let value: u24 = 0x123456_u32.into();
assert_eq!(value.hi, 0x12);
assert_eq!(value.mi, 0x34);
assert_eq!(value.lo, 0x56);
}

#[test]
fn test_u32_from_u24() {
let u24_value = u24::new(0x123456);
let u32_value: u32 = u24_value.into();
assert_eq!(u32_value, 0x123456);
}
}
2 changes: 2 additions & 0 deletions crates/league-toolkit/src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pub mod hash;
pub mod int;
pub mod reader;
pub mod writer;

pub use int::*;
pub use reader::*;
pub use writer::*;

Expand Down
13 changes: 9 additions & 4 deletions crates/league-toolkit/src/util/reader.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::io::{self, Read};

use super::u24;
use crate::core::primitives::{Color, Sphere, AABB};
use byteorder::{ByteOrder, ReadBytesExt};
use glam::{Mat4, Quat, Vec2, Vec3, Vec4};

use crate::core::primitives::{Color, Sphere, AABB};
use std::io::{self, Read};

#[derive(Debug, thiserror::Error)]
pub enum ReaderError {
Expand All @@ -18,6 +17,12 @@ pub enum ReaderError {
pub type ReaderResult<T> = core::result::Result<T, ReaderError>;

pub trait ReaderExt: Read {
fn read_u24<T: ByteOrder>(&mut self) -> io::Result<u24> {
let mut buf = [0; 3];
self.read_exact(&mut buf)?;
Ok(u24::new(T::read_u32(&buf)))
}

fn read_padded_string<T: ByteOrder, const N: usize>(&mut self) -> ReaderResult<String> {
let mut buf: [u8; N] = [0; N];
self.read_exact(&mut buf)?;
Expand Down
39 changes: 39 additions & 0 deletions crates/league-toolkit/tests/snapshots/wad__read.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
source: crates/league-toolkit/tests/wad.rs
expression: wad.chunks()
---
{
2227312568830940929: WadChunk(
path_hash: 2227312568830940929,
data_offset: 10911,
compressed_size: 23023,
uncompressed_size: 131084,
compression_type: Zstd,
is_duplicated: false,
frame_count: 0,
start_frame: 0,
checksum: 3922618146135880204,
),
3349600290122526159: WadChunk(
path_hash: 3349600290122526159,
data_offset: 33934,
compressed_size: 20786,
uncompressed_size: 131084,
compression_type: Zstd,
is_duplicated: false,
frame_count: 0,
start_frame: 0,
checksum: 13212711047613655776,
),
11042693609879973350: WadChunk(
path_hash: 11042693609879973350,
data_offset: 368,
compressed_size: 10543,
uncompressed_size: 32780,
compression_type: Zstd,
is_duplicated: false,
frame_count: 0,
start_frame: 0,
checksum: 3841611443327106610,
),
}
13 changes: 13 additions & 0 deletions crates/league-toolkit/tests/wad.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use insta::assert_ron_snapshot;
use league_toolkit::core::wad::Wad;
use std::io::Cursor;

#[test]
fn read() {
let r = Cursor::new(include_bytes!("wads/UI.en_US.wad.client.v3-4"));
let wad = Wad::mount(r).unwrap();

insta::with_settings!({sort_maps => true}, {
assert_ron_snapshot!(wad.chunks());
});
}
Binary file not shown.
Loading