aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src-tauri/src/core/modpack/extractor.rs
diff options
context:
space:
mode:
author苏向夜 <46275354+fu050409@users.noreply.github.com>2026-04-02 11:27:09 +0800
committerGitHub <noreply@github.com>2026-04-02 11:27:09 +0800
commite5f94912615e69c32c353fd6a63790e9b16685e4 (patch)
treedc1a8a0f757e4b4829bbc08c401ac9016f9b52b3 /src-tauri/src/core/modpack/extractor.rs
parent0f44b957f0e4bd76146c95ef5c8b75cd61b457c1 (diff)
downloadDropOut-e5f94912615e69c32c353fd6a63790e9b16685e4.tar.gz
DropOut-e5f94912615e69c32c353fd6a63790e9b16685e4.zip
refactor(modpack): split modpack module and extract curseforge api (#127)
Co-authored-by: wsrsq <wsrsq001@163.com> Co-authored-by: 简律纯 <i@jyunko.cn> Co-authored-by: HsiangNianian <44714368+HsiangNianian@users.noreply.github.com>
Diffstat (limited to 'src-tauri/src/core/modpack/extractor.rs')
-rw-r--r--src-tauri/src/core/modpack/extractor.rs96
1 files changed, 96 insertions, 0 deletions
diff --git a/src-tauri/src/core/modpack/extractor.rs b/src-tauri/src/core/modpack/extractor.rs
new file mode 100644
index 0000000..b8d4ff2
--- /dev/null
+++ b/src-tauri/src/core/modpack/extractor.rs
@@ -0,0 +1,96 @@
+use std::{fs, path::Path};
+
+use super::archive;
+
+pub(crate) trait ProgressReporter {
+ fn report(&mut self, current: usize, total: usize, name: &str);
+}
+
+impl<F> ProgressReporter for F
+where
+ F: FnMut(usize, usize, &str),
+{
+ fn report(&mut self, current: usize, total: usize, name: &str) {
+ self(current, total, name);
+ }
+}
+
+pub(crate) trait OverrideExtractor: Send + Sync {
+ fn extract(
+ &self,
+ path: &Path,
+ game_dir: &Path,
+ override_prefixes: &[String],
+ reporter: &mut dyn ProgressReporter,
+ ) -> Result<(), String>;
+}
+
+#[derive(Debug, Default, Clone, Copy)]
+pub(crate) struct ZipOverrideExtractor;
+
+impl ZipOverrideExtractor {
+ pub(crate) fn new() -> Self {
+ Self
+ }
+}
+
+impl OverrideExtractor for ZipOverrideExtractor {
+ fn extract(
+ &self,
+ path: &Path,
+ game_dir: &Path,
+ override_prefixes: &[String],
+ reporter: &mut dyn ProgressReporter,
+ ) -> Result<(), String> {
+ let mut archive = archive::open(path)?;
+ let all_names = archive::list_names(&mut archive);
+ let prefixes: Vec<&str> = override_prefixes
+ .iter()
+ .filter(|prefix| {
+ all_names
+ .iter()
+ .any(|name| name.starts_with(prefix.as_str()))
+ })
+ .map(String::as_str)
+ .collect();
+ let strip = |name: &str| -> Option<String> {
+ prefixes.iter().find_map(|prefix| {
+ let relative = name.strip_prefix(*prefix)?;
+ (!relative.is_empty()).then(|| relative.to_string())
+ })
+ };
+ let total = all_names
+ .iter()
+ .filter(|name| strip(name).is_some())
+ .count();
+ let mut current = 0;
+
+ for index in 0..archive.len() {
+ let mut entry = archive.by_index(index).map_err(|e| e.to_string())?;
+ let name = entry.name().to_string();
+ let Some(relative) = strip(&name) else {
+ continue;
+ };
+
+ let outpath = game_dir.join(&relative);
+ if !outpath.starts_with(game_dir) {
+ continue;
+ }
+
+ if entry.is_dir() {
+ fs::create_dir_all(&outpath).map_err(|e| e.to_string())?;
+ } else {
+ if let Some(parent) = outpath.parent() {
+ fs::create_dir_all(parent).map_err(|e| e.to_string())?;
+ }
+ let mut file = fs::File::create(&outpath).map_err(|e| e.to_string())?;
+ std::io::copy(&mut entry, &mut file).map_err(|e| e.to_string())?;
+ }
+
+ current += 1;
+ reporter.report(current, total, &relative);
+ }
+
+ Ok(())
+ }
+}