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
//! Units of data in a [`CacheIndex`](crate::index::CacheIndex).
//!
//! Each [`Archive`] conatins files, which contain the actual data that can be parsed with
//! the appropriate deserializer in [`definitions`](../../rs3cache/definitions/index.html).
//!
//! None of the structs in this module can be constructed directly.
//! Instead, construct a [`CacheIndex`](crate::index::CacheIndex)
//! and use its [`IntoIterator`] implementation or its [`archive`](crate::index::CacheIndex::archive())
//! method instead.

use std::collections::BTreeMap;

use bytes::{Buf, Bytes};
#[cfg(feature = "pyo3")]
use pyo3::{exceptions::PyKeyError, prelude::*, types::PyBytes};
#[cfg(feature = "dat")]
use {
    crate::buf::BufExtra,
    crate::error::{self, CacheResult},
    ::error::Context,
};

use crate::meta::Metadata;
/// A collection of files.
#[cfg_attr(feature = "pyo3", pyclass(frozen))]
#[derive(Clone, Default)]
pub struct Archive {
    pub(crate) index_id: u32,
    pub(crate) archive_id: u32,
    pub(crate) files: BTreeMap<u32, Bytes>,
    #[cfg(feature = "dat")]
    pub(crate) files_named: BTreeMap<i32, Bytes>,
}

impl std::fmt::Debug for Archive {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Archive")
            .field("index_id", &self.index_id)
            .field("archive_id", &self.archive_id)
            .field("files", &self.files.keys().collect::<Vec<_>>())
            .finish()
    }
}

impl Archive {
    /// The [`index id`](crate::indextype::IndexType) of `self`.
    pub const fn index_id(&self) -> u32 {
        self.index_id
    }

    /// The archive id of `self`.
    pub const fn archive_id(&self) -> u32 {
        self.archive_id
    }

    #[cfg(any(feature = "sqlite", feature = "dat2"))]
    pub(crate) fn deserialize(metadata: &Metadata, data: Bytes) -> Archive {
        let index_id = metadata.index_id();
        let archive_id = metadata.archive_id();
        let files = match metadata.child_count() {
            0 => unreachable!(),
            1 => {
                let mut files = BTreeMap::new();
                files.insert(metadata.child_indices()[0], data);
                files
            }

            #[cfg(feature = "sqlite")]
            child_count => {
                use rs3cache_utils::adapters::Pairwisor;

                assert_eq!(data[0], 1);

                let mut offset_data = data.slice(1..((child_count + 1) * 4 + 1) as usize);

                let offsets = std::iter::repeat_with(|| offset_data.get_i32() as usize)
                    .take(child_count as usize + 1)
                    .pairwise();

                itertools::izip!(metadata.child_indices(), offsets)
                    .map(|(i, (start, end))| (*i, data.slice(start..end)))
                    .collect::<BTreeMap<_, _>>()
            }

            #[cfg(feature = "dat2")]
            child_count => {
                use rs3cache_utils::adapters::Accumulator;
                let mut data = data;

                let n_chunks = *data.last().unwrap() as usize;

                let offset_start = data.len().checked_sub(4 * n_chunks * (child_count as usize) + 1).unwrap();
                let mut offset_data = data.split_off(offset_start);

                let offsets = std::iter::repeat_with(|| offset_data.get_i32())
                    .take(child_count as usize)
                    .accumulate(|x, y| x + y);

                itertools::izip!(metadata.child_indices(), offsets)
                    .map(|(i, n)| (*i, data.split_to(n.try_into().unwrap())))
                    .collect::<BTreeMap<_, _>>()
            }
        };

        Archive { index_id, archive_id, files }
    }

    /// Gets a File.
    pub fn file(&self, file_id: &u32) -> Option<Bytes> {
        self.files.get(file_id).cloned()
    }

    /// Take the files. Consumes the [`Archive`].
    pub fn take_files(self) -> BTreeMap<u32, Bytes> {
        self.files
    }

    /// The quantity of files currently in the archive.
    pub fn file_count(&self) -> usize {
        self.files.len()
    }

