tiger_pkg/manager/
path_cache.rs

1use std::{
2    collections::HashMap,
3    fs,
4    path::{Path, PathBuf},
5    time::SystemTime,
6};
7
8use itertools::Itertools;
9use rustc_hash::FxHashMap;
10use tracing::{info, warn};
11
12use super::PackageManager;
13use crate::{package::PackagePlatform, GameVersion, Version};
14
15impl PackageManager {
16    #[cfg(feature = "ignore_package_cache")]
17    pub(super) fn read_package_cache(silent: bool) -> Option<PathCache> {
18        if !silent {
19            info!("Not loading tag cache: ignore_package_cache is enabled")
20        }
21        None
22    }
23
24    #[cfg(feature = "ignore_package_cache")]
25    pub(super) fn write_package_cache(&self) -> anyhow::Result<()> {
26        Ok(())
27    }
28
29    #[cfg(not(feature = "ignore_package_cache"))]
30    pub(super) fn read_package_cache(silent: bool) -> Option<PathCache> {
31        let cache: Option<PathCache> = serde_json::from_str(
32            &std::fs::read_to_string(exe_relative_path("package_cache.json")).ok()?,
33        )
34        .ok();
35
36        if let Some(ref c) = cache {
37            if c.cache_version != PathCache::VERSION {
38                if !silent {
39                    warn!("Package cache is outdated, building a new one");
40                }
41                return None;
42            }
43        }
44
45        cache
46    }
47
48    #[cfg(not(feature = "ignore_package_cache"))]
49    pub(super) fn write_package_cache(&self) -> anyhow::Result<()> {
50        let mut cache = Self::read_package_cache(true).unwrap_or_default();
51
52        let timestamp = fs::metadata(&self.package_dir)
53            .ok()
54            .and_then(|m| {
55                Some(
56                    m.modified()
57                        .ok()?
58                        .duration_since(SystemTime::UNIX_EPOCH)
59                        .ok()?
60                        .as_secs(),
61                )
62            })
63            .unwrap_or(0);
64
65        let entry = cache
66            .versions
67            .entry(self.cache_key())
68            .or_insert_with(|| PathCacheEntry {
69                timestamp,
70                version: self.version,
71                platform: self.platform,
72                base_path: self.package_dir.clone(),
73                paths: Default::default(),
74            });
75
76        entry.timestamp = timestamp;
77        entry.base_path = self.package_dir.clone();
78        entry.paths.clear();
79
80        for (id, path) in &self.package_paths {
81            entry.paths.insert(*id, path.path.clone());
82        }
83
84        Ok(std::fs::write(
85            exe_relative_path("package_cache.json"),
86            serde_json::to_string_pretty(&cache)?,
87        )?)
88    }
89
90    #[must_use]
91    pub(super) fn validate_cache(
92        version: GameVersion,
93        platform: Option<PackagePlatform>,
94        packages_dir: &Path,
95    ) -> Result<FxHashMap<u16, String>, String> {
96        if let Some(cache) = Self::read_package_cache(false) {
97            info!("Loading package cache");
98            if let Some(p) = cache
99                .get_paths(version, platform, Some(packages_dir))
100                .ok()
101                .flatten()
102            {
103                let timestamp = fs::metadata(packages_dir)
104                    .ok()
105                    .and_then(|m| {
106                        Some(
107                            m.modified()
108                                .ok()?
109                                .duration_since(SystemTime::UNIX_EPOCH)
110                                .ok()?
111                                .as_secs(),
112                        )
113                    })
114                    .unwrap_or(0);
115
116                if p.timestamp < timestamp {
117                    Err("Package directory changed".to_string())
118                } else if p.base_path != packages_dir {
119                    Err("Package directory path changed".to_string())
120                } else {
121                    Ok(p.paths.clone())
122                }
123            } else {
124                Err(format!(
125                    "No cache entry found for version {version:?}, platform {platform:?}"
126                ))
127            }
128        } else {
129            Err("Failed to load package cache".to_string())
130        }
131    }
132
133    /// Generates a key unique to the game version + platform combination
134    /// eg. GameVersion::DestinyTheTakenKing and PackagePlatform::PS4 generates cache key "d1_ttk_ps4"
135    pub fn cache_key(&self) -> String {
136        format!("{}_{}", self.version.id(), self.platform)
137    }
138}
139
140#[derive(serde::Serialize, serde::Deserialize)]
141pub(crate) struct PathCache {
142    cache_version: usize,
143    versions: HashMap<String, PathCacheEntry>,
144}
145
146impl Default for PathCache {
147    fn default() -> Self {
148        Self {
149            cache_version: Self::VERSION,
150            versions: HashMap::new(),
151        }
152    }
153}
154
155impl PathCache {
156    pub const VERSION: usize = 4;
157
158    /// Gets path cache entry by version and platform
159    /// If `platform` is None, the first
160    /// This function will return an error if there are multiple entries for the same version when `platform` is None
161    pub fn get_paths(
162        &self,
163        version: GameVersion,
164        platform: Option<PackagePlatform>,
165        base_path: Option<&Path>,
166    ) -> anyhow::Result<Option<&PathCacheEntry>> {
167        if let Some(platform) = platform {
168            return Ok(self.versions.get(&format!("{}_{}", version.id(), platform)));
169        }
170
171        let mut matches = self
172            .versions
173            .iter()
174            .filter(|(_k, v)| {
175                v.version == version && platform.map(|p| v.platform == p).unwrap_or(true)
176            })
177            .map(|(_, v)| v)
178            .collect_vec();
179
180        if matches.len() > 1 {
181            if let Some(base_path) = base_path {
182                matches.retain(|c| c.base_path == base_path)
183            }
184        }
185
186        if matches.len() > 1 {
187            anyhow::bail!(
188                "There is more than one cache entry for version '{}', but no platform was given",
189                version.name()
190            );
191        }
192
193        Ok(matches.first().map(|v| *v))
194    }
195}
196
197#[derive(serde::Serialize, serde::Deserialize)]
198pub(crate) struct PathCacheEntry {
199    /// Timestamp of the packages directory
200    timestamp: u64,
201    version: GameVersion,
202    platform: PackagePlatform,
203    base_path: PathBuf,
204    paths: FxHashMap<u16, String>,
205}
206
207pub fn exe_directory() -> PathBuf {
208    std::env::current_exe()
209        .unwrap()
210        .parent()
211        .unwrap()
212        .to_path_buf()
213}
214
215pub fn exe_relative_path(path: &str) -> PathBuf {
216    exe_directory().join(path)
217}