1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
use std::{
    fmt::{Display, Formatter},
    io::{Read, Seek},
    str::FromStr,
    sync::Arc,
};

use anyhow::{anyhow, ensure};
use binrw::{BinRead, Endian};
use clap::ValueEnum;

use crate::{
    d1_internal_alpha::PackageD1InternalAlpha, d1_legacy::PackageD1Legacy,
    d1_roi::PackageD1RiseOfIron, d2_beta::PackageD2Beta, d2_beyondlight::PackageD2BeyondLight,
    d2_shared::PackageNamedTagEntry, PackageD2PreBL, TagHash,
};

pub const BLOCK_CACHE_SIZE: usize = 128;

pub trait ReadSeek: Read + Seek {}
impl<R: Read + Seek> ReadSeek for R {}

#[derive(Clone, Debug)]
pub struct UEntryHeader {
    pub reference: u32,
    pub file_type: u8,
    pub file_subtype: u8,
    pub starting_block: u32,
    pub starting_block_offset: u32,
    pub file_size: u32,
}

#[derive(Clone)]
pub struct UHashTableEntry {
    pub hash64: u64,
    pub hash32: TagHash,
    pub reference: TagHash,
}

#[derive(BinRead, Debug, Copy, Clone)]
#[br(repr = u16)]
pub enum PackageLanguage {
    None = 0,
    English = 1,
    French = 2,
    Italian = 3,
    German = 4,
    Spanish = 5,
    Japanese = 6,
    Portuguese = 7,
}

impl PackageLanguage {
    pub fn english_or_none(&self) -> bool {
        matches!(self, Self::None | Self::English)
    }
}

#[derive(
    serde::Serialize, serde::Deserialize, clap::ValueEnum, PartialEq, PartialOrd, Debug, Clone, Copy,
)]
pub enum GameVersion {
    /// X360 december 2013 internal alpha version of Destiny
    #[value(name = "d1_devalpha")]
    DestinyInternalAlpha = 1_0500,

    /// PS3/X360 version of Destiny (The Taken King)
    #[value(name = "d1_ttk")]
    DestinyTheTakenKing = 1_2000,

    /// The latest version of Destiny (Rise of Iron)
    #[value(name = "d1_roi")]
    DestinyRiseOfIron = 1_2400,

    /// Destiny 2 Beta
    #[value(name = "d2_beta")]
    Destiny2Beta = 2_1000,

    #[value(name = "d2_fs")]
    Destiny2Forsaken = 2_2000,

    /// The last version of Destiny before Beyond Light (Shadowkeep/Season of Arrivals)
    #[value(name = "d2_sk")]
    Destiny2Shadowkeep = 2_2600,

    /// Destiny 2 (Beyond Light/Season of the Lost)
    #[value(name = "d2_bl")]
    Destiny2BeyondLight,

    /// Destiny 2 (Witch Queen/Season of the Seraph)
    #[value(name = "d2_wq")]
    Destiny2WitchQueen = 4000,

    /// Destiny 2 (Lightfall)
    #[value(name = "d2_lf")]
    Destiny2Lightfall = 7000,

    #[value(name = "d2_tfs")]
    Destiny2TheFinalShape = 8000,
}

impl GameVersion {
    pub fn open(&self, path: &str) -> anyhow::Result<Arc<dyn Package>> {
        Ok(match self {
            GameVersion::DestinyInternalAlpha => Arc::new(PackageD1InternalAlpha::open(path)?),
            GameVersion::DestinyTheTakenKing => Arc::new(PackageD1Legacy::open(path)?),
            GameVersion::DestinyRiseOfIron => Arc::new(PackageD1RiseOfIron::open(path)?),
            GameVersion::Destiny2Beta => Arc::new(PackageD2Beta::open(path)?),

            GameVersion::Destiny2Forsaken | GameVersion::Destiny2Shadowkeep => {
                Arc::new(PackageD2PreBL::open(path)?)
            }

            GameVersion::Destiny2BeyondLight
            | GameVersion::Destiny2WitchQueen
            | GameVersion::Destiny2Lightfall
            | GameVersion::Destiny2TheFinalShape => {
                Arc::new(PackageD2BeyondLight::open(path, *self)?)
            }
        })
    }

