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).