refactor: share filesystem helpers and correct pack imports
This commit is contained in:
1 parent
4de886fcd0
commit
2114cdc72a
19 files changed
+524
-378
No files matched your search
@@ -1,9 +1,6 @@
|
||||
// SPDX-FileCopyrightText: 2026 Constantin Bauer
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
// curseforge modpack archives: manifest.json references provider file ids,
|
||||
// while the overrides directory contains configs and other bundled files.
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
use serde::Deserialize;
|
||||
@@ -97,7 +94,7 @@ fn loader(manifest: &Manifest) -> (ModLoader, Option<String>) {
|
||||
pub fn build_summary(path: &Path) -> Result<ImportSummary, String> {
|
||||
let manifest = parse(path)?;
|
||||
let (loader, loader_version) = loader(&manifest);
|
||||
let override_count = count_overrides(path, &manifest.overrides)?;
|
||||
let override_count = super::override_files(path, &[&manifest.overrides])?.len();
|
||||
Ok(ImportSummary {
|
||||
name: manifest.name,
|
||||
pack_version: manifest.version,
|
||||
@@ -131,15 +128,18 @@ async fn download_files(
|
||||
manifest: &Manifest,
|
||||
minecraft_dir: &Path,
|
||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
let api_key =
|
||||
crate::net::curseforge::api_key().ok_or("CurseForge API key is not configured")?;
|
||||
let client = crate::net::HttpClient::new();
|
||||
let ids = manifest
|
||||
.files
|
||||
.iter()
|
||||
.filter(|file| file.required)
|
||||
.map(|file| file.file_id)
|
||||
.collect::<Vec<_>>();
|
||||
if ids.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
let api_key =
|
||||
crate::net::curseforge::api_key().ok_or("CurseForge API key is not configured")?;
|
||||
let client = crate::net::HttpClient::new();
|
||||
let versions = crate::net::curseforge::fetch_file_versions(&client, api_key, &ids).await?;
|
||||
if versions.len() != ids.len() {
|
||||
return Err(format!(
|
||||
@@ -218,53 +218,13 @@ pub(super) async fn owned_files(path: &Path) -> Result<Vec<std::path::PathBuf>,
|
||||
Ok(files)
|
||||
}
|
||||
|
||||
fn count_overrides(path: &Path, root: &str) -> Result<usize, String> {
|
||||
let root = root.trim_matches('/');
|
||||
if root.is_empty() {
|
||||
return Ok(0);
|
||||
}
|
||||
let file = std::fs::File::open(path).map_err(|error| error.to_string())?;
|
||||
let archive = zip::ZipArchive::new(file).map_err(|error| error.to_string())?;
|
||||
let prefix = format!("{root}/");
|
||||
Ok(archive
|
||||
.file_names()
|
||||
.filter(|name| name.starts_with(&prefix) && !name.ends_with('/'))
|
||||
.count())
|
||||
}
|
||||
|
||||
fn extract_overrides(
|
||||
path: &Path,
|
||||
minecraft_dir: &Path,
|
||||
root: &str,
|
||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
progress::set_action("Extracting overrides...");
|
||||
let file = std::fs::File::open(path)?;
|
||||
let mut archive = zip::ZipArchive::new(file)?;
|
||||
let root = root.trim_matches('/');
|
||||
if root.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
for index in 0..archive.len() {
|
||||
let mut entry = archive.by_index(index)?;
|
||||
let enclosed = entry.enclosed_name().ok_or("Unsafe override path")?;
|
||||
let Ok(relative) = enclosed.strip_prefix(root) else {
|
||||
continue;
|
||||
};
|
||||
if relative.as_os_str().is_empty() {
|
||||
continue;
|
||||
}
|
||||
let destination = minecraft_dir.join(relative);
|
||||
if entry.is_dir() {
|
||||
std::fs::create_dir_all(destination)?;
|
||||
continue;
|
||||
}
|
||||
if let Some(parent) = destination.parent() {
|
||||
std::fs::create_dir_all(parent)?;
|
||||
}
|
||||
let mut destination = std::fs::File::create(destination)?;
|
||||
std::io::copy(&mut entry, &mut destination)?;
|
||||
}
|
||||
Ok(())
|
||||
super::archive::extract_overrides(path, minecraft_dir, &[root])
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in new issue
Block a user