    pub fn endian(&self) -> Endian {
        match self {
            GameVersion::DestinyInternalAlpha | GameVersion::DestinyTheTakenKing => Endian::Big,
            _ => Endian::Little,
        }
    }

    pub fn is_d1(&self) -> bool {
        matches!(
            self,
            GameVersion::DestinyInternalAlpha
                | GameVersion::DestinyTheTakenKing
                | GameVersion::DestinyRiseOfIron
        )
    }

    pub fn is_d2(&self) -> bool {
        matches!(
            self,
            GameVersion::Destiny2Beta
                | GameVersion::Destiny2Shadowkeep
                | GameVersion::Destiny2BeyondLight
                | GameVersion::Destiny2WitchQueen
                | GameVersion::Destiny2Lightfall
        )
    }

    pub fn id(&self) -> String {
        self.to_possible_value()
            .expect("Package version is missing an id/commandline value")
            .get_name()
            .to_string()
    }

    pub fn name(&self) -> &'static str {
        match self {
            GameVersion::DestinyInternalAlpha => "Destiny X360 Internal Alpha",
            GameVersion::DestinyTheTakenKing => "Destiny: The Taken King",
            GameVersion::DestinyRiseOfIron => "Destiny: Rise of Iron",
            GameVersion::Destiny2Beta => "Destiny 2: Beta",
            GameVersion::Destiny2Forsaken => "Destiny 2: Forsaken",
            GameVersion::Destiny2Shadowkeep => "Destiny 2: Shadowkeep",
            GameVersion::Destiny2BeyondLight => "Destiny 2: Beyond Light",
            GameVersion::Destiny2WitchQueen => "Destiny 2: Witch Queen",
            GameVersion::Destiny2Lightfall => "Destiny 2: Lightfall",
            GameVersion::Destiny2TheFinalShape => "Destiny 2: The Final Shape",
        }
    }
}

// TODO(cohae): Package language
pub trait Package: Send + Sync {
    fn endianness(&self) -> binrw::Endian;

    fn pkg_id(&self) -> u16;
    fn patch_id(&self) -> u16;

    /// Every hash64 in this package.
    /// Does not apply to Destiny 1
    fn hash64_table(&self) -> Vec<UHashTableEntry>;

    fn named_tags(&self) -> Vec<PackageNamedTagEntry>;

    fn entries(&self) -> &[UEntryHeader];

    fn entry(&self, index: usize) -> Option<UEntryHeader>;

    fn language(&self) -> PackageLanguage {
        PackageLanguage::None
    }

    /// Gets/reads a specific block from the file.
    /// It's recommended that the implementation caches blocks to prevent re-reads
    fn get_block(&self, index: usize) -> anyhow::Result<Arc<Vec<u8>>>;

    /// Reads the entire specified entry's data
    fn read_entry(&self, index: usize) -> anyhow::Result<Vec<u8>> {
        let _span = tracing::debug_span!("Package::read_entry").entered();
        let entry = self
            .entry(index)
            .ok_or(anyhow!("Entry index is out of range"))?;

        let mut buffer = Vec::with_capacity(entry.file_size as usize);
        let mut current_offset = 0usize;
        let mut current_block = entry.starting_block;

        while current_offset < entry.file_size as usize {
            let remaining_bytes = entry.file_size as usize - current_offset;
            let block_data = self.get_block(current_block as usize)?;

            if current_block == entry.starting_block {
                let block_start_offset = entry.starting_block_offset as usize;
                let block_remaining = block_data.len() - block_start_offset;
                let copy_size = if block_remaining < remaining_bytes {
                    block_remaining
                } else {
                    remaining_bytes
                };

                buffer.extend_from_slice(
                    &block_data[block_start_offset..block_start_offset + copy_size],
                );

                current_offset += copy_size;
            } else if remaining_bytes < block_data.len() {
                // If the block has more bytes than we need, it means we're on the last block
                buffer.extend_from_slice(&block_data[..remaining_bytes]);
                current_offset += remaining_bytes;
            } else {
                // If the previous 2 conditions failed, it means this whole block belongs to the file
                buffer.extend_from_slice(&block_data[..]);
                current_offset += block_data.len();
            }

            current_block += 1;
        }

        Ok(buffer)
    }

