diff --git a/Cargo.lock b/Cargo.lock index 5af8dfd15..54be02330 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1331,8 +1331,10 @@ name = "horizon-mods" version = "0.1.0" dependencies = [ "log", + "minisign-verify", "serde", "serde_json", + "sha2", "toml", "wasmtime", "wit-component 0.239.0", @@ -1919,6 +1921,12 @@ dependencies = [ "libc", ] +[[package]] +name = "minisign-verify" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "871285dc19d8d0ebe0eef3d0e99a205f2a71363b122632cbbfa4a6c370a960ce" + [[package]] name = "miniz_oxide" version = "0.8.9" diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index aa0490655..23135f16f 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -140,8 +140,9 @@ pub(super) struct Remote { stepped: HashMap, /// `LivingEntity.lerpHeadTo`: the head yaw and the ticks left to reach it. head_lerps: HashMap, - /// Item entities waiting for their item data. - pending_items: HashMap, + /// Item entities waiting for their item data: where they are and their + /// motion. + pending_items: HashMap, health: (f32, i32, f32), /// `LocalPlayer.flashOnSetHealth`: set by the first health the server /// sends; from then on a drop starts the hurt animation. @@ -1386,7 +1387,7 @@ impl Game<'_> { } if let Some(r) = self.remote.as_mut() { if kind == "minecraft:item" { - r.pending_items.insert(id, position); + r.pending_items.insert(id, (position, Vec3::ZERO)); } r.entities.insert( id, @@ -1465,6 +1466,23 @@ impl Game<'_> { m.on_ground = on_ground; return; } + if let Some(p) = r.pending_items.get_mut(&id) { + p.0 = position; + } + // ItemEntity and FallingBlockEntity keep Entity's NO_OP + // InterpolationHandler: moveOrInterpolateTo puts them there at + // once, and their own tick moves them on between the server's + // updates (`tick_remote_entities`). + let at = Vec3::new(position[0], position[1], position[2]); + if let Some(d) = self.dropped.iter_mut().find(|d| d.id == id as u32) { + d.position = at; + d.on_ground = on_ground; + return; + } + if let Some(f) = self.falling.iter_mut().find(|f| f.id == id as u32) { + f.position = at; + return; + } // AbstractInterpolationHandler.interpolateTo: without a rotation, // the one it was heading for. let rotation = rotation.or_else(|| r.lerps.get(&id).and_then(|l| l.rotation)); @@ -1476,9 +1494,6 @@ impl Game<'_> { steps: 3, }, ); - if let Some(p) = r.pending_items.get_mut(&id) { - *p = position; - } } self.turn_remote_object(id, rotation.map(|(yaw, _)| yaw), on_ground); self.turn_remote_projectile(id, rotation); @@ -1811,6 +1826,8 @@ impl Game<'_> { } } self.spawn_entity(p.id, &kind, p.position, rotation); + // Entity.recreateFromPacket: setDeltaMovement(packet.getMovement()). + self.remote_motion(p.id, p.velocity); if !self.spawn_remote_object(p.id, &kind, p.position, rotation[0], p.data) { self.spawn_remote_projectile( p.id, @@ -2129,6 +2146,8 @@ impl Game<'_> { pl.velocity = Vec3::new(p.velocity[0], p.velocity[1], p.velocity[2]); } } + // The same packet for another entity: Entity.lerpMotion. + C::SetEntityMotion(p) => self.remote_motion(p.id, p.velocity), // ClientPacketListener.handleDamageEvent: LivingEntity.handleDamageEvent // jolts the walk animation and restarts the hurt flash. Its hurt // sound comes as a sound from Horizon's host, which plays it itself. @@ -2952,7 +2971,9 @@ impl Game<'_> { item.stack = stack; return; } - let Some(p) = position else { return }; + let Some((p, velocity)) = position else { + return; + }; let at = Vec3::new(p[0], p[1], p[2]); let bob_offset = self.world.random.next_float() * std::f32::consts::PI * 2.0; self.dropped.push(ItemEntity { @@ -2963,7 +2984,7 @@ impl Game<'_> { stack, position: at, previous: at, - velocity: Vec3::ZERO, + velocity, on_ground: false, age: 0, pickup_delay: 0, @@ -2971,6 +2992,23 @@ impl Game<'_> { ticks: 0, }); } + /// `Entity.lerpMotion` (and `recreateFromPacket`'s motion) for the + /// entities whose own tick moves them on a client: dropped items and + /// falling blocks. + fn remote_motion(&mut self, id: i32, velocity: [f64; 3]) { + let v = Vec3::new(velocity[0], velocity[1], velocity[2]); + if let Some(d) = self.dropped.iter_mut().find(|d| d.id == id as u32) { + d.velocity = v; + } else if let Some(f) = self.falling.iter_mut().find(|f| f.id == id as u32) { + f.velocity = v; + } else if let Some(p) = self + .remote + .as_mut() + .and_then(|r| r.pending_items.get_mut(&id)) + { + p.1 = v; + } + } fn apply_remote_health(&mut self) { let Some((health, food, saturation)) = self.remote.as_ref().map(|r| r.health) else { return; @@ -3491,20 +3529,12 @@ impl Game<'_> { l.steps -= 1; } } + // ItemEntity.tick runs on a client too: gravity, collisions and + // friction move the item between the server's updates (every 20 + // ticks), and only the server removes it. + let (world, facts, collision) = (&self.world, self.res.facts(), self.collision); for d in self.dropped.iter_mut().filter(|_| running) { - d.previous = d.position; - d.age += 1; - if let Some(l) = lerps.get_mut(&(d.id as i32)) - && l.steps > 0 - { - let n = f64::from(l.steps); - d.position = Vec3::new( - d.position.x + (l.target[0] - d.position.x) / n, - d.position.y + (l.target[1] - d.position.y) / n, - d.position.z + (l.target[2] - d.position.z) / n, - ); - l.steps -= 1; - } + d.tick(world, facts, collision, false); } head_lerps.retain(|_, (_, steps)| *steps > 0); for (item, ticks, at, eye, pitch, yaw) in using { diff --git a/crates/horizon-client/src/game/remote_projectiles.rs b/crates/horizon-client/src/game/remote_projectiles.rs index 5b2a22789..bc6d0b13e 100644 --- a/crates/horizon-client/src/game/remote_projectiles.rs +++ b/crates/horizon-client/src/game/remote_projectiles.rs @@ -2,7 +2,7 @@ //! end crystals on a server's client (`ClientPacketListener.handleAddEntity` //! and `handleSetEntityData`): the server simulates them, the client draws //! them where it says and interpolates their moves. -use super::{FallingBlock, Game}; +use super::{FallStep, FallingBlock, Game}; use horizon_client::projectiles::{Projectile, ProjectileKind}; use horizon_core::{geometry::Vec3, position::BlockPos}; use horizon_protocol::entity_data::Value; @@ -193,11 +193,17 @@ impl Game<'_> { t.previous = t.position; t.position = step(t.id, t.position); } - for f in &mut self.falling { - f.previous = f.position; - f.age += 1; - f.position = step(f.id, f.position); + // FallingBlockEntity.tick runs on a client too: the block falls by + // itself between the server's updates (every 20 ticks), then waits + // where it lands for the server to place it and remove it. + let mut falling = std::mem::take(&mut self.falling); + for f in &mut falling { + if let Ok(FallStep::Landed(_)) = self.fall_step(f, false) { + // Block.updateEntityMovementAfterFallOn, then the air drag. + f.velocity = Vec3::new(f.velocity.x * 0.98, 0.0, f.velocity.z * 0.98); + } } + self.falling = falling; for p in &mut self.creatures.projectiles { p.previous = p.position; p.age += 1; diff --git a/crates/horizon-mods/Cargo.toml b/crates/horizon-mods/Cargo.toml index 3dfc02331..840fc28c5 100644 --- a/crates/horizon-mods/Cargo.toml +++ b/crates/horizon-mods/Cargo.toml @@ -17,6 +17,9 @@ wasmtime = { version = "36.0.18", default-features = false, features = ["runtime # Turns a mod's core module into a component (cargo horizon-mod build, the tests). wit-component = { version = "0.239", optional = true } wit-parser = { version = "0.239", optional = true } +# The registry index's checks (feature `index`): SHA-256, and minisign signatures (no dependencies of its own). +sha2 = { version = "0.10", optional = true } +minisign-verify = { version = "0.3", optional = true } [features] # Loads and runs mods (the game's `mods` feature); without it only manifests, @@ -24,6 +27,8 @@ wit-parser = { version = "0.239", optional = true } # game's tests and probes) skip wasmtime and cranelift. runtime = ["dep:wasmtime"] pack = ["dep:wit-component", "dep:wit-parser"] +# The mod registry's index (docs/api/mod-index.md): the launcher, the registry server and its CI. +index = ["dep:sha2", "dep:minisign-verify"] # Runs real mods: `cargo test -p horizon-mods --features runtime,pack`. [[test]] diff --git a/crates/horizon-mods/src/index.rs b/crates/horizon-mods/src/index.rs new file mode 100644 index 000000000..73af7b46d --- /dev/null +++ b/crates/horizon-mods/src/index.rs @@ -0,0 +1,538 @@ +//! The mod registry's index, `index.json` (docs/api/mod-index.md): every +//! reviewed mod and modpack, its versions, the file of each with its size, +//! SHA-256 and minisign signature, and the files taken down. The launcher +//! reads it to browse and install, the website to show it, and the +//! registry's CI writes it. +//! +//! [`check_file`] is what a file must pass before it is installed: its size +//! and hash are the ones the index lists, its signature verifies against the +//! project's pinned key, and what its own manifest says matches the entry. +use crate::{ + Capability, Package, Side, + modpack::{self, Modpack}, +}; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use std::{io::Read, path::Path}; + +/// The index format this build reads and writes. A reader refuses a newer +/// one rather than misread it. +pub const SCHEMA: u32 = 1; +/// The biggest file the registry takes (a `.hmod` with content, a `.hpack`). +pub const MAX_FILE: u64 = 64 << 20; +/// Where files may be downloaded from: the registry's release assets. +pub const FILE_HOST: &str = "https://github.com/ludovic111/horizon-mods/releases/download/"; + +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +pub struct Index { + pub schema: u32, + /// When CI built it, RFC 3339. + #[serde(default)] + pub generated: String, + #[serde(default)] + pub projects: Vec, + #[serde(default)] + pub revoked: Vec, +} + +/// What a project is. New kinds (data packs, worlds, themes) can be added +/// without a new schema: a reader keeps a kind it doesn't know as +/// [`Kind::Other`] and doesn't offer it. +#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(from = "String", into = "String")] +pub enum Kind { + /// A `.hmod`. + Mod, + /// A `.hpack`: registry mods played together as a profile. + Modpack, + /// A resource pack (`.zip` with `pack.mcmeta`) made or tested for Horizon. + ResourcePack, + /// A Horizon shader pack (the native WGSL format, not Iris or OptiFine + /// packs). Listed by the schema; submissions open with its API. + Shader, + Other(String), +} + +impl Kind { + pub fn name(&self) -> &str { + match self { + Kind::Mod => "mod", + Kind::Modpack => "modpack", + Kind::ResourcePack => "resourcepack", + Kind::Shader => "shader", + Kind::Other(name) => name, + } + } + + /// The extension of its files; None for a kind this build doesn't know. + pub fn extension(&self) -> Option<&'static str> { + match self { + Kind::Mod => Some("hmod"), + Kind::Modpack => Some("hpack"), + Kind::ResourcePack | Kind::Shader => Some("zip"), + Kind::Other(_) => None, + } + } +} + +impl From for Kind { + fn from(name: String) -> Kind { + match name.as_str() { + "mod" => Kind::Mod, + "modpack" => Kind::Modpack, + "resourcepack" => Kind::ResourcePack, + "shader" => Kind::Shader, + _ => Kind::Other(name), + } + } +} + +impl From for String { + fn from(kind: Kind) -> String { + kind.name().to_owned() + } +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct Project { + /// The manifest's id, `[a-z0-9_]{1,64}`: unique across the index. + pub id: String, + pub kind: Kind, + pub name: String, + /// One line for lists. + pub summary: String, + /// Markdown, for the project's page. + #[serde(default)] + pub description: String, + #[serde(default)] + pub authors: Vec, + /// The GitHub logins allowed to publish its updates. + #[serde(default)] + pub owners: Vec, + /// An SPDX expression. + pub license: String, + /// Where its source code is, `https://`. + pub source: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub icon: Option, + /// The author's minisign public key: every version must be signed with + /// it. None: unsigned (the SHA-256 still pins the reviewed file). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub key: Option, + /// Newest first. + pub versions: Vec, +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct Version { + pub version: String, + /// The day it was published, `YYYY-MM-DD`. + #[serde(default)] + pub published: String, + #[serde(default, skip_serializing_if = "String::is_empty")] + pub changelog: String, + /// A mod's API line (`0.1`), from its manifest; a modpack's format (`1`). + pub api: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub side: Option, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub capabilities: Vec, + /// The Minecraft version it is for. + pub game: String, + /// A modpack's mods, as its manifest lists them. + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub requires: Vec, + pub file: File, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct Requirement { + pub id: String, + pub version: String, + pub sha256: String, +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct File { + pub name: String, + pub url: String, + pub size: u64, + /// Lowercase hex. + pub sha256: String, + /// The `.minisig` file's text. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub signature: Option, +} + +/// A file taken down: by hash, or every version of `id` when `sha256` is +/// None. The launcher refuses to install it and turns installed copies off. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct Revoked { + pub id: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub sha256: Option, + pub reason: String, + #[serde(default)] + pub date: String, +} + +/// How far a checked file is trusted. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Trust { + /// Signed with the project's pinned key. + Verified, + /// The project has no key: only the hash pins it. + Unsigned, +} + +impl Index { + /// Parses and checks an index (see [`Index::check`]). + pub fn parse(text: &str) -> Result { + Self::parse_with(text, false) + } + + /// [`Index::parse`] for a local test registry, whose files may be + /// `file://` paths (trying a registry out before it is published). + pub fn parse_local(text: &str) -> Result { + Self::parse_with(text, true) + } + + fn parse_with(text: &str, local_files: bool) -> Result { + #[derive(Deserialize)] + struct Head { + schema: u32, + } + let head: Head = serde_json::from_str(text).map_err(|e| format!("not a mod index: {e}"))?; + if head.schema > SCHEMA { + return Err(format!( + "the mod index is schema {}; this build reads {SCHEMA}: update Horizon", + head.schema + )); + } + let index: Index = serde_json::from_str(text).map_err(|e| format!("mod index: {e}"))?; + index.check_with(local_files)?; + Ok(index) + } + + /// Every rule a reader relies on: unique valid ids, at least one + /// version each, plain file names with the kind's extension, `https` + /// URLs, hex hashes, keys and signatures that decode, and each + /// modpack's mods present in the index with the same hash. + pub fn check(&self) -> Result<(), String> { + self.check_with(false) + } + + fn check_with(&self, local_files: bool) -> Result<(), String> { + for (i, p) in self.projects.iter().enumerate() { + let at = |e: String| format!("{}: {e}", p.id); + if !valid_id(&p.id) { + return Err(format!("project id {:?} is not [a-z0-9_]{{1,64}}", p.id)); + } + if self.projects[..i].iter().any(|q| q.id == p.id) { + return Err(at("listed twice".into())); + } + if p.name.trim().is_empty() || p.license.trim().is_empty() { + return Err(at("needs a name and a licence".into())); + } + https(&p.source).map_err(at)?; + if let Some(icon) = &p.icon { + https(icon).map_err(at)?; + } + if let Some(key) = &p.key { + minisign_verify::PublicKey::from_base64(key.trim()) + .map_err(|e| at(format!("key: {e}")))?; + } + if p.versions.is_empty() { + return Err(at("has no version".into())); + } + for (j, v) in p.versions.iter().enumerate() { + let at = |e: String| format!("{} {}: {e}", p.id, v.version); + if v.version.trim().is_empty() + || p.versions[..j].iter().any(|w| w.version == v.version) + { + return Err(at("empty or repeated version".into())); + } + let f = &v.file; + let extension_ok = p + .kind + .extension() + .is_none_or(|e| f.name.ends_with(&format!(".{e}"))); + if !plain_name(&f.name) || !extension_ok { + return Err(at(format!( + "{:?} is not a plain file name for a {}", + f.name, + p.kind.name() + ))); + } + if !(local_files && f.url.starts_with("file:///")) { + https(&f.url).map_err(at)?; + } + hex64(&f.sha256).map_err(at)?; + if f.size == 0 || f.size > MAX_FILE { + return Err(at(format!( + "size {} is not 1 byte to {} MiB", + f.size, + MAX_FILE >> 20 + ))); + } + match (&p.key, &f.signature) { + (Some(_), None) => { + return Err(at("unsigned, but the project has a key".into())); + } + (None, Some(_)) => return Err(at("signed, but the project has no key".into())), + (Some(_), Some(sig)) => { + minisign_verify::Signature::decode(sig) + .map_err(|e| at(format!("signature: {e}")))?; + } + (None, None) => {} + } + for r in &v.requires { + let found = self + .project(&r.id) + .and_then(|q| q.versions.iter().find(|w| w.version == r.version)); + match found { + Some(w) if w.file.sha256 == r.sha256 => {} + Some(_) => { + return Err(at(format!( + "{} {}: another file than the index's", + r.id, r.version + ))); + } + None => { + return Err(at(format!( + "needs {} {}, which is not in the index", + r.id, r.version + ))); + } + } + } + } + } + for r in &self.revoked { + if let Some(h) = &r.sha256 { + hex64(h).map_err(|e| format!("revoked {}: {e}", r.id))?; + } + } + Ok(()) + } + + pub fn project(&self, id: &str) -> Option<&Project> { + self.projects.iter().find(|p| p.id == id) + } + + /// Why the file of `id` with this hash was taken down, if it was. + pub fn revoked(&self, id: &str, sha256: &str) -> Option<&Revoked> { + self.revoked.iter().find(|r| { + r.id == id + && r.sha256 + .as_ref() + .is_none_or(|h| h.eq_ignore_ascii_case(sha256)) + }) + } +} + +/// `[a-z0-9_]{1,64}`, the manifests' id rule. +pub fn valid_id(id: &str) -> bool { + (1..=64).contains(&id.len()) + && id + .bytes() + .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'_') +} + +fn https(url: &str) -> Result<(), String> { + match url.strip_prefix("https://") { + Some(rest) if !rest.is_empty() && !url.chars().any(char::is_whitespace) => Ok(()), + _ => Err(format!("{url:?} is not an https:// address")), + } +} + +fn hex64(h: &str) -> Result<(), String> { + if h.len() == 64 + && h.bytes() + .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) + { + Ok(()) + } else { + Err(format!("{h:?} is not a lowercase hex SHA-256")) + } +} + +/// One plain file name: no folder, no hidden file, nothing a file system +/// reads as something else. +fn plain_name(name: &str) -> bool { + !name.is_empty() + && name.len() <= 128 + && !name.starts_with('.') + && name + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b"._-+".contains(&b)) +} + +/// The SHA-256 of a file, lowercase hex. +pub fn sha256_file(path: &Path) -> Result { + let mut file = std::fs::File::open(path).map_err(|e| format!("{}: {e}", path.display()))?; + let mut hasher = Sha256::new(); + let mut buf = vec![0u8; 1 << 16]; + loop { + let n = file.read(&mut buf).map_err(|e| e.to_string())?; + if n == 0 { + break; + } + hasher.update(&buf[..n]); + } + Ok(hex(&hasher.finalize())) +} + +pub fn sha256(bytes: &[u8]) -> String { + hex(&Sha256::digest(bytes)) +} + +fn hex(bytes: &[u8]) -> String { + bytes.iter().map(|b| format!("{b:02x}")).collect() +} + +/// Checks a minisign signature (`.minisig` text) of `path` against `key`. +pub fn verify_signature(key: &str, signature: &str, path: &Path) -> Result<(), String> { + let key = + minisign_verify::PublicKey::from_base64(key.trim()).map_err(|e| format!("key: {e}"))?; + let sig = + minisign_verify::Signature::decode(signature).map_err(|e| format!("signature: {e}"))?; + let file = std::fs::File::open(path).map_err(|e| format!("{}: {e}", path.display()))?; + let mut verifier = key + .verify_stream(&sig) + .map_err(|e| format!("signature: {e}"))?; + let mut reader = std::io::BufReader::new(file); + let mut buf = vec![0u8; 1 << 16]; + loop { + let n = reader.read(&mut buf).map_err(|e| e.to_string())?; + if n == 0 { + break; + } + verifier.update(&buf[..n]); + } + verifier + .finalize() + .map_err(|_| "the signature doesn't match the project's key".to_string()) +} + +/// Checks the file at `path` against its entry before it is installed: +/// size, SHA-256, revocation, signature, and that its manifest says what the +/// index says (id, version, API, side, capabilities; a modpack's mods). +pub fn check_file( + index: &Index, + project: &Project, + version: &Version, + path: &Path, +) -> Result { + let size = std::fs::metadata(path) + .map_err(|e| format!("{}: {e}", path.display()))? + .len(); + if size != version.file.size { + return Err(format!( + "{} bytes, the index lists {}", + size, version.file.size + )); + } + let hash = sha256_file(path)?; + if hash != version.file.sha256 { + return Err("its SHA-256 isn't the one the index lists: nothing was installed".into()); + } + if let Some(r) = index.revoked(&project.id, &hash) { + return Err(format!("{} was taken down: {}", project.name, r.reason)); + } + let trust = match (&project.key, &version.file.signature) { + (Some(key), Some(sig)) => { + verify_signature(key, sig, path)?; + Trust::Verified + } + (None, None) => Trust::Unsigned, + _ => return Err("its signature and the project's key don't go together".into()), + }; + match project.kind { + Kind::Mod => { + let m = Package::read(path)?.manifest; + crate::Content::read(path, &m)?; + let mut caps = m.capabilities.clone(); + caps.sort(); + let mut listed = version.capabilities.clone(); + listed.sort(); + let differs = m.id != project.id + || m.version != version.version + || m.api != version.api + || Some(m.side) != version.side + || caps != listed; + if differs { + return Err(format!( + "its horizon-mod.toml ({} {}, API {}, {:?}, {}) isn't what the index lists", + m.id, + m.version, + m.api, + m.side, + names(&caps) + )); + } + } + Kind::ResourcePack => check_resource_pack(path)?, + Kind::Shader | Kind::Other(_) => { + return Err(format!( + "this Horizon can't install a {} yet", + project.kind.name() + )); + } + Kind::Modpack => { + let pack = Modpack::read(path)?; + let m = &pack.manifest; + let mods: Vec = m.mods.iter().map(modpack::PackMod::requirement).collect(); + if m.id != project.id + || m.version != version.version + || m.format.to_string() != version.api + || m.game != version.game + || mods != version.requires + { + return Err(format!( + "its {} isn't what the index lists", + modpack::MANIFEST + )); + } + } + } + Ok(trust) +} + +/// A resource pack: a zip with `pack.mcmeta` at its root holding a `pack` +/// object, and only plain paths. +fn check_resource_pack(path: &Path) -> Result<(), String> { + let file = std::fs::File::open(path).map_err(|e| format!("{}: {e}", path.display()))?; + let mut zip = zip::ZipArchive::new(file).map_err(|e| e.to_string())?; + for i in 0..zip.len() { + let name = zip + .by_index_raw(i) + .map_err(|e| e.to_string())? + .name() + .to_owned(); + if name.starts_with('/') || name.split('/').any(|part| part == "..") || name.contains('\\') + { + return Err(format!("{name:?} is not a plain path")); + } + } + let mut meta = String::new(); + zip.by_name("pack.mcmeta") + .map_err(|_| "pack.mcmeta: missing".to_string())? + .take(1 << 20) + .read_to_string(&mut meta) + .map_err(|e| format!("pack.mcmeta: {e}"))?; + let json: serde_json::Value = + serde_json::from_str(&meta).map_err(|e| format!("pack.mcmeta: {e}"))?; + if !json["pack"].is_object() { + return Err("pack.mcmeta: no \"pack\" object".into()); + } + Ok(()) +} + +fn names(caps: &[Capability]) -> String { + let names: Vec = caps.iter().map(ToString::to_string).collect(); + format!("[{}]", names.join(", ")) +} + +#[cfg(test)] +mod tests; diff --git a/crates/horizon-mods/src/index/tests.rs b/crates/horizon-mods/src/index/tests.rs new file mode 100644 index 000000000..b6d7f5bfb --- /dev/null +++ b/crates/horizon-mods/src/index/tests.rs @@ -0,0 +1,162 @@ +//! The sample index (`tests/index/index-v1.json`, the one docs/api/mod-index.md +//! shows) and its files: `coords_hud-0.1.0.hmod`, signed with the throwaway +//! key `test.pub` by minisign 0.12 (its secret key was deleted), and +//! `cozy-1.0.0.hpack`, unsigned. +use super::*; +use std::path::PathBuf; + +const OTHER_KEY: &str = "RWSe8ZD+cTOAqNd/jnXTmDtoKp0c0HxkGMrBBWcWQeEkPmcHB+XkaE9S"; + +fn fixtures() -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/index") +} + +fn sample() -> Index { + Index::parse(&std::fs::read_to_string(fixtures().join("index-v1.json")).unwrap()).unwrap() +} + +fn check(index: &Index, id: &str, file: &Path) -> Result { + let p = index.project(id).unwrap(); + check_file(index, p, &p.versions[0], file) +} + +#[test] +fn reads_the_sample_and_keeps_unknown_kinds() { + let index = sample(); + assert_eq!(index.schema, SCHEMA); + let kinds: Vec<&str> = index.projects.iter().map(|p| p.kind.name()).collect(); + assert_eq!(kinds, ["mod", "modpack", "world"]); + assert_eq!(index.projects[2].kind, Kind::Other("world".into())); + let hud = index.project("coords_hud").unwrap(); + assert_eq!( + hud.versions[0].capabilities, + [Capability::GameRead, Capability::Hud, Capability::Chat] + ); + // What CI writes reads back the same. + let again = Index::parse(&serde_json::to_string(&index).unwrap()).unwrap(); + assert_eq!(again, index); +} + +#[test] +fn files_pass_against_their_entries() { + let index = sample(); + let hmod = fixtures().join("coords_hud-0.1.0.hmod"); + assert_eq!(check(&index, "coords_hud", &hmod), Ok(Trust::Verified)); + let hpack = fixtures().join("cozy-1.0.0.hpack"); + assert_eq!(check(&index, "cozy", &hpack), Ok(Trust::Unsigned)); + let world = index.project("skyblock_start").unwrap(); + let e = check_file(&index, world, &world.versions[0], &hpack).unwrap_err(); + assert!(e.contains("bytes"), "{e}"); +} + +#[test] +fn a_changed_file_key_or_manifest_is_refused() { + let index = sample(); + let dir = std::env::temp_dir().join(format!("horizon-index-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let hmod = fixtures().join("coords_hud-0.1.0.hmod"); + let mut bytes = std::fs::read(&hmod).unwrap(); + let last = bytes.len() - 1; + bytes[last] ^= 1; + let tampered = dir.join("coords_hud-0.1.0.hmod"); + std::fs::write(&tampered, &bytes).unwrap(); + let e = check(&index, "coords_hud", &tampered).unwrap_err(); + assert!(e.contains("SHA-256"), "{e}"); + + let mut other_key = index.clone(); + other_key.projects[0].key = Some(OTHER_KEY.into()); + let e = check(&other_key, "coords_hud", &hmod).unwrap_err(); + assert!(e.contains("key"), "{e}"); + + let mut more_caps = index.clone(); + more_caps.projects[0].versions[0].capabilities.pop(); + let e = check(&more_caps, "coords_hud", &hmod).unwrap_err(); + assert!(e.contains("isn't what the index lists"), "{e}"); + + let mut revoked = index.clone(); + revoked.revoked.push(Revoked { + id: "coords_hud".into(), + sha256: None, + reason: "Test.".into(), + date: String::new(), + }); + let e = check(&revoked, "coords_hud", &hmod).unwrap_err(); + assert!(e.contains("taken down: Test."), "{e}"); + std::fs::remove_dir_all(&dir).unwrap(); +} + +#[test] +fn revocations_match_by_hash_or_by_id() { + let index = sample(); + let ones = "1".repeat(64); + assert!(index.revoked("bad_mod", &ones).is_some()); + assert!(index.revoked("bad_mod", &"2".repeat(64)).is_none()); + assert!(index.revoked("coords_hud", &ones).is_none()); +} + +#[test] +fn refuses_broken_indexes() { + let text = std::fs::read_to_string(fixtures().join("index-v1.json")).unwrap(); + let newer = text.replacen("\"schema\": 1", "\"schema\": 2", 1); + assert!(Index::parse(&newer).unwrap_err().contains("schema 2")); + let broken = |edit: &dyn Fn(&mut Index)| { + let mut index = sample(); + edit(&mut index); + index.check().unwrap_err() + }; + let e = broken(&|i| { + let twin = i.projects[0].clone(); + i.projects.push(twin) + }); + assert!(e.contains("listed twice"), "{e}"); + let e = broken(&|i| i.projects[0].versions[0].file.url = "http://example.org/x.hmod".into()); + assert!(e.contains("https"), "{e}"); + // A local test registry may list files on disk; the published one may not. + let local = text.replace( + "https://github.com/ludovic111/horizon-mods/releases/download/coords_hud-0.1.0/", + "file:///tmp/", + ); + assert!(Index::parse(&local).unwrap_err().contains("https")); + assert!(Index::parse_local(&local).is_ok()); + let e = broken(&|i| i.projects[0].versions[0].file.name = "../coords_hud.hmod".into()); + assert!(e.contains("plain file name"), "{e}"); + let e = broken(&|i| i.projects[0].versions[0].file.name = "coords_hud.zip".into()); + assert!(e.contains("plain file name"), "{e}"); + let e = broken(&|i| i.projects[0].versions[0].file.signature = None); + assert!(e.contains("unsigned"), "{e}"); + let e = broken(&|i| i.projects[0].key = Some("not a key".into())); + assert!(e.contains("key"), "{e}"); + let e = broken(&|i| i.projects[1].versions[0].requires[0].version = "9.9".into()); + assert!(e.contains("not in the index"), "{e}"); + let e = broken(&|i| i.projects[1].versions[0].requires[0].sha256 = "2".repeat(64)); + assert!(e.contains("another file"), "{e}"); + let e = broken(&|i| i.projects[0].id = "Coords".into()); + assert!(e.contains("is not"), "{e}"); +} + +#[test] +fn resource_packs_need_a_pack_mcmeta() { + use std::io::Write; + let dir = std::env::temp_dir().join(format!("horizon-index-rp-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let pack = |files: &[(&str, &str)]| { + let path = dir.join("look.zip"); + let mut zip = zip::ZipWriter::new(std::fs::File::create(&path).unwrap()); + for (name, text) in files { + zip.start_file(*name, zip::write::SimpleFileOptions::default()) + .unwrap(); + zip.write_all(text.as_bytes()).unwrap(); + } + zip.finish().unwrap(); + path + }; + let meta = r#"{"pack": {"description": "Look", "min_format": 75, "max_format": 75}}"#; + assert_eq!(check_resource_pack(&pack(&[("pack.mcmeta", meta)])), Ok(())); + let e = check_resource_pack(&pack(&[("assets/x.png", "")])).unwrap_err(); + assert!(e.contains("missing"), "{e}"); + let e = check_resource_pack(&pack(&[("pack.mcmeta", "{}")])).unwrap_err(); + assert!(e.contains("\"pack\""), "{e}"); + let e = check_resource_pack(&pack(&[("pack.mcmeta", meta), ("../x", "")])).unwrap_err(); + assert!(e.contains("plain path"), "{e}"); + std::fs::remove_dir_all(&dir).unwrap(); +} diff --git a/crates/horizon-mods/src/lib.rs b/crates/horizon-mods/src/lib.rs index 5060bf747..17a6c5aa0 100644 --- a/crates/horizon-mods/src/lib.rs +++ b/crates/horizon-mods/src/lib.rs @@ -6,7 +6,10 @@ //! 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; +#[cfg(feature = "index")] +pub mod index; pub mod manifest; +pub mod modpack; #[cfg(feature = "runtime")] mod runtime; diff --git a/crates/horizon-mods/src/manifest.rs b/crates/horizon-mods/src/manifest.rs index a71210883..3a93b2512 100644 --- a/crates/horizon-mods/src/manifest.rs +++ b/crates/horizon-mods/src/manifest.rs @@ -1,5 +1,5 @@ //! `horizon-mod.toml`, a mod's manifest, and the `.hmod` file around it. -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use std::{ fmt, io::Read, @@ -20,6 +20,9 @@ pub const APIS: [&str; 2] = [API, "0.2"]; /// Namespaces no mod may claim: vanilla's, the convention tags' (mods add /// to those, docs/mods.md) and the game's own. const RESERVED_NAMESPACES: [&str; 3] = ["minecraft", "c", "horizon"]; +/// A mod's data (capability `storage`), in `horizon-mods/` beside the mods +/// (`find` never mistakes it for a mod: it has no manifest). +pub const STORAGE_DIR: &str = ".horizon-mod-data"; /// The biggest `mod.wasm` a mod may ship. const MAX_WASM: u64 = 32 << 20; @@ -48,7 +51,7 @@ pub struct Manifest { pub namespaces: Vec, } -#[derive(Clone, Copy, Debug, Default, Deserialize, PartialEq)] +#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq)] #[serde(rename_all = "lowercase")] pub enum Side { /// Runs in the player's game only (HUD, keys, chat commands). @@ -62,7 +65,7 @@ pub enum Side { /// What a mod may do beyond writing its log. Each is a WIT interface the /// component imports; the player sees them before turning the mod on. -#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord)] #[serde(rename_all = "kebab-case")] pub enum Capability { GameRead, diff --git a/crates/horizon-mods/src/modpack.rs b/crates/horizon-mods/src/modpack.rs new file mode 100644 index 000000000..83c9eef5c --- /dev/null +++ b/crates/horizon-mods/src/modpack.rs @@ -0,0 +1,299 @@ +//! `.hpack`, a Horizon modpack (docs/api/mod-index.md): registry mods +//! played together as a profile. A zip holding `horizon-modpack.toml`, which +//! names each mod by id, version and SHA-256 (the files themselves come from +//! the registry), and optionally `overrides/`: settings and resource packs +//! copied into the profile's game folder. +use crate::manifest::STORAGE_DIR; +use serde::Deserialize; +use std::{ + io::Read, + path::{Path, PathBuf}, +}; + +/// The manifest's file name in a `.hpack`. +pub const MANIFEST: &str = "horizon-modpack.toml"; +/// The manifest formats this build reads. +pub const FORMAT: u32 = 1; +const OVERRIDES: &str = "overrides/"; +/// Bytes per override file and in all, unpacked. +const MAX_OVERRIDE: u64 = 64 << 20; +const MAX_OVERRIDES: u64 = 256 << 20; +/// The settings files an override may replace. +const SETTINGS: [&str; 3] = ["options.txt", "horizon-graphics.txt", "horizon-extras.txt"]; + +#[derive(Clone, Debug, Deserialize, PartialEq)] +#[serde(deny_unknown_fields)] +pub struct Manifest { + pub format: u32, + /// `[a-z0-9_]{1,64}`: its profile's folder. + pub id: String, + pub name: String, + pub version: String, + #[serde(default)] + pub description: String, + #[serde(default)] + pub authors: Vec, + /// The Minecraft version it is for. + pub game: String, + #[serde(default)] + pub mods: Vec, +} + +#[derive(Clone, Debug, Deserialize, PartialEq)] +#[serde(deny_unknown_fields)] +pub struct PackMod { + pub id: String, + pub version: String, + pub sha256: String, + /// Turned on in the profile (`horizon-mods.txt`). + #[serde(default = "on")] + pub on: bool, +} + +fn on() -> bool { + true +} + +#[cfg(feature = "index")] +impl PackMod { + pub fn requirement(&self) -> crate::index::Requirement { + crate::index::Requirement { + id: self.id.clone(), + version: self.version.clone(), + sha256: self.sha256.clone(), + } + } +} + +impl Manifest { + pub fn parse(text: &str) -> Result { + let m: Manifest = toml::from_str(text).map_err(|e| format!("{MANIFEST}: {e}"))?; + if m.format != FORMAT { + return Err(format!( + "{MANIFEST}: format {}; this Horizon reads {FORMAT}", + m.format + )); + } + let id_ok = |id: &str| { + (1..=64).contains(&id.len()) + && id + .bytes() + .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'_') + }; + if !id_ok(&m.id) { + return Err(format!( + "{MANIFEST}: id {:?} is not [a-z0-9_]{{1,64}}", + m.id + )); + } + for (i, pm) in m.mods.iter().enumerate() { + let hex = pm.sha256.len() == 64 + && pm + .sha256 + .bytes() + .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)); + if !id_ok(&pm.id) || !hex || pm.version.is_empty() { + return Err(format!( + "{MANIFEST}: mod {:?} needs an id, a version and a lowercase hex sha256", + pm.id + )); + } + if m.mods[..i].iter().any(|other| other.id == pm.id) { + return Err(format!("{MANIFEST}: mod {} is listed twice", pm.id)); + } + } + Ok(m) + } +} + +/// A `.hpack` as read from disk: its manifest and its override files, by +/// path under `overrides/`. +#[derive(Debug)] +pub struct Modpack { + pub manifest: Manifest, + pub overrides: Vec, +} + +impl Modpack { + pub fn read(path: &Path) -> Result { + let file = std::fs::File::open(path).map_err(|e| format!("{}: {e}", path.display()))?; + let mut zip = zip::ZipArchive::new(file).map_err(|e| e.to_string())?; + let mut text = String::new(); + zip.by_name(MANIFEST) + .map_err(|_| format!("{MANIFEST}: missing"))? + .take(1 << 20) + .read_to_string(&mut text) + .map_err(|e| format!("{MANIFEST}: {e}"))?; + let manifest = Manifest::parse(&text)?; + let (mut overrides, mut total) = (Vec::new(), 0u64); + 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 == MANIFEST || entry.is_dir() { + continue; + } + let Some(rest) = name.strip_prefix(OVERRIDES) else { + return Err(format!( + "{name}: a modpack holds only {MANIFEST} and overrides/" + )); + }; + check_override(rest, &manifest)?; + if entry.size() > MAX_OVERRIDE { + return Err(format!("{name} is over {} MiB", MAX_OVERRIDE >> 20)); + } + total += entry.size(); + overrides.push(rest.to_owned()); + } + if total > MAX_OVERRIDES { + return Err(format!("overrides are over {} MiB", MAX_OVERRIDES >> 20)); + } + overrides.sort(); + Ok(Modpack { + manifest, + overrides, + }) + } + + /// Writes the overrides of the pack at `path` into the game folder + /// `home`, each checked again. + pub fn extract_overrides(path: &Path, home: &Path) -> Result, String> { + let pack = Modpack::read(path)?; + let file = std::fs::File::open(path).map_err(|e| format!("{}: {e}", path.display()))?; + let mut zip = zip::ZipArchive::new(file).map_err(|e| e.to_string())?; + let mut written = Vec::new(); + for name in &pack.overrides { + let mut entry = zip + .by_name(&format!("{OVERRIDES}{name}")) + .map_err(|e| format!("{name}: {e}"))?; + let target = home.join(name); + if let Some(dir) = target.parent() { + std::fs::create_dir_all(dir).map_err(|e| format!("{}: {e}", dir.display()))?; + } + let mut out = + std::fs::File::create(&target).map_err(|e| format!("{}: {e}", target.display()))?; + std::io::copy(&mut (&mut entry).take(MAX_OVERRIDE), &mut out) + .map_err(|e| format!("{}: {e}", target.display()))?; + written.push(target); + } + Ok(written) + } +} + +/// An override's path under `overrides/`: a settings file, a resource pack +/// zip, or the stored settings of one of the pack's mods. Plain parts only. +fn check_override(path: &str, manifest: &Manifest) -> Result<(), String> { + let bad = || { + format!( + "overrides/{path}: only {}, resourcepacks/.zip and horizon-mods/{STORAGE_DIR}// may be overridden", + SETTINGS.join(", ") + ) + }; + let parts: Vec<&str> = path.split('/').collect(); + let plain = |p: &&str| { + !p.is_empty() + && !p.starts_with('.') + && p.bytes() + .all(|b| b.is_ascii_alphanumeric() || b"._-+ ".contains(&b)) + }; + let ok = match parts.as_slice() { + [file] => SETTINGS.contains(file), + ["resourcepacks", name] => plain(name) && name.ends_with(".zip"), + ["horizon-mods", dir, id, rest @ ..] => { + *dir == STORAGE_DIR + && manifest.mods.iter().any(|m| m.id == *id) + && !rest.is_empty() + && rest.iter().all(plain) + } + _ => false, + }; + if ok { Ok(()) } else { Err(bad()) } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::io::Write; + + const PACK: &str = r#" +format = 1 +id = "cozy" +name = "Cozy" +version = "1.0.0" +game = "26.3" +[[mods]] +id = "coords_hud" +version = "0.1.0" +sha256 = "0000000000000000000000000000000000000000000000000000000000000000" +"#; + + fn write_pack(dir: &Path, manifest: &str, files: &[(&str, &[u8])]) -> PathBuf { + let path = dir.join("cozy.hpack"); + let mut zip = zip::ZipWriter::new(std::fs::File::create(&path).unwrap()); + let options = zip::write::SimpleFileOptions::default(); + zip.start_file(MANIFEST, options).unwrap(); + zip.write_all(manifest.as_bytes()).unwrap(); + for (name, bytes) in files { + zip.start_file(*name, options).unwrap(); + zip.write_all(bytes).unwrap(); + } + zip.finish().unwrap(); + path + } + + fn scratch(name: &str) -> PathBuf { + let dir = std::env::temp_dir().join(format!("horizon-hpack-{name}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + dir + } + + #[test] + fn reads_a_pack_and_writes_its_overrides() { + let dir = scratch("read"); + let storage = format!("overrides/horizon-mods/{STORAGE_DIR}/coords_hud/corner.txt"); + let path = write_pack( + &dir, + PACK, + &[ + ("overrides/options.txt", b"renderDistance:8\n"), + ("overrides/resourcepacks/Cozy Look.zip", b"zip"), + (&storage, b"top-left"), + ], + ); + let pack = Modpack::read(&path).unwrap(); + assert_eq!(pack.manifest.mods[0].id, "coords_hud"); + assert!(pack.manifest.mods[0].on); + assert_eq!(pack.overrides.len(), 3); + let home = dir.join("home"); + Modpack::extract_overrides(&path, &home).unwrap(); + let options = std::fs::read_to_string(home.join("options.txt")).unwrap(); + assert_eq!(options, "renderDistance:8\n"); + let corner = home.join(format!("horizon-mods/{STORAGE_DIR}/coords_hud/corner.txt")); + assert!(corner.is_file()); + std::fs::remove_dir_all(&dir).unwrap(); + } + + #[test] + fn refuses_what_a_pack_may_not_hold() { + let dir = scratch("refuse"); + for name in [ + "mods/evil.jar", + "overrides/../../.bashrc", + "overrides/horizon-mods/evil.hmod", + "overrides/resourcepacks/../options.txt", + "overrides/servers.dat", + "overrides/horizon-mods/.horizon-mod-data/other_mod/x", + "overrides/resourcepacks/.hidden.zip", + ] { + let path = write_pack(&dir, PACK, &[(name, b"x")]); + assert!(Modpack::read(&path).is_err(), "{name} was accepted"); + } + let twice = format!("{PACK}{}", &PACK[PACK.find("[[mods]]").unwrap()..]); + assert!(Manifest::parse(&twice).unwrap_err().contains("twice")); + let newer = PACK.replace("format = 1", "format = 2"); + assert!(Manifest::parse(&newer).unwrap_err().contains("format 2")); + let short = PACK.replace("0000000000000000", "00"); + assert!(Manifest::parse(&short).is_err()); + std::fs::remove_dir_all(&dir).unwrap(); + } +} diff --git a/crates/horizon-mods/src/runtime.rs b/crates/horizon-mods/src/runtime.rs index 9dd2eb54e..52da641b4 100644 --- a/crates/horizon-mods/src/runtime.rs +++ b/crates/horizon-mods/src/runtime.rs @@ -49,9 +49,6 @@ const STORAGE_MAX_KEYS: usize = 64; /// a cheap-in-fuel loop replacing one key with large payloads needs its own /// cap, separate from the on-disk quota it would otherwise stay under. const STORAGE_WRITE_BUDGET: u64 = 1 << 20; -/// A mod's data, beside its `horizon-mods` folder (`find`'s `path.parent()` -/// never has a manifest there, so `find` never mistakes it for a mod). -const STORAGE_DIR: &str = ".horizon-mod-data"; /// What mods may read of the game, copied once per tick by the game. #[derive(Clone, Debug, Default)] @@ -237,7 +234,7 @@ impl Mods { let data_dir = path .parent() .unwrap_or(Path::new(".")) - .join(STORAGE_DIR) + .join(crate::manifest::STORAGE_DIR) .join(&m.id); let state = State { name: m.name.clone(), diff --git a/crates/horizon-mods/tests/index/coords_hud-0.1.0.hmod b/crates/horizon-mods/tests/index/coords_hud-0.1.0.hmod new file mode 100644 index 000000000..1a4a5ba35 Binary files /dev/null and b/crates/horizon-mods/tests/index/coords_hud-0.1.0.hmod differ diff --git a/crates/horizon-mods/tests/index/coords_hud-0.1.0.hmod.minisig b/crates/horizon-mods/tests/index/coords_hud-0.1.0.hmod.minisig new file mode 100644 index 000000000..5f9b17534 --- /dev/null +++ b/crates/horizon-mods/tests/index/coords_hud-0.1.0.hmod.minisig @@ -0,0 +1,4 @@ +untrusted comment: signature from minisign secret key +RUTVtuGb/GRXcIa5yCyCHFGFt55tj3T/FLUDv3ecNLVNNiQqQu7xyBrfc3wsvMHmudsEq6yr+QF21XN3+hnr/Ged3ULr5rv3zgE= +trusted comment: coords_hud 0.1.0 +IESQkJdTzLOvWegcGFxOMrQbnwJp2OKSSxoCKAxdfwyRb+9atdF+Q5/OpbdYurMnrNN1X1ybb43AszPVecdiCw== diff --git a/crates/horizon-mods/tests/index/cozy-1.0.0.hpack b/crates/horizon-mods/tests/index/cozy-1.0.0.hpack new file mode 100644 index 000000000..b25663480 Binary files /dev/null and b/crates/horizon-mods/tests/index/cozy-1.0.0.hpack differ diff --git a/crates/horizon-mods/tests/index/index-v1.json b/crates/horizon-mods/tests/index/index-v1.json new file mode 100644 index 000000000..215c94667 --- /dev/null +++ b/crates/horizon-mods/tests/index/index-v1.json @@ -0,0 +1,109 @@ +{ + "schema": 1, + "generated": "2026-10-24T18:00:00Z", + "projects": [ + { + "id": "coords_hud", + "kind": "mod", + "name": "Coordinates HUD", + "summary": "Your coordinates, facing and the time of day in a corner of the screen.", + "description": "`/where` puts them in chat, `/coords` hides or shows the panel.", + "authors": [ + "Horizon" + ], + "owners": [ + "ludovic111" + ], + "license": "MIT", + "source": "https://github.com/ludovic111/horizon/tree/main/crates/horizon-mod-sdk/examples/coords-hud", + "icon": "https://ludovic111.github.io/horizon-mods/icons/coords_hud.png", + "key": "RWTVtuGb/GRXcBf3Re5hzM07EzoStr2sLTJjghJia0xX15ULoZRFFZDb", + "versions": [ + { + "version": "0.1.0", + "published": "2026-10-24", + "api": "0.1", + "side": "client", + "capabilities": [ + "game-read", + "hud", + "chat" + ], + "game": "26.3", + "file": { + "name": "coords_hud-0.1.0.hmod", + "url": "https://github.com/ludovic111/horizon-mods/releases/download/coords_hud-0.1.0/coords_hud-0.1.0.hmod", + "size": 438, + "sha256": "e1135345692f6dd8fd4c51920151944b82333e17049c9bb7d756f27b8deefc53", + "signature": "untrusted comment: signature from minisign secret key\nRUTVtuGb/GRXcIa5yCyCHFGFt55tj3T/FLUDv3ecNLVNNiQqQu7xyBrfc3wsvMHmudsEq6yr+QF21XN3+hnr/Ged3ULr5rv3zgE=\ntrusted comment: coords_hud 0.1.0\nIESQkJdTzLOvWegcGFxOMrQbnwJp2OKSSxoCKAxdfwyRb+9atdF+Q5/OpbdYurMnrNN1X1ybb43AszPVecdiCw==\n" + } + } + ] + }, + { + "id": "cozy", + "kind": "modpack", + "name": "Cozy", + "summary": "Coordinates and a calm render distance.", + "authors": [ + "Horizon" + ], + "owners": [ + "ludovic111" + ], + "license": "MIT", + "source": "https://github.com/ludovic111/horizon-mods", + "versions": [ + { + "version": "1.0.0", + "published": "2026-10-24", + "api": "1", + "game": "26.3", + "requires": [ + { + "id": "coords_hud", + "version": "0.1.0", + "sha256": "e1135345692f6dd8fd4c51920151944b82333e17049c9bb7d756f27b8deefc53" + } + ], + "file": { + "name": "cozy-1.0.0.hpack", + "url": "https://github.com/ludovic111/horizon-mods/releases/download/cozy-1.0.0/cozy-1.0.0.hpack", + "size": 471, + "sha256": "241ac840f9c5ea3f6252d849da10c90d36cb1da4d6cd8d1d268c2760a353cd65" + } + } + ] + }, + { + "id": "skyblock_start", + "kind": "world", + "name": "Skyblock start", + "summary": "A kind this reader doesn't know yet: kept, not offered.", + "license": "CC0-1.0", + "source": "https://example.org/skyblock", + "future_field": true, + "versions": [ + { + "version": "1", + "api": "1", + "game": "26.3", + "file": { + "name": "skyblock.hworld", + "url": "https://example.org/skyblock.hworld", + "size": 1, + "sha256": "0000000000000000000000000000000000000000000000000000000000000000" + } + } + ] + } + ], + "revoked": [ + { + "id": "bad_mod", + "sha256": "1111111111111111111111111111111111111111111111111111111111111111", + "reason": "Sent chat messages it didn't declare.", + "date": "2026-10-24" + } + ] +} diff --git a/crates/horizon-mods/tests/index/test.pub b/crates/horizon-mods/tests/index/test.pub new file mode 100644 index 000000000..5cb41bd62 --- /dev/null +++ b/crates/horizon-mods/tests/index/test.pub @@ -0,0 +1,2 @@ +untrusted comment: minisign public key 705764FC9BE1B6D5 +RWTVtuGb/GRXcBf3Re5hzM07EzoStr2sLTJjghJia0xX15ULoZRFFZDb diff --git a/docs/api/mod-index.md b/docs/api/mod-index.md new file mode 100644 index 000000000..959b969fb --- /dev/null +++ b/docs/api/mod-index.md @@ -0,0 +1,95 @@ +# The registry index and `.hpack` + +Horizon's registry lists what is made for Horizon and reviewed: mods (`.hmod`), modpacks (`.hpack`), resource packs +and, once their format exists, Horizon shader packs. Submissions are pull requests in +[ludovic111/horizon-mods](https://github.com/ludovic111/horizon-mods); merging one publishes it. The launcher and the +website read one file, `index.json`, which that repository's CI builds. The Rust types and every check below are +`horizon_mods::index` and `horizon_mods::modpack` (feature `index`); the sample is +`crates/horizon-mods/tests/index/index-v1.json`, read by the tests with the files it lists. + +Level: preview ([stability.md](stability.md)). Schema 1. + +## `index.json` + +```json +{ + "schema": 1, + "generated": "2026-10-24T18:00:00Z", + "projects": [{ + "id": "coords_hud", "kind": "mod", "name": "Coordinates HUD", "summary": "One line.", + "description": "Markdown.", "authors": ["Horizon"], "owners": ["ludovic111"], + "license": "MIT", "source": "https://github.com/...", "icon": "https://...png", "key": "RWT...", + "versions": [{ + "version": "0.1.0", "published": "2026-10-24", "changelog": "", + "api": "0.1", "side": "client", "capabilities": ["game-read", "hud", "chat"], "game": "26.3", + "file": { "name": "coords_hud-0.1.0.hmod", "url": "https://github.com/ludovic111/horizon-mods/releases/download/...", + "size": 438, "sha256": "e113...", "signature": "untrusted comment: ...\n...\ntrusted comment: ...\n...\n" } + }] + }], + "revoked": [{ "id": "bad_mod", "sha256": "1111...", "reason": "Sent chat messages it didn't declare.", "date": "2026-10-24" }] +} +``` + +| Field | Meaning | +| --- | --- | +| `schema` | 1. A reader refuses a higher number with "update Horizon" rather than misread it. | +| `projects[].id` | `[a-z0-9_]{1,64}`, unique; a mod's is its manifest's `id`. | +| `kind` | `mod`, `modpack`, `resourcepack`, `shader` (Horizon's own shader packs, not Iris or OptiFine packs). Other kinds (data packs, worlds, themes) may come without a new schema: a reader keeps a kind it doesn't know and doesn't offer it. | +| `summary`, `description` | One line for lists; Markdown for the project's page. | +| `owners` | GitHub logins allowed to publish updates. | +| `license`, `source` | An SPDX expression and an `https://` address of the source code: both required. | +| `icon` | `https://`, optional. | +| `key` | The author's [minisign](https://jedisct1.github.io/minisign/) public key. When set, every version must be signed with it; an update signed with another key is refused. Absent: the project is unsigned and the launcher says so. | +| `versions` | Newest first, at least one. | +| `api` | A mod's API line (its manifest's `api`); a modpack's manifest format (`1`). | +| `side`, `capabilities` | A mod's, read from its manifest by CI, never typed by hand. | +| `game` | The Minecraft version it is for. | +| `requires` | A modpack's mods: `{ id, version, sha256 }`, each a version in this index with that hash. | +| `file` | A plain name with the kind's extension (`.hmod`, `.hpack`, `.zip`), an `https://` URL, the size (at most 64 MiB), the lowercase hex SHA-256, and the `.minisig` text when the project has a key. | +| `revoked` | Files taken down: one file by `sha256`, or every version of `id` without it. The launcher refuses to install them and turns installed copies off with the reason. | + +Readers ignore fields they don't know, so an optional field can be added to schema 1. Renaming or removing a field, or +changing what one means, makes schema 2. + +## Before a file is installed + +`index::check_file` runs on the downloaded file, before it reaches the game folder: + +1. Its size and SHA-256 are the ones the entry lists, and it isn't revoked. +2. With a project key: its signature verifies with that key (minisign's prehashed and legacy signatures both). +3. What the file says matches the entry: a mod's `horizon-mod.toml` (id, version, API, side, capabilities) and its + content's fence ([mods.md](../mods.md#content-mods-api-02)); a modpack's manifest (id, version, format, game, + mods); a resource pack's `pack.mcmeta`. +4. A kind this build can't install is refused with "this Horizon can't install a … yet". + +The game still checks a mod's component imports against its capabilities when it starts it, as for any mod. + +## `.hpack` + +A zip holding `horizon-modpack.toml` and, optionally, `overrides/`. The mods are not inside: the launcher downloads +each from the index and checks it as above. + +```toml +format = 1 +id = "cozy" # [a-z0-9_]{1,64}: the profile's folder +name = "Cozy" +version = "1.0.0" +description = "Coordinates and a calm render distance." +authors = ["Horizon"] +game = "26.3" + +[[mods]] +id = "coords_hud" +version = "0.1.0" +sha256 = "e1135345692f6dd8fd4c51920151944b82333e17049c9bb7d756f27b8deefc53" +on = true # optional, default true: turned on in the profile's horizon-mods.txt +``` + +Unknown keys are refused. `overrides/` may hold only `options.txt`, `horizon-graphics.txt`, `horizon-extras.txt`, +`resourcepacks/.zip` and `horizon-mods/.horizon-mod-data//...` (a mod's own saved settings, +capability `storage`), each at most 64 MiB and 256 MiB in all; anything else refuses the pack. The launcher copies +them into the profile's game folder. + +## Changelog + +- Schema 1 (2026-10-24): kinds `mod`, `modpack`, `resourcepack`, `shader`; `.hpack` format 1. diff --git a/docs/mods.md b/docs/mods.md index 94abfd636..6f3216d38 100644 --- a/docs/mods.md +++ b/docs/mods.md @@ -19,7 +19,8 @@ folder, loads no runtime and plays exactly like vanilla 26.3. | Authoring tool | `crates/cargo-horizon-mod` | `cargo horizon-mod new / build / run` | | In the game | `crates/horizon-client/src/game/mods.rs`, Options > Mods | client mods | | Launcher mod browser (Modrinth, CurseForge with a key) | [mod-browser.md](mod-browser.md) | resource packs, modpacks | -| `.hmod` index, signing, server mods, registries | this page, [Roadmap](#roadmap) | planned | +| Registry index (`index.json`, `.hpack`), signatures | `horizon_mods::index`, [api/mod-index.md](api/mod-index.md) | schema 1; launcher and website next | +| Server mods, registries | this page, [Roadmap](#roadmap) | planned | ## Design goals diff --git a/launcher/src/backend.rs b/launcher/src/backend.rs index d12dcd13c..efdfa2415 100644 --- a/launcher/src/backend.rs +++ b/launcher/src/backend.rs @@ -2170,6 +2170,8 @@ pub enum Msg { Links(Links), Progress(String, f32), Installed(String, Result<(), String>), + /// A version's files are gone (or could not be deleted). + Removed(String, Result<(), String>), Line(Job, String), Exit(Job, Result<(), String>), Updates(Result), diff --git a/launcher/src/main.rs b/launcher/src/main.rs index 4a68ecdef..0bcbc4011 100644 --- a/launcher/src/main.rs +++ b/launcher/src/main.rs @@ -144,6 +144,9 @@ struct Launcher { releases: Load>, installed: Vec, installing: Option<(String, f32)>, + /// The version being deleted on a background thread. It is already out of `installed`, so + /// nothing can launch, select or prepare it, and its row can't install or remove it again. + removing: Option, install_error: Option<(String, String)>, confirm_remove: Option, picker_open: bool, @@ -286,6 +289,7 @@ impl Launcher { releases: Load::Loading, installed, installing: None, + removing: None, install_error: None, confirm_remove: None, picker_open: false, @@ -415,7 +419,7 @@ impl Launcher { let Some(release) = self.updates.horizon.clone() else { return; }; - if self.installing.is_some() { + if self.installing.is_some() || self.removing.as_ref() == Some(&release.tag) { return; } let select = @@ -542,6 +546,17 @@ impl Launcher { Err(e) => self.install_error = Some((tag, e)), } } + Msg::Removed(tag, result) => { + if self.removing.as_ref() == Some(&tag) { + self.removing = None; + } + self.installed = backend::installed(); + self.settings.selected = + selection_after_removal(&self.installed, Settings::load().selected); + if let Err(e) = result { + self.install_error = Some((tag, e)); + } + } Msg::Updates(Ok(updates)) => { self.update_error = None; self.updates = updates; @@ -706,7 +721,7 @@ impl Launcher { } fn install(&mut self, release: &Release) { - if release.asset.is_none() { + if release.asset.is_none() || self.removing.as_ref() == Some(&release.tag) { return; } let (release, tx) = (release.clone(), self.tx.clone()); @@ -724,15 +739,26 @@ impl Launcher { }); } + /// Deletes an installed version on a background thread: a version is thousands of files and + /// the window must keep drawing meanwhile. `Msg::Removed` ends it. fn remove(&mut self, tag: &str) { - if let Err(e) = backend::remove_version(tag) { - self.install_error = Some((tag.to_string(), e)); - } self.confirm_remove = None; - self.installed = backend::installed(); - self.settings.selected = Settings::load() - .selected - .or_else(|| self.installed.first().cloned()); + if self.removing.is_some() + || self.installing.as_ref().is_some_and(|(t, _)| t == tag) + || !self.installed.iter().any(|t| t == tag) + { + return; + } + self.install_error = None; + self.removing = Some(tag.to_string()); + self.installed.retain(|t| t != tag); + self.settings.selected = + selection_after_removal(&self.installed, self.settings.selected.take()); + let (tag, tx) = (tag.to_string(), self.tx.clone()); + std::thread::spawn(move || { + let result = backend::remove_version(&tag); + let _ = tx.send(Msg::Removed(tag, result)); + }); } fn prepare(&mut self) { @@ -1158,6 +1184,14 @@ fn segments( })) } +/// The selected version once one has been removed: the saved choice if it is still installed, +/// else the newest left. +fn selection_after_removal(installed: &[String], saved: Option) -> Option { + saved + .filter(|s| installed.contains(s)) + .or_else(|| installed.first().cloned()) +} + fn disabled(b: Stateful
) -> Stateful
{ b.opacity(theme::DISABLED).cursor_default().tab_stop(false) } @@ -2923,6 +2957,7 @@ impl Launcher { self.installing.as_ref().is_some_and(|(t, _)| t == tag) || self.install_error.as_ref().is_some_and(|(t, _)| t == tag) || self.confirm_remove.as_ref() == Some(tag) + || self.removing.as_ref() == Some(tag) }; let list = self.long_list.clone().column( &self.content_scroll.handle, @@ -2959,6 +2994,15 @@ impl Launcher { .child(format!("{:.0}%", p * 100.)), ) .into_any_element() + } else if self.removing.as_ref() == Some(&tag) { + div() + .flex() + .items_center() + .gap(px(10.)) + .w(px(180.)) + .child(progress_bar(None, &t)) + .child(text(theme::MICRO).text_color(t.muted).child("Removing…")) + .into_any_element() } else if installed { let confirming = self.confirm_remove.as_ref() == Some(&tag); let (t1, t2) = (tag.clone(), tag.clone()); @@ -3587,6 +3631,20 @@ mod tests { assert!(hit.iter().any(|b| b.action().partial_eq(&CloseWindow))); } + #[test] + fn the_selection_survives_a_removal_or_moves_to_the_newest_left() { + let left = ["v0.2.49".to_string(), "v0.2.48".to_string()]; + let pick = + |saved: Option<&str>| super::selection_after_removal(&left, saved.map(String::from)); + assert_eq!(pick(Some("v0.2.48")).as_deref(), Some("v0.2.48")); + assert_eq!(pick(Some("v0.2.47")).as_deref(), Some("v0.2.49")); + assert_eq!(pick(None).as_deref(), Some("v0.2.49")); + assert_eq!( + super::selection_after_removal(&[], Some("v0.2.47".into())), + None + ); + } + #[test] fn the_app_menu_has_quit() { let menus = super::menus(); diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index cf6bc19af..e44931cb1 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -230,6 +230,9 @@ address. The script then checks, from both sides, what a guest's game used to ge - villagers: the guest sees each villager's and zombie villager's type, profession and level as the host has them, and an unemployed villager that takes a composter becomes a farmer on the guest too (the guest used to draw every villager as an unemployed plains one). +- falling: an item and a falling block the host summons high up fall smoothly on the guest, a + little further every tick between the server's updates (every 20 ticks), as their own + client tick moves them (they used to hang, then jump down once a second, #1568). Each check is reported on its own (a failing one doesn't stop the others) and the screenshots of both sides go to --out (host-*.png, guest-*.png). @@ -5605,5 +5608,46 @@ CHECKS += ['shulker_bullets'] CHECK_FNS.update(shulker_bullets=check_shulker_bullets) +def check_falling(host, guest, out): + """ItemEntity and FallingBlockEntity tick on a client too (gravity, collisions, drag), and their + position packets snap them (Entity's NO_OP InterpolationHandler). The server sends an item's + and a falling block's position every 20 ticks, so between two updates the guest's own tick is + what moves them: sampled every 2 ticks over the first 16, both are lower nearly every time.""" + x, y, z = (int(v // 1) for v in pos_of(guest)) + top = y + 30 + tag = 'falling_e2e' + host('command', command=f'fill {x + 2} {y} {z - 1} {x + 4} {top + 2} {z + 1} minecraft:air') + host('command', command=f'summon minecraft:item {x + 3.5} {top} {z + 0.5} ' + f'{{Tags:["{tag}"],Item:{{id:"minecraft:diamond",count:1}},PickupDelay:32767}}') + host('command', command=f'summon minecraft:falling_block {x + 2.5} {top} {z - 0.5} ' + f'{{Tags:["{tag}"],BlockState:{{Name:"minecraft:sand"}},DropItem:0b}}') + heights = {'item': [], 'falling block': []} + try: + for _ in range(8): + items = [e['pos'][1] for e in guest.raw('entities', 48, 'minecraft:item')['entities'] + if abs(e['pos'][0] - x - 3.5) < 0.5 and abs(e['pos'][2] - z - 0.5) < 0.5] + blocks = [o['position'][1] for o in guest('status')['projectiles'] + if o.get('kind') == 'minecraft:falling_block' + and abs(o['position'][0] - x - 2.5) < 0.5 and abs(o['position'][2] - z + 0.5) < 0.5] + heights['item'].append(items[0] if items else None) + heights['falling block'].append(blocks[0] if blocks else None) + guest.wait(2) + guest.shot(out, 'falling') + finally: + host('command', command=f'kill @e[tag={tag}]') + host('command', command=f'fill {x + 2} {y} {z - 1} {x + 4} {top + 2} {z + 1} minecraft:air') + for name, ys in heights.items(): + seen = [h for h in ys if h is not None] + assert len(seen) >= 6, f'the guest saw the {name} {len(seen)} times of 8: {heights}' + drops = sum(b < a for a, b in zip(seen, seen[1:])) + # A tick or two can pass between two samples with no move; never most of them. + assert drops >= len(seen) - 2, f'the {name} fell in {drops} of {len(seen) - 1} samples: {heights}' + return heights + + +CHECKS += ['falling'] +CHECK_FNS.update(falling=check_falling) + + if __name__ == '__main__': main()