From a016379a94cd0c67bb5cfe2218346e930326d073 Mon Sep 17 00:00:00 2001 From: cohaereo Date: Sun, 23 Jul 2023 10:02:50 +0200 Subject: [PATCH] d2: Deduplicate common structures --- src/d2_beta/impl.rs | 3 ++- src/d2_beta/structs.rs | 44 ++-------------------------------- src/d2_beyondlight/impl.rs | 3 ++- src/d2_beyondlight/structs.rs | 45 ++--------------------------------- src/d2_prebl/impl.rs | 3 ++- src/d2_prebl/structs.rs | 44 ++-------------------------------- src/d2_shared.rs | 42 ++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 8 files changed, 55 insertions(+), 130 deletions(-) create mode 100644 src/d2_shared.rs diff --git a/src/d2_beta/impl.rs b/src/d2_beta/impl.rs index b538097..b7ec455 100644 --- a/src/d2_beta/impl.rs +++ b/src/d2_beta/impl.rs @@ -11,7 +11,8 @@ use binrw::{BinReaderExt, Endian, VecArgs}; use nohash_hasher::IntMap; use crate::crypto::PkgGcmState; -use crate::d2_beta::structs::{BlockHeader, EntryHeader, PackageHeader}; +use crate::d2_beta::structs::PackageHeader; +use crate::d2_shared::{BlockHeader, EntryHeader}; use crate::package::{Package, ReadSeek, UEntryHeader, UHashTableEntry, BLOCK_CACHE_SIZE}; use crate::{oodle, PackageVersion}; diff --git a/src/d2_beta/structs.rs b/src/d2_beta/structs.rs index e65b95f..2daa3f5 100644 --- a/src/d2_beta/structs.rs +++ b/src/d2_beta/structs.rs @@ -1,5 +1,5 @@ -use crate::TagHash; -use binrw::{BinRead, BinWrite}; + +use binrw::{BinRead}; use std::fmt::Debug; use std::io::SeekFrom; @@ -34,43 +34,3 @@ pub struct PackageHeader { #[br(seek_before = SeekFrom::Start(0x164))] pub file_size: u32, } - -#[derive(BinRead, Debug, Clone)] -pub struct EntryHeader { - pub reference: u32, - - _type_info: u32, - - #[br(calc = (_type_info >> 9) as u8 & 0x7f)] - pub file_type: u8, - #[br(calc = (_type_info >> 6) as u8 & 0x7)] - pub file_subtype: u8, - - _block_info: u64, - - #[br(calc = _block_info as u32 & 0x3fff)] - pub starting_block: u32, - - #[br(calc = ((_block_info >> 14) as u32 & 0x3FFF) << 4)] - pub starting_block_offset: u32, - - #[br(calc = (_block_info >> 28) as u32)] - pub file_size: u32, -} - -#[derive(BinRead, Debug, Clone)] -pub struct BlockHeader { - pub offset: u32, - pub size: u32, - pub patch_id: u16, - pub flags: u16, - pub hash: [u8; 20], - pub gcm_tag: [u8; 16], -} - -#[derive(BinRead, BinWrite, Debug, Clone)] -pub struct HashTableEntry { - pub hash64: u64, - pub hash32: TagHash, - pub reference: TagHash, -} diff --git a/src/d2_beyondlight/impl.rs b/src/d2_beyondlight/impl.rs index 93eba2b..25c2e31 100644 --- a/src/d2_beyondlight/impl.rs +++ b/src/d2_beyondlight/impl.rs @@ -11,7 +11,8 @@ use binrw::{BinReaderExt, Endian, VecArgs}; use nohash_hasher::IntMap; use crate::crypto::PkgGcmState; -use crate::d2_beyondlight::structs::{BlockHeader, EntryHeader, PackageHeader}; +use crate::d2_beyondlight::structs::PackageHeader; +use crate::d2_shared::{BlockHeader, EntryHeader}; use crate::package::{Package, ReadSeek, UEntryHeader, UHashTableEntry, BLOCK_CACHE_SIZE}; use crate::{oodle, PackageVersion}; diff --git a/src/d2_beyondlight/structs.rs b/src/d2_beyondlight/structs.rs index c365413..1879fb5 100644 --- a/src/d2_beyondlight/structs.rs +++ b/src/d2_beyondlight/structs.rs @@ -1,5 +1,5 @@ -use crate::TagHash; -use binrw::{BinRead, BinWrite}; + +use binrw::{BinRead}; use std::fmt::Debug; use std::io::SeekFrom; @@ -27,44 +27,3 @@ pub struct PackageHeader { #[br(seek_before = SeekFrom::Start(0x120))] pub file_size: u32, } - -// TODO(cohae): We can share these with all D2 implementations -#[derive(BinRead, Debug, Clone)] -pub struct EntryHeader { - pub reference: u32, - - _type_info: u32, - - #[br(calc = (_type_info >> 9) as u8 & 0x7f)] - pub file_type: u8, - #[br(calc = (_type_info >> 6) as u8 & 0x7)] - pub file_subtype: u8, - - _block_info: u64, - - #[br(calc = _block_info as u32 & 0x3fff)] - pub starting_block: u32, - - #[br(calc = ((_block_info >> 14) as u32 & 0x3FFF) << 4)] - pub starting_block_offset: u32, - - #[br(calc = (_block_info >> 28) as u32)] - pub file_size: u32, -} - -#[derive(BinRead, Debug, Clone)] -pub struct BlockHeader { - pub offset: u32, - pub size: u32, - pub patch_id: u16, - pub flags: u16, - pub hash: [u8; 20], - pub gcm_tag: [u8; 16], -} - -#[derive(BinRead, BinWrite, Debug, Clone)] -pub struct HashTableEntry { - pub hash64: u64, - pub hash32: TagHash, - pub reference: TagHash, -} diff --git a/src/d2_prebl/impl.rs b/src/d2_prebl/impl.rs index d8940c2..7455bdf 100644 --- a/src/d2_prebl/impl.rs +++ b/src/d2_prebl/impl.rs @@ -1,5 +1,6 @@ use crate::crypto::PkgGcmState; -use crate::d2_prebl::structs::{BlockHeader, EntryHeader, HashTableEntry, PackageHeader}; +use crate::d2_prebl::structs::PackageHeader; +use crate::d2_shared::{BlockHeader, EntryHeader, HashTableEntry}; use crate::package::{Package, ReadSeek, UEntryHeader, UHashTableEntry, BLOCK_CACHE_SIZE}; use crate::{oodle, PackageVersion}; use anyhow::Context; diff --git a/src/d2_prebl/structs.rs b/src/d2_prebl/structs.rs index de18a37..968abe7 100644 --- a/src/d2_prebl/structs.rs +++ b/src/d2_prebl/structs.rs @@ -1,5 +1,5 @@ -use crate::TagHash; -use binrw::{BinRead, BinWrite}; + +use binrw::{BinRead}; use std::fmt::Debug; use std::io::SeekFrom; @@ -38,43 +38,3 @@ pub struct PackageHeader { #[br(seek_before = SeekFrom::Start(0x164))] pub file_size: u32, } - -#[derive(BinRead, Debug, Clone)] -pub struct EntryHeader { - pub reference: u32, - - _type_info: u32, - - #[br(calc = (_type_info >> 9) as u8 & 0x7f)] - pub file_type: u8, - #[br(calc = (_type_info >> 6) as u8 & 0x7)] - pub file_subtype: u8, - - _block_info: u64, - - #[br(calc = _block_info as u32 & 0x3fff)] - pub starting_block: u32, - - #[br(calc = ((_block_info >> 14) as u32 & 0x3FFF) << 4)] - pub starting_block_offset: u32, - - #[br(calc = (_block_info >> 28) as u32)] - pub file_size: u32, -} - -#[derive(BinRead, Debug, Clone)] -pub struct BlockHeader { - pub offset: u32, - pub size: u32, - pub patch_id: u16, - pub flags: u16, - pub hash: [u8; 20], - pub gcm_tag: [u8; 16], -} - -#[derive(BinRead, BinWrite, Debug, Clone)] -pub struct HashTableEntry { - pub hash64: u64, - pub hash32: TagHash, - pub reference: TagHash, -} diff --git a/src/d2_shared.rs b/src/d2_shared.rs new file mode 100644 index 0000000..3e69c33 --- /dev/null +++ b/src/d2_shared.rs @@ -0,0 +1,42 @@ +use crate::TagHash; +use binrw::BinRead; + +#[derive(BinRead, Debug, Clone)] +pub struct EntryHeader { + pub reference: u32, + + _type_info: u32, + + #[br(calc = (_type_info >> 9) as u8 & 0x7f)] + pub file_type: u8, + #[br(calc = (_type_info >> 6) as u8 & 0x7)] + pub file_subtype: u8, + + _block_info: u64, + + #[br(calc = _block_info as u32 & 0x3fff)] + pub starting_block: u32, + + #[br(calc = ((_block_info >> 14) as u32 & 0x3FFF) << 4)] + pub starting_block_offset: u32, + + #[br(calc = (_block_info >> 28) as u32)] + pub file_size: u32, +} + +#[derive(BinRead, Debug, Clone)] +pub struct BlockHeader { + pub offset: u32, + pub size: u32, + pub patch_id: u16, + pub flags: u16, + pub hash: [u8; 20], + pub gcm_tag: [u8; 16], +} + +#[derive(BinRead, Debug, Clone)] +pub struct HashTableEntry { + pub hash64: u64, + pub hash32: TagHash, + pub reference: TagHash, +} diff --git a/src/lib.rs b/src/lib.rs index bf89945..d0b5008 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ extern crate core; mod crypto; +mod d2_shared; mod oodle; mod d1_legacy;