fix(import): index Modrinth modpack content
This commit is contained in:
1 parent
c1af0f1b51
commit
9c9c6d9976
2 files changed
+135
No files matched your search
@@ -7,8 +7,12 @@ use std::path::Path;
|
|||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::feedback::progress;
|
use crate::feedback::progress;
|
||||||
|
use crate::instance::content::manifest::{
|
||||||
|
ContentFileRecord, ContentKind, ContentManifest, ProviderProject, Resolution, fingerprint,
|
||||||
|
};
|
||||||
use crate::instance::manager::InstanceManager;
|
use crate::instance::manager::InstanceManager;
|
||||||
use crate::instance::models::ModLoader;
|
use crate::instance::models::ModLoader;
|
||||||
|
use crate::storage::InstancePaths;
|
||||||
|
|
||||||
use super::{ImportSummary, PackFormat};
|
use super::{ImportSummary, PackFormat};
|
||||||
|
|
||||||
@@ -29,6 +33,8 @@ pub struct MrpackIndex {
|
|||||||
#[derive(Debug, Clone, Deserialize)]
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
pub struct MrpackFile {
|
pub struct MrpackFile {
|
||||||
pub path: String,
|
pub path: String,
|
||||||
|
#[serde(default)]
|
||||||
|
pub hashes: HashMap<String, String>,
|
||||||
pub downloads: Vec<String>,
|
pub downloads: Vec<String>,
|
||||||
#[serde(rename = "fileSize")]
|
#[serde(rename = "fileSize")]
|
||||||
pub file_size: u64,
|
pub file_size: u64,
|
||||||
@@ -170,6 +176,10 @@ pub async fn execute_import(
|
|||||||
.map_err(|e| -> Box<dyn std::error::Error + Send + Sync> { e.into() })?;
|
.map_err(|e| -> Box<dyn std::error::Error + Send + Sync> { e.into() })?;
|
||||||
download_mod_files(&index, &minecraft_dir).await?;
|
download_mod_files(&index, &minecraft_dir).await?;
|
||||||
extract_overrides(&summary.archive_path, &minecraft_dir)?;
|
extract_overrides(&summary.archive_path, &minecraft_dir)?;
|
||||||
|
seed_content_manifest(
|
||||||
|
&index,
|
||||||
|
&InstancePaths::new(manager.instances_dir.join(&name)),
|
||||||
|
)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
.await;
|
.await;
|
||||||
@@ -292,6 +302,82 @@ async fn download_mod_files(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn seed_content_manifest(
|
||||||
|
index: &MrpackIndex,
|
||||||
|
paths: &InstancePaths,
|
||||||
|
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||||
|
let mut manifest = ContentManifest::default();
|
||||||
|
for file in &index.files {
|
||||||
|
let relative_path = Path::new(&file.path);
|
||||||
|
let Some(kind) = mrpack_content_kind(relative_path) else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
let Some((project_id, version_id)) = file.downloads.iter().find_map(|url| {
|
||||||
|
let path = url.strip_prefix("https://cdn.modrinth.com/data/")?;
|
||||||
|
let (project_id, path) = path.split_once("/versions/")?;
|
||||||
|
let version_id = path.split('/').next()?;
|
||||||
|
(!project_id.is_empty() && !version_id.is_empty())
|
||||||
|
.then_some((project_id.to_owned(), version_id.to_owned()))
|
||||||
|
}) else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
let Some(expected_sha512) = file.hashes.get("sha512") else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
let file_fingerprint = fingerprint(&paths.minecraft().join(relative_path))?;
|
||||||
|
if file_fingerprint
|
||||||
|
.hash("sha512")
|
||||||
|
.is_none_or(|actual| !actual.eq_ignore_ascii_case(expected_sha512))
|
||||||
|
{
|
||||||
|
tracing::warn!(
|
||||||
|
"Skipping stale Modrinth identity for '{}' because its downloaded hash changed",
|
||||||
|
file.path
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
manifest.upsert(ContentFileRecord {
|
||||||
|
relative_path: relative_path.to_owned(),
|
||||||
|
kind,
|
||||||
|
enabled: true,
|
||||||
|
fingerprint: file_fingerprint,
|
||||||
|
resolution: Resolution::Resolved {
|
||||||
|
project: ProviderProject {
|
||||||
|
provider: "modrinth".to_owned(),
|
||||||
|
project_id,
|
||||||
|
version_id,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
let seeded = manifest.files.len();
|
||||||
|
manifest.save(&paths.content_manifest())?;
|
||||||
|
tracing::debug!(
|
||||||
|
"Seeded {} exact Modrinth content record(s) from .mrpack '{}'",
|
||||||
|
seeded,
|
||||||
|
index.name
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mrpack_content_kind(path: &Path) -> Option<ContentKind> {
|
||||||
|
use std::path::Component;
|
||||||
|
|
||||||
|
if path
|
||||||
|
.components()
|
||||||
|
.any(|component| !matches!(component, Component::Normal(_)))
|
||||||
|
{
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let directory = path.components().next()?.as_os_str().to_str()?;
|
||||||
|
let name = path.file_name()?.to_str()?.to_ascii_lowercase();
|
||||||
|
match (directory, name.as_str()) {
|
||||||
|
("mods", name) if name.ends_with(".jar") => Some(ContentKind::Mod),
|
||||||
|
("resourcepacks", name) if name.ends_with(".zip") => Some(ContentKind::ResourcePack),
|
||||||
|
("shaderpacks", name) if name.ends_with(".zip") => Some(ContentKind::Shader),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn extract_overrides(
|
fn extract_overrides(
|
||||||
mrpack_path: &Path,
|
mrpack_path: &Path,
|
||||||
minecraft_dir: &Path,
|
minecraft_dir: &Path,
|
||||||
|
|||||||
@@ -158,11 +158,13 @@ fn mod_files_download_to_their_manifest_paths() {
|
|||||||
files: vec![
|
files: vec![
|
||||||
MrpackFile {
|
MrpackFile {
|
||||||
path: "mods/first.jar".to_owned(),
|
path: "mods/first.jar".to_owned(),
|
||||||
|
hashes: Default::default(),
|
||||||
downloads: vec![format!("{}/first.jar", server.uri())],
|
downloads: vec![format!("{}/first.jar", server.uri())],
|
||||||
file_size: 5,
|
file_size: 5,
|
||||||
},
|
},
|
||||||
MrpackFile {
|
MrpackFile {
|
||||||
path: "resourcepacks/second.jar".to_owned(),
|
path: "resourcepacks/second.jar".to_owned(),
|
||||||
|
hashes: Default::default(),
|
||||||
downloads: vec![format!("{}/second.jar", server.uri())],
|
downloads: vec![format!("{}/second.jar", server.uri())],
|
||||||
file_size: 6,
|
file_size: 6,
|
||||||
},
|
},
|
||||||
@@ -202,6 +204,7 @@ fn mod_file_without_a_download_url_fails_without_creating_a_file() {
|
|||||||
dependencies: Default::default(),
|
dependencies: Default::default(),
|
||||||
files: vec![MrpackFile {
|
files: vec![MrpackFile {
|
||||||
path: "mods/missing.jar".to_owned(),
|
path: "mods/missing.jar".to_owned(),
|
||||||
|
hashes: Default::default(),
|
||||||
downloads: Vec::new(),
|
downloads: Vec::new(),
|
||||||
file_size: 0,
|
file_size: 0,
|
||||||
}],
|
}],
|
||||||
@@ -213,3 +216,49 @@ fn mod_file_without_a_download_url_fails_without_creating_a_file() {
|
|||||||
crate::feedback::progress::clear();
|
crate::feedback::progress::clear();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn import_seeds_exact_modrinth_content_identity() {
|
||||||
|
let tmp = tempfile::tempdir().unwrap();
|
||||||
|
let paths = crate::storage::InstancePaths::new(tmp.path());
|
||||||
|
let relative_path = PathBuf::from("resourcepacks/example.zip");
|
||||||
|
std::fs::create_dir_all(paths.minecraft().join("resourcepacks")).unwrap();
|
||||||
|
std::fs::write(paths.minecraft().join(&relative_path), b"resource pack").unwrap();
|
||||||
|
let sha512 =
|
||||||
|
crate::instance::content::manifest::fingerprint(&paths.minecraft().join(&relative_path))
|
||||||
|
.unwrap()
|
||||||
|
.hash("sha512")
|
||||||
|
.unwrap()
|
||||||
|
.to_owned();
|
||||||
|
let index = MrpackIndex {
|
||||||
|
format_version: 1,
|
||||||
|
game: "minecraft".to_owned(),
|
||||||
|
version_id: "1".to_owned(),
|
||||||
|
name: "Indexed".to_owned(),
|
||||||
|
dependencies: Default::default(),
|
||||||
|
files: vec![MrpackFile {
|
||||||
|
path: relative_path.to_string_lossy().into_owned(),
|
||||||
|
hashes: HashMap::from([("sha512".to_owned(), sha512)]),
|
||||||
|
downloads: vec![
|
||||||
|
"https://cdn.modrinth.com/data/project/versions/version/example.zip".to_owned(),
|
||||||
|
],
|
||||||
|
file_size: 13,
|
||||||
|
}],
|
||||||
|
};
|
||||||
|
|
||||||
|
seed_content_manifest(&index, &paths).unwrap();
|
||||||
|
|
||||||
|
let manifest = crate::instance::ContentManifest::load(&paths.content_manifest()).unwrap();
|
||||||
|
let record = manifest.record(&relative_path).unwrap();
|
||||||
|
assert_eq!(record.kind, crate::instance::ContentKind::ResourcePack);
|
||||||
|
assert_eq!(
|
||||||
|
record.resolution,
|
||||||
|
crate::instance::Resolution::Resolved {
|
||||||
|
project: crate::instance::ProviderProject {
|
||||||
|
provider: "modrinth".to_owned(),
|
||||||
|
project_id: "project".to_owned(),
|
||||||
|
version_id: "version".to_owned(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in new issue
Block a user