From da996b9fd13e02ab3e758393b6d428d4ef6f07d0 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:06:28 +0200 Subject: [PATCH 01/11] port-pack: check one mod jar (Works, Partly, Doesn't run) from its content and classes `horizon-port-pack check ` carries a single jar into a folder content mod and reads its class files (constant pool, superclass, declared methods) for what the carry can't take: block entities, menus, entities, blocks and items with behaviour of their own, recipe types, worldgen code, Mixins, game events. The verdict and the lists of what ports and what is skipped go to check.json, for the launcher's mod browser to show before install. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-port-pack/src/check.rs | 366 ++++++++++++++ crates/horizon-port-pack/src/classes.rs | 642 ++++++++++++++++++++++++ crates/horizon-port-pack/src/lib.rs | 2 + crates/horizon-port-pack/src/main.rs | 17 +- docs/modpacks.md | 16 + 5 files changed, 1042 insertions(+), 1 deletion(-) create mode 100644 crates/horizon-port-pack/src/check.rs create mode 100644 crates/horizon-port-pack/src/classes.rs diff --git a/crates/horizon-port-pack/src/check.rs b/crates/horizon-port-pack/src/check.rs new file mode 100644 index 000000000..16b360ab6 --- /dev/null +++ b/crates/horizon-port-pack/src/check.rs @@ -0,0 +1,366 @@ +//! One mod jar on its own, as the launcher's mod browser installs it: what +//! of it Horizon runs, said before install (Works, Partly, Doesn't run), +//! from the jar itself. The jar is carried over ([`crate::carry`]) into a +//! folder content mod, and its classes are read ([`crate::classes`]) for +//! what the carry can't take: block entities, entities, Mixins, behaviour. +//! +//! - **Works**: content only. Its blocks, items, recipes, loot, tags and +//! worldgen port, and its code only registers them. +//! - **Partly**: the content ports; the code features listed don't. +//! - **Doesn't run**: nothing to port (everything it does is Java code), or +//! the jar can't be carried at all. +use crate::{carry, classes, jar}; +use serde::{Deserialize, Serialize}; +use std::{collections::BTreeMap, fs, io::Read, path::Path}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum Verdict { + Works, + Partly, + No, +} + +impl Verdict { + pub fn label(self) -> &'static str { + match self { + Verdict::Works => "Works", + Verdict::Partly => "Partly", + Verdict::No => "Doesn't run", + } + } +} + +/// What checking a jar found. Written as `check.json` beside the carry. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct Check { + pub mod_id: String, + pub name: String, + pub version: String, + /// `neoforge`, `forge`, `fabric`, `quilt`, or `` for a plain zip. + pub loader: String, + pub license: String, + pub verdict: Verdict, + /// One line: why this verdict. + pub summary: String, + /// What ports: "12 blocks", "40 recipes"... + pub ports: Vec, + /// What doesn't, with why. + pub skipped: Vec, + /// Required mods, by id. + pub depends: Vec, + /// The folder mod's namespaces (its id first): two mods on at once may + /// not share one. + pub namespaces: Vec, + pub code: classes::Code, +} + +/// The loader a jar is made for, from its metadata file. +fn loader(names: &[String]) -> &'static str { + let has = |n: &str| names.iter().any(|x| x == n); + if has("META-INF/neoforge.mods.toml") { + "neoforge" + } else if has("META-INF/mods.toml") { + "forge" + } else if has("fabric.mod.json") { + "fabric" + } else if has("quilt.mod.json") { + "quilt" + } else { + "" + } +} + +/// Data folders named as before 1.21 (`recipes/`, `loot_tables/`) and none +/// as 1.21 names them: the game doesn't read them, and the carry upgrades +/// from 1.21.1 on. (Some jars keep a stale plural folder beside the new one.) +fn old_layout(names: &[String]) -> bool { + let has = |kinds: [&str; 2]| { + names.iter().any(|n| { + let p: Vec<&str> = n.split('/').collect(); + p.len() > 3 && p[0] == "data" && kinds.contains(&p[2]) && n.ends_with(".json") + }) + }; + has(["recipes", "loot_tables"]) && !has(["recipe", "loot_table"]) +} + +fn plural(n: usize, one: &str, many: &str) -> String { + format!("{n} {}", if n == 1 { one } else { many }) +} + +/// Checks `jar` and carries it over into `out/carry/` (`out/check.json`, +/// `out/carry-report.json` beside it). The carry is the folder mod an +/// install copies. +pub fn check(jar_path: &Path, out: &Path) -> Result { + let info = jar::read(jar_path)?; + let file = fs::File::open(jar_path).map_err(|e| format!("{}: {e}", jar_path.display()))?; + let mut zip = zip::ZipArchive::new(file).map_err(|e| format!("{}: {e}", jar_path.display()))?; + let names: Vec = zip.file_names().map(str::to_owned).collect(); + let mut bytes = Vec::new(); + for n in names.iter().filter(|n| n.ends_with(".class")) { + let mut b = Vec::new(); + if let Ok(mut e) = zip.by_name(n) + && e.size() < 1 << 24 + && e.read_to_end(&mut b).is_ok() + { + bytes.push(b); + } + } + let code = classes::census(bytes.iter().map(Vec::as_slice)); + let mut check = Check { + mod_id: info.mod_id.clone(), + name: info.name.clone(), + version: info.version.clone(), + loader: loader(&names).to_owned(), + license: info.license.clone(), + verdict: Verdict::No, + summary: String::new(), + ports: Vec::new(), + skipped: Vec::new(), + depends: info.depends.clone(), + namespaces: Vec::new(), + code, + }; + fs::create_dir_all(out).map_err(|e| format!("{}: {e}", out.display()))?; + let done = |check: Check| -> Result { + crate::pipeline::write_json(&out.join("check.json"), &check)?; + Ok(check) + }; + if old_layout(&names) { + check.summary = "Made for Minecraft 1.20 or older: Horizon ports mods made for 1.21 \ + and later." + .into(); + return done(check); + } + let alone = |m: &str| m == info.mod_id || m == "minecraft"; + let pack = carry::Pack { + loaded: &alone, + mods: &alone, + }; + let report = match carry::carry( + jar_path, + &info, + out, + None, + &|_| true, + &pack, + &BTreeMap::new(), + ) { + Ok(r) => r, + Err(e) => { + check.summary = format!("Can't be carried over: {e}"); + return done(check); + } + }; + check.namespaces = std::iter::once(info.mod_id.clone()) + .chain(report.namespaces.iter().cloned()) + .collect(); + let c = &info.census; + let recipes = report.recipes_upgraded; + let loot = c.loot_tables + report.loot_tables_added; + let content = + report.blocks.len() + report.items + recipes + loot + report.worldgen + c.structures; + for (n, one, many) in [ + (report.blocks.len(), "block", "blocks"), + (report.items, "item", "items"), + (recipes, "recipe", "recipes"), + (loot, "loot table", "loot tables"), + (report.worldgen, "worldgen file", "worldgen files"), + (c.textures, "texture", "textures"), + ] { + if n > 0 { + check.ports.push(plural(n, one, many)); + } + } + if !report.blocks.is_empty() { + check + .ports + .push("blocks take the closest vanilla block's shape, hardness and sounds".into()); + } + let mut skipped = Vec::new(); + if !report.blocks_unknown.is_empty() { + skipped.push(plural( + report.blocks_unknown.len(), + "block with no vanilla block to copy (left out)", + "blocks with no vanilla block to copy (left out)", + )); + } + if !report.recipes_kept.is_empty() { + skipped.push(plural( + report.recipes_kept.len(), + "recipe of a type Horizon doesn't have", + "recipes of types Horizon doesn't have", + )); + } + if !report.unreadable.is_empty() { + skipped.push(plural( + report.unreadable.len(), + "file the game can't read", + "files the game can't read", + )); + } + let wg = &report.worldgen_types; + let dropped = wg.processors_dropped.values().sum::(); + if dropped > 0 { + skipped.push(plural( + dropped, + "structure processor written in Java", + "structure processors written in Java", + )); + } + let unsupported = wg.unsupported.values().sum::(); + if unsupported > 0 { + skipped.push(plural( + unsupported, + "worldgen file of a type written in Java", + "worldgen files of types written in Java", + )); + } + for (f, n) in &check.code.features { + let mut line = f.describe(*n); + if *f == classes::Feature::Events { + let names: Vec<&str> = check + .code + .events + .iter() + .take(3) + .map(String::as_str) + .collect(); + line.push_str(&format!(" ({}", names.join(", "))); + if check.code.events.len() > 3 { + line.push_str(", ..."); + } + line.push(')'); + } + skipped.push(line); + } + check.skipped = skipped; + let behaviour = check.code.behaviour(); + (check.verdict, check.summary) = if content == 0 { + let what = if behaviour || check.code.classes > 0 { + "everything it does is Java code, which Horizon doesn't run" + } else { + "it has no blocks, items, recipes or worldgen" + }; + (Verdict::No, format!("Nothing to port: {what}.")) + } else if check.skipped.is_empty() { + ( + Verdict::Works, + format!( + "Content only: {} port.", + check.ports[..check.ports.len().min(3)].join(", ") + ), + ) + } else { + ( + Verdict::Partly, + format!( + "{} port; not {}.", + check.ports[..check.ports.len().min(2)].join(", "), + check.skipped[..check.skipped.len().min(2)].join(", ") + ), + ) + }; + done(check) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::io::Write; + + fn jar(dir: &Path, files: &[(&str, &[u8])]) -> std::path::PathBuf { + let path = dir.join("m.jar"); + let mut zip = zip::ZipWriter::new(fs::File::create(&path).unwrap()); + for (name, bytes) in files { + zip.start_file(*name, zip::write::SimpleFileOptions::default()) + .unwrap(); + zip.write_all(bytes).unwrap(); + } + zip.finish().unwrap(); + path + } + + const TOML: &[u8] = br#"modLoader = "javafml" +license = "MIT" +[[mods]] +modId = "ruby" +version = "1.0" +displayName = "Ruby" +"#; + + fn temp(name: &str) -> std::path::PathBuf { + let dir = + std::env::temp_dir().join(format!("port-pack-check-{name}-{}", std::process::id())); + let _ = fs::remove_dir_all(&dir); + fs::create_dir_all(&dir).unwrap(); + dir + } + + #[test] + fn content_only_works_and_code_makes_it_partly() { + let dir = temp("content"); + let blockstate = br#"{"variants": {"": {"model": "ruby:block/ruby_block"}}}"#; + let recipe = br#"{"type": "minecraft:crafting_shapeless", "ingredients": ["minecraft:red_dye"], "result": {"id": "ruby:ruby"}}"#; + let plain = [ + ("META-INF/neoforge.mods.toml", TOML), + ("assets/ruby/blockstates/ruby_block.json", blockstate), + ("assets/ruby/models/item/ruby.json", b"{}"), + ("data/ruby/recipe/ruby.json", recipe), + ]; + let c = check(&jar(&dir, &plain), &dir.join("out")).unwrap(); + assert_eq!(c.verdict, Verdict::Works, "{c:?}"); + assert_eq!(c.loader, "neoforge"); + assert_eq!(&c.ports[..3], ["1 block", "1 item", "1 recipe"]); + assert!(dir.join("out/carry/horizon-mod.toml").is_file()); + assert!(dir.join("out/check.json").is_file()); + + let tile = crate::classes::tests_support::class_file( + "ruby/Tile", + "net/minecraft/world/level/block/entity/BlockEntity", + ); + let mut coded = plain.to_vec(); + coded.push(("ruby/Tile.class", &tile)); + let c = check(&jar(&dir, &coded), &dir.join("out")).unwrap(); + assert_eq!(c.verdict, Verdict::Partly); + assert!( + c.skipped[0].starts_with("1 block entity"), + "{:?}", + c.skipped + ); + fs::remove_dir_all(&dir).unwrap(); + } + + #[test] + fn code_only_and_old_layouts_dont_run() { + let dir = temp("code"); + let tile = crate::classes::tests_support::class_file("ruby/Mixin", "java/lang/Object"); + let c = check( + &jar( + &dir, + &[ + ("META-INF/neoforge.mods.toml", TOML), + ("ruby/A.class", &tile), + ], + ), + &dir.join("out"), + ) + .unwrap(); + assert_eq!(c.verdict, Verdict::No); + assert!(c.summary.starts_with("Nothing to port"), "{}", c.summary); + let c = check( + &jar( + &dir, + &[ + ("META-INF/mods.toml", TOML), + ("data/ruby/recipes/ruby.json", b"{}"), + ], + ), + &dir.join("out"), + ) + .unwrap(); + assert_eq!(c.verdict, Verdict::No); + assert!(c.summary.contains("1.20 or older")); + fs::remove_dir_all(&dir).unwrap(); + } +} diff --git a/crates/horizon-port-pack/src/classes.rs b/crates/horizon-port-pack/src/classes.rs new file mode 100644 index 000000000..d39c72e1a --- /dev/null +++ b/crates/horizon-port-pack/src/classes.rs @@ -0,0 +1,642 @@ +//! What a mod's Java code does, read from its class files without running +//! or decompiling them: each class's superclass, interfaces, declared +//! methods and the classes and annotations its constant pool names. The +//! mod's own classes are followed up to the first game class they extend, +//! and that class says what the code is: a block entity, a menu, an entity, +//! a block or item with behaviour of its own, a worldgen feature... +//! +//! Since 26.1 the game ships unobfuscated, and NeoForge has run on Mojang's +//! names since 1.20.5, so game classes and methods are read by name. Fabric +//! jars before 26.1 name them in intermediary (`net/minecraft/class_2248`) +//! and Forge's methods are SRG (`m_60503_`): a class extending such a game +//! class is counted as code of its own, whatever it overrides. +use serde::{Deserialize, Serialize}; +use std::collections::{BTreeMap, BTreeSet, HashMap}; + +/// One class file, as much as this module reads. +#[derive(Debug, Default, Clone, PartialEq)] +pub struct Class { + pub name: String, + pub superclass: String, + pub interfaces: Vec, + /// Declared methods, by name (constructors and lambdas left out). + pub methods: Vec, + /// Every class the constant pool names (`net/minecraft/...`). + pub refs: BTreeSet, + /// Annotation and descriptor strings (`Lorg/spongepowered/asm/mixin/Mixin;`). + pub mixin: bool, +} + +/// Reads a class file's header, constant pool and declared methods. +pub fn parse(bytes: &[u8]) -> Option { + let mut r = Reader { bytes, at: 0 }; + if r.u4()? != 0xCAFE_BABE { + return None; + } + r.skip(4)?; // minor, major + let count = r.u2()? as usize; + let mut utf8: HashMap = HashMap::new(); + let mut class_of: HashMap = HashMap::new(); + let mut i = 1; + while i < count { + let tag = r.u1()?; + match tag { + 1 => { + let len = r.u2()? as usize; + let s = r.take(len)?; + utf8.insert(i, String::from_utf8_lossy(s).into_owned()); + } + 7 => { + class_of.insert(i, r.u2()? as usize); + } + 8 | 16 | 19 | 20 => r.skip(2)?, + 3 | 4 | 9..=12 | 17 | 18 => r.skip(4)?, + 15 => r.skip(3)?, + 5 | 6 => { + r.skip(8)?; + i += 1; + } + _ => return None, + } + i += 1; + } + let class_name = |idx: usize| -> String { + class_of + .get(&idx) + .and_then(|u| utf8.get(u)) + .cloned() + .unwrap_or_default() + }; + r.skip(2)?; // access flags + let name = class_name(r.u2()? as usize); + let superclass = class_name(r.u2()? as usize); + let n = r.u2()? as usize; + let mut interfaces = Vec::with_capacity(n); + for _ in 0..n { + interfaces.push(class_name(r.u2()? as usize)); + } + // Fields, then methods: flags, name, descriptor, attributes. + let mut methods = Vec::new(); + for member in 0..2 { + let n = r.u2()?; + for _ in 0..n { + r.skip(2)?; + let name = r.u2()? as usize; + r.skip(2)?; + let attributes = r.u2()?; + for _ in 0..attributes { + r.skip(2)?; + let len = r.u4()? as usize; + r.skip(len)?; + } + if member == 1 + && let Some(m) = utf8.get(&name) + && !m.starts_with('<') + && !m.starts_with("lambda$") + { + methods.push(m.clone()); + } + } + } + let refs = class_of + .values() + .filter_map(|u| utf8.get(u)) + .filter(|s| !s.starts_with('[')) + .cloned() + .collect(); + let mixin = utf8 + .values() + .any(|s| s == "Lorg/spongepowered/asm/mixin/Mixin;"); + Some(Class { + name, + superclass, + interfaces, + methods, + refs, + mixin, + }) +} + +struct Reader<'a> { + bytes: &'a [u8], + at: usize, +} + +impl<'a> Reader<'a> { + fn take(&mut self, n: usize) -> Option<&'a [u8]> { + let s = self.bytes.get(self.at..self.at.checked_add(n)?)?; + self.at += n; + Some(s) + } + fn skip(&mut self, n: usize) -> Option<()> { + self.take(n).map(drop) + } + fn u1(&mut self) -> Option { + Some(self.take(1)?[0]) + } + fn u2(&mut self) -> Option { + let b = self.take(2)?; + Some(u16::from_be_bytes([b[0], b[1]])) + } + fn u4(&mut self) -> Option { + let b = self.take(4)?; + Some(u32::from_be_bytes([b[0], b[1], b[2], b[3]])) + } +} + +/// What a class does, as the port sees it. +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum Feature { + /// A block with behaviour of its own (use, ticks, collisions...). + BlockBehaviour, + /// A block with a shape of its own: it ports with its template's. + BlockShape, + /// An item with behaviour of its own (use, on hit...). + ItemBehaviour, + BlockEntity, + Menu, + Entity, + Screen, + Renderer, + /// A worldgen feature, placement or structure type written in Java. + Worldgen, + /// A recipe type (its recipes don't craft) or ingredient type. + Recipe, + Effect, + Enchantment, + Particle, + Command, + Network, + /// Reacts to game events (NeoForge's bus, Fabric's callbacks). + Events, + /// A Mixin: a patch to the game's own code. + Mixin, + /// Extends a game class under obfuscated names (Fabric before 26.1, + /// Forge's methods): what it does can't be read. + Unread, +} + +impl Feature { + /// How the coverage line names `n` of them. + pub fn describe(self, n: usize) -> String { + let (one, many) = match self { + Feature::BlockBehaviour => ( + "block with behaviour of its own (use, ticks, collisions)", + "blocks with behaviour of their own (use, ticks, collisions)", + ), + Feature::BlockShape => ( + "block with a shape of its own: it ports with its template's", + "blocks with shapes of their own: they port with their templates'", + ), + Feature::ItemBehaviour => ( + "item with behaviour of its own (use, on hit)", + "items with behaviour of their own (use, on hit)", + ), + Feature::BlockEntity => ( + "block entity (machine, container or animated block)", + "block entities (machines, containers, animated blocks)", + ), + Feature::Menu => ("menu (a container screen)", "menus (container screens)"), + Feature::Entity => ("mob or entity", "mobs or entities"), + Feature::Screen => ("screen of its own", "screens of its own"), + Feature::Renderer => ("renderer or model in code", "renderers or models in code"), + Feature::Worldgen => ( + "worldgen feature or structure type in code", + "worldgen features or structure types in code", + ), + Feature::Recipe => ( + "recipe type of its own (its recipes don't craft)", + "recipe types of its own (their recipes don't craft)", + ), + Feature::Effect => ("status effect", "status effects"), + Feature::Enchantment => ("enchantment effect in code", "enchantment effects in code"), + Feature::Particle => ("particle type", "particle types"), + Feature::Command => ("command", "commands"), + Feature::Network => ("network message", "network messages"), + Feature::Events => ("game event it reacts to", "game events it reacts to"), + Feature::Mixin => ( + "Mixin (a patch to the game's code)", + "Mixins (patches to the game's code)", + ), + Feature::Unread => ( + "class extending the game under obfuscated names", + "classes extending the game under obfuscated names", + ), + }; + format!("{n} {}", if n == 1 { one } else { many }) + } +} + +/// Block methods that only say how it looks or is placed: the template's +/// do the same for the states a blockstate file lists. +const BLOCK_LOOKS: [&str; 18] = [ + "codec", + "createBlockStateDefinition", + "getStateForPlacement", + "rotate", + "mirror", + "getRenderShape", + "skipRendering", + "getShadeBrightness", + "propagatesSkylightDown", + "getCloneItemStack", + "isPathfindable", + "getFluidState", + "updateShape", + "canBeReplaced", + "getLightBlock", + "useShapeForLightOcclusion", + "getDescriptionId", + "animateTick", +]; +/// Block methods that only change its outline or collision. +const BLOCK_SHAPES: [&str; 6] = [ + "getShape", + "getCollisionShape", + "getVisualShape", + "getOcclusionShape", + "getInteractionShape", + "getBlockSupportShape", +]; +/// Item methods that only say how it looks or is named. +const ITEM_LOOKS: [&str; 6] = [ + "appendHoverText", + "getName", + "isFoil", + "getDescriptionId", + "getBarColor", + "getBarWidth", +]; + +/// The game's own, loader and library classes: the first of these a mod's +/// class chain reaches is what it extends. +fn external(name: &str, own: &BTreeMap) -> bool { + !own.contains_key(name) +} + +fn obfuscated_method(m: &str) -> bool { + let digits = |s: &str| !s.is_empty() && s.bytes().all(|b| b.is_ascii_digit()); + m.strip_prefix("method_").is_some_and(digits) + || m.strip_prefix("m_") + .and_then(|s| s.strip_suffix('_')) + .is_some_and(digits) +} + +/// What a game class (Mojang's names) is, by its package and name. +fn kind_of_game_class(name: &str) -> Option { + let rest = name.strip_prefix("net/minecraft/")?; + if rest.starts_with("class_") { + return Some(Feature::Unread); + } + let (package, simple) = rest.rsplit_once('/').unwrap_or(("", rest)); + Some(match package { + "world/level/block/entity" => Feature::BlockEntity, + p if p.starts_with("world/level/block") => Feature::BlockBehaviour, + "world/inventory" if simple.ends_with("Menu") => Feature::Menu, + p if p.starts_with("world/entity") => Feature::Entity, + p if p.starts_with("world/item/crafting") => Feature::Recipe, + p if p.starts_with("world/item/enchantment") => Feature::Enchantment, + p if p.starts_with("world/item") => Feature::ItemBehaviour, + p if p.starts_with("world/level/levelgen") => Feature::Worldgen, + "world/effect" => Feature::Effect, + p if p.starts_with("client/gui/screens") || p.starts_with("client/gui/components") => { + Feature::Screen + } + p if p.starts_with("client/renderer") || p.starts_with("client/model") => Feature::Renderer, + p if p.starts_with("client/particle") || p.starts_with("core/particles") => { + Feature::Particle + } + _ => return None, + }) +} + +/// Events a content mod uses to register what it has: not behaviour. +fn registration_event(name: &str) -> bool { + [ + "BuildCreativeModeTabContentsEvent", + "RegisterEvent", + "Register", + "Lifecycle", + "ModConfigEvent", + "AddPackFindersEvent", + "ItemGroupEvents", + "ClientLifecycleEvents", + "ServerLifecycleEvents", + ] + .iter() + .any(|k| name.rsplit('/').next().unwrap_or(name).contains(k)) + || name.contains("/lifecycle/") + || name.contains("/registries/") + || name.contains("/registry/") +} + +/// The code census: how many of each feature, and examples. +#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)] +pub struct Code { + pub classes: usize, + pub features: BTreeMap, + /// The game events it subscribes to, by simple name. + pub events: BTreeSet, + /// Blocks and items with behaviour, and the methods they override. + pub examples: BTreeMap>, +} + +impl Code { + /// Whether it does anything beyond registering blocks and items. + pub fn behaviour(&self) -> bool { + self.features + .keys() + .any(|f| *f != Feature::BlockShape && *f != Feature::Particle) + } +} + +/// Reads every class of a jar. `classes` are the class files' bytes. +pub fn census<'a>(classes: impl IntoIterator) -> Code { + let own: BTreeMap = classes + .into_iter() + .filter_map(parse) + .map(|c| (c.name.clone(), c)) + .collect(); + let mut code = Code { + classes: own.len(), + ..Code::default() + }; + let mut count = |f: Feature| *code.features.entry(f).or_default() += 1; + for c in own.values() { + if c.mixin { + count(Feature::Mixin); + continue; + } + // The first class outside the jar the chain reaches, and the + // methods declared along the way. + let mut methods: BTreeSet<&str> = c.methods.iter().map(String::as_str).collect(); + let mut at = c; + let mut seen = 0; + while !external(&at.superclass, &own) && seen < 64 { + at = &own[&at.superclass]; + methods.extend(at.methods.iter().map(String::as_str)); + seen += 1; + } + let base = at.superclass.as_str(); + let kind = kind_of_game_class(base).or_else(|| { + c.interfaces.iter().find_map(|i| match i.as_str() { + "net/minecraft/network/protocol/common/custom/CustomPacketPayload" => { + Some(Feature::Network) + } + "net/minecraft/world/MenuProvider" => Some(Feature::Menu), + _ => None, + }) + }); + let Some(kind) = kind else { + continue; + }; + let feature = match kind { + Feature::BlockBehaviour => { + let behaviour: Vec<&str> = methods + .iter() + .copied() + .filter(|m| !BLOCK_LOOKS.contains(m) && !BLOCK_SHAPES.contains(m)) + .collect(); + if !behaviour.is_empty() { + code.examples.insert( + c.name.rsplit('/').next().unwrap_or("").to_owned(), + behaviour.iter().map(|m| m.to_string()).collect(), + ); + if behaviour.iter().any(|m| obfuscated_method(m)) { + Some(Feature::Unread) + } else { + Some(Feature::BlockBehaviour) + } + } else if methods.iter().any(|m| BLOCK_SHAPES.contains(m)) { + Some(Feature::BlockShape) + } else { + None + } + } + Feature::ItemBehaviour => { + let behaviour: Vec<&str> = methods + .iter() + .copied() + .filter(|m| !ITEM_LOOKS.contains(m)) + .collect(); + if behaviour.is_empty() { + None + } else { + code.examples.insert( + c.name.rsplit('/').next().unwrap_or("").to_owned(), + behaviour.iter().map(|m| m.to_string()).collect(), + ); + Some(Feature::ItemBehaviour) + } + } + // A class extending an obfuscated game class without declaring + // anything adds nothing to read. + Feature::Unread if methods.is_empty() => None, + k => Some(k), + }; + if let Some(f) = feature { + count(f); + } + } + for c in own.values() { + for r in &c.refs { + let event = (r.starts_with("net/neoforged/neoforge/event/") + || r.starts_with("net/minecraftforge/event/") + || r.starts_with("net/fabricmc/fabric/api/event/") + || r.starts_with("net/fabricmc/fabric/api/entity/event/")) + && !registration_event(r); + if event { + let simple = r.rsplit('/').next().unwrap_or(r); + code.events + .insert(simple.split('$').next().unwrap_or(simple).to_owned()); + } + if r.starts_with("com/mojang/brigadier/") + && !code.features.contains_key(&Feature::Command) + { + code.features.insert(Feature::Command, 1); + } + } + } + if !code.events.is_empty() { + code.features.insert(Feature::Events, code.events.len()); + } + code +} + +#[cfg(test)] +pub(crate) mod tests_support { + /// A class file with `name` extending `superclass`, declaring `methods` + /// and naming `refs` in its constant pool. + pub fn class_with( + name: &str, + superclass: &str, + methods: &[&str], + refs: &[&str], + mixin: bool, + ) -> Vec { + let mut pool: Vec> = Vec::new(); + let utf = |pool: &mut Vec>, s: &str| -> u16 { + let mut e = vec![1]; + e.extend((s.len() as u16).to_be_bytes()); + e.extend(s.as_bytes()); + pool.push(e); + pool.len() as u16 + }; + let class = |pool: &mut Vec>, s: &str| -> u16 { + let u = utf(pool, s); + let mut e = vec![7]; + e.extend(u.to_be_bytes()); + pool.push(e); + pool.len() as u16 + }; + let this = class(&mut pool, name); + let sup = class(&mut pool, superclass); + for r in refs { + class(&mut pool, r); + } + // A long takes two slots. + pool.push(vec![5, 0, 0, 0, 0, 0, 0, 0, 1]); + pool.push(Vec::new()); + if mixin { + utf(&mut pool, "Lorg/spongepowered/asm/mixin/Mixin;"); + } + let desc = utf(&mut pool, "()V"); + let names: Vec = methods.iter().map(|m| utf(&mut pool, m)).collect(); + let mut out = vec![0xCA, 0xFE, 0xBA, 0xBE, 0, 0, 0, 65]; + out.extend(((pool.len() + 1) as u16).to_be_bytes()); + for e in &pool { + out.extend(e); + } + out.extend([0, 0x21]); + out.extend(this.to_be_bytes()); + out.extend(sup.to_be_bytes()); + out.extend([0, 0]); // interfaces + out.extend([0, 0]); // fields + out.extend((names.len() as u16).to_be_bytes()); + for n in names { + out.extend([0, 1]); + out.extend(n.to_be_bytes()); + out.extend(desc.to_be_bytes()); + // One attribute, to be skipped. + out.extend([0, 1, 0, 1, 0, 0, 0, 2, 0, 0]); + } + out.extend([0, 0]); // class attributes + out + } + + /// A class extending `superclass` and declaring nothing. + pub fn class_file(name: &str, superclass: &str) -> Vec { + class_with(name, superclass, &[], &[], false) + } +} + +#[cfg(test)] +mod tests { + use super::tests_support::class_with as class_file; + use super::*; + + #[test] + fn reads_the_header_methods_and_pool() { + let bytes = class_file( + "a/Lamp", + "net/minecraft/world/level/block/Block", + &["", "useWithoutItem", "lambda$x$0"], + &["net/minecraft/world/item/Item"], + false, + ); + let c = parse(&bytes).unwrap(); + assert_eq!(c.name, "a/Lamp"); + assert_eq!(c.superclass, "net/minecraft/world/level/block/Block"); + assert_eq!(c.methods, ["useWithoutItem"]); + assert!(c.refs.contains("net/minecraft/world/item/Item")); + assert!(!c.mixin); + assert!(parse(&bytes[..bytes.len() - 3]).is_none()); + assert!(parse(b"not a class").is_none()); + } + + #[test] + fn follows_own_classes_to_the_game() { + let block = "net/minecraft/world/level/block/Block"; + let files = [ + // Looks only: a template covers it. + class_file("m/Plain", block, &["getStateForPlacement"], &[], false), + class_file("m/Round", block, &["getShape"], &[], false), + // Behaviour two levels down. + class_file("m/Base", block, &["randomTick"], &[], false), + class_file("m/Lamp", "m/Base", &[], &[], false), + class_file( + "m/Tile", + "net/minecraft/world/level/block/entity/BlockEntity", + &[], + &[], + false, + ), + class_file("m/Mix", "java/lang/Object", &[], &[], true), + class_file( + "m/Old", + "net/minecraft/class_2248", + &["method_9534"], + &[], + false, + ), + class_file( + "m/Events", + "java/lang/Object", + &[], + &[ + "net/neoforged/neoforge/event/entity/player/PlayerInteractEvent$RightClickBlock", + "net/neoforged/neoforge/event/BuildCreativeModeTabContentsEvent", + "net/neoforged/neoforge/registries/DeferredRegister", + ], + false, + ), + class_file( + "m/Gem", + "net/minecraft/world/item/Item", + &["appendHoverText"], + &[], + false, + ), + ]; + let code = census(files.iter().map(Vec::as_slice)); + assert_eq!(code.classes, 9); + let n = |f| code.features.get(&f).copied().unwrap_or(0); + assert_eq!(n(Feature::BlockBehaviour), 2, "{code:?}"); + assert_eq!(n(Feature::BlockShape), 1); + assert_eq!(n(Feature::BlockEntity), 1); + assert_eq!(n(Feature::Mixin), 1); + assert_eq!(n(Feature::Unread), 1); + assert_eq!(n(Feature::ItemBehaviour), 0); + assert_eq!( + code.events.iter().collect::>(), + ["PlayerInteractEvent"] + ); + assert!(code.behaviour()); + } + + #[test] + fn registration_only_is_not_behaviour() { + let files = [ + class_file( + "m/Mod", + "java/lang/Object", + &["register"], + &[ + "net/neoforged/neoforge/registries/DeferredRegister", + "net/minecraft/world/level/block/Block", + ], + false, + ), + class_file( + "m/Pillar", + "net/minecraft/world/level/block/RotatedPillarBlock", + &[], + &[], + false, + ), + ]; + let code = census(files.iter().map(Vec::as_slice)); + assert!(!code.behaviour(), "{code:?}"); + } +} diff --git a/crates/horizon-port-pack/src/lib.rs b/crates/horizon-port-pack/src/lib.rs index 08f9b273f..f729b6c52 100644 --- a/crates/horizon-port-pack/src/lib.rs +++ b/crates/horizon-port-pack/src/lib.rs @@ -6,6 +6,8 @@ //! jars they downloaded and what is carried over from them stay there and //! are never uploaded or committed. pub mod carry; +pub mod check; +pub mod classes; pub mod classify; pub mod fetch; pub mod import; diff --git a/crates/horizon-port-pack/src/main.rs b/crates/horizon-port-pack/src/main.rs index e766a442d..2598b47ed 100644 --- a/crates/horizon-port-pack/src/main.rs +++ b/crates/horizon-port-pack/src/main.rs @@ -19,6 +19,10 @@ //! - `report `: the coverage report, per mod ported, partial, not yet //! or not needed with the reason, in `/coverage.json` and //! `coverage.md`. +//! - `check `: one mod jar on its own, as the launcher's mod +//! browser installs it: carried over into `/carry/`, its classes +//! read, and the verdict (Works, Partly, Doesn't run) in +//! `/check.json`. //! - `port [--from ] [--resources ]`: every //! step for the portable mods, as the launcher's "Import modpack" runs it. //! @@ -30,7 +34,7 @@ use std::{ process::ExitCode, }; -const USAGE: &str = "usage: horizon-port-pack import | inventory [--offline] | fetch [--from ] [...] | carry [--resources ] [...] | report | port [--from ] [--resources ]"; +const USAGE: &str = "usage: horizon-port-pack import | inventory [--offline] | fetch [--from ] [...] | carry [--resources ] [...] | report | check | port [--from ] [--resources ]"; fn main() -> ExitCode { let mut args: Vec = std::env::args().skip(1).collect(); @@ -66,6 +70,17 @@ fn main() -> ExitCode { println!("{}", work.join("coverage.md").display()); Ok(()) } + Some("check") => { + let c = horizon_port_pack::check::check(path(1)?, path(2)?)?; + println!("{}: {}. {}", c.mod_id, c.verdict.label(), c.summary); + for p in &c.ports { + println!(" ports: {p}"); + } + for s in &c.skipped { + println!(" skipped: {s}"); + } + Ok(()) + } Some("port") => { let work = path(2)?; pipeline::import(path(1)?, work, log)?; diff --git a/docs/modpacks.md b/docs/modpacks.md index 7d254beca..d062eb941 100644 --- a/docs/modpacks.md +++ b/docs/modpacks.md @@ -302,6 +302,22 @@ cargo run -p horizon-port-pack -- report ~/.horizon/ports/pack # coverage cargo run -p horizon-port-pack -- port ~/Downloads/pack.zip ~/.horizon/ports/pack [--from ] [--resources ] ``` +`check ` takes one mod jar on its own, as the launcher's mod browser installs it +([mod-browser.md](mod-browser.md)): it carries the jar into `/carry/` and reads its class +files (constant pool, superclass, declared methods; no decompiling) for what the carry can't +take, then writes the verdict to `/check.json`. **Works**: content only, and the code just +registers it. **Partly**: the content ports, and the code features listed don't (block entities, +menus, entities, blocks and items with behaviour of their own, recipe types, Mixins, game events). +**Doesn't run**: nothing to port, or a jar made for 1.20 or older. Each mod class is followed up to +the first game class it extends; a block subclass that only overrides how it looks or is placed +(`getStateForPlacement`, `rotate`...) is registration. Game classes are read by Mojang's names +(26.1 and later, and NeoForge since 1.20.5); older Fabric jars name them in intermediary, so a +class extending one counts as code that can't be read. On ten 26.3 jars (2026-10-22): Macaw's +Fences, Windows and Furniture, Ecologics, Compressed Blocks, More Decorative Blocks and Moog's +Nether Structures are Partly; Sodium and GeckoLib don't run (Mixins, renderers, nothing to port); +Fabric API doesn't run (its id has a dash, which content mods can't use). A check takes 0.2 s to +3 s, and 24 s for Compressed Blocks (18 MB, 9,360 recipes, writing the carry dominates). + `fetch` gets each jar the player's way, never from us: copied from a folder they have (matched by SHA-1, or through a CurseForge app instance's `minecraftinstance.json`), else downloaded from the link the pack itself gives (a `.mrpack`'s, on the format's hosts only) or, for a CurseForge From 4a350bb0a03c64b34245ddd5e3f526de305988f2 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:57:24 +0200 Subject: [PATCH 02/11] port-pack carry: leave out recipes for items it didn't declare A recipe whose result is one of the mod's own items that the carry didn't declare (its block had no vanilla template) loaded and crafted air. Vanilla refuses a recipe for an unknown item; the carry now moves such recipes to outside/ and lists them (recipes_unregistered), and check says "N recipes for left-out blocks". Found by the mod browser's end-to-end run on Macaw's Fences. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-port-pack/src/carry.rs | 89 +++++++++++++++++++++++++++ crates/horizon-port-pack/src/check.rs | 47 +++++++++++++- 2 files changed, 135 insertions(+), 1 deletion(-) diff --git a/crates/horizon-port-pack/src/carry.rs b/crates/horizon-port-pack/src/carry.rs index e6056e519..c1fe2b32a 100644 --- a/crates/horizon-port-pack/src/carry.rs +++ b/crates/horizon-port-pack/src/carry.rs @@ -37,6 +37,10 @@ pub struct Report { pub worldgen: usize, /// Recipes left as they were: custom types, NeoForge conditions. pub recipes_kept: Vec, + /// Recipes whose result is one of the mod's own items that isn't declared (its block was + /// left out), moved to `outside/`: vanilla refuses a recipe for an unknown item, and the + /// game would craft air. + pub recipes_unregistered: Vec, pub item_definitions_added: usize, /// Templates and properties came from the NeoForge oracle's facts, not /// from guesses. @@ -1083,6 +1087,9 @@ pub fn carry( report.items += 1; } } + let owned: Vec = owned.iter().map(|n| n.to_string()).collect(); + let owned: Vec<&str> = owned.iter().map(String::as_str).collect(); + unregistered_recipes(&inside, &outside, id, &owned, &mut report)?; let q = |s: &str| toml::Value::String(s.to_owned()).to_string(); let name = if info.name.is_empty() { id } else { &info.name }; let mut manifest = format!( @@ -1109,6 +1116,88 @@ pub fn carry( Ok(report) } +/// The item a recipe makes: `result.id` (1.21 and later), `result.item` or a bare id. +fn recipe_result(recipe: &Value) -> Option<&str> { + let r = &recipe["result"]; + r.as_str() + .or_else(|| r["id"].as_str()) + .or_else(|| r["item"].as_str()) +} + +/// Moves the recipes whose result is an undeclared item of the mod's own namespaces from +/// `inside` to `outside` ([`Report::recipes_unregistered`]). The declared items are the blocks +/// with their item and the items under `data//horizon/`. +fn unregistered_recipes( + inside: &Path, + outside: &Path, + id: &str, + owned: &[&str], + report: &mut Report, +) -> Result<(), String> { + let decl = inside.join(format!("data/{id}/horizon")); + let mut declared = BTreeSet::new(); + for kind in ["block", "item"] { + for f in fs::read_dir(decl.join(kind)) + .into_iter() + .flatten() + .flatten() + { + let path = f.path(); + let Some(name) = path.file_stem().and_then(|n| n.to_str()) else { + continue; + }; + let has_item = kind == "item" + || fs::read(&path) + .ok() + .and_then(|b| serde_json::from_slice::(&b).ok()) + .is_none_or(|v| v["item"] != json!(false)); + if has_item { + declared.insert(format!("{id}:{name}")); + } + } + } + for ns in owned { + let dir = inside.join(format!("data/{ns}/recipe")); + let mut stack = vec![dir.clone()]; + while let Some(d) = stack.pop() { + for f in fs::read_dir(&d).into_iter().flatten().flatten() { + let path = f.path(); + if path.is_dir() { + stack.push(path); + continue; + } + let Some(recipe) = fs::read(&path) + .ok() + .and_then(|b| serde_json::from_slice::(&b).ok()) + else { + continue; + }; + let Some(result) = recipe_result(&recipe) else { + continue; + }; + let unknown = result + .split_once(':') + .is_some_and(|(n, _)| owned.contains(&n) && !declared.contains(result)); + if !unknown { + continue; + } + let rel = path.strip_prefix(inside).map_err(|e| e.to_string())?; + let to = outside.join(rel); + if let Some(parent) = to.parent() { + fs::create_dir_all(parent).map_err(|e| format!("{}: {e}", parent.display()))?; + } + fs::rename(&path, &to).map_err(|e| format!("{}: {e}", to.display()))?; + report.copied = report.copied.saturating_sub(1); + report + .recipes_unregistered + .push(rel.to_string_lossy().into_owned()); + } + } + } + report.recipes_unregistered.sort(); + Ok(()) +} + /// The tags a recipe's ingredients name (`#ns:path` or `{"tag": ...}`). fn tags_used(v: &Value, out: &mut BTreeSet) { match v { diff --git a/crates/horizon-port-pack/src/check.rs b/crates/horizon-port-pack/src/check.rs index 16b360ab6..68331dbc4 100644 --- a/crates/horizon-port-pack/src/check.rs +++ b/crates/horizon-port-pack/src/check.rs @@ -156,7 +156,9 @@ pub fn check(jar_path: &Path, out: &Path) -> Result { .chain(report.namespaces.iter().cloned()) .collect(); let c = &info.census; - let recipes = report.recipes_upgraded; + let recipes = report + .recipes_upgraded + .saturating_sub(report.recipes_unregistered.len()); let loot = c.loot_tables + report.loot_tables_added; let content = report.blocks.len() + report.items + recipes + loot + report.worldgen + c.structures; @@ -185,6 +187,13 @@ pub fn check(jar_path: &Path, out: &Path) -> Result { "blocks with no vanilla block to copy (left out)", )); } + if !report.recipes_unregistered.is_empty() { + skipped.push(plural( + report.recipes_unregistered.len(), + "recipe for a left-out block", + "recipes for left-out blocks", + )); + } if !report.recipes_kept.is_empty() { skipped.push(plural( report.recipes_kept.len(), @@ -331,6 +340,42 @@ displayName = "Ruby" fs::remove_dir_all(&dir).unwrap(); } + #[test] + fn recipes_for_left_out_blocks_are_left_out_too() { + let dir = temp("left-out"); + let odd = br#"{"variants": {"wobble=1": {"model": "ruby:block/odd"}}}"#; + let plain = br#"{"variants": {"": {"model": "ruby:block/ruby_block"}}}"#; + let recipe = |result: &str| { + format!( + r#"{{"type": "minecraft:crafting_shapeless", "ingredients": ["minecraft:red_dye"], "result": {{"id": "{result}"}}}}"# + ) + }; + let (odd_recipe, block_recipe) = (recipe("ruby:odd"), recipe("ruby:ruby_block")); + let files = [ + ("META-INF/neoforge.mods.toml", TOML), + ("assets/ruby/blockstates/odd.json", odd.as_slice()), + ("assets/ruby/blockstates/ruby_block.json", plain.as_slice()), + ("data/ruby/recipe/odd.json", odd_recipe.as_bytes()), + ("data/ruby/recipe/ruby_block.json", block_recipe.as_bytes()), + ]; + let c = check(&jar(&dir, &files), &dir.join("out")).unwrap(); + assert_eq!(c.verdict, Verdict::Partly); + assert!(c.ports.contains(&"1 recipe".to_string()), "{:?}", c.ports); + assert!( + c.skipped + .contains(&"1 recipe for a left-out block".to_string()), + "{:?}", + c.skipped + ); + assert!(!dir.join("out/carry/data/ruby/recipe/odd.json").exists()); + assert!(dir.join("out/outside/data/ruby/recipe/odd.json").exists()); + assert!( + dir.join("out/carry/data/ruby/recipe/ruby_block.json") + .exists() + ); + fs::remove_dir_all(&dir).unwrap(); + } + #[test] fn code_only_and_old_layouts_dont_run() { let dir = temp("code"); From 35305605819cece46b2c53bdc94cc9de5151bd80 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:28:12 +0200 Subject: [PATCH 03/11] Launcher: Java mods in the mod browser, checked before install; Modpacks moves into Mods The Mods page gets a Mods tab: Fabric, NeoForge, Forge and Quilt mods for 26.3 (1.21 and later with Show everything). Each result's jar is downloaded (allowed CDNs, checksum), checked with horizon-port-pack (Works, Partly, Doesn't run, with what ports and what doesn't) and the answer cached per file. Install copies the carry into horizon-mods// and turns it on in horizon-mods.txt, with required mods that port; libraries with nothing to port are listed as not ported; id or namespace clashes are refused. Remove deletes it and turns it off. The Modpacks page becomes the Mods page's Modpacks tab: imported packs first, then the search. CLI: mods search --kind mod, mods check . Co-Authored-By: Claude Opus 5.5 --- docs/mod-browser.md | 59 +++- launcher/src/browser.rs | 70 ++++- launcher/src/browser/curseforge.rs | 3 +- launcher/src/browser/installed.rs | 43 ++- launcher/src/browser/installed/tests.rs | 2 +- launcher/src/browser/java.rs | 340 ++++++++++++++++++++++++ launcher/src/browser/modrinth.rs | 10 + launcher/src/browser/tests.rs | 55 +++- launcher/src/cli.rs | 70 ++++- launcher/src/main.rs | 5 +- launcher/src/modpacks_page.rs | 33 +-- launcher/src/mods_page.rs | 139 +++++++++- website/docs/modpacks.md | 2 +- website/docs/mods.md | 12 +- 14 files changed, 760 insertions(+), 83 deletions(-) create mode 100644 launcher/src/browser/java.rs diff --git a/docs/mod-browser.md b/docs/mod-browser.md index 115877075..bc7a80b16 100644 --- a/docs/mod-browser.md +++ b/docs/mod-browser.md @@ -1,25 +1,56 @@ # The launcher's mod browser -Search Modrinth (and CurseForge, once the launcher has a key) for packs Horizon loads, see how well each one runs, -and install it with its checksum checked. This is item 16 of the roadmap and issue #577. The code is in +Search Modrinth (and CurseForge, once the launcher has a key) for Java mods, modpacks, resource packs and shader +packs, see how well each one runs before installing it, and install it with its checksum checked. This is item 16 of the roadmap and issue #577. The code is in `launcher/src/browser.rs` and `launcher/src/browser/`. ## What it offers -Only what Horizon actually loads is offered. Java mod jars (Fabric, NeoForge, Forge) don't run in Horizon -([mods.md](mods.md), [research/java-mod-compat.md](research/java-mod-compat.md)), so they are never searched. -Horizon `.hmod` mods aren't on Modrinth or CurseForge yet (Modrinth has no `horizon` loader). +Horizon doesn't run Java code ([research/java-mod-compat.md](research/java-mod-compat.md)). It ports what a Java mod +carries as data (blocks, items, recipes, loot tables, tags, worldgen, textures, names) with `horizon-port-pack`, and +says before install which of the mod's features that leaves out. Horizon `.hmod` mods aren't on Modrinth or CurseForge +yet (Modrinth has no `horizon` loader). | Kind | Goes to | How well it runs | |---|---|---| +| Java mods (Fabric, NeoForge, Forge, Quilt) | ported into `horizon-mods//` and turned on | Checked from its jar before install: **Works**, **Partly** or **Doesn't run** ([below](#java-mods)) | | Resource packs | `resourcepacks/` beside `options.txt` | **Works** when made for 26.3 ([resource-packs.md](resource-packs.md) lists what Horizon reads). Older packs are **Partly**: they load with vanilla's compatibility warning | | Shader packs (Iris, OptiFine) | `shaderpacks/` | **Partly**: translated to WGSL, the full-screen passes draw and the world and shadow passes don't yet ([shader-packs.md](shader-packs.md)). Packs for other loaders (Canvas, core-shader packs) **don't run** and can't be installed | | Modpacks (`.mrpack`, CurseForge zip) | the Modpacks page's import | **Partly**: Horizon runs the mods it can port, from the player's own downloads ([modpacks.md](modpacks.md)) | -By default a search shows only what runs: resource packs made for 26.3, shader packs for Iris or OptiFine, and every -modpack. **Show everything** lists the rest of the kind too, each marked with how well it runs; what doesn't run is +By default a search shows only what runs: Java mods made for 26.3, resource packs made for 26.3, shader packs for Iris +or OptiFine, and every modpack. **Show everything** lists the rest of the kind too, each marked with how well it runs; what doesn't run is never offered for install. +## Java mods + +Before a mod is offered for install, the launcher reads its jar. For each result it picks the version it would +install (the newest for 26.3; with **Show everything**, else the newest for 1.21 or later, the versions port-pack +upgrades from), downloads that file with the checks below, and runs `horizon-port-pack check` on it +([modpacks.md](modpacks.md#the-pipeline-horizon-port-pack)): the jar is carried over into a folder content mod and its +class files are read, not run, for what the carry can't take. Two checks run at once; the row says **Checking** until +its answer comes. + +- **Works**: content only. Its blocks, items, recipes, loot, tags and worldgen port, and its code only registers + them. Blocks take the closest vanilla block's shape, hardness and sounds (a block's numbers live in Java code; the + NeoForge oracle that reads them for modpacks is a developer tool). +- **Partly**: the content ports; the row lists what doesn't: blocks with no vanilla block to copy, recipes of the + mod's own types, block entities (machines, containers), menus, mobs and entities, blocks and items with behaviour + of their own, worldgen written in Java, Mixins, the game events it reacts to. +- **Doesn't run**: nothing to port (performance mods, libraries, everything in code), a jar made for 1.20 or older, + or a mod id with characters Horizon mods can't use (`fabric-api`). Never offered for install. + +The answer is kept in `mod-browser/checks-1//` under the game folder, with the carry when something +ports; the jar itself isn't kept. A file is checked once. On ten 26.3 jars a check took 0.2 to 3 s (24 s for an +18 MB jar with 9,360 recipes). + +**Install** copies the carry into `horizon-mods//`, turns it on in `horizon-mods.txt` (the game's Options > +Mods lists it) and records it in `mod-browser.json` with its verdict and what didn't port, which the Installed list +shows. Its required mods that port are installed with it; one with nothing to port (a library such as Cloth Config +or GeckoLib) is listed as not ported. A mod that shares its id or a namespace with an installed one is refused. The +game reads mod content when it starts: start it again after installing. **Remove** deletes the folder and turns it +off. + ## Checks on every download - The file must come from Modrinth's or CurseForge's file servers (`cdn.modrinth.com`, `edge.forgecdn.net`, @@ -32,10 +63,11 @@ never offered for install. ## The Mods page -The launcher's **Mods** page has the three kinds as tabs, a search field (Enter searches), **Show everything** and, +The launcher's **Mods** page has the four kinds as tabs (Mods, Modpacks, Resource packs, Shader packs), a search field (Enter searches), **Show everything** and, once CurseForge is set up, a Modrinth / CurseForge switch. Each result is an index row: its summary, author, downloads, source and licence, how well it runs (and why), **Page** (the project's page on its source) and -**Install** (**Import** for a modpack). Installed packs are listed above the results with their version, source, +**Install** (**Import** for a modpack). The Modpacks tab lists the imported packs first, with their coverage +report, **Port** and **Play** ([modpacks.md](modpacks.md)). Installed packs are listed above the results with their version, source, file and the checksum they were checked against, with **Update to …** when a newer version runs and **Remove** (asked twice). @@ -51,14 +83,15 @@ same name the player put there is never overwritten. Packs, as in vanilla and Iris. - **Update** installs the newer version and removes the old file. - **Remove** deletes the file and its entry. Packs installed as dependencies stay. -- **A modpack** is downloaded (checked the same way) into `ports/downloads/` and imported on the Modpacks page, - which says what of it Horizon runs and ports it. +- **A modpack** is downloaded (checked the same way) into `ports/downloads/` and imported; the Modpacks tab says + what of it Horizon runs and ports it. From a terminal (or an agent): ```sh -horizon-launcher mods search faithful # --kind resource|shader|modpack, --everything, --source -horizon-launcher mods install faithful-32x # id or slug; a modpack is imported +horizon-launcher mods search faithful # --kind mod|modpack|resource|shader, --everything, --source +horizon-launcher mods check macaws-fences-and-walls # a Java mod's verdict, what ports and what doesn't (--json) +horizon-launcher mods install faithful-32x # id or slug; a Java mod is ported, a modpack imported horizon-launcher mods list # --json horizon-launcher mods update [faithful-32x] [--check] horizon-launcher mods remove faithful-32x diff --git a/launcher/src/browser.rs b/launcher/src/browser.rs index 49ec1e696..b8248fd7c 100644 --- a/launcher/src/browser.rs +++ b/launcher/src/browser.rs @@ -2,12 +2,14 @@ //! Horizon loads, say how well each result runs, and download a file only after its checksum //! matches (docs/mod-browser.md). //! -//! Only what Horizon actually loads is offered: resource packs (`resourcepacks/`), Java shader -//! packs (`shaderpacks/`, Iris and OptiFine) and modpacks (imported and ported on the Modpacks -//! page). Java mod jars don't run in Horizon (docs/mods.md), so they are never searched. +//! Offered: Java mods (Fabric, NeoForge, Forge), whose content Horizon ports and whose jar is +//! checked before install ([`java`]), modpacks (imported and ported), resource packs +//! (`resourcepacks/`) and Java shader packs (`shaderpacks/`, Iris and OptiFine). Java code itself +//! doesn't run in Horizon (docs/mods.md). pub mod curseforge; pub mod download; pub mod installed; +pub mod java; pub mod modrinth; use serde::{Deserialize, Serialize}; @@ -23,25 +25,34 @@ pub enum Kind { ResourcePack, ShaderPack, Modpack, + /// A Java mod: its content is ported ([`java`]). + Mod, } impl Kind { - pub const ALL: [Kind; 3] = [Kind::ResourcePack, Kind::ShaderPack, Kind::Modpack]; + pub const ALL: [Kind; 4] = [ + Kind::Mod, + Kind::Modpack, + Kind::ResourcePack, + Kind::ShaderPack, + ]; pub fn label(self) -> &'static str { match self { + Kind::Mod => "Mods", Kind::ResourcePack => "Resource packs", Kind::ShaderPack => "Shader packs", Kind::Modpack => "Modpacks", } } - /// The folder beside `options.txt` the file goes in; a modpack is imported instead. + /// The folder beside `options.txt` the file goes in; a modpack is imported and a mod + /// ported instead. pub fn folder(self) -> Option<&'static str> { match self { Kind::ResourcePack => Some("resourcepacks"), Kind::ShaderPack => Some("shaderpacks"), - Kind::Modpack => None, + Kind::Modpack | Kind::Mod => None, } } } @@ -256,11 +267,34 @@ pub fn support(kind: Kind, game_versions: &[String], loaders: &[String]) -> Supp )) } } - Kind::Modpack => Support::Partial( - "Imported on the Modpacks page: Horizon runs the mods it can port, from your own \ - downloads." - .into(), - ), + Kind::Modpack => { + Support::Partial("Horizon runs the mods it can port, from your own downloads.".into()) + } + Kind::Mod => { + if !loaders.iter().any(|l| java::LOADERS.contains(&l.as_str())) { + Support::No("Not a Fabric, NeoForge or Forge mod.".into()) + } else if !game_versions.iter().any(|v| portable_version(v)) { + let newest = newest_release(game_versions).unwrap_or("an unknown version"); + Support::No(format!( + "Made for {newest}: Horizon ports mods made for 1.21 and later." + )) + } else { + Support::Partial( + "Not checked yet: the launcher reads its jar to say what of it ports.".into(), + ) + } + } + } +} + +/// A Minecraft version whose mods port-pack carries over: 1.21 and later (the data folders +/// took their current names in 1.21). +pub fn portable_version(v: &str) -> bool { + let parts: Vec = v.split('.').map_while(|p| p.parse().ok()).collect(); + match parts.as_slice() { + [1, minor, ..] => *minor >= 21 && v.split('.').count() == parts.len(), + [year, ..] => *year >= 26 && v.split('.').count() == parts.len(), + [] => false, } } @@ -286,6 +320,20 @@ pub fn pick(kind: Kind, versions: &[Version], everything: bool) -> Option<&Versi let mut usable = versions .iter() .filter(|v| v.blocked.is_none() && v.file().is_some()); + if kind == Kind::Mod { + let java = |v: &&Version| { + v.loaders + .iter() + .any(|l| java::LOADERS.contains(&l.as_str())) + }; + let made_for = |v: &&Version| v.game_versions.iter().any(|g| g == GAME); + let ported = |v: &&Version| v.game_versions.iter().any(|g| portable_version(g)); + return usable + .clone() + .filter(java) + .find(made_for) + .or_else(|| usable.filter(java).find(|v| everything && ported(v))); + } let runs = |v: &&Version| match support(kind, &v.game_versions, &v.loaders) { Support::Works => true, // A modpack's version is for its own Minecraft; any shader pack Horizon translates. diff --git a/launcher/src/browser/curseforge.rs b/launcher/src/browser/curseforge.rs index 347cf0d40..04e722bba 100644 --- a/launcher/src/browser/curseforge.rs +++ b/launcher/src/browser/curseforge.rs @@ -24,6 +24,7 @@ fn class(kind: Kind) -> u64 { Kind::ResourcePack => 12, Kind::ShaderPack => 6552, Kind::Modpack => 4471, + Kind::Mod => 6, } } @@ -73,7 +74,7 @@ pub(super) fn search_url(query: &Query) -> String { query.offset, query.limit.clamp(1, 50), ); - if !query.everything && query.kind == Kind::ResourcePack { + if !query.everything && matches!(query.kind, Kind::ResourcePack | Kind::Mod) { url.push_str(&format!("&gameVersion={GAME}")); } if !query.text.is_empty() { diff --git a/launcher/src/browser/installed.rs b/launcher/src/browser/installed.rs index d1aac1af5..1ed9b3cf8 100644 --- a/launcher/src/browser/installed.rs +++ b/launcher/src/browser/installed.rs @@ -1,7 +1,8 @@ //! What the browser installed into the game folder, and installing, updating and removing it. //! //! `mod-browser.json` beside `options.txt` lists each file the browser put in `resourcepacks/` -//! or `shaderpacks/`: where it came from, its version and its checksum. Files the player put +//! or `shaderpacks/`, and each Java mod it ported into `horizon-mods/`: where it came from, its +//! version and its checksum. Files the player put //! there themselves are never touched: the browser only replaces or removes what it lists. use super::{pick, safe_name, Kind, Project, Source, SourceId, Support, Version}; use serde::{Deserialize, Serialize}; @@ -29,6 +30,15 @@ pub struct Entry { /// Installed because another pack needs it. #[serde(default)] pub dependency: bool, + /// A Java mod's verdict when it was installed: `Works` or `Partly`. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub verdict: Option, + /// What of a Java mod didn't port. + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub skipped: Vec, + /// A Java mod's namespaces: two installed mods never share one. + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub namespaces: Vec, } #[derive(Debug, Default, Serialize, Deserialize)] @@ -78,9 +88,15 @@ pub struct Step { /// What installing `project` takes, dependencies first, checked before any byte is downloaded: /// every file runs in Horizon and has a version to install. pub fn plan(source: &dyn Source, project: &Project, everything: bool) -> Result, String> { + if project.kind == Kind::Modpack { + return Err(format!( + "{} is a modpack: it is imported, not installed.", + project.title + )); + } if project.kind.folder().is_none() { return Err(format!( - "{} is a modpack: it is imported on the Modpacks page.", + "{} is ported, not installed as a file.", project.title )); } @@ -122,10 +138,7 @@ fn add( ) })?; if needed.kind.folder().is_none() { - return Err(format!( - "{} needs the modpack {}.", - project.title, needed.title - )); + return Err(format!("{} needs {}.", project.title, needed.title)); } add(source, needed, everything, true, depth + 1, steps)?; } @@ -146,6 +159,9 @@ pub fn install( home: &Path, progress: &dyn Fn(&str, f32), ) -> Result, String> { + if project.kind == Kind::Mod { + return super::java::install(source, project, everything, home, progress); + } let steps = plan(source, project, everything)?; let mut installed = Installed::load(home); let mut done = Vec::new(); @@ -199,6 +215,9 @@ pub fn install( page: p.page.clone(), // Asked for by the player once: it stays theirs. dependency: step.dependency && old.as_ref().is_none_or(|o| o.dependency), + verdict: None, + skipped: Vec::new(), + namespaces: Vec::new(), }; debug_assert!(path.ends_with(name)); installed @@ -218,10 +237,14 @@ pub fn remove(home: &Path, name: &str) -> Result { .lookup(name) .cloned() .ok_or_else(|| format!("{name} wasn't installed by the browser."))?; - match fs::remove_file(home.join(&entry.file)) { - Ok(()) => {} - Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} - Err(e) => return Err(format!("{}: {e}", entry.file)), + if entry.kind == Kind::Mod { + super::java::remove(home, &entry)?; + } else { + match fs::remove_file(home.join(&entry.file)) { + Ok(()) => {} + Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} + Err(e) => return Err(format!("{}: {e}", entry.file)), + } } installed .entries diff --git a/launcher/src/browser/installed/tests.rs b/launcher/src/browser/installed/tests.rs index 33dc525ab..af55ec78d 100644 --- a/launcher/src/browser/installed/tests.rs +++ b/launcher/src/browser/installed/tests.rs @@ -227,7 +227,7 @@ fn nothing_is_installed_when_a_check_fails() { let pack = project("pack", Kind::Modpack); assert!(plan(&fake, &pack, false) .unwrap_err() - .contains("Modpacks page")); + .contains("imported, not installed")); fs::remove_dir_all(&home).unwrap(); } diff --git a/launcher/src/browser/java.rs b/launcher/src/browser/java.rs new file mode 100644 index 000000000..76ce43c95 --- /dev/null +++ b/launcher/src/browser/java.rs @@ -0,0 +1,340 @@ +//! Java mods (Fabric, NeoForge, Forge) from the browser (docs/mod-browser.md). Horizon doesn't +//! run their code; it ports what they carry (blocks, items, recipes, loot, tags, worldgen) with +//! `horizon-port-pack`. Before a mod is offered for install its jar is downloaded (checked as +//! every download is), carried over and its classes read (`horizon_port_pack::check`): the +//! verdict (Works, Partly, Doesn't run) and the lists of what ports and what doesn't come from +//! that, and are kept per file so a jar is checked once. +//! +//! Installing copies the carried-over folder mod into `horizon-mods//` beside +//! `options.txt` and turns it on in `horizon-mods.txt`, where the game's Options > Mods reads it. +use super::{installed::Entry, pick, FileRef, Kind, Project, Source, Support, Version}; +use horizon_port_pack::check::{self, Check, Verdict}; +use std::fs; +use std::path::{Path, PathBuf}; + +/// Where the checks are kept, under the game folder. Its name changes when the check finds +/// more, so older answers are checked again. +const CHECKS: &str = "mod-browser/checks-1"; +/// The game's mod folder and on-list, beside `options.txt` (`horizon_client::mod_list`). +pub const MODS_DIR: &str = "horizon-mods"; +const MODS_LIST: &str = "horizon-mods.txt"; +/// How deep required mods are followed. +const MAX_DEPTH: usize = 3; + +/// The loaders whose jars port-pack reads. +pub const LOADERS: [&str; 4] = ["neoforge", "fabric", "forge", "quilt"]; + +/// How well a checked mod runs. +pub fn support_of(check: &Check) -> Support { + match check.verdict { + Verdict::Works => Support::Works, + Verdict::Partly => Support::Partial(check.summary.clone()), + Verdict::No => Support::No(check.summary.clone()), + } +} + +fn checks(home: &Path) -> PathBuf { + home.join(CHECKS) +} + +/// The folder a file's check is kept in, by its checksum. +fn checked_dir(home: &Path, file: &FileRef) -> Option { + let (_, hash) = file.checksum()?; + let hash: String = hash + .chars() + .filter(char::is_ascii_hexdigit) + .take(40) + .collect(); + (hash.len() >= 32).then(|| checks(home).join(hash)) +} + +fn read_check(dir: &Path) -> Option { + serde_json::from_str(&fs::read_to_string(dir.join("check.json")).ok()?).ok() +} + +/// The check of `file` if it was made before. +pub fn cached(home: &Path, file: &FileRef) -> Option { + read_check(&checked_dir(home, file)?) +} + +/// Checks `file`: the cached answer, else its jar downloaded, carried over and read. The jar +/// itself isn't kept; the carry is, when something of it ports, for the install to copy. +pub fn check_file( + home: &Path, + source: &dyn Source, + file: &FileRef, + progress: &dyn Fn(f32), +) -> Result { + let dir = checked_dir(home, file).ok_or("Its source gives no checksum.")?; + if let Some(c) = read_check(&dir) { + return Ok(c); + } + let jar = source.fetch(file, &checks(home).join(".downloads"), progress)?; + let part = dir.with_extension("part"); + let _ = fs::remove_dir_all(&part); + let result = check::check(&jar, &part); + let _ = fs::remove_file(&jar); + let c = result.inspect_err(|_| { + let _ = fs::remove_dir_all(&part); + })?; + if c.verdict == Verdict::No { + for d in ["carry", "outside"] { + let _ = fs::remove_dir_all(part.join(d)); + } + } + let _ = fs::remove_dir_all(&dir); + fs::rename(&part, &dir).map_err(|e| format!("{}: {e}", dir.display()))?; + Ok(c) +} + +/// The version of `project` to install and its check. +pub fn check_project( + home: &Path, + source: &dyn Source, + project: &Project, + everything: bool, + progress: &dyn Fn(f32), +) -> Result<(Version, Check), String> { + let versions = source.versions(project)?; + let version = pick(Kind::Mod, &versions, everything) + .cloned() + .ok_or_else(|| match versions.iter().find_map(|v| v.blocked.clone()) { + Some(why) => format!("{} can't be downloaded from here: {why}", project.title), + None => format!( + "{} has no Fabric, NeoForge or Forge version Horizon can port.", + project.title + ), + })?; + let file = version.file().expect("pick keeps versions with a file"); + let check = check_file(home, source, file, progress)?; + Ok((version, check)) +} + +/// Turns mod `id` on (or off) in `horizon-mods.txt`, keeping the other lines. +pub fn set_enabled(home: &Path, id: &str, on: bool) -> Result<(), String> { + let path = home.join(MODS_LIST); + let text = fs::read_to_string(&path).unwrap_or_default(); + let key = format!("mod.{id}"); + let mut lines: Vec = text + .lines() + .filter(|l| l.split_once(':').is_none_or(|(k, _)| k.trim() != key)) + .map(str::to_owned) + .collect(); + if on { + lines.push(format!("{key}:true")); + } + let mut out = lines.join("\n"); + if !out.is_empty() { + out.push('\n'); + } + fs::write(&path, out).map_err(|e| format!("{}: {e}", path.display())) +} + +fn copy_dir(from: &Path, to: &Path) -> Result<(), String> { + fs::create_dir_all(to).map_err(|e| format!("{}: {e}", to.display()))?; + for entry in fs::read_dir(from).map_err(|e| format!("{}: {e}", from.display()))? { + let entry = entry.map_err(|e| e.to_string())?; + let (src, dst) = (entry.path(), to.join(entry.file_name())); + if entry.file_type().map_err(|e| e.to_string())?.is_dir() { + copy_dir(&src, &dst)?; + } else { + fs::copy(&src, &dst).map_err(|e| format!("{}: {e}", dst.display()))?; + } + } + Ok(()) +} + +/// Installs the Java mod `project`: checked, carried over into `horizon-mods//` and turned +/// on, with the mods it requires that port too. A required mod with nothing to port (a library) +/// is listed in what was skipped. Returns the entries installed, the asked-for one last. +pub fn install( + source: &dyn Source, + project: &Project, + everything: bool, + home: &Path, + progress: &dyn Fn(&str, f32), +) -> Result, String> { + let job = Job { + source, + everything, + home, + progress, + }; + let mut done = Vec::new(); + job.add(project, false, 0, &mut done)?; + Ok(done) +} + +/// One install: where from, where to, and how it reports. +struct Job<'a> { + source: &'a dyn Source, + everything: bool, + home: &'a Path, + progress: &'a dyn Fn(&str, f32), +} + +impl Job<'_> { + fn add( + &self, + project: &Project, + dependency: bool, + depth: usize, + done: &mut Vec, + ) -> Result<(), String> { + let Job { + source, + everything, + home, + progress, + } = *self; + if done.iter().any(|e| e.project == project.id) { + return Ok(()); + } + let (version, check) = check_project(home, source, project, everything, &|f| { + progress(&format!("Checking {}", project.title), f) + })?; + if check.verdict == Verdict::No { + return Err(format!( + "{} doesn't run in Horizon: {}", + project.title, check.summary + )); + } + let mut skipped = check.skipped.clone(); + for id in &version.required { + let Ok(needed) = source.project(id) else { + continue; + }; + if needed.kind != Kind::Mod || depth >= MAX_DEPTH { + continue; + } + match self.add(&needed, true, depth + 1, done) { + Ok(()) => {} + Err(_) if dependency => {} + Err(why) => skipped.push(format!("needs {}: {why}", needed.title)), + } + } + let file = version.file().expect("pick keeps versions with a file"); + let carry = checked_dir(home, file) + .expect("checked above") + .join("carry"); + let relative = format!("{MODS_DIR}/{}", check.mod_id); + let mut installed = super::installed::Installed::load(home); + let ours = |e: &Entry| e.source == project.source && e.project == project.id; + if let Some(other) = installed.entries.iter().find(|e| { + e.kind == Kind::Mod + && !ours(e) + && (e.file == relative || e.namespaces.iter().any(|n| check.namespaces.contains(n))) + }) { + return Err(format!( + "{} shares its id or namespace with {}, installed already: remove one first.", + project.title, other.title + )); + } + let target = home.join(&relative); + let old = installed.entries.iter().find(|e| ours(e)).cloned(); + if target.exists() && old.as_ref().is_none_or(|o| o.file != relative) { + return Err(format!( + "{relative} is already there and wasn't installed by the browser: move it away first." + )); + } + progress(&format!("Installing {}", project.title), 1.); + let part = home.join(MODS_DIR).join(format!(".{}.part", check.mod_id)); + let _ = fs::remove_dir_all(&part); + copy_dir(&carry, &part).inspect_err(|_| { + let _ = fs::remove_dir_all(&part); + })?; + if let Some(old) = &old { + let _ = fs::remove_dir_all(home.join(&old.file)); + if old.file != relative { + let _ = set_enabled(home, old.file.trim_start_matches("horizon-mods/"), false); + } + } + fs::rename(&part, &target).map_err(|e| format!("{}: {e}", target.display()))?; + set_enabled(home, &check.mod_id, true)?; + let (algo, hash) = file.checksum().expect("checked files have one"); + let entry = Entry { + source: project.source, + project: project.id.clone(), + slug: project.slug.clone(), + title: project.title.clone(), + kind: Kind::Mod, + version: version.id.clone(), + number: version.number.clone(), + file: relative, + checksum: format!("{algo}:{hash}"), + page: project.page.clone(), + dependency: dependency && old.as_ref().is_none_or(|o| o.dependency), + verdict: Some(check.verdict.label().to_owned()), + skipped, + namespaces: check.namespaces.clone(), + }; + installed.entries.retain(|e| !ours(e)); + installed.entries.push(entry.clone()); + installed.save(home)?; + done.push(entry); + Ok(()) + } +} + +/// Removes an installed mod's folder and turns it off. +pub fn remove(home: &Path, entry: &Entry) -> Result<(), String> { + let Some(id) = entry.file.strip_prefix("horizon-mods/") else { + return Err(format!("{} isn't in {MODS_DIR}/.", entry.file)); + }; + match fs::remove_dir_all(home.join(&entry.file)) { + Ok(()) => {} + Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} + Err(e) => return Err(format!("{}: {e}", entry.file)), + } + set_enabled(home, id, false) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn the_on_list_keeps_other_lines() { + let home = std::env::temp_dir().join(format!("horizon-java-mods-{}", std::process::id())); + let _ = fs::remove_dir_all(&home); + fs::create_dir_all(&home).unwrap(); + fs::write( + home.join(MODS_LIST), + "mod.coords_hud:true\nmod.ruby:false\n", + ) + .unwrap(); + set_enabled(&home, "ruby", true).unwrap(); + set_enabled(&home, "gem", true).unwrap(); + assert_eq!( + fs::read_to_string(home.join(MODS_LIST)).unwrap(), + "mod.coords_hud:true\nmod.ruby:true\nmod.gem:true\n" + ); + set_enabled(&home, "ruby", false).unwrap(); + set_enabled(&home, "gem", false).unwrap(); + assert_eq!( + fs::read_to_string(home.join(MODS_LIST)).unwrap(), + "mod.coords_hud:true\n" + ); + fs::remove_dir_all(&home).unwrap(); + } + + #[test] + fn checks_are_kept_by_checksum() { + let home = Path::new("/h"); + let file = |sha512: Option<&str>, sha1: Option<&str>| FileRef { + url: String::new(), + name: "a.jar".into(), + size: 1, + sha512: sha512.map(str::to_owned), + sha1: sha1.map(str::to_owned), + primary: true, + }; + let long = "ab".repeat(64); + assert_eq!( + checked_dir(home, &file(Some(&long), None)).unwrap(), + home.join(CHECKS).join("ab".repeat(20)) + ); + assert!(checked_dir(home, &file(None, Some("../../etc"))).is_none()); + assert!(checked_dir(home, &file(None, None)).is_none()); + } +} diff --git a/launcher/src/browser/modrinth.rs b/launcher/src/browser/modrinth.rs index e7e39628a..77b5475bb 100644 --- a/launcher/src/browser/modrinth.rs +++ b/launcher/src/browser/modrinth.rs @@ -13,6 +13,7 @@ fn project_type(kind: Kind) -> &'static str { Kind::ResourcePack => "resourcepack", Kind::ShaderPack => "shader", Kind::Modpack => "modpack", + Kind::Mod => "mod", } } @@ -32,8 +33,17 @@ pub(super) fn facets(query: &Query) -> String { all.push(vec!["categories:iris".into(), "categories:optifine".into()]) } Kind::Modpack => {} + Kind::Mod => all.push(vec![format!("versions:{GAME}")]), } } + if query.kind == Kind::Mod { + all.push( + super::java::LOADERS + .iter() + .map(|l| format!("categories:{l}")) + .collect(), + ); + } serde_json::to_string(&all).unwrap_or_default() } diff --git a/launcher/src/browser/tests.rs b/launcher/src/browser/tests.rs index ced342d13..9e75c55a1 100644 --- a/launcher/src/browser/tests.rs +++ b/launcher/src/browser/tests.rs @@ -140,6 +140,42 @@ fn modrinth_search_asks_for_what_runs() { modrinth::facets(&Query::new("", Kind::Modpack)), r#"[["project_type:modpack"]]"# ); + assert_eq!( + modrinth::facets(&Query::new("", Kind::Mod)), + r#"[["project_type:mod"],["versions:26.3"],["categories:neoforge","categories:fabric","categories:forge","categories:quilt"]]"# + ); +} + +#[test] +fn java_mods_are_checked_for_1_21_and_later() { + assert_eq!( + support(Kind::Mod, &v(&["1.20.1"]), &v(&["forge"])), + Support::No("Made for 1.20.1: Horizon ports mods made for 1.21 and later.".into()) + ); + assert!(!support(Kind::Mod, &v(&[GAME]), &v(&["rift"])).installable()); + assert!(matches!( + support(Kind::Mod, &v(&["1.21.1"]), &v(&["neoforge"])), + Support::Partial(_) + )); + for (version, ok) in [ + ("1.21", true), + ("1.21.11", true), + ("26.3", true), + ("1.20.6", false), + ("26.1-snapshot-2", false), + ("", false), + ] { + assert_eq!(portable_version(version), ok, "{version}"); + } + let versions = [ + version("snapshot", &["26.4-snapshot-1"], &["fabric"]), + version("shaders", &[GAME], &["iris"]), + version("now", &[GAME], &["neoforge"]), + version("older", &["1.21.1"], &["neoforge"]), + ]; + assert_eq!(pick(Kind::Mod, &versions, false).unwrap().id, "now"); + assert_eq!(pick(Kind::Mod, &versions[3..], false), None); + assert_eq!(pick(Kind::Mod, &versions[3..], true).unwrap().id, "older"); } #[test] @@ -157,9 +193,16 @@ fn modrinth_answers() { assert_eq!(p.page, "https://modrinth.com/resourcepack/fresh-animations"); assert_eq!(p.support(), Support::Works); assert_eq!(p.downloads, 51404735); - // A Java mod is never a result. - let jar = json!({"project_id": "AANobbMI", "project_type": "mod", "slug": "sodium"}); - assert!(modrinth::parse_hit(&jar).is_none()); + // A Java mod is a result, checked before it is offered. + let jar = json!({"project_id": "AANobbMI", "project_type": "mod", "slug": "sodium", + "versions": ["26.3"], "categories": ["fabric", "optimization"]}); + let sodium = modrinth::parse_hit(&jar).unwrap(); + assert_eq!(sodium.kind, Kind::Mod); + assert_eq!(sodium.page, "https://modrinth.com/mod/sodium"); + assert!(matches!(sodium.support(), Support::Partial(_))); + // Data packs and plugins are not. + let plugin = json!({"project_id": "x", "project_type": "plugin", "slug": "p"}); + assert!(modrinth::parse_hit(&plugin).is_none()); let project = json!({ "id": "HVnmMxH1", "project_type": "shader", "slug": "complementary-reimagined", @@ -243,7 +286,11 @@ fn curseforge_answers() { assert_eq!(p.loaders, ["iris", "optifine"]); assert_eq!(p.author, "CaptTatsu"); assert!(p.support().installable()); - assert!(curseforge::parse_mod(&json!({"id": 1, "classId": 6})).is_none()); + assert_eq!( + curseforge::parse_mod(&json!({"id": 1, "classId": 6})).map(|p| p.kind), + Some(Kind::Mod) + ); + assert!(curseforge::parse_mod(&json!({"id": 1, "classId": 17})).is_none()); let file = json!({ "id": 5000, "displayName": "BSL v10", "fileName": "BSL_v10.zip", "fileLength": 300, diff --git a/launcher/src/cli.rs b/launcher/src/cli.rs index e498fed2b..530d220dd 100644 --- a/launcher/src/cli.rs +++ b/launcher/src/cli.rs @@ -170,8 +170,18 @@ enum ModsCmd { #[arg(long, value_enum, default_value_t = SourceArg::Modrinth)] source: SourceArg, }, - /// Install a pack and what it needs, by its id or slug (checksums checked); a modpack is - /// imported on the Modpacks page + /// Say how well a Java mod runs in Horizon: its jar is downloaded (checksum checked), + /// carried over and read; the answer is kept + Check { + project: String, + /// Allow a version made for another Minecraft version (1.21 and later) + #[arg(long)] + everything: bool, + #[arg(long, value_enum, default_value_t = SourceArg::Modrinth)] + source: SourceArg, + }, + /// Install a pack and what it needs, by its id or slug (checksums checked); a Java mod is + /// ported into horizon-mods and turned on; a modpack is imported Install { project: String, /// Allow a version made for another Minecraft version @@ -193,6 +203,7 @@ enum ModsCmd { #[derive(Clone, Copy, clap::ValueEnum)] enum KindArg { + Mod, Resource, Shader, Modpack, @@ -950,6 +961,7 @@ fn mods(action: ModsCmd, as_json: bool) -> Result<(), String> { KindArg::Resource => Kind::ResourcePack, KindArg::Shader => Kind::ShaderPack, KindArg::Modpack => Kind::Modpack, + KindArg::Mod => Kind::Mod, }; let query = Query { everything, @@ -968,20 +980,67 @@ fn mods(action: ModsCmd, as_json: bool) -> Result<(), String> { } else { for p in &found { let support = p.support(); + let unchecked = p.kind == Kind::Mod && support.installable(); println!( "{} ({}) {} · {} downloads\n {}", p.title, p.slug, - support.label(), + if unchecked { + "Not checked" + } else { + support.label() + }, p.downloads, p.summary ); - if let Some(why) = support.reason() { + if unchecked { + println!(" horizon-launcher mods check {}", p.slug); + } else if let Some(why) = support.reason() { println!(" {why}"); } } } } + ModsCmd::Check { + project, + everything, + source, + } => { + let source = source.get()?; + let p = source.project(&project)?; + if p.kind != Kind::Mod { + return Err(format!("{} isn't a Java mod.", p.title)); + } + let (version, c) = horizon_launcher::browser::java::check_project( + &home, + &*source, + &p, + everything, + &progress(p.title.clone()), + )?; + if as_json { + let mut v = serde_json::to_value(&c).unwrap_or_default(); + v["version"] = version.number.clone().into(); + v["label"] = c.verdict.label().into(); + println!("{v}"); + return Ok(()); + } + println!( + "{} {} ({} {}): {}. {}", + p.title, + version.number, + c.loader, + c.mod_id, + c.verdict.label(), + c.summary + ); + for line in &c.ports { + println!(" ports: {line}"); + } + for line in &c.skipped { + println!(" doesn't: {line}"); + } + } ModsCmd::Install { project, everything, @@ -1009,6 +1068,9 @@ fn mods(action: ModsCmd, as_json: bool) -> Result<(), String> { } for e in done.iter().filter(|_| !as_json) { println!("{} {}: {} ({})", e.title, e.number, e.file, e.checksum); + for s in &e.skipped { + println!(" not ported: {s}"); + } } } ModsCmd::Update { project, check } => { diff --git a/launcher/src/main.rs b/launcher/src/main.rs index c1184fe10..bb1e8dceb 100644 --- a/launcher/src/main.rs +++ b/launcher/src/main.rs @@ -37,18 +37,16 @@ enum Page { Play, Versions, Mods, - Modpacks, Servers, Notes, Settings, } impl Page { - const ALL: [(Page, &'static str); 7] = [ + const ALL: [(Page, &'static str); 6] = [ (Page::Play, "Play"), (Page::Versions, "Versions"), (Page::Mods, "Mods"), - (Page::Modpacks, "Modpacks"), (Page::Servers, "Servers"), (Page::Notes, "Patch notes"), (Page::Settings, "Settings"), @@ -2788,7 +2786,6 @@ impl Render for Launcher { Page::Play => self.play_page(cx), Page::Versions => self.versions_page(cx), Page::Mods => self.mods_page(cx), - Page::Modpacks => self.modpacks_page(cx), Page::Servers => self.servers_page(cx), Page::Notes => self.notes_page(cx), Page::Settings => self.settings_page(cx), diff --git a/launcher/src/modpacks_page.rs b/launcher/src/modpacks_page.rs index 2398421c6..b776321b3 100644 --- a/launcher/src/modpacks_page.rs +++ b/launcher/src/modpacks_page.rs @@ -1,5 +1,5 @@ -//! The Modpacks page: import a pack, read its coverage report, port what this computer can and -//! play it as its own profile (`horizon_launcher::modpacks`). +//! Imported modpacks, on the Mods page's Modpacks tab: import a pack, read its coverage report, +//! port what this computer can and play it as its own profile (`horizon_launcher::modpacks`). use super::*; use horizon_launcher::modpacks::{self, Imported}; use horizon_port_pack::report::Status; @@ -124,7 +124,8 @@ impl Launcher { self.start_client(tag); } - pub(super) fn modpacks_page(&mut self, cx: &mut Context) -> Div { + /// The imported packs, above the Modpacks tab's search. + pub(super) fn imported_packs(&mut self, cx: &mut Context) -> Div { let t = self.theme; if self.packs_loaded.is_none() { self.packs = modpacks::list(); @@ -133,12 +134,6 @@ impl Launcher { } self.packs_loaded = Some(Instant::now()); } - let header = page_header( - "Modpacks", - "Bring a Java modpack: see what Horizon runs of it, port that on this computer from \ - your own downloads, and play it as its own profile. Nothing is uploaded or shared.", - &t, - ); let busy = self.pack_busy; let pick = |id: &'static str, label: &'static str, folder: bool| { let b = button(id, label, Kind::Secondary, &t); @@ -159,14 +154,11 @@ impl Launcher { .child(pick("import-file", "Import a pack file…", false)) .child(pick("import-folder", "Import an instance folder…", true)) .child(text(theme::CAPTION).text_color(t.muted).child( - ".mrpack (Modrinth), .zip (CurseForge), or a Prism, MultiMC or CurseForge instance", + ".mrpack (Modrinth), .zip (CurseForge), or a Prism, MultiMC or CurseForge instance. \ + Horizon ports what it can on this computer, from your own downloads, and plays \ + the pack as its own profile. Nothing is uploaded or shared.", )); - let mut page = div() - .flex() - .flex_col() - .gap(px(32.)) - .child(header) - .child(actions); + let mut page = div().flex().flex_col().gap(px(32.)).child(actions); if busy || !self.pack_log.is_empty() { page = page.child(self.pack_log_plate(&t)); } @@ -174,10 +166,11 @@ impl Launcher { page = page.child(notice(e.clone(), &t)); } if self.packs.is_empty() { - return page.child(centered( - "No modpack yet. Import one to see what Horizon can run of it.", - &t, - )); + return page.child( + text(theme::CAPTION) + .text_color(t.muted) + .child("No modpack imported yet: import one, or find one below."), + ); } if self.packs.len() > 1 { let mut list = div().flex().flex_col().border_t_1().border_color(t.rule); diff --git a/launcher/src/mods_page.rs b/launcher/src/mods_page.rs index 25a825c93..136ca7953 100644 --- a/launcher/src/mods_page.rs +++ b/launcher/src/mods_page.rs @@ -1,19 +1,25 @@ -//! The Mods page: search Modrinth (and CurseForge with a key) for resource packs, shader packs -//! and modpacks Horizon loads, see where each comes from and how well it runs, install it with -//! its checksum checked, and update or remove what was installed (`horizon_launcher::browser`). +//! The Mods page: search Modrinth (and CurseForge with a key) for Java mods, modpacks, resource +//! packs and shader packs, see where each comes from and how well it runs (a Java mod's jar is +//! checked first), install it with its checksum checked, and update or remove what was installed +//! (`horizon_launcher::browser`). Imported modpacks are listed on its Modpacks tab. use super::servers_page::{type_key, Key}; use super::*; use horizon_launcher::browser::{ - self, curseforge::CurseForge, installed, Kind as PackKind, Project, Query, SourceId, Support, + self, curseforge::CurseForge, installed, java, Kind as PackKind, Project, Query, SourceId, + Support, }; use horizon_launcher::modpacks; +use horizon_port_pack::check::Check; use std::collections::HashMap; -const KINDS: [(PackKind, &str); 3] = [ +const KINDS: [(PackKind, &str); 4] = [ + (PackKind::Mod, "Mods"), + (PackKind::Modpack, "Modpacks"), (PackKind::ResourcePack, "Resource packs"), (PackKind::ShaderPack, "Shader packs"), - (PackKind::Modpack, "Modpacks"), ]; +/// Java mods checked at once. +const CHECKING: usize = 2; const SOURCES: [(SourceId, &str); 2] = [ (SourceId::Modrinth, "Modrinth"), (SourceId::CurseForge, "CurseForge"), @@ -26,6 +32,15 @@ pub(super) enum Event { Progress(String, f32), Done(Result, bool), Updates(HashMap<(SourceId, String), String>), + /// A Java mod's check: its jar read, or why it couldn't be. + Checked((SourceId, String), Result, String>), +} + +/// Where a Java mod's check is. +pub(super) enum Checking { + Running, + Done(Box), + Failed(String), } pub(super) struct Mods { @@ -50,6 +65,8 @@ pub(super) struct Mods { message: Option>, updates: HashMap<(SourceId, String), String>, confirm_remove: Option, + /// Java mods' checks, by source and project. + checks: HashMap<(SourceId, String), Checking>, tx: Sender, rx: Receiver, } @@ -58,7 +75,7 @@ impl Mods { pub(super) fn new(cx: &mut App) -> Self { let (tx, rx) = channel(); Self { - kind: PackKind::ResourcePack, + kind: PackKind::Mod, source: SourceId::Modrinth, curseforge: CurseForge::from_env().is_some(), text: String::new(), @@ -77,6 +94,7 @@ impl Mods { message: None, updates: HashMap::new(), confirm_remove: None, + checks: HashMap::new(), tx, rx, } @@ -91,6 +109,19 @@ impl Mods { .iter() .find(|e| e.source == p.source && e.project == p.id) } + + /// How well a result runs: a Java mod's from its check once it has one. + fn support(&self, p: &Project) -> Support { + let metadata = p.support(); + if p.kind != PackKind::Mod || !metadata.installable() { + return metadata; + } + match self.checks.get(&(p.source, p.id.clone())) { + Some(Checking::Done(c)) => java::support_of(c), + Some(Checking::Failed(e)) => Support::Partial(format!("Not checked: {e}")), + _ => Support::Partial("Checking its jar…".into()), + } + } } /// `51404735` → `51.4M`. @@ -115,6 +146,7 @@ impl Launcher { /// Called with the launcher's other work: what the background sent. pub(super) fn mods_tick(&mut self) -> bool { let mut changed = false; + let mut to_check = Vec::new(); while let Ok(event) = self.mods.rx.try_recv() { changed = true; let m = &mut self.mods; @@ -126,8 +158,9 @@ impl Launcher { Ok(found) => { m.more = found.len() == PAGE; m.results.truncate(offset); - m.results.extend(found); + m.results.extend(found.iter().cloned()); m.error = None; + to_check.extend(found); } Err(e) => m.error = Some(e), } @@ -149,8 +182,16 @@ impl Launcher { } } Event::Updates(found) => m.updates = found, + Event::Checked(key, result) => { + let state = match result { + Ok(c) => Checking::Done(c), + Err(e) => Checking::Failed(e), + }; + m.checks.insert(key, state); + } } } + self.mods_check(to_check); changed } @@ -177,6 +218,43 @@ impl Launcher { }); } + /// Checks the Java mods among `found` that aren't checked yet, a few at a time, on threads: + /// a cached check answers at once, else the jar is downloaded and read. + fn mods_check(&mut self, found: Vec) { + let m = &mut self.mods; + let todo: Vec = found + .into_iter() + .filter(|p| p.kind == PackKind::Mod && p.support().installable()) + .filter(|p| !m.checks.contains_key(&(p.source, p.id.clone()))) + .collect(); + if todo.is_empty() { + return; + } + for p in &todo { + m.checks.insert((p.source, p.id.clone()), Checking::Running); + } + let (tx, everything) = (m.tx.clone(), m.everything); + let queue = std::sync::Mutex::new(todo.into_iter()); + std::thread::spawn(move || { + let home = backend::game_home(); + std::thread::scope(|s| { + for _ in 0..CHECKING { + s.spawn(|| { + while let Some(p) = queue.lock().ok().and_then(|mut q| q.next()) { + let result = browser::source(p.source) + .ok_or_else(|| format!("{} isn't set up.", p.source.label())) + .and_then(|source| { + java::check_project(&home, &*source, &p, everything, &|_| {}) + }) + .map(|(_, c)| Box::new(c)); + let _ = tx.send(Event::Checked((p.source, p.id), result)); + } + }); + } + }); + }); + } + /// Looks for newer versions of what the browser installed. fn mods_check_updates(&mut self) { let entries = self.mods.installed.clone(); @@ -233,7 +311,7 @@ impl Launcher { let log = |line: String| progress(&line, 1.); let pack = modpacks::import(&file, &log)?; return Ok(format!( - "{} is imported: open it on the Modpacks page to port it.", + "{} is imported: it is listed above, ready to port.", pack.title() )); } @@ -243,6 +321,19 @@ impl Launcher { .iter() .map(|e| format!("{} {}", e.title, e.number)) .collect(); + if p.kind == PackKind::Mod { + let skipped = done.last().map_or(0, |e| e.skipped.len()); + return Ok(format!( + "Ported {} into horizon-mods and turned on (Options > Mods in the \ + game).{}", + names.join(", "), + if skipped > 0 { + " What didn't port is listed under Installed." + } else { + "" + } + )); + } let turn_on = match p.kind { PackKind::ShaderPack => "Video Settings > Shader Packs", _ => "Options > Resource Packs", @@ -330,8 +421,10 @@ impl Launcher { } let header = page_header( "Mods", - "Resource packs, shader packs and modpacks that Horizon loads. Each file is checked \ - against its source's checksum before it reaches your game folder.", + "Java mods, modpacks, resource packs and shader packs. Horizon ports what a Java mod \ + carries (blocks, items, recipes, worldgen) and says before you install what runs. \ + Each file is checked against its source's checksum before it reaches your game \ + folder.", &t, ); let this = cx.entity().downgrade(); @@ -436,6 +529,9 @@ impl Launcher { Some(Err(e)) => page = page.child(notice(e.clone(), &t)), None => {} } + if self.mods.kind == PackKind::Modpack { + page = page.child(self.imported_packs(cx)); + } if let Some(list) = self.mods_installed_list(cx) { page = page.child(list); } @@ -512,7 +608,11 @@ impl Launcher { .flex() .flex_col() .child(format!( - "{} · from {}{} · {}", + "{}{} · from {}{} · {}", + e.verdict + .as_ref() + .map(|v| format!("{v} · ")) + .unwrap_or_default(), e.number, e.source.label(), if e.dependency { @@ -522,6 +622,11 @@ impl Launcher { }, e.file )) + .children(e.skipped.iter().map(|s| { + div() + .text_color(t.ink_soft) + .child(format!("Not ported: {s}")) + })) .child( div() .font_family(theme::MONO) @@ -564,7 +669,13 @@ impl Launcher { let busy = m.busy.as_ref().map(|b| b.0.as_str()); let mut list = div().flex().flex_col().border_t_1().border_color(t.rule); for (i, p) in m.results.iter().enumerate() { - let support = p.support(); + let support = m.support(p); + let checking = p.kind == PackKind::Mod + && matches!( + m.checks.get(&(p.source, p.id.clone())), + None | Some(Checking::Running) + ) + && p.support().installable(); let color = match support { Support::Works => t.success, Support::Partial(_) => t.accent, @@ -591,6 +702,8 @@ impl Launcher { right = right.child(caps("Installing").text_color(t.accent)); } else if m.entry(p).is_some() { right = right.child(caps("Installed").text_color(t.muted)); + } else if checking { + right = right.child(caps("Checking").text_color(t.muted)); } else if support.installable() { let label = if p.kind == PackKind::Modpack { "Import" diff --git a/website/docs/modpacks.md b/website/docs/modpacks.md index a4396392b..b78c59851 100644 --- a/website/docs/modpacks.md +++ b/website/docs/modpacks.md @@ -6,7 +6,7 @@ Java mod jars do not run in Horizon. Instead, the launcher reads a modpack, tell ## What the Modpacks page does -Open **Modpacks** in the launcher. +Open **Modpacks** in the launcher. (From the next launcher release, it is the **Modpacks** tab of the **Mods** page.) 1. **Import a pack.** Choose **Import a pack file…** for a Modrinth `.mrpack` or a CurseForge zip, or **Import an instance folder…** for a Prism, MultiMC or CurseForge instance. The **Mods** page can also find a Modrinth modpack and import it for you (see [Mods and data packs](/docs/mods#finding-resource-packs-shader-packs-and-modpacks)). 2. **Read the coverage report.** For every mod in the pack it says **Ported**, **Partial**, **Not yet** or **Not needed**, with the reason. You can filter the list by status. diff --git a/website/docs/mods.md b/website/docs/mods.md index 05dcaee2d..d3305fa69 100644 --- a/website/docs/mods.md +++ b/website/docs/mods.md @@ -36,7 +36,17 @@ cargo horizon-mod run The launcher's **Mods** page (launcher 0.17 and newer) searches [Modrinth](https://modrinth.com) for what Horizon can load: resource packs, Java shader packs and modpacks. Each result says how well it runs in Horizon (**Works**, **Partly**, or **Doesn't run**), and by default the page shows only what runs. **Install** downloads the file, checks it against the checksum Modrinth lists and refuses anything that does not match, then puts it in the right folder, with the packs it needs. **Update** and **Remove** are there too. A resource pack still has to be turned on in the game's Resource Packs screen, and a shader pack in Video, Shader Packs. A modpack is imported on the [Modpacks page](/docs/modpacks). -The page cannot search CurseForge yet. Java mod jars are never offered, because they do not run. The same things work from a terminal: `horizon-launcher mods search`, `install`, `update` and `remove`. +The page cannot search CurseForge yet. The same things work from a terminal: `horizon-launcher mods search`, `install`, `update` and `remove`. + +## Java mods from the Mods page (next launcher release) + +Java code does not run in Horizon, but much of what a mod adds is data: blocks, items, recipes, loot tables, tags, worldgen, textures. From the next launcher release, the Mods page also lists Fabric, NeoForge and Forge mods made for 26.3 and, before offering one, downloads its jar, checks it against Modrinth's checksum and reads it (without running it) to say what of it Horizon runs: + +- **Works**: the mod is only content, and all of it carries over. +- **Partly**: its content carries over; the row lists what does not, such as machines and containers, mobs, blocks or items with behaviour of their own, recipe types of its own, or Mixins. +- **Doesn't run**: there is nothing to carry over (performance mods, libraries, mods that are all code). It is not offered. + +**Install** brings the mod over into `horizon-mods` and turns it on; start the game again to load it. Blocks take the closest vanilla block's shape, hardness and sounds, so a carried-over block can break faster or slower than in Java. Most popular mods today are **Partly** or **Doesn't run**: the verdict is the honest answer for each one. ## Data packs From 240e76160bfcb0bcb1f2bfc537f6317aed14d7f0 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:56:19 +0200 Subject: [PATCH 04/11] Mod content: give mod items and blocks network ids, so they don't travel as air The game and its integrated server map items and blocks to network ids through the registries report (registries.json), which the content root left vanilla: a mod's item in a slot, given or picked up, reached the client as air. The content root now rewrites registries.json too, appending the new items and blocks after vanilla's ids in plan order. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/mod_content.rs | 34 ++++++++++++++++++- .../horizon-client/src/mod_content_tests.rs | 29 ++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/mod_content.rs b/crates/horizon-client/src/mod_content.rs index 0497f06b4..b3a836b3a 100644 --- a/crates/horizon-client/src/mod_content.rs +++ b/crates/horizon-client/src/mod_content.rs @@ -538,6 +538,37 @@ impl<'a> Plan<'a> { } } "items.json" => self.items(doc, true), + "registries.json" => { + // Network ids: the game and its server map items and blocks through + // this report, so new ones get the ids after vanilla's, in plan order. + let new_items = self.items.iter().map(|i| i.rename.to.clone()); + let new_blocks = self.blocks.iter().map(|b| b.decl.id.clone()); + for (registry, names) in [ + ("minecraft:item", new_items.collect::>()), + ("minecraft:block", new_blocks.collect()), + ] { + let Some(J::Obj(entries)) = + doc.get_mut(registry).and_then(|r| r.get_mut("entries")) + else { + return Err(format!("no {registry} entries").into()); + }; + let next = entries + .iter() + .filter_map(|(_, e)| match e { + J::Obj(o) => o.iter().find(|(k, _)| k == "protocol_id")?.1.as_u32(), + _ => None, + }) + .max() + .map_or(0, |m| m + 1); + for (i, name) in names.into_iter().enumerate() { + if entries.iter().any(|(k, _)| *k == name) { + continue; + } + let id = J::Num((next + i as u32).into()); + entries.push((name, J::Obj(vec![("protocol_id".into(), id)]))); + } + } + } "command-registries.json" => { self.names(doc.at("blocks").ok_or("no blocks")?, false); let new_items = || self.items.iter().map(|i| J::Str(i.rename.to.clone())); @@ -618,8 +649,9 @@ impl<'a> Plan<'a> { } /// The JSON and NBT tables rewritten for new blocks and items. -const JSON_TABLES: [&str; 13] = [ +const JSON_TABLES: [&str; 14] = [ "facts.json", + "registries.json", "gameplay-facts.json", "block-behaviours.json", "model-states.json", diff --git a/crates/horizon-client/src/mod_content_tests.rs b/crates/horizon-client/src/mod_content_tests.rs index 1c0907bde..3d38fae4d 100644 --- a/crates/horizon-client/src/mod_content_tests.rs +++ b/crates/horizon-client/src/mod_content_tests.rs @@ -148,6 +148,35 @@ fn content_root_loads_with_every_table() { new.default_state("ruby:ruby_glass"), Some(BlockStateId(n + 65)) ); + // Network ids: the new items and blocks follow vanilla's in the registries report, which + // the game and its server both read (a missing one travels as air). + let report = |root: &Path| { + horizon_protocol::registry::Builtin::load(&root.join("registries.json")).unwrap() + }; + let (vanilla_ids, ids) = (report(&base), report(&root)); + let item = |b: &horizon_protocol::registry::Builtin, name: &str| { + b.get("minecraft:item").and_then(|r| r.id(name)) + }; + let vanilla_items = (0..) + .take_while(|&i| vanilla_ids.get("minecraft:item").unwrap().name(i).is_some()) + .count() as i32; + assert_eq!( + item(&ids, "minecraft:stone"), + item(&vanilla_ids, "minecraft:stone") + ); + let ruby_item = item(&ids, "ruby:ruby_block").unwrap(); + assert!(ruby_item >= vanilla_items, "{ruby_item} < {vanilla_items}"); + assert_eq!( + ids.get("minecraft:item").unwrap().name(ruby_item), + Some("ruby:ruby_block") + ); + assert!(item(&ids, "ruby:ruby_glass").is_none(), "no item declared"); + assert!( + ids.get("minecraft:block") + .unwrap() + .id("ruby:ruby_glass") + .is_some() + ); let old_facts = BlockFacts::load(&base, old).unwrap(); let facts = BlockFacts::load(&root, new).unwrap(); From a3f81f3a30ec54d2cb84e806334db2db8a14684b Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 18:10:20 +0200 Subject: [PATCH 05/11] Launcher: test a Java mod's install, dependencies, id clash and removal offline Co-Authored-By: Claude Opus 5.5 --- launcher/src/browser/installed/tests.rs | 82 +++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/launcher/src/browser/installed/tests.rs b/launcher/src/browser/installed/tests.rs index af55ec78d..e3fe7899c 100644 --- a/launcher/src/browser/installed/tests.rs +++ b/launcher/src/browser/installed/tests.rs @@ -250,3 +250,85 @@ fn shader_packs_go_to_their_folder_and_modpacks_download_checked() { assert!(!downloads.join("atm-2.zip").exists()); fs::remove_dir_all(&home).unwrap(); } + +/// A NeoForge jar with `files` beside its `neoforge.mods.toml` for mod `id`. +fn mod_jar(id: &str, files: &[(&str, &str)]) -> Vec { + use std::io::Write; + let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let toml = format!( + "modLoader = \"javafml\"\nlicense = \"MIT\"\n[[mods]]\nmodId = \"{id}\"\nversion = \"1.0\"\ndisplayName = \"{id}\"\n" + ); + for (name, text) in [("META-INF/neoforge.mods.toml", toml.as_str())] + .into_iter() + .chain(files.iter().copied()) + { + zip.start_file(name, zip::write::SimpleFileOptions::default()) + .unwrap(); + zip.write_all(text.as_bytes()).unwrap(); + } + zip.finish().unwrap().into_inner() +} + +#[test] +fn java_mods_are_ported_turned_on_refused_on_clashes_and_removed() { + let home = home("java"); + let mut fake = Fake::default(); + for id in ["ruby", "lib", "gem"] { + let mut p = project(id, Kind::Mod); + p.loaders = vec!["neoforge".into()]; + fake.add(p); + } + let block = r#"{"variants": {"": {"model": "ruby:block/ruby_block"}}}"#; + let recipe = r#"{"type": "minecraft:crafting_shapeless", "ingredients": ["minecraft:red_dye"], "result": {"id": "ruby:ruby_block"}}"#; + let ruby = mod_jar( + "ruby", + &[ + ("assets/ruby/blockstates/ruby_block.json", block), + ("data/ruby/recipe/ruby_block.json", recipe), + ], + ); + // A library: nothing to port. + let lib = mod_jar("lib", &[]); + // Another project with the same mod id. + let gem = mod_jar("ruby", &[("assets/ruby/blockstates/gem.json", block)]); + fake.release("ruby", "1", &["lib"], &ruby, false); + fake.release("lib", "1", &[], &lib, false); + fake.release("gem", "1", &[], &gem, false); + + let done = install(&fake, &fake.project("ruby").unwrap(), false, &home, NONE).unwrap(); + assert_eq!(done.len(), 1, "the library isn't installed"); + let entry = &done[0]; + assert_eq!(entry.file, "horizon-mods/ruby"); + assert_eq!(entry.verdict.as_deref(), Some("Works")); + assert!( + entry + .skipped + .iter() + .any(|s| s.starts_with("needs Pack lib")), + "{:?}", + entry.skipped + ); + assert!(entry.namespaces.contains(&"ruby".to_owned())); + assert!(home.join("horizon-mods/ruby/horizon-mod.toml").is_file()); + assert_eq!( + fs::read_to_string(home.join("horizon-mods.txt")).unwrap(), + "mod.ruby:true\n" + ); + // The jar isn't kept, its check is. + assert!(!home + .join("mod-browser/checks-1/.downloads/ruby-1.zip") + .exists()); + + let clash = install(&fake, &fake.project("gem").unwrap(), false, &home, NONE).unwrap_err(); + assert!(clash.contains("shares its id or namespace"), "{clash}"); + assert!(!home.join("horizon-mods/gem").exists()); + + let removed = remove(&home, "ruby").unwrap(); + assert_eq!(removed.kind, Kind::Mod); + assert!(!home.join("horizon-mods/ruby").exists()); + assert_eq!( + fs::read_to_string(home.join("horizon-mods.txt")).unwrap(), + "" + ); + fs::remove_dir_all(&home).unwrap(); +} From b8f79247b865c2a4bba9d5e65803e280f581ceca Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 18:50:25 +0200 Subject: [PATCH 06/11] Mod browser: CurseForge is coming soon Ludovic dropped CurseForge for now (its API needs a paid key). The Mods page shows "CurseForge (coming soon)" beside the Modrinth switch, disabled and inert; browser::sources() is Modrinth only, so no search or install goes to CurseForge, and `--source curseforge` says it is coming soon. Text that asked for a key or an environment variable now says CurseForge support is coming soon. Importing a CurseForge zip or instance still works; jars only CurseForge serves are listed as "CurseForge downloads coming soon". Co-Authored-By: Claude Opus 5.5 --- docs/mod-browser.md | 30 ++++++++++----------------- launcher/README.md | 10 ++++----- launcher/src/browser.rs | 14 ++++++------- launcher/src/browser/tests.rs | 7 +++++++ launcher/src/cli.rs | 10 ++++----- launcher/src/modpacks.rs | 3 ++- launcher/src/mods_page.rs | 38 +++++++++++++++++------------------ website/docs/modpacks.md | 2 +- website/docs/mods.md | 2 +- 9 files changed, 58 insertions(+), 58 deletions(-) diff --git a/docs/mod-browser.md b/docs/mod-browser.md index bc7a80b16..6601fea70 100644 --- a/docs/mod-browser.md +++ b/docs/mod-browser.md @@ -1,6 +1,6 @@ # The launcher's mod browser -Search Modrinth (and CurseForge, once the launcher has a key) for Java mods, modpacks, resource packs and shader +Search Modrinth (CurseForge support is coming soon) for Java mods, modpacks, resource packs and shader packs, see how well each one runs before installing it, and install it with its checksum checked. This is item 16 of the roadmap and issue #577. The code is in `launcher/src/browser.rs` and `launcher/src/browser/`. @@ -63,8 +63,8 @@ off. ## The Mods page -The launcher's **Mods** page has the four kinds as tabs (Mods, Modpacks, Resource packs, Shader packs), a search field (Enter searches), **Show everything** and, -once CurseForge is set up, a Modrinth / CurseForge switch. Each result is an index row: its summary, author, +The launcher's **Mods** page has the four kinds as tabs (Mods, Modpacks, Resource packs, Shader packs), a search field (Enter searches), **Show everything** and +the source: Modrinth, with **CurseForge (coming soon)** beside it, disabled. Each result is an index row: its summary, author, downloads, source and licence, how well it runs (and why), **Page** (the project's page on its source) and **Install** (**Import** for a modpack). The Modpacks tab lists the imported packs first, with their coverage report, **Port** and **Play** ([modpacks.md](modpacks.md)). Installed packs are listed above the results with their version, source, @@ -102,21 +102,13 @@ horizon-launcher mods remove faithful-32x **Modrinth.** The public API (`api.modrinth.com/v2`) needs no key. The launcher names itself in its user agent, as Modrinth asks. -### CurseForge +### CurseForge (coming soon) -CurseForge's API (`api.curseforge.com/v1`) needs an API key from its developer console. Ludovic approved CurseForge -for the browser on 2026-10-21; the key is his (agents never sign up for him). Until a key exists the CurseForge source -is off and the browser searches Modrinth only. +CurseForge support is coming soon. Its API (`api.curseforge.com/v1`) needs a paid key, so for now the launcher +doesn't use it: the Mods page shows **CurseForge (coming soon)** beside the Modrinth switch, disabled, and no search +or install goes to CurseForge (`browser::sources()` is Modrinth only; `horizon-launcher mods ... --source curseforge` +says it is coming soon). The CurseForge source (`browser/curseforge.rs`) is kept for when it comes. -To turn it on: - -1. Ludovic gets a key at (sign in, API keys). CurseForge's terms for a launcher - that ships the key apply: the key is built into the binary, so anyone with the binary can read it. -2. He stores it in the 1Password **Agents** vault as an item `CurseForge API`, field `credential`. -3. It goes into the repository secret `HORIZON_CURSEFORGE_API_KEY` - (`~/fleet/op read "op://Agents/CurseForge API/credential" | gh secret set HORIZON_CURSEFORGE_API_KEY`). - The launcher workflow passes it to the build, which builds it in (`option_env!`). - -At run time `HORIZON_CURSEFORGE_API_KEY` in the environment overrides the built-in key; empty means none. A file -whose author allows downloads from CurseForge's own site only (no `downloadUrl` in the API) is shown but can't be -installed. +A CurseForge modpack zip or instance the player already has still imports, since that needs no key: its jars come +from the player's installed instance or, when Modrinth has the same file, from Modrinth. A jar only CurseForge serves +is listed under the pack as "CurseForge downloads coming soon". diff --git a/launcher/README.md b/launcher/README.md index 08ec11f98..85b74e6aa 100644 --- a/launcher/README.md +++ b/launcher/README.md @@ -24,10 +24,10 @@ The opening, then Play ([clip](docs/opening.webm)): preparation reuses checksum-verified game jars and libraries from the current or another installed Horizon version, so the original Minecraft install is only required when no valid imported copy exists. -- **Mods**: search Modrinth (and CurseForge once the launcher has its API key) for resource +- **Mods**: search Modrinth (CurseForge support is coming soon) for resource packs, Iris/OptiFine shader packs and modpacks, each marked Works, Partly or Doesn't run in Horizon; Java mod jars are never offered. Install puts a pack and what it needs in - `resourcepacks/` or `shaderpacks/` only after its SHA-512 (SHA-1 on CurseForge) matches, and a + `resourcepacks/` or `shaderpacks/` only after its SHA-512 matches, and a modpack goes to the Modpacks page; installed packs show their source and checksum, with Update and Remove ([docs/mod-browser.md](../docs/mod-browser.md)). - **Modpacks**: import a Java modpack (a Modrinth `.mrpack`, a CurseForge zip, or a Prism, @@ -35,8 +35,8 @@ The opening, then Play ([clip](docs/opening.webm)): not yet or not needed, with the reason ([docs/modpacks.md](../docs/modpacks.md)). **Port** gets the jars of what this computer can port today (content mods with data and assets only) the player's own way: copied from a folder they have, or downloaded from the pack's own links - or the same file on Modrinth's CDN, hash checked; files only CurseForge has need its API key, so - they come from your installed instance. It carries them over for the selected version, and + or the same file on Modrinth's CDN, hash checked; files only CurseForge has come from your + installed instance (CurseForge downloads are coming soon). It carries them over for the selected version, and **Play** starts the game as the pack's own profile (`ports//home/` in the game's home: its own saves and options) with those mods. Nothing is uploaded or shared: mod jars and ports stay on this computer. @@ -155,7 +155,7 @@ The same binary is a command-line tool; without a subcommand it opens the window horizon-launcher modpack play [--version ] play it as its own profile horizon-launcher modpack remove horizon-launcher mods [list] what the mod browser installed (docs/mod-browser.md) - horizon-launcher mods search [--kind resource|shader|modpack] [--everything] [--source modrinth|curseforge] + horizon-launcher mods search [--kind resource|shader|modpack] [--everything] [--source modrinth] horizon-launcher mods install checksum checked, with what it needs; a modpack is imported horizon-launcher mods update [] [--check] horizon-launcher mods remove diff --git a/launcher/src/browser.rs b/launcher/src/browser.rs index b8248fd7c..c4a6ff726 100644 --- a/launcher/src/browser.rs +++ b/launcher/src/browser.rs @@ -1,4 +1,4 @@ -//! The mod browser's sources: search Modrinth (and CurseForge, once a key exists) for what +//! The mod browser's sources: search Modrinth (CurseForge support is coming soon) for what //! Horizon loads, say how well each result runs, and download a file only after its checksum //! matches (docs/mod-browser.md). //! @@ -221,13 +221,13 @@ pub trait Source: Send + Sync { } } -/// The sources this launcher can use: Modrinth always, CurseForge when it has an API key. +/// What the launcher says wherever CurseForge would be. +pub const CURSEFORGE_SOON: &str = "CurseForge support is coming soon."; + +/// The sources this launcher uses: Modrinth. CurseForge support is coming soon: its API needs a +/// paid key, so no search or install goes to it; `curseforge` is kept for then. pub fn sources() -> Vec> { - let mut all: Vec> = vec![Box::new(modrinth::Modrinth)]; - if let Some(cf) = curseforge::CurseForge::from_env() { - all.push(Box::new(cf)); - } - all + vec![Box::new(modrinth::Modrinth)] } /// The source of that id, when this launcher can use it. diff --git a/launcher/src/browser/tests.rs b/launcher/src/browser/tests.rs index 9e75c55a1..2137ecc94 100644 --- a/launcher/src/browser/tests.rs +++ b/launcher/src/browser/tests.rs @@ -400,3 +400,10 @@ fn live_modrinth_search_pick_and_download() { std::fs::remove_dir_all(&dir).unwrap(); assert_eq!(source.project(&p.id).unwrap().slug, p.slug); } + +#[test] +fn curseforge_is_coming_soon_and_never_searched() { + let ids: Vec = sources().iter().map(|s| s.id()).collect(); + assert_eq!(ids, [SourceId::Modrinth]); + assert!(source(SourceId::CurseForge).is_none()); +} diff --git a/launcher/src/cli.rs b/launcher/src/cli.rs index 530d220dd..ebcc95cd6 100644 --- a/launcher/src/cli.rs +++ b/launcher/src/cli.rs @@ -212,6 +212,7 @@ enum KindArg { #[derive(Clone, Copy, clap::ValueEnum)] enum SourceArg { Modrinth, + /// CurseForge support is coming soon Curseforge, } @@ -222,9 +223,7 @@ impl SourceArg { SourceArg::Modrinth => SourceId::Modrinth, SourceArg::Curseforge => SourceId::CurseForge, }; - browser::source(id).ok_or_else(|| { - "CurseForge needs an API key: HORIZON_CURSEFORGE_API_KEY (docs/mod-browser.md).".into() - }) + browser::source(id).ok_or_else(|| browser::CURSEFORGE_SOON.into()) } } @@ -337,8 +336,9 @@ enum Cmd { #[command(subcommand)] action: Option, }, - /// The mod browser: resource packs, shader packs and modpacks from Modrinth (and CurseForge - /// with a key), each file checked against its checksum (docs/mod-browser.md) + /// The mod browser: Java mods, resource packs, shader packs and modpacks from Modrinth + /// (CurseForge support is coming soon), each file checked against its checksum + /// (docs/mod-browser.md) Mods { #[command(subcommand)] action: Option, diff --git a/launcher/src/modpacks.rs b/launcher/src/modpacks.rs index b69ff0976..2495e1ad6 100644 --- a/launcher/src/modpacks.rs +++ b/launcher/src/modpacks.rs @@ -84,7 +84,8 @@ impl Imported { (How::Present, "already here"), ( How::NeedsKey, - "only on CurseForge (needs its API key: choose your installed instance instead)", + "only on CurseForge (CurseForge downloads coming soon: choose your installed \ + instance instead)", ), (How::NotFound, "not found"), (How::Failed, "failed"), diff --git a/launcher/src/mods_page.rs b/launcher/src/mods_page.rs index 136ca7953..fd6a24b76 100644 --- a/launcher/src/mods_page.rs +++ b/launcher/src/mods_page.rs @@ -1,12 +1,11 @@ -//! The Mods page: search Modrinth (and CurseForge with a key) for Java mods, modpacks, resource +//! The Mods page: search Modrinth (CurseForge: coming soon) for Java mods, modpacks, resource //! packs and shader packs, see where each comes from and how well it runs (a Java mod's jar is //! checked first), install it with its checksum checked, and update or remove what was installed //! (`horizon_launcher::browser`). Imported modpacks are listed on its Modpacks tab. use super::servers_page::{type_key, Key}; use super::*; use horizon_launcher::browser::{ - self, curseforge::CurseForge, installed, java, Kind as PackKind, Project, Query, SourceId, - Support, + self, installed, java, Kind as PackKind, Project, Query, SourceId, Support, }; use horizon_launcher::modpacks; use horizon_port_pack::check::Check; @@ -20,10 +19,8 @@ const KINDS: [(PackKind, &str); 4] = [ ]; /// Java mods checked at once. const CHECKING: usize = 2; -const SOURCES: [(SourceId, &str); 2] = [ - (SourceId::Modrinth, "Modrinth"), - (SourceId::CurseForge, "CurseForge"), -]; +/// The sources to pick from; CurseForge is shown beside them as coming soon. +const SOURCES: [(SourceId, &str); 1] = [(SourceId::Modrinth, "Modrinth")]; const PAGE: usize = 20; pub(super) enum Event { @@ -46,7 +43,6 @@ pub(super) enum Checking { pub(super) struct Mods { kind: PackKind, source: SourceId, - curseforge: bool, text: String, focus: FocusHandle, pub(super) focused: bool, @@ -77,7 +73,6 @@ impl Mods { Self { kind: PackKind::Mod, source: SourceId::Modrinth, - curseforge: CurseForge::from_env().is_some(), text: String::new(), focus: cx.focus_handle().tab_index(0).tab_stop(true), focused: false, @@ -458,8 +453,14 @@ impl Launcher { this.mods_search(0); cx.notify(); })); - let source_note = if self.mods.curseforge { - segments("mods-source", &SOURCES, self.mods.source, &t, { + // CurseForge support is coming soon: shown, inert, never searched (like the Microsoft + // account's "Coming soon"). + let source_note = div() + .flex() + .flex_wrap() + .items_center() + .gap(px(8.)) + .child(segments("mods-source", &SOURCES, self.mods.source, &t, { let this = this.clone(); move |source, _, cx| { let _ = this.update(cx, |this, cx| { @@ -470,14 +471,13 @@ impl Launcher { cx.notify(); }); } - }) - .into_any_element() - } else { - text(theme::CAPTION) - .text_color(t.muted) - .child("From Modrinth. CurseForge comes once the launcher has its API key.") - .into_any_element() - }; + })) + .child(disabled(button( + "mods-source-curseforge", + "CurseForge (coming soon)", + Kind::Ghost, + &t, + ))); let controls = div() .flex() .flex_col() diff --git a/website/docs/modpacks.md b/website/docs/modpacks.md index b78c59851..e3ed053d9 100644 --- a/website/docs/modpacks.md +++ b/website/docs/modpacks.md @@ -10,7 +10,7 @@ Open **Modpacks** in the launcher. (From the next launcher release, it is the ** 1. **Import a pack.** Choose **Import a pack file…** for a Modrinth `.mrpack` or a CurseForge zip, or **Import an instance folder…** for a Prism, MultiMC or CurseForge instance. The **Mods** page can also find a Modrinth modpack and import it for you (see [Mods and data packs](/docs/mods#finding-resource-packs-shader-packs-and-modpacks)). 2. **Read the coverage report.** For every mod in the pack it says **Ported**, **Partial**, **Not yet** or **Not needed**, with the reason. You can filter the list by status. -3. **Port.** **Port N mods** gets the jars of the mods this computer can carry over today, and carries them over for the version you picked. It copies the jars from a folder you have, or downloads them from the pack's own links (or the same file from Modrinth's download network), and checks their hashes. Files that only CurseForge has need CurseForge's API key, so for those choose **Use jars from my instance…** and point it at your installed instance. +3. **Port.** **Port N mods** gets the jars of the mods this computer can carry over today, and carries them over for the version you picked. It copies the jars from a folder you have, or downloads them from the pack's own links (or the same file from Modrinth's download network), and checks their hashes. CurseForge downloads are coming soon, so for files that only CurseForge has, choose **Use jars from my instance…** and point it at your installed instance. 4. **Play.** **Play with N mods** starts the game as the pack's own profile, with its own saves and options, and those mods on. Nothing is uploaded or shared. The mod jars and what is made from them stay on your computer. diff --git a/website/docs/mods.md b/website/docs/mods.md index d3305fa69..b0bd8c31f 100644 --- a/website/docs/mods.md +++ b/website/docs/mods.md @@ -36,7 +36,7 @@ cargo horizon-mod run The launcher's **Mods** page (launcher 0.17 and newer) searches [Modrinth](https://modrinth.com) for what Horizon can load: resource packs, Java shader packs and modpacks. Each result says how well it runs in Horizon (**Works**, **Partly**, or **Doesn't run**), and by default the page shows only what runs. **Install** downloads the file, checks it against the checksum Modrinth lists and refuses anything that does not match, then puts it in the right folder, with the packs it needs. **Update** and **Remove** are there too. A resource pack still has to be turned on in the game's Resource Packs screen, and a shader pack in Video, Shader Packs. A modpack is imported on the [Modpacks page](/docs/modpacks). -The page cannot search CurseForge yet. The same things work from a terminal: `horizon-launcher mods search`, `install`, `update` and `remove`. +CurseForge support is coming soon: the page shows it, but doesn't search it yet. The same things work from a terminal: `horizon-launcher mods search`, `install`, `update` and `remove`. ## Java mods from the Mods page (next launcher release) From f53998674de95895558b99e00f572175293db3e0 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:37:12 +0200 Subject: [PATCH 07/11] Launcher mod browser: project pages, search filters, project icons and gallery A result's title opens its page in the launcher: icon, authors, downloads, followers, licence, verdict (for a Java mod, what ports and what doesn't) and Install, links, then Description (Markdown), Gallery and Versions tabs (browser::details, Source::details on Modrinth's project and members). Filters: sort (relevance, downloads, follows, newest, updated), loader, game version, environment and categories, from the source's tag lists. Icons and gallery images are fetched at run time over HTTPS from the sources' image CDNs only, size-capped, decoded and scaled to PNGs in a 128 MB disk cache (browser::images); a placeholder shows until they arrive. Vanilla's mark is a grass block drawn from the player's prepared game files. Modrinth's logo isn't shown: its branding is all rights reserved without written permission. Co-Authored-By: Claude Opus 5.5 --- docs/mod-browser.md | 35 ++ launcher/Cargo.toml | 2 +- launcher/src/browser.rs | 15 + launcher/src/browser/curseforge.rs | 7 +- launcher/src/browser/details.rs | 368 +++++++++++++++++ launcher/src/browser/images.rs | 265 +++++++++++++ launcher/src/browser/modrinth.rs | 69 +++- launcher/src/main.rs | 1 + launcher/src/mods_page.rs | 113 ++++-- launcher/src/mods_project.rs | 617 +++++++++++++++++++++++++++++ 10 files changed, 1435 insertions(+), 57 deletions(-) create mode 100644 launcher/src/browser/details.rs create mode 100644 launcher/src/browser/images.rs create mode 100644 launcher/src/mods_project.rs diff --git a/docs/mod-browser.md b/docs/mod-browser.md index 6601fea70..74959ab4c 100644 --- a/docs/mod-browser.md +++ b/docs/mod-browser.md @@ -71,6 +71,41 @@ report, **Port** and **Play** ([modpacks.md](modpacks.md)). Installed packs are file and the checksum they were checked against, with **Update to …** when a newer version runs and **Remove** (asked twice). +### Project pages, filters and icons + +A result's title opens its page inside the launcher, not in a web browser: its icon, authors, downloads, followers, +last update, licence and source, how well it runs (for a Java mod, what ports and what doesn't, from its check) with +**Install** or **Import**, the project's links (its page on the source, source code, issues, wiki, Discord, +donations; `https://` only, opened in the system browser), then three tabs: **Description** (the project's Markdown, +drawn by the launcher's renderer), **Gallery** and **Versions** (each version's date, loaders, game versions, file +and size, and the verdict of any version already checked). The data is `browser::details` (`Source::details`; on +Modrinth `GET /project/{id}` and `/project/{id}/members`, then `/project/{id}/version`). + +**Filters** sit above the results: the order (best match, relevance, downloads, follows, newest, updated) and, under +**Filters**, the loader, game version, environment (client, server) and categories, with the values the source lists +for the kind (Modrinth's `/tag/category`, `/tag/loader`, `/tag/game_version`, asked once per run). A loader or version +picked replaces the default (Java mod loaders and 26.3 for mods); a version other than 26.3 checks and installs the +newest file for 1.21 or later, as **Show everything** does. Changing tabs clears the filters. CurseForge support +is coming soon; its source already maps the order and the game version. + +**Icons and gallery images** are the projects' own, fetched at run time from the source's image server +(`browser::images`): HTTPS from `cdn.modrinth.com` (or CurseForge's `media.forgecdn.net` / `edge.forgecdn.net`) only, +at most 8 MB each, decoded (PNG, JPEG, WebP, GIF, at most 8192 pixels a side), scaled (icons 128 px, gallery 960 px) +and kept as PNGs in `mod-browser/images/` under the game folder, which is kept under 128 MB by dropping the oldest +files. A plate with the project's initial stands in until the icon arrives. Nothing of a project's art is in the +launcher or the repository. + +**Vanilla Minecraft** is marked with a grass block drawn at run time from the player's own prepared game files +(`assets/minecraft/textures/block/grass_block_*.png`, tinted with the plains grass colour): the game-version filter +shows it. No Mojang file or logo is in the launcher or the repository. + +**Source logos.** Modrinth's logo is not shown: Modrinth's own repository says its branding is all rights reserved and +"the use of Modrinth branding elements, including but not limited to the wrench-in-labyrinth logo, ... is strictly +prohibited without explicit written permission from Rinth, Inc." (`packages/assets/COPYING.md` in +[modrinth/code](https://github.com/modrinth/code/blob/main/packages/assets/COPYING.md)), and its +[terms](https://modrinth.com/legal/terms) ask for prior written permission to use its marks. Results name their source +in text ("from Modrinth") until Rinth, Inc. agrees. CurseForge support, and so its logo, is coming soon. + ## Installing, updating, removing `mod-browser.json` beside `options.txt` lists every file the browser installed: its source, project, version, file diff --git a/launcher/Cargo.toml b/launcher/Cargo.toml index 1097d5675..006e35c5e 100644 --- a/launcher/Cargo.toml +++ b/launcher/Cargo.toml @@ -39,7 +39,7 @@ horizon-telemetry = { path = "../crates/horizon-telemetry" } getrandom = "0.3" # Players' heads on the Servers page, from the default skins in the player's own game files (already # in the lockfile through gpui). -image = { version = "0.25", default-features = false, features = ["png"] } +image = { version = "0.25", default-features = false, features = ["png", "jpeg", "webp", "gif"] } # Microsoft account secrets in the OS keychain: the Secret Service on Linux through oo7 (the same # build gpui uses), Keychain and Credential Manager through keyring. diff --git a/launcher/src/browser.rs b/launcher/src/browser.rs index c4a6ff726..091560638 100644 --- a/launcher/src/browser.rs +++ b/launcher/src/browser.rs @@ -7,7 +7,9 @@ //! (`resourcepacks/`) and Java shader packs (`shaderpacks/`, Iris and OptiFine). Java code itself //! doesn't run in Horizon (docs/mods.md). pub mod curseforge; +pub mod details; pub mod download; +pub mod images; pub mod installed; pub mod java; pub mod modrinth; @@ -189,6 +191,7 @@ pub struct Query { pub everything: bool, pub offset: usize, pub limit: usize, + pub filters: details::Filters, } impl Query { @@ -199,6 +202,7 @@ impl Query { everything: false, offset: 0, limit: 20, + filters: details::Filters::default(), } } } @@ -210,6 +214,17 @@ pub trait Source: Send + Sync { fn project(&self, id: &str) -> Result; /// Versions, newest first. fn versions(&self, project: &Project) -> Result, String>; + /// The project's page: description, gallery, links, authors. + fn details(&self, project: &Project) -> Result { + Ok(details::Details { + body: project.summary.clone(), + ..details::Details::default() + }) + } + /// The values the search's filters can take for `kind`. + fn tags(&self, _kind: Kind) -> Result { + Ok(details::Tags::default()) + } /// Downloads one of its files into `folder`, checked ([`download::fetch`]). fn fetch( &self, diff --git a/launcher/src/browser/curseforge.rs b/launcher/src/browser/curseforge.rs index 04e722bba..0a7a04a5e 100644 --- a/launcher/src/browser/curseforge.rs +++ b/launcher/src/browser/curseforge.rs @@ -69,12 +69,15 @@ impl CurseForge { pub(super) fn search_url(query: &Query) -> String { let mut url = format!( - "{API}/mods/search?gameId={MINECRAFT}&classId={}&sortField=2&sortOrder=desc&index={}&pageSize={}", + "{API}/mods/search?gameId={MINECRAFT}&classId={}&sortField={}&sortOrder=desc&index={}&pageSize={}", class(query.kind), + query.filters.sort(&query.text).curseforge(), query.offset, query.limit.clamp(1, 50), ); - if !query.everything && matches!(query.kind, Kind::ResourcePack | Kind::Mod) { + if let Some(v) = &query.filters.game_version { + url.push_str(&format!("&gameVersion={}", encode(v))); + } else if !query.everything && matches!(query.kind, Kind::ResourcePack | Kind::Mod) { url.push_str(&format!("&gameVersion={GAME}")); } if !query.text.is_empty() { diff --git a/launcher/src/browser/details.rs b/launcher/src/browser/details.rs new file mode 100644 index 000000000..004db12b4 --- /dev/null +++ b/launcher/src/browser/details.rs @@ -0,0 +1,368 @@ +//! What the browser shows beyond a search hit: a project's page (description, gallery, links, +//! authors) and the search's filters (categories, loader, game version, environment, sort), with +//! the lists of values each source offers for them. Data only: `mods_page` draws it. +use super::{Kind, GAME}; +use serde_json::Value; + +/// How results are ordered. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +pub enum Sort { + #[default] + Relevance, + Downloads, + Follows, + Newest, + Updated, +} + +impl Sort { + pub const ALL: [Sort; 5] = [ + Sort::Relevance, + Sort::Downloads, + Sort::Follows, + Sort::Newest, + Sort::Updated, + ]; + pub fn label(self) -> &'static str { + match self { + Sort::Relevance => "Relevance", + Sort::Downloads => "Downloads", + Sort::Follows => "Follows", + Sort::Newest => "Newest", + Sort::Updated => "Updated", + } + } + /// Modrinth's `index`. + pub fn modrinth(self) -> &'static str { + match self { + Sort::Relevance => "relevance", + Sort::Downloads => "downloads", + Sort::Follows => "follows", + Sort::Newest => "newest", + Sort::Updated => "updated", + } + } + /// CurseForge's `sortField` (`ModsSearchSortField`). + pub fn curseforge(self) -> u8 { + match self { + Sort::Relevance | Sort::Follows => 2, + Sort::Downloads => 6, + Sort::Newest => 11, + Sort::Updated => 3, + } + } +} + +/// Where a project runs. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum Environment { + Client, + Server, +} + +impl Environment { + pub const ALL: [Environment; 2] = [Environment::Client, Environment::Server]; + pub fn label(self) -> &'static str { + match self { + Environment::Client => "Client", + Environment::Server => "Server", + } + } +} + +/// A search's filters. Empty: the browser's defaults (what runs in Horizon). +#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)] +pub struct Filters { + /// Every one of these categories (`decoration`, `worldgen`...). + pub categories: Vec, + /// One loader (`neoforge`, `fabric`, `iris`...), in place of the kind's defaults. + pub loader: Option, + /// One Minecraft version, in place of the one Horizon plays. + pub game_version: Option, + pub environment: Option, + /// None: by relevance with search text, else by downloads. + pub sort: Option, +} + +impl Filters { + /// The sort a search uses. + pub fn sort(&self, text: &str) -> Sort { + self.sort.unwrap_or(if text.is_empty() { + Sort::Downloads + } else { + Sort::Relevance + }) + } + /// A game version other than the one Horizon plays was asked for. + pub fn other_version(&self) -> bool { + self.game_version.as_deref().is_some_and(|v| v != GAME) + } +} + +/// One value a filter can take. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Tag { + pub name: String, + /// The group a category is in on its source (`categories`, `features`, `resolutions`). + pub header: String, +} + +/// The values each filter can take for a kind. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct Tags { + pub categories: Vec, + pub loaders: Vec, + /// Release versions, newest first. + pub game_versions: Vec, +} + +/// A gallery image. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Image { + pub url: String, + pub title: String, + pub description: String, + pub featured: bool, +} + +/// A link a project gives: its source, issues, wiki, Discord, donations. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Link { + pub label: String, + pub url: String, +} + +/// A project's page. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct Details { + /// Markdown (Modrinth's body; HTML blocks in it are left as text the renderer skips). + pub body: String, + pub gallery: Vec, + pub links: Vec, + pub authors: Vec, + pub followers: u64, + pub published: String, + pub updated: String, + pub categories: Vec, + /// `required`, `optional`, `unsupported` or `unknown`. + pub client_side: String, + pub server_side: String, +} + +fn strings(v: &Value) -> Vec { + v.as_array() + .into_iter() + .flatten() + .filter_map(|s| s.as_str().map(str::to_owned)) + .collect() +} + +fn text(v: &Value) -> String { + v.as_str().unwrap_or_default().to_owned() +} + +/// Only `https://` links are kept: a page never opens another scheme. +fn https(url: &str) -> Option { + url.starts_with("https://").then(|| url.to_owned()) +} + +/// Modrinth's `GET /project/{id}` and `GET /project/{id}/members`. +pub fn modrinth_details(project: &Value, members: &Value) -> Details { + let mut gallery: Vec<(i64, Image)> = project["gallery"] + .as_array() + .into_iter() + .flatten() + .filter_map(|g| { + Some(( + g["ordering"].as_i64().unwrap_or(0), + Image { + url: https(g["url"].as_str()?)?, + title: text(&g["title"]), + description: text(&g["description"]), + featured: g["featured"].as_bool().unwrap_or(false), + }, + )) + }) + .collect(); + gallery.sort_by_key(|(order, i)| (!i.featured, *order)); + let mut links = Vec::new(); + for (key, label) in [ + ("source_url", "Source"), + ("issues_url", "Issues"), + ("wiki_url", "Wiki"), + ("discord_url", "Discord"), + ] { + if let Some(url) = project[key].as_str().and_then(https) { + links.push(Link { + label: label.into(), + url, + }); + } + } + for d in project["donation_urls"].as_array().into_iter().flatten() { + if let Some(url) = d["url"].as_str().and_then(https) { + links.push(Link { + label: format!("Donate ({})", text(&d["platform"])), + url, + }); + } + } + let mut owners: Vec<(bool, String)> = members + .as_array() + .into_iter() + .flatten() + .filter_map(|m| { + let name = m["user"]["username"].as_str()?.to_owned(); + Some((m["role"].as_str() != Some("Owner"), name)) + }) + .collect(); + owners.sort(); + let mut categories = strings(&project["categories"]); + categories.extend(strings(&project["additional_categories"])); + Details { + body: text(&project["body"]), + gallery: gallery.into_iter().map(|(_, i)| i).collect(), + links, + authors: owners.into_iter().map(|(_, n)| n).collect(), + followers: project["followers"].as_u64().unwrap_or(0), + published: text(&project["published"]), + updated: text(&project["updated"]), + categories, + client_side: text(&project["client_side"]), + server_side: text(&project["server_side"]), + } +} + +/// Modrinth's `/tag/category`, `/tag/loader` and `/tag/game_version` for one project type. +pub fn modrinth_tags( + project_type: &str, + categories: &Value, + loaders: &Value, + versions: &Value, +) -> Tags { + let loader_names: Vec = loaders + .as_array() + .into_iter() + .flatten() + .filter(|l| { + l["supported_project_types"] + .as_array() + .is_some_and(|t| t.iter().any(|x| x == project_type)) + }) + .filter_map(|l| l["name"].as_str().map(str::to_owned)) + .collect(); + let mut categories: Vec = categories + .as_array() + .into_iter() + .flatten() + .filter(|c| c["project_type"] == project_type) + .filter_map(|c| { + let name = c["name"].as_str()?.to_owned(); + // Loaders are listed as categories too; the loader filter has them. + (!loader_names.contains(&name)).then(|| Tag { + name, + header: text(&c["header"]), + }) + }) + .collect(); + categories.sort_by(|a, b| (&a.header, &a.name).cmp(&(&b.header, &b.name))); + Tags { + categories, + loaders: loader_names, + game_versions: versions + .as_array() + .into_iter() + .flatten() + .filter(|v| v["version_type"] == "release") + .filter_map(|v| v["version"].as_str().map(str::to_owned)) + .collect(), + } +} + +/// The loaders a kind lists by default, when no loader is picked: Java mod loaders for mods, +/// Iris and OptiFine for shader packs (unless `everything`). +pub fn default_loaders(kind: Kind, everything: bool) -> Vec<&'static str> { + match kind { + Kind::Mod => super::java::LOADERS.to_vec(), + Kind::ShaderPack if !everything => vec!["iris", "optifine"], + _ => Vec::new(), + } +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + #[test] + fn a_modrinth_page() { + let project = json!({ + "body": "# Fences\nMore fences.", + "gallery": [ + {"url": "https://cdn.modrinth.com/data/x/images/b.png", "featured": false, "ordering": 0, "title": "B"}, + {"url": "https://cdn.modrinth.com/data/x/images/a.png", "featured": true, "ordering": 5, "title": "A"}, + {"url": "http://example.com/c.png", "featured": false, "ordering": 1} + ], + "source_url": "https://github.com/x/y", "issues_url": "javascript:alert(1)", + "wiki_url": null, "discord_url": "https://discord.gg/x", + "donation_urls": [{"platform": "Ko-fi", "url": "https://ko-fi.com/x"}], + "followers": 12, "categories": ["decoration"], "additional_categories": ["utility"], + "client_side": "required", "server_side": "required" + }); + let members = json!([ + {"user": {"username": "helper"}, "role": "Member"}, + {"user": {"username": "Macaw"}, "role": "Owner"} + ]); + let d = modrinth_details(&project, &members); + assert_eq!( + d.gallery + .iter() + .map(|i| i.title.as_str()) + .collect::>(), + ["A", "B"] + ); + assert_eq!( + d.links.iter().map(|l| l.label.as_str()).collect::>(), + ["Source", "Discord", "Donate (Ko-fi)"] + ); + assert_eq!(d.authors, ["Macaw", "helper"]); + assert_eq!(d.categories, ["decoration", "utility"]); + assert_eq!(d.followers, 12); + } + + #[test] + fn modrinth_tags_by_project_type() { + let categories = json!([ + {"name": "decoration", "project_type": "mod", "header": "categories"}, + {"name": "fabric", "project_type": "mod", "header": "loaders"}, + {"name": "16x", "project_type": "resourcepack", "header": "resolutions"} + ]); + let loaders = json!([ + {"name": "fabric", "supported_project_types": ["mod", "modpack"]}, + {"name": "iris", "supported_project_types": ["shader"]} + ]); + let versions = json!([ + {"version": "26.3", "version_type": "release"}, + {"version": "26.4-snapshot-1", "version_type": "snapshot"} + ]); + let t = modrinth_tags("mod", &categories, &loaders, &versions); + assert_eq!(t.categories.len(), 1); + assert_eq!(t.categories[0].name, "decoration"); + assert_eq!(t.loaders, ["fabric"]); + assert_eq!(t.game_versions, ["26.3"]); + } + + #[test] + fn sorts_default_to_relevance_with_text() { + let f = Filters::default(); + assert_eq!(f.sort(""), Sort::Downloads); + assert_eq!(f.sort("fences"), Sort::Relevance); + let f = Filters { + sort: Some(Sort::Updated), + game_version: Some("1.21.1".into()), + ..Filters::default() + }; + assert_eq!(f.sort("x"), Sort::Updated); + assert!(f.other_version()); + } +} diff --git a/launcher/src/browser/images.rs b/launcher/src/browser/images.rs new file mode 100644 index 000000000..fee2f219e --- /dev/null +++ b/launcher/src/browser/images.rs @@ -0,0 +1,265 @@ +//! Project icons and gallery images, fetched at run time from the sources' image servers and +//! kept on disk: nothing of a project's art is in the launcher. A URL is fetched only over HTTPS +//! from Modrinth's or CurseForge's CDN, at most [`MAX_DOWNLOAD`] bytes, decoded (PNG, JPEG, WebP, +//! GIF), scaled down and stored as a PNG the UI draws; the cache is kept under [`MAX_CACHE`] by +//! dropping the oldest files. +use sha1::{Digest, Sha1}; +use std::fs; +use std::io::Read; +use std::path::{Path, PathBuf}; + +/// Hosts images may come from. +const HOSTS: [&str; 3] = [ + "https://cdn.modrinth.com/", + "https://media.forgecdn.net/", + "https://edge.forgecdn.net/", +]; +/// The most an image may weigh before decoding. +pub const MAX_DOWNLOAD: u64 = 8 << 20; +/// The most the cache keeps on disk. +pub const MAX_CACHE: u64 = 128 << 20; +/// Pixels: an icon's side, a gallery image's longest side. +pub const ICON: u32 = 128; +pub const GALLERY: u32 = 960; + +fn dir(home: &Path) -> PathBuf { + home.join("mod-browser/images") +} + +/// Why `url` is never fetched. +pub fn refuse(url: &str) -> Option<&'static str> { + if !HOSTS.iter().any(|h| url.starts_with(h)) { + return Some("not on Modrinth's or CurseForge's image servers over HTTPS"); + } + if url.len() > 2048 || url.contains(char::is_whitespace) { + return Some("not a plain URL"); + } + None +} + +/// Where the image of `url` scaled to `size` is kept. +pub fn path(home: &Path, url: &str, size: u32) -> PathBuf { + let hash: String = Sha1::digest(url.as_bytes()) + .iter() + .map(|b| format!("{b:02x}")) + .collect(); + dir(home).join(format!("{hash}-{size}.png")) +} + +/// The image if it is cached already. +pub fn cached(home: &Path, url: &str, size: u32) -> Option { + let p = path(home, url, size); + p.is_file().then_some(p) +} + +/// The image of `url`, no larger than `size` pixels a side: the cached one, else fetched, +/// decoded, scaled and stored. +pub fn fetch(home: &Path, url: &str, size: u32) -> Result { + if let Some(p) = cached(home, url, size) { + return Ok(p); + } + if let Some(why) = refuse(url) { + return Err(format!("{url}: {why}")); + } + let response = super::download::agent() + .get(url) + .call() + .map_err(|e| format!("{url}: {e}"))?; + let mut bytes = Vec::new(); + response + .into_reader() + .take(MAX_DOWNLOAD + 1) + .read_to_end(&mut bytes) + .map_err(|e| format!("{url}: {e}"))?; + if bytes.len() as u64 > MAX_DOWNLOAD { + return Err(format!("{url}: larger than {} MB", MAX_DOWNLOAD >> 20)); + } + store(home, url, size, &bytes) +} + +/// Decodes `bytes`, scales them to fit `size` and writes the PNG. +pub fn store(home: &Path, url: &str, size: u32, bytes: &[u8]) -> Result { + let mut reader = image::ImageReader::new(std::io::Cursor::new(bytes)) + .with_guessed_format() + .map_err(|e| e.to_string())?; + let mut limits = image::Limits::default(); + limits.max_image_width = Some(8192); + limits.max_image_height = Some(8192); + limits.max_alloc = Some(256 << 20); + reader.limits(limits); + let decoded = reader.decode().map_err(|e| format!("{url}: {e}"))?; + let scaled = if decoded.width() > size || decoded.height() > size { + decoded.resize(size, size, image::imageops::FilterType::Triangle) + } else { + decoded + }; + let target = path(home, url, size); + let folder = dir(home); + fs::create_dir_all(&folder).map_err(|e| format!("{}: {e}", folder.display()))?; + let part = target.with_extension("part"); + scaled + .save_with_format(&part, image::ImageFormat::Png) + .map_err(|e| format!("{}: {e}", part.display()))?; + fs::rename(&part, &target).map_err(|e| format!("{}: {e}", target.display()))?; + prune(&folder, MAX_CACHE); + Ok(target) +} + +/// Drops the oldest files until the folder holds at most `max` bytes. +fn prune(folder: &Path, max: u64) { + let mut files: Vec<(std::time::SystemTime, u64, PathBuf)> = fs::read_dir(folder) + .into_iter() + .flatten() + .flatten() + .filter_map(|e| { + let m = e.metadata().ok()?; + Some((m.modified().ok()?, m.len(), e.path())) + }) + .collect(); + let mut total: u64 = files.iter().map(|f| f.1).sum(); + files.sort(); + for (_, len, path) in files { + if total <= max { + break; + } + if fs::remove_file(&path).is_ok() { + total -= len; + } + } +} + +/// Vanilla Minecraft's mark: a grass block drawn from the player's own prepared game files +/// (`/assets/minecraft/textures/block/`), never shipped. Kept beside the cache. +pub fn grass_block(home: &Path, resources: &Path) -> Option { + let target = dir(home).join("grass-block.png"); + if target.is_file() { + return Some(target); + } + let textures = resources.join("assets/minecraft/textures/block"); + let load = |name: &str| image::open(textures.join(name)).ok().map(|i| i.to_rgba8()); + let (top, side, overlay) = ( + load("grass_block_top.png")?, + load("grass_block_side.png")?, + load("grass_block_side_overlay.png")?, + ); + let icon = draw_block(&top, &side, &overlay); + fs::create_dir_all(dir(home)).ok()?; + icon.save(&target).ok()?; + Some(target) +} + +/// The plains grass colour vanilla's grass block item shows (`GrassColor.getDefaultColor` +/// is the colormap at 0.5, 1.0: `#91BD59`). +const GRASS: [u8; 3] = [0x91, 0xBD, 0x59]; + +/// An isometric block, 64 pixels a side, from 16x16 faces: the top tinted, the sides with their +/// tinted overlay, the left side shaded as the game shades a side facing away from the light. +fn draw_block( + top: &image::RgbaImage, + side: &image::RgbaImage, + overlay: &image::RgbaImage, +) -> image::RgbaImage { + let size = 64u32; + let mut out = image::RgbaImage::new(size, size); + let at = |img: &image::RgbaImage, u: f32, v: f32| { + let (w, h) = (img.width().min(16), img.height().min(16)); + let x = ((u * w as f32) as u32).min(w - 1); + let y = ((v * h as f32) as u32).min(h - 1); + *img.get_pixel(x, y) + }; + let tint = |p: image::Rgba, shade: f32| { + let mut q = p; + for c in 0..3 { + q[c] = (p[c] as f32 * GRASS[c] as f32 / 255. * shade) as u8; + } + q + }; + let shade = |p: image::Rgba, s: f32| { + let mut q = p; + for c in 0..3 { + q[c] = (p[c] as f32 * s) as u8; + } + q + }; + let side_px = |u: f32, v: f32, s: f32| { + let o = at(overlay, u, v); + if o[3] > 0 { + tint(o, s) + } else { + shade(at(side, u, v), s) + } + }; + // The cube: the top face a diamond, two sides below it. + let (cx, h) = (size as f32 / 2., size as f32 / 4.); + for y in 0..size { + for x in 0..size { + let (px, py) = (x as f32 + 0.5, y as f32 + 0.5); + let dx = (px - cx) / cx; + let dy = (py - h) / h; + let u = (dy + dx) / 2. + 0.5; + let v = (dy - dx) / 2. + 0.5; + let pixel = if (0. ..1.).contains(&u) && (0. ..1.).contains(&v) { + Some(tint(at(top, u, v), 1.0)) + } else if px < cx { + let u = px / cx; + let v = (py - h - u * h) / (2. * h); + (0. ..1.).contains(&v).then(|| side_px(u, v, 0.8)) + } else { + let u = (px - cx) / cx; + let v = (py - 2. * h + u * h) / (2. * h); + (0. ..1.).contains(&v).then(|| side_px(u, v, 0.6)) + }; + if let Some(p) = pixel { + out.put_pixel(x, y, p); + } + } + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn only_the_sources_image_servers() { + assert!(refuse("https://cdn.modrinth.com/data/AANobbMI/icon.png").is_none()); + assert!(refuse("https://media.forgecdn.net/avatars/1/2.png").is_none()); + assert!(refuse("http://cdn.modrinth.com/data/x/icon.png").is_some()); + assert!(refuse("https://cdn.modrinth.com.evil.example/x.png").is_some()); + assert!(refuse("https://example.com/x.png").is_some()); + } + + #[test] + fn images_are_scaled_stored_and_pruned() { + let home = std::env::temp_dir().join(format!("horizon-images-{}", std::process::id())); + let _ = fs::remove_dir_all(&home); + let big = image::RgbaImage::from_pixel(300, 150, image::Rgba([1, 2, 3, 255])); + let mut png = Vec::new(); + big.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png) + .unwrap(); + let url = "https://cdn.modrinth.com/data/x/icon.png"; + let p = store(&home, url, ICON, &png).unwrap(); + assert_eq!(cached(&home, url, ICON), Some(p.clone())); + assert_eq!(image::open(&p).unwrap().width(), ICON); + assert!(store(&home, url, ICON, b"not an image").is_err()); + prune(&dir(&home), 0); + assert!(cached(&home, url, ICON).is_none()); + fs::remove_dir_all(&home).unwrap(); + } + + #[test] + fn a_grass_block_from_faces() { + let face = |c: [u8; 4]| image::RgbaImage::from_pixel(16, 16, image::Rgba(c)); + let icon = draw_block( + &face([200, 200, 200, 255]), + &face([120, 80, 50, 255]), + &face([0, 0, 0, 0]), + ); + // The top is tinted grass, the sides dirt shaded, the corners empty. + assert_eq!(icon.get_pixel(32, 16)[1], (200. * 0xBD as f32 / 255.) as u8); + assert_eq!(icon.get_pixel(4, 40).0, [96, 64, 40, 255]); + assert_eq!(icon.get_pixel(60, 40).0, [72, 48, 30, 255]); + assert_eq!(icon.get_pixel(1, 1)[3], 0); + } +} diff --git a/launcher/src/browser/modrinth.rs b/launcher/src/browser/modrinth.rs index 77b5475bb..3fde04493 100644 --- a/launcher/src/browser/modrinth.rs +++ b/launcher/src/browser/modrinth.rs @@ -1,5 +1,5 @@ //! Modrinth's public API (api.modrinth.com/v2): no key, files with SHA-512 and SHA-1. -use super::{FileRef, Kind, Project, Query, Source, SourceId, Version, GAME}; +use super::{details, FileRef, Kind, Project, Query, Source, SourceId, Version, GAME}; use serde_json::Value; const API: &str = "https://api.modrinth.com/v2"; @@ -23,26 +23,34 @@ fn kind_of(project_type: &str) -> Option { .find(|k| self::project_type(*k) == project_type) } -/// The search's facets (AND of ORs): the kind, and unless `everything` what runs. +/// The search's facets (AND of ORs): the kind, the filters, and unless `everything` what runs. pub(super) fn facets(query: &Query) -> String { + let f = &query.filters; let mut all = vec![vec![format!("project_type:{}", project_type(query.kind))]]; - if !query.everything { - match query.kind { - Kind::ResourcePack => all.push(vec![format!("versions:{GAME}")]), - Kind::ShaderPack => { - all.push(vec!["categories:iris".into(), "categories:optifine".into()]) - } - Kind::Modpack => {} - Kind::Mod => all.push(vec![format!("versions:{GAME}")]), + match &f.game_version { + Some(v) => all.push(vec![format!("versions:{v}")]), + None if !query.everything && matches!(query.kind, Kind::ResourcePack | Kind::Mod) => { + all.push(vec![format!("versions:{GAME}")]) } + None => {} } - if query.kind == Kind::Mod { - all.push( - super::java::LOADERS - .iter() - .map(|l| format!("categories:{l}")) - .collect(), - ); + let loaders: Vec = match &f.loader { + Some(l) => vec![format!("categories:{l}")], + None => details::default_loaders(query.kind, query.everything) + .iter() + .map(|l| format!("categories:{l}")) + .collect(), + }; + if !loaders.is_empty() { + all.push(loaders); + } + for c in &f.categories { + all.push(vec![format!("categories:{c}")]); + } + match f.environment { + Some(details::Environment::Client) => all.push(vec!["client_side!=unsupported".into()]), + Some(details::Environment::Server) => all.push(vec!["server_side!=unsupported".into()]), + None => {} } serde_json::to_string(&all).unwrap_or_default() } @@ -51,11 +59,7 @@ pub(super) fn search_url(query: &Query) -> String { let mut url = format!( "{API}/search?facets={}&index={}&offset={}&limit={}", encode(&facets(query)), - if query.text.is_empty() { - "downloads" - } else { - "relevance" - }, + query.filters.sort(&query.text).modrinth(), query.offset, query.limit.clamp(1, 100), ); @@ -193,6 +197,27 @@ impl Source for Modrinth { all.sort_by(|a, b| b.published.cmp(&a.published)); Ok(all) } + + fn details(&self, project: &Project) -> Result { + let id = encode(&project.id); + let p = get(&format!("{API}/project/{id}"))?; + let members = get(&format!("{API}/project/{id}/members")).unwrap_or_default(); + Ok(details::modrinth_details(&p, &members)) + } + + fn tags(&self, kind: Kind) -> Result { + static TAGS: std::sync::Mutex> = std::sync::Mutex::new(None); + let mut cached = TAGS.lock().map_err(|_| "tags".to_string())?; + if cached.is_none() { + *cached = Some(( + get(&format!("{API}/tag/category"))?, + get(&format!("{API}/tag/loader"))?, + get(&format!("{API}/tag/game_version"))?, + )); + } + let (c, l, v) = cached.as_ref().expect("filled above"); + Ok(details::modrinth_tags(project_type(kind), c, l, v)) + } } fn get(url: &str) -> Result { diff --git a/launcher/src/main.rs b/launcher/src/main.rs index bb1e8dceb..5456c235a 100644 --- a/launcher/src/main.rs +++ b/launcher/src/main.rs @@ -9,6 +9,7 @@ mod markdown; mod mcp; mod modpacks_page; mod mods_page; +mod mods_project; mod perf; mod scrollbar; mod servers_page; diff --git a/launcher/src/mods_page.rs b/launcher/src/mods_page.rs index fd6a24b76..e66c02490 100644 --- a/launcher/src/mods_page.rs +++ b/launcher/src/mods_page.rs @@ -31,6 +31,8 @@ pub(super) enum Event { Updates(HashMap<(SourceId, String), String>), /// A Java mod's check: its jar read, or why it couldn't be. Checked((SourceId, String), Result, String>), + /// A project page's part, an image or a filter's values (`mods_project`). + Loaded(super::mods_project::Loaded), } /// Where a Java mod's check is. @@ -41,8 +43,8 @@ pub(super) enum Checking { } pub(super) struct Mods { - kind: PackKind, - source: SourceId, + pub(super) kind: PackKind, + pub(super) source: SourceId, text: String, focus: FocusHandle, pub(super) focused: bool, @@ -57,13 +59,15 @@ pub(super) struct Mods { installed: Vec, loaded: bool, /// The project being installed, and the line and fraction of its download. - busy: Option<(String, String, f32)>, + pub(super) busy: Option<(String, String, f32)>, message: Option>, updates: HashMap<(SourceId, String), String>, confirm_remove: Option, /// Java mods' checks, by source and project. - checks: HashMap<(SourceId, String), Checking>, - tx: Sender, + pub(super) checks: HashMap<(SourceId, String), Checking>, + /// The project page, filters and images. + pub(super) browse: super::mods_project::Browse, + pub(super) tx: Sender, rx: Receiver, } @@ -90,6 +94,7 @@ impl Mods { updates: HashMap::new(), confirm_remove: None, checks: HashMap::new(), + browse: Default::default(), tx, rx, } @@ -99,14 +104,14 @@ impl Mods { self.focused = self.focus.is_focused(window); } - fn entry(&self, p: &Project) -> Option<&installed::Entry> { + pub(super) fn entry(&self, p: &Project) -> Option<&installed::Entry> { self.installed .iter() .find(|e| e.source == p.source && e.project == p.id) } /// How well a result runs: a Java mod's from its check once it has one. - fn support(&self, p: &Project) -> Support { + pub(super) fn support(&self, p: &Project) -> Support { let metadata = p.support(); if p.kind != PackKind::Mod || !metadata.installable() { return metadata; @@ -142,6 +147,7 @@ impl Launcher { pub(super) fn mods_tick(&mut self) -> bool { let mut changed = false; let mut to_check = Vec::new(); + let mut loaded = Vec::new(); while let Ok(event) = self.mods.rx.try_recv() { changed = true; let m = &mut self.mods; @@ -184,13 +190,17 @@ impl Launcher { }; m.checks.insert(key, state); } + Event::Loaded(l) => loaded.push(l), } } self.mods_check(to_check); + for l in loaded { + self.mods_loaded(l); + } changed } - fn mods_search(&mut self, offset: usize) { + pub(super) fn mods_search(&mut self, offset: usize) { let m = &mut self.mods; m.search += 1; m.searching = true; @@ -202,6 +212,7 @@ impl Launcher { everything: m.everything, offset, limit: PAGE, + filters: m.browse.filters.clone(), ..Query::new(&m.text, m.kind) }; let (tx, search, source) = (m.tx.clone(), m.search, m.source); @@ -228,7 +239,9 @@ impl Launcher { for p in &todo { m.checks.insert((p.source, p.id.clone()), Checking::Running); } - let (tx, everything) = (m.tx.clone(), m.everything); + // A version other than 26.3 asked for: check the newest file for 1.21 and later. + let everything = m.everything || m.browse.filters.other_version(); + let tx = m.tx.clone(); let queue = std::sync::Mutex::new(todo.into_iter()); std::thread::spawn(move || { let home = backend::game_home(); @@ -273,14 +286,15 @@ impl Launcher { /// Installs (or updates) a project on a thread: a pack into its folder, a modpack into /// Modpacks. `known` is the project when the page has it; else it is asked for first. - fn mods_install(&mut self, source: SourceId, id: String, known: Option) { + pub(super) fn mods_install(&mut self, source: SourceId, id: String, known: Option) { if self.mods.busy.is_some() { return; } self.mods.message = None; self.mods.busy = Some((id.clone(), format!("Asking {}…", source.label()), 0.)); self.mods.updates.remove(&(source, id.clone())); - let (tx, everything) = (self.mods.tx.clone(), self.mods.everything); + let everything = self.mods.everything || self.mods.browse.filters.other_version(); + let tx = self.mods.tx.clone(); std::thread::spawn(move || { let last = std::cell::Cell::new(-1); let progress = |line: &str, f: f32| { @@ -429,6 +443,9 @@ impl Launcher { let _ = this.update(cx, |this, cx| { if this.mods.kind != kind { this.mods.kind = kind; + // Categories and loaders differ by kind. + this.mods.browse.filters = Default::default(); + this.mods.browse.open = None; this.mods_search(0); } cx.notify(); @@ -492,15 +509,39 @@ impl Launcher { .child(self.mods_field(cx)) .child(toggle), ) - .child(source_note); + .child(source_note) + .child(self.mods_filters(cx)); + if let Some(project) = self.mods_project_page(cx) { + return div() + .flex() + .flex_col() + .gap(px(32.)) + .child(header) + .children(self.mods_busy_plate()) + .child(project); + } let mut page = div() .flex() .flex_col() .gap(px(32.)) .child(header) .child(controls); + page = page.children(self.mods_busy_plate()); + if self.mods.kind == PackKind::Modpack { + page = page.child(self.imported_packs(cx)); + } + if let Some(list) = self.mods_installed_list(cx) { + page = page.child(list); + } + page.child(self.mods_results(cx)) + } + + /// The install under way and the last install's answer. + pub(super) fn mods_busy_plate(&self) -> Vec
{ + let t = self.theme; + let mut out = Vec::new(); if let Some((_, line, f)) = &self.mods.busy { - page = page.child( + out.push( plate(&t) .flex() .flex_col() @@ -517,25 +558,17 @@ impl Launcher { ); } match &self.mods.message { - Some(Ok(done)) => { - page = page.child( - plate(&t) - .p(px(16.)) - .text_size(px(theme::UI.px)) - .text_color(t.ink) - .child(done.clone()), - ) - } - Some(Err(e)) => page = page.child(notice(e.clone(), &t)), + Some(Ok(done)) => out.push( + plate(&t) + .p(px(16.)) + .text_size(px(theme::UI.px)) + .text_color(t.ink) + .child(done.clone()), + ), + Some(Err(e)) => out.push(notice(e.clone(), &t)), None => {} } - if self.mods.kind == PackKind::Modpack { - page = page.child(self.imported_packs(cx)); - } - if let Some(list) = self.mods_installed_list(cx) { - page = page.child(list); - } - page.child(self.mods_results(cx)) + out } fn mods_installed_list(&self, cx: &mut Context) -> Option
{ @@ -650,8 +683,10 @@ impl Launcher { ) } - fn mods_results(&self, cx: &mut Context) -> Div { + fn mods_results(&mut self, cx: &mut Context) -> Div { let t = self.theme; + let found = self.mods.results.clone(); + let icons: Vec = found.iter().map(|p| self.mods_icon(p, 40.)).collect(); let m = &self.mods; if let Some(e) = &m.error { return div().child(notice(e.clone(), &t)); @@ -667,6 +702,7 @@ impl Launcher { return centered(line, &t); } let busy = m.busy.as_ref().map(|b| b.0.as_str()); + let mut icons = icons.into_iter(); let mut list = div().flex().flex_col().border_t_1().border_color(t.rule); for (i, p) in m.results.iter().enumerate() { let support = m.support(p); @@ -746,9 +782,22 @@ impl Launcher { .reason() .map(|r| div().text_color(t.ink_soft).child(r.to_string())), ); + let open = p.clone(); + let name = div() + .id(SharedString::from(format!("mods-open-{}", p.id))) + .flex() + .items_center() + .gap(px(12.)) + .cursor_pointer() + .child(icons.next().expect("one icon per result")) + .child(div().hover(|s| s.underline()).child(p.title.clone())) + .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { + this.mods_open(open.clone()); + cx.notify(); + })); list = list.child(index_row( i + 1, - p.title.clone(), + name, Some(detail.into_any_element()), Some(right.into_any_element()), &t, diff --git a/launcher/src/mods_project.rs b/launcher/src/mods_project.rs new file mode 100644 index 000000000..ef1d97975 --- /dev/null +++ b/launcher/src/mods_project.rs @@ -0,0 +1,617 @@ +//! The Mods page's project page, search filters and project icons (`horizon_launcher::browser`: +//! `details` for the data, `images` for the icons and gallery). Drawn only with the launcher's +//! shared components, so a restyle of those restyles this. +//! +//! - A result's title opens its page inside the launcher: icon, author, downloads, followers, +//! licence, how well it runs and Install, then Description (Markdown), Gallery, Versions and +//! the project's links. +//! - Filters: sort, loader, game version, environment and categories, with the values the +//! source lists for the kind. +//! - Icons and gallery images come from the source's image servers at run time, cached on disk; +//! a placeholder stands in until one arrives. +use super::mods_page::{Checking, Event}; +use super::*; +use gpui::img; +use horizon_launcher::browser::{ + self, + details::{Details, Environment, Filters, Sort, Tags}, + images, java, Kind as PackKind, Project, SourceId, Support, Version, +}; +use std::collections::{HashMap, HashSet}; +use std::path::PathBuf; + +/// A project page's tabs. +#[derive(Clone, Copy, PartialEq, Eq)] +pub(super) enum Tab { + Description, + Gallery, + Versions, +} + +const TABS: [(Tab, &str); 3] = [ + (Tab::Description, "Description"), + (Tab::Gallery, "Gallery"), + (Tab::Versions, "Versions"), +]; + +/// The open project page. +pub(super) struct Opened { + pub(super) project: Project, + tab: Tab, + details: Option>, + versions: Option, String>>, +} + +/// The project page, filters and images' share of the Mods page's state. +#[derive(Default)] +pub(super) struct Browse { + pub(super) open: Option, + pub(super) filters: Filters, + pub(super) show_filters: bool, + tags: HashMap<(SourceId, PackKind), Tags>, + tags_asked: HashSet<(SourceId, PackKind)>, + /// Images on disk, by URL and size; `None` while they load or when they failed. + images: HashMap<(String, u32), Option>, + image_queue: Option>, +} + +/// What the background sends this module. +pub(super) enum Loaded { + Details((SourceId, String), Result), + Versions((SourceId, String), Result, String>), + Image((String, u32), Option), + Tags((SourceId, PackKind), Result), +} + +/// Images fetched at once. +const IMAGE_WORKERS: usize = 4; + +impl Launcher { + pub(super) fn mods_loaded(&mut self, loaded: Loaded) { + let b = &mut self.mods.browse; + fn open<'a>(b: &'a mut Browse, key: &(SourceId, String)) -> Option<&'a mut Opened> { + b.open + .as_mut() + .filter(|o| (o.project.source, &o.project.id) == (key.0, &key.1)) + } + match loaded { + Loaded::Details(key, d) => { + if let Some(o) = open(b, &key) { + o.details = Some(d); + } + } + Loaded::Versions(key, v) => { + if let Some(o) = open(b, &key) { + o.versions = Some(v); + } + } + Loaded::Image(key, path) => { + b.images.insert(key, path); + } + Loaded::Tags(key, Ok(tags)) => { + b.tags.insert(key, tags); + } + Loaded::Tags(_, Err(_)) => {} + } + } + + /// Opens a project's page and asks its source for the rest of it. + pub(super) fn mods_open(&mut self, project: Project) { + let key = (project.source, project.id.clone()); + let tx = self.mods.tx.clone(); + let p = project.clone(); + std::thread::spawn(move || { + let Some(source) = browser::source(p.source) else { + return; + }; + let d = source.details(&p); + let _ = tx.send(Event::Loaded(Loaded::Details(key.clone(), d))); + let v = source.versions(&p); + let _ = tx.send(Event::Loaded(Loaded::Versions(key, v))); + }); + self.mods.browse.open = Some(Opened { + project, + tab: Tab::Description, + details: None, + versions: None, + }); + } + + /// The image of `url` at `size` when it is on disk; else asks for it once. + fn mods_image(&mut self, url: &str, size: u32) -> Option { + let key = (url.to_owned(), size); + if let Some(state) = self.mods.browse.images.get(&key) { + return state.clone(); + } + let home = backend::game_home(); + if let Some(p) = images::cached(&home, url, size) { + self.mods.browse.images.insert(key, Some(p.clone())); + return Some(p); + } + self.mods.browse.images.insert(key.clone(), None); + if images::refuse(url).is_some() { + return None; + } + let b = &mut self.mods.browse; + let queue = b.image_queue.get_or_insert_with(|| { + let (send, receive) = std::sync::mpsc::channel::<(String, u32)>(); + let receive = std::sync::Arc::new(std::sync::Mutex::new(receive)); + for _ in 0..IMAGE_WORKERS { + let (receive, tx, home) = (receive.clone(), self.mods.tx.clone(), home.clone()); + std::thread::spawn(move || loop { + let Ok(next) = receive.lock().map(|r| r.recv()) else { + break; + }; + let Ok((url, size)) = next else { + break; + }; + let path = images::fetch(&home, &url, size).ok(); + if tx + .send(Event::Loaded(Loaded::Image((url, size), path))) + .is_err() + { + break; + } + }); + } + send + }); + let _ = queue.send(key); + None + } + + /// A project's icon, `side` pixels: its image, or its first letter on a plate until then. + pub(super) fn mods_icon(&mut self, p: &Project, side: f32) -> AnyElement { + let t = self.theme; + let path = p + .icon + .clone() + .and_then(|url| self.mods_image(&url, images::ICON)); + let frame = plate(&t) + .flex() + .flex_none() + .items_center() + .justify_center() + .size(px(side)) + .overflow_hidden(); + match path { + Some(path) => frame.child(img(path).size(px(side))).into_any_element(), + None => frame + .text_color(t.muted) + .child( + text(theme::UI).child( + p.title + .chars() + .next() + .map(|c| c.to_uppercase().to_string()) + .unwrap_or_default(), + ), + ) + .into_any_element(), + } + } + + /// The values the filters can take for the current kind and source, asked for once. + fn mods_tags(&mut self) -> Option<&Tags> { + let key = (self.mods.source, self.mods.kind); + if self.mods.browse.tags_asked.insert(key) { + let tx = self.mods.tx.clone(); + std::thread::spawn(move || { + let tags = browser::source(key.0) + .ok_or_else(|| "not set up".to_string()) + .and_then(|s| s.tags(key.1)); + let _ = tx.send(Event::Loaded(Loaded::Tags(key, tags))); + }); + } + self.mods.browse.tags.get(&key) + } + + /// Sets the filters and searches again. + fn mods_filter(&mut self, change: impl FnOnce(&mut Filters)) { + change(&mut self.mods.browse.filters); + self.mods_search(0); + } + + /// The filter bar: sort always; loader, version, environment and categories when shown. + pub(super) fn mods_filters(&mut self, cx: &mut Context) -> Div { + let t = self.theme; + let this = cx.entity().downgrade(); + let f = self.mods.browse.filters.clone(); + let sorts: Vec<(Option, &'static str)> = std::iter::once((None, "Best match")) + .chain(Sort::ALL.iter().map(|s| (Some(*s), s.label()))) + .collect(); + let sort = segments("mods-sort", &sorts, f.sort, &t, { + let this = this.clone(); + move |value, _, cx| { + let _ = this.update(cx, |this, cx| { + this.mods_filter(|f| f.sort = value); + cx.notify(); + }); + } + }); + let shown = self.mods.browse.show_filters; + let active = f.categories.len() + + f.loader.is_some() as usize + + f.game_version.is_some() as usize + + f.environment.is_some() as usize; + let toggle = button( + "mods-filters", + if active > 0 { + format!("Filters ({active})") + } else { + "Filters".to_string() + }, + if shown { Kind::Secondary } else { Kind::Ghost }, + &t, + ) + .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { + this.mods.browse.show_filters = !this.mods.browse.show_filters; + cx.notify(); + })); + let mut bar = div().flex().flex_col().gap(px(12.)).child( + div() + .flex() + .flex_wrap() + .items_center() + .gap(px(12.)) + .child(sort) + .child(toggle), + ); + if !shown { + return bar; + } + let Some(tags) = self.mods_tags().cloned() else { + return bar.child(caps("Loading filters").text_color(t.muted)); + }; + // A chip: on or off, one value of a filter. + let chip = |id: String, label: String, on: bool| { + button( + id, + label, + if on { Kind::Secondary } else { Kind::Ghost }, + &t, + ) + }; + let row = |title: &'static str| { + div() + .flex() + .flex_wrap() + .items_center() + .gap(px(4.)) + .child(caps(title).text_color(t.muted).w(px(120.))) + }; + let mut loaders = row("Loader"); + for l in &tags.loaders { + let on = f.loader.as_deref() == Some(l.as_str()); + let value = l.clone(); + loaders = loaders.child(chip(format!("mods-loader-{l}"), l.clone(), on).on_click( + cx.listener(move |this, _: &ClickEvent, _, cx| { + let v = value.clone(); + this.mods_filter(|f| f.loader = if on { None } else { Some(v) }); + cx.notify(); + }), + )); + } + let mut versions = row("Game version"); + if let Some(path) = self.vanilla_icon() { + versions = versions.child(img(path).size(px(20.)).flex_none()); + } + for v in tags.game_versions.iter().take(12) { + let on = f.game_version.as_deref() == Some(v.as_str()); + let value = v.clone(); + versions = versions.child(chip(format!("mods-version-{v}"), v.clone(), on).on_click( + cx.listener(move |this, _: &ClickEvent, _, cx| { + let v = value.clone(); + this.mods_filter(|f| f.game_version = if on { None } else { Some(v) }); + cx.notify(); + }), + )); + } + let mut environment = row("Environment"); + for e in Environment::ALL { + let on = f.environment == Some(e); + environment = environment.child( + chip(format!("mods-env-{}", e.label()), e.label().into(), on).on_click( + cx.listener(move |this, _: &ClickEvent, _, cx| { + this.mods_filter(|f| f.environment = if on { None } else { Some(e) }); + cx.notify(); + }), + ), + ); + } + let mut categories = row("Categories"); + for c in &tags.categories { + let on = f.categories.contains(&c.name); + let value = c.name.clone(); + categories = categories.child( + chip(format!("mods-cat-{}", c.name), c.name.replace('-', " "), on).on_click( + cx.listener(move |this, _: &ClickEvent, _, cx| { + let v = value.clone(); + this.mods_filter(|f| { + if !f.categories.contains(&v) { + f.categories.push(v); + } else { + f.categories.retain(|x| *x != v); + } + }); + cx.notify(); + }), + ), + ); + } + let clear = button("mods-clear", "Clear filters", Kind::Ghost, &t).on_click(cx.listener( + |this, _: &ClickEvent, _, cx| { + this.mods_filter(|f| *f = Filters::default()); + cx.notify(); + }, + )); + bar = bar + .child(loaders) + .child(versions) + .child(environment) + .child(categories); + if active > 0 { + bar = bar.child(div().flex().child(clear)); + } + bar + } + + /// Vanilla's mark, drawn from the player's prepared game files when there are some. + fn vanilla_icon(&self) -> Option { + let tag = self.selected().filter(|tag| backend::is_prepared(tag))?; + images::grass_block(&backend::game_home(), &backend::resources(tag)) + } + + /// The open project's page. + pub(super) fn mods_project_page(&mut self, cx: &mut Context) -> Option
{ + let t = self.theme; + let o = self.mods.browse.open.as_ref()?; + let p = o.project.clone(); + let tab = o.tab; + let support = self.mods.support(&p); + let color = match support { + Support::Works => t.success, + Support::Partial(_) => t.accent, + Support::No(_) => t.muted, + }; + let back = button("mods-back", "Back to results", Kind::Ghost, &t).on_click(cx.listener( + |this, _: &ClickEvent, _, cx| { + this.mods.browse.open = None; + cx.notify(); + }, + )); + let details = o.details.clone(); + let versions = o.versions.clone(); + let mut meta = vec![format!("{} downloads", thousands(p.downloads))]; + if let Some(Ok(d)) = &details { + meta.push(format!("{} followers", thousands(d.followers))); + if let Some(day) = d.updated.get(..10) { + meta.push(format!("updated {day}")); + } + } + if !p.license.is_empty() { + meta.push(p.license.clone()); + } + meta.push(format!("from {}", p.source.label())); + let authors = match &details { + Some(Ok(d)) if !d.authors.is_empty() => d.authors.join(", "), + _ => p.author.clone(), + }; + let icon = self.mods_icon(&p, 72.); + let busy = self.mods.busy.as_ref().map(|b| b.0.clone()); + let installed = self.mods.entry(&p).is_some(); + let checking = matches!( + self.mods.checks.get(&(p.source, p.id.clone())), + None | Some(Checking::Running) + ) && p.kind == PackKind::Mod + && p.support().installable(); + let mut action = div().flex().items_center().gap(px(8.)).child( + caps(if checking { + "Checking" + } else { + support.label() + }) + .flex_none() + .text_color(color), + ); + if busy.as_deref() == Some(p.id.as_str()) { + action = action.child(caps("Installing").text_color(t.accent)); + } else if installed { + action = action.child(caps("Installed").text_color(t.muted)); + } else if support.installable() && !checking { + let label = if p.kind == PackKind::Modpack { + "Import" + } else { + "Install" + }; + let mut install = button("mods-page-install", label, Kind::Primary, &t); + if busy.is_some() { + install = disabled(install); + } else { + let p = p.clone(); + install = install.on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { + this.mods_install(p.source, p.id.clone(), Some(p.clone())); + cx.notify(); + })); + } + action = action.child(install); + } + let head = div() + .flex() + .items_center() + .gap(px(20.)) + .child(icon) + .child( + div() + .flex() + .flex_col() + .flex_1() + .min_w_0() + .gap(px(4.)) + .child(text(theme::TITLE).text_color(t.ink).child(p.title.clone())) + .child( + text(theme::UI) + .text_color(t.muted) + .child(if authors.is_empty() { + p.summary.clone() + } else { + format!("by {authors} · {}", p.summary) + }), + ) + .child( + text(theme::CAPTION) + .text_color(t.muted) + .child(meta.join(" · ")), + ), + ) + .child(action); + let mut page = div() + .flex() + .flex_col() + .gap(px(24.)) + .child(div().flex().child(back)) + .child(head); + if let Some(why) = support.reason() { + page = page.child( + text(theme::UI) + .text_color(t.ink_soft) + .child(why.to_string()), + ); + } + if let Some(Checking::Done(c)) = self.mods.checks.get(&(p.source, p.id.clone())) { + let lines = |title: &str, list: &[String]| { + div() + .flex() + .flex_col() + .gap(px(2.)) + .child(caps(title).text_color(t.muted)) + .children( + list.iter() + .map(|l| text(theme::CAPTION).text_color(t.ink_soft).child(l.clone())), + ) + }; + let mut what = div().flex().gap(px(32.)); + if !c.ports.is_empty() { + what = what.child(lines("Ports", &c.ports)); + } + if !c.skipped.is_empty() { + what = what.child(lines("Doesn't port", &c.skipped)); + } + page = page.child(what); + } + if let Some(Ok(d)) = &details { + let mut links = div().flex().flex_wrap().gap(px(8.)); + let page_url = p.page.clone(); + if !page_url.is_empty() { + links = links.child( + button( + "mods-link-page", + format!("{} page", p.source.label()), + Kind::Ghost, + &t, + ) + .on_click(move |_, _, cx| cx.open_url(&page_url)), + ); + } + for (i, l) in d.links.iter().enumerate() { + let url = l.url.clone(); + links = links.child( + button(format!("mods-link-{i}"), l.label.clone(), Kind::Ghost, &t) + .on_click(move |_, _, cx| cx.open_url(&url)), + ); + } + page = page.child(links); + } + let this = cx.entity().downgrade(); + page = page.child(div().flex().child(segments("mods-tab", &TABS, tab, &t, { + move |value, _, cx| { + let _ = this.update(cx, |this, cx| { + if let Some(o) = &mut this.mods.browse.open { + o.tab = value; + } + cx.notify(); + }); + } + }))); + let body = match tab { + Tab::Description => match &details { + None => centered("Loading…", &t), + Some(Err(e)) => notice(e.clone(), &t), + Some(Ok(d)) => div().max_w(px(760.)).child(markdown::render(&d.body, &t)), + }, + Tab::Gallery => match &details { + None => centered("Loading…", &t), + Some(Err(e)) => notice(e.clone(), &t), + Some(Ok(d)) if d.gallery.is_empty() => centered("No gallery images.", &t), + Some(Ok(d)) => { + let mut grid = div().flex().flex_wrap().gap(px(16.)); + for g in &d.gallery { + let shot = match self.mods_image(&g.url, images::GALLERY) { + Some(path) => img(path).w(px(360.)).h(px(203.)).into_any_element(), + None => plate(&t).w(px(360.)).h(px(203.)).into_any_element(), + }; + grid = grid.child( + div() + .flex() + .flex_col() + .gap(px(6.)) + .w(px(360.)) + .child(plate(&t).overflow_hidden().child(shot)) + .when(!g.title.is_empty(), |d| { + d.child( + text(theme::CAPTION) + .text_color(t.ink_soft) + .child(g.title.clone()), + ) + }), + ); + } + grid + } + }, + Tab::Versions => match &versions { + None => centered("Loading…", &t), + Some(Err(e)) => notice(e.clone(), &t), + Some(Ok(list)) => { + let mut rows = div().flex().flex_col().border_t_1().border_color(t.rule); + for (i, v) in list.iter().take(50).enumerate() { + let file = v.file(); + let mut detail = vec![ + v.published.get(..10).unwrap_or_default().to_string(), + v.loaders.join(", "), + ]; + let games: Vec<&str> = v + .game_versions + .iter() + .rev() + .take(4) + .map(String::as_str) + .collect(); + detail.push(games.join(", ")); + if let Some(f) = file { + detail.push(format!("{} · {:.1} MB", f.name, f.size as f64 / 1e6)); + } + let checked = file.and_then(|f| { + (p.kind == PackKind::Mod) + .then(|| java::cached(&backend::game_home(), f)) + .flatten() + }); + rows = rows.child(index_row( + i + 1, + v.number.clone(), + Some(detail.join(" · ").into_any_element()), + checked.map(|c| { + caps(c.verdict.label()) + .text_color(t.muted) + .into_any_element() + }), + &t, + )); + } + rows + } + }, + }; + Some(page.child(body)) + } +} From b1dc23551b93ed2e307c42073b560c0cb7ae6734 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 18:02:25 +0200 Subject: [PATCH 08/11] tools: end-to-end check of a Java mod from the mod browser mod_browser_harness.py searches, checks and installs a mod through the launcher's mods CLI in a scratch game folder, opens a world with it offscreen, places one of its blocks and crafts one of its recipes on a crafting table, and times the world load with and without it. Macaw's Fences: check 4.0 s, install 3.1 s, world load 5.4 s -> 7.3 s, hedge placed, 4 crafted. Co-Authored-By: Claude Opus 5.5 --- docs/mod-browser.md | 13 ++ tools/mod_browser_harness.py | 270 +++++++++++++++++++++++++++++++++++ 2 files changed, 283 insertions(+) create mode 100755 tools/mod_browser_harness.py diff --git a/docs/mod-browser.md b/docs/mod-browser.md index 6601fea70..fe86328db 100644 --- a/docs/mod-browser.md +++ b/docs/mod-browser.md @@ -51,6 +51,19 @@ or GeckoLib) is listed as not ported. A mod that shares its id or a namespace wi game reads mod content when it starts: start it again after installing. **Remove** deletes the folder and turns it off. +### End-to-end check + +`tools/mod_browser_harness.py [--mod SLUG] [--query TEXT] [--keep]` runs the whole path a player takes, in a scratch +game folder: the launcher's `mods search --kind mod`, `mods check` and `mods install` (Modrinth), then the game +opens a survival world with the mod offscreen (horizon-ctl), places one of its blocks, and crafts one of its recipes +from vanilla ingredients on a crafting table and takes the result. It times the world's load with and without the +mod and prints a JSON summary; it exits 1 when a step fails. It needs the launcher, the game and horizon-ctl built +and the prepared 26.3 resources in `.local`. + +On the Mac mini (M4 Pro, offscreen) with Macaw's Fences and Walls (the default): check 4.0 s, install 3.1 s, world +load 5.4 s without mods and 7.3 s with it (resources 1.1 s and 2.6 s: the mod content folder is built on the first +start); the hedge places and its recipe crafts 4. + ## Checks on every download - The file must come from Modrinth's or CurseForge's file servers (`cdn.modrinth.com`, `edge.forgecdn.net`, diff --git a/tools/mod_browser_harness.py b/tools/mod_browser_harness.py new file mode 100755 index 000000000..4531570bc --- /dev/null +++ b/tools/mod_browser_harness.py @@ -0,0 +1,270 @@ +#!/usr/bin/env python3 +"""End-to-end harness for Java mods from the launcher's mod browser (docs/mod-browser.md). + + mod_browser_harness.py [--mod SLUG] [--query TEXT] [--keep] + +In a scratch game folder, as a player would through the Mods page (its CLI): + +1. `horizon-launcher mods search --kind mod TEXT` finds the mod on Modrinth; +2. `horizon-launcher mods check SLUG` downloads its jar (checksum checked) and says Works or Partly; +3. `horizon-launcher mods install SLUG` ports it into horizon-mods/ and turns it on; +4. the game opens a survival world with it (horizon-ctl, offscreen), and +5. a ported block item is given, held, and placed: the world then holds the mod's block; +6. a ported recipe with vanilla ingredients is crafted on a crafting table: the result slot shows + the mod's item and taking it puts it in the inventory. + +It also times the install and the world's load with the mod against the same world with none +(a ported mod must not slow a game that doesn't use it; this measures what one that does costs). +Prints one JSON line per step and a summary; exits 1 when a step fails. + +Needs the launcher (launcher/target/debug/horizon-launcher, built with +`--features gpui/runtime_shaders` on macOS), the game and horizon-ctl +(target/fast/horizon-client, target/fast/horizon-ctl, or --bin DIR) and the prepared resources in +.local/horizon-client/26.3. Downloads from Modrinth. +""" +import argparse +import json +import os +import re +import shutil +import subprocess +import sys +import tempfile +import time +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] + + +class Failed(Exception): + pass + + +def run(cmd, env, cwd=None, timeout=600): + p = subprocess.run(cmd, env=env, cwd=cwd, capture_output=True, text=True, timeout=timeout) + if p.returncode != 0: + raise Failed(f"{' '.join(map(str, cmd))}: exit {p.returncode}: {p.stderr.strip()[-800:] or p.stdout.strip()[-800:]}") + return p.stdout + + +def report(step, **fields): + print(json.dumps({"step": step, **fields}), flush=True) + + +class Game: + def __init__(self, ctl, client, resources, home, env): + self.ctl, self.client, self.resources, self.home, self.env = ctl, client, resources, home, env + self.socket = str(home / "ctl.sock") + self.pid = None + + def __call__(self, *args, timeout=120): + out = run([self.ctl, "--socket", self.socket, "--timeout", str(timeout), *map(str, args)], self.env, + timeout=timeout + 30) + return json.loads(out.strip().splitlines()[-1]) + + def launch(self, world): + start = time.monotonic() + state = self("launch", "--game", self.client, "--world", world, "--seed", "1234", "--wait", "--offscreen", + "--resources", self.resources, timeout=300) + self.pid = state.get("launch", {}).get("pid") + return time.monotonic() - start, state + + def quit(self): + try: + self("quit", timeout=30) + except Exception: + pass + if self.pid: + for _ in range(50): + try: + os.kill(self.pid, 0) + except OSError: + break + time.sleep(0.2) + else: + os.kill(self.pid, 9) + self.pid = None + + +def resources_loaded(log): + """Seconds the game's log says its resources took.""" + m = re.findall(r"resources loaded in ([0-9.]+)s", Path(log).read_text(errors="replace")) + return float(m[-1]) if m else None + + +def pick_block(mod): + """A block the carry declares with its item.""" + for f in sorted((mod / "data").glob("*/horizon/block/*.json")): + decl = json.loads(f.read_text()) + if decl.get("item", True): + return f"{f.parts[-4]}:{f.stem}" + raise Failed("the carry declares no block with an item") + + +def pick_recipe(mod, ns): + """A crafting recipe of the mod's whose result is its own and whose ingredients are plain vanilla + item ids, shaped first.""" + found = [] + for f in sorted((mod / "data").glob("*/recipe/**/*.json")): + try: + r = json.loads(f.read_text()) + except ValueError: + continue + result = (r.get("result") or {}).get("id", "") + if not result.startswith(ns + ":"): + continue + if r.get("type") == "minecraft:crafting_shaped": + keys = r.get("key", {}) + if all(isinstance(v, str) and v.startswith("minecraft:") for v in keys.values()) and \ + len(r["pattern"]) <= 3 and all(len(row) <= 3 for row in r["pattern"]): + found.append((0, f.name, r)) + elif r.get("type") == "minecraft:crafting_shapeless": + ing = r.get("ingredients", []) + if ing and len(ing) <= 9 and all(isinstance(v, str) and v.startswith("minecraft:") for v in ing): + found.append((1, f.name, r)) + if not found: + raise Failed("no crafting recipe of the mod's with vanilla ingredients") + return sorted(found, key=lambda x: (x[0], x[1]))[0][2] + + +def grid_cells(recipe): + """(slot, item) for each grid cell of a 3x3 crafting table (slots 100-108).""" + if recipe["type"] == "minecraft:crafting_shaped": + return [(100 + 3 * y + x, recipe["key"][c]) for y, row in enumerate(recipe["pattern"]) + for x, c in enumerate(row) if c != " "] + return [(100 + i, item) for i, item in enumerate(recipe["ingredients"])] + + +def inventory_slot(game, item): + inv = game("inventory") + for s in inv["hotbar"] + inv["main"]: + if s.get("item") == item: + return s["slot"] + raise Failed(f"{item} is not in the inventory: {inv.get('counts')}") + + +def main(): + ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + ap.add_argument("--mod", default="macaws-fences-and-walls", help="Modrinth slug of a Works or Partly mod") + ap.add_argument("--query", default="macaw", help="the search text that finds it") + ap.add_argument("--launcher", default=str(ROOT / "launcher/target/debug/horizon-launcher")) + ap.add_argument("--bin", default=str(ROOT / "target/fast"), help="folder with horizon-client and horizon-ctl") + ap.add_argument("--resources", default=str(ROOT / ".local/horizon-client/26.3")) + ap.add_argument("--keep", action="store_true", help="keep the scratch game folder") + args = ap.parse_args() + + home = Path(tempfile.mkdtemp(prefix="horizon-mod-browser-")) + env = dict(os.environ, HORIZON_HOME=str(home), HORIZON_NO_AUDIO="1", HORIZON_NO_HEARTBEAT="1", + HORIZON_CONTROL=str(home / "ctl.sock")) + launcher = [args.launcher] + game = Game(str(Path(args.bin) / "horizon-ctl"), str(Path(args.bin) / "horizon-client"), + args.resources, home, env) + summary = {"mod": args.mod} + ok = False + try: + # A baseline world first, with no mod: the same world's load time with the mod is compared. + base_home = home / "vanilla" + base_home.mkdir() + base = Game(game.ctl, game.client, args.resources, base_home, + dict(env, HORIZON_HOME=str(base_home), HORIZON_CONTROL=str(base_home / "ctl.sock"))) + base.socket = str(base_home / "ctl.sock") + os.chdir(base_home) + took, _ = base.launch("browser") + summary["world_load_without_mods_s"] = round(took, 2) + summary["resources_without_mods_s"] = resources_loaded(base_home / "ctl.log") + base.quit() + + found = json.loads(run(launcher + ["--json", "mods", "search", "--kind", "mod", *args.query.split()], env)) + slugs = [p["slug"] for p in found] + report("search", query=args.query, results=len(found), found=args.mod in slugs) + if args.mod not in slugs: + raise Failed(f"{args.mod} not in the results: {slugs[:10]}") + + start = time.monotonic() + check = json.loads(run(launcher + ["--json", "mods", "check", args.mod], env).strip().splitlines()[-1]) + summary["check_s"] = round(time.monotonic() - start, 2) + report("check", verdict=check["label"], summary=check["summary"], seconds=summary["check_s"]) + if check["verdict"] not in ("works", "partly"): + raise Failed(f"{args.mod} doesn't run: {check['summary']}") + + start = time.monotonic() + run(launcher + ["mods", "install", args.mod], env) + summary["install_s"] = round(time.monotonic() - start, 2) + mod_id = check["mod_id"] + mod = home / "horizon-mods" / mod_id + on = (home / "horizon-mods.txt").read_text() + report("install", folder=str(mod.relative_to(home)), turned_on=f"mod.{mod_id}:true" in on, + seconds=summary["install_s"]) + if not (mod / "horizon-mod.toml").is_file() or f"mod.{mod_id}:true" not in on: + raise Failed("not installed or not turned on") + + os.chdir(home) + took, state = game.launch("browser") + summary["world_load_with_mod_s"] = round(took, 2) + summary["resources_with_mod_s"] = resources_loaded(home / "ctl.log") + log = (home / "ctl.log").read_text(errors="replace") + loaded = "content from 1 mod(s)" in log + report("world", seconds=round(took, 2), content_loaded=loaded, screen=state.get("screen")) + if not loaded: + raise Failed("the game didn't load the mod's content") + + x, y, z = (int(v // 1) for v in state["player"]["position"]) + block = pick_block(mod) + game("command", "gamemode survival") + game("command", f"fill {x + 1} {y - 1} {z - 2} {x + 4} {y - 1} {z + 2} minecraft:stone") + game("command", f"fill {x + 1} {y} {z - 2} {x + 4} {y + 2} {z + 2} minecraft:air") + game("give", block, 1) + game("select", block) + held = game("state")["held"] + placed = game("place", x + 2, y, z, "--item", block) + at = game("block", x + 2, y, z) + report("place", block=block, held=held, placed=at.get("block")) + if at.get("block") != block: + raise Failed(f"placing {block} left {at.get('block')}") + + recipe = pick_recipe(mod, mod_id) + result = recipe["result"]["id"] + # A server command: horizon-ctl's own setblock changes the client's world only. + game("command", f"setblock {x + 2} {y} {z + 2} minecraft:crafting_table") + cells = grid_cells(recipe) + game("select", 9) + game("interact", x + 2, y, z + 2) + if game("screen").get("craft_size") != 3: + raise Failed("the crafting table didn't open") + # One item at a time, picked up and put down with left clicks: quick right clicks can + # cross the server's answers and be dropped. + for slot, item in cells: + game("give", item, 1) + time.sleep(0.2) + game("click-slot", inventory_slot(game, item)) + time.sleep(0.2) + game("click-slot", slot) + time.sleep(0.2) + screen = game("screen") + out = next((s for s in screen.get("slots", []) if s.get("slot") == 200), {}) + report("craft", recipe=result, result_slot=out.get("item"), count=out.get("count")) + if out.get("item") != result: + raise Failed(f"the table shows {out.get('item')}, not {result}") + game("click-slot", 200, "shift") + time.sleep(0.3) + counts = game("inventory")["counts"] + report("crafted", inventory=counts.get(result, 0)) + if counts.get(result, 0) < 1: + raise Failed(f"{result} didn't reach the inventory") + game("close") + ok = True + except Failed as e: + report("failed", why=str(e)) + finally: + game.quit() + summary["ok"] = ok + print(json.dumps({"summary": summary}), flush=True) + if args.keep: + print(f"kept {home}", file=sys.stderr) + else: + shutil.rmtree(home, ignore_errors=True) + return 0 if ok else 1 + + +if __name__ == "__main__": + sys.exit(main()) From f926d1d2b13fafc6d82c8abc7e893b650e631a03 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 18:28:58 +0200 Subject: [PATCH 09/11] Modpacks from the mod browser: ported in the same step, mods loaded together Importing a modpack from the Mods page (or `mods install`) now ports it right away for the selected version, so Play works straight after. port-pack leaves a namespace two carried mods both claim to none of them (YUNG's mods all ship `yungsapi`, which made the game drop three of them), and the launcher applies the game's first-wins rule before it starts a pack, listing what it left off. Measured on Vanilla Perfected, Slimes Adventure and All the Mods 10 (docs/modpacks.md): 2, 20 and 42 mods ported, every world loads, no crash. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-port-pack/src/carry.rs | 12 +++ crates/horizon-port-pack/src/pipeline.rs | 114 ++++++++++++++++++++++- docs/mod-browser.md | 11 ++- docs/modpacks.md | 29 ++++++ launcher/src/cli.rs | 14 +-- launcher/src/modpacks.rs | 63 ++++++++++++- launcher/src/modpacks_page.rs | 3 + launcher/src/mods_page.rs | 11 ++- website/docs/modpacks.md | 6 +- 9 files changed, 243 insertions(+), 20 deletions(-) diff --git a/crates/horizon-port-pack/src/carry.rs b/crates/horizon-port-pack/src/carry.rs index c1fe2b32a..03b5d7dc9 100644 --- a/crates/horizon-port-pack/src/carry.rs +++ b/crates/horizon-port-pack/src/carry.rs @@ -582,6 +582,18 @@ fn extra_namespaces( claimed } +/// The namespaces besides `id` the mod in `jar` would own ([`extra_namespaces`]), for the +/// pack to find the ones several mods claim. +pub fn namespaces_claimed( + jar: &Path, + id: &str, + pack_mod: &dyn Fn(&str) -> bool, +) -> Result, String> { + let file = fs::File::open(jar).map_err(|e| format!("{}: {e}", jar.display()))?; + let mut zip = zip::ZipArchive::new(file).map_err(|e| format!("{}: {e}", jar.display()))?; + Ok(extra_namespaces(&mut zip, id, pack_mod)) +} + fn contains(haystack: &[u8], needle: &[u8]) -> bool { haystack.windows(needle.len()).any(|w| w == needle) } diff --git a/crates/horizon-port-pack/src/pipeline.rs b/crates/horizon-port-pack/src/pipeline.rs index 26744d7bd..c34f58411 100644 --- a/crates/horizon-port-pack/src/pipeline.rs +++ b/crates/horizon-port-pack/src/pipeline.rs @@ -150,7 +150,16 @@ pub fn carry( }) .flatten() .collect(); - let in_pack = |m: &str| in_pack.contains(m); + // A namespace two carried mods both claim (a library's they both use, such as YUNG's API's + // `yungsapi`, whose own jar isn't read) is neither's: either would refuse the other in the + // game. Its files stay outside both. + let claims = records.iter().filter(|r| chosen(r)).filter_map(|r| { + let (jar, info) = (r.jar.as_ref()?, r.info.as_ref()?); + let owned = carry::namespaces_claimed(jar, &info.mod_id, &|m| in_pack.contains(m)).ok()?; + Some((r.key.as_str(), owned)) + }); + let shared = shared_namespaces(claims, log); + let in_pack = |m: &str| in_pack.contains(m) || shared.contains(m); let pack = carry::Pack { loaded: &loaded, mods: &in_pack, @@ -219,6 +228,61 @@ pub fn carry( Ok(done) } +/// Which carried-over mods (`mods//carry` folders) the game loads together, in the order +/// given: as the game does, the first of two owning one namespace stays on. Returns the ones on, +/// and a line for each one left off. +pub fn loadable(mods: &[std::path::PathBuf]) -> (Vec, Vec) { + let mut on: Vec<(std::path::PathBuf, String, Vec)> = Vec::new(); + let mut off = Vec::new(); + for path in mods { + let manifest = fs::read_to_string(path.join(horizon_mods::manifest::MANIFEST)) + .map_err(|e| e.to_string()) + .and_then(|t| horizon_mods::Manifest::parse(&t)); + let m = match manifest { + Ok(m) => m, + Err(e) => { + off.push(format!("{}: {e}", path.display())); + continue; + } + }; + let owned: Vec = std::iter::once(m.id.clone()) + .chain(m.namespaces.iter().cloned()) + .collect(); + let clash = on + .iter() + .find_map(|(_, id, ns)| horizon_mods::content::clash((id, ns), (&m.id, &owned))); + match clash { + Some(why) => off.push(format!("{} not loaded: {why}", m.id)), + None => on.push((path.clone(), m.id, owned)), + } + } + (on.into_iter().map(|(p, _, _)| p).collect(), off) +} + +/// The namespaces more than one mod claims, from each mod's (key, claimed namespaces). +fn shared_namespaces<'a>( + claims: impl Iterator)>, + log: Log, +) -> BTreeSet { + let mut by_namespace: BTreeMap> = BTreeMap::new(); + for (key, owned) in claims { + for ns in owned { + by_namespace.entry(ns).or_default().push(key); + } + } + by_namespace + .into_iter() + .filter(|(_, mods)| mods.len() > 1) + .map(|(ns, mods)| { + log(format!( + "namespace {ns} is claimed by {}: left to none of them", + mods.join(", ") + )); + ns + }) + .collect() +} + /// The pack's title as the report heads it. pub fn title(pack: &import::Pack) -> String { format!( @@ -279,3 +343,51 @@ pub fn port( } Ok((fetched, report(work, log)?)) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_namespace_two_mods_claim_is_neither_s() { + let claims = [ + ("betterdungeons", vec!["yungsapi".to_owned()]), + ("betterfortresses", vec!["yungsapi".to_owned()]), + ( + "towns_and_towers", + vec!["kaisyn".to_owned(), "towns_and_towers".to_owned()], + ), + ]; + let shared = shared_namespaces(claims.into_iter(), &|_| {}); + assert_eq!(shared.into_iter().collect::>(), ["yungsapi"]); + } + + #[test] + fn the_first_of_two_mods_owning_a_namespace_loads() { + let root = std::env::temp_dir().join(format!("port-pack-loadable-{}", std::process::id())); + let _ = fs::remove_dir_all(&root); + let carry = |key: &str, namespaces: &str| { + let dir = root.join(key).join("carry"); + fs::create_dir_all(&dir).unwrap(); + fs::write( + dir.join("horizon-mod.toml"), + format!( + "id = \"{key}\"\nname = \"{key}\"\nversion = \"1\"\napi = \"0.2\"\n\ + capabilities = [\"registries\"]\nnamespaces = [{namespaces}]\n" + ), + ) + .unwrap(); + dir + }; + let mods = [ + carry("betterdungeons", "\"yungsapi\""), + carry("betterfortresses", "\"yungsapi\""), + carry("ruby", ""), + ]; + let (on, off) = loadable(&mods); + assert_eq!(on, [mods[0].clone(), mods[2].clone()]); + assert_eq!(off.len(), 1); + assert!(off[0].starts_with("betterfortresses not loaded"), "{off:?}"); + fs::remove_dir_all(&root).unwrap(); + } +} diff --git a/docs/mod-browser.md b/docs/mod-browser.md index 74959ab4c..8c29e0dd6 100644 --- a/docs/mod-browser.md +++ b/docs/mod-browser.md @@ -118,15 +118,20 @@ same name the player put there is never overwritten. Packs, as in vanilla and Iris. - **Update** installs the newer version and removes the old file. - **Remove** deletes the file and its entry. Packs installed as dependencies stay. -- **A modpack** is downloaded (checked the same way) into `ports/downloads/` and imported; the Modpacks tab says - what of it Horizon runs and ports it. +- **A modpack** is downloaded (checked the same way) into `ports/downloads/`, imported, and ported straight away + for the selected version when it is prepared (`modpacks::import_and_port`): the jars of the mods Horizon can carry + over come from the pack's links or Modrinth, and the message says how many of its files play. **Play** on the + Modpacks tab starts it with every ported mod on together. A ported mod that owns a namespace an earlier one owns + (the game would refuse it) is left off before the game starts and listed under the pack ("Left off: ..."); the + coverage list says why each mod that didn't port didn't. A port that fails leaves the pack imported, with **Port** + to try again. Measured on three packs in [modpacks.md](modpacks.md#three-packs-through-the-mod-browser-measured). From a terminal (or an agent): ```sh horizon-launcher mods search faithful # --kind mod|modpack|resource|shader, --everything, --source horizon-launcher mods check macaws-fences-and-walls # a Java mod's verdict, what ports and what doesn't (--json) -horizon-launcher mods install faithful-32x # id or slug; a Java mod is ported, a modpack imported +horizon-launcher mods install faithful-32x # id or slug; a Java mod is ported, a modpack imported and ported horizon-launcher mods list # --json horizon-launcher mods update [faithful-32x] [--check] horizon-launcher mods remove faithful-32x diff --git a/docs/modpacks.md b/docs/modpacks.md index d062eb941..e233f176c 100644 --- a/docs/modpacks.md +++ b/docs/modpacks.md @@ -240,6 +240,10 @@ How the game loads them, without touching vanilla's path: when its Java original ships them so (`namespaces = ["kaisyn", "towns_and_towers"]` for Towns and Towers, mod id `t_and_t`; docs/mods.md). No namespace has two owners: a mod whose id or listed namespace another mod that is on already owns is left out, with both named in the log. + `horizon-port-pack` avoids that inside a pack: a namespace two carried mods both claim (YUNG's + structure mods all ship files under `yungsapi`, the namespace of YUNG's API, whose own jar isn't + carried) is left to none of them, and its files stay outside. The launcher applies the game's + rule before it starts a pack (`pipeline::loadable`) and lists what it left off. - The overlay is built on the first start with a set of mods and kept for later starts. A stamp (`horizon-mod-content.stamp`) hashes the game executable, the base tables and every mod file's size and mtime. Adding, removing or editing a mod, or a new build of Horizon, rebuilds it. For @@ -1047,6 +1051,31 @@ What is missing, ranked by how much a player notices: 6. **Smaller gaps:** Mo' Structures' mixin that keeps lakes out of its structures and its two block-ageing processors, and the 5 missing block models. +### Three packs through the mod browser, measured + +2026-10-22, Mac mini M4 Pro (offscreen, a debug launcher, release-profile `horizon-port-pack` and game), while +other sessions kept the CPU at 85 to 93 %, so times are upper bounds. The two Modrinth packs were imported with +`horizon-launcher mods install ` (the browser's Import), ATM10 from its CurseForge zip with `modpack import` +(CurseForge isn't searchable without a key), then each ported with `--resources` for 26.3 and opened as a survival +world with every ported mod on (horizon-ctl `launch --dev-mod ...`, from a scratch script). + +| Pack | Files | Import | Port | Ported mods | Coverage | Declared | World load (resources) | Crashes | +|---|---|---|---|---|---|---|---|---| +| Vanilla Perfected 26.3 (Fabric, small) | 108 | 30.4 s | 1.0 s | 2 | 2 partial, 79 not yet, 27 not needed | 0 blocks (a trail effect, a torch-flower texture) | 5.2 s (2.1 s) | none | +| Slimes Adventure 1.13.4 (Fabric 1.21.1, medium) | 105 | 18.8 s | 22.8 s | 20 | 20 partial, 46 not yet, 39 not needed | 8,208 blocks, 41 items, 1,376 recipes | 23.0 s (15.0 s) | none | +| All the Mods 10 8.3 (NeoForge 1.21.1, CurseForge zip) | 496 | 3.1 s | 29.4 s | 42 | 42 partial, 333 not yet, 121 not needed | 10,000 blocks, 1,273 items, 4,178 recipes | 38.3 s (28.7 s) | none | + +The same world with no mods loads in 5.4 s (resources 1.1 s). Import is the pack download and Modrinth's answers by +file hash; Vanilla Perfected's 30 s is mostly its 108 lookups and its download. Port is the jars (downloaded for the +two Modrinth packs; ATM10's 42 copied from a folder, 12 more are on CurseForge only and need its key) and the carry, +which writes tens of thousands of small files. World load grows with the blocks the pack declares: ATM10 adds +about 155,000 block states, and loading their models and textures is most of its 28.7 s. + +Every world loaded and stayed up, with no panic and no error in the log. Before the namespace fix above, Slimes +Adventure lost three of YUNG's mods at start ("both use the namespace yungsapi"); after it all 20 load. As a share of +the mods that need porting (not counting performance mods and libraries), 2 of 81 ported partly, 20 of 66 and 42 of +375. Every ported mod is Partial: their Java code (block entities, entities, Mixins) doesn't come along. + ## Open questions for Ludovic - Contacting authors of permissively licensed mods to publish ports (none contacted). diff --git a/launcher/src/cli.rs b/launcher/src/cli.rs index ebcc95cd6..089cf8284 100644 --- a/launcher/src/cli.rs +++ b/launcher/src/cli.rs @@ -1053,13 +1053,13 @@ fn mods(action: ModsCmd, as_json: bool) -> Result<(), String> { let file = installed::fetch_modpack(&*source, &p, &folder, &progress(p.title.clone()))?; let log = &|line: String| eprintln!("{line}"); - let pack = modpacks::import(&file, log)?; - println!( - "Imported {} ({}): horizon-launcher modpack port {}", - pack.title(), - pack.summary(), - pack.slug - ); + let resources = backend::resolve_version(None) + .ok() + .filter(|t| backend::is_prepared(t)) + .map(|t| backend::resources(&t)); + let pack = modpacks::import_and_port(&file, resources.as_deref(), log)?; + println!("{} ({})", modpacks::outcome(&pack), pack.summary()); + println!("Play it: horizon-launcher modpack play {}", pack.slug); return Ok(()); } let done = installed::install(&*source, &p, everything, &home, &line)?; diff --git a/launcher/src/modpacks.rs b/launcher/src/modpacks.rs index 2495e1ad6..67325e624 100644 --- a/launcher/src/modpacks.rs +++ b/launcher/src/modpacks.rs @@ -45,6 +45,8 @@ pub struct Imported { pub fetched: Fetched, /// The carried-over mods the profile loads. pub mods: Vec, + /// Carried-over mods left off because an earlier one owns a namespace of theirs, with why. + pub left_off: Vec, /// The mods the port step gets and carries over (content, data tier). pub portable: usize, } @@ -105,6 +107,7 @@ pub fn load(work: &Path) -> Result { let rows: Vec = pipeline::read_json(&work.join("coverage.json")).unwrap_or_default(); let records: Vec = pipeline::read_json(&work.join("inventory.json")).unwrap_or_default(); + let (mods, left_off) = pipeline::loadable(&carried(work)); Ok(Imported { slug: work .file_name() @@ -114,7 +117,8 @@ pub fn load(work: &Path) -> Result { pack, rows, fetched: Fetched::load(work), - mods: carried(work), + mods, + left_off, portable: records.iter().filter(|r| pipeline::portable(r)).count(), }) } @@ -176,6 +180,47 @@ pub fn port( load(work) } +/// What the mod browser does with a modpack: imports it, then ports what this computer can +/// (from the pack's links and Modrinth), so it plays straight away. A port that fails leaves the +/// pack imported; the error says so. +pub fn import_and_port( + path: &Path, + resources: Option<&Path>, + log: Log, +) -> Result { + let pack = import(path, log)?; + if pack.portable == 0 { + return Ok(pack); + } + port(&pack.work, resources, None, log) + .map_err(|e| format!("{} is imported, but porting it failed: {e}", pack.title())) +} + +/// One line on what an import and port gave: the mods carried over and the ones that weren't. +pub fn outcome(pack: &Imported) -> String { + let not = pack.count(Status::NotYet); + format!( + "{} is imported{}: {} of its {} files play in Horizon ({} ported, {} partly), {} don't \ + yet, {} aren't needed{}. Its list says why.", + pack.title(), + if pack.mods.is_empty() { + "" + } else { + " and ported" + }, + pack.count(Status::Ported) + pack.count(Status::Partial), + pack.rows.len(), + pack.count(Status::Ported), + pack.count(Status::Partial), + not, + pack.count(Status::NotNeeded), + match pack.left_off.len() { + 0 => String::new(), + n => format!("; {n} ported mods are left off, sharing a namespace with another"), + }, + ) +} + /// Removes an imported pack: its jars, ports and the profile's saves. pub fn remove(work: &Path) -> Result<(), String> { if work.parent() != Some(ports_dir().as_path()) { @@ -192,7 +237,8 @@ pub fn profile_home(work: &Path) -> PathBuf { /// Turns the game's command into this pack's profile: its own home (the player's options.txt /// copied in the first time, so keys and video settings carry over) and its carried-over mods. pub fn apply_profile(cmd: &mut Command, work: &Path) -> Result<(), String> { - let mods = carried(work); + // Only mods that load together: the game would refuse the later of two sharing a namespace. + let (mods, _) = pipeline::loadable(&carried(work)); if mods.is_empty() { return Err("Nothing is ported yet: port the pack first.".into()); } @@ -229,12 +275,19 @@ mod tests { let work = root.join("ports/small"); let mut cmd = Command::new("true"); assert!(apply_profile(&mut cmd, &work).is_err()); - for id in ["a", "b"] { + for (id, namespaces) in [("a", ""), ("b", ""), ("d", "\"a\"")] { let carry = work.join("mods").join(id).join("carry"); fs::create_dir_all(&carry).unwrap(); - fs::write(carry.join("horizon-mod.toml"), "").unwrap(); + fs::write( + carry.join("horizon-mod.toml"), + format!( + "id = \"{id}\"\nname = \"{id}\"\nversion = \"1\"\napi = \"0.2\"\n\ + capabilities = [\"registries\"]\nnamespaces = [{namespaces}]\n" + ), + ) + .unwrap(); } - // Left out: no manifest. + // Left out: no manifest (c), and one owning a namespace of an earlier mod (d). fs::create_dir_all(work.join("mods/c/carry")).unwrap(); apply_profile(&mut cmd, &work).unwrap(); let args: Vec = cmd diff --git a/launcher/src/modpacks_page.rs b/launcher/src/modpacks_page.rs index b776321b3..f3e2f1d7b 100644 --- a/launcher/src/modpacks_page.rs +++ b/launcher/src/modpacks_page.rs @@ -327,6 +327,9 @@ impl Launcher { if let Some(jars) = p.jars() { notes.push(jars); } + for line in &p.left_off { + notes.push(format!("Left off: {line}.")); + } if !can_play { notes.push("Playing needs a prepared Horizon version (Play page).".into()); } diff --git a/launcher/src/mods_page.rs b/launcher/src/mods_page.rs index e66c02490..214048519 100644 --- a/launcher/src/mods_page.rs +++ b/launcher/src/mods_page.rs @@ -294,6 +294,11 @@ impl Launcher { self.mods.busy = Some((id.clone(), format!("Asking {}…", source.label()), 0.)); self.mods.updates.remove(&(source, id.clone())); let everything = self.mods.everything || self.mods.browse.filters.other_version(); + // A modpack is ported for the selected version as soon as it is imported. + let resources = self + .selected() + .filter(|tag| backend::is_prepared(tag)) + .map(|tag| backend::resources(tag)); let tx = self.mods.tx.clone(); std::thread::spawn(move || { let last = std::cell::Cell::new(-1); @@ -318,10 +323,10 @@ impl Launcher { progress(&p.title, f) })?; let log = |line: String| progress(&line, 1.); - let pack = modpacks::import(&file, &log)?; + let pack = modpacks::import_and_port(&file, resources.as_deref(), &log)?; return Ok(format!( - "{} is imported: it is listed above, ready to port.", - pack.title() + "{} Play it from the list above.", + modpacks::outcome(&pack) )); } let home = backend::game_home(); diff --git a/website/docs/modpacks.md b/website/docs/modpacks.md index e3ed053d9..a1aa1e2ea 100644 --- a/website/docs/modpacks.md +++ b/website/docs/modpacks.md @@ -8,7 +8,7 @@ Java mod jars do not run in Horizon. Instead, the launcher reads a modpack, tell Open **Modpacks** in the launcher. (From the next launcher release, it is the **Modpacks** tab of the **Mods** page.) -1. **Import a pack.** Choose **Import a pack file…** for a Modrinth `.mrpack` or a CurseForge zip, or **Import an instance folder…** for a Prism, MultiMC or CurseForge instance. The **Mods** page can also find a Modrinth modpack and import it for you (see [Mods and data packs](/docs/mods#finding-resource-packs-shader-packs-and-modpacks)). +1. **Import a pack.** Choose **Import a pack file…** for a Modrinth `.mrpack` or a CurseForge zip, or **Import an instance folder…** for a Prism, MultiMC or CurseForge instance. The **Mods** page can also find a Modrinth modpack and import it for you (see [Mods and data packs](/docs/mods#finding-resource-packs-shader-packs-and-modpacks)). From the next launcher release it also ports it in the same step, so you can press **Play** right away. 2. **Read the coverage report.** For every mod in the pack it says **Ported**, **Partial**, **Not yet** or **Not needed**, with the reason. You can filter the list by status. 3. **Port.** **Port N mods** gets the jars of the mods this computer can carry over today, and carries them over for the version you picked. It copies the jars from a folder you have, or downloads them from the pack's own links (or the same file from Modrinth's download network), and checks their hashes. CurseForge downloads are coming soon, so for files that only CurseForge has, choose **Use jars from my instance…** and point it at your installed instance. 4. **Play.** **Play with N mods** starts the game as the pack's own profile, with its own saves and options, and those mods on. @@ -29,6 +29,10 @@ Nothing is uploaded or shared. The mod jars and what is made from them stay on y So a pack is a list of mods, and today Horizon can bring over a part of that list. The coverage report is the honest answer for your pack. +If two ported mods use the same namespace, the game can only load one of them. The launcher keeps the first, leaves the other off, and lists it under the pack. + +Measured on three packs: Vanilla Perfected (108 files) ports 2 mods, Slimes Adventure (105) ports 20, and All the Mods 10 (496) ports 42. Each opens a world, with no crash. Every ported mod is partial: its blocks, items, recipes and structures come over, its Java code doesn't. A world with All the Mods 10's 42 ported mods takes about 40 seconds to load, against about 5 without mods. + ## Example: All the Mods 10 All the Mods 10 has 495 jars. Horizon's report sorts them: about 290 add content, 80 are libraries, and the rest are integrations, interface mods, tools and performance mods. A few dozen of the content mods carry over today. The measured numbers are in [docs/modpacks.md](https://github.com/ludovic111/horizon/blob/main/docs/modpacks.md). From da991045f9e30c210cf8168b213849b76d18b3a9 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 18:54:33 +0200 Subject: [PATCH 10/11] Modpacks: jars only CurseForge serves are left off, "CurseForge downloads coming soon" The pack lists them with the mods left off, the port's log and fetch label say the same, and docs/modpacks.md no longer asks for a key. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-port-pack/src/fetch.rs | 6 ++-- docs/modpacks.md | 12 +++---- launcher/src/modpacks.rs | 45 ++++++++++++++++++++++++--- 3 files changed, 50 insertions(+), 13 deletions(-) diff --git a/crates/horizon-port-pack/src/fetch.rs b/crates/horizon-port-pack/src/fetch.rs index f8f7b2dc7..f2803baa2 100644 --- a/crates/horizon-port-pack/src/fetch.rs +++ b/crates/horizon-port-pack/src/fetch.rs @@ -40,7 +40,7 @@ pub enum How { Downloaded, /// Copied from the player's folder. Local, - /// Only on CurseForge, whose downloads need an API key. + /// Only on CurseForge, whose downloads are coming soon (its API needs a paid key). NeedsKey, NotFound, Failed, @@ -52,7 +52,7 @@ impl How { How::Present => "already here", How::Downloaded => "downloaded", How::Local => "copied from your folder", - How::NeedsKey => "needs a CurseForge key", + How::NeedsKey => "CurseForge downloads coming soon", How::NotFound => "not found", How::Failed => "failed", } @@ -163,7 +163,7 @@ pub fn fetch( } write_json(&work.join("fetch.json"), &fetched)?; log(format!( - "jars: {} downloaded, {} copied, {} already here, {} need a CurseForge key, {} not found, {} failed", + "jars: {} downloaded, {} copied, {} already here, {} on CurseForge only (CurseForge downloads coming soon), {} not found, {} failed", fetched.count(How::Downloaded), fetched.count(How::Local), fetched.count(How::Present), diff --git a/docs/modpacks.md b/docs/modpacks.md index e233f176c..c15d0767e 100644 --- a/docs/modpacks.md +++ b/docs/modpacks.md @@ -326,8 +326,8 @@ Fabric API doesn't run (its id has a dash, which content mods can't use). A chec by SHA-1, or through a CurseForge app instance's `minecraftinstance.json`), else downloaded from the link the pack itself gives (a `.mrpack`'s, on the format's hosts only) or, for a CurseForge pack, the same file on Modrinth's public CDN, found by its SHA-1. A jar is kept only when its -hash matches. A file only CurseForge has is listed as needing a CurseForge API key (Ludovic's -decision; open question below), with the player's installed instance as the way round it. +hash matches. A file only CurseForge has is listed as "CurseForge downloads coming soon" (its API +needs a paid key; Ludovic, 2026-10-22), with the player's installed instance as the way round it. `/fetch.json` says what happened to each file. On All the Mods 10 8.3's CurseForge zip (2026-10-17): of the 54 data-tier content mods, 42 come from Modrinth's CDN (84 MB) and 12 need the key or the instance; `port` then carries the 42 over (0 ported, 42 partial: the harness is a @@ -1056,7 +1056,7 @@ What is missing, ranked by how much a player notices: 2026-10-22, Mac mini M4 Pro (offscreen, a debug launcher, release-profile `horizon-port-pack` and game), while other sessions kept the CPU at 85 to 93 %, so times are upper bounds. The two Modrinth packs were imported with `horizon-launcher mods install ` (the browser's Import), ATM10 from its CurseForge zip with `modpack import` -(CurseForge isn't searchable without a key), then each ported with `--resources` for 26.3 and opened as a survival +(CurseForge support is coming soon), then each ported with `--resources` for 26.3 and opened as a survival world with every ported mod on (horizon-ctl `launch --dev-mod ...`, from a scratch script). | Pack | Files | Import | Port | Ported mods | Coverage | Declared | World load (resources) | Crashes | @@ -1067,7 +1067,7 @@ world with every ported mod on (horizon-ctl `launch --dev-mod ...`, from a scrat The same world with no mods loads in 5.4 s (resources 1.1 s). Import is the pack download and Modrinth's answers by file hash; Vanilla Perfected's 30 s is mostly its 108 lookups and its download. Port is the jars (downloaded for the -two Modrinth packs; ATM10's 42 copied from a folder, 12 more are on CurseForge only and need its key) and the carry, +two Modrinth packs; ATM10's 42 copied from a folder, 12 more are on CurseForge only: CurseForge downloads coming soon) and the carry, which writes tens of thousands of small files. World load grows with the blocks the pack declares: ATM10 adds about 155,000 block states, and loading their models and textures is most of its 28.7 s. @@ -1081,5 +1081,5 @@ the mods that need porting (not counting performance mods and libraries), 2 of 8 - Contacting authors of permissively licensed mods to publish ports (none contacted). - Whether players should run code-port agents themselves (their own Claude Code or Codex login, as Zenith does) or only download published ports. -- A CurseForge API key for the launcher (CurseForge grants them to launchers on request). Until - then, CurseForge packs import from an installed instance. +- CurseForge: dropped for now (its API needs a paid key; Ludovic, 2026-10-22), shown as coming + soon. CurseForge packs import from their zip or an installed instance. diff --git a/launcher/src/modpacks.rs b/launcher/src/modpacks.rs index 67325e624..909c6cfae 100644 --- a/launcher/src/modpacks.rs +++ b/launcher/src/modpacks.rs @@ -45,7 +45,8 @@ pub struct Imported { pub fetched: Fetched, /// The carried-over mods the profile loads. pub mods: Vec, - /// Carried-over mods left off because an earlier one owns a namespace of theirs, with why. + /// Mods left off, with why: a ported one an earlier one owns a namespace of, or one whose + /// jar only CurseForge serves (CurseForge downloads coming soon). pub left_off: Vec, /// The mods the port step gets and carries over (content, data tier). pub portable: usize, @@ -107,7 +108,15 @@ pub fn load(work: &Path) -> Result { let rows: Vec = pipeline::read_json(&work.join("coverage.json")).unwrap_or_default(); let records: Vec = pipeline::read_json(&work.join("inventory.json")).unwrap_or_default(); - let (mods, left_off) = pipeline::loadable(&carried(work)); + let (mods, mut left_off) = pipeline::loadable(&carried(work)); + let fetched = Fetched::load(work); + left_off.extend( + fetched + .files + .iter() + .filter(|o| o.how == How::NeedsKey) + .map(|o| format!("{}: CurseForge downloads coming soon", o.key)), + ); Ok(Imported { slug: work .file_name() @@ -116,7 +125,7 @@ pub fn load(work: &Path) -> Result { work: work.to_path_buf(), pack, rows, - fetched: Fetched::load(work), + fetched, mods, left_off, portable: records.iter().filter(|r| pipeline::portable(r)).count(), @@ -216,7 +225,7 @@ pub fn outcome(pack: &Imported) -> String { pack.count(Status::NotNeeded), match pack.left_off.len() { 0 => String::new(), - n => format!("; {n} ported mods are left off, sharing a namespace with another"), + n => format!("; {n} are left off (listed under the pack)"), }, ) } @@ -267,6 +276,34 @@ mod tests { assert_eq!(slug(" "), "pack"); } + #[test] + fn jars_only_curseforge_serves_are_left_off_as_coming_soon() { + use horizon_port_pack::fetch::Outcome; + let work = std::env::temp_dir().join(format!("horizon-launcher-cf-{}", std::process::id())); + let _ = fs::remove_dir_all(&work); + fs::create_dir_all(&work).unwrap(); + pipeline::write_json(&work.join("pack.json"), &Pack::default()).unwrap(); + let fetched = Fetched { + files: vec![Outcome { + key: "allthemodium".into(), + file: "mods/allthemodium.jar".into(), + how: How::NeedsKey, + detail: String::new(), + }], + }; + pipeline::write_json(&work.join("fetch.json"), &fetched).unwrap(); + let pack = load(&work).unwrap(); + assert_eq!( + pack.left_off, + ["allthemodium: CurseForge downloads coming soon"] + ); + assert!(pack + .jars() + .unwrap() + .contains("CurseForge downloads coming soon")); + fs::remove_dir_all(&work).unwrap(); + } + #[test] fn a_profile_has_its_own_home_and_mods() { let root = From a4e68e6a53a9ac54773cd13cd31e182a993890c5 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 01:20:51 +0200 Subject: [PATCH 11/11] Mount inventories for a server's players, and llama inventories A server's players (single player on its server thread, LAN guests, dedicated-server players) get the horse inventory: shift + use on a tamed horse, donkey, mule, camel or llama, or the inventory key while riding one (ServerboundPlayerCommandPacket OPEN_INVENTORY), opens HorseInventoryMenu over the mount with ClientboundMountScreenOpenPacket. Clicks run on the menu engine's new Kind::Mount (saddle and body ArmorSlots that take what the mount may wear, the chest, AbstractMountInventoryMenu.quickMoveStack), and the slots go back to the mount with the item's equip sound. The client shows a server's mount screen, predicts its clicks with the same engine and sends OPEN_INVENTORY when the inventory key is pressed while riding. Llamas now have their inventory in the window too: the carpet slot and a chest of `strength` columns, no saddle slot. lan_e2e `mounts` checks it for the host and a guest, with and without --server-thread. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 14 ++ crates/horizon-client/src/game/lan_actions.rs | 21 +- .../horizon-client/src/game/lan_mount_menu.rs | 222 ++++++++++++++++++ crates/horizon-client/src/game/llamas.rs | 3 +- crates/horizon-client/src/game/mount_menu.rs | 206 +++++++++++++--- crates/horizon-client/src/game/remote.rs | 45 ++++ crates/horizon-client/src/lan/containers.rs | 17 ++ crates/horizon-client/src/lan/hub.rs | 4 + crates/horizon-client/src/menu.rs | 138 ++++++++++- crates/horizon-protocol/src/packets/play.rs | 9 + tools/lan_e2e.py | 102 +++++++- 11 files changed, 745 insertions(+), 36 deletions(-) create mode 100644 crates/horizon-client/src/game/lan_mount_menu.rs diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 52bbad605..72017eea7 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -72,6 +72,7 @@ mod lan_chat; mod lan_crafting; mod lan_entities; mod lan_mobs; +mod lan_mount_menu; mod lan_pvp; mod lan_screen; mod lan_stations; @@ -5488,6 +5489,16 @@ impl Game<'_> { let mut v = vec![None; 45]; v.extend((0..9).map(Some)); (Kind::ItemPicker, v) + } else if let Some(columns) = self + .mount_menu_columns() + .filter(|_| self.remote.is_some() && self.mount_active()) + { + // A server's HorseInventoryMenu: the saddle, the body armour, + // the chest, then the inventory. + let mut v = vec![Some(mount_menu::SADDLE), Some(mount_menu::ARMOR)]; + v.extend((0..columns * 3).map(|i| Some(mount_menu::CHEST + i))); + v.extend((9..36).chain(0..9).map(Some)); + (Kind::Mount(columns), v) } else if merchant_open { // MerchantMenu: two payment slots, the result, then the inventory. let mut v: Vec> = vec![ @@ -5620,6 +5631,9 @@ impl Game<'_> { None => game_of.iter().position(|x| *x == Some(g)), }; let mut menu = Menu::new(kind); + if matches!(kind, Kind::Mount(_)) { + menu.mount = self.mount_menu_entity(); + } let total = menu.slot_count(); let before: Vec> = game_of .iter() diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 569f4937b..0d70b16f6 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -57,6 +57,8 @@ pub(super) struct GuestPlay { pub(super) crafting: super::lan_crafting::GuestCrafting, /// The workstation menu the guest has open (game/lan_stations.rs). pub(super) station: Option, + /// The mount inventory the guest has open (game/lan_mount_menu.rs). + pub(super) mount_window: Option, /// The lectern the guest reads (game/lan_stations.rs). pub(super) lectern: Option, /// What the guest's body was given for its effects and enchantments. @@ -493,7 +495,9 @@ impl Game<'_> { _ => None, }; self.command_block_screen = host_command_block; - self.mount_menu = host_mount_menu; + // A mount inventory the guest's action opened (shift + use, or the + // inventory key while riding). + let guest_mount = std::mem::replace(&mut self.mount_menu, host_mount_menu).opened_mob(); self.keys = host_keys; let g = &mut host.hub.guests[i]; let left_vehicle = g.vehicle.filter(|_| guest_vehicle.is_none()); @@ -616,6 +620,9 @@ impl Game<'_> { if opened_table { self.lan_open_crafting(host, conn); } + if let Some(mob) = guest_mount { + self.lan_open_mount(host, conn, mob); + } } /// `ServerLevel.players()` in turn: the host's own player, then each @@ -971,6 +978,14 @@ impl Game<'_> { S::ContainerClick(p) if self.lan_machine_click(host, conn, &p) => {} S::ContainerClick(p) if self.lan_station_click(host, conn, &p) => {} S::ContainerClick(p) if self.lan_lectern_click(host, conn, &p) => {} + S::ContainerClick(p) if self.lan_mount_click(host, conn, &p) => {} + // ServerboundPlayerCommandPacket OPEN_INVENTORY: the ridden + // mount's inventory (`HasCustomInventoryScreen`). + S::PlayerCommand(p) if p.action == 5 => { + self.as_guest(host, conn, |g| { + g.open_ridden_mount_menu(); + }); + } S::ContainerButtonClick(p) => { self.lan_station_button(host, conn, p.container, p.button); } @@ -988,7 +1003,8 @@ impl Game<'_> { } S::ContainerClose(p) if self.lan_crafting_closed(host, conn, Some(p.container)) - || self.lan_station_closed(host, conn, Some(p.container)) => + || self.lan_station_closed(host, conn, Some(p.container)) + || self.lan_mount_closed(host, conn, Some(p.container)) => { let g = &mut host.hub.guests[gi]; if let Some(c) = g.carried.take() { @@ -1528,6 +1544,7 @@ impl Game<'_> { pub(super) fn tick_guests(&mut self, host: &mut LanHost) { self.lan_merchant_tick(host); self.lan_stations_tick(host); + self.tick_lan_mount_menus(host); self.announce_guest_advancements(host); let conns: Vec = host .hub diff --git a/crates/horizon-client/src/game/lan_mount_menu.rs b/crates/horizon-client/src/game/lan_mount_menu.rs new file mode 100644 index 000000000..78b306252 --- /dev/null +++ b/crates/horizon-client/src/game/lan_mount_menu.rs @@ -0,0 +1,222 @@ +//! The horse inventory for a server's players (`ServerPlayer.openHorseInventory`): +//! shift + use on a tamed horse, donkey, mule, camel or llama, or the +//! inventory key while riding one (`ServerboundPlayerCommandPacket` +//! OPEN_INVENTORY), opens `HorseInventoryMenu` over the mount with +//! `ClientboundMountScreenOpenPacket`. Clicks run on the menu engine +//! (`Kind::Mount`) with the mount's saddle, body item and chest copied in and +//! written back, and the slots go to the client after each change, as +//! `broadcastChanges` does. +use super::{Game, lan::LanHost, lan_crafting::input}; +use horizon_client::{ + items::ItemStack, + menu::{Kind, Menu}, +}; +use horizon_protocol::packets::play::{self, Clientbound as C}; + +/// A guest's open mount menu. +pub(super) struct GuestMount { + window: i32, + mob: u32, + columns: usize, + state: i32, + /// The menu's slots and the carried stack the client was last sent. + sent: Option<(Vec>, Option)>, +} + +impl Game<'_> { + /// `ServerPlayer.openHorseInventory` for mount `mob`: the open menu + /// closes, the client gets the mount screen and the menu's slots. + pub(super) fn lan_open_mount(&mut self, host: &mut LanHost, conn: u32, mob: u32) { + let Some(index) = self.mobs.iter().position(|m| m.id == mob) else { + return; + }; + let columns = super::mount_menu::mount_columns(&self.mobs[index]); + self.lan_mount_closed(host, conn, None); + self.lan_station_closed(host, conn, None); + self.lan_crafting_closed(host, conn, None); + self.guest_container_closed(host, conn); + let Some(window) = host.hub.open_mount_window(conn, columns as i32, mob as i32) else { + return; + }; + host.play.entry(conn).or_default().mount_window = Some(GuestMount { + window, + mob, + columns, + state: 0, + sent: None, + }); + self.lan_send_mount(host, conn); + } + + /// `HorseInventoryMenu` over mount `index`, with the player's inventory + /// loaded by the caller. + fn mount_engine_menu(&self, index: usize, columns: usize) -> Menu { + let env = self.menu_env(); + let mut menu = Menu::new(Kind::Mount(columns)); + menu.mount = Some(self.mobs[index].kind.id.to_string()); + let (saddle, body, items) = self.mount_contents(index); + menu.set_slot(&env, 0, saddle); + menu.set_slot(&env, 1, body.flatten()); + for (k, item) in items.into_iter().take(columns * 3).enumerate() { + menu.set_slot(&env, 2 + k, item); + } + menu + } + + /// The menu's slots (the mount's, then the inventory and the hotbar) + /// and the carried stack, sent when they changed since the last time. + fn lan_send_mount(&mut self, host: &mut LanHost, conn: u32) { + let Some(gm) = host.play.get(&conn).and_then(|p| p.mount_window.as_ref()) else { + return; + }; + let (mob, columns) = (gm.mob, gm.columns); + let Some(index) = self.mobs.iter().position(|m| m.id == mob) else { + return; + }; + let Some(g) = host.hub.guests.iter().find(|g| g.conn == conn) else { + return; + }; + let (saddle, body, items) = self.mount_contents(index); + let mut slots = vec![saddle, body.flatten()]; + slots.extend(items.into_iter().take(columns * 3)); + slots.extend((9..36).chain(0..9).map(|i| g.inventory[i].clone())); + let carried = g.carried.clone(); + let now = Some((slots, carried)); + let Some(gm) = host + .play + .get_mut(&conn) + .and_then(|p| p.mount_window.as_mut()) + else { + return; + }; + if gm.sent == now { + return; + } + gm.state = (gm.state + 1) & 0x7FFF; + let (window, state) = (gm.window, gm.state); + let (slots, carried) = now.clone().unwrap_or_default(); + gm.sent = now; + let content = C::ContainerSetContent(play::ContainerSetContent { + container: window, + state, + items: slots.iter().map(|s| host.hub.stack(s)).collect(), + carried: host.hub.stack(&carried), + }); + host.hub.send_to(conn, &content); + } + + /// A click on the guest's mount window; false for other windows. + pub(super) fn lan_mount_click( + &mut self, + host: &mut LanHost, + conn: u32, + p: &play::ContainerClick, + ) -> bool { + let Some((mob, columns)) = host + .play + .get(&conn) + .and_then(|pl| pl.mount_window.as_ref()) + .filter(|m| m.window == p.container) + .map(|m| (m.mob, m.columns)) + else { + return false; + }; + if let Some(gi) = host.hub.guests.iter().position(|g| g.conn == conn) + && !host.hub.guests[gi].spectator() + && let Some(index) = self.mobs.iter().position(|m| m.id == mob) + { + let mut menu = self.mount_engine_menu(index, columns); + self.guest_menu_op(host, gi, &mut menu, |menu, env| { + let _ = menu.click(env, i32::from(p.slot), i32::from(p.button), input(p.input)); + }); + self.lan_mount_store(index, columns, &menu); + } + // The client is told what the server has, whatever it predicted. + if let Some(gm) = host + .play + .get_mut(&conn) + .and_then(|pl| pl.mount_window.as_mut()) + { + gm.sent = None; + } + self.lan_send_mount(host, conn); + true + } + + /// The engine menu's mount slots back on mount `index`. + fn lan_store_slots(menu: &Menu, columns: usize) -> Vec> { + (0..columns * 3) + .map(|k| menu.slot(2 + k).cloned()) + .collect() + } + fn lan_mount_store(&mut self, index: usize, columns: usize, menu: &Menu) { + let (_, body, _) = self.mount_contents(index); + let body = body.map(|_| menu.slot(1).cloned()); + let items = Self::lan_store_slots(menu, columns); + self.mount_store(index, menu.slot(0).cloned(), body, items); + } + + /// `AbstractContainerMenu.removed` of the guest's mount menu (`window`; + /// None closes it whichever it is): the carried stack goes back to the + /// inventory. False when no such window is open. + pub(super) fn lan_mount_closed( + &mut self, + host: &mut LanHost, + conn: u32, + window: Option, + ) -> bool { + let closed = host + .play + .get_mut(&conn) + .and_then(|p| { + p.mount_window + .take_if(|m| window.is_none_or(|w| m.window == w)) + }) + .is_some(); + if closed + && let Some(gi) = host.hub.guests.iter().position(|g| g.conn == conn) + && let Some(carried) = host.hub.guests[gi].carried.take() + { + self.lan_place_back(host, gi, vec![carried]); + } + closed + } + + /// Each tick: a mount menu whose mount died or is out of reach closes + /// (`AbstractMountInventoryMenu.stillValid`: alive, within the entity + /// interaction range plus 4 of the eyes), and the others follow the + /// mount's and the inventory's slots. + pub(super) fn tick_lan_mount_menus(&mut self, host: &mut LanHost) { + let open: Vec<(u32, u32, i32)> = host + .play + .iter() + .filter_map(|(c, p)| p.mount_window.as_ref().map(|m| (*c, m.mob, m.window))) + .collect(); + for (conn, mob, window) in open { + let Some(g) = host.hub.guests.iter().find(|g| g.conn == conn) else { + continue; + }; + let eye = [g.position[0], g.position[1] + g.eye_height(), g.position[2]]; + let valid = self.mobs.iter().find(|m| m.id == mob).is_some_and(|m| { + let (w, h) = (f64::from(m.width()) / 2.0, f64::from(m.height())); + let p = [m.position.x, m.position.y, m.position.z]; + let lo = [p[0] - w, p[1], p[2] - w]; + let hi = [p[0] + w, p[1] + h, p[2] + w]; + let d: f64 = (0..3) + .map(|k| (eye[k] - eye[k].clamp(lo[k], hi[k])).powi(2)) + .sum(); + // Player.ENTITY_INTERACTION_RANGE 3, plus 4. + m.alive() && d < 7.0 * 7.0 + }); + if valid { + self.lan_send_mount(host, conn); + } else { + self.lan_mount_closed(host, conn, Some(window)); + host.hub.send_to( + conn, + &C::ContainerClose(play::ContainerClose { container: window }), + ); + } + } + } +} diff --git a/crates/horizon-client/src/game/llamas.rs b/crates/horizon-client/src/game/llamas.rs index b9be87e3e..1ce518f31 100644 --- a/crates/horizon-client/src/game/llamas.rs +++ b/crates/horizon-client/src/game/llamas.rs @@ -93,7 +93,8 @@ impl Game<'_> { return false; } if tamed && sneaking { - // The horse inventory screen is not available yet. + // AbstractHorse.openCustomInventoryScreen. + self.open_mount_menu(index); return true; } let m = &self.mobs[index]; diff --git a/crates/horizon-client/src/game/mount_menu.rs b/crates/horizon-client/src/game/mount_menu.rs index 1db2210d3..e9e1e380b 100644 --- a/crates/horizon-client/src/game/mount_menu.rs +++ b/crates/horizon-client/src/game/mount_menu.rs @@ -1,12 +1,15 @@ //! The horse inventory (`HorseInventoryMenu`, `HorseInventoryScreen`): the -//! saddle and body armour slots, the 3x5 chest of donkeys and mules and the -//! mount drawn following the cursor. It opens with E while riding a tamed -//! mount or with shift + use on a tamed one. +//! saddle and body armour slots (a llama's carpet), the chest of donkeys, +//! mules and llamas and the mount drawn following the cursor. It opens with +//! E while riding a tamed mount or with shift + use on a tamed one. On a +//! server the screen shows the server's menu (`ClientboundMountScreenOpenPacket`) +//! and its clicks go out as container clicks; a server's players get theirs +//! from game/lan_mount_menu.rs. use super::{Game, Screen}; use horizon_client::{ hud::{HudBatch, rgba}, items::ItemStack, - mobs::{Mob, Species, equine}, + mobs::{Mob, Species, equine, llama}, }; /// Menu slot ids: the saddle, the body armour and the chest from 710. @@ -17,11 +20,43 @@ pub(super) const CHEST: usize = 710; const CHEST_SIZE: usize = 15; const COLUMNS: usize = 5; +/// `AbstractHorse.getInventoryColumns`: five for a donkey's or mule's chest, +/// a llama's strength for its chest, else none. +pub(super) fn mount_columns(m: &Mob) -> usize { + let species = m.species(); + if llama::is_llama(species) { + llama::inventory_columns(m).clamp(0, 5) as usize + } else if m.extra.chested && matches!(species, Species::Donkey | Species::Mule) { + COLUMNS + } else { + 0 + } +} +/// Whether the mount has a horse inventory (`AbstractHorse`). +pub(super) fn has_mount_menu(species: Species) -> bool { + equine::is_equine(species) || llama::is_llama(species) +} +/// The saddle slot is active (`canUseSlot(SADDLE)` and `CAN_EQUIP_SADDLE`): +/// not on llamas. +fn shows_saddle(species: Species) -> bool { + !llama::is_llama(species) +} +/// The body slot is active: horse armour, or a llama's carpet. +fn shows_body(species: Species) -> bool { + can_wear_horse_armor(species) || llama::is_llama(species) +} + /// The open mount menu: copies of the mount's slots, written back on every change. #[derive(Default)] pub(super) struct MountMenu { open: Option, } +impl MountMenu { + /// The mount whose menu is open. + pub(super) fn opened_mob(&self) -> Option { + self.open.as_ref().map(|o| o.mob) + } +} struct Open { mob: u32, species: Species, @@ -40,6 +75,9 @@ fn can_wear_horse_armor(species: Species) -> bool { } /// Whether `item` fits the body slot of the species (`isEquippableInSlot`). fn body_armor_fits(species: Species, item: &str) -> bool { + if llama::is_llama(species) { + return item.ends_with("_carpet"); + } can_wear_horse_armor(species) && item.ends_with("_horse_armor") } @@ -55,18 +93,16 @@ impl Game<'_> { pub(super) fn open_mount_menu(&mut self, index: usize) { let m = &self.mobs[index]; let species = m.species(); - if !equine::is_equine(species) { + if !has_mount_menu(species) { return; } - let chested = m.extra.chested && matches!(species, Species::Donkey | Species::Mule); - let columns = if chested { COLUMNS } else { 0 }; let mut items = m.extra.items.clone(); items.resize(CHEST_SIZE, None); - let show_armor = can_wear_horse_armor(species); + let show_armor = shows_body(species); self.mount_menu.open = Some(Open { mob: m.id, species, - columns, + columns: mount_columns(m), saddle: m.saddle_item(), armor: m .extra @@ -75,10 +111,40 @@ impl Game<'_> { .filter(|_| show_armor) .map(|i| ItemStack::new(i, 1)), items, - show_saddle: true, + show_saddle: shows_saddle(species), show_armor, }); - self.carried = None; + self.show_mount_menu(); + } + /// `ClientPacketListener.handleMountScreenOpen`: a server's horse + /// inventory over mount `entity`, its slots to come in the menu's content. + pub(super) fn open_remote_mount_menu(&mut self, entity: u32, columns: usize) -> bool { + let Some(species) = self + .mobs + .iter() + .find(|m| m.id == entity) + .map(Mob::species) + .filter(|s| has_mount_menu(*s)) + else { + return false; + }; + self.mount_menu.open = Some(Open { + mob: entity, + species, + columns: columns.min(COLUMNS), + saddle: None, + armor: None, + items: vec![None; CHEST_SIZE], + show_saddle: shows_saddle(species), + show_armor: shows_body(species), + }); + self.show_mount_menu(); + true + } + fn show_mount_menu(&mut self) { + if self.remote.is_none() && self.lan_acting.is_none() { + self.carried = None; + } self.station = None; self.container = None; self.craft_size = 2; @@ -94,15 +160,39 @@ impl Game<'_> { return false; }; let m = &self.mobs[index]; - if !equine::is_equine(m.species()) || !m.extra.tame || !m.alive() { + if !has_mount_menu(m.species()) || !m.extra.tame || !m.alive() { return false; } self.open_mount_menu(index); true } + /// Riding a mount that has a horse inventory (`HasCustomInventoryScreen`). + pub(super) fn riding_mount_with_menu(&self) -> bool { + self.riding + .and_then(|id| self.mount_mob_index(id)) + .is_some_and(|i| has_mount_menu(self.mobs[i].species())) + } + /// A server closed its window: the screen's copy goes. + pub(super) fn mount_menu_reset(&mut self) { + self.mount_menu.open = None; + } + /// The entity type of the open mount menu's mount (`minecraft:horse`). + pub(super) fn mount_menu_entity(&self) -> Option { + let open = self.mount_menu.open.as_ref()?; + let m = self.mobs.iter().find(|m| m.id == open.mob)?; + Some(m.kind.id.to_string()) + } + /// The chest columns of the open mount menu (the server's window's). + pub(super) fn mount_menu_columns(&self) -> Option { + self.mount_menu.open.as_ref().map(|o| o.columns) + } /// The menu ends when the mount dies or is more than 4 blocks away /// (`AbstractMountInventoryMenu.stillValid`). pub(super) fn tick_mount_menu(&mut self) { + // A server's menu ends when the server closes it. + if self.remote.is_some() { + return; + } let Some(open) = self.mount_menu.open.as_ref() else { return; }; @@ -126,8 +216,12 @@ impl Game<'_> { self.mount_sync(); self.mount_menu.open = None; } - /// Writes the menu slots to the mount, with the equip sounds. + /// Writes the menu slots to the mount, with the equip sounds. A + /// server's menu is the server's to write. fn mount_sync(&mut self) { + if self.remote.is_some() { + return; + } let Some(open) = self.mount_menu.open.as_ref() else { return; }; @@ -135,25 +229,66 @@ impl Game<'_> { return; }; let (saddle, armor) = (open.saddle.clone(), open.armor.clone()); + let body = open.show_armor.then_some(armor); let items = open.items.clone(); - let m = &mut self.mobs[index]; + self.mount_store(index, saddle, body, items); + } + /// The slots of mount `index` as its menu shows them: the saddle, the + /// body item (None when the slot isn't active), the chest. + pub(super) fn mount_contents( + &self, + index: usize, + ) -> ( + Option, + Option>, + Vec>, + ) { + let m = &self.mobs[index]; + let species = m.species(); + let body = + shows_body(species).then(|| m.extra.head.as_deref().map(|i| ItemStack::new(i, 1))); + let mut items = m.extra.items.clone(); + items.resize(mount_columns(m) * 3, None); + (m.saddle_item(), body, items) + } + /// The menu's slots back on mount `index` (`body` None: not its slot), + /// with the equip sound of what was put on (`Equippable.equipSound`). + pub(super) fn mount_store( + &mut self, + index: usize, + saddle: Option, + body: Option>, + items: Vec>, + ) { + let m = &self.mobs[index]; let at = m.center(); - let sound = if saddle.is_some() && !m.extra.saddled { - Some("minecraft:entity.horse.saddle") - } else if armor.is_some() && m.extra.head.is_none() { - Some("minecraft:entity.horse.armor") + let put_on = if saddle.is_some() && !m.extra.saddled { + saddle.as_ref() } else { - None + body.as_ref() + .and_then(Option::as_ref) + .filter(|_| m.extra.head.is_none()) }; + let sound = put_on.map(|s| { + self.res + .items() + .get(&s.item) + .and_then(|i| i.equip_sound.clone()) + .unwrap_or_else(|| "minecraft:item.armor.equip_generic".into()) + }); + let m = &mut self.mobs[index]; m.set_saddle(saddle); - if open.show_armor { - m.extra.head = armor.map(|a| a.item); + if let Some(body) = body { + m.extra.head = body.map(|a| a.item); } + let mut items = items; + let keep = m.extra.items.len().max(CHEST_SIZE); + items.resize(keep, None); m.extra.items = items; let (x, z) = (at[0].floor() as i32 >> 4, at[2].floor() as i32 >> 4); self.world.modified.insert((x, z)); if let Some(sound) = sound { - self.audio.play(sound, Some(at), 0.5, 1.0); + self.audio.play(&sound, Some(at), 0.5, 1.0); } } pub(super) fn mount_slots(&self) -> Option> { @@ -214,7 +349,8 @@ impl Game<'_> { } /// A click in the mount menu before the generic handling. pub(super) fn mount_click(&mut self, right: bool) -> bool { - if !self.mount_active() { + // A server's menu: the click goes out like any container click. + if !self.mount_active() || self.remote.is_some() { return false; } let Some(r) = self.renderer.as_ref() else { @@ -314,12 +450,12 @@ impl Game<'_> { if open.show_armor { batch.sprite(atlas, "gui/sprites/container/slot", ox + 7.0, oy + 35.0); if open.armor.is_none() { - batch.sprite( - atlas, - "gui/sprites/container/slot/horse_armor", - ox + 8.0, - oy + 36.0, - ); + let sprite = if llama::is_llama(open.species) { + "gui/sprites/container/slot/llama_armor" + } else { + "gui/sprites/container/slot/horse_armor" + }; + batch.sprite(atlas, sprite, ox + 8.0, oy + 36.0); } } let grey = rgba(64, 64, 64, 255); @@ -448,4 +584,16 @@ mod tests { "minecraft:iron_chestplate" )); } + + #[test] + fn llamas_wear_carpets_and_no_saddle() { + assert!(body_armor_fits(Species::Llama, "minecraft:red_carpet")); + assert!(!body_armor_fits( + Species::Llama, + "minecraft:iron_horse_armor" + )); + assert!(shows_body(Species::TraderLlama)); + assert!(!shows_saddle(Species::Llama)); + assert!(shows_saddle(Species::Donkey) && !shows_body(Species::Donkey)); + } } diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index c482d9728..7ae8b9ed3 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -183,6 +183,9 @@ const CHUNK_PACKETS_PER_FRAME: Duration = Duration::from_millis(1); /// whatever their cost: the packets after a slow machine's chunks never /// wait more than a tick. const BACKLOG_LIMIT: Duration = Duration::from_millis(50); +/// The open menu name of a server's horse inventory, then its chest's +/// columns (the packet names no `MenuType`). +const MOUNT_MENU: &str = "horizon:mount"; fn face_direction(face: [i32; 3]) -> u8 { match face { @@ -1440,6 +1443,19 @@ impl Game<'_> { // CrafterMenu: the grid, the inventory, then the result (the // client's own). "minecraft:crafter_3x3" => slots(9), + // HorseInventoryMenu: the saddle, the body armour, the chest, + // then the inventory. + m if m.starts_with(MOUNT_MENU) => { + let n = m[MOUNT_MENU.len()..].parse::().ok()? * 3; + match s { + 0 => Some(super::mount_menu::SADDLE), + 1 => Some(super::mount_menu::ARMOR), + _ if s < 2 + n => Some(super::mount_menu::CHEST + s - 2), + _ if s < 2 + n + 27 => Some(9 + s - 2 - n), + _ if s < 2 + n + 36 => Some(s - 2 - n - 27), + _ => None, + } + } // A workstation: its inputs, its result, then the inventory. m => { let kind = Self::remote_station_kind(m)?; @@ -2204,6 +2220,7 @@ impl Game<'_> { } } C::OpenScreen(p) => self.remote_open_screen(p), + C::MountScreenOpen(p) => self.remote_open_mount(p), C::RecipeBookAdd(p) => self.remote_recipe_book_add(p), C::RecipeBookRemove(p) => self.remote_recipe_book_remove(p), C::RecipeBookSettings(p) => self.remote_recipe_book_settings(p), @@ -4099,6 +4116,18 @@ impl Game<'_> { } Ok(true) } + // MultiPlayerGameMode.isServerControlledInventory: riding a + // horse or llama, the server opens its inventory + // (ServerboundPlayerCommandPacket OPEN_INVENTORY). + KeyCode::KeyE if self.riding_mount_with_menu() => { + let id = self.remote.as_ref().map_or(0, |r| r.entity_id); + self.send(S::PlayerCommand(play::PlayerCommand { + id, + action: 5, + data: 0, + })); + Ok(true) + } KeyCode::KeyE => { // The inventory opens without telling the server. self.screen = if self.creative { @@ -4343,6 +4372,7 @@ impl Game<'_> { r.menu = None; r.menu_title = None; } + self.mount_menu_reset(); // The client's menu has no level access: what it holds is the server's. self.station = None; self.craft_size = 2; @@ -4527,6 +4557,21 @@ impl Game<'_> { self.screen = Screen::Inventory; self.capture(false); } + /// `ClientPacketListener.handleMountScreenOpen`: a horse's or llama's + /// inventory; for another entity the window is closed again. + fn remote_open_mount(&mut self, p: play::MountScreenOpen) { + let columns = usize::try_from(p.columns).unwrap_or(0).min(5); + if !self.open_remote_mount_menu(p.entity as u32, columns) { + self.send(S::ContainerClose(play::ContainerClose { + container: p.container, + })); + return; + } + if let Some(r) = self.remote.as_mut() { + r.menu = Some((p.container, format!("{MOUNT_MENU}{columns}"))); + r.menu_title = None; + } + } /// The type of the menu a server opened (`MenuType`'s id). pub(super) fn remote_menu_type(&self) -> Option<&str> { Some(self.remote.as_ref()?.menu.as_ref()?.1.as_str()) diff --git a/crates/horizon-client/src/lan/containers.rs b/crates/horizon-client/src/lan/containers.rs index 88f50aa3b..d43cb5c27 100644 --- a/crates/horizon-client/src/lan/containers.rs +++ b/crates/horizon-client/src/lan/containers.rs @@ -234,6 +234,23 @@ impl Hub { Some(window) } + /// ServerPlayer.openHorseInventory: the open container closes, a new + /// window id opens mount `entity`'s inventory with its chest's columns + /// (ClientboundMountScreenOpenPacket). + pub fn open_mount_window(&mut self, conn: u32, columns: i32, entity: i32) -> Option { + let i = self.guests.iter().position(|g| g.conn == conn)?; + self.close_container_at(i); + let g = &mut self.guests[i]; + g.next_window = g.next_window % 100 + 1; + let window = g.next_window; + g.send(&C::MountScreenOpen(play::MountScreenOpen { + container: window, + columns, + entity, + })); + Some(window) + } + fn send_menu_content(&mut self, i: usize, mut items: Vec>) { for j in 0..36 { items.push(self.guests[i].inventory[player_slot(j)].clone()); diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index 8d7b5e29a..bf66f979b 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -1972,6 +1972,10 @@ impl Hub { self.guests[i].sneaking = p.flags & 32 != 0; self.guests[i].sprinting = p.flags & 64 != 0 && p.flags & 1 != 0; } + // OPEN_INVENTORY is the game's: the ridden mount's inventory. + S::PlayerCommand(p) if p.action == 5 => { + events.push(HostEvent::Packet(conn, Box::new(S::PlayerCommand(p)))); + } S::PlayerCommand(p) => match p.action { 1 => self.guests[i].sprinting = true, 2 => self.guests[i].sprinting = false, diff --git a/crates/horizon-client/src/menu.rs b/crates/horizon-client/src/menu.rs index cb804e15b..cdaf73eed 100644 --- a/crates/horizon-client/src/menu.rs +++ b/crates/horizon-client/src/menu.rs @@ -114,6 +114,10 @@ pub enum Kind { Lectern, /// `MerchantMenu`: two payment slots, the result, then the inventory. Merchant, + /// `HorseInventoryMenu`: the saddle and body armour slots (`ArmorSlot`s + /// of the mount, whose entity type is `Menu::mount`), a chest of three + /// rows of this many columns (`getInventoryColumns`), then the inventory. + Mount(usize), /// `CreativeModeInventoryScreen.ItemPickerMenu`: an item tab's 45 /// display slots, then the hotbar. ItemPicker, @@ -124,6 +128,9 @@ impl Kind { if let Some(rows) = name.strip_prefix("chest") { return rows.parse().ok().map(Self::Chest); } + if let Some(columns) = name.strip_prefix("mount") { + return columns.parse().ok().map(Self::Mount); + } Some(match name { "inventory" => Self::Inventory, "crafting" => Self::Crafting, @@ -207,6 +214,9 @@ enum Role { /// `CustomCreativeSlot`: a creative tab's display slot, which the /// screen clicks itself; drags and PICKUP_ALL pass it by. CreativeDisplay, + /// An `ArmorSlot` of a mount for this `EquipmentSlot` ("saddle" or + /// "body"): `isEquippableInSlot` for `Menu::mount`, one item. + MountEquip(&'static str), } #[derive(Clone, Copy)] @@ -298,6 +308,9 @@ pub struct Menu { pub enchant_button: Option, /// `Player.mayBuild` (not adventure mode): the lectern's Take Book. pub may_build: bool, + /// A mount menu's entity type (`minecraft:horse`): what its saddle and + /// body slots take. + pub mount: Option, } /// `AnvilMenu`'s state besides its slots and `cost`. @@ -560,6 +573,22 @@ impl Menu { // The open page (LecternBlockEntity.dataAccess). data = 1; } + Kind::Mount(columns) => { + containers[MAIN] = vec![None; 2 + columns * 3]; + for (index, slot) in ["saddle", "body"].into_iter().enumerate() { + slots.push(Slot { + container: MAIN, + index, + role: Role::MountEquip(slot), + }); + } + slots.extend((2..2 + columns * 3).map(|index| Slot { + container: MAIN, + index, + role: Role::Plain, + })); + player(&mut slots); + } Kind::ItemPicker => { containers[MAIN] = vec![None; 45]; slots.extend(main(45, Role::CreativeDisplay)); @@ -654,6 +683,7 @@ impl Menu { enchant_changed: false, enchant_button: None, may_build: true, + mount: None, } .with_initial_data() } @@ -1186,6 +1216,15 @@ impl Menu { Some(slot == eq && allowed) }) .unwrap_or(false), + // LivingEntity.isEquippableInSlot: the item's equippable slot, + // and the mount among its allowed entities. + Role::MountEquip(slot) => env.items.get(&stack.item).is_some_and(|i| { + i.equip_slot.as_deref() == Some(slot) + && self + .mount + .as_deref() + .is_some_and(|m| i.equip_allowed.as_ref().is_none_or(|a| a.contains(m))) + }), Role::Shulker => Self::fits_inside(stack), Role::Fuel => Self::is_fuel(env, stack) || Self::is_bucket(stack), Role::Output | Role::CraftResult | Role::Inert => false, @@ -1257,7 +1296,7 @@ impl Menu { ); o.satisfied_by(env.items, a, b) || o.satisfied_by(env.items, b, a) }), - Role::Armor(_) => match self.slot(s) { + Role::Armor(_) | Role::MountEquip(_) => match self.slot(s) { Some(stack) => { self.creative || stack.enchantment_level("minecraft:binding_curse") == 0 } @@ -1270,7 +1309,11 @@ impl Menu { /// `Slot.getMaxStackSize(stack)`. fn slot_max(&self, env: &Env, s: usize, stack: &ItemStack) -> u32 { let own = match self.slots[s].role { - Role::Armor(_) | Role::Potion | Role::EnchantItem | Role::BeaconPay => 1, + Role::Armor(_) + | Role::MountEquip(_) + | Role::Potion + | Role::EnchantItem + | Role::BeaconPay => 1, // The lectern's bookAccess holds one. Role::Plain if self.kind == Kind::Lectern && self.slots[s].container == MAIN => 1, Role::Fuel if Self::is_bucket(stack) => return 1, @@ -2026,6 +2069,38 @@ impl Menu { } // LecternMenu.quickMoveStack moves nothing. Kind::Lectern => return None, + Kind::Mount(columns) => { + // AbstractMountInventoryMenu.quickMoveStack: no count check + // and no onTake. + check_count = false; + let start = 2 + columns * 3; + if index < start { + if !self.move_to(env, &mut stack, start, total, true) { + return None; + } + } else if self.may_place(env, 1, &stack) && self.containers[MAIN][1].is_none() { + if !self.move_to(env, &mut stack, 1, 2, false) { + return None; + } + } else if self.may_place(env, 0, &stack) && self.containers[MAIN][0].is_none() { + if !self.move_to(env, &mut stack, 0, 1, false) { + return None; + } + } else if columns == 0 || !self.move_to(env, &mut stack, 2, start, false) { + // Between the inventory and the hotbar: the stack shrinks + // in its slot and EMPTY is returned either way. + let (main_end, hotbar_end) = (start + 27, start + 36); + if (main_end..hotbar_end).contains(&index) { + self.move_to(env, &mut stack, start, main_end, false); + } else if (start..main_end).contains(&index) { + self.move_to(env, &mut stack, main_end, hotbar_end, false); + } + let slot = self.slots[index]; + self.containers[slot.container][slot.index] = + (stack.count > 0).then(|| stack.clone()); + return None; + } + } Kind::Beacon => { // BeaconMenu.quickMoveStack. after_take = true; @@ -2946,6 +3021,65 @@ pub(crate) mod tests { test(&env, &mut menu); } + /// `HorseInventoryMenu`: a shift-click wears horse armour, then saddles, + /// then fills the chest; the body slot takes only what the mount may wear. + #[test] + fn mount_menu_slots_take_what_the_mount_wears() { + with_menu(Kind::Mount(5), |env, menu| { + menu.creative = false; + menu.mount = Some("minecraft:donkey".into()); + // 2 saddle and body, 15 chest, then the inventory from 17. + assert_eq!(menu.slot_count(), 2 + 15 + 36); + let hotbar = 17 + 27; + menu.set_slot(env, hotbar, stack("iron_horse_armor", 1)); + menu.click(env, hotbar as i32, 0, Input::QuickMove).unwrap(); + // A donkey wears no horse armour: it goes in the chest. + assert_eq!(menu.slot(1), None); + assert_eq!( + menu.slot(2).map(|s| &*s.item), + Some("minecraft:iron_horse_armor") + ); + menu.set_slot(env, hotbar, stack("saddle", 1)); + menu.click(env, hotbar as i32, 0, Input::QuickMove).unwrap(); + assert_eq!(menu.slot(0).map(|s| &*s.item), Some("minecraft:saddle")); + // Back out of the mount: to the inventory, from the hotbar's end. + menu.click(env, 0, 0, Input::QuickMove).unwrap(); + assert_eq!(menu.slot(0), None); + assert_eq!( + menu.slot(17 + 35).map(|s| &*s.item), + Some("minecraft:saddle") + ); + }); + with_menu(Kind::Mount(0), |env, menu| { + menu.creative = false; + menu.mount = Some("minecraft:horse".into()); + let hotbar = 2 + 27; + menu.set_slot(env, hotbar, stack("diamond_horse_armor", 1)); + menu.click(env, hotbar as i32, 0, Input::QuickMove).unwrap(); + assert_eq!( + menu.slot(1).map(|s| &*s.item), + Some("minecraft:diamond_horse_armor") + ); + // No chest: dirt goes between the hotbar and the inventory. + menu.set_slot(env, hotbar, stack("dirt", 3)); + menu.click(env, hotbar as i32, 0, Input::QuickMove).unwrap(); + assert_eq!( + menu.slot(2).map(|s| (&*s.item, s.count)), + Some(("minecraft:dirt", 3)) + ); + assert_eq!(menu.slot(hotbar), None); + // Placing by hand: a saddle doesn't go in the body slot. + menu.carried = stack("saddle", 1); + menu.click(env, 1, 0, Input::Pickup).unwrap(); + assert_eq!( + menu.slot(1).map(|s| &*s.item), + Some("minecraft:diamond_horse_armor") + ); + menu.click(env, 0, 0, Input::Pickup).unwrap(); + assert_eq!(menu.slot(0).map(|s| &*s.item), Some("minecraft:saddle")); + }); + } + /// `onEquipItem` runs from the armour and off-hand slots' `setByPlayer` /// when the item changes: never from `Inventory.setItem` (a swap's source). #[test] diff --git a/crates/horizon-protocol/src/packets/play.rs b/crates/horizon-protocol/src/packets/play.rs index e737258c5..24eec6a05 100644 --- a/crates/horizon-protocol/src/packets/play.rs +++ b/crates/horizon-protocol/src/packets/play.rs @@ -458,6 +458,9 @@ move_entity!(MoveEntityPos, pos: true, rot: false); move_entity!(MoveEntityPosRot, pos: true, rot: true); move_entity!(MoveEntityRot, pos: false, rot: true); packet!(OpenScreen { container: i32 => VarInt, menu: i32 => VarInt, title: Tag => Nbt }); +// ClientboundMountScreenOpenPacket: a horse's or llama's inventory, with its +// chest's columns (`getInventoryColumns`). +packet!(MountScreenOpen { container: i32 => VarInt, columns: i32 => VarInt, entity: i32 => Int }); packet!(PlayerAbilities { flags: i8 => Byte, flying_speed: f32 => Float, walking_speed: f32 => Float }); /// MessageSignature.Packed: an index into the last-seen cache or a full signature. pub struct PackedSignature; @@ -1497,6 +1500,7 @@ packet_enum!(Clientbound, crate::ids::play::clientbound { MoveEntityPosRot(MoveEntityPosRot) = MOVE_ENTITY_POS_ROT, MoveEntityRot(MoveEntityRot) = MOVE_ENTITY_ROT, MerchantOffers(MerchantOffers) = MERCHANT_OFFERS, + MountScreenOpen(MountScreenOpen) = MOUNT_SCREEN_OPEN, OpenScreen(OpenScreen) = OPEN_SCREEN, Ping(Ping) = PING, PongResponse(crate::packets::status::Ping) = PONG_RESPONSE, @@ -2179,6 +2183,11 @@ mod tests { menu: 2, title: literal("Chest"), }), + Clientbound::MountScreenOpen(MountScreenOpen { + container: 4, + columns: 5, + entity: 300, + }), Clientbound::MerchantOffers(MerchantOffers { container: 3, offers: vec![NetOffer { diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index 894bb8627..c5a90150f 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -123,6 +123,9 @@ address. The script then checks, from both sides, what a guest's game used to ge it (#1222). - frames: the host and the guest each put a diamond in an item frame and turn it, and put a helmet on an armor stand and take it back (#1222). +- mounts: the host and the guest each open a tamed horse's, donkey's and llama's inventory + (shift + right click, then the inventory key while riding), put on a saddle, armour + or a carpet and fill the chest by shift-clicking. - riding: the host and the guest each saddle and ride a horse (walk, jump), a pig and a strider (with their sticks), a camel and a donkey, sit on a llama and fly a happy ghast, and get off by sneaking; the other side sees the animal move. @@ -169,7 +172,7 @@ ROOT = Path(__file__).resolve().parents[1] GUEST_NAME = 'Guest' # The host plays single player on its server thread (--server-thread). SERVER_THREAD = False -CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'arrows', 'kills', 'hurt', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing', 'enchanting', 'beacon', 'crafter', 'lectern', 'cartography', 'trials', 'clicks', 'sleep', 'predict', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth', 'flight', 'ladder', 'boats', 'minecarts', 'frames', 'lightning', 'riding'] +CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'arrows', 'kills', 'hurt', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing', 'enchanting', 'beacon', 'crafter', 'lectern', 'cartography', 'trials', 'clicks', 'sleep', 'predict', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth', 'flight', 'ladder', 'boats', 'minecarts', 'frames', 'lightning', 'riding', 'mounts'] class Game: @@ -2638,6 +2641,101 @@ def check_riding(host, guest, out): return result +# Mount inventories: (kind, summon NBT, what goes in by shift-click and the menu slot it lands +# in: 700 the saddle, 701 the body armour or carpet, 710 the chest's first slot). +MOUNT_MENUS = [ + ('horse', '{Tame:1b,NoAI:1b}', [('minecraft:saddle', 700), ('minecraft:iron_horse_armor', 701)]), + ('donkey', '{Tame:1b,NoAI:1b,ChestedHorse:1b}', [('minecraft:saddle', 700), ('minecraft:dirt', 710)]), + ('llama', '{Tame:1b,NoAI:1b,ChestedHorse:1b,Strength:3}', + [('minecraft:red_carpet', 701), ('minecraft:dirt', 710)]), +] + + +def mount_screen(game, ticks=20): + """The open mount menu's slots and items ({700: 'minecraft:saddle', ...}), waiting up to + `ticks` for it to open; None when no mount menu opened.""" + for _ in range(ticks // 2): + slots = {} + for w in game.raw('ui').get('widgets', []): + wid = w.get('id', '') + if wid.startswith('slot-') and wid[5:].isdigit() and int(wid[5:]) >= 700: + slots[int(wid[5:])] = (w.get('stack') or {}).get('item') + if slots: + return slots + game.wait(2) + return None + + +def check_mounts(host, guest, out): + """The host and the guest each open a tamed horse's, donkey's and llama's inventory with + shift + right click (`ClientboundMountScreenOpenPacket` for a server's player), put on a + saddle, horse armour or a carpet and fill the chest by shift-clicking, close it, then ride the + mount and open its inventory with the inventory key (OPEN_INVENTORY): it shows what was put + in, and the server took the items from the inventory.""" + spots = apart(host, guest) + result = {} + for label, player in (('host', host), ('guest', guest)): + x, y, z = spots[label] + player('command', command='gamemode survival') + result[label] = {} + for kind, nbt, puts in MOUNT_MENUS: + player('command', command='clear @s') + for item, _ in puts: + player('command', command=f'give @s {item}') + player('command', command=f'summon minecraft:{kind} {x + 0.5} {y} {z + 2.5} {nbt}') + player.wait(5) + found = [m for m in player.raw('mobs', 16).get('near', []) + if m['kind'].endswith(kind) and m['health'] > 0] + assert found, f'the {label} sees no {kind}' + mob = found[0] + p = mob['pos'] + aim = (f'{p[0]:.2f}', f'{p[1] + HEIGHTS[kind] / 2:.2f}', f'{p[2]:.2f}') + try: + player.raw('look-at', *aim) + player.raw('key', 'shift', 'down') + player.wait(3) + player.raw('mouse', 'right', 'click') + player.wait(5) + player.raw('key', 'shift', 'up') + slots = mount_screen(player) + assert slots is not None, f"the {label}'s shift + right click on the {kind} opened no inventory" + for item, _ in puts: + player.raw('click-slot', hotbar_slot(player, item), 'shift') + player.wait(5) + slots = mount_screen(player) + for item, slot in puts: + assert slots.get(slot) == item, f"the {label}'s {kind}: {item} shift-clicked, slot {slot} holds {slots.get(slot)}" + player.raw('key', 'esc', 'tap') + player.wait(5) + for item, _ in puts: + left = server_count(player, item.split(':')[1]) + assert left == 0, f'the server still counts {left} {item} on the {label}' + player.raw('look-at', *aim) + player.raw('mouse', 'right', 'click') + player.wait(8) + riding = player('status').get('riding') + assert riding == mob['id'], f'the {label} right-clicked its {kind}: riding {riding}' + player.raw('key', 'e', 'tap') + again = mount_screen(player) + assert again is not None, f'the inventory key on the {label}\'s {kind} opened no inventory' + for item, slot in puts: + assert again.get(slot) == item, f"the {label}'s {kind} reopened: slot {slot} holds {again.get(slot)}, not {item}" + player.raw('key', 'esc', 'tap') + player.wait(3) + hold(player, 'shift', 10) + player.wait(5) + result[label][kind] = {slot: item for item, slot in puts} + finally: + back_to_game(player) + player('command', command=f'kill @e[type=minecraft:{kind},x={x},y={y},z={z},distance=..16]') + player.wait(2) + player('command', command=f'kill @e[type=minecraft:item,x={x},y={y},z={z},distance=..16]') + player('command', command='clear @s') + player('command', command=f'tp @s {x + 0.5} {y} {z + 0.5} 0 0') + player.wait(5) + return result + + def check_lightning(host, guest, out): """A bolt summoned beside each player: both sides get the bolt entity and flash the sky (the host's window too on its server thread, FEATURES 4z(f)).""" @@ -2977,7 +3075,7 @@ CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'name 'xp': check_xp, 'objects': check_objects, 'projectiles': check_projectiles, 'blast': check_blast, 'light': check_light, 'plates': check_plates, 'swings': check_swings, 'cracks': check_cracks, - 'time': check_time, 'growth': check_growth, 'flight': check_flight, 'fireball': check_fireball, 'deflect': check_deflect, 'barter': check_barter, 'worn': check_worn, 'kills': check_kills, 'arrows': check_arrows, 'predict': check_predict, 'ladder': check_ladder, 'boats': check_boats, 'minecarts': check_minecarts, 'frames': check_frames, 'lightning': check_lightning, 'riding': check_riding} + 'time': check_time, 'growth': check_growth, 'flight': check_flight, 'fireball': check_fireball, 'deflect': check_deflect, 'barter': check_barter, 'worn': check_worn, 'kills': check_kills, 'arrows': check_arrows, 'predict': check_predict, 'ladder': check_ladder, 'boats': check_boats, 'minecarts': check_minecarts, 'frames': check_frames, 'lightning': check_lightning, 'riding': check_riding, 'mounts': check_mounts} def run(ctl, out, resources, checks):