    /// Reads the entire specified entry's data
    /// Tag needs to be in this package
    fn read_tag(&self, tag: TagHash) -> anyhow::Result<Vec<u8>> {
        ensure!(tag.pkg_id() == self.pkg_id());
        self.read_entry(tag.entry_index() as _)
    }

    // /// Reads the entire specified entry's data
    // /// Hash needs to be in this package
    // fn read_hash64(&self, hash: u64) -> anyhow::Result<Vec<u8>> {
    //     let tag = self.translate_hash64(hash).ok_or_else(|| {
    //         anyhow::anyhow!(
    //             "Could not find hash 0x{hash:016x} in this package ({:04x})",
    //             self.pkg_id()
    //         )
    //     })?;
    //     ensure!(tag.pkg_id() == self.pkg_id());
    //     self.read_entry(tag.entry_index() as _)
    // }

    fn get_all_by_reference(&self, reference: u32) -> Vec<(usize, UEntryHeader)> {
        self.entries()
            .iter()
            .enumerate()
            .filter(|(_, e)| e.reference == reference)
            .map(|(i, e)| (i, e.clone()))
            .collect()
    }

    fn get_all_by_type(&self, etype: u8, esubtype: Option<u8>) -> Vec<(usize, UEntryHeader)> {
        self.entries()
            .iter()
            .enumerate()
            .filter(|(_, e)| {
                e.file_type == etype && esubtype.map(|t| t == e.file_subtype).unwrap_or(true)
            })
            .map(|(i, e)| (i, e.clone()))
            .collect()
    }
}

/// ! Currently only works for Pre-BL Destiny 2
pub fn classify_file_prebl(ftype: u8, fsubtype: u8) -> String {
    match (ftype, fsubtype) {
        // WWise audio bank
        (26, 5) => "bnk".to_string(),
        // WWise audio stream
        (26, 6) => "wem".to_string(),
        // Havok file
        (26, 7) => "hkx".to_string(),
        // CriWare USM video
        (27, _) => "usm".to_string(),
        (32, 1) => "texture.header".to_string(),
        (32, 2) => "texture_cube.header".to_string(),
        (32, 4) => "vertex.header".to_string(),
        (32, 6) => "index.header".to_string(),
        (40, 4) => "vertex.data".to_string(),
        (40, 6) => "index.data".to_string(),
        (48, 1) => "texture.data".to_string(),
        (48, 2) => "texture_cube.data".to_string(),
        // DXBC data
        (41, shader_type) => {
            let ty = match shader_type {
                0 => "fragment".to_string(),
                1 => "vertex".to_string(),
                6 => "compute".to_string(),
                u => format!("unk{u}"),
            };

            format!("cso.{ty}")
        }
        (8, _) => "8080".to_string(),
        _ => "bin".to_string(),
    }
}

#[derive(
    serde::Serialize, serde::Deserialize, clap::ValueEnum, PartialEq, Eq, Debug, Clone, Copy,
)]
pub enum PackagePlatform {
    PS3,
    PS4,
    X360,
    XboxOne,
    Windows,
}

impl PackagePlatform {
    pub fn endianness(&self) -> Endian {
        match self {
            Self::PS3 | Self::X360 => Endian::Big,
            Self::XboxOne | Self::PS4 | Self::Windows => Endian::Little,
        }
    }
}

impl FromStr for PackagePlatform {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s {
            "ps3" => Self::PS3,
            "ps4" => Self::PS4,
            "360" => Self::X360,
            "w64" => Self::Windows,
            "xboxone" => Self::XboxOne,
            s => return Err(anyhow!("Invalid platform '{s}'")),
        })
    }
}

impl Display for PackagePlatform {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            PackagePlatform::PS3 => f.write_str("ps3"),
            PackagePlatform::PS4 => f.write_str("ps4"),
            PackagePlatform::X360 => f.write_str("360"),
            PackagePlatform::XboxOne => f.write_str("xboxone"),
            PackagePlatform::Windows => f.write_str("w64"),
        }
    }
}