Skip to content

Commit

Permalink
d2: Deduplicate common structures
Browse files Browse the repository at this point in the history
  • Loading branch information
cohaereo committed Jul 23, 2023
1 parent cfa4847 commit a016379
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 130 deletions.
3 changes: 2 additions & 1 deletion src/d2_beta/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
44 changes: 2 additions & 42 deletions src/d2_beta/structs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::TagHash;
use binrw::{BinRead, BinWrite};

use binrw::{BinRead};
use std::fmt::Debug;
use std::io::SeekFrom;

Expand Down Expand Up @@ -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,
}
3 changes: 2 additions & 1 deletion src/d2_beyondlight/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
45 changes: 2 additions & 43 deletions src/d2_beyondlight/structs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::TagHash;
use binrw::{BinRead, BinWrite};

use binrw::{BinRead};
use std::fmt::Debug;
use std::io::SeekFrom;

Expand Down Expand Up @@ -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,
}
3 changes: 2 additions & 1 deletion src/d2_prebl/impl.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
44 changes: 2 additions & 42 deletions src/d2_prebl/structs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::TagHash;
use binrw::{BinRead, BinWrite};

use binrw::{BinRead};
use std::fmt::Debug;
use std::io::SeekFrom;

Expand Down Expand Up @@ -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,
}
42 changes: 42 additions & 0 deletions src/d2_shared.rs
Original file line number Diff line number Diff line change
@@ -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,
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extern crate core;

mod crypto;
mod d2_shared;
mod oodle;

mod d1_legacy;
Expand Down

0 comments on commit a016379

Please sign in to comment.