diff --git a/Cargo.lock b/Cargo.lock index 0920bf3..26b7aff 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -33,20 +33,22 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "binrw" -version = "0.9.2" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4abb4fd60add897b9e8827e0d5fa6c2ca129ece2432d9aa13454b21ac2ecc18f" +checksum = "f846d8732b2a55b569b885852ecc925a2b1f24568f4707f8b1ccd5dc6805ea9b" dependencies = [ "array-init", "binrw_derive", + "bytemuck", ] [[package]] name = "binrw_derive" -version = "0.9.0" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a36ff195a3a1a82d5eeb98e0069bdf3ea076042d28591396d9020fac763bf66f" +checksum = "5c2aa66a5e35daf7f91ed44c945886597ef4c327f34f68b6bbf22951a250ceeb" dependencies = [ + "either", "owo-colors", "proc-macro2", "quote", @@ -76,6 +78,12 @@ version = "3.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1ad822118d20d2c234f427000d5acc36eabe1e29a348c89b63dd60b13f28e5d" +[[package]] +name = "bytemuck" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f5715e491b5a1598fc2bef5a606847b5dc1d48ea625bd3c02c00de8285591da" + [[package]] name = "cast" version = "0.3.0" diff --git a/Cargo.toml b/Cargo.toml index 8505893..09b06b7 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,7 @@ patch_testing = [] crc = "3.0.0" # amazing binary parsing/writing library -binrw = "0.9.2" +binrw = "0.10.0" # used for zlib compression in sqpack files libz-sys = { version = "1.1.8", default-features = false } diff --git a/src/dat.rs b/src/dat.rs index ec98e15..3b66464 100755 --- a/src/dat.rs +++ b/src/dat.rs @@ -1,7 +1,7 @@ use crate::gamedata::MemoryBuffer; use crate::model::ModelFileHeader; use crate::sqpack::read_data_block; -use binrw::binrw; +use binrw::{binrw, Endian, ReadOptions}; use binrw::BinRead; use binrw::BinWrite; use std::io::Write; @@ -116,6 +116,7 @@ struct TextureBlock { /// A SqPack file info header. It can optionally contain extra information, such as texture or /// model data depending on the file type. #[derive(BinRead)] +#[br(little)] struct FileInfo { size: u32, file_type: FileType, @@ -132,6 +133,7 @@ struct FileInfo { } #[binrw] +#[br(little)] pub struct Block { #[br(pad_after = 4)] offset: i32, @@ -154,6 +156,7 @@ pub enum CompressionMode { #[binrw::binread] #[derive(Debug)] +#[br(little)] pub struct BlockHeader { #[br(pad_after = 4)] pub size: u32, @@ -380,7 +383,7 @@ impl DatFile { buffer.seek(SeekFrom::Start(0)).ok()?; - header.write_to(&mut buffer).ok()?; + header.write(&mut buffer).ok()?; Some(buffer.into_inner()) } @@ -421,7 +424,8 @@ impl DatFile { self.file.seek(SeekFrom::Start(original_pos)).ok()?; - running_block_total += i16::read(&mut self.file).ok()? as u64; + let mut options = ReadOptions::new(Endian::Little); + running_block_total += i16::read_options(&mut self.file, &options, ()).ok()? as u64; } } diff --git a/src/fiin.rs b/src/fiin.rs index f8bf08e..e54d4e7 100644 --- a/src/fiin.rs +++ b/src/fiin.rs @@ -7,6 +7,7 @@ use std::io::Cursor; #[binrw] #[brw(magic = b"FileInfo")] #[derive(Debug)] +#[br(little)] pub struct FileInfo { #[brw(pad_before = 16)] #[br(ignore)] diff --git a/src/index.rs b/src/index.rs index 624bed3..5baee1c 100755 --- a/src/index.rs +++ b/src/index.rs @@ -60,6 +60,7 @@ pub struct IndexEntry { } #[binrw] +#[br(little)] pub struct IndexFile { sqpack_header: SqPackHeader, diff --git a/src/log.rs b/src/log.rs index dc85bf4..662cfae 100644 --- a/src/log.rs +++ b/src/log.rs @@ -5,6 +5,7 @@ use std::io::{Cursor, Seek, SeekFrom}; #[binread] #[allow(dead_code)] +#[br(little)] pub struct ChatLogHeader { content_size: u32, file_size: u32, @@ -46,6 +47,7 @@ enum EventChannel { #[binread] #[derive(Debug)] #[allow(dead_code)] +#[br(little)] pub struct ChatLogEntry { timestamp: u32, filter: EventFilter, diff --git a/src/model.rs b/src/model.rs index e9a6796..625fb23 100755 --- a/src/model.rs +++ b/src/model.rs @@ -1,5 +1,5 @@ use crate::gamedata::MemoryBuffer; -use binrw::binread; +use binrw::{binread, Endian, ReadOptions}; use binrw::binrw; use binrw::BinRead; use half::f16; @@ -7,6 +7,7 @@ use std::io::{Cursor, Seek, SeekFrom}; #[binrw] #[derive(Debug)] +#[brw(little)] pub struct ModelFileHeader { pub(crate) version: u32, @@ -196,6 +197,7 @@ struct BoundingBox { #[binread] #[derive(Debug)] #[allow(dead_code)] +#[br(little)] struct ModelData { header: ModelHeader, @@ -285,6 +287,7 @@ enum VertexUsage { #[binread] #[derive(Copy, Clone, Debug)] #[allow(dead_code)] +#[br(little)] struct VertexElement { stream: u8, offset: u8, @@ -404,14 +407,16 @@ impl MDL { )) .ok()?; + let mut options = ReadOptions::new(Endian::Little); + match element.vertex_usage { VertexUsage::Position => { vertices[k as usize].position = - <[f32; 3]>::read(&mut cursor).unwrap(); + <[f32; 3]>::read_options(&mut cursor, &options, ()).unwrap(); } VertexUsage::BlendWeights => { vertices[k as usize].bone_weight = - <[f32; 4]>::read(&mut cursor).unwrap(); + <[f32; 4]>::read_options(&mut cursor, &options, ()).unwrap(); } VertexUsage::BlendIndices => { vertices[k as usize].bone_id = @@ -420,21 +425,21 @@ impl MDL { VertexUsage::Normal => { // TODO: normals are assumed to be half4 vertices[k as usize].normal[0] = - f16::from_bits(::read(&mut cursor).unwrap()) + f16::from_bits(::read_options(&mut cursor, &options, ()).unwrap()) .to_f32(); vertices[k as usize].normal[1] = - f16::from_bits(::read(&mut cursor).unwrap()) + f16::from_bits(::read_options(&mut cursor, &options, ()).unwrap()) .to_f32(); vertices[k as usize].normal[2] = - f16::from_bits(::read(&mut cursor).unwrap()) + f16::from_bits(::read_options(&mut cursor, &options, ()).unwrap()) .to_f32(); } VertexUsage::UV => { vertices[k as usize].uv[0] = - f16::from_bits(::read(&mut cursor).unwrap()) + f16::from_bits(::read_options(&mut cursor, &options, ()).unwrap()) .to_f32(); vertices[k as usize].uv[1] = - f16::from_bits(::read(&mut cursor).unwrap()) + f16::from_bits(::read_options(&mut cursor, &options, ()).unwrap()) .to_f32(); } VertexUsage::Tangent2 => {} @@ -456,7 +461,8 @@ impl MDL { let mut indices: Vec = Vec::with_capacity(model.meshes[j as usize].index_count as usize); for _ in 0..model.meshes[j as usize].index_count { - indices.push(::read(&mut cursor).unwrap()); + let mut options = ReadOptions::new(Endian::Little); + indices.push(::read_options(&mut cursor, &options, ()).unwrap()); } parts.push(Part { vertices, indices }); diff --git a/src/mtrl.rs b/src/mtrl.rs index 96b07c6..575422f 100644 --- a/src/mtrl.rs +++ b/src/mtrl.rs @@ -82,6 +82,7 @@ struct Sampler { #[binread] #[derive(Debug)] #[allow(dead_code)] +#[br(little)] struct MaterialData { file_header: MaterialFileHeader, diff --git a/src/patch.rs b/src/patch.rs index 1f96643..e0233a8 100755 --- a/src/patch.rs +++ b/src/patch.rs @@ -11,6 +11,7 @@ use std::path::PathBuf; #[binread] #[derive(Debug)] +#[br(little)] struct PatchHeader { #[br(temp)] #[br(count = 7)] @@ -22,6 +23,7 @@ struct PatchHeader { #[derive(BinRead, Debug)] #[allow(dead_code)] +#[br(little)] struct PatchChunk { #[br(big)] size: u32, diff --git a/src/tex.rs b/src/tex.rs index 8b44a88..8b1cb6a 100644 --- a/src/tex.rs +++ b/src/tex.rs @@ -49,6 +49,7 @@ enum TextureFormat { #[binread] #[derive(Debug)] #[allow(dead_code)] +#[br(little)] struct TexHeader { attribute: TextureAttribute, format: TextureFormat,