rpkg_rs/resource/
resource_partition.rs

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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
use crate::resource::partition_manager::PartitionState;
use crate::resource::pdefs::PartitionInfo;
use crate::resource::resource_info::ResourceInfo;
use crate::{utils, GlacierResource, GlacierResourceError, WoaVersion};
use lazy_regex::regex::Regex;
use std::cmp::Ordering;
use std::fmt::Debug;
use std::{collections::HashMap, path::Path};
use std::{fmt, io};
use thiserror::Error;

use crate::resource::resource_package::{ResourcePackage, ResourcePackageError};

use super::runtime_resource_id::RuntimeResourceID;

#[derive(Debug, Error)]
pub enum ResourcePartitionError {
    #[error("Failed to open file: {0}")]
    IoError(#[from] io::Error),

    #[error("Error while reading ResourcePackage({1}): {0}")]
    ReadResourcePackageError(ResourcePackageError, String),

    #[error("Failed to parse patch index as u16: {0}")]
    ParsePatchIndexError(#[from] std::num::ParseIntError),

    #[error("Base package not found: {0}")]
    BasePackageNotFound(String),

    #[error("Failed to read package: {0}")]
    ReadPackageError(String),

    #[error("No partition mounted")]
    NotMounted,

    #[error("Resource not available")]
    ResourceNotAvailable,

    #[error("Interal resource error: {0}")]
    ResourceError(#[from] GlacierResourceError),
}

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum PatchId {
    Base,
    Patch(usize),
}

impl PatchId {
    pub fn is_base(&self) -> bool {
        match self {
            PatchId::Base => true,
            PatchId::Patch(_) => false,
        }
    }

    pub fn is_patch(&self) -> bool {
        !self.is_base()
    }
}

impl Ord for PatchId {
    fn cmp(&self, other: &Self) -> Ordering {
        match (self, other) {
            (PatchId::Base, PatchId::Base) => Ordering::Equal,
            (PatchId::Base, PatchId::Patch(_)) => Ordering::Less,
            (PatchId::Patch(_), PatchId::Base) => Ordering::Greater,
            (PatchId::Patch(a), PatchId::Patch(b)) => a.cmp(b),
        }
    }
}

impl PartialOrd for PatchId {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

pub struct ResourcePartition {
    info: PartitionInfo,
    pub packages: HashMap<PatchId, ResourcePackage>,
    pub(crate) resources: HashMap<RuntimeResourceID, PatchId>,
}

impl ResourcePartition {
    pub fn new(info: PartitionInfo) -> Self {
        Self {
            info,
            packages: Default::default(),
            resources: Default::default(),
        }
    }

    /// search through the package_dir to figure out which patch indices are there.
    /// We have to use this instead of using the patchlevel inside the PartitionInfo.
    fn read_patch_indices(
        &self,
        package_dir: &Path,
    ) -> Result<Vec<PatchId>, ResourcePartitionError> {
        let mut patch_indices = vec![];

        let filename = self.info.filename(PatchId::Base);
        if !package_dir.join(&filename).exists() {
            return Err(ResourcePartitionError::BasePackageNotFound(filename));
        }

        let regex_str = format!(r"^(?:{}patch([0-9]+).rpkg)$", self.info.id);
        let patch_package_re = Regex::new(regex_str.as_str()).unwrap();

        for file_name in utils::read_file_names(package_dir)
            .iter()
            .flat_map(|file_name| file_name.to_str())
        {
            if let Some(cap) = patch_package_re.captures(file_name) {
                let patch_level = cap[1].parse::<usize>()?;
                if patch_level <= self.info.patch_level {
                    patch_indices.push(PatchId::Patch(patch_level));
                }
            }
        }

        patch_indices.sort();
        Ok(patch_indices)
    }

    /// Mounts resource packages in the partition.
    ///
    /// This function attempts to mount all necessary resource packages into the current partition.
    /// If successful, the resources will be available for use within the partition.
    /// This function will fail silently when this package can't be found inside runtime directory
    pub fn mount_resource_packages_in_partition(
        &mut self,
        runtime_path: &Path,
    ) -> Result<(), ResourcePartitionError> {
        self.mount_resource_packages_in_partition_with_callback(runtime_path, |_| {})
    }

    /// Mounts resource packages in the partition with a callback.
    ///
    /// This function attempts to mount all necessary resource packages into the current partition.
    /// If successful, the resources will be available for use within the partition.
    /// This function will fail silently when this package can't be found inside runtime directory.
    pub fn mount_resource_packages_in_partition_with_callback<F>(
        &mut self,
        runtime_path: &Path,
        mut progress_callback: F,
    ) -> Result<(), ResourcePartitionError>
    where
        F: FnMut(&PartitionState),
    {
        let mut state = PartitionState {
            installing: true,
            mounted: false,
            install_progress: 0.0,
        };

        //The process can silently fail here. You are able to detect this using a callback.
        //This behaviour was chosen because the game is able to refer to non-installed partitions in its packagedefs file.
        let patch_idx_result = self.read_patch_indices(runtime_path);
        if patch_idx_result.is_err() {
            state.installing = false;
            progress_callback(&state);
            return Ok(());
        }

        let patch_indices = patch_idx_result?;

        let base_package_path = runtime_path.join(self.info.filename(PatchId::Base));
        self.mount_package(base_package_path.as_path(), PatchId::Base)?;

        for (index, patch_id) in patch_indices.clone().into_iter().enumerate() {
            let patch_package_path = runtime_path.join(self.info.filename(patch_id));
            self.mount_package(patch_package_path.as_path(), patch_id)?;

            state.install_progress = index as f32 / patch_indices.len() as f32;
            progress_callback(&state);
        }
        state.install_progress = 1.0;
        state.installing = false;
        state.mounted = true;
        progress_callback(&state);

        Ok(())
    }

    fn mount_package(
        &mut self,
        package_path: &Path,
        patch_index: PatchId,
    ) -> Result<(), ResourcePartitionError> {
        let rpkg = ResourcePackage::from_file(package_path).map_err(|e| {
            ResourcePartitionError::ReadResourcePackageError(
                e,
                package_path
                    .file_name()
                    .unwrap_or_default()
                    .to_string_lossy()
                    .into_owned(),
            )
        })?;

        //remove the deletions if there are any
        for deletion in rpkg.unneeded_resource_ids() {
            if self.resources.contains_key(deletion) {
                self.resources.remove_entry(deletion);
            }
        }

        for rrid in rpkg.resources.keys() {
            self.resources.insert(*rrid, patch_index);
        }

        self.packages.insert(patch_index, rpkg);
        Ok(())
    }

    pub fn contains(&self, rrid: &RuntimeResourceID) -> bool {
        self.resources.contains_key(rrid)
    }

    pub fn num_patches(&self) -> usize {
        self.packages.len().saturating_sub(1)
    }

    pub fn latest_resources(&self) -> Vec<(&ResourceInfo, PatchId)> {
        self.resources
            .iter()
            .flat_map(|(rrid, idx)| {
                if let Ok(info) = self.resource_info_from(rrid, *idx) {
                    Some((info, *idx))
                } else {
                    None
                }
            })
            .collect()
    }

    pub fn latest_resources_of_type(&self, resource_type: &str) -> Vec<(&ResourceInfo, PatchId)>{
        self.resources
            .iter()
            .flat_map(|(rrid, idx)| {
                if let Ok(info) = self.resource_info_from(rrid, *idx) {
                    Some((info, *idx))
                } else {
                    None
                }
            }).filter(|(resource, _)| resource.data_type() == resource_type)
            .collect()
    }
    
    pub fn latest_resources_of_glacier_type<G: GlacierResource>(&self) -> Vec<(&ResourceInfo, PatchId)>{
        let resource_type: String = String::from_utf8_lossy(&G::resource_type()).into_owned();
        self.latest_resources_of_type(resource_type.as_str())
    }
    
    pub fn read_resource(
        &self,
        rrid: &RuntimeResourceID,
    ) -> Result<Vec<u8>, ResourcePartitionError> {
        let package_index = *self
            .resources
            .get(rrid)
            .ok_or(ResourcePartitionError::ResourceNotAvailable)?;

        let rpkg = self
            .packages
            .get(&package_index)
            .ok_or(ResourcePartitionError::NotMounted)?;

        rpkg.read_resource(rrid).map_err(|e| {
            ResourcePartitionError::ReadResourcePackageError(e, self.info.filename(package_index))
        })
    }

    pub fn read_glacier_resource<T>(
        &self,
        woa_version: WoaVersion,
        rrid: &RuntimeResourceID,
    ) -> Result<T::Output, ResourcePartitionError>
    where
        T: GlacierResource,
    {
        let package_index = *self
            .resources
            .get(rrid)
            .ok_or(ResourcePartitionError::ResourceNotAvailable)?;

        let rpkg = self
            .packages
            .get(&package_index)
            .ok_or(ResourcePartitionError::NotMounted)?;

        let bytes = rpkg.read_resource(rrid).map_err(|e| {
            ResourcePartitionError::ReadResourcePackageError(e, self.info.filename(package_index))
        })?;

        T::process_data(woa_version, bytes).map_err(ResourcePartitionError::ResourceError)
    }
    
    pub fn read_resource_from(
        &self,
        rrid: &RuntimeResourceID,
        patch_id: PatchId,
    ) -> Result<Vec<u8>, ResourcePartitionError> {
        let rpkg = self
            .packages
            .get(&patch_id)
            .ok_or(ResourcePartitionError::NotMounted)?;

        rpkg.read_resource(rrid).map_err(|e| {
            ResourcePartitionError::ReadResourcePackageError(e, self.info.filename(patch_id))
        })
    }

    pub fn get_resource_info(
        &self,
        rrid: &RuntimeResourceID,
    ) -> Result<&ResourceInfo, ResourcePartitionError> {
        let package_index = self
            .resources
            .get(rrid)
            .ok_or(ResourcePartitionError::ResourceNotAvailable)?;

        let rpkg = self
            .packages
            .get(package_index)
            .ok_or(ResourcePartitionError::NotMounted)?;

        rpkg.resources
            .get(rrid)
            .ok_or(ResourcePartitionError::ResourceNotAvailable)
    }

    pub fn resource_info_from(
        &self,
        rrid: &RuntimeResourceID,
        patch_id: PatchId,
    ) -> Result<&ResourceInfo, ResourcePartitionError> {
        let rpkg = self
            .packages
            .get(&patch_id)
            .ok_or(ResourcePartitionError::NotMounted)?;

        rpkg.resources
            .get(rrid)
            .ok_or(ResourcePartitionError::ResourceNotAvailable)
    }

    pub fn partition_info(&self) -> &PartitionInfo {
        &self.info
    }

    pub fn resource_patch_indices(&self, rrid: &RuntimeResourceID) -> Vec<PatchId> {
        self.packages
            .iter()
            .filter(|(_, package)| package.resources.contains_key(rrid))
            .map(|(id, _)| *id)
            .collect::<Vec<PatchId>>()
    }

    pub fn resource_removal_indices(&self, rrid: &RuntimeResourceID) -> Vec<PatchId> {
        self.packages
            .iter()
            .filter(|(_, package)| package.has_unneeded_resource(rrid))
            .map(|(id, _)| *id)
            .collect::<Vec<PatchId>>()
    }
}

impl Debug for ResourcePartition {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let total = self
            .packages
            .values()
            .map(|v| v.resources.len())
            .sum::<usize>();

        write!(
            f,
            "{{index: {}, name: {}, edge_resources: {}, total_resources: {} }}",
            self.info.filename(PatchId::Base),
            self.info.name.clone().unwrap_or_default(),
            self.resources.len(),
            total
        )?;

        Ok(())
    }
}