feat(content): add datapack discovery and world installs
This commit is contained in:
1 parent
a98426e22a
commit
7986bab3a7
27 files changed
+1138
-119
No files matched your search
@@ -0,0 +1,9 @@
|
||||
// data pack scanning backed by the same pack.mcmeta reader as resource packs.
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
use super::entry::ContentEntry;
|
||||
|
||||
pub fn scan_one_datapack(path: &Path, file_stem: &str, enabled: bool) -> ContentEntry {
|
||||
super::packs::scan_one_pack(path, file_stem, enabled)
|
||||
}
|
||||
@@ -17,6 +17,8 @@ pub struct InstallRoot {
|
||||
pub title: String,
|
||||
pub version: VersionInfo,
|
||||
pub installed_path: Option<PathBuf>,
|
||||
pub kind: ContentKind,
|
||||
pub target_world: Option<PathBuf>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -26,6 +28,8 @@ pub struct PlannedInstall {
|
||||
pub title: String,
|
||||
pub version: VersionInfo,
|
||||
pub installed_path: Option<PathBuf>,
|
||||
pub kind: ContentKind,
|
||||
pub destination: PathBuf,
|
||||
pub provider_aliases: Vec<ProviderProject>,
|
||||
pub required_dependencies: Vec<ProviderProject>,
|
||||
pub automatic_dependency: bool,
|
||||
@@ -77,7 +81,6 @@ pub async fn install(
|
||||
registry: &ProviderRegistry,
|
||||
manifest_path: &Path,
|
||||
minecraft_dir: &Path,
|
||||
destination: &Path,
|
||||
plan: &DependencyPlan,
|
||||
transaction_id: u64,
|
||||
) -> Result<InstallResult, NetError> {
|
||||
@@ -85,22 +88,16 @@ pub async fn install(
|
||||
.items
|
||||
.first()
|
||||
.ok_or_else(|| NetError::Parse("Dependency plan is empty".to_owned()))?;
|
||||
tokio::fs::create_dir_all(destination).await?;
|
||||
let staging = destination.join(format!(".rmcl-install-{transaction_id}"));
|
||||
for item in &plan.items {
|
||||
tokio::fs::create_dir_all(&item.destination).await?;
|
||||
}
|
||||
let staging = minecraft_dir.join(format!(".rmcl-install-{transaction_id}"));
|
||||
if staging.exists() {
|
||||
tokio::fs::remove_dir_all(&staging).await?;
|
||||
}
|
||||
tokio::fs::create_dir(&staging).await?;
|
||||
|
||||
let result = install_staged(
|
||||
registry,
|
||||
manifest_path,
|
||||
minecraft_dir,
|
||||
destination,
|
||||
&staging,
|
||||
plan,
|
||||
)
|
||||
.await;
|
||||
let result = install_staged(registry, manifest_path, minecraft_dir, &staging, plan).await;
|
||||
if let Err(error) = tokio::fs::remove_dir_all(&staging).await
|
||||
&& error.kind() != std::io::ErrorKind::NotFound
|
||||
{
|
||||
@@ -133,7 +130,6 @@ async fn install_staged(
|
||||
registry: &ProviderRegistry,
|
||||
manifest_path: &Path,
|
||||
minecraft_dir: &Path,
|
||||
destination: &Path,
|
||||
staging: &Path,
|
||||
plan: &DependencyPlan,
|
||||
) -> Result<(PathBuf, Vec<PathBuf>), NetError> {
|
||||
@@ -146,8 +142,10 @@ async fn install_staged(
|
||||
let provider = registry.get(&item.provider).ok_or_else(|| {
|
||||
NetError::Parse(format!("{} content provider is unavailable", item.provider))
|
||||
})?;
|
||||
let item_staging = staging.join(format!("item-{index}"));
|
||||
tokio::fs::create_dir(&item_staging).await?;
|
||||
let outcome = provider
|
||||
.download_version(&item.version, staging, None)
|
||||
.download_version(&item.version, &item_staging, None)
|
||||
.await?;
|
||||
let source = match outcome {
|
||||
crate::net::modrinth::DownloadOutcome::Downloaded(path)
|
||||
@@ -159,7 +157,7 @@ async fn install_staged(
|
||||
item.title
|
||||
))
|
||||
})?;
|
||||
let target = destination.join(file_name);
|
||||
let target = item.destination.join(file_name);
|
||||
if !targets.insert(target.clone()) {
|
||||
return Err(NetError::Parse(format!(
|
||||
"Multiple selected projects install '{}'",
|
||||
@@ -321,7 +319,7 @@ fn build_records(
|
||||
old_relative,
|
||||
ContentFileRecord {
|
||||
relative_path,
|
||||
kind: ContentKind::Mod,
|
||||
kind: item.kind,
|
||||
enabled: true,
|
||||
fingerprint: crate::instance::content::manifest::fingerprint(&path)?,
|
||||
resolution: crate::instance::Resolution::Resolved {
|
||||
@@ -376,6 +374,7 @@ impl ProjectKey {
|
||||
#[derive(Debug, Clone)]
|
||||
struct Node {
|
||||
title: String,
|
||||
kind: ContentKind,
|
||||
version: VersionInfo,
|
||||
installed: Option<InstalledMatch>,
|
||||
automatic_dependency: bool,
|
||||
@@ -415,11 +414,16 @@ pub async fn resolve(
|
||||
instance: &InstanceConfig,
|
||||
root: InstallRoot,
|
||||
) -> Result<DependencyPlan, NetError> {
|
||||
if root.kind == ContentKind::DataPack && root.target_world.is_none() {
|
||||
return Err(NetError::Parse(
|
||||
"A target world is required for datapack installation".to_owned(),
|
||||
));
|
||||
}
|
||||
let provider = registry.get(&root.provider).ok_or_else(|| {
|
||||
NetError::Parse(format!("{} content provider is unavailable", root.provider))
|
||||
})?;
|
||||
let root_version = provider.version(&root.version.id).await?;
|
||||
validate_compatible(&root_version, instance)?;
|
||||
validate_compatible(&root_version, instance, root.kind)?;
|
||||
if !root_version.project_id.is_empty() && root_version.project_id != root.project_id {
|
||||
return Err(NetError::Parse(format!(
|
||||
"Selected version belongs to project '{}', not '{}'",
|
||||
@@ -433,6 +437,7 @@ pub async fn resolve(
|
||||
root_key.clone(),
|
||||
Node {
|
||||
title: root.title,
|
||||
kind: root.kind,
|
||||
version: root_version.clone(),
|
||||
automatic_dependency: false,
|
||||
cleanup_eligible: false,
|
||||
@@ -462,14 +467,26 @@ pub async fn resolve(
|
||||
continue;
|
||||
}
|
||||
let exact = requirement.dependency.version_id.is_some();
|
||||
let parent_kind = nodes
|
||||
.get(&requirement.parent)
|
||||
.map(|node| node.kind)
|
||||
.unwrap_or(root.kind);
|
||||
let choice = resolve_requirement(
|
||||
provider,
|
||||
manifest,
|
||||
&remote_matches,
|
||||
instance,
|
||||
parent_kind,
|
||||
root.target_world.as_deref(),
|
||||
&requirement.dependency,
|
||||
)
|
||||
.await?;
|
||||
if choice.kind == ContentKind::DataPack && root.target_world.is_none() {
|
||||
return Err(NetError::Parse(format!(
|
||||
"Datapack dependency '{}' requires a target world",
|
||||
choice.title
|
||||
)));
|
||||
}
|
||||
let key = ProjectKey::new(provider.id(), choice.version.project_id.clone());
|
||||
if requirement.ancestors.contains(&key) {
|
||||
return Err(NetError::Parse(format!(
|
||||
@@ -505,6 +522,7 @@ pub async fn resolve(
|
||||
key.clone(),
|
||||
Node {
|
||||
title: choice.title,
|
||||
kind: choice.kind,
|
||||
version: choice.version.clone(),
|
||||
installed: choice.installed,
|
||||
automatic_dependency: choice.automatic_dependency,
|
||||
@@ -527,7 +545,14 @@ pub async fn resolve(
|
||||
}
|
||||
|
||||
let order = reachable_order(&root_key, &nodes);
|
||||
reject_installed_incompatibilities(provider, manifest, &remote_matches, &order, &nodes)?;
|
||||
reject_installed_incompatibilities(
|
||||
provider,
|
||||
manifest,
|
||||
&remote_matches,
|
||||
&order,
|
||||
&nodes,
|
||||
root.target_world.as_deref(),
|
||||
)?;
|
||||
let optional_dependencies = order
|
||||
.iter()
|
||||
.filter_map(|key| nodes.get(key))
|
||||
@@ -536,9 +561,15 @@ pub async fn resolve(
|
||||
let items = order
|
||||
.iter()
|
||||
.filter_map(|key| {
|
||||
nodes
|
||||
.get(key)
|
||||
.map(|node| planned_install(key, node, &nodes, minecraft_dir))
|
||||
nodes.get(key).map(|node| {
|
||||
planned_install(
|
||||
key,
|
||||
node,
|
||||
&nodes,
|
||||
minecraft_dir,
|
||||
root.target_world.as_deref(),
|
||||
)
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
Ok(DependencyPlan {
|
||||
@@ -549,6 +580,7 @@ pub async fn resolve(
|
||||
|
||||
struct ResolvedChoice {
|
||||
title: String,
|
||||
kind: ContentKind,
|
||||
version: VersionInfo,
|
||||
installed: Option<InstalledMatch>,
|
||||
automatic_dependency: bool,
|
||||
@@ -560,6 +592,8 @@ async fn resolve_requirement(
|
||||
manifest: &ContentManifest,
|
||||
remote_matches: &HashMap<PathBuf, ProviderProject>,
|
||||
instance: &InstanceConfig,
|
||||
parent_kind: ContentKind,
|
||||
target_world: Option<&Path>,
|
||||
dependency: &VersionDependency,
|
||||
) -> Result<ResolvedChoice, NetError> {
|
||||
let exact_version = match dependency.version_id.as_deref() {
|
||||
@@ -581,7 +615,24 @@ async fn resolve_requirement(
|
||||
dependency.file_name.as_deref().unwrap_or("unknown")
|
||||
))
|
||||
})?;
|
||||
let installed = find_installed(manifest, remote_matches, provider.id(), &project_id);
|
||||
let project = match provider.project(&project_id).await {
|
||||
Ok(project) => Some(project),
|
||||
Err(error) => {
|
||||
tracing::warn!(
|
||||
"Could not load metadata for dependency '{project_id}'; keeping it out of automatic cleanup: {error}"
|
||||
);
|
||||
None
|
||||
}
|
||||
};
|
||||
let kind = dependency_kind(project.as_ref(), exact_version.as_ref(), parent_kind);
|
||||
let installed = find_installed(
|
||||
manifest,
|
||||
remote_matches,
|
||||
provider.id(),
|
||||
&project_id,
|
||||
kind,
|
||||
target_world,
|
||||
);
|
||||
let version = if let Some(version) = exact_version {
|
||||
version
|
||||
} else {
|
||||
@@ -599,15 +650,10 @@ async fn resolve_requirement(
|
||||
None => None,
|
||||
};
|
||||
match installed_version {
|
||||
Some(version) if validate_compatible(&version, instance).is_ok() => version,
|
||||
Some(version) if validate_compatible(&version, instance, kind).is_ok() => version,
|
||||
_ => {
|
||||
let versions = provider
|
||||
.compatible_versions(
|
||||
&project_id,
|
||||
ContentKind::Mod,
|
||||
&instance.game_version,
|
||||
instance.loader,
|
||||
)
|
||||
.compatible_versions(&project_id, kind, &instance.game_version, instance.loader)
|
||||
.await?;
|
||||
select_preferred_version(versions).ok_or_else(|| {
|
||||
NetError::Parse(format!(
|
||||
@@ -617,22 +663,13 @@ async fn resolve_requirement(
|
||||
}
|
||||
}
|
||||
};
|
||||
validate_compatible(&version, instance)?;
|
||||
validate_compatible(&version, instance, kind)?;
|
||||
if !version.project_id.is_empty() && version.project_id != project_id {
|
||||
return Err(NetError::Parse(format!(
|
||||
"Dependency version '{}' belongs to project '{}', not '{}'",
|
||||
version.version_number, version.project_id, project_id
|
||||
)));
|
||||
}
|
||||
let project = match provider.project(&project_id).await {
|
||||
Ok(project) => Some(project),
|
||||
Err(error) => {
|
||||
tracing::warn!(
|
||||
"Could not load metadata for dependency '{project_id}'; keeping it out of automatic cleanup: {error}"
|
||||
);
|
||||
None
|
||||
}
|
||||
};
|
||||
let automatic_dependency = installed
|
||||
.as_ref()
|
||||
.is_none_or(|installed| installed.automatic_dependency);
|
||||
@@ -643,6 +680,7 @@ async fn resolve_requirement(
|
||||
.map(|project| project.title)
|
||||
.unwrap_or_else(|| project_id.clone()),
|
||||
version,
|
||||
kind,
|
||||
automatic_dependency,
|
||||
cleanup_eligible,
|
||||
installed,
|
||||
@@ -710,6 +748,46 @@ fn select_preferred_version(mut versions: Vec<VersionInfo>) -> Option<VersionInf
|
||||
versions.into_iter().next()
|
||||
}
|
||||
|
||||
fn dependency_kind(
|
||||
project: Option<&ProjectInfo>,
|
||||
version: Option<&VersionInfo>,
|
||||
parent_kind: ContentKind,
|
||||
) -> ContentKind {
|
||||
if version.is_some_and(|version| {
|
||||
version
|
||||
.loaders
|
||||
.iter()
|
||||
.any(|loader| loader.eq_ignore_ascii_case("datapack"))
|
||||
}) {
|
||||
return ContentKind::DataPack;
|
||||
}
|
||||
if version.is_some_and(|version| {
|
||||
version.loaders.iter().any(|loader| {
|
||||
matches!(
|
||||
loader.to_ascii_lowercase().as_str(),
|
||||
"fabric" | "forge" | "neoforge" | "quilt"
|
||||
)
|
||||
})
|
||||
}) {
|
||||
return ContentKind::Mod;
|
||||
}
|
||||
if project.is_some_and(|project| {
|
||||
project
|
||||
.loaders
|
||||
.iter()
|
||||
.any(|loader| loader.eq_ignore_ascii_case("datapack"))
|
||||
}) {
|
||||
return ContentKind::DataPack;
|
||||
}
|
||||
match project.map(|project| project.project_type.as_str()) {
|
||||
Some("resourcepack") => ContentKind::ResourcePack,
|
||||
Some("shader") => ContentKind::Shader,
|
||||
Some("datapack") => ContentKind::DataPack,
|
||||
Some("mod") => ContentKind::Mod,
|
||||
_ => parent_kind,
|
||||
}
|
||||
}
|
||||
|
||||
fn release_rank(version_type: VersionType) -> u8 {
|
||||
match version_type {
|
||||
VersionType::Release => 0,
|
||||
@@ -719,17 +797,22 @@ fn release_rank(version_type: VersionType) -> u8 {
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_compatible(version: &VersionInfo, instance: &InstanceConfig) -> Result<(), NetError> {
|
||||
fn validate_compatible(
|
||||
version: &VersionInfo,
|
||||
instance: &InstanceConfig,
|
||||
kind: ContentKind,
|
||||
) -> Result<(), NetError> {
|
||||
let loader = instance.loader.to_string().to_ascii_lowercase();
|
||||
if !version
|
||||
let supports_game = version
|
||||
.game_versions
|
||||
.iter()
|
||||
.any(|game_version| game_version == &instance.game_version)
|
||||
|| !version
|
||||
.any(|game_version| game_version == &instance.game_version);
|
||||
let supports_loader = kind != ContentKind::Mod
|
||||
|| version
|
||||
.loaders
|
||||
.iter()
|
||||
.any(|candidate| candidate.eq_ignore_ascii_case(&loader))
|
||||
{
|
||||
.any(|candidate| candidate.eq_ignore_ascii_case(&loader));
|
||||
if !supports_game || !supports_loader {
|
||||
return Err(NetError::Parse(format!(
|
||||
"Required dependency '{}' does not support Minecraft {} with {}",
|
||||
version.version_number, instance.game_version, instance.loader
|
||||
@@ -745,10 +828,10 @@ async fn resolve_installed_files(
|
||||
let files = manifest
|
||||
.files
|
||||
.iter()
|
||||
.filter(|record| record.kind == ContentKind::Mod && record.enabled)
|
||||
.filter(|record| record.enabled)
|
||||
.map(|record| FingerprintQuery {
|
||||
key: record.relative_path.to_string_lossy().into_owned(),
|
||||
kind: ContentKind::Mod,
|
||||
kind: record.kind,
|
||||
fingerprint: record.fingerprint.clone(),
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
@@ -785,11 +868,14 @@ fn find_installed(
|
||||
remote_matches: &HashMap<PathBuf, ProviderProject>,
|
||||
provider: &str,
|
||||
project_id: &str,
|
||||
kind: ContentKind,
|
||||
target_world: Option<&Path>,
|
||||
) -> Option<InstalledMatch> {
|
||||
manifest
|
||||
.files
|
||||
.iter()
|
||||
.filter(|record| record.enabled)
|
||||
.filter(|record| record.enabled && record.kind == kind)
|
||||
.filter(|record| record_in_target(record, kind, target_world))
|
||||
.find_map(|record| {
|
||||
let remote = remote_matches.get(&record.relative_path);
|
||||
if !record.matches_project(provider, project_id)
|
||||
@@ -803,6 +889,22 @@ fn find_installed(
|
||||
})
|
||||
}
|
||||
|
||||
fn record_in_target(
|
||||
record: &ContentFileRecord,
|
||||
kind: ContentKind,
|
||||
target_world: Option<&Path>,
|
||||
) -> bool {
|
||||
if kind != ContentKind::DataPack {
|
||||
return true;
|
||||
}
|
||||
let Some(world_name) = target_world.and_then(Path::file_name) else {
|
||||
return false;
|
||||
};
|
||||
record
|
||||
.relative_path
|
||||
.starts_with(Path::new("saves").join(world_name).join("datapacks"))
|
||||
}
|
||||
|
||||
fn installed_match(
|
||||
record: &ContentFileRecord,
|
||||
remote: Option<&ProviderProject>,
|
||||
@@ -861,17 +963,19 @@ fn reject_installed_incompatibilities(
|
||||
remote_matches: &HashMap<PathBuf, ProviderProject>,
|
||||
order: &[ProjectKey],
|
||||
nodes: &HashMap<ProjectKey, Node>,
|
||||
target_world: Option<&Path>,
|
||||
) -> Result<(), NetError> {
|
||||
for key in order {
|
||||
let Some(node) = nodes.get(key) else {
|
||||
continue;
|
||||
};
|
||||
for incompatible in &node.incompatible {
|
||||
let installed = find_installed(
|
||||
let installed = find_installed_incompatible(
|
||||
manifest,
|
||||
remote_matches,
|
||||
provider.id(),
|
||||
&incompatible.project_id,
|
||||
target_world,
|
||||
);
|
||||
let selected = order.iter().find_map(|selected_key| {
|
||||
(selected_key.provider == provider.id()
|
||||
@@ -901,11 +1005,37 @@ fn reject_installed_incompatibilities(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn find_installed_incompatible(
|
||||
manifest: &ContentManifest,
|
||||
remote_matches: &HashMap<PathBuf, ProviderProject>,
|
||||
provider: &str,
|
||||
project_id: &str,
|
||||
target_world: Option<&Path>,
|
||||
) -> Option<InstalledMatch> {
|
||||
manifest
|
||||
.files
|
||||
.iter()
|
||||
.filter(|record| record.enabled)
|
||||
.filter(|record| {
|
||||
record.kind != ContentKind::DataPack
|
||||
|| record_in_target(record, ContentKind::DataPack, target_world)
|
||||
})
|
||||
.find_map(|record| {
|
||||
let remote = remote_matches.get(&record.relative_path);
|
||||
(record.matches_project(provider, project_id)
|
||||
|| remote.is_some_and(|project| {
|
||||
project.provider == provider && project.project_id == project_id
|
||||
}))
|
||||
.then(|| installed_match(record, remote, provider, project_id))
|
||||
})
|
||||
}
|
||||
|
||||
fn planned_install(
|
||||
key: &ProjectKey,
|
||||
node: &Node,
|
||||
nodes: &HashMap<ProjectKey, Node>,
|
||||
minecraft_dir: &Path,
|
||||
target_world: Option<&Path>,
|
||||
) -> PlannedInstall {
|
||||
let installed_path = node
|
||||
.installed
|
||||
@@ -920,6 +1050,13 @@ fn planned_install(
|
||||
title: node.title.clone(),
|
||||
version: node.version.clone(),
|
||||
installed_path,
|
||||
kind: node.kind,
|
||||
destination: match node.kind {
|
||||
ContentKind::DataPack => target_world
|
||||
.expect("datapack dependency plan requires a world")
|
||||
.join("datapacks"),
|
||||
kind => minecraft_dir.join(kind.directory()),
|
||||
},
|
||||
provider_aliases: node.installed.as_ref().map_or_else(Vec::new, |installed| {
|
||||
if replacement {
|
||||
Vec::new()
|
||||
|
||||
@@ -29,6 +29,7 @@ pub struct WorldDetails {
|
||||
pub last_played: Option<chrono::DateTime<chrono::Utc>>,
|
||||
pub minecraft_version: Option<String>,
|
||||
pub size: Option<String>,
|
||||
pub datapacks: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
||||
@@ -17,6 +17,7 @@ pub enum ContentKind {
|
||||
Mod,
|
||||
ResourcePack,
|
||||
Shader,
|
||||
DataPack,
|
||||
}
|
||||
|
||||
impl ContentKind {
|
||||
@@ -25,6 +26,7 @@ impl ContentKind {
|
||||
Self::Mod => "mods",
|
||||
Self::ResourcePack => "resourcepacks",
|
||||
Self::Shader => "shaderpacks",
|
||||
Self::DataPack => "datapacks",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +37,7 @@ impl ContentKind {
|
||||
match self {
|
||||
Self::Mod => Some("Vanilla does not support mods."),
|
||||
Self::Shader => Some("Vanilla does not support shaders."),
|
||||
Self::ResourcePack => None,
|
||||
Self::ResourcePack | Self::DataPack => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -207,6 +209,20 @@ impl ContentManifest {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn resolved_project_path_under(
|
||||
&self,
|
||||
provider: &str,
|
||||
project_id: &str,
|
||||
minecraft_dir: &Path,
|
||||
directory: &Path,
|
||||
) -> Option<PathBuf> {
|
||||
self.files.iter().find_map(|record| {
|
||||
let path = minecraft_dir.join(&record.relative_path);
|
||||
(record.matches_project(provider, project_id) && path.starts_with(directory))
|
||||
.then_some(path)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn resolved_project_record(
|
||||
&self,
|
||||
provider: &str,
|
||||
@@ -224,11 +240,7 @@ impl ContentManifest {
|
||||
self.files
|
||||
.iter()
|
||||
.filter(|record| record.relative_path != relative_path)
|
||||
.filter(|record| {
|
||||
record.required_dependencies.iter().any(|dependency| {
|
||||
target.matches_project(&dependency.provider, &dependency.project_id)
|
||||
})
|
||||
})
|
||||
.filter(|record| dependency_applies(target, record))
|
||||
.map(|record| record.relative_path.clone())
|
||||
.collect()
|
||||
}
|
||||
@@ -262,11 +274,7 @@ impl ContentManifest {
|
||||
.files
|
||||
.iter()
|
||||
.filter(|record| !removed.contains(&record.relative_path))
|
||||
.any(|record| {
|
||||
record.required_dependencies.iter().any(|dependency| {
|
||||
candidate.matches_project(&dependency.provider, &dependency.project_id)
|
||||
})
|
||||
});
|
||||
.any(|record| dependency_applies(candidate, record));
|
||||
if !still_required {
|
||||
removed.insert(candidate.relative_path.clone());
|
||||
orphans.push(candidate.relative_path.clone());
|
||||
@@ -281,6 +289,24 @@ impl ContentManifest {
|
||||
}
|
||||
}
|
||||
|
||||
fn dependency_applies(candidate: &ContentFileRecord, dependent: &ContentFileRecord) -> bool {
|
||||
let matches = dependent
|
||||
.required_dependencies
|
||||
.iter()
|
||||
.any(|dependency| candidate.matches_project(&dependency.provider, &dependency.project_id));
|
||||
if !matches || candidate.kind != ContentKind::DataPack {
|
||||
return matches;
|
||||
}
|
||||
datapack_world(&candidate.relative_path) == datapack_world(&dependent.relative_path)
|
||||
}
|
||||
|
||||
fn datapack_world(path: &Path) -> Option<&std::ffi::OsStr> {
|
||||
let mut components = path.components();
|
||||
(components.next()?.as_os_str() == "saves")
|
||||
.then(|| components.next().map(std::path::Component::as_os_str))
|
||||
.flatten()
|
||||
}
|
||||
|
||||
impl ContentFileRecord {
|
||||
pub fn resolved_project(&self) -> Option<&ProviderProject> {
|
||||
match &self.resolution {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// scanning and toggling instance content (mods, resource packs, shaders, worlds).
|
||||
// minecraft uses a ".disabled" suffix convention for disabled content, so this leans on that heavily.
|
||||
|
||||
pub mod datapacks;
|
||||
pub mod dependencies;
|
||||
pub mod entry;
|
||||
pub mod icons;
|
||||
@@ -13,6 +14,7 @@ pub mod resource_packs;
|
||||
pub mod shaders;
|
||||
pub mod worlds;
|
||||
|
||||
pub use datapacks::scan_one_datapack;
|
||||
pub(crate) use icons::{
|
||||
IconCell, fallback_icon, fallback_icon_large, make_icon_pixels, make_icon_pixels_from_image,
|
||||
make_icon_quadrants_from_image,
|
||||
|
||||
@@ -522,13 +522,29 @@ fn content_files(minecraft_dir: &Path) -> std::io::Result<Vec<(ContentKind, Path
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Ok(worlds) = std::fs::read_dir(minecraft_dir.join("saves")) {
|
||||
for world in worlds.flatten().filter(|entry| entry.path().is_dir()) {
|
||||
let Ok(entries) = std::fs::read_dir(world.path().join("datapacks")) else {
|
||||
continue;
|
||||
};
|
||||
for entry in entries.flatten() {
|
||||
let path = entry.path();
|
||||
if supported_content_path(ContentKind::DataPack, &path) {
|
||||
files.push((ContentKind::DataPack, path));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
files.sort_by(|left, right| left.1.cmp(&right.1));
|
||||
Ok(files)
|
||||
}
|
||||
|
||||
fn supported_content_path(kind: ContentKind, path: &Path) -> bool {
|
||||
if path.is_dir() {
|
||||
return matches!(kind, ContentKind::ResourcePack | ContentKind::Shader);
|
||||
return matches!(
|
||||
kind,
|
||||
ContentKind::ResourcePack | ContentKind::Shader | ContentKind::DataPack
|
||||
);
|
||||
}
|
||||
if !path.is_file() {
|
||||
return false;
|
||||
@@ -538,7 +554,7 @@ fn supported_content_path(kind: ContentKind, path: &Path) -> bool {
|
||||
};
|
||||
match kind {
|
||||
ContentKind::Mod => name.ends_with(".jar") || name.ends_with(".jar.disabled"),
|
||||
ContentKind::ResourcePack | ContentKind::Shader => {
|
||||
ContentKind::ResourcePack | ContentKind::Shader | ContentKind::DataPack => {
|
||||
name.ends_with(".zip") || name.ends_with(".zip.disabled")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ pub fn scan_one_world(path: &Path, file_stem: &str, enabled: bool) -> ContentEnt
|
||||
.filter(|version| !version.trim().is_empty())
|
||||
.map(str::to_owned),
|
||||
size: (size > 0).then(|| format_size(size)),
|
||||
datapacks: datapack_names(path),
|
||||
};
|
||||
|
||||
ContentEntry {
|
||||
@@ -56,6 +57,28 @@ pub fn scan_one_world(path: &Path, file_stem: &str, enabled: bool) -> ContentEnt
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn datapack_names(world: &Path) -> Vec<String> {
|
||||
let Ok(entries) = std::fs::read_dir(world.join("datapacks")) else {
|
||||
return Vec::new();
|
||||
};
|
||||
let mut names = entries
|
||||
.flatten()
|
||||
.filter_map(|entry| {
|
||||
let path = entry.path();
|
||||
let name = path.file_name()?.to_str()?;
|
||||
if path.is_dir() {
|
||||
Some(name.trim_end_matches(".disabled").to_owned())
|
||||
} else {
|
||||
name.strip_suffix(".zip")
|
||||
.or_else(|| name.strip_suffix(".zip.disabled"))
|
||||
.map(str::to_owned)
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
names.sort_by_key(|name| name.to_lowercase());
|
||||
names
|
||||
}
|
||||
|
||||
pub fn scan_worlds(instances_dir: &Path, instance_name: &str) -> Vec<ContentEntry> {
|
||||
let saves_dir = instances_dir
|
||||
.join(instance_name)
|
||||
|
||||
+2
-2
@@ -18,8 +18,8 @@ pub use content::manifest::{
|
||||
ContentFileRecord, ContentKind, ContentManifest, FileFingerprint, ProviderProject, Resolution,
|
||||
};
|
||||
pub use content::{
|
||||
scan_mods, scan_one_mod, scan_one_resource_pack, scan_one_shader, scan_one_world,
|
||||
scan_resource_packs, scan_shaders, scan_worlds,
|
||||
scan_mods, scan_one_datapack, scan_one_mod, scan_one_resource_pack, scan_one_shader,
|
||||
scan_one_world, scan_resource_packs, scan_shaders, scan_worlds,
|
||||
};
|
||||
pub use launch::LaunchError;
|
||||
pub use loader::{GameVersion, ModLoaderInstaller, VanillaInstaller, get_installer};
|
||||
|
||||
@@ -15,6 +15,7 @@ struct FakeProvider {
|
||||
versions: HashMap<String, VersionInfo>,
|
||||
compatible: HashMap<String, Vec<String>>,
|
||||
projects: HashMap<String, String>,
|
||||
project_types: HashMap<String, String>,
|
||||
categories: HashMap<String, Vec<String>>,
|
||||
resolved: HashMap<String, ProviderProject>,
|
||||
fail_project: Option<String>,
|
||||
@@ -81,6 +82,12 @@ impl ContentProvider for FakeProvider {
|
||||
icon_url: None,
|
||||
categories: self.categories.get(project_id).cloned().unwrap_or_default(),
|
||||
additional_categories: Vec::new(),
|
||||
project_type: self
|
||||
.project_types
|
||||
.get(project_id)
|
||||
.cloned()
|
||||
.unwrap_or_else(|| "mod".to_owned()),
|
||||
loaders: Vec::new(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -123,7 +130,12 @@ impl ContentProvider for FakeProvider {
|
||||
version.id
|
||||
)));
|
||||
}
|
||||
let path = destination.join(format!("{}.jar", version.id));
|
||||
let extension = if version.loaders.iter().any(|loader| loader == "datapack") {
|
||||
"zip"
|
||||
} else {
|
||||
"jar"
|
||||
};
|
||||
let path = destination.join(format!("{}.{extension}", version.id));
|
||||
tokio::fs::write(&path, version.id.as_bytes()).await?;
|
||||
Ok(crate::net::modrinth::DownloadOutcome::Downloaded(path))
|
||||
}
|
||||
@@ -183,6 +195,8 @@ fn root(version: VersionInfo) -> InstallRoot {
|
||||
title: "Root".to_owned(),
|
||||
version,
|
||||
installed_path: None,
|
||||
kind: ContentKind::Mod,
|
||||
target_world: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,6 +250,7 @@ fn provider(versions: Vec<VersionInfo>) -> FakeProvider {
|
||||
.collect(),
|
||||
compatible,
|
||||
projects,
|
||||
project_types: HashMap::new(),
|
||||
categories,
|
||||
resolved: HashMap::new(),
|
||||
fail_project: None,
|
||||
@@ -243,6 +258,117 @@ fn provider(versions: Vec<VersionInfo>) -> FakeProvider {
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn datapack_dependencies_are_routed_to_the_world_or_instance_by_kind() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let minecraft = temp.path().join("minecraft");
|
||||
let world = minecraft.join("saves/world");
|
||||
let manifest_path = temp.path().join("manifest.json");
|
||||
let mut root_version = version(
|
||||
"root",
|
||||
"root",
|
||||
VersionType::Release,
|
||||
"2026-01-01",
|
||||
vec![dependency("library", DependencyType::Required)],
|
||||
);
|
||||
root_version.loaders = vec!["datapack".to_owned()];
|
||||
let library = version(
|
||||
"library",
|
||||
"library",
|
||||
VersionType::Release,
|
||||
"2026-01-01",
|
||||
Vec::new(),
|
||||
);
|
||||
let registry = provider(vec![root_version.clone(), library]).registry();
|
||||
let mut install_root = root(root_version);
|
||||
install_root.kind = ContentKind::DataPack;
|
||||
install_root.target_world = Some(world.clone());
|
||||
|
||||
let plan = resolve(
|
||||
®istry,
|
||||
&ContentManifest::default(),
|
||||
&minecraft,
|
||||
&instance(),
|
||||
install_root,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(plan.items[0].kind, ContentKind::DataPack);
|
||||
assert_eq!(plan.items[0].destination, world.join("datapacks"));
|
||||
assert_eq!(plan.items[1].kind, ContentKind::Mod);
|
||||
assert_eq!(plan.items[1].destination, minecraft.join("mods"));
|
||||
|
||||
install(®istry, &manifest_path, &minecraft, &plan, 7)
|
||||
.await
|
||||
.unwrap();
|
||||
let manifest = ContentManifest::load(&manifest_path).unwrap();
|
||||
assert_eq!(
|
||||
manifest
|
||||
.record(Path::new("saves/world/datapacks/root.zip"))
|
||||
.unwrap()
|
||||
.kind,
|
||||
ContentKind::DataPack
|
||||
);
|
||||
assert_eq!(
|
||||
manifest.record(Path::new("mods/library.jar")).unwrap().kind,
|
||||
ContentKind::Mod
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn datapack_install_rejects_an_incompatible_instance_mod() {
|
||||
let mut root_version = version(
|
||||
"root",
|
||||
"root",
|
||||
VersionType::Release,
|
||||
"2026-01-01",
|
||||
vec![dependency("conflict", DependencyType::Incompatible)],
|
||||
);
|
||||
root_version.loaders = vec!["datapack".to_owned()];
|
||||
let registry = provider(vec![root_version.clone()]).registry();
|
||||
let mut install_root = root(root_version);
|
||||
install_root.kind = ContentKind::DataPack;
|
||||
install_root.target_world = Some(PathBuf::from("/minecraft/saves/world"));
|
||||
let manifest = ContentManifest {
|
||||
version: 1,
|
||||
files: vec![installed_record("conflict", "conflict-version", true)],
|
||||
};
|
||||
|
||||
let error = resolve(
|
||||
®istry,
|
||||
&manifest,
|
||||
Path::new("/minecraft"),
|
||||
&instance(),
|
||||
install_root,
|
||||
)
|
||||
.await
|
||||
.unwrap_err();
|
||||
|
||||
assert!(error.to_string().contains("incompatible"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_modrinth_datapack_projects_are_classified_by_loader() {
|
||||
let project = ProjectInfo {
|
||||
id: "legacy".to_owned(),
|
||||
slug: "legacy".to_owned(),
|
||||
title: "Legacy datapack".to_owned(),
|
||||
description: String::new(),
|
||||
body: String::new(),
|
||||
icon_url: None,
|
||||
categories: Vec::new(),
|
||||
additional_categories: Vec::new(),
|
||||
project_type: "mod".to_owned(),
|
||||
loaders: vec!["datapack".to_owned()],
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
dependency_kind(Some(&project), None, ContentKind::DataPack),
|
||||
ContentKind::DataPack
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn required_dependencies_prefer_the_newest_stable_release() {
|
||||
let root_version = version(
|
||||
@@ -810,7 +936,7 @@ async fn dependency_install_commits_files_and_manifest_together() {
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let installed = install(®istry, &manifest_path, &minecraft, &mods, &plan, 1)
|
||||
let installed = install(®istry, &manifest_path, &minecraft, &plan, 1)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
@@ -874,7 +1000,7 @@ async fn failed_dependency_download_leaves_no_partial_install() {
|
||||
.unwrap();
|
||||
|
||||
assert!(
|
||||
install(®istry, &manifest_path, &minecraft, &mods, &plan, 2)
|
||||
install(®istry, &manifest_path, &minecraft, &plan, 2)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
|
||||
@@ -233,3 +233,44 @@ fn orphan_cleanup_keeps_automatic_non_library_dependencies() {
|
||||
.is_empty()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn datapack_dependencies_are_scoped_to_their_world() {
|
||||
let mut first_root = managed_record(
|
||||
"saves/first/datapacks/root.zip",
|
||||
"root",
|
||||
false,
|
||||
vec![dependency("library")],
|
||||
);
|
||||
first_root.kind = ContentKind::DataPack;
|
||||
let mut second_root = managed_record(
|
||||
"saves/second/datapacks/root.zip",
|
||||
"root",
|
||||
false,
|
||||
vec![dependency("library")],
|
||||
);
|
||||
second_root.kind = ContentKind::DataPack;
|
||||
let mut first_library = managed_record(
|
||||
"saves/first/datapacks/library.zip",
|
||||
"library",
|
||||
true,
|
||||
Vec::new(),
|
||||
);
|
||||
first_library.kind = ContentKind::DataPack;
|
||||
let mut second_library = managed_record(
|
||||
"saves/second/datapacks/library.zip",
|
||||
"library",
|
||||
true,
|
||||
Vec::new(),
|
||||
);
|
||||
second_library.kind = ContentKind::DataPack;
|
||||
let manifest = ContentManifest {
|
||||
version: 1,
|
||||
files: vec![first_root, second_root, first_library, second_library],
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
manifest.orphaned_dependencies_after_removing(Path::new("saves/first/datapacks/root.zip")),
|
||||
vec![PathBuf::from("saves/first/datapacks/library.zip")]
|
||||
);
|
||||
}
|
||||
@@ -80,7 +80,7 @@ fn unchanged_saved_index_reuses_fingerprint_and_skips_provider_query() {
|
||||
let fingerprint = first.manifest.files[0].fingerprint.clone();
|
||||
first.manifest.files[0].resolution = Resolution::Unmatched {
|
||||
checked_at: chrono::Utc::now().timestamp(),
|
||||
providers: vec!["modrinth".to_owned()],
|
||||
providers: vec!["modrinth".to_owned(), "curseforge".to_owned()],
|
||||
};
|
||||
first.manifest.save(&manifest_path).unwrap();
|
||||
|
||||
@@ -123,3 +123,28 @@ fn directory_packs_are_indexed_without_provider_queries() {
|
||||
PathBuf::from("resourcepacks/example")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn datapacks_are_indexed_under_their_world() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let minecraft = temp.path().join("minecraft");
|
||||
let datapacks = minecraft.join("saves/world/datapacks");
|
||||
std::fs::create_dir_all(datapacks.join("folder-pack")).unwrap();
|
||||
std::fs::write(datapacks.join("zipped-pack.zip"), b"zip").unwrap();
|
||||
|
||||
let inventory = reconcile_inventory(
|
||||
&temp.path().join("manifest.json"),
|
||||
&minecraft,
|
||||
24,
|
||||
512,
|
||||
&NoopProgress,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(inventory.manifest.files.len(), 2);
|
||||
assert!(inventory.manifest.files.iter().all(|record| {
|
||||
record.kind == ContentKind::DataPack
|
||||
&& record.relative_path.starts_with("saves/world/datapacks")
|
||||
}));
|
||||
assert_eq!(inventory.queries.len(), 1);
|
||||
}
|
||||
@@ -53,3 +53,19 @@ fn world_scan_uses_level_dat_metadata_and_falls_back_cleanly() {
|
||||
Some("2.0 KB")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn world_scan_lists_directory_and_zip_datapacks() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let world = temp.path().join("world");
|
||||
std::fs::create_dir_all(world.join("datapacks/folder-pack")).unwrap();
|
||||
std::fs::write(world.join("datapacks/zipped-pack.zip"), b"zip").unwrap();
|
||||
std::fs::write(world.join("datapacks/disabled-pack.zip.disabled"), b"zip").unwrap();
|
||||
|
||||
let details = scan_one_world(&world, "world", true).world_details.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
details.datapacks,
|
||||
["disabled-pack", "folder-pack", "zipped-pack"]
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user