From 7a1638a1908f5183102b7f649a79c4d95db8fe7c Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 24 Oct 2026 16:12:55 +0200 Subject: [PATCH 001/107] Renderer: remove the section face groups modelled on another renderer #503 grouped each section's solid quads by the axis side they face and skipped the groups facing away from the eye. Its code and PR named an internal class of another project as the model. Horizon doesn't build on another project's code, so the grouping goes and every section draws its solid quads in one draw, as vanilla does; the GPU's back-face culling discards the back faces, so the picture is unchanged. This costs frame time, measured on the Mac mini in the PR. A clean-room version can come back later, designed from vanilla and from first principles only. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 32 +-- crates/horizon-client/src/lod.rs | 4 - crates/horizon-client/src/lod/mesh.rs | 4 +- crates/horizon-client/src/mesher.rs | 11 - crates/horizon-client/src/mesher/facing.rs | 267 ------------------ .../src/mesher/leaf_culling_tests.rs | 2 +- 6 files changed, 15 insertions(+), 305 deletions(-) delete mode 100644 crates/horizon-client/src/mesher/facing.rs diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 59a3b5d03..b1143be6c 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -177,8 +177,6 @@ struct SectionGpu { start: u64, solid: u32, translucent: u32, - /// The solid quads by facing: back-facing groups are skipped. - faces: crate::mesher::FaceGroups, sort: translucency::SortState, sorted_indices: Option<(u32, u64)>, /// Its id for flat draws (`flat::Flat::add`), when they are on. @@ -2751,7 +2749,6 @@ impl Renderer { start, solid, translucent, - faces: mesh.faces, sort: translucency::SortState::new( mesh.translucent .as_chunks::<4>() @@ -3621,10 +3618,6 @@ impl Renderer { // as one list of indirect draws over the section arena. self.draws.clear(); self.draw_pages.clear(); - // The eye, camera-relative: view bobbing moves it off the camera. - let eye = (frame.bob * crate::camera::view_matrix(frame.yaw, frame.pitch)) - .inverse() - .transform_point3(Vec3::ZERO); // A Java pack that draws the terrain (`gfx/java_terrain.rs`): the // world pass leaves it out, and occlusion culling tests against the // pack's depth (`Cull::test_gl`). @@ -3655,14 +3648,11 @@ impl Renderer { }; let start = draws.len(); let instances = u32::from(first_phase); - let mut indices = 0; - // Only the solid groups that can face the eye. - s.faces.ranges(eye - Vec3::from(v.origin), |first, quads| { - let first = s.start + u64::from(first) * 4; - draws.push([quads * 6, instances, 0, first as u32, i as u32]); + let indices = s.solid * 6; + if indices > 0 { + draws.push([indices, instances, 0, s.start as u32, i as u32]); pages.push((s.page, None)); - indices += quads * 6; - }); + } let runs = if first_phase { &mut self.solid_runs } else { @@ -3699,12 +3689,16 @@ impl Renderer { for (i, v) in visible_lods.iter().enumerate() { let s = &self.lods[&v.key].mesh; let start = self.draws.len(); - s.faces.ranges(eye - Vec3::from(v.origin), |first, quads| { - let first = s.start + u64::from(first) * 4; - self.draws - .push([quads * 6, 1, 0, first as u32, (visible.len() + i) as u32]); + if s.solid > 0 { + self.draws.push([ + s.solid * 6, + 1, + 0, + s.start as u32, + (visible.len() + i) as u32, + ]); self.draw_pages.push((s.page, None)); - }); + } self.solid_runs .push(v.distance, s.page, start, self.draws.len() - start, [0; 3]); } diff --git a/crates/horizon-client/src/lod.rs b/crates/horizon-client/src/lod.rs index f8adb1f16..ee17acee1 100644 --- a/crates/horizon-client/src/lod.rs +++ b/crates/horizon-client/src/lod.rs @@ -1727,10 +1727,6 @@ mod tests { let expected = (key.size() * key.size() - 16 * 16) as f32; assert_eq!(top_area(&mesh.solid, 70.0), expected); assert_eq!(top_area(&mesh.translucent, 80.0 - 1.0 / 9.0), expected); - assert_eq!( - mesh.faces.quads.iter().sum::() as usize, - mesh.solid.len() / 4 - ); assert!(mesh.translucent.iter().all(|v| v.light == 240)); let side = |axis: usize, plane: f32, low: f32, high: f32| { let other = if axis == 0 { 2 } else { 0 }; diff --git a/crates/horizon-client/src/lod/mesh.rs b/crates/horizon-client/src/lod/mesh.rs index 73287af50..852f45e5b 100644 --- a/crates/horizon-client/src/lod/mesh.rs +++ b/crates/horizon-client/src/lod/mesh.rs @@ -2,7 +2,7 @@ use super::{Column, Span, TileKey}; use crate::{ fast_hash::{FxHashMap, FxHashSet}, - mesher::{FaceGroups, OPAQUE_LAYER, SectionMesh, TerrainVertex}, + mesher::{OPAQUE_LAYER, SectionMesh, TerrainVertex}, streaming::WorkerContext, }; use horizon_assets::model::Direction; @@ -558,7 +558,6 @@ fn mesh_columns_with_edges( emit(&mut mesh, key, rect) } } - mesh.faces = FaceGroups::group(&mut mesh.solid); mesh } @@ -1003,7 +1002,6 @@ mod tests { } let mesh = mesh(&columns); assert_eq!(mesh.solid.len(), 6 * 4); - assert_eq!(mesh.faces.quads.iter().sum::(), 6); assert!(mesh.translucent.is_empty()); for q in mesh.solid.as_chunks::<4>().0.iter() { let center = q diff --git a/crates/horizon-client/src/mesher.rs b/crates/horizon-client/src/mesher.rs index a9e9f6823..ec6f2ae73 100644 --- a/crates/horizon-client/src/mesher.rs +++ b/crates/horizon-client/src/mesher.rs @@ -3,7 +3,6 @@ //! `ModelBlockRenderer`/`BlockModelLighter` (smooth light, ambient occlusion, //! cardinal shading, tint sources, block offsets; see `lighting`). Fluids //! follow `FluidRenderer` (see `fluid`). -mod facing; mod fluid; mod lighting; use crate::{ @@ -13,7 +12,6 @@ use crate::{ chunk::{Chunk, MIN_Y}, models::catalog::BakedFace, }; -pub use facing::{FaceGroups, GROUPS as FACE_GROUPS}; use horizon_core::{ biome::BiomeZoom, geometry::Direction, position::BlockPos, random::position_seed, }; @@ -116,8 +114,6 @@ mod vertex_tests { } pub struct SectionMesh { pub solid: Vec, - /// `solid`'s quads by the side they face (`facing`). - pub faces: FaceGroups, pub translucent: Vec, /// VisGraph: bit `a * 6 + b` is set when faces `a` and `b` (Direction /// order) are connected through non-opaque blocks. @@ -131,7 +127,6 @@ impl Default for SectionMesh { fn default() -> Self { Self { solid: Vec::new(), - faces: FaceGroups::default(), translucent: Vec::new(), visibility: ALL_VISIBLE, block_entities: Vec::new(), @@ -698,12 +693,6 @@ pub struct Mesher<'a> { impl Mesher<'_> { /// `region[dz][dx]` are the chunks around the meshed column (centre 1,1). pub fn mesh(&mut self, region: &[[Option<&Chunk>; 3]; 3], sy: i32) -> SectionMesh { - let mut out = self.tesselate(region, sy); - out.faces = facing::group(&mut out.solid); - out - } - /// The section's quads in model order, before `facing::group`. - pub(crate) fn tesselate(&mut self, region: &[[Option<&Chunk>; 3]; 3], sy: i32) -> SectionMesh { let mut out = SectionMesh::default(); let center = region[1][1].expect("centre chunk"); let section = ¢er.sections[sy as usize]; diff --git a/crates/horizon-client/src/mesher/facing.rs b/crates/horizon-client/src/mesher/facing.rs deleted file mode 100644 index 7bafe9e82..000000000 --- a/crates/horizon-client/src/mesher/facing.rs +++ /dev/null @@ -1,267 +0,0 @@ -//! Solid quads grouped by the side they face, as Sodium's `ModelQuadFacing` -//! does. A quad in the plane `x = p` facing +X is front-facing only while the -//! eye is at `x > p`, and terrain culls back faces, so when the eye is on the -//! back side of every quad of a group the renderer skips the group's draw: -//! the GPU would have discarded all of it after shading its vertices. The -//! picture is unchanged; the vertex work for those quads is gone. -use super::TerrainVertex; -use glam::Vec3; - -/// Groups in buffer order: the negative sides, quads not in an axis plane -/// (rotated model elements, sloped fluid tops), then the positive sides. -/// With the eye outside a section on every axis, one side per axis is -/// visible, so the drawn groups form one to three runs. -pub const GROUPS: usize = 7; -const UNALIGNED: usize = 3; -/// The group of a quad facing `-axis` (0 x, 1 y, 2 z) is `NEGATIVE[axis]`, of -/// one facing `+axis` `GROUPS - 1 - NEGATIVE[axis]`. -const NEGATIVE: [usize; 3] = [0, 2, 1]; -/// How far (blocks) the eye must be behind every plane of a group before -/// the group is skipped: rounding on the GPU never makes such a quad -/// front-facing. -const MARGIN: f32 = 1.0 / 16.0; - -/// The solid quads' groups, filled by `group`. -#[derive(Clone, Copy, Debug, PartialEq)] -pub struct FaceGroups { - /// Quads per group, in buffer order. - pub quads: [u32; GROUPS], - /// Per axis group, the plane nearest the side the group faces: the - /// lowest plane of a positive group and the highest of a negative one. - planes: [f32; GROUPS], -} - -impl Default for FaceGroups { - /// Everything unaligned: always drawn. - fn default() -> Self { - Self { - quads: [0; GROUPS], - planes: [0.0; GROUPS], - } - } -} - -impl FaceGroups { - /// Group arbitrary-sized terrain quads, including distant LOD tiles. - pub fn group(solid: &mut Vec) -> Self { - group(solid) - } - - /// Whether group `g` can hold a front-facing quad for an eye at `eye`, - /// relative to the section's minimum corner. - pub fn visible(&self, g: usize, eye: Vec3) -> bool { - if g == UNALIGNED { - return true; - } - let (axis, positive) = axis(g); - if positive { - eye[axis] > self.planes[g] - MARGIN - } else { - eye[axis] < self.planes[g] + MARGIN - } - } - /// The quad ranges (first quad, quad count) to draw for an eye at `eye` - /// relative to the section's minimum corner, adjacent groups merged. - pub fn ranges(&self, eye: Vec3, mut out: impl FnMut(u32, u32)) { - let mut first = 0; - let mut run: Option<(u32, u32)> = None; - for g in 0..GROUPS { - let quads = self.quads[g]; - if quads > 0 { - if self.visible(g, eye) { - run = Some(match run { - Some((start, len)) => (start, len + quads), - None => (first, quads), - }); - } else if let Some((start, len)) = run.take() { - out(start, len); - } - } - first += quads; - } - if let Some((start, len)) = run { - out(start, len); - } - } -} - -/// The axis and sign of each axis group (`UNALIGNED`'s entry is unused). -const AXES: [(usize, bool); GROUPS] = { - let mut axes = [(0, false); GROUPS]; - let mut axis = 0; - while axis < 3 { - axes[NEGATIVE[axis]] = (axis, false); - axes[GROUPS - 1 - NEGATIVE[axis]] = (axis, true); - axis += 1; - } - axes -}; - -/// The axis and sign of axis group `g`. -fn axis(g: usize) -> (usize, bool) { - debug_assert_ne!(g, UNALIGNED); - AXES[g] -} - -/// The group of one quad, and its plane: an axis group only when all four -/// corners lie exactly in one axis plane. -fn facing(quad: &[TerrainVertex]) -> (usize, f32) { - let p = |i: usize| Vec3::from(quad[i].pos); - let normal = (p(1) - p(0)).cross(p(2) - p(0)); - for axis in 0..3 { - let [a, b] = [(axis + 1) % 3, (axis + 2) % 3]; - let plane = quad[0].pos[axis]; - if normal[axis] != 0.0 - && normal[a] == 0.0 - && normal[b] == 0.0 - && quad.iter().all(|v| v.pos[axis] == plane) - { - let negative = NEGATIVE[axis]; - let g = if normal[axis] > 0.0 { - GROUPS - 1 - negative - } else { - negative - }; - return (g, plane); - } - } - (UNALIGNED, 0.0) -} - -/// Reorder `solid` by group, keeping the order of quads within a group (so -/// coplanar quads such as a grass block's overlay still draw after their -/// base), and return the groups. -pub fn group(solid: &mut Vec) -> FaceGroups { - let mut groups = FaceGroups::default(); - for (g, plane) in groups.planes.iter_mut().enumerate() { - if g != UNALIGNED { - *plane = if axis(g).1 { f32::MAX } else { f32::MIN }; - } - } - let faces: Vec = solid - .as_chunks::<4>() - .0 - .iter() - .map(|quad| { - let (g, plane) = facing(quad); - groups.quads[g] += 1; - if g != UNALIGNED { - let nearest = &mut groups.planes[g]; - *nearest = if axis(g).1 { - nearest.min(plane) - } else { - nearest.max(plane) - }; - } - g as u8 - }) - .collect(); - if faces.windows(2).all(|w| w[0] <= w[1]) { - return groups; - } - let mut next = [0usize; GROUPS]; - let mut start = 0; - for g in 0..GROUPS { - next[g] = start; - start += groups.quads[g] as usize; - } - let mut sorted = vec![solid[0]; solid.len()]; - for (quad, &g) in solid.as_chunks::<4>().0.iter().zip(&faces) { - let at = next[g as usize] * 4; - sorted[at..at + 4].copy_from_slice(quad); - next[g as usize] += 1; - } - *solid = sorted; - groups -} - -#[cfg(test)] -mod tests { - use super::*; - - fn vertex(pos: [f32; 3], layer: u16) -> TerrainVertex { - TerrainVertex::new(pos, [0.0; 2], layer, 0, 0) - } - /// A unit quad in the plane `axis = plane`, wound to face `sign`, tagged - /// with `layer` so tests can follow it. - fn quad(axis: usize, plane: f32, sign: f32, layer: u16) -> Vec { - let corner = |u: f32, v: f32| { - let mut p = [0.0; 3]; - p[axis] = plane; - p[(axis + 1) % 3] = u; - p[(axis + 2) % 3] = v; - vertex(p, layer) - }; - let mut q = vec![ - corner(0.0, 0.0), - corner(1.0, 0.0), - corner(1.0, 1.0), - corner(0.0, 1.0), - ]; - if sign < 0.0 { - q.reverse(); - } - q - } - - #[test] - fn quads_group_by_the_side_their_winding_faces() { - for a in 0..3 { - for sign in [-1.0, 1.0] { - let q = quad(a, 4.0, sign, 0); - let (g, plane) = facing(&q); - assert_eq!(axis(g), (a, sign > 0.0), "axis {a} sign {sign}"); - assert_eq!(plane, 4.0); - let normal = (Vec3::from(q[1].pos) - Vec3::from(q[0].pos)) - .cross(Vec3::from(q[2].pos) - Vec3::from(q[0].pos)); - assert_eq!(normal[a].signum(), sign); - } - } - // A cross plant's diagonal quad faces no axis. - let diagonal = [ - [0.0, 0.0, 0.0], - [1.0, 0.0, 1.0], - [1.0, 1.0, 1.0], - [0.0, 1.0, 0.0], - ] - .map(|p| vertex(p, 0)); - assert_eq!(facing(&diagonal).0, UNALIGNED); - } - - #[test] - fn grouping_is_stable_and_skips_only_back_facing_groups() { - // +X at x = 3 and x = 7 (layers 1, 2), -X at x = 5 (3), an unaligned - // quad (4), a coplanar overlay on the x = 3 quad (5). - let diagonal = [ - [0.0, 0.0, 0.0], - [1.0, 0.0, 1.0], - [1.0, 1.0, 1.0], - [0.0, 1.0, 0.0], - ] - .map(|p| vertex(p, 4)); - let mut solid = [ - quad(0, 3.0, 1.0, 1), - quad(0, 5.0, -1.0, 3), - diagonal.to_vec(), - quad(0, 7.0, 1.0, 2), - quad(0, 3.0, 1.0, 5), - ] - .concat(); - let groups = group(&mut solid); - let layers: Vec = solid.chunks(4).map(|q| q[0].layer).collect(); - assert_eq!(layers, [3, 4, 1, 2, 5]); - let ranges = |eye: Vec3| { - let mut out = Vec::new(); - groups.ranges(eye, |first, count| out.push((first, count))); - out - }; - // Between the planes: every group can face the eye. - assert_eq!(ranges(Vec3::new(4.0, 8.0, 8.0)), [(0, 5)]); - // Beyond x = 7: the -X quad at x = 5 faces away. - assert_eq!(ranges(Vec3::new(20.0, 8.0, 8.0)), [(1, 4)]); - // Before x = 3: every +X quad faces away. - assert_eq!(ranges(Vec3::new(-10.0, 8.0, 8.0)), [(0, 2)]); - // Within the margin of the nearest plane: still drawn. - assert_eq!(ranges(Vec3::new(3.0, 8.0, 8.0)), [(0, 5)]); - } -} diff --git a/crates/horizon-client/src/mesher/leaf_culling_tests.rs b/crates/horizon-client/src/mesher/leaf_culling_tests.rs index 679b19090..1273c806d 100644 --- a/crates/horizon-client/src/mesher/leaf_culling_tests.rs +++ b/crates/horizon-client/src/mesher/leaf_culling_tests.rs @@ -42,7 +42,7 @@ fn leaf_culling_removes_only_internal_leaf_and_root_faces() { cutout_leaves, cull_leaves, } - .tesselate(®ion, (0 - MIN_Y) / 16) + .mesh(®ion, (0 - MIN_Y) / 16) .solid }; From d271176f2342e1c2033f4ca38005892c60801f3e Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 24 Oct 2026 18:31:02 +0200 Subject: [PATCH 002/107] Tools and harnesses start the game in scratch folders, not a player's The game logs into HORIZON_HOME and falls back to the player's own game folder when it is unset. Harnesses that set only HORIZON_NO_AUDIO (and test_native_client, which set nothing) wrote logs, and on a world run advancements, into ~/Library/Application Support/horizon. lab.game_env gives every game a fresh HORIZON_HOME, HORIZON_LAUNCHER_HOME, XDG_DATA_HOME and XDG_CONFIG_HOME, no audio and no launcher self-update; a folder the caller passes is kept. Every launcher of the game uses it, parity.py gives each harness its own HORIZON_HOME, and tests/test_game_home.py fails for a script in tools/ that starts the game without it. Co-Authored-By: Claude Sonnet 5.5 --- docs/parity-workflow.md | 4 + tests/test_game_home.py | 102 ++++++++++++++++++++++++++ tools/agent_control_e2e.py | 15 ++-- tools/graphics_bench.py | 7 +- tools/lab.py | 33 +++++++++ tools/lan_e2e.py | 3 +- tools/launcher_mcp_e2e.py | 3 +- tools/mod_browser_harness.py | 8 +- tools/parity.py | 7 ++ tools/perf_bench.py | 2 +- tools/server_bench.py | 2 +- tools/test_block_removal_parity.py | 2 +- tools/test_command_exec_parity.py | 2 +- tools/test_composter_parity.py | 2 +- tools/test_crafter_parity.py | 2 +- tools/test_dedicated_server_parity.py | 4 +- tools/test_dispenser_parity.py | 2 +- tools/test_friends.py | 2 +- tools/test_mace_parity.py | 2 +- tools/test_native_client.py | 2 +- tools/test_online_mode_parity.py | 4 +- tools/test_save_roundtrip_parity.py | 4 +- tools/test_sculk_parity.py | 2 +- tools/test_secure_chat_parity.py | 2 +- tools/test_use_item_on_parity.py | 2 +- tools/test_world_reopen_parity.py | 5 +- tools/test_world_spawn_parity.py | 2 +- 27 files changed, 190 insertions(+), 37 deletions(-) create mode 100644 tests/test_game_home.py diff --git a/docs/parity-workflow.md b/docs/parity-workflow.md index 1b00080e7..f9f222071 100644 --- a/docs/parity-workflow.md +++ b/docs/parity-workflow.md @@ -32,6 +32,10 @@ Passes are shared between worktrees. A harness's key depends only on the content Every runner on the machine shares `PARITY_MACHINE_JOBS` slots (default: half the CPU threads), so several agents' runs together can't overload it. Parallel harnesses are also admitted by memory: each harness's recorded peak is reserved, within three quarters of the memory available at the start, and a harness starts only if the memory free at that moment still leaves 2 GB. `-j` caps the count here (default: `PARITY_LOCAL_JOBS`, else 3 while other machines help or a third of the CPU threads alone, within `$FLEET_LOAD_JOBS`). +### Harnesses that start the game + +The game logs into `HORIZON_HOME` and falls back to the player's own game folder when it is unset, so a harness that starts the game, `horizon-ctl`, the launcher or the dedicated server builds the environment with `lab.game_env(**extra)`: no audio, and fresh scratch folders for `HORIZON_HOME`, `HORIZON_LAUNCHER_HOME`, `XDG_DATA_HOME` and `XDG_CONFIG_HOME`, removed when the harness exits (a folder you pass, or that the environment already has, is kept). The runner also gives each harness its own `HORIZON_HOME` under `.local/parity-runner/game-home/`. `tests/test_game_home.py` fails for a script in `tools/` that starts the game without it. + ### Other machines Harnesses can also run on the Mac mini and the ThinkPad (`tools/parity_hosts.json` describes them; its `default` list is empty, so pass `--hosts macmini,thinkpad` or set `PARITY_HOSTS`, after copying `shared-local/` to `~/code/horizon-parity/shared-local` on each host). The runner mirrors the worktree there with rsync (not `.git`, `.local`, `target` or ignored files) into `~/code/horizon-parity/remote/`, whose `.local` links to the vanilla data in `~/code/horizon-parity/shared-local`, and starts `tools/parity.py --worker` over ssh. The worker builds the probes (about 80 s the first time) and runs what it is sent under the same tracer, admitting harnesses against its own free memory. One queue, longest first, feeds every machine, remote ones first: the Mac takes up to 14 harnesses, the desktop 3 and only those under 800 MB. Each pass comes back with the content hash of every file it depended on, so it is keyed on exactly what ran, and logs come back to `.local/parity-runner/logs/`; state, sharing and `gate.py` work as before. If a host can't be reached, or drops out, its harnesses run here. diff --git a/tests/test_game_home.py b/tests/test_game_home.py new file mode 100644 index 000000000..b4d9e017f --- /dev/null +++ b/tests/test_game_home.py @@ -0,0 +1,102 @@ +"""A tool or harness that starts the game must not write into a real player's folders. + +The game logs into HORIZON_HOME and falls back to the player's own game folder when it is unset, so every +spawn goes through lab.game_env, which sets fresh scratch folders. This test fails for a script in tools/ +that starts the game (or horizon-ctl, the launcher or the dedicated server) without it.""" + +import ast +import os +from pathlib import Path +import tempfile +import unittest +from unittest import mock + +from tools import lab + +TOOLS = Path(lab.ROOT) / "tools" + +# Strings that mean "this script starts the game": a binary a harness runs, or the audio switch every game run sets. +SPAWN_MARKERS = { + "HORIZON_NO_AUDIO", "horizon-ctl", "horizon-ctl.exe", "horizon-server", "horizon-server.exe", + "horizon-client.exe", "release/horizon-client", "fast/horizon-client", "debug/horizon-client", +} + +# Scripts that mention those strings without starting a game themselves, and why. +EXEMPT = { + "lab.py": "defines game_env", + "parity.py": "runs the harnesses and gives each its own HORIZON_HOME", + "package_native_client.py": "packs the binaries into an archive", + "build_release.py": "builds and packs the release", + "release_train.py": "release bookkeeping", + "pr_ready.py": "runs cargo checks and parity.py", + "port_harness.py": "ports Java code, starts no game", +} + + +def constants(tree): + """Every string in a script that is a whole literal or a keyword name (docstrings are longer text).""" + for node in ast.walk(tree): + if isinstance(node, ast.Constant) and isinstance(node.value, str): + yield node.value + elif isinstance(node, ast.keyword) and node.arg: + yield node.arg + + +def uses_game_env(tree): + """The script calls lab.game_env, or starts its servers through test_dedicated_server_parity.Server, which does.""" + return any(isinstance(node, ast.Attribute) and node.attr == "game_env" + or isinstance(node, ast.Name) and node.id == "game_env" + or isinstance(node, ast.ImportFrom) and (node.module or "").endswith("test_dedicated_server_parity") + for node in ast.walk(tree)) + + +def sets_audio_by_hand(tree): + return "HORIZON_NO_AUDIO" in set(constants(tree)) + + +class GameEnvTests(unittest.TestCase): + def test_gives_every_game_scratch_folders(self): + with mock.patch.dict(os.environ, {}, clear=False): + for key in ("HORIZON_HOME", "HORIZON_LAUNCHER_HOME", "XDG_DATA_HOME", "XDG_CONFIG_HOME"): + os.environ.pop(key, None) + first, second = lab.game_env(), lab.game_env() + self.assertEqual(first["HORIZON_NO_AUDIO"], "1") + self.assertEqual(first["HORIZON_LAUNCHER_NO_SELF_UPDATE"], "1") + for key in ("HORIZON_HOME", "HORIZON_LAUNCHER_HOME", "XDG_DATA_HOME", "XDG_CONFIG_HOME"): + self.assertTrue(Path(first[key]).is_dir(), key) + self.assertNotEqual(first[key], second[key], key) + + def test_keeps_the_callers_folders(self): + with tempfile.TemporaryDirectory() as temp: + env = lab.game_env({"PATH": "x", "HORIZON_HOME": "/from/base"}, HORIZON_LAUNCHER_HOME=Path(temp), A=1) + self.assertEqual(env["HORIZON_HOME"], "/from/base") + self.assertEqual(env["HORIZON_LAUNCHER_HOME"], temp) + self.assertEqual((env["PATH"], env["A"]), ("x", "1")) + + def test_does_not_touch_the_process_environment(self): + before = dict(os.environ) + lab.game_env() + self.assertEqual(dict(os.environ), before) + + +class SpawnerTests(unittest.TestCase): + def test_every_script_that_starts_the_game_uses_game_env(self): + offenders = [] + for path in sorted(TOOLS.rglob("*.py")): + if path.name in EXEMPT: + continue + tree = ast.parse(path.read_text(encoding="utf-8"), filename=str(path)) + if SPAWN_MARKERS & set(constants(tree)) and not uses_game_env(tree): + offenders.append(str(path.relative_to(lab.ROOT))) + self.assertEqual(offenders, [], "these start the game without lab.game_env (HORIZON_HOME falls back to a " + "real player's folder); use it, or list the script in EXEMPT") + + def test_nobody_sets_the_audio_switch_by_hand(self): + offenders = [str(path.relative_to(lab.ROOT)) for path in sorted(TOOLS.rglob("*.py")) + if path.name not in EXEMPT + and sets_audio_by_hand(ast.parse(path.read_text(encoding="utf-8"), filename=str(path)))] + self.assertEqual(offenders, [], "set it through lab.game_env, which also gives the game scratch folders") + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/agent_control_e2e.py b/tools/agent_control_e2e.py index 3fcf39553..7f0c52bce 100644 --- a/tools/agent_control_e2e.py +++ b/tools/agent_control_e2e.py @@ -67,17 +67,20 @@ class Failed(Exception): pass -def game_env(screen): +def game_env(screen, home=None): + """The environment of a game: lab.game_env with HORIZON_HOME `home` (a scratch folder when None), no live-count + ping, and on Linux the virtual screen `screen`.""" + extra = {'HORIZON_TELEMETRY_URL': 'off'} # no live-count ping from test games + if home is not None: + extra['HORIZON_HOME'] = home if sys.platform == 'darwin': - return dict(os.environ, HORIZON_NO_AUDIO='1', HORIZON_TELEMETRY_URL='off') - env = dict(os.environ, VSCREEN=screen) + return lab.game_env(**extra) + env = lab.game_env(VSCREEN=screen, **extra) subprocess.run(['vscreen', 'up'], env=env, check=False, capture_output=True) text = subprocess.run(['vscreen', 'env'], env=env, capture_output=True, text=True, check=True).stdout env.pop('DISPLAY', None) for match in re.finditer(r'(\w+)=(\S+)', text.split('unset')[0]): env[match.group(1)] = match.group(2) - env['HORIZON_NO_AUDIO'] = '1' - env['HORIZON_TELEMETRY_URL'] = 'off' # no live-count ping from test games if LAVAPIPE.exists(): env['VK_ICD_FILENAMES'] = str(LAVAPIPE) return env @@ -632,7 +635,7 @@ def scratch(mode, resources): (work / '.local/horizon-client').mkdir() os.symlink(resources, work / '.local/horizon-client' / lab.VERSION) # horizon-ctl reads No AI in the game home: this one, not the player's. - env = dict(game_env(os.environ.get('VSCREEN', 'agent-control')), HORIZON_HOME=str(work)) + env = game_env(os.environ.get('VSCREEN', 'agent-control'), home=work) return work, env diff --git a/tools/graphics_bench.py b/tools/graphics_bench.py index 0e42e94d5..f19165f62 100644 --- a/tools/graphics_bench.py +++ b/tools/graphics_bench.py @@ -53,15 +53,14 @@ guiScale:0 def vscreen_env(): out = subprocess.run(["vscreen", "env"], cwd=ROOT, capture_output=True, text=True, check=True) - env = dict(os.environ) + env = lab.game_env() for part in out.stdout.replace(";", " ").split(): if "=" in part and not part.startswith("export"): k, v = part.split("=", 1) env[k] = v env.pop("DISPLAY", None) - # Benchmark games never play sound on the machine's speakers. - env["HORIZON_NO_AUDIO"] = "1" - # Nor reach the telemetry server (docs/telemetry.md). + # lab.game_env: no sound on the machine's speakers, and scratch folders instead of the player's. + # Benchmark games do not reach the telemetry server (docs/telemetry.md). env["HORIZON_TELEMETRY_URL"] = "off" return env diff --git a/tools/lab.py b/tools/lab.py index a62a38f59..b7cc2f795 100644 --- a/tools/lab.py +++ b/tools/lab.py @@ -5,6 +5,7 @@ generated files stay in .local/. Also the small helpers the harnesses share (free ports, offline UUIDs, VarInts, float bits).""" import argparse +import atexit from collections import Counter import hashlib import json @@ -54,6 +55,38 @@ def cargo_target(): return ROOT / os.environ.get("CARGO_TARGET_DIR", "target") +_SCRATCH_DIRS = [] + + +def _scratch_dir(prefix): + path = tempfile.mkdtemp(prefix=prefix) + _SCRATCH_DIRS.append(path) + return path + + +@atexit.register +def _remove_scratch_dirs(): + for path in _SCRATCH_DIRS: + shutil.rmtree(path, ignore_errors=True) + + +def game_env(base=None, **extra): + """The environment for every run of the game, the launcher or horizon-ctl from a tool or a harness: no audio, + and fresh scratch folders for the game's home (logs, saves, options), the launcher's home and the XDG folders, + so a run never writes into a real player's folders (the game falls back to them when HORIZON_HOME is unset). + A folder the caller already has in `base` (default os.environ) or passes in `extra` is kept. The scratch folders + are removed when this process exits.""" + env = dict(os.environ if base is None else base) + env.update({key: str(value) for key, value in extra.items()}) + env["HORIZON_NO_AUDIO"] = "1" + env["HORIZON_LAUNCHER_NO_SELF_UPDATE"] = "1" + for key, prefix in (("HORIZON_HOME", "horizon-home-"), ("HORIZON_LAUNCHER_HOME", "horizon-launcher-home-"), + ("XDG_DATA_HOME", "horizon-xdg-data-"), ("XDG_CONFIG_HOME", "horizon-xdg-config-")): + if not env.get(key): + env[key] = _scratch_dir(prefix) + return env + + def write_json(path, data): path.parent.mkdir(parents=True, exist_ok=True) path.write_text(json.dumps(data, indent=2) + "\n", encoding="utf-8") diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index 3ece4b448..9d0586a2c 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -275,8 +275,7 @@ class Game: (self.work / '.local/horizon-client').mkdir(parents=True) os.symlink(resources, self.work / '.local/horizon-client' / lab.VERSION) self.socket = self.work / 'c.sock' - env = game_env(os.environ.get('VSCREEN', 'lan-e2e')) - env['HORIZON_HOME'] = str(self.work / 'home') + env = game_env(os.environ.get('VSCREEN', 'lan-e2e'), home=self.work / 'home') self.d = Cli(ctl, self.socket, self.work, env) self.d.name = name diff --git a/tools/launcher_mcp_e2e.py b/tools/launcher_mcp_e2e.py index 0b827d288..748acafc2 100644 --- a/tools/launcher_mcp_e2e.py +++ b/tools/launcher_mcp_e2e.py @@ -167,8 +167,7 @@ def main(): os.symlink(bins / name, version / name) os.symlink(resources, version / '.local/horizon-client' / lab.VERSION) (version / '.installed').write_text('') - env = dict(os.environ, HORIZON_LAUNCHER_HOME=str(work / 'launcher'), HORIZON_HOME=str(work / 'home'), - HORIZON_NO_AUDIO='1', HORIZON_TELEMETRY_URL='off') + env = lab.game_env(HORIZON_LAUNCHER_HOME=work / 'launcher', HORIZON_HOME=work / 'home', HORIZON_TELEMETRY_URL='off') m = Mcp(Path(args.launcher).resolve(), env) try: summary = scenario(m, out) diff --git a/tools/mod_browser_harness.py b/tools/mod_browser_harness.py index 4531570bc..cf33d85de 100755 --- a/tools/mod_browser_harness.py +++ b/tools/mod_browser_harness.py @@ -33,6 +33,11 @@ import tempfile import time from pathlib import Path +try: + from . import lab +except ImportError: + import lab + ROOT = Path(__file__).resolve().parents[1] @@ -154,8 +159,7 @@ def main(): args = ap.parse_args() home = Path(tempfile.mkdtemp(prefix="horizon-mod-browser-")) - env = dict(os.environ, HORIZON_HOME=str(home), HORIZON_NO_AUDIO="1", HORIZON_NO_HEARTBEAT="1", - HORIZON_CONTROL=str(home / "ctl.sock")) + env = lab.game_env(HORIZON_HOME=home, HORIZON_NO_HEARTBEAT="1", HORIZON_CONTROL=home / "ctl.sock") launcher = [args.launcher] game = Game(str(Path(args.bin) / "horizon-ctl"), str(Path(args.bin) / "horizon-client"), args.resources, home, env) diff --git a/tools/parity.py b/tools/parity.py index 4ed75f5ef..38a2fcde6 100644 --- a/tools/parity.py +++ b/tools/parity.py @@ -39,6 +39,7 @@ import json import os from pathlib import Path, PurePosixPath import re +import shutil import socket import subprocess import sys @@ -809,6 +810,12 @@ class LocalExecutor: trace.unlink(missing_ok=True) log = (STATE_DIR / "logs" / f"{name}.log").open("w") environment = run_environment(reuse) + # A game the harness starts logs into HORIZON_HOME, which is a real player's folder when it is unset + # (lab.game_env keeps this one). + game_home = STATE_DIR / "game-home" / name + shutil.rmtree(game_home, ignore_errors=True) + game_home.mkdir(parents=True) + environment.setdefault("HORIZON_HOME", str(game_home)) environment["PARITY_SHIM_OUTPUTS"] = str(outputs_listing(name, fresh=True)) environment.pop("PARITY_SHARD", None) if shard: diff --git a/tools/perf_bench.py b/tools/perf_bench.py index 31024737c..5f5f57305 100644 --- a/tools/perf_bench.py +++ b/tools/perf_bench.py @@ -400,7 +400,7 @@ def run_horizon(args, distance): for carry in dev_mods(args): command += ["--dev-mod", str(carry)] # Benchmark runs stay out of the live count and send nothing (docs/telemetry.md). - env = dict(os.environ, HORIZON_NO_AUDIO="1", HORIZON_TELEMETRY_URL="off") + env = lab.game_env(HORIZON_TELEMETRY_URL="off") if args.warm: # A first start to the title screen builds what later starts reuse (the content-mod # root, the block behaviour snapshot). It is not measured: players see it once. diff --git a/tools/server_bench.py b/tools/server_bench.py index a9a494efd..bf1cdabeb 100755 --- a/tools/server_bench.py +++ b/tools/server_bench.py @@ -120,7 +120,7 @@ class Server: self.process = subprocess.Popen([str(c) for c in command], cwd=directory, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, encoding='utf-8', errors='replace', - env=dict(os.environ, HORIZON_NO_AUDIO='1')) + env=lab.game_env()) threading.Thread(target=self._read, daemon=True).start() def _read(self): diff --git a/tools/test_block_removal_parity.py b/tools/test_block_removal_parity.py index 6ebbbf1d3..3185bd4bb 100755 --- a/tools/test_block_removal_parity.py +++ b/tools/test_block_removal_parity.py @@ -183,7 +183,7 @@ def main(): d.run(['cargo', 'build', '-q', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT, env={**os.environ, 'CARGO_BUILD_JOBS': os.environ.get('CARGO_BUILD_JOBS', '3')}) d.run([str(lab.cargo_target() / 'release/horizon-client'), '--resources', str(resources)], cwd=work, - env={**os.environ, 'HORIZON_DISPENSE_FIXTURE': f'{work / "cases.jsonl"},{work / "rust.txt"}', 'HORIZON_NO_AUDIO': '1'}) + env=lab.game_env(HORIZON_DISPENSE_FIXTURE=f'{work / "cases.jsonl"},{work / "rust.txt"}')) # Loot table names (D) are vanilla's bookkeeping; the drops themselves are compared. ignore = tuple(prefix + ' ' for prefix in args.ignore.split(',') if prefix) + ('D ',) native = h.split_cases((work / 'native.txt').read_text(), ignore) diff --git a/tools/test_command_exec_parity.py b/tools/test_command_exec_parity.py index 9d57d3f69..877bd0b38 100644 --- a/tools/test_command_exec_parity.py +++ b/tools/test_command_exec_parity.py @@ -82,7 +82,7 @@ def main(): rust_dir = build / 'rust' shutil.rmtree(rust_dir, ignore_errors=True) rust_dir.mkdir() - env = dict(os.environ, HORIZON_NO_AUDIO='1', HORIZON_COMMAND_SCRIPT=str(script_path), HORIZON_COMMAND_OUTPUT=str(build / 'rust.jsonl')) + env = lab.game_env(HORIZON_COMMAND_SCRIPT=script_path, HORIZON_COMMAND_OUTPUT=build / 'rust.jsonl') run([binary, '--resources', lab.RESOURCES, '--world', 'harness', '--seed', SEED, '--radius', 4], 'rust', cwd=rust_dir, env=env) vanilla = [json.loads(l) for l in (build / 'vanilla.jsonl').open(encoding='utf-8')] diff --git a/tools/test_composter_parity.py b/tools/test_composter_parity.py index e632527ce..074b227a8 100644 --- a/tools/test_composter_parity.py +++ b/tools/test_composter_parity.py @@ -125,7 +125,7 @@ def main(): d.run(['cargo', 'build', '-q', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT, env={**os.environ, 'CARGO_BUILD_JOBS': os.environ.get('CARGO_BUILD_JOBS', '3')}) d.run([str(lab.cargo_target() / 'release/horizon-client'), '--resources', str(resources)], cwd=work, - env={**os.environ, 'HORIZON_DISPENSE_FIXTURE': f'{work / "cases.jsonl"},{work / "rust.txt"}', 'HORIZON_NO_AUDIO': '1'}) + env=lab.game_env(HORIZON_DISPENSE_FIXTURE=f'{work / "cases.jsonl"},{work / "rust.txt"}')) ignore = tuple(prefix + ' ' for prefix in args.ignore.split(',') if prefix) + ('D ',) native = h.split_cases((work / 'native.txt').read_text(), ignore) rust = h.split_cases((work / 'rust.txt').read_text(), ignore) diff --git a/tools/test_crafter_parity.py b/tools/test_crafter_parity.py index 1b9564f41..b9c343d4d 100644 --- a/tools/test_crafter_parity.py +++ b/tools/test_crafter_parity.py @@ -245,7 +245,7 @@ def main(): d.run(['cargo', 'build', '-q', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT, env={**os.environ, 'CARGO_BUILD_JOBS': os.environ.get('CARGO_BUILD_JOBS', '3')}) d.run([str(lab.cargo_target() / 'release/horizon-client'), '--resources', str(resources)], cwd=work, - env={**os.environ, 'HORIZON_DISPENSE_FIXTURE': f'{work / "cases.jsonl"},{work / "rust.txt"}', 'HORIZON_NO_AUDIO': '1'}) + env=lab.game_env(HORIZON_DISPENSE_FIXTURE=f'{work / "cases.jsonl"},{work / "rust.txt"}')) ignore = tuple(prefix + ' ' for prefix in args.ignore.split(',') if prefix) + ('D ',) native = h.split_cases((work / 'native.txt').read_text(), ignore) rust = h.split_cases((work / 'rust.txt').read_text(), ignore) diff --git a/tools/test_dedicated_server_parity.py b/tools/test_dedicated_server_parity.py index f45df9822..6a6d258d4 100644 --- a/tools/test_dedicated_server_parity.py +++ b/tools/test_dedicated_server_parity.py @@ -126,7 +126,7 @@ class Server: self.lines = [] self.process = subprocess.Popen(list(map(str, command)), cwd=directory, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, - encoding='utf-8', errors='replace') + encoding='utf-8', errors='replace', env=lab.game_env()) self.reader = threading.Thread(target=self._read, daemon=True) self.reader.start() @@ -352,7 +352,7 @@ def horizon_client_joins(vanilla_server, binary, world, build, reference): server = Server(vanilla_server, directory, label) try: server.wait_for(r'Done \(') - env = dict(os.environ, HORIZON_NO_AUDIO='1', HORIZON_NET_REPORT=str(report)) + env = lab.game_env(HORIZON_NET_REPORT=report) with (build / 'horizon-client-run.log').open('w') as log: subprocess.run([str(c) for c in [binary, '--resources', lab.RESOURCES, '--render-distance', 2, '--server', f'127.0.0.1:{port}', '--name', PLAYER]], diff --git a/tools/test_dispenser_parity.py b/tools/test_dispenser_parity.py index 844322aeb..a6d1f429a 100644 --- a/tools/test_dispenser_parity.py +++ b/tools/test_dispenser_parity.py @@ -417,7 +417,7 @@ def main(): run(['cargo', 'build', '-q', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT, env={**os.environ, 'CARGO_BUILD_JOBS': os.environ.get('CARGO_BUILD_JOBS', '3')}) run([str(lab.cargo_target() / 'release/horizon-client'), '--resources', str(resources)], cwd=work, - env={**os.environ, 'HORIZON_DISPENSE_FIXTURE': f'{work / "cases.jsonl"},{work / "rust.txt"}', 'HORIZON_NO_AUDIO': '1'}) + env=lab.game_env(HORIZON_DISPENSE_FIXTURE=f'{work / "cases.jsonl"},{work / "rust.txt"}')) # Loot table names (D) are vanilla's bookkeeping; the drops themselves are compared. ignore = tuple(prefix + ' ' for prefix in args.ignore.split(',') if prefix) + ('D ',) native = h.split_cases((work / 'native.txt').read_text(), ignore) diff --git a/tools/test_friends.py b/tools/test_friends.py index 8fa1a4fee..cc5d15b92 100644 --- a/tools/test_friends.py +++ b/tools/test_friends.py @@ -96,7 +96,7 @@ class Game: self.process = subprocess.Popen(Game.prefix + [ str(client), '--resources', str(resources), '--render-distance', '2', '--test-terrain', '--control-socket', str(self.endpoint), '--timeout-seconds', '600', - ] + Game.extra, cwd=self.home, env=dict(os.environ, HORIZON_HOME=str(self.home), HORIZON_NO_AUDIO='1', **env), + ] + Game.extra, cwd=self.home, env=lab.game_env(HORIZON_HOME=self.home, **env), stdout=self.log, stderr=subprocess.STDOUT) wait_for(self.endpoint, 'screen', lambda v: v['screen'] == 'title', 120, f'{name} title') diff --git a/tools/test_mace_parity.py b/tools/test_mace_parity.py index 123f19693..578e9e6ee 100644 --- a/tools/test_mace_parity.py +++ b/tools/test_mace_parity.py @@ -170,7 +170,7 @@ def main(): d.run(['cargo', 'build', '-q', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT, env={**os.environ, 'CARGO_BUILD_JOBS': os.environ.get('CARGO_BUILD_JOBS', '3')}) d.run([str(lab.cargo_target() / 'release/horizon-client'), '--resources', str(resources)], cwd=work, - env={**os.environ, 'HORIZON_DISPENSE_FIXTURE': f'{work / "cases.jsonl"},{work / "rust.txt"}', 'HORIZON_NO_AUDIO': '1'}) + env=lab.game_env(HORIZON_DISPENSE_FIXTURE=f'{work / "cases.jsonl"},{work / "rust.txt"}')) ignore = tuple(prefix + ' ' for prefix in args.ignore.split(',') if prefix) + ('D ',) native = h.split_cases((work / 'native.txt').read_text(), ignore) rust = h.split_cases((work / 'rust.txt').read_text(), ignore) diff --git a/tools/test_native_client.py b/tools/test_native_client.py index 73e6f3223..552ea8c42 100644 --- a/tools/test_native_client.py +++ b/tools/test_native_client.py @@ -71,7 +71,7 @@ def main(): str(executable), '--radius', '1', '--frames', '30', '--smoke-report', str(report), '--timeout-seconds', '60'] with (root / 'startup-smoke.log').open('w') as log: - subprocess.run(command, cwd=lab.ROOT, env=environment, check=True, + subprocess.run(command, cwd=lab.ROOT, env=lab.game_env(environment), check=True, stdout=log, stderr=subprocess.STDOUT, timeout=120) result = json.loads(report.read_text()) if (result.get('error') is not None or result.get('presented_frames') != 30 diff --git a/tools/test_online_mode_parity.py b/tools/test_online_mode_parity.py index d430c2a68..d03838bf3 100644 --- a/tools/test_online_mode_parity.py +++ b/tools/test_online_mode_parity.py @@ -179,7 +179,7 @@ def run_client(binary, build, port, label, token, discovery): shutil.rmtree(directory, ignore_errors=True) directory.mkdir() report = directory / 'report.json' - env = dict(os.environ, HORIZON_NO_AUDIO='1', HORIZON_NET_REPORT=str(report), HORIZON_API_DISCOVERY=discovery) + env = lab.game_env(HORIZON_NET_REPORT=report, HORIZON_API_DISCOVERY=discovery) command = [binary, '--resources', lab.RESOURCES, '--render-distance', 2, '--server', f'127.0.0.1:{port}', '--name', PLAYER] if token: @@ -246,7 +246,7 @@ def run_side(label, command, binary, build, services, discovery): props = dict(PROPERTIES, **{'server-port': str(port)}) (directory / 'server.properties').write_text(''.join(f'{k}={v}\n' for k, v in props.items())) (directory / 'eula.txt').write_text('eula=true\n') - os.environ.update(HORIZON_NO_AUDIO='1', HORIZON_API_DISCOVERY=discovery) + os.environ.update(lab.game_env(HORIZON_API_DISCOVERY=discovery)) server = Server(command, directory, f'{label}-online') checks = [] starts = [] diff --git a/tools/test_save_roundtrip_parity.py b/tools/test_save_roundtrip_parity.py index bd8b07a29..732429881 100644 --- a/tools/test_save_roundtrip_parity.py +++ b/tools/test_save_roundtrip_parity.py @@ -330,7 +330,7 @@ def entity_uuid_text(i): def vscreen_env(): text = subprocess.run(['vscreen', 'env'], capture_output=True, text=True, check=True).stdout - env = dict(os.environ) + env = lab.game_env() env.pop('DISPLAY', None) for match in re.finditer(r'(\w+)=(\S+)', text.split('unset')[0]): env[match.group(1)] = match.group(2) @@ -346,7 +346,7 @@ def horizon_resave(source, destination, bin_dir): ctl = [str(bin_dir / 'horizon-ctl'), '--socket', str(socket)] extra = [] if sys.platform == 'darwin': - env = dict(os.environ, HORIZON_NO_AUDIO='1') + env = lab.game_env() extra = ['--offscreen'] else: alsa = destination / 'null-asound.conf' diff --git a/tools/test_sculk_parity.py b/tools/test_sculk_parity.py index b9c7417a5..ae2c1c6aa 100644 --- a/tools/test_sculk_parity.py +++ b/tools/test_sculk_parity.py @@ -339,7 +339,7 @@ def main(): oracle_harness.run_logged(['cargo', 'build', '--locked', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], build, 'compile-rust') oracle_harness.run_logged([lab.cargo_target() / 'release/horizon-client', '--resources', resources], build, 'horizon', - env={**os.environ, 'HORIZON_SCULK_FIXTURE': f'{inputs},{build / "horizon.txt"}', 'HORIZON_NO_AUDIO': '1'}) + env=lab.game_env(HORIZON_SCULK_FIXTURE=f'{inputs},{build / "horizon.txt"}')) if args.java_only: return names = {s[0] for _, s in chosen} diff --git a/tools/test_secure_chat_parity.py b/tools/test_secure_chat_parity.py index c6c76a892..b9062568a 100644 --- a/tools/test_secure_chat_parity.py +++ b/tools/test_secure_chat_parity.py @@ -264,7 +264,7 @@ def main(): oh.run_logged([*java, 'keys', build], build, 'keys') services = Services(json.loads((build / 'publickeys.json').read_text())) discovery = services.url + '/minecraft/client' - os.environ.update(HORIZON_NO_AUDIO='1', HORIZON_API_DISCOVERY=discovery) + os.environ.update(lab.game_env(HORIZON_API_DISCOVERY=discovery)) servers = { 'vanilla': ['java', '-Xmx2g', '-Duser.timezone=UTC', f'-Dminecraft.api.discovery.host={discovery}', '-cp', dependencies, 'net.minecraft.server.Main', 'nogui'], diff --git a/tools/test_use_item_on_parity.py b/tools/test_use_item_on_parity.py index d6e5f899f..89b463116 100644 --- a/tools/test_use_item_on_parity.py +++ b/tools/test_use_item_on_parity.py @@ -375,7 +375,7 @@ def main(): d.run(['cargo', 'build', '-q', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT, env={**os.environ, 'CARGO_BUILD_JOBS': os.environ.get('CARGO_BUILD_JOBS', '3')}) d.run([str(lab.cargo_target() / 'release/horizon-client'), '--resources', str(resources)], cwd=work, - env={**os.environ, 'HORIZON_DISPENSE_FIXTURE': f'{work / "cases.jsonl"},{work / "rust.txt"}', 'HORIZON_NO_AUDIO': '1'}) + env=lab.game_env(HORIZON_DISPENSE_FIXTURE=f'{work / "cases.jsonl"},{work / "rust.txt"}')) ignore = tuple(prefix + ' ' for prefix in args.ignore.split(',') if prefix) + ('D ',) native = h.split_cases((work / 'native.txt').read_text(), ignore) rust = h.split_cases((work / 'rust.txt').read_text(), ignore) diff --git a/tools/test_world_reopen_parity.py b/tools/test_world_reopen_parity.py index 381927013..4ef9a5e87 100644 --- a/tools/test_world_reopen_parity.py +++ b/tools/test_world_reopen_parity.py @@ -24,6 +24,9 @@ import sys import tempfile from pathlib import Path +sys.path.insert(0, str(Path(__file__).resolve().parent)) +import lab # noqa: E402 + ROOT = Path(__file__).resolve().parent.parent RESOURCES = ROOT / '.local' / 'horizon-client' / '26.3' RUN = ROOT / '.local' / 'world-reopen' @@ -32,7 +35,7 @@ CYCLES = 3 def game_env(): """macOS renders offscreen; Linux uses the virtual screen (as test_save_roundtrip_parity).""" - env = dict(os.environ, HORIZON_NO_AUDIO='1') + env = lab.game_env() if sys.platform == 'darwin': return env, ['--offscreen'] subprocess.run(['vscreen', 'up'], check=False, capture_output=True) diff --git a/tools/test_world_spawn_parity.py b/tools/test_world_spawn_parity.py index 5daa38a45..00c390005 100644 --- a/tools/test_world_spawn_parity.py +++ b/tools/test_world_spawn_parity.py @@ -140,7 +140,7 @@ def main(): # One worker thread: the chunks decorated in a fixed order (see above). vanilla_server = ['java', '-Xmx2g', '-Dmax.bg.threads=1', '-cp', dependencies, 'net.minecraft.server.Main', 'nogui'] horizon_server = [binary, '--resources', RESOURCES, '--dedicated', '--nogui'] - os.environ['HORIZON_NO_AUDIO'] = '1' + os.environ.update(lab.game_env()) def one(seed): t0 = time.time() From 9223b401e7bb8864711e1f05c891b9d3187bbadb Mon Sep 17 00:00:00 2001 From: some-du6e <103129948+some-du6e@users.noreply.github.com> Date: Sat, 24 Oct 2026 12:20:45 -0600 Subject: [PATCH 003/107] Decode every item data component a server sends Horizon decoded 25 of 26.3's 122 data component types. Item data has no length prefix, so a stack with any other component (an armour trim, banner patterns, pot sherds, a stew's effects, a plugin's item) made the whole packet undecodable and it was dropped: on a Paper server the inventory arrived empty until each slot was clicked, the advancements screen said "There doesn't seem to be anything here..." and the recipe book was empty. Every other component is now read to its end with its vanilla StreamCodec (item/skip.rs) and kept as its bytes, written back as they came; components without a network codec go as their NBT. The client leaves them out of the item's components, so the item keeps its prototype's values for them. tools/test_item_wire_parity.py checks it against vanilla: the oracle encodes 374 stacks with ItemStack.OPTIONAL_STREAM_CODEC (every default component value of every item, SNBT fixtures for the rest, direct holders and removals built in Java), covering all 122 component types, and the Rust probe must consume every byte, find the same component ids and encode the read-past ones back to the same bytes. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/remote.rs | 2 + crates/horizon-protocol/Cargo.toml | 4 + .../examples/item_wire_probe.rs | 64 +++ crates/horizon-protocol/src/item.rs | 45 +- crates/horizon-protocol/src/item/skip.rs | 427 ++++++++++++++++++ .../parity/VanillaItemWireOracle.java | 155 +++++++ tools/test_item_wire_parity.py | 177 ++++++++ 7 files changed, 871 insertions(+), 3 deletions(-) create mode 100644 crates/horizon-protocol/examples/item_wire_probe.rs create mode 100644 crates/horizon-protocol/src/item/skip.rs create mode 100644 mods/horizon/src/test/java/dev/horizonlab/parity/VanillaItemWireOracle.java create mode 100755 tools/test_item_wire_parity.py diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index da0def0fb..0e6127178 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -596,6 +596,8 @@ impl Game<'_> { continue; }; let value = match c { + // Read past, not decoded: the item keeps its prototype's value. + Component::Raw(_) => continue, Component::Int(v) if *kind == component::RARITY => { let rarity = ["common", "uncommon", "rare", "epic"]; Tag::String(rarity.get(*v as usize).copied().unwrap_or("common").into()) diff --git a/crates/horizon-protocol/Cargo.toml b/crates/horizon-protocol/Cargo.toml index 71b93a1fe..6467ed849 100644 --- a/crates/horizon-protocol/Cargo.toml +++ b/crates/horizon-protocol/Cargo.toml @@ -22,5 +22,9 @@ sha2 = "0.10" name = "protocol_probe" path = "examples/probe.rs" +[[example]] +name = "item_wire_probe" +path = "examples/item_wire_probe.rs" + [lints] workspace = true diff --git a/crates/horizon-protocol/examples/item_wire_probe.rs b/crates/horizon-protocol/examples/item_wire_probe.rs new file mode 100644 index 000000000..19760ca1a --- /dev/null +++ b/crates/horizon-protocol/examples/item_wire_probe.rs @@ -0,0 +1,64 @@ +//! The item wire harness's Rust side (tools/test_item_wire_parity.py): reads +//! the oracle's stacks (JSON lines with `label` and `hex`), decodes each with +//! `item::Slot` and prints, per stack, whether every byte was consumed, the +//! patch's component ids (added, then removed), which of them Horizon +//! decoded into its own types (the rest it reads past, keeping their bytes) +//! and whether encoding the decoded stack gives the same bytes back. +//! +//! cargo run -p horizon-protocol --example item_wire_probe -- vanilla.jsonl rust.jsonl +use horizon_protocol::{ + buf::{Codec, Reader, Writer}, + item::{Component, Slot}, +}; +use serde_json::{Value, json}; +use std::io::Write; + +fn hex(s: &str) -> Option> { + (0..s.len()) + .step_by(2) + .map(|i| u8::from_str_radix(s.get(i..i + 2)?, 16).ok()) + .collect() +} + +fn main() -> Result<(), Box> { + let args: Vec = std::env::args().collect(); + let input = std::fs::read_to_string(args.get(1).ok_or("usage: item_wire_probe IN OUT")?)?; + let mut out = std::io::BufWriter::new(std::fs::File::create( + args.get(2).ok_or("usage: item_wire_probe IN OUT")?, + )?); + for line in input.lines().filter(|l| !l.trim().is_empty()) { + let v: Value = serde_json::from_str(line)?; + let label = v["label"].clone(); + let Some(bytes) = v["hex"].as_str().and_then(hex) else { + continue; + }; + let mut r = Reader::new(&bytes); + let result = match Slot::read(&mut r) { + Err(e) => json!({"label": label, "error": e.to_string()}), + Ok(stack) => { + let mut w = Writer::new(); + Slot::write(&stack, &mut w); + let (added, removed): (Vec, Vec) = + stack.as_ref().map_or_else(Default::default, |s| { + (s.added.iter().map(|(k, _)| *k).collect(), s.removed.clone()) + }); + let typed: Vec = stack + .iter() + .flat_map(|s| &s.added) + .filter(|(_, c)| !matches!(c, Component::Raw(_))) + .map(|(k, _)| *k) + .collect(); + json!({ + "label": label, + "left": r.remaining(), + "added": added, + "removed": removed, + "typed": typed, + "same_bytes": w.data == bytes, + }) + } + }; + writeln!(out, "{result}")?; + } + Ok(()) +} diff --git a/crates/horizon-protocol/src/item.rs b/crates/horizon-protocol/src/item.rs index 650fa4baa..0aa940a38 100644 --- a/crates/horizon-protocol/src/item.rs +++ b/crates/horizon-protocol/src/item.rs @@ -2,11 +2,13 @@ //! item's registry id and a data component patch, plus the hashed form the //! client sends back in container clicks (HashedStack). use crate::{ - Error, Result, + Result, buf::{Codec, Reader, Writer}, }; use horizon_world::nbt::Tag; +mod skip; + /// minecraft:data_component_type registry ids (26.3). pub mod component { pub const CUSTOM_DATA: i32 = 0; @@ -78,6 +80,9 @@ pub enum Component { WrittenBook(WrittenBook), /// `ChargedProjectiles`: a loaded crossbow's ammunition, as templates. Stacks(Vec), + /// A component Horizon reads past without interpreting it: its encoded + /// bytes, written back as they came. + Raw(Vec), } /// `Filterable.streamCodec`: the raw value, then an optional filtered one @@ -224,11 +229,16 @@ impl Component { hidden: (0..n).map(|_| r.varint()).collect::>()?, } } - other => return Err(Error::new(format!("unsupported item component {other}"))), + other => { + let start = r.position(); + skip::component(other, r)?; + Self::Raw(r.since(start).to_vec()) + } }) } fn write(&self, kind: i32, w: &mut Writer) { match self { + Self::Raw(bytes) => w.bytes(bytes), Self::Int(v) if kind == component::DYED_COLOR => w.i32(*v), Self::Int(v) => w.varint(*v), Self::Unit => {} @@ -582,7 +592,8 @@ mod tests { #[test] fn unknown_components_are_errors() { - let bytes = [1, 5, 1, 0, 99]; + // 122 is past DataComponents' last id (cushion_color, 121). + let bytes = [1, 5, 1, 0, 122]; assert!(Slot::read(&mut Reader::new(&bytes)).is_err()); } @@ -631,4 +642,32 @@ mod tests { [("textures".to_owned(), "x".to_owned(), None)] ); } + + #[test] + fn components_read_past_keep_their_bytes() { + // Vanilla 26.3's encoding (tools/test_item_wire_parity.py) of a chestplate's + // armour trim, a banner's patterns (one direct), a pot's sherds and a stew's + // effects: one server's inventory, recipe book and advancements had them. + for (hex, kind) in [ + ("01bd0801003a0502", 58), + ("01890b01004a02200e000b6d696e6563726166743a7801790b", 74), + ("01880301004c01f60801000001be0c0100000001d30c010000", 76), + ("01a408010037020f6412a001", 55), + ] { + let bytes: Vec = (0..hex.len()) + .step_by(2) + .map(|i| u8::from_str_radix(&hex[i..i + 2], 16).unwrap()) + .collect(); + let mut r = Reader::new(&bytes); + let stack = Slot::read(&mut r).unwrap(); + assert_eq!(r.remaining(), 0, "component {kind}"); + assert!(matches!( + stack.as_ref().and_then(|s| s.component(kind)), + Some(Component::Raw(_)) + )); + let mut w = Writer::new(); + Slot::write(&stack, &mut w); + assert_eq!(w.data, bytes, "component {kind}"); + } + } } diff --git a/crates/horizon-protocol/src/item/skip.rs b/crates/horizon-protocol/src/item/skip.rs new file mode 100644 index 000000000..152d0f0f1 --- /dev/null +++ b/crates/horizon-protocol/src/item/skip.rs @@ -0,0 +1,427 @@ +//! Reading past the data components Horizon doesn't interpret: each +//! function consumes exactly what its vanilla StreamCodec writes +//! (DataComponents' `networkSynchronized` codecs; a component without one +//! goes as its persistent Codec's NBT, `ByteBufCodecs.fromCodecWithRegistries`). +//! Item data has no length prefix, so a component read short or long makes +//! everything after it unreadable. +use super::{Component, Template}; +use crate::{ + Error, Result, + buf::{Codec, Reader}, +}; + +type Read = fn(&mut Reader) -> Result<()>; + +/// DataComponents' registry order (26.3) for the ids `Component::read` +/// doesn't decode itself. +pub(super) fn component(kind: i32, r: &mut Reader) -> Result<()> { + match kind { + // Unit.STREAM_CODEC: glider, waxed. + 34 | 120 => Ok(()), + // Components without a network codec go as their NBT: intangible_projectile, + // map_decorations, debug_stick_state, recipes, lock, container_loot. + 22 | 49 | 59 | 68 | 81 | 82 => nbt(r), + // A VarInt (VAR_INT, an idMapper, a registry or holderRegistry id): damage_type, + // enchantable, additional_trade_cost, block_transformer, villager_food, dye, + // ominous_bottle_amplifier, base_color, the mob variants and colours, the pottery + // pattern and cushion_color. + 8 | 31 | 42 | 43 | 44 | 46 | 65 | 75 | 88..=108 | 110..=117 | 121 => varint(r), + // FLOAT: minimum_attack_charge, potion_duration_scale. + 7 | 54 => f32(r), + // Identifier.STREAM_CODEC: tooltip_style, note_block_sound. + 35 | 73 => ident(r), + // UseEffects: canSprint, interactVibrations, speedMultiplier. + 5 => { + r.bool()?; + r.bool()?; + f32(r) + } + // AdventureModePredicate: can_place_on, can_break. + 14 | 15 => list(r, block_predicate), + 16 => list(r, attribute_modifier), + // FoodProperties.DIRECT_STREAM_CODEC: nutrition, saturation, canAlwaysEat. + 23 => { + varint(r)?; + f32(r)?; + boolean(r) + } + // Consumable: consumeSeconds, animation, sound, hasConsumeParticles, onConsumeEffects. + 24 => { + f32(r)?; + varint(r)?; + sound(r)?; + boolean(r)?; + list(r, consume_effect) + } + // UseRemainder, SulfurCubeContent: one template. + 25 | 80 => template(r), + // UseCooldown: seconds, cooldownGroup. + 26 => { + f32(r)?; + optional(r, ident) + } + // DamageResistant, Repairable, provides_banner_patterns: a holder set. + 27 | 33 | 67 => holder_set(r), + 28 => tool(r), + // Weapon: itemDamagePerAttack, disableBlockingForSeconds. + 29 => { + varint(r)?; + f32(r) + } + 32 => equippable(r), + // DeathProtection: deathEffects. + 36 => list(r, consume_effect), + 37 => blocks_attacks(r), + // PiercingWeapon: dealsKnockback, dismounts, sound, hitSound. + 38 => { + boolean(r)?; + boolean(r)?; + optional(r, sound)?; + optional(r, sound) + } + 39 => kinetic_weapon(r), + // SwingAnimation: type, duration. + 40 | 41 => { + varint(r)?; + varint(r) + } + // BundleContents: templates. + 52 => list(r, template), + 53 => potion_contents(r), + // SuspiciousStewEffects: effect, duration. + 55 => list(r, |r| { + varint(r)?; + varint(r) + }), + // ArmorTrim: material, then Holder (assetId, description, decal). + 58 => { + trim_material(r)?; + holder(r, |r| { + ident(r)?; + nbt(r)?; + boolean(r) + }) + } + // TypedEntityData (entity_data, block_entity_data): the type id, then a compound. + 60 | 62 => { + varint(r)?; + nbt(r) + } + // CustomData: bucket_entity_data. + 61 => nbt(r), + // InstrumentComponent: Holder (soundEvent, useDuration, range, + // durabilityDamage, description). + 63 => holder(r, |r| { + sound(r)?; + f32(r)?; + f32(r)?; + varint(r)?; + nbt(r) + }), + 64 => trim_material(r), + // JukeboxPlayable: Holder (soundEvent, description, lengthInSeconds, + // comparatorOutput). + 66 => holder(r, |r| { + sound(r)?; + nbt(r)?; + f32(r)?; + varint(r) + }), + // LodestoneTracker: target (GlobalPos: dimension, BlockPos), tracked. + 69 => { + optional(r, |r| { + ident(r)?; + r.i64().map(drop) + })?; + boolean(r) + } + 70 => firework_explosion(r), + // Fireworks: flightDuration, explosions. + 71 => { + varint(r)?; + list(r, firework_explosion) + } + // BannerPatternLayers: Holder (assetId, translationKey), colour. + 74 => list(r, |r| { + holder(r, |r| { + ident(r)?; + string(r) + })?; + varint(r) + }), + // PotDecorations: back, left, right, front. + 76 => (0..4).try_for_each(|_| optional(r, template)), + // ItemContainerContents: optional templates. + 77 => list(r, |r| optional(r, template)), + // BlockItemStateProperties: a string to string map. + 78 => list(r, |r| { + string(r)?; + string(r) + }), + // Bees: Occupant (entity type and data, ticksInHive, minTicksInHive). + 79 => list(r, |r| { + varint(r)?; + nbt(r)?; + varint(r)?; + varint(r) + }), + 83 => sound(r), + // Compostable: layers. + 84 => resolvable(r, i32), + // CookingFuel (burnTime, speedMultiplier), BrewingFuel (uses, speedMultiplier). + 85 | 86 => { + resolvable(r, i32)?; + resolvable(r, f32) + } + // MobVisibility: targetingEntityTypes, visibility. + 87 => { + holder_set(r)?; + f32(r) + } + // Holder: width, height, assetId, title, author. + 109 => holder(r, |r| { + varint(r)?; + varint(r)?; + ident(r)?; + optional(r, nbt)?; + optional(r, nbt) + }), + // SignText: four messages, four optional filtered ones, colour, hasGlowingText. + 118 | 119 => { + (0..4).try_for_each(|_| nbt(r))?; + optional(r, |r| (0..4).try_for_each(|_| nbt(r)))?; + varint(r)?; + boolean(r) + } + other => Err(Error::new(format!("unsupported item component {other}"))), + } +} + +fn boolean(r: &mut Reader) -> Result<()> { + r.bool().map(drop) +} +fn varint(r: &mut Reader) -> Result<()> { + r.varint().map(drop) +} +fn f32(r: &mut Reader) -> Result<()> { + r.f32().map(drop) +} +fn i32(r: &mut Reader) -> Result<()> { + r.i32().map(drop) +} +/// ByteBufCodecs.STRING_UTF8. +fn string(r: &mut Reader) -> Result<()> { + r.string(32767).map(drop) +} +/// Identifier.STREAM_CODEC, ResourceKey.streamCodec. +fn ident(r: &mut Reader) -> Result<()> { + string(r) +} +/// A text component or compound (fromCodecWithRegistries, COMPOUND_TAG). +fn nbt(r: &mut Reader) -> Result<()> { + r.nbt().map(drop) +} +fn template(r: &mut Reader) -> Result<()> { + Template::read(r).map(drop) +} +/// ByteBufCodecs.optional: a flag, then the value. +fn optional(r: &mut Reader, value: Read) -> Result<()> { + if r.bool()? { value(r) } else { Ok(()) } +} +/// ByteBufCodecs.list and map: a VarInt count, then the elements (a map's +/// key and value each). Every element is at least a byte, so the count +/// can't pass what is left. +fn list(r: &mut Reader, element: Read) -> Result<()> { + let n = r.count(r.remaining())?; + (0..n).try_for_each(|_| element(r)) +} +/// ByteBufCodecs.holder: 0 and the value itself, or the registry id plus one. +fn holder(r: &mut Reader, direct: Read) -> Result<()> { + if r.varint()? == 0 { direct(r) } else { Ok(()) } +} +/// ByteBufCodecs.holderSet: 0 and a tag id, or the size plus one and the ids. +fn holder_set(r: &mut Reader) -> Result<()> { + match r.varint()? { + 0 => ident(r), + n => { + let n = usize::try_from(n - 1).map_err(|_| Error::new("negative holder set size"))?; + if n > r.remaining() { + return Err(Error::new("holder set past the end")); + } + (0..n).try_for_each(|_| varint(r)) + } + } +} +/// SoundEvent.STREAM_CODEC: a holder of a location and an optional fixed range. +fn sound(r: &mut Reader) -> Result<()> { + holder(r, |r| { + ident(r)?; + optional(r, f32) + }) +} +/// ResolvableInt, ResolvableFloat: Either a constant (true) or a context provider's key. +fn resolvable(r: &mut Reader, constant: Read) -> Result<()> { + if r.bool()? { constant(r) } else { ident(r) } +} +/// TrimMaterial.STREAM_CODEC: a holder of a palette id and a description. +fn trim_material(r: &mut Reader) -> Result<()> { + holder(r, |r| { + ident(r)?; + nbt(r) + }) +} +/// MobEffectInstance: the effect id, then its Details. +fn mob_effect(r: &mut Reader) -> Result<()> { + varint(r)?; + effect_details(r) +} +/// MobEffectInstance.Details: amplifier, duration, ambient, showParticles, showIcon, then the +/// hidden effect's Details, recursively. +fn effect_details(r: &mut Reader) -> Result<()> { + varint(r)?; + varint(r)?; + boolean(r)?; + boolean(r)?; + boolean(r)?; + optional(r, effect_details) +} +/// ConsumeEffect.STREAM_CODEC: the consume_effect_type id, then that type's fields. +fn consume_effect(r: &mut Reader) -> Result<()> { + match r.varint()? { + // apply_effects: effects, probability. + 0 => { + list(r, mob_effect)?; + f32(r) + } + // remove_effects: a mob effect holder set. + 1 => holder_set(r), + // clear_all_effects. + 2 => Ok(()), + // teleport_randomly: diameter, directionalParticles. + 3 => { + f32(r)?; + boolean(r) + } + // play_sound. + 4 => sound(r), + other => Err(Error::new(format!("unknown consume effect type {other}"))), + } +} +/// BlockPredicate: blocks, properties, nbt, then DataComponentMatchers (exact, partial). +fn block_predicate(r: &mut Reader) -> Result<()> { + optional(r, holder_set)?; + // StatePropertiesPredicate: PropertyMatchers, each a name and an + // Either. + optional(r, |r| { + list(r, |r| { + string(r)?; + if r.bool()? { + string(r) + } else { + optional(r, string)?; + optional(r, string) + } + }) + })?; + optional(r, nbt)?; + // DataComponentExactPredicate: TypedDataComponents, a component type id and its value. + list(r, |r| { + let kind = r.varint()?; + Component::read(kind, r).map(drop) + })?; + // DataComponentPredicate.STREAM_CODEC: an Either of a predicate type id or a component + // type id, then the predicate as NBT (fromCodecWithRegistries). + list(r, |r| { + boolean(r)?; + varint(r)?; + nbt(r) + }) +} +/// ItemAttributeModifiers.Entry: attribute, modifier (id, amount, operation), slot, display. +fn attribute_modifier(r: &mut Reader) -> Result<()> { + varint(r)?; + ident(r)?; + r.f64()?; + varint(r)?; + varint(r)?; + // Display.Type: default, hidden, or override and its text. + match r.varint()? { + 0 | 1 => Ok(()), + 2 => nbt(r), + other => Err(Error::new(format!( + "unknown attribute modifier display {other}" + ))), + } +} +/// Tool: rules (blocks, speed, correctForDrops), defaultMiningSpeed, damagePerBlock, +/// canDestroyBlocksInCreative. +fn tool(r: &mut Reader) -> Result<()> { + list(r, |r| { + holder_set(r)?; + optional(r, f32)?; + optional(r, boolean) + })?; + f32(r)?; + varint(r)?; + boolean(r) +} +/// Equippable: slot, equipSound, assetId, cameraOverlay, allowedEntities, dispensable, +/// swappable, damageOnHurt, equipOnInteract, canBeSheared, shearingSound. +fn equippable(r: &mut Reader) -> Result<()> { + varint(r)?; + sound(r)?; + optional(r, ident)?; + optional(r, ident)?; + optional(r, holder_set)?; + (0..5).try_for_each(|_| boolean(r))?; + sound(r) +} +/// BlocksAttacks: blockDelaySeconds, disableCooldownScale, damageReductions (angle, type, +/// base, factor), itemDamage (threshold, base, factor), bypassedBy, blockSound, disableSound. +fn blocks_attacks(r: &mut Reader) -> Result<()> { + f32(r)?; + f32(r)?; + list(r, |r| { + f32(r)?; + optional(r, holder_set)?; + f32(r)?; + f32(r) + })?; + (0..3).try_for_each(|_| f32(r))?; + optional(r, holder_set)?; + optional(r, sound)?; + optional(r, sound) +} +/// KineticWeapon: contactCooldownTicks, delayTicks, the dismount, knockback and damage +/// conditions (maxDurationTicks, minSpeed, minRelativeSpeed), forwardMovement, +/// damageMultiplier, sound, hitSound. +fn kinetic_weapon(r: &mut Reader) -> Result<()> { + varint(r)?; + varint(r)?; + (0..3).try_for_each(|_| { + optional(r, |r| { + varint(r)?; + f32(r)?; + f32(r) + }) + })?; + f32(r)?; + f32(r)?; + optional(r, sound)?; + optional(r, sound) +} +/// PotionContents: potion, customColor, customEffects, customName. +fn potion_contents(r: &mut Reader) -> Result<()> { + optional(r, varint)?; + optional(r, i32)?; + list(r, mob_effect)?; + optional(r, string) +} +/// FireworkExplosion: shape, colors, fadeColors, hasTrail, hasTwinkle. +fn firework_explosion(r: &mut Reader) -> Result<()> { + varint(r)?; + list(r, i32)?; + list(r, i32)?; + boolean(r)?; + boolean(r) +} diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaItemWireOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaItemWireOracle.java new file mode 100644 index 000000000..cc95bb311 --- /dev/null +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaItemWireOracle.java @@ -0,0 +1,155 @@ +package dev.horizonlab.parity; + +import io.netty.buffer.Unpooled; +import it.unimi.dsi.fastutil.objects.Reference2ObjectMap; +import it.unimi.dsi.fastutil.objects.Reference2ObjectMaps; +import java.io.PrintWriter; +import java.lang.reflect.Field; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.HexFormat; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.Optional; +import net.minecraft.core.Holder; +import net.minecraft.core.component.DataComponentPatch; +import net.minecraft.core.component.DataComponentType; +import net.minecraft.core.component.DataComponents; +import net.minecraft.core.component.TypedDataComponent; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.chat.Component; +import net.minecraft.resources.Identifier; +import net.minecraft.sounds.SoundEvents; +import net.minecraft.world.entity.decoration.painting.PaintingVariant; +import net.minecraft.world.item.JukeboxPlayable; +import net.minecraft.world.item.JukeboxSong; +import net.minecraft.world.item.component.MapPostProcessing; +import net.minecraft.util.Unit; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; + +/** + * Item stacks as a server sends them (ItemStack.OPTIONAL_STREAM_CODEC), for + * every data component: each default component value of every vanilla item + * set on a stick (so it is in the patch), the SNBT fixtures given in a file + * (one `labelstack SNBT` per line), and a few patches built directly. + * Prints one JSON line per stack: its label, the encoded bytes in hex and the + * patch's component type ids in encoding order (added, then removed), or + * the error. + */ +public final class VanillaItemWireOracle { + private static final int PER_TYPE = 12; + + public static void main(String[] args) throws Exception { + ParityData.init(); + List fixtures = new ArrayList<>(); + for (String line : Files.readAllLines(Path.of(args[0]))) { + if (line.isBlank() || line.startsWith("#")) continue; + fixtures.add(line.split("\t", 2)); + } + try (PrintWriter out = new PrintWriter(Files.newBufferedWriter(Path.of(args[1])))) { + // Each distinct default value, once per component type and + // encoding, and at most PER_TYPE of them for one type. + Set seen = new HashSet<>(); + Map, Integer> perType = new HashMap<>(); + for (var item : BuiltInRegistries.ITEM) { + for (TypedDataComponent c : item.components()) { + ItemStack stack = new ItemStack(Items.STICK); + if (!set(stack, c)) continue; + String hex = encode(stack); + if (hex == null || perType.getOrDefault(c.type(), 0) >= PER_TYPE) continue; + if (!seen.add(BuiltInRegistries.DATA_COMPONENT_TYPE.getKey(c.type()) + hex)) continue; + perType.merge(c.type(), 1, Integer::sum); + print(out, "default " + BuiltInRegistries.ITEM.getKey(item) + " " + BuiltInRegistries.DATA_COMPONENT_TYPE.getKey(c.type()), stack); + } + } + for (String[] f : fixtures) { + try { + print(out, f[0], ParityData.stack(f[1])); + } catch (Exception e) { + out.println("{\"label\":" + json(f[0]) + ",\"error\":" + json(String.valueOf(e)) + "}"); + } + } + // Components without a persistent codec, direct holders item SNBT + // can't name, and removals. + ItemStack direct = new ItemStack(Items.STICK); + direct.set(DataComponents.ADDITIONAL_TRADE_COST, 4); + direct.set(DataComponents.MAP_POST_PROCESSING, MapPostProcessing.SCALE); + direct.set(DataComponents.JUKEBOX_PLAYABLE, new JukeboxPlayable(Holder.direct( + new JukeboxSong(SoundEvents.ARMOR_EQUIP_CHAIN, Component.literal("S"), 10f, 3)))); + direct.set(DataComponents.PAINTING_VARIANT, Holder.direct(new PaintingVariant( + 2, 1, Identifier.withDefaultNamespace("kebab"), Optional.of(Component.literal("T")), Optional.empty()))); + print(out, "non-persistent and direct", direct); + ItemStack locked = new ItemStack(Items.STONE); + locked.set(DataComponents.CREATIVE_SLOT_LOCK, Unit.INSTANCE); + print(out, "creative_slot_lock", locked); + ItemStack removed = new ItemStack(Items.APPLE, 3); + removed.remove(DataComponents.FOOD); + removed.remove(DataComponents.CONSUMABLE); + removed.set(DataComponents.DAMAGE, 4); + print(out, "removed food and consumable", removed); + print(out, "empty", ItemStack.EMPTY); + } + } + + @SuppressWarnings("unchecked") + private static boolean set(ItemStack stack, TypedDataComponent c) { + if (stack.getPrototype().has(c.type()) && java.util.Objects.equals(stack.getPrototype().get(c.type()), c.value())) return false; + stack.set((DataComponentType) c.type(), c.value()); + return true; + } + + private static String encode(ItemStack stack) { + var buf = new RegistryFriendlyByteBuf(Unpooled.buffer(), ParityData.access); + try { + ItemStack.OPTIONAL_STREAM_CODEC.encode(buf, stack); + } catch (RuntimeException e) { + return null; + } + byte[] bytes = new byte[buf.readableBytes()]; + buf.readBytes(bytes); + return HexFormat.of().formatHex(bytes); + } + + private static void print(PrintWriter out, String label, ItemStack stack) { + String hex = encode(stack); + if (hex == null) { + out.println("{\"label\":" + json(label) + ",\"error\":\"encode failed\"}"); + return; + } + // DataComponentPatch's encoding order: its map's fast iteration, added + // components, then removals. + StringBuilder kinds = new StringBuilder(); + StringBuilder removed = new StringBuilder(); + for (var e : Reference2ObjectMaps.fastIterable(patchMap(stack.getComponentsPatch()))) { + // Removed (package-private) marks a removal. + StringBuilder to = e.getValue().getClass().getName().equals("net.minecraft.core.component.Removed") ? removed : kinds; + to.append(to.isEmpty() ? "" : ",").append(id(e.getKey())); + } + out.println("{\"label\":" + json(label) + ",\"hex\":\"" + hex + "\",\"added\":[" + kinds + "],\"removed\":[" + removed + "]}"); + } + + @SuppressWarnings("unchecked") + private static Reference2ObjectMap, Object> patchMap(DataComponentPatch patch) { + try { + Field map = DataComponentPatch.class.getDeclaredField("map"); + map.setAccessible(true); + return (Reference2ObjectMap, Object>) map.get(patch); + } catch (ReflectiveOperationException e) { + throw new IllegalStateException(e); + } + } + + private static int id(DataComponentType type) { + return BuiltInRegistries.DATA_COMPONENT_TYPE.getId(type); + } + + private static String json(String s) { + return "\"" + s.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n") + "\""; + } +} diff --git a/tools/test_item_wire_parity.py b/tools/test_item_wire_parity.py new file mode 100755 index 000000000..1be6042ec --- /dev/null +++ b/tools/test_item_wire_parity.py @@ -0,0 +1,177 @@ +#!/usr/bin/env python3 +"""Item stacks on the wire: every data component, as vanilla 26.3 encodes it. + +VanillaItemWireOracle encodes stacks with ItemStack.OPTIONAL_STREAM_CODEC +(what a server sends in inventories, recipe books and advancements): each +default component value of every vanilla item, the SNBT fixtures below +(direct holders, tags and lists, optional fields, every consume effect, +block predicates with component matchers, the mob variants) and patches +built in Java (components without a persistent codec, removals). The Rust +probe decodes each with horizon_protocol::item::Slot and must consume every +byte and find the same component ids in the same order; a stack whose +components Horizon only reads past must also encode back to the same bytes. +Every id in DataComponents must be covered.""" +import argparse +import json +import os +import platform +import subprocess +from pathlib import Path +try: + from . import lab, test_rust_parity as base +except ImportError: + import lab + import test_rust_parity as base + +COMPONENT_TYPES = 122 + +FIXTURES = [ + ('custom_data', '{custom_data:{a:1,b:"x",c:[1,2]}}'), + ('unbreakable', '{unbreakable:{}}'), + ('custom_name', '{custom_name:{text:"Hi",color:"red",bold:1b}}'), + ('lore', '{lore:["a",{text:"b",italic:0b}]}'), + ('enchantments', '{enchantments:{"minecraft:sharpness":5,"minecraft:unbreaking":3}}'), + ('can_break tag', '{can_break:{blocks:"#minecraft:logs"}}'), + ('can_break list state nbt', '{can_break:[{blocks:["minecraft:stone","minecraft:dirt"],state:{snowy:"true",age:{min:"1",max:"3"},level:{min:"2"}},nbt:"{a:1b}"}]}'), + ('can_place_on components', '{can_place_on:{blocks:"minecraft:chest",components:{"minecraft:custom_name":"x","minecraft:damage":3},predicates:{"minecraft:custom_data":{a:1},"minecraft:damage":{durability:{min:1}}}}}'), + ('can_place_on empty', '{can_place_on:[{}]}'), + ('custom_model_data', '{custom_model_data:{floats:[1.0f,2.5f],flags:[1b,0b],strings:["a"],colors:[16711680]}}'), + ('tooltip_display', '{tooltip_display:{hide_tooltip:1b,hidden_components:["minecraft:enchantments","minecraft:damage"]}}'), + ('repair_cost', '{repair_cost:7}'), + ('intangible_projectile', '{intangible_projectile:{}}'), + ('tooltip_style', '{tooltip_style:"minecraft:fancy"}'), + ('interact_animation', '{interact_animation:{type:"stab",duration:9}}'), + ('dyed_color', '{dyed_color:16711935}'), + ('map_id', '{map_id:42}'), + ('written_book_content', '{written_book_content:{title:{raw:"T",filtered:"F"},author:"me",generation:1,pages:[{raw:"p1",filtered:"q1"},"p2"],resolved:1b}}'), + ('trim registry', '{trim:{material:"minecraft:gold",pattern:"minecraft:coast"}}'), + ('trim direct', '{trim:{material:{palette_id:"minecraft:x",description:"X"},pattern:{asset_id:"minecraft:y",description:"Y",decal:1b}}}'), + ('block_entity_data', '{block_entity_data:{id:"minecraft:chest",Items:[]}}'), + ('lodestone_tracker target', '{lodestone_tracker:{target:{dimension:"minecraft:the_nether",pos:[1,-64,3]},tracked:0b}}'), + ('lodestone_tracker empty', '{lodestone_tracker:{}}'), + ('firework_explosion', '{firework_explosion:{shape:"star",colors:[1,2],fade_colors:[3],has_trail:1b,has_twinkle:0b}}'), + ('profile name', '{profile:{name:"Notch"}}'), + ('profile full', '{profile:{name:"jeb_",id:[I;1,2,3,4],properties:[{name:"textures",value:"abc",signature:"sig"}]}}'), + ('note_block_sound', '{note_block_sound:"minecraft:block.note_block.bell"}'), + ('base_color', '{base_color:"lime"}'), + ('sulfur_cube_content', '{sulfur_cube_content:{id:"minecraft:stone",count:1}}'), + ('lock', '{lock:{items:"minecraft:diamond"}}'), + ('container_loot', '{container_loot:{loot_table:"minecraft:chests/simple_dungeon",seed:5L}}'), + ('waxed', '{waxed:{}}'), + ('banner_patterns', '{banner_patterns:[{pattern:"minecraft:stripe_bottom",color:"red"},{pattern:{asset_id:"minecraft:x",translation_key:"y"},color:"blue"}]}'), + ('pot_decorations', '{pot_decorations:{back:{id:"minecraft:brick"},left:{id:"minecraft:angler_pottery_sherd"},front:{id:"minecraft:skull_pottery_sherd",count:1}}}'), + ('suspicious_stew_effects', '{suspicious_stew_effects:[{id:"minecraft:night_vision",duration:100},{id:"minecraft:poison"}]}'), + ('potion_contents full', '{potion_contents:{potion:"minecraft:swiftness",custom_color:123,custom_effects:[{id:"minecraft:speed",amplifier:1b,duration:200,ambient:1b,show_particles:0b,show_icon:1b,hidden_effect:{id:"minecraft:speed",duration:100,hidden_effect:{id:"minecraft:speed",duration:50}}}],custom_name:"x"}}'), + ('potion_contents bare', '{potion_contents:{}}'), + ('container', '{container:[{slot:0,item:{id:"minecraft:stone",count:3}},{slot:5,item:{id:"minecraft:diamond_sword",components:{"minecraft:damage":5,"minecraft:trim":{material:"minecraft:gold",pattern:"minecraft:coast"}}}}]}'), + ('bundle_contents', '{bundle_contents:[{id:"minecraft:stone",count:2},{id:"minecraft:potion",components:{"minecraft:potion_contents":{potion:"minecraft:water"}}}]}'), + ('charged_projectiles', '{charged_projectiles:[{id:"minecraft:arrow"},{id:"minecraft:firework_rocket",components:{"minecraft:fireworks":{flight_duration:2b,explosions:[{shape:"burst",colors:[5]}]}}}]}'), + ('block_state', '{block_state:{facing:"north",half:"top"}}'), + ('bees', '{bees:[{entity_data:{id:"minecraft:bee",Health:5f},ticks_in_hive:5,min_ticks_in_hive:10}]}'), + ('entity_data', '{entity_data:{id:"minecraft:pig",NoAI:1b}}'), + ('bucket_entity_data', '{bucket_entity_data:{Health:5f}}'), + ('instrument direct', '{instrument:{sound_event:{sound_id:"minecraft:x",range:8f},use_duration:3f,range:64f,description:"Horn"}}'), + ('painting variant', '{"minecraft:painting/variant":"minecraft:kebab"}'), + ('sign_text_front', '{sign_text_front:{messages:["a","b","c","d"],filtered_messages:["e","f","g","h"],color:"red",has_glowing_text:1b}}'), + ('sign_text_back', '{sign_text_back:{messages:["a","","",""]}}'), + ('map_decorations', '{map_decorations:{a:{type:"minecraft:player",x:1.0d,z:2.0d,rotation:0f}}}'), + ('debug_stick_state', '{debug_stick_state:{"minecraft:oak_stairs":"facing"}}'), + ('recipes', '{recipes:["minecraft:stone_bricks"]}'), + ('attribute_modifiers', '{attribute_modifiers:[{type:"minecraft:attack_damage",id:"minecraft:x",amount:1.5d,operation:"add_value",slot:"mainhand",display:{type:"override",value:"hi"}},{type:"minecraft:armor",id:"minecraft:y",amount:2d,operation:"add_multiplied_total",display:{type:"hidden"}}]}'), + ('consumable all effects', '{consumable:{consume_seconds:2f,animation:"drink",sound:{sound_id:"minecraft:x",range:4f},has_consume_particles:0b,on_consume_effects:[{type:"apply_effects",effects:[{id:"minecraft:speed",duration:20}],probability:0.5f},{type:"remove_effects",effects:"minecraft:poison"},{type:"remove_effects",effects:["minecraft:poison","minecraft:wither"]},{type:"clear_all_effects"},{type:"teleport_randomly",diameter:8f},{type:"play_sound",sound:"minecraft:entity.generic.eat"}]}}'), + ('death_protection', '{death_protection:{death_effects:[{type:"clear_all_effects"}]}}'), + ('equippable full', '{equippable:{slot:"head",equip_sound:"minecraft:item.armor.equip_gold",asset_id:"minecraft:gold",camera_overlay:"minecraft:misc/pumpkinblur",allowed_entities:"#minecraft:skeletons",dispensable:0b,swappable:0b,damage_on_hurt:0b,equip_on_interact:1b,can_be_sheared:1b,shearing_sound:{sound_id:"minecraft:y"}}}'), + ('equippable bare', '{equippable:{slot:"chest"}}'), + ('tool rules', '{tool:{rules:[{blocks:"#minecraft:mineable/pickaxe",speed:4f,correct_for_drops:1b},{blocks:["minecraft:stone"]}],default_mining_speed:2f,damage_per_block:2,can_destroy_blocks_in_creative:0b}}'), + ('blocks_attacks full', '{blocks_attacks:{block_delay_seconds:0.5f,disable_cooldown_scale:2f,damage_reductions:[{horizontal_blocking_angle:45f,type:"#minecraft:is_fire",base:1f,factor:0.5f},{base:0f,factor:1f}],item_damage:{threshold:3f,base:1f,factor:2f},bypassed_by:"#minecraft:bypasses_shield",block_sound:"minecraft:item.shield.block",disable_sound:{sound_id:"minecraft:z"}}}'), + ('damage_resistant', '{damage_resistant:{types:"#minecraft:is_fire"}}'), + ('repairable list', '{repairable:{items:["minecraft:diamond","minecraft:emerald"]}}'), + ('use_cooldown group', '{use_cooldown:{seconds:1.5f,cooldown_group:"minecraft:x"}}'), + ('use_remainder', '{use_remainder:{id:"minecraft:bowl",count:2}}'), + ('fireworks', '{fireworks:{flight_duration:3b,explosions:[{shape:"creeper",colors:[1],fade_colors:[2,3],has_trail:1b},{shape:"small_ball"}]}}'), + ('provides_banner_patterns', '{provides_banner_patterns:"#minecraft:pattern_item/globe"}'), + ('wolf variant', '{"minecraft:wolf/variant":"minecraft:ashen","minecraft:wolf/sound_variant":"minecraft:angry","minecraft:wolf/collar":"red"}'), + ('fox salmon parrot', '{"minecraft:fox/variant":"snow","minecraft:salmon/size":"large","minecraft:parrot/variant":"blue"}'), + ('tropical fish', '{"minecraft:tropical_fish/pattern":"flopper","minecraft:tropical_fish/base_color":"white","minecraft:tropical_fish/pattern_color":"black"}'), + ('mooshroom rabbit', '{"minecraft:mooshroom/variant":"brown","minecraft:rabbit/variant":"gold"}'), + ('pig cow chicken', '{"minecraft:pig/variant":"minecraft:cold","minecraft:pig/sound_variant":"minecraft:big","minecraft:cow/variant":"minecraft:warm","minecraft:cow/sound_variant":"minecraft:classic","minecraft:chicken/variant":"minecraft:cold","minecraft:chicken/sound_variant":"minecraft:classic"}'), + ('nautilus frog horse', '{"minecraft:zombie_nautilus/variant":"minecraft:warm","minecraft:frog/variant":"minecraft:cold","minecraft:horse/variant":"chestnut"}'), + ('llama axolotl cat', '{"minecraft:llama/variant":"brown","minecraft:axolotl/variant":"blue","minecraft:cat/variant":"minecraft:tabby","minecraft:cat/sound_variant":"minecraft:classic","minecraft:cat/collar":"blue"}'), + ('sheep shulker villager', '{"minecraft:sheep/color":"pink","minecraft:shulker/color":"purple","minecraft:villager/variant":"minecraft:taiga"}'), + ('trim material direct provider', '{provides_trim_material:{palette_id:"minecraft:x",description:"X"}}'), + ('everything at once', '{damage:3,unbreakable:{},custom_name:"n",enchantments:{"minecraft:mending":1},trim:{material:"minecraft:gold",pattern:"minecraft:coast"},banner_patterns:[{pattern:"minecraft:globe",color:"red"}],pot_decorations:{right:{id:"minecraft:brick"}},lore:["x"],rarity:"epic"}'), +] +# Fixtures on another item than a stick (the components need it, or read better there). +ITEMS = {'profile name': 'player_head', 'profile full': 'player_head', 'trim registry': 'diamond_chestplate', + 'trim direct': 'diamond_chestplate', 'banner_patterns': 'white_banner', 'pot_decorations': 'decorated_pot', + 'everything at once': 'diamond_chestplate'} + + +def compare(vanilla, rust): + """Failures, stacks checked, and the component ids covered.""" + results = {r['label']: r for r in rust} + failures, covered = [], set() + for v in vanilla: + if 'error' in v: + failures.append(f"oracle {v['label']}: {v['error']}") + continue + covered.update(v['added'] + v['removed']) + r = results.get(v['label']) + if r is None or 'error' in r: + failures.append(f"{v['label']}: {r and r['error']}") + elif r['left'] or r['added'] != v['added'] or r['removed'] != v['removed']: + failures.append(f"{v['label']}: {r['left']} bytes left, ids {r['added']}/{r['removed']}, vanilla {v['added']}/{v['removed']}") + elif not r['same_bytes'] and not r['typed']: + failures.append(f"{v['label']}: read past, but encoded back differently") + missing = sorted(set(range(COMPONENT_TYPES)) - covered) + if missing: + failures.append(f'component ids no stack covers: {missing}') + return failures, len(vanilla), covered + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--prepare', action='store_true') + args = parser.parse_args() + build = lab.LOCAL / 'item-wire-parity' + build.mkdir(parents=True, exist_ok=True) + report_path = build / 'report.json' + report_path.unlink(missing_ok=True) + if args.prepare: + lab.prepare() + lock, jars, libraries = base.verify_artifacts() + oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/VanillaItemWireOracle.java' + classes = build / 'classes' + classes.mkdir(exist_ok=True) + + def run(command, name): + with (build / (name + '.log')).open('w', encoding='utf-8') as log: + base.run(list(map(str, command)), stdout=log, stderr=subprocess.STDOUT) + dependency_path = os.pathsep.join(map(str, libraries)) + run(['javac', '--release', lock['minecraft']['java_major'], '-proc:none', '-sourcepath', + lab.ROOT / 'mods/horizon/src/test/java', '-cp', str(jars['server']) + os.pathsep + dependency_path, + '-d', classes, oracle], 'compile-java') + fixtures = build / 'fixtures.tsv' + fixtures.write_text(''.join(f'{label}\t{{id:"minecraft:{ITEMS.get(label, "stick")}",count:1,components:{snbt}}}\n' + for label, snbt in FIXTURES), encoding='utf-8') + vanilla_path, rust_path = build / 'vanilla.jsonl', build / 'rust.jsonl' + run(['java', '-Xmx1G', '-cp', os.pathsep.join([str(classes), str(jars['server']), dependency_path]), + 'dev.horizonlab.parity.VanillaItemWireOracle', fixtures, vanilla_path], 'oracle') + run(['cargo', 'build', '--release', '-p', 'horizon-protocol', '--example', 'item_wire_probe'], 'compile-rust') + binary = lab.cargo_target() / 'release/examples' / ('item_wire_probe.exe' if os.name == 'nt' else 'item_wire_probe') + run([binary, vanilla_path, rust_path], 'rust') + read = lambda path: [json.loads(line) for line in path.read_text(encoding='utf-8').splitlines() if line.strip()] + failures, stacks, covered = compare(read(vanilla_path), read(rust_path)) + for failure in failures[:40]: + print(failure) + if failures: + raise SystemExit(f'{len(failures)} item wire mismatches (of {stacks} stacks)') + lab.write_json(report_path, { + 'schema': 1, 'minecraft': lock['minecraft']['version'], 'platform': platform.platform(), + 'stacks': stacks, 'component_types_covered': len(covered), + 'oracle_sha256': lab.digest(vanilla_path, 'sha256'), 'rust_sha256': lab.digest(rust_path, 'sha256')}) + print(f'Item wire parity passed: {stacks} stacks, all {len(covered)} component types: {report_path}') + + +if __name__ == '__main__': + main() From eb0a6f74e6d5911cc63e637b6dcd14076c28ac0e Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 24 Oct 2026 20:42:05 +0200 Subject: [PATCH 004/107] Shader packs, clean room 2: macros and #if conditions The integer arithmetic of #if/#elif that a pack's .properties files and, later, its GLSL preprocessor share: defined X and defined(X), decimal, hexadecimal and octal literals, the C operators with C precedence, macros expanded to their values (a macro is not expanded inside its own expansion), and an identifier that is not a macro read as 0. A bad condition says why. The standard macros that need no renderer: MC_VERSION, from the pinned version by the documented rule (26.3 is 260300), and the system's MC_OS_*. Nothing uses it yet: players see no change. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 1 + crates/horizon-shaders/Cargo.toml | 1 + crates/horizon-shaders/src/condition.rs | 265 ++++++++++++++++++++++++ crates/horizon-shaders/src/lib.rs | 5 + crates/horizon-shaders/src/macros.rs | 131 ++++++++++++ 5 files changed, 403 insertions(+) create mode 100644 crates/horizon-shaders/src/condition.rs create mode 100644 crates/horizon-shaders/src/macros.rs diff --git a/Cargo.lock b/Cargo.lock index 6c7279aa7..7036ea5ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1434,6 +1434,7 @@ dependencies = [ name = "horizon-shaders" version = "0.1.0" dependencies = [ + "horizon-core", "zip", ] diff --git a/crates/horizon-shaders/Cargo.toml b/crates/horizon-shaders/Cargo.toml index ed3a24266..750949e83 100644 --- a/crates/horizon-shaders/Cargo.toml +++ b/crates/horizon-shaders/Cargo.toml @@ -7,6 +7,7 @@ rust-version = "1.85" description = "Shader packs (the shaders/ folder format): reading packs, their .properties and .lang files, and later their GLSL" [dependencies] +horizon-core = { path = "../horizon-core" } zip = { version = "2", default-features = false, features = ["deflate"] } [lints] diff --git a/crates/horizon-shaders/src/condition.rs b/crates/horizon-shaders/src/condition.rs new file mode 100644 index 000000000..b911eb985 --- /dev/null +++ b/crates/horizon-shaders/src/condition.rs @@ -0,0 +1,265 @@ +//! The integer arithmetic of `#if` and `#elif` (spec 1.5, GLSL 4.60 §3.3): `defined`, integer +//! literals, macros expanded to their values, and the C operators with C precedence. +//! An identifier that is not a macro counts as 0, as in C: GLSL would reject the line, and a +//! pack would then lose a whole section of its settings over one unknown name. + +use crate::Macros; +use crate::macros::{is_ident, is_ident_start}; + +/// The value of a condition; `Err` says what is wrong with it. +pub fn eval_condition(expr: &str, macros: &Macros) -> Result { + let tokens = tokens(&resolve_defined(expr, macros)?, macros)?; + let mut parser = Parser { + tokens: &tokens, + at: 0, + }; + let value = parser.binary(0)?; + match parser.tokens.get(parser.at) { + None => Ok(value), + Some(t) => Err(format!("unexpected `{}`", t.text())), + } +} + +/// `defined X` and `defined(X)` become `1` or `0` before anything is expanded. +fn resolve_defined(expr: &str, macros: &Macros) -> Result { + let mut out = String::with_capacity(expr.len()); + let mut rest = expr; + while let Some(at) = find_word(rest, "defined") { + out.push_str(&rest[..at]); + let after = rest[at + "defined".len()..].trim_start(); + let (inner, tail) = match after.strip_prefix('(') { + Some(open) => { + let close = open.find(')').ok_or("`defined(` without `)`")?; + (open[..close].trim(), &open[close + 1..]) + } + None => { + let len = after.find(|c: char| !is_ident(c)).unwrap_or(after.len()); + (&after[..len], &after[len..]) + } + }; + if inner.is_empty() || !inner.starts_with(is_ident_start) || !inner.chars().all(is_ident) { + return Err("`defined` needs a macro name".into()); + } + out.push_str(if macros.is_defined(inner) { + " 1 " + } else { + " 0 " + }); + rest = tail; + } + out.push_str(rest); + Ok(out) +} + +/// Where `word` starts in `text` as a whole identifier. +fn find_word(text: &str, word: &str) -> Option { + let mut from = 0; + while let Some(i) = text[from..].find(word).map(|i| i + from) { + let before = text[..i].chars().next_back().is_some_and(is_ident); + let after = text[i + word.len()..].chars().next().is_some_and(is_ident); + if !before && !after { + return Some(i); + } + from = i + word.len(); + } + None +} + +#[derive(Clone, Copy, Debug, PartialEq)] +enum Token { + Num(i64), + Op(&'static str), +} + +impl Token { + fn text(&self) -> String { + match self { + Token::Num(n) => n.to_string(), + Token::Op(op) => (*op).to_owned(), + } + } +} + +const OPS: [&str; 22] = [ + "<<", ">>", "<=", ">=", "==", "!=", "&&", "||", "(", ")", "!", "~", "+", "-", "*", "/", "%", + "<", ">", "&", "^", "|", +]; + +fn tokens(expr: &str, macros: &Macros) -> Result, String> { + let expanded = macros.expand(expr); + let mut out = Vec::new(); + let mut rest = expanded.trim_start(); + while let Some(c) = rest.chars().next() { + if c.is_ascii_digit() { + let len = rest + .find(|c: char| !c.is_ascii_alphanumeric()) + .unwrap_or(rest.len()); + if rest[len..].starts_with('.') { + return Err(format!("`{}`: `#if` takes integers only", &rest[..len + 1])); + } + out.push(Token::Num(int_literal(&rest[..len])?)); + rest = &rest[len..]; + } else if is_ident_start(c) { + // Not a macro: 0, as the C preprocessor counts it. + let len = rest.find(|c: char| !is_ident(c)).unwrap_or(rest.len()); + out.push(Token::Num(0)); + rest = &rest[len..]; + } else { + let op = OPS + .iter() + .find(|op| rest.starts_with(**op)) + .ok_or_else(|| format!("unexpected `{c}`"))?; + out.push(Token::Op(op)); + rest = &rest[op.len()..]; + } + rest = rest.trim_start(); + } + Ok(out) +} + +/// Decimal, `0x` hexadecimal or leading-zero octal, with an optional `u` suffix. +fn int_literal(text: &str) -> Result { + let digits = text.strip_suffix(['u', 'U']).unwrap_or(text); + let parsed = if let Some(hex) = digits.strip_prefix("0x").or(digits.strip_prefix("0X")) { + i64::from_str_radix(hex, 16) + } else if digits.len() > 1 && digits.starts_with('0') { + i64::from_str_radix(&digits[1..], 8) + } else { + digits.parse() + }; + parsed.map_err(|_| format!("`{text}` is not an integer")) +} + +struct Parser<'a> { + tokens: &'a [Token], + at: usize, +} + +/// Binary operators from the loosest to the tightest (GLSL §5.1 / §3.3). +const LEVELS: [&[&str]; 10] = [ + &["||"], + &["&&"], + &["|"], + &["^"], + &["&"], + &["==", "!="], + &["<", ">", "<=", ">="], + &["<<", ">>"], + &["+", "-"], + &["*", "/", "%"], +]; + +impl Parser<'_> { + fn next(&mut self) -> Option { + let t = self.tokens.get(self.at).copied(); + self.at += 1; + t + } + + fn binary(&mut self, level: usize) -> Result { + if level == LEVELS.len() { + return self.unary(); + } + let mut left = self.binary(level + 1)?; + while let Some(&Token::Op(op)) = self.tokens.get(self.at) { + if !LEVELS[level].contains(&op) { + break; + } + self.at += 1; + let right = self.binary(level + 1)?; + left = apply(op, left, right)?; + } + Ok(left) + } + + fn unary(&mut self) -> Result { + match self.next() { + Some(Token::Num(n)) => Ok(n), + Some(Token::Op("(")) => { + let value = self.binary(0)?; + match self.next() { + Some(Token::Op(")")) => Ok(value), + _ => Err("missing `)`".into()), + } + } + Some(Token::Op("-")) => Ok(self.unary()?.wrapping_neg()), + Some(Token::Op("+")) => self.unary(), + Some(Token::Op("!")) => Ok((self.unary()? == 0) as i64), + Some(Token::Op("~")) => Ok(!self.unary()?), + Some(t) => Err(format!("unexpected `{}`", t.text())), + None => Err("the condition ends too early".into()), + } + } +} + +fn apply(op: &str, a: i64, b: i64) -> Result { + Ok(match op { + "||" => (a != 0 || b != 0) as i64, + "&&" => (a != 0 && b != 0) as i64, + "|" => a | b, + "^" => a ^ b, + "&" => a & b, + "==" => (a == b) as i64, + "!=" => (a != b) as i64, + "<" => (a < b) as i64, + ">" => (a > b) as i64, + "<=" => (a <= b) as i64, + ">=" => (a >= b) as i64, + "<<" => a.wrapping_shl(b as u32), + ">>" => a.wrapping_shr(b as u32), + "+" => a.wrapping_add(b), + "-" => a.wrapping_sub(b), + "*" => a.wrapping_mul(b), + "/" | "%" if b == 0 => return Err("division by zero".into()), + "/" => a.wrapping_div(b), + _ => a.wrapping_rem(b), + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn eval(expr: &str) -> Result { + let mut m = Macros::standard(); + m.define("SHADOW_RES", "2048"); + m.define("WAVES", ""); + m.define("QUALITY", "SHADOW_RES / 1024"); + eval_condition(expr, &m) + } + + #[test] + fn precedence_and_operators_follow_c() { + assert_eq!(eval("1 + 2 * 3"), Ok(7)); + assert_eq!(eval("(1 + 2) * 3"), Ok(9)); + assert_eq!(eval("1 << 2 + 1"), Ok(8)); + assert_eq!(eval("6 & 3 == 3"), Ok(0)); + assert_eq!(eval("1 || 0 && 0"), Ok(1)); + assert_eq!(eval("-7 / 2"), Ok(-3)); + assert_eq!(eval("-7 % 2"), Ok(-1)); + assert_eq!(eval("!0 + ~0"), Ok(0)); + assert_eq!(eval("0x10 + 010 + 3u"), Ok(27)); + } + + #[test] + fn macros_and_defined() { + assert_eq!(eval("MC_VERSION >= 11300"), Ok(1)); + assert_eq!(eval("MC_VERSION <= 12111"), Ok(0)); + assert_eq!(eval("QUALITY == 2"), Ok(1)); + assert_eq!(eval("defined WAVES && defined(SHADOW_RES)"), Ok(1)); + assert_eq!(eval("!defined NOPE"), Ok(1)); + assert_eq!(eval("NOPE == 0"), Ok(1)); + assert_eq!(eval("undefinedFeature"), Ok(0)); + } + + #[test] + fn bad_conditions_say_why() { + assert!(eval("1 / 0").is_err()); + assert!(eval("SHADOW_RES == 0.5").is_err()); + assert!(eval("(1 + 2").is_err()); + assert!(eval("1 2").is_err()); + assert!(eval("").is_err()); + assert!(eval("defined").is_err()); + assert!(eval("1 @ 2").is_err()); + } +} diff --git a/crates/horizon-shaders/src/lib.rs b/crates/horizon-shaders/src/lib.rs index 1c9b46dd1..e7ccb26d6 100644 --- a/crates/horizon-shaders/src/lib.rs +++ b/crates/horizon-shaders/src/lib.rs @@ -5,11 +5,16 @@ //! - [`Pack`]: a pack as a folder or a zip, with paths relative to its `shaders` folder (spec 1.1). //! - [`discover`]: the packs in a `shaderpacks` folder. //! - [`Lang`]: the pack's translated names (8.4). +//! - [`Macros`] and [`eval_condition`]: the macros packs test, and the `#if` arithmetic (1.5). +mod condition; mod discover; mod lang; +mod macros; mod pack; +pub use condition::eval_condition; pub use discover::{Found, discover}; pub use lang::Lang; +pub use macros::{MC_VERSION, Macros}; pub use pack::{Pack, PackError}; diff --git a/crates/horizon-shaders/src/macros.rs b/crates/horizon-shaders/src/macros.rs new file mode 100644 index 000000000..f3a53722f --- /dev/null +++ b/crates/horizon-shaders/src/macros.rs @@ -0,0 +1,131 @@ +//! Object-like macros: the ones packs test in `#if` lines and use inside property values +//! (spec 1.5, 1.5.1). The GLSL preprocessor's function-like macros are not needed here. + +use std::collections::BTreeMap; + +/// `MC_VERSION` of the version Horizon plays: 26.3 → 260300. +pub const MC_VERSION: u32 = mc_version(horizon_core::version::VERSION_ID); + +/// The documented rule, `major * 10000 + minor * 100 + patch` (spec 1.5.1, OQ-1): `1.21` → 12100, +/// `1.7.10` → 10710. Parts that are not digits count as 0. +pub const fn mc_version(id: &str) -> u32 { + let bytes = id.as_bytes(); + let (mut parts, mut part, mut i) = ([0u32; 3], 0, 0); + while i < bytes.len() && part < 3 { + match bytes[i] { + b'.' => part += 1, + b @ b'0'..=b'9' => parts[part] = parts[part] * 10 + (b - b'0') as u32, + _ => break, + } + i += 1; + } + parts[0] * 10000 + parts[1] * 100 + parts[2] +} + +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Macros(BTreeMap); + +impl Macros { + /// The standard macros that need no renderer: `MC_VERSION` and this system's `MC_OS_*`. + /// The graphics ones arrive with the renderer. + pub fn standard() -> Self { + let mut m = Self::default(); + m.define("MC_VERSION", MC_VERSION.to_string()); + let os = match std::env::consts::OS { + "windows" => "MC_OS_WINDOWS", + "macos" => "MC_OS_MAC", + "linux" => "MC_OS_LINUX", + _ => "MC_OS_OTHER", + }; + m.define(os, ""); + m + } + + pub fn define(&mut self, name: impl Into, value: impl Into) { + self.0.insert(name.into(), value.into()); + } + + pub fn undef(&mut self, name: &str) { + self.0.remove(name); + } + + pub fn is_defined(&self, name: &str) -> bool { + self.0.contains_key(name) + } + + pub fn get(&self, name: &str) -> Option<&str> { + self.0.get(name).map(String::as_str) + } + + /// `text` with every identifier that names a macro replaced by its value, repeatedly; a macro + /// is not expanded again inside its own expansion, so `A` defined as `A + 1` stops. + pub fn expand(&self, text: &str) -> String { + self.expand_hiding(text, &mut Vec::new()) + } + + fn expand_hiding<'a>(&'a self, text: &str, hidden: &mut Vec<&'a str>) -> String { + let mut out = String::with_capacity(text.len()); + let mut rest = text; + while let Some(start) = rest.find(is_ident_start) { + out.push_str(&rest[..start]); + // A digit run glued to letters (`1e5`, `0x1F`) is a number, not a name. + let glued = out + .chars() + .next_back() + .is_some_and(|c| c.is_ascii_alphanumeric() || c == '.'); + let len = rest[start..] + .find(|c: char| !is_ident(c)) + .unwrap_or(rest.len() - start); + let word = &rest[start..start + len]; + match self.0.get_key_value(word) { + Some((name, value)) if !glued && !hidden.contains(&name.as_str()) => { + hidden.push(name); + out.push_str(&self.expand_hiding(value, hidden)); + hidden.pop(); + } + _ => out.push_str(word), + } + rest = &rest[start + len..]; + } + out.push_str(rest); + out + } +} + +pub(crate) fn is_ident_start(c: char) -> bool { + c.is_ascii_alphabetic() || c == '_' +} + +pub(crate) fn is_ident(c: char) -> bool { + c.is_ascii_alphanumeric() || c == '_' +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn versions_follow_the_documented_rule() { + assert_eq!(mc_version("1.21"), 12100); + assert_eq!(mc_version("1.21.0"), 12100); + assert_eq!(mc_version("1.7.10"), 10710); + assert_eq!(mc_version("26.3"), 260300); + assert_eq!(MC_VERSION, 260300); + assert_eq!(Macros::standard().get("MC_VERSION"), Some("260300")); + } + + #[test] + fn expansion_replaces_whole_words_and_stops_on_recursion() { + let mut m = Macros::default(); + m.define("RES", "SIZE"); + m.define("SIZE", "512"); + m.define("LOOP", "LOOP + 1"); + assert_eq!(m.expand("RES RES 0.5"), "512 512 0.5"); + assert_eq!( + m.expand("RESOLUTION colortex4 LOOP"), + "RESOLUTION colortex4 LOOP + 1" + ); + m.define("e5", "x"); + assert_eq!(m.expand("1e5"), "1e5"); + } +} From 397d529db72a17a1def83e1d7c50e9f866beb69e Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 24 Oct 2026 20:42:33 +0200 Subject: [PATCH 005/107] Shader packs, clean room 3: .properties files with conditional directives shaders.properties and the id files (block, item, entity, dimension): key=value lines in file order with their line numbers, # comments, a value continued on the next line by a trailing backslash, and the conditional directives evaluated with the standard and option macros. A # line is a directive only when its first word is one, so a commented-out key such as #ifdefault=1 stays a comment. #define and #undef apply to the lines after them in every one of these files: a public pack defines a macro inside #ifdef MC_OS_MAC in its shaders.properties and tests it further down. A later duplicate key replaces the earlier one; broken conditionals, lines that are not key=value and duplicates are kept as notes for the pack's log. Nothing uses it yet: players see no change. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-shaders/src/lib.rs | 3 + crates/horizon-shaders/src/properties.rs | 279 +++++++++++++++++++++ crates/horizon-shaders/tests/real_packs.rs | 30 ++- 3 files changed, 305 insertions(+), 7 deletions(-) create mode 100644 crates/horizon-shaders/src/properties.rs diff --git a/crates/horizon-shaders/src/lib.rs b/crates/horizon-shaders/src/lib.rs index e7ccb26d6..bdfef7067 100644 --- a/crates/horizon-shaders/src/lib.rs +++ b/crates/horizon-shaders/src/lib.rs @@ -4,6 +4,7 @@ //! //! - [`Pack`]: a pack as a folder or a zip, with paths relative to its `shaders` folder (spec 1.1). //! - [`discover`]: the packs in a `shaderpacks` folder. +//! - [`Properties`]: `shaders.properties` and the id files, with their conditional directives (3.1, 1.5). //! - [`Lang`]: the pack's translated names (8.4). //! - [`Macros`] and [`eval_condition`]: the macros packs test, and the `#if` arithmetic (1.5). @@ -12,9 +13,11 @@ mod discover; mod lang; mod macros; mod pack; +mod properties; pub use condition::eval_condition; pub use discover::{Found, discover}; pub use lang::Lang; pub use macros::{MC_VERSION, Macros}; pub use pack::{Pack, PackError}; +pub use properties::{Entry, Note, Properties}; diff --git a/crates/horizon-shaders/src/properties.rs b/crates/horizon-shaders/src/properties.rs new file mode 100644 index 000000000..fc3f00c81 --- /dev/null +++ b/crates/horizon-shaders/src/properties.rs @@ -0,0 +1,279 @@ +//! `.properties` files: `shaders.properties` (spec 3.1) and the id files `block.properties`, +//! `item.properties`, `entity.properties` (4.1). `key=value` lines, `#` comments, a trailing `\` +//! continuing a value on the next line, and conditional directives evaluated with the +//! standard and option macros (1.5 item 2). `#define` and `#undef` work in every one of them: +//! a public pack defines a macro in its `shaders.properties` and tests it further down. +//! No other escape is read: values are names, numbers and expressions. A later duplicate key +//! replaces the earlier one, with a note. + +use crate::{Macros, eval_condition}; + +/// One `key=value`, with the line it starts on (from 1). +#[derive(Clone, Debug, PartialEq)] +pub struct Entry { + pub key: String, + pub value: String, + pub line: u32, +} + +/// Something wrong with a line, for the pack's log. +#[derive(Clone, Debug, PartialEq)] +pub struct Note { + pub line: u32, + pub message: String, +} + +#[derive(Debug, Default, PartialEq)] +pub struct Properties { + /// In file order; a key appears once (a later duplicate replaces the earlier one). + pub entries: Vec, + pub notes: Vec, +} + +/// One branch level of `#if` ... `#endif`. +struct Branch { + /// This branch's lines are read. + on: bool, + /// A branch of this `#if` was already taken (the rest are skipped). + taken: bool, + /// The enclosing level is read. + outer: bool, + seen_else: bool, +} + +impl Properties { + /// Reads `text`; `#define`/`#undef` change the macros for the lines after them only. + pub fn parse(text: &str, macros: &Macros) -> Self { + let mut out = Self::default(); + let mut macros = macros.clone(); + let mut branches: Vec = Vec::new(); + let mut lines = text.lines().zip(1u32..); + while let Some((raw, number)) = lines.next() { + let line = raw.trim(); + let on = branches.last().is_none_or(|b| b.on); + if let Some(directive) = line.strip_prefix('#') { + out.directive(directive.trim_start(), number, &mut branches, &mut macros); + continue; + } + // Continuation lines belong to their entry even inside a skipped branch. + let mut logical = line.to_owned(); + while ends_with_continuation(&logical) { + logical.pop(); + match lines.next() { + Some((next, _)) => logical.push_str(next.trim()), + None => break, + } + } + if !on || logical.is_empty() { + continue; + } + match logical.split_once('=') { + Some((key, value)) if !key.trim().is_empty() => out.insert(Entry { + key: key.trim().to_owned(), + value: value.trim().to_owned(), + line: number, + }), + _ => out.note(number, "not a key=value line"), + } + } + if !branches.is_empty() { + out.note(text.lines().count() as u32, "`#if` without `#endif`"); + } + out + } + + pub fn get(&self, key: &str) -> Option<&str> { + self.entry(key).map(|e| e.value.as_str()) + } + + pub fn entry(&self, key: &str) -> Option<&Entry> { + self.entries.iter().find(|e| e.key == key) + } + + fn insert(&mut self, entry: Entry) { + if let Some(at) = self.entries.iter().position(|e| e.key == entry.key) { + let first = self.entries.remove(at).line; + self.note( + entry.line, + &format!( + "`{}` is set again (first on line {first}); this one counts", + entry.key + ), + ); + } + self.entries.push(entry); + } + + fn note(&mut self, line: u32, message: &str) { + self.notes.push(Note { + line, + message: message.to_owned(), + }); + } + + /// A line starting with `#`. It is a directive only when its first word is one; anything + /// else (`# text`, `#commented.key=1`, `#ifdefault`) is a comment. + fn directive( + &mut self, + line: &str, + number: u32, + branches: &mut Vec, + macros: &mut Macros, + ) { + let word_len = line + .find(|c: char| !c.is_ascii_alphabetic()) + .unwrap_or(line.len()); + let (word, rest) = line.split_at(word_len); + if !(rest.is_empty() || rest.starts_with(char::is_whitespace) || rest.starts_with("//")) { + return; + } + let rest = rest.split("//").next().unwrap_or("").trim(); + let on = branches.last().is_none_or(|b| b.on); + match word { + "if" | "ifdef" | "ifndef" => { + let value = on && self.condition(word, rest, number, macros); + branches.push(Branch { + on: value, + taken: value, + outer: on, + seen_else: false, + }); + } + "elif" | "else" => { + let Some(b) = branches.last_mut() else { + return self.note(number, &format!("`#{word}` without `#if`")); + }; + if b.seen_else { + return self.note(number, &format!("`#{word}` after `#else`")); + } + let take = b.outer && !b.taken; + b.seen_else = word == "else"; + b.on = take && (word == "else" || self.condition("if", rest, number, macros)); + b.taken |= b.on; + } + "endif" => { + if branches.pop().is_none() { + self.note(number, "`#endif` without `#if`"); + } + } + "define" if on => { + let (name, value) = rest.split_once(char::is_whitespace).unwrap_or((rest, "")); + macros.define(name, value.trim()); + } + "undef" if on => macros.undef(rest), + _ => {} + } + } + + fn condition(&mut self, word: &str, rest: &str, number: u32, macros: &Macros) -> bool { + let name = rest.split_whitespace().next().unwrap_or(""); + let result = match word { + "ifdef" => Ok(macros.is_defined(name) as i64), + "ifndef" => Ok(!macros.is_defined(name) as i64), + _ => eval_condition(rest, macros), + }; + result.unwrap_or_else(|e| { + self.note(number, &format!("`#{word} {rest}`: {e}; read as false")); + 0 + }) != 0 + } +} + +/// An odd number of trailing backslashes: the last one joins the next line (`\\` is a literal). +fn ends_with_continuation(line: &str) -> bool { + line.bytes().rev().take_while(|&b| b == b'\\').count() % 2 == 1 +} + +#[cfg(test)] +mod tests { + use super::*; + + fn read(text: &str) -> Properties { + let mut m = Macros::standard(); + m.define("SHADOWS", ""); + m.define("QUALITY", "2"); + Properties::parse(text, &m) + } + + fn pairs(p: &Properties) -> Vec<(&str, &str)> { + p.entries + .iter() + .map(|e| (e.key.as_str(), e.value.as_str())) + .collect() + } + + #[test] + fn keys_values_and_comments() { + let p = + read("# a comment\n\n sun = false\n#commented.key=1\nscreen=A B [C]\r\nbad line\n"); + assert_eq!(pairs(&p), [("sun", "false"), ("screen", "A B [C]")]); + assert_eq!(p.entry("screen").unwrap().line, 5); + assert_eq!( + p.notes, + [Note { + line: 6, + message: "not a key=value line".into() + }] + ); + } + + #[test] + fn continuation_lines_join() { + let p = read("screen=A B \\\n C D\\\n E\nsliders=X\nliteral=a\\\\\nnext=1"); + assert_eq!( + pairs(&p), + [ + ("screen", "A B C DE"), + ("sliders", "X"), + ("literal", "a\\\\"), + ("next", "1") + ] + ); + assert_eq!(p.entry("sliders").unwrap().line, 4); + } + + #[test] + fn conditionals_pick_one_branch() { + let p = read( + "#if MC_VERSION >= 11300\nnew=1\n#else\nold=1\n#endif\n\ + #ifdef SHADOWS\n#if QUALITY == 1\nq=1\n#elif QUALITY == 2\nq=2\n#elif QUALITY >= 2\nq=3\n#endif\n#endif\n\ + #ifndef SHADOWS\nnone=1\n#elif 0\nnever=1\n#else\nfallback=1\n#endif\n# ifdef SHADOWS // trailing comment\nspaced=1\n#endif", + ); + assert_eq!( + pairs(&p), + [("new", "1"), ("q", "2"), ("fallback", "1"), ("spaced", "1")] + ); + assert!(p.notes.is_empty(), "{:?}", p.notes); + } + + #[test] + fn comment_lines_are_not_directives() { + let p = read("#ifdefault=1\n#iffy\n#endiff\n#else-where\nkey=1"); + assert_eq!(pairs(&p), [("key", "1")]); + assert!(p.notes.is_empty(), "{:?}", p.notes); + } + + #[test] + fn defines_apply_to_the_lines_after_them() { + let text = "#ifdef MC_OS_NONE\n#define WATER 0\n#endif\n#define WATER 8\n\ + #if WATER == 8\nblock.8=minecraft:water\n#endif\n#undef WATER\n#ifdef WATER\nno=1\n#endif"; + let p = Properties::parse(text, &Macros::default()); + assert_eq!(pairs(&p), [("block.8", "minecraft:water")]); + assert!(p.notes.is_empty(), "{:?}", p.notes); + } + + #[test] + fn duplicate_keys_keep_the_last() { + let p = read("a=1\nb=2\na=3"); + assert_eq!(pairs(&p), [("b", "2"), ("a", "3")]); + assert_eq!(p.notes[0].line, 3); + } + + #[test] + fn broken_conditionals_are_noted() { + let p = read("#endif\n#else\n#if 1 +\nx=1\n#endif\n#if 1\n#else\n#else\n"); + let lines: Vec = p.notes.iter().map(|n| n.line).collect(); + assert_eq!(lines, [1, 2, 3, 8, 8]); + assert!(p.entries.is_empty()); + } +} diff --git a/crates/horizon-shaders/tests/real_packs.rs b/crates/horizon-shaders/tests/real_packs.rs index b35aef519..e0ad38c25 100644 --- a/crates/horizon-shaders/tests/real_packs.rs +++ b/crates/horizon-shaders/tests/real_packs.rs @@ -5,7 +5,7 @@ use std::path::Path; use std::time::Instant; -use horizon_shaders::{Lang, Pack, discover}; +use horizon_shaders::{Lang, Macros, Pack, Properties, discover}; #[test] #[ignore = "needs HORIZON_TEST_SHADER_PACKS"] @@ -22,21 +22,37 @@ fn real_packs_read() { for f in found.iter().filter(|f| f.problem.is_none()) { let start = Instant::now(); let pack = Pack::open(&f.path).unwrap(); - let properties = pack.read_text("shaders.properties"); + let macros = Macros::standard(); + let shaders = Properties::parse( + &pack.read_text("shaders.properties").unwrap_or_default(), + ¯os, + ); + let ids: Vec = ["block", "item", "entity", "dimension"] + .iter() + .filter_map(|n| pack.read_text(&format!("{n}.properties"))) + .map(|t| Properties::parse(&t, ¯os)) + .collect(); let lang = Lang::load(&pack, "en_us"); + let elapsed = start.elapsed(); println!( - "{}: {} files at the root, shaders.properties {} bytes, lang {}, {:.2} ms", + "{}: shaders.properties {} keys ({} notes), {} id files {} keys ({} notes), lang {}, {:.2} ms", f.name, - pack.list("").len(), - properties.as_ref().map_or(0, String::len), + shaders.entries.len(), + shaders.notes.len(), + ids.len(), + ids.iter().map(|p| p.entries.len()).sum::(), + ids.iter().map(|p| p.notes.len()).sum::(), if lang == Lang::default() { "none" } else { "read" }, - start.elapsed().as_secs_f64() * 1000.0 + elapsed.as_secs_f64() * 1000.0 ); - assert!(properties.is_some(), "{}: no shaders.properties", f.name); + for n in &shaders.notes { + println!(" shaders.properties:{}: {}", n.line, n.message); + } + assert!(!shaders.entries.is_empty(), "{}: no keys read", f.name); read += 1; } assert!(read > 0, "no packs under {}", dir.display()); From 352645b41711436306bc30e41e139b2d3809005a Mon Sep 17 00:00:00 2001 From: levzzz <86072227+levzzz5154@users.noreply.github.com> Date: Sun, 25 Oct 2026 00:07:31 +0500 Subject: [PATCH 006/107] Predict block placement on the client, so fast bridging stops losing clicks MultiPlayerGameMode.useItemOn places the block on the client's level at once, kept under the use's sequence until BlockChangedAck. Without it the next fast click aimed past the block still on its way from the server, the server refused it, and the arm swung for nothing. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/remote.rs | 56 +++++++++++++++++++----- 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index da0def0fb..e54b33b02 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -3862,11 +3862,17 @@ impl Game<'_> { self.predict_item_on_block(stack, &hit); } if held_block { - if reacts - || held - .as_ref() - .is_some_and(|stack| self.remote_can_place(&hit, stack)) - { + let placed = if reacts { + None + } else { + held.as_ref() + .and_then(|stack| self.remote_can_place(&hit, stack)) + }; + if let Some(placed) = &placed { + self.predict_place(placed, seq); + self.with_hand(hand, Game::consume_held); + } + if reacts || placed.is_some() { self.with_hand(hand, Game::swing_arm); } // BlockItem.place returns FAIL when placement is rejected: @@ -3997,12 +4003,41 @@ impl Game<'_> { }) } - /// Run BlockItem.place on a disposable level before predicting its swing. - /// The real level supplies collision shapes and building-blocking entities; - /// block writes, random draws, scheduled ticks and events stay in the preview. - fn remote_can_place(&mut self, hit: &super::Hit, stack: &ItemStack) -> bool { + /// MultiPlayerGameMode.useItemOn inside startPrediction, for a block item: + /// the blocks `BlockItem.place` set are placed on this client's level at + /// once, each kept as predicted under the use's sequence until the server + /// acknowledges it (a placement the server refused goes back). Without + /// this, the next click of a fast bridge aims past the block still on its + /// way from the server, and the server refuses it. + fn predict_place( + &mut self, + changes: &[(horizon_core::position::BlockPos, BlockStateId)], + seq: i32, + ) { + for &(p, state) in changes { + let pos = [p.x, p.y, p.z]; + let before = self.world.block(p.x, p.y, p.z); + // BlockStatePredictionHandler.retainKnownServerState. + if let Some(r) = self.remote.as_mut() { + r.predictions + .entry(pos) + .and_modify(|e| e.0 = seq) + .or_insert((seq, before.0 as i32)); + } + self.set_block(pos, state.0 as i32); + } + } + + /// Run BlockItem.place on a disposable level: the blocks it set, if it + /// placed. The real level supplies collision shapes and building-blocking + /// entities; random draws, scheduled ticks and events stay in the preview. + fn remote_can_place( + &mut self, + hit: &super::Hit, + stack: &ItemStack, + ) -> Option> { if !self.may_build() { - return false; + return None; } let ctx = horizon_world::level::PlaceContext { clicked_pos: horizon_core::position::BlockPos::new(hit.pos[0], hit.pos[1], hit.pos[2]), @@ -4025,6 +4060,7 @@ impl Game<'_> { ), horizon_world::level::PlaceOutcome::Placed { .. } ) + .then(|| level.changed.into_iter().collect()) }) } From 4c65ed2f5c6c4e089cf4f0bec4b32ab97004e4bc Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 24 Oct 2026 21:09:07 +0200 Subject: [PATCH 007/107] Shader packs, clean room 4: function-like macros and the standard macros The macro engine the GLSL preprocessor needs, shared with the .properties reader: function-like macros take their arguments (expanded first, raw beside ##, which pastes) when a ( follows their name; a macro is still not expanded inside its own expansion. Text is cut into preprocessing tokens, so numbers such as 1e5 or 0x1F are never mistaken for names. The #if/#elif/#else/#endif state moves out of the .properties reader so the preprocessor uses the same code. Standard macros: MC_GL_VERSION and MC_GLSL_VERSION 460, the 24 MC_RENDER_STAGE_* as ascending integers in draw order, render and shadow quality 1.0, MC_HAND_DEPTH 0.125, MAX_COLOR_BUFFERS 16. Nothing uses it yet: players see no change. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-shaders/src/condition.rs | 104 ++++++++++ crates/horizon-shaders/src/lib.rs | 2 +- crates/horizon-shaders/src/macros.rs | 242 ++++++++++++++++++++--- crates/horizon-shaders/src/properties.rs | 98 +++------ 4 files changed, 343 insertions(+), 103 deletions(-) diff --git a/crates/horizon-shaders/src/condition.rs b/crates/horizon-shaders/src/condition.rs index b911eb985..c476bb663 100644 --- a/crates/horizon-shaders/src/condition.rs +++ b/crates/horizon-shaders/src/condition.rs @@ -20,6 +20,110 @@ pub fn eval_condition(expr: &str, macros: &Macros) -> Result { } } +/// The text after a line's `#`, split into the directive word and the rest without its `//` +/// comment. `None` when the first word is followed by something other than white space, `//` or +/// the end of the line (`#ifdefault=1` is a comment, not `#ifdef`). +pub(crate) fn split_directive(after_hash: &str) -> Option<(&str, &str)> { + let line = after_hash.trim_start(); + let len = line + .find(|c: char| !c.is_ascii_alphabetic()) + .unwrap_or(line.len()); + let (word, rest) = line.split_at(len); + let ok = rest.is_empty() || rest.starts_with(char::is_whitespace) || rest.starts_with("//"); + ok.then(|| (word, rest.split("//").next().unwrap_or("").trim())) +} + +/// One level of `#if` ... `#endif`. +struct Branch { + /// This branch's lines are read. + on: bool, + /// A branch of this `#if` was already taken (the rest are skipped). + taken: bool, + /// The enclosing level is read. + outer: bool, + seen_else: bool, +} + +/// The state of nested conditional directives. +#[derive(Default)] +pub(crate) struct Conditionals(Vec); + +impl Conditionals { + /// Lines here are read. + pub(crate) fn active(&self) -> bool { + self.0.last().is_none_or(|b| b.on) + } + + /// An `#if` is still open. + pub(crate) fn open(&self) -> bool { + !self.0.is_empty() + } + + /// Applies `#word rest` when it is a conditional directive (`Ok(false)` when it is not). A + /// condition that can't be evaluated reads as false and returns why. + pub(crate) fn apply( + &mut self, + word: &str, + rest: &str, + macros: &Macros, + ) -> Result { + let on = self.active(); + match word { + "if" | "ifdef" | "ifndef" => { + let value = if on { + test(word, rest, macros) + } else { + Ok(false) + }; + let v = *value.as_ref().unwrap_or(&false); + self.0.push(Branch { + on: v, + taken: v, + outer: on, + seen_else: false, + }); + value.map(|_| true) + } + "elif" | "else" => { + let b = self + .0 + .last_mut() + .ok_or_else(|| format!("`#{word}` without `#if`"))?; + if b.seen_else { + return Err(format!("`#{word}` after `#else`")); + } + b.seen_else = word == "else"; + let take = b.outer && !b.taken; + let value = match word { + "else" => Ok(take), + _ if take => test("if", rest, macros), + _ => Ok(false), + }; + b.on = *value.as_ref().unwrap_or(&false); + b.taken |= b.on; + value.map(|_| true) + } + "endif" => self + .0 + .pop() + .map(|_| true) + .ok_or_else(|| "`#endif` without `#if`".into()), + _ => Ok(false), + } + } +} + +fn test(word: &str, rest: &str, macros: &Macros) -> Result { + let name = rest.split_whitespace().next().unwrap_or(""); + match word { + "ifdef" => Ok(macros.is_defined(name)), + "ifndef" => Ok(!macros.is_defined(name)), + _ => eval_condition(rest, macros) + .map(|v| v != 0) + .map_err(|e| format!("`#{word} {rest}`: {e}; read as false")), + } +} + /// `defined X` and `defined(X)` become `1` or `0` before anything is expanded. fn resolve_defined(expr: &str, macros: &Macros) -> Result { let mut out = String::with_capacity(expr.len()); diff --git a/crates/horizon-shaders/src/lib.rs b/crates/horizon-shaders/src/lib.rs index bdfef7067..05e51a53d 100644 --- a/crates/horizon-shaders/src/lib.rs +++ b/crates/horizon-shaders/src/lib.rs @@ -18,6 +18,6 @@ mod properties; pub use condition::eval_condition; pub use discover::{Found, discover}; pub use lang::Lang; -pub use macros::{MC_VERSION, Macros}; +pub use macros::{MC_VERSION, Macros, RENDER_STAGES}; pub use pack::{Pack, PackError}; pub use properties::{Entry, Note, Properties}; diff --git a/crates/horizon-shaders/src/macros.rs b/crates/horizon-shaders/src/macros.rs index f3a53722f..d8a184f41 100644 --- a/crates/horizon-shaders/src/macros.rs +++ b/crates/horizon-shaders/src/macros.rs @@ -22,12 +22,48 @@ pub const fn mc_version(id: &str) -> u32 { parts[0] * 10000 + parts[1] * 100 + parts[2] } +#[derive(Clone, Debug, PartialEq)] +struct Macro { + /// `None` for an object-like macro; the parameter names of a function-like one. + params: Option>, + body: String, +} + #[derive(Clone, Debug, Default, PartialEq)] -pub struct Macros(BTreeMap); +pub struct Macros(BTreeMap); + +/// `MC_RENDER_STAGE_*` in the order the stages draw (spec 5.4); each is its index (OQ-7). +pub const RENDER_STAGES: [&str; 24] = [ + "NONE", + "SKY", + "SUNSET", + "CUSTOM_SKY", + "SUN", + "MOON", + "STARS", + "VOID", + "TERRAIN_SOLID", + "TERRAIN_CUTOUT_MIPPED", + "TERRAIN_CUTOUT", + "ENTITIES", + "BLOCK_ENTITIES", + "DESTROY", + "OUTLINE", + "DEBUG", + "HAND_SOLID", + "TERRAIN_TRANSLUCENT", + "TRIPWIRE", + "PARTICLES", + "CLOUDS", + "RAIN_SNOW", + "WORLD_BORDER", + "HAND_TRANSLUCENT", +]; impl Macros { - /// The standard macros that need no renderer: `MC_VERSION` and this system's `MC_OS_*`. - /// The graphics ones arrive with the renderer. + /// The standard macros that need no renderer (spec 1.5.1): `MC_VERSION`, this system's + /// `MC_OS_*`, the GL and GLSL versions Horizon's translation accepts, the render stages and + /// the host settings at their defaults. The GPU vendor and family arrive with the renderer. pub fn standard() -> Self { let mut m = Self::default(); m.define("MC_VERSION", MC_VERSION.to_string()); @@ -38,11 +74,32 @@ impl Macros { _ => "MC_OS_OTHER", }; m.define(os, ""); + m.define("MC_GL_VERSION", "460"); + m.define("MC_GLSL_VERSION", "460"); + for (i, stage) in RENDER_STAGES.iter().enumerate() { + m.define(format!("MC_RENDER_STAGE_{stage}"), i.to_string()); + } + m.define("MC_RENDER_QUALITY", "1.0"); + m.define("MC_SHADOW_QUALITY", "1.0"); + m.define("MC_HAND_DEPTH", "0.125"); + m.define("MAX_COLOR_BUFFERS", "16"); m } pub fn define(&mut self, name: impl Into, value: impl Into) { - self.0.insert(name.into(), value.into()); + let body = value.into(); + self.0.insert(name.into(), Macro { params: None, body }); + } + + pub fn define_function(&mut self, name: &str, params: Vec, body: &str) { + let body = body.to_owned(); + self.0.insert( + name.to_owned(), + Macro { + params: Some(params), + body, + }, + ); } pub fn undef(&mut self, name: &str) { @@ -53,43 +110,158 @@ impl Macros { self.0.contains_key(name) } + /// The value of an object-like macro. pub fn get(&self, name: &str) -> Option<&str> { - self.0.get(name).map(String::as_str) + self.0 + .get(name) + .filter(|m| m.params.is_none()) + .map(|m| m.body.as_str()) } - /// `text` with every identifier that names a macro replaced by its value, repeatedly; a macro - /// is not expanded again inside its own expansion, so `A` defined as `A + 1` stops. + /// `text` with every macro expanded, repeatedly; a macro is not expanded again inside its own + /// expansion, so `A` defined as `A + 1` stops. Function-like macros take their arguments + /// (expanded first, raw beside `##`, which pastes) when a `(` follows their name. pub fn expand(&self, text: &str) -> String { self.expand_hiding(text, &mut Vec::new()) } fn expand_hiding<'a>(&'a self, text: &str, hidden: &mut Vec<&'a str>) -> String { let mut out = String::with_capacity(text.len()); - let mut rest = text; - while let Some(start) = rest.find(is_ident_start) { - out.push_str(&rest[..start]); - // A digit run glued to letters (`1e5`, `0x1F`) is a number, not a name. - let glued = out - .chars() - .next_back() - .is_some_and(|c| c.is_ascii_alphanumeric() || c == '.'); - let len = rest[start..] - .find(|c: char| !is_ident(c)) - .unwrap_or(rest.len() - start); - let word = &rest[start..start + len]; - match self.0.get_key_value(word) { - Some((name, value)) if !glued && !hidden.contains(&name.as_str()) => { - hidden.push(name); - out.push_str(&self.expand_hiding(value, hidden)); - hidden.pop(); - } - _ => out.push_str(word), + let tokens = lex(text); + let mut i = 0; + while i < tokens.len() { + let word = tokens[i]; + i += 1; + let Some((name, m)) = self.0.get_key_value(word) else { + out.push_str(word); + continue; + }; + if hidden.contains(&name.as_str()) { + out.push_str(word); + continue; } - rest = &rest[start + len..]; + let body = match &m.params { + None => m.body.clone(), + Some(params) => { + let Some((args, next)) = arguments(&tokens, i) else { + out.push_str(word); + continue; + }; + i = next; + self.substitute(params, &args, &m.body, hidden) + } + }; + hidden.push(name); + out.push_str(&self.expand_hiding(&body, hidden)); + hidden.pop(); } - out.push_str(rest); out } + + /// A function-like macro's body with its parameters replaced. + fn substitute<'a>( + &'a self, + params: &[String], + args: &[String], + body: &str, + hidden: &mut Vec<&'a str>, + ) -> String { + let tokens = lex(body); + let pasted = |at: usize, step: isize| { + let mut j = at as isize + step; + while (0..tokens.len() as isize).contains(&j) && tokens[j as usize].trim().is_empty() { + j += step; + } + (0..tokens.len() as isize).contains(&j) && tokens[j as usize] == "##" + }; + let mut out = String::new(); + for (at, token) in tokens.iter().enumerate() { + match params.iter().position(|p| p == token) { + Some(k) => { + let arg = args.get(k).map_or("", |a| a.trim()); + if pasted(at, -1) || pasted(at, 1) { + out.push_str(arg); + } else { + out.push_str(&self.expand_hiding(arg, hidden)); + } + } + None => out.push_str(token), + } + } + // `a ## b` → `ab`. + let mut joined = String::with_capacity(out.len()); + for (k, part) in out.split("##").enumerate() { + joined.push_str(if k == 0 { part.trim_end() } else { part.trim() }); + } + if out.contains("##") { joined } else { out } + } +} + +/// The arguments of a call whose `(` is the next non-blank token from `i`, split at top-level +/// commas, and the index after its `)`. `None` when no `(` follows or it never closes. +fn arguments(tokens: &[&str], mut i: usize) -> Option<(Vec, usize)> { + while tokens.get(i)?.trim().is_empty() { + i += 1; + } + if tokens[i] != "(" { + return None; + } + let (mut depth, mut args, mut from) = (0, Vec::new(), i + 1); + for (j, t) in tokens.iter().enumerate().skip(i + 1) { + match *t { + "(" => depth += 1, + ")" if depth == 0 => { + args.push(tokens[from..j].concat()); + return Some((args, j + 1)); + } + ")" => depth -= 1, + "," if depth == 0 => { + args.push(tokens[from..j].concat()); + from = j + 1; + } + _ => {} + } + } + None +} + +/// Preprocessing tokens: identifiers, numbers (with their suffixes and exponents), runs of +/// white space, `##`, and single characters. Concatenated they give back `text`. +pub(crate) fn lex(text: &str) -> Vec<&str> { + let bytes = text.as_bytes(); + let mut out = Vec::new(); + let mut i = 0; + while i < bytes.len() { + let c = bytes[i] as char; + let start = i; + i += 1; + if is_ident_start(c) { + while i < bytes.len() && is_ident(bytes[i] as char) { + i += 1; + } + } else if c.is_ascii_digit() || (c == '.' && bytes.get(i).is_some_and(u8::is_ascii_digit)) { + while i < bytes.len() { + let d = bytes[i] as char; + let exp_sign = (d == '+' || d == '-') && matches!(bytes[i - 1], b'e' | b'E'); + if !(is_ident(d) || d == '.' || exp_sign) { + break; + } + i += 1; + } + } else if c.is_whitespace() { + while i < bytes.len() && (bytes[i] as char).is_whitespace() { + i += 1; + } + } else if c == '#' && bytes.get(i) == Some(&b'#') { + i += 1; + } else { + while !text.is_char_boundary(i) { + i += 1; + } + } + out.push(&text[start..i]); + } + out } pub(crate) fn is_ident_start(c: char) -> bool { @@ -128,4 +300,18 @@ mod tests { m.define("e5", "x"); assert_eq!(m.expand("1e5"), "1e5"); } + + #[test] + fn function_like_macros_take_arguments_and_paste() { + let mut m = Macros::default(); + m.define("N", "4"); + m.define_function("SQ", vec!["x".into()], "((x) * (x))"); + m.define_function("CAT", vec!["a".into(), "b".into()], "a ## b"); + m.define_function("PI", vec![], "3.14"); + assert_eq!(m.expand("SQ(N + 1)"), "((4 + 1) * (4 + 1))"); + assert_eq!(m.expand("SQ(min(N, 2))"), "((min(4, 2)) * (min(4, 2)))"); + assert_eq!(m.expand("CAT(N, 2) CAT(col, ortex4)"), "N2 colortex4"); + assert_eq!(m.expand("PI() SQ SQ(1"), "3.14 SQ SQ(1"); + assert_eq!(m.get("SQ"), None); + } } diff --git a/crates/horizon-shaders/src/properties.rs b/crates/horizon-shaders/src/properties.rs index fc3f00c81..b9d27f3e3 100644 --- a/crates/horizon-shaders/src/properties.rs +++ b/crates/horizon-shaders/src/properties.rs @@ -6,7 +6,8 @@ //! No other escape is read: values are names, numbers and expressions. A later duplicate key //! replaces the earlier one, with a note. -use crate::{Macros, eval_condition}; +use crate::Macros; +use crate::condition::{Conditionals, split_directive}; /// One `key=value`, with the line it starts on (from 1). #[derive(Clone, Debug, PartialEq)] @@ -30,29 +31,20 @@ pub struct Properties { pub notes: Vec, } -/// One branch level of `#if` ... `#endif`. -struct Branch { - /// This branch's lines are read. - on: bool, - /// A branch of this `#if` was already taken (the rest are skipped). - taken: bool, - /// The enclosing level is read. - outer: bool, - seen_else: bool, -} - impl Properties { /// Reads `text`; `#define`/`#undef` change the macros for the lines after them only. pub fn parse(text: &str, macros: &Macros) -> Self { let mut out = Self::default(); let mut macros = macros.clone(); - let mut branches: Vec = Vec::new(); + let mut conditionals = Conditionals::default(); let mut lines = text.lines().zip(1u32..); while let Some((raw, number)) = lines.next() { let line = raw.trim(); - let on = branches.last().is_none_or(|b| b.on); + let on = conditionals.active(); if let Some(directive) = line.strip_prefix('#') { - out.directive(directive.trim_start(), number, &mut branches, &mut macros); + if let Some((word, rest)) = split_directive(directive) { + out.directive(word, rest, number, &mut conditionals, &mut macros); + } continue; } // Continuation lines belong to their entry even inside a skipped branch. @@ -76,7 +68,7 @@ impl Properties { _ => out.note(number, "not a key=value line"), } } - if !branches.is_empty() { + if conditionals.open() { out.note(text.lines().count() as u32, "`#if` without `#endif`"); } out @@ -111,72 +103,30 @@ impl Properties { }); } - /// A line starting with `#`. It is a directive only when its first word is one; anything - /// else (`# text`, `#commented.key=1`, `#ifdefault`) is a comment. + /// A line starting with `#` whose first word may be a directive; any other word makes it a + /// comment. fn directive( &mut self, - line: &str, + word: &str, + rest: &str, number: u32, - branches: &mut Vec, + conditionals: &mut Conditionals, macros: &mut Macros, ) { - let word_len = line - .find(|c: char| !c.is_ascii_alphabetic()) - .unwrap_or(line.len()); - let (word, rest) = line.split_at(word_len); - if !(rest.is_empty() || rest.starts_with(char::is_whitespace) || rest.starts_with("//")) { - return; - } - let rest = rest.split("//").next().unwrap_or("").trim(); - let on = branches.last().is_none_or(|b| b.on); - match word { - "if" | "ifdef" | "ifndef" => { - let value = on && self.condition(word, rest, number, macros); - branches.push(Branch { - on: value, - taken: value, - outer: on, - seen_else: false, - }); - } - "elif" | "else" => { - let Some(b) = branches.last_mut() else { - return self.note(number, &format!("`#{word}` without `#if`")); - }; - if b.seen_else { - return self.note(number, &format!("`#{word}` after `#else`")); + match conditionals.apply(word, rest, macros) { + Err(e) => self.note(number, &e), + Ok(true) => {} + Ok(false) if conditionals.active() => match word { + "define" => { + let (name, value) = rest.split_once(char::is_whitespace).unwrap_or((rest, "")); + macros.define(name, value.trim()); } - let take = b.outer && !b.taken; - b.seen_else = word == "else"; - b.on = take && (word == "else" || self.condition("if", rest, number, macros)); - b.taken |= b.on; - } - "endif" => { - if branches.pop().is_none() { - self.note(number, "`#endif` without `#if`"); - } - } - "define" if on => { - let (name, value) = rest.split_once(char::is_whitespace).unwrap_or((rest, "")); - macros.define(name, value.trim()); - } - "undef" if on => macros.undef(rest), - _ => {} + "undef" => macros.undef(rest), + _ => {} + }, + Ok(false) => {} } } - - fn condition(&mut self, word: &str, rest: &str, number: u32, macros: &Macros) -> bool { - let name = rest.split_whitespace().next().unwrap_or(""); - let result = match word { - "ifdef" => Ok(macros.is_defined(name) as i64), - "ifndef" => Ok(!macros.is_defined(name) as i64), - _ => eval_condition(rest, macros), - }; - result.unwrap_or_else(|e| { - self.note(number, &format!("`#{word} {rest}`: {e}; read as false")); - 0 - }) != 0 - } } /// An odd number of trailing backslashes: the last one joins the next line (`\\` is a literal). From f2399559979c1fd1a0e62c578a83ae4e5a29cd75 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 24 Oct 2026 21:09:29 +0200 Subject: [PATCH 008/107] Shader packs, clean room 5: the GLSL preprocessor and #include A stage file goes through comment removal, #include, conditionals and macro expansion, and every output line remembers the pack file and line it came from, so later errors point at the pack's own code. - #include "x" from the including file's folder, "/x" from the shaders folder, expanded in live code only (an include inside a false #if is not read), ten levels at most, paths that leave the pack refused; a file is read once per run however often it is included. - #define, #undef, line continuations, __LINE__ and __VERSION__; #version and #extension kept apart for the translation's own header; the block comments of live code kept for the directives packs write in them (DRAWBUFFERS, buffer formats); #error and unknown directives diagnosed. Black box over the public test packs: every stage file of the base and dimension folders (126 to 219 per pack) preprocesses with no diagnostic. Nothing uses it yet: players see no change. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-shaders/src/lib.rs | 3 + crates/horizon-shaders/src/pack.rs | 2 +- crates/horizon-shaders/src/preprocess.rs | 374 +++++++++++++++++++++ crates/horizon-shaders/tests/real_packs.rs | 36 +- 4 files changed, 413 insertions(+), 2 deletions(-) create mode 100644 crates/horizon-shaders/src/preprocess.rs diff --git a/crates/horizon-shaders/src/lib.rs b/crates/horizon-shaders/src/lib.rs index 05e51a53d..14f85b758 100644 --- a/crates/horizon-shaders/src/lib.rs +++ b/crates/horizon-shaders/src/lib.rs @@ -6,6 +6,7 @@ //! - [`discover`]: the packs in a `shaderpacks` folder. //! - [`Properties`]: `shaders.properties` and the id files, with their conditional directives (3.1, 1.5). //! - [`Lang`]: the pack's translated names (8.4). +//! - [`preprocess`]: a stage file through `#include` and the GLSL preprocessor, lines mapped back (1.4, 1.5). //! - [`Macros`] and [`eval_condition`]: the macros packs test, and the `#if` arithmetic (1.5). mod condition; @@ -13,6 +14,7 @@ mod discover; mod lang; mod macros; mod pack; +mod preprocess; mod properties; pub use condition::eval_condition; @@ -20,4 +22,5 @@ pub use discover::{Found, discover}; pub use lang::Lang; pub use macros::{MC_VERSION, Macros, RENDER_STAGES}; pub use pack::{Pack, PackError}; +pub use preprocess::{Diagnostic, Origin, Preprocessed, preprocess}; pub use properties::{Entry, Note, Properties}; diff --git a/crates/horizon-shaders/src/pack.rs b/crates/horizon-shaders/src/pack.rs index 113db9ac9..ae2230618 100644 --- a/crates/horizon-shaders/src/pack.rs +++ b/crates/horizon-shaders/src/pack.rs @@ -170,7 +170,7 @@ impl Pack { } /// `a/./b/../c` → `a/c`; `None` for a path that climbs out of `shaders`. -fn normalize(path: &str) -> Option { +pub(crate) fn normalize(path: &str) -> Option { let mut parts: Vec<&str> = Vec::new(); for part in path.split(['/', '\\']) { match part { diff --git a/crates/horizon-shaders/src/preprocess.rs b/crates/horizon-shaders/src/preprocess.rs new file mode 100644 index 000000000..4884f28f7 --- /dev/null +++ b/crates/horizon-shaders/src/preprocess.rs @@ -0,0 +1,374 @@ +//! The GLSL preprocessor a program's stage file goes through before translation (spec 1.4, +//! 1.5, 1.6): comments removed, `#include` expanded, conditionals evaluated, macros expanded. +//! Every output line remembers the pack file and line it came from, so errors found later point +//! at the pack's own code. +//! +//! `#include` is expanded where it stands in live code only: an include inside a false `#if` +//! is not read, so it can't fail (OQ-14). Ten levels deep at most, which also stops a file that +//! includes itself. `#version` and `#extension` are taken out of the code and kept apart: the +//! translation writes its own header. + +use std::collections::HashMap; + +use crate::Macros; +use crate::Pack; +use crate::condition::{Conditionals, split_directive}; +use crate::pack::normalize; + +const MAX_DEPTH: usize = 10; + +/// Where a line came from: an index into [`Preprocessed::files`] and a line number from 1. +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub struct Origin { + pub file: u16, + pub line: u32, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct Diagnostic { + pub origin: Origin, + pub message: String, +} + +#[derive(Debug, Default)] +pub struct Preprocessed { + /// The code, one line per entry of `lines`. + pub code: String, + pub lines: Vec, + /// The pack paths of the files read, the stage file first. + pub files: Vec, + /// `#version` of the stage file: the number and the profile word, if any. + pub version: Option<(u32, Option)>, + /// `#extension` lines of live code, as written after `#extension`. + pub extensions: Vec, + /// The text of every block comment in live code (`DRAWBUFFERS:`, buffer formats). + pub comments: Vec<(Origin, String)>, + pub diagnostics: Vec, +} + +impl Preprocessed { + /// `file:line` of an output line (from 0), for messages. + pub fn locate(&self, line: usize) -> String { + let o = self.lines.get(line).copied().unwrap_or_default(); + let file = self.files.get(o.file as usize).map_or("?", String::as_str); + format!("{file}:{}", o.line) + } +} + +/// Runs the preprocessor on the stage file `path` of `pack` with `macros` defined. +pub fn preprocess(pack: &Pack, path: &str, macros: &Macros) -> Preprocessed { + let mut run = Run { + pack, + macros: macros.clone(), + conditionals: Conditionals::default(), + out: Preprocessed::default(), + code: Vec::new(), + texts: HashMap::new(), + }; + run.file(path, 0, Origin::default()); + if run.conditionals.open() { + run.diagnose(Origin::default(), "`#if` without `#endif`"); + } + run.out.code = run.code.join("\n"); + run.out +} + +struct Run<'p> { + pack: &'p Pack, + macros: Macros, + conditionals: Conditionals, + out: Preprocessed, + code: Vec, + /// Files already read: a guarded header is included many times. + texts: HashMap>, +} + +impl Run<'_> { + fn file(&mut self, path: &str, depth: usize, from: Origin) { + let cached = self + .texts + .entry(path.to_owned()) + .or_insert_with(|| self.pack.read_text(path)); + let Some(text) = cached.clone() else { + return self.diagnose(from, &format!("can't find `{path}`")); + }; + let file = match self.out.files.iter().position(|f| f == path) { + Some(i) => i, + None => { + self.out.files.push(path.to_owned()); + self.out.files.len() - 1 + } + } as u16; + let (text, comments) = strip_comments(&text); + let lines: Vec<&str> = text.split('\n').map(|l| l.trim_end_matches('\r')).collect(); + let mut comments = comments.into_iter().peekable(); + let mut n = 0; + while n < lines.len() { + let first = n; + let mut line = lines[n].to_owned(); + while line.ends_with('\\') && n + 1 < lines.len() { + line.pop(); + n += 1; + line.push_str(lines[n]); + } + n += 1; + let origin = Origin { + file, + line: first as u32 + 1, + }; + self.line(&line, origin, path, depth); + for k in first + 1..n { + self.emit( + String::new(), + Origin { + file, + line: k as u32 + 1, + }, + ); + } + while let Some((_, comment)) = comments.next_if(|(at, _)| *at < n) { + if self.conditionals.active() { + self.out.comments.push((origin, comment)); + } + } + } + } + + fn line(&mut self, line: &str, origin: Origin, path: &str, depth: usize) { + let Some(after) = line.trim_start().strip_prefix('#') else { + let code = if self.conditionals.active() { + self.macros.define("__LINE__", origin.line.to_string()); + self.macros.expand(line) + } else { + String::new() + }; + return self.emit(code, origin); + }; + self.emit(String::new(), origin); + let Some((word, rest)) = split_directive(after) else { + return self.diagnose(origin, &format!("unknown directive `#{}`", after.trim())); + }; + match self.conditionals.apply(word, rest, &self.macros) { + Err(e) => return self.diagnose(origin, &e), + Ok(true) => return, + Ok(false) if !self.conditionals.active() => return, + Ok(false) => {} + } + match word { + "define" => self.define(rest, origin), + "undef" => self.macros.undef(rest), + "include" => { + let target = rest.trim_matches(|c| c == '"' || c == '<' || c == '>'); + match resolve(path, target) { + _ if depth + 1 >= MAX_DEPTH => self.diagnose( + origin, + &format!("`{target}`: includes nest more than {MAX_DEPTH} deep"), + ), + Some(file) => { + // The include line's own (empty) output line stays; the file follows. + self.file(&file, depth + 1, origin); + } + None => self.diagnose(origin, &format!("`{target}` is outside the pack")), + } + } + "version" if depth == 0 && self.out.version.is_none() => { + let mut words = rest.split_whitespace(); + let number = words.next().and_then(|v| v.parse().ok()).unwrap_or(110); + self.out.version = Some((number, words.next().map(str::to_owned))); + self.macros.define("__VERSION__", number.to_string()); + } + "extension" => self.out.extensions.push(rest.to_owned()), + "error" => self.diagnose(origin, &format!("#error {rest}")), + "version" | "pragma" | "line" | "" => {} + _ => self.diagnose(origin, &format!("unknown directive `#{word}`")), + } + } + + /// `#define NAME body` or `#define NAME(a, b) body` (no space before the `(`). + fn define(&mut self, rest: &str, origin: Origin) { + let len = rest + .find(|c: char| !(c.is_ascii_alphanumeric() || c == '_')) + .unwrap_or(rest.len()); + let (name, tail) = rest.split_at(len); + if name.is_empty() { + return self.diagnose(origin, "`#define` without a name"); + } + if let Some(params) = tail.strip_prefix('(') { + let Some((params, body)) = params.split_once(')') else { + return self.diagnose(origin, &format!("`#define {name}(` without `)`")); + }; + let params = params + .split(',') + .map(|p| p.trim().to_owned()) + .filter(|p| !p.is_empty()) + .collect(); + self.macros.define_function(name, params, body.trim()); + } else { + self.macros.define(name, tail.trim()); + } + } + + fn emit(&mut self, code: String, origin: Origin) { + self.code.push(code); + self.out.lines.push(origin); + } + + fn diagnose(&mut self, origin: Origin, message: &str) { + self.out.diagnostics.push(Diagnostic { + origin, + message: message.to_owned(), + }); + } +} + +/// An `#include` path: from the `shaders` folder with a leading `/`, else from the including +/// file's folder (spec 1.4). `None` when it climbs out of the pack. +fn resolve(from_file: &str, target: &str) -> Option { + match target.strip_prefix('/') { + Some(absolute) => normalize(absolute), + None => { + let dir = from_file.rsplit_once('/').map_or("", |(dir, _)| dir); + normalize(&format!("{dir}/{target}")) + } + } +} + +/// `text` with comments replaced by spaces (new lines kept, so line numbers hold), and the body of +/// each block comment with the index of the line it starts on. +fn strip_comments(text: &str) -> (String, Vec<(usize, String)>) { + let mut out = String::with_capacity(text.len()); + let mut comments = Vec::new(); + let (mut rest, mut line) = (text, 0); + while let Some(at) = rest.find('/') { + let (before, from) = rest.split_at(at); + out.push_str(before); + line += before.matches('\n').count(); + if let Some(body) = from.strip_prefix("//") { + let end = body.find('\n').unwrap_or(body.len()); + rest = &body[end..]; + } else if let Some(body) = from.strip_prefix("/*") { + let end = body.find("*/").unwrap_or(body.len()); + let comment = &body[..end]; + comments.push((line, comment.to_owned())); + let newlines = comment.matches('\n').count(); + line += newlines; + out.push(' '); + out.extend(std::iter::repeat_n('\n', newlines)); + rest = body.get(end + 2..).unwrap_or(""); + } else { + out.push('/'); + rest = &from[1..]; + } + } + out.push_str(rest); + (out, comments) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::pack::tests::zip; + + fn run(files: &[(&str, &str)]) -> Preprocessed { + let files: Vec<(String, &str)> = files + .iter() + .map(|(p, t)| (format!("shaders/{p}"), *t)) + .collect(); + let files: Vec<(&str, &str)> = files.iter().map(|(p, t)| (p.as_str(), *t)).collect(); + let pack = Pack::from_zip_bytes("P.zip", zip(&files)).unwrap(); + preprocess(&pack, "program/final.fsh", &Macros::standard()) + } + + fn code_lines(p: &Preprocessed) -> Vec<(&str, String)> { + p.code + .lines() + .enumerate() + .filter(|(_, l)| !l.trim().is_empty()) + .map(|(i, l)| (l.trim(), p.locate(i))) + .collect() + } + + #[test] + fn includes_resolve_and_lines_map_back() { + let p = run(&[ + ( + "program/final.fsh", + "#version 330 compatibility\n#include \"/lib/a.glsl\"\nvoid main() {}\n", + ), + ( + "lib/a.glsl", + "#ifndef A\n#define A\n#include \"b.glsl\"\nfloat a;\n#endif\n", + ), + ("lib/b.glsl", "// b\nfloat b;\n#include \"a.glsl\"\n"), + ]); + assert_eq!(p.version, Some((330, Some("compatibility".into())))); + assert_eq!( + code_lines(&p), + [ + ("float b;", "lib/b.glsl:2".into()), + ("float a;", "lib/a.glsl:4".into()), + ("void main() {}", "program/final.fsh:3".into()), + ] + ); + assert!(p.diagnostics.is_empty(), "{:?}", p.diagnostics); + } + + #[test] + fn missing_and_runaway_includes_are_diagnosed_where_they_stand() { + let p = run(&[( + "program/final.fsh", + "#ifdef NOPE\n#include \"gone.glsl\"\n#endif\n#include \"gone.glsl\"\n#include \"final.fsh\"\n", + )]); + let messages: Vec = p + .diagnostics + .iter() + .map(|d| format!("{}: {}", d.origin.line, d.message)) + .collect(); + assert_eq!(messages[0], "4: can't find `program/gone.glsl`"); + assert!( + messages + .last() + .unwrap() + .ends_with("includes nest more than 10 deep") + ); + assert!( + run(&[("program/final.fsh", "#include \"../../x\"")]).diagnostics[0] + .message + .contains("outside") + ); + } + + #[test] + fn macros_expand_like_the_glsl_preprocessor() { + let p = run(&[( + "program/final.fsh", + "#define SQ(x) ((x) * (x))\n#define CAT(a, b) a ## b\n#define N 4\n#define \\\n LONG 1\n\ + float v = SQ(N + 1) + CAT(N, 2) + LONG;\nint l = __LINE__;\n#if MC_VERSION > 12100 && defined MC_RENDER_STAGE_SKY\nint ok;\n#else\nint bad;\n#endif\n", + )]); + let lines: Vec<&str> = code_lines(&p).into_iter().map(|(l, _)| l).collect(); + assert_eq!( + lines, + [ + "float v = ((4 + 1) * (4 + 1)) + N2 + 1;", + "int l = 7;", + "int ok;" + ] + ); + assert_eq!(p.lines.len(), p.code.split('\n').count()); + } + + #[test] + fn comments_extensions_and_errors() { + let p = run(&[( + "program/final.fsh", + "#version 120\n#extension GL_ARB_shader_texture_lod : enable\n/* DRAWBUFFERS:04 */\n#if 0\n/* RENDERTARGETS: 1 */\n#error dead\n#endif\n\ + /* multi\nline */ float x; // gone\n#error live\n#bogus\n", + )]); + assert_eq!(p.extensions, ["GL_ARB_shader_texture_lod : enable"]); + let comments: Vec<&str> = p.comments.iter().map(|(_, c)| c.trim()).collect(); + assert_eq!(comments, ["DRAWBUFFERS:04", "multi\nline"]); + assert_eq!(code_lines(&p), [("float x;", "program/final.fsh:9".into())]); + let messages: Vec<&str> = p.diagnostics.iter().map(|d| d.message.as_str()).collect(); + assert_eq!(messages, ["#error live", "unknown directive `#bogus`"]); + } +} diff --git a/crates/horizon-shaders/tests/real_packs.rs b/crates/horizon-shaders/tests/real_packs.rs index e0ad38c25..c252df6e3 100644 --- a/crates/horizon-shaders/tests/real_packs.rs +++ b/crates/horizon-shaders/tests/real_packs.rs @@ -5,7 +5,7 @@ use std::path::Path; use std::time::Instant; -use horizon_shaders::{Lang, Macros, Pack, Properties, discover}; +use horizon_shaders::{Lang, Macros, Pack, Properties, discover, preprocess}; #[test] #[ignore = "needs HORIZON_TEST_SHADER_PACKS"] @@ -52,6 +52,40 @@ fn real_packs_read() { for n in &shaders.notes { println!(" shaders.properties:{}: {}", n.line, n.message); } + // Every stage file of the base and dimension folders through the preprocessor. + let start = Instant::now(); + let (mut stages, mut lines, mut diagnostics) = (0, 0, Vec::new()); + for dir in ["", "world0", "world-1", "world1"] { + for file in pack.list(dir) { + if [".vsh", ".fsh", ".gsh", ".csh"] + .iter() + .any(|e| file.ends_with(e)) + { + let path = if dir.is_empty() { + file + } else { + format!("{dir}/{file}") + }; + let p = preprocess(&pack, &path, ¯os); + stages += 1; + lines += p.lines.len(); + diagnostics.extend(p.diagnostics.iter().map(|d| { + let file = &p.files[d.origin.file as usize]; + format!("{file}:{}: {}", d.origin.line, d.message) + })); + } + } + } + println!( + " {stages} stage files preprocessed, {lines} lines out, {} diagnostics, {:.1} ms", + diagnostics.len(), + start.elapsed().as_secs_f64() * 1000.0 + ); + diagnostics.sort(); + diagnostics.dedup(); + for d in diagnostics.iter().take(8) { + println!(" {d}"); + } assert!(!shaders.entries.is_empty(), "{}: no keys read", f.name); read += 1; } From 7c1dd4981062ce9e0cb0db0b7d919109243b2470 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 24 Oct 2026 21:13:00 +0200 Subject: [PATCH 009/107] Renderer: leave out solid quads that face away from the eye Meshing sorts each section's and distant tile's solid quads into seven buckets: one per axis direction (all four corners in one axis plane, both triangles wound the same way) and one for everything else, and keeps each axis bucket's extreme plane. A quad facing +X at x = p is a back face from an eye at x <= p, which the solid pipeline culls anyway, so the frame draws only the buckets that can face the eye, merged into runs of adjacent buckets: one to three indirect draws per section on the existing page-run, occlusion-culling and flat paths. The eye comes from the frame's view-projection, so view bobbing is exact. The picture does not change; `horizon-ctl section-sides on|off` switches it for A/B checks. Written from scratch from the general back-face technique; no other renderer's code or Horizon's removed code read. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/control.rs | 10 + .../horizon-client/src/game/render_control.rs | 1 + crates/horizon-client/src/gfx.rs | 94 ++++- crates/horizon-client/src/lod/mesh.rs | 6 +- crates/horizon-client/src/mesher.rs | 14 + crates/horizon-client/src/mesher/sides.rs | 346 ++++++++++++++++++ 6 files changed, 455 insertions(+), 16 deletions(-) create mode 100644 crates/horizon-client/src/mesher/sides.rs diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index 205a66074..6fbd984fd 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -226,6 +226,16 @@ impl Game<'_> { } Ok(Some(json!({ "ok": true, "cull": r.section_cull() }))) } + "section-sides" => { + let r = self.renderer.as_mut().ok_or("no renderer")?; + match words.get(1).copied() { + None => {} + Some("on") => r.set_section_sides(true), + Some("off") => r.set_section_sides(false), + Some(other) => return Err(format!("unknown section sides mode {other}").into()), + } + Ok(Some(json!({ "ok": true, "sides": r.section_sides() }))) + } "sculk" => Ok(Some(self.sculk_command(&words)?)), "status" => Ok(Some(self.control_status())), "pad" => Ok(Some(self.pad_command(&words)?)), diff --git a/crates/horizon-client/src/game/render_control.rs b/crates/horizon-client/src/game/render_control.rs index 31a7a33a0..0a2f9b715 100644 --- a/crates/horizon-client/src/game/render_control.rs +++ b/crates/horizon-client/src/game/render_control.rs @@ -13,6 +13,7 @@ pub const HELP: &[&str] = &[ "signedit X Y Z [front|back] open the sign editor for the sign at X Y Z", "section-draws [flat|indirect] how terrain sections are drawn (A/B checks); replies the mode", "section-cull [on|off] occlusion culling of solid sections (A/B checks); replies the mode", + "section-sides [on|off] leave out solid quads facing away from the eye (A/B checks); replies the mode", ]; impl Game<'_> { diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 9ed0bd8a5..e4c3367f9 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -6,7 +6,7 @@ use crate::{ environment::Sky, graphics::GraphicsApi, hud::{HudBatch, HudRenderer}, - mesher::{SectionMesh, TerrainVertex, Vertex}, + mesher::{SectionMesh, Sides, TerrainVertex, Vertex}, sprites::SpriteAnimation, }; use glam::{DVec3, Mat4, Vec3, Vec4}; @@ -156,6 +156,8 @@ struct SectionGpu { page: u32, start: u64, solid: u32, + /// Where the solid quads' buckets by facing side end (`mesher::Sides`). + sides: Sides, translucent: u32, sort: translucency::SortState, sorted_indices: Option<(u32, u64)>, @@ -323,6 +325,8 @@ pub struct Renderer { /// counts reach a multi-draw's entries (Vulkan, Metal). cull: Option, cull_on: bool, + /// Solid quads facing away from the eye are left out (`mesher::Sides`). + sides_on: bool, sky: wgpu::RenderPipeline, dark_disc: wgpu::RenderPipeline, lines: wgpu::RenderPipeline, @@ -2202,6 +2206,7 @@ impl Renderer { translucent, flat_on: flat.is_some(), cull_on: cull.is_some(), + sides_on: true, cull, flat, sky, @@ -2451,6 +2456,14 @@ impl Renderer { self.flat_on = on; Ok(()) } + /// Whether solid quads facing away from the eye are left out. + pub fn section_sides(&self) -> bool { + self.sides_on + } + /// Switches leaving out solid quads facing away, for A/B checks. + pub fn set_section_sides(&mut self, on: bool) { + self.sides_on = on; + } /// Whether solid sections are occlusion culled (`cull`). pub fn section_cull(&self) -> bool { self.cull_on @@ -2573,6 +2586,11 @@ impl Renderer { page, start, solid, + sides: if mesh.sides.quads() == solid { + mesh.sides + } else { + Sides::unsorted(solid) + }, translucent, sort: translucency::SortState::new( mesh.translucent @@ -3039,6 +3057,9 @@ impl Renderer { }; let camera = frame.camera; let camera_section = camera_section(camera); + // Solid quads facing away from the eye are left out: the bob moves + // the eye off `camera`, so it is found from the projection. + let eye = projection_center(view_proj).filter(|_| self.sides_on); let culling = !frame.title && frame.cave_culling; if culling { crate::spans::time("occlusion", || { @@ -3127,11 +3148,13 @@ impl Renderer { }; let start = draws.len(); let instances = u32::from(first_phase); - let indices = s.solid * 6; - if indices > 0 { - draws.push([indices, instances, 0, s.start as u32, i as u32]); + let mut indices = 0; + facing_runs(&s.sides, eye, v.origin, |first, quads| { + indices += quads * 6; + let base = s.start as u32 + first * 4; + draws.push([quads * 6, instances, 0, base, i as u32]); pages.push((s.page, None)); - } + }); let runs = if first_phase { &mut self.solid_runs } else { @@ -3168,16 +3191,12 @@ impl Renderer { for (i, v) in visible_lods.iter().enumerate() { let s = &self.lods[&v.key].mesh; let start = self.draws.len(); - if s.solid > 0 { - self.draws.push([ - s.solid * 6, - 1, - 0, - s.start as u32, - (visible.len() + i) as u32, - ]); + let instance = (visible.len() + i) as u32; + facing_runs(&s.sides, eye, v.origin, |first, quads| { + let base = s.start as u32 + first * 4; + self.draws.push([quads * 6, 1, 0, base, instance]); self.draw_pages.push((s.page, None)); - } + }); self.solid_runs .push(v.distance, s.page, start, self.draws.len() - start, [0; 3]); } @@ -4196,6 +4215,34 @@ fn world_projection(fov_degrees: f32, aspect: f32, far: f32) -> Mat4 { Mat4::perspective_rh(fov_degrees.to_radians(), aspect, far, 0.05) } +/// Where a perspective `view_proj` sees from, in its own (camera-relative) +/// space: the point whose clip x, y and w are all 0. `None` for a +/// projection without one (orthographic). +fn projection_center(view_proj: Mat4) -> Option { + let m = view_proj.as_dmat4(); + let rows = [m.row(0), m.row(1), m.row(3)]; + let a = glam::DMat3::from_cols( + DVec3::new(rows[0].x, rows[1].x, rows[2].x), + DVec3::new(rows[0].y, rows[1].y, rows[2].y), + DVec3::new(rows[0].z, rows[1].z, rows[2].z), + ); + let det = a.determinant(); + if !det.is_normal() { + return None; + } + let b = DVec3::new(rows[0].w, rows[1].w, rows[2].w); + Some((a.inverse() * -b).as_vec3()) +} +/// Calls `draw(first, quads)` for each run of a mesh's solid quads that can +/// face `eye` (`projection_center`), the mesh at the camera-relative +/// `origin`; all of them when there is no eye. +fn facing_runs(sides: &Sides, eye: Option, origin: [f32; 3], mut draw: impl FnMut(u32, u32)) { + match eye { + Some(eye) => sides.visible((eye - Vec3::from(origin)).to_array(), draw), + None if sides.quads() > 0 => draw(0, sides.quads()), + None => {} + } +} fn frustum(m: Mat4) -> [Vec4; 6] { let r = |i| m.row(i); let planes = [ @@ -4423,6 +4470,25 @@ mod tests { assert_eq!(super::outline_width(3840), 5.0); } #[test] + fn the_projection_center_follows_the_view_bob() { + use glam::{Mat4, Vec3}; + let projection = super::world_projection(70.0, 16.0 / 9.0, 512.0); + let view = crate::camera::view_matrix(30.0, 20.0); + let eye = super::projection_center(projection * view).unwrap(); + assert!(eye.length() < 1e-5, "{eye}"); + // A pose translated by the bob: the eye is where the pose's inverse + // takes the view's origin. + let bob = Mat4::from_rotation_z(0.02) * Mat4::from_translation(Vec3::new(0.03, -0.08, 0.0)); + let expected = (bob * view).inverse().transform_point3(Vec3::ZERO); + let eye = super::projection_center(projection * bob * view).unwrap(); + assert!(eye.distance(expected) < 1e-5, "{eye} {expected}"); + assert!(expected.length() > 0.05); + assert!( + super::projection_center(Mat4::orthographic_rh(-1.0, 1.0, -1.0, 1.0, 0.0, 1.0)) + .is_none() + ); + } + #[test] fn indirect_draws_are_validated_only_in_debug_builds_and_on_directx_12() { let validated = |backends| { super::instance_descriptor(backends) diff --git a/crates/horizon-client/src/lod/mesh.rs b/crates/horizon-client/src/lod/mesh.rs index 852f45e5b..bdd77fbea 100644 --- a/crates/horizon-client/src/lod/mesh.rs +++ b/crates/horizon-client/src/lod/mesh.rs @@ -67,7 +67,7 @@ impl Mesher { neighbors: &[Vec; 4], finer: &[TileKey], ) -> SectionMesh { - mesh_columns_with_edges( + let mut mesh = mesh_columns_with_edges( key, columns, covered, @@ -75,7 +75,9 @@ impl Mesher { neighbors, finer, |span, direction| self.material(span, direction, context), - ) + ); + mesh.sort_solid(); + mesh } fn material(&mut self, span: &Span, direction: Direction, context: &WorkerContext) -> Material { let styles = *self diff --git a/crates/horizon-client/src/mesher.rs b/crates/horizon-client/src/mesher.rs index ec6f2ae73..d1b27bec9 100644 --- a/crates/horizon-client/src/mesher.rs +++ b/crates/horizon-client/src/mesher.rs @@ -5,6 +5,7 @@ //! follow `FluidRenderer` (see `fluid`). mod fluid; mod lighting; +mod sides; use crate::{ assets::{Assets, RenderAssets}, biome::Biomes, @@ -20,6 +21,7 @@ use horizon_world::{ fluid::{FluidKind, FluidState}, registry::BlockStateId, }; +pub use sides::Sides; use std::sync::Arc; /// An entity, item and effect vertex (the entity pipelines), with a face @@ -114,6 +116,9 @@ mod vertex_tests { } pub struct SectionMesh { pub solid: Vec, + /// The buckets `solid` is sorted in by the side its quads face + /// (`sort_solid`; empty until then). + pub sides: Sides, pub translucent: Vec, /// VisGraph: bit `a * 6 + b` is set when faces `a` and `b` (Direction /// order) are connected through non-opaque blocks. @@ -127,12 +132,20 @@ impl Default for SectionMesh { fn default() -> Self { Self { solid: Vec::new(), + sides: Sides::default(), translucent: Vec::new(), visibility: ALL_VISIBLE, block_entities: Vec::new(), } } } +impl SectionMesh { + /// Sorts the solid quads by the side they face, for the renderer to + /// leave out those facing away from the eye (`sides`). + pub fn sort_solid(&mut self) { + self.sides = sides::sort(&mut self.solid); + } +} pub const ALL_VISIBLE: u64 = (1 << 36) - 1; /// VisGraph.resolve: flood fill non-opaque cells and connect the faces each region touches. fn visibility(pad: &Padded, info: &[Info]) -> u64 { @@ -798,6 +811,7 @@ impl Mesher<'_> { } } } + out.sort_solid(); out } /// `BlockColors` tint source `index`, or `None` for an untinted (-1) layer. diff --git a/crates/horizon-client/src/mesher/sides.rs b/crates/horizon-client/src/mesher/sides.rs new file mode 100644 index 000000000..797d60127 --- /dev/null +++ b/crates/horizon-client/src/mesher/sides.rs @@ -0,0 +1,346 @@ +//! A mesh's solid quads sorted by the side they face, so that the renderer +//! can leave out the quads turned away from the eye before the GPU shades +//! their vertices. +//! +//! A quad in the plane `x = p` whose corners go counter-clockwise seen from +//! +X faces +X. From an eye at `x < p` it is seen from behind, and at +//! `x = p` edge on; the solid pipeline culls back faces, so it draws nothing +//! from there. A bucket of such quads draws nothing at all while the eye is +//! at or below the lowest of their planes, and likewise for the other five +//! axis directions. The seventh bucket holds every other quad (sloped, +//! rotated, degenerate, or whose two triangles wind opposite ways): it is +//! drawn from everywhere. +//! +//! The buckets lie one after another in the mesh, in an order chosen so that +//! the buckets an eye can see are usually one or two runs of quads: one draw +//! each. Inside a bucket quads keep their model order, so coplanar quads +//! facing one way (an overlay on its base) still draw in that order. +use super::TerrainVertex; + +/// The buckets in mesh order: +X, +Z, other, +Y, -Z, -X, -Y. Seen from +/// above (+Y, the commonest case), each pair of horizontal sides with a +/// common corner is then one run, or two. +const BUCKETS: [Bucket; 7] = [ + Bucket::Side(0, true), + Bucket::Side(2, true), + Bucket::Other, + Bucket::Side(1, true), + Bucket::Side(2, false), + Bucket::Side(0, false), + Bucket::Side(1, false), +]; +const OTHER: usize = 2; +/// How far, in blocks, the eye must be behind a bucket's planes before the +/// bucket is left out. The renderer computes both the eye and the section +/// origins in `f32`, with errors far below this. +const MARGIN: f32 = 1.0 / 16.0; + +#[derive(Clone, Copy)] +enum Bucket { + /// Quads in a plane across `axis` facing its positive (`true`) or + /// negative direction. + Side(usize, bool), + Other, +} + +/// Where each bucket of a mesh's solid quads ends, and the planes that tell +/// whether it can face the eye. +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub struct Sides { + /// The solid quads up to the end of each bucket, in `BUCKETS` order. + ends: [u32; 7], + /// Per bucket facing a positive direction, its lowest plane; facing a + /// negative one, its highest (unused for the other bucket). + planes: [f32; 7], +} + +impl Sides { + /// `quads` solid quads in one bucket drawn from everywhere: for meshes + /// that were not sorted. + pub fn unsorted(quads: u32) -> Self { + let mut ends = [0; 7]; + ends[OTHER..].fill(quads); + Self { + ends, + planes: [0.0; 7], + } + } + + /// The number of solid quads the buckets hold. + pub fn quads(&self) -> u32 { + self.ends[6] + } + + /// Calls `draw(first, count)` for each run of quads an eye at `eye` + /// (relative to the mesh's origin) can see the front of, nearest the + /// start first. Empty buckets do not break a run. + pub fn visible(&self, eye: [f32; 3], mut draw: impl FnMut(u32, u32)) { + let mut start = 0; + let mut run: Option = None; + for (i, bucket) in BUCKETS.iter().enumerate() { + let end = self.ends[i]; + if end == start { + continue; + } + let seen = match *bucket { + Bucket::Side(axis, true) => eye[axis] > self.planes[i] - MARGIN, + Bucket::Side(axis, false) => eye[axis] < self.planes[i] + MARGIN, + Bucket::Other => true, + }; + match (seen, run) { + (true, None) => run = Some(start), + (false, Some(first)) => { + draw(first, start - first); + run = None; + } + _ => {} + } + start = end; + } + if let Some(first) = run { + draw(first, start - first); + } + } +} + +/// The bucket of a quad: the axis plane it lies in and the side its +/// triangles (corners 0, 1, 2 and 0, 2, 3, as the renderer draws them) face. +fn bucket(quad: &[TerrainVertex; 4]) -> usize { + let p = quad.map(|v| v.pos); + for axis in 0..3 { + if p.iter().any(|v| v[axis] != p[0][axis]) { + continue; + } + // The normal's component along `axis`, twice each triangle's area. + let (u, w) = ((axis + 1) % 3, (axis + 2) % 3); + let area = |a: usize, b: usize, c: usize| { + (p[b][u] - p[a][u]) * (p[c][w] - p[a][w]) - (p[b][w] - p[a][w]) * (p[c][u] - p[a][u]) + }; + let (first, second) = (area(0, 1, 2), area(0, 2, 3)); + let positive = first >= 0.0 && second >= 0.0; + let negative = first <= 0.0 && second <= 0.0; + if positive == negative { + // Opposite windings, or no area at all. + return OTHER; + } + return BUCKETS + .iter() + .position(|b| matches!(*b, Bucket::Side(a, s) if a == axis && s == positive)) + .expect("every side has a bucket"); + } + OTHER +} + +/// Reorders `solid`'s quads by bucket, keeping their order inside each. +pub fn sort(solid: &mut Vec) -> Sides { + let (quads, rest) = solid.as_chunks::<4>(); + debug_assert!(rest.is_empty(), "solid vertices come in quads"); + let buckets: Vec = quads.iter().map(|q| bucket(q) as u8).collect(); + let mut counts = [0u32; 7]; + let mut planes = [0f32; 7]; + for (quad, &b) in quads.iter().zip(&buckets) { + let b = usize::from(b); + if let Bucket::Side(axis, positive) = BUCKETS[b] { + let p = quad[0].pos[axis]; + planes[b] = match (counts[b], positive) { + (0, _) => p, + (_, true) => planes[b].min(p), + (_, false) => planes[b].max(p), + }; + } + counts[b] += 1; + } + let mut ends = [0u32; 7]; + let mut total = 0; + for (end, count) in ends.iter_mut().zip(counts) { + total += count; + *end = total; + } + if counts.iter().filter(|&&c| c > 0).count() > 1 { + let mut next: [u32; 7] = std::array::from_fn(|b| ends[b] - counts[b]); + let mut sorted = vec![quads[0]; quads.len()]; + for (quad, &b) in quads.iter().zip(&buckets) { + let slot = &mut next[usize::from(b)]; + sorted[*slot as usize] = *quad; + *slot += 1; + } + *solid = sorted.into_flattened(); + } + Sides { ends, planes } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn quad(corners: [[f32; 3]; 4], layer: u16) -> [TerrainVertex; 4] { + corners.map(|p| TerrainVertex::new(p, [0.0; 2], layer, 0, 0)) + } + + /// A unit block's six faces at `origin`, wound counter-clockwise seen + /// from outside, as block models are. + fn cube(origin: [f32; 3], layer: u16) -> Vec { + let [x, y, z] = origin; + let (a, b, c) = (x + 1.0, y + 1.0, z + 1.0); + [ + [[a, y, z], [a, b, z], [a, b, c], [a, y, c]], // +X + [[x, y, z], [x, y, c], [x, b, c], [x, b, z]], // -X + [[x, b, z], [x, b, c], [a, b, c], [a, b, z]], // +Y + [[x, y, z], [a, y, z], [a, y, c], [x, y, c]], // -Y + [[x, y, c], [a, y, c], [a, b, c], [x, b, c]], // +Z + [[x, y, z], [x, b, z], [a, b, z], [a, y, z]], // -Z + ] + .into_iter() + .flat_map(|q| quad(q, layer)) + .collect() + } + + fn runs(sides: &Sides, eye: [f32; 3]) -> Vec<(u32, u32)> { + let mut out = Vec::new(); + sides.visible(eye, |first, count| out.push((first, count))); + out + } + + /// The quads of `solid` whose triangles face `eye`, by their layer. + fn front_facing(solid: &[TerrainVertex], eye: [f32; 3]) -> Vec { + let sub = |a: [f32; 3], b: [f32; 3]| [a[0] - b[0], a[1] - b[1], a[2] - b[2]]; + solid + .as_chunks::<4>() + .0 + .iter() + .filter(|q| { + [[0, 1, 2], [0, 2, 3]].iter().any(|t| { + let (e1, e2) = (sub(q[t[1]].pos, q[t[0]].pos), sub(q[t[2]].pos, q[t[0]].pos)); + let n = [ + e1[1] * e2[2] - e1[2] * e2[1], + e1[2] * e2[0] - e1[0] * e2[2], + e1[0] * e2[1] - e1[1] * e2[0], + ]; + let to_eye = sub(eye, q[t[0]].pos); + n[0] * to_eye[0] + n[1] * to_eye[1] + n[2] * to_eye[2] > 0.0 + }) + }) + .map(|q| q[0].layer) + .collect() + } + + #[test] + fn cube_faces_go_to_their_buckets_in_order() { + let mut solid = cube([3.0, 4.0, 5.0], 0); + for (i, v) in solid.iter_mut().enumerate() { + v.layer = (i / 4) as u16; + } + let sides = sort(&mut solid); + assert_eq!(sides.ends, [1, 2, 2, 3, 4, 5, 6]); + let layers: Vec = solid.iter().step_by(4).map(|v| v.layer).collect(); + // +X, +Z, +Y, -Z, -X, -Y. + assert_eq!(layers, [0, 4, 2, 5, 1, 3]); + assert_eq!(sides.planes[0], 4.0); // +X at x = 4 + assert_eq!(sides.planes[5], 3.0); // -X at x = 3 + assert_eq!(sides.planes[3], 5.0); // +Y at y = 5 + } + + #[test] + fn the_drawn_runs_hold_every_quad_that_faces_the_eye() { + // Blocks across a section, with a sloped quad and a bowtie. + let mut solid = Vec::new(); + let mut layer = 0; + for origin in [ + [0.0, 0.0, 0.0], + [7.0, 3.0, 12.0], + [15.0, 15.0, 15.0], + [2.0, 9.0, 4.0], + ] { + for q in cube(origin, 0).chunks(4) { + solid.extend(q.iter().map(|v| TerrainVertex { layer, ..*v })); + layer += 1; + } + } + let slope = [ + [1.0, 1.0, 1.0], + [2.0, 1.0, 1.0], + [2.0, 2.0, 2.0], + [1.0, 2.0, 2.0], + ]; + let bowtie = [ + [5.0, 1.0, 1.0], + [5.0, 2.0, 2.0], + [5.0, 1.0, 2.0], + [5.0, 2.0, 1.0], + ]; + for corners in [slope, bowtie] { + solid.extend(quad(corners, layer)); + layer += 1; + } + let sides = sort(&mut solid); + assert_eq!(sides.quads(), u32::from(layer)); + let eyes = [-20.0f32, -0.5, 0.0, 3.5, 8.0, 15.99, 16.0, 30.0]; + for &x in &eyes { + for &y in &eyes { + for &z in &eyes { + let eye = [x, y, z]; + let mut drawn = Vec::new(); + for (first, count) in runs(&sides, eye) { + drawn.extend( + solid[first as usize * 4..(first + count) as usize * 4] + .iter() + .step_by(4) + .map(|v| v.layer), + ); + } + for layer in front_facing(&solid, eye) { + assert!(drawn.contains(&layer), "{eye:?} misses quad {layer}"); + } + } + } + } + } + + #[test] + fn runs_merge_across_hidden_and_empty_buckets() { + let mut solid = cube([0.0; 3], 0); + let sides = sort(&mut solid); + // From above, +X and +Z: +X, +Z, (no other), +Y is one run. + assert_eq!(runs(&sides, [8.0, 8.0, 8.0]), [(0, 3)]); + // From below, -X and -Z: -Z, -X, -Y. + assert_eq!(runs(&sides, [-8.0, -8.0, -8.0]), [(3, 3)]); + // From above, -X and +Z: +Z, +Y, then -X. + assert_eq!(runs(&sides, [-8.0, 8.0, 8.0]), [(1, 2), (4, 1)]); + // Inside the block every face is seen from behind. + assert!(runs(&sides, [0.5; 3]).is_empty()); + // Level with its top, the eye may see it: the margin keeps it. + assert_eq!(runs(&sides, [0.5, 1.0, 0.5]), [(2, 1)]); + } + + #[test] + fn unsorted_meshes_draw_everything() { + let sides = Sides::unsorted(9); + assert_eq!(sides.quads(), 9); + assert_eq!(runs(&sides, [-100.0; 3]), [(0, 9)]); + assert!(runs(&Sides::unsorted(0), [0.0; 3]).is_empty()); + } + + #[test] + fn one_bucket_keeps_the_mesh_as_it_is() { + let mut solid: Vec<_> = (0..3) + .flat_map(|i| { + quad( + [ + [1.0, 0.0, 0.0], + [1.0, 1.0, 0.0], + [1.0, 1.0, 1.0], + [1.0, 0.0, 1.0], + ], + i, + ) + }) + .collect(); + let before = solid.clone(); + let sides = sort(&mut solid); + assert_eq!(sides.ends, [3; 7]); + assert_eq!( + solid.iter().map(|v| v.layer).collect::>(), + before.iter().map(|v| v.layer).collect::>() + ); + } +} From d742d725f4ac51a55d8748ef646522c7b574de1b Mon Sep 17 00:00:00 2001 From: socksprox Date: Sat, 24 Oct 2026 21:29:08 +0200 Subject: [PATCH 010/107] =?UTF-8?q?Client:=20the=20boss-overlay=20?= =?UTF-8?q?=E2=80=94=20BossEvent=20bars,=20wither=20darkening=20and=20worl?= =?UTF-8?q?d=20fog?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The clientbound boss_event packet (add/update/remove/style/progress), a BossOverlay that renders each bar, darkens the world under a wither, and suppresses the create-the-world fog — vanilla's BossHealthOverlay and GameRenderer behaviour on a real server. Co-Authored-By: Devin --- crates/horizon-client/src/boss_overlay.rs | 201 +++++++ crates/horizon-client/src/bossbar.rs | 108 ++-- crates/horizon-client/src/fog.rs | 29 + crates/horizon-client/src/game.rs | 15 +- crates/horizon-client/src/game/bossbars.rs | 553 ++++++++++++++++-- crates/horizon-client/src/game/dragon.rs | 46 +- crates/horizon-client/src/game/raids.rs | 74 ++- crates/horizon-client/src/game/remote.rs | 4 + crates/horizon-client/src/game/render_tick.rs | 5 +- crates/horizon-client/src/game/wither.rs | 41 -- crates/horizon-client/src/gfx.rs | 6 + crates/horizon-client/src/lib.rs | 1 + crates/horizon-protocol/src/packets/play.rs | 156 +++++ 13 files changed, 1055 insertions(+), 184 deletions(-) create mode 100644 crates/horizon-client/src/boss_overlay.rs diff --git a/crates/horizon-client/src/boss_overlay.rs b/crates/horizon-client/src/boss_overlay.rs new file mode 100644 index 000000000..e51a59668 --- /dev/null +++ b/crates/horizon-client/src/boss_overlay.rs @@ -0,0 +1,201 @@ +//! `BossHealthOverlay` and `LerpingBossEvent`: the boss bars a client is +//! showing, in the order their add packets arrived. +use crate::{hud_overlays::BossBar, text::Text}; + +/// `LerpingBossEvent.LERP_MILLISECONDS`. +const LERP_MS: u128 = 100; + +/// `Util.getMillis`. +pub fn now_millis() -> u128 { + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|d| d.as_millis()) + .unwrap_or(0) +} + +/// One `LerpingBossEvent`. +#[derive(Clone, Debug)] +struct Event { + id: u128, + name: Text, + /// The progress the lerp started from (`progress`). + progress: f32, + /// `targetPercent`. + target: f32, + /// `setTime`. + set_at: u128, + color: u8, + overlay: u8, + darken: bool, + music: bool, + fog: bool, +} +impl Event { + /// `LerpingBossEvent.getProgress`. + fn displayed(&self, now: u128) -> f32 { + let t = ((now.saturating_sub(self.set_at)) as f32 / LERP_MS as f32).clamp(0.0, 1.0); + self.progress + (self.target - self.progress) * t + } + /// `LerpingBossEvent.setProgress`: the bar starts the next lerp from + /// whatever it is showing now. + fn set_progress(&mut self, progress: f32, now: u128) { + self.progress = self.displayed(now); + self.target = progress; + self.set_at = now; + } +} + +/// `BossHealthOverlay`: the events, plus `GameRenderer`'s world darkening. +#[derive(Clone, Debug, Default)] +pub struct BossOverlay { + events: Vec, + /// `bossOverlayWorldDarkening` and the previous tick's value. + darken: f32, + darken_o: f32, +} +impl BossOverlay { + pub fn reset(&mut self) { + *self = Self::default(); + } + /// `Handler.add`. An id already shown keeps its place. + pub fn add( + &mut self, + id: u128, + name: Text, + progress: f32, + color: u8, + overlay: u8, + darken: bool, + music: bool, + fog: bool, + now: u128, + ) { + let event = Event { + id, + name, + progress, + target: progress, + set_at: now, + color: color.min(6), + overlay: overlay.min(4), + darken, + music, + fog, + }; + if let Some(slot) = self.events.iter_mut().find(|e| e.id == id) { + *slot = event; + } else { + self.events.push(event); + } + } + pub fn remove(&mut self, id: u128) { + self.events.retain(|e| e.id != id); + } + pub fn set_progress(&mut self, id: u128, progress: f32, now: u128) { + if let Some(e) = self.events.iter_mut().find(|e| e.id == id) { + e.set_progress(progress, now); + } + } + pub fn set_name(&mut self, id: u128, name: Text) { + if let Some(e) = self.events.iter_mut().find(|e| e.id == id) { + e.name = name; + } + } + pub fn set_style(&mut self, id: u128, color: u8, overlay: u8) { + if let Some(e) = self.events.iter_mut().find(|e| e.id == id) { + e.color = color.min(6); + e.overlay = overlay.min(4); + } + } + pub fn set_properties(&mut self, id: u128, darken: bool, music: bool, fog: bool) { + if let Some(e) = self.events.iter_mut().find(|e| e.id == id) { + e.darken = darken; + e.music = music; + e.fog = fog; + } + } + /// `GameRenderer.tick` while the level runs. + pub fn tick_darken(&mut self, running: bool) { + if !running { + return; + } + self.darken_o = self.darken; + if self.should_darken() { + self.darken = (self.darken + 0.05).min(1.0); + } else if self.darken > 0.0 { + self.darken = (self.darken - 0.0125).max(0.0); + } + } + /// `GameRenderer.bossOverlayWorldDarkening`. + pub fn darken_amount(&self, partial: f32) -> f32 { + let t = partial.clamp(0.0, 1.0); + self.darken_o + (self.darken - self.darken_o) * t + } + pub fn should_darken(&self) -> bool { + self.events.iter().any(|e| e.darken) + } + pub fn should_play_music(&self) -> bool { + self.events.iter().any(|e| e.music) + } + pub fn should_create_world_fog(&self) -> bool { + self.events.iter().any(|e| e.fog) + } + /// The bars `extractRenderState` walks, with the lerped progress. + pub fn bars(&self, now: u128) -> Vec { + self.events + .iter() + .map(|e| BossBar { + name: e.name.clone(), + progress: e.displayed(now), + color: e.color as usize, + overlay: e.overlay as usize, + }) + .collect() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn progress_lerps_over_a_tenth_of_a_second() { + let mut overlay = BossOverlay::default(); + overlay.add(1, Text::literal("W"), 0.0, 5, 0, true, false, false, 0); + overlay.set_progress(1, 1.0, 0); + assert!((overlay.bars(50)[0].progress - 0.5).abs() < 1.0e-5); + assert!((overlay.bars(100)[0].progress - 1.0).abs() < 1.0e-5); + // A new target starts from the value on screen. + overlay.set_progress(1, 0.0, 50); + assert!((overlay.bars(150)[0].progress - 0.0).abs() < 1.0e-5); + } + + #[test] + fn darken_ramps_and_fades() { + let mut overlay = BossOverlay::default(); + overlay.add(1, Text::literal("W"), 1.0, 5, 0, true, false, false, 0); + overlay.tick_darken(true); + assert!((overlay.darken_amount(1.0) - 0.05).abs() < 1.0e-5); + overlay.remove(1); + overlay.tick_darken(true); + assert!((overlay.darken - 0.0375).abs() < 1.0e-5); + assert!(!overlay.should_play_music()); + } + + #[test] + fn bars_keep_arrival_order() { + let mut overlay = BossOverlay::default(); + overlay.add(2, Text::literal("b"), 1.0, 0, 0, false, true, true, 0); + overlay.add(1, Text::literal("a"), 1.0, 0, 0, false, false, false, 0); + overlay.add(2, Text::literal("c"), 0.5, 1, 2, false, true, true, 0); + let names: Vec<_> = overlay + .bars(0) + .into_iter() + .map(|b| b.name.plain()) + .collect(); + assert_eq!(names, ["c", "a"]); + assert!(overlay.should_create_world_fog()); + overlay.remove(2); + assert!(!overlay.should_create_world_fog()); + } +} diff --git a/crates/horizon-client/src/bossbar.rs b/crates/horizon-client/src/bossbar.rs index eab31a5e5..2ecdb25e9 100644 --- a/crates/horizon-client/src/bossbar.rs +++ b/crates/horizon-client/src/bossbar.rs @@ -14,40 +14,64 @@ pub const STYLES: [&str; 5] = [ "notched_12", "notched_20", ]; -/// `BossBarColor.getFormatting`. +/// `BossBarColor.getFormatting`. Purple is dark blue in 26.3. pub fn color_rgb(color: &str) -> u32 { let name = match color { "pink" => "red", "red" => "dark_red", - "purple" => "dark_purple", + "purple" => "dark_blue", other => other, }; text::named_color(name).unwrap_or(text::WHITE) } +/// A version-4 shaped id from a resource id. Saved boss events do not keep +/// their UUID (`CustomBossEvents` mints a new one on load). +pub fn stable_uuid(id: &str) -> u128 { + let mut h = 0xcbf29ce484222325u64; + for b in id.as_bytes() { + h ^= u64::from(*b); + h = h.wrapping_mul(0x100000001b3); + } + let most = (h as i64) & -61441 | 16384; + let least = (h.rotate_left(17) as i64) & 4_611_686_018_427_387_903 | i64::MIN; + ((most as u64 as u128) << 64) | (least as u64 as u128) +} + #[derive(Clone, Debug, PartialEq)] pub struct BossBar { pub id: String, + /// The `ServerBossEvent` UUID. Not saved. + pub uuid: u128, pub name: Comp, pub color: String, pub style: String, pub value: i32, pub max: i32, + /// `ServerBossEvent.visible`. A new bar is visible; nobody sees it until + /// a player is added. pub visible: bool, - /// Whether the (single) local player is in the bar's player set. - pub player: bool, + pub darken: bool, + pub music: bool, + pub fog: bool, + /// `CustomBossEvent.players`, the UUIDs told about the bar. + pub players: Vec<[i32; 4]>, } impl BossBar { pub fn new(id: &str, name: Comp) -> Self { Self { id: id.to_owned(), + uuid: stable_uuid(id), name, color: "white".into(), style: "progress".into(), value: 0, max: 100, visible: true, - player: false, + darken: false, + music: false, + fog: false, + players: Vec::new(), } } /// `CustomBossEvent.getDisplayName`. @@ -91,31 +115,34 @@ impl BossBars { pub fn ids(&self) -> Vec { self.bars.iter().map(|b| b.id.clone()).collect() } - /// The saved compound; the player's UUID stands for "the local player". - pub fn pack(&self, player: Option<[i32; 4]>) -> Tag { + /// The saved compound (`CustomBossEvent.Packed`). + pub fn pack(&self) -> Tag { let mut c = Compound::new(); for b in &self.bars { - let mut e = vec![ - ("Name", json_to_tag(&b.name)), - ("Visible", Tag::Byte(i8::from(b.visible))), - ("Value", Tag::Int(b.value)), - ("Max", Tag::Int(b.max)), - ("Color", Tag::String(b.color.as_str().into())), - ("Overlay", Tag::String(b.style.as_str().into())), - ("DarkenScreen", Tag::Byte(0)), - ("PlayBossMusic", Tag::Byte(0)), - ("CreateWorldFog", Tag::Byte(0)), - ]; - let players = match (b.player, player) { - (true, Some(u)) => vec![Tag::IntArray(u.to_vec())], - _ => Vec::new(), - }; - e.push(("Players", Tag::List(players))); - c.insert(b.id.as_str().into(), Tag::compound(e)); + let players = b + .players + .iter() + .map(|u| Tag::IntArray(u.to_vec())) + .collect(); + c.insert( + b.id.as_str().into(), + Tag::compound([ + ("Name", json_to_tag(&b.name)), + ("Visible", Tag::Byte(i8::from(b.visible))), + ("Value", Tag::Int(b.value)), + ("Max", Tag::Int(b.max)), + ("Color", Tag::String(b.color.as_str().into())), + ("Overlay", Tag::String(b.style.as_str().into())), + ("DarkenScreen", Tag::Byte(i8::from(b.darken))), + ("PlayBossMusic", Tag::Byte(i8::from(b.music))), + ("CreateWorldFog", Tag::Byte(i8::from(b.fog))), + ("Players", Tag::List(players)), + ]), + ); } Tag::Compound(c) } - pub fn unpack(data: &Tag, player: Option<[i32; 4]>) -> Self { + pub fn unpack(data: &Tag) -> Self { let mut bars = Vec::new(); if let Tag::Compound(c) = data { for (id, e) in c { @@ -130,21 +157,30 @@ impl BossBars { Some(Tag::Byte(v)) => i32::from(*v), _ => d, }; - let in_players = match (get("Players"), player) { - (Some(Tag::List(l)), Some(u)) => l + let players = match get("Players") { + Some(Tag::List(l)) => l .iter() - .any(|t| matches!(t, Tag::IntArray(a) if a.as_slice() == u)), - _ => false, + .filter_map(|t| match t { + Tag::IntArray(a) if a.len() == 4 => Some([a[0], a[1], a[2], a[3]]), + _ => None, + }) + .collect(), + _ => Vec::new(), }; + let id = id.to_string_lossy(); bars.push(BossBar { - id: id.to_string_lossy(), + uuid: stable_uuid(&id), + id, name: get("Name").map_or_else(|| literal(""), tag_to_json), color: text("Color", "white"), style: text("Overlay", "progress"), value: num("Value", 0), max: num("Max", 100), visible: num("Visible", 0) != 0, - player: in_players, + darken: num("DarkenScreen", 0) != 0, + music: num("PlayBossMusic", 0) != 0, + fog: num("CreateWorldFog", 0) != 0, + players, }); } } @@ -167,17 +203,15 @@ mod tests { bar.value = 25; bar.max = 50; bar.visible = true; - bar.player = true; - let uuid = Some([1, 2, 3, 4]); - let back = BossBars::unpack(&b.pack(uuid), uuid); + bar.darken = true; + bar.players = vec![[1, 2, 3, 4]]; + let back = BossBars::unpack(&b.pack()); assert_eq!(back, b); - // Another player's UUID does not include the local player. - assert!(!BossBars::unpack(&b.pack(uuid), Some([9, 9, 9, 9])).bars[0].player); let bar = b.get("test:x").unwrap(); assert_eq!(bar.progress(), 0.5); assert_eq!(bar.display_name().plain(), "chat.square_brackets"); assert_eq!(color_rgb("pink"), text::RED); - assert_eq!(color_rgb("purple"), 0xAA00AA); + assert_eq!(color_rgb("purple"), text::named_color("dark_blue").unwrap()); b.remove("test:x"); assert!(b.bars.is_empty()); } diff --git a/crates/horizon-client/src/fog.rs b/crates/horizon-client/src/fog.rs index 942bae556..11ffda151 100644 --- a/crates/horizon-client/src/fog.rs +++ b/crates/horizon-client/src/fog.rs @@ -198,6 +198,8 @@ pub struct FogInputs<'a> { pub blindness: Option<(i32, bool)>, /// The darkness effect's blend factor, while the effect is on. pub darkness: Option, + /// `GameRenderer.bossOverlayWorldDarkening`: a wither's bar dims the fog. + pub darken_world: f32, /// `BossHealthOverlay.shouldCreateWorldFog`: a boss bar with /// `createWorldFog` is shown (the Ender Dragon's). pub boss_world_fog: bool, @@ -263,6 +265,13 @@ pub fn setup_fog(tables: &FogTables, state: &FogState, input: &FogInputs) -> Fog let brightness = (1.0 - darkness) * (1.0 - darkness); color = color.map(|c| c * brightness); } + // FogRenderer.computeFogColor: a boss bar's world darkening. + if input.darken_world > 0.0 { + let t = input.darken_world; + color[0] += (color[0] * 0.7 - color[0]) * t; + color[1] += (color[1] * 0.6 - color[1]) * t; + color[2] += (color[2] * 0.6 - color[2]) * t; + } let brighten = if eye_in_water { water_vision } else { 0.0 }; if color.iter().all(|&c| c != 0.0) { let scale = 1.0 / color[0].max(color[1]).max(color[2]); @@ -394,6 +403,7 @@ mod tests { skybox: true, blindness: None, darkness: None, + darken_world: 0.0, boss_world_fog: false, } } @@ -422,6 +432,25 @@ mod tests { assert_eq!(water.environmental_start, -8.0); } #[test] + fn boss_darkening_dims_fog_color() { + // FogRenderer.computeFogColor: lerp toward 70/60/60% of each channel. + let sky = Sky { + fog_color: [1.0, 1.0, 1.0], + sky_color: [1.0, 1.0, 1.0], + ..Sky::default() + }; + let tables = FogTables { + water_fog_end: vec![1.0], + }; + let mut input = inputs(&sky); + input.skybox = false; + input.darken_world = 1.0; + let fog = setup_fog(&tables, &FogState::default(), &input); + assert!((fog.color[0] - 0.7).abs() < 1.0e-5); + assert!((fog.color[1] - 0.6).abs() < 1.0e-5); + assert!((fog.color[2] - 0.6).abs() < 1.0e-5); + } + #[test] fn void_darkness_starts_32_above_the_floor() { let sky = Sky { fog_end: 1024.0, diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 533973805..f0765e0f6 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1562,6 +1562,8 @@ struct Game<'a> { score: scoreboard::ScoreState, fns: functions::FnState, bosses: bossbars::BossState, + /// `Hud.bossOverlay`: the bars this window is showing. + boss_overlay: horizon_client::boss_overlay::BossOverlay, exec: commands::execute::ExecState, rand_sequences: horizon_client::random_sequences::RandomSequences, border: horizon_client::worldborder::WorldBorder, @@ -4060,6 +4062,8 @@ impl Game<'_> { self.track_experience_display(); // `/tick freeze` stops the world but not the player. let running = self.clock.world_runs(); + // GameRenderer.tick: a wither's bar darkens the world, then fades. + self.boss_overlay.tick_darken(running); if running { self.world.game_time += 1; if self.ui.rules.bool("advance_weather") { @@ -4435,6 +4439,8 @@ impl Game<'_> { }); horizon_client::spans::time("tick_particles", || self.tick_particles()); } + // ServerBossEvent broadcasts, after this level's entities have ticked. + self.publish_boss_bars(); // Minecraft.handleKeybinds: held use repeats only while no item is in // use (a raised shield or a drawn bow places and uses nothing). if self.right @@ -7952,6 +7958,8 @@ impl Game<'_> { self.session_lock = None; horizon_client::crash::clear_detail("World"); self.debug_charts.reset(); + self.boss_overlay.reset(); + self.bosses = Default::default(); self.streaming.paused = true; self.world = World::new(self.res.facts().air, 0); self.player = None; @@ -10095,7 +10103,7 @@ impl Game<'_> { if !self.settings.horizon.fog && !underwater && self.darkness_fog().is_none() - && !self.dragon_bar_shown() + && !self.boss_overlay.should_create_world_fog() && self.potions.effects.get("blindness").is_none() && fog.environmental_end >= (self.streaming.radius * 16) as f32 { @@ -10166,6 +10174,9 @@ impl Game<'_> { darkness: self.darkness_lighting(alpha as f32), night_vision: self.night_vision_intensity(), block_flicker: self.fog.block_light_flicker, + boss_darken: self + .boss_overlay + .darken_amount(self.clock.fraction() as f32), menu_blur: self.menu_blur(), }; let result = match self.renderer.as_mut() { @@ -11953,6 +11964,7 @@ impl<'a> Game<'a> { score: Default::default(), fns: Default::default(), bosses: Default::default(), + boss_overlay: Default::default(), exec: Default::default(), rand_sequences: Default::default(), border: Default::default(), @@ -12680,6 +12692,7 @@ fn loading_frame(hud: &HudBatch) -> Frame<'_> { darkness: (0.0, 0.0), night_vision: 0.0, block_flicker: 0.0, + boss_darken: 0.0, menu_blur: None, } } diff --git a/crates/horizon-client/src/game/bossbars.rs b/crates/horizon-client/src/game/bossbars.rs index ec7b72632..85638da4a 100644 --- a/crates/horizon-client/src/game/bossbars.rs +++ b/crates/horizon-client/src/game/bossbars.rs @@ -1,22 +1,29 @@ //! `/bossbar` (BossBarCommands) and drawing the custom boss bars. use super::{ - Game, - commands::{R, err}, + Dim, Game, + commands::{R, Target, err}, }; use horizon_client::{ + boss_overlay, bossbar::{self, BossBars}, command::{Arg, Context}, - hud_overlays, save, - scoreboard::{self as sb, Comp, comp_text}, + mobs::Species, + save, + scoreboard::{self as sb, Comp, comp_text, json_to_tag, tag_to_json}, scoreboard_cmd::format_list, text::Text, }; +use horizon_core::random::RandomSource; +use horizon_protocol::packets::play::{self, BossAction, Clientbound as C}; use horizon_world::nbt::Tag; +use std::collections::{HashMap, HashSet}; #[derive(Default)] pub(super) struct BossState { pub bars: BossBars, loaded: bool, + /// What each viewer was last sent, so a change becomes one packet. + sent: HashMap>, } fn num(n: impl std::fmt::Display) -> Text { @@ -36,7 +43,7 @@ impl Game<'_> { if let Some(root) = save::read_dat(&self.bossbar_path()) && let Some(data) = root.get("data") { - self.bosses.bars = BossBars::unpack(data, self.level.player_uuid); + self.bosses.bars = BossBars::unpack(data); } self.ui.registries.bossbars = self.bosses.bars.ids(); } @@ -50,7 +57,7 @@ impl Game<'_> { let _ = save::write_dat( &self.bossbar_path(), Tag::compound([ - ("data", self.bosses.bars.pack(self.level.player_uuid)), + ("data", self.bosses.bars.pack()), ("DataVersion", Tag::Int(horizon_world::DATA_VERSION)), ]), ); @@ -68,6 +75,7 @@ impl Game<'_> { b.value = b.value.min(b.max); } } + self.publish_boss_bars(); } fn bar_id(ctx: &Context) -> String { match ctx.get("id") { @@ -86,6 +94,7 @@ impl Game<'_> { self.load_bossbars(); let result = self.bossbar_command(id, ctx); self.ui.registries.bossbars = self.bosses.bars.ids(); + self.publish_boss_bars(); result } fn bossbar_command(&mut self, id: &str, ctx: &Context) -> R { @@ -97,6 +106,10 @@ impl Game<'_> { } let name = self.component_json(ctx, "name")?; self.bosses.bars.add(&bar, name); + if let Some(b) = self.bosses.bars.get_mut(&bar) { + // CustomBossEvents.create: Mth.createInsecureUUID(level.random). + b.uuid = insecure_uuid(&mut self.world.random); + } let shown = self .bosses .bars @@ -173,19 +186,20 @@ impl Game<'_> { i32::from(b.visible) } _ => { - if b.player { - let name = self.target_name(super::commands::Target::Player); - self.feedback(Text::translate( - "commands.bossbar.get.players.some", - &[s, num(1), format_list(vec![name])], - )); - } else { + let names = self.bossbar_player_names(&b.players); + let n = names.len(); + if n == 0 { self.feedback(Text::translate( "commands.bossbar.get.players.none", &[s], )); + } else { + self.feedback(Text::translate( + "commands.bossbar.get.players.some", + &[s, num(n), format_list(names)], + )); } - i32::from(b.player) + n as i32 } }) } @@ -295,31 +309,46 @@ impl Game<'_> { Ok(0) } _ => { - // players [] - let include = if ctx.get("targets").is_some() { - !self.entities(ctx, "targets", true)?.is_empty() + // players []: replace the set. No argument clears it. + let targets = if ctx.get("targets").is_some() { + self.entities(ctx, "targets", true)? } else { - false + Vec::new() }; + let mut uuids = Vec::new(); + let mut names = Vec::new(); + for t in &targets { + let Some(uuid) = self.player_target_uuid(*t) else { + continue; + }; + if !uuids.contains(&uuid) { + uuids.push(uuid); + names.push(self.target_name(*t)); + } + } let b = self.bosses.bars.get_mut(bar).expect("checked"); - if b.player == include { + let mut had = b.players.clone(); + had.sort(); + let mut next = uuids.clone(); + next.sort(); + if had == next { return Err(err("commands.bossbar.set.players.unchanged", &[])); } - b.player = include; + b.players = uuids; + let n = b.players.len(); let s = shown(self); - if include { - let name = self.target_name(super::commands::Target::Player); - self.feedback(Text::translate( - "commands.bossbar.set.players.success.some", - &[s, num(1), format_list(vec![name])], - )); - } else { + if n == 0 { self.feedback(Text::translate( "commands.bossbar.set.players.success.none", &[s], )); + } else { + self.feedback(Text::translate( + "commands.bossbar.set.players.success.some", + &[s, num(targets.len()), format_list(names)], + )); } - Ok(i32::from(include)) + Ok(n as i32) } } } @@ -333,29 +362,455 @@ impl Game<'_> { Ok(self.resolve_component(value)) } - // ---- drawing ------------------------------------------------------------------ + /// Names of the online players in a boss bar's set, in join order. + fn bossbar_player_names(&self, players: &[[i32; 4]]) -> Vec { + self.known_boss_players() + .into_iter() + .filter(|(uuid, _)| players.contains(uuid)) + .map(|(_, name)| name) + .collect() + } + /// The players who can be told about a bar: the one in place, then guests. + fn known_boss_players(&self) -> Vec<([i32; 4], Text)> { + let mut out = Vec::new(); + if self.player.is_some() { + out.push((self.acting_uuid(), self.target_name(Target::Player))); + } + if let Some(host) = &self.lan { + for g in host.hub.guests.iter().filter(|g| g.placed) { + let ints = horizon_client::lan::playerdata::uuid_ints(g.uuid); + if out.iter().any(|(uuid, _)| *uuid == ints) { + continue; + } + out.push((ints, Text::literal(g.name.clone()))); + } + } + out + } + fn player_target_uuid(&self, target: Target) -> Option<[i32; 4]> { + match target { + Target::Player => Some(self.acting_uuid()), + Target::Guest(conn) => self.lan.as_ref().and_then(|h| { + h.hub + .guests + .iter() + .find(|g| g.conn == conn) + .map(|g| horizon_client::lan::playerdata::uuid_ints(g.uuid)) + }), + _ => None, + } + } - /// The visible custom bars (`/bossbar`), after the built-in ones. - pub(super) fn custom_bossbars(&self, bars: &mut Vec) { - for b in self - .bosses - .bars - .bars - .iter() - .filter(|b| b.visible && b.player) - { - bars.push(hud_overlays::BossBar { - name: comp_text(&b.name), - progress: b.progress(), - color: bossbar::COLORS - .iter() - .position(|c| *c == b.color) - .unwrap_or(0), - overlay: bossbar::STYLES - .iter() - .position(|s| *s == b.style) - .unwrap_or(0), + // ---- ServerBossEvent delivery ------------------------------------------------ + + /// Tell every viewer the boss events they should be seeing. The window + /// that simulates the world applies them to its own overlay; everyone + /// else gets `ClientboundBossEventPacket`s. + pub(super) fn publish_boss_bars(&mut self) { + if self.remote.is_some() { + return; + } + self.load_bossbars(); + let now = boss_overlay::now_millis(); + let viewers = self.boss_viewers(); + let live: HashSet = viewers.iter().map(|v| v.uuid).collect(); + self.bosses.sent.retain(|id, _| live.contains(id)); + let mut packets = Vec::new(); + for viewer in &viewers { + let desired = self.boss_events_for(viewer); + let changes = self.diff_boss_events(viewer, &desired); + if viewer.local { + apply_local(&mut self.boss_overlay, &changes, now); + } else if let Some(conn) = viewer.conn { + for change in changes { + packets.push((conn, boss_packet(change))); + } + } + } + if packets.is_empty() { + return; + } + let Some(host) = self.lan.as_ref() else { + return; + }; + for (conn, packet) in &packets { + host.hub.send_to(*conn, packet); + } + } + fn boss_viewers(&self) -> Vec { + let mut out = Vec::new(); + // While `as_guest` has swapped a guest in, the host's own position + // is not in place. Their overlay updates once the tick puts it back. + let local = + self.serving.is_none() && self.options.dedicated.is_none() && self.lan_acting.is_none(); + if local && let Some(p) = &self.player { + let ints = self.level.player_uuid.unwrap_or_else(|| self.acting_uuid()); + out.push(Viewer { + uuid: ints_to_uuid(ints), + ints, + at: [p.position.x, p.position.y, p.position.z], + alive: !p.health.dead_or_dying(), + here: true, + local: true, + conn: None, + view_chunks: self.settings.render_distance, }); } + if let Some(host) = &self.lan { + for g in host.hub.guests.iter().filter(|g| g.placed) { + if out.iter().any(|v| v.uuid == g.uuid) { + continue; + } + out.push(Viewer { + uuid: g.uuid, + ints: horizon_client::lan::playerdata::uuid_ints(g.uuid), + at: g.position, + alive: !g.dead, + here: g.dimension == self.dim.id(), + local: false, + conn: Some(g.conn), + view_chunks: g.view_distance, + }); + } + } + out + } + /// Dragon, withers, the raid and custom bars, in that order. + fn boss_events_for(&self, viewer: &Viewer) -> Vec { + let mut out = Vec::new(); + if viewer.here + && let Some(progress) = self.dragon_boss_progress(viewer.at, viewer.alive) + { + out.push(Offer { + id: bossbar::stable_uuid("minecraft:ender_dragon"), + name: translate_tag("entity.minecraft.ender_dragon"), + progress, + color: 0, + overlay: 0, + darken: false, + music: true, + fog: true, + scope: Scope::Level(self.dim), + }); + } + if viewer.here { + let range = f64::from(10.min(viewer.view_chunks.max(0)) * 16); + for m in self + .mobs + .iter() + .filter(|m| m.species() == Species::Wither && m.alive() && !m.removed) + { + let (dx, dz) = (m.position.x - viewer.at[0], m.position.z - viewer.at[2]); + if dx * dx + dz * dz > range * range { + continue; + } + let progress = if m.extra.counter > 0 { + 1.0 - m.extra.counter as f32 / horizon_client::mobs::wither::INVULNERABLE as f32 + } else { + (m.health / m.attrs.max_health).clamp(0.0, 1.0) + }; + let name = if let Some(text) = &m.custom_name_text { + json_to_tag(&text.component()) + } else if let Some(name) = &m.custom_name { + Tag::String(name.as_str().into()) + } else { + translate_tag("entity.minecraft.wither") + }; + out.push(Offer { + id: super::lan::entity_uuid(m.id), + name, + progress, + color: 5, + overlay: 0, + darken: true, + music: false, + fog: false, + scope: Scope::Level(self.dim), + }); + } + } + if viewer.here + && let Some((id, name, progress)) = self.raid_boss_at(viewer.at, viewer.alive) + { + out.push(Offer { + id: bossbar::stable_uuid(&format!("raid:{id}")), + name, + progress, + color: 2, + overlay: 2, + darken: false, + music: false, + fog: false, + scope: Scope::Level(self.dim), + }); + } + for b in &self.bosses.bars.bars { + if !b.visible || !b.players.contains(&viewer.ints) { + continue; + } + out.push(Offer { + id: b.uuid, + name: json_to_tag(&b.name), + progress: b.progress(), + color: color_index(&b.color) as u8, + overlay: style_index(&b.style) as u8, + darken: b.darken, + music: b.music, + fog: b.fog, + scope: Scope::Global, + }); + } + out + } + fn diff_boss_events(&mut self, viewer: &Viewer, desired: &[Offer]) -> Vec { + let full = viewer.local; + let dim = self.dim; + let sent = self.bosses.sent.entry(viewer.uuid).or_default(); + let mut changes = Vec::new(); + let gone: Vec = sent + .iter() + .filter(|(id, prev)| { + let owned = full || prev.scope == Scope::Global || prev.scope == Scope::Level(dim); + owned && !desired.iter().any(|e| e.id == **id) + }) + .map(|(id, _)| *id) + .collect(); + for id in gone { + sent.remove(&id); + changes.push(Change::Remove(id)); + } + for event in desired { + let snap = event.snap(); + match sent.get(&event.id).map(|s| s.snap.clone()) { + None => { + sent.insert( + event.id, + Sent { + snap, + scope: event.scope, + }, + ); + changes.push(Change::Add(event.clone())); + } + Some(prev) => { + if prev.progress != snap.progress { + changes.push(Change::Progress(event.id, snap.progress)); + } + if prev.name != snap.name { + changes.push(Change::Name(event.id, snap.name.clone())); + } + if prev.color != snap.color || prev.overlay != snap.overlay { + changes.push(Change::Style(event.id, snap.color, snap.overlay)); + } + if prev.darken != snap.darken + || prev.music != snap.music + || prev.fog != snap.fog + { + changes.push(Change::Properties( + event.id, + snap.darken, + snap.music, + snap.fog, + )); + } + sent.insert( + event.id, + Sent { + snap, + scope: event.scope, + }, + ); + } + } + } + changes + } + /// `ClientPacketListener.handleBossUpdate`. + pub(super) fn apply_boss_event(&mut self, packet: play::BossEvent) { + let now = boss_overlay::now_millis(); + let ord = |v: i32| v.clamp(0, 255) as u8; + match packet.action { + BossAction::Add { + name, + progress, + color, + overlay, + darken, + music, + fog, + } => self.boss_overlay.add( + packet.id, + component_text(&name), + progress, + ord(color), + ord(overlay), + darken, + music, + fog, + now, + ), + BossAction::Remove => self.boss_overlay.remove(packet.id), + BossAction::UpdateProgress { progress } => { + self.boss_overlay.set_progress(packet.id, progress, now) + } + BossAction::UpdateName { name } => { + self.boss_overlay.set_name(packet.id, component_text(&name)) + } + BossAction::UpdateStyle { color, overlay } => { + self.boss_overlay + .set_style(packet.id, ord(color), ord(overlay)) + } + BossAction::UpdateProperties { darken, music, fog } => self + .boss_overlay + .set_properties(packet.id, darken, music, fog), + } } } + +/// `Mth.createInsecureUUID`. +fn insecure_uuid(random: &mut impl RandomSource) -> u128 { + let most = random.next_long() & -61441 | 16384; + let least = random.next_long() & 4_611_686_018_427_387_903 | i64::MIN; + ((most as u64 as u128) << 64) | (least as u64 as u128) +} +fn ints_to_uuid(ints: [i32; 4]) -> u128 { + ints.iter() + .fold(0u128, |acc, &i| (acc << 32) | u128::from(i as u32)) +} +fn translate_tag(key: &str) -> Tag { + Tag::compound([("translate", Tag::String(key.into()))]) +} +fn color_index(color: &str) -> usize { + bossbar::COLORS + .iter() + .position(|c| *c == color) + .unwrap_or(6) +} +fn style_index(style: &str) -> usize { + bossbar::STYLES + .iter() + .position(|s| *s == style) + .unwrap_or(0) +} +fn component_text(tag: &Tag) -> Text { + comp_text(&tag_to_json(tag)) +} + +#[derive(Clone, Copy, PartialEq, Eq)] +enum Scope { + Global, + Level(Dim), +} +#[derive(Clone)] +struct Snap { + name: Tag, + progress: f32, + color: u8, + overlay: u8, + darken: bool, + music: bool, + fog: bool, +} +struct Sent { + snap: Snap, + scope: Scope, +} +struct Viewer { + uuid: u128, + ints: [i32; 4], + at: [f64; 3], + alive: bool, + here: bool, + local: bool, + conn: Option, + view_chunks: i32, +} +#[derive(Clone)] +struct Offer { + id: u128, + name: Tag, + progress: f32, + color: u8, + overlay: u8, + darken: bool, + music: bool, + fog: bool, + scope: Scope, +} +impl Offer { + fn snap(&self) -> Snap { + Snap { + name: self.name.clone(), + progress: self.progress, + color: self.color, + overlay: self.overlay, + darken: self.darken, + music: self.music, + fog: self.fog, + } + } +} +enum Change { + Add(Offer), + Remove(u128), + Progress(u128, f32), + Name(u128, Tag), + Style(u128, u8, u8), + Properties(u128, bool, bool, bool), +} +fn apply_local(overlay: &mut boss_overlay::BossOverlay, changes: &[Change], now: u128) { + for change in changes { + match change { + Change::Add(e) => overlay.add( + e.id, + component_text(&e.name), + e.progress, + e.color, + e.overlay, + e.darken, + e.music, + e.fog, + now, + ), + Change::Remove(id) => overlay.remove(*id), + Change::Progress(id, progress) => overlay.set_progress(*id, *progress, now), + Change::Name(id, name) => overlay.set_name(*id, component_text(name)), + Change::Style(id, color, overlay_style) => { + overlay.set_style(*id, *color, *overlay_style) + } + Change::Properties(id, darken, music, fog) => { + overlay.set_properties(*id, *darken, *music, *fog) + } + } + } +} +fn boss_packet(change: Change) -> C { + let (id, action) = match change { + Change::Add(e) => ( + e.id, + BossAction::Add { + name: e.name, + progress: e.progress, + color: i32::from(e.color), + overlay: i32::from(e.overlay), + darken: e.darken, + music: e.music, + fog: e.fog, + }, + ), + Change::Remove(id) => (id, BossAction::Remove), + Change::Progress(id, progress) => (id, BossAction::UpdateProgress { progress }), + Change::Name(id, name) => (id, BossAction::UpdateName { name }), + Change::Style(id, color, overlay) => ( + id, + BossAction::UpdateStyle { + color: i32::from(color), + overlay: i32::from(overlay), + }, + ), + Change::Properties(id, darken, music, fog) => { + (id, BossAction::UpdateProperties { darken, music, fog }) + } + }; + C::BossEvent(play::BossEvent { id, action }) +} diff --git a/crates/horizon-client/src/game/dragon.rs b/crates/horizon-client/src/game/dragon.rs index c6f5d419b..54b260c67 100644 --- a/crates/horizon-client/src/game/dragon.rs +++ b/crates/horizon-client/src/game/dragon.rs @@ -12,7 +12,6 @@ use horizon_client::{ hud_layout, hud_overlays, mesher::Vertex, particles::{ParticleOptions, ParticleType}, - text::Text, }; use horizon_core::{ geometry::{Aabb, Vec3}, @@ -2157,23 +2156,12 @@ impl Game<'_> { }); } } - /// `BossHealthOverlay`: the dragon's pink bar, then the withers', the - /// raids' and the custom bars, in that order. + /// `BossHealthOverlay.extractRenderState`: whatever `publish_boss_bars` + /// (or a server's packets) put on this client's overlay. pub(super) fn draw_boss_bars(&self, batch: &mut HudBatch) { - let mut bars = Vec::new(); - if let Some(d) = self.dragon.as_ref().filter(|_| self.dragon_bar_shown()) { - bars.push(hud_overlays::BossBar { - name: Text::literal( - self.translate("entity.minecraft.ender_dragon", "Ender Dragon"), - ), - progress: (d.health / MAX_HEALTH).clamp(0.0, 1.0), - color: 0, - overlay: 0, - }); - } - self.wither_bars(&mut bars); - self.raid_bars(&mut bars); - self.custom_bossbars(&mut bars); + let bars = self + .boss_overlay + .bars(horizon_client::boss_overlay::now_millis()); let draws = hud_overlays::boss_overlay( batch.width as i32, batch.height as i32, @@ -2182,14 +2170,26 @@ impl Game<'_> { ); hud_layout::paint(batch, &self.gui.atlas, &draws); } - /// The dragon fight's boss bar shows (`EnderDragonFight.dragonEvent`, - /// with `setCreateWorldFog`). - pub(super) fn dragon_bar_shown(&self) -> bool { - self.dim == Dim::End && self.dragon.is_some() + /// The dragon fight's bar for a player at `at` + /// (`EnderDragonFight.dragonEvent`, visible while the fight is not won + /// and the player is alive within 192 blocks of (0, 128, 0)). + pub(super) fn dragon_boss_progress(&self, at: [f64; 3], alive: bool) -> Option { + if self.dim != Dim::End || !alive || !self.fight.loaded || self.fight.killed { + return None; + } + let near = at[0].powi(2) + (at[1] - 128.0).powi(2) + at[2].powi(2) <= 192.0 * 192.0; + if !near { + return None; + } + Some( + self.dragon + .as_ref() + .map_or(1.0, |d| (d.health / MAX_HEALTH).clamp(0.0, 1.0)), + ) } - /// `music.dragon` while the boss bar plays music. + /// `BossHealthOverlay.shouldPlayMusic`. pub(super) fn boss_music(&self) -> bool { - self.dim == Dim::End && self.dragon.is_some() || self.wither_near() + self.boss_overlay.should_play_music() } } pub(super) struct DragonFireball { diff --git a/crates/horizon-client/src/game/raids.rs b/crates/horizon-client/src/game/raids.rs index bff4a9918..bedca5708 100644 --- a/crates/horizon-client/src/game/raids.rs +++ b/crates/horizon-client/src/game/raids.rs @@ -6,7 +6,6 @@ use super::{Dim, Game}; use horizon_client::mobs::villager as mobs_villager; use horizon_client::{ effects::{EffectInstance, OmenTick}, - hud_overlays, mobs::{ Mob, Species, raider, spawn_rules::{self, SpawnLevel}, @@ -15,9 +14,9 @@ use horizon_client::{ }, patrol::{GameplayAttributes, Member, PatrolLevel, PatrolSpawner}, raid::{self, Action, BarName, RaidLevel, RaidPlayer, RaiderInfo, RaiderType, Raids, Spawn}, - text::Text, }; use horizon_core::{geometry::Vec3, random::RandomSource}; +use horizon_world::nbt::Tag; use std::collections::{HashMap, HashSet}; /// Patrol and raid state owned by the game. @@ -1063,37 +1062,18 @@ impl Game<'_> { } } - /// The raid boss bars: red and notched in tens, with the raid's title. - pub(super) fn raid_bars(&self, bars: &mut Vec) { - for raid in self.raiding.raids.map.values() { - if !raid.bar.visible || !raid.bar.player || raid.is_stopped() { - continue; - } - let title = self.translate("event.minecraft.raid", "Raid"); - let name = match raid.bar.name { - BarName::Raid => title, - BarName::Remaining(n) => format!( - "{title} - {}", - self.translate( - "event.minecraft.raid.raiders_remaining", - "Raiders Remaining: %s" - ) - .replace("%s", &n.to_string()) - ), - BarName::Victory => { - self.translate("event.minecraft.raid.victory.full", "Raid - Victory") - } - BarName::Defeat => { - self.translate("event.minecraft.raid.defeat.full", "Raid - Defeat") - } - }; - bars.push(hud_overlays::BossBar { - name: Text::literal(name), - progress: raid.bar.progress, - color: 2, - overlay: 2, - }); + /// The raid bar a living player at `at` is in (`Raid.getPlayers`): red, + /// notched in tens. The name is the translate component the client draws. + pub(super) fn raid_boss_at(&self, at: [f64; 3], alive: bool) -> Option<(i32, Tag, f32)> { + if !alive || self.dim != Dim::Overworld { + return None; } + let block = block_of(Vec3::new(at[0], at[1], at[2])); + let (id, raid) = self.raiding.raids.nearby(block)?; + if !raid.bar.visible || raid.is_stopped() { + return None; + } + Some((id, raid_name_tag(raid.bar.name), raid.bar.progress)) } /// A firework a villager launches to celebrate (`CelebrateVillagersSurvivedRaid`). pub(super) fn villager_firework(&mut self, at: [f64; 3], color: usize, flight: u8) { @@ -1239,6 +1219,36 @@ impl Game<'_> { } } +/// `Raid.raidEvent`'s name: the raid title, the remaining-raiders line, or +/// the victory and defeat lines. +fn raid_name_tag(name: BarName) -> Tag { + let key = |k: &str| Tag::compound([("translate", Tag::String(k.into()))]); + match name { + BarName::Raid => key("event.minecraft.raid"), + BarName::Victory => key("event.minecraft.raid.victory.full"), + BarName::Defeat => key("event.minecraft.raid.defeat.full"), + BarName::Remaining(n) => Tag::compound([ + ("translate", Tag::String("event.minecraft.raid".into())), + ( + "extra", + Tag::List(vec![ + Tag::String(" - ".into()), + Tag::compound([ + ( + "translate", + Tag::String("event.minecraft.raid.raiders_remaining".into()), + ), + ( + "with", + Tag::List(vec![Tag::String(n.to_string().as_str().into())]), + ), + ]), + ]), + ), + ]), + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index da0def0fb..9c0504a2c 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -369,6 +369,7 @@ impl Game<'_> { } self.apply_settings(); self.clear_waypoints(); + self.boss_overlay.reset(); self.remote = Some(Remote { session, brand: "unknown".into(), @@ -450,6 +451,7 @@ impl Game<'_> { self.extras_left(); } self.clear_waypoints(); + self.boss_overlay.reset(); if let Some(mut r) = self.remote.take() { r.session.disconnect(); } @@ -2640,6 +2642,7 @@ impl Game<'_> { } // ClientPacketListener.setActionBarText, setTitleText, // setSubtitleText, setTitlesAnimation and handleTitlesClear. + C::BossEvent(p) => self.apply_boss_event(p), C::SetActionBarText(p) => self.titles.set_overlay(remote_text(&p.text), false), C::SetTitleText(p) => self.titles.set_title(remote_text(&p.text)), C::SetSubtitleText(p) => self.titles.set_subtitle(remote_text(&p.text)), @@ -3053,6 +3056,7 @@ impl Game<'_> { // Minecraft.tick: TickRateManager.tick; ClientLevel.tickTime advances // the time only while the level runs normally. let running = self.clock.world_runs(); + self.boss_overlay.tick_darken(running); if running { self.world.game_time += 1; self.remote_clock_tick(); diff --git a/crates/horizon-client/src/game/render_tick.rs b/crates/horizon-client/src/game/render_tick.rs index 331db8833..e013c8d29 100644 --- a/crates/horizon-client/src/game/render_tick.rs +++ b/crates/horizon-client/src/game/render_tick.rs @@ -100,7 +100,10 @@ impl Game<'_> { skybox: self.dim == Dim::Overworld, blindness: None, darkness: self.darkness_fog(), - boss_world_fog: self.dragon_bar_shown(), + darken_world: self + .boss_overlay + .darken_amount(self.clock.fraction() as f32), + boss_world_fog: self.boss_overlay.should_create_world_fog(), }, ) } diff --git a/crates/horizon-client/src/game/wither.rs b/crates/horizon-client/src/game/wither.rs index 7b86dc527..91fe91b00 100644 --- a/crates/horizon-client/src/game/wither.rs +++ b/crates/horizon-client/src/game/wither.rs @@ -2,10 +2,8 @@ //! its boss bar, and its skulls hitting and exploding. use super::Game; use horizon_client::{ - hud_overlays, mobs::{Damage, DamageKind, Species, Who}, projectiles::{Projectile, ProjectileKind}, - text::Text, }; impl Game<'_> { @@ -78,45 +76,6 @@ impl Game<'_> { } Ok(()) } - /// The wither boss bars (purple), within range. - pub(super) fn wither_bars(&self, bars: &mut Vec) { - let Some(p) = self.player.as_ref() else { - return; - }; - for m in self - .mobs - .iter() - .filter(|m| m.species() == Species::Wither && m.alive()) - { - if m.position.distance_squared(p.position) > 64.0 * 64.0 { - continue; - } - let progress = if m.extra.counter > 0 { - 1.0 - m.extra.counter as f32 / horizon_client::mobs::wither::INVULNERABLE as f32 - } else { - (m.health / m.attrs.max_health).clamp(0.0, 1.0) - }; - let name = m - .custom_name - .clone() - .unwrap_or_else(|| self.translate("entity.minecraft.wither", "Wither")); - bars.push(hud_overlays::BossBar { - name: Text::literal(name), - progress, - color: 5, - overlay: 0, - }); - } - } - pub(super) fn wither_near(&self) -> bool { - self.player.as_ref().is_some_and(|p| { - self.mobs.iter().any(|m| { - m.species() == Species::Wither - && m.alive() - && m.position.distance_squared(p.position) <= 64.0 * 64.0 - }) - }) - } /// `WitherSkull.onHitEntity` / `onHit`: 8 damage (healing the wither 5 /// when it kills), withering for 10 seconds, then a power 1 blast. pub(super) fn wither_skull_hit( diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index c369ee1c7..005e9ce90 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -268,6 +268,8 @@ pub struct Frame<'a> { pub night_vision: f32, /// Torch flicker added to the block light factor of 1.4. pub block_flicker: f32, + /// `lightmap.fsh` BossOverlayWorldDarkeningFactor (a wither's bar). + pub boss_darken: f32, /// `Screen.extractBlurredBackground`: blur the frame by this radius /// (`menuBackgroundBlurriness`) before the screen is drawn over it. pub menu_blur: Option, @@ -2778,6 +2780,7 @@ impl Renderer { gamma: f32, night_vision: f32, flicker: f32, + boss_darken: f32, ) { // lightmap.fsh: per (block, sky) level. let brightness = |level: f32| level / (4.0 - 3.0 * level); @@ -2795,6 +2798,8 @@ impl Renderer { let parabolic = (2.0 * block_level - 1.0).powi(2); let block_color = Vec3::from(sky.block_light_tint).lerp(Vec3::ONE, 0.9 * parabolic); color += block_color * block_brightness; + // lightmap.fsh: a boss bar mixes the light toward 70/60/60%. + color = color.lerp(color * Vec3::new(0.7, 0.6, 0.6), boss_darken); color -= Vec3::splat(darkness); color = color.clamp(Vec3::ZERO, Vec3::ONE); let max = color.max_element().max(1e-6); @@ -2909,6 +2914,7 @@ impl Renderer { (self.gamma - frame.darkness.0).max(0.0), frame.night_vision, frame.block_flicker, + frame.boss_darken, ); let view_proj = self.view_projection(frame); if frame.title { diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index 3c741e963..43a399ef0 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -24,6 +24,7 @@ pub mod block_entity_render; pub mod block_transform; pub mod blocks; pub mod border_render; +pub mod boss_overlay; pub mod bossbar; pub mod breaking; pub mod brewing; diff --git a/crates/horizon-protocol/src/packets/play.rs b/crates/horizon-protocol/src/packets/play.rs index 206ed9aff..7dd8156c4 100644 --- a/crates/horizon-protocol/src/packets/play.rs +++ b/crates/horizon-protocol/src/packets/play.rs @@ -1580,6 +1580,125 @@ impl Codec> for StatList { } } +/// `ClientboundBossEventPacket` (`boss_event`). +#[derive(Clone, Debug, PartialEq)] +pub enum BossAction { + Add { + name: Tag, + progress: f32, + /// `BossBarColor` ordinal. + color: i32, + /// `BossBarOverlay` ordinal. + overlay: i32, + darken: bool, + music: bool, + fog: bool, + }, + Remove, + UpdateProgress { + progress: f32, + }, + UpdateName { + name: Tag, + }, + UpdateStyle { + color: i32, + overlay: i32, + }, + UpdateProperties { + darken: bool, + music: bool, + fog: bool, + }, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct BossEvent { + pub id: u128, + pub action: BossAction, +} +fn boss_flags(darken: bool, music: bool, fog: bool) -> u8 { + u8::from(darken) | (u8::from(music) << 1) | (u8::from(fog) << 2) +} +fn split_flags(flags: u8) -> (bool, bool, bool) { + (flags & 1 != 0, flags & 2 != 0, flags & 4 != 0) +} +impl Codec for BossEvent { + fn read(r: &mut Reader) -> Result { + let id = r.uuid()?; + let action = match r.varint()? { + 0 => { + let name = r.nbt()?; + let progress = r.f32()?; + let color = r.varint()?; + let overlay = r.varint()?; + let (darken, music, fog) = split_flags(r.u8()?); + BossAction::Add { + name, + progress, + color, + overlay, + darken, + music, + fog, + } + } + 1 => BossAction::Remove, + 2 => BossAction::UpdateProgress { progress: r.f32()? }, + 3 => BossAction::UpdateName { name: r.nbt()? }, + 4 => BossAction::UpdateStyle { + color: r.varint()?, + overlay: r.varint()?, + }, + 5 => { + let (darken, music, fog) = split_flags(r.u8()?); + BossAction::UpdateProperties { darken, music, fog } + } + op => return Err(Error::new(format!("unknown boss event operation {op}"))), + }; + Ok(BossEvent { id, action }) + } + fn write(v: &BossEvent, w: &mut Writer) { + w.uuid(v.id); + match &v.action { + BossAction::Add { + name, + progress, + color, + overlay, + darken, + music, + fog, + } => { + w.varint(0); + w.nbt(name); + w.f32(*progress); + w.varint(*color); + w.varint(*overlay); + w.u8(boss_flags(*darken, *music, *fog)); + } + BossAction::Remove => w.varint(1), + BossAction::UpdateProgress { progress } => { + w.varint(2); + w.f32(*progress); + } + BossAction::UpdateName { name } => { + w.varint(3); + w.nbt(name); + } + BossAction::UpdateStyle { color, overlay } => { + w.varint(4); + w.varint(*color); + w.varint(*overlay); + } + BossAction::UpdateProperties { darken, music, fog } => { + w.varint(5); + w.u8(boss_flags(*darken, *music, *fog)); + } + } + } +} + packet_enum!(Clientbound, crate::ids::play::clientbound { BundleDelimiter(Empty) = BUNDLE_DELIMITER, AddEntity(AddEntity) = ADD_ENTITY, @@ -1590,6 +1709,7 @@ packet_enum!(Clientbound, crate::ids::play::clientbound { BlockEntityData(BlockEntityData) = BLOCK_ENTITY_DATA, BlockEvent(BlockEvent) = BLOCK_EVENT, BlockUpdate(BlockUpdate) = BLOCK_UPDATE, + BossEvent(BossEvent) = BOSS_EVENT, ChangeDifficulty(ChangeDifficulty) = CHANGE_DIFFICULTY, ChunkBatchFinished(ChunkBatchFinished) = CHUNK_BATCH_FINISHED, ChunkBatchStart(Empty) = CHUNK_BATCH_START, @@ -2860,4 +2980,40 @@ mod tests { w.varint(101); assert!(BookPages::read(&mut Reader::new(&w.data)).is_err()); } + fn boss_event_round_trips() { + let add = Clientbound::BossEvent(BossEvent { + id: 0x0123_4567_89ab_cdef_fedc_ba98_7654_3210, + action: BossAction::Add { + name: Tag::String("Wither".into()), + progress: 0.5, + color: 5, + overlay: 0, + darken: true, + music: false, + fog: true, + }, + }); + let body = add.encode(); + // Packet id 9, then the uuid, operation 0, and the property byte + // (darken 1, fog 4). + assert_eq!(body[0], 9); + assert_eq!(*body.last().unwrap(), 5); + round_trip(add, Clientbound::encode, Clientbound::decode); + round_trip( + Clientbound::BossEvent(BossEvent { + id: 7, + action: BossAction::UpdateProgress { progress: 0.25 }, + }), + Clientbound::encode, + Clientbound::decode, + ); + round_trip( + Clientbound::BossEvent(BossEvent { + id: 7, + action: BossAction::Remove, + }), + Clientbound::encode, + Clientbound::decode, + ); + } } From b61b25f80fa2afdb7249ccc48c5e2e0f9eb90c68 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 24 Oct 2026 21:35:37 +0200 Subject: [PATCH 011/107] Shader packs, clean room 6: composite-style programs to naga A program's preprocessed GLSL, any version from 110 to 460, compatibility or core, rewritten into the Vulkan-flavoured GLSL 450 that naga's frontend reads, then parsed and validated into naga modules with the program's interface: its uniform block (offsets from naga's layout), its samplers and their bindings, its output locations. The rewrite works on preprocessing tokens and keeps the pack's lines, so every error points at the pack's own file and line through the preprocessor's line map. - Loose uniforms go into one block shared by both stages; bool is stored as int. Samplers are split into texture and sampler objects, combined again where used, and passed as two arguments to the pack's functions. - The compatibility built-ins become declarations of ours (prefix hz_): gl_Vertex, gl_MultiTexCoordN, the matrices and ftransform(), gl_FragData, gl_FragColor, gl_TexCoord, gl_FrontColor, gl_Fog (from vanilla's fog), texture2D and the other old texture functions, shadow2D; varyings get locations by name across the stages; words that became keywords after GLSL 1.x are renamed in old code. - naga gaps covered: modf, conditional initializers of globals (run by our main), and unsized arrays sized from their constructor. - The pack's main is called by one of ours that draws the quad from (0,0) to (1,1) and adapts GL's conventions: clip y negated for every target but the window, so memory rows are GL's rows and gl_FragCoord needs no change; clip z from GL's [-w, w] to [0, w], so stored depth is GL's window depth. In final, gl_FragCoord.y is counted from the bottom of the window. Black box over the public test packs: all 105 composite-style programs (composite, deferred, final, prepare, begin, shadowcomp of the base and dimension folders) translate and validate. Nothing uses it yet: players see no change. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 11 + crates/horizon-shaders/Cargo.toml | 1 + crates/horizon-shaders/src/lib.rs | 3 + crates/horizon-shaders/src/translate/mod.rs | 436 ++++++++++++++++++ .../horizon-shaders/src/translate/rewrite.rs | 257 +++++++++++ crates/horizon-shaders/src/translate/scan.rs | 399 ++++++++++++++++ crates/horizon-shaders/tests/real_packs.rs | 73 ++- 7 files changed, 1179 insertions(+), 1 deletion(-) create mode 100644 crates/horizon-shaders/src/translate/mod.rs create mode 100644 crates/horizon-shaders/src/translate/rewrite.rs create mode 100644 crates/horizon-shaders/src/translate/scan.rs diff --git a/Cargo.lock b/Cargo.lock index 7036ea5ca..7b1770094 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1435,6 +1435,7 @@ name = "horizon-shaders" version = "0.1.0" dependencies = [ "horizon-core", + "naga", "zip", ] @@ -1951,6 +1952,7 @@ dependencies = [ "naga-types", "num-traits", "once_cell", + "pp-rs", "rustc-hash 1.1.0", "spirv", "thiserror 2.0.21", @@ -2627,6 +2629,15 @@ dependencies = [ "zerovec", ] +[[package]] +name = "pp-rs" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb458bb7f6e250e6eb79d5026badc10a3ebb8f9a15d1fff0f13d17c71f4d6dee" +dependencies = [ + "unicode-xid", +] + [[package]] name = "presser" version = "0.3.1" diff --git a/crates/horizon-shaders/Cargo.toml b/crates/horizon-shaders/Cargo.toml index 750949e83..38e068b94 100644 --- a/crates/horizon-shaders/Cargo.toml +++ b/crates/horizon-shaders/Cargo.toml @@ -8,6 +8,7 @@ description = "Shader packs (the shaders/ folder format): reading packs, their . [dependencies] horizon-core = { path = "../horizon-core" } +naga = { version = "30.0.1", features = ["glsl-in", "wgsl-in"] } zip = { version = "2", default-features = false, features = ["deflate"] } [lints] diff --git a/crates/horizon-shaders/src/lib.rs b/crates/horizon-shaders/src/lib.rs index 14f85b758..bae3c0dcd 100644 --- a/crates/horizon-shaders/src/lib.rs +++ b/crates/horizon-shaders/src/lib.rs @@ -7,6 +7,7 @@ //! - [`Properties`]: `shaders.properties` and the id files, with their conditional directives (3.1, 1.5). //! - [`Lang`]: the pack's translated names (8.4). //! - [`preprocess`]: a stage file through `#include` and the GLSL preprocessor, lines mapped back (1.4, 1.5). +//! - [`translate_composite`]: a composite-style program into naga modules and its interface (1.6, 2.2). //! - [`Macros`] and [`eval_condition`]: the macros packs test, and the `#if` arithmetic (1.5). mod condition; @@ -16,6 +17,7 @@ mod macros; mod pack; mod preprocess; mod properties; +mod translate; pub use condition::eval_condition; pub use discover::{Found, discover}; @@ -24,3 +26,4 @@ pub use macros::{MC_VERSION, Macros, RENDER_STAGES}; pub use pack::{Pack, PackError}; pub use preprocess::{Diagnostic, Origin, Preprocessed, preprocess}; pub use properties::{Entry, Note, Properties}; +pub use translate::{Error, Interface, Program, Sampler, Target, Uniform, translate_composite}; diff --git a/crates/horizon-shaders/src/translate/mod.rs b/crates/horizon-shaders/src/translate/mod.rs new file mode 100644 index 000000000..9cc2ea07a --- /dev/null +++ b/crates/horizon-shaders/src/translate/mod.rs @@ -0,0 +1,436 @@ +//! A program's preprocessed GLSL (any version from 110 to 460, compatibility or core) turned +//! into the Vulkan-flavoured GLSL 450 naga reads, then into validated naga modules (spec 1.6, +//! 2.2). The rewrite works on preprocessing tokens and never adds or removes a line of the pack's +//! code, so naga's line numbers map back through the preprocessor to the pack's files. +//! +//! What it does: +//! - Loose uniforms go into one uniform block shared by both stages (`bool` stored as `int`). +//! - Each sampler is split into a texture and a sampler object, combined again where it is used +//! (`sampler2D(t, s)`), or passed as two arguments to the pack's own functions. +//! - The compatibility built-ins (`gl_Vertex`, `gl_MultiTexCoordN`, `gl_*Matrix`, `ftransform()`, +//! `gl_FragData`, `gl_FragColor`, `gl_TexCoord`, `texture2D`...) become declarations of ours, +//! named with the reserved prefix `hz_`; varyings get locations by name across the stages. +//! - The pack's `main` becomes `hz_main`, called by a `main` of ours that sets the inputs and +//! adapts GL's conventions (spec 5.5): clip y is negated for every target but the window, so +//! memory rows are GL's rows and `gl_FragCoord` needs no change, and clip z goes from GL's +//! [-w, w] to [0, w], so stored depth is GL's window depth. +//! +//! Composite-style programs draw one quad from (0,0) to (1,1) with an identity model-view and an +//! orthographic projection onto it, the texture coordinate equal to the position (OQ-6). + +use std::collections::BTreeMap; +use std::fmt::Write; + +use naga::ShaderStage; +use naga::front::glsl::{Frontend, Options}; +use naga::valid::{Capabilities, ValidationFlags, Validator}; + +use crate::Preprocessed; + +mod rewrite; +mod scan; + +use scan::Stage; + +/// What a program's stages need from the game. +#[derive(Debug, Default, PartialEq)] +pub struct Interface { + /// The uniform block at binding 0: its size and each value's name, GLSL type and offset. + /// Ours come first: `hz_target` (the target's width, height, 1/width, 1/height), and the + /// fixed-function fog the compatibility `gl_Fog` reads (OQ-28): `hz_fog_color` (vanilla's fog + /// colour) and `hz_fog` (density, start, end, 1 / (end - start)). + pub uniform_size: u32, + pub uniforms: Vec, + /// Each sampler: texture at `binding`, sampler object at `binding + 1`. + pub samplers: Vec, + /// The fragment stage's output locations. + pub outputs: Vec, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct Uniform { + pub name: String, + /// The type the pack declared (`float`, `ivec2`, `mat4`; `bool` is stored as an `int`). + pub ty: String, + pub offset: u32, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct Sampler { + pub name: String, + /// The type the pack declared: `sampler2D`, `sampler2DShadow`, `usampler2D`, `sampler3D`... + pub ty: String, + pub binding: u32, +} + +/// A program that failed, at a pack file and line. +#[derive(Clone, Debug, PartialEq)] +pub struct Error { + pub file: String, + pub line: u32, + pub message: String, +} + +pub struct Program { + pub vertex: naga::Module, + pub fragment: naga::Module, + pub interface: Interface, +} + +/// Where the program draws, which decides the conventions of 5.5. +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum Target { + /// Pack buffers: rows stored as GL stores them. + Buffers, + /// The window (`final`). + Window, +} + +/// Translates a composite-style program (`composite`, `deferred`, `final`...). +pub fn translate_composite( + vertex: &Preprocessed, + fragment: &Preprocessed, + target: Target, +) -> Result> { + let mut v = Stage::scan(vertex, ShaderStage::Vertex); + let mut f = Stage::scan(fragment, ShaderStage::Fragment); + let mut errors: Vec = v.errors.drain(..).chain(f.errors.drain(..)).collect(); + + // One uniform block and one set of samplers for both stages. + let mut uniforms: BTreeMap = BTreeMap::new(); + let mut samplers: BTreeMap = BTreeMap::new(); + for stage in [&v, &f] { + for (name, ty, at) in &stage.uniforms { + if let Some(old) = uniforms.insert(name.clone(), ty.clone()) + && &old != ty + { + errors.push(stage.error( + *at, + &format!("`{name}` is `{old}` in one stage and `{ty}` in the other"), + )); + } + } + for (name, ty) in &stage.samplers { + samplers.insert(name.clone(), ty.clone()); + } + } + let samplers: Vec = samplers + .into_iter() + .enumerate() + .map(|(i, (name, ty))| Sampler { + name, + ty, + binding: 1 + 2 * i as u32, + }) + .collect(); + + // Varyings by name: the vertex outputs, then fragment inputs the vertex stage lacks. + let mut locations: BTreeMap = BTreeMap::new(); + for var in v.outs.iter().chain(&f.ins) { + let next = locations.len() as u32; + locations + .entry(var.name.clone()) + .or_insert((next, var.decl.clone())); + } + + let header = |stage: &Stage| { + let mut h = String::from("#version 450\n"); + h.push_str("layout(set = 0, binding = 0) uniform HzUniforms { vec4 hz_target; vec4 hz_fog_color; vec4 hz_fog;"); + for (name, ty) in &uniforms { + match ty.as_str() { + "bool" => write!(h, " int hz_bool_{name};"), + _ => match ty.split_once('[') { + Some((base, size)) => write!(h, " {base} {name}[{size};"), + None => write!(h, " {ty} {name};"), + }, + } + .unwrap(); + } + h.push_str(" };\n"); + for s in &samplers { + let (texture, sampler) = split_sampler(&s.ty); + writeln!( + h, + "layout(set = 0, binding = {}) uniform {texture} {}; layout(set = 0, binding = {}) uniform {sampler} hz_s_{};", + s.binding, s.name, s.binding + 1, s.name + ) + .unwrap(); + } + let direction = if stage.kind == ShaderStage::Vertex { + "out" + } else { + "in" + }; + for (name, (location, decl)) in &locations { + writeln!( + h, + "layout(location = {location}) {direction} {}", + decl.replace("@", name) + ) + .unwrap(); + } + h.push_str(&stage.prelude()); + h + }; + let flip = if target == Target::Buffers { "-" } else { "" }; + let fog = + " hz_Fog = hz_FogParameters(hz_fog_color, hz_fog.x, hz_fog.y, hz_fog.z, hz_fog.w);\n"; + let mut vertex_main = String::from( + "\nvoid main() {\n vec2 hz_p = hz_quad[gl_VertexIndex];\n hz_Vertex = vec4(hz_p, 0.0, 1.0);\n \ + hz_MultiTexCoord0 = hz_Vertex;\n hz_Color = vec4(1.0);\n hz_Normal = vec3(0.0, 0.0, 1.0);\n", + ); + for (name, value) in &v.attributes { + writeln!(vertex_main, " {name} = {value};").unwrap(); + } + vertex_main.push_str(fog); + for name in &v.inits { + writeln!(vertex_main, " {name} = hz_init_{name}();").unwrap(); + } + write!( + vertex_main, + " hz_main();\n gl_Position = vec4(gl_Position.x, {flip}gl_Position.y, (gl_Position.z + gl_Position.w) * 0.5, gl_Position.w);\n}}\n" + ) + .unwrap(); + let mut fragment_main = String::from("\nvoid main() {\n"); + fragment_main.push_str(fog); + if target == Target::Window && f.frag_coord { + fragment_main.push_str(" hz_FragCoord = vec4(gl_FragCoord.x, hz_target.y - gl_FragCoord.y, gl_FragCoord.zw);\n"); + } + for name in &f.inits { + writeln!(fragment_main, " {name} = hz_init_{name}();").unwrap(); + } + fragment_main.push_str(" hz_main();\n}\n"); + + let v_text = format!( + "{}{}{vertex_main}", + header(&v), + v.rewrite(&samplers, &uniforms) + ); + let f_text = format!( + "{}{}{fragment_main}", + header(&f), + f.rewrite(&samplers, &uniforms) + ); + if !errors.is_empty() { + return Err(errors); + } + let vertex_module = compile(&v, &v_text, header(&v).lines().count()); + let fragment_module = compile(&f, &f_text, header(&f).lines().count()); + let (vertex, fragment) = match (vertex_module, fragment_module) { + (Ok(v), Ok(f)) => (v, f), + (a, b) => return Err(a.err().into_iter().chain(b.err()).flatten().collect()), + }; + let (uniform_size, offsets) = block_layout(&fragment); + let uniforms = offsets + .into_iter() + .filter_map(|(member, offset)| { + let name = member + .strip_prefix("hz_bool_") + .unwrap_or(&member) + .to_owned(); + let ty = if name.starts_with("hz_") { + "vec4".to_owned() + } else { + uniforms.get(&name)?.clone() + }; + Some(Uniform { name, ty, offset }) + }) + .collect(); + let outputs = f.outputs.iter().copied().collect(); + Ok(Program { + vertex, + fragment, + interface: Interface { + uniform_size, + uniforms, + samplers, + outputs, + }, + }) +} + +/// `sampler2DShadow` → (`texture2D`, `samplerShadow`); `isampler3D` → (`itexture3D`, `sampler`). +pub(super) fn split_sampler(ty: &str) -> (String, &'static str) { + let shadow = ty.ends_with("Shadow"); + let base = ty + .trim_end_matches("Shadow") + .replacen("sampler", "texture", 1); + (base, if shadow { "samplerShadow" } else { "sampler" }) +} + +/// Parses and validates one stage; errors at the pack's files and lines. +fn compile(stage: &Stage, text: &str, header_lines: usize) -> Result> { + let at = |span: naga::Span, message: String| { + let start = span.to_range().map_or(0, |r| r.start); + let line = text[..start.min(text.len())].matches('\n').count(); + match line + .checked_sub(header_lines) + .and_then(|l| stage.pp.lines.get(l)) + { + Some(o) => Error { + file: stage + .pp + .files + .get(o.file as usize) + .cloned() + .unwrap_or_default(), + line: o.line, + message, + }, + None => Error { + file: "".into(), + line: line as u32 + 1, + message, + }, + } + }; + let module = Frontend::default() + .parse(&Options::from(stage.kind), text) + .map_err(|e| { + e.errors + .into_iter() + .map(|e| at(e.meta, e.kind.to_string())) + .collect::>() + })?; + Validator::new(ValidationFlags::all(), Capabilities::all()) + .validate(&module) + .map_err(|e| { + let span = e.spans().next().map_or(naga::Span::default(), |(s, _)| *s); + // The validator nests its reasons: say them all. + let inner = e.into_inner(); + let mut message = inner.to_string(); + let mut source = std::error::Error::source(&inner); + while let Some(s) = source { + write!(message, ": {s}").unwrap(); + source = s.source(); + } + vec![at(span, message)] + })?; + Ok(module) +} + +/// The uniform block's size and member offsets, from naga's layout. +fn block_layout(module: &naga::Module) -> (u32, Vec<(String, u32)>) { + for (_, var) in module.global_variables.iter() { + if var.space != naga::AddressSpace::Uniform { + continue; + } + if let naga::TypeInner::Struct { members, span } = &module.types[var.ty].inner { + let offsets = members + .iter() + .filter_map(|m| Some((m.name.clone()?, m.offset))) + .collect(); + return (*span, offsets); + } + } + (16, vec![("hz_target".into(), 0)]) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::pack::tests::zip; + use crate::{Macros, Pack, preprocess}; + + fn program(files: &[(&str, &str)], target: Target) -> Result> { + let files: Vec<(String, &str)> = files + .iter() + .map(|(p, t)| (format!("shaders/{p}"), *t)) + .collect(); + let files: Vec<(&str, &str)> = files.iter().map(|(p, t)| (p.as_str(), *t)).collect(); + let pack = Pack::from_zip_bytes("P.zip", zip(&files)).unwrap(); + let m = Macros::standard(); + translate_composite( + &preprocess(&pack, "composite.vsh", &m), + &preprocess(&pack, "composite.fsh", &m), + target, + ) + } + + const VSH_120: &str = "#version 120\nvarying vec2 texcoord;\nvoid main() {\n gl_Position = ftransform();\n \ + texcoord = (gl_TextureMatrix[0] * gl_MultiTexCoord0).xy;\n}\n"; + + #[test] + fn compatibility_programs_translate() { + let fsh = "#version 120\nuniform sampler2D colortex0;\nuniform sampler2D depthtex0;\nuniform float viewWidth;\n\ + uniform bool hideGUI;\nuniform int frameCounter;\nvarying vec2 texcoord;\n\ + vec3 tint(sampler2D s, vec2 c) { return texture2D(s, c).rgb; }\nvoid main() {\n \ + vec3 c = tint(colortex0, texcoord) * texture2D(depthtex0, texcoord).r;\n if (hideGUI) c *= 0.5;\n \ + /* DRAWBUFFERS:04 */\n gl_FragData[0] = vec4(c, 1.0);\n gl_FragData[1] = vec4(frameCounter / viewWidth);\n}\n"; + let p = program( + &[("composite.vsh", VSH_120), ("composite.fsh", fsh)], + Target::Buffers, + ) + .unwrap(); + let i = &p.interface; + let samplers: Vec<(&str, u32)> = i + .samplers + .iter() + .map(|s| (s.name.as_str(), s.binding)) + .collect(); + assert_eq!(samplers, [("colortex0", 1), ("depthtex0", 3)]); + let uniforms: Vec<(&str, &str)> = i + .uniforms + .iter() + .map(|u| (u.name.as_str(), u.ty.as_str())) + .collect(); + assert_eq!( + uniforms, + [ + ("hz_target", "vec4"), + ("hz_fog_color", "vec4"), + ("hz_fog", "vec4"), + ("frameCounter", "int"), + ("hideGUI", "bool"), + ("viewWidth", "float") + ] + ); + assert_eq!(i.uniforms[0].offset, 0); + assert!(i.uniform_size >= 28); + assert_eq!(i.outputs, [0, 1]); + } + + #[test] + fn core_programs_translate() { + let vsh = "#version 330 core\nin vec3 vaPosition;\nin vec2 vaUV0;\nuniform mat4 modelViewMatrix, projectionMatrix;\n\ + out vec2 uv;\nflat out int flatId;\nvoid main() {\n \ + gl_Position = projectionMatrix * modelViewMatrix * vec4(vaPosition, 1.0);\n uv = vaUV0;\n flatId = 1;\n}\n"; + let fsh = "#version 330 core\nin vec2 uv;\nuniform sampler2D colortex0;\nuniform usampler2D colortex5;\n\ + layout(location = 2) out vec4 color;\nvoid main() {\n \ + if (texture(colortex0, uv).a < 0.1) discard;\n color = texture(colortex0, uv) + float(texelFetch(colortex5, ivec2(gl_FragCoord.xy), 0).r);\n}\n"; + let p = program( + &[("composite.vsh", vsh), ("composite.fsh", fsh)], + Target::Window, + ) + .unwrap(); + assert_eq!(p.interface.outputs, [2]); + assert!( + p.interface + .uniforms + .iter() + .any(|u| u.name == "projectionMatrix" && u.ty == "mat4") + ); + } + + #[test] + fn errors_point_at_the_pack_file_and_line() { + let fsh = + "#version 120\n#include \"/lib/bad.glsl\"\nvoid main() { gl_FragColor = vec4(f()); }\n"; + let bad = "// fine\nfloat f() {\n return undefinedThing;\n}\n"; + let errors = program( + &[ + ("composite.vsh", VSH_120), + ("composite.fsh", fsh), + ("lib/bad.glsl", bad), + ], + Target::Buffers, + ) + .err() + .unwrap(); + assert_eq!( + (errors[0].file.as_str(), errors[0].line), + ("lib/bad.glsl", 3), + "{errors:?}" + ); + } +} diff --git a/crates/horizon-shaders/src/translate/rewrite.rs b/crates/horizon-shaders/src/translate/rewrite.rs new file mode 100644 index 000000000..312575139 --- /dev/null +++ b/crates/horizon-shaders/src/translate/rewrite.rs @@ -0,0 +1,257 @@ +//! The second pass: the stage's code written again for naga, line for line, and the +//! declarations of ours it needs. + +use std::collections::{BTreeMap, HashMap}; +use std::fmt::Write; + +use naga::ShaderStage; + +use super::scan::{LATER_KEYWORDS, Stage, UNIFORM_STATE, VERTEX_INPUTS, is_ident, significant}; +use super::{Sampler, split_sampler}; + +impl Stage<'_> { + /// Declarations of ours this stage uses (after the uniform block, samplers and varyings). + pub(super) fn prelude(&self) -> String { + let mut p = String::from( + "float hz_modf(float x, out float i) { i = trunc(x); return x - i; }\n\ + vec2 hz_modf(vec2 x, out vec2 i) { i = trunc(x); return x - i; }\n\ + vec3 hz_modf(vec3 x, out vec3 i) { i = trunc(x); return x - i; }\n\ + vec4 hz_modf(vec4 x, out vec4 i) { i = trunc(x); return x - i; }\n\ + struct hz_FogParameters { vec4 color; float density; float start; float end; float scale; };\n\ + hz_FogParameters hz_Fog;\n\ + const mat4 hz_ModelViewMatrix = mat4(1.0); const mat4 hz_ProjectionMatrix = mat4(2.0, 0.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, -1.0, -1.0, 0.0, 1.0);\n\ + const mat4 hz_ModelViewProjectionMatrix = hz_ProjectionMatrix; const mat4 hz_ModelViewMatrixInverse = mat4(1.0); const mat3 hz_NormalMatrix = mat3(1.0);\n\ + const mat4 hz_ProjectionMatrixInverse = mat4(0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.5, 0.5, 0.0, 1.0);\n\ + const mat4 hz_TextureMatrix[8] = mat4[8](mat4(1.0), mat4(1.0), mat4(1.0), mat4(1.0), mat4(1.0), mat4(1.0), mat4(1.0), mat4(1.0));\n", + ); + if self.kind == ShaderStage::Vertex { + p.push_str( + "const vec2 hz_quad[6] = vec2[6](vec2(0.0, 0.0), vec2(1.0, 0.0), vec2(1.0, 1.0), vec2(0.0, 0.0), vec2(1.0, 1.0), vec2(0.0, 1.0));\n\ + vec4 hz_Vertex; vec4 hz_Color; vec4 hz_SecondaryColor; vec3 hz_Normal; float hz_FogCoord;\n\ + vec4 hz_MultiTexCoord0; vec4 hz_MultiTexCoord1; vec4 hz_MultiTexCoord2; vec4 hz_MultiTexCoord3;\n\ + vec4 hz_MultiTexCoord4; vec4 hz_MultiTexCoord5; vec4 hz_MultiTexCoord6; vec4 hz_MultiTexCoord7;\n", + ); + } else { + for n in &self.outputs { + if !self.frag_outs.iter().any(|(at, _)| at == n) { + writeln!(p, "layout(location = {n}) out vec4 hz_out{n};").unwrap(); + } + } + for (_, decl) in &self.frag_outs { + writeln!(p, "{decl}").unwrap(); + } + p.push_str("vec4 hz_FragCoord;\n"); + } + p + } + + /// The pack's code with the rewrites above; the same number of lines. + pub(super) fn rewrite( + &self, + samplers: &[Sampler], + uniforms: &BTreeMap, + ) -> String { + let removed = |i: usize| self.removed.iter().any(|r| r.contains(&i)); + let sampler_ty: HashMap<&str, &str> = samplers + .iter() + .map(|s| (s.name.as_str(), s.ty.as_str())) + .collect(); + let old = self.pp.version.as_ref().is_none_or(|(v, _)| *v < 400); + let fragment = self.kind == ShaderStage::Fragment; + let mut out = String::with_capacity(self.pp.code.len() + 1024); + // Per open bracket: the function called, and the closing text to add. + let mut calls: Vec<(&str, &str, usize)> = Vec::new(); + // Sampler parameters of the function being written: name → type. + let mut params: HashMap = HashMap::new(); + let mut depth = 0; + let mut skip_until = 0; + let next = |i: usize| (i + 1..self.tokens.len()).find(|&j| significant(self.tokens[j])); + let prev = |i: usize| (0..i).rev().find(|&j| significant(self.tokens[j])); + for (i, &t) in self.tokens.iter().enumerate() { + if i < skip_until { + continue; + } + if let Some(text) = self.replaced.get(&i) { + out.push_str(text); + continue; + } + if removed(i) { + out.extend(t.chars().filter(|&c| c == '\n')); + if t.contains(' ') && !t.contains('\n') { + out.push(' '); + } + continue; + } + let called = next(i).is_some_and(|j| self.tokens[j] == "("); + match t { + "{" => depth += 1, + "}" => { + depth -= 1; + if depth == 0 { + params.clear(); + } + } + "(" => { + let callee = prev(i).map_or("", |j| self.tokens[j]); + let close = if matches!(callee, "shadow2D" | "shadow2DLod" | "shadow2DProj") { + ")" + } else { + "" + }; + calls.push((callee, close, 0)); + } + ")" => { + if let Some((_, close, _)) = calls.pop() { + out.push_str(close); + } + } + "," => { + if let Some(c) = calls.last_mut() { + c.2 += 1; + } + } + _ => {} + } + // `T x[] = T[](a, b)`: naga gives unsized arrays no size, so the count is written in. + if t == "[" + && let Some(close) = next(i).filter(|&j| self.tokens[j] == "]") + { + if let Some(n) = self.array_length(close) { + write!(out, "[{n}]").unwrap(); + skip_until = close + 1; + continue; + } + } + if !is_ident(t) { + out.push_str(t); + continue; + } + // A function definition's sampler parameters: `sampler2D s` → `texture2D s, sampler hz_s_s`. + if depth == 0 && t.contains("sampler") && calls.len() == 1 { + if let Some(n) = next(i).filter(|&n| is_ident(self.tokens[n])) { + let name = self.tokens[n]; + let (texture, sampler) = split_sampler(t); + write!(out, "{texture} {name}, {sampler} hz_s_{name}").unwrap(); + params.insert(name.to_owned(), t.to_owned()); + skip_until = n + 1; + continue; + } + } + let sampler = params + .get(t) + .map(String::as_str) + .or_else(|| sampler_ty.get(t).copied()); + if let Some(ty) = sampler { + let as_argument = calls.last().is_some_and(|(callee, _, arg)| { + self.sampler_functions + .get(*callee) + .is_some_and(|p| p.get(*arg) == Some(&true)) + }) && prev(i) + .is_some_and(|j| matches!(self.tokens[j], "(" | ",")); + if as_argument { + write!(out, "{t}, hz_s_{t}").unwrap(); + } else { + write!(out, "{ty}({t}, hz_s_{t})").unwrap(); + } + continue; + } + if uniforms.get(t).is_some_and(|ty| ty == "bool") { + write!(out, "(hz_bool_{t} != 0)").unwrap(); + continue; + } + let replaced: Option = match t { + "main" if depth == 0 && called => Some("hz_main".into()), + // GLSL's `modf`, which naga's frontend lacks. + "modf" if called => Some("hz_modf".into()), + "texture2D" | "texture1D" | "texture3D" | "textureCube" if called => { + Some("texture".into()) + } + "texture2DLod" | "texture3DLod" | "texture2DLodARB" | "textureCubeLod" + if called => + { + Some("textureLod".into()) + } + "texture2DProj" if called => Some("textureProj".into()), + "texture2DGrad" | "texture2DGradARB" if called => Some("textureGrad".into()), + "shadow2D" if called => Some("vec4(texture".into()), + "shadow2DLod" if called => Some("vec4(textureLod".into()), + "shadow2DProj" if called => Some("vec4(textureProj".into()), + "ftransform" if called => { + // `ftransform()` → the product it stands for; its `()` stays as a no-op call... so skip it. + let close = next(i).and_then(next); + skip_until = close.map_or(i + 1, |c| c + 1); + Some("(hz_ModelViewProjectionMatrix * hz_Vertex)".into()) + } + "gl_FragColor" => Some("hz_out0".into()), + "gl_FragData" | "gl_TexCoord" => match self.index_after(i) { + Some((n, close)) => { + skip_until = close + 1; + Some(match t { + "gl_FragData" => format!("hz_out{n}"), + _ => format!("hz_TexCoord{n}"), + }) + } + None => None, + }, + "gl_Color" if fragment => Some("hz_FrontColor".into()), + "gl_FragCoord" if !fragment => None, + "gl_FragCoord" => Some("hz_FragCoord".into()), + "gl_FrontColor" => Some("hz_FrontColor".into()), + "gl_Fog" => Some("hz_Fog".into()), + _ if UNIFORM_STATE.iter().any(|b| t.starts_with(b)) => { + Some(format!("hz_{}", &t[3..])) + } + _ if !fragment && VERTEX_INPUTS.iter().any(|b| t.starts_with(b)) => { + Some(format!("hz_{}", &t[3..])) + } + "gl_VertexID" => Some("gl_VertexIndex".into()), + "gl_InstanceID" => Some("gl_InstanceIndex".into()), + _ if old && LATER_KEYWORDS.contains(&t) => Some(format!("hz_kw_{t}")), + _ => None, + }; + out.push_str(replaced.as_deref().unwrap_or(t)); + } + out + } +} + +impl Stage<'_> { + /// The element count of an unsized array whose `[]` ends at `close`: the arguments of the + /// constructor that follows (`[] = T[](...)` or `[](...)`). + pub(super) fn array_length(&self, close: usize) -> Option { + let mut j = close + 1; + let mut seen_eq = false; + while let Some(&t) = self.tokens.get(j) { + match t { + "(" => break, + "=" if !seen_eq => seen_eq = true, + ";" | "," | ")" | "{" => return None, + _ => {} + } + j += 1; + } + let (mut depth, mut args, mut any) = (0, 1, false); + for &t in &self.tokens[j + 1..] { + match t { + "(" | "[" | "{" => depth += 1, + ")" if depth == 0 => return any.then_some(args), + ")" | "]" | "}" => depth -= 1, + "," if depth == 0 => args += 1, + _ if significant(t) => any = true, + _ => {} + } + } + None + } +} + +/// What our vertex `main` gives a vertex input of a composite-style program. +pub(super) fn attribute_value(name: &str, ty: &str) -> String { + match name { + "vaPosition" => "vec3(hz_p, 0.0)".into(), + "vaUV0" => "hz_p".into(), + "vaColor" => "vec4(1.0)".into(), + "vaNormal" => "vec3(0.0, 0.0, 1.0)".into(), + _ => format!("{ty}(0)"), + } +} diff --git a/crates/horizon-shaders/src/translate/scan.rs b/crates/horizon-shaders/src/translate/scan.rs new file mode 100644 index 000000000..1fd91dd94 --- /dev/null +++ b/crates/horizon-shaders/src/translate/scan.rs @@ -0,0 +1,399 @@ +//! The first pass over a stage's tokens: what it declares at global scope, which of its +//! functions take samplers, which outputs and built-ins it uses. + +use std::collections::{BTreeSet, HashMap}; +use std::fmt::Write; + +use naga::ShaderStage; + +use super::Error; +use super::rewrite::attribute_value; +use crate::Preprocessed; +use crate::macros::lex; + +/// A varying: its name and its declaration with `@` where the name goes (`flat ivec2 @[2];`). +pub(super) struct Var { + pub(super) name: String, + pub(super) decl: String, +} + +pub(super) struct Stage<'p> { + pub(super) kind: ShaderStage, + pub(super) pp: &'p Preprocessed, + pub(super) tokens: Vec<&'p str>, + /// Token ranges to blank out (declarations moved into the header). + pub(super) removed: Vec>, + /// Name, type with its array suffix, token of the type. + pub(super) uniforms: Vec<(String, String, usize)>, + pub(super) samplers: Vec<(String, String)>, + pub(super) ins: Vec, + pub(super) outs: Vec, + /// Vertex inputs, now private globals, and the value our `main` gives each. + pub(super) attributes: Vec<(String, String)>, + /// The pack's fragment outputs, declared again with their locations. + pub(super) frag_outs: Vec<(u32, String)>, + /// The pack's functions that take samplers: which parameters are samplers. + pub(super) sampler_functions: HashMap>, + pub(super) outputs: BTreeSet, + pub(super) frag_coord: bool, + /// Tokens written differently, by index. + pub(super) replaced: HashMap, + /// Globals whose initializer runs in our `main` (`g = hz_init_g();`), in order. + pub(super) inits: Vec, + pub(super) errors: Vec, +} + +pub(super) const STORAGE: [&str; 5] = ["uniform", "in", "out", "attribute", "varying"]; +pub(super) const QUALIFIERS: [&str; 11] = [ + "flat", + "smooth", + "noperspective", + "centroid", + "invariant", + "highp", + "mediump", + "lowp", + "precise", + "sample", + "const", +]; +/// The compatibility profile's uniform state (with its `Inverse`/`Transpose` forms). +pub(super) const UNIFORM_STATE: [&str; 5] = [ + "gl_ModelViewMatrix", + "gl_ProjectionMatrix", + "gl_ModelViewProjectionMatrix", + "gl_NormalMatrix", + "gl_TextureMatrix", +]; +/// The compatibility profile's vertex inputs. +pub(super) const VERTEX_INPUTS: [&str; 6] = [ + "gl_Vertex", + "gl_Color", + "gl_SecondaryColor", + "gl_Normal", + "gl_FogCoord", + "gl_MultiTexCoord", +]; +/// Words that became keywords after GLSL 1.x and that old packs use as names. +pub(super) const LATER_KEYWORDS: [&str; 17] = [ + "sample", + "buffer", + "shared", + "filter", + "input", + "output", + "common", + "partition", + "active", + "resource", + "patch", + "subroutine", + "coherent", + "volatile", + "restrict", + "readonly", + "writeonly", +]; + +pub(super) fn significant(t: &str) -> bool { + !t.trim().is_empty() +} + +pub(super) fn is_ident(t: &str) -> bool { + t.starts_with(|c: char| c.is_ascii_alphabetic() || c == '_') +} + +impl<'p> Stage<'p> { + pub(super) fn scan(pp: &'p Preprocessed, kind: ShaderStage) -> Self { + let mut s = Stage { + kind, + pp, + tokens: lex(&pp.code), + removed: Vec::new(), + uniforms: Vec::new(), + samplers: Vec::new(), + ins: Vec::new(), + outs: Vec::new(), + attributes: Vec::new(), + frag_outs: Vec::new(), + sampler_functions: HashMap::new(), + outputs: BTreeSet::new(), + frag_coord: false, + replaced: HashMap::new(), + inits: Vec::new(), + errors: Vec::new(), + }; + let (mut start, mut depth, mut parens) = (0, 0usize, 0usize); + for i in 0..s.tokens.len() { + match s.tokens[i] { + "(" => parens += 1, + ")" => parens = parens.saturating_sub(1), + "{" if depth == 0 && parens == 0 => { + s.global(start, i, true); + depth = 1; + } + "{" => depth += 1, + "}" if depth > 0 => { + depth -= 1; + if depth == 0 { + start = i + 1; + } + } + ";" if depth == 0 && parens == 0 => { + s.global(start, i, false); + start = i + 1; + } + "gl_FragData" | "gl_FragColor" if kind == ShaderStage::Fragment => { + let n = if s.tokens[i] == "gl_FragColor" { + Some(0) + } else { + s.index_after(i).map(|(n, _)| n) + }; + match n { + Some(n) => _ = s.outputs.insert(n), + None => s + .errors + .push(s.error(i, "`gl_FragData` needs a constant index")), + } + } + "gl_FragCoord" => s.frag_coord = true, + "gl_TexCoord" => { + if let Some((n, _)) = s.index_after(i) { + s.builtin_varying(format!("hz_TexCoord{n}")); + } + } + "gl_FrontColor" => s.builtin_varying("hz_FrontColor".into()), + "gl_Color" if kind == ShaderStage::Fragment => { + s.builtin_varying("hz_FrontColor".into()) + } + _ => {} + } + } + s + } + + pub(super) fn builtin_varying(&mut self, name: String) { + let list = if self.kind == ShaderStage::Vertex { + &mut self.outs + } else { + &mut self.ins + }; + if !list.iter().any(|v| v.name == name) { + list.push(Var { + name, + decl: "vec4 @;".into(), + }); + } + } + + /// `[ n ]` right after token `i`: the number and the index of the `]`. + pub(super) fn index_after(&self, i: usize) -> Option<(u32, usize)> { + let mut it = (i + 1..self.tokens.len()).filter(|&j| significant(self.tokens[j])); + let open = it.next()?; + let n = it.next()?; + let close = it.next()?; + (self.tokens[open] == "[" && self.tokens[close] == "]") + .then(|| self.tokens[n].parse().ok().map(|n| (n, close))) + .flatten() + } + + pub(super) fn error(&self, token: usize, message: &str) -> Error { + let line: usize = self.tokens[..token.min(self.tokens.len())] + .iter() + .map(|t| t.matches('\n').count()) + .sum(); + let o = self.pp.lines.get(line).copied().unwrap_or_default(); + Error { + file: self + .pp + .files + .get(o.file as usize) + .cloned() + .unwrap_or_default(), + line: o.line, + message: message.to_owned(), + } + } + + /// A statement at global scope, tokens `from..to` (without its `;`, or before a `{`). + pub(super) fn global(&mut self, from: usize, to: usize, body: bool) { + let words: Vec = (from..to) + .filter(|&i| significant(self.tokens[i])) + .collect(); + // A prototype: a `(` before any `=`, and no storage qualifier. + let paren = words + .iter() + .position(|&i| matches!(self.tokens[i], "(" | "=")); + let prototype = paren.is_some_and(|p| self.tokens[words[p]] == "(") + && !words.iter().any(|&i| STORAGE.contains(&self.tokens[i])); + if body || prototype { + return self.function(&words); + } + let mut k = 0; + let mut storage = None; + let mut location = None; + let mut interpolation = String::new(); + while let Some(&i) = words.get(k) { + let w = self.tokens[i]; + if w == "layout" { + // layout(location = n, ...) + let mut j = k + 1; + while let Some(&t) = words.get(j) { + j += 1; + if self.tokens[t] == "location" { + location = words + .get(j + 1) + .and_then(|&n| self.tokens[n].parse::().ok()); + } + if self.tokens[t] == ")" { + break; + } + } + k = j; + continue; + } + if STORAGE.contains(&w) { + storage = Some(w); + } else if QUALIFIERS.contains(&w) { + if matches!( + w, + "flat" | "smooth" | "noperspective" | "centroid" | "sample" + ) { + write!(interpolation, "{w} ").unwrap(); + } + } else { + break; + } + k += 1; + } + let Some(storage) = storage else { + return self.initializer(&words, to); + }; + let Some(&ty_at) = words.get(k) else { return }; + let ty = self.tokens[ty_at].to_owned(); + if words[k..].iter().any(|&i| self.tokens[i] == "{") { + return; // An interface block: left as the pack wrote it. + } + // Declarators: name [array] [= init], separated by commas. + let mut declarators: Vec<(String, String)> = Vec::new(); + let mut j = k + 1; + while let Some(&n) = words.get(j) { + let name = self.tokens[n].to_owned(); + let mut array = String::new(); + j += 1; + let mut nesting = 0; + while let Some(&t) = words.get(j) { + match self.tokens[t] { + "," if nesting == 0 => break, + "(" | "[" => nesting += 1, + ")" | "]" => nesting -= 1, + _ => {} + } + if self.tokens[t] == "=" && nesting == 0 { + // An initializer: uniforms are set by the game, so it is dropped. + while words.get(j).is_some_and(|&t| self.tokens[t] != ",") { + j += 1; + } + break; + } + array.push_str(self.tokens[t]); + j += 1; + } + j += 1; + declarators.push((name, array)); + } + let fragment = self.kind == ShaderStage::Fragment; + let input = matches!(storage, "in" | "attribute") || (storage == "varying" && fragment); + self.removed.push(from..to + 1); + for (name, array) in declarators { + let decl = format!("{interpolation}{ty} @{array};"); + match storage { + "uniform" if ty.contains("sampler") => self.samplers.push((name, ty.clone())), + "uniform" if ty.contains("image") => self + .errors + .push(self.error(ty_at, &format!("`{name}`: images are not supported yet"))), + "uniform" => self.uniforms.push((name, format!("{ty}{array}"), ty_at)), + _ if input && !fragment => { + let value = attribute_value(&name, &ty); + self.attributes.push((name, value)); + self.removed.pop(); + // A private global of ours now: `in`/`attribute` and qualifiers go. + for &w in &words[..k] { + self.removed.push(w..w + 1); + } + } + _ if input => self.ins.push(Var { name, decl }), + _ if fragment => { + let n = location.unwrap_or(self.outputs.len() as u32); + self.outputs.insert(n); + self.frag_outs + .push((n, format!("layout(location = {n}) out {ty} {name}{array};"))); + } + _ => self.outs.push(Var { name, decl }), + } + } + } + + /// A global `T name = a ? b : c;`: naga can't evaluate a conditional at global scope, so + /// the initializer becomes a function of ours on the same line (`T name; T hz_init_name() { + /// return a ? b : c; }`), called by our `main` before the pack's. + pub(super) fn initializer(&mut self, words: &[usize], to: usize) { + let text = |i: usize| self.tokens[i]; + let Some(eq) = words.iter().position(|&i| text(i) == "=") else { + return; + }; + if !words[eq..].iter().any(|&i| text(i) == "?") + || words[..eq].iter().any(|&i| text(i) == ",") + { + return; + } + let Some(name_at) = words[..eq].iter().rposition(|&i| is_ident(text(i))) else { + return; + }; + let name = text(words[name_at]).to_owned(); + let array: String = words[name_at + 1..eq].iter().map(|&i| text(i)).collect(); + let ty: Vec<&str> = words[..name_at] + .iter() + .map(|&i| text(i)) + .filter(|&t| t != "const") + .collect(); + let Some(ty) = ty.last() else { return }; + for &i in &words[..name_at] { + if text(i) == "const" { + self.removed.push(i..i + 1); + } + } + self.replaced.insert( + words[eq], + format!("; {ty}{array} hz_init_{name}() {{ return"), + ); + self.replaced.insert(to, "; }".into()); + self.inits.push(name); + } + + /// A function definition or prototype: remember which parameters are samplers. + pub(super) fn function(&mut self, words: &[usize]) { + let Some(open) = words.iter().position(|&i| self.tokens[i] == "(") else { + return; + }; + let Some(&name) = open.checked_sub(1).and_then(|n| words.get(n)) else { + return; + }; + let mut params = Vec::new(); + let mut is_sampler = false; + for &i in &words[open + 1..] { + match self.tokens[i] { + "," | ")" => { + params.push(is_sampler); + is_sampler = false; + } + t if t.contains("sampler") => is_sampler = true, + _ => {} + } + } + if params.iter().any(|&p| p) { + self.sampler_functions + .insert(self.tokens[name].to_owned(), params); + } + } +} diff --git a/crates/horizon-shaders/tests/real_packs.rs b/crates/horizon-shaders/tests/real_packs.rs index c252df6e3..4fac4b0d7 100644 --- a/crates/horizon-shaders/tests/real_packs.rs +++ b/crates/horizon-shaders/tests/real_packs.rs @@ -5,7 +5,11 @@ use std::path::Path; use std::time::Instant; -use horizon_shaders::{Lang, Macros, Pack, Properties, discover, preprocess}; +use std::collections::BTreeMap; + +use horizon_shaders::{ + Lang, Macros, Pack, Properties, Target, discover, preprocess, translate_composite, +}; #[test] #[ignore = "needs HORIZON_TEST_SHADER_PACKS"] @@ -86,6 +90,73 @@ fn real_packs_read() { for d in diagnostics.iter().take(8) { println!(" {d}"); } + // Every composite-style program with both stages, translated and validated. + let start = Instant::now(); + let (mut ok, mut failed, mut causes) = (0, 0, BTreeMap::::new()); + for dir in ["", "world0", "world-1", "world1"] { + for file in pack.list(dir) { + let Some(name) = file.strip_suffix(".fsh") else { + continue; + }; + let composite = [ + "composite", + "deferred", + "final", + "prepare", + "begin", + "shadowcomp", + ] + .iter() + .any(|p| { + name.strip_prefix(p) + .is_some_and(|n| n.chars().all(|c| c.is_ascii_digit())) + }); + let path = |ext: &str| { + if dir.is_empty() { + format!("{name}.{ext}") + } else { + format!("{dir}/{name}.{ext}") + } + }; + if !composite || !pack.exists(&path("vsh")) { + continue; + } + let target = if name == "final" { + Target::Window + } else { + Target::Buffers + }; + let v = preprocess(&pack, &path("vsh"), ¯os); + let fr = preprocess(&pack, &path("fsh"), ¯os); + match translate_composite(&v, &fr, target) { + Ok(_) => ok += 1, + Err(errors) => { + failed += 1; + let first = &errors[0]; + let cause: String = first.message.chars().take(90).collect(); + *causes.entry(cause).or_default() += 1; + if std::env::var_os("SHOW_ERRORS").is_some() { + println!( + " {}: {}:{}: {}", + path("fsh"), + first.file, + first.line, + first.message + ); + } + } + } + } + } + println!( + " composite-style programs: {ok} translate, {failed} fail, {:.0} ms", + start.elapsed().as_secs_f64() * 1000.0 + ); + let mut causes: Vec<_> = causes.into_iter().collect(); + causes.sort_by_key(|c| std::cmp::Reverse(c.1)); + for (cause, n) in causes.iter().take(6) { + println!(" {n} x {cause}"); + } assert!(!shaders.entries.is_empty(), "{}: no keys read", f.name); read += 1; } From 90b5e162ae0f586d9494a68a86319400bb75db9c Mon Sep 17 00:00:00 2001 From: levzzz <86072227+levzzz5154@users.noreply.github.com> Date: Sun, 25 Oct 2026 00:38:39 +0500 Subject: [PATCH 012/107] Play a placed block's sound once for its placer BlockItem.place plays it with playSound(player, ...): the server sends it to the others, and the placer's client plays it as its block appears. The server sent it to the placer too, so each block sounded twice. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/lan_actions.rs | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 5bee2af96..dabd5c2a6 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -2439,6 +2439,32 @@ impl Game<'_> { .map_or(&[][..], |log| log.get(logged..).unwrap_or(&[])) .to_vec(); let facts = self.res.facts(); + // BlockItem.place: `playSound(player, ...)`, for the others only. The + // placer's client plays it as its block appears. + let placed: Vec<([f64; 3], String)> = changed + .iter() + .map(|&(x, y, z)| { + let at = [f64::from(x) + 0.5, f64::from(y) + 0.5, f64::from(z) + 0.5]; + let name = &facts.get(self.world.block(x, y, z)).sounds.place; + (at, name.trim_start_matches("minecraft:").to_owned()) + }) + .collect(); + if let Some(capture) = self.audio.capture.as_mut() { + let mut i = heard.min(capture.len()); + while i < capture.len() { + let (name, at, ..) = &capture[i]; + let place = placed + .iter() + .any(|(p, n)| p == at && name.trim_start_matches("minecraft:") == n.as_str()); + if place { + let (name, at, volume, pitch) = capture.remove(i); + let source = horizon_client::settings::sound_source(&name) as i32; + host.hub.sound(&name, at, volume, pitch, source, Some(conn)); + } else { + i += 1; + } + } + } if changed.is_empty() || !changed.iter().all(|&(x, y, z)| { super::remote::hand_operated(&facts.get(self.world.block(x, y, z)).name) From a5f5d8df34293f561e0bb102d1e74f1ae1fce55d Mon Sep 17 00:00:00 2001 From: socksprox Date: Sat, 24 Oct 2026 21:22:42 +0200 Subject: [PATCH 013/107] Server packs: the download progress toast #1570 landed the client pack manager without vanilla's PackDownloadNotificationComponent: the SystemToast that follows a download with download.pack.progress.percent or .bytes and stays as download.pack.failed when a batch fails. The download queue reports requestStart/downloadedBytes/downloadFinished on a channel (vanilla's DownloadProgressListener), the game ticks them into a toast it slides in for the vanilla 5 seconds with the toast in/out sounds. Co-Authored-By: Devin --- crates/horizon-client/src/game.rs | 1 + .../horizon-client/src/game/server_packs.rs | 139 +++++++++++++++++- .../src/server_packs/download.rs | 81 ++++++++-- crates/horizon-client/src/title.rs | 2 +- 4 files changed, 204 insertions(+), 19 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index bf8327cc1..e162c9e0f 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -5034,6 +5034,7 @@ impl Game<'_> { Screen::Playing | Screen::Options | Screen::Win => {} } self.draw_toasts(&mut batch); + self.draw_pack_toast(&mut batch); self.remote_hud(&mut batch); self.draw_sign_editor(&mut batch); self.draw_book(&mut batch); diff --git a/crates/horizon-client/src/game/server_packs.rs b/crates/horizon-client/src/game/server_packs.rs index 6c891a831..6fdec751e 100644 --- a/crates/horizon-client/src/game/server_packs.rs +++ b/crates/horizon-client/src/game/server_packs.rs @@ -12,10 +12,11 @@ use horizon_client::{ scoreboard::{comp_text, tag_to_json}, server_packs::{Prompt, ServerPacks, action, download, parse_sha1, valid_url}, text::{Text, named_color}, + toasts::Slide, ui, }; use horizon_protocol::{packets::configuration::ResourcePackPush, servers}; -use std::path::PathBuf; +use std::{path::PathBuf, sync::mpsc::Receiver}; /// `ClientCommonPacketListenerImpl.PackConfirmScreen`: the packs it asks /// about, whether the last one is required, and the server's message. @@ -75,6 +76,32 @@ pub(super) struct ServerPackState { user: (String, u128), /// The packs loaded on top of the player's own, bottom first. pub(super) active: Vec, + /// `packDownloadNotifier` (`PackDownloadNotificationComponent`): the + /// toast while packs download; `downloadFinished` hides it, a failed + /// batch turns it into `download.pack.failed` until it slides out. + toast: Option, + toast_rx: Option>, +} +/// The download toast: vanilla's `SystemToast` `download.pack.title` with the +/// running progress, `download.pack.failed` when a batch fails. +#[derive(Default)] +struct PackToast { + slide: Slide, + /// `downloadedBytes`: the bytes the request in flight has written. + bytes: u64, + /// Its Content-Length — the progress is a percent, else a byte count. + size: Option, + failed: bool, +} +/// `Unit.humanReadable`, for `download.pack.progress.bytes`. +fn human_readable(bytes: u64) -> String { + const UNITS: [&str; 7] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"]; + let (mut value, mut unit) = (bytes as f64, 0); + while value >= 1024.0 && unit < UNITS.len() - 1 { + value /= 1024.0; + unit += 1; + } + format!("{value:.1} {}", UNITS[unit]) } fn saves() -> PathBuf { @@ -89,6 +116,8 @@ impl Game<'_> { let state = &mut self.server_packs; state.user = user; state.prompt = None; + state.toast = None; + state.toast_rx = None; let Some(address) = address else { state.address = None; state.packs.local_world(); @@ -103,6 +132,8 @@ impl Game<'_> { /// `Minecraft.clearDownloadedResourcePacks`: every server pack goes at once. pub(super) fn server_packs_disconnect(&mut self) { self.server_packs.prompt = None; + self.server_packs.toast = None; + self.server_packs.toast_rx = None; self.server_packs.packs.disconnect(); self.tick_server_packs(); } @@ -197,10 +228,56 @@ impl Game<'_> { /// that finished, the ones to start, a resource reload when the active /// packs change, and the statuses for the server. pub(super) fn tick_server_packs(&mut self) { + // SystemToast.update: the toast slides for 5000 ms fully shown. + if let Some(toast) = &mut self.server_packs.toast { + let (sound, finished) = toast.slide.update_now(|fully| fully < 5000); + if let Some(sound) = sound { + self.audio.play( + if sound { + "minecraft:ui.toast.in" + } else { + "minecraft:ui.toast.out" + }, + None, + 1.0, + 1.0, + ); + } + if finished { + self.server_packs.toast = None; + } + } // A reload waits for the world's resources (`reload_packs`). if self.rest_loader.is_some() || self.world_loader.is_some() { return; } + let progress = self + .server_packs + .toast_rx + .as_ref() + .map(|rx| rx.try_iter().collect::>()) + .unwrap_or_default(); + for p in progress { + let toast = &mut self.server_packs.toast; + match p { + download::Progress::RequestStart(size) => { + if let Some(t) = toast { + t.bytes = 0; + t.size = size; + } + } + download::Progress::Bytes(done) => { + if let Some(t) = toast { + t.bytes = done; + } + } + download::Progress::Finished => { + if toast.as_ref().is_some_and(|t| !t.failed) { + self.server_packs.toast = None; + } + } + } + } let finished = self .server_packs .queue @@ -208,7 +285,16 @@ impl Game<'_> { .map(download::Queue::finished) .unwrap_or_default(); for batch in finished { + let failed = !batch.failed.is_empty(); self.server_packs.packs.downloaded(batch); + if failed { + // PackDownloadNotificationComponent.onFailure: the failure + // toast replaces the progress one and stays its 5 seconds. + self.server_packs.toast = Some(PackToast { + failed: true, + ..PackToast::default() + }); + } } while self.server_packs.packs.wants_tick() { let tick = self.server_packs.packs.tick(); @@ -238,6 +324,10 @@ impl Game<'_> { Ok(queue) => state.queue = Some(queue), Err(e) => { log::error!("Horizon: failed to open the download queue: {e}"); + state.toast = Some(PackToast { + failed: true, + ..PackToast::default() + }); let failed = requests.iter().map(|r| r.id).collect(); state.packs.downloaded(horizon_client::server_packs::Batch { downloaded: Vec::new(), @@ -248,8 +338,12 @@ impl Game<'_> { } } let headers = download::headers(&state.user.0, state.user.1); + // PackDownloadNotificationComponent: a toast follows the download. + let (progress, rx) = std::sync::mpsc::channel(); + state.toast = Some(PackToast::default()); + state.toast_rx = Some(rx); if let Some(queue) = &state.queue { - queue.download(requests, headers); + queue.download(requests, headers, progress); } } /// `DownloadedPackSource.startReload`: the packs on top of the player's @@ -297,6 +391,47 @@ impl Game<'_> { .layout(atlas, (w, h)) .draw(batch, atlas, self.cursor, [&yes, &no]); } + /// `PackDownloadNotificationComponent`'s toast: `download.pack.title` + /// with the running progress, or `download.pack.failed` alone. + pub(super) fn draw_pack_toast(&self, batch: &mut HudBatch) { + let Some(toast) = &self.server_packs.toast else { + return; + }; + let atlas = &self.gui.atlas; + let t = |key: &str| Text::translate(key, &[]).plain(); + let title = if toast.failed { + t("download.pack.failed") + } else { + t("download.pack.title") + }; + let body = (!toast.failed).then(|| { + if let Some(size) = toast.size.filter(|s| *s > 0) { + Text::tr( + "download.pack.progress.percent", + &[&(toast.bytes * 100 / size)], + ) + .plain() + } else { + Text::tr( + "download.pack.progress.bytes", + &[&human_readable(toast.bytes)], + ) + .plain() + } + }); + let width = body.as_ref().map_or_else( + || HudBatch::text_width(atlas, &title), + |body| HudBatch::text_width(atlas, &title).max(HudBatch::text_width(atlas, body)), + ); + let width = width.ceil().max(160.0) + 30.0; + // Toast.xPos: in from the right edge by the visible portion. + let x = batch.width - width * toast.slide.portion; + horizon_client::title::toast_background(atlas, batch, x, width); + batch.text(atlas, &title, x + 18.0, 7.0, u32::MAX, false); + if let Some(body) = &body { + batch.text(atlas, body, x + 18.0, 19.0, u32::MAX, false); + } + } /// A left click on the prompt: its answer when it hit a button. pub(super) fn pack_prompt_click(&mut self) -> Result<()> { let Some(prompt) = &self.server_packs.prompt else { diff --git a/crates/horizon-client/src/server_packs/download.rs b/crates/horizon-client/src/server_packs/download.rs index 595612df8..c299db9b9 100644 --- a/crates/horizon-client/src/server_packs/download.rs +++ b/crates/horizon-client/src/server_packs/download.rs @@ -34,7 +34,19 @@ pub fn headers(name: &str, uuid: u128) -> Vec<(&'static str, String)> { ] } -type Job = (Vec, Vec<(&'static str, String)>); +/// `DownloadProgressListener`, on the channel a download was given. +#[derive(Clone, Copy, Debug)] +pub enum Progress { + /// `requestStart`: a request began; its Content-Length when the + /// response said one. + RequestStart(Option), + /// `downloadedBytes`: the bytes this request has written so far. + Bytes(u64), + /// `downloadFinished`: the batch ran, whatever it downloaded. + Finished, +} + +type Job = (Vec, Vec<(&'static str, String)>, Sender); /// `DownloadQueue`: batches download in order, on one thread. pub struct Queue { @@ -50,16 +62,24 @@ impl Queue { std::thread::Builder::new() .name("download-queue".into()) .spawn(move || { - for (requests, headers) in todo { - if finished.send(run(&dir, &requests, &headers)).is_err() { + for (requests, headers, progress) in todo { + if finished + .send(run(&dir, &requests, &headers, &progress)) + .is_err() + { break; } } })?; Ok(Self { jobs, done }) } - pub fn download(&self, requests: Vec, headers: Vec<(&'static str, String)>) { - let _ = self.jobs.send((requests, headers)); + pub fn download( + &self, + requests: Vec, + headers: Vec<(&'static str, String)>, + progress: Sender, + ) { + let _ = self.jobs.send((requests, headers, progress)); } /// The batches finished since the last call. pub fn finished(&self) -> Vec { @@ -68,11 +88,16 @@ impl Queue { } /// `DownloadQueue.runDownload`: each pack into `//`, logged. -fn run(dir: &Path, requests: &[Request], headers: &[(&'static str, String)]) -> Batch { +fn run( + dir: &Path, + requests: &[Request], + headers: &[(&'static str, String)], + progress: &Sender, +) -> Batch { let mut batch = Batch::default(); for request in requests { let id = horizon_protocol::crypto::uuid_string(request.id); - let result = download_file(&dir.join(&id), request, headers, MAX_SIZE); + let result = download_file(&dir.join(&id), request, headers, MAX_SIZE, progress); // DownloadQueue.LogEntry, its fields in the codec's order. let mut entry = vec![ ("id", serde_json::Value::from(id)), @@ -112,6 +137,7 @@ fn run(dir: &Path, requests: &[Request], headers: &[(&'static str, String)]) -> Err(_) => batch.failed.push(request.id), } } + let _ = progress.send(Progress::Finished); batch } @@ -165,6 +191,7 @@ pub fn download_file( request: &Request, headers: &[(&'static str, String)], max_size: u64, + progress: &Sender, ) -> Result { let target = request.sha1.as_ref().map(|sha1| dir.join(sha1)); if let (Some(target), Some(sha1)) = (&target, &request.sha1) { @@ -205,10 +232,15 @@ pub fn download_file( "Filesize is bigger than maximum allowed (file is {size}, limit is {max_size})" )); } + let _ = progress.send(Progress::RequestStart(size)); let mut input = response.into_reader(); + let mut report = |written: u64| { + let _ = progress.send(Progress::Bytes(written)); + }; match (&target, &request.sha1) { (Some(target), Some(sha1)) => { - let actual = save_hashed(&mut input, target, max_size).map_err(|e| e.to_string())?; + let actual = save_hashed(&mut input, target, max_size, &mut report) + .map_err(|e| e.to_string())?; if &actual != sha1 { return Err(format!( "Hash of downloaded file ({actual}) did not match requested ({sha1})" @@ -218,7 +250,7 @@ pub fn download_file( } _ => { let tmp = dir.join(format!("download{:016x}.tmp", rand_suffix())); - let result = save_hashed(&mut input, &tmp, max_size).and_then(|actual| { + let result = save_hashed(&mut input, &tmp, max_size, &mut report).and_then(|actual| { let path = dir.join(&actual); if matches(&path, &actual) { touch(&path); @@ -237,7 +269,12 @@ fn rand_suffix() -> u64 { RandomState::new().hash_one(std::time::Instant::now()) } /// `downloadAndHash`: write what comes, at most `max_size` bytes. -fn save_hashed(input: &mut dyn Read, path: &Path, max_size: u64) -> io::Result { +fn save_hashed( + input: &mut dyn Read, + path: &Path, + max_size: u64, + progress: &mut dyn FnMut(u64), +) -> io::Result { let mut out = fs::File::create(path)?; let mut hasher = Sha1::new(); let mut buffer = [0u8; 8196]; @@ -248,6 +285,7 @@ fn save_hashed(input: &mut dyn Read, path: &Path, max_size: u64) -> io::Result max_size { return Err(io::Error::other(format!( "Filesize was bigger than maximum allowed (got >= {read_so_far}, limit was {max_size})" @@ -366,7 +404,14 @@ mod tests { url, sha1: Some(sha1(&body)), }; - let path = download_file(&dir, &request, &headers("Alex", 0xab), MAX_SIZE).unwrap(); + let path = download_file( + &dir, + &request, + &headers("Alex", 0xab), + MAX_SIZE, + &channel().0, + ) + .unwrap(); assert_eq!(path, dir.join(sha1(&body))); assert_eq!(fs::read(&path).unwrap(), body); let sent = server.join().unwrap(); @@ -377,7 +422,10 @@ mod tests { url: "http://127.0.0.1:9/gone.zip".into(), ..request }; - assert_eq!(download_file(&dir, &cached, &[], MAX_SIZE).unwrap(), path); + assert_eq!( + download_file(&dir, &cached, &[], MAX_SIZE, &channel().0).unwrap(), + path + ); fs::remove_dir_all(dir).unwrap(); } @@ -390,7 +438,7 @@ mod tests { url, sha1: Some("0".repeat(40)), }; - let err = download_file(&dir, &wrong, &[], MAX_SIZE).unwrap_err(); + let err = download_file(&dir, &wrong, &[], MAX_SIZE, &channel().0).unwrap_err(); assert!(err.contains("did not match requested"), "{err}"); server.join().unwrap(); let dead = Request { @@ -398,7 +446,7 @@ mod tests { url: "http://127.0.0.1:9/pack.zip".into(), sha1: None, }; - assert!(download_file(&dir.join("dead"), &dead, &[], MAX_SIZE).is_err()); + assert!(download_file(&dir.join("dead"), &dead, &[], MAX_SIZE, &channel().0).is_err()); assert!(!dir.join("dead").exists()); let (url, server) = serve("200 OK", vec![0; 100]); let large = Request { @@ -406,7 +454,7 @@ mod tests { url, sha1: None, }; - let err = download_file(&dir, &large, &[], 10).unwrap_err(); + let err = download_file(&dir, &large, &[], 10, &channel().0).unwrap_err(); assert!(err.contains("bigger than maximum"), "{err}"); let _ = server.join(); let (url, server) = serve("404 Not Found", Vec::new()); @@ -415,7 +463,7 @@ mod tests { url, sha1: None, }; - assert!(download_file(&dir, &missing, &[], MAX_SIZE).is_err()); + assert!(download_file(&dir, &missing, &[], MAX_SIZE, &channel().0).is_err()); server.join().unwrap(); fs::remove_dir_all(dir).unwrap(); } @@ -433,6 +481,7 @@ mod tests { sha1: None, }], Vec::new(), + channel().0, ); let batch = loop { if let Some(b) = queue.finished().pop() { diff --git a/crates/horizon-client/src/title.rs b/crates/horizon-client/src/title.rs index f77ac93eb..94b1ba2e8 100644 --- a/crates/horizon-client/src/title.rs +++ b/crates/horizon-client/src/title.rs @@ -1554,7 +1554,7 @@ impl Title { } } /// SystemToast's 32-pixel background keeps the notice icon and borders intact. -fn toast_background(atlas: &HudAtlas, batch: &mut HudBatch, x: f32, width: f32) { +pub fn toast_background(atlas: &HudAtlas, batch: &mut HudBatch, x: f32, width: f32) { let Some(rect) = atlas.sprites.get("gui/sprites/toast/system").copied() else { return; }; From d83b3e18bc98d93c20fd47ce5b85367a324f7900 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Dr=C3=A4ger?= <20014538+sdraeger@users.noreply.github.com> Date: Sat, 24 Oct 2026 13:21:23 -0700 Subject: [PATCH 014/107] Keep mob pitch on /tp --- crates/horizon-client/src/game/commands.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/game/commands.rs b/crates/horizon-client/src/game/commands.rs index a229cbdc9..bab925c4f 100644 --- a/crates/horizon-client/src/game/commands.rs +++ b/crates/horizon-client/src/game/commands.rs @@ -1098,9 +1098,11 @@ impl Game<'_> { if let Some(m) = self.mobs.iter_mut().find(|m| m.id == id) { m.position = v; m.previous = v; - if let Some([yaw, _]) = rotation { + if let Some([yaw, pitch]) = rotation { m.yaw = yaw; m.body_yaw = yaw; + // Entity.setXRot clamps to -90..90. + m.head_pitch = pitch.clamp(-90.0, 90.0); } } } From 891e1c12830ce848d799e0b8829bffc5832fcf04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Dr=C3=A4ger?= <20014538+sdraeger@users.noreply.github.com> Date: Sat, 24 Oct 2026 13:21:23 -0700 Subject: [PATCH 015/107] Run client mod hooks on the server thread --- crates/horizon-client/src/game/chat.rs | 8 +++++++- crates/horizon-client/src/game/remote.rs | 5 +++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/game/chat.rs b/crates/horizon-client/src/game/chat.rs index f760bd82f..1bc30513b 100644 --- a/crates/horizon-client/src/game/chat.rs +++ b/crates/horizon-client/src/game/chat.rs @@ -570,8 +570,14 @@ impl Game<'_> { /// ChatScreen.handleChatInput: history, then a command or a chat line. pub(super) fn handle_chat_input(&mut self, message: &str) { self.ui.chat.add_recent(message); - // On a server (single player's own included) the server runs it. + // On a server (single player's own included) the server runs it, + // unless a client mod owns the command. if self.remote.is_some() { + if let Some(command) = message.strip_prefix('/') + && self.mod_command(command) + { + return; + } self.send_chat(message); return; } diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 32cffa306..23c6677c2 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -733,6 +733,9 @@ impl Game<'_> { self.ticks, self.screen == Screen::Chat, ); + if self.mods.running() { + self.mods_chat(&text); + } if let Some(r) = self.remote.as_mut() { r.chat.push((text, Instant::now())); r.chat_received += 1; @@ -3041,6 +3044,8 @@ impl Game<'_> { /// One client tick while connected (Minecraft.tick with a ClientLevel). pub(super) fn tick_remote(&mut self) -> Result<()> { self.tick_remote_level()?; + // Client mods tick on any server, single player's own included. + self.tick_mods(); // Minecraft.tick sends this even when the player cannot tick yet, // including while waiting for chunks. Keep it outside early returns. if self.remote.as_ref().is_some_and(|r| r.joined) { From 72f88befb804421c88c9584b02f0974c33508d0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Dr=C3=A4ger?= <20014538+sdraeger@users.noreply.github.com> Date: Sat, 24 Oct 2026 13:21:23 -0700 Subject: [PATCH 016/107] Allow spaces in horizon-ctl screenshot paths --- crates/horizon-client/src/game/control.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index 205a66074..c6690d435 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -503,7 +503,10 @@ impl Game<'_> { if self.pending_screenshot.is_some() { return Err("a screenshot is already pending".into()); } - renderer.screenshot = Some(arg(1)?.into()); + arg(1)?; + // The rest of the line: a path may hold spaces. + let path = line.trim_start().strip_prefix("screenshot").unwrap_or(""); + renderer.screenshot = Some(path.trim().into()); self.pending_screenshot = Some(reply.clone()); Ok(None) } From d064dba945969f26b74231e34e2010b1523dc946 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 24 Oct 2026 22:23:43 +0200 Subject: [PATCH 017/107] Shader packs, clean room 7: buffer settings and output lists from stage files The configuration packs write into their stage files, read from live code only (the preprocessor has dropped false branches, so an option's choice is the one in effect): - the buffers a fragment shader writes: the last live DRAWBUFFERS or RENDERTARGETS comment, at most 8, buffers 0-7 without one; - per colortex buffer (names and aliases): Format in block comments, with every format name of the spec in any case, as channels, bits and kind; Clear, ClearColor and MipmapEnabled. A later file changing a setting, an unknown format and a value of the wrong type are reported with their file and line. Buffer 1 becomes RGBA32F when a program declares gdepth and the pack set no format. Nothing uses it yet: players see no change. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-shaders/src/config.rs | 431 +++++++++++++++++++++++++++ crates/horizon-shaders/src/lib.rs | 3 + 2 files changed, 434 insertions(+) create mode 100644 crates/horizon-shaders/src/config.rs diff --git a/crates/horizon-shaders/src/config.rs b/crates/horizon-shaders/src/config.rs new file mode 100644 index 000000000..0f21e4900 --- /dev/null +++ b/crates/horizon-shaders/src/config.rs @@ -0,0 +1,431 @@ +//! The configuration packs write into their stage files (spec 1.7, 6.1): which buffers a fragment +//! shader writes (`/* DRAWBUFFERS:0257 */`, `/* RENDERTARGETS: 0,2 */`) and, per colortex buffer, +//! its format (`const int colortex2Format = RGBA16F;`, inside a block comment because the format +//! names are not GLSL), whether and to what it is cleared, and whether it gets mipmaps. +//! +//! Everything is read from live code only: the preprocessor has already dropped the false +//! branches, so a format chosen by an option is the one the option selects. Every stage file of +//! the programs that run is read (OQ-17); a later file overrides an earlier one, with a note when +//! the values differ. + +use crate::Preprocessed; + +/// colortex buffers (6.1.1). +pub const BUFFERS: usize = 16; + +/// A colour format of 6.7, as channels, bits per channel and kind. Packed formats are described +/// by their widest channel: `RGB10_A2` is 4 × 10 bits, `R11F_G11F_B10F` 3 × 11 bits float. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Format { + pub channels: u8, + pub bits: u8, + pub kind: Kind, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Kind { + Unorm, + Snorm, + Float, + Int, + Uint, +} + +impl Format { + /// The default `RGBA` (6.1.3). + pub const RGBA8: Format = Format { + channels: 4, + bits: 8, + kind: Kind::Unorm, + }; + + /// A format name of 6.7, in any case. + pub fn parse(name: &str) -> Option { + let name = name.trim().to_ascii_uppercase(); + let packed = |channels, bits, kind| { + Some(Format { + channels, + bits, + kind, + }) + }; + match name.as_str() { + "RGBA" => return Some(Self::RGBA8), + "R3_G3_B2" => return packed(3, 3, Kind::Unorm), + "RGB5_A1" => return packed(4, 5, Kind::Unorm), + "RGB10_A2" => return packed(4, 10, Kind::Unorm), + "RGB10_A2UI" => return packed(4, 10, Kind::Uint), + "R11F_G11F_B10F" => return packed(3, 11, Kind::Float), + "RGB9_E5" => return packed(3, 9, Kind::Float), + "RGBA4" => return packed(4, 4, Kind::Unorm), + "RGB565" => return packed(3, 6, Kind::Unorm), + "RGBA2" => return packed(4, 2, Kind::Unorm), + _ => {} + } + let letters = name.bytes().take_while(u8::is_ascii_alphabetic).count(); + let channels = match &name[..letters] { + "R" => 1, + "RG" => 2, + "RGB" => 3, + "RGBA" => 4, + _ => return None, + }; + let rest = &name[letters..]; + let digits = rest.bytes().take_while(u8::is_ascii_digit).count(); + let bits: u8 = rest[..digits].parse().ok()?; + let kind = match &rest[digits..] { + "" => Kind::Unorm, + "_SNORM" => Kind::Snorm, + "F" => Kind::Float, + "I" => Kind::Int, + "UI" => Kind::Uint, + _ => return None, + }; + let valid = match kind { + Kind::Unorm | Kind::Snorm => matches!(bits, 8 | 16), + Kind::Float => matches!(bits, 16 | 32), + Kind::Int | Kind::Uint => matches!(bits, 8 | 16 | 32), + }; + valid.then_some(Format { + channels, + bits, + kind, + }) + } +} + +/// The settings of one colortex buffer. +#[derive(Clone, Debug, PartialEq)] +pub struct Buffer { + pub format: Format, + /// Cleared every frame (`Clear`, default true). + pub clear: bool, + /// `ClearColor`, else 6.1.5's default: colortex0 the fog colour, colortex1 white, + /// the others transparent black. + pub clear_color: Option<[f32; 4]>, + /// `MipmapEnabled`. + pub mipmap: bool, +} + +impl Default for Buffer { + fn default() -> Self { + Self { + format: Format::RGBA8, + clear: true, + clear_color: None, + mipmap: false, + } + } +} + +#[derive(Clone, Debug, PartialEq)] +pub struct Config { + pub buffers: [Buffer; BUFFERS], + /// Conflicting or unreadable settings, as `file:line: message`. + pub notes: Vec, + format_set: [bool; BUFFERS], + /// Each setting read so far and its value, to report a later file changing it. + seen: Vec<(String, String)>, + gdepth: bool, +} + +impl Default for Config { + fn default() -> Self { + Self { + buffers: std::array::from_fn(|_| Buffer::default()), + notes: Vec::new(), + format_set: [false; BUFFERS], + seen: Vec::new(), + gdepth: false, + } + } +} + +/// The colortex index of a buffer name or alias (6.1.1). +pub fn buffer_index(name: &str) -> Option { + let alias = [ + "gcolor", + "gdepth", + "gnormal", + "composite", + "gaux1", + "gaux2", + "gaux3", + "gaux4", + ]; + if let Some(i) = alias.iter().position(|a| *a == name) { + return Some(i); + } + let n: usize = name.strip_prefix("colortex")?.parse().ok()?; + (n < BUFFERS).then_some(n) +} + +impl Config { + /// Reads one preprocessed stage file. + pub fn read(&mut self, pp: &Preprocessed) { + let line = |at: usize| pp.locate(code_line(&pp.code, at)); + for (at, ty, name, value) in constants(&pp.code) { + let place = line(at); + self.constant(&place, ty, name, value); + } + for (origin, comment) in &pp.comments { + for (_, ty, name, value) in constants(comment) { + let file = pp + .files + .get(origin.file as usize) + .map_or("?", String::as_str); + let place = format!("{file}:{}", origin.line); + self.constant(&place, ty, name, value); + } + } + if declares(&pp.code, "gdepth") { + self.gdepth = true; + } + } + + /// The settings once every file is read: buffer 1 becomes RGBA32F when a program declares + /// `gdepth` and the pack gave it no format (6.1.3). + pub fn finish(mut self) -> Self { + if self.gdepth && !self.format_set[1] { + self.buffers[1].format = Format { + channels: 4, + bits: 32, + kind: Kind::Float, + }; + } + self + } + + fn constant(&mut self, place: &str, ty: &str, name: &str, value: &str) { + let settings = [ + ("Format", "int"), + ("ClearColor", "vec4"), + ("Clear", "bool"), + ("MipmapEnabled", "bool"), + ]; + let Some((buffer, setting)) = settings.iter().find_map(|(suffix, want)| { + let buffer = buffer_index(name.strip_suffix(suffix)?)?; + (ty == *want).then_some((buffer, *suffix)) + }) else { + return; + }; + let b = &mut self.buffers[buffer]; + let ok = match setting { + "Format" => Format::parse(value).map(|f| { + b.format = f; + self.format_set[buffer] = true; + }), + "ClearColor" => vec4(value).map(|c| b.clear_color = Some(c)), + "Clear" => boolean(value).map(|v| b.clear = v), + _ => boolean(value).map(|v| b.mipmap = v), + }; + if ok.is_none() { + let problem = match setting { + "Format" => format!("unknown format `{value}`"), + "ClearColor" => format!("`{name}` is not a vec4: `{value}`"), + _ => format!("`{name}` is not a bool: `{value}`"), + }; + self.notes.push(format!("{place}: {problem}")); + return; + } + // The same setting under another name (an alias) counts as the same. + let key = format!("{buffer}{setting}"); + match self.seen.iter_mut().find(|(k, _)| *k == key) { + Some((_, old)) if old != value => { + self.notes.push(format!( + "{place}: `{name}` changed by a later file to `{value}`" + )); + *old = value.to_owned(); + } + Some(_) => {} + None => self.seen.push((key, value.to_owned())), + } + } +} + +/// The buffers a fragment shader writes, from the last `DRAWBUFFERS` or `RENDERTARGETS` comment +/// of its live code, or `None` without one (then it writes buffers 0 to 7). 6.1.4. +pub fn draw_buffers(pp: &Preprocessed) -> Option> { + let mut found = None; + for (_, comment) in &pp.comments { + let text = comment.trim(); + if let Some(digits) = text.strip_prefix("DRAWBUFFERS:") { + let list: Option> = digits.trim().chars().map(|c| c.to_digit(10)).collect(); + found = list.or(found); + } else if let Some(list) = text.strip_prefix("RENDERTARGETS:") { + let list: Option> = list + .split(',') + .map(|n| { + n.trim() + .parse() + .ok() + .filter(|&n: &u32| (n as usize) < BUFFERS) + }) + .collect(); + found = list.or(found); + } + } + found.map(|mut list| { + list.truncate(8); + list + }) +} + +/// `const = ;` anywhere in `text`: (byte offset, type, name, value). +fn constants(text: &str) -> Vec<(usize, &str, &str, &str)> { + let mut out = Vec::new(); + let mut from = 0; + while let Some(found) = text[from..].find("const") { + let at = from + found; + from = at + 5; + let before = text[..at].chars().next_back(); + if before.is_some_and(|c| c.is_alphanumeric() || c == '_') { + continue; + } + let rest = &text[from..]; + let Some(end) = rest.find(';') else { break }; + let decl = &rest[..end]; + let Some((left, value)) = decl.split_once('=') else { + continue; + }; + let mut words = left.split_whitespace(); + if let (Some(ty), Some(name), None) = (words.next(), words.next(), words.next()) { + out.push((at, ty, name, value.trim())); + } + } + out +} + +/// Whether `text` declares a uniform sampler called `name`. +fn declares(text: &str, name: &str) -> bool { + text.split(';').any(|decl| { + let words: Vec<&str> = decl.split_whitespace().collect(); + words.first() == Some(&"uniform") && words.last() == Some(&name) + }) +} + +fn code_line(code: &str, at: usize) -> usize { + code[..at].matches('\n').count() +} + +fn boolean(value: &str) -> Option { + match value { + "true" => Some(true), + "false" => Some(false), + _ => None, + } +} + +fn number(text: &str) -> Option { + let text = text.trim(); + text.strip_suffix(['f', 'F']).unwrap_or(text).parse().ok() +} + +/// `vec4(r, g, b, a)` or `vec4(x)`. +fn vec4(value: &str) -> Option<[f32; 4]> { + let inner = value + .strip_prefix("vec4")? + .trim() + .strip_prefix('(')? + .strip_suffix(')')?; + let parts: Option> = inner.split(',').map(number).collect(); + match parts?.as_slice() { + [x] => Some([*x; 4]), + [r, g, b, a] => Some([*r, *g, *b, *a]), + _ => None, + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::pack::tests::zip; + use crate::{Macros, Pack, preprocess}; + + fn stage(text: &str) -> Preprocessed { + let pack = Pack::from_zip_bytes("t", zip(&[("shaders/composite.fsh", text)])).unwrap(); + preprocess(&pack, "composite.fsh", &Macros::standard()) + } + + #[test] + fn formats_parse() { + let f = |channels, bits, kind| { + Some(Format { + channels, + bits, + kind, + }) + }; + assert_eq!(Format::parse("rgba16f"), f(4, 16, Kind::Float)); + assert_eq!(Format::parse("R32UI"), f(1, 32, Kind::Uint)); + assert_eq!(Format::parse("RG8_SNORM"), f(2, 8, Kind::Snorm)); + assert_eq!(Format::parse("R11F_G11F_B10F"), f(3, 11, Kind::Float)); + assert_eq!(Format::parse("RGB16"), f(3, 16, Kind::Unorm)); + assert_eq!(Format::parse("RGBA12"), None); + assert_eq!(Format::parse("R16_SNORMX"), None); + } + + #[test] + fn draw_buffers_and_constants_come_from_live_code() { + let pp = stage( + "#version 120\n\ + #define HQ\n\ + /*\n\ + const int colortex2Format = RGBA16F;\n\ + const int gaux4Format = R32F;\n\ + */\n\ + #ifdef HQ\n\ + /* RENDERTARGETS: 0,2,11 */\n\ + #else\n\ + /* DRAWBUFFERS:01 */\n\ + const bool colortex5Clear = true;\n\ + #endif\n\ + const bool colortex5Clear = false;\n\ + const vec4 colortex3ClearColor = vec4(0.5, 0.25, 0.0, 1.0f);\n\ + const bool colortex3MipmapEnabled = true;\n\ + const vec4 gnormalClearColor = vec4(1.0);\n\ + uniform sampler2D gdepth;\n\ + void main() {}\n", + ); + assert_eq!(draw_buffers(&pp), Some(vec![0, 2, 11])); + let mut config = Config::default(); + config.read(&pp); + let config = config.finish(); + let b = &config.buffers; + assert_eq!(b[2].format, Format::parse("RGBA16F").unwrap()); + assert_eq!(b[7].format, Format::parse("R32F").unwrap()); + assert!(!b[5].clear && b[0].clear); + assert_eq!(b[3].clear_color, Some([0.5, 0.25, 0.0, 1.0])); + assert!(b[3].mipmap && !b[2].mipmap); + assert_eq!(b[2].clear_color, Some([1.0; 4])); + assert_eq!(b[1].format, Format::parse("RGBA32F").unwrap(), "gdepth"); + assert!(config.notes.is_empty(), "{:?}", config.notes); + } + + #[test] + fn no_directive_and_conflicts() { + let pp = stage("void main() {}\n"); + assert_eq!(draw_buffers(&pp), None); + let a = + stage("/* const int colortex1Format = RGB8; */\nconst bool colortex1Clear = maybe;\n"); + let b = + stage("/* const int colortex1Format = RGBA16; const int colortex2Format = XYZ; */\n"); + let mut config = Config::default(); + config.read(&a); + config.read(&b); + let notes = config.notes.join("\n"); + assert!( + notes.contains("composite.fsh:2: `colortex1Clear` is not a bool"), + "{notes}" + ); + assert!( + notes.contains("composite.fsh:1: `colortex1Format` changed"), + "{notes}" + ); + assert!(notes.contains("unknown format `XYZ`"), "{notes}"); + assert_eq!( + config.finish().buffers[1].format, + Format::parse("RGBA16").unwrap() + ); + } +} diff --git a/crates/horizon-shaders/src/lib.rs b/crates/horizon-shaders/src/lib.rs index bae3c0dcd..465658fdf 100644 --- a/crates/horizon-shaders/src/lib.rs +++ b/crates/horizon-shaders/src/lib.rs @@ -8,9 +8,11 @@ //! - [`Lang`]: the pack's translated names (8.4). //! - [`preprocess`]: a stage file through `#include` and the GLSL preprocessor, lines mapped back (1.4, 1.5). //! - [`translate_composite`]: a composite-style program into naga modules and its interface (1.6, 2.2). +//! - [`Config`] and [`draw_buffers`]: the buffer settings and output lists stage files carry (1.7, 6.1). //! - [`Macros`] and [`eval_condition`]: the macros packs test, and the `#if` arithmetic (1.5). mod condition; +mod config; mod discover; mod lang; mod macros; @@ -20,6 +22,7 @@ mod properties; mod translate; pub use condition::eval_condition; +pub use config::{BUFFERS, Buffer, Config, Format, Kind, buffer_index, draw_buffers}; pub use discover::{Found, discover}; pub use lang::Lang; pub use macros::{MC_VERSION, Macros, RENDER_STAGES}; From 6b2ec5f8307eb80eb1158eff35916973ee84aaf9 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 24 Oct 2026 22:23:43 +0200 Subject: [PATCH 018/107] Shader packs, clean room 8: a dimension's post-processing programs What the renderer needs to run a pack's post-processing, still without wgpu: the programs of one dimension (prepare, deferred, composite and final with their numbered variants, in the order they run), each preprocessed and translated, with the buffers it writes and the flips that follow it, and the buffer settings its stage files carry. - A dimension's folder (world0, world-1, world1) replaces the base folder when it exists. program..enabled expressions over switch options, flip.. keys and the virtual deferred_pre and composite_pre. Programs with a geometry stage, or that fail to preprocess or translate, are reported and skipped. - tests/packs/post-test: Horizon's own small test pack (tint, depth in colortex2, depth bands on half the screen, vignette). Black box over the public test packs: every dimension of every pack loads its post-processing programs (2 to 9) with no note. Nothing uses it yet: players see no change. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-shaders/src/lib.rs | 3 + crates/horizon-shaders/src/post.rs | 295 ++++++++++++++++++ .../tests/packs/post-test/README.md | 6 + .../packs/post-test/shaders/composite.fsh | 23 ++ .../packs/post-test/shaders/composite.vsh | 8 + .../packs/post-test/shaders/composite1.fsh | 25 ++ .../packs/post-test/shaders/composite1.vsh | 8 + .../tests/packs/post-test/shaders/final.fsh | 12 + .../tests/packs/post-test/shaders/final.vsh | 8 + .../post-test/shaders/shaders.properties | 2 + crates/horizon-shaders/tests/real_packs.rs | 18 +- 11 files changed, 407 insertions(+), 1 deletion(-) create mode 100644 crates/horizon-shaders/src/post.rs create mode 100644 crates/horizon-shaders/tests/packs/post-test/README.md create mode 100644 crates/horizon-shaders/tests/packs/post-test/shaders/composite.fsh create mode 100644 crates/horizon-shaders/tests/packs/post-test/shaders/composite.vsh create mode 100644 crates/horizon-shaders/tests/packs/post-test/shaders/composite1.fsh create mode 100644 crates/horizon-shaders/tests/packs/post-test/shaders/composite1.vsh create mode 100644 crates/horizon-shaders/tests/packs/post-test/shaders/final.fsh create mode 100644 crates/horizon-shaders/tests/packs/post-test/shaders/final.vsh create mode 100644 crates/horizon-shaders/tests/packs/post-test/shaders/shaders.properties diff --git a/crates/horizon-shaders/src/lib.rs b/crates/horizon-shaders/src/lib.rs index 465658fdf..fa39de067 100644 --- a/crates/horizon-shaders/src/lib.rs +++ b/crates/horizon-shaders/src/lib.rs @@ -9,6 +9,7 @@ //! - [`preprocess`]: a stage file through `#include` and the GLSL preprocessor, lines mapped back (1.4, 1.5). //! - [`translate_composite`]: a composite-style program into naga modules and its interface (1.6, 2.2). //! - [`Config`] and [`draw_buffers`]: the buffer settings and output lists stage files carry (1.7, 6.1). +//! - [`load_post`]: a dimension's `prepare`, `deferred`, `composite` and `final` programs, in order (2.1). //! - [`Macros`] and [`eval_condition`]: the macros packs test, and the `#if` arithmetic (1.5). mod condition; @@ -17,6 +18,7 @@ mod discover; mod lang; mod macros; mod pack; +mod post; mod preprocess; mod properties; mod translate; @@ -27,6 +29,7 @@ pub use discover::{Found, discover}; pub use lang::Lang; pub use macros::{MC_VERSION, Macros, RENDER_STAGES}; pub use pack::{Pack, PackError}; +pub use post::{Group, Post, PostProgram, dimension_folder, load_post}; pub use preprocess::{Diagnostic, Origin, Preprocessed, preprocess}; pub use properties::{Entry, Note, Properties}; pub use translate::{Error, Interface, Program, Sampler, Target, Uniform, translate_composite}; diff --git a/crates/horizon-shaders/src/post.rs b/crates/horizon-shaders/src/post.rs new file mode 100644 index 000000000..f0427303e --- /dev/null +++ b/crates/horizon-shaders/src/post.rs @@ -0,0 +1,295 @@ +//! The post-processing programs of one dimension, ready for the renderer: `prepare`, `deferred`, +//! `composite` and `final` with their numbered variants, in the order they run (spec 2.1), each +//! preprocessed and translated, with the buffers it writes and the flips that follow it, and the +//! buffer settings their files carry (6.1). +//! +//! Decisions (impl-plan.md): +//! - OQ-12: a dimension's folder (`world0`, `world-1`, `world1`) replaces the base folder for that +//! dimension when it exists; every stage file is read from it. +//! - OQ-29: a program with a geometry stage is reported and treated as missing. +//! - A program that fails to translate is reported and treated as missing (2.5). + +use crate::config::{self, Config}; +use crate::{Macros, Pack, Preprocessed, Program, Properties, Target, eval_condition}; +use crate::{preprocess, translate_composite}; + +/// Which group of 2.1 a program belongs to. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Group { + Prepare, + Deferred, + Composite, + Final, +} + +const GROUPS: [(Group, &str); 4] = [ + (Group::Prepare, "prepare"), + (Group::Deferred, "deferred"), + (Group::Composite, "composite"), + (Group::Final, "final"), +]; + +pub struct PostProgram { + /// The program's name (`composite3`). + pub name: String, + pub group: Group, + pub program: Program, + /// colortex buffer of each fragment output location (location n writes `buffers[n]`). + pub buffers: Vec, + /// `flip..`: buffers forced to flip (true) or kept (false) after this program. + pub flips: Vec<(u32, bool)>, +} + +pub struct Post { + /// The folder the programs came from: `""` for `shaders` itself, else `world0`... + pub folder: String, + pub programs: Vec, + /// The flips of the virtual `deferred_pre` and `composite_pre` (6.1.6). + pub deferred_pre: Vec<(u32, bool)>, + pub composite_pre: Vec<(u32, bool)>, + pub config: Config, + /// Programs skipped and why, as `program: message` or `file:line: message`. + pub notes: Vec, +} + +/// The folder a dimension reads its programs from: `world` when the pack has it, else the +/// base folder `""` (1.2). `id` is the legacy number: 0 the Overworld, -1 the Nether, 1 the End. +pub fn dimension_folder(pack: &Pack, id: i32) -> String { + let folder = format!("world{id}"); + if pack.list("").iter().any(|f| f == &folder) || !pack.list(&folder).is_empty() { + folder + } else { + String::new() + } +} + +/// Loads the post-processing programs of `folder`. +pub fn load_post(pack: &Pack, folder: &str, properties: &Properties, macros: &Macros) -> Post { + let mut post = Post { + folder: folder.to_owned(), + programs: Vec::new(), + deferred_pre: flips(properties, "deferred_pre"), + composite_pre: flips(properties, "composite_pre"), + config: Config::default(), + notes: Vec::new(), + }; + let path = |name: &str, ext: &str| match folder { + "" => format!("{name}.{ext}"), + f => format!("{f}/{name}.{ext}"), + }; + for (group, base) in GROUPS { + let names: Vec = if group == Group::Final { + vec![base.to_owned()] + } else { + std::iter::once(base.to_owned()) + .chain((1..=99).map(|n| format!("{base}{n}"))) + .collect() + }; + for name in names { + let (vsh, fsh) = (path(&name, "vsh"), path(&name, "fsh")); + if !pack.exists(&fsh) || !pack.exists(&vsh) { + continue; + } + if !enabled(properties, folder, &name, macros) { + continue; + } + if pack.exists(&path(&name, "gsh")) { + post.notes.push(format!( + "{name}: uses a geometry stage (not supported); skipped" + )); + continue; + } + let vertex = preprocess(pack, &vsh, macros); + let fragment = preprocess(pack, &fsh, macros); + let problems: Vec = [&vertex, &fragment] + .iter() + .flat_map(|pp| diagnostics(pp)) + .collect(); + if !problems.is_empty() { + post.notes.extend(problems); + post.notes.push(format!("{name}: skipped")); + continue; + } + let target = match group { + Group::Final => Target::Window, + _ => Target::Buffers, + }; + let program = match translate_composite(&vertex, &fragment, target) { + Ok(p) => p, + Err(errors) => { + post.notes.extend( + errors + .iter() + .map(|e| format!("{}:{}: {}", e.file, e.line, e.message)), + ); + post.notes.push(format!("{name}: skipped")); + continue; + } + }; + post.config.read(&vertex); + post.config.read(&fragment); + let buffers = config::draw_buffers(&fragment).unwrap_or_else(|| (0..8).collect()); + post.programs.push(PostProgram { + flips: flips(properties, &name), + name, + group, + program, + buffers, + }); + } + } + post.config = std::mem::take(&mut post.config).finish(); + post +} + +fn diagnostics(pp: &Preprocessed) -> Vec { + pp.diagnostics + .iter() + .map(|d| { + let file = pp + .files + .get(d.origin.file as usize) + .map_or("?", String::as_str); + format!("{file}:{}: {}", d.origin.line, d.message) + }) + .collect() +} + +/// `program..enabled` (or `program./.enabled`): a boolean expression over +/// switch options (3.3). An option is on when its macro is defined. +fn enabled(properties: &Properties, folder: &str, name: &str, macros: &Macros) -> bool { + let keys = [ + format!("program.{folder}/{name}.enabled"), + format!("program.{name}.enabled"), + ]; + let Some(expr) = keys.iter().find_map(|k| properties.get(k)) else { + return true; + }; + // `true`/`false` as numbers, every other name as `defined(name)`, then the `#if` arithmetic. + let mut condition = String::new(); + for token in crate::macros::lex(expr) { + match token { + "true" => condition.push('1'), + "false" => condition.push('0'), + t if t.starts_with(|c: char| c.is_alphabetic() || c == '_') => { + condition.push_str(&format!("defined({t})")); + } + t => condition.push_str(t), + } + } + eval_condition(&condition, macros).ok() != Some(0) +} + +/// `flip..=` keys of one program. +fn flips(properties: &Properties, program: &str) -> Vec<(u32, bool)> { + let prefix = format!("flip.{program}."); + properties + .entries + .iter() + .filter_map(|e| { + let buffer = config::buffer_index(e.key.strip_prefix(&prefix)?)?; + let value = match e.value.trim() { + "true" => true, + "false" => false, + _ => return None, + }; + Some((buffer as u32, value)) + }) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::pack::tests::zip; + + const VSH: &str = "#version 120\nvarying vec2 uv;\nvoid main() {\n\ + gl_Position = ftransform();\nuv = gl_MultiTexCoord0.xy;\n}\n"; + const FSH: &str = "#version 120\nuniform sampler2D colortex0;\nvarying vec2 uv;\n\ + void main() {\n/* DRAWBUFFERS:3 */\n\ + gl_FragData[0] = texture2D(colortex0, uv);\n}\n"; + + #[test] + fn programs_load_in_order_with_their_settings() { + let pack = Pack::from_zip_bytes( + "t", + zip(&[ + ("shaders/composite.vsh", VSH), + ("shaders/composite.fsh", FSH), + ("shaders/composite2.vsh", VSH), + ("shaders/composite2.fsh", FSH), + ("shaders/deferred.vsh", VSH), + ( + "shaders/deferred.fsh", + "#version 120\nvoid main() { gl_FragColor = vec4(1.0); }\n", + ), + ("shaders/composite1.fsh", FSH), + ("shaders/composite3.vsh", VSH), + ("shaders/composite3.fsh", FSH), + ("shaders/composite4.vsh", VSH), + ("shaders/composite4.fsh", FSH), + ("shaders/composite4.gsh", "x"), + ("shaders/composite5.vsh", VSH), + ( + "shaders/composite5.fsh", + "#version 120\nvoid main() { undefined(); }\n", + ), + ("shaders/final.vsh", VSH), + ("shaders/final.fsh", FSH), + ("shaders/world-1/final.vsh", VSH), + ("shaders/world-1/final.fsh", FSH), + ]), + ) + .unwrap(); + let properties = Properties::parse( + "program.composite3.enabled = HQ && !LOW\nflip.composite2.gaux1=true\n\ + flip.composite.colortex3 = false\nflip.composite_pre.colortex1=true\n", + &Macros::default(), + ); + assert_eq!(dimension_folder(&pack, 0), ""); + assert_eq!(dimension_folder(&pack, -1), "world-1"); + let post = load_post(&pack, "", &properties, &Macros::standard()); + let names: Vec<&str> = post.programs.iter().map(|p| p.name.as_str()).collect(); + assert_eq!(names, ["deferred", "composite", "composite2", "final"]); + let p = &post.programs; + assert_eq!(p[0].buffers, (0..8).collect::>()); + assert_eq!(p[1].buffers, [3]); + assert_eq!(p[1].flips, [(3, false)]); + assert_eq!(p[2].flips, [(4, true)]); + assert_eq!(post.composite_pre, [(1, true)]); + assert_eq!(p[3].group, Group::Final); + let notes = post.notes.join("\n"); + assert!( + notes.contains("composite4: uses a geometry stage"), + "{notes}" + ); + assert!(notes.contains("composite5.fsh:2:"), "{notes}"); + assert!(notes.contains("composite5: skipped"), "{notes}"); + + let mut macros = Macros::standard(); + macros.define("HQ", ""); + let post = load_post(&pack, "world-1", &properties, ¯os); + let names: Vec<&str> = post.programs.iter().map(|p| p.name.as_str()).collect(); + assert_eq!(names, ["final"]); + let post = load_post(&pack, "", &properties, ¯os); + assert!(post.programs.iter().any(|p| p.name == "composite3")); + } + + #[test] + fn horizons_test_pack_loads() { + let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/packs/post-test"); + let pack = Pack::open(&dir).unwrap(); + let macros = Macros::standard(); + let properties = Properties::parse(&pack.read_text("shaders.properties").unwrap(), ¯os); + let post = load_post(&pack, &dimension_folder(&pack, 0), &properties, ¯os); + assert!(post.notes.is_empty(), "{:?}", post.notes); + let names: Vec<&str> = post.programs.iter().map(|p| p.name.as_str()).collect(); + assert_eq!(names, ["composite", "composite1", "final"]); + assert_eq!(post.programs[0].buffers, [0, 2]); + assert_eq!(post.programs[1].buffers, [0]); + assert_eq!( + post.config.buffers[2].format, + crate::Format::parse("RGBA16F").unwrap() + ); + } +} diff --git a/crates/horizon-shaders/tests/packs/post-test/README.md b/crates/horizon-shaders/tests/packs/post-test/README.md new file mode 100644 index 000000000..c3d124eec --- /dev/null +++ b/crates/horizon-shaders/tests/packs/post-test/README.md @@ -0,0 +1,6 @@ +# post-test + +Horizon's own shader pack for testing post-processing (written for Horizon, no other pack's code). +`composite` tints the world warm and stores linear depth in colortex2, `composite1` shows that +depth as fog bands on the right half with a little noise, and `final` adds a vignette. Copy the +folder into `shaderpacks/` and run the game with `HORIZON_SHADER_PACK=post-test`. diff --git a/crates/horizon-shaders/tests/packs/post-test/shaders/composite.fsh b/crates/horizon-shaders/tests/packs/post-test/shaders/composite.fsh new file mode 100644 index 000000000..9261e9fae --- /dev/null +++ b/crates/horizon-shaders/tests/packs/post-test/shaders/composite.fsh @@ -0,0 +1,23 @@ +#version 120 + +/* +const int colortex2Format = RGBA16F; +*/ + +uniform sampler2D colortex0; +uniform sampler2D depthtex0; +uniform mat4 gbufferProjectionInverse; +uniform float far; + +varying vec2 texcoord; + +void main() { + vec3 color = texture2D(colortex0, texcoord).rgb; + float depth = texture2D(depthtex0, texcoord).r; + // Window depth back to a view-space distance. + vec4 view = gbufferProjectionInverse * vec4(texcoord * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0); + float distance = -view.z / view.w; + /* DRAWBUFFERS:02 */ + gl_FragData[0] = vec4(color * vec3(1.08, 1.0, 0.86), 1.0); + gl_FragData[1] = vec4(distance / far, depth, 0.0, 1.0); +} diff --git a/crates/horizon-shaders/tests/packs/post-test/shaders/composite.vsh b/crates/horizon-shaders/tests/packs/post-test/shaders/composite.vsh new file mode 100644 index 000000000..6dbee8d1f --- /dev/null +++ b/crates/horizon-shaders/tests/packs/post-test/shaders/composite.vsh @@ -0,0 +1,8 @@ +#version 120 + +varying vec2 texcoord; + +void main() { + gl_Position = ftransform(); + texcoord = (gl_TextureMatrix[0] * gl_MultiTexCoord0).xy; +} diff --git a/crates/horizon-shaders/tests/packs/post-test/shaders/composite1.fsh b/crates/horizon-shaders/tests/packs/post-test/shaders/composite1.fsh new file mode 100644 index 000000000..5207ad758 --- /dev/null +++ b/crates/horizon-shaders/tests/packs/post-test/shaders/composite1.fsh @@ -0,0 +1,25 @@ +#version 330 compatibility + +uniform sampler2D colortex0; +uniform sampler2D colortex2; +uniform sampler2D noisetex; +uniform float viewWidth; +uniform float viewHeight; + +in vec2 texcoord; + +/* RENDERTARGETS: 0 */ +layout(location = 0) out vec4 outColor; + +void main() { + vec3 color = texture(colortex0, texcoord).rgb; + if (gl_FragCoord.x > viewWidth * 0.5) { + float d = clamp(texture(colortex2, texcoord).r, 0.0, 1.0); + float band = step(0.5, fract(d * 8.0)); + vec3 fog = mix(vec3(0.2, 0.45, 0.9), vec3(0.95, 0.75, 0.3), d); + color = mix(color, fog, 0.35 + 0.15 * band); + float grain = texture(noisetex, gl_FragCoord.xy / 256.0).r - 0.5; + color += grain * 0.04; + } + outColor = vec4(color, 1.0); +} diff --git a/crates/horizon-shaders/tests/packs/post-test/shaders/composite1.vsh b/crates/horizon-shaders/tests/packs/post-test/shaders/composite1.vsh new file mode 100644 index 000000000..12ef6da1f --- /dev/null +++ b/crates/horizon-shaders/tests/packs/post-test/shaders/composite1.vsh @@ -0,0 +1,8 @@ +#version 330 compatibility + +out vec2 texcoord; + +void main() { + gl_Position = ftransform(); + texcoord = gl_MultiTexCoord0.xy; +} diff --git a/crates/horizon-shaders/tests/packs/post-test/shaders/final.fsh b/crates/horizon-shaders/tests/packs/post-test/shaders/final.fsh new file mode 100644 index 000000000..5efbf50df --- /dev/null +++ b/crates/horizon-shaders/tests/packs/post-test/shaders/final.fsh @@ -0,0 +1,12 @@ +#version 120 + +uniform sampler2D colortex0; + +varying vec2 texcoord; + +void main() { + vec3 color = texture2D(colortex0, texcoord).rgb; + vec2 centred = texcoord - 0.5; + float vignette = 1.0 - dot(centred, centred) * 0.9; + gl_FragColor = vec4(color * vignette, 1.0); +} diff --git a/crates/horizon-shaders/tests/packs/post-test/shaders/final.vsh b/crates/horizon-shaders/tests/packs/post-test/shaders/final.vsh new file mode 100644 index 000000000..6dbee8d1f --- /dev/null +++ b/crates/horizon-shaders/tests/packs/post-test/shaders/final.vsh @@ -0,0 +1,8 @@ +#version 120 + +varying vec2 texcoord; + +void main() { + gl_Position = ftransform(); + texcoord = (gl_TextureMatrix[0] * gl_MultiTexCoord0).xy; +} diff --git a/crates/horizon-shaders/tests/packs/post-test/shaders/shaders.properties b/crates/horizon-shaders/tests/packs/post-test/shaders/shaders.properties new file mode 100644 index 000000000..510ada3a9 --- /dev/null +++ b/crates/horizon-shaders/tests/packs/post-test/shaders/shaders.properties @@ -0,0 +1,2 @@ +# Horizon's post-processing test pack. +program.composite1.enabled = !NO_SPLIT diff --git a/crates/horizon-shaders/tests/real_packs.rs b/crates/horizon-shaders/tests/real_packs.rs index 4fac4b0d7..6885b40f7 100644 --- a/crates/horizon-shaders/tests/real_packs.rs +++ b/crates/horizon-shaders/tests/real_packs.rs @@ -8,7 +8,8 @@ use std::time::Instant; use std::collections::BTreeMap; use horizon_shaders::{ - Lang, Macros, Pack, Properties, Target, discover, preprocess, translate_composite, + Lang, Macros, Pack, Properties, Target, dimension_folder, discover, load_post, preprocess, + translate_composite, }; #[test] @@ -157,6 +158,21 @@ fn real_packs_read() { for (cause, n) in causes.iter().take(6) { println!(" {n} x {cause}"); } + // The post-processing set of each dimension, as the game loads it. + for id in [0, -1, 1] { + let start = std::time::Instant::now(); + let folder = dimension_folder(&pack, id); + let post = load_post(&pack, &folder, &shaders, &Macros::standard()); + println!( + " dimension {id} (folder `{folder}`): {} post programs, {} notes, {:.0} ms", + post.programs.len(), + post.notes.len() + post.config.notes.len(), + start.elapsed().as_secs_f64() * 1000.0 + ); + for n in post.notes.iter().chain(&post.config.notes).take(5) { + println!(" {n}"); + } + } assert!(!shaders.entries.is_empty(), "{}: no keys read", f.name); read += 1; } From 3f1a7b28af0ba3fac58ad43194d06912ae969cd0 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 24 Oct 2026 22:39:13 +0200 Subject: [PATCH 019/107] Placement prediction: one helper for predicted blocks, the sound where the block went - predict_use and predict_place set their blocks through one predict_blocks (retainKnownServerState), instead of two copies of the loop. - remote_can_place returns where BlockItem.place put the block, and the placer hears the place sound there: before, set_block only played it at the clicked face's neighbour, so a block that replaced grass or snow where the player clicked made no sound for its placer now that the server leaves the placer out. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/remote.rs | 83 +++++++++++------------- 1 file changed, 39 insertions(+), 44 deletions(-) diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index e54b33b02..11eb3baed 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -3932,18 +3932,7 @@ impl Game<'_> { { return; } - for &(p, state) in changes { - let pos = [p.x, p.y, p.z]; - let before = self.world.block(p.x, p.y, p.z); - // BlockStatePredictionHandler.retainKnownServerState. - if let Some(r) = self.remote.as_mut() { - r.predictions - .entry(pos) - .and_modify(|e| e.0 = seq) - .or_insert((seq, before.0 as i32)); - } - self.set_block(pos, state.0 as i32); - } + self.predict_blocks(changes, seq); // `level.playSound(player, ...)`: the clicker hears it from its own use; the // server sends it to the others only. for (pos, sound, volume, pitch) in sounds { @@ -4004,20 +3993,27 @@ impl Game<'_> { } /// MultiPlayerGameMode.useItemOn inside startPrediction, for a block item: - /// the blocks `BlockItem.place` set are placed on this client's level at - /// once, each kept as predicted under the use's sequence until the server - /// acknowledges it (a placement the server refused goes back). Without - /// this, the next click of a fast bridge aims past the block still on its - /// way from the server, and the server refuses it. - fn predict_place( - &mut self, - changes: &[(horizon_core::position::BlockPos, BlockStateId)], - seq: i32, - ) { + /// the blocks `BlockItem.place` set at `pos` are placed on this client's + /// level at once, so the next click of a fast bridge aims at them rather + /// than past them (the server would refuse it). Its place sound is the + /// placer's own (`playSound(player, ...)`: the server sends it to the + /// others only). + fn predict_place(&mut self, (pos, changes): &DryPlace, seq: i32) { + let ticks = self.ticks; + if let Some(r) = self.remote.as_mut() { + // set_block plays the place sound of the block at `placing`. + r.placing = Some(([pos.x, pos.y, pos.z], ticks)); + } + self.predict_blocks(changes, seq); + } + /// The blocks a use changed, set on this client's level and kept as + /// predicted under the use's sequence until the server acknowledges it + /// (`BlockStatePredictionHandler.retainKnownServerState`): a use the + /// server refused goes back. + fn predict_blocks(&mut self, changes: &DryChanges, seq: i32) { for &(p, state) in changes { let pos = [p.x, p.y, p.z]; let before = self.world.block(p.x, p.y, p.z); - // BlockStatePredictionHandler.retainKnownServerState. if let Some(r) = self.remote.as_mut() { r.predictions .entry(pos) @@ -4028,14 +4024,11 @@ impl Game<'_> { } } - /// Run BlockItem.place on a disposable level: the blocks it set, if it - /// placed. The real level supplies collision shapes and building-blocking - /// entities; random draws, scheduled ticks and events stay in the preview. - fn remote_can_place( - &mut self, - hit: &super::Hit, - stack: &ItemStack, - ) -> Option> { + /// Run BlockItem.place on a disposable level: where it placed and the + /// blocks it set. The real level supplies collision shapes and + /// building-blocking entities; random draws, scheduled ticks and events + /// stay in the preview. + fn remote_can_place(&mut self, hit: &super::Hit, stack: &ItemStack) -> Option { if !self.may_build() { return None; } @@ -4052,15 +4045,16 @@ impl Game<'_> { }; self.with_level(|b, level, _| { let mut level = DryLevel::new(level); - matches!( - b.place_block( - &mut level, - &mut horizon_world::level::Ticks::default(), - &ctx - ), - horizon_world::level::PlaceOutcome::Placed { .. } - ) - .then(|| level.changed.into_iter().collect()) + match b.place_block( + &mut level, + &mut horizon_world::level::Ticks::default(), + &ctx, + ) { + horizon_world::level::PlaceOutcome::Placed { pos, .. } => { + Some((pos, level.changed.into_iter().collect())) + } + horizon_world::level::PlaceOutcome::Failed => None, + } }) } @@ -5109,11 +5103,12 @@ type DrySound = ( f32, f32, ); +/// The blocks a use on the dry level set. +type DryChanges = Vec<(horizon_core::position::BlockPos, BlockStateId)>; /// What a use on the dry level changed, and the sounds it played. -type DryUse = ( - Vec<(horizon_core::position::BlockPos, BlockStateId)>, - Vec, -); +type DryUse = (DryChanges, Vec); +/// Where `BlockItem.place` on the dry level placed, and the blocks it set. +type DryPlace = (horizon_core::position::BlockPos, DryChanges); /// The blocks a guest's hand flips that its use predicts (and hears at once): a wooden or /// copper door, trapdoor or fence gate, a lever or a button. An iron door or trapdoor has From 8c611e677b6e84f27116cd0c486f2f64703e1568 Mon Sep 17 00:00:00 2001 From: some-du6e <103129948+some-du6e@users.noreply.github.com> Date: Sat, 24 Oct 2026 14:44:17 -0600 Subject: [PATCH 020/107] Advancement titles and descriptions show a whole text component A server's custom advancements often name themselves with an empty text and the words in its siblings ({"text":"","extra":[...]}); only the top-level text or translate key was read, so their tooltips were blank. Read them as the component they are (ComponentSerialization), with siblings, styles, translation arguments and fallbacks. Co-Authored-By: Claude Opus 5.5 --- .../horizon-client/src/game/advancements.rs | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/crates/horizon-client/src/game/advancements.rs b/crates/horizon-client/src/game/advancements.rs index b0f948c5d..20d54d290 100644 --- a/crates/horizon-client/src/game/advancements.rs +++ b/crates/horizon-client/src/game/advancements.rs @@ -1051,17 +1051,10 @@ impl Game<'_> { } } } -/// A text component from advancement JSON (`translate` or `text`). +/// An advancement's title or description: a whole component (ComponentSerialization.CODEC), +/// with its siblings and styles, as a plugin's `{"text":"","extra":[...]}` needs. pub(super) fn text_of(v: &Value) -> Text { - if let Some(key) = v["translate"].as_str() { - Text::translate(key, &[]) - } else if let Some(s) = v["text"].as_str() { - Text::literal(s) - } else if let Some(s) = v.as_str() { - Text::literal(s) - } else { - Text::empty() - } + horizon_client::scoreboard::comp_text(v) } #[cfg(test)] @@ -1095,6 +1088,20 @@ mod tests { (seen, None) } + #[test] + fn titles_keep_their_siblings_and_styles() { + let title = text_of(&serde_json::json!({ + "text": "", + "extra": [{"text": "Quest ", "color": "gold"}, "Board"], + })); + assert_eq!(title.plain(), "Quest Board"); + assert_eq!(title.spans[0].style.color, Some(horizon_client::text::GOLD)); + assert_eq!( + text_of(&serde_json::json!({"translate": "advancements.story.root.title"})), + Text::translate("advancements.story.root.title", &[]) + ); + } + #[test] fn toasts_slide_in_squared_and_out_after_five_seconds() { let mut toast = recipes(1); From 4ad05aa94a4a1a7c4c9b4b39717a238af5d6584c Mon Sep 17 00:00:00 2001 From: some-du6e <103129948+some-du6e@users.noreply.github.com> Date: Sat, 24 Oct 2026 14:44:17 -0600 Subject: [PATCH 021/107] Show armour trims on items from a server Decode the trim component as its material and pattern registry ids when both are references (a direct holder stays read past) and name them through the server's synced trim_material and trim_pattern registries, as enchantments are, so worn and held trimmed armour draws its trim in multiplayer. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/remote.rs | 13 ++++++ .../examples/item_wire_probe.rs | 6 +-- crates/horizon-protocol/src/item.rs | 45 ++++++++++++++++--- 3 files changed, 55 insertions(+), 9 deletions(-) diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 0e6127178..8cf3d52a0 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -598,6 +598,19 @@ impl Game<'_> { let value = match c { // Read past, not decoded: the item keeps its prototype's value. Component::Raw(_) => continue, + // Trim materials and patterns are synced registries, as enchantments are. + Component::Trim { material, pattern } => { + let (Some(material), Some(pattern)) = ( + r.registries.name("minecraft:trim_material", *material), + r.registries.name("minecraft:trim_pattern", *pattern), + ) else { + continue; + }; + Tag::compound([ + ("material", Tag::String(material.into())), + ("pattern", Tag::String(pattern.into())), + ]) + } Component::Int(v) if *kind == component::RARITY => { let rarity = ["common", "uncommon", "rare", "epic"]; Tag::String(rarity.get(*v as usize).copied().unwrap_or("common").into()) diff --git a/crates/horizon-protocol/examples/item_wire_probe.rs b/crates/horizon-protocol/examples/item_wire_probe.rs index 19760ca1a..097a9f40d 100644 --- a/crates/horizon-protocol/examples/item_wire_probe.rs +++ b/crates/horizon-protocol/examples/item_wire_probe.rs @@ -2,8 +2,8 @@ //! the oracle's stacks (JSON lines with `label` and `hex`), decodes each with //! `item::Slot` and prints, per stack, whether every byte was consumed, the //! patch's component ids (added, then removed), which of them Horizon -//! decoded into its own types (the rest it reads past, keeping their bytes) -//! and whether encoding the decoded stack gives the same bytes back. +//! decoded into its own lossy types (the rest it reads past keeping their +//! bytes, or decodes into ids it writes back unchanged, like trims) and whether encoding the decoded stack gives the same bytes back. //! //! cargo run -p horizon-protocol --example item_wire_probe -- vanilla.jsonl rust.jsonl use horizon_protocol::{ @@ -45,7 +45,7 @@ fn main() -> Result<(), Box> { let typed: Vec = stack .iter() .flat_map(|s| &s.added) - .filter(|(_, c)| !matches!(c, Component::Raw(_))) + .filter(|(_, c)| !matches!(c, Component::Raw(_) | Component::Trim { .. })) .map(|(k, _)| *k) .collect(); json!({ diff --git a/crates/horizon-protocol/src/item.rs b/crates/horizon-protocol/src/item.rs index 0aa940a38..2872c0228 100644 --- a/crates/horizon-protocol/src/item.rs +++ b/crates/horizon-protocol/src/item.rs @@ -37,6 +37,7 @@ pub mod component { pub const CHARGED_PROJECTILES: i32 = 51; pub const WRITABLE_BOOK_CONTENT: i32 = 56; pub const WRITTEN_BOOK_CONTENT: i32 = 57; + pub const TRIM: i32 = 58; } /// A written book's content (`WrittenBookContent.STREAM_CODEC`): the @@ -80,6 +81,12 @@ pub enum Component { WrittenBook(WrittenBook), /// `ChargedProjectiles`: a loaded crossbow's ammunition, as templates. Stacks(Vec), + /// `ArmorTrim` naming its material and pattern by registry id (`trim_material`, + /// `trim_pattern`); a trim with a direct holder stays [`Component::Raw`]. + Trim { + material: i32, + pattern: i32, + }, /// A component Horizon reads past without interpreting it: its encoded /// bytes, written back as they came. Raw(Vec), @@ -232,13 +239,28 @@ impl Component { other => { let start = r.position(); skip::component(other, r)?; - Self::Raw(r.since(start).to_vec()) + let bytes = r.since(start); + // Two references: each holder is just its id plus one. + let mut ids = Reader::new(bytes); + match (other, ids.varint(), ids.varint()) { + (TRIM, Ok(material @ 1..), Ok(pattern @ 1..)) if ids.remaining() == 0 => { + Self::Trim { + material: material - 1, + pattern: pattern - 1, + } + } + _ => Self::Raw(bytes.to_vec()), + } } }) } fn write(&self, kind: i32, w: &mut Writer) { match self { Self::Raw(bytes) => w.bytes(bytes), + Self::Trim { material, pattern } => { + w.varint(material + 1); + w.varint(pattern + 1); + } Self::Int(v) if kind == component::DYED_COLOR => w.i32(*v), Self::Int(v) => w.varint(*v), Self::Unit => {} @@ -646,7 +668,7 @@ mod tests { #[test] fn components_read_past_keep_their_bytes() { // Vanilla 26.3's encoding (tools/test_item_wire_parity.py) of a chestplate's - // armour trim, a banner's patterns (one direct), a pot's sherds and a stew's + // armour trim (decoded: both holders are references), a banner's patterns (one direct), a pot's sherds and a stew's // effects: one server's inventory, recipe book and advancements had them. for (hex, kind) in [ ("01bd0801003a0502", 58), @@ -661,10 +683,21 @@ mod tests { let mut r = Reader::new(&bytes); let stack = Slot::read(&mut r).unwrap(); assert_eq!(r.remaining(), 0, "component {kind}"); - assert!(matches!( - stack.as_ref().and_then(|s| s.component(kind)), - Some(Component::Raw(_)) - )); + let expected = |c: &Component| match kind { + 58 => { + *c == Component::Trim { + material: 4, + pattern: 1, + } + } + _ => matches!(c, Component::Raw(_)), + }; + assert!( + stack + .as_ref() + .and_then(|s| s.component(kind)) + .is_some_and(expected) + ); let mut w = Writer::new(); Slot::write(&stack, &mut w); assert_eq!(w.data, bytes, "component {kind}"); From be34f4cd5f1db4dc36eb44d92d0fccfdfd9afd54 Mon Sep 17 00:00:00 2001 From: some-du6e <103129948+some-du6e@users.noreply.github.com> Date: Sat, 24 Oct 2026 12:20:35 -0600 Subject: [PATCH 022/107] Connect to a server's IPv4 address first, as vanilla does Vanilla resolves a server with InetAddress.getByName, and Java's default (java.net.preferIPv6Addresses=false) puts IPv4 addresses first. Horizon tried addresses in the system resolver's order, which on macOS puts IPv6 first: a server whose IPv6 address resets the connection showed "Connection reset by peer (os error 54)" where vanilla joined. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-protocol/src/connection.rs | 39 ++++++++++++++++++++--- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/crates/horizon-protocol/src/connection.rs b/crates/horizon-protocol/src/connection.rs index 8f7ae28cc..e7d465288 100644 --- a/crates/horizon-protocol/src/connection.rs +++ b/crates/horizon-protocol/src/connection.rs @@ -6,7 +6,7 @@ use crate::{Error, Result, buf::Reader, crypto::Cfb8, frame}; use std::{ io::{Read, Write}, - net::{Shutdown, TcpStream, ToSocketAddrs}, + net::{Shutdown, SocketAddr, TcpStream, ToSocketAddrs}, sync::{ Arc, Condvar, Mutex, atomic::{AtomicI64, Ordering}, @@ -180,6 +180,13 @@ pub struct ConnWriter { pub threshold: Option, counters: Arc, } +/// InetAddress.getByName with Java's default `java.net.preferIPv6Addresses=false`: IPv4 addresses +/// first, whatever order the system resolver gives (macOS puts IPv6 first). A server whose IPv6 +/// address doesn't answer the game still lets vanilla in over IPv4. +fn ipv4_first(mut addrs: Vec) -> Vec { + addrs.sort_by_key(SocketAddr::is_ipv6); + addrs +} /// Both halves before the split. pub struct Connection { pub reader: ConnReader, @@ -187,10 +194,12 @@ pub struct Connection { } impl Connection { pub fn connect(host: &str, port: u16, timeout: Duration) -> Result { - let addrs: Vec<_> = (host, port) - .to_socket_addrs() - .map_err(|e| Error::new(format!("Unknown host {host}: {e}")))? - .collect(); + let addrs = ipv4_first( + (host, port) + .to_socket_addrs() + .map_err(|e| Error::new(format!("Unknown host {host}: {e}")))? + .collect(), + ); let mut last = Error::new(format!("Unknown host {host}")); for addr in addrs { match TcpStream::connect_timeout(&addr, timeout) { @@ -315,6 +324,26 @@ mod tests { use super::*; use std::net::TcpListener; + #[test] + fn ipv4_addresses_come_first_in_resolver_order() { + let a = |s: &str| s.parse::().unwrap(); + let resolved = vec![ + a("[2001:db8::1]:25565"), + a("192.0.2.1:25565"), + a("[2001:db8::2]:25565"), + a("10.0.0.1:25565"), + ]; + assert_eq!( + ipv4_first(resolved), + [ + a("192.0.2.1:25565"), + a("10.0.0.1:25565"), + a("[2001:db8::1]:25565"), + a("[2001:db8::2]:25565"), + ] + ); + } + #[test] fn loopback_with_compression_and_encryption() { let listener = TcpListener::bind("127.0.0.1:0").unwrap(); From 2716ad41bed4da8f7ac71e7a5bfe482cfc93881e Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 24 Oct 2026 23:16:05 +0200 Subject: [PATCH 023/107] Renderer: Metal's flat draws and occlusion culling switch on by sections in view Both switched on past 4,096 section draws, measured when a section had 2.2 to 2.5 draws. A section's draw count now depends on the sides it shows the eye, so the bars count sections in view: 1,600, where 4,096 draws fell. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 8 +++++--- crates/horizon-client/src/gfx/cull.rs | 22 ++++++++++++---------- crates/horizon-client/src/gfx/flat.rs | 22 ++++++++++++---------- 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index e4c3367f9..a848d36a5 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -2159,7 +2159,7 @@ impl Renderer { cull::Cull::new(&device, config.format.remove_srgb_suffix(), depth_test); cull.always = cull_env.as_deref() == Ok("all"); if !cull.always && adapter.get_info().backend == wgpu::Backend::Metal { - cull.min_draws = cull::TILED_MIN_DRAWS; + cull.min_sections = cull::TILED_MIN_SECTIONS; } cull }); @@ -3131,7 +3131,7 @@ impl Renderer { .cull .as_mut() .filter(|c| { - self.cull_on && !frame.title && self.multi_draw && self.section_draws >= c.min_draws + self.cull_on && !frame.title && self.multi_draw && visible.len() >= c.min_sections }) .and_then(|cull| cull.begin(&self.device).then_some(cull)); self.phase2_draws.clear(); @@ -3533,7 +3533,9 @@ impl Renderer { } let flat_frame = match &mut self.flat { Some(flat) - if self.flat_on && !self.draws.is_empty() && self.draws.len() >= flat.min_draws => + if self.flat_on + && !self.draws.is_empty() + && self.drawn_sections >= flat.min_sections => { let sections = &self.sections; let lods = &self.lods; diff --git a/crates/horizon-client/src/gfx/cull.rs b/crates/horizon-client/src/gfx/cull.rs index 856bd86dd..449401b96 100644 --- a/crates/horizon-client/src/gfx/cull.rs +++ b/crates/horizon-client/src/gfx/cull.rs @@ -46,13 +46,15 @@ const RETEST: u32 = 8; /// more than they save. Else one frame in `PROBE` tests every section. const MIN_SHARE: f32 = 0.1; const PROBE: u32 = 64; -/// On Metal, section draws from which a frame culls. Apple's GPUs store and -/// reload the whole frame at each of the passes the test adds, a fixed cost -/// that the sections it hides repay only at high render distances: on an M4 -/// Pro at 1080p and render distance 12 (1,000–1,600 draws) culling added -/// about 0.05 ms of GPU time while hiding 15–30% of the solid indices; at -/// 32 (drawn flat, over this many draws) it saved 0.25–1.6 ms. -pub const TILED_MIN_DRAWS: usize = 4096; +/// On Metal, sections in view from which a frame culls. Apple's GPUs store +/// and reload the whole frame at each of the passes the test adds, a fixed +/// cost that the sections it hides repay only at high render distances: on +/// an M4 Pro at 1080p and render distance 12 (1,000–1,600 draws) culling +/// added about 0.05 ms of GPU time while hiding 15–30% of the solid +/// indices; at 32 (over 4,096 draws) it saved 0.25–1.6 ms. Those draws were +/// 2.2 to 2.5 per section; a section's draw count now depends on the sides +/// it shows the eye (`mesher::Sides`), so the bar counts sections. +pub const TILED_MIN_SECTIONS: usize = 1600; /// Boxes over more than this share of the screen count as seen untested. const BIG: f32 = 0.25; /// Results read back at once, some frames late so the CPU never waits. @@ -148,8 +150,8 @@ pub struct Cull { pub hidden: ([usize; 2], f32), /// Cull every frame, whatever the hidden share (A/B checks). pub always: bool, - /// Frames with fewer section draws than this do not cull. - pub min_draws: usize, + /// Frames with fewer sections in view than this do not cull. + pub min_sections: usize, } fn buffer( @@ -348,7 +350,7 @@ impl Cull { stamp: 0, hidden: ([0; 2], 1.0), always: false, - min_draws: 0, + min_sections: 0, } } diff --git a/crates/horizon-client/src/gfx/flat.rs b/crates/horizon-client/src/gfx/flat.rs index f6823c5d8..ea6df0d5b 100644 --- a/crates/horizon-client/src/gfx/flat.rs +++ b/crates/horizon-client/src/gfx/flat.rs @@ -96,17 +96,19 @@ pub struct Flat { pub calls: Vec, pub phase1_calls: usize, pub solid_calls: usize, - /// Frames with fewer section draws keep the per-section draws. - pub min_draws: usize, + /// Frames with fewer sections in view keep the per-section draws. + pub min_sections: usize, range_align: u64, } -/// Section draws from which a frame is drawn flat. Below it the index list -/// costs the GPU more than the draw calls cost the CPU: on an M4 Pro at -/// render distance 16 (2,900 draws) the copy and the list add about 0.4 ms -/// of GPU time for 0.5–0.8 ms less CPU time; at 32 (9,300 draws) the CPU -/// saves 1.3 ms. -const MIN_DRAWS: usize = 4096; +/// Sections in view (and distant tiles) from which a frame is drawn flat. +/// Below it the index list costs the GPU more than the draw calls cost the +/// CPU: on an M4 Pro at render distance 16 (2,900 draws) the copy and the +/// list add about 0.4 ms of GPU time for 0.5–0.8 ms less CPU time; at 32 +/// (9,300 draws) the CPU saves 1.3 ms. The bar was 4,096 draws when they +/// were 2.2 to 2.5 per section; a section's draw count now depends on the +/// sides it shows the eye (`mesher::Sides`), so it counts sections. +const MIN_SECTIONS: usize = 1600; /// Whether flat draws apply: the backend emulates multi-draw, and vertex /// shaders can read storage buffers large enough for an index page. @@ -337,10 +339,10 @@ impl Flat { calls: Vec::new(), phase1_calls: 0, solid_calls: 0, - min_draws: if std::env::var("HORIZON_FLAT_DRAWS").as_deref() == Ok("1") { + min_sections: if std::env::var("HORIZON_FLAT_DRAWS").as_deref() == Ok("1") { 0 } else { - MIN_DRAWS + MIN_SECTIONS }, range_align: (u64::from(device.limits().min_storage_buffer_offset_alignment) / 16) .max(1), From 303d497a676cd9a2667ca0c28345f6fc1ad45ee7 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 24 Oct 2026 23:26:42 +0200 Subject: [PATCH 024/107] Shader packs, clean room 9: frame uniforms, and gl_FragCoord in buffer passes The values a pack's post-processing programs read once per frame, in GL's conventions: camera position and the previous one, eye altitude, up/sun/moon/shadow-light positions in view space (the sky's own frame for the sun and moon), sun and shadow angles, world time, day and moon phase, frame counter, frame time and run time, view size and aspect, near and far, rain, smoothed wetness, thunder, fog colour, start and end, sky colour, eye in fluid, blindness, night vision, darkness, brightness, GUI hidden, eye brightness and its smoothed value, and the model-view and projection matrices with their inverses and last frame's. The projection is GL's perspective with the world's near and far planes times vanilla's view bobbing. Each value is written as the type the pack declared. The game passes what only it knows (day time, weather levels, the fluid at the camera, light at the camera, blindness, F1, brightness) in the renderer's Frame when HORIZON_SHADER_PACK is set. Fix (slice 4): programs that draw to buffers read gl_FragCoord through a variable only final set; every target sets it now, with a test. Found by the first frame drawn with a pack. Nothing reads the uniforms yet: players see no change. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 8 + crates/horizon-client/src/game/fov.rs | 2 +- .../horizon-client/src/game/shader_packs.rs | 34 ++ crates/horizon-client/src/gfx.rs | 3 + .../horizon-client/src/gfx/pack_uniforms.rs | 329 ++++++++++++++++++ crates/horizon-shaders/src/translate/mod.rs | 36 +- 6 files changed, 409 insertions(+), 3 deletions(-) create mode 100644 crates/horizon-client/src/game/shader_packs.rs create mode 100644 crates/horizon-client/src/gfx/pack_uniforms.rs diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 533973805..15aabd222 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -138,6 +138,7 @@ mod screens; mod sculk; mod sculk_fixture; mod server_travel; +mod shader_packs; mod shield; mod sign_edit; mod sniffers; @@ -10063,6 +10064,11 @@ impl Game<'_> { } else { self.gather_dynamic_lights(camera, alpha); } + let pack_inputs = if title { + Default::default() + } else { + self.shader_pack_inputs(camera, alpha as f32) + }; let frame = Frame { weather: &weather, weather_time: horizon_client::weather::Time::new(self.world.game_time, alpha as f32), @@ -10146,6 +10152,7 @@ impl Game<'_> { hand: &hand, hand_fov_degrees: self.hud_fov_degrees(camera_alpha as f32, camera.to_array()), screen: &screen, + pack: pack_inputs, border, game_time: self.world.game_time as f32, shader_game_time: ((self.world.game_time % 24000) as f32 + alpha as f32) / 24000.0, @@ -12671,6 +12678,7 @@ fn loading_frame(hud: &HudBatch) -> Frame<'_> { hand: &[], hand_fov_degrees: 70.0, screen: &[], + pack: Default::default(), border: None, game_time: 0.0, shader_game_time: 0.0, diff --git a/crates/horizon-client/src/game/fov.rs b/crates/horizon-client/src/game/fov.rs index 37796c8c3..27d3fb61e 100644 --- a/crates/horizon-client/src/game/fov.rs +++ b/crates/horizon-client/src/game/fov.rs @@ -74,7 +74,7 @@ impl Game<'_> { }) } /// The fluid at the camera, as `Camera.getFluidInCamera` reports it. - fn camera_fluid(&self, camera: [f64; 3]) -> CameraFluid { + pub(super) fn camera_fluid(&self, camera: [f64; 3]) -> CameraFluid { match fog_type(&self.world, self.res.facts(), camera, self.forward()) { FogType::Water => CameraFluid::Water, FogType::Lava => CameraFluid::Lava, diff --git a/crates/horizon-client/src/game/shader_packs.rs b/crates/horizon-client/src/game/shader_packs.rs new file mode 100644 index 000000000..e2cff8902 --- /dev/null +++ b/crates/horizon-client/src/game/shader_packs.rs @@ -0,0 +1,34 @@ +//! Shader packs, for development until the Shader Packs page exists: `HORIZON_SHADER_PACK=` +//! (the runtime that uses it comes next). What the pack's uniforms need from the game. + +use horizon_client::camera::CameraFluid; +use horizon_client::gfx::pack_uniforms::PackInputs; + +use super::Game; + +impl Game<'_> { + /// This frame's inputs to the pack's uniforms. + pub(super) fn shader_pack_inputs(&mut self, camera: glam::DVec3, partial: f32) -> PackInputs { + if std::env::var_os("HORIZON_SHADER_PACK").is_none() { + return PackInputs::default(); + } + let lerp = |(old, new): (f32, f32)| old + (new - old) * partial; + let at = camera.floor().as_ivec3(); + let light = self.world.light(at.x, at.y, at.z); + PackInputs { + day_time: self.world.day_time, + rain: lerp(self.world.weather.rain_level), + thunder: lerp(self.world.weather.thunder_level), + eye_in: match self.camera_fluid(camera.to_array()) { + CameraFluid::None => 0, + CameraFluid::Water => 1, + CameraFluid::Lava => 2, + CameraFluid::PowderSnow => 3, + }, + blindness: f32::from(u8::from(self.potions.effects.get("blindness").is_some())), + eye_light: [i32::from(light & 15), i32::from(light >> 4)], + hide_gui: self.ui.hide_gui, + brightness: self.settings.gamma, + } + } +} diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index c369ee1c7..02cac38d6 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -68,6 +68,7 @@ mod flat; pub mod lightning; mod lod; mod occlusion; +pub mod pack_uniforms; mod page_runs; pub mod panorama; mod resident; @@ -249,6 +250,8 @@ pub struct Frame<'a> { pub hand_fov_degrees: f32, /// Full-screen camera effects drawn over the world and hand, under the HUD. pub screen: &'a [crate::screen_effects::ScreenQuad], + /// What a shader pack's uniforms need from the game. + pub pack: pack_uniforms::PackInputs, /// The world border wall, when near enough to be drawn. pub border: Option, pub game_time: f32, diff --git a/crates/horizon-client/src/gfx/pack_uniforms.rs b/crates/horizon-client/src/gfx/pack_uniforms.rs new file mode 100644 index 000000000..7d2a73a6a --- /dev/null +++ b/crates/horizon-client/src/gfx/pack_uniforms.rs @@ -0,0 +1,329 @@ +//! The uniforms a shader pack's post-processing programs read once per frame (spec 5.2, class +//! F), from the renderer's `Frame` and the few values only the game knows (`PackInputs`). +//! Matrices follow GL's conventions (5.5): `gbufferProjection` maps view depth to clip z in +//! [-w, w], and the depth buffers packs read hold GL window depth. + +use glam::{Mat4, Vec3, Vec4}; + +use super::Frame; + +/// What the game passes for the pack's uniforms, beyond what `Frame` carries. +#[derive(Clone, Copy, Debug, Default)] +pub struct PackInputs { + /// `Level.getDayTime`. + pub day_time: i64, + /// The interpolated rain and thunder levels, 0..1. + pub rain: f32, + pub thunder: f32, + /// The fluid at the camera: 0 none, 1 water, 2 lava, 3 powder snow. + pub eye_in: i32, + pub blindness: f32, + /// Block and sky light at the camera, 0..15. + pub eye_light: [i32; 2], + pub hide_gui: bool, + /// The brightness option, 0..1. + pub brightness: f32, +} + +/// One uniform's value: up to 16 floats or 3 integers. +#[derive(Clone, Copy, Debug)] +pub enum Value { + F([f32; 16]), + I([i32; 3]), +} + +impl Value { + fn f(v: &[f32]) -> Self { + let mut a = [0.0; 16]; + a[..v.len()].copy_from_slice(v); + Value::F(a) + } + fn i(v: &[i32]) -> Self { + let mut a = [0; 3]; + a[..v.len()].copy_from_slice(v); + Value::I(a) + } + + /// Writes the value as the type the pack declared, at `offset` of a std140 block. + pub fn write(&self, ty: &str, out: &mut [u8], offset: usize) { + let ints = matches!(ty, "int" | "bool" | "uint") || ty.starts_with(['i', 'u']); + let (floats, integers): ([f32; 16], [i32; 16]) = match *self { + Value::F(f) => (f, f.map(|v| v as i32)), + Value::I(i) => { + let mut f = [0.0; 16]; + let mut v = [0; 16]; + for k in 0..3 { + f[k] = i[k] as f32; + v[k] = i[k]; + } + (f, v) + } + }; + let count = match ty { + "mat4" => 16, + "mat3" => { + // std140: three columns, each padded to a vec4, from the mat4's upper left. + for c in 0..3 { + let column: [f32; 3] = std::array::from_fn(|r| floats[c * 4 + r]); + put(out, offset + c * 16, bytemuck::cast_slice(&column)); + } + return; + } + t => match t.as_bytes().last() { + Some(n @ b'2'..=b'4') if t.contains("vec") => (n - b'0') as usize, + _ => 1, + }, + }; + if ints { + put(out, offset, bytemuck::cast_slice(&integers[..count])); + } else { + put(out, offset, bytemuck::cast_slice(&floats[..count])); + } + } +} + +fn put(out: &mut [u8], offset: usize, bytes: &[u8]) { + if let Some(dst) = out.get_mut(offset..offset + bytes.len()) { + dst.copy_from_slice(bytes); + } +} + +/// The uniforms provided, in the order of [`FrameUniforms::values`]. +pub const NAMES: &[&str] = &[ + "cameraPosition", + "previousCameraPosition", + "eyeAltitude", + "upPosition", + "sunPosition", + "moonPosition", + "shadowLightPosition", + "sunAngle", + "shadowAngle", + "worldTime", + "worldDay", + "moonPhase", + "frameCounter", + "frameTime", + "frameTimeCounter", + "viewWidth", + "viewHeight", + "aspectRatio", + "near", + "far", + "rainStrength", + "wetness", + "thunderStrength", + "fogColor", + "skyColor", + "fogMode", + "fogStart", + "fogEnd", + "fogDensity", + "fogShape", + "isEyeInWater", + "blindness", + "nightVision", + "darknessFactor", + "darknessLightFactor", + "screenBrightness", + "hideGUI", + "eyeBrightness", + "eyeBrightnessSmooth", + "heldItemId", + "heldItemId2", + "heldBlockLightValue", + "heldBlockLightValue2", + "renderStage", + "gbufferModelView", + "gbufferModelViewInverse", + "gbufferProjection", + "gbufferProjectionInverse", + "gbufferPreviousModelView", + "gbufferPreviousProjection", +]; + +/// Values carried from frame to frame. +pub struct FrameUniforms { + pub values: Vec, + start: std::time::Instant, + last: Option, + frame: u32, + previous: Option<(Vec3, Mat4, Mat4)>, + wetness: f32, + eye_smooth: [f32; 2], +} + +impl Default for FrameUniforms { + fn default() -> Self { + Self { + values: Vec::new(), + start: std::time::Instant::now(), + last: None, + frame: 0, + previous: None, + wetness: 0.0, + eye_smooth: [0.0; 2], + } + } +} + +/// `(0, 100, 0)` on the frame the sky draws a body on: Y -90°, then X by its angle in degrees. +fn body(angle: f32) -> Vec3 { + (Mat4::from_rotation_y((-90.0f32).to_radians()) * Mat4::from_rotation_x(angle.to_radians())) + .transform_vector3(Vec3::new(0.0, 100.0, 0.0)) +} + +/// `value` moved toward `target` with a half-life of `half_life` seconds over `dt` (5.6). +fn smooth(value: f32, target: f32, half_life: f32, dt: f32) -> f32 { + target + (value - target) * 0.5f32.powf(dt / half_life) +} + +impl FrameUniforms { + /// This frame's values for a render size of `size` pixels. + pub fn update(&mut self, frame: &Frame, inputs: &PackInputs, size: [u32; 2]) { + let now = std::time::Instant::now(); + let dt = self.last.map_or(0.0, |l| (now - l).as_secs_f32()); + self.last = Some(now); + let aspect = size[0] as f32 / size[1].max(1) as f32; + let view = crate::camera::view_matrix(frame.yaw, frame.pitch); + let projection = Mat4::perspective_rh_gl( + frame.fov_degrees.to_radians(), + aspect, + 0.05, + frame.depth_far, + ) * frame.bob; + let camera = frame.camera.as_vec3(); + let (previous_camera, previous_view, previous_projection) = + self.previous + .unwrap_or((Vec3::ZERO, Mat4::ZERO, Mat4::ZERO)); + self.previous = Some((camera, view, projection)); + + let to_view = |v: Vec3| view.transform_vector3(v); + let sun_angle = (frame.sky.sun_angle / 360.0 + 0.25).rem_euclid(1.0); + let day = sun_angle < 0.5; + let sun = to_view(body(frame.sky.sun_angle)); + let moon = to_view(body(frame.sky.moon_angle)); + // Half-lives are given in ticks (7.2); smoothing runs per frame in seconds (OQ-26). + let half_life = if inputs.rain > self.wetness { + 600.0 + } else { + 200.0 + }; + self.wetness = smooth(self.wetness, inputs.rain, half_life / 20.0, dt); + let eye = inputs.eye_light.map(|l| (l * 16) as f32); + for (smoothed, target) in self.eye_smooth.iter_mut().zip(eye) { + *smoothed = smooth(*smoothed, target, 10.0 / 20.0, dt); + } + let (fog_color, fog_start, fog_end) = match &frame.fog { + Some(f) => ( + [f.color[0], f.color[1], f.color[2]], + f.environmental_start.min(f.render_distance_start), + f.environmental_end.min(f.render_distance_end), + ), + None => (frame.sky.fog_color, frame.distance * 0.9, frame.distance), + }; + let day_time = inputs.day_time.rem_euclid(24000) as i32; + let world_day = inputs.day_time.div_euclid(24000); + let seconds = (now - self.start).as_secs_f32() % 3600.0; + + let value = |name: &str| -> Value { + let v3 = |v: Vec3| Value::f(&v.to_array()); + let m4 = |m: Mat4| Value::f(&m.to_cols_array()); + match name { + "cameraPosition" => v3(camera), + "previousCameraPosition" => v3(previous_camera), + "eyeAltitude" => Value::f(&[frame.camera.y as f32]), + "upPosition" => v3(to_view(Vec3::new(0.0, 100.0, 0.0))), + "sunPosition" => v3(sun), + "moonPosition" => v3(moon), + "shadowLightPosition" => v3(if day { sun } else { moon }), + "sunAngle" => Value::f(&[sun_angle]), + "shadowAngle" => Value::f(&[if day { sun_angle } else { sun_angle - 0.5 }]), + "worldTime" => Value::i(&[day_time]), + "worldDay" => Value::i(&[world_day as i32]), + "moonPhase" => Value::i(&[world_day.rem_euclid(8) as i32]), + "frameCounter" => Value::i(&[self.frame as i32]), + "frameTime" => Value::f(&[dt]), + "frameTimeCounter" => Value::f(&[seconds]), + "viewWidth" => Value::f(&[size[0] as f32]), + "viewHeight" => Value::f(&[size[1] as f32]), + "aspectRatio" => Value::f(&[aspect]), + "near" => Value::f(&[0.05]), + "far" => Value::f(&[frame.distance]), + "rainStrength" => Value::f(&[inputs.rain]), + "wetness" => Value::f(&[self.wetness]), + "thunderStrength" => Value::f(&[inputs.thunder]), + "fogColor" => Value::f(&fog_color), + "skyColor" => Value::f(&frame.sky.sky_color), + // GL_LINEAR: vanilla's fog is linear. + "fogMode" => Value::i(&[9729]), + "fogStart" => Value::f(&[fog_start]), + "fogEnd" => Value::f(&[fog_end]), + "fogDensity" => Value::f(&[1.0]), + "fogShape" => Value::i(&[0]), + "isEyeInWater" => Value::i(&[inputs.eye_in]), + "blindness" => Value::f(&[inputs.blindness]), + "nightVision" => Value::f(&[frame.night_vision]), + "darknessFactor" => Value::f(&[frame.darkness.0]), + "darknessLightFactor" => Value::f(&[frame.darkness.1]), + "screenBrightness" => Value::f(&[inputs.brightness]), + "hideGUI" => Value::i(&[inputs.hide_gui as i32]), + "eyeBrightness" => Value::i(&eye.map(|v| v as i32)), + "eyeBrightnessSmooth" => Value::i(&self.eye_smooth.map(|v| v.round() as i32)), + "gbufferModelView" => m4(view), + "gbufferModelViewInverse" => m4(view.inverse()), + "gbufferProjection" => m4(projection), + "gbufferProjectionInverse" => m4(projection.inverse()), + "gbufferPreviousModelView" => m4(previous_view), + "gbufferPreviousProjection" => m4(previous_projection), + // Held items wait for `item.properties` (5.3: 0 without the file); renderStage + // is NONE outside gbuffers draws. + _ => Value::i(&[0]), + } + }; + self.values = NAMES.iter().map(|n| value(n)).collect(); + self.frame = (self.frame + 1) % 720_720; + } + + /// `hz_fog_color` and `hz_fog` of the translator's block (the compatibility `gl_Fog`). + pub fn fog(&self) -> [Vec4; 2] { + let get = |name| match index(name).and_then(|i| self.values.get(i)) { + Some(Value::F(f)) => *f, + _ => [0.0; 16], + }; + let (color, start, end) = (get("fogColor"), get("fogStart")[0], get("fogEnd")[0]); + [ + Vec4::new(color[0], color[1], color[2], 1.0), + Vec4::new(1.0, start, end, 1.0 / (end - start).max(1e-3)), + ] + } +} + +/// The position of `name` in [`NAMES`]. +pub fn index(name: &str) -> Option { + NAMES.iter().position(|n| *n == name) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn values_follow_the_declared_type() { + let mut out = vec![0u8; 64]; + Value::i(&[7]).write("float", &mut out, 0); + assert_eq!(f32::from_ne_bytes(out[0..4].try_into().unwrap()), 7.0); + Value::f(&[1.5, 2.5]).write("ivec2", &mut out, 4); + assert_eq!(i32::from_ne_bytes(out[8..12].try_into().unwrap()), 2); + Value::i(&[1]).write("bool", &mut out, 16); + assert_eq!(i32::from_ne_bytes(out[16..20].try_into().unwrap()), 1); + Value::f(&[1.0; 16]).write("mat4", &mut out, 60); + assert_eq!(out[60..64], [0; 4], "a value past the block is dropped"); + } + + #[test] + fn noon_is_straight_up_and_the_sun_sets_in_the_west() { + assert!(body(0.0).abs_diff_eq(Vec3::new(0.0, 100.0, 0.0), 1e-3)); + assert!(body(90.0).abs_diff_eq(Vec3::new(-100.0, 0.0, 0.0), 1e-3)); + } +} diff --git a/crates/horizon-shaders/src/translate/mod.rs b/crates/horizon-shaders/src/translate/mod.rs index 9cc2ea07a..c6435025d 100644 --- a/crates/horizon-shaders/src/translate/mod.rs +++ b/crates/horizon-shaders/src/translate/mod.rs @@ -193,8 +193,11 @@ pub fn translate_composite( .unwrap(); let mut fragment_main = String::from("\nvoid main() {\n"); fragment_main.push_str(fog); - if target == Target::Window && f.frag_coord { - fragment_main.push_str(" hz_FragCoord = vec4(gl_FragCoord.x, hz_target.y - gl_FragCoord.y, gl_FragCoord.zw);\n"); + if f.frag_coord { + fragment_main.push_str(match target { + Target::Window => " hz_FragCoord = vec4(gl_FragCoord.x, hz_target.y - gl_FragCoord.y, gl_FragCoord.zw);\n", + Target::Buffers => " hz_FragCoord = gl_FragCoord;\n", + }); } for name in &f.inits { writeln!(fragment_main, " {name} = hz_init_{name}();").unwrap(); @@ -412,6 +415,35 @@ mod tests { ); } + #[test] + fn frag_coord_is_set_for_every_target() { + let fsh = "#version 120\nvoid main() { gl_FragColor = vec4(gl_FragCoord.x); }\n"; + for target in [Target::Buffers, Target::Window] { + let p = program( + &[("composite.vsh", VSH_120), ("composite.fsh", fsh)], + target, + ) + .unwrap(); + let m = &p.fragment; + let (handle, _) = m + .global_variables + .iter() + .find(|(_, g)| g.name.as_deref() == Some("hz_FragCoord")) + .unwrap(); + // naga calls our `main` from its entry point: look in every function. + let functions = m.functions.iter().map(|(_, f)| f); + let stores = functions.chain([&m.entry_points[0].function]).any(|f| { + f.body.iter().any(|s| match s { + naga::Statement::Store { pointer, .. } => { + matches!(f.expressions[*pointer], naga::Expression::GlobalVariable(h) if h == handle) + } + _ => false, + }) + }); + assert!(stores, "{target:?}: main does not set hz_FragCoord"); + } + } + #[test] fn errors_point_at_the_pack_file_and_line() { let fsh = From 59281c5c7357c8e8659c491f3312b3db229607c6 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 24 Oct 2026 23:42:17 +0200 Subject: [PATCH 025/107] Mod registry: cargo horizon-mod check, each kind's own checks - `horizon_mods::index::inspect`: each kind's own check (a mod's manifest and content, a modpack's manifest and overrides, a resource pack's pack.mcmeta), returning what the file says; `check_file` now compares those facts with the index. Shader packs and unknown kinds are refused, and `index::open` says which kinds the registry takes: shader submissions stay closed until Horizon's own format exists. - `cargo horizon-mod check `: those checks plus a trial run of a mod in the sandbox (ticks, frames, a chat line, each command), text or JSON. Co-Authored-By: Claude Opus 5.5 --- crates/cargo-horizon-mod/Cargo.toml | 4 +- crates/cargo-horizon-mod/src/check.rs | 268 +++++++++++++++++++++++++ crates/cargo-horizon-mod/src/main.rs | 38 +++- crates/horizon-mods/src/index.rs | 176 +++++++++++----- crates/horizon-mods/src/index/tests.rs | 5 +- docs/mods.md | 14 +- 6 files changed, 449 insertions(+), 56 deletions(-) create mode 100644 crates/cargo-horizon-mod/src/check.rs diff --git a/crates/cargo-horizon-mod/Cargo.toml b/crates/cargo-horizon-mod/Cargo.toml index 6d0a231ec..34eefe227 100644 --- a/crates/cargo-horizon-mod/Cargo.toml +++ b/crates/cargo-horizon-mod/Cargo.toml @@ -4,10 +4,10 @@ version = "0.1.0" edition.workspace = true license.workspace = true rust-version = "1.86" -description = "cargo horizon-mod new/build/run: make Horizon mods in Rust (docs/mods.md)" +description = "cargo horizon-mod new/build/run/check: make Horizon mods in Rust (docs/mods.md)" [dependencies] -horizon-mods = { path = "../horizon-mods", default-features = false, features = ["pack"] } +horizon-mods = { path = "../horizon-mods", default-features = false, features = ["pack", "runtime", "index"] } serde_json = "1.0.151" [lints] diff --git a/crates/cargo-horizon-mod/src/check.rs b/crates/cargo-horizon-mod/src/check.rs new file mode 100644 index 000000000..0294bec98 --- /dev/null +++ b/crates/cargo-horizon-mod/src/check.rs @@ -0,0 +1,268 @@ +//! `cargo horizon-mod check `: what the registry checks of a submission, +//! run anywhere: its kind's own checks (docs/api/mod-index.md), its signature +//! when given a key, and for a mod a trial run in the sandbox: it must start, +//! survive ticks, frames, a chat line and each of its commands, and import +//! no more than its capabilities. +use horizon_mods::{ + Clock, Mods, Output, Package, Player, Snapshot, Status, + index::{self, Facts, Kind}, + manifest::component_imports, +}; +use serde_json::{Value, json}; +use std::path::Path; + +/// How long the trial run plays: two seconds of game. +const TICKS: u64 = 40; +const FRAMES_PER_TICK: u64 = 3; + +pub struct Report { + pub kind: Kind, + pub facts: Facts, + pub sha256: String, + pub size: u64, + pub signed: bool, + /// A mod's component imports. + pub imports: Vec, + /// A mod's commands, each run once. + pub commands: Vec, + /// What the mod showed during the trial run. + pub output: Vec, +} + +impl Report { + pub fn json(&self) -> Value { + let requires: Vec = self + .facts + .requires + .iter() + .map(|r| json!({"id": r.id, "version": r.version, "sha256": r.sha256})) + .collect(); + json!({ + "kind": self.kind.name(), + "id": self.facts.id, + "version": self.facts.version, + "api": self.facts.api, + "side": self.facts.side, + "capabilities": self.facts.capabilities, + "game": self.facts.game, + "requires": requires, + "sha256": self.sha256, + "size": self.size, + "signed": self.signed, + "imports": self.imports, + "commands": self.commands, + "output": self.output, + }) + } + + pub fn text(&self) -> String { + let f = &self.facts; + let mut out = format!( + "{} {} ({}", + f.id.as_deref().unwrap_or("resource pack"), + f.version.as_deref().unwrap_or(""), + self.kind.name() + ); + if let Some(side) = f.side { + let side = serde_json::to_value(side).unwrap_or_default(); + out += &format!(", API {}, {}", f.api, side.as_str().unwrap_or("?")); + } else if !f.api.is_empty() { + out += &format!(", format {}", f.api); + } + if let Some(game) = &f.game { + out += &format!(", for {game}"); + } + let signed = if self.signed { + "signature verified" + } else { + "unsigned" + }; + out += &format!( + ")\n {} bytes, SHA-256 {}, {signed}\n", + self.size, self.sha256 + ); + for c in &f.capabilities { + out += &format!(" may {}\n", c.describe()); + } + for r in &f.requires { + out += &format!(" needs {} {} ({})\n", r.id, r.version, &r.sha256[..12]); + } + if self.kind == Kind::Mod { + let commands = if self.commands.is_empty() { + String::new() + } else { + format!(", /{}", self.commands.join(", /")) + }; + out += &format!( + " trial run: started, {TICKS} ticks, {} frames, a chat line{commands}: ok\n", + TICKS * FRAMES_PER_TICK + ); + } + out + } +} + +/// The kind a file's extension says. +pub fn kind_of(path: &Path) -> Option { + match path.extension()?.to_str()? { + "hmod" => Some(Kind::Mod), + "hpack" => Some(Kind::Modpack), + "zip" => Some(Kind::ResourcePack), + _ => None, + } +} + +/// Checks the file at `path` as `kind`; `key` and `signature` (the +/// `.minisig` text) check its signature. +pub fn check( + path: &Path, + kind: Kind, + key: Option<&str>, + signature: Option<&str>, +) -> Result { + let size = std::fs::metadata(path) + .map_err(|e| format!("{}: {e}", path.display()))? + .len(); + if size > index::MAX_FILE { + return Err(format!("over {} MiB", index::MAX_FILE >> 20)); + } + index::open(&kind)?; + let facts = index::inspect(&kind, path)?; + let signed = match (key, signature) { + (Some(key), Some(sig)) => { + index::verify_signature(key, sig, path)?; + true + } + (None, None) => false, + _ => return Err("--key and --signature go together".into()), + }; + let mut report = Report { + kind, + facts, + sha256: index::sha256_file(path)?, + size, + signed, + imports: Vec::new(), + commands: Vec::new(), + output: Vec::new(), + }; + if report.kind == Kind::Mod { + trial_run(path, &mut report)?; + } + Ok(report) +} + +/// Runs a mod in the sandbox, as the game does, from a scratch copy so its +/// storage lands in a folder removed afterwards. +fn trial_run(path: &Path, report: &mut Report) -> Result<(), String> { + let package = Package::read(path)?; + let wasm = package.wasm.as_deref().ok_or("the .hmod has no mod.wasm")?; + report.imports = component_imports(wasm)?; + package + .manifest + .check_imports(report.imports.iter().map(String::as_str))?; + let dir = std::env::temp_dir().join(format!("horizon-mod-check-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).map_err(|e| e.to_string())?; + let copy = dir.join("mod.hmod"); + let result = std::fs::copy(path, ©) + .map_err(|e| e.to_string()) + .and_then(|_| play(©, report)); + let _ = std::fs::remove_dir_all(&dir); + result +} + +fn play(path: &Path, report: &mut Report) -> Result<(), String> { + let mut mods = Mods::load(&[path.to_path_buf()], |_| true, |_| false); + let failed = |mods: &Mods| match &mods.entries()[0].status { + Status::Failed(reason) => Some(reason.clone()), + _ => None, + }; + if let Some(reason) = failed(&mods) { + return Err(format!("it didn't start: {reason}")); + } + report.commands = mods.commands().map(str::to_owned).collect(); + for t in 0..TICKS { + mods.set_snapshot(Snapshot { + player: Some(Player { + x: 0.5 + t as f64 * 0.1, + y: 64.0, + z: 0.5, + yaw: 90.0, + pitch: 0.0, + health: 20.0, + food: 20, + dimension: "minecraft:overworld".into(), + }), + clock: Some(Clock { + game_time: t, + day_time: 1000 + t, + }), + }); + mods.tick(t); + for _ in 0..FRAMES_PER_TICK { + mods.frame(1.0 / 60.0); + } + } + mods.chat(" hello"); + for name in report.commands.clone() { + mods.command(&name); + } + for out in mods.drain() { + report.output.push(match out { + Output::Chat { text, .. } => format!("chat: {text}"), + Output::Feedback { text, error } => { + format!("{}: {text}", if error { "error" } else { "feedback" }) + } + Output::Stopped { reason, .. } => return Err(format!("it stopped: {reason}")), + }); + } + match failed(&mods) { + Some(reason) => Err(format!("it stopped: {reason}")), + None => Ok(()), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn fixture(name: &str) -> std::path::PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../horizon-mods/tests/index") + .join(name) + } + + #[test] + fn checks_each_kind_its_own_way() { + let pack = fixture("cozy-1.0.0.hpack"); + let report = check(&pack, kind_of(&pack).unwrap(), None, None).unwrap(); + assert_eq!(report.facts.requires[0].id, "coords_hud"); + assert!( + report + .text() + .contains("cozy 1.0.0 (modpack, format 1, for 26.3)") + ); + // The fixture mod's code is a stand-in: its manifest reads, its run fails. + let hmod = fixture("coords_hud-0.1.0.hmod"); + let key = std::fs::read_to_string(fixture("test.pub")).unwrap(); + let sig = std::fs::read_to_string(fixture("coords_hud-0.1.0.hmod.minisig")).unwrap(); + let key = key.lines().last().unwrap(); + let e = check(&hmod, Kind::Mod, Some(key), Some(&sig)) + .err() + .unwrap(); + assert!(e.contains("didn't start"), "{e}"); + let e = check( + &hmod, + Kind::Mod, + Some(key), + Some("untrusted comment: x\nRUQ=\n"), + ) + .err() + .unwrap(); + assert!(e.contains("signature"), "{e}"); + let e = check(&pack, Kind::Shader, None, None).err().unwrap(); + assert!(e.contains("shader packs open"), "{e}"); + assert!(check(&pack, Kind::Mod, None, None).is_err()); + } +} diff --git a/crates/cargo-horizon-mod/src/main.rs b/crates/cargo-horizon-mod/src/main.rs index 6b088d278..9e0076083 100644 --- a/crates/cargo-horizon-mod/src/main.rs +++ b/crates/cargo-horizon-mod/src/main.rs @@ -4,10 +4,14 @@ //! - `build [--debug]`: the `.hmod` in `target/horizon-mod/`. //! - `run [--game ] [-- ]`: build, then play with //! the mod; run `build` again and the game reloads it. +//! - `check [--json]`: what the registry checks of a submission +//! (`.hmod`, `.hpack`, resource pack `.zip`), with a trial run for a mod. use horizon_mods::{ Manifest, Package, manifest::{MANIFEST, component_imports, componentize}, }; +mod check; + use std::{ path::{Path, PathBuf}, process::{Command, ExitCode, Stdio}, @@ -28,7 +32,8 @@ fn main() -> ExitCode { println!("Built {}", p.display()); }), Some("run") => run(&args[1..]), - _ => Err("usage: cargo horizon-mod new | build [--debug] | run [--game ] [-- ]".into()), + Some("check") => check(&args[1..]), + _ => Err("usage: cargo horizon-mod new | build [--debug] | run [--game ] [-- ] | check [--kind ] [--key --signature ] [--json]".into()), }; match result { Ok(()) => ExitCode::SUCCESS, @@ -150,6 +155,37 @@ fn build(release: bool) -> Result { Ok(path) } +/// `check `: the registry's checks of a submission, printed; fails +/// when one does. +fn check(args: &[String]) -> Result<(), String> { + let file = args + .first() + .filter(|a| !a.starts_with("--")) + .ok_or("cargo horizon-mod check ")?; + let path = Path::new(file); + let kind = match flag(args, "--kind") { + Some(k) => horizon_mods::index::Kind::from(k.to_string()), + None => check::kind_of(path) + .ok_or("pass --kind mod|modpack|resourcepack: the extension doesn't say")?, + }; + let signature = flag(args, "--signature") + .map(|p| std::fs::read_to_string(p).map_err(|e| format!("{p}: {e}"))) + .transpose()?; + let report = check::check(path, kind, flag(args, "--key"), signature.as_deref()); + if args.iter().any(|a| a == "--json") { + let json = match &report { + Ok(r) => serde_json::json!({"ok": true, "report": r.json()}), + Err(e) => serde_json::json!({"ok": false, "error": e}), + }; + println!("{json}"); + } + let report = report?; + if !args.iter().any(|a| a == "--json") { + print!("{}", report.text()); + } + Ok(()) +} + fn run(args: &[String]) -> Result<(), String> { let path = build(true)?; let game = flag(args, "--game") diff --git a/crates/horizon-mods/src/index.rs b/crates/horizon-mods/src/index.rs index 73af7b46d..692b1ae73 100644 --- a/crates/horizon-mods/src/index.rs +++ b/crates/horizon-mods/src/index.rs @@ -448,60 +448,109 @@ pub fn check_file( (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 - )); - } - } + if matches!(project.kind, Kind::Shader | Kind::Other(_)) { + return Err(format!( + "this Horizon can't install a {} yet", + project.kind.name() + )); } + inspect(&project.kind, path)?.matches(project, version)?; Ok(trust) } +/// What a file says about itself, read by its kind's own check +/// ([`inspect`]). The registry's CI writes these into the index; the +/// launcher compares them with the index before it installs. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Facts { + /// A mod's or a modpack's manifest id; None for a resource pack. + pub id: Option, + pub version: Option, + /// A mod's API line, a modpack's format, a resource pack's pack format. + pub api: String, + pub side: Option, + /// In the manifest's order. + pub capabilities: Vec, + /// The Minecraft version, when the file names one (a modpack does). + pub game: Option, + pub requires: Vec, +} + +impl Facts { + /// Whether the file is the one `version` of `project` describes. + pub fn matches(&self, project: &Project, version: &Version) -> Result<(), String> { + let (mut caps, mut listed) = (self.capabilities.clone(), version.capabilities.clone()); + caps.sort(); + listed.sort(); + let differs = self.id.as_ref().is_some_and(|id| *id != project.id) + || self.version.as_ref().is_some_and(|v| *v != version.version) + || self.api != version.api + || self.side != version.side + || caps != listed + || self.game.as_ref().is_some_and(|g| *g != version.game) + || self.requires != version.requires; + if !differs { + return Ok(()); + } + Err(match project.kind { + Kind::Mod => format!( + "its horizon-mod.toml ({} {}, API {}, {:?}, {}) isn't what the index lists", + self.id.as_deref().unwrap_or("?"), + self.version.as_deref().unwrap_or("?"), + self.api, + self.side, + names(&self.capabilities) + ), + Kind::Modpack => format!("its {} isn't what the index lists", modpack::MANIFEST), + _ => format!("its pack format {} isn't what the index lists", self.api), + }) + } +} + +/// Reads and checks a file as its kind: a mod's manifest and content, a +/// modpack's manifest and overrides, a resource pack's `pack.mcmeta` and +/// paths. Shader packs and kinds this build doesn't know are refused: their +/// checks come with their formats. +pub fn inspect(kind: &Kind, path: &Path) -> Result { + match kind { + Kind::Mod => { + let m = Package::read(path)?.manifest; + crate::Content::read(path, &m)?; + Ok(Facts { + capabilities: m.capabilities, + id: Some(m.id), + version: Some(m.version), + api: m.api, + side: Some(m.side), + ..Facts::default() + }) + } + Kind::Modpack => { + let m = Modpack::read(path)?.manifest; + Ok(Facts { + requires: m.mods.iter().map(modpack::PackMod::requirement).collect(), + id: Some(m.id), + version: Some(m.version), + api: m.format.to_string(), + game: Some(m.game), + ..Facts::default() + }) + } + Kind::ResourcePack => Ok(Facts { + api: check_resource_pack(path)?, + ..Facts::default() + }), + Kind::Shader => Err( + "Horizon's own shader pack format isn't out yet: shader packs open when it is".into(), + ), + Kind::Other(name) => Err(format!("this Horizon can't read a {name} yet")), + } +} + /// 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> { +/// object, and only plain paths. Returns its pack format (`pack_format`, or +/// `min_format` in newer packs), empty when it names none. +fn check_resource_pack(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())?; for i in 0..zip.len() { @@ -523,10 +572,35 @@ fn check_resource_pack(path: &Path) -> Result<(), String> { .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() { + let pack = &json["pack"]; + if !pack.is_object() { return Err("pack.mcmeta: no \"pack\" object".into()); } - Ok(()) + let format = [&pack["pack_format"], &pack["min_format"]] + .into_iter() + .find(|f| !f.is_null()) + .map(|f| match f { + serde_json::Value::Array(parts) => parts + .iter() + .map(ToString::to_string) + .collect::>() + .join("."), + other => other.to_string(), + }) + .unwrap_or_default(); + Ok(format) +} + +/// The kinds the registry takes submissions for. Shader packs wait for +/// Horizon's own format; other kinds for theirs. +pub fn open(kind: &Kind) -> Result<(), String> { + match kind { + Kind::Mod | Kind::Modpack | Kind::ResourcePack => Ok(()), + Kind::Shader => Err( + "shader packs open when Horizon's own shader pack format is out (Iris and OptiFine packs stay on Modrinth)".into(), + ), + Kind::Other(name) => Err(format!("the registry doesn't take {name} yet")), + } } fn names(caps: &[Capability]) -> String { diff --git a/crates/horizon-mods/src/index/tests.rs b/crates/horizon-mods/src/index/tests.rs index b6d7f5bfb..e3883f79e 100644 --- a/crates/horizon-mods/src/index/tests.rs +++ b/crates/horizon-mods/src/index/tests.rs @@ -151,7 +151,10 @@ fn resource_packs_need_a_pack_mcmeta() { path }; let meta = r#"{"pack": {"description": "Look", "min_format": 75, "max_format": 75}}"#; - assert_eq!(check_resource_pack(&pack(&[("pack.mcmeta", meta)])), Ok(())); + assert_eq!( + check_resource_pack(&pack(&[("pack.mcmeta", meta)])), + Ok("75".into()) + ); 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(); diff --git a/docs/mods.md b/docs/mods.md index 6f3216d38..ac8afda94 100644 --- a/docs/mods.md +++ b/docs/mods.md @@ -16,7 +16,7 @@ folder, loads no runtime and plays exactly like vanilla 26.3. | SDK for mod authors | `crates/horizon-mod-sdk` | 0.1 | | Example mod | `crates/horizon-mod-sdk/examples/coords-hud` | coordinates, facing and time HUD, `/where`, `/coords` | | Runtime | `crates/horizon-mods` | wasmtime 36 LTS, component model | -| Authoring tool | `crates/cargo-horizon-mod` | `cargo horizon-mod new / build / run` | +| Authoring tool | `crates/cargo-horizon-mod` | `cargo horizon-mod new / build / run / check` | | 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 | | Registry index (`index.json`, `.hpack`), signatures | `horizon_mods::index`, [api/mod-index.md](api/mod-index.md) | schema 1; launcher and website next | @@ -226,6 +226,18 @@ always on, and the game checks its file once a second: rebuild and it restarts with "Mods reloaded" in chat. Its in-memory state starts over; whatever it saved with capability `storage` survives the reload. +Before you submit it to the registry, run what its CI runs: + +```sh +cargo horizon-mod check target/horizon-mod/my_mod.hmod # --json for the report as JSON +``` + +It reads the manifest and content, refuses imports the capabilities don't +cover, then plays the mod in the sandbox for 40 ticks, 120 frames, a chat line +and each of its commands: it must start and never trap or overrun. It checks a +`.hpack` and a resource pack `.zip` too, and a signature with `--key --signature .minisig`. + ### Template layout ``` From 7d76923a04fbbae783e56577a93465dceeab8bec Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 24 Oct 2026 23:42:57 +0200 Subject: [PATCH 026/107] Mod registry tools: the registry's entries, build and review - `horizon_mods::index::registry`: the registry repository's `mods/.toml` and `revoked.toml`, `build` (every entry and file checked, every problem listed, the index written) and `review` (a published file never changes or disappears, a kind never changes; non-owner, owner and key changes pointed out). - `cargo horizon-mod registry build|review|check|list` for the registry's CI. Co-Authored-By: Claude Opus 5.5 --- crates/cargo-horizon-mod/src/main.rs | 5 +- crates/cargo-horizon-mod/src/registry.rs | 159 +++++++ crates/horizon-mods/src/index.rs | 1 + crates/horizon-mods/src/index/registry.rs | 415 ++++++++++++++++++ .../horizon-mods/src/index/registry/tests.rs | 237 ++++++++++ docs/api/mod-index.md | 53 +++ docs/mods.md | 2 +- 7 files changed, 870 insertions(+), 2 deletions(-) create mode 100644 crates/cargo-horizon-mod/src/registry.rs create mode 100644 crates/horizon-mods/src/index/registry.rs create mode 100644 crates/horizon-mods/src/index/registry/tests.rs diff --git a/crates/cargo-horizon-mod/src/main.rs b/crates/cargo-horizon-mod/src/main.rs index 9e0076083..925633930 100644 --- a/crates/cargo-horizon-mod/src/main.rs +++ b/crates/cargo-horizon-mod/src/main.rs @@ -6,11 +6,13 @@ //! the mod; run `build` again and the game reloads it. //! - `check [--json]`: what the registry checks of a submission //! (`.hmod`, `.hpack`, resource pack `.zip`), with a trial run for a mod. +//! - `registry build|review|check|list`: the registry repository's CI. use horizon_mods::{ Manifest, Package, manifest::{MANIFEST, component_imports, componentize}, }; mod check; +mod registry; use std::{ path::{Path, PathBuf}, @@ -33,7 +35,8 @@ fn main() -> ExitCode { }), Some("run") => run(&args[1..]), Some("check") => check(&args[1..]), - _ => Err("usage: cargo horizon-mod new | build [--debug] | run [--game ] [-- ] | check [--kind ] [--key --signature ] [--json]".into()), + Some("registry") => registry::main(&args[1..]), + _ => Err("usage: cargo horizon-mod new | build [--debug] | run [--game ] [-- ] | check [--kind ] [--key --signature ] [--json] | registry build|review|check|list".into()), }; match result { Ok(()) => ExitCode::SUCCESS, diff --git a/crates/cargo-horizon-mod/src/registry.rs b/crates/cargo-horizon-mod/src/registry.rs new file mode 100644 index 000000000..ac5e77ea4 --- /dev/null +++ b/crates/cargo-horizon-mod/src/registry.rs @@ -0,0 +1,159 @@ +//! `cargo horizon-mod registry build|review|check|list`: the registry repository's CI +//! (`ludovic111/horizon-mods`, docs/api/mod-index.md). Network-free: the +//! workflow downloads the files and publishes what this writes. +use horizon_mods::index::{ + FILE_HOST, + registry::{self, Sources}, +}; +use std::path::Path; + +/// Where the registry's site is published (its icons). +const SITE: &str = "https://ludovic111.github.io/horizon-mods/"; + +const USAGE: &str = "usage: cargo horizon-mod registry build --files --out [--release ] [--site ] [--generated