destiny_pkg/
d2_shared.rs

1use std::{
2    borrow::Cow,
3    collections::hash_map::Entry,
4    fs::File,
5    io::{Read, Seek, SeekFrom},
6    sync::{
7        atomic::{AtomicUsize, Ordering},
8        Arc,
9    },
10};
11
12use anyhow::Context;
13use binrw::{BinRead, BinReaderExt, NullString};
14use parking_lot::RwLock;
15use rustc_hash::FxHashMap;
16
17use crate::{
18    crypto::PkgGcmState,
19    oodle,
20    package::{PackageLanguage, ReadSeek, UEntryHeader, BLOCK_CACHE_SIZE},
21    GameVersion, TagHash,
22};
23
24#[derive(BinRead, Debug, Clone)]
25pub struct EntryHeader {
26    pub reference: u32,
27
28    _type_info: u32,
29
30    #[br(calc = (_type_info >> 9) as u8 & 0x7f)]
31    pub file_type: u8,
32    #[br(calc = (_type_info >> 6) as u8 & 0x7)]
33    pub file_subtype: u8,
34
35    _block_info: u64,
36
37    #[br(calc = _block_info as u32 & 0x3fff)]
38    pub starting_block: u32,
39
40    #[br(calc = ((_block_info >> 14) as u32 & 0x3FFF) << 4)]
41    pub starting_block_offset: u32,
42
43    #[br(calc = (_block_info >> 28) as u32)]
44    pub file_size: u32,
45}
46
47#[derive(BinRead, Debug, Clone)]
48pub struct BlockHeader {
49    pub offset: u32,
50    pub size: u32,
51    pub patch_id: u16,
52    pub flags: u16,
53    pub hash: [u8; 20],
54    pub gcm_tag: [u8; 16],
55}
56
57#[derive(BinRead, Debug, Clone)]
58pub struct HashTableEntry {
59    pub hash64: u64,
60    pub hash32: TagHash,
61    pub reference: TagHash,
62}
63
64pub const BLOCK_SIZE: usize = 0x40000;
65
66pub struct PackageCommonD2 {
67    pub(crate) version: GameVersion,
68    pub(crate) pkg_id: u16,
69    pub(crate) patch_id: u16,
70    pub(crate) language: PackageLanguage,
71
72    pub(crate) gcm: RwLock<PkgGcmState>,
73    pub(crate) _entries: Vec<EntryHeader>,
74    pub(crate) entries_unified: Arc<[UEntryHeader]>,
75    pub(crate) blocks: Vec<BlockHeader>,
76    pub(crate) hashes: Vec<HashTableEntry>,
77
78    pub(crate) reader: RwLock<Box<dyn ReadSeek>>,
79    pub(crate) path_base: String,
80
81    /// Used for purging old blocks
82    pub(crate) block_counter: AtomicUsize,
83    pub(crate) block_cache: RwLock<FxHashMap<usize, (usize, Arc<Vec<u8>>)>>,
84    pub(crate) file_handles: RwLock<FxHashMap<usize, File>>,
85}
86
87impl PackageCommonD2 {
88    pub fn new<R: ReadSeek + 'static>(
89        reader: R,
90        version: GameVersion,
91        pkg_id: u16,
92        patch_id: u16,
93        group_id: u64,
94        entries: Vec<EntryHeader>,
95        blocks: Vec<BlockHeader>,
96        hashes: Vec<HashTableEntry>,
97        path: String,
98        language: PackageLanguage,
99    ) -> anyhow::Result<PackageCommonD2> {
100        let last_underscore_pos = path.rfind('_').unwrap();
101        let path_base = path[..last_underscore_pos].to_owned();
102
103        let entries_unified: Vec<UEntryHeader> = entries
104            .iter()
105            .map(|e| UEntryHeader {
106                reference: e.reference,
107                file_type: e.file_type,
108                file_subtype: e.file_subtype,
109                starting_block: e.starting_block,
110                starting_block_offset: e.starting_block_offset,
111                file_size: e.file_size,
112            })
113            .collect();
114
115        Ok(PackageCommonD2 {
116            version,
117            pkg_id,
118            patch_id,
119            language,
120            gcm: RwLock::new(PkgGcmState::new(pkg_id, version, group_id)),
121            _entries: entries,
122            entries_unified: entries_unified.into(),
123            blocks,
124            hashes,
125            reader: RwLock::new(Box::new(reader)),
126            path_base,
127            block_counter: AtomicUsize::default(),
128            block_cache: Default::default(),
129            file_handles: Default::default(),
130        })
131    }
132
133    fn get_block_raw(&self, block_index: usize) -> anyhow::Result<Cow<[u8]>> {
134        let _span = tracing::debug_span!("PackageCommonD2::get_block_raw", block_index).entered();
135
136        let bh = &self.blocks[block_index];
137        let mut data = vec![0u8; bh.size as usize];
138
139        if self.patch_id == bh.patch_id {
140            self.reader
141                .write()
142                .seek(SeekFrom::Start(bh.offset as u64))?;
143            self.reader.write().read_exact(&mut data)?;
144        } else {
145            match self.file_handles.write().entry(bh.patch_id as _) {
146                Entry::Occupied(mut f) => {
147                    let f = f.get_mut();
148                    f.seek(SeekFrom::Start(bh.offset as u64))?;
149                    f.read_exact(&mut data)?;
150                }
151                Entry::Vacant(e) => {
152                    let f = File::open(format!("{}_{}.pkg", self.path_base, bh.patch_id))
153                        .with_context(|| {
154                            format!(
155                                "Failed to open package file {}_{}.pkg",
156                                self.path_base, bh.patch_id
157                            )
158                        })?;
159
160                    let f = e.insert(f);
161                    f.seek(SeekFrom::Start(bh.offset as u64))?;
162                    f.read_exact(&mut data)?;
163                }
164            };
165        };
166
167        Ok(Cow::Owned(data))
168    }
169
170    /// Reads, decrypts and decompresses the specified block
171    fn read_block(&self, block_index: usize) -> anyhow::Result<Vec<u8>> {
172        let _span = tracing::debug_span!("PackageCommonD2::read_block", block_index).entered();
173
174        let bh = self.blocks[block_index].clone();
175
176        let mut block_data = self.get_block_raw(block_index)?.to_vec();
177
178        if (bh.flags & 0x2) != 0 {
179            let _espan =
180                tracing::debug_span!("PackageCommonD2::get_block_raw decrypt", block_index)
181                    .entered();
182            self.gcm
183                .write()
184                .decrypt_block_in_place(bh.flags, &bh.gcm_tag, &mut block_data)?;
185        };
186
187        let decompressed_data = if (bh.flags & 0x1) != 0 {
188            let _dspan =
189                tracing::debug_span!("PackageCommonD2::get_block_raw decompress", block_index)
190                    .entered();
191
192            let mut buffer = vec![0u8; BLOCK_SIZE];
193            let _decompressed_size = match self.version {
194                // Destiny 1
195                GameVersion::DestinyInternalAlpha
196                | GameVersion::DestinyFirstLookAlpha
197                | GameVersion::DestinyTheTakenKing
198                | GameVersion::DestinyRiseOfIron => oodle::decompress_3,
199
200                // Destiny 2 (Red War - Beyond Light)
201                GameVersion::Destiny2Beta
202                | GameVersion::Destiny2Forsaken
203                | GameVersion::Destiny2Shadowkeep => oodle::decompress_3,
204
205                // Destiny 2 (Beyond Light - Latest)
206                GameVersion::Destiny2BeyondLight
207                | GameVersion::Destiny2WitchQueen
208                | GameVersion::Destiny2Lightfall
209                | GameVersion::Destiny2TheFinalShape => oodle::decompress_9,
210            }(&block_data, &mut buffer)?;
211
212            buffer
213        } else {
214            block_data
215        };
216
217        Ok(decompressed_data)
218    }
219
220    pub fn get_block(&self, block_index: usize) -> anyhow::Result<Arc<Vec<u8>>> {
221        let _span = tracing::debug_span!("PackageCommonD2::get_block", block_index).entered();
222        let (_, b) = match self.block_cache.write().entry(block_index) {
223            Entry::Occupied(o) => o.get().clone(),
224            Entry::Vacant(v) => {
225                let block = self.read_block(*v.key())?;
226                let b = v
227                    .insert((self.block_counter.load(Ordering::Relaxed), Arc::new(block)))
228                    .clone();
229
230                self.block_counter.store(
231                    self.block_counter.load(Ordering::Relaxed) + 1,
232                    Ordering::Relaxed,
233                );
234
235                b
236            }
237        };
238
239        while self.block_cache.read().len() > BLOCK_CACHE_SIZE {
240            let bc = self.block_cache.read();
241            let (oldest, _) = bc
242                .iter()
243                .min_by(|(_, (at, _)), (_, (bt, _))| at.cmp(bt))
244                .unwrap();
245
246            let oldest = *oldest;
247            drop(bc);
248
249            self.block_cache.write().remove(&oldest);
250        }
251
252        Ok(b)
253    }
254}
255
256#[derive(Debug, Clone, bincode::Decode, bincode::Encode)]
257pub struct PackageNamedTagEntry {
258    pub hash: TagHash,
259    pub class_hash: u32,
260    pub name: String,
261}
262
263impl BinRead for PackageNamedTagEntry {
264    type Args<'a> = ();
265
266    fn read_options<R: Read + Seek>(
267        reader: &mut R,
268        endian: binrw::Endian,
269        _args: Self::Args<'_>,
270    ) -> binrw::BinResult<Self> {
271        let hash = reader.read_type(endian)?;
272        let class_hash = reader.read_type(endian)?;
273
274        let name_offset: u64 = reader.read_type(endian)?;
275        let pos_save = reader.stream_position()?;
276
277        reader.seek(SeekFrom::Start(pos_save - 8 + name_offset))?;
278        let name_cstring: NullString = reader.read_type(endian)?;
279        reader.seek(SeekFrom::Start(pos_save))?;
280
281        Ok(Self {
282            hash,
283            class_hash,
284            name: name_cstring.to_string(),
285        })
286    }
287}