launcher: find data packs on Modrinth and install one into a world

The Mods page gains a Data packs tab: Modrinth's data packs for 26.3,
each marked Partly with what Horizon runs of a data pack today
(functions, function tags, predicates, item modifiers). Install puts the
project's data pack file (not its mod build), checksum checked, into the
datapacks/ of a world picked on the tab, and says what of that zip runs
and what isn't read yet. A pack with nothing Horizon runs (worldgen
only) is refused. The world's list on Home shows it.

CLI and MCP: `mods search --kind datapack`, `mods install <pack> --world
<world>`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-27 03:15:49 +01:00
parent 9cb2779608
commit 2a7420651e
16 changed files with 548 additions and 21 deletions
+1
View File
@@ -25,6 +25,7 @@ fn class(kind: Kind) -> u64 {
Kind::Modpack => 4471,
Kind::Mod => 6,
Kind::Shader => 6552,
Kind::DataPack => 6945,
}
}
+1
View File
@@ -484,6 +484,7 @@ fn relative(kind: Kind, id: &str, file_name: &str) -> Result<String, String> {
Kind::ResourcePack => Ok(format!("resourcepacks/{file_name}")),
Kind::Shader => Ok(format!("shaderpacks/{file_name}")),
Kind::Modpack => Err("A modpack installs as its own profile (pack::install).".into()),
Kind::DataPack => Err("Horizon's registry has no data packs.".into()),
}
}
+62
View File
@@ -296,5 +296,67 @@ pub fn fetch_modpack(
source.fetch(file, folder, progress)
}
/// A data pack installed into a world.
pub struct DataPackInstall {
pub pack: crate::datapacks::DataPack,
/// Its version's number.
pub number: String,
/// What of it runs in Horizon.
pub coverage: crate::datapacks::Coverage,
}
/// Downloads a project's data pack (checksum checked) into the world folder `world`, under a
/// free name. Refused when nothing in it runs in Horizon yet. It isn't recorded with the
/// browser's installs: the world's data pack list (Home, `worlds datapack-list`) has it.
pub fn install_datapack(
source: &dyn Source,
project: &Project,
everything: bool,
world: &Path,
progress: &dyn Fn(f32),
) -> Result<DataPackInstall, String> {
if project.kind != Kind::DataPack && !project.loaders.iter().any(|l| l == "datapack") {
return Err(format!("{} isn't a data pack.", project.title));
}
let versions = source.versions(project)?;
let version = pick(Kind::DataPack, &versions, everything).ok_or_else(|| {
format!(
"{} has no data pack for {}{}.",
project.title,
super::GAME,
if everything {
" or any other version"
} else {
""
}
)
})?;
let file = version.file().expect("pick keeps versions with a file");
let folder = std::env::temp_dir().join(format!(
"horizon-launcher-datapack-{}-{}",
std::process::id(),
version.id
));
let result = source.fetch(file, &folder, progress).and_then(|zip| {
let coverage = crate::datapacks::coverage(&zip)?;
if coverage.read.is_empty() {
return Err(format!(
"{} would change nothing in Horizon yet: it has no functions, predicates or item \
modifiers, and Horizon doesn't read its {} yet.",
project.title,
coverage.not_read()
));
}
let pack = crate::datapacks::add(world, &zip)?;
Ok(DataPackInstall {
pack,
number: version.number.clone(),
coverage,
})
});
let _ = fs::remove_dir_all(&folder);
result
}
#[cfg(test)]
mod tests;
+88
View File
@@ -340,3 +340,91 @@ fn shader_packs_older_launchers_installed_are_left_alone() {
assert!(home.join("shaderpacks/old-pack.zip").is_file());
fs::remove_dir_all(&home).unwrap();
}
/// A data pack zip with these files.
fn datapack_zip(files: &[&str]) -> Vec<u8> {
use std::io::Write;
let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
let options = zip::write::SimpleFileOptions::default();
zip.start_file("pack.mcmeta", options).unwrap();
zip.write_all(br#"{"pack":{"pack_format":94,"description":"Test"}}"#)
.unwrap();
for name in files {
zip.start_file(*name, options).unwrap();
zip.write_all(b"{}").unwrap();
}
zip.finish().unwrap().into_inner()
}
#[test]
fn data_packs_go_into_a_world_when_something_in_them_runs() {
let world = home("datapack").join("saves/New World");
fs::create_dir_all(&world).unwrap();
let mut fake = Fake::default();
let mut tools = project("tools", Kind::DataPack);
tools.loaders = vec!["datapack".into()];
fake.add(tools);
let mut terrain = project("terrain", Kind::Mod);
terrain.loaders = vec!["datapack".into(), "fabric".into()];
fake.add(terrain);
let mut jar = project("jar", Kind::Mod);
jar.loaders = vec!["fabric".into()];
fake.add(jar);
let runs = datapack_zip(&[
"data/t/function/a.mcfunction",
"data/t/function/b.mcfunction",
"data/minecraft/tags/function/load.json",
"data/t/recipe/r.json",
]);
fake.release("tools", "2.0", &[], &runs, false);
fake.release(
"terrain",
"1.0",
&[],
&datapack_zip(&["data/t/worldgen/biome/b.json"]),
false,
);
let tools = fake.project("tools").unwrap();
let off = &|_: f32| {};
let done = install_datapack(&fake, &tools, false, &world, off).unwrap();
assert_eq!(done.pack.file, "tools-2.0.zip");
assert!(done.pack.enabled);
assert_eq!(done.number, "2.0");
assert_eq!(
done.coverage.summary(),
"Runs its 2 functions, 1 function tag. Not read yet: 1 recipe."
);
assert!(world.join("datapacks/tools-2.0.zip").is_file());
// Nothing of it in the browser's own list.
assert!(Installed::load(world.parent().unwrap().parent().unwrap())
.entries
.is_empty());
let again = install_datapack(&fake, &tools, false, &world, off).unwrap();
assert_eq!(again.pack.file, "tools-2.0 (2).zip");
let terrain = fake.project("terrain").unwrap();
let refused = install_datapack(&fake, &terrain, false, &world, off)
.err()
.unwrap();
assert_eq!(
refused,
"Pack terrain would change nothing in Horizon yet: it has no functions, predicates or \
item modifiers, and Horizon doesn't read its 1 worldgen yet."
);
let jar = fake.project("jar").unwrap();
assert!(install_datapack(&fake, &jar, false, &world, off).is_err());
let mut bad = project("bad", Kind::DataPack);
bad.loaders = vec!["datapack".into()];
fake.add(bad);
fake.release("bad", "1", &[], &runs, true);
let bad = fake.project("bad").unwrap();
assert!(install_datapack(&fake, &bad, false, &world, off).is_err());
let names: Vec<String> = fs::read_dir(world.join("datapacks"))
.unwrap()
.map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
.collect();
assert_eq!(names.len(), 2, "{names:?}");
fs::remove_dir_all(world.parent().unwrap().parent().unwrap()).unwrap();
}
+21 -4
View File
@@ -14,14 +14,17 @@ fn project_type(kind: Kind) -> &'static str {
Kind::Modpack => "modpack",
Kind::Mod => "mod",
Kind::Shader => "shader",
Kind::DataPack => "datapack",
}
}
/// The kind of a Modrinth project; its shader packs (the shaders/ format) are not offered.
/// The kind of a Modrinth project; its shader packs (the shaders/ format) are not offered. A
/// data pack says it is a `mod` (most also ship a mod build): the search for data packs marks
/// its hits, and an install into a world asks for its data pack files.
fn kind_of(project_type: &str) -> Option<Kind> {
Kind::ALL
.into_iter()
.filter(|k| *k != Kind::Shader)
.filter(|k| !matches!(k, Kind::Shader | Kind::DataPack))
.find(|k| self::project_type(*k) == project_type)
}
@@ -31,7 +34,9 @@ pub(super) fn facets(query: &Query) -> String {
let mut all = vec![vec![format!("project_type:{}", project_type(query.kind))]];
match &f.game_version {
Some(v) => all.push(vec![format!("versions:{v}")]),
None if !query.everything && matches!(query.kind, Kind::ResourcePack | Kind::Mod) => {
None if !query.everything
&& matches!(query.kind, Kind::ResourcePack | Kind::Mod | Kind::DataPack) =>
{
all.push(vec![format!("versions:{GAME}")])
}
None => {}
@@ -182,6 +187,10 @@ impl Source for Modrinth {
.into_iter()
.flatten()
.filter_map(parse_hit)
.map(|p| match query.kind {
Kind::DataPack => p.as_datapack(),
_ => p,
})
.collect())
}
@@ -222,7 +231,15 @@ impl Source for Modrinth {
));
}
let (c, l, v) = cached.as_ref().expect("filled above");
Ok(details::modrinth_tags(project_type(kind), c, l, v))
// Data packs share the mods' categories; their one loader is `datapack`.
let mut tags = match kind {
Kind::DataPack => details::modrinth_tags("mod", c, l, v),
_ => details::modrinth_tags(project_type(kind), c, l, v),
};
if kind == Kind::DataPack {
tags.loaders.clear();
}
Ok(tags)
}
}
+45
View File
@@ -72,6 +72,51 @@ fn picks_the_newest_version_that_runs() {
assert!(pick(Kind::ResourcePack, &[blocked, empty], true).is_none());
}
#[test]
fn data_packs_are_their_own_kind() {
// Most data packs on Modrinth also ship a mod build; only the data pack files are picked.
let versions = [
version("fabric", &[GAME], &["fabric", "neoforge"]),
version("pack", &[GAME], &["datapack"]),
version("old", &["1.21.4"], &["datapack"]),
];
assert_eq!(pick(Kind::DataPack, &versions, false).unwrap().id, "pack");
assert!(pick(Kind::DataPack, &versions[2..], false).is_none());
assert_eq!(
pick(Kind::DataPack, &versions[2..], true).unwrap().id,
"old"
);
assert!(pick(Kind::DataPack, &versions[..1], true).is_none());
let now = support(Kind::DataPack, &v(&[GAME]), &v(&["datapack"]));
assert_eq!(now.reason(), Some(DATAPACK_SUPPORT));
let old = support(Kind::DataPack, &v(&["1.21.4"]), &v(&["datapack"]));
assert!(
old.reason().unwrap().starts_with("Made for 1.21.4"),
"{old:?}"
);
assert!(old.installable());
assert_eq!(
modrinth::facets(&Query::new("", Kind::DataPack)),
r#"[["project_type:datapack"],["versions:26.3"]]"#
);
// A data pack's hit says `mod`: the search for data packs marks it.
let hit = serde_json::json!({
"project_id": "bVrj3hwG", "project_type": "mod", "slug": "terralith",
"title": "Terralith", "categories": ["datapack", "fabric", "worldgen"],
"versions": [GAME],
});
let p = modrinth::parse_hit(&hit).unwrap();
assert_eq!(p.kind, Kind::Mod);
let p = p.as_datapack();
assert_eq!(
(p.kind, p.page.as_str()),
(Kind::DataPack, "https://modrinth.com/datapack/terralith")
);
assert_eq!(Kind::DataPack.folder(), None);
}
#[test]
fn file_names_must_be_plain() {
for good in [