From b9d6d1c7d91b7654f63fa1d58903b666af747c6c Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 16 Oct 2026 15:24:11 +0200 Subject: [PATCH 01/12] Stronghold ring searches run on every worker that needs them The 128 stronghold ring positions each need a biome search over a 57x57 quart area. The first worker to ask computed all of them inside a OnceLock while every other worker blocked on it: about 2 s on an M4 Pro with 12 of 13 workers idle, after every world load and every return to the Overworld. Vanilla's generateRingPositions runs each search as its own task on the background executor. Now the ring chunks and search randoms are planned serially (cheap), and every thread that needs the rings takes searches until none are left, then waits for the rest. Positions are unchanged: each search only uses its own forked random. Co-Authored-By: Claude Opus 5.5 --- .../src/worldgen/generator_structures.rs | 153 ++++++++++++++++-- .../src/worldgen/structure/start.rs | 48 +++--- .../src/worldgen/structure/stronghold.rs | 61 +++++-- 3 files changed, 211 insertions(+), 51 deletions(-) diff --git a/crates/horizon-world/src/worldgen/generator_structures.rs b/crates/horizon-world/src/worldgen/generator_structures.rs index 8a5f15d65..76b4d39c9 100644 --- a/crates/horizon-world/src/worldgen/generator_structures.rs +++ b/crates/horizon-world/src/worldgen/generator_structures.rs @@ -5,7 +5,10 @@ use super::{ generator::{GeneratorData, GeneratorError}, structure::{ StructureResources, - start::{Rings, StructureRegistry, StructureStart, beardifier, create_starts}, + start::{ + Rings, StartContext, StructureRegistry, StructureStart, beardifier, create_starts, + }, + stronghold::RingSearch, }, }; use horizon_core::{density::Beardifier, position::ChunkPos}; @@ -13,7 +16,10 @@ use serde_json::Value; use std::collections::BTreeMap; use std::{ collections::{BTreeSet, HashMap}, - sync::{Arc, Mutex, OnceLock}, + sync::{ + Arc, Condvar, Mutex, OnceLock, + atomic::{AtomicUsize, Ordering}, + }, }; pub(super) type Starts = Arc>>; @@ -27,6 +33,78 @@ pub(super) struct StructureState { starts: Mutex>, /// Concentric ring positions, computed on first use. rings: OnceLock, + /// Their biome searches while they are computed. + ring_work: OnceLock, +} + +/// generateRingPositions' biome searches, shared out. Vanilla runs each as +/// its own task on its worker pool; here every thread that needs the rings +/// before they exist takes searches until none are left, then waits for the +/// ones others took. Done by the first thread alone, the 128 stronghold +/// searches kept every other worker blocked for seconds after each world load. +struct RingWork { + /// The concentric-ring sets, some with no positions. + sets: Vec, + /// Set index and search, sets in `possible_sets` order. + searches: Vec<(usize, RingSearch)>, + next: AtomicUsize, + found: Vec>, + left: Mutex, + finished: Condvar, +} +impl RingWork { + fn new(sets: Vec<(usize, Vec)>) -> Self { + let indices = sets.iter().map(|(set, _)| *set).collect(); + let searches: Vec<(usize, RingSearch)> = sets + .into_iter() + .flat_map(|(set, searches)| searches.into_iter().map(move |s| (set, s))) + .collect(); + Self { + sets: indices, + next: AtomicUsize::new(0), + found: (0..searches.len()).map(|_| OnceLock::new()).collect(), + left: Mutex::new(searches.len()), + finished: Condvar::new(), + searches, + } + } + /// Run searches until none are left to take, then wait for the rest. + fn help<'a>( + &self, + preferred: impl Fn(usize) -> &'a BTreeSet, + biome: &mut dyn FnMut([i32; 3]) -> BiomeId, + ) { + loop { + let i = self.next.fetch_add(1, Ordering::Relaxed); + let Some((set, search)) = self.searches.get(i) else { + break; + }; + let _ = self.found[i].set(search.find(preferred(*set), biome)); + let mut left = self.left.lock().unwrap(); + *left -= 1; + if *left == 0 { + self.finished.notify_all(); + } + } + let mut left = self.left.lock().unwrap(); + while *left > 0 { + left = self.finished.wait(left).unwrap(); + } + } + /// The positions by set name, once every search is done. + fn rings(&self, name: impl Fn(usize) -> String) -> Rings { + let mut rings: Rings = self + .sets + .iter() + .map(|&set| (name(set), Vec::new())) + .collect(); + for ((set, _), found) in self.searches.iter().zip(&self.found) { + if let Some(positions) = rings.get_mut(&name(*set)) { + positions.push(*found.get().expect("every search finished")); + } + } + rings + } } const START_CACHE_LIMIT: usize = 65536; @@ -89,6 +167,7 @@ impl StructureState { by_step, starts: Mutex::new(HashMap::new()), rings: OnceLock::new(), + ring_work: OnceLock::new(), })) } /// Concentric ring positions of a structure set, once the first start @@ -96,6 +175,25 @@ impl StructureState { pub(super) fn cached_rings(&self, set: &str) -> Option> { self.rings.get().and_then(|rings| rings.get(set).cloned()) } + /// The concentric ring positions, computing them with the threads that + /// need them first (see `RingWork`). + fn rings(&self, context: &mut impl StartContext) -> &Rings { + if let Some(rings) = self.rings.get() { + return rings; + } + let work = self.ring_work.get_or_init(|| { + RingWork::new( + self.registry + .ring_searches(&self.possible_sets, context.seed()), + ) + }); + let registry = &self.registry; + work.help(|set| registry.ring_biomes(set), &mut |q| { + context.noise_biome(q) + }); + self.rings + .get_or_init(|| work.rings(|set| registry.sets[set].name.clone())) + } /// Drop the cached starts, so that later chunks build fresh pieces (the /// pieces keep per-start placement state such as a shipwreck's height). pub(super) fn forget_starts(&self) { @@ -110,11 +208,7 @@ impl StructureState { if let Some(found) = self.starts.lock().unwrap().get(&(chunk.x, chunk.z)) { return found.clone(); } - let rings = self.rings.get_or_init(|| { - let seed = context.seed(); - self.registry - .ring_positions(&self.possible_sets, seed, &mut |q| context.noise_biome(q)) - }); + let rings = self.rings(context); let starts: Starts = Arc::new( create_starts( &self.registry, @@ -148,13 +242,7 @@ impl StructureState { radius: i32, ) -> Option<(String, [i32; 3])> { let seed = context.seed(); - let rings = self - .rings - .get_or_init(|| { - self.registry - .ring_positions(&self.possible_sets, seed, &mut |q| context.noise_biome(q)) - }) - .clone(); + let rings = self.rings(context).clone(); let wanted = |s: usize| names.iter().any(|n| *n == self.registry.structures[s].name); let dist = |p: [i32; 3]| { let (dx, dz) = (f64::from(p[0] - from[0]), f64::from(p[2] - from[2])); @@ -288,3 +376,40 @@ impl StructureState { beardifier(&self.registry, references, chunk) } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::worldgen::structure::stronghold::{ring_positions, ring_searches}; + + /// Searches shared between threads give the positions of one thread + /// running them in order, set by set. + #[test] + fn ring_work_shared_between_threads_matches_serial_search() { + let preferred: BTreeSet = (0..20).map(|b| BiomeId(b * 3)).collect(); + let biome = |q: [i32; 3]| { + BiomeId((q[0].wrapping_mul(31) ^ q[2].wrapping_mul(17)).rem_euclid(64) as u32) + }; + let none = BTreeSet::new(); + for seed in [0, 1234, -77, i64::MAX] { + let work = RingWork::new(vec![ + (0, ring_searches(seed, 32, 3, 128)), + (1, ring_searches(seed ^ 5, 10, 4, 0)), + (2, ring_searches(seed + 1, 20, 5, 40)), + ]); + let sets = [&preferred, &none, &preferred]; + std::thread::scope(|scope| { + for _ in 0..4 { + scope.spawn(|| work.help(|set| sets[set], &mut { biome })); + } + }); + let rings = work.rings(|set| format!("set{set}")); + let serial = |s: i64, distance, spread, count, preferred| { + ring_positions(s, distance, spread, count, preferred, &mut { biome }) + }; + assert_eq!(rings["set0"], serial(seed, 32, 3, 128, &preferred)); + assert_eq!(rings["set1"], Vec::new()); + assert_eq!(rings["set2"], serial(seed + 1, 20, 5, 40, &preferred)); + } + } +} diff --git a/crates/horizon-world/src/worldgen/structure/start.rs b/crates/horizon-world/src/worldgen/structure/start.rs index 1526e08ed..1b443eba5 100644 --- a/crates/horizon-world/src/worldgen/structure/start.rs +++ b/crates/horizon-world/src/worldgen/structure/start.rs @@ -299,31 +299,37 @@ impl StructureRegistry { pools: Pools::new(pools), }) } - /// ChunkGeneratorStructureState ring positions of the concentric-ring - /// sets among `possible_sets`. - pub fn ring_positions( + /// ChunkGeneratorStructureState.generatePositions: the ring positions of + /// each concentric-ring set among `possible_sets` before their biome + /// searches (set index, searches in order). + pub fn ring_searches( &self, possible_sets: &[usize], seed: i64, - biome: &mut dyn FnMut([i32; 3]) -> BiomeId, - ) -> Rings { - let mut rings = Rings::new(); - for &i in possible_sets { - let set = &self.sets[i]; - if let Placement::ConcentricRings { - distance, - spread, - count, - preferred, - } = &set.placement - { - let positions = super::stronghold::ring_positions( - seed, *distance, *spread, *count, preferred, biome, - ); - rings.insert(set.name.clone(), positions); - } + ) -> Vec<(usize, Vec)> { + possible_sets + .iter() + .filter_map(|&i| match &self.sets[i].placement { + Placement::ConcentricRings { + distance, + spread, + count, + .. + } => Some(( + i, + super::stronghold::ring_searches(seed, *distance, *spread, *count), + )), + _ => None, + }) + .collect() + } + /// The preferred biomes of a concentric-ring set (empty for others). + pub fn ring_biomes(&self, set: usize) -> &BTreeSet { + static NONE: BTreeSet = BTreeSet::new(); + match &self.sets[set].placement { + Placement::ConcentricRings { preferred, .. } => preferred, + _ => &NONE, } - rings } /// RandomSpreadStructurePlacement.getPotentialStructureChunk et al. /// RandomSpreadStructurePlacement.getPotentialStructureChunk for the diff --git a/crates/horizon-world/src/worldgen/structure/stronghold.rs b/crates/horizon-world/src/worldgen/structure/stronghold.rs index eb31e0627..270b412af 100644 --- a/crates/horizon-world/src/worldgen/structure/stronghold.rs +++ b/crates/horizon-world/src/worldgen/structure/stronghold.rs @@ -22,20 +22,30 @@ pub fn ring_positions( preferred: &BTreeSet, biome: &mut dyn FnMut([i32; 3]) -> BiomeId, ) -> Vec { - let mut random = LegacyRandom::new(seed); - let mut angle = random.next_double() * PI * 2.0; - let (mut in_circle, mut circle, mut spread) = (0, 0, spread); - let mut out = Vec::with_capacity(count.max(0) as usize); - for i in 0..count { - let dist = f64::from(4 * distance + distance * circle * 6) - + (random.next_double() - 0.5) * (f64::from(distance) * 2.5); - let round = |v: f64| (v + 0.5).floor() as i64 as i32; - let (x, z) = ( - round(horizon_core::jmath::cos(angle) * dist), - round(horizon_core::jmath::sin(angle) * dist), - ); - let mut search = random.fork(); - let (cx, cz) = ((x * 16 + 8) >> 2, (z * 16 + 8) >> 2); + ring_searches(seed, distance, spread, count) + .iter() + .map(|search| search.find(preferred, biome)) + .collect() +} + +/// One ring position before its biome search: the chunk on the ring and the +/// random its search uses. Vanilla runs each search as its own task. +#[derive(Clone, Debug)] +pub struct RingSearch { + x: i32, + z: i32, + random: LegacyRandom, +} +impl RingSearch { + /// BiomeSource.findBiomeHorizontal around the ring chunk's centre: a + /// random preferred biome within 112 blocks, else the ring chunk itself. + pub fn find( + &self, + preferred: &BTreeSet, + biome: &mut dyn FnMut([i32; 3]) -> BiomeId, + ) -> ChunkPos { + let mut search = self.random.clone(); + let (cx, cz) = ((self.x * 16 + 8) >> 2, (self.z * 16 + 8) >> 2); let mut found = 0; let mut result = None; for dz in -28..=28 { @@ -48,9 +58,28 @@ pub fn ring_positions( } } } - out.push(match result { + match result { Some((bx, bz)) => ChunkPos::new(bx >> 4, bz >> 4), - None => ChunkPos::new(x, z), + None => ChunkPos::new(self.x, self.z), + } + } +} + +/// The ring chunks and search randoms of generateRingPositions, in order: +/// the serial part, cheap; the searches can then run anywhere. +pub fn ring_searches(seed: i64, distance: i32, spread: i32, count: i32) -> Vec { + let mut random = LegacyRandom::new(seed); + let mut angle = random.next_double() * PI * 2.0; + let (mut in_circle, mut circle, mut spread) = (0, 0, spread); + let mut out = Vec::with_capacity(count.max(0) as usize); + for i in 0..count { + let dist = f64::from(4 * distance + distance * circle * 6) + + (random.next_double() - 0.5) * (f64::from(distance) * 2.5); + let round = |v: f64| (v + 0.5).floor() as i64 as i32; + out.push(RingSearch { + x: round(horizon_core::jmath::cos(angle) * dist), + z: round(horizon_core::jmath::sin(angle) * dist), + random: random.fork(), }); angle += PI * 2.0 / f64::from(spread); in_circle += 1; From c47dc83eab96ade6a625c76e1482a8f2f97751bf Mon Sep 17 00:00:00 2001 From: Cosmic <84875247+Online5277@users.noreply.github.com> Date: Fri, 16 Oct 2026 12:16:05 -0400 Subject: [PATCH 02/12] Honor villager texture hat metadata in type layers --- crates/horizon-client/src/game/packs.rs | 2 + crates/horizon-client/src/mobs.rs | 1 + crates/horizon-client/src/mobs/data.rs | 4 + crates/horizon-client/src/mobs/render.rs | 8 ++ .../horizon-client/src/mobs/villager_hats.rs | 128 ++++++++++++++++++ 5 files changed, 143 insertions(+) create mode 100644 crates/horizon-client/src/mobs/villager_hats.rs diff --git a/crates/horizon-client/src/game/packs.rs b/crates/horizon-client/src/game/packs.rs index 599399b96..2e977ddc9 100644 --- a/crates/horizon-client/src/game/packs.rs +++ b/crates/horizon-client/src/game/packs.rs @@ -147,6 +147,8 @@ impl Game<'_> { self.res.clouds = super::load_clouds(root); self.res.forcefield = super::load_forcefield(root); self.res.names = super::Names::load(root); + self.res.mob_data.villager_hats = + horizon_client::mobs::villager_hats::VillagerHats::load(root); horizon_client::ui::load_metadata(root); lang::set(lang::Language::load(&[&root.to_path_buf()], &lang::code())); self.audio.reload(root); diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index a98cbb897..d531c8c4b 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -70,6 +70,7 @@ pub mod undead; pub mod vex; pub mod villager; pub mod villager_brain; +pub mod villager_hats; pub mod warden; pub mod warden_brain; pub mod witch; diff --git a/crates/horizon-client/src/mobs/data.rs b/crates/horizon-client/src/mobs/data.rs index 48fc4234f..ad948ac1f 100644 --- a/crates/horizon-client/src/mobs/data.rs +++ b/crates/horizon-client/src/mobs/data.rs @@ -46,6 +46,7 @@ pub struct MobData { /// Each entity type's `SpawnPlacements.getHeightmapType`. pub heightmaps: HashMap, pub animations: HashMap, + pub villager_hats: super::villager_hats::VillagerHats, /// Biome tags (spawns_warm_variant_farm_animals, is_ocean...) by tag id. pub biome_tags: HashMap>, /// Entity type tags (`data/*/tags/entity_type`). @@ -68,6 +69,7 @@ impl MobData { }; data.biome_tags = crate::items::load_tags(root, "worldgen/biome").unwrap_or_default(); data.entity_tags = crate::items::load_tags(root, "entity_type").unwrap_or_default(); + data.villager_hats = super::villager_hats::VillagerHats::load(root); data } /// Only the keyframe animation definitions of `mob-data.json`. @@ -128,6 +130,7 @@ impl MobData { placements, heightmaps, animations, + villager_hats: Default::default(), biome_tags: HashMap::new(), entity_tags: HashMap::new(), } @@ -182,6 +185,7 @@ impl MobData { placements: HashMap::new(), heightmaps: HashMap::new(), animations: HashMap::new(), + villager_hats: Default::default(), biome_tags: HashMap::new(), entity_tags: HashMap::new(), } diff --git a/crates/horizon-client/src/mobs/render.rs b/crates/horizon-client/src/mobs/render.rs index a3b29f63b..46505ec9d 100644 --- a/crates/horizon-client/src/mobs/render.rs +++ b/crates/horizon-client/src/mobs/render.rs @@ -1061,6 +1061,14 @@ fn render_visible( }; let mut poses = Poses::new(&part); anim::setup(m, &s, &mut poses, &data.animations); + if matches!(m.species(), Species::Villager | Species::ZombieVillager) + && let Some((folder, variant)) = layer.texture.split_once("/type/") + { + let variant = variant.strip_prefix("baby/").unwrap_or(variant); + let profession = PROFESSIONS[m.extra.variant2 as usize % PROFESSIONS.len()]; + data.villager_hats + .apply(folder, variant, profession, &mut poses); + } let light = if layer.full_bright { 240 | 240 << 8 } else { diff --git a/crates/horizon-client/src/mobs/villager_hats.rs b/crates/horizon-client/src/mobs/villager_hats.rs new file mode 100644 index 000000000..b81fa35ef --- /dev/null +++ b/crates/horizon-client/src/mobs/villager_hats.rs @@ -0,0 +1,128 @@ +//! VillagerProfessionLayer's cached texture metadata and type-layer head visibility. +use super::{ + anim::Poses, + render::{PROFESSIONS, VILLAGER_TYPES}, +}; +use std::{collections::HashMap, fs, path::Path}; + +#[derive(Clone, Copy, Default, PartialEq, Eq, Debug)] +enum Hat { + #[default] + None, + Partial, + Full, +} + +#[derive(Default)] +pub struct VillagerHats(HashMap); + +impl VillagerHats { + pub fn load(root: &Path) -> Self { + let mut hats = HashMap::new(); + for folder in ["villager", "zombie_villager"] { + for (group, names) in [ + ("type", VILLAGER_TYPES.as_slice()), + ("profession", PROFESSIONS.as_slice()), + ] { + for name in names { + let texture = format!("entity/{folder}/{group}/{name}"); + let path = root.join(format!("assets/minecraft/textures/{texture}.png.mcmeta")); + let metadata = fs::read(path) + .ok() + .and_then(|bytes| serde_json::from_slice::(&bytes).ok()); + let hat = match metadata + .as_ref() + .and_then(|v| v["villager"]["hat"].as_str()) + { + Some("partial") => Hat::Partial, + Some("full") => Hat::Full, + _ => Hat::None, + }; + hats.insert(texture, hat); + } + } + } + Self(hats) + } + + pub fn apply(&self, folder: &str, variant: &str, profession: &str, poses: &mut Poses) { + let hat = |group, name| { + self.0 + .get(&format!("{folder}/{group}/{name}")) + .copied() + .unwrap_or_default() + }; + let visible = match hat("profession", profession) { + Hat::None => true, + Hat::Partial => hat("type", variant) != Hat::Full, + Hat::Full => false, + }; + // VillagerModel.createNoHatModel clears the head and every child. + poses.visible("head", visible); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + #[ignore = "requires prepared 26.3 models and textures in HORIZON_TEST_RESOURCES"] + fn metadata_head_visibility_matches_exported_no_hat_models() { + use crate::mobs::{load_model, render::draw}; + let root = std::env::var_os("HORIZON_TEST_RESOURCES").expect("HORIZON_TEST_RESOURCES"); + let mut hats = VillagerHats::load(Path::new(&root)); + assert_eq!(hats.0["entity/villager/profession/farmer"], Hat::Full); + assert_eq!(hats.0["entity/villager/type/plains"], Hat::None); + for (name, folder) in [ + ("villager", "entity/villager"), + ("villager_baby", "entity/villager"), + ("zombie_villager", "entity/zombie_villager"), + ("zombie_villager_baby", "entity/zombie_villager"), + ] { + let model = load_model(Path::new(&root), &format!("minecraft:{name}#main")).unwrap(); + let no_hat = + load_model(Path::new(&root), &format!("minecraft:{name}_no_hat#main")).unwrap(); + for (type_hat, profession_hat, hidden) in [ + (Hat::None, Hat::Full, true), + (Hat::Full, Hat::Partial, true), + (Hat::Partial, Hat::Partial, false), + (Hat::Full, Hat::None, false), + ] { + hats.0.insert(format!("{folder}/type/plains"), type_hat); + hats.0 + .insert(format!("{folder}/profession/farmer"), profession_hat); + let mut poses = Poses::new(&model); + hats.apply(folder, "plains", "farmer", &mut poses); + let mut actual = Vec::new(); + draw( + &model, + glam::Mat4::IDENTITY, + &poses, + [0.0, 0.0, 1.0, 1.0], + u32::MAX, + 0, + 0, + &mut actual, + ); + let expected_model = if hidden { &no_hat } else { &model }; + let mut expected = Vec::new(); + draw( + expected_model, + glam::Mat4::IDENTITY, + &Poses::new(expected_model), + [0.0, 0.0, 1.0, 1.0], + u32::MAX, + 0, + 0, + &mut expected, + ); + assert_eq!( + bytemuck::cast_slice::<_, u8>(&actual), + bytemuck::cast_slice::<_, u8>(&expected), + "{name}: {type_hat:?}/{profession_hat:?}" + ); + } + } + } +} From 4c3b2b789f687a3613cdff015f659c39798f04f6 Mon Sep 17 00:00:00 2001 From: Cosmic <84875247+Online5277@users.noreply.github.com> Date: Fri, 16 Oct 2026 12:20:41 -0400 Subject: [PATCH 03/12] Draw translated hover tooltips for Classic options --- crates/horizon-client/src/options_ui.rs | 112 ++++++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/crates/horizon-client/src/options_ui.rs b/crates/horizon-client/src/options_ui.rs index d6a5fa476..38be29576 100644 --- a/crates/horizon-client/src/options_ui.rs +++ b/crates/horizon-client/src/options_ui.rs @@ -223,6 +223,25 @@ impl Opt { _ => Kind::Cycle, } } + /// OptionInstance's static or value-dependent tooltip translation. + fn tooltip_key(self, s: &Settings) -> String { + let key = match self { + Opt::InactivityFps => { + if s.inactivity_fps_limit == "minimized" { + "options.inactivityFpsLimit.minimized" + } else { + "options.inactivityFpsLimit.afk" + } + } + Opt::ChunkBuilder => [ + "options.prioritizeChunkUpdates.none", + "options.prioritizeChunkUpdates.byPlayer", + "options.prioritizeChunkUpdates.nearby", + ][usize::from(s.prioritize_chunk_updates.min(2))], + _ => self.caption(), + }; + format!("{key}.tooltip") + } /// A slider's position (0..1), or `None` for a cycle button. pub fn slider_position(self, s: &Settings) -> Option { match self.kind(s) { @@ -1611,6 +1630,27 @@ impl OptionsUi { let start = batch.vertices.len(); let hover = ui::hovered(cursor, wd.x, wd.y, wd.w, wd.h) && (!wd.list || cursor.1 >= top && cursor.1 < bottom); + if hover { + let key = match &wd.kind { + WKind::Button { + act: Act::Cycle(opt), + .. + } + | WKind::Slider { opt, .. } => Some(opt.tooltip_key(s)), + WKind::Button { + act: Act::Push(Page::Language), + .. + } => Some("options.language.tooltip".into()), + WKind::Button { + act: Act::Push(Page::Accessibility), + .. + } => Some("options.accessibility.tooltip".into()), + _ => None, + }; + if let Some(key) = key.filter(|key| lang::has(key)) { + tooltip = Some(atlas.font.split(&tr(&key), 170.0)); + } + } match &wd.kind { WKind::Button { label, active, act } => { ui::button(batch, atlas, wd.x, wd.y, wd.w, wd.h, label, *active, hover); @@ -2161,6 +2201,78 @@ mod tests { HudAtlas::build(std::path::Path::new("/nonexistent/horizon-test-assets")).unwrap() } + #[test] + fn option_tooltip_keys_follow_value_dependent_descriptions() { + let mut s = Settings::default(); + assert_eq!( + Opt::InactivityFps.tooltip_key(&s), + "options.inactivityFpsLimit.afk.tooltip" + ); + s.inactivity_fps_limit = "minimized".into(); + assert_eq!( + Opt::InactivityFps.tooltip_key(&s), + "options.inactivityFpsLimit.minimized.tooltip" + ); + for (value, name) in ["none", "byPlayer", "nearby"].iter().enumerate() { + s.prioritize_chunk_updates = value as u8; + assert_eq!( + Opt::ChunkBuilder.tooltip_key(&s), + format!("options.prioritizeChunkUpdates.{name}.tooltip") + ); + } + assert_eq!( + Opt::MenuBlur.tooltip_key(&s), + "options.accessibility.menu_background_blurriness.tooltip" + ); + } + + #[test] + #[ignore = "requires prepared language data in HORIZON_TEST_RESOURCES; run alone"] + fn classic_hover_draws_the_selected_options_wrapped_tooltip() { + let root = std::path::PathBuf::from( + std::env::var_os("HORIZON_TEST_RESOURCES").expect("HORIZON_TEST_RESOURCES"), + ); + lang::set(lang::Language::load(&[&root], "en_us")); + let atlas = atlas(); + let mut s = Settings::default(); + let mut ui = OptionsUi::default(); + ui.show(Page::Video); + for mode in ["afk", "minimized"] { + s.inactivity_fps_limit = mode.into(); + let (widgets, _, _, _) = ui.layout(&s, 480.0, 480.0); + let option = widgets + .iter() + .find(|w| { + matches!( + w.kind, + WKind::Button { + act: Act::Cycle(Opt::InactivityFps), + .. + } + ) + }) + .unwrap(); + let cursor = (option.x + 1.0, option.y + 1.0); + let mut actual = HudBatch::new([480, 480]); + ui.draw(&s, &mut actual, &atlas, cursor); + let key = format!("options.inactivityFpsLimit.{mode}.tooltip"); + assert!(lang::has(&key)); + let mut expected = HudBatch::new([480, 480]); + ui::tooltip( + &mut expected, + &atlas, + &atlas.font.split(&tr(&key), 170.0), + cursor, + ); + assert!(!expected.vertices.is_empty()); + let tail = &actual.vertices[actual.vertices.len() - expected.vertices.len()..]; + assert_eq!( + bytemuck::cast_slice::<_, u8>(tail), + bytemuck::cast_slice::<_, u8>(&expected.vertices) + ); + } + } + #[test] fn classic_video_gui_scale_cycles_on_press_without_dragging() { let atlas = atlas(); From 746c888cd3b85bfe27dbc15b292f0c62054300f6 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 16 Oct 2026 18:20:46 +0200 Subject: [PATCH 04/12] horizon-mods: content mods (API 0.2), blocks and items declared from vanilla templates A .hmod may carry assets/ and data/ beside mod.wasm, or instead of it. The manifest needs api = "0.2" and the new registries capability ("add blocks and items to your worlds"). Content mods have no WIT interface: code still links API 0.1. horizon_mods::content::Content reads a mod's content and checks it: - plain resource paths only; - 65,536 files, 16 MiB each, 512 MiB in all; - files in the mod's own namespace, plus additions to vanilla tags; - blocks and items declared in data//horizon/{block,item}/*.json, each copying a vanilla template with typed overrides (hardness, resistance, light, tool, friction, map colour, sounds; stack size, rarity), unknown keys refused. Mods::load refuses an enabled mod whose content fails these checks, with the reason on its row. A content-only mod is on with no engine and no hooks. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 1 + crates/horizon-mods/Cargo.toml | 1 + crates/horizon-mods/src/content.rs | 557 +++++++++++++++++++++++++++ crates/horizon-mods/src/lib.rs | 2 + crates/horizon-mods/src/manifest.rs | 110 ++++-- crates/horizon-mods/src/runtime.rs | 22 +- crates/horizon-mods/tests/sandbox.rs | 34 +- docs/mods.md | 22 ++ 8 files changed, 711 insertions(+), 38 deletions(-) create mode 100644 crates/horizon-mods/src/content.rs diff --git a/Cargo.lock b/Cargo.lock index 68d0b250d..90b288a8e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1483,6 +1483,7 @@ dependencies = [ "horizon-mods", "log", "serde", + "serde_json", "toml", "wasmtime", "wit-component 0.239.0", diff --git a/crates/horizon-mods/Cargo.toml b/crates/horizon-mods/Cargo.toml index ee325261d..8db24ac35 100644 --- a/crates/horizon-mods/Cargo.toml +++ b/crates/horizon-mods/Cargo.toml @@ -8,6 +8,7 @@ description = "Horizon's mod runtime: sandboxed WebAssembly components (docs/mod [dependencies] serde = { version = "1", features = ["derive"] } +serde_json = "1" toml = "1.1.6" zip = { version = "2", default-features = false, features = ["deflate"] } log = "0.4" diff --git a/crates/horizon-mods/src/content.rs b/crates/horizon-mods/src/content.rs new file mode 100644 index 000000000..411fa7277 --- /dev/null +++ b/crates/horizon-mods/src/content.rs @@ -0,0 +1,557 @@ +//! A content mod's `assets/` and `data/` (API 0.2, docs/modpacks.md): the +//! files it adds to the game's resource tree, and the blocks and items it +//! declares in `data//horizon/block/` and `data//horizon/item/`. +//! +//! Content is data, not code: nothing here runs in the sandbox. What a mod +//! may add is fenced instead. Its own namespace, plus additions to vanilla +//! tags. Paths are plain, files and totals are capped, and vanilla ids +//! cannot be redefined. +use crate::manifest::{Capability, MANIFEST, Manifest}; +use serde::Deserialize; +use std::{ + collections::BTreeSet, + io::Read, + path::{Path, PathBuf}, +}; + +/// Content files per mod, bytes per file and bytes per mod. +pub const MAX_FILES: usize = 65_536; +pub const MAX_FILE: u64 = 16 << 20; +pub const MAX_TOTAL: u64 = 512 << 20; + +/// Checks a content path: `assets/` or `data/`, then `/`-separated parts of +/// `[a-z0-9_.-]`, none of them `.` or `..` (vanilla's resource-location +/// characters, so the path means the same on every OS). +pub fn check_path(name: &str) -> Result<(), String> { + let bad = || format!("{name:?} is not a plain assets/ or data/ path"); + let mut parts = name.split('/'); + if !matches!(parts.next(), Some("assets" | "data")) { + return Err(bad()); + } + let mut count = 0; + for part in parts { + count += 1; + let chars_ok = part + .bytes() + .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b"_.-".contains(&b)); + if part.is_empty() || part == "." || part == ".." || !chars_ok { + return Err(bad()); + } + } + // assets// at least. + if count < 2 { + return Err(bad()); + } + Ok(()) +} + +/// A vanilla identifier, `minecraft:path`. +fn vanilla_id(id: &str) -> bool { + id.strip_prefix("minecraft:").is_some_and(|path| { + !path.is_empty() + && path + .bytes() + .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b"_./-".contains(&b)) + }) +} + +/// What a declared block changes from its template; unset keeps the +/// template's value. +#[derive(Clone, Debug, Default, Deserialize, PartialEq)] +#[serde(deny_unknown_fields)] +pub struct BlockProperties { + /// Destroy time in seconds of hand mining (-1: unbreakable). + pub hardness: Option, + pub explosion_resistance: Option, + /// Light emitted, 0 to 15. + pub light: Option, + pub requires_tool: Option, + pub friction: Option, + /// A `MapColor` id, 0 to 61. + pub map_color: Option, + /// A vanilla block whose sounds this block uses. + pub sounds: Option, +} + +#[derive(Debug, Deserialize)] +#[serde(deny_unknown_fields)] +struct BlockFile { + template: String, + #[serde(default)] + properties: BlockProperties, + #[serde(default = "yes")] + item: bool, +} +fn yes() -> bool { + true +} + +/// A block the mod adds: a vanilla template's behaviour, states and shapes +/// under a new id, with `properties` applied. +#[derive(Clone, Debug, PartialEq)] +pub struct Block { + /// `:`. + pub id: String, + /// The vanilla block it copies, `minecraft:`. + pub template: String, + pub properties: BlockProperties, + /// Also add a block item with the same id. + pub item: bool, +} + +#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Eq)] +#[serde(rename_all = "lowercase")] +pub enum Rarity { + Common, + Uncommon, + Rare, + Epic, +} + +#[derive(Debug, Deserialize)] +#[serde(deny_unknown_fields)] +struct ItemFile { + template: String, + max_stack_size: Option, + rarity: Option, +} + +/// An item the mod adds: a vanilla template item's components under a new +/// id, with these changed. +#[derive(Clone, Debug, PartialEq)] +pub struct Item { + pub id: String, + /// The vanilla item it copies, `minecraft:`. + pub template: String, + pub max_stack_size: Option, + pub rarity: Option, +} + +#[derive(Debug)] +enum Source { + Dir(PathBuf), + Zip(PathBuf), +} + +/// A mod's content: its files and declarations. +#[derive(Debug)] +pub struct Content { + source: Source, + /// Every content file (`assets/...`, `data/...`), sorted. + pub files: Vec, + pub blocks: Vec, + pub items: Vec, +} + +impl Content { + /// The content of the mod at `path` (a `.hmod` or a mod folder). + pub fn read(path: &Path, manifest: &Manifest) -> Result { + let (source, sized) = if path.is_dir() { + let mut out = Vec::new(); + for top in ["assets", "data"] { + walk(path, &path.join(top), &mut out)?; + } + (Source::Dir(path.to_owned()), out) + } else { + let mut zip = open_zip(path)?; + let mut out = Vec::new(); + for i in 0..zip.len() { + let entry = zip.by_index_raw(i).map_err(|e| e.to_string())?; + let name = entry.name(); + if (name.starts_with("assets/") || name.starts_with("data/")) && !entry.is_dir() { + out.push((name.to_owned(), entry.size())); + } + } + (Source::Zip(path.to_owned()), out) + }; + let mut files = Vec::with_capacity(sized.len()); + let mut total = 0u64; + for (name, size) in sized { + check_path(&name)?; + if size > MAX_FILE { + return Err(format!("{name} is over {} MiB", MAX_FILE >> 20)); + } + total += size; + files.push(name); + } + if files.len() > MAX_FILES { + return Err(format!("more than {MAX_FILES} content files")); + } + if total > MAX_TOTAL { + return Err(format!("content is over {} MiB", MAX_TOTAL >> 20)); + } + files.sort(); + files.dedup(); + let mut content = Content { + source, + files, + blocks: Vec::new(), + items: Vec::new(), + }; + content.check(manifest)?; + Ok(content) + } + + /// No files: a mod with code only. + pub fn is_empty(&self) -> bool { + self.files.is_empty() + } + + /// One content file's bytes. + pub fn file(&self, name: &str) -> Result, String> { + check_path(name)?; + let mut out = Vec::new(); + match &self.source { + Source::Dir(root) => { + let file = + std::fs::File::open(root.join(name)).map_err(|e| format!("{name}: {e}"))?; + file.take(MAX_FILE + 1) + .read_to_end(&mut out) + .map_err(|e| format!("{name}: {e}"))?; + } + Source::Zip(path) => { + let mut zip = open_zip(path)?; + let entry = zip.by_name(name).map_err(|e| format!("{name}: {e}"))?; + entry + .take(MAX_FILE + 1) + .read_to_end(&mut out) + .map_err(|e| format!("{name}: {e}"))?; + } + } + if out.len() as u64 > MAX_FILE { + return Err(format!("{name} is over {} MiB", MAX_FILE >> 20)); + } + Ok(out) + } + + /// Where content may go, and the declarations. + fn check(&mut self, manifest: &Manifest) -> Result<(), String> { + let id = manifest.id.as_str(); + if !self.files.is_empty() && !manifest.capabilities.contains(&Capability::Registries) { + return Err(format!( + "has assets/ or data/ but not capability registries in {MANIFEST}" + )); + } + let mut blocks = Vec::new(); + let mut items = Vec::new(); + for name in &self.files { + let mut parts = name.splitn(3, '/'); + let (top, namespace, rest) = ( + parts.next().unwrap_or(""), + parts.next().unwrap_or(""), + parts.next().unwrap_or(""), + ); + let own = namespace == id; + // Vanilla's own files stay vanilla's; a mod may only add to its tags. + let allowed = + own || (top == "data" && namespace == "minecraft" && rest.starts_with("tags/")); + if !allowed { + return Err(format!( + "{name}: a mod adds files in its own namespace ({top}/{id}/) or to vanilla tags" + )); + } + let Some(decl) = (own && top == "data") + .then(|| rest.strip_prefix("horizon/")) + .flatten() + else { + continue; + }; + let parse = |kind: &str| -> Option { + let path = decl + .strip_prefix(kind)? + .strip_prefix('/')? + .strip_suffix(".json")?; + Some(format!("{id}:{path}")) + }; + if let Some(block) = parse("block") { + blocks.push((block, name.clone())); + } else if let Some(item) = parse("item") { + items.push((item, name.clone())); + } else { + return Err(format!( + "{name}: data/{id}/horizon/ holds block/*.json and item/*.json only" + )); + } + } + let mut ids = BTreeSet::new(); + for (block, name) in blocks { + let file: BlockFile = + serde_json::from_slice(&self.file(&name)?).map_err(|e| format!("{name}: {e}"))?; + check_block(&file).map_err(|e| format!("{name}: {e}"))?; + if file.item { + ids.insert(block.clone()); + } + self.blocks.push(Block { + id: block, + template: file.template, + properties: file.properties, + item: file.item, + }); + } + for (item, name) in items { + let file: ItemFile = + serde_json::from_slice(&self.file(&name)?).map_err(|e| format!("{name}: {e}"))?; + if !vanilla_id(&file.template) { + return Err(format!( + "{name}: template {:?} is not a vanilla item (minecraft:...)", + file.template + )); + } + if file.max_stack_size.is_some_and(|n| !(1..=99).contains(&n)) { + return Err(format!("{name}: max_stack_size is 1 to 99")); + } + if !ids.insert(item.clone()) { + return Err(format!("{name}: {item} is also a block's item")); + } + self.items.push(Item { + id: item, + template: file.template, + max_stack_size: file.max_stack_size, + rarity: file.rarity, + }); + } + Ok(()) + } +} + +fn check_block(file: &BlockFile) -> Result<(), String> { + if !vanilla_id(&file.template) { + return Err(format!( + "template {:?} is not a vanilla block (minecraft:...)", + file.template + )); + } + let p = &file.properties; + let finite = |v: Option| v.is_none_or(f32::is_finite); + if !finite(p.hardness) || !finite(p.explosion_resistance) || !finite(p.friction) { + return Err("hardness, explosion_resistance and friction are numbers".into()); + } + if p.hardness.is_some_and(|h| h < 0.0 && h != -1.0) { + return Err("hardness is 0 or more, or -1 (unbreakable)".into()); + } + if p.explosion_resistance.is_some_and(|r| r < 0.0) { + return Err("explosion_resistance is 0 or more".into()); + } + if p.friction.is_some_and(|f| !(0.0..=2.0).contains(&f)) { + return Err("friction is 0 to 2".into()); + } + if p.light.is_some_and(|l| l > 15) { + return Err("light is 0 to 15".into()); + } + if p.map_color.is_some_and(|c| c > 61) { + return Err("map_color is a MapColor id, 0 to 61".into()); + } + if p.sounds.as_deref().is_some_and(|s| !vanilla_id(s)) { + return Err("sounds names a vanilla block (minecraft:...)".into()); + } + Ok(()) +} + +fn open_zip(path: &Path) -> Result, String> { + let file = std::fs::File::open(path).map_err(|e| e.to_string())?; + zip::ZipArchive::new(file).map_err(|e| e.to_string()) +} + +/// Files under `dir`, relative to `root` with `/` separators. Symbolic links +/// are not followed: a mod folder holds its own files. +fn walk(root: &Path, dir: &Path, out: &mut Vec<(String, u64)>) -> Result<(), String> { + let Ok(read) = std::fs::read_dir(dir) else { + return Ok(()); + }; + for entry in read.flatten() { + let path = entry.path(); + let meta = std::fs::symlink_metadata(&path).map_err(|e| e.to_string())?; + if meta.is_dir() { + walk(root, &path, out)?; + } else if meta.is_file() { + let rel = path.strip_prefix(root).map_err(|e| e.to_string())?; + let parts: Option> = rel.iter().map(|p| p.to_str()).collect(); + let name = parts.ok_or_else(|| format!("{} is not UTF-8", rel.display()))?; + out.push((name.join("/"), meta.len())); + } + if out.len() > MAX_FILES { + return Err(format!("more than {MAX_FILES} content files")); + } + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::manifest::Package; + + const MANIFEST_TEXT: &str = r#" +id = "ruby" +name = "Ruby" +version = "0.1.0" +api = "0.2" +side = "both" +capabilities = ["registries"] +"#; + const BLOCK: &[u8] = br#"{"template": "minecraft:iron_block", + "properties": {"hardness": 4.0, "light": 3, "sounds": "minecraft:amethyst_block"}}"#; + const ITEM: &[u8] = br#"{"template": "minecraft:diamond", "rarity": "uncommon"}"#; + + fn scratch(name: &str) -> PathBuf { + let dir = + std::env::temp_dir().join(format!("horizon-content-{}-{name}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + dir + } + fn hmod(name: &str, manifest: &str, files: &[(&str, &[u8])]) -> Result { + let dir = scratch(name); + let path = dir.join("m.hmod"); + std::fs::write(&path, Package::write_files(manifest, None, files)?).unwrap(); + let package = Package::read(&path); + let content = package.and_then(|p| Content::read(&path, &p.manifest)); + std::fs::remove_dir_all(&dir).unwrap(); + content + } + + #[test] + fn content_only_mod_round_trips() { + let files: &[(&str, &[u8])] = &[ + ("data/ruby/horizon/block/ruby_block.json", BLOCK), + ("data/ruby/horizon/item/ruby.json", ITEM), + ("data/ruby/recipe/ruby_block.json", b"{}"), + ("data/minecraft/tags/block/mineable/pickaxe.json", b"{}"), + ("assets/ruby/textures/block/ruby_block.png", b"png"), + ]; + let content = hmod("ok", MANIFEST_TEXT, files).unwrap(); + assert_eq!(content.files.len(), 5); + assert_eq!( + content.blocks, + [Block { + id: "ruby:ruby_block".into(), + template: "minecraft:iron_block".into(), + properties: BlockProperties { + hardness: Some(4.0), + light: Some(3), + sounds: Some("minecraft:amethyst_block".into()), + ..BlockProperties::default() + }, + item: true, + }] + ); + assert_eq!(content.items[0].id, "ruby:ruby"); + assert_eq!(content.items[0].rarity, Some(Rarity::Uncommon)); + } + + #[test] + fn folder_mod_reads_the_same() { + let dir = scratch("folder"); + std::fs::write(dir.join(MANIFEST), MANIFEST_TEXT).unwrap(); + let block = dir.join("data/ruby/horizon/block"); + std::fs::create_dir_all(&block).unwrap(); + std::fs::write(block.join("ruby_block.json"), BLOCK).unwrap(); + let package = Package::read(&dir).unwrap(); + assert!(package.wasm.is_none()); + let content = Content::read(&dir, &package.manifest).unwrap(); + assert_eq!(content.files, ["data/ruby/horizon/block/ruby_block.json"]); + assert_eq!(content.file(&content.files[0]).unwrap(), BLOCK); + std::fs::remove_dir_all(&dir).unwrap(); + } + + #[test] + fn refuses_content_outside_its_fence() { + let refused = |name: &str, files: &[(&str, &[u8])]| { + let e = hmod(name, MANIFEST_TEXT, files).unwrap_err(); + assert!(!e.is_empty()); + e + }; + let e = refused("other-ns", &[("data/other/horizon/block/a.json", BLOCK)]); + assert!(e.contains("own namespace"), "{e}"); + refused( + "vanilla-asset", + &[("assets/minecraft/textures/block/stone.png", b"x")], + ); + refused( + "vanilla-recipe", + &[("data/minecraft/recipe/stone.json", b"{}")], + ); + let e = refused( + "modded-template", + &[( + "data/ruby/horizon/block/a.json", + br#"{"template": "ruby:other"}"#, + )], + ); + assert!(e.contains("not a vanilla block"), "{e}"); + refused( + "light", + &[( + "data/ruby/horizon/block/a.json", + br#"{"template": "minecraft:stone", "properties": {"light": 16}}"#, + )], + ); + refused( + "unknown-key", + &[( + "data/ruby/horizon/block/a.json", + br#"{"template": "minecraft:stone", "glow": true}"#, + )], + ); + let e = refused( + "clash", + &[ + ("data/ruby/horizon/block/a.json", BLOCK), + ("data/ruby/horizon/item/a.json", ITEM), + ], + ); + assert!(e.contains("also a block's item"), "{e}"); + refused("other-kind", &[("data/ruby/horizon/entity/a.json", b"{}")]); + refused( + "stack", + &[( + "data/ruby/horizon/item/a.json", + br#"{"template": "minecraft:stick", "max_stack_size": 100}"#, + )], + ); + } + + #[test] + fn content_needs_the_registries_capability() { + let code_only = MANIFEST_TEXT + .replace("api = \"0.2\"", "api = \"0.1\"") + .replace("capabilities = [\"registries\"]", "capabilities = []"); + // No code and no registries: nothing to load. + let e = hmod("empty", &code_only, &[]).unwrap_err(); + assert!(e.contains("mod.wasm: missing"), "{e}"); + // Registries on API 0.1. + let old = MANIFEST_TEXT.replace("api = \"0.2\"", "api = \"0.1\""); + assert!(Manifest::parse(&old).unwrap_err().contains("needs api")); + // Content without the capability, in a mod with code. + let dir = scratch("no-cap"); + let path = dir.join("m.hmod"); + let files: &[(&str, &[u8])] = &[("assets/ruby/lang/en_us.json", b"{}")]; + std::fs::write( + &path, + Package::write_files(&code_only, Some(b"\0asm"), files).unwrap(), + ) + .unwrap(); + let package = Package::read(&path).unwrap(); + let e = Content::read(&path, &package.manifest).err().unwrap(); + assert!(e.contains("not capability registries"), "{e}"); + std::fs::remove_dir_all(&dir).unwrap(); + } + + #[test] + fn paths_are_plain() { + assert!(check_path("assets/ruby/textures/block/ruby_block.png").is_ok()); + assert!(check_path("data/ruby/loot_table/blocks/a.json").is_ok()); + for bad in [ + "assets/ruby", + "assets/../x/y", + "assets/ruby//a", + "assets/Ruby/a", + "assets/ruby/a b", + "/assets/ruby/a", + "other/ruby/a", + "assets\\ruby\\a", + ] { + assert!(check_path(bad).is_err(), "{bad}"); + } + } +} diff --git a/crates/horizon-mods/src/lib.rs b/crates/horizon-mods/src/lib.rs index f2bb3833c..ec9ebe67d 100644 --- a/crates/horizon-mods/src/lib.rs +++ b/crates/horizon-mods/src/lib.rs @@ -5,10 +5,12 @@ //! //! Nothing here runs unless a mod is installed and turned on: an empty //! [`Mods`] makes no engine and every hook returns at once. +pub mod content; pub mod manifest; #[cfg(feature = "runtime")] mod runtime; +pub use content::Content; pub use manifest::{Capability, Manifest, Package, Side}; #[cfg(feature = "runtime")] pub use runtime::*; diff --git a/crates/horizon-mods/src/manifest.rs b/crates/horizon-mods/src/manifest.rs index 984ef7e6b..47eeba8fe 100644 --- a/crates/horizon-mods/src/manifest.rs +++ b/crates/horizon-mods/src/manifest.rs @@ -9,6 +9,10 @@ pub const WASM: &str = "mod.wasm"; /// The API (WIT package `horizon:mods`) versions this build runs: a mod /// built for any 0.1.x SDK. pub const API: &str = "0.1"; +/// The manifest `api` lines this build loads. 0.2 is 0.1 plus content: +/// `assets/` and `data/` in the `.hmod`, with blocks and items declared in +/// `data//horizon/` (docs/modpacks.md). Code still links API 0.1. +pub const APIS: [&str; 2] = [API, "0.2"]; /// The biggest `mod.wasm` a mod may ship. const MAX_WASM: u64 = 32 << 20; @@ -23,7 +27,7 @@ pub struct Manifest { pub description: String, #[serde(default)] pub authors: Vec, - /// The API version the mod was built for: `0.1`. + /// The API line the mod was built for: `0.1`, or `0.2` for content. pub api: String, #[serde(default)] pub side: Side, @@ -51,15 +55,24 @@ pub enum Capability { GameRead, Chat, Hud, + /// New blocks and items from the mod's `data/` (API 0.2): no WIT + /// interface, the game reads them before the world loads. + Registries, } impl Capability { - pub const ALL: [Capability; 3] = [Capability::GameRead, Capability::Chat, Capability::Hud]; + pub const ALL: [Capability; 4] = [ + Capability::GameRead, + Capability::Chat, + Capability::Hud, + Capability::Registries, + ]; /// The WIT interface it grants, in `horizon:mods`. pub fn interface(self) -> &'static str { match self { Capability::GameRead => "game", Capability::Chat => "chat", Capability::Hud => "hud", + Capability::Registries => "registries", } } /// What the player reads before turning the mod on: "May ". @@ -68,6 +81,7 @@ impl Capability { Capability::GameRead => "see your position, health and the time", Capability::Chat => "show you chat messages", Capability::Hud => "draw text on your screen", + Capability::Registries => "add blocks and items to your worlds", } } /// The capability a component import needs, `Ok(None)` for one every @@ -84,6 +98,7 @@ impl Capability { } Capability::ALL .into_iter() + .filter(|c| *c != Capability::Registries) .find(|c| c.interface() == interface) .map(Some) .ok_or_else(unknown) @@ -95,6 +110,7 @@ impl fmt::Display for Capability { Capability::GameRead => "game-read", Capability::Chat => "chat", Capability::Hud => "hud", + Capability::Registries => "registries", }; f.write_str(name) } @@ -113,8 +129,17 @@ impl Manifest { m.id )); } - if m.api != API { - return Err(format!("built for API {}; this Horizon runs {API}", m.api)); + if !APIS.contains(&m.api.as_str()) { + return Err(format!( + "built for API {}; this Horizon runs {}", + m.api, + APIS.join(" and ") + )); + } + if m.capabilities.contains(&Capability::Registries) && m.api == API { + return Err(format!( + "{MANIFEST}: capability registries needs api = \"0.2\"" + )); } Ok(m) } @@ -137,54 +162,79 @@ impl Manifest { /// A mod as found on disk: its manifest and component bytes. pub struct Package { pub manifest: Manifest, - pub wasm: Vec, + /// None for a content-only mod (capability `registries`, no code). + pub wasm: Option>, } impl Package { - /// A `.hmod` (a zip holding `horizon-mod.toml` and `mod.wasm`) or a - /// folder holding the same two files. + /// A `.hmod` (a zip holding `horizon-mod.toml`, `mod.wasm` and, for + /// content, `assets/` and `data/`) or a folder holding the same. pub fn read(path: &Path) -> Result { let (toml, wasm) = if path.is_dir() { let toml = std::fs::read_to_string(path.join(MANIFEST)); - let wasm = std::fs::read(path.join(WASM)); - ( - toml.map_err(|e| format!("{MANIFEST}: {e}"))?, - wasm.map_err(|e| format!("{WASM}: {e}"))?, - ) + let wasm = path.join(WASM); + let wasm = match wasm.exists() { + true => Some(std::fs::read(wasm).map_err(|e| format!("{WASM}: {e}"))?), + false => None, + }; + (toml.map_err(|e| format!("{MANIFEST}: {e}"))?, wasm) } else { let file = std::fs::File::open(path).map_err(|e| e.to_string())?; let mut zip = zip::ZipArchive::new(file).map_err(|e| e.to_string())?; - let mut entry = |name: &str| -> Result, String> { - let f = zip.by_name(name).map_err(|e| format!("{name}: {e}"))?; + let mut entry = |name: &str| -> Result>, String> { + let f = match zip.by_name(name) { + Ok(f) => f, + Err(zip::result::ZipError::FileNotFound) => return Ok(None), + Err(e) => return Err(format!("{name}: {e}")), + }; let mut out = Vec::new(); f.take(MAX_WASM + 1) .read_to_end(&mut out) .map_err(|e| e.to_string())?; - Ok(out) + Ok(Some(out)) }; - let toml = String::from_utf8(entry(MANIFEST)?) - .map_err(|_| format!("{MANIFEST}: not UTF-8"))?; + let toml = entry(MANIFEST)?.ok_or_else(|| format!("{MANIFEST}: missing"))?; + let toml = String::from_utf8(toml).map_err(|_| format!("{MANIFEST}: not UTF-8"))?; (toml, entry(WASM)?) }; - if wasm.len() as u64 > MAX_WASM { + if wasm.as_ref().is_some_and(|w| w.len() as u64 > MAX_WASM) { return Err(format!("{WASM} is over {} MiB", MAX_WASM >> 20)); } - Ok(Package { - manifest: Manifest::parse(&toml)?, - wasm, - }) + let manifest = Manifest::parse(&toml)?; + if wasm.is_none() && !manifest.capabilities.contains(&Capability::Registries) { + return Err(format!( + "{WASM}: missing (only a content mod, with capability registries, may have no code)" + )); + } + Ok(Package { manifest, wasm }) } /// The `.hmod` bytes for `manifest_text` and a component. pub fn write(manifest_text: &str, wasm: &[u8]) -> Result, String> { + Self::write_files(manifest_text, Some(wasm), &[]) + } + /// The `.hmod` bytes for `manifest_text`, a component if the mod has + /// code, and content `files` (`assets/...`, `data/...`) by path. + pub fn write_files( + manifest_text: &str, + wasm: Option<&[u8]>, + files: &[(&str, &[u8])], + ) -> Result, String> { use std::io::Write; Manifest::parse(manifest_text)?; let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); let options = zip::write::SimpleFileOptions::default(); let err = |e: zip::result::ZipError| e.to_string(); - zip.start_file(MANIFEST, options).map_err(err)?; - zip.write_all(manifest_text.as_bytes()) - .map_err(|e| e.to_string())?; - zip.start_file(WASM, options).map_err(err)?; - zip.write_all(wasm).map_err(|e| e.to_string())?; + let mut put = |name: &str, bytes: &[u8]| -> Result<(), String> { + zip.start_file(name, options).map_err(err)?; + zip.write_all(bytes).map_err(|e| e.to_string()) + }; + put(MANIFEST, manifest_text.as_bytes())?; + if let Some(wasm) = wasm { + put(WASM, wasm)?; + } + for (name, bytes) in files { + crate::content::check_path(name)?; + put(name, bytes)?; + } Ok(zip.finish().map_err(err)?.into_inner()) } } @@ -256,8 +306,8 @@ capabilities = ["game-read", "hud"] fn refuses_bad_ids_and_apis() { let bad_id = EXAMPLE.replace("coords_hud", "Coords HUD"); assert!(Manifest::parse(&bad_id).unwrap_err().contains("is not")); - let bad_api = EXAMPLE.replace("api = \"0.1\"", "api = \"0.2\""); - assert!(Manifest::parse(&bad_api).unwrap_err().contains("API 0.2")); + let bad_api = EXAMPLE.replace("api = \"0.1\"", "api = \"0.3\""); + assert!(Manifest::parse(&bad_api).unwrap_err().contains("API 0.3")); let unknown = format!("{EXAMPLE}network = true\n"); assert!(Manifest::parse(&unknown).is_err()); } @@ -270,7 +320,7 @@ capabilities = ["game-read", "hud"] std::fs::write(&path, Package::write(EXAMPLE, b"\0asm").unwrap()).unwrap(); let p = Package::read(&path).unwrap(); assert_eq!(p.manifest.id, "coords_hud"); - assert_eq!(p.wasm, b"\0asm"); + assert_eq!(p.wasm.as_deref(), Some(&b"\0asm"[..])); std::fs::remove_dir_all(&dir).unwrap(); } } diff --git a/crates/horizon-mods/src/runtime.rs b/crates/horizon-mods/src/runtime.rs index 7e3741287..f8a6e5865 100644 --- a/crates/horizon-mods/src/runtime.rs +++ b/crates/horizon-mods/src/runtime.rs @@ -153,9 +153,12 @@ impl Mods { let on = enabled(&package.manifest.id); entry.manifest = Some(package.manifest.clone()); if on { - entry.status = match mods.start(&package, &reserved) { + // Content is read before the world loads; a mod whose + // content is refused stays off, with the reason. + let content = crate::Content::read(path, &package.manifest); + entry.status = match content.and_then(|_| mods.start(&package, &reserved)) { Ok(run) => { - entry.run = Some(run); + entry.run = run; Status::Running } Err(e) => Status::Failed(e), @@ -184,12 +187,17 @@ impl Mods { Ok(self.engine.as_ref().expect("made above")) } + /// Starts a mod's code; `Ok(None)` for a content-only mod, which has + /// nothing to run (the game reads its content before the world loads). fn start( &mut self, package: &Package, reserved: &dyn Fn(&str) -> bool, - ) -> Result { + ) -> Result, String> { let m = &package.manifest; + let Some(wasm) = &package.wasm else { + return Ok(None); + }; if m.side == Side::Server { return Err("server mods do not run yet".into()); } @@ -202,7 +210,7 @@ impl Mods { let snapshot = self.snapshot.clone(); let (engine, linker) = self.engine()?; let component = - Component::new(engine, &package.wasm).map_err(|e| format!("not a component: {e}"))?; + Component::new(engine, wasm).map_err(|e| format!("not a component: {e}"))?; let ty = component.component_type(); let imports: Vec = ty .imports(engine) @@ -237,11 +245,11 @@ impl Mods { free }); self.out.append(&mut store.data_mut().out); - Ok(Running { + Ok(Some(Running { store, exports, hooks, - }) + })) } /// True when at least one mod runs: the game skips its mod work otherwise. @@ -403,7 +411,7 @@ impl Mods { match started { Ok(run) => { log::info!("Horizon: mod {id} reloaded"); - entry.run = Some(run); + entry.run = run; entry.status = Status::Running; } Err(e) => { diff --git a/crates/horizon-mods/tests/sandbox.rs b/crates/horizon-mods/tests/sandbox.rs index 6a0389d55..6aa45e5cc 100644 --- a/crates/horizon-mods/tests/sandbox.rs +++ b/crates/horizon-mods/tests/sandbox.rs @@ -131,7 +131,8 @@ fn undeclared_capabilities_are_refused() { std::fs::create_dir_all(&dir).unwrap(); let wasm = Package::read(&mods_dir().join("horizon_mods_test_guest.hmod")) .unwrap() - .wasm; + .wasm + .expect("the guest has code"); let manifest = GUEST_MANIFEST.replace("[\"chat\"]", "[]"); std::fs::write(dir.join("horizon-mod.toml"), manifest).unwrap(); std::fs::write(dir.join("mod.wasm"), wasm).unwrap(); @@ -241,3 +242,34 @@ fn hook_overhead() { started.elapsed().as_secs_f64() * 1000.0 ); } + +#[test] +fn content_only_mods_load_without_an_engine() { + let manifest = r#" +id = "ruby" +name = "Ruby" +version = "0.1.0" +api = "0.2" +side = "both" +capabilities = ["registries"] +"#; + let block: &[u8] = br#"{"template": "minecraft:iron_block"}"#; + let dir = mods_dir().join("content"); + std::fs::create_dir_all(&dir).unwrap(); + let ok = dir.join("ruby.hmod"); + let files: &[(&str, &[u8])] = &[("data/ruby/horizon/block/ruby_block.json", block)]; + std::fs::write(&ok, Package::write_files(manifest, None, files).unwrap()).unwrap(); + let bad = dir.join("bad.hmod"); + let files: &[(&str, &[u8])] = &[("data/minecraft/recipe/stone.json", b"{}")]; + let bad_manifest = manifest.replace("\"ruby\"", "\"bad\""); + std::fs::write( + &bad, + Package::write_files(&bad_manifest, None, files).unwrap(), + ) + .unwrap(); + let mods = Mods::load(&[ok, bad], |_| true, |_| false); + assert_eq!(mods.entries()[0].status, Status::Running); + assert!(matches!(&mods.entries()[1].status, Status::Failed(e) if e.contains("own namespace"))); + // Content has no hooks: the game's per-tick mod work stays skipped. + assert!(!mods.is_running()); +} diff --git a/docs/mods.md b/docs/mods.md index 6b9f88096..6203c4066 100644 --- a/docs/mods.md +++ b/docs/mods.md @@ -262,6 +262,28 @@ capabilities = ["game-read", "hud", "chat"] Unknown keys are refused, so a typo is an error, not a silently ignored permission. +### Content mods (API 0.2) + +A mod with `api = "0.2"` and capability `registries` may carry `assets/` and +`data/` in the `.hmod`, and needs no `mod.wasm` if it has no code. It adds +blocks and items by declaring them in `data//horizon/block/*.json` and +`data//horizon/item/*.json`, each copying a vanilla template +([modpacks.md](modpacks.md#registries-02-data-driven-blocks-and-items)): + +```json +{ "template": "minecraft:iron_block", + "properties": { "hardness": 4.0, "light": 3, "sounds": "minecraft:amethyst_block" }, + "item": true } +``` + +The fence: files go in the mod's own namespace (`assets//`, +`data//`), plus additions to vanilla tags (`data/minecraft/tags/`). +Templates and `sounds` name vanilla blocks; unknown keys are refused. Paths +use vanilla's resource characters, at most 65,536 files, 16 MiB each and +512 MiB in all. A mod whose content is refused stays off, with the reason on +its row. The game reading this content into its block and item tables comes +next (docs/modpacks.md). + ## Versions and compatibility - The API is the WIT package `horizon:mods@0.1.0`. New functions and From 002cfcd8593dfc293b3ed9e07523bcfd0e0f3870 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 16 Oct 2026 18:33:11 +0200 Subject: [PATCH 05/12] Content mods in the resource root: template rows appended to every block table horizon_client::mod_content builds the resource root for content mods as an overlay over the base root (vanilla, or the resource-pack overlay): - the base is linked; - the block and item tables are rewritten, each mod block getting its template's rows appended. That covers 13 JSON tables (facts, gameplay, behaviours, models, render, landing, items, command registries, mob data, worldgen flags, structure, survival) and 6 NBT ones (occlusion, collision, support, movement, worldgen block properties). Names, translation keys and in-table state ids move over, and the declared overrides apply; - each mod's own assets/ and data/ are copied in; - tag additions are merged into the tag files and both block-tag bundles. Vanilla state ids never move: mod states come after them, mods and blocks in id order. Rewritten JSON keeps its key order. With no content mod the root is the base itself. The game doesn't call this yet. Tested over the real prepared resources: every loader reads the overlay (Assets, BlockFacts, Items, collision, visibility, light, motion, worldgen GeneratorData). The mod blocks carry their templates' facts with the overrides applied, and vanilla's rows and ids are unchanged. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/lib.rs | 1 + crates/horizon-client/src/mod_content.rs | 818 ++++++++++++++++++ .../horizon-client/src/mod_content_tests.rs | 213 +++++ 3 files changed, 1032 insertions(+) create mode 100644 crates/horizon-client/src/mod_content.rs create mode 100644 crates/horizon-client/src/mod_content_tests.rs diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index b56891996..d6b4dd345 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -107,6 +107,7 @@ pub mod mesher; pub mod minimap; pub mod mipmap; pub mod mobs; +pub mod mod_content; pub mod mod_list; mod model_library; pub mod models; diff --git a/crates/horizon-client/src/mod_content.rs b/crates/horizon-client/src/mod_content.rs new file mode 100644 index 000000000..106d73010 --- /dev/null +++ b/crates/horizon-client/src/mod_content.rs @@ -0,0 +1,818 @@ +//! Content mods in the resource root (docs/modpacks.md, registries 0.2). +//! +//! Every loader reads one resource root, and most of its block and item +//! tables are exports from vanilla indexed by block-state id or keyed by +//! name. A content mod's block copies a vanilla *template*. So the mods' +//! root is an overlay, like the resource-pack one: +//! - the base root linked, with the per-state and per-name tables +//! rewritten: each mod block gets its template's rows appended (states +//! after vanilla's, mods and blocks in id order) with names, translation +//! keys and in-table state ids moved over, and the declared overrides +//! applied; +//! - each mod's own `assets/` and `data/` files; +//! - additions to tags merged into the tag files and bundles. +//! +//! The loaders need no mod code, vanilla state ids never move, and with no +//! content mod the root is the base root itself. +use crate::{Result, blocks::MAP_COLORS}; +use horizon_mods::content::{Block, Content, Rarity}; +use horizon_world::nbt::{self, JavaString, Limits, Tag}; +use serde::{Deserialize, Serialize, de}; +use std::{ + collections::BTreeMap, + fmt, fs, + ops::Range, + path::{Path, PathBuf}, +}; + +/// A content mod to put in the root: its id and content. +pub struct ContentMod { + pub id: String, + pub content: Content, +} + +/// JSON with its object keys in file order, so a rewritten table differs +/// from the original only by what was added. +#[derive(Clone, Debug, PartialEq)] +enum J { + Null, + Bool(bool), + Num(serde_json::Number), + Str(String), + Arr(Vec), + Obj(Vec<(String, J)>), +} +impl<'de> Deserialize<'de> for J { + fn deserialize>(d: D) -> std::result::Result { + struct V; + impl<'de> de::Visitor<'de> for V { + type Value = J; + fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str("JSON") + } + fn visit_unit(self) -> std::result::Result { + Ok(J::Null) + } + fn visit_bool(self, v: bool) -> std::result::Result { + Ok(J::Bool(v)) + } + fn visit_i64(self, v: i64) -> std::result::Result { + Ok(J::Num(v.into())) + } + fn visit_u64(self, v: u64) -> std::result::Result { + Ok(J::Num(v.into())) + } + fn visit_f64(self, v: f64) -> std::result::Result { + serde_json::Number::from_f64(v) + .map(J::Num) + .ok_or_else(|| E::custom("not a JSON number")) + } + fn visit_str(self, v: &str) -> std::result::Result { + Ok(J::Str(v.to_owned())) + } + fn visit_string(self, v: String) -> std::result::Result { + Ok(J::Str(v)) + } + fn visit_seq>( + self, + mut a: A, + ) -> std::result::Result { + let mut out = Vec::new(); + while let Some(v) = a.next_element()? { + out.push(v); + } + Ok(J::Arr(out)) + } + fn visit_map>( + self, + mut a: A, + ) -> std::result::Result { + let mut out = Vec::new(); + while let Some(kv) = a.next_entry()? { + out.push(kv); + } + Ok(J::Obj(out)) + } + } + d.deserialize_any(V) + } +} +impl Serialize for J { + fn serialize(&self, s: S) -> std::result::Result { + use serde::ser::{SerializeMap, SerializeSeq}; + match self { + J::Null => s.serialize_unit(), + J::Bool(b) => s.serialize_bool(*b), + J::Num(n) => n.serialize(s), + J::Str(t) => s.serialize_str(t), + J::Arr(a) => { + let mut seq = s.serialize_seq(Some(a.len()))?; + for v in a { + seq.serialize_element(v)?; + } + seq.end() + } + J::Obj(o) => { + let mut map = s.serialize_map(Some(o.len()))?; + for (k, v) in o { + map.serialize_entry(k, v)?; + } + map.end() + } + } + } +} +impl J { + fn get(&self, key: &str) -> Option<&J> { + match self { + J::Obj(o) => o.iter().find(|(k, _)| k == key).map(|(_, v)| v), + _ => None, + } + } + fn get_mut(&mut self, key: &str) -> Option<&mut J> { + match self { + J::Obj(o) => o.iter_mut().find(|(k, _)| k == key).map(|(_, v)| v), + _ => None, + } + } + /// Sets `key`, in place if present, else at the end. + fn set(&mut self, key: &str, value: J) { + if let Some(v) = self.get_mut(key) { + *v = value; + } else if let J::Obj(o) = self { + o.push((key.to_owned(), value)); + } + } + /// The value at a `/`-separated path ("" is the document). + fn at(&mut self, path: &str) -> Option<&mut J> { + path.split('/') + .filter(|p| !p.is_empty()) + .try_fold(self, |j, key| j.get_mut(key)) + } + fn f64(v: f64) -> J { + serde_json::Number::from_f64(v).map_or(J::Null, J::Num) + } + fn as_u32(&self) -> Option { + match self { + J::Num(n) => n.as_u64().and_then(|v| u32::try_from(v).ok()), + _ => None, + } + } + /// Renames strings and, with `ids`, moves state ids, everywhere inside. + fn rename(&mut self, r: &Rename, ids: bool) { + match self { + J::Str(s) => { + if let Some(new) = r.string(s) { + *s = new; + } + } + J::Num(n) if ids => { + if let Some(new) = n + .as_u64() + .and_then(|v| u32::try_from(v).ok()) + .and_then(|id| r.id(id)) + { + *n = new.into(); + } + } + J::Arr(a) => a.iter_mut().for_each(|v| v.rename(r, ids)), + J::Obj(o) => o.iter_mut().for_each(|(_, v)| v.rename(r, ids)), + _ => {} + } + } +} + +/// One id moving to another: `minecraft:iron_block` → `ruby:ruby_block`, +/// with its state range for blocks. +#[derive(Clone)] +struct Rename { + from: String, + to: String, + states: Range, + first: u32, +} +impl Rename { + fn new(from: &str, to: &str) -> Self { + Rename { + from: from.to_owned(), + to: to.to_owned(), + states: 0..0, + first: 0, + } + } + /// The id itself, `Block{id}`, and `block..` / `item..` keys. + fn string(&self, s: &str) -> Option { + if s == self.from { + return Some(self.to.clone()); + } + if s.strip_prefix("Block{").and_then(|x| x.strip_suffix('}')) == Some(&self.from) { + return Some(format!("Block{{{}}}", self.to)); + } + let key = |id: &str| id.replacen(':', ".", 1).replace('/', "."); + for kind in ["block", "item"] { + if s.strip_prefix(kind).and_then(|x| x.strip_prefix('.')) == Some(&key(&self.from)) { + return Some(format!("{kind}.{}", key(&self.to))); + } + } + None + } + fn id(&self, id: u32) -> Option { + self.states + .contains(&id) + .then(|| self.first + (id - self.states.start)) + } +} + +/// A block being added: its declaration and where its states go. +struct NewBlock<'a> { + decl: &'a Block, + rename: Rename, + /// The template's item, if it has one. + item: Option, +} +/// An item being added (declared, or a block's). +struct NewItem { + rename: Rename, + max_stack_size: Option, + rarity: Option, +} + +struct Plan<'a> { + blocks: Vec>, + items: Vec, + /// Vanilla's state count (the tables' length before). + states: usize, + /// Each vanilla block's default state. + defaults: BTreeMap, + /// The gameplay rows' sound objects, by block, for `sounds` overrides. + sounds: BTreeMap, +} + +fn read_json(path: &Path) -> Result { + Ok(serde_json::from_slice(&fs::read(path)?).map_err(|e| format!("{}: {e}", path.display()))?) +} + +impl<'a> Plan<'a> { + fn new(base: &Path, mods: &'a [ContentMod]) -> Result { + let mut facts = read_json(&base.join("facts.json"))?; + let names: Vec = match facts.at("states") { + Some(J::Arr(states)) => states + .iter() + .map(|s| match s { + J::Arr(row) => match row.first() { + Some(J::Str(name)) => Ok(name.clone()), + _ => Err("facts.json: a state without a name"), + }, + _ => Err("facts.json: a state that is not a row"), + }) + .collect::>()?, + _ => return Err("facts.json: no states".into()), + }; + let defaults: BTreeMap = match facts.at("defaults") { + Some(J::Obj(o)) => o + .iter() + .filter_map(|(k, v)| Some((k.clone(), v.as_u32()?))) + .collect(), + _ => return Err("facts.json: no defaults".into()), + }; + let range = |name: &str| -> Option> { + let start = names.iter().position(|n| n == name)?; + let len = names[start..].iter().take_while(|n| *n == name).count(); + // A block's states are one run in registry order. + (names[start + len..].iter().all(|n| n != name)) + .then_some(start as u32..(start + len) as u32) + }; + let mut blocks = Vec::new(); + let mut next = names.len() as u32; + let mut sorted: Vec<&ContentMod> = mods.iter().collect(); + sorted.sort_by(|a, b| a.id.cmp(&b.id)); + let mut wanted_sounds = Vec::new(); + for m in &sorted { + let mut declared: Vec<&Block> = m.content.blocks.iter().collect(); + declared.sort_by(|a, b| a.id.cmp(&b.id)); + for decl in declared { + if defaults.contains_key(&decl.id) { + return Err(format!("{}: {} is already a block", m.id, decl.id).into()); + } + let states = range(&decl.template).ok_or_else(|| { + format!("{}: template {} is not a block", decl.id, decl.template) + })?; + let mut rename = Rename::new(&decl.template, &decl.id); + rename.first = next; + next += states.len() as u32; + rename.states = states; + if let Some(s) = &decl.properties.sounds { + if !defaults.contains_key(s) { + return Err(format!("{}: sounds {s} is not a block", decl.id).into()); + } + wanted_sounds.push(s.clone()); + } + blocks.push(NewBlock { + decl, + rename, + item: None, + }); + } + } + // Block items copy the template block's item. + let gameplay = read_json(&base.join("gameplay-facts.json"))?; + let rows = match gameplay.get("states") { + Some(J::Arr(rows)) if rows.len() == names.len() => rows, + _ => return Err("gameplay-facts.json does not match facts.json".into()), + }; + let mut sounds = BTreeMap::new(); + for s in wanted_sounds { + let row = &rows[defaults[&s] as usize]; + sounds.insert(s, row.get("sound").cloned().unwrap_or(J::Null)); + } + let mut items = Vec::new(); + for b in &mut blocks { + let row = &rows[b.rename.states.start as usize]; + b.item = match row.get("item") { + Some(J::Str(i)) if i != "minecraft:air" => Some(i.clone()), + _ => None, + }; + if b.decl.item { + let item = b.item.as_deref().ok_or_else(|| { + format!("{}: template {} has no item", b.decl.id, b.decl.template) + })?; + items.push(NewItem { + rename: Rename::new(item, &b.decl.id), + max_stack_size: None, + rarity: None, + }); + } + } + for m in &sorted { + for i in &m.content.items { + items.push(NewItem { + rename: Rename::new(&i.template, &i.id), + max_stack_size: i.max_stack_size, + rarity: i.rarity, + }); + } + } + Ok(Plan { + blocks, + items, + states: names.len(), + defaults, + sounds, + }) + } + + /// What moves in a new block's copies of its template's rows: the + /// block, and the template's item when it has another name. A block + /// without an item gets `minecraft:air` where rows name one. + fn renames(&self, b: &NewBlock) -> Vec { + let mut out = vec![b.rename.clone()]; + if let Some(item) = b.item.as_deref().filter(|i| *i != b.decl.template) + && b.decl.item + { + out.push(Rename::new(item, &b.decl.id)); + } + out + } + + /// Appends each new block's copies of the template's states to `list`; + /// `gameplay` rows get their index and the declared overrides. + fn states(&self, list: &mut Vec, ids: bool, gameplay: bool) -> Result<()> { + if list.len() != self.states { + return Err(format!("{} states, not {}", list.len(), self.states).into()); + } + for b in &self.blocks { + let renames = self.renames(b); + for (i, id) in b.rename.states.clone().enumerate() { + let mut row = list[id as usize].clone(); + for r in &renames { + row.rename(r, ids); + } + if gameplay { + row.set("id", J::Num((b.rename.first + i as u32).into())); + if !b.decl.item { + row.set("item", J::Str("minecraft:air".into())); + } + self.override_gameplay(b, &mut row); + } + list.push(row); + } + } + Ok(()) + } + + /// The declared properties, on a gameplay row. + fn override_gameplay(&self, b: &NewBlock, row: &mut J) { + let p = &b.decl.properties; + if let Some(v) = p.hardness { + row.set("hardness", J::f64(v as f64)); + } + if let Some(v) = p.explosion_resistance { + row.set("explosion_resistance", J::f64(v as f64)); + } + if let Some(v) = p.light { + row.set("emission", J::Num(v.into())); + } + if let Some(v) = p.requires_tool { + row.set("requires_tool", J::Bool(v)); + } + if let Some(v) = p.friction { + row.set("friction", J::f64(v as f64)); + } + if let Some(v) = p.map_color { + let col = if v == 0 { 0 } else { MAP_COLORS[v as usize] }; + row.set("map_color", J::Num(col.into())); + } + if let Some(s) = &p.sounds { + row.set("sound", self.sounds[s].clone()); + } + } + + /// Adds each new block's copy of the template's entry to a map by name. + fn names(&self, map: &mut J, ids: bool) { + for b in &self.blocks { + if let Some(v) = map.get(&b.decl.template) { + let mut v = v.clone(); + for r in &self.renames(b) { + v.rename(r, ids); + } + map.set(&b.decl.id, v); + } + } + } + + /// Adds each new item's copy of its template's entry to a map by name; + /// `components` applies the overrides (items.json). + fn items(&self, map: &mut J, components: bool) { + for i in &self.items { + let Some(v) = map.get(&i.rename.from) else { + continue; + }; + let mut v = v.clone(); + v.rename(&i.rename, false); + if components { + if let Some(n) = i.max_stack_size { + v.set("minecraft:max_stack_size", J::Num(n.into())); + } + if let Some(r) = i.rarity { + let r = match r { + Rarity::Common => "common", + Rarity::Uncommon => "uncommon", + Rarity::Rare => "rare", + Rarity::Epic => "epic", + }; + v.set("minecraft:rarity", J::Str(r.into())); + } + } + map.set(&i.rename.to, v); + } + } + + /// One JSON table, rewritten. + fn json(&self, name: &str, doc: &mut J) -> Result<()> { + let states = |doc: &mut J, path: &str, ids: bool, gameplay: bool| -> Result<()> { + match doc.at(path) { + Some(J::Arr(a)) => self.states(a, ids, gameplay), + _ => Err(format!("no list at /{path}").into()), + } + }; + match name { + "facts.json" => { + states(doc, "states", false, false)?; + let defaults = doc.at("defaults").ok_or("no defaults")?; + for b in &self.blocks { + let default = self.defaults[&b.decl.template]; + let id = b.rename.id(default).ok_or("a default outside its block")?; + defaults.set(&b.decl.id, J::Num(id.into())); + } + } + "gameplay-facts.json" => states(doc, "states", false, true)?, + "block-behaviours.json" => { + states(doc, "states", false, false)?; + let Some(J::Arr(blocks)) = doc.at("blocks") else { + return Err("no blocks".into()); + }; + for b in &self.blocks { + let template = blocks.iter().find( + |x| matches!(x.get("name"), Some(J::Str(n)) if *n == b.decl.template), + ); + if let Some(mut v) = template.cloned() { + for r in &self.renames(b) { + v.rename(r, false); + } + if !b.decl.item { + v.set("item", J::Str("minecraft:air".into())); + } + blocks.push(v); + } + } + self.items(doc.at("items").ok_or("no items")?, false); + for odds in ["fire/burnOdds", "fire/igniteOdds"] { + if let Some(map) = doc.at(odds) { + self.names(map, false); + } + } + } + "model-states.json" + | "landing-definitions.json" + | "worldgen/blocks/survival-rules.json" => self.names(doc, false), + "render-definitions.json" => self.names(doc, true), + "worldgen/blocks/survival-extra-definitions.json" => { + if let Some(map) = doc.at("ignition_odds") { + self.names(map, false); + } + } + "items.json" => self.items(doc, true), + "command-registries.json" => { + self.names(doc.at("blocks").ok_or("no blocks")?, false); + let Some(J::Arr(items)) = doc.at("items") else { + return Err("no items".into()); + }; + items.extend(self.items.iter().map(|i| J::Str(i.rename.to.clone()))); + } + "mob-data.json" => states(doc, "states", false, false)?, + "worldgen/block_flags.json" => states(doc, "", false, false)?, + "worldgen/blocks/structure-blocks.json" => { + for key in ["rotate", "mirror", "full_block"] { + states(doc, key, true, false)?; + } + let added: usize = self.blocks.iter().map(|b| b.rename.states.len()).sum(); + doc.set("states", J::Num(((self.states + added) as u64).into())); + } + _ => return Err("no rule for this table".into()), + } + Ok(()) + } + + /// One NBT table: `states` lists, `cached_shapes` arrays and `blocks` + /// compounds, wherever they are. + fn nbt(&self, tag: &mut Tag) { + let Tag::Compound(c) = tag else { + return; + }; + for (key, value) in c.iter_mut() { + match (key.to_string_lossy().as_str(), value) { + ("states", Tag::List(list)) if list.len() == self.states => { + for b in &self.blocks { + for id in b.rename.states.clone() { + let mut state = list[id as usize].clone(); + if let Some(slot) = state.get_mut("id") { + *slot = Tag::String(b.decl.id.as_str().into()); + } + list.push(state); + } + } + } + ("cached_shapes", Tag::IntArray(a)) if a.len() == self.states => { + for b in &self.blocks { + let r = &b.rename.states; + let copy = a[r.start as usize..r.end as usize].to_vec(); + a.extend(copy); + } + } + ("blocks", Tag::Compound(map)) => { + for b in &self.blocks { + let Some(mut v) = map + .get(&JavaString::from(b.decl.template.as_str())) + .cloned() + else { + continue; + }; + if let (Some(f), Some(slot)) = + (b.decl.properties.friction, v.get_mut("friction")) + { + *slot = Tag::Float(f.to_bits()); + } + map.insert(b.decl.id.as_str().into(), v); + } + } + (_, inner @ Tag::Compound(_)) => self.nbt(inner), + _ => {} + } + } + } +} + +/// The JSON and NBT tables rewritten for new blocks and items. +const JSON_TABLES: [&str; 13] = [ + "facts.json", + "gameplay-facts.json", + "block-behaviours.json", + "model-states.json", + "render-definitions.json", + "landing-definitions.json", + "items.json", + "command-registries.json", + "mob-data.json", + "worldgen/block_flags.json", + "worldgen/blocks/structure-blocks.json", + "worldgen/blocks/survival-rules.json", + "worldgen/blocks/survival-extra-definitions.json", +]; +const NBT_TABLES: [&str; 6] = [ + "occlusion.nbt", + "cached-collision.nbt", + "movement-properties.nbt", + "worldgen/blocks/block-properties.nbt", + "worldgen/blocks/cached-support.nbt", + "worldgen/blocks/cached-collision.nbt", +]; +/// Bundles of block tag files (tag id → the tag file's text). +const TAG_BUNDLES: [&str; 2] = ["block-tags.json", "worldgen/block_tags.json"]; + +/// The overlay being written: `out` mirrors `base` through links, and a +/// directory becomes real only when something inside it is written. +struct Overlay<'a> { + base: &'a Path, + out: &'a Path, +} +impl Overlay<'_> { + fn new<'a>(base: &'a Path, out: &'a Path) -> Result> { + if fs::symlink_metadata(out).is_ok() { + fs::remove_dir_all(out)?; + } + fs::create_dir_all(out)?; + let overlay = Overlay { base, out }; + overlay.link_children(Path::new(""))?; + Ok(overlay) + } + fn link_children(&self, rel: &Path) -> Result<()> { + let Ok(read) = fs::read_dir(self.base.join(rel)) else { + return Ok(()); + }; + for entry in read.flatten() { + let rel = rel.join(entry.file_name()); + link(&self.base.join(&rel), &self.out.join(&rel))?; + } + Ok(()) + } + /// Makes `rel`'s directories real, each holding links to the base's. + fn dir(&self, rel: &Path) -> Result<()> { + let mut at = PathBuf::new(); + for part in rel.iter() { + at.push(part); + let path = self.out.join(&at); + let meta = fs::symlink_metadata(&path); + if meta + .as_ref() + .is_ok_and(|m| m.is_dir() && !m.file_type().is_symlink()) + { + continue; + } + if meta.is_ok() { + fs::remove_file(&path)?; + } + fs::create_dir(&path)?; + self.link_children(&at)?; + } + Ok(()) + } + fn write(&self, rel: &str, bytes: &[u8]) -> Result<()> { + let rel = Path::new(rel); + if let Some(parent) = rel.parent() { + self.dir(parent)?; + } + let path = self.out.join(rel); + if fs::symlink_metadata(&path).is_ok() { + fs::remove_file(&path)?; + } + fs::write(path, bytes)?; + Ok(()) + } +} +#[cfg(unix)] +fn link(target: &Path, at: &Path) -> std::io::Result<()> { + std::os::unix::fs::symlink(target, at) +} +#[cfg(not(unix))] +fn link(target: &Path, at: &Path) -> std::io::Result<()> { + if target.is_dir() { + fs::create_dir_all(at)?; + for entry in fs::read_dir(target)?.flatten() { + link(&entry.path(), &at.join(entry.file_name()))?; + } + Ok(()) + } else { + fs::copy(target, at).map(|_| ()) + } +} + +/// A tag file's JSON text with `more` appended to its values (no repeats). +fn merge_tag(text: Option<&str>, more: &[J], what: &str) -> Result { + let mut doc = match text { + Some(t) => serde_json::from_str(t).map_err(|e| format!("{what}: {e}"))?, + None => J::Obj(vec![("values".into(), J::Arr(Vec::new()))]), + }; + let Some(J::Arr(values)) = doc.get_mut("values") else { + return Err(format!("{what}: no values").into()); + }; + for v in more { + if !values.contains(v) { + values.push(v.clone()); + } + } + Ok(serde_json::to_string_pretty(&doc)?) +} + +/// Builds the content root for `mods` over `base` in `out`. +pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> { + let plan = Plan::new(base, mods)?; + let overlay = Overlay::new(base, out)?; + for name in JSON_TABLES { + let path = base.join(name); + if !path.exists() { + continue; + } + let mut doc = read_json(&path)?; + plan.json(name, &mut doc) + .map_err(|e| format!("{name}: {e}"))?; + overlay.write(name, &serde_json::to_vec(&doc)?)?; + } + for name in NBT_TABLES { + let path = base.join(name); + if !path.exists() { + continue; + } + let (mut named, _) = nbt::read_named(&fs::read(&path)?, Limits::default()) + .map_err(|e| format!("{name}: {e}"))?; + plan.nbt(&mut named.tag); + let bytes = + nbt::write_named(&named, Limits::default()).map_err(|e| format!("{name}: {e}"))?; + overlay.write(name, &bytes)?; + } + // Mod files, and every tag addition gathered per tag file. + let mut tags: BTreeMap> = BTreeMap::new(); + for m in mods { + let declarations = format!("data/{}/horizon/", m.id); + for file in &m.content.files { + let bytes = m.content.file(file)?; + let is_tag = file.starts_with("data/") && file.contains("/tags/"); + if is_tag && file.ends_with(".json") { + let doc: J = serde_json::from_slice(&bytes).map_err(|e| format!("{file}: {e}"))?; + if doc.get("replace") == Some(&J::Bool(true)) { + let e = format!( + "{}: {file}: a mod adds to tags, it cannot replace them", + m.id + ); + return Err(e.into()); + } + let Some(J::Arr(values)) = doc.get("values") else { + return Err(format!("{}: {file}: no values", m.id).into()); + }; + tags.entry(file.clone()) + .or_default() + .extend(values.iter().cloned()); + } else if !file.starts_with(&declarations) { + overlay.write(file, &bytes)?; + } + } + } + let mut bundles: Vec<(&str, J)> = Vec::new(); + for name in TAG_BUNDLES { + if base.join(name).exists() { + bundles.push((name, read_json(&base.join(name))?)); + } + } + for (file, values) in &tags { + let vanilla = fs::read_to_string(base.join(file)).ok(); + overlay.write( + file, + merge_tag(vanilla.as_deref(), values, file)?.as_bytes(), + )?; + // data//tags/block/.json is the block tag :. + let Some((ns, path)) = file + .strip_prefix("data/") + .and_then(|r| r.split_once("/tags/block/")) + .and_then(|(ns, p)| Some((ns, p.strip_suffix(".json")?))) + else { + continue; + }; + let id = format!("{ns}:{path}"); + for (name, doc) in &mut bundles { + let text = match doc.get(&id) { + Some(J::Str(t)) => Some(t.clone()), + _ => None, + }; + let merged = merge_tag(text.as_deref(), values, name)?; + doc.set(&id, J::Str(merged)); + } + } + for (name, doc) in &bundles { + overlay.write(name, &serde_json::to_vec(doc)?)?; + } + Ok(()) +} + +/// The root to load: `base` itself with no content, else the mods' overlay +/// over it, built in `cache/mod-content`. +pub fn resource_root(base: &Path, mods: &[ContentMod], cache: &Path) -> Result { + if mods.iter().all(|m| m.content.is_empty()) { + return Ok(base.to_owned()); + } + let out = cache.join("mod-content"); + build(base, mods, &out)?; + Ok(out) +} + +#[cfg(test)] +#[path = "mod_content_tests.rs"] +mod tests; diff --git a/crates/horizon-client/src/mod_content_tests.rs b/crates/horizon-client/src/mod_content_tests.rs new file mode 100644 index 000000000..b5f235916 --- /dev/null +++ b/crates/horizon-client/src/mod_content_tests.rs @@ -0,0 +1,213 @@ +use super::*; +use crate::{assets::Assets, blocks::BlockFacts, items::Items}; + +#[test] +fn renames_ids_keys_and_states() { + let mut r = Rename::new("minecraft:iron_block", "ruby:ruby_block"); + r.states = 10..12; + r.first = 100; + assert_eq!( + r.string("minecraft:iron_block").as_deref(), + Some("ruby:ruby_block") + ); + assert_eq!( + r.string("Block{minecraft:iron_block}").as_deref(), + Some("Block{ruby:ruby_block}") + ); + assert_eq!( + r.string("block.minecraft.iron_block").as_deref(), + Some("block.ruby.ruby_block") + ); + assert_eq!(r.string("minecraft:iron_blocks"), None); + assert_eq!(r.string("block.minecraft.iron_bars"), None); + assert_eq!( + (r.id(9), r.id(10), r.id(11), r.id(12)), + (None, Some(100), Some(101), None) + ); +} + +#[test] +fn json_keeps_key_order_and_numbers() { + let text = r#"{"z":1,"a":[0.6,5.0,-1,{"m":null,"b":true}],"k":"v"}"#; + let doc: J = serde_json::from_str(text).unwrap(); + assert_eq!(serde_json::to_string(&doc).unwrap(), text); +} + +#[test] +fn tags_merge_without_repeats() { + let vanilla = r#"{"values": ["minecraft:stone"]}"#; + let more = [ + J::Str("ruby:ruby_block".into()), + J::Str("minecraft:stone".into()), + ]; + let merged: serde_json::Value = + serde_json::from_str(&merge_tag(Some(vanilla), &more, "t").unwrap()).unwrap(); + assert_eq!( + merged["values"], + serde_json::json!(["minecraft:stone", "ruby:ruby_block"]) + ); +} + +fn prepared() -> Option { + let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3"); + root.join("gameplay-facts.json").exists().then_some(root) +} + +/// A content mod folder: a full block, a door and an item-less glass from +/// templates, an item, a vanilla tag addition and a language file. +fn ruby_mod(dir: &Path) -> ContentMod { + let files: [(&str, &str); 7] = [ + ( + "horizon-mod.toml", + "id = \"ruby\"\nname = \"Ruby\"\nversion = \"0.1.0\"\napi = \"0.2\"\nside = \"both\"\ncapabilities = [\"registries\"]\n", + ), + ( + "data/ruby/horizon/block/ruby_block.json", + r#"{"template": "minecraft:iron_block", "properties": {"hardness": 4.0, "light": 3, + "sounds": "minecraft:amethyst_block", "map_color": 28, "friction": 0.8}}"#, + ), + ( + "data/ruby/horizon/block/ruby_door.json", + r#"{"template": "minecraft:oak_door"}"#, + ), + ( + "data/ruby/horizon/block/ruby_glass.json", + r#"{"template": "minecraft:glass", "item": false}"#, + ), + ( + "data/ruby/horizon/item/ruby.json", + r#"{"template": "minecraft:diamond", "max_stack_size": 16, "rarity": "rare"}"#, + ), + ( + "data/minecraft/tags/block/mineable/pickaxe.json", + r#"{"values": ["ruby:ruby_block"]}"#, + ), + ( + "assets/ruby/lang/en_us.json", + r#"{"block.ruby.ruby_block": "Block of Ruby"}"#, + ), + ]; + for (name, text) in files { + let path = dir.join(name); + fs::create_dir_all(path.parent().unwrap()).unwrap(); + fs::write(path, text).unwrap(); + } + let package = horizon_mods::Package::read(dir).unwrap(); + ContentMod { + id: "ruby".into(), + content: Content::read(dir, &package.manifest).unwrap(), + } +} + +/// The overlay over the real prepared resources: every loader reads it, the +/// new blocks behave like their templates with the overrides, and vanilla's +/// states are where they were. +#[test] +fn content_root_loads_with_every_table() { + let Some(base) = prepared() else { + return; + }; + let scratch = std::env::temp_dir().join(format!("horizon-mod-content-{}", std::process::id())); + let _ = fs::remove_dir_all(&scratch); + let m = ruby_mod(&scratch.join("ruby")); + let root = resource_root(&base, std::slice::from_ref(&m), &scratch.join("cache")).unwrap(); + assert_ne!(root, base); + + let vanilla = Assets::load(&base).unwrap(); + let assets = Assets::load(&root).unwrap(); + let (old, new) = (vanilla.registry, assets.registry); + let n = old.len() as u32; + let iron = old.default_state("minecraft:iron_block").unwrap(); + let door = old.default_state("minecraft:oak_door").unwrap(); + let door_first = (0..n) + .map(BlockStateId) + .find(|&s| old.state(s).unwrap().name == "minecraft:oak_door") + .unwrap(); + // Vanilla keeps its ids; blocks come after, sorted: ruby_block, ruby_door, ruby_glass. + assert_eq!(new.len(), old.len() + 1 + 64 + 1); + assert_eq!(new.default_state("minecraft:iron_block"), Some(iron)); + let ruby = new.default_state("ruby:ruby_block").unwrap(); + assert_eq!(ruby, BlockStateId(n)); + let ruby_door = new.default_state("ruby:ruby_door").unwrap(); + assert_eq!(ruby_door, BlockStateId(n + 1 + (door.0 - door_first.0))); + assert_eq!( + new.default_state("ruby:ruby_glass"), + Some(BlockStateId(n + 65)) + ); + + let old_facts = BlockFacts::load(&base, old).unwrap(); + let facts = BlockFacts::load(&root, new).unwrap(); + let (a, b) = (old_facts.get(iron), facts.get(ruby)); + assert_eq!((b.hardness, b.emission, b.friction), (4.0, 3, 0.8)); + assert_eq!(b.explosion_resistance, a.explosion_resistance); + assert_eq!(b.outline, a.outline); + assert_eq!(b.classes, a.classes); + assert_eq!(&*b.item, "ruby:ruby_block"); + assert_eq!(b.map_color, 28); + let amethyst = old_facts.get(old.default_state("minecraft:amethyst_block").unwrap()); + assert_eq!(b.sounds.break_sound, amethyst.sounds.break_sound); + for i in 0..64 { + let (v, m) = ( + old_facts.get(BlockStateId(door_first.0 + i)), + facts.get(BlockStateId(n + 1 + i)), + ); + assert_eq!( + (v.outline.clone(), v.hardness, v.classes.clone()), + (m.outline.clone(), m.hardness, m.classes.clone()) + ); + assert_eq!(&*m.name, "ruby:ruby_door"); + } + let glass = facts.get(BlockStateId(n + 65)); + assert_eq!(&*glass.item, "minecraft:air"); + // Vanilla's own rows are untouched. + assert_eq!(facts.get(iron).hardness, a.hardness); + assert_eq!(&*facts.get(iron).name, "minecraft:iron_block"); + + let items = Items::load(&root, new, &facts).unwrap(); + let ruby_block = &items.items["ruby:ruby_block"]; + assert_eq!(ruby_block.block, Some(ruby)); + let gem = &items.items["ruby:ruby"]; + assert_eq!(gem.max_stack, 16); + assert!(!items.items.contains_key("ruby:ruby_glass")); + assert!(items.block_tags["minecraft:mineable/pickaxe"].contains("ruby:ruby_block")); + + crate::collisions::load(&root, new).unwrap(); + crate::models::load_visibility(&root, new).unwrap(); + crate::light::LightTables::load(&root, new, &facts).unwrap(); + crate::motion::MotionResources::load(&root, &assets).unwrap(); + horizon_world::worldgen::generator::GeneratorData::load(&root.join("worldgen"), new).unwrap(); + assert!(root.join("assets/ruby/lang/en_us.json").is_file()); + + // The base root was only read. + assert!( + fs::symlink_metadata(base.join("facts.json")) + .unwrap() + .is_file() + ); + fs::remove_dir_all(&scratch).unwrap(); +} + +#[test] +fn refuses_unknown_templates_and_vanilla_ids() { + let Some(base) = prepared() else { + return; + }; + let scratch = std::env::temp_dir().join(format!("horizon-mod-refuse-{}", std::process::id())); + let _ = fs::remove_dir_all(&scratch); + let mut m = ruby_mod(&scratch.join("ruby")); + m.content.blocks[0].template = "minecraft:not_a_block".into(); + let e = build(&base, std::slice::from_ref(&m), &scratch.join("out")).unwrap_err(); + assert!(e.to_string().contains("is not a block"), "{e}"); + fs::remove_dir_all(&scratch).unwrap(); +} + +#[test] +fn no_content_keeps_the_base_root() { + let base = Path::new("/nonexistent/base"); + assert_eq!( + resource_root(base, &[], Path::new("/nonexistent/cache")).unwrap(), + base + ); +} + +use horizon_world::registry::BlockStateId; From ac7b7f98c0292ead567c90b4553bdbd911128329 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 16 Oct 2026 19:13:17 +0200 Subject: [PATCH 06/12] Content mods in the game: loot, recipes, models, names and icons from mod namespaces The game builds the content root from the enabled content mods at startup. Loaders that only read the minecraft namespace now also read the content mods' namespaces (from the root's marker file): block loot tables, recipes, blockstates, language files, and the HUD atlas's item sprites. /give and the command registries know the new items and blocks. A block without a blockstate of its own takes its template's, so every state has a model. Adds examples/ruby-content, a folder content mod: a ruby block and a ruby item with recipes, loot, tags, models, textures and names. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/crafting.rs | 39 +++++++++---- crates/horizon-client/src/creative.rs | 21 ++++--- crates/horizon-client/src/game.rs | 5 ++ crates/horizon-client/src/game/mods.rs | 54 ++++++++++++++++++ crates/horizon-client/src/hud.rs | 6 +- crates/horizon-client/src/items.rs | 38 ++++++++---- crates/horizon-client/src/mod_content.rs | 41 ++++++++++++- .../horizon-client/src/mod_content_tests.rs | 5 ++ crates/horizon-client/src/models.rs | 2 +- crates/horizon-client/src/models/catalog.rs | 6 +- .../assets/ruby/blockstates/ruby_block.json | 7 +++ .../ruby-content/assets/ruby/items/ruby.json | 6 ++ .../assets/ruby/items/ruby_block.json | 6 ++ .../ruby-content/assets/ruby/lang/en_us.json | 4 ++ .../assets/ruby/models/block/ruby_block.json | 6 ++ .../assets/ruby/models/item/ruby.json | 6 ++ .../assets/ruby/textures/block/ruby_block.png | Bin 0 -> 149 bytes .../assets/ruby/textures/item/ruby.png | Bin 0 -> 149 bytes .../tags/block/mineable/pickaxe.json | 5 ++ .../minecraft/tags/block/needs_iron_tool.json | 5 ++ .../data/ruby/horizon/block/ruby_block.json | 9 +++ .../data/ruby/horizon/item/ruby.json | 4 ++ .../ruby/loot_table/blocks/ruby_block.json | 18 ++++++ .../data/ruby/recipe/ruby_block.json | 15 +++++ .../ruby/recipe/ruby_from_ruby_block.json | 10 ++++ .../examples/ruby-content/horizon-mod.toml | 11 ++++ 26 files changed, 294 insertions(+), 35 deletions(-) create mode 100644 crates/horizon-mod-sdk/examples/ruby-content/assets/ruby/blockstates/ruby_block.json create mode 100644 crates/horizon-mod-sdk/examples/ruby-content/assets/ruby/items/ruby.json create mode 100644 crates/horizon-mod-sdk/examples/ruby-content/assets/ruby/items/ruby_block.json create mode 100644 crates/horizon-mod-sdk/examples/ruby-content/assets/ruby/lang/en_us.json create mode 100644 crates/horizon-mod-sdk/examples/ruby-content/assets/ruby/models/block/ruby_block.json create mode 100644 crates/horizon-mod-sdk/examples/ruby-content/assets/ruby/models/item/ruby.json create mode 100644 crates/horizon-mod-sdk/examples/ruby-content/assets/ruby/textures/block/ruby_block.png create mode 100644 crates/horizon-mod-sdk/examples/ruby-content/assets/ruby/textures/item/ruby.png create mode 100644 crates/horizon-mod-sdk/examples/ruby-content/data/minecraft/tags/block/mineable/pickaxe.json create mode 100644 crates/horizon-mod-sdk/examples/ruby-content/data/minecraft/tags/block/needs_iron_tool.json create mode 100644 crates/horizon-mod-sdk/examples/ruby-content/data/ruby/horizon/block/ruby_block.json create mode 100644 crates/horizon-mod-sdk/examples/ruby-content/data/ruby/horizon/item/ruby.json create mode 100644 crates/horizon-mod-sdk/examples/ruby-content/data/ruby/loot_table/blocks/ruby_block.json create mode 100644 crates/horizon-mod-sdk/examples/ruby-content/data/ruby/recipe/ruby_block.json create mode 100644 crates/horizon-mod-sdk/examples/ruby-content/data/ruby/recipe/ruby_from_ruby_block.json create mode 100644 crates/horizon-mod-sdk/examples/ruby-content/horizon-mod.toml diff --git a/crates/horizon-client/src/crafting.rs b/crates/horizon-client/src/crafting.rs index e4a70811a..457ddf497 100644 --- a/crates/horizon-client/src/crafting.rs +++ b/crates/horizon-client/src/crafting.rs @@ -14,7 +14,7 @@ use serde_json::Value; use std::{ collections::{HashMap, HashSet}, fs, - path::Path, + path::{Path, PathBuf}, sync::Arc, }; @@ -1182,18 +1182,33 @@ impl Recipes { let mut crafting = Vec::new(); let mut cooking = Vec::new(); let mut meta = HashMap::new(); - let mut files: Vec<_> = fs::read_dir(root.join("data/minecraft/recipe"))? - .flatten() - .map(|e| e.path()) - .filter(|p| p.extension().is_some_and(|e| e == "json")) + let json_files = |dir: &Path| -> Result> { + let mut files: Vec<_> = fs::read_dir(dir)? + .flatten() + .map(|e| e.path()) + .filter(|p| p.extension().is_some_and(|e| e == "json")) + .collect(); + files.sort(); + Ok(files) + }; + // Vanilla's recipes by bare name, then content mods' as `:`. + let mut files: Vec<(String, PathBuf)> = json_files(&root.join("data/minecraft/recipe"))? + .into_iter() + .map(|p| (String::new(), p)) .collect(); - files.sort(); - for path in files { - let name = path - .file_stem() - .and_then(|s| s.to_str()) - .unwrap_or("") - .to_owned(); + for namespace in crate::mod_content::namespaces(root) { + let dir = root.join(format!("data/{namespace}/recipe")); + if dir.is_dir() { + files.extend( + json_files(&dir)? + .into_iter() + .map(|p| (format!("{namespace}:"), p)), + ); + } + } + for (prefix, path) in files { + let stem = path.file_stem().and_then(|s| s.to_str()).unwrap_or(""); + let name = format!("{prefix}{stem}"); let v: Value = serde_json::from_slice(&fs::read(&path)?)?; let text = |k: &str| v[k].as_str().unwrap_or("").to_owned(); meta.insert(name.clone(), (text("category"), text("group"))); diff --git a/crates/horizon-client/src/creative.rs b/crates/horizon-client/src/creative.rs index 942bf13b9..80445d541 100644 --- a/crates/horizon-client/src/creative.rs +++ b/crates/horizon-client/src/creative.rs @@ -49,17 +49,24 @@ pub struct Names { } impl Names { pub fn load(root: &Path) -> Self { - let map = fs::read(root.join("assets/minecraft/lang/en_us.json")) - .ok() - .and_then(|b| serde_json::from_slice::>(&b).ok()) - .unwrap_or_default(); + let read = |namespace: &str| { + fs::read(root.join(format!("assets/{namespace}/lang/en_us.json"))) + .ok() + .and_then(|b| serde_json::from_slice::>(&b).ok()) + .unwrap_or_default() + }; + let mut map = read("minecraft"); + // Content mods' names (docs/modpacks.md); none in a vanilla root. + for namespace in crate::mod_content::namespaces(root) { + map.extend(read(&namespace)); + } Self { map } } pub fn item(&self, id: &str) -> String { - let path = id.trim_start_matches("minecraft:"); + let (namespace, path) = id.split_once(':').unwrap_or(("minecraft", id)); self.map - .get(&format!("item.minecraft.{path}")) - .or_else(|| self.map.get(&format!("block.minecraft.{path}"))) + .get(&format!("item.{namespace}.{path}")) + .or_else(|| self.map.get(&format!("block.{namespace}.{path}"))) .cloned() .unwrap_or_else(|| path.replace('_', " ")) } diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 7072abeb5..fb83c291f 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -9746,6 +9746,11 @@ pub fn run() -> Result<()> { let vanilla_root = options.resources.clone(); let early_settings = Settings::load(&std::env::current_dir()?.join("options.txt")); options.resources = packs::startup_root(&vanilla_root, &early_settings.resource_packs); + options.resources = mods::content_root( + &options.resources, + &early_settings.mods.enabled, + &options.dev_mods, + ); let assets = match Assets::load(&options.resources) { Ok(assets) => assets, // Minecraft.rollbackResourcePacks: the packs break loading, start diff --git a/crates/horizon-client/src/game/mods.rs b/crates/horizon-client/src/game/mods.rs index 56cb1d0d8..939cb671d 100644 --- a/crates/horizon-client/src/game/mods.rs +++ b/crates/horizon-client/src/game/mods.rs @@ -47,6 +47,60 @@ fn vanilla_command(name: &str) -> bool { .any(|&c| d.nodes[c].name() == name) } +/// `root` with the content mods that are on layered over it +/// (docs/modpacks.md): their blocks, items, data and assets. Read once at +/// start, before the resources load; a mod whose content is refused is left +/// out here and its row on the Mods page says why. +pub(super) fn content_root( + root: &std::path::Path, + enabled: &std::collections::BTreeSet, + dev: &[std::path::PathBuf], +) -> std::path::PathBuf { + use horizon_client::mod_content::{self, ContentMod}; + use horizon_mods::{Capability, Content, Package}; + let here = std::env::current_dir().unwrap_or_default(); + let mut paths = horizon_mods::find(&here.join(mod_list::DIR)); + paths.retain(|p| !dev.contains(p)); + paths.extend(dev.iter().cloned()); + let mut mods: Vec = Vec::new(); + for path in &paths { + let Ok(package) = Package::read(path) else { + continue; + }; + let m = &package.manifest; + let on = enabled.contains(&m.id) || dev.contains(path); + if !on + || !m.capabilities.contains(&Capability::Registries) + || mods.iter().any(|c| c.id == m.id) + { + continue; + } + match Content::read(path, m) { + Ok(content) => mods.push(ContentMod { + id: m.id.clone(), + content, + }), + Err(e) => log::warn!("Horizon: mod {} content not loaded: {e}", m.id), + } + } + match mod_content::resource_root(root, &mods, &here.join(".horizon-cache")) { + Ok(out) => { + if out != root { + log::info!( + "Horizon: content from {} mod(s) in {}", + mods.len(), + out.display() + ); + } + out + } + Err(e) => { + log::warn!("Horizon: content mods not loaded: {e}"); + root.to_owned() + } + } +} + impl Game<'_> { /// Reads `horizon-mods/` and the dev mods, and starts the ones turned on. pub(super) fn start_mods(&mut self) { diff --git a/crates/horizon-client/src/hud.rs b/crates/horizon-client/src/hud.rs index e73803ee7..cf4861458 100644 --- a/crates/horizon-client/src/hud.rs +++ b/crates/horizon-client/src/hud.rs @@ -102,8 +102,10 @@ fn atlas_sprites(root: &Path, packed: &[PackedImage]) -> Vec { let image = match &origin { SpriteOrigin::File(texture) => { let path = texture.strip_prefix("minecraft:").unwrap_or(texture); - // Packed under its own name already: only a renamed file is new. - if path == name { + // A vanilla file is packed under its own name already: + // only a renamed one is new. Other namespaces' files + // (content mods) are never in the walked directories. + if texture.starts_with("minecraft:") && path == name { continue; } load(texture, "textures").map(|(w, h, p)| { diff --git a/crates/horizon-client/src/items.rs b/crates/horizon-client/src/items.rs index bf49efec7..0aca99cef 100644 --- a/crates/horizon-client/src/items.rs +++ b/crates/horizon-client/src/items.rs @@ -512,11 +512,16 @@ impl Items { let mut loot = HashMap::new(); // Every loot table, keyed by its id (blocks/stone, entities/sheep/white, // gameplay/chicken_lay, shearing/sheep/red...). - fn walk_loot(dir: &Path, base: &Path, loot: &mut HashMap) -> Result<()> { + fn walk_loot( + dir: &Path, + base: &Path, + namespace: &str, + loot: &mut HashMap, + ) -> Result<()> { for entry in fs::read_dir(dir)?.flatten() { let p = entry.path(); if p.is_dir() { - walk_loot(&p, base, loot)?; + walk_loot(&p, base, namespace, loot)?; } else if p.extension().is_some_and(|e| e == "json") { let id = p .strip_prefix(base)? @@ -524,7 +529,7 @@ impl Items { .to_string_lossy() .replace('\\', "/"); loot.insert( - format!("minecraft:{id}"), + format!("{namespace}:{id}"), serde_json::from_slice(&fs::read(&p)?)?, ); } @@ -532,7 +537,14 @@ impl Items { Ok(()) } let loot_root = root.join("data/minecraft/loot_table"); - walk_loot(&loot_root, &loot_root, &mut loot)?; + walk_loot(&loot_root, &loot_root, "minecraft", &mut loot)?; + // Content mods' tables (docs/modpacks.md): `:blocks/...`. + for namespace in crate::mod_content::namespaces(root) { + let dir = root.join(format!("data/{namespace}/loot_table")); + if dir.is_dir() { + walk_loot(&dir, &dir, &namespace, &mut loot)?; + } + } let registry_order = registry_order(root, &order); Ok(Self { items, @@ -644,12 +656,15 @@ impl Items { state: BlockStateId, ) -> Option { let block = registry.state(state)?; - let name = block.name.trim_start_matches("minecraft:"); + let (namespace, name) = block + .name + .split_once(':') + .unwrap_or(("minecraft", &block.name)); let standing = name.replacen("_wall_", "_", 1); let standing = standing.strip_prefix("wall_").unwrap_or(&standing); let standing = standing.replacen("wall_", "", 1); - self.loot_sequence(&format!("minecraft:blocks/{name}")) - .or_else(|| self.loot_sequence(&format!("minecraft:blocks/{standing}"))) + self.loot_sequence(&format!("{namespace}:blocks/{name}")) + .or_else(|| self.loot_sequence(&format!("{namespace}:blocks/{standing}"))) } /// Evaluate the block's loot table for a player break without explosions. pub fn block_drops( @@ -737,7 +752,10 @@ impl Items { let Some(block) = registry.state(state) else { return vec![]; }; - let name = block.name.trim_start_matches("minecraft:"); + let (namespace, name) = block + .name + .split_once(':') + .unwrap_or(("minecraft", &block.name)); // `Blocks.wallVariant`: wall signs, banners, heads, fans and torches // reuse the standing block's loot table. let standing = name.replacen("_wall_", "_", 1); @@ -745,8 +763,8 @@ impl Items { let standing = standing.replacen("wall_", "", 1); let table = self .loot - .get(&format!("minecraft:blocks/{name}")) - .or_else(|| self.loot.get(&format!("minecraft:blocks/{standing}"))); + .get(&format!("{namespace}:blocks/{name}")) + .or_else(|| self.loot.get(&format!("{namespace}:blocks/{standing}"))); let Some(table) = table else { return vec![]; }; diff --git a/crates/horizon-client/src/mod_content.rs b/crates/horizon-client/src/mod_content.rs index 106d73010..3a8c2920e 100644 --- a/crates/horizon-client/src/mod_content.rs +++ b/crates/horizon-client/src/mod_content.rs @@ -524,10 +524,19 @@ impl<'a> Plan<'a> { "items.json" => self.items(doc, true), "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())); + let new_blocks = || self.blocks.iter().map(|b| J::Str(b.decl.id.clone())); let Some(J::Arr(items)) = doc.at("items") else { return Err("no items".into()); }; - items.extend(self.items.iter().map(|i| J::Str(i.rename.to.clone()))); + items.extend(new_items()); + // The registries commands check ids against. + if let Some(J::Arr(ids)) = doc.at("by_registry/minecraft:item/ids") { + ids.extend(new_items()); + } + if let Some(J::Arr(ids)) = doc.at("by_registry/minecraft:block/ids") { + ids.extend(new_blocks()); + } } "mob-data.json" => states(doc, "states", false, false)?, "worldgen/block_flags.json" => states(doc, "", false, false)?, @@ -616,6 +625,19 @@ const NBT_TABLES: [&str; 6] = [ "worldgen/blocks/cached-support.nbt", "worldgen/blocks/cached-collision.nbt", ]; +/// In a root built here: the content mods' ids, the namespaces loaders +/// read besides `minecraft`. +const MARKER: &str = "horizon-mod-content.json"; + +/// The content mods' namespaces in a root built by [`build`]; none in any +/// other root, so vanilla's loaders read exactly what they did. +pub fn namespaces(root: &Path) -> Vec { + fs::read(root.join(MARKER)) + .ok() + .and_then(|b| serde_json::from_slice(&b).ok()) + .unwrap_or_default() +} + /// Bundles of block tag files (tag id → the tag file's text). const TAG_BUNDLES: [&str; 2] = ["block-tags.json", "worldgen/block_tags.json"]; @@ -799,6 +821,23 @@ pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> { for (name, doc) in &bundles { overlay.write(name, &serde_json::to_vec(doc)?)?; } + // A block without its own blockstate looks like its template, so every + // state still has a model. + for m in mods { + for block in &m.content.blocks { + let (ns, path) = block.id.split_once(':').unwrap_or((&m.id, &block.id)); + let own = format!("assets/{ns}/blockstates/{path}.json"); + if m.content.files.contains(&own) { + continue; + } + let template = block.template.trim_start_matches("minecraft:"); + let vanilla = base.join(format!("assets/minecraft/blockstates/{template}.json")); + overlay.write(&own, &fs::read(&vanilla)?)?; + } + } + let mut ids: Vec<&str> = mods.iter().map(|m| m.id.as_str()).collect(); + ids.sort(); + overlay.write(MARKER, &serde_json::to_vec(&ids)?)?; Ok(()) } diff --git a/crates/horizon-client/src/mod_content_tests.rs b/crates/horizon-client/src/mod_content_tests.rs index b5f235916..6dd92ecfd 100644 --- a/crates/horizon-client/src/mod_content_tests.rs +++ b/crates/horizon-client/src/mod_content_tests.rs @@ -177,6 +177,11 @@ fn content_root_loads_with_every_table() { crate::motion::MotionResources::load(&root, &assets).unwrap(); horizon_world::worldgen::generator::GeneratorData::load(&root.join("worldgen"), new).unwrap(); assert!(root.join("assets/ruby/lang/en_us.json").is_file()); + // Blocks without a blockstate of their own take their template's. + assert_eq!( + fs::read(root.join("assets/ruby/blockstates/ruby_door.json")).unwrap(), + fs::read(base.join("assets/minecraft/blockstates/oak_door.json")).unwrap() + ); // The base root was only read. assert!( diff --git a/crates/horizon-client/src/models.rs b/crates/horizon-client/src/models.rs index 85d90b087..ba5bffefb 100644 --- a/crates/horizon-client/src/models.rs +++ b/crates/horizon-client/src/models.rs @@ -73,7 +73,7 @@ pub(crate) fn prepare_with_models( let blocks: Vec<&str> = facts .keys() .filter(|name| registry.default_state(name).is_some()) - .filter_map(|name| name.strip_prefix("minecraft:")) + .map(|name| name.strip_prefix("minecraft:").unwrap_or(name)) .collect(); let catalog = StateModels::from_facts_with_models(root, registry, &blocks, sprites, &facts, models)?; diff --git a/crates/horizon-client/src/models/catalog.rs b/crates/horizon-client/src/models/catalog.rs index 59a885821..cccf08900 100644 --- a/crates/horizon-client/src/models/catalog.rs +++ b/crates/horizon-client/src/models/catalog.rs @@ -94,7 +94,9 @@ impl StateModels { } } for block in blocks.iter().copied().collect::>() { - let name = format!("minecraft:{block}"); + // Vanilla blocks come bare; content mods' blocks with their namespace. + let (namespace, path) = block.split_once(':').unwrap_or(("minecraft", block)); + let name = format!("{namespace}:{path}"); let facts = facts.get(&name).ok_or("missing block model state schema")?; let schema: PropertySchema = facts["schema"] .as_object() @@ -133,7 +135,7 @@ impl StateModels { .collect::>()?; let possible: Vec = serde_json::from_value(facts["states"].clone())?; let resource = fs::read_to_string( - root.join(format!("assets/minecraft/blockstates/{block}.json")), + root.join(format!("assets/{namespace}/blockstates/{path}.json")), )?; let definition = BlockStateDefinition::from_json(&resource)?; let bound = definition.bind(&schema, &possible)?; diff --git a/crates/horizon-mod-sdk/examples/ruby-content/assets/ruby/blockstates/ruby_block.json b/crates/horizon-mod-sdk/examples/ruby-content/assets/ruby/blockstates/ruby_block.json new file mode 100644 index 000000000..53d9292f2 --- /dev/null +++ b/crates/horizon-mod-sdk/examples/ruby-content/assets/ruby/blockstates/ruby_block.json @@ -0,0 +1,7 @@ +{ + "variants": { + "": { + "model": "ruby:block/ruby_block" + } + } +} diff --git a/crates/horizon-mod-sdk/examples/ruby-content/assets/ruby/items/ruby.json b/crates/horizon-mod-sdk/examples/ruby-content/assets/ruby/items/ruby.json new file mode 100644 index 000000000..683211550 --- /dev/null +++ b/crates/horizon-mod-sdk/examples/ruby-content/assets/ruby/items/ruby.json @@ -0,0 +1,6 @@ +{ + "model": { + "type": "minecraft:model", + "model": "ruby:item/ruby" + } +} diff --git a/crates/horizon-mod-sdk/examples/ruby-content/assets/ruby/items/ruby_block.json b/crates/horizon-mod-sdk/examples/ruby-content/assets/ruby/items/ruby_block.json new file mode 100644 index 000000000..7cd81eb4d --- /dev/null +++ b/crates/horizon-mod-sdk/examples/ruby-content/assets/ruby/items/ruby_block.json @@ -0,0 +1,6 @@ +{ + "model": { + "type": "minecraft:model", + "model": "ruby:block/ruby_block" + } +} diff --git a/crates/horizon-mod-sdk/examples/ruby-content/assets/ruby/lang/en_us.json b/crates/horizon-mod-sdk/examples/ruby-content/assets/ruby/lang/en_us.json new file mode 100644 index 000000000..0b1723511 --- /dev/null +++ b/crates/horizon-mod-sdk/examples/ruby-content/assets/ruby/lang/en_us.json @@ -0,0 +1,4 @@ +{ + "block.ruby.ruby_block": "Block of Ruby", + "item.ruby.ruby": "Ruby" +} diff --git a/crates/horizon-mod-sdk/examples/ruby-content/assets/ruby/models/block/ruby_block.json b/crates/horizon-mod-sdk/examples/ruby-content/assets/ruby/models/block/ruby_block.json new file mode 100644 index 000000000..e29b03d89 --- /dev/null +++ b/crates/horizon-mod-sdk/examples/ruby-content/assets/ruby/models/block/ruby_block.json @@ -0,0 +1,6 @@ +{ + "parent": "minecraft:block/cube_all", + "textures": { + "all": "ruby:block/ruby_block" + } +} diff --git a/crates/horizon-mod-sdk/examples/ruby-content/assets/ruby/models/item/ruby.json b/crates/horizon-mod-sdk/examples/ruby-content/assets/ruby/models/item/ruby.json new file mode 100644 index 000000000..8589c680f --- /dev/null +++ b/crates/horizon-mod-sdk/examples/ruby-content/assets/ruby/models/item/ruby.json @@ -0,0 +1,6 @@ +{ + "parent": "minecraft:item/generated", + "textures": { + "layer0": "ruby:item/ruby" + } +} diff --git a/crates/horizon-mod-sdk/examples/ruby-content/assets/ruby/textures/block/ruby_block.png b/crates/horizon-mod-sdk/examples/ruby-content/assets/ruby/textures/block/ruby_block.png new file mode 100644 index 0000000000000000000000000000000000000000..c6e4eeaf2b6023df2dfe5faed484a150fd8300d3 GIT binary patch literal 149 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`F`h1tAr-fhPY4J6@#joq5cHoK z^MCy$lVb-I3MG!I@tZR&lbD^BVz+{G}T@x>>JM28dS yUwqQo=+HC2@EFs^I<=b(7fT$@mpE{;F`S?B!{FkAd0T+?FnGH9xvX7RVqreg;b3MG!I@tZR&lbD^BVz+{G}T@x>>JM28dS xUwqQo=+HC2@EFs^I<=b(7fT$@mpE{;F?{~4u+)1)t0T}J22WQ%mvv4FO#t8qHlP3i literal 0 HcmV?d00001 diff --git a/crates/horizon-mod-sdk/examples/ruby-content/data/minecraft/tags/block/mineable/pickaxe.json b/crates/horizon-mod-sdk/examples/ruby-content/data/minecraft/tags/block/mineable/pickaxe.json new file mode 100644 index 000000000..d39b250dc --- /dev/null +++ b/crates/horizon-mod-sdk/examples/ruby-content/data/minecraft/tags/block/mineable/pickaxe.json @@ -0,0 +1,5 @@ +{ + "values": [ + "ruby:ruby_block" + ] +} diff --git a/crates/horizon-mod-sdk/examples/ruby-content/data/minecraft/tags/block/needs_iron_tool.json b/crates/horizon-mod-sdk/examples/ruby-content/data/minecraft/tags/block/needs_iron_tool.json new file mode 100644 index 000000000..d39b250dc --- /dev/null +++ b/crates/horizon-mod-sdk/examples/ruby-content/data/minecraft/tags/block/needs_iron_tool.json @@ -0,0 +1,5 @@ +{ + "values": [ + "ruby:ruby_block" + ] +} diff --git a/crates/horizon-mod-sdk/examples/ruby-content/data/ruby/horizon/block/ruby_block.json b/crates/horizon-mod-sdk/examples/ruby-content/data/ruby/horizon/block/ruby_block.json new file mode 100644 index 000000000..a07c26d08 --- /dev/null +++ b/crates/horizon-mod-sdk/examples/ruby-content/data/ruby/horizon/block/ruby_block.json @@ -0,0 +1,9 @@ +{ + "template": "minecraft:iron_block", + "properties": { + "hardness": 4.0, + "light": 3, + "sounds": "minecraft:amethyst_block", + "map_color": 28 + } +} diff --git a/crates/horizon-mod-sdk/examples/ruby-content/data/ruby/horizon/item/ruby.json b/crates/horizon-mod-sdk/examples/ruby-content/data/ruby/horizon/item/ruby.json new file mode 100644 index 000000000..4c89fe38f --- /dev/null +++ b/crates/horizon-mod-sdk/examples/ruby-content/data/ruby/horizon/item/ruby.json @@ -0,0 +1,4 @@ +{ + "template": "minecraft:diamond", + "rarity": "uncommon" +} diff --git a/crates/horizon-mod-sdk/examples/ruby-content/data/ruby/loot_table/blocks/ruby_block.json b/crates/horizon-mod-sdk/examples/ruby-content/data/ruby/loot_table/blocks/ruby_block.json new file mode 100644 index 000000000..e58b73946 --- /dev/null +++ b/crates/horizon-mod-sdk/examples/ruby-content/data/ruby/loot_table/blocks/ruby_block.json @@ -0,0 +1,18 @@ +{ + "type": "minecraft:block", + "pools": [ + { + "condition": { + "type": "minecraft:survives_explosion" + }, + "entries": [ + { + "type": "minecraft:item", + "name": "ruby:ruby_block" + } + ], + "rolls": 1 + } + ], + "random_sequence": "ruby:blocks/ruby_block" +} diff --git a/crates/horizon-mod-sdk/examples/ruby-content/data/ruby/recipe/ruby_block.json b/crates/horizon-mod-sdk/examples/ruby-content/data/ruby/recipe/ruby_block.json new file mode 100644 index 000000000..da374d024 --- /dev/null +++ b/crates/horizon-mod-sdk/examples/ruby-content/data/ruby/recipe/ruby_block.json @@ -0,0 +1,15 @@ +{ + "type": "minecraft:crafting_shaped", + "category": "building", + "key": { + "#": "ruby:ruby" + }, + "pattern": [ + "###", + "###", + "###" + ], + "result": { + "id": "ruby:ruby_block" + } +} diff --git a/crates/horizon-mod-sdk/examples/ruby-content/data/ruby/recipe/ruby_from_ruby_block.json b/crates/horizon-mod-sdk/examples/ruby-content/data/ruby/recipe/ruby_from_ruby_block.json new file mode 100644 index 000000000..dc416fa2f --- /dev/null +++ b/crates/horizon-mod-sdk/examples/ruby-content/data/ruby/recipe/ruby_from_ruby_block.json @@ -0,0 +1,10 @@ +{ + "type": "minecraft:crafting_shapeless", + "ingredients": [ + "ruby:ruby_block" + ], + "result": { + "count": 9, + "id": "ruby:ruby" + } +} diff --git a/crates/horizon-mod-sdk/examples/ruby-content/horizon-mod.toml b/crates/horizon-mod-sdk/examples/ruby-content/horizon-mod.toml new file mode 100644 index 000000000..39355bb57 --- /dev/null +++ b/crates/horizon-mod-sdk/examples/ruby-content/horizon-mod.toml @@ -0,0 +1,11 @@ +# A content-only Horizon mod (API 0.2, docs/modpacks.md): no code, a block +# and an item declared from vanilla templates, with their recipes, loot, +# tags, models, textures and names. Try it: horizon-client --dev-mod +id = "ruby" +name = "Ruby" +version = "0.1.0" +description = "A ruby gem and a block of ruby" +authors = ["Horizon"] +api = "0.2" +side = "both" +capabilities = ["registries"] From 85858610bfbbd91141fec78b960334d178d0f656 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 16 Oct 2026 19:24:49 +0200 Subject: [PATCH 07/12] horizon-port-pack: import a modpack and classify its mods A CLI for the first steps of docs/modpacks.md's pipeline: - import: a Modrinth .mrpack, a CurseForge zip or a Prism / MultiMC instance (or a folder of jars) to pack.json: Minecraft and loader versions, each file with its hash and sides, and the overrides. - inventory: Modrinth metadata by SHA-1 (cached in modrinth.json, so reruns work offline), and the jars when the user's instance has them (neoforge.mods.toml / mods.toml / fabric.mod.json and a file census, without decompiling). Each mod gets a class and API tier from the reviewed ATM10 table, then rules on categories, sides and the census, and a dependency layer. Writes inventory.json and inventory.csv. Only metadata is downloaded; jars are never fetched. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 12 + Cargo.toml | 2 +- crates/horizon-port-pack/Cargo.toml | 18 ++ crates/horizon-port-pack/src/classify.rs | 233 +++++++++++++++ crates/horizon-port-pack/src/import.rs | 335 ++++++++++++++++++++++ crates/horizon-port-pack/src/inventory.rs | 225 +++++++++++++++ crates/horizon-port-pack/src/jar.rs | 245 ++++++++++++++++ crates/horizon-port-pack/src/main.rs | 104 +++++++ crates/horizon-port-pack/src/modrinth.rs | 113 ++++++++ docs/modpacks.md | 14 + 10 files changed, 1300 insertions(+), 1 deletion(-) create mode 100644 crates/horizon-port-pack/Cargo.toml create mode 100644 crates/horizon-port-pack/src/classify.rs create mode 100644 crates/horizon-port-pack/src/import.rs create mode 100644 crates/horizon-port-pack/src/inventory.rs create mode 100644 crates/horizon-port-pack/src/jar.rs create mode 100644 crates/horizon-port-pack/src/main.rs create mode 100644 crates/horizon-port-pack/src/modrinth.rs diff --git a/Cargo.lock b/Cargo.lock index 90b288a8e..9c3ad04ab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1498,6 +1498,18 @@ dependencies = [ "horizon-mod-sdk", ] +[[package]] +name = "horizon-port-pack" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", + "sha1", + "toml", + "ureq", + "zip", +] + [[package]] name = "horizon-prepare" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 49ef5785f..1b4cb92d4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] exclude = ["launcher", "crates/horizon-telemetry-server"] -members = ["crates/horizon-core", "crates/horizon-dualsense", "crates/horizon-world", "crates/voxel-kernels", "crates/horizon-client", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-shaderpack", "crates/horizon-prepare", "crates/horizon-telemetry", "crates/horizon-bench", "crates/horizon-friends", "crates/horizon-relay", "crates/horizon-mod-sdk", "crates/horizon-mods", "crates/horizon-mods/tests/guest", "crates/cargo-horizon-mod", "crates/horizon-mod-sdk/examples/coords-hud", "xtask"] +members = ["crates/horizon-core", "crates/horizon-dualsense", "crates/horizon-world", "crates/voxel-kernels", "crates/horizon-client", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-shaderpack", "crates/horizon-prepare", "crates/horizon-telemetry", "crates/horizon-bench", "crates/horizon-friends", "crates/horizon-relay", "crates/horizon-mod-sdk", "crates/horizon-mods", "crates/horizon-mods/tests/guest", "crates/cargo-horizon-mod", "crates/horizon-port-pack", "crates/horizon-mod-sdk/examples/coords-hud", "xtask"] resolver = "2" [workspace.package] diff --git a/crates/horizon-port-pack/Cargo.toml b/crates/horizon-port-pack/Cargo.toml new file mode 100644 index 000000000..1b33b6c01 --- /dev/null +++ b/crates/horizon-port-pack/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "horizon-port-pack" +version = "0.1.0" +edition.workspace = true +license.workspace = true +rust-version = "1.86" +description = "Port a Java modpack to Horizon: import, inventory, classification and data carry-over (docs/modpacks.md)" + +[dependencies] +serde = { version = "1", features = ["derive"] } +serde_json = "1" +sha1 = "0.10" +toml = "1.1.6" +ureq = { version = "2", default-features = false, features = ["tls"] } +zip = { version = "2", default-features = false, features = ["deflate"] } + +[lints] +workspace = true diff --git a/crates/horizon-port-pack/src/classify.rs b/crates/horizon-port-pack/src/classify.rs new file mode 100644 index 000000000..7ebb65644 --- /dev/null +++ b/crates/horizon-port-pack/src/classify.rs @@ -0,0 +1,233 @@ +//! Step 2 and 3: classify each mod (class and API tier, `docs/modpacks.md`) +//! and order them by required dependencies. +use std::collections::{BTreeMap, HashMap}; + +/// The reviewed table: ATM10's 495 rows (`docs/modpacks/atm10-mods.csv`). +/// It wins over the heuristics. +const KNOWN_CSV: &str = include_str!("../../../docs/modpacks/atm10-mods.csv"); + +pub struct Known { + by_key: HashMap, +} + +/// Lowercase letters and digits only: `Get It Together, Drops!` -> +/// `getittogetherdrops`. +pub fn normalise(s: &str) -> String { + s.chars() + .filter(char::is_ascii_alphanumeric) + .map(|c| c.to_ascii_lowercase()) + .collect() +} + +/// One CSV line, with `"quoted, fields"`. +fn csv_fields(line: &str) -> Vec { + let (mut out, mut field, mut quoted) = (Vec::new(), String::new(), false); + let mut chars = line.chars().peekable(); + while let Some(c) = chars.next() { + match c { + '"' if quoted && chars.peek() == Some(&'"') => { + field.push('"'); + chars.next(); + } + '"' => quoted = !quoted, + ',' if !quoted => out.push(std::mem::take(&mut field)), + c => field.push(c), + } + } + out.push(field); + out +} + +impl Known { + pub fn atm10() -> Self { + let mut by_key = HashMap::new(); + for line in KNOWN_CSV.lines().skip(1) { + let f = csv_fields(line); + if f.len() < 6 || f[5] == "guess" { + continue; + } + let value = (f[3].clone(), f[4].clone()); + by_key.insert(normalise(&f[0]), value.clone()); + by_key.entry(normalise(&f[1])).or_insert(value); + } + Known { by_key } + } + + /// By mod id, Modrinth slug or name, in that order. + pub fn get(&self, keys: &[&str]) -> Option<&(String, String)> { + keys.iter() + .filter(|k| !k.is_empty()) + .find_map(|k| self.by_key.get(&normalise(k))) + } +} + +/// What the rules look at. +#[derive(Default)] +pub struct Facts<'a> { + /// `mods`, `resourcepacks`, `shaderpacks`, `datapacks`. + pub kind: &'a str, + pub client: &'a str, + pub server: &'a str, + /// Modrinth categories. + pub categories: &'a [String], + /// From the jar: classes, and whether it has blocks, items or data. + pub classes: Option, + pub has_content: bool, +} + +/// Class, tier and confidence (`known` is set by the caller). +pub fn rules(f: &Facts) -> (&'static str, &'static str, &'static str) { + let has = |c: &str| f.categories.iter().any(|x| x == c); + match f.kind { + "resourcepacks" => return ("other", "data", "kind"), + "shaderpacks" => return ("other", "none", "kind"), + "datapacks" => return ("content", "data", "kind"), + _ => {} + } + if has("library") { + return ("library", "none", "categories"); + } + if has("optimization") { + return ("performance", "none", "categories"); + } + if f.client != "unsupported" && f.server == "unsupported" { + return ("client-ui", "client", "sides"); + } + if f.client == "unsupported" && f.server != "unsupported" && !f.server.is_empty() { + return ("server-only", "logic", "sides"); + } + if f.classes == Some(0) && f.has_content { + return ("content", "data", "jar"); + } + let content = [ + "adventure", + "decoration", + "equipment", + "food", + "game-mechanics", + "magic", + "mobs", + "storage", + "technology", + "transportation", + "worldgen", + ]; + if f.categories.iter().any(|c| content.contains(&c.as_str())) { + let tier = if has("mobs") || has("magic") || has("adventure") { + "deep" + } else if has("technology") || has("storage") || has("transportation") { + "machines" + } else if f.categories.iter().all(|c| { + ["worldgen", "decoration", "food"].contains(&c.as_str()) + || !content.contains(&c.as_str()) + }) { + "data" + } else { + "logic" + }; + return ("content", tier, "categories"); + } + if has("utility") || has("management") || has("social") { + return ("other", "logic", "guess"); + } + ("unknown", "none", "unknown") +} + +/// Layer of each mod: 0 needs nothing in the pack, n needs layer n-1. +/// Mods in a cycle get one layer past the rest; dependencies outside the +/// pack are left out (the caller lists them). +pub fn layers(depends: &BTreeMap>) -> BTreeMap { + let mut out: BTreeMap = BTreeMap::new(); + loop { + let mut progressed = false; + for (id, deps) in depends { + if out.contains_key(id) { + continue; + } + let inside = deps.iter().filter(|d| depends.contains_key(*d) && *d != id); + let mut layer = 0; + let mut ready = true; + for d in inside { + match out.get(d) { + Some(l) => layer = layer.max(l + 1), + None => ready = false, + } + } + if ready { + out.insert(id.clone(), layer); + progressed = true; + } + } + if !progressed { + break; + } + } + let past = out.values().max().map_or(0, |m| m + 1); + for id in depends.keys() { + out.entry(id.clone()).or_insert(past); + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn known_table_matches_ids_and_names() { + let k = Known::atm10(); + assert_eq!(k.get(&["create"]).map(|v| v.0.as_str()), Some("content")); + assert_eq!( + k.get(&["", "Get It Together, Drops!"]) + .map(|v| v.0.as_str()), + Some("performance") + ); + assert_eq!(k.get(&["sodium"]).map(|v| v.1.as_str()), Some("none")); + assert!(k.get(&["not-a-mod"]).is_none()); + } + + #[test] + fn rules_follow_sides_then_categories() { + let cats = |c: &[&str]| c.iter().map(|s| s.to_string()).collect::>(); + let f = |client, server, c: &[String]| { + rules(&Facts { + kind: "mods", + client, + server, + categories: c, + ..Facts::default() + }) + }; + assert_eq!(f("required", "unsupported", &[]).0, "client-ui"); + assert_eq!(f("unsupported", "required", &[]).0, "server-only"); + assert_eq!(f("required", "required", &cats(&["library"])).0, "library"); + assert_eq!( + f("required", "unsupported", &cats(&["optimization"])).0, + "performance" + ); + assert_eq!( + f("required", "required", &cats(&["technology", "utility"])), + ("content", "machines", "categories") + ); + assert_eq!( + f("required", "required", &cats(&["worldgen", "neoforge"])), + ("content", "data", "categories") + ); + assert_eq!(f("", "", &[]).0, "unknown"); + } + + #[test] + fn layers_put_dependencies_first() { + let d = |v: &[&str]| v.iter().map(|s| s.to_string()).collect::>(); + let deps = BTreeMap::from([ + ("lib".to_string(), d(&[])), + ("create".to_string(), d(&["lib", "outside"])), + ("addon".to_string(), d(&["create", "lib"])), + ("a".to_string(), d(&["b"])), + ("b".to_string(), d(&["a"])), + ]); + let l = layers(&deps); + assert_eq!((l["lib"], l["create"], l["addon"]), (0, 1, 2)); + assert_eq!((l["a"], l["b"]), (3, 3)); + } +} diff --git a/crates/horizon-port-pack/src/import.rs b/crates/horizon-port-pack/src/import.rs new file mode 100644 index 000000000..b0f5d04c2 --- /dev/null +++ b/crates/horizon-port-pack/src/import.rs @@ -0,0 +1,335 @@ +//! Step 1, import: a Modrinth `.mrpack`, a CurseForge zip, or an instance +//! folder (Prism / MultiMC, or a plain `mods/` folder) to `pack.json`. +use serde::{Deserialize, Serialize}; +use serde_json::Value; +use std::{ + collections::BTreeMap, + fs, + io::Read, + path::{Path, PathBuf}, +}; + +#[derive(Debug, Default, Serialize, Deserialize)] +pub struct Pack { + pub name: String, + /// `mrpack`, `curseforge` or `instance`. + pub format: String, + pub minecraft: String, + /// `neoforge`, `forge`, `fabric` or `quilt`. + pub loader: String, + pub loader_version: String, + pub files: Vec, + /// Override files by top folder (`config`, `kubejs`, ...). + pub overrides: BTreeMap, +} + +#[derive(Debug, Default, Serialize, Deserialize)] +pub struct PackFile { + /// Path in the instance (`mods/create-1.21.1-6.0.4.jar`). + pub path: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub sha1: Option, + /// `required`, `optional` or `unsupported`; empty when not stated. + pub client: String, + pub server: String, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub downloads: Vec, + /// CurseForge project and file ids. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub curseforge: Option<(u64, u64)>, + /// The jar on disk (instances only). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub jar: Option, +} + +impl PackFile { + /// `mods`, `resourcepacks`, `shaderpacks`, `datapacks`... + pub fn kind(&self) -> &str { + self.path.split('/').next().unwrap_or("") + } + pub fn file_name(&self) -> &str { + self.path.rsplit('/').next().unwrap_or(&self.path) + } +} + +const LOADERS: [(&str, &str); 4] = [ + ("neoforge", "neoforge"), + ("forge", "forge"), + ("fabric-loader", "fabric"), + ("quilt-loader", "quilt"), +]; + +pub fn import(path: &Path) -> Result { + if path.is_dir() { + return instance(path); + } + let file = fs::File::open(path).map_err(|e| format!("{}: {e}", path.display()))?; + let mut zip = zip::ZipArchive::new(file).map_err(|e| format!("{}: {e}", path.display()))?; + if let Some(index) = read_json(&mut zip, "modrinth.index.json")? { + return Ok(mrpack(&index, &names(&zip))); + } + if let Some(manifest) = read_json(&mut zip, "manifest.json")? { + return Ok(curseforge(&manifest, &names(&zip))); + } + Err(format!( + "{}: neither modrinth.index.json nor manifest.json", + path.display() + )) +} + +fn names(zip: &zip::ZipArchive) -> Vec { + zip.file_names().map(str::to_owned).collect() +} + +fn read_json(zip: &mut zip::ZipArchive, name: &str) -> Result, String> { + let Ok(mut entry) = zip.by_name(name) else { + return Ok(None); + }; + let mut text = String::new(); + entry + .read_to_string(&mut text) + .map_err(|e| format!("{name}: {e}"))?; + serde_json::from_str(&text) + .map(Some) + .map_err(|e| format!("{name}: {e}")) +} + +fn str_of(v: &Value, key: &str) -> String { + v.get(key).and_then(Value::as_str).unwrap_or("").to_owned() +} + +/// Override files by top folder, below any of `roots`. +fn overrides(names: &[String], roots: &[&str]) -> BTreeMap { + let mut out = BTreeMap::new(); + for name in names.iter().filter(|n| !n.ends_with('/')) { + for root in roots { + if let Some(rest) = name.strip_prefix(root).and_then(|r| r.strip_prefix('/')) { + let top = rest.split('/').next().unwrap_or(rest); + *out.entry(top.to_owned()).or_default() += 1; + } + } + } + out +} + +pub fn mrpack(index: &Value, names: &[String]) -> Pack { + let deps = &index["dependencies"]; + let (loader, loader_version) = LOADERS + .iter() + .find_map(|(key, name)| Some((name.to_string(), deps.get(*key)?.as_str()?.to_owned()))) + .unwrap_or_default(); + let files = index["files"] + .as_array() + .into_iter() + .flatten() + .map(|f| PackFile { + path: str_of(f, "path"), + sha1: f["hashes"]["sha1"].as_str().map(str::to_owned), + client: str_of(&f["env"], "client"), + server: str_of(&f["env"], "server"), + downloads: f["downloads"] + .as_array() + .into_iter() + .flatten() + .filter_map(|d| d.as_str().map(str::to_owned)) + .collect(), + ..PackFile::default() + }) + .collect(); + Pack { + name: str_of(index, "name"), + format: "mrpack".into(), + minecraft: str_of(deps, "minecraft"), + loader, + loader_version, + files, + overrides: overrides( + names, + &["overrides", "client-overrides", "server-overrides"], + ), + } +} + +pub fn curseforge(manifest: &Value, names: &[String]) -> Pack { + let primary = manifest["minecraft"]["modLoaders"] + .as_array() + .into_iter() + .flatten() + .find(|l| l["primary"].as_bool().unwrap_or(true)) + .map(|l| str_of(l, "id")) + .unwrap_or_default(); + let (loader, loader_version) = primary.split_once('-').unwrap_or((&primary, "")); + let files = manifest["files"] + .as_array() + .into_iter() + .flatten() + .map(|f| { + let (p, id) = (f["projectID"].as_u64(), f["fileID"].as_u64()); + PackFile { + path: format!("mods/curseforge-{}-{}", p.unwrap_or(0), id.unwrap_or(0)), + // CurseForge states no side; its API needs a key, so names + // come from the user's installed instance (import that). + curseforge: p.zip(id), + ..PackFile::default() + } + }) + .collect(); + let root = manifest["overrides"].as_str().unwrap_or("overrides"); + Pack { + name: str_of(manifest, "name"), + format: "curseforge".into(), + minecraft: str_of(&manifest["minecraft"], "version"), + loader: loader.to_owned(), + loader_version: loader_version.to_owned(), + files, + overrides: overrides(names, &[root]), + } +} + +/// A Prism / MultiMC instance (`mmc-pack.json`), a game folder, or a +/// folder of jars: the jars on disk, hashed. +fn instance(dir: &Path) -> Result { + let mut pack = Pack { + name: dir + .file_name() + .map(|n| n.to_string_lossy().into_owned()) + .unwrap_or_default(), + format: "instance".into(), + ..Pack::default() + }; + if let Ok(text) = fs::read_to_string(dir.join("mmc-pack.json")) { + let mmc: Value = serde_json::from_str(&text).map_err(|e| format!("mmc-pack.json: {e}"))?; + for c in mmc["components"].as_array().into_iter().flatten() { + let version = str_of(c, "version"); + match c["uid"].as_str().unwrap_or("") { + "net.minecraft" => pack.minecraft = version, + "net.neoforged" => { + (pack.loader, pack.loader_version) = ("neoforge".into(), version) + } + "net.minecraftforge" => { + (pack.loader, pack.loader_version) = ("forge".into(), version) + } + "net.fabricmc.fabric-loader" => { + (pack.loader, pack.loader_version) = ("fabric".into(), version) + } + "org.quiltmc.quilt-loader" => { + (pack.loader, pack.loader_version) = ("quilt".into(), version) + } + _ => {} + } + } + } + let game = [".minecraft", "minecraft", "."] + .iter() + .map(|g| dir.join(g)) + .find(|g| g.join("mods").is_dir()) + .or_else(|| dir.read_dir().is_ok().then(|| dir.to_path_buf())) + .ok_or_else(|| format!("{}: no mods folder", dir.display()))?; + let mods = if game.join("mods").is_dir() { + game.join("mods") + } else { + game.clone() + }; + let mut jars: Vec = fs::read_dir(&mods) + .map_err(|e| format!("{}: {e}", mods.display()))? + .flatten() + .map(|e| e.path()) + .filter(|p| p.extension().is_some_and(|x| x == "jar")) + .collect(); + jars.sort(); + for jar in jars { + let bytes = fs::read(&jar).map_err(|e| format!("{}: {e}", jar.display()))?; + pack.files.push(PackFile { + path: format!( + "mods/{}", + jar.file_name().unwrap_or_default().to_string_lossy() + ), + sha1: Some(sha1_hex(&bytes)), + jar: Some(jar), + ..PackFile::default() + }); + } + for top in ["config", "kubejs", "defaultconfigs", "datapacks"] { + let n = count_files(&game.join(top)); + if n > 0 { + pack.overrides.insert(top.into(), n); + } + } + Ok(pack) +} + +fn count_files(dir: &Path) -> usize { + let Ok(entries) = fs::read_dir(dir) else { + return 0; + }; + entries + .flatten() + .map(|e| { + let p = e.path(); + if p.is_dir() { count_files(&p) } else { 1 } + }) + .sum() +} + +pub fn sha1_hex(bytes: &[u8]) -> String { + use sha1::Digest; + sha1::Sha1::digest(bytes) + .iter() + .map(|b| format!("{b:02x}")) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + #[test] + fn reads_an_mrpack_index() { + let index = json!({"name": "Small", "dependencies": {"minecraft": "1.21.1", "neoforge": "21.1.233"}, + "files": [{"path": "mods/create.jar", "hashes": {"sha1": "ab"}, "env": {"client": "required", "server": "required"}, + "downloads": ["https://cdn.modrinth.com/create.jar"]}, + {"path": "resourcepacks/look.zip", "hashes": {"sha1": "cd"}}]}); + let names = [ + "overrides/config/a.toml", + "overrides/config/b/c.toml", + "client-overrides/kubejs/x.js", + "overrides/", + ] + .map(String::from); + let p = mrpack(&index, &names); + assert_eq!( + ( + p.minecraft.as_str(), + p.loader.as_str(), + p.loader_version.as_str() + ), + ("1.21.1", "neoforge", "21.1.233") + ); + assert_eq!(p.files.len(), 2); + assert_eq!( + (p.files[0].kind(), p.files[0].file_name()), + ("mods", "create.jar") + ); + assert_eq!(p.files[0].sha1.as_deref(), Some("ab")); + assert_eq!(p.files[1].kind(), "resourcepacks"); + assert_eq!( + p.overrides, + BTreeMap::from([("config".into(), 2), ("kubejs".into(), 1)]) + ); + } + + #[test] + fn reads_a_curseforge_manifest() { + let manifest = json!({"name": "CF", "overrides": "overrides", + "minecraft": {"version": "1.21.1", "modLoaders": [{"id": "neoforge-21.1.259", "primary": true}]}, + "files": [{"projectID": 238222, "fileID": 5101366, "required": true}]}); + let p = curseforge(&manifest, &["overrides/kubejs/server_scripts/a.js".into()]); + assert_eq!( + (p.loader.as_str(), p.loader_version.as_str()), + ("neoforge", "21.1.259") + ); + assert_eq!(p.files[0].curseforge, Some((238222, 5101366))); + assert_eq!(p.overrides["kubejs"], 1); + } +} diff --git a/crates/horizon-port-pack/src/inventory.rs b/crates/horizon-port-pack/src/inventory.rs new file mode 100644 index 000000000..49661f1ae --- /dev/null +++ b/crates/horizon-port-pack/src/inventory.rs @@ -0,0 +1,225 @@ +//! Step 2, inventory: one record per pack file, from the jar (when the +//! user's instance has it) and Modrinth's metadata, classified and ordered. +use crate::{ + classify::{self, Facts, Known}, + import::Pack, + jar::{self, JarInfo}, + modrinth::Cache, +}; +use serde::{Deserialize, Serialize}; +use std::{ + collections::{BTreeMap, HashMap}, + path::{Path, PathBuf}, +}; + +#[derive(Debug, Default, Serialize, Deserialize)] +pub struct Record { + /// The mod id from the jar, else the Modrinth slug, else the file name. + pub key: String, + pub name: String, + pub version: String, + pub path: String, + pub class: String, + pub tier: String, + /// `known` (reviewed table), `sides`, `categories`, `jar`, `kind`, + /// `guess` or `unknown`. + pub confidence: String, + /// `modrinth`, `curseforge` or `file`. + pub source: String, + pub license: String, + pub client: String, + pub server: String, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub categories: Vec, + /// Required dependencies inside the pack, by key. + pub depends: Vec, + /// Required dependencies the pack does not have. + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub missing: Vec, + pub layer: usize, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub jar: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub info: Option, +} + +/// The jar of a pack file: the instance's own, or one the user put in +/// `/jars/`. +fn jar_of(pack_jar: Option<&PathBuf>, file_name: &str, work: &Path) -> Option { + pack_jar + .cloned() + .or_else(|| Some(work.join("jars").join(file_name))) + .filter(|p| p.is_file()) +} + +pub fn build(pack: &Pack, cache: &Cache, work: &Path) -> Vec { + let known = Known::atm10(); + let mut records = Vec::new(); + // Modrinth project id -> record key, for dependencies. + let mut by_project: HashMap = HashMap::new(); + let mut project_deps: Vec> = Vec::new(); + for f in &pack.files { + let jar = jar_of(f.jar.as_ref(), f.file_name(), work); + let info = jar.as_deref().and_then(|j| jar::read(j).ok()); + let sha1 = f.sha1.as_deref().unwrap_or(""); + let project = cache.project_of(sha1); + let p = |k: &str| project.and_then(|p| p[k].as_str()).unwrap_or("").to_owned(); + let categories: Vec = project + .into_iter() + .flat_map(|p| { + ["categories", "additional_categories"] + .iter() + .flat_map(|k| p[*k].as_array().cloned().unwrap_or_default()) + }) + .filter_map(|c| c.as_str().map(str::to_owned)) + .collect(); + let stem = f.file_name().trim_end_matches(".jar").to_owned(); + let key = info + .as_ref() + .map(|i| i.mod_id.clone()) + .filter(|k| !k.is_empty()) + .or_else(|| Some(p("slug")).filter(|s| !s.is_empty())) + .unwrap_or(stem); + let (client, server) = match project { + Some(_) if f.client.is_empty() => (p("client_side"), p("server_side")), + _ => (f.client.clone(), f.server.clone()), + }; + let census = info.as_ref().map(|i| &i.census); + let facts = Facts { + kind: f.kind(), + client: &client, + server: &server, + categories: &categories, + classes: census.map(|c| c.classes), + has_content: census.is_some_and(|c| c.blockstates + c.item_models + c.recipes > 0), + }; + let name = info + .as_ref() + .map(|i| i.name.clone()) + .filter(|n| !n.is_empty()) + .unwrap_or_else(|| p("title")); + let (class, tier, confidence) = + match known.get(&[key.as_str(), p("slug").as_str(), name.as_str()]) { + Some((c, t)) => (c.clone(), t.clone(), "known".to_owned()), + None => { + let (c, t, conf) = classify::rules(&facts); + (c.into(), t.into(), conf.into()) + } + }; + let license = info + .as_ref() + .map(|i| i.license.clone()) + .filter(|l| !l.is_empty()) + .or_else(|| project.and_then(|p| p["license"]["id"].as_str().map(str::to_owned))) + .unwrap_or_else(|| "unknown".into()); + if let Some(id) = project.and_then(|p| p["id"].as_str()) { + by_project.insert(id.to_owned(), key.clone()); + } + project_deps.push(cache.required(sha1)); + records.push(Record { + name, + version: info.as_ref().map(|i| i.version.clone()).unwrap_or_else(|| { + cache + .versions + .get(sha1) + .and_then(|v| v["version_number"].as_str()) + .unwrap_or("") + .to_owned() + }), + path: f.path.clone(), + class, + tier, + confidence, + source: if project.is_some() { + "modrinth".into() + } else if f.curseforge.is_some() { + "curseforge".into() + } else { + "file".into() + }, + license, + client, + server, + categories, + depends: info.as_ref().map(|i| i.depends.clone()).unwrap_or_default(), + jar, + info, + key, + ..Record::default() + }); + } + // Dependencies: the jar's mod ids, else Modrinth's required projects. + let keys: std::collections::HashSet = records.iter().map(|r| r.key.clone()).collect(); + for (r, deps) in records.iter_mut().zip(project_deps) { + if r.depends.is_empty() { + r.depends = deps + .iter() + .map(|p| { + by_project + .get(p) + .cloned() + .unwrap_or_else(|| format!("modrinth:{p}")) + }) + .collect(); + } + let (inside, missing): (Vec, Vec) = + r.depends.drain(..).partition(|d| keys.contains(d)); + r.depends = inside; + r.missing = missing; + } + let graph: BTreeMap> = records + .iter() + .map(|r| (r.key.clone(), r.depends.clone())) + .collect(); + let layers = classify::layers(&graph); + for r in &mut records { + r.layer = layers.get(&r.key).copied().unwrap_or(0); + } + records.sort_by(|a, b| (a.layer, &a.key).cmp(&(b.layer, &b.key))); + records +} + +/// Counts by a field, largest first. +pub fn counts<'a>( + records: &'a [Record], + field: impl Fn(&'a Record) -> &'a str, +) -> Vec<(&'a str, usize)> { + let mut m: BTreeMap<&str, usize> = BTreeMap::new(); + for r in records { + *m.entry(field(r)).or_default() += 1; + } + let mut v: Vec<_> = m.into_iter().collect(); + v.sort_by(|a, b| b.1.cmp(&a.1).then(a.0.cmp(b.0))); + v +} + +/// `inventory.csv`: the columns of `docs/modpacks/atm10-mods.csv`, plus the layer. +pub fn csv(records: &[Record]) -> String { + let field = |s: &str| { + if s.contains([',', '"', '\n']) { + format!("\"{}\"", s.replace('"', "\"\"")) + } else { + s.to_owned() + } + }; + let mut out = String::from( + "mod_id,name,version,class,tier,confidence,source,license,client,server,layer\n", + ); + for r in records { + let row = [ + &r.key, + &r.name, + &r.version, + &r.class, + &r.tier, + &r.confidence, + &r.source, + &r.license, + &r.client, + &r.server, + ]; + let cells: Vec = row.iter().map(|s| field(s)).collect(); + out.push_str(&format!("{},{}\n", cells.join(","), r.layer)); + } + out +} diff --git a/crates/horizon-port-pack/src/jar.rs b/crates/horizon-port-pack/src/jar.rs new file mode 100644 index 000000000..67028755e --- /dev/null +++ b/crates/horizon-port-pack/src/jar.rs @@ -0,0 +1,245 @@ +//! What a mod jar holds, read without decompiling: its metadata +//! (`neoforge.mods.toml`, `mods.toml`, `fabric.mod.json`) and a census of +//! its data, assets and classes. +use serde::{Deserialize, Serialize}; +use std::{fs, io::Read, path::Path}; + +#[derive(Debug, Default, Clone, Serialize, Deserialize)] +pub struct JarInfo { + pub mod_id: String, + pub name: String, + pub version: String, + pub license: String, + /// Required dependencies by mod id (loaders and `minecraft` left out). + pub depends: Vec, + pub census: Census, +} + +#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)] +pub struct Census { + pub blockstates: usize, + pub item_models: usize, + pub recipes: usize, + pub loot_tables: usize, + pub tags: usize, + pub worldgen: usize, + pub structures: usize, + pub languages: usize, + pub textures: usize, + pub sounds: usize, + pub classes: usize, + pub mixin_configs: usize, +} + +const NOT_MODS: [&str; 5] = ["minecraft", "neoforge", "forge", "fabricloader", "java"]; + +pub fn read(path: &Path) -> Result { + let file = fs::File::open(path).map_err(|e| format!("{}: {e}", path.display()))?; + let mut zip = zip::ZipArchive::new(file).map_err(|e| format!("{}: {e}", path.display()))?; + let text = |zip: &mut zip::ZipArchive, name: &str| -> Option { + let mut s = String::new(); + zip.by_name(name).ok()?.read_to_string(&mut s).ok()?; + Some(s) + }; + let mut info = if let Some(t) = text(&mut zip, "META-INF/neoforge.mods.toml") + .or_else(|| text(&mut zip, "META-INF/mods.toml")) + { + mods_toml(&t)? + } else if let Some(t) = text(&mut zip, "fabric.mod.json") { + fabric(&t)? + } else { + JarInfo::default() + }; + let names: Vec = zip.file_names().map(str::to_owned).collect(); + info.census = census(&names); + if info.mod_id.is_empty() { + info.mod_id = path + .file_stem() + .map(|s| s.to_string_lossy().into_owned()) + .unwrap_or_default(); + } + Ok(info) +} + +/// `[[mods]]` (the first one is the jar's mod), the top-level `license`, and +/// `[[dependencies.]]` with `type = "required"` (or the older +/// `mandatory = true`). +pub fn mods_toml(text: &str) -> Result { + let doc: toml::Table = toml::from_str(text).map_err(|e| format!("mods.toml: {e}"))?; + let first = doc + .get("mods") + .and_then(|m| m.as_array()) + .and_then(|m| m.first()) + .and_then(|m| m.as_table()); + let get = |t: Option<&toml::Table>, k: &str| { + t.and_then(|t| t.get(k)) + .and_then(|v| v.as_str()) + .unwrap_or("") + .to_owned() + }; + let mod_id = get(first, "modId"); + let mut depends = Vec::new(); + if let Some(deps) = doc + .get("dependencies") + .and_then(|d| d.get(&mod_id)) + .and_then(|d| d.as_array()) + { + for d in deps.iter().filter_map(|d| d.as_table()) { + let required = d.get("type").and_then(|t| t.as_str()).map_or_else( + || d.get("mandatory").and_then(|m| m.as_bool()) == Some(true), + |t| t.eq_ignore_ascii_case("required"), + ); + let id = get(Some(d), "modId"); + if required && !NOT_MODS.contains(&id.as_str()) && !id.is_empty() { + depends.push(id); + } + } + } + Ok(JarInfo { + name: get(first, "displayName"), + version: get(first, "version"), + license: get(Some(&doc), "license"), + mod_id, + depends, + census: Census::default(), + }) +} + +pub fn fabric(text: &str) -> Result { + let v: serde_json::Value = + serde_json::from_str(text).map_err(|e| format!("fabric.mod.json: {e}"))?; + let s = |k: &str| v[k].as_str().unwrap_or("").to_owned(); + let license = match &v["license"] { + serde_json::Value::Array(l) => l + .iter() + .filter_map(|x| x.as_str()) + .collect::>() + .join(" OR "), + l => l.as_str().unwrap_or("").to_owned(), + }; + let depends = v["depends"] + .as_object() + .into_iter() + .flatten() + .map(|(id, _)| id.clone()) + .filter(|id| !NOT_MODS.contains(&id.as_str()) && !id.starts_with("fabric")) + .collect(); + Ok(JarInfo { + mod_id: s("id"), + name: s("name"), + version: s("version"), + license, + depends, + census: Census::default(), + }) +} + +pub fn census(names: &[String]) -> Census { + let mut c = Census::default(); + for n in names { + let parts: Vec<&str> = n.split('/').collect(); + if n.ends_with(".class") { + c.classes += 1; + } else if n.ends_with(".mixins.json") || (parts.len() == 1 && n.contains("mixins")) { + c.mixin_configs += 1; + } + // assets///... and data///... + let (Some(root), Some(kind)) = (parts.first(), parts.get(2)) else { + continue; + }; + let json = n.ends_with(".json"); + match (*root, *kind) { + ("assets", "blockstates") if json => c.blockstates += 1, + ("assets", "models") if json && parts.get(3) == Some(&"item") => c.item_models += 1, + ("assets", "lang") => c.languages += 1, + ("assets", "textures") if n.ends_with(".png") => c.textures += 1, + ("assets", "sounds.json") => c.sounds += 1, + ("data", "recipe" | "recipes") if json => c.recipes += 1, + ("data", "loot_table" | "loot_tables") if json => c.loot_tables += 1, + ("data", "tags") if json => c.tags += 1, + ("data", "worldgen") if json => c.worldgen += 1, + ("data", "structure" | "structures") if n.ends_with(".nbt") => c.structures += 1, + _ => {} + } + } + c +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn reads_neoforge_metadata() { + let info = mods_toml( + r#"modLoader = "javafml" +license = "MIT" +[[mods]] +modId = "ruby" +version = "1.2.0" +displayName = "Ruby" +[[dependencies.ruby]] +modId = "neoforge" +type = "required" +[[dependencies.ruby]] +modId = "geckolib" +type = "required" +[[dependencies.ruby]] +modId = "jei" +type = "optional" +[[dependencies.ruby]] +modId = "curios" +mandatory = true +"#, + ) + .unwrap(); + assert_eq!( + ( + info.mod_id.as_str(), + info.version.as_str(), + info.license.as_str() + ), + ("ruby", "1.2.0", "MIT") + ); + assert_eq!(info.depends, ["geckolib", "curios"]); + } + + #[test] + fn reads_fabric_metadata() { + let info = fabric( + r#"{"id": "gem", "version": "3", "name": "Gem", "license": ["MIT", "Apache-2.0"], + "depends": {"fabricloader": "*", "fabric-api": "*", "minecraft": "1.21.1", "cloth-config": "*"}}"#, + ) + .unwrap(); + assert_eq!(info.license, "MIT OR Apache-2.0"); + assert_eq!(info.depends, ["cloth-config"]); + } + + #[test] + fn counts_files_by_kind() { + let names = [ + "assets/ruby/blockstates/ruby_block.json", + "assets/ruby/models/item/ruby.json", + "assets/ruby/models/block/ruby_block.json", + "assets/ruby/textures/item/ruby.png", + "assets/ruby/lang/en_us.json", + "data/ruby/recipe/ruby.json", + "data/ruby/loot_table/blocks/ruby_block.json", + "data/minecraft/tags/block/mineable/pickaxe.json", + "data/ruby/structure/hut.nbt", + "com/example/Ruby.class", + "ruby.mixins.json", + ] + .map(String::from); + let c = census(&names); + assert_eq!( + (c.blockstates, c.item_models, c.textures, c.languages), + (1, 1, 1, 1) + ); + assert_eq!( + (c.recipes, c.loot_tables, c.tags, c.structures), + (1, 1, 1, 1) + ); + assert_eq!((c.classes, c.mixin_configs), (1, 1)); + } +} diff --git a/crates/horizon-port-pack/src/main.rs b/crates/horizon-port-pack/src/main.rs new file mode 100644 index 000000000..200e42334 --- /dev/null +++ b/crates/horizon-port-pack/src/main.rs @@ -0,0 +1,104 @@ +//! `horizon-port-pack`: port a Java modpack to Horizon (`docs/modpacks.md`). +//! +//! - `import `: a `.mrpack`, CurseForge zip or instance folder +//! to `/pack.json`. +//! - `inventory [--offline]`: Modrinth metadata (cached in +//! `/modrinth.json`), the jars in the instance or `/jars/`, +//! classes, tiers and dependency layers in `/inventory.json` and +//! `inventory.csv`. +//! +//! The work folder belongs to the user: jars stay there and are never +//! uploaded or committed. +mod classify; +mod import; +mod inventory; +mod jar; +mod modrinth; + +use std::{fs, path::Path, process::ExitCode}; + +const USAGE: &str = "usage: horizon-port-pack import | inventory [--offline]"; + +fn main() -> ExitCode { + let args: Vec = std::env::args().skip(1).collect(); + let path = |i: usize| args.get(i).map(Path::new).ok_or(USAGE.to_owned()); + let result = match args.first().map(String::as_str) { + Some("import") => path(1) + .and_then(|p| Ok((p, path(2)?))) + .and_then(|(p, w)| import(p, w)), + Some("inventory") => { + path(1).and_then(|w| inventory(w, args.iter().any(|a| a == "--offline"))) + } + _ => Err(USAGE.to_owned()), + }; + match result { + Ok(()) => ExitCode::SUCCESS, + Err(e) => { + eprintln!("error: {e}"); + ExitCode::FAILURE + } + } +} + +fn write_json(path: &Path, value: &impl serde::Serialize) -> Result<(), String> { + let text = serde_json::to_string_pretty(value).map_err(|e| e.to_string())?; + fs::write(path, text).map_err(|e| format!("{}: {e}", path.display())) +} + +fn read_json(path: &Path) -> Result { + let text = fs::read_to_string(path).map_err(|e| format!("{}: {e}", path.display()))?; + serde_json::from_str(&text).map_err(|e| format!("{}: {e}", path.display())) +} + +fn import(pack: &Path, work: &Path) -> Result<(), String> { + let pack = import::import(pack)?; + fs::create_dir_all(work).map_err(|e| format!("{}: {e}", work.display()))?; + write_json(&work.join("pack.json"), &pack)?; + let mut kinds = std::collections::BTreeMap::<&str, usize>::new(); + for f in &pack.files { + *kinds.entry(f.kind()).or_default() += 1; + } + println!( + "{} ({}): Minecraft {}, {} {}; files {kinds:?}; overrides {:?}", + pack.name, pack.format, pack.minecraft, pack.loader, pack.loader_version, pack.overrides + ); + Ok(()) +} + +fn inventory(work: &Path, offline: bool) -> Result<(), String> { + let pack: import::Pack = read_json(&work.join("pack.json"))?; + let cache_path = work.join("modrinth.json"); + let mut cache = modrinth::Cache::load(&cache_path); + if !offline { + let hashes: Vec = pack.files.iter().filter_map(|f| f.sha1.clone()).collect(); + cache.fetch(&hashes)?; + write_json(&cache_path, &cache)?; + } + let records = inventory::build(&pack, &cache, work); + write_json(&work.join("inventory.json"), &records)?; + fs::write(work.join("inventory.csv"), inventory::csv(&records)) + .map_err(|e| format!("inventory.csv: {e}"))?; + let jars = records.iter().filter(|r| r.jar.is_some()).count(); + println!("{} files, {jars} jars read", records.len()); + for (title, field) in [ + ( + "class", + (|r: &inventory::Record| r.class.as_str()) as fn(&inventory::Record) -> &str, + ), + ("tier", |r| r.tier.as_str()), + ("confidence", |r| r.confidence.as_str()), + ("source", |r| r.source.as_str()), + ] { + println!("{title}: {:?}", inventory::counts(&records, field)); + } + let layers = records.iter().map(|r| r.layer).max().map_or(0, |m| m + 1); + let per_layer: Vec = (0..layers) + .map(|l| records.iter().filter(|r| r.layer == l).count()) + .collect(); + println!("layers: {per_layer:?}"); + let missing: usize = records.iter().map(|r| r.missing.len()).sum(); + if missing > 0 { + println!("{missing} required dependencies are not in the pack (see inventory.json)"); + } + Ok(()) +} diff --git a/crates/horizon-port-pack/src/modrinth.rs b/crates/horizon-port-pack/src/modrinth.rs new file mode 100644 index 000000000..8c34a2004 --- /dev/null +++ b/crates/horizon-port-pack/src/modrinth.rs @@ -0,0 +1,113 @@ +//! Modrinth metadata for the pack's files: the version of each file by +//! SHA-1, and its project (categories, licence, sides). Only metadata: no +//! jar is downloaded. Answers are cached in `/modrinth.json`, so a +//! rerun works offline. +use serde::{Deserialize, Serialize}; +use serde_json::Value; +use std::{collections::BTreeMap, path::Path}; + +const API: &str = "https://api.modrinth.com/v2"; +const AGENT: &str = "ludovic111/horizon horizon-port-pack/0.1 (docs/modpacks.md)"; + +#[derive(Debug, Default, Serialize, Deserialize)] +pub struct Cache { + /// Version objects by file SHA-1. + pub versions: BTreeMap, + /// Project objects by project id. + pub projects: BTreeMap, +} + +impl Cache { + pub fn load(path: &Path) -> Self { + std::fs::read_to_string(path) + .ok() + .and_then(|t| serde_json::from_str(&t).ok()) + .unwrap_or_default() + } + + /// Ask Modrinth about the hashes and projects not cached yet. + pub fn fetch(&mut self, hashes: &[String]) -> Result<(), String> { + let missing: Vec<&String> = hashes + .iter() + .filter(|h| !self.versions.contains_key(*h)) + .collect(); + for chunk in missing.chunks(500) { + let body = serde_json::json!({"hashes": chunk, "algorithm": "sha1"}); + let found = post(&format!("{API}/version_files"), &body)?; + for (hash, version) in found.as_object().into_iter().flatten() { + self.versions.insert(hash.clone(), version.clone()); + } + } + let mut ids: Vec = self + .versions + .values() + .filter_map(|v| v["project_id"].as_str().map(str::to_owned)) + .filter(|id| !self.projects.contains_key(id)) + .collect(); + ids.sort(); + ids.dedup(); + for chunk in ids.chunks(100) { + let list = serde_json::to_string(chunk).unwrap_or_default(); + let found = get(&format!("{API}/projects?ids={}", encode(&list)))?; + for project in found.as_array().into_iter().flatten() { + if let Some(id) = project["id"].as_str() { + self.projects.insert(id.to_owned(), project.clone()); + } + } + } + Ok(()) + } + + pub fn project_of(&self, sha1: &str) -> Option<&Value> { + let id = self.versions.get(sha1)?["project_id"].as_str()?; + self.projects.get(id) + } + + /// Required dependencies of a file's version, as project ids. + pub fn required(&self, sha1: &str) -> Vec { + let Some(v) = self.versions.get(sha1) else { + return Vec::new(); + }; + v["dependencies"] + .as_array() + .into_iter() + .flatten() + .filter(|d| d["dependency_type"] == "required") + .filter_map(|d| d["project_id"].as_str().map(str::to_owned)) + .collect() + } +} + +fn post(url: &str, body: &Value) -> Result { + let response = ureq::post(url) + .set("User-Agent", AGENT) + .set("Content-Type", "application/json") + .send_string(&body.to_string()) + .map_err(|e| format!("{url}: {e}"))?; + read(url, response) +} + +fn get(url: &str) -> Result { + let response = ureq::get(url) + .set("User-Agent", AGENT) + .call() + .map_err(|e| format!("{url}: {e}"))?; + read(url, response) +} + +fn read(url: &str, response: ureq::Response) -> Result { + let text = response.into_string().map_err(|e| format!("{url}: {e}"))?; + serde_json::from_str(&text).map_err(|e| format!("{url}: {e}")) +} + +/// Percent-encoding for a query value. +fn encode(s: &str) -> String { + s.bytes() + .map(|b| match b { + b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => { + (b as char).to_string() + } + _ => format!("%{b:02X}"), + }) + .collect() +} diff --git a/docs/modpacks.md b/docs/modpacks.md index 54dc43bdc..753299fda 100644 --- a/docs/modpacks.md +++ b/docs/modpacks.md @@ -262,6 +262,20 @@ inspected: └── coverage.json, coverage.md ``` +**Status.** `import` and `inventory` run today, `carry` follows (see below): + +``` +cargo run -p horizon-port-pack -- import ~/Downloads/pack.mrpack ~/.horizon/ports/pack +cargo run -p horizon-port-pack -- inventory ~/.horizon/ports/pack # --offline reuses modrinth.json +``` + +`inventory` reads metadata only: Modrinth answers by file hash, and the jars only if the user's +instance has them (an imported instance folder, or jars put in `/jars/`). Without jars, +classes come from the reviewed table, Modrinth categories and sides; with jars, from their +`mods.toml` / `fabric.mod.json` and file census too. On Create+ 6.0.0 alpha f (155 files, metadata +only): content 54, library 33, client-ui 26, performance 25, other 14, unknown 2, server-only 1; +40 rows matched the reviewed ATM10 table; three dependency layers (93 / 57 / 5). + ### 1. Import | Format | Read | Jars come from | From 673ac847243b44b515b7effdfe93bc730bfd97c4 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 16 Oct 2026 19:25:28 +0200 Subject: [PATCH 08/12] horizon-port-pack carry: a jar's data and assets as a content mod carry [...] turns each content mod with a jar into a folder content mod (API 0.2) in /mods//carry/, for local use: - assets//, data// and data/minecraft/tags/ are copied; other namespaces and vanilla data go to outside/ for review; no class file is copied. - Vanilla-type recipes get 26.3 ingredients ("id", "#tag", lists); custom types and NeoForge conditions are kept and listed. - Item models get the items/ client definitions 1.21.4 introduced. - Each block is declared from a vanilla template guessed from its blockstate properties; unknown property sets are listed. Items without a block are declared from minecraft:stick. The test builds a small NeoForge jar, carries it over, and reads the result with horizon-mods' own Content reader. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 1 + crates/horizon-port-pack/Cargo.toml | 3 + crates/horizon-port-pack/src/carry.rs | 440 ++++++++++++++++++++++++++ crates/horizon-port-pack/src/main.rs | 54 +++- docs/modpacks.md | 12 +- 5 files changed, 506 insertions(+), 4 deletions(-) create mode 100644 crates/horizon-port-pack/src/carry.rs diff --git a/Cargo.lock b/Cargo.lock index 9c3ad04ab..6ebc69964 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1502,6 +1502,7 @@ dependencies = [ name = "horizon-port-pack" version = "0.1.0" dependencies = [ + "horizon-mods", "serde", "serde_json", "sha1", diff --git a/crates/horizon-port-pack/Cargo.toml b/crates/horizon-port-pack/Cargo.toml index 1b33b6c01..3b903ad03 100644 --- a/crates/horizon-port-pack/Cargo.toml +++ b/crates/horizon-port-pack/Cargo.toml @@ -14,5 +14,8 @@ toml = "1.1.6" ureq = { version = "2", default-features = false, features = ["tls"] } zip = { version = "2", default-features = false, features = ["deflate"] } +[dev-dependencies] +horizon-mods = { path = "../horizon-mods", default-features = false } + [lints] workspace = true diff --git a/crates/horizon-port-pack/src/carry.rs b/crates/horizon-port-pack/src/carry.rs new file mode 100644 index 000000000..42c457f02 --- /dev/null +++ b/crates/horizon-port-pack/src/carry.rs @@ -0,0 +1,440 @@ +//! Step 4, carry-over: a jar's `assets/` and `data/` into a folder content +//! mod (API 0.2) for local use. Files are copied, never code; recipes are +//! upgraded from 1.21.1 to 26.3, item models get their `items/` client +//! definitions, and each blockstate gets a vanilla template guessed from its +//! properties until the oracle's facts replace the guess. +use crate::jar::JarInfo; +use serde::Serialize; +use serde_json::{Value, json}; +use std::{ + collections::{BTreeMap, BTreeSet}, + fs, + io::Read, + path::Path, +}; + +#[derive(Debug, Default, Serialize)] +pub struct Report { + pub copied: usize, + /// Files outside what a content mod may hold (other namespaces, vanilla + /// data other than tags), kept in `outside/` for review. + pub outside: Vec, + pub recipes_upgraded: usize, + /// Recipes left as they were: custom types, NeoForge conditions. + pub recipes_kept: Vec, + pub item_definitions_added: usize, + /// Declared blocks and their guessed templates. + pub blocks: BTreeMap, + /// Blockstates whose properties match no template. + pub blocks_unknown: Vec, + pub items: usize, +} + +/// Vanilla blocks by their sorted property names. +const TEMPLATES: &[(&str, &str)] = &[ + ("", "minecraft:stone"), + ("axis", "minecraft:oak_log"), + ("type,waterlogged", "minecraft:oak_slab"), + ("facing,half,shape,waterlogged", "minecraft:oak_stairs"), + ("facing,half,hinge,open,powered", "minecraft:oak_door"), + ( + "facing,half,open,powered,waterlogged", + "minecraft:oak_trapdoor", + ), + ("east,north,south,waterlogged,west", "minecraft:oak_fence"), + ( + "east,north,south,up,waterlogged,west", + "minecraft:cobblestone_wall", + ), + ("facing,in_wall,open,powered", "minecraft:oak_fence_gate"), + ("face,facing,powered", "minecraft:oak_button"), + ("powered", "minecraft:oak_pressure_plate"), + ("facing", "minecraft:carved_pumpkin"), + ("facing,lit", "minecraft:furnace"), + ("lit", "minecraft:redstone_lamp"), + ("waterlogged", "minecraft:chain"), + ("age", "minecraft:wheat"), + ("distance,persistent,waterlogged", "minecraft:oak_leaves"), + ("stage", "minecraft:oak_sapling"), +]; + +pub fn template(properties: &BTreeSet) -> Option<&'static str> { + let key = properties.iter().cloned().collect::>().join(","); + TEMPLATES.iter().find(|(k, _)| *k == key).map(|(_, t)| *t) +} + +/// The property names a blockstate file uses. +pub fn properties(blockstate: &Value) -> BTreeSet { + let mut out = BTreeSet::new(); + let mut add = |pairs: &str| { + for pair in pairs.split(',').filter(|p| !p.is_empty()) { + out.insert(pair.split('=').next().unwrap_or(pair).to_owned()); + } + }; + for key in blockstate["variants"] + .as_object() + .into_iter() + .flatten() + .map(|(k, _)| k) + { + add(key); + } + for part in blockstate["multipart"].as_array().into_iter().flatten() { + let when = &part["when"]; + let mut conditions: Vec<&Value> = vec![when]; + for or in ["OR", "AND"] { + conditions.extend(when[or].as_array().into_iter().flatten()); + } + for c in conditions { + for (k, _) in c.as_object().into_iter().flatten() { + if k != "OR" && k != "AND" { + out.insert(k.clone()); + } + } + } + } + out +} + +/// A 1.21.1 ingredient (`{"item": id}`, `{"tag": t}` or a list of those) as +/// 26.3's (`"id"`, `"#t"`, `["id", ...]`); `None` for what has no 26.3 form +/// (custom ingredient types, tags inside lists). +pub fn ingredient(v: &Value) -> Option { + match v { + Value::String(_) => Some(v.clone()), + Value::Object(o) if o.len() == 1 => { + if let Some(Value::String(id)) = o.get("item") { + Some(json!(id)) + } else if let Some(Value::String(tag)) = o.get("tag") { + Some(json!(format!("#{tag}"))) + } else { + None + } + } + Value::Array(list) => { + let ids: Option> = list + .iter() + .map(|x| { + x.get("item") + .filter(|_| x.as_object().is_some_and(|o| o.len() == 1)) + .cloned() + }) + .collect(); + match ids? { + ids if ids.len() == 1 => ids.into_iter().next(), + ids => Some(Value::Array(ids)), + } + } + _ => None, + } +} + +/// A vanilla recipe upgraded in place; `false` (unchanged) when it is not +/// a vanilla type or one of its ingredients has no 26.3 form. +pub fn upgrade_recipe(recipe: &mut Value) -> bool { + let kind = recipe["type"].as_str().unwrap_or(""); + if !kind.starts_with("minecraft:") || recipe.get("neoforge:conditions").is_some() { + return false; + } + let mut out = recipe.clone(); + if let Some(key) = out.get_mut("key").and_then(Value::as_object_mut) { + for v in key.values_mut() { + let Some(new) = ingredient(v) else { + return false; + }; + *v = new; + } + } + if let Some(list) = out.get_mut("ingredients").and_then(Value::as_array_mut) { + for v in list.iter_mut() { + let Some(new) = ingredient(v) else { + return false; + }; + *v = new; + } + } + for field in ["ingredient", "base", "addition", "template"] { + if let Some(v) = out.get_mut(field) { + let Some(new) = ingredient(v) else { + return false; + }; + *v = new; + } + } + *recipe = out; + true +} + +/// Where a jar entry goes: `Some(true)` inside the content mod, `Some(false)` +/// to `outside/`, `None` not carried (code, metadata). +fn place(name: &str, id: &str) -> Option { + let mut parts = name.splitn(3, '/'); + let (root, ns, rest) = (parts.next()?, parts.next()?, parts.next()?); + if !(root == "assets" || root == "data") || rest.is_empty() || name.ends_with('/') { + return None; + } + Some(ns == id || (root == "data" && ns == "minecraft" && rest.starts_with("tags/"))) +} + +pub fn carry(jar: &Path, info: &JarInfo, out: &Path) -> Result { + let id = &info.mod_id; + if id.is_empty() + || id.len() > 64 + || !id + .bytes() + .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'_') + { + return Err(format!( + "mod id {id:?} is not [a-z0-9_]{{1,64}}: rename it before carrying over" + )); + } + 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()))?; + let (inside, outside) = (out.join("carry"), out.join("outside")); + let _ = fs::remove_dir_all(&inside); + let _ = fs::remove_dir_all(&outside); + let mut report = Report::default(); + let mut blockstates: BTreeMap = BTreeMap::new(); + let mut item_models = BTreeSet::new(); + for i in 0..zip.len() { + let mut entry = zip.by_index(i).map_err(|e| e.to_string())?; + let name = entry.name().to_owned(); + let Some(keep) = place(&name, id) else { + continue; + }; + if name.split('/').any(|p| p == ".." || p.is_empty()) { + continue; + } + let mut bytes = Vec::new(); + entry + .read_to_end(&mut bytes) + .map_err(|e| format!("{name}: {e}"))?; + let dest = if keep { + report.copied += 1; + inside.join(&name) + } else { + report.outside.push(name.clone()); + outside.join(&name) + }; + if keep { + let rel = name.split_once(&format!("/{id}/")).map_or("", |(_, r)| r); + if let Some(file) = rel + .strip_prefix("recipe/") + .filter(|_| name.starts_with("data/")) + { + if let Ok(mut recipe) = serde_json::from_slice::(&bytes) { + if upgrade_recipe(&mut recipe) { + report.recipes_upgraded += 1; + bytes = serde_json::to_vec_pretty(&recipe).unwrap_or(bytes); + } else { + report.recipes_kept.push(file.to_owned()); + } + } + } else if let Some(b) = rel + .strip_prefix("blockstates/") + .and_then(|b| b.strip_suffix(".json")) + { + if let Ok(v) = serde_json::from_slice(&bytes) { + blockstates.insert(b.to_owned(), v); + } + } else if let Some(m) = rel + .strip_prefix("models/item/") + .and_then(|m| m.strip_suffix(".json")) + { + item_models.insert(m.to_owned()); + } + } + write(&dest, &bytes)?; + } + // 1.21.4 moved item models behind `items/.json` definitions. + for m in &item_models { + let def = inside.join(format!("assets/{id}/items/{m}.json")); + if !def.exists() { + let model = + json!({"model": {"type": "minecraft:model", "model": format!("{id}:item/{m}")}}); + write(&def, &serde_json::to_vec_pretty(&model).unwrap_or_default())?; + report.item_definitions_added += 1; + } + } + let decl = inside.join(format!("data/{id}/horizon")); + for (b, state) in &blockstates { + match template(&properties(state)) { + Some(t) => { + write( + &decl.join(format!("block/{b}.json")), + json!({"template": t}).to_string().as_bytes(), + )?; + report.blocks.insert(b.clone(), t.to_owned()); + } + None => report.blocks_unknown.push(b.clone()), + } + } + for m in item_models.iter().filter(|m| !blockstates.contains_key(*m)) { + let item = json!({"template": "minecraft:stick"}); + write( + &decl.join(format!("item/{m}.json")), + item.to_string().as_bytes(), + )?; + report.items += 1; + } + let q = |s: &str| toml::Value::String(s.to_owned()).to_string(); + let name = if info.name.is_empty() { id } else { &info.name }; + let manifest = format!( + "# Carried over from {jar} by horizon-port-pack, for local use only.\n# Licence: {license}\nid = {}\nname = {}\nversion = {}\napi = \"0.2\"\nside = \"both\"\ncapabilities = [\"registries\"]\n", + q(id), + q(name), + q(&info.version), + jar = jar.file_name().unwrap_or_default().to_string_lossy(), + license = if info.license.is_empty() { + "not stated" + } else { + &info.license + }, + ); + write(&inside.join("horizon-mod.toml"), manifest.as_bytes())?; + write( + &out.join("carry-report.json"), + &serde_json::to_vec_pretty(&report).unwrap_or_default(), + )?; + Ok(report) +} + +fn write(path: &Path, bytes: &[u8]) -> Result<(), String> { + if let Some(dir) = path.parent() { + fs::create_dir_all(dir).map_err(|e| format!("{}: {e}", dir.display()))?; + } + fs::write(path, bytes).map_err(|e| format!("{}: {e}", path.display())) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::io::Write; + + #[test] + fn upgrades_ingredients() { + let mut r = json!({"type": "minecraft:crafting_shaped", "pattern": ["##"], + "key": {"#": {"item": "gem:gem"}, "s": {"tag": "c:rods/wooden"}}, + "result": {"id": "gem:gem_block", "count": 1}}); + assert!(upgrade_recipe(&mut r)); + assert_eq!(r["key"], json!({"#": "gem:gem", "s": "#c:rods/wooden"})); + let mut custom = json!({"type": "gem:infuse", "ingredient": {"item": "gem:gem"}}); + assert!(!upgrade_recipe(&mut custom)); + let mut tag_list = json!({"type": "minecraft:smelting", + "ingredient": [{"tag": "c:ores"}, {"item": "gem:gem"}]}); + assert!(!upgrade_recipe(&mut tag_list)); + } + + #[test] + fn templates_from_blockstate_properties() { + let door = + json!({"variants": {"facing=east,half=lower,hinge=left,open=false,powered=false": {}}}); + assert_eq!(template(&properties(&door)), Some("minecraft:oak_door")); + let fence = json!({"multipart": [{"apply": {}}, {"when": {"north": "true"}, "apply": {}}, + {"when": {"east": "true"}}, {"when": {"south": "true"}}, {"when": {"west": "true"}}, + {"when": {"OR": [{"waterlogged": "true"}]}}]}); + assert_eq!(template(&properties(&fence)), Some("minecraft:oak_fence")); + assert_eq!( + template(&properties(&json!({"variants": {"": {}}}))), + Some("minecraft:stone") + ); + let odd = json!({"variants": {"charge=1,mode=fast": {}}}); + assert_eq!(template(&properties(&odd)), None); + } + + /// A small NeoForge jar carried over is a content mod Horizon loads. + #[test] + fn carried_jar_is_a_content_mod() { + let dir = std::env::temp_dir().join(format!("horizon-port-pack-{}", std::process::id())); + let _ = fs::remove_dir_all(&dir); + fs::create_dir_all(&dir).unwrap(); + let jar = dir.join("gem-1.0.jar"); + let mut zip = zip::ZipWriter::new(fs::File::create(&jar).unwrap()); + let files: &[(&str, &str)] = &[ + ( + "META-INF/neoforge.mods.toml", + "license = \"MIT\"\n[[mods]]\nmodId = \"gem\"\nversion = \"1.0\"\ndisplayName = \"Gem\"\n", + ), + ("com/example/gem/Gem.class", "not real bytecode"), + ( + "assets/gem/blockstates/gem_block.json", + r#"{"variants": {"": {"model": "gem:block/gem_block"}}}"#, + ), + ( + "assets/gem/blockstates/gem_door.json", + r#"{"variants": {"facing=east,half=lower,hinge=left,open=false,powered=false": {"model": "gem:block/gem_door"}}}"#, + ), + ( + "assets/gem/blockstates/gem_core.json", + r#"{"variants": {"charge=1": {"model": "gem:block/gem_core"}}}"#, + ), + ( + "assets/gem/models/item/gem.json", + r#"{"parent": "minecraft:item/generated", "textures": {"layer0": "gem:item/gem"}}"#, + ), + ( + "assets/gem/models/item/gem_block.json", + r#"{"parent": "gem:block/gem_block"}"#, + ), + ("assets/gem/textures/item/gem.png", "png"), + ("assets/gem/lang/en_us.json", r#"{"item.gem.gem": "Gem"}"#), + ( + "data/gem/recipe/gem_block.json", + r####"{"type": "minecraft:crafting_shaped", "pattern": ["###", "###", "###"], + "key": {"#": {"item": "gem:gem"}}, "result": {"id": "gem:gem_block", "count": 1}}"####, + ), + ( + "data/gem/recipe/infuse.json", + r#"{"type": "gem:infuse", "ingredient": {"item": "gem:gem"}}"#, + ), + ( + "data/gem/loot_table/blocks/gem_block.json", + r#"{"type": "minecraft:block", "pools": []}"#, + ), + ( + "data/minecraft/tags/block/mineable/pickaxe.json", + r#"{"values": ["gem:gem_block"]}"#, + ), + ("data/c/tags/item/gems.json", r#"{"values": ["gem:gem"]}"#), + ]; + for (name, text) in files { + zip.start_file(*name, zip::write::SimpleFileOptions::default()) + .unwrap(); + zip.write_all(text.as_bytes()).unwrap(); + } + zip.finish().unwrap(); + + let info = crate::jar::read(&jar).unwrap(); + assert_eq!( + (info.mod_id.as_str(), info.license.as_str()), + ("gem", "MIT") + ); + assert_eq!(info.census.classes, 1); + let out = dir.join("work/mods/gem"); + let report = carry(&jar, &info, &out).unwrap(); + assert_eq!(report.copied, 11); + assert_eq!(report.outside, ["data/c/tags/item/gems.json"]); + assert_eq!((report.recipes_upgraded, report.recipes_kept.len()), (1, 1)); + assert_eq!(report.item_definitions_added, 2); + assert_eq!(report.blocks["gem_door"], "minecraft:oak_door"); + assert_eq!(report.blocks["gem_block"], "minecraft:stone"); + assert_eq!(report.blocks_unknown, ["gem_core"]); + assert_eq!(report.items, 1); + let carried = out.join("carry"); + assert!(!carried.join("com").exists()); + let recipe: Value = serde_json::from_slice( + &fs::read(carried.join("data/gem/recipe/gem_block.json")).unwrap(), + ) + .unwrap(); + assert_eq!(recipe["key"]["#"], "gem:gem"); + + // Horizon's own reader accepts it: blocks and items declared, files fenced. + let package = horizon_mods::Package::read(&carried).unwrap(); + let content = horizon_mods::Content::read(&carried, &package.manifest).unwrap(); + let blocks: Vec<&str> = content.blocks.iter().map(|b| b.id.as_str()).collect(); + assert_eq!(blocks, ["gem:gem_block", "gem:gem_door"]); + assert_eq!(content.items.len(), 1); + assert_eq!(content.items[0].id, "gem:gem"); + fs::remove_dir_all(&dir).unwrap(); + } +} diff --git a/crates/horizon-port-pack/src/main.rs b/crates/horizon-port-pack/src/main.rs index 200e42334..4df3ef3ca 100644 --- a/crates/horizon-port-pack/src/main.rs +++ b/crates/horizon-port-pack/src/main.rs @@ -6,9 +6,12 @@ //! `/modrinth.json`), the jars in the instance or `/jars/`, //! classes, tiers and dependency layers in `/inventory.json` and //! `inventory.csv`. +//! - `carry [...]`: the content mods' data and assets into +//! `/mods//carry/`, a folder mod for local use. //! -//! The work folder belongs to the user: jars stay there and are never -//! uploaded or committed. +//! The work folder belongs to the user: jars and carried-over files stay +//! there and are never uploaded or committed. +mod carry; mod classify; mod import; mod inventory; @@ -17,7 +20,7 @@ mod modrinth; use std::{fs, path::Path, process::ExitCode}; -const USAGE: &str = "usage: horizon-port-pack import | inventory [--offline]"; +const USAGE: &str = "usage: horizon-port-pack import | inventory [--offline] | carry [...]"; fn main() -> ExitCode { let args: Vec = std::env::args().skip(1).collect(); @@ -29,6 +32,7 @@ fn main() -> ExitCode { Some("inventory") => { path(1).and_then(|w| inventory(w, args.iter().any(|a| a == "--offline"))) } + Some("carry") => path(1).and_then(|w| carry(w, &args[2..])), _ => Err(USAGE.to_owned()), }; match result { @@ -102,3 +106,47 @@ fn inventory(work: &Path, offline: bool) -> Result<(), String> { } Ok(()) } + +fn carry(work: &Path, only: &[String]) -> Result<(), String> { + let records: Vec = read_json(&work.join("inventory.json"))?; + let mut done = 0; + for r in &records { + let chosen = if only.is_empty() { + r.class == "content" + } else { + only.contains(&r.key) + }; + let (Some(jar), Some(info)) = (&r.jar, &r.info) else { + if chosen && !only.is_empty() { + eprintln!("{}: no jar (put it in {}/jars/)", r.key, work.display()); + } + continue; + }; + if !chosen { + continue; + } + match carry::carry(jar, info, &work.join("mods").join(&r.key)) { + Ok(report) => { + done += 1; + println!( + "{}: {} files, {} recipes upgraded ({} kept), {} item definitions added, {} blocks ({} without a template), {} items, {} outside", + r.key, + report.copied, + report.recipes_upgraded, + report.recipes_kept.len(), + report.item_definitions_added, + report.blocks.len(), + report.blocks_unknown.len(), + report.items, + report.outside.len() + ); + } + Err(e) => eprintln!("{}: {e}", r.key), + } + } + println!( + "{done} mods carried over into {}", + work.join("mods").display() + ); + Ok(()) +} diff --git a/docs/modpacks.md b/docs/modpacks.md index 753299fda..7951ad4d4 100644 --- a/docs/modpacks.md +++ b/docs/modpacks.md @@ -262,11 +262,12 @@ inspected: └── coverage.json, coverage.md ``` -**Status.** `import` and `inventory` run today, `carry` follows (see below): +**Status.** `import`, `inventory` and `carry` run today: ``` cargo run -p horizon-port-pack -- import ~/Downloads/pack.mrpack ~/.horizon/ports/pack cargo run -p horizon-port-pack -- inventory ~/.horizon/ports/pack # --offline reuses modrinth.json +cargo run -p horizon-port-pack -- carry ~/.horizon/ports/pack # content mods with a jar, or name them ``` `inventory` reads metadata only: Modrinth answers by file hash, and the jars only if the user's @@ -276,6 +277,15 @@ classes come from the reviewed table, Modrinth categories and sides; with jars, only): content 54, library 33, client-ui 26, performance 25, other 14, unknown 2, server-only 1; 40 rows matched the reviewed ATM10 table; three dependency layers (93 / 57 / 5). +`carry` copies a jar's `assets//`, `data//` and `data/minecraft/tags/` into +`mods//carry/`, a folder content mod (API 0.2) the game loads with `--dev-mod`, and keeps +what a content mod may not hold (other namespaces such as `c` tags, vanilla data) in `outside/`. +No class file is copied. It upgrades vanilla-type recipes from 1.21.1 ingredients to 26.3's, +adds the `items/` definitions 1.21.4 introduced, and declares each block from a vanilla template +guessed from its blockstate properties (stone for none, a door for `facing, half, hinge, open, +powered`, ...); blocks with an unknown property set, custom recipe types and NeoForge +conditions are listed in `carry-report.json` for the oracle step. + ### 1. Import | Format | Read | Jars come from | From be6a9d2cb57c63bf8dbcc3a08238582f6d5e31f5 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 16 Oct 2026 19:32:45 +0200 Subject: [PATCH 09/12] Stronghold ring test: wrap the seed + 1 for i64::MAX The threaded test added seed + 1 for seed = i64::MAX, which overflows in debug builds (CI's verify). Co-Authored-By: Claude Opus 5.5 --- crates/horizon-world/src/worldgen/generator_structures.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/horizon-world/src/worldgen/generator_structures.rs b/crates/horizon-world/src/worldgen/generator_structures.rs index 76b4d39c9..4ccf2ebb8 100644 --- a/crates/horizon-world/src/worldgen/generator_structures.rs +++ b/crates/horizon-world/src/worldgen/generator_structures.rs @@ -395,7 +395,7 @@ mod tests { let work = RingWork::new(vec![ (0, ring_searches(seed, 32, 3, 128)), (1, ring_searches(seed ^ 5, 10, 4, 0)), - (2, ring_searches(seed + 1, 20, 5, 40)), + (2, ring_searches(seed.wrapping_add(1), 20, 5, 40)), ]); let sets = [&preferred, &none, &preferred]; std::thread::scope(|scope| { @@ -409,7 +409,7 @@ mod tests { }; assert_eq!(rings["set0"], serial(seed, 32, 3, 128, &preferred)); assert_eq!(rings["set1"], Vec::new()); - assert_eq!(rings["set2"], serial(seed + 1, 20, 5, 40, &preferred)); + assert_eq!(rings["set2"], serial(seed.wrapping_add(1), 20, 5, 40, &preferred)); } } } From bfb6a0458d45f0e02f21e68d2c988cc804cae37e Mon Sep 17 00:00:00 2001 From: Cosmic <84875247+Online5277@users.noreply.github.com> Date: Fri, 16 Oct 2026 14:33:44 -0400 Subject: [PATCH 10/12] Restrict crafter result rendering to crafter menus --- crates/horizon-client/src/game.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 7072abeb5..57beb9f77 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -4109,7 +4109,9 @@ impl Game<'_> { fn slot_view(&mut self, i: usize) -> Option { match i { 200 => self.craft_result(), - 309 => self.crafter_result(), + 309 if matches!(self.container_kind(), Some(BlockEntity::Crafter(_))) => { + self.crafter_result() + } _ => self.slot_mut(i).clone(), } } From 24bb3f97aa21d9107fd5b6243337847fd7b0f134 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 16 Oct 2026 20:35:59 +0200 Subject: [PATCH 11/12] Stronghold ring test: rustfmt Co-Authored-By: Claude Opus 5.5 --- crates/horizon-world/src/worldgen/generator_structures.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/horizon-world/src/worldgen/generator_structures.rs b/crates/horizon-world/src/worldgen/generator_structures.rs index 4ccf2ebb8..4f804712f 100644 --- a/crates/horizon-world/src/worldgen/generator_structures.rs +++ b/crates/horizon-world/src/worldgen/generator_structures.rs @@ -409,7 +409,10 @@ mod tests { }; assert_eq!(rings["set0"], serial(seed, 32, 3, 128, &preferred)); assert_eq!(rings["set1"], Vec::new()); - assert_eq!(rings["set2"], serial(seed.wrapping_add(1), 20, 5, 40, &preferred)); + assert_eq!( + rings["set2"], + serial(seed.wrapping_add(1), 20, 5, 40, &preferred) + ); } } } From 9c03535011dedea6cb7f87f6d64fa957c94a8678 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 16 Oct 2026 22:22:56 +0200 Subject: [PATCH 12/12] docs/server.md: hosting a Horizon server, the design What the dedicated server does today (measured against vanilla 26.3), the horizon-server supervisor and its packaging, server.toml, the CLI and TUI, the web panel, backups and updates, the control API and MCP, the bot benchmark plan against vanilla and Paper, the working version for 2026-10-23, and the PRs that build it (#551 to #556). Co-Authored-By: Claude Opus 5.5 --- docs/README.md | 1 + docs/server.md | 522 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 523 insertions(+) create mode 100644 docs/server.md diff --git a/docs/README.md b/docs/README.md index af78b7f01..7cf322042 100644 --- a/docs/README.md +++ b/docs/README.md @@ -6,6 +6,7 @@ To play, use [the launcher](../launcher/README.md); the project site is [playhor - [Build from source](../README.md#build-from-source): your own copy of the game, resource preparation, `cargo run`. - [Native client](native-client.md): options, controls, the dedicated server, `horizon-ctl` scripting, packaging, architecture. +- [Hosting a server](server.md): `horizon-server`, the design and the plan: install, `server.toml`, the CLI, the web panel, backups, services, Docker, and the bot benchmark against vanilla. - [Agent control](agent-control.md): what AI agents can drive in the launcher and the game, and the design of the control socket, `horizon-ctl` and MCP. - [Graphics](graphics.md): the Simple and Advanced graphics settings, the graphics benchmark and what it measured. - [Benchmark](benchmark.md): Horizon against vanilla 26.3 and vanilla with performance mods, on the same machine, world, path and settings: what is measured, how it is kept fair, how to reproduce it. diff --git a/docs/server.md b/docs/server.md new file mode 100644 index 000000000..6e165f534 --- /dev/null +++ b/docs/server.md @@ -0,0 +1,522 @@ +# Hosting a Horizon server: design + +Horizon's dedicated server should be the easiest and lightest Minecraft 26.3 server to host. It should be one +small native program with no Java and no Python, one command to install it, one config file, and a CLI, a web +panel and a control API to manage it. At the same view and simulation distance and with the same players, it +should beat vanilla, Paper and Fabric + Lithium on TPS/MSPT, RAM, CPU, chunk generation and start time. +Gameplay stays vanilla: there are no Paper-style rule changes unless the owner turns one on. + +This page covers what the server does today (measured), the design of `horizon-server`, and what ships for the +first working version (2026-10-23). It replaces the Java/Fabric starter (`docs/server-quickstart.txt`, +`tools/server.py`), which goes once the benchmark below exists. + +## 1. Today + +### How it runs + +The dedicated server is the game itself, run headless: +`horizon-client --resources --dedicated [--nogui] [--port N] [--world NAME] [--universe DIR] +[--initSettings]`, started in the server folder (`crates/horizon-client/src/game/dedicated.rs`). + +- **Startup.** It follows vanilla's `Main.main` and `DedicatedServer.initServer`. It reads `server.properties` + and writes it straight back with vanilla's defaults filled in (`horizon_client::server`, byte-exact with + vanilla's `Properties.store`), and refuses to start until `eula.txt` says `eula=true`. It then loads the + operator, whitelist and ban lists in vanilla's order and format, and opens `/` with + the same `Game` as single player, minus the local player. +- **Network.** Players join through the play-state hub that Open to LAN uses (`crates/horizon-client/src/lan`). + Status ping, legacy ping, compression, login gate (bans, whitelist, IP bans), operator permission levels, + the player limit with `bypassesPlayerLimit`, MOTD and `server-icon.png` all follow `server.properties`. +- **Main loop.** It ticks at 20 TPS on one thread, with chunk generation and lighting on the worker pool. + Chunks stay loaded around every player (`lan_update_streaming`), or around the spawn while nobody is + online. It supports `pause-when-empty-seconds`, `player-idle-timeout` and the autosave. +- **Console (stdin).** `stop`, `save-all`, `save-on`/`save-off`, `list [uuids]`, `op`/`deop`, + `whitelist on|off|list|reload|add|remove`, `ban`, `ban-ip`, `pardon`, `pardon-ip`, `banlist`, `kick` and + `say` behave as in `DedicatedServer`. Every other command goes to the game's own dispatcher as the server + (permission level 4). The log follows vanilla's Log4j pattern, in `logs/latest.log` and on stdout. + +### Measured (2026-10-16, Mac mini M4 Pro, main at bd8dff20, release build) + +The server ran in a scratch folder with `online-mode=false`, `level-seed=12345` and view and simulation +distance 10. The vanilla server was the pinned 26.3 `server.jar` on Java 25 with `-Xmx4G`. + +| | Horizon (`horizon-client --dedicated`) | Vanilla 26.3 | +|---|---|---| +| Start, new world (wall clock, to `Done`) | 11.4 s (`Done (9.762s)`) | 12.5 s (`Done (2.066s)`, includes the bundler's first unpack) | +| Start, existing world (wall clock) | **5.8 s** (resources load in 1.6 s) | 10.7 s (`Done (0.446s)`: the JVM and DataFixerUpper take the rest) | +| RAM, idle with nobody online (RSS) | 1.40–1.51 GB | **0.93–0.97 GB** | +| CPU, idle with nobody online | 1.2 % of one core | **0.6 %** | +| `stop` to exit | 0.6 s | 0.6 s | +| What has to be installed | one 46.8 MB binary + 589 MB of prepared resources | Java 25 + the 62 MB jar | +| Crates linked | 384 (wgpu, winit, cpal, gilrs, arboard, naga…) | n/a | + +On Linux the binary loads `libasound.so.2` (cpal) and `libudev.so.1` (gilrs) at startup, even on a server. +wgpu, winit and the Wayland/X11 libraries are loaded only when a window opens. That makes a minimal +container image need ALSA and udev for a program that never plays a sound or reads a controller. + +The two clear losses are RAM and idle CPU. The dedicated server loads everything the client needs to draw +(the HUD atlas, item renderer, block-entity models, panorama, fog and music tables, entity models), and it +builds the meshing tables at startup. None of these are used without a window. Loading them is startup work, +not tick cost, so it is in scope here (see section 9). + +### Joining it + +- **Horizon client:** works. A headless Horizon client (`--server 127.0.0.1:PORT`, the harness mode) logged + in, received its 49 chunks at render distance 2, chatted, saw `/list`, and left. The server's log matched + vanilla's lines (`logged in with entity id`, `joined the game`, `[Not Secure] …`, `lost connection: + Disconnected`, `left the game`). +- **Vanilla 26.3 client:** checked by `tools/test_dedicated_server_parity.py`. VanillaProtocolClient, a + headless client built from vanilla's own codecs, pings and joins Horizon's server and vanilla's from the + same world copy. It compares every login, configuration and play packet up to the first chunk batch, the + chunk bytes, chat, `/list`, failing commands, the console lines and the files left behind. Result on main: + see "Harnesses on main" below. The real vanilla GUI client (`tools/vanilla_client.py`) could not be run + here: the tool only knows Linux natives, and the Mac mini's session was locked, which stalls the client's + rendering. It runs on the desktop under vscreen. + +### What fails or is missing + +| Area | Status | +|---|---| +| `online-mode=true` | **Not supported.** The server logs a warning and runs offline: no session `hasJoined` check, no encryption, no secure chat. (The client side of online mode works and is tested by `test_online_mode_parity.py` against vanilla's server.) A public server must not run like this. This is the top engine item. | +| Nether and End | **Players can't change dimension.** The engine keeps one dimension loaded (`Game::enter_dimension` moves only the local single-player player), and the hub serves one `dimension` to everyone. | +| Refused logins | Nothing is logged. Vanilla logs `Disconnecting : ` (`ServerLoginPacketListenerImpl.disconnect`) and ` lost connection: …`. | +| Gameplay for remote players | The LAN guest leftovers apply: an item used on a block is used twice, the recipe book doesn't work, status effects don't apply to guests, arrows don't record their owner, team friendly fire is ignored (#484). Block desync and short freezes are reported on a same-machine server (#500). | +| Defaults that surprise owners | `white-list=true` is vanilla 26.3's default, so a fresh server refuses everyone until they are added. That matches vanilla, so the first-run flow (section 3) asks about the whitelist up front. | +| Management | Only stdin. There is no service, no backups, no status beyond `list`, no restart after a crash, and a server can't be managed while it runs in the background. | +| Packaging | The server is the full game client. Resources come from `horizon-prepare`, which needs a JDK and the official launcher's client install, both awkward on a headless VPS. | +| Tick times | The dedicated loop never recorded its tick times in the game clock (only the windowed loop did), so `/tick query` on a dedicated server reported 0 ms. Fixed in PR 2b, which the benchmark needs (section 9). | +| Scale | Not measured with more than one player. The tick is single-threaded, and each remote player costs the hub one tracking and chunk-sending pass. | + +### Harnesses on main + +On the tip of the code PRs (2a to 3, rebased on b4ad6538), `tools/parity.py dedicated_server online_mode +server_files server_player_combat server_player_damage` passed all five. That includes the vanilla protocol +client joining Horizon's server and vanilla's from the same world. The full `--changed origin/main` set +(309 harnesses, since the PRs touch the client crate) had passed 60 of 60 when it was stopped for disk +space; CI runs it on every pull request. + +## 2. Architecture: a supervisor and an engine + +``` + owner ──► horizon-server (CLI, TUI) browser ──► web panel AI ──► MCP (stdio) + │ │ │ + └────────── control API: HTTP + JSON on 127.0.0.1, token ────────┘ + │ + horizon-server run (the supervisor: no GPU, no game code) + server.toml ──► server.properties ──► │ ◄── stdout: the vanilla log + backups, updates, crash restart │ ──► stdin: console commands + │ ◄─► engine endpoint: status, TPS/MSPT, players + ▼ + the engine: horizon-client --dedicated (the game, headless) +``` + +`horizon-server` is a new crate and binary (`crates/horizon-server`). It is the only program an owner runs. +It depends only on the light crates (`horizon-core`, `horizon-protocol`, `horizon-world`, `horizon-bench`) +plus `serde`, `toml`, `toml_edit` and `zip`, which are already in the lockfile. It doesn't link wgpu, winit, +audio or controller crates. It runs the engine as a child process. + +The supervisor/engine split stays even after the engine becomes headless, for these reasons: + +- **Crash restart and diagnostics.** The supervisor outlives an engine crash. It keeps the crash report, + restarts the engine with backoff, and the panel stays up to say what happened. +- **Safe updates.** An update swaps the engine binary and restarts. If the new engine fails its start check, + the supervisor rolls back to the previous one. +- **Parity stays cheap.** The engine keeps reading vanilla's own files (`server.properties`, the four JSON + lists, `eula.txt`) exactly as `test_dedicated_server_parity.py` checks them today. The clean config is a + layer on top and never changes what the engine sees. +- **No conflicts.** The supervisor touches none of the game code that the tick and integrated-server work + (perf-server-1, perf-ticks-2) are changing. + +**The engine today** is `horizon-client --dedicated`. It needs one small, additive change: a status endpoint. +With `--control-socket /.horizon/engine.json` (the existing control protocol: TCP on 127.0.0.1 plus a token, on +every OS), the dedicated loop answers `server-status` with TPS, MSPT (average, P50/P95/P99 from the tick +clock `/tick query` uses), players (name, UUID, address, ping), loaded chunks and uptime. It answers +`console ` with the command's output. + +**The headless engine** comes after perf-ticks-2's work lands: Resources split into shared and per-game, the +game built in `Game::new`, the integrated server on its own thread. On that base: + +1. The dedicated path skips everything only a window uses: the HUD atlas, item renderer, block-entity and + entity models, panorama, fog, music, and mesh tables (perf-ticks-2 already stops server-side meshing). + This is the RAM and start-time item. +2. The game moves into a library crate, with the window, audio and controller parts behind a `client` + feature. A `horizon-engine` binary built without that feature links no wgpu, winit, cpal or gilrs, and + needs neither ALSA nor udev. The supervisor finds it beside itself and falls back to `horizon-client`. + +## 3. Packaging + +**Binaries.** The release archives already hold `horizon-client`, `horizon-ctl` and `horizon-prepare`. +`horizon-server` joins them (PR 3: `xtask package` and `release.yml` build it and check it starts). Once +`horizon-engine` exists (section 2), a separate small archive per platform +(`horizon-server--.tar.gz` or `.zip`) holds just the server's binaries. + +**Game files: the owner's own copy, as with `prepare`.** Nothing of Mojang's is redistributed and nothing is +downloaded on the owner's behalf. `horizon-server` looks for prepared resources in this order: + +1. `[engine] resources` in `server.toml`; +2. `resources/` in the server folder; +3. `.local/horizon-client/26.3` beside the engine or up to four folders above it: a release folder, the + installer's `game-files` (below), or a checkout. + +If none is found, `init` says where it looked. Today the owner runs `horizon-prepare --prepare --server-jar +PATH` from the release folder (it needs a JDK and the client files, as for playing), or copies a folder +prepared on their own computer to the server (`scp -r`). Planned wrappers: + +- **On this machine:** `horizon-server prepare --server-jar PATH` runs `horizon-prepare` on the owner's own + server jar plus their launcher install. +- **Prepared elsewhere:** `horizon-server import-resources DIR` copies a folder prepared on the owner's own + computer, for example by the launcher, to a headless machine. + +A server-only `prepare` (from `server.jar` alone, without the client jar or a JDK) is the follow-up that +removes the last Java step. It needs a list of which prepared files the server really reads, and that list +comes out of the headless-engine work. + +**One-command install** (`packaging/server/`): + +- Linux and macOS: + `curl -fsSL https://raw.githubusercontent.com/ludovic111/horizon/main/packaging/server/install.sh | sh` + The script picks the release archive for the OS and architecture (Linux x86_64, macOS arm64), downloads + it from GitHub Releases, and checks it against the SHA-256 GitHub publishes for each asset. It never asks + for sudo. Options: `--version vX.Y.Z`, `--dir` (default `~/.local/share/horizon`), `--bin` (default + `~/.local/bin`). +- Windows: + `irm https://raw.githubusercontent.com/ludovic111/horizon/main/packaging/server/install.ps1 | iex` + The script does the same into `%LOCALAPPDATA%\Horizon` (`-Version`, `-Dir`), with junctions instead of + links (no admin rights), and adds `bin` to the user's PATH. +- **Layout.** Each release goes to `/releases/` and `/current` points at the one in use, so an + update or a rollback is one link. `/horizon-server` and `/horizon-prepare` are two-line launchers + that run `current`. The prepared game files live in `/game-files/horizon-client` and each release's + `.local/horizon-client` links there, so updates keep them. The script then prints the next steps + (`horizon-prepare`, `horizon-server init`). + +**Docker** (`packaging/server/Dockerfile`, built the same way as the relay's image): a multi-stage build that +ends on `debian:bookworm-slim` as user `horizon`. It adds `libasound2` and `libudev1` until the headless engine +lands. It uses volume `/data` for the server folder, takes the owner's prepared game files as a read-only +mount on `/opt/horizon/.local/horizon-client/26.3`, exposes port `25565/tcp`, and keeps the control API (and +later the panel) on 127.0.0.1 inside the container: `docker exec horizon-server status|cmd|backup`. +On the first start, `HORIZON_EULA=true` and `HORIZON_WORLD`, `HORIZON_SEED`, `HORIZON_PORT`, +`HORIZON_WHITELIST`, `HORIZON_PLAYERS` and `HORIZON_OP` answer `init`'s questions; without the EULA answer +the container exits and says why. `docker stop` sends SIGTERM, which saves and stops cleanly (give it +`--stop-timeout 180`). The image is published only with Ludovic's OK (it's hosted infrastructure). + +**Services.** + +- `horizon-server service install` writes and enables a systemd unit (`--user`, or a system unit with + `--system`) on Linux, a launchd agent (`~/Library/LaunchAgents/dev.horizon.server..plist`) on + macOS, or a Scheduled Task at logon on Windows (a real Windows service needs a wrapper and comes later). +- Each runs `horizon-server run --dir ` through the installer's launcher, so an update applies at the + next restart. The supervisor restarts the engine itself, and the service manager restarts the supervisor. + `service uninstall` stops and removes it and leaves the server folder alone. +- `service print` shows the unit or agent it would write, for owners who'd rather install it by hand. + +**First run (`horizon-server init`, interactive on a terminal):** + +1. **EULA.** It shows the link to https://aka.ms/MinecraftEULA and asks "Do you agree? [y/N]". Only a yes + writes `eula=true`. +2. **World.** It asks for the world name (`world`) and an optional seed. An existing world folder, or a + vanilla server folder, is detected and kept: its `server.properties` is imported (section 4). +3. **Port.** 25565 by default, checked free right away. +4. **Players.** It asks whether to use a whitelist (yes by default, as vanilla) and who to add first, then + offers to make the first player an operator. +5. **Game files.** It finds or explains the prepared resources (see above). +6. It writes `server.toml`, shows the panel address and the commands to start it in the background + (`horizon-server start`) or as a service. + +Every question has a flag (`--eula`, `--world`, `--seed`, `--port`, `--whitelist`/`--no-whitelist`, +`--player NAME`, `--op NAME`, `--yes`) and an environment variable for Docker (`HORIZON_EULA`, …; flags win). + +## 4. One config file: `server.toml` + +The owner edits only `server.toml`. The supervisor writes `server.properties` from it before every engine +start, so the engine stays exactly vanilla. The file is short: typed sections for what owners change, and +a `[properties]` table that passes any other vanilla key through unchanged. + +```toml +# Horizon server settings (docs/server.md). The server writes them into server.properties before +# every start: edit this file, not that one. + +[server] +motd = "A Minecraft Server" +port = 25565 +bind = "" # listen on every address, or only on this one (server-ip) +max_players = 20 +online_mode = true # not supported by Horizon yet: the server runs offline and says so +whitelist = true # only whitelisted players and operators can join +enforce_whitelist = false # kick online players taken off the whitelist +view_distance = 10 +simulation_distance = 10 + +[world] +name = "world" # the world folder +seed = "" # empty: a random seed on the first start +game_mode = "survival" # survival, creative, adventure or spectator +difficulty = "easy" # peaceful, easy, normal or hard +hardcore = false + +# Any other server.properties key, spelled as vanilla spells it, for example: +# spawn-protection = 16 +[properties] + +[engine] +path = "" # the game program; empty: the one installed beside horizon-server +resources = "" # the prepared game files; empty: resources/ here, else the usual places +restart_on_crash = true + +[backups] +every = "6h" # automatic backups while the server runs: 30m, 6h, 1d...; "0" turns them off +keep = 12 # automatic backups kept (backups made by hand are never deleted) +``` + +This is the file `init` writes today (PR 4 adds `[panel] bind = "127.0.0.1:8130"` and `allow_public = +false`, section 6). The defaults are vanilla 26.3's (a test checks each one). Unknown keys are errors, as are +a typed key set again under `[properties]` and values vanilla would refuse. Planned: `backups.before_update` +(section 7). + +- **Import.** `horizon-server init` in a folder with a `server.properties`, or `horizon-server import + server.properties`, reads it with vanilla's own parser (`ServerProperties`). Keys with typed fields go into + their sections; any other key that differs from vanilla's default goes into `[properties]`. Keys left at + their defaults are dropped, so the TOML stays short. The original file is kept as + `server.properties.imported`. +- **Changes made in game.** The engine writes `server.properties` itself, for example after `/whitelist on`. + After it stops, the supervisor compares the file with what it wrote and copies any changed keys back into + `server.toml`, so an in-game change survives the next start. Edits go through `toml_edit`, so the owner's + comments and order are kept. +- **Reload (planned).** The four lists already reload through the engine (`whitelist reload`). `reload` will + send what can change live and say which settings need a restart. A real hot reload (view distance, MOTD) needs engine + support and comes later. +- `server.properties`, `ops.json`, `whitelist.json`, `banned-*.json` and `world/` keep vanilla's names and + formats. A vanilla server folder works as is, and so does going back to vanilla. + +## 5. CLI and TUI + +``` +horizon-server init first run (section 3) +horizon-server run supervisor + engine in the foreground; the console on stdin +horizon-server start | stop | restart in the background (detached `run`); each waits for the game +horizon-server status [--json] up?, uptime, TPS, MSPT, players, RAM, CPU, crashes +horizon-server cmd one console command, with its output +horizon-server logs [-f] [-n N] +horizon-server op|deop +horizon-server whitelist on|off|list|add|remove [player] +horizon-server backup [NAME] backups restore [--yes] +horizon-server import +horizon-server service install|uninstall|print [--system] +horizon-server panel the panel's address and a one-time login link + +Planned: +horizon-server console attach: the live log, and send commands (Ctrl-D detaches) +horizon-server ban|pardon [reason] ban-ip|pardon-ip
(`cmd ban …` works now) +horizon-server update [--check] [--version vX.Y.Z] rollback +horizon-server import-resources prepare --server-jar PATH +horizon-server mcp MCP server on stdio (section 8) +``` + +- Every command takes `--dir` (the server folder; default: the current folder). +- While the server runs, management commands go through the control API, so the engine applies them as + vanilla's console would and its lists stay right. While it's stopped, they edit the vanilla files directly + with the same code the engine uses (`horizon_server_files::user_lists`). +- **The TUI** is `horizon-server console`: the log, a one-line header (TPS, MSPT, players, RAM) that updates + every second, and a command line. It uses plain ANSI with no TUI framework, and falls back to plain lines + when stdout isn't a terminal. + +## 6. Web panel + +**Scope (MVP, PR 4):** status (phase, uptime, version, crashes), live TPS, MSPT, RAM and CPU graphs (the last +10 minutes, sampled once a second), players (list with ping, op, kick), the console (live log plus a command +line), and backups (list, back up now, restore with confirmation), with restart and stop. The panel lives in +the supervisor, so it is up exactly when the server is: starting a stopped server stays a command (or the +service manager's job). Whitelist and bans go through the console for now. + +**Later:** bans and IP bans, worlds, updates with rollback, `server.properties` import, logs and crash +reports, and modpacks (with the modpack work). + +**Tech:** one HTML page with plain JS and CSS (`crates/horizon-server/panel/`), embedded in the binary with +`include_str!`. No build step, no framework, no CDN. It uses Horizon's design system tokens (`horizon-theme`'s +colours, light and dark) and draws charts on a canvas. It polls the control API's routes once a second +(`status`, `metrics?since=`, `logs?since=`), served by the same small HTTP code on `std::net`, with no async +runtime or new dependency. Server-sent events can replace the polling later if a tab ever costs too much. + +**Auth: local or tailnet only, never an open admin port.** + +- **Bind.** The panel listens on `127.0.0.1:8130` by default. `panel.bind` can name a tailnet address + (100.64.0.0/10 or Tailscale's fd7a:115c:a1e0::/48) or another loopback address. Any other address, such as + 0.0.0.0 or a public IP, is refused unless `panel.allow_public = true` is set, and even then the panel logs + a warning at every start. +- **Who may act.** The API token in `.horizon/api.json` (file mode 0600) is what the CLI and MCP use. Browsers + get a one-time login link from `horizon-server panel`: a code good for 10 minutes, swapped for an + `HttpOnly; SameSite=Strict` session cookie. Anyone who can read the server folder can manage the server, + and no one else can. Sessions end when the supervisor stops. +- **Protection.** The `Host` header must name the panel itself: its address, `localhost` on loopback, or a + tailnet name (a MagicDNS short name or `*.ts.net`). That stops DNS rebinding. Changes are POST only and need + the `X-Horizon: 1` header, which stops CSRF. After 10 failed logins in a minute, logins are refused for a + while. The page is served with a strict Content-Security-Policy (nothing from elsewhere) and can't be + framed. +- **Remote access** goes over Tailscale/SSH (`ssh -L 8130:127.0.0.1:8130 host`), with no TLS or passwords to + manage. HTTPS comes for free with `tailscale serve`, and the doc shows how to set it up. + +## 7. Backups, updates and crash restart + +- **Backups.** The supervisor runs `save-off`, then `save-all flush`, then zips the world plus the vanilla + files and `server.toml` into `backups/-.zip`, then runs `save-on`. Players stay online. Zip + works for everyone, Windows included. It runs on the `every` schedule and on demand (later also before an + update). It keeps the last `keep` automatic backups and never deletes one made by hand. A backup is written + to a temporary name and renamed when complete, so a crash never leaves a half-written backup that looks + finished. A stopped server is backed up straight from its files. +- **Restore.** `restore NAME` takes a file name, a unique prefix, or the name given to `backup` (the newest + with it). The server stops. The current world is moved to `backups/replaced-