    #[cfg(feature = "dat")]
    pub(crate) fn deserialize_jag(metadata: &Metadata, mut buffer: Bytes) -> CacheResult<Archive> {
        use std::io::Read;

        #[derive(Debug)]
        struct JagHeader {
            filename: i32,
            decompressed_len: u32,
            compressed_len: u32,
        }

        assert_eq!(metadata.index_id(), 0, "called deserialize_jag on data not from index 0");

        let decompressed_len = buffer.try_get_uint(3).context(error::Read { what: "metadata" })?;
        let compressed_len = buffer.try_get_uint(3).context(error::Read { what: "metadata" })?;

        let extracted = if decompressed_len != compressed_len {
            let mut compressed = bytes::BytesMut::from(b"BZh1".as_slice());
            compressed.extend(buffer.split_to(compressed_len as usize));

            let mut decoded = Vec::with_capacity(decompressed_len as usize);

            let mut decoder = bzip2_rs::DecoderReader::new(&compressed[..]);

            decoder.read_to_end(&mut decoded).unwrap();
            buffer = Bytes::from(decoded);
            true
        } else {
            false
        };

        let files_length = buffer.get_u16();

        let mut headers = buffer.split_to((files_length * 10).try_into().unwrap());
        let mut archive = Archive {
            index_id: metadata.index_id(),
            archive_id: metadata.archive_id(),
            ..Default::default()
        };

        for i in 0..files_length {
            let header = JagHeader {
                filename: headers.try_get_i32().context(error::Read { what: "metadata" })?,
                decompressed_len: headers.try_get_uint(3).context(error::Read { what: "metadata" })? as u32,
                compressed_len: headers.try_get_uint(3).context(error::Read { what: "metadata" })? as u32,
            };

            let decompressed = if extracted {
                buffer.split_to(header.decompressed_len as usize)
            } else {
                let mut compressed = bytes::BytesMut::from(b"BZh1".as_slice());
                compressed.extend(buffer.split_to(header.compressed_len as usize));

                use pyo3::prelude::*;
                pyo3::prepare_freethreaded_python();

                let res = Python::with_gil(|py| {
                    let zlib = PyModule::import(py, "bz2")?;
                    let decompress = zlib.getattr("decompress")?;
                    let bytes = PyBytes::new(py, &compressed);
                    let value = decompress.call1((bytes,))?;
                    value.extract::<Vec<u8>>()
                })
                .unwrap();
                Bytes::from(res)
            };
            archive.files.insert(i as u32, decompressed.clone());
            archive.files_named.insert(header.filename, decompressed);
        }
        assert_eq!(buffer.remaining(), 0);

        Ok(archive)
    }

    #[cfg(feature = "dat")]
    pub fn file_named(&self, name: impl AsRef<str>) -> CacheResult<Bytes> {
        let name = name.as_ref();

        let hash = crate::hash::hash_archive(name);

        self.files_named
            .get(&hash)
            .with_context(|| crate::index::FileMissingNamed {
                index_id: self.index_id,
                archive_id: self.archive_id,
                name: name.into(),
            })
            .context(error::Integrity)
            .cloned()
    }

    /// Take the files. Consumes the [`Archive`].
    #[cfg(feature = "dat")]
    pub fn take_files_named(self) -> BTreeMap<i32, Bytes> {
        self.files_named
    }
}

#[cfg(feature = "pyo3")]
#[pymethods]
impl Archive {
    #[pyo3(name = "file")]
    fn py_file<'p>(&self, py: Python<'p>, file_id: u32) -> PyResult<&'p PyBytes> {
        if let Some(file) = self.files.get(&file_id) {
            Ok(PyBytes::new(py, file))
        } else {
            Err(PyKeyError::new_err(format!("File {file_id} is not present.")))
        }
    }

    #[pyo3(name = "files")]
    fn py_files<'p>(&self, py: Python<'p>) -> PyResult<BTreeMap<u32, &'p PyBytes>> {
        Ok(self.files.iter().map(|(&id, file)| (id, PyBytes::new(py, file))).collect())
    }

    fn __repr__(&self) -> PyResult<String> {
        Ok(format!("Archive({}, {})", self.index_id(), self.archive_id()))
    }

    fn __str__(&self) -> PyResult<String> {
        Ok(format!("Archive({}, {})", self.index_id(), self.archive_id()))
    }
}