diff --git a/AGENTS.md b/AGENTS.md index 0656e40fc..a72c6723a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -45,6 +45,7 @@ Game workspace (root `Cargo.toml`); `launcher/` and `crates/horizon-telemetry-se | `horizon-friends`, `horizon-relay` | Play with Friends: invite codes, encrypted tunnel, the relay ([docs/play-with-friends.md](docs/play-with-friends.md)) | | `horizon-mods`, `horizon-mod-sdk`, `cargo-horizon-mod` | Sandboxed WebAssembly mods: runtime, SDK + WIT, `cargo horizon-mod` ([docs/mods.md](docs/mods.md)) | | `horizon-port-pack` | Porting Java modpacks ([docs/modpacks.md](docs/modpacks.md)) | +| `horizon-shader-api` | Horizon's own WGSL shader packs ([docs/api/shaders.md](docs/api/shaders.md)): `horizon-shaders.toml`, the `.hshader` package; shared by the game, the launcher and the registry | | `horizon-shaders` | Shader packs (the `shaders/` folder format), rebuilt clean-room from a written spec of the format; no wgpu | | `horizon-theme` | Design tokens and fonts for the launcher, the game's own menus and the website | | `horizon-telemetry`, `horizon-telemetry-server`, `horizon-bench` | Opt-in data collection, its ingest server, the benchmark report format | diff --git a/Cargo.lock b/Cargo.lock index 2d09d2d0e..70c3c831c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1437,6 +1437,19 @@ dependencies = [ "ureq", ] +[[package]] +name = "horizon-shader-api" +version = "0.1.0" +dependencies = [ + "bytemuck", + "naga", + "serde", + "serde_json", + "sha2", + "toml", + "zip", +] + [[package]] name = "horizon-shaders" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index c6f2bc581..294e93aa9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] exclude = ["launcher", "crates/horizon-telemetry-server", "crates/horizon-mod-registry", "third_party/wgpu-hal"] -members = ["crates/horizon-core", "crates/horizon-dualsense", "crates/horizon-threads", "crates/horizon-world", "crates/horizon-client", "crates/horizon-server-files", "crates/horizon-server", "crates/horizon-bots", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-prepare", "crates/horizon-telemetry", "crates/horizon-bench", "crates/horizon-friends", "crates/horizon-relay", "crates/horizon-mod-sdk", "crates/horizon-mods", "crates/horizon-mods/tests/guest", "crates/cargo-horizon-mod", "crates/horizon-port-pack", "crates/horizon-shaders", "crates/horizon-release-sign", "crates/horizon-mod-sdk/examples/coords-hud", "xtask"] +members = ["crates/horizon-core", "crates/horizon-dualsense", "crates/horizon-threads", "crates/horizon-world", "crates/horizon-client", "crates/horizon-server-files", "crates/horizon-server", "crates/horizon-bots", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-prepare", "crates/horizon-telemetry", "crates/horizon-bench", "crates/horizon-friends", "crates/horizon-relay", "crates/horizon-mod-sdk", "crates/horizon-mods", "crates/horizon-mods/tests/guest", "crates/cargo-horizon-mod", "crates/horizon-port-pack", "crates/horizon-shaders", "crates/horizon-shader-api", "crates/horizon-release-sign", "crates/horizon-mod-sdk/examples/coords-hud", "xtask"] resolver = "2" [workspace.package] diff --git a/crates/horizon-mod-registry/README.md b/crates/horizon-mod-registry/README.md index 936fe5acf..f7cdc68c5 100644 --- a/crates/horizon-mod-registry/README.md +++ b/crates/horizon-mod-registry/README.md @@ -40,5 +40,8 @@ REGISTRY_PUBLIC_URL=http://localhost:4173 REGISTRY_SESSION_KEY=... GITHUB_CLIENT | `REGISTRY_REPO` | Default `ludovic111/horizon-mods` | | `PORT` | Default 8080 | -On Railway it is its own service (`railway.json`, built with the `Dockerfile` from the repository root); the website's -Caddy sends `/api/mods/*` to it over Railway's private network. +On Railway it is the service `horizon-mod-registry` of the project `horizon-site`, next to the website: built from +`main` with this `Dockerfile` from the repository root, health check `/healthz`, no public domain and no volume. The +website's Caddy sends `/api/mods/*` to it over Railway's private network (`REGISTRY_UPSTREAM` on the website service: +`http://horizon-mod-registry.railway.internal:8080`). `railway.json` lists the same settings; Railway reads them from +the service, not from the file. diff --git a/crates/horizon-mod-registry/src/lib.rs b/crates/horizon-mod-registry/src/lib.rs index 9799bace8..72970017a 100644 --- a/crates/horizon-mod-registry/src/lib.rs +++ b/crates/horizon-mod-registry/src/lib.rs @@ -625,11 +625,16 @@ pub fn publish( .map(|r| format!("- needs {} {}", r.id, r.version)) .collect(); let body = format!( - "\nSubmitted by @{login} through [the form]({public}/mods/submit).\n\n\ + "\n🤖 Submitted through [{site}]({public}/mods/submit) by @{login}. The form opens the pull request with \ + the registry's token: neither Ludovic nor a reviewer wrote it.\n\n\ | | |\n|---|---|\n| Project | `{id}` ({kind}, {new}) |\n| Version | {version} for {game} |\n| File | `{file}`, {size} bytes, SHA-256 `{sha256}` |\n| Signed | {signed} |\n| Source | {source} |\n\n{lists}\n\n\ The checks below run the file's own checks again and, for a mod, a trial run in the sandbox. \ Reviewers: read the source and the checks' report before you approve.", public = config.public_url, + site = config + .public_url + .split_once("://") + .map_or(config.public_url.as_str(), |(_, host)| host), id = form.id, kind = kind.name(), new = if is_new { "new" } else { "update" }, diff --git a/crates/horizon-mod-registry/tests/submit.rs b/crates/horizon-mod-registry/tests/submit.rs index 77487089d..f586effac 100644 --- a/crates/horizon-mod-registry/tests/submit.rs +++ b/crates/horizon-mod-registry/tests/submit.rs @@ -303,7 +303,11 @@ async fn a_submission_becomes_a_pull_request() { assert!(message.contains("Co-authored-by: alice <7+alice@users.noreply.github.com>")); assert_eq!(replaced, &None); let body = m.pulls[0]["body"].as_str().unwrap(); - assert!(body.starts_with(""), "{body}"); + assert!( + body.starts_with("\n🤖 Submitted through "), + "{body}" + ); + assert!(body.contains("/mods/submit) by @alice."), "{body}"); assert!(body.contains("may draw text on your screen"), "{body}"); } // The same version again: its branch is there. diff --git a/crates/horizon-shader-api/Cargo.toml b/crates/horizon-shader-api/Cargo.toml new file mode 100644 index 000000000..3d365cd83 --- /dev/null +++ b/crates/horizon-shader-api/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "horizon-shader-api" +version = "0.1.0" +edition.workspace = true +license.workspace = true +rust-version = "1.85" +description = "Horizon's own shader packs (WGSL, docs/shaders.md): the format, the stages, the check, and the horizon-shader tool" + +[dependencies] +bytemuck = { version = "1.24", features = ["derive"] } +# Parses and validates a pack's WGSL, and translates it as wgpu does for Vulkan, Metal and DirectX 12. +naga = { version = "30.0.1", features = ["wgsl-in", "spv-out", "msl-out", "hlsl-out"] } +serde = { version = "1", features = ["derive"] } +serde_json = "1" +sha2 = "0.10" +toml = "1.1.6" +zip = { version = "2", default-features = false, features = ["deflate"] } + +[lints] +workspace = true diff --git a/crates/horizon-shader-api/packs/golden_hour/README.md b/crates/horizon-shader-api/packs/golden_hour/README.md new file mode 100644 index 000000000..943bce85a --- /dev/null +++ b/crates/horizon-shader-api/packs/golden_hour/README.md @@ -0,0 +1,4 @@ +# Golden Hour + +Horizon's example shader pack (docs/shaders.md): a sky stage and two post passes, with five options. Check it with +`horizon-shader check crates/horizon-shader-api/packs/golden_hour`. diff --git a/crates/horizon-shader-api/packs/golden_hour/common.wgsl b/crates/horizon-shader-api/packs/golden_hour/common.wgsl new file mode 100644 index 000000000..687e76f4b --- /dev/null +++ b/crates/horizon-shader-api/packs/golden_hour/common.wgsl @@ -0,0 +1,13 @@ +fn luminance(c: vec3) -> f32 { + return dot(c, vec3(0.2126, 0.7152, 0.0722)); +} + +// 1 while the sun is up in the Overworld, fading out around the horizon. +fn daylight() -> f32 { + return select(0.0, smoothstep(-0.1, 0.1, frame.sun_direction.y), frame.dimension == 0); +} + +// 1 when the sun is on the horizon, 0 from a quarter of the way up. +fn sun_low() -> f32 { + return 1.0 - smoothstep(0.0, 0.35, abs(frame.sun_direction.y)); +} diff --git a/crates/horizon-shader-api/packs/golden_hour/horizon-shaders.toml b/crates/horizon-shader-api/packs/golden_hour/horizon-shaders.toml new file mode 100644 index 000000000..8b67cff3b --- /dev/null +++ b/crates/horizon-shader-api/packs/golden_hour/horizon-shaders.toml @@ -0,0 +1,69 @@ +schema = 1 +id = "golden_hour" +name = "Golden Hour" +version = "1.0.0" +api = "0.1" +description = "Warm light at sunrise and sunset, a soft glow around bright things and a gentle colour grade." +authors = ["Horizon"] +license = "MIT" +shared = ["common.wgsl"] + +[sky] +file = "sky.wgsl" +name = "Warm horizon" + +[[post]] +file = "post/glow.wgsl" +name = "Glow" + +[[post]] +file = "post/grade.wgsl" +name = "Colour grade" + +[[options]] +id = "sun_glow" +name = "Sunset glow" +description = "How warm the sky is around a low sun." +type = "float" +default = 0.6 +min = 0.0 +max = 1.0 +step = 0.05 + +[[options]] +id = "glow" +name = "Glow" +description = "The soft halo around bright things: the sun, lava, torches." +type = "float" +default = 0.35 +min = 0.0 +max = 1.0 +step = 0.05 + +[[options]] +id = "warmth" +name = "Warmth" +description = "How golden the light turns when the sun is low." +type = "float" +default = 0.4 +min = 0.0 +max = 1.0 +step = 0.05 + +[[options]] +id = "saturation" +name = "Saturation" +type = "float" +default = 1.1 +min = 0.0 +max = 2.0 +step = 0.05 + +[[options]] +id = "vignette" +name = "Vignette" +type = "bool" +default = true + +[budget] +gpu_ms = 1.0 diff --git a/crates/horizon-shader-api/packs/golden_hour/post/glow.wgsl b/crates/horizon-shader-api/packs/golden_hour/post/glow.wgsl new file mode 100644 index 000000000..4d523c9ef --- /dev/null +++ b/crates/horizon-shader-api/packs/golden_hour/post/glow.wgsl @@ -0,0 +1,22 @@ +// A soft halo around bright pixels: twelve taps on two rings, scaled with +// the picture's height so it looks the same at any resolution. +override glow: f32 = 0.35; + +fn bright(c: vec3) -> vec3 { + return c * smoothstep(0.7, 1.0, luminance(c)); +} + +fn post(input: PostInput) -> vec4 { + if glow <= 0.0 { + return input.color; + } + let step = frame.resolution.y / 720.0 / frame.resolution; + var sum = vec3(0.0); + for (var i = 0; i < 12; i++) { + let angle = f32(i) * 0.5235988; + let radius = select(3.0, 7.0, (i & 1) == 1); + let offset = vec2(cos(angle), sin(angle)) * radius * step; + sum += bright(textureSampleLevel(previous, linear_sampler, input.uv + offset, 0.0).rgb); + } + return vec4(input.color.rgb + sum * (glow / 6.0), input.color.a); +} diff --git a/crates/horizon-shader-api/packs/golden_hour/post/grade.wgsl b/crates/horizon-shader-api/packs/golden_hour/post/grade.wgsl new file mode 100644 index 000000000..809b0877e --- /dev/null +++ b/crates/horizon-shader-api/packs/golden_hour/post/grade.wgsl @@ -0,0 +1,19 @@ +// Golden light when the sun is low, a soft shoulder for the brightest +// colours (white stays white), saturation and a light vignette. +override warmth: f32 = 0.4; +override saturation: f32 = 1.1; +override vignette: bool = true; + +fn post(input: PostInput) -> vec4 { + var c = input.color.rgb; + let golden = warmth * mix(0.3, 1.0, sun_low()) * daylight() * (1.0 - frame.rain * 0.7); + c *= mix(vec3(1.0), vec3(1.08, 1.0, 0.86), golden); + c = c / (1.0 + 0.25 * c) * 1.25; + c = mix(vec3(luminance(c)), c, saturation); + if vignette { + let aspect = frame.resolution.x / frame.resolution.y; + let d = length((input.uv - 0.5) * vec2(aspect, 1.0)) / length(vec2(aspect, 1.0) * 0.5); + c *= 1.0 - smoothstep(0.55, 1.1, d) * 0.3; + } + return vec4(clamp(c, vec3(0.0), vec3(1.0)), input.color.a); +} diff --git a/crates/horizon-shader-api/packs/golden_hour/sky.wgsl b/crates/horizon-shader-api/packs/golden_hour/sky.wgsl new file mode 100644 index 000000000..dbf1e0ab5 --- /dev/null +++ b/crates/horizon-shader-api/packs/golden_hour/sky.wgsl @@ -0,0 +1,14 @@ +// Warms the sky around a low sun, and deepens the blue overhead by day. +override sun_glow: f32 = 0.6; + +fn sky(input: SkyInput) -> vec4 { + let d = input.direction; + let toward = max(dot(d, frame.sun_direction), 0.0); + let near_horizon = 1.0 - smoothstep(0.0, 0.4, abs(d.y)); + let day = daylight() * (1.0 - frame.rain); + let halo = pow(toward, 6.0) * 0.6 + near_horizon * toward * toward * 0.4; + let warm = vec3(1.0, 0.55, 0.25) * halo * sun_low() * day * sun_glow; + let zenith = smoothstep(0.1, 1.0, d.y) * day * 0.12; + let base = input.color.rgb * (1.0 - zenith * vec3(0.5, 0.25, 0.0)); + return vec4(base + warm, input.color.a); +} diff --git a/crates/horizon-shader-api/src/bin/horizon-shader.rs b/crates/horizon-shader-api/src/bin/horizon-shader.rs new file mode 100644 index 000000000..b6130282d --- /dev/null +++ b/crates/horizon-shader-api/src/bin/horizon-shader.rs @@ -0,0 +1,307 @@ +//! `horizon-shader`: make Horizon shader packs (docs/shaders.md). +//! +//! - `new [--template post|sky|material] [--name "Name"]`: a pack from a template. +//! - `check ... [--json]`: the check the game, the launcher and the registry run. +//! - `pack [--out ]`: check, then write the reproducible `.hshader`. +use horizon_shader_api::{EXTENSION, MANIFEST, Pack, Problem, check, package::sha256_hex}; +use serde::Serialize; +use std::{path::Path, process::ExitCode}; + +const USAGE: &str = "usage: horizon-shader new [--template post|sky|material] [--name \"Name\"] | check ... [--json] | pack [--out ]"; + +/// (template, its files as (name, text)). +const TEMPLATES: [(&str, [(&str, &str); 2]); 3] = [ + ( + "post", + [ + ( + MANIFEST, + include_str!("../../templates/post/horizon-shaders.toml"), + ), + ("post.wgsl", include_str!("../../templates/post/post.wgsl")), + ], + ), + ( + "sky", + [ + ( + MANIFEST, + include_str!("../../templates/sky/horizon-shaders.toml"), + ), + ("sky.wgsl", include_str!("../../templates/sky/sky.wgsl")), + ], + ), + ( + "material", + [ + ( + MANIFEST, + include_str!("../../templates/material/horizon-shaders.toml"), + ), + ( + "material.wgsl", + include_str!("../../templates/material/material.wgsl"), + ), + ], + ), +]; + +fn main() -> ExitCode { + let args: Vec = std::env::args().skip(1).collect(); + let result = match args.first().map(String::as_str) { + Some("new") => new(&args[1..]), + Some("check") => check_command(&args[1..]), + Some("pack") => pack(&args[1..]), + _ => Err(USAGE.into()), + }; + match result { + Ok(()) => ExitCode::SUCCESS, + Err(e) => { + eprintln!("error: {e}"); + ExitCode::FAILURE + } + } +} + +/// The value after `flag` in `args`. +fn flag<'a>(args: &'a [String], flag: &str) -> Option<&'a str> { + let i = args.iter().position(|a| a == flag)?; + args.get(i + 1).map(String::as_str) +} + +/// The arguments that are neither flags nor their values. +fn positional(args: &[String], flags: &[&str]) -> Vec { + let mut out = Vec::new(); + let mut it = args.iter(); + while let Some(a) = it.next() { + if flags.contains(&a.as_str()) { + it.next(); + } else if !a.starts_with("--") { + out.push(a.clone()); + } + } + out +} + +fn new(args: &[String]) -> Result<(), String> { + let [dir] = positional(args, &["--template", "--name"]) + .try_into() + .map_err(|_| USAGE.to_string())?; + let dir = Path::new(&dir); + if dir.exists() { + return Err(format!("{} already exists", dir.display())); + } + let template = flag(args, "--template").unwrap_or("post"); + let (_, files) = TEMPLATES + .iter() + .find(|(name, _)| *name == template) + .ok_or_else(|| format!("no template {template:?}: post, sky or material"))?; + let base = dir + .file_name() + .and_then(|n| n.to_str()) + .ok_or("the path needs a folder name")?; + let mut id: String = base + .chars() + .map(|c| match c { + 'a'..='z' | '0'..='9' => c, + 'A'..='Z' => c.to_ascii_lowercase(), + _ => '_', + }) + .collect(); + id.truncate(64); + let name = flag(args, "--name").map_or_else(|| base.replace(['-', '_'], " "), str::to_owned); + std::fs::create_dir_all(dir).map_err(|e| format!("{}: {e}", dir.display()))?; + for (file, text) in files { + let text = text + .replace("{{id}}", &id) + .replace("{{name}}", &name.replace('"', "'")); + let path = dir.join(file); + std::fs::write(&path, text).map_err(|e| format!("{}: {e}", path.display()))?; + } + println!( + "Made {} from the {template} template. Edit it, then: horizon-shader check {}", + dir.display(), + dir.display() + ); + Ok(()) +} + +#[derive(Serialize)] +struct StageJson { + stage: &'static str, + file: String, + label: String, + expressions: usize, + texture_reads: usize, +} + +#[derive(Serialize)] +struct ProblemJson { + file: String, + line: usize, + column: usize, + message: String, +} + +#[derive(Serialize)] +struct CheckJson { + path: String, + ok: bool, + id: Option, + version: Option, + api: Option, + /// The file's own SHA-256 (a `.hshader`), else the reproducible package's. + sha256: Option, + stages: Vec, + problems: Vec, +} + +/// Reads and checks the pack at `path`. +fn check_one(path: &Path) -> CheckJson { + let mut out = CheckJson { + path: path.display().to_string(), + ok: false, + id: None, + version: None, + api: None, + sha256: None, + stages: Vec::new(), + problems: Vec::new(), + }; + let problem = |message: String| ProblemJson { + file: path.display().to_string(), + line: 0, + column: 0, + message, + }; + let pack = match Pack::read(path) { + Ok(p) => p, + Err(e) => { + out.problems.push(problem(e)); + return out; + } + }; + out.id = Some(pack.manifest.id.clone()); + out.version = Some(pack.manifest.version.clone()); + out.api = Some(pack.manifest.api.clone()); + out.sha256 = if path.is_dir() { + pack.sha256().ok() + } else { + std::fs::read(path).ok().map(|b| sha256_hex(&b)) + }; + match check(&pack) { + Ok(report) => { + out.ok = true; + out.stages = report + .stages + .into_iter() + .map(|s| StageJson { + stage: s.kind.name(), + file: s.file, + label: s.label, + expressions: s.expressions, + texture_reads: s.texture_reads, + }) + .collect(); + } + Err(problems) => { + out.problems = problems + .into_iter() + .map( + |Problem { + file, + line, + column, + message, + }| ProblemJson { + file, + line, + column, + message, + }, + ) + .collect(); + } + } + out +} + +fn check_command(args: &[String]) -> Result<(), String> { + let paths = positional(args, &[]); + if paths.is_empty() { + return Err(USAGE.into()); + } + let results: Vec = paths.iter().map(|p| check_one(Path::new(p))).collect(); + let failed = results.iter().filter(|r| !r.ok).count(); + if args.iter().any(|a| a == "--json") { + let json = serde_json::to_string_pretty(&results).map_err(|e| e.to_string())?; + println!("{json}"); + } else { + for r in &results { + print_result(r); + } + } + match failed { + 0 => Ok(()), + n => Err(format!("{n} of {} packs failed the check", results.len())), + } +} + +fn print_result(r: &CheckJson) { + let what = match (&r.id, &r.version, &r.api) { + (Some(id), Some(v), Some(api)) => format!("{id} {v} (shader API {api})"), + _ => r.path.clone(), + }; + if !r.ok { + println!("{what}: FAILED"); + for p in &r.problems { + if p.line == 0 { + println!(" {}: {}", p.file, p.message); + } else { + println!(" {}:{}:{}: {}", p.file, p.line, p.column, p.message); + } + } + return; + } + println!("{what}: ok"); + for s in &r.stages { + println!( + " {:<9} {:<28} {:>6} expressions, {} texture reads", + s.stage, s.label, s.expressions, s.texture_reads + ); + } + if let Some(sha) = &r.sha256 { + println!(" sha256 {sha}"); + } +} + +fn pack(args: &[String]) -> Result<(), String> { + let [dir] = positional(args, &["--out"]) + .try_into() + .map_err(|_| USAGE.to_string())?; + let dir = Path::new(&dir) + .canonicalize() + .map_err(|e| format!("{dir}: {e}"))?; + if !dir.is_dir() { + return Err(format!("{} is not a pack folder", dir.display())); + } + let dir = dir.as_path(); + let pack = Pack::read(dir)?; + if let Err(problems) = check(&pack) { + for p in &problems { + eprintln!("{p}"); + } + return Err(format!("{} failed the check", dir.display())); + } + let bytes = pack.package()?; + let out = flag(args, "--out").map_or_else( + || { + let m = &pack.manifest; + dir.with_file_name(format!("{}-{}.{EXTENSION}", m.id, m.version)) + }, + Into::into, + ); + std::fs::write(&out, &bytes).map_err(|e| format!("{}: {e}", out.display()))?; + println!("Wrote {} (sha256 {})", out.display(), sha256_hex(&bytes)); + Ok(()) +} diff --git a/crates/horizon-shader-api/src/check.rs b/crates/horizon-shader-api/src/check.rs new file mode 100644 index 000000000..6c43718f7 --- /dev/null +++ b/crates/horizon-shader-api/src/check.rs @@ -0,0 +1,495 @@ +//! The check a pack passes before it runs: in the game when it loads (a pack +//! that fails keeps the last good one drawing), in the launcher before it +//! installs, in the registry's CI, and in `horizon-shader check`. +//! +//! Each stage is built as the renderer builds it, parsed and validated by +//! naga, checked against its signature and the API's rules (no bindings, no +//! entry points, no `horizon_` names), then translated to SPIR-V, Metal and +//! HLSL with its options at their defaults, minimums and maximums. +use crate::manifest::{OptionKind, PackOption}; +use crate::package::Pack; +use crate::stages::{Composed, StageKind, compose}; +use naga::valid::{Capabilities, ModuleInfo, ValidationFlags, Validator}; +use std::collections::HashSet; +use std::fmt; + +/// naga expressions a stage may hold, over all its functions. +pub const MAX_EXPRESSIONS: usize = 20_000; +/// Texture reads (samples, gathers and loads) a stage may write. +pub const MAX_TEXTURE_READS: usize = 64; + +/// What is wrong, and where: a file of the pack, with the line and column +/// when known (0 when not). +#[derive(Clone, Debug, PartialEq)] +pub struct Problem { + pub file: String, + pub line: usize, + pub column: usize, + pub message: String, +} + +impl fmt::Display for Problem { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if self.line == 0 { + write!(f, "{}: {}", self.file, self.message) + } else { + write!( + f, + "{}:{}:{}: {}", + self.file, self.line, self.column, self.message + ) + } + } +} + +fn problem(file: &str, message: impl Into) -> Problem { + Problem { + file: file.to_owned(), + line: 0, + column: 0, + message: message.into(), + } +} + +/// One stage that passed, ready for the renderer. +#[derive(Debug)] +pub struct CheckedStage { + pub kind: StageKind, + pub file: String, + /// The manifest's name for it, else its file. + pub label: String, + pub module: naga::Module, + pub info: ModuleInfo, + pub expressions: usize, + pub texture_reads: usize, +} + +#[derive(Debug)] +pub struct Report { + pub stages: Vec, +} + +/// Checks every stage of `pack`, and that each option is an `override` of +/// its type in some stage. Returns every problem found, not just the first. +pub fn check(pack: &Pack) -> Result> { + let m = &pack.manifest; + let stages = m + .sky + .iter() + .map(|s| (StageKind::Sky, s)) + .chain(m.material.iter().map(|s| (StageKind::Material, s))) + .chain(m.post.iter().map(|s| (StageKind::Post, s))); + let (mut checked, mut problems) = (Vec::new(), Vec::new()); + for (kind, stage) in stages { + match check_stage(pack, kind, &stage.file) { + Ok(mut c) => { + c.label = stage.name.clone().unwrap_or_else(|| stage.file.clone()); + checked.push(c); + } + Err(p) => problems.extend(p), + } + } + if problems.is_empty() { + for option in &m.options { + let used = checked.iter().any(|c| { + c.module + .overrides + .iter() + .any(|(_, o)| o.name.as_deref() == Some(&option.id)) + }); + if !used { + problems.push(problem( + crate::MANIFEST, + format!( + "option {:?}: no stage declares `override {}: {}`", + option.id, + option.id, + option.kind.wgsl_type() + ), + )); + } + } + } + if problems.is_empty() { + Ok(Report { stages: checked }) + } else { + Err(problems) + } +} + +/// Builds and checks one stage of `pack` from its file. +pub fn check_stage(pack: &Pack, kind: StageKind, file: &str) -> Result> { + // First without the entry points, so a missing or mistyped stage function + // is reported as such rather than as a call that doesn't resolve. + let mut own = compose(pack, kind, file); + let entries = own.parts.pop().expect("entries are the last part"); + own.text.truncate(entries.1.start); + let module = parse(&own)?; + let mut problems = signature(&module, kind, file); + problems.extend(api_rules(&module, kind, &own)); + problems.extend(options(&module, &pack.manifest.options, file)); + if !problems.is_empty() { + return Err(problems); + } + let composed = compose(pack, kind, file); + let module = parse(&composed)?; + let info = Validator::new(ValidationFlags::all(), Capabilities::default()) + .validate(&module) + .map_err(|e| { + let at = e.spans().next().and_then(|(span, _)| span.to_range()); + let mut message = e.as_inner().to_string(); + let mut source = std::error::Error::source(e.as_inner()); + while let Some(s) = source { + message += &format!(": {s}"); + source = s.source(); + } + vec![located(&composed, at.map(|r| r.start), message)] + })?; + let (expressions, texture_reads) = cost(&module); + let mut problems = Vec::new(); + if expressions > MAX_EXPRESSIONS { + problems.push(problem( + file, + format!("{expressions} expressions; a stage may hold {MAX_EXPRESSIONS}"), + )); + } + if texture_reads > MAX_TEXTURE_READS { + problems.push(problem( + file, + format!("{texture_reads} texture reads; a stage may write {MAX_TEXTURE_READS}"), + )); + } + problems.extend(translate(&module, &info, &pack.manifest.options, file)); + if !problems.is_empty() { + return Err(problems); + } + Ok(CheckedStage { + kind, + file: file.to_owned(), + label: file.to_owned(), + module, + info, + expressions, + texture_reads, + }) +} + +fn located(composed: &Composed, offset: Option, message: String) -> Problem { + match offset { + Some(at) => { + let (file, line, column) = composed.locate(at); + Problem { + file: file.to_owned(), + line, + column, + message, + } + } + None => problem(&composed.parts[composed.parts.len() - 2].0, message), + } +} + +fn parse(composed: &Composed) -> Result> { + naga::front::wgsl::parse_str(&composed.text).map_err(|e| { + let label = e.labels().next(); + let mut message = e.message().to_owned(); + if let Some((_, text)) = label.filter(|(_, t)| !t.is_empty() && !message.contains(*t)) { + message += &format!(" ({text})"); + } + let at = label.and_then(|(span, _)| span.to_range()).map(|r| r.start); + vec![located(composed, at, message)] + }) +} + +fn type_name(module: &naga::Module, ty: naga::Handle) -> String { + let t = &module.types[ty]; + if let Some(name) = &t.name { + return name.clone(); + } + match t.inner { + naga::TypeInner::Scalar(s) => scalar_name(s).to_owned(), + naga::TypeInner::Vector { size, scalar } => { + format!("vec{}<{}>", size as u8, scalar_name(scalar)) + } + _ => format!("{:?}", t.inner), + } +} + +fn scalar_name(s: naga::Scalar) -> &'static str { + match (s.kind, s.width) { + (naga::ScalarKind::Bool, _) => "bool", + (naga::ScalarKind::Float, 4) => "f32", + (naga::ScalarKind::Sint, 4) => "i32", + (naga::ScalarKind::Uint, 4) => "u32", + _ => "another scalar", + } +} + +fn signature(module: &naga::Module, kind: StageKind, file: &str) -> Vec { + let found = module + .functions + .iter() + .find(|(_, f)| f.name.as_deref() == Some(kind.function())); + let Some((_, f)) = found else { + return vec![problem(file, format!("defines no `{}`", kind.signature()))]; + }; + let args: Vec = f + .arguments + .iter() + .map(|a| type_name(module, a.ty)) + .collect(); + let result = f.result.as_ref().map(|r| type_name(module, r.ty)); + if args == [kind.input().name] && result.as_deref() == Some(kind.output()) { + return Vec::new(); + } + vec![problem( + file, + format!( + "`fn {}({}) -> {}` should be `{}`", + kind.function(), + args.join(", "), + result.as_deref().unwrap_or("()"), + kind.signature() + ), + )] +} + +/// The names of everything a module defines at its top level. +fn names(module: &naga::Module) -> HashSet { + let types = module.types.iter().filter_map(|(_, t)| t.name.clone()); + let functions = module.functions.iter().filter_map(|(_, f)| f.name.clone()); + let globals = module + .global_variables + .iter() + .filter_map(|(_, g)| g.name.clone()); + let constants = module.constants.iter().filter_map(|(_, c)| c.name.clone()); + let overrides = module.overrides.iter().filter_map(|(_, o)| o.name.clone()); + types + .chain(functions) + .chain(globals) + .chain(constants) + .chain(overrides) + .collect() +} + +/// A pack's files declare no bindings, no entry points and no `horizon_` +/// names: what the prelude doesn't define is the pack's. +fn api_rules(module: &naga::Module, kind: StageKind, own: &Composed) -> Vec { + let prelude = naga::front::wgsl::parse_str(&kind.prelude()).expect("the prelude parses"); + let ours = names(&prelude); + let file = &own.parts[own.parts.len() - 1].0; + let mut problems = Vec::new(); + for e in &module.entry_points { + problems.push(problem( + file, + format!( + "declares the entry point `{}`: a stage is a function, Horizon adds the entry points", + e.name + ), + )); + } + for (_, g) in module.global_variables.iter() { + let name = g.name.clone().unwrap_or_default(); + if g.binding.is_some() && !ours.contains(&name) { + problems.push(problem( + file, + format!( + "declares the binding `{name}`: a stage reads only the resources of its prelude" + ), + )); + } + } + let mut theirs: Vec = names(module) + .into_iter() + .filter(|n| !ours.contains(n) && n.starts_with("horizon_")) + .collect(); + theirs.sort(); + for name in theirs { + problems.push(problem( + file, + format!("`{name}`: names starting with horizon_ are Horizon's"), + )); + } + problems +} + +/// Each `override` is an option of its type, or has a default of its own. +fn options(module: &naga::Module, options: &[PackOption], file: &str) -> Vec { + let mut problems = Vec::new(); + for (_, o) in module.overrides.iter() { + let name = o.name.as_deref().unwrap_or("?"); + let ty = type_name(module, o.ty); + match options.iter().find(|opt| opt.id == name) { + Some(opt) if ty != opt.kind.wgsl_type() => problems.push(problem( + file, + format!( + "`override {name}: {ty}`: option {name:?} is a {}, so its override is `{}`", + kind_name(&opt.kind), + opt.kind.wgsl_type() + ), + )), + None if o.init.is_none() => problems.push(problem( + file, + format!("`override {name}` has no default and no [[options]] entry"), + )), + _ => {} + } + } + problems +} + +fn kind_name(kind: &OptionKind) -> &'static str { + match kind { + OptionKind::Bool { .. } => "bool", + OptionKind::Float { .. } => "float", + OptionKind::Int { .. } => "int", + OptionKind::Choice { .. } => "choice", + } +} + +fn cost(module: &naga::Module) -> (usize, usize) { + let functions = module + .functions + .iter() + .map(|(_, f)| f) + .chain(module.entry_points.iter().map(|e| &e.function)); + let (mut expressions, mut reads) = (0, 0); + for f in functions { + expressions += f.expressions.len(); + reads += f + .expressions + .iter() + .filter(|(_, e)| { + matches!( + e, + naga::Expression::ImageSample { .. } | naga::Expression::ImageLoad { .. } + ) + }) + .count(); + } + (expressions, reads) +} + +/// The option values a pack is translated with: its defaults, then every +/// option at its minimum, then at its maximum. +fn option_sets(options: &[PackOption]) -> [(&'static str, Vec<(String, f64)>); 3] { + let at = |pick: fn(&OptionKind) -> f64| { + options + .iter() + .map(|o| (o.id.clone(), pick(&o.kind))) + .collect() + }; + [ + ("defaults", at(OptionKind::default_constant)), + ( + "minimums", + at(|k| match *k { + OptionKind::Bool { .. } | OptionKind::Choice { .. } => 0.0, + OptionKind::Float { min, .. } => f64::from(min), + OptionKind::Int { min, .. } => f64::from(min), + }), + ), + ( + "maximums", + at(|k| match k { + OptionKind::Bool { .. } => 1.0, + OptionKind::Choice { values, .. } => (values.len() - 1) as f64, + OptionKind::Float { max, .. } => f64::from(*max), + OptionKind::Int { max, .. } => f64::from(*max), + }), + ), + ] +} + +/// Translates every entry point for Vulkan (SPIR-V), Metal and DirectX 12 +/// (HLSL) with each option set, as wgpu does when it builds the pipelines. +fn translate( + module: &naga::Module, + info: &ModuleInfo, + options: &[PackOption], + file: &str, +) -> Vec { + let present: HashSet<&str> = module + .overrides + .iter() + .filter_map(|(_, o)| o.name.as_deref()) + .collect(); + let mut problems = Vec::new(); + for (set, values) in option_sets(options) { + let mut constants = naga::back::PipelineConstants::default(); + for (id, value) in values + .into_iter() + .filter(|(id, _)| present.contains(id.as_str())) + { + constants.insert(id, value); + } + for e in &module.entry_points { + let fail = |backend: &str, err: String| { + problem( + file, + format!("with the options at their {set}, {backend} refuses it: {err}"), + ) + }; + let processed = naga::back::pipeline_constants::process_overrides( + module, + info, + Some((e.stage, &e.name)), + &constants, + ); + let (m, i) = match processed { + Ok(p) => p, + Err(err) => { + problems.push(fail("the override step", err.to_string())); + return problems; + } + }; + let spv = naga::back::spv::write_vec( + &m, + &i, + &naga::back::spv::Options::default(), + Some(&naga::back::spv::PipelineOptions { + shader_stage: e.stage, + entry_point: e.name.clone(), + }), + ); + let msl = naga::back::msl::write_string( + &m, + &i, + &naga::back::msl::Options { + lang_version: (2, 2), + ..Default::default() + }, + &naga::back::msl::PipelineOptions { + entry_point: Some((e.stage, e.name.clone())), + ..Default::default() + }, + ); + let mut hlsl = String::new(); + let hlsl_result = naga::back::hlsl::Writer::new( + &mut hlsl, + &naga::back::hlsl::Options::default(), + &naga::back::hlsl::PipelineOptions { + entry_point: Some((e.stage, e.name.clone())), + }, + ) + .write(&m, &i, None); + let errors = [ + ("Vulkan (SPIR-V)", spv.err().map(|e| e.to_string())), + ("Metal", msl.err().map(|e| e.to_string())), + ( + "DirectX 12 (HLSL)", + hlsl_result.err().map(|e| e.to_string()), + ), + ]; + for (backend, err) in errors { + if let Some(err) = err { + problems.push(fail(backend, err)); + } + } + if !problems.is_empty() { + return problems; + } + } + } + problems +} diff --git a/crates/horizon-shader-api/src/inputs.rs b/crates/horizon-shader-api/src/inputs.rs new file mode 100644 index 000000000..5bc82fd82 --- /dev/null +++ b/crates/horizon-shader-api/src/inputs.rs @@ -0,0 +1,218 @@ +//! What a pack's WGSL reads: the frame uniform every stage sees, which the +//! renderer fills and uploads as the Rust struct below, and the stage input +//! and output structs. The WGSL of each is generated from these definitions +//! (`Struct::wgsl`), and so is the reference (docs/api/shaders-reference.md). + +/// One field of a WGSL struct, for the prelude and the reference. +#[derive(Debug)] +pub struct Field { + pub name: &'static str, + pub ty: &'static str, + /// Its meaning, unit and range. + pub doc: &'static str, +} + +/// A struct a pack's WGSL can name. +#[derive(Debug)] +pub struct Struct { + pub name: &'static str, + pub doc: &'static str, + pub fields: &'static [Field], +} + +impl Struct { + pub fn wgsl(&self) -> String { + let mut out = format!("struct {} {{\n", self.name); + for f in self.fields { + out += &format!(" {}: {},\n", f.name, f.ty); + } + out + "}\n" + } +} + +/// The WGSL type of a Rust field the renderer uploads. +pub trait Wgsl { + const TYPE: &'static str; +} + +macro_rules! wgsl_types { + ($($rust:ty => $wgsl:literal),* $(,)?) => { + $(impl Wgsl for $rust { + const TYPE: &'static str = $wgsl; + })* + }; +} + +wgsl_types! { + f32 => "f32", + u32 => "u32", + i32 => "i32", + [f32; 2] => "vec2", + [f32; 3] => "vec3", + [f32; 4] => "vec4", + [[f32; 4]; 4] => "mat4x4", +} + +/// A uniform struct: the Rust type the renderer uploads, and its WGSL. +macro_rules! uniform { + ($(#[doc = $doc:literal])* $name:ident { + $($(#[doc = $fdoc:literal])* $field:ident: $ty:ty,)* + }) => { + $(#[doc = $doc])* + #[repr(C)] + #[derive(Clone, Copy, Debug, Default, PartialEq, bytemuck::Pod, bytemuck::Zeroable)] + pub struct $name { + $($(#[doc = $fdoc])* pub $field: $ty,)* + } + + impl $name { + pub const WGSL: Struct = Struct { + name: stringify!($name), + doc: concat!($($doc, "\n"),*), + fields: &[$(Field { + name: stringify!($field), + ty: <$ty as Wgsl>::TYPE, + doc: concat!($($fdoc),*), + }),*], + }; + /// Each field's byte offset, in `WGSL.fields`' order. + pub const OFFSETS: &'static [usize] = &[$(std::mem::offset_of!($name, $field)),*]; + } + }; +} + +uniform! { + /// The frame, as `frame` in every stage. Positions and directions are + /// in world axes relative to the camera (x east, y up, z south), so they + /// stay precise far from the world's origin; depth is reversed (1 near, + /// 0 at the far plane and in the sky). + Frame { + /// Camera-relative world to view space: the camera's rotation. + view: [[f32; 4]; 4], + /// View to clip space (reversed depth, wgpu's clip space: z 0..1). + projection: [[f32; 4]; 4], + /// View to camera-relative world space. + view_inverse: [[f32; 4]; 4], + /// Clip to view space. + projection_inverse: [[f32; 4]; 4], + /// The camera's world position, in blocks (about a millimetre of precision 10 000 blocks out). + camera_position: [f32; 3], + /// Seconds since the pack was loaded, wrapping to 0 every hour. + time: f32, + /// Unit vector toward the sun (below the horizon at night). + sun_direction: [f32; 3], + /// The time of day, 0..1: 0 sunrise, 0.25 noon, 0.5 sunset, 0.75 midnight. + day_time: f32, + /// Unit vector toward the moon. + moon_direction: [f32; 3], + /// Vanilla's GameTime: (game time % 24000 + partial tick) / 24000, 0..1. + game_time: f32, + /// Vanilla's sky colour this frame, 0..1. + sky_color: [f32; 3], + /// Rain strength, 0..1. + rain: f32, + /// Vanilla's fog colour this frame (also the clear colour), 0..1. + fog_color: [f32; 3], + /// Thunder strength, 0..1. + thunder: f32, + /// The size of the world's picture, in pixels (the resolution scale applied). + resolution: [f32; 2], + /// Block and sky light at the camera, 0..1 (level / 15). + eye_light: [f32; 2], + /// Where vanilla's fog starts, in blocks from the camera. + fog_start: f32, + /// Where vanilla's fog is full, in blocks. + fog_end: f32, + /// The render distance, in blocks. + render_distance: f32, + /// The far plane, in blocks. + far: f32, + /// Night vision strength, 0..1. + night_vision: f32, + /// Blindness strength, 0..1. + blindness: f32, + /// The darkness effect's strength, 0..1. + darkness: f32, + /// The Brightness option, 0..1 (Moody to Bright). + brightness: f32, + /// What the camera is in: 0 air, 1 water, 2 lava, 3 powder snow. + eye_in: u32, + /// 0 the Overworld, -1 the Nether, 1 the End, 2 any other dimension. + dimension: i32, + /// Frames drawn since the pack was loaded (wraps). + frame_index: u32, + /// 1 while F1 hides the HUD, else 0. + hide_gui: u32, + } +} + +macro_rules! fields { + ($($name:ident: $ty:literal = $doc:literal,)*) => { + &[$(Field { name: stringify!($name), ty: $ty, doc: $doc }),*] + }; +} + +/// The post stage's input: one pixel of the picture. +pub const POST_INPUT: Struct = Struct { + name: "PostInput", + doc: "One pixel of the picture, for `fn post(input: PostInput) -> vec4`.", + fields: fields! { + uv: "vec2" = "Where the pixel is, 0..1 from the top left.", + pixel: "vec2" = "The pixel's centre, in pixels from the top left.", + color: "vec4" = "The previous pass's colour here (the world's picture for the first pass).", + depth: "f32" = "The world's depth here, reversed: 0 is the sky.", + }, +}; + +/// The material stage's input: one point of a block's surface. +pub const MATERIAL_INPUT: Struct = Struct { + name: "MaterialInput", + doc: "One point of a block's surface, for `fn material(input: MaterialInput) -> Surface`.", + fields: fields! { + albedo: "vec4" = "The block texture's colour here (alpha 0..1).", + tint: "vec4" = "The vertex colour: biome tint, face shade and ambient occlusion.", + light: "vec2" = "Block and sky light, 0..1, smoothed over the face.", + lightmap: "vec3" = "Vanilla's light colour for that light this frame.", + position: "vec3" = "The point, camera-relative, in blocks.", + uv: "vec2" = "The texture coordinate inside the block atlas.", + translucent: "bool" = "Drawn in the translucent pass (water, stained glass, ice).", + }, +}; + +/// What the material stage returns. +pub const SURFACE: Struct = Struct { + name: "Surface", + doc: "What `material` returns. Vanilla's own is `albedo * tint * vec4(lightmap, 1.0)`; \ + Horizon then applies the alpha test (cutout and translucent) and fog.", + fields: fields! { + color: "vec4" = "The lit colour, 0..1, with its alpha.", + }, +}; + +/// The sky stage's input: one direction of the sky. +pub const SKY_INPUT: Struct = Struct { + name: "SkyInput", + doc: "One direction of the sky, for `fn sky(input: SkyInput) -> vec4`. The sun, moon, \ + stars and clouds are drawn over the result.", + fields: fields! { + direction: "vec3" = "Unit view ray, camera-relative world axes.", + color: "vec4" = "Vanilla's sky colour along the ray (sky, fog and sunrise).", + }, +}; + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn frame_fields_carry_their_docs() { + let wgsl = Frame::WGSL; + assert_eq!(wgsl.fields.len(), 28); + assert!(wgsl.fields.iter().all(|f| !f.doc.trim().is_empty())); + assert_eq!(std::mem::size_of::(), 4 * 64 + 9 * 16); + assert!( + wgsl.wgsl() + .contains(" camera_position: vec3,\n time: f32,\n") + ); + } +} diff --git a/crates/horizon-shader-api/src/lib.rs b/crates/horizon-shader-api/src/lib.rs new file mode 100644 index 000000000..ff676f64f --- /dev/null +++ b/crates/horizon-shader-api/src/lib.rs @@ -0,0 +1,17 @@ +//! Horizon's own shader packs (docs/api/shaders.md): WGSL written against +//! the renderer's stages, described by `horizon-shaders.toml` and shipped as +//! a folder or a `.hshader`. This crate reads and checks packs; the game, the +//! launcher and the registry all use it, so a pack is judged the same way +//! everywhere. +pub mod check; +pub mod inputs; +pub mod manifest; +pub mod package; +pub mod reference; +pub mod stages; + +pub use check::{Problem, Report, check}; +pub use inputs::Frame; +pub use manifest::{API, MANIFEST, Manifest, OptionKind, PackOption, SCHEMA, Stage}; +pub use package::{EXTENSION, Pack}; +pub use stages::{Composed, StageKind, compose}; diff --git a/crates/horizon-shader-api/src/manifest.rs b/crates/horizon-shader-api/src/manifest.rs new file mode 100644 index 000000000..d03a4f213 --- /dev/null +++ b/crates/horizon-shader-api/src/manifest.rs @@ -0,0 +1,365 @@ +//! `horizon-shaders.toml`, a shader pack's manifest (schema 1, docs/shaders.md). +use serde::Deserialize; + +/// The manifest's file name at the root of a pack. +pub const MANIFEST: &str = "horizon-shaders.toml"; +/// The manifest schema this build reads. +pub const SCHEMA: u32 = 1; +/// The shader API line this build serves: the stages, their signatures and +/// the inputs a pack's WGSL may read. +pub const API: &str = "0.1"; +/// Full-screen passes a pack may chain. +pub const MAX_POST_PASSES: usize = 8; +/// Options a pack may declare. +pub const MAX_OPTIONS: usize = 64; +/// The most GPU time a pack may declare for itself, in milliseconds. +pub const MAX_GPU_MS: f32 = 16.0; + +/// A checked manifest. +#[derive(Clone, Debug, PartialEq)] +pub struct Manifest { + /// `[a-z0-9_]{1,64}`: the pack's settings key and registry id. + pub id: String, + pub name: String, + pub version: String, + /// The API line the pack is written for. + pub api: String, + pub description: String, + pub authors: Vec, + pub license: Option, + /// WGSL files put before every stage's own file, in order. + pub shared: Vec, + pub sky: Option, + pub material: Option, + /// Full-screen passes, in the order they run. + pub post: Vec, + pub options: Vec, + pub budget: Budget, +} + +/// One stage: the WGSL file that defines its function. +#[derive(Clone, Debug, Deserialize, PartialEq)] +#[serde(deny_unknown_fields)] +pub struct Stage { + pub file: String, + /// Shown in the GPU time overlay; the file name when absent. + #[serde(default)] + pub name: Option, +} + +/// An option the player can change: a WGSL `override` constant of the +/// same name, set when the pipelines are built. +#[derive(Clone, Debug, PartialEq)] +pub struct PackOption { + /// The `override`'s name in the pack's WGSL. + pub id: String, + pub name: String, + pub description: String, + pub kind: OptionKind, +} + +#[derive(Clone, Debug, PartialEq)] +pub enum OptionKind { + /// `override : bool`. + Bool { default: bool }, + /// `override : f32`; the slider moves by `step`. + Float { + default: f32, + min: f32, + max: f32, + step: f32, + }, + /// `override : i32`. + Int { default: i32, min: i32, max: i32 }, + /// `override : u32`, the index of the chosen value. + Choice { values: Vec, default: u32 }, +} + +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Budget { + /// The GPU time the pack aims to add to a frame, in milliseconds; the + /// overlay shows the pack over budget past it. + pub gpu_ms: Option, +} + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct Raw { + schema: u32, + id: String, + name: String, + version: String, + api: String, + #[serde(default)] + description: String, + #[serde(default)] + authors: Vec, + license: Option, + #[serde(default)] + shared: Vec, + sky: Option, + material: Option, + #[serde(default)] + post: Vec, + #[serde(default)] + options: Vec, + #[serde(default)] + budget: RawBudget, +} + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct RawOption { + id: String, + name: String, + #[serde(default)] + description: String, + #[serde(rename = "type")] + kind: String, + default: toml::Value, + min: Option, + max: Option, + step: Option, + values: Option>, +} + +#[derive(Default, Deserialize)] +#[serde(deny_unknown_fields)] +struct RawBudget { + gpu_ms: Option, +} + +impl Manifest { + pub fn parse(text: &str) -> Result { + let raw: Raw = toml::from_str(text).map_err(|e| format!("{MANIFEST}: {e}"))?; + let err = |e: String| format!("{MANIFEST}: {e}"); + if raw.schema != SCHEMA { + return Err(err(format!( + "schema {}; this Horizon reads {SCHEMA}", + raw.schema + ))); + } + if raw.api != API { + return Err(err(format!( + "written for shader API {}; this Horizon serves {API}", + raw.api + ))); + } + let id_ok = (1..=64).contains(&raw.id.len()) + && raw + .id + .bytes() + .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'_'); + if !id_ok { + return Err(err(format!("id {:?} is not [a-z0-9_]{{1,64}}", raw.id))); + } + if raw.name.trim().is_empty() || raw.version.trim().is_empty() { + return Err(err("name and version can't be empty".into())); + } + if raw.sky.is_none() && raw.material.is_none() && raw.post.is_empty() { + return Err(err( + "no stage: give it a [sky], a [material] or a [[post]]".into() + )); + } + if raw.post.len() > MAX_POST_PASSES { + return Err(err(format!( + "{} post passes; a pack may chain {MAX_POST_PASSES}", + raw.post.len() + ))); + } + let stages = raw.sky.iter().chain(&raw.material).chain(&raw.post); + for file in raw.shared.iter().chain(stages.map(|s| &s.file)) { + if !crate::package::plain_path(file) || !file.ends_with(".wgsl") { + return Err(err(format!("{file:?} is not a plain path to a .wgsl file"))); + } + } + if raw.options.len() > MAX_OPTIONS { + return Err(err(format!( + "{} options; a pack may declare {MAX_OPTIONS}", + raw.options.len() + ))); + } + let mut options = Vec::with_capacity(raw.options.len()); + for (i, o) in raw.options.into_iter().enumerate() { + let id = o.id.clone(); + let option = o.check().map_err(|e| err(format!("option {id:?}: {e}")))?; + if options[..i].iter().any(|p: &PackOption| p.id == option.id) { + return Err(err(format!("option {id:?} is declared twice"))); + } + options.push(option); + } + let gpu_ms = match raw.budget.gpu_ms { + Some(ms) if !(ms > 0.0 && ms <= f64::from(MAX_GPU_MS)) => { + return Err(err(format!( + "budget.gpu_ms {ms} is not in (0, {MAX_GPU_MS}]" + ))); + } + ms => ms.map(|ms| ms as f32), + }; + Ok(Manifest { + id: raw.id, + name: raw.name, + version: raw.version, + api: raw.api, + description: raw.description, + authors: raw.authors, + license: raw.license, + shared: raw.shared, + sky: raw.sky, + material: raw.material, + post: raw.post, + options, + budget: Budget { gpu_ms }, + }) + } + + /// Every WGSL file the manifest names, shared files first, each once. + pub fn files(&self) -> Vec<&str> { + let mut files: Vec<&str> = Vec::new(); + let stages = self.sky.iter().chain(&self.material).chain(&self.post); + for file in self.shared.iter().chain(stages.map(|s| &s.file)) { + if !files.contains(&file.as_str()) { + files.push(file); + } + } + files + } +} + +/// A WGSL identifier the renderer leaves to packs: ASCII, not starting +/// with a digit, `_` or the renderer's own `horizon_` prefix. +pub fn pack_identifier(name: &str) -> bool { + let mut bytes = name.bytes(); + bytes.next().is_some_and(|b| b.is_ascii_alphabetic()) + && bytes.all(|b| b.is_ascii_alphanumeric() || b == b'_') + && name.len() <= 64 + && !name.starts_with("horizon_") +} + +impl RawOption { + fn check(self) -> Result { + if !pack_identifier(&self.id) { + return Err( + "the id must be a WGSL identifier: a letter, then letters, digits or _, up to 64, not starting with horizon_".into(), + ); + } + let int = |v: &toml::Value| { + v.as_integer() + .and_then(|i| i32::try_from(i).ok()) + .ok_or_else(|| format!("{v} is not a 32-bit integer")) + }; + let float = |v: &toml::Value| { + v.as_float() + .or_else(|| v.as_integer().map(|i| i as f64)) + .filter(|f| f.is_finite()) + .map(|f| f as f32) + .ok_or_else(|| format!("{v} is not a number")) + }; + let need = |v: Option, key: &str| v.ok_or_else(|| format!("needs {key}")); + let unused = |present: bool, key: &str| { + if present { + Err(format!("{key} doesn't apply to type = {:?}", self.kind)) + } else { + Ok(()) + } + }; + let kind = match self.kind.as_str() { + "bool" => { + unused(self.min.is_some() || self.max.is_some(), "min/max")?; + unused(self.step.is_some(), "step")?; + unused(self.values.is_some(), "values")?; + let default = self + .default + .as_bool() + .ok_or("default must be true or false")?; + OptionKind::Bool { default } + } + "float" => { + unused(self.values.is_some(), "values")?; + let (min, max) = ( + float(&need(self.min, "min")?)?, + float(&need(self.max, "max")?)?, + ); + let default = float(&self.default)?; + let step = self.step.map_or((max - min) / 100.0, |s| s as f32); + if !(min < max && (min..=max).contains(&default)) { + return Err(format!("needs min < max and default in [{min}, {max}]")); + } + if !(step > 0.0 && step <= max - min) { + return Err(format!("step {step} is not in (0, max - min]")); + } + OptionKind::Float { + default, + min, + max, + step, + } + } + "int" => { + unused(self.step.is_some(), "step")?; + unused(self.values.is_some(), "values")?; + let (min, max) = (int(&need(self.min, "min")?)?, int(&need(self.max, "max")?)?); + let default = int(&self.default)?; + if !(min < max && (min..=max).contains(&default)) { + return Err(format!("needs min < max and default in [{min}, {max}]")); + } + OptionKind::Int { default, min, max } + } + "choice" => { + unused(self.min.is_some() || self.max.is_some(), "min/max")?; + unused(self.step.is_some(), "step")?; + let values = self.values.ok_or("needs values")?; + if !(2..=32).contains(&values.len()) { + return Err("needs 2 to 32 values".into()); + } + if values + .iter() + .enumerate() + .any(|(i, v)| v.trim().is_empty() || values[..i].contains(v)) + { + return Err("values must be distinct and not empty".into()); + } + let default = self + .default + .as_str() + .and_then(|d| values.iter().position(|v| v == d)) + .ok_or("default must be one of the values")?; + OptionKind::Choice { + values, + default: default as u32, + } + } + other => { + return Err(format!("type {other:?} is not bool, float, int or choice")); + } + }; + Ok(PackOption { + id: self.id, + name: self.name, + description: self.description, + kind, + }) + } +} + +impl OptionKind { + /// The WGSL type of the option's `override`. + pub fn wgsl_type(&self) -> &'static str { + match self { + OptionKind::Bool { .. } => "bool", + OptionKind::Float { .. } => "f32", + OptionKind::Int { .. } => "i32", + OptionKind::Choice { .. } => "u32", + } + } + + /// The default as the pipeline constant wgpu takes. + pub fn default_constant(&self) -> f64 { + match *self { + OptionKind::Bool { default } => f64::from(u8::from(default)), + OptionKind::Float { default, .. } => f64::from(default), + OptionKind::Int { default, .. } => f64::from(default), + OptionKind::Choice { default, .. } => f64::from(default), + } + } +} diff --git a/crates/horizon-shader-api/src/package.rs b/crates/horizon-shader-api/src/package.rs new file mode 100644 index 000000000..e2e644a04 --- /dev/null +++ b/crates/horizon-shader-api/src/package.rs @@ -0,0 +1,190 @@ +//! A shader pack on disk: a folder holding `horizon-shaders.toml`, or the +//! same files zipped as a `.hshader` (what the registry and the launcher +//! carry). Both read into one [`Pack`]. +use crate::manifest::{MANIFEST, Manifest}; +use std::{ + collections::BTreeMap, + io::{Read, Write}, + path::Path, +}; + +/// A packaged pack's extension. +pub const EXTENSION: &str = "hshader"; +/// Bytes per WGSL file, per other file, and in all. +pub const MAX_WGSL: u64 = 256 << 10; +const MAX_OTHER: u64 = 1 << 20; +pub const MAX_TOTAL: u64 = 8 << 20; +/// Files in a `.hshader`. +const MAX_ENTRIES: usize = 512; +/// Files a pack may hold beside its manifest and WGSL. +const EXTRAS: [&str; 5] = [ + "README.md", + "LICENSE", + "LICENSE.md", + "LICENSE.txt", + "icon.png", +]; + +/// A pack read from a folder or a `.hshader`: its manifest and every file, +/// by path from the pack's root (`/`-separated). +#[derive(Debug)] +pub struct Pack { + pub manifest: Manifest, + pub files: BTreeMap>, +} + +/// A relative path made of plain parts: no root, `..`, `\`, empty or +/// hidden parts. +pub fn plain_path(path: &str) -> bool { + !path.is_empty() + && path.split('/').all(|part| { + !part.is_empty() + && !part.starts_with('.') + && part + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b"._-".contains(&b)) + }) +} + +impl Pack { + /// Reads `path`: a `.hshader` file, or a folder (its hidden files and + /// folders, such as `.git`, are skipped). + pub fn read(path: &Path) -> Result { + let mut files = BTreeMap::new(); + if path.is_dir() { + read_dir(path, "", &mut files)?; + } else { + let file = std::fs::File::open(path).map_err(|e| format!("{}: {e}", path.display()))?; + let mut zip = zip::ZipArchive::new(file) + .map_err(|e| format!("{}: not a .{EXTENSION} (zip): {e}", path.display()))?; + if zip.len() > MAX_ENTRIES { + return Err(format!("over {MAX_ENTRIES} files in the package")); + } + for i in 0..zip.len() { + let mut entry = zip.by_index(i).map_err(|e| e.to_string())?; + if entry.is_dir() { + continue; + } + let name = entry.name().to_owned(); + let mut bytes = Vec::new(); + (&mut entry) + .take(MAX_OTHER.max(MAX_WGSL) + 1) + .read_to_end(&mut bytes) + .map_err(|e| format!("{name}: {e}"))?; + if files.insert(name.clone(), bytes).is_some() { + return Err(format!("{name} is in the package twice")); + } + } + } + Pack::from_files(files) + } + + /// Checks a pack's files: a manifest, the WGSL it names (as UTF-8), + /// nothing it doesn't use, and sizes within bounds. + pub fn from_files(files: BTreeMap>) -> Result { + let text = files + .get(MANIFEST) + .ok_or_else(|| format!("{MANIFEST}: missing at the pack's root"))?; + let manifest = Manifest::parse( + std::str::from_utf8(text).map_err(|_| format!("{MANIFEST}: not UTF-8"))?, + )?; + let used = manifest.files(); + let mut total = 0; + for (name, bytes) in &files { + if !plain_path(name) { + return Err(format!("{name:?} is not a plain path")); + } + let size = bytes.len() as u64; + total += size; + let max = if name.ends_with(".wgsl") { + if !used.contains(&name.as_str()) { + return Err(format!("{name} is not used by any stage or in shared")); + } + if std::str::from_utf8(bytes).is_err() { + return Err(format!("{name}: not UTF-8")); + } + MAX_WGSL + } else if name == MANIFEST || EXTRAS.contains(&name.as_str()) { + MAX_OTHER + } else { + return Err(format!( + "{name}: a pack holds only {MANIFEST}, .wgsl files and {}", + EXTRAS.join(", ") + )); + }; + if size > max { + return Err(format!("{name} is over {} KiB", max >> 10)); + } + } + if total > MAX_TOTAL { + return Err(format!("the pack is over {} MiB", MAX_TOTAL >> 20)); + } + if let Some(missing) = used.iter().find(|f| !files.contains_key(**f)) { + return Err(format!("{missing}: named in {MANIFEST} but missing")); + } + Ok(Pack { manifest, files }) + } + + /// A WGSL file's text (every file the manifest names is checked UTF-8). + pub fn source(&self, file: &str) -> Option<&str> { + self.files + .get(file) + .and_then(|b| std::str::from_utf8(b).ok()) + } + + /// The `.hshader` bytes: the files in path order, with fixed times and + /// permissions, so the same pack always gives the same bytes (and the + /// same SHA-256). + pub fn package(&self) -> Result, String> { + let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let options = zip::write::SimpleFileOptions::default() + .compression_method(zip::CompressionMethod::Deflated) + .last_modified_time(zip::DateTime::default()) + .unix_permissions(0o644); + let err = |e: zip::result::ZipError| e.to_string(); + for (name, bytes) in &self.files { + zip.start_file(name.as_str(), options).map_err(err)?; + zip.write_all(bytes).map_err(|e| e.to_string())?; + } + Ok(zip.finish().map_err(err)?.into_inner()) + } + + /// The SHA-256 of [`Pack::package`], in lowercase hex: the pack's + /// identity in the registry. + pub fn sha256(&self) -> Result { + Ok(sha256_hex(&self.package()?)) + } +} + +/// SHA-256 in lowercase hex. +pub fn sha256_hex(bytes: &[u8]) -> String { + use sha2::Digest; + sha2::Sha256::digest(bytes) + .iter() + .map(|b| format!("{b:02x}")) + .collect() +} + +fn read_dir(dir: &Path, prefix: &str, files: &mut BTreeMap>) -> Result<(), String> { + let entries = std::fs::read_dir(dir).map_err(|e| format!("{}: {e}", dir.display()))?; + for entry in entries { + let entry = entry.map_err(|e| format!("{}: {e}", dir.display()))?; + let name = entry.file_name().to_string_lossy().into_owned(); + if name.starts_with('.') { + continue; + } + let path = entry.path(); + let rel = format!("{prefix}{name}"); + if path.is_dir() { + read_dir(&path, &format!("{rel}/"), files)?; + } else { + let size = entry.metadata().map_err(|e| format!("{rel}: {e}"))?.len(); + if size > MAX_TOTAL { + return Err(format!("{rel} is over {} MiB", MAX_TOTAL >> 20)); + } + let bytes = std::fs::read(&path).map_err(|e| format!("{rel}: {e}"))?; + files.insert(rel, bytes); + } + } + Ok(()) +} diff --git a/crates/horizon-shader-api/src/reference.rs b/crates/horizon-shader-api/src/reference.rs new file mode 100644 index 000000000..df74b7633 --- /dev/null +++ b/crates/horizon-shader-api/src/reference.rs @@ -0,0 +1,97 @@ +//! docs/api/shaders-reference.md, generated from the stages and inputs this +//! crate defines (`tests/reference.rs` fails when the file drifts). +use crate::check::{MAX_EXPRESSIONS, MAX_TEXTURE_READS}; +use crate::inputs::{Frame, SURFACE, Struct}; +use crate::manifest::{API, MAX_GPU_MS, MAX_OPTIONS, MAX_POST_PASSES}; +use crate::package::{MAX_TOTAL, MAX_WGSL}; +use crate::stages::{GROUP, StageKind}; + +fn table(s: &Struct, offsets: Option<&[usize]>) -> String { + let mut out = format!("### `{}`\n\n{}\n\n", s.name, one_line(s.doc)); + out += if offsets.is_some() { + "| Field | Type | Offset | Meaning |\n| --- | --- | --- | --- |\n" + } else { + "| Field | Type | Meaning |\n| --- | --- | --- |\n" + }; + for (i, f) in s.fields.iter().enumerate() { + let offset = offsets.map_or(String::new(), |o| format!(" {} |", o[i])); + out += &format!( + "| `{}` | `{}` |{offset} {} |\n", + f.name, + f.ty, + one_line(f.doc) + ); + } + out + "\n" +} + +fn one_line(doc: &str) -> String { + doc.split_whitespace().collect::>().join(" ") +} + +pub fn markdown() -> String { + let mut out = format!( + "# Shader API reference ({API})\n\n\ + Generated from `crates/horizon-shader-api` (`HORIZON_WRITE_DOCS=1 cargo test -p horizon-shader-api --test reference`\n\ + rewrites it; the test fails when it drifts). The API's design and pack format: [shaders.md](shaders.md).\n\n\ + A stage is a function your WGSL defines, with the signature below; Horizon puts the prelude (these structs, the\n\ + resources and helpers) before your files and calls the function from its own pass. Your files declare no\n\ + bindings and no entry points, and no names starting with `horizon_`.\n\n\ + ## Stages\n\n\ + | Stage | In `horizon-shaders.toml` | Your function |\n| --- | --- | --- |\n" + ); + for kind in StageKind::ALL { + let key = match kind { + StageKind::Post => "[[post]]", + StageKind::Sky => "[sky]", + StageKind::Material => "[material]", + }; + out += &format!("| {} | `{key}` | `{}` |\n", kind.name(), kind.signature()); + } + out += "\n"; + for kind in StageKind::ALL { + out += &format!("## {}\n\n`{}`\n\n", kind.name(), kind.signature()); + out += &table(kind.input(), None); + if kind == StageKind::Material { + out += &table(&SURFACE, None); + } + out += &format!( + "Resources (bind group {GROUP}):\n\n| Binding | Declaration | Meaning |\n| --- | --- | --- |\n" + ); + for r in kind.resources() { + out += &format!("| {} | `{}` | {} |\n", r.binding, r.ty, r.doc); + } + out += "\n"; + if kind == StageKind::Post { + out += "Helpers:\n\n\ + - `fn depth_at(uv: vec2) -> f32`: the world's depth at `uv`.\n\ + - `fn view_position(uv: vec2, depth: f32) -> vec3`: the camera-relative position, in blocks, of\n \ + the point at `uv` and `depth`.\n\n"; + } + } + out += "## The frame\n\n"; + out += &table(&Frame::WGSL, Some(Frame::OFFSETS)); + out += &format!( + "## Options\n\n\ + Each `[[options]]` entry sets the WGSL `override` of the same name when the pipelines are built:\n\n\ + | `type` | Keys | The `override` |\n| --- | --- | --- |\n\ + | `bool` | `default` | `override : bool` |\n\ + | `float` | `default`, `min`, `max`, `step` | `override : f32` |\n\ + | `int` | `default`, `min`, `max` | `override : i32` |\n\ + | `choice` | `values`, `default` (one of them) | `override : u32`, the value's index |\n\n\ + ## Limits\n\n\ + - {MAX_POST_PASSES} post passes, {MAX_OPTIONS} options, `budget.gpu_ms` up to {MAX_GPU_MS}.\n\ + - {} KiB per WGSL file, {} MiB per pack.\n\ + - Per stage: {MAX_EXPRESSIONS} naga expressions over all its functions, {MAX_TEXTURE_READS} texture reads (samples,\n \ + gathers and loads, the prelude's included).\n\n\ + ## The check\n\n\ + A pack runs only after it passes `horizon_shader_api::check` (the game when it loads a pack, the launcher before it\n\ + installs one, the registry's CI): each stage is parsed and validated by naga, checked against its signature and\n\ + the rules above, and translated for Vulkan (SPIR-V), Metal and DirectX 12 (HLSL) with its options at their\n\ + defaults, minimums and maximums. Every option must be an `override` of its type in some stage; an `override`\n\ + that is no option needs a default.\n", + MAX_WGSL >> 10, + MAX_TOTAL >> 20 + ); + out +} diff --git a/crates/horizon-shader-api/src/stages.rs b/crates/horizon-shader-api/src/stages.rs new file mode 100644 index 000000000..12bd3e038 --- /dev/null +++ b/crates/horizon-shader-api/src/stages.rs @@ -0,0 +1,265 @@ +//! The stages a pack can fill, each a WGSL function with a fixed signature +//! that Horizon calls inside its own passes, and the WGSL put around a +//! pack's files to build a stage: the prelude before (the inputs and the +//! resources), the entry points after. +use crate::inputs::{Frame, MATERIAL_INPUT, POST_INPUT, SKY_INPUT, SURFACE, Struct}; +use crate::package::Pack; + +/// The bind group of a pack's resources; groups 0 to 2 are the renderer's. +pub const GROUP: u32 = 3; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum StageKind { + /// The sky's colour along each view ray, before the sun, moon and stars. + Sky, + /// The colour of block surfaces (terrain), before the alpha test and fog. + Material, + /// A full-screen pass over the world's picture, under the hand and HUD. + Post, +} + +/// A resource a stage reads, declared by its prelude. +#[derive(Debug)] +pub struct Resource { + pub binding: u32, + pub name: &'static str, + pub ty: &'static str, + pub doc: &'static str, +} + +const FRAME: Resource = Resource { + binding: 0, + name: "frame", + ty: "var frame: Frame", + doc: "The frame (`Frame`).", +}; + +const POST_RESOURCES: [Resource; 6] = [ + FRAME, + Resource { + binding: 1, + name: "scene_color", + ty: "var scene_color: texture_2d", + doc: "The world's picture before any pass.", + }, + Resource { + binding: 2, + name: "scene_depth", + ty: "var scene_depth: texture_depth_2d", + doc: "The world's depth, reversed (1 near, 0 far and sky).", + }, + Resource { + binding: 3, + name: "previous", + ty: "var previous: texture_2d", + doc: "The previous pass's output (`scene_color` for the first pass).", + }, + Resource { + binding: 4, + name: "linear_sampler", + ty: "var linear_sampler: sampler", + doc: "Bilinear filtering, clamped to the edges.", + }, + Resource { + binding: 5, + name: "nearest_sampler", + ty: "var nearest_sampler: sampler", + doc: "Nearest texel, clamped to the edges.", + }, +]; + +/// Functions the post prelude gives packs. +const POST_HELPERS: &str = "\ +// The world's depth at `uv` (reversed: 0 is the sky). +fn depth_at(uv: vec2) -> f32 { + let size = vec2(textureDimensions(scene_depth)); + let texel = vec2(clamp(uv * size, vec2(0.0), size - 1.0)); + return textureLoad(scene_depth, texel, 0); +} +// The camera-relative world position, in blocks, of the point at `uv` and `depth`. +fn view_position(uv: vec2, depth: f32) -> vec3 { + let clip = vec4(uv.x * 2.0 - 1.0, 1.0 - uv.y * 2.0, depth, 1.0); + let view = frame.projection_inverse * clip; + return (frame.view_inverse * vec4(view.xyz / view.w, 0.0)).xyz; +} +"; + +/// The post stage's entry points, as the renderer builds them: a full-screen +/// triangle, and a fragment that calls the pack's `post` for each pixel. +const POST_ENTRIES: &str = "\ +struct HorizonPostOut { + @builtin(position) position: vec4, + @location(0) uv: vec2, +} +@vertex fn horizon_vs_post(@builtin(vertex_index) i: u32) -> HorizonPostOut { + let p = vec2(f32((i << 1u) & 2u), f32(i & 2u)); + var out: HorizonPostOut; + out.position = vec4(p * 2.0 - 1.0, 0.0, 1.0); + out.uv = vec2(p.x, 1.0 - p.y); + return out; +} +@fragment fn horizon_fs_post(in: HorizonPostOut) -> @location(0) vec4 { + let color = textureSampleLevel(previous, linear_sampler, in.uv, 0.0); + return post(PostInput(in.uv, in.position.xy, color, depth_at(in.uv))); +} +"; + +/// Entry points with the material stage's inputs: the renderer calls +/// `material` from its own terrain shader in the same way. +const MATERIAL_ENTRIES: &str = "\ +struct HorizonMaterialIn { + @builtin(position) position: vec4, + @location(0) albedo: vec4, + @location(1) tint: vec4, + @location(2) light: vec2, + @location(3) lightmap: vec3, + @location(4) world: vec3, + @location(5) uv: vec2, + @location(6) @interpolate(flat) translucent: u32, +} +@fragment fn horizon_fs_material(in: HorizonMaterialIn) -> @location(0) vec4 { + let input = MaterialInput(in.albedo, in.tint, in.light, in.lightmap, in.world, in.uv, in.translucent != 0u); + return material(input).color; +} +"; + +/// Entry points with the sky stage's inputs: the renderer calls `sky` from +/// its own sky shader in the same way. +const SKY_ENTRIES: &str = "\ +struct HorizonSkyIn { + @builtin(position) position: vec4, + @location(0) direction: vec3, + @location(1) color: vec4, +} +@fragment fn horizon_fs_sky(in: HorizonSkyIn) -> @location(0) vec4 { + return sky(SkyInput(normalize(in.direction), in.color)); +} +"; + +impl StageKind { + pub const ALL: [StageKind; 3] = [StageKind::Sky, StageKind::Material, StageKind::Post]; + + /// The manifest's name for the stage (`[sky]`, `[material]`, `[[post]]`). + pub fn name(self) -> &'static str { + match self { + StageKind::Sky => "sky", + StageKind::Material => "material", + StageKind::Post => "post", + } + } + + /// The function a pack defines: it has the stage's name. + pub fn function(self) -> &'static str { + self.name() + } + + pub fn input(self) -> &'static Struct { + match self { + StageKind::Sky => &SKY_INPUT, + StageKind::Material => &MATERIAL_INPUT, + StageKind::Post => &POST_INPUT, + } + } + + /// The type the function returns. + pub fn output(self) -> &'static str { + match self { + StageKind::Material => SURFACE.name, + StageKind::Sky | StageKind::Post => "vec4", + } + } + + /// `fn (input: ) -> `. + pub fn signature(self) -> String { + format!( + "fn {}(input: {}) -> {}", + self.function(), + self.input().name, + self.output() + ) + } + + pub fn resources(self) -> &'static [Resource] { + match self { + StageKind::Post => &POST_RESOURCES, + StageKind::Sky | StageKind::Material => std::slice::from_ref(&FRAME), + } + } + + /// Everything the pack's files may use, declared before them. + pub fn prelude(self) -> String { + let mut out = + String::from("// Horizon shader API prelude (docs/api/shaders-reference.md)\n"); + out += &Frame::WGSL.wgsl(); + out += &self.input().wgsl(); + if self == StageKind::Material { + out += &SURFACE.wgsl(); + } + for r in self.resources() { + out += &format!("@group({GROUP}) @binding({}) {};\n", r.binding, r.ty); + } + if self == StageKind::Post { + out += POST_HELPERS; + } + out + } + + /// The entry points put after the pack's files. + pub fn entries(self) -> &'static str { + match self { + StageKind::Sky => SKY_ENTRIES, + StageKind::Material => MATERIAL_ENTRIES, + StageKind::Post => POST_ENTRIES, + } + } +} + +/// A stage's WGSL, and where each part of it came from. +#[derive(Debug)] +pub struct Composed { + pub text: String, + /// (the file, or `` / ``, its byte range in `text`). + pub parts: Vec<(String, std::ops::Range)>, +} + +impl Composed { + /// The file, line and column (1-based) of a byte offset in `text`. + pub fn locate(&self, offset: usize) -> (&str, usize, usize) { + let (file, range) = self + .parts + .iter() + .find(|(_, r)| r.contains(&offset)) + .or(self.parts.last()) + .expect("a composed stage has parts"); + let before = &self.text[range.start..offset.clamp(range.start, range.end)]; + let line = before.matches('\n').count() + 1; + let column = before.len() - before.rfind('\n').map_or(0, |i| i + 1) + 1; + (file, line, column) + } +} + +/// The WGSL of the stage `kind` whose own file is `file`: the prelude, the +/// pack's shared files in order, the file, then the entry points. +pub fn compose(pack: &Pack, kind: StageKind, file: &str) -> Composed { + let mut composed = Composed { + text: String::new(), + parts: Vec::new(), + }; + let mut push = |name: &str, text: &str| { + let start = composed.text.len(); + composed.text += text; + if !text.ends_with('\n') { + composed.text.push('\n'); + } + composed + .parts + .push((name.to_owned(), start..composed.text.len())); + }; + push("", &kind.prelude()); + for shared in pack.manifest.shared.iter().filter(|s| *s != file) { + push(shared, pack.source(shared).unwrap_or_default()); + } + push(file, pack.source(file).unwrap_or_default()); + push("", kind.entries()); + composed +} diff --git a/crates/horizon-shader-api/templates/material/horizon-shaders.toml b/crates/horizon-shader-api/templates/material/horizon-shaders.toml new file mode 100644 index 000000000..8cb7737e6 --- /dev/null +++ b/crates/horizon-shader-api/templates/material/horizon-shaders.toml @@ -0,0 +1,18 @@ +schema = 1 +id = "{{id}}" +name = "{{name}}" +version = "0.1.0" +api = "0.1" +description = "How block surfaces are lit." + +[material] +file = "material.wgsl" + +[[options]] +id = "contrast" +name = "Light contrast" +type = "float" +default = 1.2 +min = 0.5 +max = 2.0 +step = 0.05 diff --git a/crates/horizon-shader-api/templates/material/material.wgsl b/crates/horizon-shader-api/templates/material/material.wgsl new file mode 100644 index 000000000..ef9d89a2b --- /dev/null +++ b/crates/horizon-shader-api/templates/material/material.wgsl @@ -0,0 +1,9 @@ +// The colour of each point of a block's surface, before the alpha test and +// fog. Vanilla's own: albedo * tint * vec4(lightmap, 1.0). +// Reference: docs/api/shaders-reference.md. +override contrast: f32 = 1.2; + +fn material(input: MaterialInput) -> Surface { + let light = pow(input.lightmap, vec3(contrast)); + return Surface(input.albedo * input.tint * vec4(light, 1.0)); +} diff --git a/crates/horizon-shader-api/templates/post/horizon-shaders.toml b/crates/horizon-shader-api/templates/post/horizon-shaders.toml new file mode 100644 index 000000000..e135ee838 --- /dev/null +++ b/crates/horizon-shader-api/templates/post/horizon-shaders.toml @@ -0,0 +1,22 @@ +schema = 1 +id = "{{id}}" +name = "{{name}}" +version = "0.1.0" +api = "0.1" +description = "A post-processing pass over the world." + +[[post]] +file = "post.wgsl" +name = "Colour" + +[[options]] +id = "saturation" +name = "Saturation" +type = "float" +default = 1.15 +min = 0.0 +max = 2.0 +step = 0.05 + +[budget] +gpu_ms = 0.5 diff --git a/crates/horizon-shader-api/templates/post/post.wgsl b/crates/horizon-shader-api/templates/post/post.wgsl new file mode 100644 index 000000000..d4d179379 --- /dev/null +++ b/crates/horizon-shader-api/templates/post/post.wgsl @@ -0,0 +1,9 @@ +// Runs once per pixel of the world's picture, under the hand and the HUD. +// Reference: docs/api/shaders-reference.md (the inputs, `frame` and the helpers). +override saturation: f32 = 1.15; + +fn post(input: PostInput) -> vec4 { + let grey = dot(input.color.rgb, vec3(0.2126, 0.7152, 0.0722)); + let color = mix(vec3(grey), input.color.rgb, saturation); + return vec4(color, input.color.a); +} diff --git a/crates/horizon-shader-api/templates/sky/horizon-shaders.toml b/crates/horizon-shader-api/templates/sky/horizon-shaders.toml new file mode 100644 index 000000000..5939f25fd --- /dev/null +++ b/crates/horizon-shader-api/templates/sky/horizon-shaders.toml @@ -0,0 +1,18 @@ +schema = 1 +id = "{{id}}" +name = "{{name}}" +version = "0.1.0" +api = "0.1" +description = "A sky of its own." + +[sky] +file = "sky.wgsl" + +[[options]] +id = "glow" +name = "Sun glow" +type = "float" +default = 0.5 +min = 0.0 +max = 1.0 +step = 0.05 diff --git a/crates/horizon-shader-api/templates/sky/sky.wgsl b/crates/horizon-shader-api/templates/sky/sky.wgsl new file mode 100644 index 000000000..b5aaaa7ad --- /dev/null +++ b/crates/horizon-shader-api/templates/sky/sky.wgsl @@ -0,0 +1,9 @@ +// The sky's colour along each view ray; the sun, moon, stars and clouds are +// drawn over it. Reference: docs/api/shaders-reference.md. +override glow: f32 = 0.5; + +fn sky(input: SkyInput) -> vec4 { + let toward_sun = max(dot(input.direction, frame.sun_direction), 0.0); + let warm = vec3(1.0, 0.6, 0.3) * pow(toward_sun, 8.0) * glow; + return vec4(input.color.rgb + warm, input.color.a); +} diff --git a/crates/horizon-shader-api/tests/check.rs b/crates/horizon-shader-api/tests/check.rs new file mode 100644 index 000000000..4c6168a43 --- /dev/null +++ b/crates/horizon-shader-api/tests/check.rs @@ -0,0 +1,146 @@ +//! `check`: the sample pack passes, and each kind of mistake is reported +//! with the pack's own file and line. +use horizon_shader_api::{MANIFEST, Pack, Problem, check}; +use std::path::Path; + +fn sample() -> Pack { + Pack::read(Path::new(concat!( + env!("CARGO_MANIFEST_DIR"), + "/tests/samples/schema-1" + ))) + .unwrap() +} + +/// The sample with `file` replaced by `text`, checked. +fn problems(file: &str, text: &str) -> Vec { + let mut files = sample().files; + files.insert(file.into(), text.as_bytes().to_vec()); + check(&Pack::from_files(files).unwrap()).expect_err("should fail") +} + +fn one(file: &str, text: &str, needle: &str) -> Problem { + let p = problems(file, text); + assert!( + p.iter().any(|p| p.message.contains(needle)), + "{needle:?} not in {p:#?}" + ); + p.into_iter().find(|p| p.message.contains(needle)).unwrap() +} + +const GRADE: &str = "post/grade.wgsl"; + +#[test] +fn the_sample_passes() { + let pack = sample(); + let report = check(&pack).unwrap(); + let labels: Vec<_> = report.stages.iter().map(|s| s.label.as_str()).collect(); + assert_eq!( + labels, + ["sky.wgsl", "Terrain", "Colour grade", "post/vignette.wgsl"] + ); + assert!(report.stages.iter().all(|s| s.expressions > 0)); + let post = &report.stages[2]; + assert!(post.texture_reads >= 2, "the entry point samples and loads"); + let sha = pack.sha256().unwrap(); + assert_eq!(sha.len(), 64); + assert_eq!(sha, sample().sha256().unwrap()); +} + +#[test] +fn a_syntax_error_points_at_its_line() { + let p = one( + GRADE, + "override saturation: f32 = 1.1;\noverride tonemap: u32 = 1u;\nfn post(input: PostInput) -> vec4 {\n return input.color +;\n}\n", + "expected", + ); + assert_eq!((p.file.as_str(), p.line), (GRADE, 4), "{p}"); + assert!(p.to_string().starts_with("post/grade.wgsl:4:"), "{p}"); +} + +#[test] +fn the_stage_function_must_have_its_signature() { + let header = "override saturation: f32 = 1.1;\noverride tonemap: u32 = 1u;\n"; + one( + GRADE, + &format!("{header}fn post(input: PostInput) -> vec3 {{ return input.color.rgb; }}\n"), + "`fn post(PostInput) -> vec3` should be `fn post(input: PostInput) -> vec4`", + ); + one( + GRADE, + &format!("{header}fn grade(input: PostInput) -> vec4 {{ return input.color; }}\n"), + "defines no `fn post(input: PostInput) -> vec4`", + ); +} + +#[test] +fn a_pack_declares_no_bindings_entry_points_or_horizon_names() { + let body = "override saturation: f32 = 1.1;\noverride tonemap: u32 = 1u;\nfn post(input: PostInput) -> vec4 { return input.color; }\n"; + one( + GRADE, + &format!("@group(3) @binding(9) var extra: texture_2d;\n{body}"), + "declares the binding `extra`", + ); + one( + GRADE, + &format!( + "{body}@fragment fn main() -> @location(0) vec4 {{ return vec4(1.0); }}\n" + ), + "declares the entry point `main`", + ); + one( + GRADE, + &format!("{body}fn horizon_mine() {{}}\n"), + "`horizon_mine`: names starting with horizon_", + ); +} + +#[test] +fn overrides_match_the_options() { + let post = "fn post(input: PostInput) -> vec4 { return input.color; }\n"; + one( + GRADE, + &format!("override saturation: i32 = 1;\noverride tonemap: u32 = 1u;\n{post}"), + "option \"saturation\" is a float, so its override is `f32`", + ); + one( + GRADE, + &format!( + "override saturation: f32 = 1.1;\noverride tonemap: u32 = 1u;\noverride extra: f32;\n{post}" + ), + "`override extra` has no default and no [[options]] entry", + ); + let p = one( + GRADE, + &format!("override saturation: f32 = 1.1;\n{post}"), + "option \"tonemap\": no stage declares `override tonemap: u32`", + ); + assert_eq!(p.file, MANIFEST); +} + +#[test] +fn validation_errors_point_into_the_pack() { + // A compute-only barrier in a fragment stage passes the parser, not the validator. + let p = one( + GRADE, + "override saturation: f32 = 1.1;\noverride tonemap: u32 = 1u;\n\ + fn post(input: PostInput) -> vec4 {\n \ + workgroupBarrier();\n \ + return input.color;\n}\n", + "forbidden at this stage", + ); + assert_eq!(p.file, GRADE, "{p}"); +} + +#[test] +fn texture_reads_have_a_budget() { + let reads: String = (0..70) + .map(|i| format!(" c += textureLoad(previous, vec2({i}, 0), 0);\n")) + .collect(); + one( + GRADE, + &format!( + "override saturation: f32 = 1.1;\noverride tonemap: u32 = 1u;\nfn post(input: PostInput) -> vec4 {{\n var c = input.color;\n{reads} return c;\n}}\n" + ), + "texture reads; a stage may write 64", + ); +} diff --git a/crates/horizon-shader-api/tests/reference.rs b/crates/horizon-shader-api/tests/reference.rs new file mode 100644 index 000000000..fc548590b --- /dev/null +++ b/crates/horizon-shader-api/tests/reference.rs @@ -0,0 +1,119 @@ +//! The stages' WGSL against the Rust definitions: the preludes compile, the +//! frame's WGSL layout is the uploaded struct's, and +//! docs/api/shaders-reference.md is up to date. Rewrite the reference with +//! `HORIZON_WRITE_DOCS=1 cargo test -p horizon-shader-api --test reference`. +use horizon_shader_api::{Frame, Pack, StageKind, compose, reference}; +use std::path::Path; + +const DOC: &str = "docs/api/shaders-reference.md"; + +fn root() -> &'static Path { + Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/../..")) +} + +fn validate(text: &str) -> naga::Module { + let module = naga::front::wgsl::parse_str(text) + .unwrap_or_else(|e| panic!("{}\n{text}", e.emit_to_string(text))); + naga::valid::Validator::new( + naga::valid::ValidationFlags::all(), + naga::valid::Capabilities::default(), + ) + .validate(&module) + .unwrap_or_else(|e| panic!("{}\n{text}", e.emit_to_string(text))); + module +} + +/// Vanilla's look, written as each stage. +fn vanilla(kind: StageKind) -> &'static str { + match kind { + StageKind::Sky => "fn sky(input: SkyInput) -> vec4 { return input.color; }\n", + StageKind::Material => { + "fn material(input: MaterialInput) -> Surface {\n \ + return Surface(input.albedo * input.tint * vec4(input.lightmap, 1.0));\n}\n" + } + StageKind::Post => "fn post(input: PostInput) -> vec4 { return input.color; }\n", + } +} + +#[test] +fn every_stage_compiles_with_vanilla_s_function() { + for kind in StageKind::ALL { + let text = format!("{}{}{}", kind.prelude(), vanilla(kind), kind.entries()); + let module = validate(&text); + assert!( + module + .entry_points + .iter() + .all(|e| e.name.starts_with("horizon_")), + "{kind:?}" + ); + } +} + +#[test] +fn the_frame_s_wgsl_layout_is_the_uploaded_struct_s() { + let text = format!("{}{}", StageKind::Sky.prelude(), vanilla(StageKind::Sky)); + let module = validate(&text); + let (_, ty) = module + .types + .iter() + .find(|(_, t)| t.name.as_deref() == Some("Frame")) + .expect("Frame in the prelude"); + let naga::TypeInner::Struct { members, span } = &ty.inner else { + panic!("Frame is a struct") + }; + assert_eq!(*span as usize, std::mem::size_of::()); + let wgsl: Vec<_> = members + .iter() + .map(|m| (m.name.clone().unwrap(), m.offset as usize)) + .collect(); + let rust: Vec<_> = Frame::WGSL + .fields + .iter() + .zip(Frame::OFFSETS) + .map(|(f, &o)| (f.name.to_owned(), o)) + .collect(); + assert_eq!(wgsl, rust); +} + +#[test] +fn the_sample_pack_s_stages_compile() { + let dir = root().join("crates/horizon-shader-api/tests/samples/schema-1"); + let pack = Pack::read(&dir).unwrap(); + let m = &pack.manifest; + let stages = [ + (StageKind::Sky, m.sky.iter().collect::>()), + (StageKind::Material, m.material.iter().collect()), + (StageKind::Post, m.post.iter().collect()), + ]; + for (kind, list) in stages { + for stage in list { + let composed = compose(&pack, kind, &stage.file); + validate(&composed.text); + // Errors point into the pack's own file. + let part = composed.parts.iter().find(|(f, _)| *f == stage.file); + let part = part.unwrap().1.clone(); + let needle = format!("fn {}(", kind.function()); + let at = part.start + composed.text[part].find(&needle).unwrap(); + let source = pack.source(&stage.file).unwrap(); + let line = source.lines().position(|l| l.starts_with(&needle)).unwrap() + 1; + assert_eq!(composed.locate(at), (stage.file.as_str(), line, 1)); + } + } +} + +#[test] +fn the_reference_is_up_to_date() { + let path = root().join(DOC); + let want = reference::markdown(); + if std::env::var_os("HORIZON_WRITE_DOCS").is_some() { + std::fs::write(&path, &want).unwrap(); + } + let have = std::fs::read_to_string(&path) + .unwrap_or_default() + .replace("\r\n", "\n"); + assert!( + have == want, + "{DOC} is out of date: HORIZON_WRITE_DOCS=1 cargo test -p horizon-shader-api --test reference" + ); +} diff --git a/crates/horizon-shader-api/tests/samples/schema-1/README.md b/crates/horizon-shader-api/tests/samples/schema-1/README.md new file mode 100644 index 000000000..a8ad4583b --- /dev/null +++ b/crates/horizon-shader-api/tests/samples/schema-1/README.md @@ -0,0 +1 @@ +The schema 1 sample read by `tests/schema.rs`; `tests/reference.rs` compiles its stages. diff --git a/crates/horizon-shader-api/tests/samples/schema-1/common.wgsl b/crates/horizon-shader-api/tests/samples/schema-1/common.wgsl new file mode 100644 index 000000000..8fef8baf9 --- /dev/null +++ b/crates/horizon-shader-api/tests/samples/schema-1/common.wgsl @@ -0,0 +1,3 @@ +fn luminance(c: vec3) -> f32 { + return dot(c, vec3(0.2126, 0.7152, 0.0722)); +} diff --git a/crates/horizon-shader-api/tests/samples/schema-1/horizon-shaders.toml b/crates/horizon-shader-api/tests/samples/schema-1/horizon-shaders.toml new file mode 100644 index 000000000..6e8a545a7 --- /dev/null +++ b/crates/horizon-shader-api/tests/samples/schema-1/horizon-shaders.toml @@ -0,0 +1,59 @@ +# Schema 1 of horizon-shaders.toml with every key. Readers of later schemas +# keep reading this file (docs/api/stability.md, "Data formats"). +schema = 1 +id = "sample_pack" +name = "Sample Pack" +version = "1.0.0" +api = "0.1" +description = "Every key of schema 1." +authors = ["Horizon"] +license = "MIT" +shared = ["common.wgsl"] + +[sky] +file = "sky.wgsl" + +[material] +file = "material.wgsl" +name = "Terrain" + +[[post]] +file = "post/grade.wgsl" +name = "Colour grade" + +[[post]] +file = "post/vignette.wgsl" + +[[options]] +id = "saturation" +name = "Saturation" +description = "How colourful the picture is." +type = "float" +default = 1.1 +min = 0.0 +max = 2.0 +step = 0.05 + +[[options]] +id = "vignette" +name = "Vignette" +type = "bool" +default = true + +[[options]] +id = "samples" +name = "Samples" +type = "int" +default = 4 +min = 1 +max = 16 + +[[options]] +id = "tonemap" +name = "Tone mapping" +type = "choice" +values = ["None", "Soft", "Filmic"] +default = "Soft" + +[budget] +gpu_ms = 1.5 diff --git a/crates/horizon-shader-api/tests/samples/schema-1/material.wgsl b/crates/horizon-shader-api/tests/samples/schema-1/material.wgsl new file mode 100644 index 000000000..830470a6b --- /dev/null +++ b/crates/horizon-shader-api/tests/samples/schema-1/material.wgsl @@ -0,0 +1,4 @@ +fn material(input: MaterialInput) -> Surface { + let lit = input.albedo * input.tint * vec4(input.lightmap, 1.0); + return Surface(vec4(lit.rgb * (0.9 + 0.1 * luminance(lit.rgb)), lit.a)); +} diff --git a/crates/horizon-shader-api/tests/samples/schema-1/post/grade.wgsl b/crates/horizon-shader-api/tests/samples/schema-1/post/grade.wgsl new file mode 100644 index 000000000..8daa1eab0 --- /dev/null +++ b/crates/horizon-shader-api/tests/samples/schema-1/post/grade.wgsl @@ -0,0 +1,8 @@ +override saturation: f32 = 1.1; +override tonemap: u32 = 1u; + +fn post(input: PostInput) -> vec4 { + let c = input.color.rgb; + let graded = mix(vec3(luminance(c)), c, saturation); + return vec4(select(graded, graded / (graded + 0.5) * 1.5, tonemap > 0u), input.color.a); +} diff --git a/crates/horizon-shader-api/tests/samples/schema-1/post/vignette.wgsl b/crates/horizon-shader-api/tests/samples/schema-1/post/vignette.wgsl new file mode 100644 index 000000000..84f7261cb --- /dev/null +++ b/crates/horizon-shader-api/tests/samples/schema-1/post/vignette.wgsl @@ -0,0 +1,8 @@ +override vignette: bool = true; +override samples: i32 = 4; + +fn post(input: PostInput) -> vec4 { + let d = distance(input.uv, vec2(0.5)); + let dark = select(1.0, 1.0 - smoothstep(0.4, 0.9, d) * 0.5, vignette); + return vec4(input.color.rgb * dark, input.color.a); +} diff --git a/crates/horizon-shader-api/tests/samples/schema-1/sky.wgsl b/crates/horizon-shader-api/tests/samples/schema-1/sky.wgsl new file mode 100644 index 000000000..55ca0ac1d --- /dev/null +++ b/crates/horizon-shader-api/tests/samples/schema-1/sky.wgsl @@ -0,0 +1,3 @@ +fn sky(input: SkyInput) -> vec4 { + return input.color; +} diff --git a/crates/horizon-shader-api/tests/schema.rs b/crates/horizon-shader-api/tests/schema.rs new file mode 100644 index 000000000..2176805ee --- /dev/null +++ b/crates/horizon-shader-api/tests/schema.rs @@ -0,0 +1,238 @@ +//! Schema 1 of `horizon-shaders.toml` and the `.hshader` package. +use horizon_shader_api::{MANIFEST, Manifest, OptionKind, Pack}; +use std::{collections::BTreeMap, path::Path}; + +fn sample_dir() -> &'static Path { + Path::new(concat!( + env!("CARGO_MANIFEST_DIR"), + "/tests/samples/schema-1" + )) +} + +fn sample_text() -> String { + std::fs::read_to_string(sample_dir().join(MANIFEST)).unwrap() +} + +fn refused(text: &str) -> String { + Manifest::parse(text).expect_err("should be refused") +} + +#[test] +fn reads_every_key_of_schema_1() { + let m = Manifest::parse(&sample_text()).unwrap(); + assert_eq!( + ( + m.id.as_str(), + m.name.as_str(), + m.version.as_str(), + m.api.as_str() + ), + ("sample_pack", "Sample Pack", "1.0.0", "0.1") + ); + assert_eq!(m.authors, ["Horizon"]); + assert_eq!(m.license.as_deref(), Some("MIT")); + assert_eq!(m.sky.as_ref().unwrap().file, "sky.wgsl"); + assert_eq!( + m.material.as_ref().unwrap().name.as_deref(), + Some("Terrain") + ); + let post: Vec<_> = m.post.iter().map(|s| s.file.as_str()).collect(); + assert_eq!(post, ["post/grade.wgsl", "post/vignette.wgsl"]); + assert_eq!( + m.files(), + [ + "common.wgsl", + "sky.wgsl", + "material.wgsl", + "post/grade.wgsl", + "post/vignette.wgsl" + ] + ); + let kinds: Vec<_> = m.options.iter().map(|o| (o.id.as_str(), &o.kind)).collect(); + assert_eq!( + kinds, + [ + ( + "saturation", + &OptionKind::Float { + default: 1.1, + min: 0.0, + max: 2.0, + step: 0.05 + } + ), + ("vignette", &OptionKind::Bool { default: true }), + ( + "samples", + &OptionKind::Int { + default: 4, + min: 1, + max: 16 + } + ), + ( + "tonemap", + &OptionKind::Choice { + values: vec!["None".into(), "Soft".into(), "Filmic".into()], + default: 1 + } + ), + ] + ); + let types: Vec<_> = m.options.iter().map(|o| o.kind.wgsl_type()).collect(); + assert_eq!(types, ["f32", "bool", "i32", "u32"]); + let constants: Vec<_> = m + .options + .iter() + .map(|o| o.kind.default_constant()) + .collect(); + assert_eq!(constants, [f64::from(1.1f32), 1.0, 4.0, 1.0]); + assert_eq!(m.budget.gpu_ms, Some(1.5)); +} + +#[test] +fn the_smallest_manifest() { + let m = Manifest::parse( + "schema = 1\nid = \"p\"\nname = \"P\"\nversion = \"1\"\napi = \"0.1\"\n[[post]]\nfile = \"a.wgsl\"\n", + ) + .unwrap(); + assert!(m.sky.is_none() && m.options.is_empty() && m.budget.gpu_ms.is_none()); +} + +#[test] +fn refuses_what_this_build_does_not_read() { + let text = sample_text(); + let cases = [ + (text.replace("schema = 1", "schema = 2"), "schema 2"), + ( + text.replace("api = \"0.1\"", "api = \"0.2\""), + "shader API 0.2", + ), + ( + text.replace("\"sample_pack\"", "\"Sample\""), + "is not [a-z0-9_]", + ), + ( + text.replace("license = \"MIT\"", "licence = \"MIT\""), + "unknown field", + ), + ( + text.replace("gpu_ms = 1.5", "gpu_ms = 40.0"), + "budget.gpu_ms", + ), + ( + text.replace("\"common.wgsl\"", "\"../common.wgsl\""), + "not a plain path", + ), + ( + text.replace("\"sky.wgsl\"", "\"sky.glsl\""), + "not a plain path to a .wgsl", + ), + ( + text.replace("id = \"samples\"", "id = \"vignette\""), + "declared twice", + ), + ( + text.replace("id = \"samples\"", "id = \"horizon_samples\""), + "WGSL identifier", + ), + ( + text.replace("default = \"Soft\"", "default = \"Hard\""), + "one of the values", + ), + ( + text.replace("default = 1.1", "default = 3.0"), + "default in [0, 2]", + ), + ( + text.replace("default = 4", "default = 4.5"), + "not a 32-bit integer", + ), + ( + text.replace("type = \"bool\"", "type = \"color\""), + "is not bool", + ), + ( + text.replace("default = true", "default = true\nmin = 0"), + "doesn't apply", + ), + ]; + for (text, needle) in cases { + let e = refused(&text); + assert!(e.contains(needle), "{needle:?} not in {e:?}"); + assert!(e.starts_with(MANIFEST), "{e}"); + } + let no_stage = "schema = 1\nid = \"p\"\nname = \"P\"\nversion = \"1\"\napi = \"0.1\"\n"; + assert!(refused(no_stage).contains("no stage")); + let many = (0..9).map(|i| format!("[[post]]\nfile = \"p{i}.wgsl\"\n")); + assert!(refused(&format!("{no_stage}{}", many.collect::())).contains("9 post passes")); +} + +#[test] +fn reads_the_sample_folder_and_its_package_alike() { + let folder = Pack::read(sample_dir()).unwrap(); + assert_eq!(folder.files.len(), 7); + assert!( + folder + .source("post/grade.wgsl") + .unwrap() + .contains("fn post") + ); + let bytes = folder.package().unwrap(); + assert_eq!( + bytes, + folder.package().unwrap(), + "packaging is reproducible" + ); + let dir = std::env::temp_dir().join(format!("horizon-shader-api-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("sample_pack.hshader"); + std::fs::write(&path, &bytes).unwrap(); + let packaged = Pack::read(&path).unwrap(); + std::fs::remove_dir_all(&dir).unwrap(); + assert_eq!(packaged.files, folder.files); + assert_eq!(packaged.manifest, folder.manifest); +} + +#[test] +fn refuses_files_a_pack_does_not_use() { + let base = || -> BTreeMap> { Pack::read(sample_dir()).unwrap().files }; + let mut files = base(); + files.insert("old.wgsl".into(), b"fn f() {}".to_vec()); + assert!( + Pack::from_files(files) + .unwrap_err() + .contains("old.wgsl is not used") + ); + let mut files = base(); + files.insert("run.sh".into(), Vec::new()); + assert!( + Pack::from_files(files) + .unwrap_err() + .contains("run.sh: a pack holds only") + ); + let mut files = base(); + files.remove("sky.wgsl"); + assert!( + Pack::from_files(files) + .unwrap_err() + .contains("sky.wgsl: named in") + ); + let mut files = base(); + files.insert("sky.wgsl".into(), vec![0xff, 0xfe]); + assert!(Pack::from_files(files).unwrap_err().contains("not UTF-8")); + let mut files = base(); + files.insert("post/grade.wgsl".into(), vec![b' '; 300 << 10]); + assert!( + Pack::from_files(files) + .unwrap_err() + .contains("over 256 KiB") + ); + let mut files = base(); + files.remove(MANIFEST); + assert!( + Pack::from_files(files) + .unwrap_err() + .contains("missing at the pack's root") + ); +} diff --git a/crates/horizon-shader-api/tests/sdk.rs b/crates/horizon-shader-api/tests/sdk.rs new file mode 100644 index 000000000..ef69a88a0 --- /dev/null +++ b/crates/horizon-shader-api/tests/sdk.rs @@ -0,0 +1,92 @@ +//! The SDK: every template and the example pack pass the check, and the +//! `horizon-shader` tool makes, checks and packages a pack. +use horizon_shader_api::{Pack, check}; +use std::{path::Path, process::Command}; + +const TOOL: &str = env!("CARGO_BIN_EXE_horizon-shader"); + +fn scratch(name: &str) -> std::path::PathBuf { + let dir = std::env::temp_dir().join(format!("horizon-shader-{name}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + dir +} + +fn run(args: &[&str]) -> (bool, String) { + let out = Command::new(TOOL).args(args).output().unwrap(); + let text = + String::from_utf8_lossy(&out.stdout).into_owned() + &String::from_utf8_lossy(&out.stderr); + (out.status.success(), text) +} + +#[test] +fn the_example_pack_passes() { + let dir = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/packs/golden_hour")); + let report = check(&Pack::read(dir).unwrap()).unwrap(); + let labels: Vec<_> = report.stages.iter().map(|s| s.label.as_str()).collect(); + assert_eq!(labels, ["Warm horizon", "Glow", "Colour grade"]); +} + +#[test] +fn every_template_makes_a_pack_that_passes() { + let root = scratch("templates"); + for template in ["post", "sky", "material"] { + let dir = root.join(format!("My-{template}")); + let dir_s = dir.to_str().unwrap(); + let (ok, text) = run(&["new", dir_s, "--template", template]); + assert!(ok, "{text}"); + let pack = Pack::read(&dir).unwrap(); + assert_eq!(pack.manifest.id, format!("my_{template}")); + assert_eq!(pack.manifest.name, format!("My {template}")); + let (ok, text) = run(&["check", dir_s]); + assert!(ok && text.contains(": ok"), "{text}"); + } + let (ok, text) = run(&[ + "new", + root.join("x").to_str().unwrap(), + "--template", + "water", + ]); + assert!(!ok && text.contains("no template \"water\""), "{text}"); + std::fs::remove_dir_all(root).unwrap(); +} + +#[test] +fn packs_and_checks_the_package() { + let root = scratch("pack"); + let dir = root.join("glow"); + let dir_s = dir.to_str().unwrap(); + assert!(run(&["new", dir_s, "--name", "Glow"]).0); + let out = root.join("glow.hshader"); + let (ok, text) = run(&["pack", dir_s, "--out", out.to_str().unwrap()]); + assert!(ok, "{text}"); + let sha = text + .split("sha256 ") + .nth(1) + .unwrap() + .trim_end_matches([')', '\n']) + .to_owned(); + let (ok, json) = run(&["check", out.to_str().unwrap(), "--json"]); + assert!(ok, "{json}"); + let report: serde_json::Value = serde_json::from_str(&json).unwrap(); + assert_eq!(report[0]["ok"], true); + assert_eq!(report[0]["id"], "glow"); + assert_eq!(report[0]["sha256"], sha.as_str()); + assert_eq!(report[0]["stages"][0]["stage"], "post"); + + std::fs::write( + dir.join("post.wgsl"), + "fn post(input: PostInput) -> vec4 { return input.color }\n", + ) + .unwrap(); + let (ok, json) = run(&["check", dir_s, "--json"]); + assert!(!ok); + let report: serde_json::Value = + serde_json::from_str(json.split("\nerror:").next().unwrap()).unwrap(); + assert_eq!(report[0]["ok"], false); + assert_eq!(report[0]["problems"][0]["file"], "post.wgsl"); + assert_eq!(report[0]["problems"][0]["line"], 1); + let (ok, text) = run(&["pack", dir_s]); + assert!(!ok && text.contains("failed the check"), "{text}"); + std::fs::remove_dir_all(root).unwrap(); +} diff --git a/docs/README.md b/docs/README.md index 1450ba388..609580d91 100644 --- a/docs/README.md +++ b/docs/README.md @@ -13,6 +13,7 @@ To play, use [the launcher](../launcher/README.md); the project site is [playhor - [Graphics](graphics.md): the Simple and Advanced graphics settings, the graphics benchmark and what it measured. - [Benchmark](benchmark.md): Horizon against vanilla 26.3 and vanilla with performance mods, on the same machine, world, path and settings: what is measured, how it is kept fair, how to reproduce it. - [Extras](extras.md): optional zoom, minimap, FPS and keystroke overlays and auto-reconnect, all off by default. +- [Shader packs](shaders.md): Horizon's own WGSL shader packs, how to make and check one (`horizon-shader`). - [Mods](mods.md): Horizon's own mod system (Rust, sandboxed WebAssembly), how to make and install mods. - [Modpacks](modpacks.md): porting whole packs (ATM10 and smaller): import, inventory, carry-over, an agent per mod with a harness against the Java original, coverage, and how "10x" is measured. - [Launcher](../launcher/README.md): installs, updates and starts builds; its CLI and MCP server; releasing it. diff --git a/docs/api/mod-index.md b/docs/api/mod-index.md index beb6ec2c8..6b807d950 100644 --- a/docs/api/mod-index.md +++ b/docs/api/mod-index.md @@ -138,9 +138,24 @@ Unknown keys are refused. Its CI runs `cargo-horizon-mod` (`crates/cargo-horizon | `cargo horizon-mod check [--json]` | A submission's checks: its kind's (above), its signature with `--key` and `--signature`, and for a mod a trial run in the sandbox (40 ticks, 120 frames, a chat line, each command). | | `cargo horizon-mod registry check --files ` | `check` on each file `head` lists and `base` doesn't: a pull request's new files. | | `cargo horizon-mod registry list ` | ` ` per version: the files to download. | -| `cargo horizon-mod registry build --files --out index.json` | Reads every entry and its file (`/`), lists every problem, and writes the index with URLs `/-/`. | +| `cargo horizon-mod registry build --files --out index.json` | Reads every entry and its file (`/`), lists every problem, and writes the index with URLs `-/` (`--release`, ending in `/`, defaults to the repository's releases). | | `cargo horizon-mod registry review --author ` | A pull request's changes: a published file never changes and is never removed (take it down in `revoked.toml`), a kind never changes; changes by a non-owner and to owners or keys are pointed out for the reviewers. | +How the repository runs (its workflows are in `.github/workflows/`): + +- **A pull request** (`check.yml`) runs `registry review`, downloads each new file from the `staging` prerelease, runs + `registry check` on it and builds the index it would publish; the report is the run's summary. The tool is + `cargo-horizon-mod` from Horizon at the commit in the repository variable `HORIZON_REV`. The form's pull requests + are opened with the token of the account in the variable `REGISTRY_BOT`; for those, the author is the login in the + body's `` line. +- **Merging** needs the `check` run and a CODEOWNER's review. `publish.yml` then makes each new version a release + `-` and deploys `index.json` and `icons/` to GitHub Pages: + `https://ludovic111.github.io/horizon-mods/index.json`, served with `Access-Control-Allow-Origin: *`. +- **Weekly**, `cleanup.yml` deletes staged copies of published files after 7 days and of files no open pull request + lists after 30. +- **Taking a file down** is a pull request adding it to `revoked.toml`. Removing an entry outright (a test, a project + nobody installed) fails `registry review` on purpose and needs an administrator's merge. + Kinds open to submissions: `mod`, `modpack`, `resourcepack`. `shader` is in the schema and stays closed until Horizon's own shader pack format exists (Iris and OptiFine packs are for the Modrinth browser); its check will compile the pack in a headless Horizon. A new kind gets its own check before it opens. diff --git a/docs/api/shaders-reference.md b/docs/api/shaders-reference.md new file mode 100644 index 000000000..f9682a1a4 --- /dev/null +++ b/docs/api/shaders-reference.md @@ -0,0 +1,162 @@ +# Shader API reference (0.1) + +Generated from `crates/horizon-shader-api` (`HORIZON_WRITE_DOCS=1 cargo test -p horizon-shader-api --test reference` +rewrites it; the test fails when it drifts). The API's design and pack format: [shaders.md](shaders.md). + +A stage is a function your WGSL defines, with the signature below; Horizon puts the prelude (these structs, the +resources and helpers) before your files and calls the function from its own pass. Your files declare no +bindings and no entry points, and no names starting with `horizon_`. + +## Stages + +| Stage | In `horizon-shaders.toml` | Your function | +| --- | --- | --- | +| sky | `[sky]` | `fn sky(input: SkyInput) -> vec4` | +| material | `[material]` | `fn material(input: MaterialInput) -> Surface` | +| post | `[[post]]` | `fn post(input: PostInput) -> vec4` | + +## sky + +`fn sky(input: SkyInput) -> vec4` + +### `SkyInput` + +One direction of the sky, for `fn sky(input: SkyInput) -> vec4`. The sun, moon, stars and clouds are drawn over the result. + +| Field | Type | Meaning | +| --- | --- | --- | +| `direction` | `vec3` | Unit view ray, camera-relative world axes. | +| `color` | `vec4` | Vanilla's sky colour along the ray (sky, fog and sunrise). | + +Resources (bind group 3): + +| Binding | Declaration | Meaning | +| --- | --- | --- | +| 0 | `var frame: Frame` | The frame (`Frame`). | + +## material + +`fn material(input: MaterialInput) -> Surface` + +### `MaterialInput` + +One point of a block's surface, for `fn material(input: MaterialInput) -> Surface`. + +| Field | Type | Meaning | +| --- | --- | --- | +| `albedo` | `vec4` | The block texture's colour here (alpha 0..1). | +| `tint` | `vec4` | The vertex colour: biome tint, face shade and ambient occlusion. | +| `light` | `vec2` | Block and sky light, 0..1, smoothed over the face. | +| `lightmap` | `vec3` | Vanilla's light colour for that light this frame. | +| `position` | `vec3` | The point, camera-relative, in blocks. | +| `uv` | `vec2` | The texture coordinate inside the block atlas. | +| `translucent` | `bool` | Drawn in the translucent pass (water, stained glass, ice). | + +### `Surface` + +What `material` returns. Vanilla's own is `albedo * tint * vec4(lightmap, 1.0)`; Horizon then applies the alpha test (cutout and translucent) and fog. + +| Field | Type | Meaning | +| --- | --- | --- | +| `color` | `vec4` | The lit colour, 0..1, with its alpha. | + +Resources (bind group 3): + +| Binding | Declaration | Meaning | +| --- | --- | --- | +| 0 | `var frame: Frame` | The frame (`Frame`). | + +## post + +`fn post(input: PostInput) -> vec4` + +### `PostInput` + +One pixel of the picture, for `fn post(input: PostInput) -> vec4`. + +| Field | Type | Meaning | +| --- | --- | --- | +| `uv` | `vec2` | Where the pixel is, 0..1 from the top left. | +| `pixel` | `vec2` | The pixel's centre, in pixels from the top left. | +| `color` | `vec4` | The previous pass's colour here (the world's picture for the first pass). | +| `depth` | `f32` | The world's depth here, reversed: 0 is the sky. | + +Resources (bind group 3): + +| Binding | Declaration | Meaning | +| --- | --- | --- | +| 0 | `var frame: Frame` | The frame (`Frame`). | +| 1 | `var scene_color: texture_2d` | The world's picture before any pass. | +| 2 | `var scene_depth: texture_depth_2d` | The world's depth, reversed (1 near, 0 far and sky). | +| 3 | `var previous: texture_2d` | The previous pass's output (`scene_color` for the first pass). | +| 4 | `var linear_sampler: sampler` | Bilinear filtering, clamped to the edges. | +| 5 | `var nearest_sampler: sampler` | Nearest texel, clamped to the edges. | + +Helpers: + +- `fn depth_at(uv: vec2) -> f32`: the world's depth at `uv`. +- `fn view_position(uv: vec2, depth: f32) -> vec3`: the camera-relative position, in blocks, of + the point at `uv` and `depth`. + +## The frame + +### `Frame` + +The frame, as `frame` in every stage. Positions and directions are in world axes relative to the camera (x east, y up, z south), so they stay precise far from the world's origin; depth is reversed (1 near, 0 at the far plane and in the sky). + +| Field | Type | Offset | Meaning | +| --- | --- | --- | --- | +| `view` | `mat4x4` | 0 | Camera-relative world to view space: the camera's rotation. | +| `projection` | `mat4x4` | 64 | View to clip space (reversed depth, wgpu's clip space: z 0..1). | +| `view_inverse` | `mat4x4` | 128 | View to camera-relative world space. | +| `projection_inverse` | `mat4x4` | 192 | Clip to view space. | +| `camera_position` | `vec3` | 256 | The camera's world position, in blocks (about a millimetre of precision 10 000 blocks out). | +| `time` | `f32` | 268 | Seconds since the pack was loaded, wrapping to 0 every hour. | +| `sun_direction` | `vec3` | 272 | Unit vector toward the sun (below the horizon at night). | +| `day_time` | `f32` | 284 | The time of day, 0..1: 0 sunrise, 0.25 noon, 0.5 sunset, 0.75 midnight. | +| `moon_direction` | `vec3` | 288 | Unit vector toward the moon. | +| `game_time` | `f32` | 300 | Vanilla's GameTime: (game time % 24000 + partial tick) / 24000, 0..1. | +| `sky_color` | `vec3` | 304 | Vanilla's sky colour this frame, 0..1. | +| `rain` | `f32` | 316 | Rain strength, 0..1. | +| `fog_color` | `vec3` | 320 | Vanilla's fog colour this frame (also the clear colour), 0..1. | +| `thunder` | `f32` | 332 | Thunder strength, 0..1. | +| `resolution` | `vec2` | 336 | The size of the world's picture, in pixels (the resolution scale applied). | +| `eye_light` | `vec2` | 344 | Block and sky light at the camera, 0..1 (level / 15). | +| `fog_start` | `f32` | 352 | Where vanilla's fog starts, in blocks from the camera. | +| `fog_end` | `f32` | 356 | Where vanilla's fog is full, in blocks. | +| `render_distance` | `f32` | 360 | The render distance, in blocks. | +| `far` | `f32` | 364 | The far plane, in blocks. | +| `night_vision` | `f32` | 368 | Night vision strength, 0..1. | +| `blindness` | `f32` | 372 | Blindness strength, 0..1. | +| `darkness` | `f32` | 376 | The darkness effect's strength, 0..1. | +| `brightness` | `f32` | 380 | The Brightness option, 0..1 (Moody to Bright). | +| `eye_in` | `u32` | 384 | What the camera is in: 0 air, 1 water, 2 lava, 3 powder snow. | +| `dimension` | `i32` | 388 | 0 the Overworld, -1 the Nether, 1 the End, 2 any other dimension. | +| `frame_index` | `u32` | 392 | Frames drawn since the pack was loaded (wraps). | +| `hide_gui` | `u32` | 396 | 1 while F1 hides the HUD, else 0. | + +## Options + +Each `[[options]]` entry sets the WGSL `override` of the same name when the pipelines are built: + +| `type` | Keys | The `override` | +| --- | --- | --- | +| `bool` | `default` | `override : bool` | +| `float` | `default`, `min`, `max`, `step` | `override : f32` | +| `int` | `default`, `min`, `max` | `override : i32` | +| `choice` | `values`, `default` (one of them) | `override : u32`, the value's index | + +## Limits + +- 8 post passes, 64 options, `budget.gpu_ms` up to 16. +- 256 KiB per WGSL file, 8 MiB per pack. +- Per stage: 20000 naga expressions over all its functions, 64 texture reads (samples, + gathers and loads, the prelude's included). + +## The check + +A pack runs only after it passes `horizon_shader_api::check` (the game when it loads a pack, the launcher before it +installs one, the registry's CI): each stage is parsed and validated by naga, checked against its signature and +the rules above, and translated for Vulkan (SPIR-V), Metal and DirectX 12 (HLSL) with its options at their +defaults, minimums and maximums. Every option must be an `override` of its type in some stage; an `override` +that is no option needs a default. diff --git a/docs/api/shaders.md b/docs/api/shaders.md index 10d1ae3de..8e9e6919c 100644 --- a/docs/api/shaders.md +++ b/docs/api/shaders.md @@ -34,10 +34,55 @@ how the world is fed to the GPU. benchmark ([benchmark-report.md](../benchmark-report.md)) records the pack and its cost, so "fast" stays measured. - **Off by default.** Without a pack the renderer is vanilla's look, and no pack code runs. +## The pack format (schema 1) + +A pack is a folder, or the same files zipped as a `.hshader`, with `horizon-shaders.toml` at its root +(crate `horizon-shader-api`; the sample with every key is +`crates/horizon-shader-api/tests/samples/schema-1/`): + +```toml +schema = 1 # the manifest's format; a reader refuses schemas newer than it knows +id = "soft_light" # [a-z0-9_]{1,64}: the settings key and the registry id +name = "Soft Light" +version = "1.0.0" +api = "0.1" # the shader API line the WGSL is written for +description = "..." # optional, like authors and license +shared = ["common.wgsl"] # optional: WGSL put before every stage's file, in order + +[sky] # each stage is optional, but a pack has at least one +file = "sky.wgsl" + +[material] +file = "material.wgsl" + +[[post]] # full-screen passes, in the order they run (8 at most) +file = "post/grade.wgsl" +name = "Colour grade" # optional: the GPU time overlay's label + +[[options]] # 64 at most; `id` is the WGSL `override` it sets +id = "saturation" +name = "Saturation" +type = "float" # bool, float (min, max, step), int (min, max), choice (values) +default = 1.1 +min = 0.0 +max = 2.0 +step = 0.05 + +[budget] +gpu_ms = 1.5 # optional: the GPU time the pack aims to add (0 to 16 ms) +``` + +An option's `override` has the type of its kind: `bool`, `f32`, `i32`, or `u32` for a choice (the index of the +value). The pack holds only the manifest, the `.wgsl` files it names (each used, UTF-8, 256 KiB at most), +`README.md`, `LICENSE` (`.md`, `.txt`) and `icon.png`, under plain paths, 8 MiB in all. A `.hshader` is built +the same way every time (files in path order, fixed dates), so a pack's SHA-256 depends only on its files. + ## Guide, reference and examples -None yet. When the first stage lands: a guide here, a generated reference (`shaders-reference.md`) and an example pack -in the repository that the renderer's tests load. +- [shaders-reference.md](shaders-reference.md): every stage's signature, input and output structs, resources and + helpers, the frame's fields with their units, option types and limits, generated from `crates/horizon-shader-api`. +- [The guide](../shaders.md): making a pack with `horizon-shader new`, `check` and `pack`; the templates and the + example pack (`crates/horizon-shader-api/packs/golden_hour`). ## Stability @@ -46,9 +91,13 @@ pack exist ([stability.md](stability.md)). ## Not there yet -Everything on this page. The order: the renderer hooks shared with the format loader, then the material and post +The stages and everything that runs a pack (the format above is read and checked). The order: the renderer hooks shared with the format loader, then the material and post stages with options and hot reload, then the overlay, lighting, sky and shadow stages. ## Changelog +- 2026-10-25: `horizon-shader` (`new`, `check`, `pack`), three templates, the example pack and the guide. +- 2026-10-25: the check (naga validation, signatures, options, budgets, SPIR-V/Metal/HLSL translation). +- 2026-10-25: the stages of API 0.1 (sky, material, post), their inputs and the generated reference. +- 2026-10-25: the pack format, schema 1: `horizon-shaders.toml` and the `.hshader` package. - 2026-10-24: the earlier shader pack code was removed; this API is designed from scratch. diff --git a/docs/launcher-commands.md b/docs/launcher-commands.md index 9dc4382f4..7c9562441 100644 --- a/docs/launcher-commands.md +++ b/docs/launcher-commands.md @@ -323,7 +323,7 @@ Search for packs Horizon loads. - `TEXT` - `--kind` (mod, resource, modpack) - `--everything`: Also what was made for other versions or runs only partly, each marked -- `--source` (modrinth, curseforge) +- `--source` (modrinth, curseforge, horizon) MCP: `mods_search`. @@ -335,7 +335,7 @@ Say how well a Java mod runs in Horizon: its jar is downloaded (checksum checked - `PROJECT` - `--everything`: Allow a version made for another Minecraft version (1.21 and later) -- `--source` (modrinth, curseforge) +- `--source` (modrinth, curseforge, horizon) MCP: `mods_check`. @@ -347,7 +347,7 @@ Install a pack and what it needs, by its id or slug (checksums checked); a Java - `PROJECT` - `--everything`: Allow a version made for another Minecraft version -- `--source` (modrinth, curseforge) +- `--source` (modrinth, curseforge, horizon) MCP: `mods_install`. @@ -372,6 +372,26 @@ Remove a pack the browser installed. MCP: `mods_remove`, needs `confirm: true`. +## `horizon-launcher mods enable` + +Turn on a mod the browser installed (horizon-mods.txt, as the game's Options > Mods does). + + horizon-launcher mods enable + +- `PROJECT` + +MCP: `mods_enable`. + +## `horizon-launcher mods disable` + +Turn off a mod the browser installed; it stays installed. + + horizon-launcher mods disable + +- `PROJECT` + +MCP: `mods_disable`. + ## `horizon-launcher servers list` The servers, their folder and whether they run. diff --git a/docs/mod-browser.md b/docs/mod-browser.md index 7a000cb6c..a803420c3 100644 --- a/docs/mod-browser.md +++ b/docs/mod-browser.md @@ -149,6 +149,32 @@ horizon-launcher mods remove faithful-32x ## Sources +**Horizon's registry.** Mods (`.hmod`) and resource packs made for Horizon, reviewed as pull requests in +[ludovic111/horizon-mods](https://github.com/ludovic111/horizon-mods) ([api/mod-index.md](api/mod-index.md)). The +launcher reads its `index.json` (`browser/horizon.rs`; `HORIZON_MOD_INDEX=` reads a test +registry instead, whose files may then be on disk) at most every ten minutes, and keeps the last copy in +`mod-browser/horizon-index.json`. Before a result is installed it shows whether its files are signed and, for a mod, +what it may do, in the words of the game's Options > Mods. A download goes to `mod-browser/downloads/` and is moved +into place only after `horizon_mods::index::check_file` passes (size, SHA-256, the author's signature when the project +has a key, the file's own manifest against the index). A mod becomes `horizon-mods/.hmod`, turned on; +`mods disable` and `mods enable` switch it in `horizon-mods.txt`. When the index takes a file down, the launcher turns +it off the next time it reads the index or starts the game, says why, and refuses to install it or turn it back on. +A Horizon modpack (`.hpack`) installs as its own profile (`browser/horizon/pack.rs`): `horizon-packs//` holds the +checked pack, `pack.json` and `home/`, the profile's game folder, where each of the pack's mods is downloaded and checked +like any registry mod and turned on or off as the pack says; its overrides (settings, resource packs, a mod's saved +settings) are copied in on the first install only, so the player's changes survive updates. A pack whose mod was taken +down installs nothing. It is listed on the Modpacks tab with **Play** and **Remove**, and plays like an imported pack +(`modpacks::apply_profile`). Shader packs are listed by the index but not installed by this launcher yet. + +```sh +horizon-launcher mods search --source horizon --kind mod coords # with whether it is signed and what it may do +horizon-launcher mods install --source horizon coords_hud +horizon-launcher mods disable coords_hud +horizon-launcher mods install --source horizon cozy # a modpack: its own profile +horizon-launcher modpack play cozy +``` + + **Modrinth.** The public API (`api.modrinth.com/v2`) needs no key. The launcher names itself in its user agent, as Modrinth asks. diff --git a/docs/mods.md b/docs/mods.md index 5fa6a290d..bac207f60 100644 --- a/docs/mods.md +++ b/docs/mods.md @@ -378,6 +378,29 @@ none. "top": "carve", "bottom": "none", "rigid_junctions": true, "inflate": true } } ``` +## Publish your mod + +Horizon's registry lists mods, modpacks (`.hpack`) and resource packs made for Horizon, each reviewed before it is +published. Players find them at [playhorizon.dev/mods](https://playhorizon.dev/mods) and in the launcher (Mods, then +**Horizon** beside Modrinth), which checks every file before it installs it. + +1. Run `cargo horizon-mod check` on the file (above): the registry runs the same checks. +2. Put the source somewhere public, under a licence that lets players run it: reviewers read it, and the file must be + built from it. +3. Open [playhorizon.dev/mods/submit](https://playhorizon.dev/mods/submit), sign in with GitHub (it reads only your + public profile) and fill in the page: the file, its name and summary, licence, source address, and optionally a + minisign public key with the file's `.minisig`. +4. The form checks the file again and opens a pull request in + [ludovic111/horizon-mods](https://github.com/ludovic111/horizon-mods) that adds `mods/.toml`, with you as its + owner. That repository's checks run the file in the sandbox and post their report on the pull request; answer the + reviewers there. +5. A reviewer merges it: within minutes the file is a release of horizon-mods, and the project is on the site and in + the launcher. + +An update is the same form with a new version; only the project's `owners` can publish one, signed with its key if it +has one. A published file never changes. Without the form, open the pull request by hand (the repository's README +says how). The formats: [api/mod-index.md](api/mod-index.md). + ## Versions and compatibility - The API is the WIT package `horizon:mods@0.1.0`. New functions and @@ -420,12 +443,10 @@ none. format: a `.hmod.minisig` beside it); the index pins each project's public key, so an update signed by another key is refused. Unsigned local files stay allowed, with an "unsigned" mark on the Mods page. -- **Where mods come from.** Modrinth is where players already look and it - stores files with hashes, but its loader list is Modrinth's to extend: we ask - Modrinth to add a `horizon` loader. Until then (and as a fallback), the - launcher reads a small index of approved mods: a JSON file in a public Git - repository (id, versions, file URLs as GitHub release assets, SHA-256, - signing key). No server of our own is needed. +- **Where mods come from.** Horizon's registry ([Publish your mod](#publish-your-mod)): reviewed pull requests in + [ludovic111/horizon-mods](https://github.com/ludovic111/horizon-mods), files as its release assets, and an + `index.json` on GitHub Pages that the launcher and the website read. A file the registry takes down + (`revoked.toml`) is refused by the launcher and turned off where installed. - **The launcher.** A Mods tab per installation: browse and search the index, install, update, turn on and off (it writes the same `horizon-mods.txt`), see capabilities before installing. The launcher's CLI and MCP tools gain diff --git a/docs/shaders.md b/docs/shaders.md new file mode 100644 index 000000000..99a08fa9a --- /dev/null +++ b/docs/shaders.md @@ -0,0 +1,127 @@ +# Horizon shader packs + +Horizon has its own shader packs: WGSL written against stages of Horizon's renderer, checked before they run. This page +is the guide for pack authors. The design and the pack format are in [api/shaders.md](api/shaders.md); every stage's +signature, input, resource and limit is in the generated [api/shaders-reference.md](api/shaders-reference.md). + +Shader packs in the Iris/OptiFine format are a separate feature with its own loader; this page is only about +Horizon's own format. + +| Part | Where | State | +|---|---|---| +| Format, stages, check | `crates/horizon-shader-api` | API 0.1, experimental | +| Authoring tool | `horizon-shader` (same crate) | `new`, `check`, `pack` | +| Templates | `crates/horizon-shader-api/templates/` | post, sky, material | +| Example pack | `crates/horizon-shader-api/packs/golden_hour` | a warm sky and two post passes | +| In the game, the launcher, the registry | | not yet: the renderer hooks come next | + +## Quick start + +```sh +cargo install --path crates/horizon-shader-api # or: cargo run -p horizon-shader-api --bin horizon-shader -- +horizon-shader new my_pack --template post # also: --template sky, --template material; --name "My Pack" +horizon-shader check my_pack # what the game, the launcher and the registry will check +horizon-shader pack my_pack # check, then write my_pack-0.1.0.hshader beside the folder +``` + +`check` prints each stage with its cost, and the pack's SHA-256: + +``` +golden_hour 1.0.0 (shader API 0.1): ok + sky Warm horizon 99 expressions, 0 texture reads + post Glow 185 expressions, 3 texture reads + post Colour grade 201 expressions, 2 texture reads + sha256 fd92a294... +``` + +or every problem, at its file, line and column: + +``` +my_pack 0.1.0 (shader API 0.1): FAILED + post.wgsl:9:1: expected `;`, found "}" +``` + +`--json` prints the same for tools (`ok`, `id`, `version`, `api`, `sha256`, `stages`, `problems`). + +## A pack + +A folder (or the same files zipped as a `.hshader`) with `horizon-shaders.toml` and WGSL files: + +```toml +schema = 1 +id = "my_pack" +name = "My Pack" +version = "0.1.0" +api = "0.1" + +[[post]] +file = "post.wgsl" + +[[options]] +id = "saturation" +name = "Saturation" +type = "float" +default = 1.15 +min = 0.0 +max = 2.0 +``` + +Every key is in [api/shaders.md](api/shaders.md#the-pack-format-schema-1). + +## Stages + +A stage is one function your file defines. Horizon puts its prelude (the structs, `frame`, the stage's resources and +helpers) before your file and calls the function from its own pass, so the world is still culled, batched and meshed +by Horizon: a pack changes how things look, not how they are drawn. + +| Stage | Your function | What it does | +| --- | --- | --- | +| `[sky]` | `fn sky(input: SkyInput) -> vec4` | the sky's colour along each view ray; the sun, moon, stars and clouds are drawn over it | +| `[material]` | `fn material(input: MaterialInput) -> Surface` | the lit colour of each point of a block's surface, before the alpha test and fog | +| `[[post]]` | `fn post(input: PostInput) -> vec4` | a full-screen pass over the world's picture (up to 8, in order), under the hand and HUD | + +A pack has at least one stage; a stage it leaves out keeps vanilla's look. Each input says what vanilla would draw +(`input.color` in the sky and post stages; `albedo * tint * vec4(lightmap, 1.0)` for a material), so a pack can start +from vanilla and change only what it means to. + +Every stage can read `frame`: the camera's matrices and position, the sun and moon directions, the time of day, +vanilla's sky and fog colours, rain and thunder, fog distances, the light at the camera, effects and the dimension. +Post passes also read `scene_color`, `scene_depth`, `previous` (the last pass's output), two samplers and the helpers +`depth_at(uv)` and `view_position(uv, depth)`. + +Rules the check enforces: your files declare no bindings (`@group`/`@binding`), no entry points and no names starting +with `horizon_`. Code shared by several stages goes in files listed in `shared`, put before every stage. + +## Options + +Each `[[options]]` entry is a WGSL `override` of the same name, set when the game builds the pipeline: no text +substitution, no recompiling a different program per value. + +```wgsl +override saturation: f32 = 1.15; // type = "float"; bool → bool, int → i32, choice → u32 (the value's index) +``` + +The game shows the options on the pack's settings screen. Branches on an option are on a constant, so the GPU +skips the side that isn't taken. + +## What the check does + +The game runs the same check when it loads a pack (a pack that fails is refused with the message, and the last good +one keeps drawing), the launcher before it installs one, and the registry's CI before a pack is listed: + +1. The manifest and the package: known keys, valid ids, every file used, sizes. +2. Each stage built as the renderer builds it and validated by naga. +3. The stage function's signature, and the rules above. +4. Each `override` matches its option's type (or has a default); each option is used. +5. Budgets: 20 000 expressions and 64 texture reads per stage. +6. Every entry point translated for Vulkan (SPIR-V), Metal and DirectX 12 (HLSL), with the options at their + defaults, minimums and maximums. + +`[budget] gpu_ms` is the GPU time your pack aims to add to a frame; the game's overlay will show each pass's time +against it. + +## Next + +The renderer hooks (shared with the Iris/OptiFine loader), then the post stage in the game with hot reload and the +GPU time per pass, Options > Shader packs listing these packs, then the material and sky stages, and the registry's +`shader` kind with installs from the launcher. diff --git a/launcher/Cargo.lock b/launcher/Cargo.lock index 5155420e0..a416a39ff 100644 --- a/launcher/Cargo.lock +++ b/launcher/Cargo.lock @@ -2673,6 +2673,7 @@ dependencies = [ "horizon-bench", "horizon-core", "horizon-friends", + "horizon-mods", "horizon-port-pack", "horizon-prepare", "horizon-release-sign", @@ -2698,8 +2699,10 @@ name = "horizon-mods" version = "0.1.0" dependencies = [ "log", + "minisign-verify", "serde", "serde_json", + "sha2 0.10.9", "toml 1.1.8+spec-1.1.0", "zip", ] @@ -3470,6 +3473,12 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" +[[package]] +name = "minisign-verify" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "871285dc19d8d0ebe0eef3d0e99a205f2a71363b122632cbbfa4a6c370a960ce" + [[package]] name = "miniz_oxide" version = "0.8.9" diff --git a/launcher/Cargo.toml b/launcher/Cargo.toml index a402d0e28..cfe9787d6 100644 --- a/launcher/Cargo.toml +++ b/launcher/Cargo.toml @@ -31,6 +31,8 @@ horizon-friends = { path = "../crates/horizon-friends" } horizon-bench = { path = "../crates/horizon-bench" } # Import modpack: the porting pipeline, run on the player's own files (docs/modpacks.md). horizon-port-pack = { path = "../crates/horizon-port-pack" } +# Horizon's mod registry: its index and the checks a download passes before install. +horizon-mods = { path = "../crates/horizon-mods", features = ["index"] } # Signed updates: archives are checked against the release's signed checksums (docs/security.md). horizon-release-sign = { path = "../crates/horizon-release-sign" } # Opt-in data collection: the consent, the outbox and its upload (docs/telemetry.md). diff --git a/launcher/src/backend.rs b/launcher/src/backend.rs index 270f856a0..9d1eeb111 100644 --- a/launcher/src/backend.rs +++ b/launcher/src/backend.rs @@ -1636,6 +1636,7 @@ pub fn need_prepared(tag: &str) -> Result<(), String> { pub fn client(tag: &str) -> Result { need_prepared(tag)?; complete_packs(tag); + crate::browser::horizon::before_play(&game_home()); let mut cmd = client_command(tag); // A sign-in that can't be renewed (no network, expired) still lets the game start, offline. if let Err(e) = crate::account::attach_session(&mut cmd, &game_home()) { diff --git a/launcher/src/browser.rs b/launcher/src/browser.rs index f02638db5..7f81d5e37 100644 --- a/launcher/src/browser.rs +++ b/launcher/src/browser.rs @@ -8,6 +8,7 @@ pub mod curseforge; pub mod details; pub mod download; +pub mod horizon; pub mod images; pub mod installed; pub mod java; @@ -56,6 +57,8 @@ impl Kind { pub enum SourceId { Modrinth, CurseForge, + /// Horizon's own registry of reviewed mods, modpacks and resource packs ([`horizon`]). + Horizon, } impl SourceId { @@ -63,6 +66,7 @@ impl SourceId { match self { SourceId::Modrinth => "Modrinth", SourceId::CurseForge => "CurseForge", + SourceId::Horizon => "Horizon", } } } @@ -132,6 +136,8 @@ pub struct FileRef { pub name: String, pub size: u64, pub sha512: Option, + /// What Horizon's registry lists. + pub sha256: Option, pub sha1: Option, pub primary: bool, } @@ -139,11 +145,13 @@ pub struct FileRef { impl FileRef { /// The strongest checksum the source gives: what the download is checked against. pub fn checksum(&self) -> Option<(&'static str, &str)> { - match (&self.sha512, &self.sha1) { - (Some(h), _) => Some(("SHA-512", h)), - (None, Some(h)) => Some(("SHA-1", h)), - (None, None) => None, - } + [ + ("SHA-512", &self.sha512), + ("SHA-256", &self.sha256), + ("SHA-1", &self.sha1), + ] + .into_iter() + .find_map(|(algo, h)| Some((algo, h.as_deref()?))) } } @@ -230,10 +238,11 @@ pub trait Source: Send + Sync { /// What the launcher says wherever CurseForge would be. pub const CURSEFORGE_SOON: &str = "CurseForge support is coming soon."; -/// The sources this launcher uses: Modrinth. CurseForge support is coming soon: its API needs a -/// paid key, so no search or install goes to it; `curseforge` is kept for then. +/// The sources this launcher uses: Modrinth and Horizon's registry. CurseForge support is coming +/// soon: its API needs a paid key, so no search or install goes to it; `curseforge` is kept for +/// then. pub fn sources() -> Vec> { - vec![Box::new(modrinth::Modrinth)] + vec![Box::new(modrinth::Modrinth), Box::new(horizon::Registry)] } /// The source of that id, when this launcher can use it. @@ -243,6 +252,15 @@ pub fn source(id: SourceId) -> Option> { /// How well a project of `kind` made for `game_versions` and `loaders` runs in Horizon. pub fn support(kind: Kind, game_versions: &[String], loaders: &[String]) -> Support { + if loaders.iter().any(|l| l == horizon::LOADER) { + // Made for Horizon and reviewed: it runs when it is for the version Horizon plays. + return match newest_release(game_versions) + .filter(|_| !game_versions.iter().any(|v| v == GAME)) + { + Some(other) => Support::No(format!("Made for Horizon on {other}.")), + None => Support::Works, + }; + } match kind { Kind::ResourcePack => { if game_versions.iter().any(|v| v == GAME) { @@ -309,6 +327,12 @@ pub fn pick(kind: Kind, versions: &[Version], everything: bool) -> Option<&Versi let mut usable = versions .iter() .filter(|v| v.blocked.is_none() && v.file().is_some()); + if versions + .iter() + .any(|v| v.loaders.iter().any(|l| l == horizon::LOADER)) + { + return usable.find(|v| v.game_versions.iter().any(|g| g == GAME)); + } if kind == Kind::Mod { let java = |v: &&Version| { v.loaders diff --git a/launcher/src/browser/curseforge.rs b/launcher/src/browser/curseforge.rs index 4894fe5f0..e17a6b822 100644 --- a/launcher/src/browser/curseforge.rs +++ b/launcher/src/browser/curseforge.rs @@ -164,6 +164,7 @@ pub(super) fn parse_file(f: &Value) -> Option { name: name.clone(), size: f["fileLength"].as_u64().unwrap_or(0), sha512: None, + sha256: None, sha1, primary: true, }] diff --git a/launcher/src/browser/download.rs b/launcher/src/browser/download.rs index 66199fd07..593273245 100644 --- a/launcher/src/browser/download.rs +++ b/launcher/src/browser/download.rs @@ -1,16 +1,18 @@ //! Downloads that reach the game folder only when their checksum matches the source's. use super::{safe_name, FileRef}; use sha1::Sha1; -use sha2::{Digest, Sha512}; +use sha2::{Digest, Sha256, Sha512}; use std::fs; use std::io::{Read, Write}; use std::path::{Path, PathBuf}; -/// Hosts a file may come from: Modrinth's and CurseForge's file servers. -const HOSTS: [&str; 3] = [ +/// Hosts a file may come from: Modrinth's and CurseForge's file servers, and the release assets +/// of Horizon's registry. +const HOSTS: [&str; 4] = [ "https://cdn.modrinth.com/", "https://edge.forgecdn.net/", "https://mediafilez.forgecdn.net/", + horizon_mods::index::FILE_HOST, ]; /// Bigger than any pack these sources serve (Modrinth caps uploads at 500 MiB). const MAX: u64 = 1 << 30; @@ -31,10 +33,15 @@ pub(super) fn agent() -> ureq::Agent { pub fn refuse(file: &FileRef) -> Option { if !HOSTS.iter().any(|h| file.url.starts_with(h)) { return Some(format!( - "{} isn't on Modrinth's or CurseForge's file servers.", + "{} isn't on Modrinth's, CurseForge's or Horizon's file servers.", file.url )); } + refuse_file(file) +} + +/// [`refuse`] without the host: the name, the checksum and the size. +fn refuse_file(file: &FileRef) -> Option { if safe_name(&file.name).is_none() { return Some(format!("{:?} isn't a plain file name.", file.name)); } @@ -50,26 +57,29 @@ pub fn refuse(file: &FileRef) -> Option { /// Hashes as they are written: the strongest one the file lists. enum Hasher { Sha512(Sha512), + Sha256(Sha256), Sha1(Sha1), } impl Hasher { fn for_file(file: &FileRef) -> Self { - if file.sha512.is_some() { - Hasher::Sha512(Sha512::new()) - } else { - Hasher::Sha1(Sha1::new()) + match file.checksum() { + Some(("SHA-512", _)) => Hasher::Sha512(Sha512::new()), + Some(("SHA-256", _)) => Hasher::Sha256(Sha256::new()), + _ => Hasher::Sha1(Sha1::new()), } } fn update(&mut self, bytes: &[u8]) { match self { Hasher::Sha512(h) => h.update(bytes), + Hasher::Sha256(h) => h.update(bytes), Hasher::Sha1(h) => h.update(bytes), } } fn hex(self) -> String { let bytes: Vec = match self { Hasher::Sha512(h) => h.finalize().to_vec(), + Hasher::Sha256(h) => h.finalize().to_vec(), Hasher::Sha1(h) => h.finalize().to_vec(), }; bytes.iter().map(|b| format!("{b:02x}")).collect() @@ -81,13 +91,26 @@ impl Hasher { /// `progress` gets the fraction done. pub fn save( file: &FileRef, - mut reader: impl Read, + reader: impl Read, folder: &Path, progress: &dyn Fn(f32), ) -> Result { if let Some(why) = refuse(file) { return Err(why); } + save_from_anywhere(file, reader, folder, progress) +} + +/// [`save`] for a file that comes from no host: a local test registry's. +pub(super) fn save_from_anywhere( + file: &FileRef, + mut reader: impl Read, + folder: &Path, + progress: &dyn Fn(f32), +) -> Result { + if let Some(why) = refuse_file(file) { + return Err(why); + } let (algo, want) = file.checksum().expect("refuse checks for one"); fs::create_dir_all(folder).map_err(|e| format!("{}: {e}", folder.display()))?; let target = folder.join(&file.name); diff --git a/launcher/src/browser/horizon.rs b/launcher/src/browser/horizon.rs new file mode 100644 index 000000000..e56f31e09 --- /dev/null +++ b/launcher/src/browser/horizon.rs @@ -0,0 +1,553 @@ +//! Horizon's own registry (docs/api/mod-index.md): mods, modpacks and resource packs made for +//! Horizon, reviewed as pull requests in ludovic111/horizon-mods. One file lists them all, +//! `index.json`; every download is checked against it before it reaches the game folder +//! (`horizon_mods::index::check_file`: size, SHA-256, the author's signature, the file's own +//! manifest), and files the index says were taken down are refused and turned off. +use super::{ + details::{Details, Link}, + download, installed, java, FileRef, Kind, Project, Query, Source, SourceId, Version, GAME, +}; +use horizon_mods::index::{self, Index, Trust}; +use std::fs; +use std::path::{Path, PathBuf}; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; + +/// Where the registry publishes its index (GitHub Pages of ludovic111/horizon-mods). +pub const INDEX_URL: &str = "https://ludovic111.github.io/horizon-mods/index.json"; +/// The registry's pages on the website, `/`. +const SITE: &str = "https://playhorizon.dev/mods"; +/// The report form of a project. +const REPORT: &str = "https://github.com/ludovic111/horizon-mods/issues/new?template=report.yml"; +/// The loader name the browser gives registry versions, so `support` and `pick` know them. +pub const LOADER: &str = "horizon"; +/// How long a fetched index is used before it is fetched again. +const FRESH: Duration = Duration::from_secs(600); +/// Larger than any index should be. +const MAX_INDEX: u64 = 16 << 20; +/// The last index fetched, kept in the game folder for when the registry can't be reached. +const CACHE: &str = "mod-browser/horizon-index.json"; + +/// Where the index comes from: `HORIZON_MOD_INDEX` (a file or an `https://` address, for a +/// test registry), else [`INDEX_URL`]. +fn location() -> String { + std::env::var("HORIZON_MOD_INDEX") + .ok() + .filter(|v| !v.is_empty()) + .unwrap_or_else(|| INDEX_URL.to_owned()) +} + +/// True when the index is a local file: its files may then be local too (`file://`), for +/// trying a registry out before it is published. +pub fn local() -> bool { + !location().starts_with("https://") +} + +static LOADED: Mutex)>> = Mutex::new(None); + +/// The index read last in this run, without asking the network: for what must not wait. +pub fn known_index() -> Option> { + LOADED + .lock() + .unwrap() + .as_ref() + .map(|(_, index)| index.clone()) +} + +/// The index: the one fetched in the last ten minutes, else fetched again (and kept in the game +/// folder), else the copy kept last time. +pub fn index() -> Result, String> { + if let Some((at, index)) = LOADED.lock().unwrap().as_ref() { + if at.elapsed() < FRESH { + return Ok(index.clone()); + } + } + let cache = crate::backend::game_home().join(CACHE); + let index = match fetch_index() { + Ok((index, text)) => { + if let Some(dir) = cache.parent() { + let _ = fs::create_dir_all(dir); + } + let _ = fs::write(&cache, text); + index + } + Err(e) => { + let kept = fs::read_to_string(&cache).map_err(|_| e.clone())?; + match local() { + true => Index::parse_local(&kept), + false => Index::parse(&kept), + } + .map_err(|_| e)? + } + }; + let index = Arc::new(index); + *LOADED.lock().unwrap() = Some((Instant::now(), index.clone())); + Ok(index) +} + +fn fetch_index() -> Result<(Index, String), String> { + use std::io::Read; + let at = location(); + let text = if at.starts_with("https://") { + let response = download::agent().get(&at).call().map_err(|e| match e { + ureq::Error::Status(code, _) => format!("The mod registry answered {code}."), + ureq::Error::Transport(t) => format!("The mod registry can't be reached: {t}"), + })?; + let mut text = String::new(); + response + .into_reader() + .take(MAX_INDEX) + .read_to_string(&mut text) + .map_err(|e| e.to_string())?; + text + } else { + fs::read_to_string(&at).map_err(|e| format!("{at}: {e}"))? + }; + let index = match local() { + true => Index::parse_local(&text)?, + false => Index::parse(&text)?, + }; + Ok((index, text)) +} + +/// The browser's kind for a registry kind; None for one the launcher doesn't install yet +/// (shader packs, kinds newer than this launcher). +fn kind_of(kind: &index::Kind) -> Option { + match kind { + index::Kind::Mod => Some(Kind::Mod), + index::Kind::ResourcePack => Some(Kind::ResourcePack), + index::Kind::Modpack => Some(Kind::Modpack), + index::Kind::Shader | index::Kind::Other(_) => None, + } +} + +/// What the player reads about a registry project before installing it: whether its files are +/// signed, and for a mod what it may do, in the game's words (Options > Mods). +pub fn trust_and_capabilities(id: &str) -> Vec { + let Ok(index) = index() else { + return Vec::new(); + }; + let Some(p) = index.project(id) else { + return Vec::new(); + }; + let mut lines = vec![match &p.key { + Some(_) => "Signed by its author: the launcher checks the signature".to_owned(), + None => "Unsigned: the launcher checks its checksum only".to_owned(), + }]; + if let Some(v) = p.versions.first().filter(|_| p.kind == index::Kind::Mod) { + lines.extend( + v.capabilities + .iter() + .map(|c| format!("May {}", c.describe())), + ); + if v.capabilities.is_empty() { + lines.push("Asks for nothing beyond its own log".into()); + } + } + lines +} + +fn project_of(p: &index::Project) -> Option { + let mut game_versions: Vec = p.versions.iter().map(|v| v.game.clone()).collect(); + game_versions.dedup(); + Some(Project { + source: SourceId::Horizon, + id: p.id.clone(), + slug: p.id.clone(), + title: p.name.clone(), + summary: p.summary.clone(), + author: p.authors.join(", "), + downloads: 0, + page: format!("{SITE}/{}", p.id), + icon: p.icon.clone(), + license: p.license.clone(), + kind: kind_of(&p.kind)?, + game_versions, + loaders: vec![LOADER.into()], + }) +} + +fn version_of(index: &Index, p: &index::Project, v: &index::Version) -> Version { + Version { + id: v.version.clone(), + number: v.version.clone(), + published: v.published.clone(), + game_versions: vec![v.game.clone()], + loaders: vec![LOADER.into()], + files: vec![FileRef { + url: v.file.url.clone(), + name: v.file.name.clone(), + size: v.file.size, + sha512: None, + sha256: Some(v.file.sha256.clone()), + sha1: None, + primary: true, + }], + required: v.requires.iter().map(|r| r.id.clone()).collect(), + blocked: index + .revoked(&p.id, &v.file.sha256) + .map(|r| format!("taken down: {}", r.reason)), + } +} + +/// A GitHub login as GitHub allows it (letters, digits, single dashes, at most 39), so an index +/// entry can't turn a link into something else. +fn github_login(login: &str) -> bool { + (1..=39).contains(&login.len()) + && login + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b == b'-') + && !login.starts_with('-') + && !login.ends_with('-') + && !login.contains("--") +} + +/// The words of a query found in a project's id, name, summary or authors. +fn matches(p: &index::Project, text: &str) -> bool { + let hay = format!("{} {} {} {}", p.id, p.name, p.summary, p.authors.join(" ")).to_lowercase(); + text.split_whitespace() + .all(|word| hay.contains(&word.to_lowercase())) +} + +/// The projects of the query's kind whose words match, for this game unless `everything`. +fn search(index: &Index, query: &Query) -> Vec { + index + .projects + .iter() + .filter(|p| kind_of(&p.kind) == Some(query.kind) && matches(p, &query.text)) + .filter(|p| query.everything || p.versions.iter().any(|v| v.game == GAME)) + .filter_map(project_of) + .skip(query.offset) + .take(query.limit) + .collect() +} + +/// The registry as a browser source. +pub struct Registry; + +impl Source for Registry { + fn id(&self) -> SourceId { + SourceId::Horizon + } + + fn search(&self, query: &Query) -> Result, String> { + Ok(search(&*index()?, query)) + } + + fn project(&self, id: &str) -> Result { + let index = index()?; + let p = index + .project(id) + .ok_or_else(|| format!("{id} isn't in Horizon's registry."))?; + project_of(p).ok_or_else(|| { + format!( + "{} is a {} in Horizon's registry: this launcher can't install those yet.", + p.name, + p.kind.name() + ) + }) + } + + fn versions(&self, project: &Project) -> Result, String> { + let index = index()?; + let p = index + .project(&project.id) + .ok_or_else(|| format!("{} isn't in Horizon's registry.", project.title))?; + Ok(p.versions + .iter() + .map(|v| version_of(&index, p, v)) + .collect()) + } + + fn details(&self, project: &Project) -> Result { + let index = index()?; + let p = index + .project(&project.id) + .ok_or_else(|| format!("{} isn't in Horizon's registry.", project.title))?; + let newest = p.versions.first(); + let mut body = p.description.clone(); + if let Some(v) = newest.filter(|v| !v.capabilities.is_empty()) { + body.push_str("\n\n## What it may do\n\n"); + for c in &v.capabilities { + body.push_str(&format!("- May {}\n", c.describe())); + } + } + if let Some(v) = newest.filter(|v| !v.requires.is_empty()) { + body.push_str( + "\n\n## Its mods\n\nInstalled with it into its own profile, each checked:\n\n", + ); + for r in &v.requires { + let found = index + .project(&r.id) + .and_then(|m| Some((m, m.versions.iter().find(|w| w.version == r.version)?))); + let Some((m, w)) = found else { continue }; + let may: Vec<&str> = w.capabilities.iter().map(|c| c.describe()).collect(); + let may = match may.is_empty() { + true => String::new(), + false => format!(": may {}", may.join(", ")), + }; + body.push_str(&format!("- {} {}{may}\n", m.name, w.version)); + } + } + // Who publishes it: the GitHub accounts the registry checked at submission. + let owners = p.owners.iter().filter(|o| github_login(o)).map(|o| Link { + label: format!("@{o}"), + url: format!("https://github.com/{o}"), + }); + Ok(Details { + body, + links: [ + Link { + label: "Source".into(), + url: p.source.clone(), + }, + Link { + label: "Page".into(), + url: project.page.clone(), + }, + Link { + label: "Report".into(), + url: format!("{REPORT}&title={}", p.id), + }, + ] + .into_iter() + .chain(owners) + .collect(), + authors: p.authors.clone(), + published: p + .versions + .last() + .map(|v| v.published.clone()) + .unwrap_or_default(), + updated: newest.map(|v| v.published.clone()).unwrap_or_default(), + ..Details::default() + }) + } + + fn fetch( + &self, + file: &FileRef, + folder: &Path, + progress: &dyn Fn(f32), + ) -> Result { + fetch(file, folder, local(), progress) + } +} + +/// Downloads a registry file, checked; `file://` files only from a local index. +fn fetch( + file: &FileRef, + folder: &Path, + local: bool, + progress: &dyn Fn(f32), +) -> Result { + match file.url.strip_prefix("file://").filter(|_| local) { + Some(path) => { + let reader = fs::File::open(path).map_err(|e| format!("{path}: {e}"))?; + download::save_from_anywhere(file, reader, folder, progress) + } + None => download::fetch(file, folder, progress), + } +} + +/// What the registry adds to an installed entry. +fn trust_label(trust: Trust) -> &'static str { + match trust { + Trust::Verified => "Verified", + Trust::Unsigned => "Unsigned", + } +} + +/// The game folder path a registry project's file goes to. +fn relative(kind: Kind, id: &str, file_name: &str) -> Result { + match kind { + Kind::Mod => Ok(format!("{}/{id}.hmod", java::MODS_DIR)), + Kind::ResourcePack => Ok(format!("resourcepacks/{file_name}")), + Kind::Modpack => Err("A modpack installs as its own profile (pack::install).".into()), + } +} + +/// Installs a registry project into the game folder `home`: its newest version for this game, +/// downloaded into `mod-browser/downloads/`, checked against the index, then moved into place. +/// A mod is `horizon-mods/.hmod`, turned on in `horizon-mods.txt`. +pub fn install( + project: &Project, + home: &Path, + progress: &dyn Fn(&str, f32), +) -> Result, String> { + install_from(&*index()?, local(), project, home, progress) +} + +fn install_from( + index: &Index, + local: bool, + project: &Project, + home: &Path, + progress: &dyn Fn(&str, f32), +) -> Result, String> { + let p = index + .project(&project.id) + .ok_or_else(|| format!("{} isn't in Horizon's registry.", project.title))?; + let v = newest(index, p)?; + install_version(index, local, p, v, home, progress).map(|e| vec![e]) +} + +/// A project's newest version for this game that wasn't taken down. +fn newest<'a>(index: &Index, p: &'a index::Project) -> Result<&'a index::Version, String> { + let mut taken_down = None; + for v in p.versions.iter().filter(|v| v.game == GAME) { + match index.revoked(&p.id, &v.file.sha256) { + None => return Ok(v), + Some(r) => { + taken_down.get_or_insert(r); + } + } + } + Err(match taken_down { + Some(r) => format!("{} was taken down: {}", p.name, r.reason), + None => format!("{} has no version for {GAME} to install.", p.name), + }) +} + +/// Installs version `v` of the registry's mod or resource pack `p` into the game folder `home`. +fn install_version( + index: &Index, + local: bool, + p: &index::Project, + v: &index::Version, + home: &Path, + progress: &dyn Fn(&str, f32), +) -> Result { + let project = project_of(p).ok_or_else(|| format!("{} can't be installed here.", p.name))?; + if let Some(r) = index.revoked(&p.id, &v.file.sha256) { + return Err(format!("{} was taken down: {}", p.name, r.reason)); + } + let relative = relative(project.kind, &p.id, &v.file.name)?; + let mut list = installed::Installed::load(home); + let ours = |e: &installed::Entry| e.source == SourceId::Horizon && e.project == p.id; + let old = list.entries.iter().find(|e| ours(e)).cloned(); + if old.as_ref().is_some_and(|o| o.version == v.version) { + return Ok(old.expect("checked")); + } + let target = home.join(&relative); + let taken_by = list.entries.iter().find(|e| !ours(e) && e.file == relative); + let folder_mod = home.join(java::MODS_DIR).join(&p.id); + if let Some(other) = taken_by { + return Err(format!( + "{relative} belongs to {}: remove it first.", + other.title + )); + } + if (target.exists() && old.as_ref().is_none_or(|o| o.file != relative)) || folder_mod.exists() { + return Err(format!( + "A mod with the id {} is already in {}: move it away first.", + p.id, + java::MODS_DIR + )); + } + let line = format!("{} {}", p.name, v.version); + let file = &version_of(index, p, v).files[0]; + let staging = home.join("mod-browser/downloads"); + let path = fetch(file, &staging, local, &|f| progress(&line, f))?; + let trust = index::check_file(index, p, v, &path).inspect_err(|_| { + let _ = fs::remove_file(&path); + })?; + if let Some(dir) = target.parent() { + fs::create_dir_all(dir).map_err(|e| format!("{}: {e}", dir.display()))?; + } + fs::rename(&path, &target).map_err(|e| format!("{}: {e}", target.display()))?; + if let Some(old) = old.as_ref().filter(|o| o.file != relative) { + let _ = fs::remove_file(home.join(&old.file)); + } + if project.kind == Kind::Mod { + java::set_enabled(home, &p.id, true)?; + } + let entry = installed::Entry { + source: SourceId::Horizon, + project: p.id.clone(), + slug: p.id.clone(), + title: p.name.clone(), + kind: project.kind, + version: v.version.clone(), + number: v.version.clone(), + file: relative, + checksum: format!("SHA-256:{}", v.file.sha256), + page: project.page.clone(), + dependency: false, + verdict: Some(trust_label(trust).into()), + skipped: Vec::new(), + namespaces: Vec::new(), + }; + list.entries.retain(|e| !ours(e)); + list.entries.push(entry.clone()); + list.save(home)?; + Ok(entry) +} + +/// The mod id of an installed mod (`horizon-mods/` or `horizon-mods/.hmod`). +pub fn mod_id(entry: &installed::Entry) -> Option<&str> { + let rest = entry.file.strip_prefix(java::MODS_DIR)?.strip_prefix('/')?; + Some(rest.strip_suffix(".hmod").unwrap_or(rest)) +} + +/// Turns the registry's files that were taken down off in the game folder `home`, and says +/// which. Their files stay, so the player sees them and can remove them. +pub fn turn_off_revoked(index: &Index, home: &Path) -> Vec { + let mut out = Vec::new(); + for e in installed::Installed::load(home).entries { + let hash = e.checksum.strip_prefix("SHA-256:").unwrap_or_default(); + let Some(r) = (e.source == SourceId::Horizon) + .then(|| index.revoked(&e.project, hash)) + .flatten() + else { + continue; + }; + if let (Kind::Mod, Some(id)) = (e.kind, mod_id(&e)) { + if java::is_enabled(home, id) && java::set_enabled(home, id, false).is_ok() { + out.push(format!( + "{} was taken down and is off: {}", + e.title, r.reason + )); + } + } else if e.kind == Kind::ResourcePack { + out.push(format!( + "{} was taken down: {} Remove it.", + e.title, r.reason + )); + } + } + out +} + +/// Before the game starts: registry files taken down are turned off, by the index kept last +/// time (no waiting on the network), and the index is fetched again in the background for the +/// next start. Nothing happens when nothing came from the registry. +pub fn before_play(home: &Path) { + let from_registry = installed::Installed::load(home) + .entries + .iter() + .any(|e| e.source == SourceId::Horizon); + if !from_registry { + return; + } + let kept = fs::read_to_string(crate::backend::game_home().join(CACHE)).ok(); + let parse = |t: String| match local() { + true => Index::parse_local(&t).ok(), + false => Index::parse(&t).ok(), + }; + if let Some(index) = kept.and_then(parse) { + for line in turn_off_revoked(&index, home) { + eprintln!("{line}"); + } + } + let home = home.to_owned(); + std::thread::spawn(move || { + if let Ok(index) = index() { + turn_off_revoked(&index, &home); + } + }); +} + +pub mod pack; +#[cfg(test)] +mod tests; diff --git a/launcher/src/browser/horizon/pack.rs b/launcher/src/browser/horizon/pack.rs new file mode 100644 index 000000000..73dcd0cc1 --- /dev/null +++ b/launcher/src/browser/horizon/pack.rs @@ -0,0 +1,294 @@ +//! Horizon modpacks from the registry (`.hpack`, docs/api/mod-index.md), each played as its own +//! profile: `horizon-packs//` in the game's home holds the pack's file, `pack.json` (what +//! was installed) and `home/`, the profile's game folder: its saves, its options, and the pack's +//! mods in `home/horizon-mods/`, each downloaded and checked against the index like any +//! registry mod, turned on or off as the pack says. The pack's overrides (settings, resource +//! packs, a mod's saved settings) are copied in once, when it is installed. +use super::{fetch, index, install_version, local, newest, Index}; +use crate::browser::{java, FileRef}; +use horizon_mods::{index as idx, modpack::Modpack}; +use serde::{Deserialize, Serialize}; +use std::fs; +use std::path::{Path, PathBuf}; +use std::process::Command; + +const RECORD: &str = "pack.json"; + +/// Where registry modpacks live. +pub fn packs_dir() -> PathBuf { + crate::backend::game_home().join("horizon-packs") +} + +/// What `pack.json` keeps of an installed pack. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct Installed { + pub id: String, + pub name: String, + pub version: String, + pub sha256: String, + /// The pack's mods: `id version`. + pub mods: Vec, + #[serde(skip)] + pub work: PathBuf, +} + +/// The installed registry packs, by name. +pub fn list() -> Vec { + list_in(&packs_dir()) +} + +fn list_in(dir: &Path) -> Vec { + let mut packs: Vec = fs::read_dir(dir) + .into_iter() + .flatten() + .flatten() + .filter_map(|e| read(&e.path())) + .collect(); + packs.sort_by_key(|p| p.name.to_lowercase()); + packs +} + +fn read(work: &Path) -> Option { + let text = fs::read_to_string(work.join(RECORD)).ok()?; + let mut pack: Installed = serde_json::from_str(&text).ok()?; + pack.work = work.to_path_buf(); + Some(pack) +} + +/// Whether `work` is a registry pack's folder. +pub fn is_pack(work: &Path) -> bool { + work.join(RECORD).is_file() +} + +/// The profile's game folder. +pub fn home(work: &Path) -> PathBuf { + work.join("home") +} + +/// Installs the registry modpack `id`: its newest version for this game, then each of its mods. +/// Installing it again updates it; its saves and options stay. +pub fn install(id: &str, progress: &dyn Fn(&str, f32)) -> Result { + install_in(&*index()?, local(), id, &packs_dir(), progress) +} + +fn install_in( + index: &Index, + local: bool, + id: &str, + dir: &Path, + progress: &dyn Fn(&str, f32), +) -> Result { + let p = index + .project(id) + .ok_or_else(|| format!("{id} isn't in Horizon's registry."))?; + if p.kind != idx::Kind::Modpack { + return Err(format!("{} is a {}, not a modpack.", p.name, p.kind.name())); + } + let v = newest(index, p)?; + let work = dir.join(&p.id); + let fresh = !work.exists(); + let result = install_version_in(index, local, p, v, &work, progress); + if result.is_err() && fresh { + let _ = fs::remove_dir_all(&work); + } + result +} + +fn install_version_in( + index: &Index, + local: bool, + p: &idx::Project, + v: &idx::Version, + work: &Path, + progress: &dyn Fn(&str, f32), +) -> Result { + let line = format!("{} {}", p.name, v.version); + let file = FileRef { + url: v.file.url.clone(), + name: v.file.name.clone(), + size: v.file.size, + sha512: None, + sha256: Some(v.file.sha256.clone()), + sha1: None, + primary: true, + }; + let path = fetch(&file, &work.join("downloads"), local, &|f| { + progress(&line, f) + })?; + idx::check_file(index, p, v, &path).inspect_err(|_| { + let _ = fs::remove_file(&path); + })?; + let pack = Modpack::read(&path)?; + let home = home(work); + fs::create_dir_all(&home).map_err(|e| format!("{}: {e}", home.display()))?; + let mut mods = Vec::new(); + for m in &pack.manifest.mods { + let found = index + .project(&m.id) + .and_then(|mp| Some((mp, mp.versions.iter().find(|w| w.version == m.version)?))); + let Some((mp, mv)) = found.filter(|(_, mv)| mv.file.sha256 == m.sha256) else { + return Err(format!( + "{} needs {} {}, which isn't in the registry.", + p.name, m.id, m.version + )); + }; + install_version(index, local, mp, mv, &home, progress)?; + java::set_enabled(&home, &m.id, m.on)?; + mods.push(format!("{} {}", m.id, m.version)); + } + // Settings once: the player's own changes to the profile stay on updates. + let first = !work.join(RECORD).exists(); + if first { + Modpack::extract_overrides(&path, &home)?; + } + let kept = work.join(&v.file.name); + fs::rename(&path, &kept).map_err(|e| format!("{}: {e}", kept.display()))?; + let record = Installed { + id: p.id.clone(), + name: p.name.clone(), + version: v.version.clone(), + sha256: v.file.sha256.clone(), + mods, + work: work.to_path_buf(), + }; + let text = serde_json::to_string_pretty(&record).map_err(|e| e.to_string())?; + fs::write(work.join(RECORD), text).map_err(|e| e.to_string())?; + Ok(record) +} + +/// Turns the game's command into the pack's profile: its own game folder (the player's +/// options.txt copied in when the pack brings none), its taken-down mods turned off first. +pub fn apply(cmd: &mut Command, work: &Path) -> Result<(), String> { + if !is_pack(work) { + return Err(format!("{} is not an installed modpack.", work.display())); + } + let home = home(work); + fs::create_dir_all(&home).map_err(|e| format!("{}: {e}", home.display()))?; + let options = home.join("options.txt"); + let own = crate::backend::game_home().join("options.txt"); + if !options.exists() && own.is_file() { + let _ = fs::copy(own, &options); + } + super::before_play(&home); + cmd.current_dir(&home).env("HORIZON_HOME", &home); + Ok(()) +} + +/// Removes an installed pack: its mods and the profile's saves. +pub fn remove(work: &Path) -> Result<(), String> { + if work.parent() != Some(packs_dir().as_path()) || !is_pack(work) { + return Err(format!("{} is not an installed modpack.", work.display())); + } + fs::remove_dir_all(work).map_err(|e| format!("{}: {e}", work.display())) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::io::Write; + + /// A local registry: the mod and a pack of it, as `file://` files. + fn registry(dir: &Path, on: bool, extra: &str) -> Index { + let hmod_src = Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../crates/horizon-mods/tests/index/coords_hud-0.1.0.hmod"); + let hmod = dir.join("coords_hud-0.1.0.hmod"); + fs::copy(hmod_src, &hmod).unwrap(); + let hmod_sha = idx::sha256_file(&hmod).unwrap(); + let hpack = dir.join("cozy-1.0.0.hpack"); + let mut zip = zip::ZipWriter::new(fs::File::create(&hpack).unwrap()); + let o = zip::write::SimpleFileOptions::default(); + zip.start_file("horizon-modpack.toml", o).unwrap(); + write!( + zip, + "format = 1\nid = \"cozy\"\nname = \"Cozy\"\nversion = \"1.0.0\"\ngame = \"26.3\"\n\ + [[mods]]\nid = \"coords_hud\"\nversion = \"0.1.0\"\nsha256 = \"{hmod_sha}\"\non = {on}\n" + ) + .unwrap(); + zip.start_file("overrides/options.txt", o).unwrap(); + zip.write_all(b"renderDistance:8\n").unwrap(); + zip.finish().unwrap(); + let size = |p: &Path| fs::metadata(p).unwrap().len(); + let text = format!( + r#"{{"schema": 1, "projects": [ + {{"id": "coords_hud", "kind": "mod", "name": "Coordinates HUD", "summary": "", "license": "MIT", + "source": "https://x.test", "versions": [{{"version": "0.1.0", "api": "0.1", "side": "client", + "capabilities": ["game-read", "hud", "chat"], "game": "26.3", "file": {{"name": "coords_hud-0.1.0.hmod", + "url": "file://{}", "size": {}, "sha256": "{hmod_sha}"}}}}]}}, + {{"id": "cozy", "kind": "modpack", "name": "Cozy", "summary": "", "license": "MIT", + "source": "https://x.test", "versions": [{{"version": "1.0.0", "api": "1", "game": "26.3", + "requires": [{{"id": "coords_hud", "version": "0.1.0", "sha256": "{hmod_sha}"}}], + "file": {{"name": "cozy-1.0.0.hpack", "url": "file://{}", "size": {}, "sha256": "{}"}}}}]}} + ]{extra}}}"#, + hmod.display(), + size(&hmod), + hpack.display(), + size(&hpack), + idx::sha256_file(&hpack).unwrap() + ); + Index::parse_local(&text).unwrap() + } + + fn scratch(name: &str) -> PathBuf { + let dir = std::env::temp_dir().join(format!( + "horizon-launcher-hpack-{name}-{}", + std::process::id() + )); + let _ = fs::remove_dir_all(&dir); + fs::create_dir_all(&dir).unwrap(); + dir + } + + #[test] + fn a_pack_installs_as_a_profile_with_its_mods() { + let dir = scratch("install"); + let index = registry(&dir, false, ""); + let packs = dir.join("horizon-packs"); + let pack = install_in(&index, true, "cozy", &packs, &|_, _| {}).unwrap(); + assert_eq!(pack.mods, ["coords_hud 0.1.0"]); + let home = home(&packs.join("cozy")); + assert!(home.join("horizon-mods/coords_hud.hmod").is_file()); + assert!( + !java::is_enabled(&home, "coords_hud"), + "the pack turns it off" + ); + assert_eq!( + fs::read_to_string(home.join("options.txt")).unwrap(), + "renderDistance:8\n" + ); + assert_eq!( + list_in(&packs), + [Installed { + work: packs.join("cozy"), + ..pack + }] + ); + // Settings the player changed stay on an update. + fs::write(home.join("options.txt"), "renderDistance:12\n").unwrap(); + install_in(&index, true, "cozy", &packs, &|_, _| {}).unwrap(); + assert_eq!( + fs::read_to_string(home.join("options.txt")).unwrap(), + "renderDistance:12\n" + ); + // Not a modpack, and not there. + assert!(install_in(&index, true, "coords_hud", &packs, &|_, _| {}) + .unwrap_err() + .contains("not a modpack")); + assert!(install_in(&index, true, "nope", &packs, &|_, _| {}).is_err()); + fs::remove_dir_all(&dir).unwrap(); + } + + #[test] + fn a_pack_with_a_taken_down_mod_installs_nothing() { + let dir = scratch("revoked"); + let index = registry( + &dir, + true, + r#", "revoked": [{"id": "coords_hud", "reason": "Test."}]"#, + ); + let packs = dir.join("horizon-packs"); + let e = install_in(&index, true, "cozy", &packs, &|_, _| {}).unwrap_err(); + assert!(e.contains("taken down: Test."), "{e}"); + assert!(!packs.join("cozy").exists(), "nothing left behind"); + fs::remove_dir_all(&dir).unwrap(); + } +} diff --git a/launcher/src/browser/horizon/tests.rs b/launcher/src/browser/horizon/tests.rs new file mode 100644 index 000000000..fb99462e1 --- /dev/null +++ b/launcher/src/browser/horizon/tests.rs @@ -0,0 +1,164 @@ +//! The registry source against the sample index of `horizon_mods::index` +//! (`crates/horizon-mods/tests/index/`), its files served from disk. +use super::*; + +fn fixtures() -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")).join("../crates/horizon-mods/tests/index") +} + +/// The sample index with its files on disk, as a local test registry lists them. +fn sample() -> Index { + let text = fs::read_to_string(fixtures().join("index-v1.json")).unwrap(); + let mut index = Index::parse(&text).unwrap(); + for p in &mut index.projects { + for v in &mut p.versions { + let path = fixtures().join(&v.file.name); + v.file.url = format!("file://{}", path.canonicalize().unwrap_or(path).display()); + } + } + index +} + +fn scratch(name: &str) -> PathBuf { + let dir = std::env::temp_dir().join(format!( + "horizon-launcher-registry-{name}-{}", + std::process::id() + )); + let _ = fs::remove_dir_all(&dir); + fs::create_dir_all(&dir).unwrap(); + dir +} + +fn hud(index: &Index) -> Project { + project_of(index.project("coords_hud").unwrap()).unwrap() +} + +#[test] +fn searches_by_kind_and_words() { + let index = sample(); + let found = |text: &str, kind| -> Vec { + search(&index, &Query::new(text, kind)) + .into_iter() + .map(|p| p.id) + .collect() + }; + assert_eq!(found("", Kind::Mod), ["coords_hud"]); + assert_eq!(found("coordinates HUD", Kind::Mod), ["coords_hud"]); + assert!(found("zoom", Kind::Mod).is_empty()); + // Modpacks under Modpacks; a kind this launcher can't install yet (a world) never. + assert_eq!(found("", Kind::Modpack), ["cozy"]); + assert!(found("skyblock", Kind::ResourcePack).is_empty()); + let p = hud(&index); + assert_eq!(p.support(), super::super::Support::Works); + assert_eq!(p.page, "https://playhorizon.dev/mods/coords_hud"); +} + +#[test] +fn installs_checked_turns_on_and_removes() { + let index = sample(); + let home = scratch("install"); + let p = hud(&index); + let done = install_from(&index, true, &p, &home, &|_, _| {}).unwrap(); + assert_eq!(done[0].file, "horizon-mods/coords_hud.hmod"); + assert_eq!(done[0].verdict.as_deref(), Some("Verified")); + assert!(done[0].checksum.starts_with("SHA-256:e1135345")); + assert!(home.join("horizon-mods/coords_hud.hmod").is_file()); + let list = fs::read_to_string(home.join("horizon-mods.txt")).unwrap(); + assert_eq!(list, "mod.coords_hud:true\n"); + assert_eq!( + fs::read_dir(home.join("mod-browser/downloads")) + .unwrap() + .count(), + 0 + ); + // Installed already: nothing to do. + let again = install_from(&index, true, &p, &home, &|_, _| {}).unwrap(); + assert_eq!(again, done); + + let mut revoked = index.clone(); + revoked.revoked.push(index::Revoked { + id: "coords_hud".into(), + sha256: None, + reason: "Test.".into(), + date: String::new(), + }); + let said = turn_off_revoked(&revoked, &home); + assert_eq!(said, ["Coordinates HUD was taken down and is off: Test."]); + assert_eq!( + fs::read_to_string(home.join("horizon-mods.txt")).unwrap(), + "" + ); + + installed::remove(&home, "coords_hud").unwrap(); + assert!(!home.join("horizon-mods/coords_hud.hmod").exists()); + assert!(installed::Installed::load(&home).entries.is_empty()); + fs::remove_dir_all(&home).unwrap(); +} + +#[test] +fn refuses_a_changed_file_a_taken_id_and_local_files_from_the_real_registry() { + let index = sample(); + let p = hud(&index); + + let home = scratch("tampered"); + let mut tampered = index.clone(); + let cozy = tampered.projects[1].versions[0].file.url.clone(); + tampered.projects[0].versions[0].file.url = cozy; + let e = install_from(&tampered, true, &p, &home, &|_, _| {}).unwrap_err(); + assert!(e.contains("larger than its source says"), "{e}"); + assert!(!home.join("horizon-mods").exists()); + + let e = install_from(&index, false, &p, &home, &|_, _| {}).unwrap_err(); + assert!(e.contains("file servers"), "{e}"); + + fs::create_dir_all(home.join("horizon-mods/coords_hud")).unwrap(); + let e = install_from(&index, true, &p, &home, &|_, _| {}).unwrap_err(); + assert!(e.contains("already in horizon-mods"), "{e}"); + + let mut taken_down = index.clone(); + taken_down.revoked.push(index::Revoked { + id: "coords_hud".into(), + sha256: Some(index.projects[0].versions[0].file.sha256.clone()), + reason: "Test.".into(), + date: String::new(), + }); + let e = install_from(&taken_down, true, &p, &home, &|_, _| {}).unwrap_err(); + assert_eq!(e, "Coordinates HUD was taken down: Test."); + fs::remove_dir_all(&home).unwrap(); +} + +#[test] +fn registry_versions_run_when_made_for_this_game() { + use super::super::{pick, support, Support}; + let v = |game: &str| vec![game.to_owned()]; + let horizon = vec![LOADER.to_owned()]; + assert_eq!(support(Kind::Mod, &v(GAME), &horizon), Support::Works); + assert!(!support(Kind::Mod, &v("26.2"), &horizon).installable()); + let index = sample(); + let p = index.project("coords_hud").unwrap(); + let versions: Vec = p + .versions + .iter() + .map(|x| version_of(&index, p, x)) + .collect(); + assert_eq!(pick(Kind::Mod, &versions, false).unwrap().number, "0.1.0"); +} + +#[test] +fn owner_links_only_take_github_logins() { + for ok in ["Stink-O", "ludovic111", "a"] { + assert!(github_login(ok), "{ok}"); + } + for bad in [ + "", + "-x", + "x-", + "a--b", + "bad login", + "a/b", + "x?y=1", + &"a".repeat(40), + ] { + assert!(!github_login(bad), "{bad}"); + } +} diff --git a/launcher/src/browser/images.rs b/launcher/src/browser/images.rs index fee2f219e..f5029a7af 100644 --- a/launcher/src/browser/images.rs +++ b/launcher/src/browser/images.rs @@ -9,10 +9,12 @@ use std::io::Read; use std::path::{Path, PathBuf}; /// Hosts images may come from. -const HOSTS: [&str; 3] = [ +const HOSTS: [&str; 4] = [ "https://cdn.modrinth.com/", "https://media.forgecdn.net/", "https://edge.forgecdn.net/", + // Horizon's registry's icons (its GitHub Pages). + "https://ludovic111.github.io/horizon-mods/", ]; /// The most an image may weigh before decoding. pub const MAX_DOWNLOAD: u64 = 8 << 20; diff --git a/launcher/src/browser/installed.rs b/launcher/src/browser/installed.rs index 451bdd43e..c866dd26d 100644 --- a/launcher/src/browser/installed.rs +++ b/launcher/src/browser/installed.rs @@ -170,6 +170,9 @@ pub fn install( home: &Path, progress: &dyn Fn(&str, f32), ) -> Result, String> { + if project.source == SourceId::Horizon { + return super::horizon::install(project, home, progress); + } if project.kind == Kind::Mod { return super::java::install(source, project, everything, home, progress); } @@ -248,7 +251,7 @@ pub fn remove(home: &Path, name: &str) -> Result { .lookup(name) .cloned() .ok_or_else(|| format!("{name} wasn't installed by the browser."))?; - if entry.kind == Kind::Mod { + if entry.kind == Kind::Mod && entry.source != SourceId::Horizon { super::java::remove(home, &entry)?; } else { match fs::remove_file(home.join(&entry.file)) { @@ -257,6 +260,9 @@ pub fn remove(home: &Path, name: &str) -> Result { Err(e) => return Err(format!("{}: {e}", entry.file)), } } + if let Some(id) = super::horizon::mod_id(&entry).filter(|_| entry.source == SourceId::Horizon) { + super::java::set_enabled(home, id, false)?; + } installed .entries .retain(|e| !(e.source == entry.source && e.project == entry.project)); diff --git a/launcher/src/browser/installed/tests.rs b/launcher/src/browser/installed/tests.rs index e940ce0f1..cb2814c29 100644 --- a/launcher/src/browser/installed/tests.rs +++ b/launcher/src/browser/installed/tests.rs @@ -86,6 +86,7 @@ impl Fake { .map(|b| format!("{b:02x}")) .collect(), ), + sha256: None, sha1: None, primary: true, }; diff --git a/launcher/src/browser/java.rs b/launcher/src/browser/java.rs index 76ce43c95..4ac7ae084 100644 --- a/launcher/src/browser/java.rs +++ b/launcher/src/browser/java.rs @@ -110,6 +110,16 @@ pub fn check_project( Ok((version, check)) } +/// Whether mod `id` is on in `horizon-mods.txt`. +pub fn is_enabled(home: &Path, id: &str) -> bool { + let key = format!("mod.{id}"); + fs::read_to_string(home.join(MODS_LIST)) + .unwrap_or_default() + .lines() + .filter_map(|l| l.split_once(':')) + .any(|(k, v)| k.trim() == key && v.trim() == "true") +} + /// Turns mod `id` on (or off) in `horizon-mods.txt`, keeping the other lines. pub fn set_enabled(home: &Path, id: &str, on: bool) -> Result<(), String> { let path = home.join(MODS_LIST); @@ -326,6 +336,7 @@ mod tests { name: "a.jar".into(), size: 1, sha512: sha512.map(str::to_owned), + sha256: None, sha1: sha1.map(str::to_owned), primary: true, }; diff --git a/launcher/src/browser/modrinth.rs b/launcher/src/browser/modrinth.rs index 722549742..52e0aa7f0 100644 --- a/launcher/src/browser/modrinth.rs +++ b/launcher/src/browser/modrinth.rs @@ -139,6 +139,7 @@ pub(super) fn parse_version(v: &Value) -> Option { name: f["filename"].as_str()?.to_owned(), size: f["size"].as_u64().unwrap_or(0), sha512: f["hashes"]["sha512"].as_str().map(str::to_ascii_lowercase), + sha256: None, sha1: f["hashes"]["sha1"].as_str().map(str::to_ascii_lowercase), primary: f["primary"].as_bool().unwrap_or(false), }) diff --git a/launcher/src/browser/tests.rs b/launcher/src/browser/tests.rs index 6167d5bd9..50bfbae40 100644 --- a/launcher/src/browser/tests.rs +++ b/launcher/src/browser/tests.rs @@ -18,6 +18,7 @@ fn version(id: &str, games: &[&str], loaders: &[&str]) -> Version { name: format!("{id}.zip"), size: 3, sha512: Some("ab".into()), + sha256: None, sha1: None, primary: true, }], @@ -305,6 +306,7 @@ fn file_for(body: &[u8], sha512: bool) -> FileRef { name: "pack.zip".into(), size: body.len() as u64, sha512: sha512.then(|| hex(&sha2::Sha512::digest(body))), + sha256: None, sha1: (!sha512).then(|| hex(&sha1::Sha1::digest(body))), primary: true, } @@ -383,6 +385,6 @@ fn live_modrinth_search_pick_and_download() { #[test] fn curseforge_is_coming_soon_and_never_searched() { let ids: Vec = sources().iter().map(|s| s.id()).collect(); - assert_eq!(ids, [SourceId::Modrinth]); + assert_eq!(ids, [SourceId::Modrinth, SourceId::Horizon]); assert!(source(SourceId::CurseForge).is_none()); } diff --git a/launcher/src/cli.rs b/launcher/src/cli.rs index d104fe7eb..25bbe0bee 100644 --- a/launcher/src/cli.rs +++ b/launcher/src/cli.rs @@ -199,6 +199,10 @@ enum ModsCmd { }, /// Remove a pack the browser installed Remove { project: String }, + /// Turn on a mod the browser installed (horizon-mods.txt, as the game's Options > Mods does) + Enable { project: String }, + /// Turn off a mod the browser installed; it stays installed + Disable { project: String }, } #[derive(Clone, Copy, clap::ValueEnum)] @@ -213,6 +217,8 @@ enum SourceArg { Modrinth, /// CurseForge support is coming soon Curseforge, + /// Horizon's own registry: mods, modpacks and resource packs made for Horizon, reviewed + Horizon, } impl SourceArg { @@ -221,6 +227,7 @@ impl SourceArg { let id = match self { SourceArg::Modrinth => SourceId::Modrinth, SourceArg::Curseforge => SourceId::CurseForge, + SourceArg::Horizon => SourceId::Horizon, }; browser::source(id).ok_or_else(|| browser::CURSEFORGE_SOON.into()) } @@ -1064,11 +1071,23 @@ fn servers(action: ServersCmd, as_json: bool) -> Result<(), String> { } fn mods(action: ModsCmd, as_json: bool) -> Result<(), String> { - use horizon_launcher::browser::{installed, Kind, Query}; + use horizon_launcher::browser::{horizon, installed, Kind, Query, SourceId}; use horizon_launcher::modpacks; let home = backend::game_home(); let entry_json = |e: &installed::Entry| serde_json::to_value(e).unwrap_or_default(); let line = |label: &str, f: f32| progress(label.to_string())(f); + let from_registry = installed::Installed::load(&home) + .entries + .iter() + .any(|e| e.source == SourceId::Horizon); + if from_registry { + if let Ok(index) = horizon::index() { + for said in horizon::turn_off_revoked(&index, &home) { + eprintln!("{said}"); + } + } + } + let on = matches!(action, ModsCmd::Enable { .. }); match action { ModsCmd::List => { let list = installed::Installed::load(&home).entries; @@ -1114,11 +1133,19 @@ fn mods(action: ModsCmd, as_json: bool) -> Result<(), String> { "id": p.id, "slug": p.slug, "title": p.title, "summary": p.summary, "author": p.author, "downloads": p.downloads, "page": p.page, "license": p.license, "support": support.label(), "reason": support.reason(), + "about": if p.source == SourceId::Horizon { horizon::trust_and_capabilities(&p.id) } else { Vec::new() }, }) }).collect()); } else { for p in &found { let support = p.support(); + if p.source == SourceId::Horizon { + println!("{} ({}) {}\n {}", p.title, p.slug, p.author, p.summary); + for line in horizon::trust_and_capabilities(&p.id) { + println!(" {line}"); + } + continue; + } let unchecked = p.kind == Kind::Mod && support.installable(); println!( "{} ({}) {} · {} downloads\n {}", @@ -1187,6 +1214,23 @@ fn mods(action: ModsCmd, as_json: bool) -> Result<(), String> { } => { let source = source.get()?; let p = source.project(&project)?; + if p.kind == Kind::Modpack && p.source == SourceId::Horizon { + let pack = horizon::pack::install(&p.id, &line)?; + if as_json { + json( + serde_json::json!({"pack": pack.id, "version": pack.version, "mods": pack.mods, "home": horizon::pack::home(&pack.work)}), + ); + } else { + println!( + "{} {} is installed as its own profile, with {}.", + pack.name, + pack.version, + pack.mods.join(", ") + ); + println!("Play it: horizon-launcher modpack play {}", pack.id); + } + return Ok(()); + } if p.kind == Kind::Modpack { let folder = modpacks::ports_dir().join("downloads"); let file = @@ -1251,15 +1295,43 @@ fn mods(action: ModsCmd, as_json: bool) -> Result<(), String> { let e = installed::remove(&home, &project)?; println!("Removed {} ({}).", e.title, e.file); } + ModsCmd::Enable { project } | ModsCmd::Disable { project } => { + let e = installed::Installed::load(&home) + .lookup(&project) + .cloned() + .ok_or_else(|| format!("{project} wasn't installed by the browser."))?; + let id = horizon::mod_id(&e) + .filter(|_| e.kind == Kind::Mod) + .ok_or_else(|| format!("{} isn't a mod: only mods turn on and off.", e.title))?; + if on { + if let Some(r) = horizon::index() + .ok() + .filter(|_| e.source == SourceId::Horizon) + .and_then(|i| { + i.revoked(&e.project, e.checksum.trim_start_matches("SHA-256:")) + .cloned() + }) + { + return Err(format!("{} was taken down: {}", e.title, r.reason)); + } + } + horizon_launcher::browser::java::set_enabled(&home, id, on)?; + println!("{} is {}.", e.title, if on { "on" } else { "off" }); + } } Ok(()) } fn modpack(action: ModpackCmd, as_json: bool) -> Result<(), String> { + use horizon_launcher::browser::horizon; use horizon_launcher::modpacks::{self, Imported}; use horizon_port_pack::report::Status; let log = &|line: String| eprintln!("{line}"); let work = |pack: &str| { + let registry = horizon::pack::packs_dir().join(pack); + if horizon::pack::is_pack(®istry) { + return Ok(registry); + } let work = modpacks::ports_dir().join(pack); modpacks::load(&work).map(|_| work) }; @@ -1293,10 +1365,26 @@ fn modpack(action: ModpackCmd, as_json: bool) -> Result<(), String> { match action { ModpackCmd::List => { let packs = modpacks::list(); - if packs.is_empty() && !as_json { + let registry = horizon::pack::list(); + if packs.is_empty() && registry.is_empty() && !as_json { println!("No modpack imported yet: horizon-launcher modpack import "); } packs.iter().for_each(show); + for p in ®istry { + if as_json { + json( + serde_json::json!({"pack": p.id, "name": p.name, "version": p.version, "source": "horizon", "mods": p.mods}), + ); + } else { + println!( + "{} {} ({}, from Horizon's registry)\n {}", + p.name, + p.version, + p.id, + p.mods.join(", ") + ); + } + } } ModpackCmd::Import { path } => show(&modpacks::import(&path, log)?), ModpackCmd::Port { diff --git a/launcher/src/main.rs b/launcher/src/main.rs index 0bcbc4011..d3a32467b 100644 --- a/launcher/src/main.rs +++ b/launcher/src/main.rs @@ -205,6 +205,8 @@ struct Launcher { /// The Modpacks page: the imported packs (read when the page first opens), the one shown, /// the report's filter, and the pipeline step running now with its log. packs: Vec, + /// Modpacks installed from Horizon's registry, each its own profile. + registry_packs: Vec, packs_loaded: Option, pack_open: Option, pack_filter: Option, @@ -330,6 +332,7 @@ impl Launcher { play_pressed: None, bench: perf::Bench::from_env(), packs: Vec::new(), + registry_packs: Vec::new(), packs_loaded: None, pack_open: None, pack_filter: None, diff --git a/launcher/src/modpacks.rs b/launcher/src/modpacks.rs index 909c6cfae..acd8241a5 100644 --- a/launcher/src/modpacks.rs +++ b/launcher/src/modpacks.rs @@ -7,6 +7,7 @@ //! `home/`, the profile's own saves and options. Nothing is uploaded or shared: mod jars are //! often All Rights Reserved, so the ports stay on the player's machine. use crate::backend; +use crate::browser::horizon::pack as horizon_pack; use horizon_port_pack::{ fetch::{Fetched, How}, import::Pack, @@ -232,6 +233,9 @@ pub fn outcome(pack: &Imported) -> String { /// Removes an imported pack: its jars, ports and the profile's saves. pub fn remove(work: &Path) -> Result<(), String> { + if horizon_pack::is_pack(work) { + return horizon_pack::remove(work); + } if work.parent() != Some(ports_dir().as_path()) { return Err(format!("{} is not an imported pack", work.display())); } @@ -245,7 +249,11 @@ pub fn profile_home(work: &Path) -> PathBuf { /// Turns the game's command into this pack's profile: its own home (the player's options.txt /// copied in the first time, so keys and video settings carry over) and its carried-over mods. +/// A modpack from Horizon's registry (`horizon-packs//`) plays its own profile the same way. pub fn apply_profile(cmd: &mut Command, work: &Path) -> Result<(), String> { + if horizon_pack::is_pack(work) { + return horizon_pack::apply(cmd, work); + } // Only mods that load together: the game would refuse the later of two sharing a namespace. let (mods, _) = pipeline::loadable(&carried(work)); if mods.is_empty() { diff --git a/launcher/src/modpacks_page.rs b/launcher/src/modpacks_page.rs index 6346f8a0b..25df3966f 100644 --- a/launcher/src/modpacks_page.rs +++ b/launcher/src/modpacks_page.rs @@ -1,6 +1,7 @@ //! Imported modpacks, on the Mods page's Modpacks tab: import a pack, read its coverage report, //! port what this computer can and play it as its own profile (`horizon_launcher::modpacks`). use super::*; +use horizon_launcher::browser::horizon::pack as horizon_pack; use horizon_launcher::modpacks::{self, Imported}; use horizon_port_pack::report::Status; use std::path::PathBuf; @@ -130,6 +131,7 @@ impl Launcher { if self.packs_loaded.is_none() { self.mods.report_list.reset(); self.packs = modpacks::list(); + self.registry_packs = horizon_pack::list(); if self.pack_open.is_none() { self.pack_open = self.packs.first().map(|p| p.slug.clone()); } @@ -177,6 +179,12 @@ impl Launcher { if let Some(e) = &self.pack_error { page = page.child(notice(e.clone(), &t)); } + if !self.registry_packs.is_empty() { + page = page.child(self.registry_pack_rows(cx)); + } + if self.packs.is_empty() && !self.registry_packs.is_empty() { + return page; + } if self.packs.is_empty() { return page.child( text(theme::CAPTION) @@ -216,6 +224,78 @@ impl Launcher { } } + /// The modpacks installed from Horizon's registry: each plays its own profile. + fn registry_pack_rows(&self, cx: &mut Context) -> Div { + let t = self.theme; + let mut list = div().flex().flex_col(); + for (i, p) in self.registry_packs.iter().enumerate() { + let (play, remove) = (p.work.clone(), p.work.clone()); + let id = p.id.clone(); + let key = format!("horizon:{id}"); + let confirming = self.confirm_remove.as_deref() == Some(key.as_str()); + let right = div() + .flex() + .gap(px(8.)) + .child( + button(format!("play-pack-{id}"), "Play", Kind::Primary, &t).on_click( + cx.listener(move |this, _: &ClickEvent, _, cx| { + this.play_pack(play.clone()); + cx.notify(); + }), + ), + ) + .child( + button( + format!("remove-pack-{id}"), + if confirming { + "Remove for good" + } else { + "Remove" + }, + if confirming { + Kind::Secondary + } else { + Kind::Ghost + }, + &t, + ) + .when(confirming, |b| b.text_color(t.danger)) + .on_click(cx.listener( + move |this, _: &ClickEvent, _, cx| { + if this.confirm_remove.as_ref() == Some(&key) { + match horizon_pack::remove(&remove) { + Ok(()) => this.packs_loaded = None, + Err(e) => this.pack_error = Some(e), + } + this.confirm_remove = None; + } else { + this.confirm_remove = Some(key.clone()); + } + cx.notify(); + }, + )), + ); + let detail = format!( + "{} from Horizon's registry, its own profile: {}", + p.version, + p.mods.join(", ") + ); + list = list.child(index_row( + i + 1, + p.name.clone(), + Some( + text(theme::CAPTION) + .text_color(t.muted) + .child(detail) + .into_any_element(), + ), + Some(right.into_any_element()), + &t, + )); + } + list + } + fn pack_log_plate(&self, t: &Theme) -> Div { let start = self.pack_log.len().saturating_sub(LOG_SHOWN); plate(t) diff --git a/launcher/src/mods_page.rs b/launcher/src/mods_page.rs index 86e5e2e48..50434756c 100644 --- a/launcher/src/mods_page.rs +++ b/launcher/src/mods_page.rs @@ -1,11 +1,13 @@ -//! The Mods page: search Modrinth (CurseForge: coming soon) for Java mods, modpacks and resource -//! packs, see where each comes from and how well it runs (a Java mod's jar is -//! checked first), install it with its checksum checked, and update or remove what was installed -//! (`horizon_launcher::browser`). Imported modpacks are listed on its Modpacks tab. +//! The Mods page: search Horizon's own registry for mods made for Horizon, or Modrinth +//! (CurseForge: coming soon) for Java mods, modpacks and resource packs, see where each comes +//! from and how well it runs (a Java mod's jar is checked first; a Horizon mod says whether it is +//! signed and what it may do), install it with its checksum checked, turn Horizon mods on and +//! off, and update or remove what was installed (`horizon_launcher::browser`). Imported modpacks +//! are listed on its Modpacks tab. use super::servers_page::{type_key, Key}; use super::*; use horizon_launcher::browser::{ - self, installed, java, Kind as PackKind, Project, Query, SourceId, Support, + self, horizon, installed, java, Kind as PackKind, Project, Query, SourceId, Support, }; use horizon_launcher::modpacks; use horizon_port_pack::check::Check; @@ -20,7 +22,10 @@ const KINDS: [(PackKind, &str, &str); 3] = [ /// Java mods checked at once. const CHECKING: usize = 2; /// The sources to pick from; CurseForge is shown beside them as coming soon. -const SOURCES: [(SourceId, &str); 1] = [(SourceId::Modrinth, "Modrinth")]; +const SOURCES: [(SourceId, &str); 2] = [ + (SourceId::Modrinth, "Modrinth"), + (SourceId::Horizon, "Horizon"), +]; const PAGE: usize = 20; pub(super) enum Event { @@ -31,6 +36,10 @@ pub(super) enum Event { Updates(HashMap<(SourceId, String), String>), /// A Java mod's check: its jar read, or why it couldn't be. Checked((SourceId, String), Result, String>), + /// What a Horizon registry project says before install: signed or not, what it may do. + About(HashMap>), + /// Registry files taken down that the launcher turned off. + TakenDown(Vec), /// A project page's part, an image or a filter's values (`mods_project`). Loaded(super::mods_project::Loaded), } @@ -68,6 +77,8 @@ pub(super) struct Mods { confirm_remove: Option, /// Java mods' checks, by source and project. pub(super) checks: HashMap<(SourceId, String), Checking>, + /// Horizon registry projects' trust and capabilities, by id ([`horizon::trust_and_capabilities`]). + pub(super) about: HashMap>, /// The project page, filters and images. pub(super) browse: super::mods_project::Browse, pub(super) tx: Sender, @@ -94,7 +105,11 @@ impl Mods { } else { PackKind::Mod }, - source: SourceId::Modrinth, + // `HORIZON_LAUNCHER_SOURCE=horizon` opens on Horizon's registry, for screenshots. + source: match std::env::var("HORIZON_LAUNCHER_SOURCE").as_deref() { + Ok("horizon") => SourceId::Horizon, + _ => SourceId::Modrinth, + }, text: String::new(), focus: cx.focus_handle().tab_index(0).tab_stop(true), focused: false, @@ -115,6 +130,7 @@ impl Mods { updates: HashMap::new(), confirm_remove: None, checks: HashMap::new(), + about: HashMap::new(), browse: Default::default(), tx, rx, @@ -134,7 +150,7 @@ impl Mods { /// How well a result runs: a Java mod's from its check once it has one. pub(super) fn support(&self, p: &Project) -> Support { let metadata = p.support(); - if p.kind != PackKind::Mod || !metadata.installable() { + if p.kind != PackKind::Mod || p.source == SourceId::Horizon || !metadata.installable() { return metadata; } match self.checks.get(&(p.source, p.id.clone())) { @@ -231,6 +247,16 @@ impl Launcher { m.checks.insert(key, state); } Event::Loaded(l) => loaded.push(l), + Event::About(about) => { + // Rows grow by a line: measure them again (`cull`). + m.about.extend(about); + m.results_list.reset(); + } + Event::TakenDown(lines) => { + m.message = Some(Ok(lines.join("\n"))); + m.installed = installed::Installed::load(&backend::game_home()).entries; + m.installed_list.reset(); + } } } // `HORIZON_LAUNCHER_PROJECT=` (with `HORIZON_LAUNCHER_PAGE=mods`): opens that @@ -278,7 +304,63 @@ impl Launcher { let result = browser::source(source) .ok_or_else(|| format!("{} isn't set up.", source.label())) .and_then(|s| s.search(&query)); + let about: HashMap> = match (&result, source) { + (Ok(found), SourceId::Horizon) => found + .iter() + .map(|p| (p.id.clone(), horizon::trust_and_capabilities(&p.id))) + .collect(), + _ => HashMap::new(), + }; let _ = tx.send(Event::Found(search, offset, result)); + if !about.is_empty() { + let _ = tx.send(Event::About(about)); + } + }); + } + + /// Reads the registry's index again when something installed came from it, and turns off + /// what it took down since (`horizon::turn_off_revoked`). + fn mods_taken_down(&mut self) { + if !self + .mods + .installed + .iter() + .any(|e| e.source == SourceId::Horizon) + { + return; + } + let tx = self.mods.tx.clone(); + std::thread::spawn(move || { + let Ok(index) = horizon::index() else { + return; + }; + let lines = horizon::turn_off_revoked(&index, &backend::game_home()); + if !lines.is_empty() { + let _ = tx.send(Event::TakenDown(lines)); + } + }); + } + + /// Turns a Horizon mod the browser installed on or off, as the game's Options > Mods does. + fn mods_switch(&mut self, e: &installed::Entry) { + let home = backend::game_home(); + let Some(id) = horizon::mod_id(e) else { + return; + }; + let on = !java::is_enabled(&home, id); + let revoked = horizon::known_index().and_then(|index| { + let hash = e.checksum.trim_start_matches("SHA-256:"); + index.revoked(&e.project, hash).map(|r| r.reason.clone()) + }); + self.mods.message = Some(match revoked.filter(|_| on) { + Some(reason) => Err(format!("{} was taken down: {reason}", e.title)), + None => java::set_enabled(&home, id, on).map(|()| { + format!( + "{} is {}. The game reads this when it starts.", + e.title, + if on { "on" } else { "off" } + ) + }), }); } @@ -288,7 +370,8 @@ impl Launcher { let m = &mut self.mods; let todo: Vec = found .into_iter() - .filter(|p| p.kind == PackKind::Mod && p.support().installable()) + .filter(|p| p.kind == PackKind::Mod && p.source != SourceId::Horizon) + .filter(|p| p.support().installable()) .filter(|p| !m.checks.contains_key(&(p.source, p.id.clone()))) .collect(); if todo.is_empty() { @@ -385,6 +468,16 @@ impl Launcher { None => source.project(&id)?, }; modpack = p.kind == PackKind::Modpack; + if modpack && p.source == SourceId::Horizon { + let pack = horizon::pack::install(&p.id, &progress)?; + return Ok(format!( + "Installed {} {} as its own profile with {}, each checked. Play it \ + from the list above.", + pack.name, + pack.version, + pack.mods.join(", ") + )); + } if modpack { let folder = modpacks::ports_dir().join("downloads"); let file = installed::fetch_modpack(&*source, &p, &folder, &|f| { @@ -403,6 +496,13 @@ impl Launcher { .iter() .map(|e| format!("{} {}", e.title, e.number)) .collect(); + if p.source == SourceId::Horizon && p.kind == PackKind::Mod { + return Ok(format!( + "Installed {} into horizon-mods and turned it on, checksum checked \ + (Options > Mods in the game).", + names.join(", ") + )); + } if p.kind == PackKind::Mod { let skipped = done.last().map_or(0, |e| e.skipped.len()); return Ok(format!( @@ -497,14 +597,15 @@ impl Launcher { self.mods.loaded = true; self.mods.installed = installed::Installed::load(&backend::game_home()).entries; self.mods_check_updates(); + self.mods_taken_down(); self.mods_search(0); } let header = page_header( "Mods", - "Java mods, modpacks and resource packs. Horizon ports what a Java mod \ - carries (blocks, items, recipes, worldgen) and says before you install what runs. \ - Each file is checked against its source's checksum before it reaches your game \ - folder.", + "Mods made for Horizon from its own reviewed registry, and Java mods, modpacks and \ + resource packs from Modrinth: Horizon ports what a Java mod carries (blocks, items, \ + recipes, worldgen) and says before you install what runs. Each file is checked \ + against its source's checksum before it reaches your game folder.", &t, ); let this = cx.entity().downgrade(); @@ -543,7 +644,10 @@ impl Launcher { })); // CurseForge support is coming soon: shown, inert, never searched (like the Microsoft // account's "Coming soon"). - let source_note = div().flex().child(source_switch(&t)); + let source_note = div().flex().child(self.mods_source_switch(cx)); + // The registry lists only what was made for Horizon: no "everything", no Modrinth filters. + let registry = self.mods.source == SourceId::Horizon; + let filters = (!registry).then(|| self.mods_filters(cx)); let controls = div() .flex() .flex_col() @@ -555,10 +659,10 @@ impl Launcher { .items_center() .gap(px(12.)) .child(self.mods_field(cx)) - .child(toggle), + .when(!registry, |d| d.child(toggle)), ) .child(source_note) - .child(self.mods_filters(cx)); + .children(filters); // The kinds are inventory tabs on the panel that holds the open one. let body = blocks::panel(&t).flex().flex_col().gap(px(24.)).p(px(20.)); let page = |body: Div| { @@ -687,6 +791,30 @@ impl Launcher { } cx.notify(); })); + if e.source == SourceId::Horizon && e.kind == PackKind::Mod { + let on = horizon::mod_id(e) + .is_some_and(|id| java::is_enabled(&backend::game_home(), id)); + let entry = e.clone(); + right = right.child( + div() + .id(SharedString::from(format!("mods-switch-{}", e.project))) + .flex() + .items_center() + .gap(px(8.)) + .cursor_pointer() + // Its id sits under the row's own, so one name serves every row. + .child(switch("mods-switch-knob", on, &t)) + .child(text(theme::LABEL).text_color(t.ink_soft).child(if on { + "On" + } else { + "Off" + })) + .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { + this.mods_switch(&entry); + cx.notify(); + })), + ); + } right = right.child(remove); let detail = div() .flex() @@ -750,6 +878,8 @@ impl Launcher { if m.results.is_empty() { let line = if m.searching || !m.searched { "Searching…" + } else if m.source == SourceId::Horizon { + "Nothing in Horizon's registry matches yet." } else if m.everything { "Nothing found." } else { @@ -768,6 +898,7 @@ impl Launcher { let busy = m.busy.as_ref().map(|b| b.0.as_str()); let support = m.support(p); let checking = p.kind == PackKind::Mod + && p.source != SourceId::Horizon && matches!( m.checks.get(&(p.source, p.id.clone())), None | Some(Checking::Running) @@ -828,7 +959,9 @@ impl Launcher { if !p.author.is_empty() { by.push(format!("by {}", p.author)); } - by.push(format!("{} downloads", count(p.downloads))); + if p.source != SourceId::Horizon { + by.push(format!("{} downloads", count(p.downloads))); + } by.push(format!("from {}", p.source.label())); if !p.license.is_empty() { by.push(p.license.clone()); @@ -842,6 +975,12 @@ impl Launcher { support .reason() .map(|r| div().text_color(t.ink_soft).child(r.to_string())), + ) + .children( + m.about + .get(&p.id) + .filter(|_| p.source == SourceId::Horizon) + .map(|lines| div().text_color(t.ink_soft).child(lines.join(" · "))), ); let open = p.clone(); let name = div() @@ -884,9 +1023,30 @@ impl Launcher { } } -/// The source switch while Modrinth is the only source: Modrinth chosen, CurseForge shown as -/// coming soon and not clickable, like the Microsoft account's "Coming soon". -fn source_switch(t: &Theme) -> Stateful
{ +impl Launcher { + /// The source switch: Modrinth or Horizon's registry, CurseForge shown as coming soon and not + /// clickable, like the Microsoft account's "Coming soon". + fn mods_source_switch(&self, cx: &mut Context) -> Stateful
{ + let this = cx.entity().downgrade(); + source_switch(&self.theme, self.mods.source, move |source, _, cx| { + let _ = this.update(cx, |this, cx| { + if this.mods.source != source { + this.mods.source = source; + this.mods.browse.filters = Default::default(); + this.mods.browse.open = None; + this.mods_search(0); + } + cx.notify(); + }); + }) + } +} + +fn source_switch( + t: &Theme, + current: SourceId, + on_pick: impl Fn(SourceId, &mut Window, &mut App) + Clone + 'static, +) -> Stateful
{ let soon = div() .flex() .items_center() @@ -899,7 +1059,7 @@ fn source_switch(t: &Theme) -> Stateful
{ .text_color(t.muted) .child("CurseForge (coming soon)"), ); - segments("mods-source", &SOURCES, SourceId::Modrinth, t, |_, _, _| {}).child(soon) + segments("mods-source", &SOURCES, current, t, on_pick).child(soon) } #[cfg(test)] diff --git a/launcher/src/mods_project.rs b/launcher/src/mods_project.rs index ad75f58cd..97d23e0aa 100644 --- a/launcher/src/mods_project.rs +++ b/launcher/src/mods_project.rs @@ -423,6 +423,7 @@ impl Launcher { self.mods.checks.get(&(p.source, p.id.clone())), None | Some(Checking::Running) ) && p.kind == PackKind::Mod + && p.source != SourceId::Horizon && p.support().installable(); let mut action = div().flex().items_center().gap(px(8.)).child( caps(if checking { diff --git a/website/Caddyfile b/website/Caddyfile index a160f2259..5295b5e9e 100644 --- a/website/Caddyfile +++ b/website/Caddyfile @@ -20,7 +20,7 @@ Referrer-Policy strict-origin-when-cross-origin X-Frame-Options DENY Permissions-Policy "interest-cohort=(), browsing-topics=(), camera=(), microphone=(), geolocation=()" - Content-Security-Policy "default-src 'self'; script-src 'self' {$CSP_SCRIPT}; style-src 'self' {$CSP_STYLE}; img-src 'self' data: https://avatars.githubusercontent.com; font-src 'self'; connect-src 'self' https://api.github.com https://raw.githubusercontent.com https://telemetry.playhorizon.dev; frame-ancestors 'none'; base-uri 'self'; form-action 'self'" + Content-Security-Policy "default-src 'self'; script-src 'self' {$CSP_SCRIPT}; style-src 'self' {$CSP_STYLE}; img-src 'self' data: https://avatars.githubusercontent.com https://ludovic111.github.io; font-src 'self'; connect-src 'self' https://api.github.com https://raw.githubusercontent.com https://telemetry.playhorizon.dev https://ludovic111.github.io; frame-ancestors 'none'; base-uri 'self'; form-action 'self'" -Server } @assets path /assets/* @@ -38,7 +38,20 @@ redir /roadmap /hub#roadmap permanent @doc path_regexp doc ^/docs/([a-z-]+)$ rewrite @doc /docs/{re.doc.1}.html - @pages path / /index.html /404.html /privacy.html /releases /releases.html /benchmarks /benchmarks.html /benchmarks.json /hub /hub.html /data/* /docs /docs/* /sitemap.xml + # The registry's submission service (crates/horizon-mod-registry), over Railway's private network. Unset, the + # form says it isn't open. + handle /api/mods/* { + reverse_proxy {$REGISTRY_UPSTREAM:127.0.0.1:9} + } + # The registry: /mods, /mods/submit and /mods/ baked at build time, else the list page, whose script shows a project + # published since. + rewrite /mods /mods.html + @mod { + path_regexp ^/mods/[a-z0-9_]+$ + file {path}.html /mods.html + } + rewrite @mod {file_match.relative} + @pages path / /index.html /404.html /privacy.html /releases /releases.html /benchmarks /benchmarks.html /benchmarks.json /hub /hub.html /data/* /docs /docs/* /mods /mods.html /mods/* /sitemap.xml header @pages Cache-Control "no-cache" # The hub's data for Kira and anyone else: readable from any origin. @data path /data/* diff --git a/website/README.md b/website/README.md index 8f35fbcf4..43001b9c8 100644 --- a/website/README.md +++ b/website/README.md @@ -26,12 +26,14 @@ src/privacy.html what the launcher can send when players allow it, retention, src/releases.html every release, newest first, with what it adds and fixes, its notes and downloads (/releases) src/benchmarks.html Horizon against vanilla 26.3 on our machines, the hardware picker, players' results (/benchmarks) src/hub.html the hub (/hub, /roadmap redirects to its Roadmap tab): Overview, Roadmap, Contribute, a Simple / Technical switch +src/mods.html the registry's list (/mods) and a page per project (/mods/), one template src/docs.html the template of every documentation page (/docs/...), with its left navigation src/tokens.css the design tokens, generated from crates/horizon-theme (do not edit) src/style.css every style, on top of the tokens src/main.js the home page: download buttons, recent releases, links, contributors, nav, tabs, lightbox, motion src/chrome.js the light/dark switch, the phone menu and GitHub fetching, shared by the pages src/releases-page.js the Releases page: the channel filter and the refresh from GitHub +src/mods.js the registry's markup (list, project page, submit form), shared by build and page; mods-page.js: the filter, the search, the refresh; mods-submit.js: the form's sign-in and sending src/benchmarks-page.js the Benchmarks page: players' results from the telemetry server, the picker src/docs-page.js the Docs pages: the shared theme switch and phone menu src/bench.js the Benchmarks tables, shared by build and page @@ -56,14 +58,15 @@ shots/ (gitignored) full-page screenshots at 360–1920 px, a 1250× ## What comes from where -- **Built in** (`build.mjs`): the parity table, its score, harness count and audit date, +- **Built in** (`build.mjs`): Horizon's registry (`/mods`, one page per project) from the index ludovic111/horizon-mods + publishes (`HORIZON_MOD_INDEX=` builds from a test index; docs/api/mod-index.md); the parity table, its score, harness count and audit date, all from `README.md`; the documentation pages (`/docs`) from `website/docs/`; the Discord and Sponsor links from `launcher/links.json`; the latest releases and the contributors (`/repos/ludovic111/horizon/contributors`, bots left out, the owner first), when GitHub answers; without an answer the build still succeeds and the Contributors block and its menu entries stay hidden until the page loads them. Locally these come from the checkout; on Railway, which builds only this folder, from `main` on GitHub. The build fails if the README has no parity table, so a broken README never deploys. -- **Fetched by the page**: the releases (the download button picks the newest +- **Fetched by the page**: the registry's index (a project merged after the build shows at once), the releases (the download button picks the newest `launcher-v*` release's asset for the visitor's OS, the home page lists the newest game releases, and the Releases page reloads its list when GitHub has a newer release), players' benchmark results (`GET https://telemetry.playhorizon.dev/v1/benchmarks/summary`, 3-second timeout), the contributors (avatars come from `avatars.githubusercontent.com`), @@ -115,7 +118,9 @@ a Caddy stage serves `dist/` on `$PORT` with compression, cache headers, a stric Content-Security-Policy and the 404 page. Its watch patterns also redeploy when the README or `launcher/links.json` change on main. No variables are needed; an optional `GITHUB_TOKEN` (read-only, public repos) lets the build bake the releases even when -GitHub's anonymous rate limit is hit. +GitHub's anonymous rate limit is hit. `REGISTRY_UPSTREAM` +(`horizon-mod-registry.railway.internal:8080`) sends `/api/mods/*` to the registry's submission +service (`crates/horizon-mod-registry`); unset, `/mods/submit` says the form isn't open. ## Rules the site keeps diff --git a/website/build.mjs b/website/build.mjs index 563bc6fa4..ffa895aca 100644 --- a/website/build.mjs +++ b/website/build.mjs @@ -17,6 +17,7 @@ import { CPUS, GPUS, singleThread } from "./src/hardware.js"; import { isGame, isLauncher, launcherVersion, newestFirst } from "./src/versions.js"; import { contributorsFrom, contributorsHtml } from "./src/contributors.js"; import { areaFilterHtml, helpWantedHtml, overviewHtml, roadmapHtml, slimIssue } from "./src/hub.js"; +import { INDEX_URL, KINDS, SUBMIT, listHtml, projectHtml, readIndex, submitHtml } from "./src/mods.js"; const site = dirname(fileURLToPath(import.meta.url)); const dist = join(site, "dist"); @@ -42,6 +43,21 @@ try { console.warn(`links.json: ${e.message}; using the built-in links`); } +// Horizon's registry (/mods): the index its repository publishes, or HORIZON_MOD_INDEX (a file or an address) to +// try a test registry. Without an answer the pages are built empty and load the index themselves. +let modProjects = []; +let modIndex = null; +try { + const from = process.env.HORIZON_MOD_INDEX || INDEX_URL; + const text = from.startsWith("https://") + ? await fetch(from, { signal: AbortSignal.timeout(15000) }).then((r) => (r.ok ? r.text() : Promise.reject(new Error(`${r.status}`)))) + : await readFile(from, "utf8"); + modIndex = JSON.parse(text); + modProjects = readIndex(modIndex) ?? (console.warn("mod index: a schema this site doesn't read"), []); +} catch (e) { + console.warn(`mod index: ${e.message}; the pages will load it themselves`); +} + // An optional token (a Railway variable, or `gh auth token` locally) lifts the rate limit. const githubHeaders = { Accept: "application/vnd.github+json", ...(process.env.GITHUB_TOKEN ? { Authorization: `Bearer ${process.env.GITHUB_TOKEN}` } : {}) }; // Every release (the Releases page lists them all), a page of 100 at a time. @@ -210,7 +226,7 @@ await writeFile(join(dist, "favicon.ico"), Buffer.concat([ico, png32])); const names = {}; const rewrite = (code) => code.replace(/"\.\/(\w+\.js)"/g, (all, file) => (names[file] ? `"./${names[file]}"` : all)); // Dependencies before what imports them. -const modules = ["markdown.js", "versions.js", "chrome.js", "hardware.js", "estimate.js", "releases.js", "contributors.js", "bench.js", "hub.js", "main.js", "releases-page.js", "benchmarks-page.js", "docs-page.js", "hub-page.js"]; +const modules = ["markdown.js", "versions.js", "chrome.js", "hardware.js", "estimate.js", "releases.js", "contributors.js", "bench.js", "hub.js", "main.js", "releases-page.js", "benchmarks-page.js", "docs-page.js", "hub-page.js", "mods.js", "mods-page.js", "mods-submit.js"]; for (const file of modules) { const code = rewrite(await readFile(join(site, "src", file), "utf8")); names[file] = `${file.replace(".js", "")}.${fingerprint(code)}.js`; @@ -243,7 +259,7 @@ const unavailable = `

The release notes could not be loaded here. // One top bar and phone menu for Home, Releases, Benchmarks and every Docs page. const navSections = [["/#about", "About"], ["/#screenshots", "In game"], ["/#parity", "Parity"], ["/#launcher", "Launcher"], ["/#recent", "New", "Recently added"], ["/#community", "Community"]]; -const navPages = [["/hub", "Hub"], ["/releases", "Releases"], ["/benchmarks", "Benchmarks"], ["/docs/", "Docs", "Documentation"]]; +const navPages = [["/hub", "Hub"], ["/mods", "Mods"], ["/releases", "Releases"], ["/benchmarks", "Benchmarks"], ["/docs/", "Docs", "Documentation"]]; const navLink = ([href, label, menuLabel], current, menu = false) => `${menu && menuLabel ? menuLabel : label}`; const siteNav = (current) => `