Skip to main content

thunderstore_engine/profile/
cache.rs

1//! Extract packages into the per-game package cache.
2//!
3//! Mirrors r2modman's `<game>/cache/<full_name>/<version>/` layout. This cache is
4//! the install source and the restore source for re-enabling `State`-tracked
5//! mods, so it must persist across installs.
6
7#![deny(missing_docs)]
8
9#[cfg(feature = "io")]
10use crate::error::{Error, Result};
11#[cfg(feature = "io")]
12use crate::extract::{ExtractLimits, extract_zip_reporting, should_extract};
13use std::path::{Path, PathBuf};
14
15/// The cache directory for one package version:
16/// `<base>/<game>/cache/<full_name>/<version>`.
17pub fn package_cache_dir(base: &Path, game: &str, full_name: &str, version: &str) -> PathBuf {
18  base.join(game).join("cache").join(full_name).join(version)
19}
20
21/// Extracts `archive` into the package cache directory if that version is not
22/// already present, and returns the directory. Uses [`should_extract`] so an
23/// unchanged version is not re-extracted.
24#[cfg(feature = "io")]
25pub fn ensure_extracted(
26  base: &Path,
27  game: &str,
28  full_name: &str,
29  version: &str,
30  archive: &Path,
31) -> Result<PathBuf> {
32  ensure_extracted_reporting(base, game, full_name, version, archive, &mut |_, _| {})
33}
34
35/// [`ensure_extracted`], passing `on_progress` to the extraction. Never called
36/// when the version is already extracted.
37#[cfg(feature = "io")]
38pub(crate) fn ensure_extracted_reporting(
39  base: &Path,
40  game: &str,
41  full_name: &str,
42  version: &str,
43  archive: &Path,
44  on_progress: &mut dyn FnMut(u64, u64),
45) -> Result<PathBuf> {
46  let dir = package_cache_dir(base, game, full_name, version);
47
48  if should_extract(&dir, version)? {
49    extract_zip_reporting(archive, &dir, &ExtractLimits::default(), on_progress)?;
50  }
51
52  if !dir.exists() {
53    return Err(Error::Profile(format!(
54      "package cache {} missing after extraction",
55      dir.display()
56    )));
57  }
58
59  Ok(dir)
60}
61
62#[cfg(all(test, feature = "io"))]
63mod tests {
64  use super::*;
65  use std::io::{Cursor, Write};
66  use tempfile::tempdir;
67  use zip::write::{FileOptions, ZipWriter};
68
69  fn build_zip() -> Vec<u8> {
70    let mut zip = ZipWriter::new(Cursor::new(Vec::new()));
71    let opts: FileOptions<'_, ()> =
72      FileOptions::default().compression_method(zip::CompressionMethod::Stored);
73
74    zip.start_file("manifest.json", opts).unwrap();
75    zip.write_all(br#"{"version_number":"1.0.0"}"#).unwrap();
76    zip.start_file("plugins/Mod.dll", opts).unwrap();
77    zip.write_all(b"dll").unwrap();
78
79    zip.finish().unwrap().into_inner()
80  }
81
82  #[test]
83  fn ensure_extracted_places_and_is_idempotent() {
84    let dir = tempdir().unwrap();
85    let base = dir.path();
86
87    let archive = base.join("Author-Mod-1.0.0.zip");
88    std::fs::write(&archive, build_zip()).unwrap();
89
90    let out = ensure_extracted(base, "valheim", "Author-Mod", "1.0.0", &archive).unwrap();
91
92    assert_eq!(
93      out,
94      package_cache_dir(base, "valheim", "Author-Mod", "1.0.0")
95    );
96    assert!(out.join("plugins/Mod.dll").exists());
97
98    // Second call is a no-op (same version present) and still returns the dir.
99    let again = ensure_extracted(base, "valheim", "Author-Mod", "1.0.0", &archive).unwrap();
100
101    assert!(again.join("plugins/Mod.dll").exists());
102  }
103}