diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 98d5b9093..eff82fa99 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,7 +3,8 @@ name: checks # check main requires, so it never has a condition: a skipped job counts as passed, and every run of this workflow # must report a real verify (a pull request that changes no Rust finishes it in seconds instead). # parity: the vanilla harnesses a pull request's changes can affect, on the Mac mini's runner, which holds the -# maintainer's own Minecraft files (Horizon never downloads the game); .github/workflows/parity.yml. A fork's pull +# maintainer's own Minecraft files (Horizon never downloads the game); .github/workflows/parity.yml. Only for a pull +# request that changes a harness, oracle, probe or the runner: the rest are checked on main by parity-main. A fork's pull # request runs it only when a maintainer adds the run-parity label (.github/workflows/run-parity.yml), so labels # never start this workflow. See docs/release-flow.md. on: @@ -72,8 +73,37 @@ jobs: run: | cargo +stable test --workspace --lib --bins --tests --features horizon-client/mods,horizon-mods/pack cargo +stable test --workspace --doc --features horizon-client/mods,horizon-mods/pack - parity: + # Most pull requests leave the harnesses to parity-main, which runs them on main a batch of merges at a time + # (.github/workflows/parity-main.yml; releases wait for it). A pull request still runs them itself when it changes + # a harness, an oracle, a probe or the runner, or carries the run-parity label (run-parity.yml, when it is added). + parity-scope: if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository + runs-on: ubuntu-latest + outputs: + run: ${{ steps.scope.outputs.run }} + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.sha }} + fetch-depth: 0 + persist-credentials: false + - id: scope + env: + BASE_SHA: ${{ github.event.pull_request.base.sha }} + LABELS: ${{ join(github.event.pull_request.labels.*.name, ' ') }} + run: | + changed=$(git diff --name-only "$(git merge-base "$BASE_SHA" HEAD)" HEAD) + harness=$(grep -E '^(tools/test_[a-z0-9_]+_parity\.py|tools/(parity[a-z_]*|lab)\.py|tools/parity_hosts\.json|mods/horizon/src/test/|crates/[^/]+/examples/|crates/horizon-client/src/game/[a-z_]*(fixture|harness)\.rs|\.github/workflows/parity[a-z-]*\.yml)' <<< "$changed" || true) + if [ -n "$harness" ] || [[ " $LABELS " == *" run-parity "* ]]; then + echo "run=true" >> "$GITHUB_OUTPUT" + echo "The pull request runs its harnesses: ${harness:-label run-parity}" + else + echo "run=false" >> "$GITHUB_OUTPUT" + echo "No harness, oracle, probe or runner change: parity-main checks this on main" + fi + parity: + needs: parity-scope + if: needs.parity-scope.outputs.run == 'true' uses: ./.github/workflows/parity.yml with: base: ${{ github.event.pull_request.base.sha }} diff --git a/.github/workflows/parity-main.yml b/.github/workflows/parity-main.yml new file mode 100644 index 000000000..1ddfb24bb --- /dev/null +++ b/.github/workflows/parity-main.yml @@ -0,0 +1,70 @@ +name: parity-main +# The vanilla harnesses on main, a batch of merges at a time (docs/release-flow.md#parity-on-main), on the Mac mini's +# runner. One run at a time: merges that land while a run goes on wait in one pending run (GitHub keeps only the +# newest), which checks every merge since main's last green parity-main (tools/parity_main.py base). On a failure it +# bisects the failing harnesses over those merges and comments on the pull request that broke them. Releases ship +# only a commit whose parity-main passed (tools/release_train.py), so nothing a harness catches reaches players. +on: + push: + branches: [main] + workflow_dispatch: +permissions: + contents: read + # The comment on the merge that broke a harness. + pull-requests: write + # tools/parity_main.py base reads earlier runs. + actions: read +concurrency: + group: parity-main + cancel-in-progress: false +env: + # Test runs never reach the telemetry server: no live-count ping, no uploads (docs/telemetry.md). + HORIZON_TELEMETRY_URL: "off" + +jobs: + parity-main: + runs-on: [self-hosted, horizon-game] + timeout-minutes: 240 # a core-crate batch reruns ~300 harnesses (~60-90 min), a bisect a few rounds of the failures + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ github.sha }} + fetch-depth: 0 + persist-credentials: false + # Keeps .local/ (parity state, probe builds, oracle results) and target/ between runs. + clean: false + - uses: dtolnay/rust-toolchain@stable + - name: Tools from the runner + run: bash tools/ci/runner-env.sh + - name: Vanilla data, and resources prepared by this preparer + run: bash tools/ci/vanilla-data.sh + - name: Main's last green parity-main + id: base + env: + GH_TOKEN: ${{ github.token }} + run: echo "sha=$(python3 tools/parity_main.py base)" >> "$GITHUB_OUTPUT" + - name: The harnesses the merges since then can affect + env: + PARITY_HOSTS: '' + BASE_SHA: ${{ steps.base.outputs.sha }} + run: | + echo "Merges since ${BASE_SHA}:" + git log --first-parent --format=' %h %s' "$BASE_SHA..HEAD" + python3 tools/parity.py --changed "$BASE_SHA" --timeout 2400 + - name: The merge that broke them + if: failure() + env: + GH_TOKEN: ${{ github.token }} + PARITY_HOSTS: '' + BASE_SHA: ${{ steps.base.outputs.sha }} + run: | + rm -rf .local/parity-main-logs && cp -R .local/parity-runner/logs .local/parity-main-logs 2>/dev/null || true + python3 tools/parity_main.py bisect "$BASE_SHA" --comment + - name: Logs of the failed harnesses + if: failure() + uses: actions/upload-artifact@v4 + with: + name: parity-main-logs + path: .local/parity-main-logs/ + if-no-files-found: ignore + retention-days: 7 diff --git a/.github/workflows/parity.yml b/.github/workflows/parity.yml index a18fc5019..577380338 100644 --- a/.github/workflows/parity.yml +++ b/.github/workflows/parity.yml @@ -37,13 +37,56 @@ jobs: run: bash tools/ci/runner-env.sh - name: Vanilla data, and resources prepared by this preparer run: bash tools/ci/vanilla-data.sh + - name: The prepared output is the one the preparer's OUTPUT_VERSION says + env: + BASE_SHA: ${{ inputs.base }} + run: python3 tools/prepare_output.py check --cache --base "$BASE_SHA" - name: The version constants are the pinned jar's run: python3 tools/gen_version_consts.py --check - name: The harnesses this change can affect env: PARITY_HOSTS: '' BASE_SHA: ${{ inputs.base }} - run: python3 tools/parity.py --changed "$BASE_SHA" --timeout 2400 # world_spawn takes ~1480 s on a busy runner + run: | + # main's selection (tools/parity.py --changed --list with main's parity.py and parity_reach.py), so a + # branch made before a better selection landed gets it too; the head's own runner then runs those + # harnesses. A branch that changes the selection itself selects with its own code. + set -euo pipefail + own() { python3 tools/parity.py --changed "$BASE_SHA" --timeout 2400; } # world_spawn takes ~1480 s on a busy runner + fork_point=$(git merge-base "$BASE_SHA" HEAD) + if ! git rev-parse --verify -q origin/main >/dev/null || ! git diff --quiet "$fork_point" HEAD -- tools/parity.py tools/parity_reach.py; then + own + exit + fi + had_reach=$(git ls-files tools/parity_reach.py) + git show origin/main:tools/parity.py > tools/parity.py + git show origin/main:tools/parity_reach.py > tools/parity_reach.py + status=0 + python3 tools/parity.py --changed "$BASE_SHA" --list > .local/parity-selection.txt || status=$? + git checkout -- tools/parity.py + if [ -n "$had_reach" ]; then git checkout -- tools/parity_reach.py; else rm -f tools/parity_reach.py; fi + cat .local/parity-selection.txt + if [ "$status" != 0 ]; then + echo "main's selection failed: the head's own selection runs" + own + exit + fi + names=$(sed '$d' .local/parity-selection.txt | grep -E '^[A-Za-z0-9_#]+$' || true) + # The summary line says how many would run: a count that doesn't match the names read (a format change) + # must not let the job pass untested. + expected=$(tail -1 .local/parity-selection.txt | sed -nE 's/^([0-9]+) of [0-9]+ harnesses would run$/\1/p') + count=$(printf '%s' "$names" | grep -c . || true) + if [ -z "$expected" ] || [ "$expected" != "$count" ]; then + echo "main's selection listed $count names, its summary says '${expected:-?}': the head's own selection runs" + own + exit + fi + if [ "$count" = 0 ]; then + echo "Nothing to run." + exit 0 + fi + # shellcheck disable=SC2086 + python3 tools/parity.py $names --timeout 2400 - name: Logs of the failed harnesses if: failure() uses: actions/upload-artifact@v4 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 4cba851fa..05f590096 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,6 +1,7 @@ name: release # Automatic game releases (docs/release-flow.md). After every green `checks` run on main, every 10 minutes (so a -# release waiting for merges to settle goes out without another merge) and by hand, tools/release_train.py decides: +# release waiting for merges to settle goes out without another merge) and by hand, tools/release_train.py decides, +# for a commit whose checks and parity-main (the vanilla harnesses on main) both passed: # a release when 5 pull requests merged since the last one changed what players run and the newest merged 5 minutes # ago, or when the oldest has waited 6 hours, or at once for a pull request labelled release-now. # RELEASES_PAUSED=1 (repository variable) stops them; so does an open issue labelled release-blocker, forced runs too. @@ -18,7 +19,8 @@ name: release # gh workflow run release -f dry_run=false -f force=true # on main: release now, whatever the rules say on: workflow_run: - workflows: [checks] + # A game release ships a commit both passed on (parity-main runs on batches: the newest commit it passed on). + workflows: [checks, parity-main] types: [completed] branches: [main] schedule: @@ -64,8 +66,8 @@ jobs: env: GH_TOKEN: ${{ github.token }} RELEASES_PAUSED: ${{ vars.RELEASES_PAUSED }} - # The commit whose checks just passed; a dry run builds the branch it was started on; otherwise - # main's newest commit with green checks. + # The commit whose checks or parity-main just passed; a dry run builds the branch it was started on; + # otherwise main's newest commit with green checks and parity-main. HEAD_SHA: ${{ github.event.workflow_run.head_sha || (steps.mode.outputs.dry_run == 'true' && github.sha) || '' }} FORCE: ${{ inputs.force && 'yes' || '' }} run: | diff --git a/AGENTS.md b/AGENTS.md index 2d883b33b..549fa5c7f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -82,7 +82,7 @@ python3 tools/agent_control_e2e.py # horizon-ctl / MCP end to end ~25, a file of the client lib ~103, of `horizon-world` ~103–109, of `horizon-core` ~119. Plan for it. - Rust integration tests: `crates/horizon-client/tests/` (`lan_bot.rs`, `generation_saves.rs`, `streaming_meshes.rs`). - **macOS:** `cargo build/test --workspace` may fail to link the two wasm guest cdylibs (undefined - `horizon:mods/events` symbols) until #1446 lands: add `--exclude coords-hud --exclude horizon-mods-test-guest --exclude horizon-mods-test-ui-guest`. + `horizon:mods/events` symbols) until #1446 lands: add `--exclude coords-hud --exclude predator-missile --exclude horizon-mods-test-guest --exclude horizon-mods-test-ui-guest`. Their real check is `cargo test -p horizon-mods` (builds them for `wasm32-unknown-unknown`). - Linux needs `libasound2-dev libudev-dev`; `--no-default-features` drops audio and controllers (the server build). - Run and drive the game (starts the `horizon-client` beside `horizon-ctl`; other arguments go to the game): diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e6e959186..21951c083 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -7,6 +7,11 @@ welcome on [Discord](https://discord.gg/HcTeMZxe5E). This repository holds the game, the launcher and the website, plus the tools, tests and docs that build, check and release them. Anything else (videos, Discord and other community material) lives elsewhere. +**Where to start: [playhorizon.dev/hub](https://playhorizon.dev/hub).** It shows what is being done now and next +([ROADMAP.md](ROADMAP.md) in the repository), the work board, good first tasks, and the forms to report a bug or +suggest something. A pull request for a roadmap item carries its `roadmap:` label; ideas go to the suggestion +queue (the hub, or `/suggest` on Discord) rather than to issues, which are for bugs. + ## Build it You need Rust (stable), Python 3.10+, JDK 25, and on Linux the ALSA and udev headers (`libasound2-dev libudev-dev`). diff --git a/Cargo.lock b/Cargo.lock index 6945e09cf..cb5f2f554 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1116,12 +1116,44 @@ dependencies = [ "stable_deref_trait", ] +[[package]] +name = "gl_generator" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a95dfc23a2b4a9a2f5ab41d194f8bfda3cabec42af4e39f08c339eb2a0c124d" +dependencies = [ + "khronos_api", + "log", + "xml-rs", +] + [[package]] name = "glam" version = "0.30.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "19fc433e8437a212d1b6f1e68c7824af3aed907da60afa994e7f542d18d12aa9" +[[package]] +name = "glow" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29038e1c483364cc6bb3cf78feee1816002e127c331a1eec55a4d202b9e1adb5" +dependencies = [ + "js-sys", + "slotmap", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "glutin_wgl_sys" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c4ee00b289aba7a9e5306d57c2d05499b2e5dc427f84ac708bd2c090212cf3e" +dependencies = [ + "gl_generator", +] + [[package]] name = "gpu-allocator" version = "0.28.0" @@ -1812,6 +1844,23 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "khronos-egl" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6aae1df220ece3c0ada96b8153459b67eebe9ae9212258bb0134ae60416fdf76" +dependencies = [ + "libc", + "libloading", + "pkg-config", +] + +[[package]] +name = "khronos_api" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc" + [[package]] name = "lazy_static" version = "1.5.1" @@ -2684,6 +2733,13 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "predator-missile" +version = "0.1.0" +dependencies = [ + "horizon-mod-sdk", +] + [[package]] name = "presser" version = "0.3.1" @@ -3149,6 +3205,15 @@ version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" +[[package]] +name = "slotmap" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bdd58c3c93c3d278ca835519292445cb4b0d4dc59ccfdf7ceadaab3f8aeb4038" +dependencies = [ + "version_check", +] + [[package]] name = "smallvec" version = "1.16.2" @@ -4274,6 +4339,7 @@ dependencies = [ "smallvec", "thiserror 2.0.21", "wgpu-core-deps-apple", + "wgpu-core-deps-emscripten", "wgpu-core-deps-windows-linux-android", "wgpu-hal", "wgpu-naga-bridge", @@ -4289,6 +4355,15 @@ dependencies = [ "wgpu-hal", ] +[[package]] +name = "wgpu-core-deps-emscripten" +version = "30.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d98b86cf4abf524a902dd35f18ca6a3f08fc2ae9847c8f10b48e30491b1f0b86" +dependencies = [ + "wgpu-hal", +] + [[package]] name = "wgpu-core-deps-windows-linux-android" version = "30.0.1" @@ -4311,13 +4386,18 @@ dependencies = [ "bytemuck", "cfg-if", "cfg_aliases", + "glow", + "glutin_wgl_sys", "gpu-allocator", "hashbrown 0.17.1", + "js-sys", + "khronos-egl", "libc", "libloading", "log", "naga", "naga-types", + "ndk-sys", "objc2 0.6.5", "objc2-core-foundation", "objc2-core-graphics", @@ -4337,10 +4417,14 @@ dependencies = [ "smallvec", "static_assertions", "thiserror 2.0.21", + "wasm-bindgen", + "wayland-sys", + "web-sys", "wgpu-naga-bridge", "wgpu-types", "windows 0.62.2", "windows-core 0.62.2", + "windows-result 0.4.1", ] [[package]] @@ -5081,6 +5165,12 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9cc00251562a284751c9973bace760d86c0276c471b4be569fe6b068ee97a56" +[[package]] +name = "xml-rs" +version = "0.8.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e450f9b2ed1dff33c94c12589a87338689467b9c4f5d8a5710bd09a847d2c8a7" + [[package]] name = "xtask" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 4076b9079..fff062e3a 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/horizon-mods/tests/ui_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"] +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/horizon-mods/tests/ui_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", "crates/horizon-mod-sdk/examples/predator-missile", "xtask"] resolver = "2" [workspace.package] diff --git a/ROADMAP.md b/ROADMAP.md index b433fad3a..a04eca2ce 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -16,7 +16,7 @@ Horizon is Minecraft: Java Edition 26.3 rewritten from scratch in Rust, so it ru - **Single player as a server: the last differences**: Single player now runs a small server in the background, as vanilla does. We are fixing what still behaves differently there; every fix also works for LAN and server players. - **Horizon's mod registry**: One place for mods, modpacks and resource packs made for Horizon: browse them on the website and install them from the launcher in one click. -**Next**: Microsoft sign-in (coming soon); CurseForge in the mod browser (coming soon); Fresh benchmarks against vanilla; Pistons push players and mobs; Horizon on Intel Macs. +**Next**: Microsoft sign-in (coming soon); CurseForge in the mod browser (coming soon); Fresh benchmarks against vanilla; Pistons push players and mobs; Horizon on Intel Macs; Faster world creation and entry on small CPUs; Servers that hold 20 TPS with 50 to 100 players; Low-end computers: Steam Deck, old Macs, 8 GB; Native upscaling; A batch of targeted optimisations; A Convert to Rust button for what isn't rewritten yet; Mods with code: machines, magic, mobs; Data packs in the mod browser; Prepare a version without Java; Server console, bans and a full admin API; Hosting polish: Windows service, safe updates; Guided hosting with Tailscale and an invite button; An official Docker image for servers; One single-player path: remove the in-frame world; Villages and combat, exactly as in vanilla; Choose the audio output device; Mods that read and change the world; Machines and blocks with inventories (API 0.4); Lua and other languages for mods; Well-known shader looks, rewritten for Horizon; A visible beta label; Wayland and Hyprland polish; A versioned agent-control protocol; Port mods with your own AI agent; Horizon on X (coming soon); Pipeline protection and a GitHub App for agents; Signed updates everywhere, installers included; A server you install with one command. **How to help**: Play it and tell us what differs from vanilla (Discord or GitHub), run the benchmark on your computer, vote for what you want next, or pick one of the Help wanted items. @@ -80,6 +80,16 @@ Servers that show dialogs, such as the register and login screens of AuthMe, wil +#### Pre-generate an area with /pregen + +A /pregen command that builds an area of the world ahead of time, so players don't wait for new terrain when they explore. + +
Technical + +Generates, lights and saves an area before anyone walks there ([#1805](https://github.com/ludovic111/horizon/issues/1805), a contributor's pull request, in review). Useful on servers, and it moves the generation cost away from the first visit; the generation speed-ups are under 'Steady frames at long render distances'. + +
+ ### Mods and API #### Horizon's mod registry @@ -94,6 +104,40 @@ More: [docs/mods.md](docs/mods.md) +#### Mods that run on the server + +Mods that run on the server side of a world, so a dedicated server or a LAN world can have mods that react to players joining, chat and blocks being broken or placed, and that add commands. + +
Technical + +API 0.3, experimental first ([#1833](https://github.com/ludovic111/horizon/issues/1833), in review): the same sandboxed component runs on the server thread and the dedicated server, with world events (block broken or placed, entity spawned or killed, player joined or left), game rules and server commands with typed arguments. The world, item and entity interfaces come after (see 'Mods that read and change the world'). [docs/api/mods.md](docs/api/mods.md), [docs/mods.md](docs/mods.md). + +
+ +#### Screens, input and drawing for mods + +Mods will be able to open their own screens and settings pages and react to keys and item use, so bigger mods, such as the Orbital Laser, can run on the normal game. + +
Technical + +Capability `ui` is in review ([#1864](https://github.com/ludovic111/horizon/issues/1864)): mod-owned screens and a settings page under Options > Mods, built from vanilla's widgets. Still to design: `input` (an item's use, key binds, the crosshair target), `render` (a budgeted draw list on the native shader API) and `world` (changing blocks). Orbital Laser is the acceptance test: it needs all four today through its author's fork (see 'Orbital Laser on the normal game'). + +More: [docs/mods.md](docs/mods.md) + +
+ +#### Tools for people who make mods + +Better tools for mod makers: a test runner that needs no game window, templates for each kind of mod, and clear promises about what keeps working from one version to the next. + +
Technical + +`cargo horizon-mod test` runs a mod's test scripts against a fake game snapshot ([#1816](https://github.com/ludovic111/horizon/issues/1816)) and, for server mods, scripted server events ([#1837](https://github.com/ludovic111/horizon/issues/1837)); the content template and the guides are merged ([#1859](https://github.com/ludovic111/horizon/issues/1859), [#1810](https://github.com/ludovic111/horizon/issues/1810), [docs/api/stability.md](docs/api/stability.md)). Left: templates for hud, server and machine mods, typed wrappers in the SDK, a frozen 0.1 compatibility mod that every runtime must load, a CI check that fails when a stable reference entry is removed without a new major version, and the SDK on crates.io once the API is stable. + +More: [website/docs/test-a-mod.md](website/docs/test-a-mod.md) + +
+ ### Gameplay parity #### Single player as a server: the last differences @@ -130,6 +174,16 @@ More: [#1561](https://github.com/ludovic111/horizon/issues/1561) · [#1771](http +#### Horizon's own shader format and kit + +Horizon's own way to make shader packs, with tools to write and test them. Every pack is checked before it runs, and the registry will list them. + +
Technical + +`horizon-shader-api` (API 0.1, experimental): WGSL stages with typed inputs inside the renderer's passes, options as WGSL override constants, and the `horizon-shader new|check|pack` tool with templates and the example pack Golden Hour. Merged: the check every pack passes ([#1820](https://github.com/ludovic111/horizon/issues/1820)) and the tool, templates and guide ([#1822](https://github.com/ludovic111/horizon/issues/1822)). In review: the post stage running in the game with hot reload ([#1861](https://github.com/ludovic111/horizon/issues/1861)), the sky stage ([#1863](https://github.com/ludovic111/horizon/issues/1863)) and registry checks that use the game's own check ([#1855](https://github.com/ludovic111/horizon/issues/1855)). Left: the material, lighting and shadow stages, a last-good fallback on reload, a GPU-time overlay per stage and budgets ([docs/shaders.md](docs/shaders.md), [docs/api/shaders.md](docs/api/shaders.md)). The Iris/OptiFine loader is a separate item, 'Shader packs, written again from scratch'. + +
+ ### Horizon extras #### Discord Rich Presence @@ -160,11 +214,65 @@ A review of the whole tree: dead code and stubs removed ([#1432](https://github. #### Fresh benchmarks against vanilla -New public numbers comparing Horizon with vanilla on many kinds of computers, measured the same way every time, on the website's Benchmarks page. +New public numbers comparing Horizon with vanilla on many kinds of computers, measured the same way every time, with a history that shows what each release changed.
Technical -Redo the comparison after the current performance work ([#1073](https://github.com/ludovic111/horizon/issues/1073)); reliability, coverage and presentation ([#1346](https://github.com/ludovic111/horizon/issues/1346)); a scene must count as loaded only once its first chunk arrives ([#1749](https://github.com/ludovic111/horizon/issues/1749)). Players' reports come in through `--benchmark` and `horizon-launcher data benchmark` ([docs/benchmark.md](docs/benchmark.md)). +Redo the comparison after the current performance work ([#1073](https://github.com/ludovic111/horizon/issues/1073)); reliability, coverage and presentation ([#1346](https://github.com/ludovic111/horizon/issues/1346)); a scene must count as loaded only once its first chunk arrives ([#1749](https://github.com/ludovic111/horizon/issues/1749)). Next: a performance history across releases (an automatic comparison of each release with the last, on a timeline), more report fields (server TPS and MSPT, power, battery drain, temperature, throttling) and more hardware (a MacBook on battery, low-end laptops, community reports). Players' reports come in through `--benchmark` and `horizon-launcher data benchmark` ([docs/benchmark.md](docs/benchmark.md)). + +
+ +#### Faster world creation and entry on small CPUs + +Creating a world and joining it should be quick even on a four-thread laptop, where a fresh world at 32 chunks can make you wait minutes for the first terrain. + +
Technical + +Cut the per-column generation cost (noise sampling, surface rules, aquifers) and the lock convoy on structure starts, and leave one core free for the rest of the game as vanilla does ([#1760](https://github.com/ludovic111/horizon/issues/1760) and [#1778](https://github.com/ludovic111/horizon/issues/1778), in review, are listed under 'Steady frames at long render distances'). Still to profile on small CPUs: the first seconds of a fresh world deliver no chunk, and fluids remesh for about a minute after loading. Measured with `tools/perf_bench.py` ([docs/benchmark.md](docs/benchmark.md), [docs/profiling.md](docs/profiling.md)). + +
+ +#### Servers that hold 20 TPS with 50 to 100 players + +Servers that keep 20 ticks per second with 50 to 100 players. Today Horizon holds it up to 50 bots and drops to about 10 at 100. + +
Technical + +Measured against vanilla on equal work ([docs/server.md](docs/server.md), Scale): Horizon is ahead with few players and when exploring, but vanilla's tick is shorter with 25 to 50 players near spawn. Ideas on the list: send entity changes instead of rebuilding every entity each tick (about 15 percent), fewer memory reads in random ticks and collision scans, cheaper item collision checks, mob AI and natural spawning. The bot benchmark (`horizon-bots`) is the proof. + +
+ +#### Low-end computers: Steam Deck, old Macs, 8 GB + +Horizon should run well on small machines: integrated graphics, the Steam Deck, older Macs and 8 GB of memory, with a battery mode that saves power on laptops. + +
Technical + +Partly done: an opt-in 60 FPS cap with fewer workers on battery, Far Detail Reduced for integrated GPUs and animated textures on the GPU (both shipped). Left: measured targets and fixes per class of machine (memory use, worker counts, startup) on a Steam Deck, old Intel and Apple Silicon Macs and 8 GB computers, reported on the Benchmarks page. + +More: [docs/benchmark.md](docs/benchmark.md) · [website/docs/performance.md](website/docs/performance.md) + +
+ +#### Native upscaling + +Draw the game at a lower resolution and sharpen it to your screen's size, in the way of FSR or DLSS, to gain frames on weaker graphics cards. An optional graphics setting. + +
Technical + +A render-scale setting below the window size with an upscaler of our own that runs on every wgpu backend. Proof: frame time against native resolution on integrated GPUs, and screenshots for image quality. Issue [#1403](https://github.com/ludovic111/horizon/issues/1403). + +
+ +#### A batch of targeted optimisations + +Several smaller speed-ups, each measured: idle block entities that sleep, cheaper particles, faster structure placement, high-rate mice and very large texture packs. + +
Technical + +Block entities and hoppers that skip work when idle (the hopper item scan is already skipped when no item is near); a particle frustum test first (also a parity gap), then particle quads built on the GPU; jigsaw shortcuts that skip blocked connectors and pool elements known not to fit, with layouts unchanged (trial chambers and ancient cities first); batched raw mouse input on Windows for 8000 Hz mice; a better atlas packer for huge resource packs (a 64x pack builds a 704 MiB atlas for 452 MiB of textures); sign-heavy scenes ([#1461](https://github.com/ludovic111/horizon/issues/1461)). Profile first, keep what pays, show before and after numbers. + +More: [docs/profiling.md](docs/profiling.md)
@@ -180,6 +288,136 @@ The mod browser lists CurseForge as coming soon ([docs/mod-browser.md](docs/mod- +#### A Convert to Rust button for what isn't rewritten yet + +When a mod, shader pack or modpack hasn't been rewritten for Horizon, one button converts what can be converted, so you don't have to wait for the rewrite. + +
Technical + +Generalise the mod browser's Port step (`horizon-port-pack`: the data, assets, worldgen and block and item definitions of Java mods, .mrpack and CurseForge zips) to every kind of content: code mods where the API allows it (see 'Mods with code') and shader packs, converting an Iris/OptiFine pack into Horizon's format. The browser says what works before the install. Java code itself does not run ([docs/modpacks.md](docs/modpacks.md), [docs/mod-browser.md](docs/mod-browser.md)). + +
+ +#### Mods with code: machines, magic, mobs + +Making mods that add machines, magic and mobs work in Horizon, in steps, starting with machines. They are most of what a big modpack is made of. + +
Technical + +333 of the 496 mods in a big pack (ATM10) contain code. Issue [#1854](https://github.com/ludovic111/horizon/issues/1854) ranks the API gaps (ticking block entities, networking, rendering, menus, inventories) that unlock them in tiers, with machines first as the recommended path and a furnace ported tick for tick as proof. The first API step is a draft ([#1189](https://github.com/ludovic111/horizon/issues/1189), see 'Machines and blocks with inventories'). The path isn't started yet ([docs/modpacks.md](docs/modpacks.md)). + +
+ +#### Data packs in the mod browser + +Search and install data packs from the launcher's mod browser, for the world you choose, as you do mods and resource packs. + +
Technical + +A Datapacks tab beside Mods and Resource packs: install into a chosen world's datapacks folder, with the same checks and 'works in Horizon' notes as the other kinds ([docs/mod-browser.md](docs/mod-browser.md)). + +
+ +### Servers and multiplayer + +#### Prepare a version without Java + +Set up Horizon, a server first, without Java on your computer: published tables are combined with your own copy of the game. + +
Technical + +Today `horizon-prepare` runs Mojang's exporters with Java. Issue [#1750](https://github.com/ludovic111/horizon/issues/1750) proposes publishing the computed version tables once from CI and assembling the rest locally from the player's own jar and assets, provided the tables hold no Mojang content; a contributor offered a pull request, and it would help phones too. A server-only prepare from `server.jar` alone is the first step ([docs/server.md](docs/server.md)). + +
+ +#### Server console, bans and a full admin API + +Manage a server from a console, ban and pardon players, and drive it from other tools through a full admin API and an MCP server for AI agents. + +
Technical + +`horizon-server console` (a text interface), `ban`, `pardon`, `ban-ip` and `mcp` over stdio. Admin API additions: the player list, kick, ban, op and whitelist, an SSE stream of logs and events, a text metrics endpoint, config GET and PATCH, backup delete and download. The panel's later features (bans, worlds, server.properties import, logs and crash reports, modpacks) and vanilla's management protocol (the JSON-RPC over WebSocket that the `management-server-*` keys configure), so vanilla's tools can manage a Horizon server. [docs/server.md](docs/server.md), [docs/api/server.md](docs/api/server.md). + +
+ +#### Hosting polish: Windows service, safe updates + +Smoother hosting: a real Windows service, a backup before every update, backups that keep the world and each player's state, and a changed view distance or message without a restart. + +
Technical + +A Windows service wrapper instead of the logon scheduled task; `backups.before_update`; hot reload of config for view distance and the message of the day (needs engine support); backups that keep world and player state ([#1808](https://github.com/ludovic111/horizon/issues/1808), a contributor's draft, in review). [docs/hosting.md](docs/hosting.md), docs/server.md. + +
+ +#### Guided hosting with Tailscale and an invite button + +In the launcher, a guide and a button to host a server and invite friends over Tailscale, so nobody has to open ports on a router. + +
Technical + +On the Servers page (create, start, back up, update and invite are shipped) add a Tailscale setup that explains and checks each step, and an invite button that hands friends what they need to join over the tailnet. Buttons first, no docs needed, with the details in an Advanced section. Remote access over Tailscale is already described in [docs/server.md](docs/server.md). + +More: [docs/hosting.md](docs/hosting.md) + +
+ +#### An official Docker image for servers + +An official image to run a Horizon dedicated server in Docker with one command, your own copy of the game files mounted in. + +
Technical + +The repository has the Dockerfile (`packaging/server/Dockerfile`: a multi-stage build on Debian bookworm-slim, user `horizon`, volume `/data`, port 25565, game files mounted read-only, `HORIZON_EULA` and friends answering the first-start questions), and anyone can build it ([docs/hosting.md](docs/hosting.md), section 9). Left: publish the image to a public registry from the release workflow, tagged per release, so it follows the same checks, and update the docs and the launcher. The image never holds Mojang's files. + +More: [docs/server.md](docs/server.md) + +
+ +#### A server you install with one command + +Install and run a Horizon dedicated server from a terminal with a single command, no launcher needed, for VPSes and headless machines. + +
Technical + +A player's idea (#horizon-ideas). `horizon-server` already runs headless; left: an install script (Linux/macOS shell, Windows PowerShell) that fetches the signed release, checks it, asks for or points at the player's own game files and starts the server, documented in [docs/hosting.md](docs/hosting.md). + +
+ +### Mods and API + +#### Mods that read and change the world + +Mods that can see and change blocks, read inventories and items, and find and affect entities, all through the server so every player sees the same thing. + +
Technical + +After server mods: read blocks, states and biomes in loaded chunks and change them through the server's block log, so clients and the parity harnesses see vanilla's updates; the player's inventory, held item, open container and item stacks with components; entity queries near a position, a safe subset of entity data and data-driven mod entity types. Snapshots and batched calls per tick, never per-block host calls in hot paths. [docs/api/mods.md](docs/api/mods.md) ('Not there yet'). + +More: [docs/modpacks.md](docs/modpacks.md) + +
+ +#### Machines and blocks with inventories (API 0.4) + +Mods that add machines: blocks that tick, hold items and have their own menus. This unlocks the technology mods of big modpacks. + +
Technical + +Block entities and menus: ticking machines with slots, server menu logic and the menu protocol to clients, energy, item and fluid transfer, data attachments, accessory slots and custom recipe types, with a typed client and server channel and typed config beside them. A draft ([#1189](https://github.com/ludovic111/horizon/issues/1189)) proves a furnace ported tick for tick and holds the ATM10 code-mods plan; the design is redone through the API policy before it merges ([docs/modpacks.md](docs/modpacks.md), docs/api/stability.md). + +
+ +#### Lua and other languages for mods + +Write mods as Lua scripts, in the style of KubeJS, with no compiler, and later in other languages that pass the tests. Every language runs in the same sandbox. + +
Technical + +Lua first: Horizon ships a Lua interpreter that is itself a WebAssembly mod, and a script mod's manifest names it and the capabilities it uses, so a script can do exactly what a compiled mod with the same manifest can, with hot reload of `main.lua`. Then C, C++, Zig, Go (TinyGo), AssemblyScript and JavaScript or TypeScript are measured against Rust on the same tick-heavy example, and each is supported only with a template, an example and a CI test. [docs/api/mods.md](docs/api/mods.md) ('Ways in'). + +
+ ### Gameplay parity #### Pistons push players and mobs @@ -192,6 +430,50 @@ Port `PistonMovingBlockEntity.moveCollidedEntities` and `moveStuckEntities` (hon +#### One single-player path: remove the in-frame world + +Single player will have one way to run, the server thread, so every fix works everywhere and there are fewer bugs. The older in-frame mode goes once everything passes without it. + +
Technical + +`--in-frame` (also `--no-server-thread` and `HORIZON_SERVER_THREAD=0`) still ticks the world inside the window's `Game`, and most harnesses test that path. Delete it once `lan_e2e --server-thread` and the parity harnesses pass on the server thread, which also halves the bug surface. See also 'Single player as a server' and [docs/server-levels.md](docs/server-levels.md). + +
+ +#### Villages and combat, exactly as in vanilla + +Villages and fights behaving as in vanilla: iron golems defending villages, villagers remembering their homes, mace smashes and knockback. + +
Technical + +Villages: iron golem village goals, villager memories across saves, villager age, job-site claiming on dedicated servers, flower offers ([#1639](https://github.com/ludovic111/horizon/issues/1639) is one report). Combat: the mobs' `attack_knockback` attribute, mace smash logic, the hit knockback source on the host, the fire burnout biome tag. Each is checked against vanilla by a harness ([docs/rust-parity.md](docs/rust-parity.md)). + +
+ +#### Choose the audio output device + +Pick the speakers or headphones Horizon plays through, in Music & Sounds. + +
Technical + +An output device list in Music & Sounds, falling back to the system default. [#1035](https://github.com/ludovic111/horizon/issues/1035) started it: a player on an older Windows computer had no sound at all, and a device choice is how such cases are found and fixed. + +
+ +### Rendering + +#### Well-known shader looks, rewritten for Horizon + +Popular shader packs rewritten in Horizon's format, so good packs exist from the start. Only packs whose licence is open source and compatible with ours are rewritten, never anything else. + +
Technical + +The first candidates are Photon, Chocapic13 and MakeUp Ultra Fast, each only after its licence is read and recorded: a pack is rewritten only if it is open source and compatible with Horizon's licence, and the rewrite is written from the shader format's documentation and the look on screen, not from anyone's code. Today only the Golden Hour example exists (`[crates/horizon-shader-api/packs/golden_hour](crates/horizon-shader-api/packs/golden_hour)`). + +More: [docs/shaders.md](docs/shaders.md) + +
+ ### Launcher #### Microsoft sign-in (coming soon) @@ -206,6 +488,18 @@ More: [#1270](https://github.com/ludovic111/horizon/issues/1270) +#### A visible beta label + +A clear 'beta' label in the launcher and the game, so nobody is surprised that Horizon is still being built. + +
Technical + +Show Beta in the launcher's header and about page, on the game's title screen and on the website, beside the version number ([docs/versioning.md](docs/versioning.md)). + +
+ +### Platforms + #### Horizon on Intel Macs Builds of the game and the launcher for Macs with an Intel processor. @@ -216,29 +510,359 @@ x86_64 game and launcher builds, picked per processor ([#1171](https://github.co -## Later +#### Wayland and Hyprland polish -### Gameplay parity - -#### Following new Minecraft versions - -When Mojang releases a new version of the game, Horizon follows it, and every system is checked against the new version again. +Fixes for Linux desktops that use Wayland and Hyprland: fullscreen, the clipboard, the GUI scale after a restart and the like.
Technical -The port pipeline detects a release, diffs its classes against 26.3 and writes a port report ([docs/version-ports.md](docs/version-ports.md)); the harnesses then run against the new version. +Open: the GUI Scale is clamped to 2 after a restart under Hyprland, while XWayland keeps it ([#1866](https://github.com/ludovic111/horizon/issues/1866)). Earlier reports covered fullscreen dropping on menu clicks, the clipboard and a white overlay ([#1007](https://github.com/ludovic111/horizon/issues/1007), closed). Checked on a real Wayland session. + +
+ +### AI and agents + +#### A versioned agent-control protocol + +A stable, documented way for AI agents and scripts to drive the game and the launcher, with versions and an event stream so tools keep working as Horizon changes. + +
Technical + +`hello` answers with the API version, the release and the features; `horizon-ctl --api`; MCP serverInfo; reply schemas generated into the reference; a `test.` namespace for hooks; transcript compatibility tests; `subscribe` as a stream of events on one connection; Horizon option files by key and saved-server commands. Agent control itself (`horizon-ctl`, both MCP servers, the No AI switch) is done: [docs/agent-control.md](docs/agent-control.md), [docs/api/agent-control.md](docs/api/agent-control.md). + +More: [docs/api/stability.md](docs/api/stability.md) + +
+ +#### Port mods with your own AI agent + +You can port a mod to Horizon with your own AI agent on your own computer. Horizon documents how and gives you the tools, but provides no hosted agents. + +
Technical + +A player runs their own agent locally with `horizon-ctl` and the MCP servers ([docs/agent-control.md](docs/agent-control.md)), `cargo horizon-mod`, `horizon-port-pack` and the parity harnesses as the check. Horizon writes the guide ([website/docs/agents.md](website/docs/agents.md), [docs/modpacks.md](docs/modpacks.md)) and runs no agents, accounts or keys for players. The No AI switch turns all of it off. + +
+ +### Community + +#### Horizon on X (coming soon) + +Coming soon: an official Horizon account on X, used to announce releases and the weekly summary. + +
Technical + +Kira already posts the roadmap items marked `announce = true` when they ship, and the weekly summary, as soon as the account's credentials exist ([roadmap/README.md](roadmap/README.md), 'The journal'). Waiting for the account itself. + +
+ +### Code and tools + +#### Pipeline protection and a GitHub App for agents + +Safer releases: changes to the release machinery need the owner's review, and AI agents use a smaller identity of their own instead of a personal account. + +
Technical + +`.github/CODEOWNERS` lists the workflows, the release and signing tools, the update-checking code and the installers ([#1015](https://github.com/ludovic111/horizon/issues/1015)); the branch protection that makes it count waits for the `horizon-agent` GitHub App, which reads and writes code, pull requests and issues but can't change settings, workflows, secrets or members, and whose tokens last an hour. It needs the owner to create the App and switch the protection on ([docs/security.md](docs/security.md)). + +
+ +#### Signed updates everywhere, installers included + +Every download Horizon makes is checked against the project's signature, including the first install with install.sh and install.ps1. + +
Technical + +Releases carry signed checksums, and the launcher and `horizon-server update` refuse anything unsigned ([#1011](https://github.com/ludovic111/horizon/issues/1011), [#1014](https://github.com/ludovic111/horizon/issues/1014)). Left: the first install through `install.sh` and `install.ps1` still trusts only the checksum GitHub publishes, because verifying Ed25519 signatures needs OpenSSL 3, which macOS doesn't ship ([docs/security.md](docs/security.md)). + +
+ +## Later + +### Performance + +#### Exact far terrain from real chunks + +Far terrain drawn from the world you really built and explored, caves and builds included, not only a preview from the seed. + +
Technical + +Keep a 3D mip store on disk built from real chunks, drawn by the GPU beside the seed preview for areas nobody has visited. Written from scratch and measured before it is kept. Builds on Far Detail and the far-terrain levels of detail already shipped; the open work on them is [#718](https://github.com/ludovic111/horizon/issues/718). + +
+ +### Modpacks + +#### Older Fabric and Forge mods + +Recognise mods built for versions before 26.1 in the mod browser, so they can be checked and carried over like newer ones. + +
Technical + +A mapping table from intermediary and SRG names so pre-26.1 jars can be classified, and a rename for mod ids with dashes (`fabric-api`); the port step then treats them like newer jars ([docs/mod-browser.md](docs/mod-browser.md)). + +
+ +#### A modpack's own resource and shader packs + +Importing a modpack also brings the resource packs and shader packs it ships with, where Horizon can use them. + +
Technical + +Porting a pack carries data, assets and worldgen today; the resource packs and shader packs bundled with it are left out. Resource packs can be carried as they are; shader packs go through the converter ([docs/modpacks.md](docs/modpacks.md)). + +
+ +#### Whole modpacks, such as ATM10, rewritten + +Rewriting a complete big modpack for Horizon so it runs far better than the original. Not a priority now: the work above comes first. + +
Technical + +The project's first goal for modpacks: support them ten times better than vanilla and port any of them. It was set aside in favour of compatibility through the browser. What exists: ATM10's world generates and 42 of its 496 mods are carried at least in part ([docs/modpacks.md](docs/modpacks.md)). It depends on 'Mods with code' and the native mod API. + +
+ +#### Running Java mods that aren't rewritten + +Run Java mods that nobody has rewritten for Horizon yet, through a bridge or a converter. Hard and not started: the Convert button and native rewrites come first. + +
Technical + +Earlier research ([#527](https://github.com/ludovic111/horizon/issues/527), closed) found that Java mods can't simply run, because they call Minecraft's own classes. Options to look at again: a jar-to-WebAssembly backend, or a bridge for simple Fabric mods. Whatever is chosen has to run inside the same sandbox and capability checks as native mods. + +
+ +### Servers and multiplayer + +#### Ely.by and other sign-in providers + +Sign in with other account providers, such as Ely.by or any Yggdrasil-compatible one, beside Microsoft. + +
Technical + +Issue [#1270](https://github.com/ludovic111/horizon/issues/1270) asks for Ely.by and Yggdrasil-compatible servers with an account selector in the launcher. It builds on the Microsoft sign-in ([docs/microsoft-account.md](docs/microsoft-account.md)). + +
+ +#### Online-mode checks for Play with Friends + +Friends who join through an invite code could prove who they are with their Microsoft account, as on online-mode servers. + +
Technical + +Play with Friends joins over an encrypted tunnel with an invite code ([docs/play-with-friends.md](docs/play-with-friends.md), 'Identity'). Online-mode checks against Mojang's session servers can be added on top of the same tunnel; they need Microsoft sign-in first, which is coming soon. Play with Friends stays self-hosted between players, with no relay run by the project. + +
+ +### Mods and API + +#### Orbital Laser on the normal game + +The community mod Orbital Laser, made for a fork of Horizon, running on the normal game and listed in the registry as the showcase of the mod API. + +
Technical + +A pull request on [https://github.com/Stink-O/orbital-laser](https://github.com/Stink-O/orbital-laser) (MIT) once the input, ui, render and world capabilities exist (see 'Screens, input and drawing for mods'): today the mod needs them through its author's fork. Tested in the game, and listed in the registry. + +
+ +#### Native versions of popular client mods + +Built-in versions of the client mods players ask for: block info on hover, a recipe viewer like JEI, a world map and schematic overlays. + +
Technical + +Done as extras: a mod list and menu, a minimap and a zoom ([docs/extras.md](docs/extras.md)). Left: block info tooltips, a recipe viewer (a contributor offered to port JEI if it becomes a native feature), a world map and a schematic overlay. They need the read-only interfaces `inventory-read`, `recipes`, `world-read` and `render` in the mod API, or they go into the game directly. + +More: [docs/mods.md](docs/mods.md) + +
+ +#### Create-style physics as a native mod + +Contraptions and physics like the Create and Aeronautics mods, as a mod for Horizon rather than part of the base game. + +
Technical + +Issue [#805](https://github.com/ludovic111/horizon/issues/805) (porting QOL mods) closed with this answer: it would come as a native Horizon mod on the mod API. It needs server mods, entities and rendering first. + +
+ +#### Proximity voice chat as a native mod + +Hear nearby players talk, as with the voice chat mods of other Minecraft versions, as a Horizon mod. + +
Technical + +Needs server mods, mod networking and an audio interface in the mod API before anything can be built ([docs/api/mods.md](docs/api/mods.md)). + +
+ +### Gameplay parity + +#### Every Minecraft version, prepared automatically + +Every past and future Minecraft version prepared for Horizon automatically. When Mojang releases one, Horizon follows it, and every system is checked against it again. + +
Technical + +The port pipeline ([docs/version-ports.md](docs/version-ports.md), `tools/version_port.py`) detects a release, prepares it, diffs its classes against the pinned 26.3, regenerates the data-driven tables, runs every parity harness on it and writes a port report that becomes issues. The goal is as little human work as possible for any version, past or future. Detection and the first slices are merged; the scheduled agent that runs it every day isn't installed yet. Two versions in one build are out of scope. + +
+ +#### Controllers: tested on real pads, and in the launcher + +Make controller support solid: tested with real PlayStation and Xbox pads and a Steam Deck, and usable in the launcher too. + +
Technical + +The game side is merged ([docs/controllers.md](docs/controllers.md)): pads read with gilrs, menus, rumble, glyphs, remapping and DualSense adaptive triggers, lightbar and haptics, but not yet confirmed on real pads or a Steam Deck (DualSense haptics stay behind a switch until then). Left: a test pass on real hardware, and the same focus navigation in the GPUI launcher. + +
+ +#### The full debug screen + +Vanilla's complete F3 screen: debug options, a profiler chart, ten-second captures and an atlas dump. + +
Technical + +Today F3 shows position, chunk, light, biome and FPS ([docs/native-client.md](docs/native-client.md)). Vanilla also has F3+F6 debug options with profiles, F3+1 the profiler pie, F3+L a ten-second capture and F3+S the atlas dump ([#562](https://github.com/ludovic111/horizon/issues/562) asked for the F3 editor). + +
+ +### Rendering + +#### A light default shader pack + +A small built-in look (tone mapping, colour grade, a cheap vignette) that costs almost nothing, for players who want a nicer picture without a heavy pack. + +
Technical + +An opt-in, composite-only pack on the native shader API, under about 0.3 ms at 1080p, shown on the Shader Packs page. Pack option sliders and profiles, and a Failed row with the full error, belong with it. + +More: [docs/shaders.md](docs/shaders.md) + +
+ +#### Optional ray tracing + +An optional ray-tracing mode for graphics cards that support it (RTX and recent AMD): shadows, water reflections and global lighting on top of the vanilla look. + +
Technical + +A spike with ray-traced sun shadows, off by default, is a draft ([#1180](https://github.com/ludovic111/horizon/issues/1180)). Reflections, global illumination, Apple M3 and newer if it is cheap, and a performance report per card come after. + +
+ +#### HDR monitors + +Output for HDR monitors: brighter highlights and richer colour where your screen supports it. + +
Technical + +Issue [#1441](https://github.com/ludovic111/horizon/issues/1441). Needs a surface format and a tone-mapping path per backend, and tests on real HDR screens on Windows, macOS and Linux. + +
+ +#### MSAA, anisotropic filtering, exclusive fullscreen + +More graphics options: multisample anti-aliasing, anisotropic filtering and an exclusive fullscreen mode. Today Horizon has FXAA and borderless fullscreen. + +
Technical + +Not implemented ([docs/graphics.md](docs/graphics.md)). Anisotropic filtering needs linear magnification in wgpu, which blurs close blocks, so close-up sharpness has to be kept first; MSAA would sit beside FXAA; exclusive fullscreen where the backend supports it. + +
+ +### Launcher + +#### Lean installs that use less disk + +Smaller installs: Horizon keeps one shared copy of the prepared game files and removes what only the preparation needed. + +
Technical + +A shared resource store keyed by the preparer's digest, no prepare intermediates left behind, the current and the previous version kept, and an automatic clean-up. Not started. + +
+ +### Platforms + +#### Horizon on Linux ARM64 + +Game and launcher builds for 64-bit ARM Linux computers. + +
Technical + +Issue [#1709](https://github.com/ludovic111/horizon/issues/1709): aarch64 builds of the game and the launcher in the release workflow, tested on a real ARM machine with its Vulkan driver. The server build already drops audio and controllers (`--no-default-features`). + +
+ +#### Android, iOS and the browser + +Versions of Horizon for Android, iOS and web browsers. Nothing official yet; contributors have started the first pieces. + +
Technical + +No official build. Android was first closed as not planned, with touch controls, a mobile interface, packaging and loading the player's own assets all missing; the roadmap now includes it, iOS and a WebAssembly build for the browser as long-term goals. First pieces from contributors: `horizon-dualsense` compiling on iOS ([#1813](https://github.com/ludovic111/horizon/issues/1813)) and helper-thread limits on Android ([#1751](https://github.com/ludovic111/horizon/issues/1751)). Preparing without Java ([#1750](https://github.com/ludovic111/horizon/issues/1750)) matters here too. Horizon ships none of Mojang's files on any platform. + +
+ +#### AppImage and a Nix flake + +Install Horizon on any Linux distribution as an AppImage, and on NixOS through a Nix flake. + +
Technical + +A player's idea (#horizon-ideas). An AppImage of the launcher and game from the release workflow, and a flake.nix packaging the release binaries with their runtime libraries (Vulkan loader, X11/Wayland, ALSA), the way horizon-parity's NixOS setup runs them today.
### Horizon extras -#### An optional AI teammate +#### A complete video studio inside the game -An AI teammate that plays beside you and that you can talk to. It is off unless you turn it on, and the No AI switch hides it completely. +Record, replay and edit videos without leaving the game: record in a keypress, replay a session, cut and finish the video. One of Horizon's original goals.
Technical -Draft pull requests: the companion ([#1068](https://github.com/ludovic111/horizon/issues/1068)), its safety modes and their evaluation ([#1069](https://github.com/ludovic111/horizon/issues/1069)), and a route that finishes the game ([#1070](https://github.com/ludovic111/horizon/issues/1070)). Local models first; it ships only once the safety evaluation passes. +Three stages. Recording: capture gameplay in a keypress with an encoder thread and little frame-time cost. Replay: a replay mode for recorded sessions. Editing: cuts, camera paths, titles and export. None of it exists yet; the agent-control socket ([docs/agent-control.md](docs/agent-control.md)) already lets tools drive the game for scripted shots. + +
+ +#### Game menus in the launcher's style + +An optional look for the game's own menus and options that matches the Horizon launcher. Off by default: the vanilla menus stay as they are. + +
Technical + +A player's idea (#horizon-ideas). A built-in resource pack or a Horizon setting that restyles the title, pause and options screens with the launcher's design tokens (crates/horizon-theme), following [docs/design-system.md](docs/design-system.md); off by default because it changes the vanilla look. + +
+ +### AI and agents + +#### An optional AI teammate + +An optional AI teammate you can talk to by voice. It plays beside you, helps or finishes the game with you, runs on your computer first, and exists only if you turn it on. The No AI switch hides it. + +
Technical + +Draft pull requests: the companion ([#1068](https://github.com/ludovic111/horizon/issues/1068)), its safety modes and their evaluation ([#1069](https://github.com/ludovic111/horizon/issues/1069)) and a route that finishes the game ([#1070](https://github.com/ludovic111/horizon/issues/1070)). In the drafts: a companion that follows, fights, mines, crafts, sleeps and respawns; chat and push-to-talk voice, with speech recognition and a choice of voices; presets (very strong, helper, beat the game); a guard model with Clean, Kid and Salty modes; perception levels from Fair to All-knowing; hardware detection with a simple mode and an Advanced tab; local models first, cloud keys optional. The No AI switch is already in the game. Left: the speedrun route doesn't finish the game reliably yet, voices for more languages, and the safety evaluation, which must pass before it ships. + +
+ +### Community + +#### A video series about Horizon + +A series of videos about how Horizon is made and what is new, with each upload announced on Discord. + +
Technical + +Kira already shows new uploads in a read-only Discord channel (done). The series follows each stage of the project: a script, edits, titles and a thumbnail per video.
@@ -330,6 +954,7 @@ Vanilla's `Chicken.aiStep` updates `flap` and `flapSpeed` when the chicken isn't ## Recently shipped +- **OpenGL for old computers** (v0.2.63): Horizon now starts on old PCs whose graphics card has no Vulkan or DirectX 12 driver: it switches to OpenGL by itself, and Options > Graphics can pick OpenGL by hand. [#1917](https://github.com/ludovic111/horizon/issues/1917) · [#1920](https://github.com/ludovic111/horizon/issues/1920) · [#1936](https://github.com/ludovic111/horizon/issues/1936) - **Server resource packs** (v0.2.48): Servers can send you their resource pack: Horizon asks, downloads it and applies it, like vanilla. [#1562](https://github.com/ludovic111/horizon/issues/1562) · [#1569](https://github.com/ludovic111/horizon/issues/1569) · [#1570](https://github.com/ludovic111/horizon/issues/1570) - **Single player on its own server thread** (v0.2.39): Single player runs on a server thread, as vanilla does, so the world keeps ticking smoothly while the window draws. [#1514](https://github.com/ludovic111/horizon/issues/1514) · [docs/server-levels.md](docs/server-levels.md) - **GPU occlusion culling on Macs** (v0.2.17): On Macs the GPU now skips terrain hidden behind hills and caves: 8 to 48% more frames per second at 32 chunks on an M4 Pro. [#1434](https://github.com/ludovic111/horizon/issues/1434) diff --git a/crates/cargo-horizon-mod/src/check.rs b/crates/cargo-horizon-mod/src/check.rs index dfb9820a2..df2254c14 100644 --- a/crates/cargo-horizon-mod/src/check.rs +++ b/crates/cargo-horizon-mod/src/check.rs @@ -21,6 +21,8 @@ pub struct Report { pub sha256: String, pub size: u64, pub signed: bool, + /// A mod has code to run (a content mod may have none). + pub code: bool, /// A mod's component imports. pub imports: Vec, /// A mod's commands, each run once. @@ -49,6 +51,7 @@ impl Report { "sha256": self.sha256, "size": self.size, "signed": self.signed, + "code": self.code, "imports": self.imports, "commands": self.commands, "output": self.output, @@ -87,7 +90,9 @@ impl Report { for r in &f.requires { out += &format!(" needs {} {} ({})\n", r.id, r.version, &r.sha256[..12]); } - if self.kind == Kind::Mod { + if self.kind == Kind::Mod && !self.code { + out += " no code: content only, nothing to run\n"; + } else if self.kind == Kind::Mod { let commands = if self.commands.is_empty() { String::new() } else { @@ -142,6 +147,7 @@ pub fn check( sha256: index::sha256_file(path)?, size, signed, + code: false, imports: Vec::new(), commands: Vec::new(), output: Vec::new(), @@ -153,10 +159,14 @@ pub fn check( } /// Runs a mod in the sandbox, as the game does, from a scratch copy so its -/// storage lands in a folder removed afterwards. +/// storage lands in a folder removed afterwards. A content mod has no code to +/// run: `inspect` already read its content as the game does. fn trial_run(path: &Path, report: &mut Report) -> Result<(), String> { let package = Package::read(path)?; - let wasm = package.wasm.as_deref().ok_or("the .hmod has no mod.wasm")?; + let Some(wasm) = package.wasm.as_deref() else { + return Ok(()); + }; + report.code = true; report.imports = component_imports(wasm)?; package .manifest @@ -216,6 +226,7 @@ fn play(path: &Path, report: &mut Report) -> Result<(), String> { Output::Feedback { text, error } => { format!("{}: {text}", if error { "error" } else { "feedback" }) } + Output::Command { line } => format!("command: /{line}"), Output::Stopped { reason, .. } => return Err(format!("it stopped: {reason}")), Output::OpenScreen { .. } => "opened a screen".to_string(), Output::CloseScreen { .. } => "closed its screen".to_string(), diff --git a/crates/cargo-horizon-mod/src/main.rs b/crates/cargo-horizon-mod/src/main.rs index 925633930..a4585f680 100644 --- a/crates/cargo-horizon-mod/src/main.rs +++ b/crates/cargo-horizon-mod/src/main.rs @@ -1,14 +1,16 @@ //! `cargo horizon-mod`: make Horizon mods in Rust (`docs/mods.md`). //! -//! - `new [--name "Name"] [--sdk-path ]`: a mod crate from the template. -//! - `build [--debug]`: the `.hmod` in `target/horizon-mod/`. +//! - `new [--template code|content] [--name "Name"] [--sdk-path ]`: +//! a mod from a template: a Rust crate, or a content mod (no code). +//! - `build [--debug]`: the `.hmod` in `target/horizon-mod/`, with the mod's +//! `assets/` and `data/`; a folder without `Cargo.toml` is content only. //! - `run [--game ] [-- ]`: build, then play with //! the mod; run `build` again and the game reloads it. //! - `check [--json]`: what the registry checks of a submission //! (`.hmod`, `.hpack`, resource pack `.zip`), with a trial run for a mod. //! - `registry build|review|check|list`: the registry repository's CI. use horizon_mods::{ - Manifest, Package, + Capability, Content, Manifest, Package, manifest::{MANIFEST, component_imports, componentize}, }; mod check; @@ -36,7 +38,7 @@ fn main() -> ExitCode { Some("run") => run(&args[1..]), Some("check") => check(&args[1..]), Some("registry") => registry::main(&args[1..]), - _ => Err("usage: cargo horizon-mod new | build [--debug] | run [--game ] [-- ] | check [--kind ] [--key --signature ] [--json] | registry build|review|check|list".into()), + _ => Err("usage: cargo horizon-mod new [--template code|content] | build [--debug] | run [--game ] [-- ] | check [--kind ] [--key --signature ] [--json] | registry build|review|check|list".into()), }; match result { Ok(()) => ExitCode::SUCCESS, @@ -90,19 +92,68 @@ fn new(args: &[String]) -> Result<(), String> { .replace("{name}", &name) .replace("{sdk}", &sdk) }; - let files = [ + let code = [ ("Cargo.toml", include_str!("../template/Cargo.toml.in")), (MANIFEST, include_str!("../template/horizon-mod.toml")), ("src/lib.rs", include_str!("../template/src/lib.rs")), (".gitignore", include_str!("../template/gitignore")), ]; - for (file, text) in files { + // A block copying a vanilla one, with its model, name, loot, recipe and tag. + let content = [ + ( + MANIFEST, + include_str!("../template/content/horizon-mod.toml"), + ), + ( + "data/{id}/horizon/block/glowing_amethyst.json", + include_str!("../template/content/block.json"), + ), + ( + "assets/{id}/blockstates/glowing_amethyst.json", + include_str!("../template/content/blockstate.json"), + ), + ( + "assets/{id}/models/block/glowing_amethyst.json", + include_str!("../template/content/model.json"), + ), + ( + "assets/{id}/items/glowing_amethyst.json", + include_str!("../template/content/item.json"), + ), + ( + "assets/{id}/lang/en_us.json", + include_str!("../template/content/lang.json"), + ), + ( + "data/{id}/loot_table/blocks/glowing_amethyst.json", + include_str!("../template/content/loot.json"), + ), + ( + "data/{id}/recipe/glowing_amethyst.json", + include_str!("../template/content/recipe.json"), + ), + ( + "data/minecraft/tags/block/mineable/pickaxe.json", + include_str!("../template/content/pickaxe.json"), + ), + (".gitignore", include_str!("../template/gitignore")), + ]; + let files: &[(&str, &str)] = match flag(args, "--template").unwrap_or("code") { + "code" => &code, + "content" => &content, + other => return Err(format!("no template {other}: code or content")), + }; + for &(file, text) in files { + let file = fill(file); + let file = file.as_str(); let to = dir.join(file); std::fs::create_dir_all(to.parent().expect("a file in the mod")) .map_err(|e| e.to_string())?; std::fs::write(&to, fill(text)).map_err(|e| format!("{}: {e}", to.display()))?; } - Manifest::parse(&std::fs::read_to_string(dir.join(MANIFEST)).map_err(|e| e.to_string())?)?; + let manifest = + Manifest::parse(&std::fs::read_to_string(dir.join(MANIFEST)).map_err(|e| e.to_string())?)?; + Content::read(dir, &manifest)?; println!( "Created {} (mod id {id}). Next: cd {path} && cargo horizon-mod run", dir.display() @@ -110,14 +161,53 @@ fn new(args: &[String]) -> Result<(), String> { Ok(()) } -/// Builds the crate in the current folder and packs `target/horizon-mod/.hmod`. +/// Builds the mod in the current folder and packs `target/horizon-mod/.hmod`: +/// the manifest, the crate's component (none without a `Cargo.toml`: a +/// content mod) and the files in `assets/` and `data/`. fn build(release: bool) -> Result { - let manifest_text = std::fs::read_to_string(MANIFEST) + build_in(Path::new("."), release) +} + +fn build_in(root: &Path, release: bool) -> Result { + let manifest_text = std::fs::read_to_string(root.join(MANIFEST)) .map_err(|e| format!("{MANIFEST}: {e} (run this in a mod's folder)"))?; let manifest = Manifest::parse(&manifest_text)?; + let (wasm, target_dir) = if root.join("Cargo.toml").exists() { + let (wasm, target) = compile(root, release)?; + // The game refuses a component importing capabilities the manifest lacks. + manifest.check_imports(component_imports(&wasm)?.iter().map(String::as_str))?; + (Some(wasm), target) + } else if manifest.capabilities.contains(&Capability::Registries) { + (None, root.join("target")) + } else { + return Err(format!( + "no Cargo.toml: only a content mod (capability registries in {MANIFEST}) has no code" + )); + }; + // The same checks as the game's, and the files in the order it lists them. + let content = Content::read(root, &manifest)?; + let mut reader = content.reader()?; + let mut files = Vec::with_capacity(content.files.len()); + for name in &content.files { + files.push((name.as_str(), reader.read(name)?)); + } + let files: Vec<(&str, &[u8])> = files.iter().map(|(n, b)| (*n, b.as_slice())).collect(); + let dir = target_dir.join("horizon-mod"); + std::fs::create_dir_all(&dir).map_err(|e| e.to_string())?; + let path = dir.join(format!("{}.hmod", manifest.id)); + // Write then rename, so a running game never reads half a file. + let partial = path.with_extension("hmod.partial"); + let bytes = Package::write_files(&manifest_text, wasm.as_deref(), &files)?; + std::fs::write(&partial, bytes).map_err(|e| e.to_string())?; + std::fs::rename(&partial, &path).map_err(|e| e.to_string())?; + Ok(path) +} + +/// Builds the crate for WebAssembly: its component, and Cargo's target folder. +fn compile(root: &Path, release: bool) -> Result<(Vec, PathBuf), String> { let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".into()); let mut cmd = Command::new(cargo); - cmd.args([ + cmd.current_dir(root).args([ "build", "--lib", "--target", @@ -145,17 +235,8 @@ fn build(release: bool) -> Result { .rfind(|f| f.extension().is_some_and(|e| e == "wasm")) .ok_or("the build made no .wasm: is the crate a cdylib?")?; let wasm = componentize(&std::fs::read(&core).map_err(|e| e.to_string())?)?; - // The game refuses a component importing capabilities the manifest lacks. - manifest.check_imports(component_imports(&wasm)?.iter().map(String::as_str))?; - let target_dir = core.ancestors().nth(3).ok_or("unexpected target folder")?; - let dir = target_dir.join("horizon-mod"); - std::fs::create_dir_all(&dir).map_err(|e| e.to_string())?; - let path = dir.join(format!("{}.hmod", manifest.id)); - // Write then rename, so a running game never reads half a file. - let partial = path.with_extension("hmod.partial"); - std::fs::write(&partial, Package::write(&manifest_text, &wasm)?).map_err(|e| e.to_string())?; - std::fs::rename(&partial, &path).map_err(|e| e.to_string())?; - Ok(path) + let target = core.ancestors().nth(3).ok_or("unexpected target folder")?; + Ok((wasm, target.to_path_buf())) } /// `check `: the registry's checks of a submission, printed; fails @@ -237,4 +318,49 @@ mod tests { ); std::fs::remove_dir_all(&dir).unwrap(); } + + #[test] + fn the_content_template_builds_and_checks_with_no_code() { + let dir = std::env::temp_dir().join(format!("horizon-mod-content-{}", std::process::id())); + let path = dir.join("gems"); + let new_args = [ + path.display().to_string(), + "--template".into(), + "content".into(), + ]; + new(&new_args).unwrap(); + assert!(!path.join("Cargo.toml").exists()); + let hmod = build_in(&path, true).unwrap(); + assert_eq!(hmod, path.join("target/horizon-mod/gems.hmod")); + let package = Package::read(&hmod).unwrap(); + assert!(package.wasm.is_none()); + let content = Content::read(&hmod, &package.manifest).unwrap(); + assert!( + content + .files + .contains(&"data/gems/horizon/block/glowing_amethyst.json".into()) + ); + assert_eq!(content.files.len(), 8, "{:?}", content.files); + let report = check::check(&hmod, check::kind_of(&hmod).unwrap(), None, None).unwrap(); + assert!(!report.code); + assert!( + report.text().contains("no code: content only"), + "{}", + report.text() + ); + // Without registries, a mod needs code. + let manifest = std::fs::read_to_string(path.join(MANIFEST)).unwrap(); + let manifest = manifest.replace("[\"registries\"]", "[]"); + std::fs::write(path.join(MANIFEST), manifest).unwrap(); + assert!(build_in(&path, true).unwrap_err().contains("no Cargo.toml")); + assert!( + new(&[ + dir.join("x").display().to_string(), + "--template".into(), + "lua".into() + ]) + .is_err() + ); + std::fs::remove_dir_all(&dir).unwrap(); + } } diff --git a/crates/cargo-horizon-mod/template/content/block.json b/crates/cargo-horizon-mod/template/content/block.json new file mode 100644 index 000000000..ea68a1a27 --- /dev/null +++ b/crates/cargo-horizon-mod/template/content/block.json @@ -0,0 +1,4 @@ +{ + "template": "minecraft:amethyst_block", + "properties": { "hardness": 2.0, "light": 7 } +} diff --git a/crates/cargo-horizon-mod/template/content/blockstate.json b/crates/cargo-horizon-mod/template/content/blockstate.json new file mode 100644 index 000000000..ea472666e --- /dev/null +++ b/crates/cargo-horizon-mod/template/content/blockstate.json @@ -0,0 +1,5 @@ +{ + "variants": { + "": { "model": "{id}:block/glowing_amethyst" } + } +} diff --git a/crates/cargo-horizon-mod/template/content/horizon-mod.toml b/crates/cargo-horizon-mod/template/content/horizon-mod.toml new file mode 100644 index 000000000..4f133a905 --- /dev/null +++ b/crates/cargo-horizon-mod/template/content/horizon-mod.toml @@ -0,0 +1,10 @@ +id = "{id}" +name = "{name}" +version = "0.1.0" +description = "" +authors = [] +# A content mod: blocks and items from data/ and assets/, no code (API 0.2). +api = "0.2" +side = "both" +# registries: "May add blocks and items to your worlds". +capabilities = ["registries"] diff --git a/crates/cargo-horizon-mod/template/content/item.json b/crates/cargo-horizon-mod/template/content/item.json new file mode 100644 index 000000000..411bc4499 --- /dev/null +++ b/crates/cargo-horizon-mod/template/content/item.json @@ -0,0 +1,3 @@ +{ + "model": { "type": "minecraft:model", "model": "{id}:block/glowing_amethyst" } +} diff --git a/crates/cargo-horizon-mod/template/content/lang.json b/crates/cargo-horizon-mod/template/content/lang.json new file mode 100644 index 000000000..74baddab8 --- /dev/null +++ b/crates/cargo-horizon-mod/template/content/lang.json @@ -0,0 +1,3 @@ +{ + "block.{id}.glowing_amethyst": "Glowing Amethyst" +} diff --git a/crates/cargo-horizon-mod/template/content/loot.json b/crates/cargo-horizon-mod/template/content/loot.json new file mode 100644 index 000000000..898111f1b --- /dev/null +++ b/crates/cargo-horizon-mod/template/content/loot.json @@ -0,0 +1,11 @@ +{ + "type": "minecraft:block", + "pools": [ + { + "condition": { "type": "minecraft:survives_explosion" }, + "entries": [{ "type": "minecraft:item", "name": "{id}:glowing_amethyst" }], + "rolls": 1 + } + ], + "random_sequence": "{id}:blocks/glowing_amethyst" +} diff --git a/crates/cargo-horizon-mod/template/content/model.json b/crates/cargo-horizon-mod/template/content/model.json new file mode 100644 index 000000000..5710fecb1 --- /dev/null +++ b/crates/cargo-horizon-mod/template/content/model.json @@ -0,0 +1,4 @@ +{ + "parent": "minecraft:block/cube_all", + "textures": { "all": "minecraft:block/amethyst_block" } +} diff --git a/crates/cargo-horizon-mod/template/content/pickaxe.json b/crates/cargo-horizon-mod/template/content/pickaxe.json new file mode 100644 index 000000000..8725edebd --- /dev/null +++ b/crates/cargo-horizon-mod/template/content/pickaxe.json @@ -0,0 +1,3 @@ +{ + "values": ["{id}:glowing_amethyst"] +} diff --git a/crates/cargo-horizon-mod/template/content/recipe.json b/crates/cargo-horizon-mod/template/content/recipe.json new file mode 100644 index 000000000..5edb03d94 --- /dev/null +++ b/crates/cargo-horizon-mod/template/content/recipe.json @@ -0,0 +1,6 @@ +{ + "type": "minecraft:crafting_shapeless", + "category": "building", + "ingredients": ["minecraft:amethyst_block", "minecraft:glowstone_dust"], + "result": { "id": "{id}:glowing_amethyst" } +} diff --git a/crates/horizon-client/Cargo.toml b/crates/horizon-client/Cargo.toml index ea5859c23..4877c18f2 100644 --- a/crates/horizon-client/Cargo.toml +++ b/crates/horizon-client/Cargo.toml @@ -68,6 +68,11 @@ rustix = { version = "1", default-features = false, features = ["std", "time"] } # on the PATH. wgpu = { version = "30.0.1", default-features = false, features = ["dx12"] } +[target.'cfg(not(target_os = "macos"))'.dependencies] +# OpenGL (WGL on Windows, EGL on Linux), the last fallback, for GPUs with no Vulkan or +# DirectX 12 driver (`graphicsApi`, graphics.rs). +wgpu = { version = "30.0.1", default-features = false, features = ["gles"] } + [target.'cfg(windows)'.build-dependencies] # The icon resource in the .exe (build.rs). winresource = "0.1" diff --git a/crates/horizon-client/examples/probe/lore_tooltip_fixture.rs b/crates/horizon-client/examples/probe/lore_tooltip_fixture.rs new file mode 100644 index 000000000..350d9be81 --- /dev/null +++ b/crates/horizon-client/examples/probe/lore_tooltip_fixture.rs @@ -0,0 +1,53 @@ +//! Lore tooltip companion for tools/test_lore_tooltip_parity.py: the spans +//! `item_tooltip::lore_texts` gives each stack (SNBT, one per line). +use horizon_client::{ + item_data::parse_stack, + item_tooltip::{TooltipDisplay, lore_texts}, +}; +use std::{error::Error, fmt::Write}; + +pub fn main() -> Result<(), Box> { + let args: Vec = crate::args().collect(); + let input = std::fs::read_to_string(&args[1])?; + let mut output = String::new(); + for line in input + .lines() + .filter(|l| !l.is_empty() && !l.starts_with('#')) + { + let stack = parse_stack(line)?.ok_or("empty stack")?; + let display = TooltipDisplay::for_stack(&stack); + if !display.shows("minecraft:lore") { + writeln!(output, "-")?; + continue; + } + let lines = lore_texts(&stack); + if lines.is_empty() { + writeln!(output, "-")?; + continue; + } + let mut rendered = Vec::new(); + for text in lines { + let mut spans = Vec::new(); + for span in &text.spans { + if span.text.is_empty() { + continue; + } + let style = &span.style; + spans.push(format!( + "{}\t{}\t{}\t{}\t{}\t{}\t{}", + span.text, + style.color.map(|c| c as i32).unwrap_or(-1), + style.bold == Some(true), + style.italic == Some(true), + style.underlined == Some(true), + style.strikethrough == Some(true), + style.obfuscated == Some(true), + )); + } + rendered.push(spans.join(" | ")); + } + writeln!(output, "{}", rendered.join(" || "))?; + } + std::fs::write(&args[2], output)?; + Ok(()) +} diff --git a/crates/horizon-client/examples/probe/main.rs b/crates/horizon-client/examples/probe/main.rs index f05c2a662..e80088fd0 100644 --- a/crates/horizon-client/examples/probe/main.rs +++ b/crates/horizon-client/examples/probe/main.rs @@ -49,6 +49,7 @@ mod input_probe; mod interaction_input_fixture; mod lan_probe; mod light_parity; +mod lore_tooltip_fixture; mod marker_particles_probe; mod menu_fixture; mod mipmap_probe; @@ -133,6 +134,7 @@ probes! { interaction_input_fixture, lan_probe, light_parity, + lore_tooltip_fixture, marker_particles_probe, menu_fixture, mipmap_probe, diff --git a/crates/horizon-client/examples/probe/marker_particles_probe.rs b/crates/horizon-client/examples/probe/marker_particles_probe.rs index 190751d79..72ebaff22 100644 --- a/crates/horizon-client/examples/probe/marker_particles_probe.rs +++ b/crates/horizon-client/examples/probe/marker_particles_probe.rs @@ -49,7 +49,7 @@ pub fn main() -> Result<()> { } } } - let mut engine = ParticleEngine::new(root, &HashMap::new()); + let mut engine = ParticleEngine::new(root, &HashMap::new(), &Default::default()); let mut out = BufWriter::new(File::create(&args[2])?); for line in BufReader::new(File::open(&args[1])?).lines() { let case: Value = serde_json::from_str(&line?)?; diff --git a/crates/horizon-client/examples/probe/menu_fixture.rs b/crates/horizon-client/examples/probe/menu_fixture.rs index 3c83b20bc..9ec558de3 100644 --- a/crates/horizon-client/examples/probe/menu_fixture.rs +++ b/crates/horizon-client/examples/probe/menu_fixture.rs @@ -169,6 +169,7 @@ pub fn main() -> horizon_client::Result<()> { cooking_timer: menu.data[2], cooking_total: menu.data[3], speed: 1.0, + recipes_used: Default::default(), }); menu.drops.clear(); menu.equipped.clear(); diff --git a/crates/horizon-client/examples/probe/particle_providers_probe.rs b/crates/horizon-client/examples/probe/particle_providers_probe.rs index c8cd9d88d..68f23f3af 100644 --- a/crates/horizon-client/examples/probe/particle_providers_probe.rs +++ b/crates/horizon-client/examples/probe/particle_providers_probe.rs @@ -152,7 +152,7 @@ pub fn main() -> Result<()> { world(&assets, &facts, false, true)?, world(&assets, &facts, true, true)?, ]; - let mut engine = ParticleEngine::new(root, &HashMap::new()); + let mut engine = ParticleEngine::new(root, &HashMap::new(), &Default::default()); for (ty, item) in [ (ParticleType::ItemSlime, "item_slime_ball"), (ParticleType::ItemSnowball, "item_snowball"), diff --git a/crates/horizon-client/src/animate_tick.rs b/crates/horizon-client/src/animate_tick.rs index ce34df510..48e8f16f4 100644 --- a/crates/horizon-client/src/animate_tick.rs +++ b/crates/horizon-client/src/animate_tick.rs @@ -63,6 +63,10 @@ enum Animate { SculkSensor, /// Potent sulfur that is not dry. PotentSulfur, + DriedGhast { + waterlogged: bool, + }, + EndPortal, } fn step(facing: &str) -> [i32; 3] { match facing { @@ -137,6 +141,10 @@ fn classify(name: &str, prop: &dyn Fn(&str) -> Option) -> Animate { drag: prop("drag").as_deref() == Some("true"), }, "fire" | "soul_fire" => Animate::Fire, + "dried_ghast" => Animate::DriedGhast { + waterlogged: prop("waterlogged").as_deref() == Some("true"), + }, + "end_portal" => Animate::EndPortal, "sculk_sensor" | "calibrated_sculk_sensor" if prop("sculk_sensor_phase").as_deref() == Some("active") => { @@ -180,6 +188,8 @@ pub struct AnimateTables { impermeable: Vec, power_provider: Vec, power_transmitter: Vec, + /// `#triggers_ambient_dried_ghast_block_sounds` (soul sand, soul soil). + dried_ghast_sounds: Vec, /// Biome id -> ambient particles and probabilities (`minecraft:visual/ambient_particles`). ambient: Vec>, } @@ -237,6 +247,7 @@ impl AnimateTables { impermeable: by_state("minecraft:impermeable"), power_provider: by_state("minecraft:enchantment_power_provider"), power_transmitter: by_state("minecraft:enchantment_power_transmitter"), + dried_ghast_sounds: by_state("minecraft:triggers_ambient_dried_ghast_block_sounds"), ambient, }) } @@ -869,6 +880,45 @@ fn block_animate_tick( ]; add(engine, T::Smoke, p, [0.0; 3]); } + Animate::DriedGhast { waterlogged } => { + // DriedGhastBlock.animateTick. + let c = [fx + 0.5, fy + 0.5, fz + 0.5]; + if waterlogged { + if next_int(engine.random(), 40) == 0 { + let name = "minecraft:block.dried_ghast.ambient_water"; + sound(engine, name, c, 1.0, 1.0); + } + let r = engine.random(); + if next_int(r, 6) == 0 { + let x = c[0] + f64::from((r.next_float() * 2.0 - 1.0) / 3.0); + let y = c[1] + 0.4; + let z = c[2] + f64::from((r.next_float() * 2.0 - 1.0) / 3.0); + let v = [0.0, f64::from(r.next_float()), 0.0]; + add(engine, T::HappyVillager, [x, y, z], v); + } + } else { + let below = ctx.state([pos[0], pos[1] - 1, pos[2]]); + let triggers = || { + tables + .dried_ghast_sounds + .get(below.0 as usize) + .copied() + .unwrap_or(false) + }; + if next_int(engine.random(), 40) == 0 && triggers() { + sound(engine, "minecraft:block.dried_ghast.ambient", c, 1.0, 1.0); + } + if next_int(engine.random(), 6) == 0 { + add(engine, T::WhiteSmoke, c, [0.0, 0.02, 0.0]); + } + } + } + Animate::EndPortal => { + // EndPortalBlock.animateTick: one smoke particle above the surface. + let r = engine.random(); + let p = [fx + r.next_double(), fy + 0.8, fz + r.next_double()]; + add(engine, T::Smoke, p, [0.0; 3]); + } Animate::EnderChest => { for _ in 0..3 { let r = engine.random(); diff --git a/crates/horizon-client/src/containers.rs b/crates/horizon-client/src/containers.rs index de2e1b2e9..26ca193a2 100644 --- a/crates/horizon-client/src/containers.rs +++ b/crates/horizon-client/src/containers.rs @@ -44,6 +44,10 @@ pub struct Furnace { pub cooking_timer: i32, pub cooking_total: i32, pub speed: f32, + /// `AbstractFurnaceBlockEntity.recipesUsed`: how many of each recipe this furnace has + /// made since a player last took from its output. The experience is owed per recipe + /// rather than per item, and a save keeps it as the `RecipesUsed` compound. + pub recipes_used: HashMap, } /// A hopper's slots: the only plain container with five. pub const HOPPER_SLOTS: usize = 5; @@ -190,6 +194,7 @@ impl BlockEntity { cooking_timer: 0, cooking_total: 0, speed: 1.0, + recipes_used: HashMap::new(), })); } if name == "minecraft:creaking_heart" { @@ -483,6 +488,14 @@ impl Furnace { self.cooking_timer = 0; } } + /// `recipesUsed`, each recipe with its `experience` (0 for one this data pack does + /// not have), in the order `getRecipesToAwardAndPopExperience` pays them. + pub fn recipe_experience(&self, recipes: &Recipes) -> Vec<(u32, f32)> { + self.recipes_used + .iter() + .map(|(id, count)| (*count, recipes.cooking_experience(id))) + .collect() + } /// A menu click wrote its slots into `items`, and `before` was the input /// slot until then: the menu called `setItem` on it only if it changed /// (`Slot.setByPlayer`; partial takes and merges keep the stack). @@ -564,6 +577,7 @@ impl Furnace { Some(out) => out.count += recipe.result.count, None => self.items[2] = Some(recipe.result.clone()), } + *self.recipes_used.entry(recipe.id.clone()).or_insert(0) += 1; if let Some(i) = self.items[0].as_mut() { i.count -= 1; if i.count == 0 { @@ -594,3 +608,41 @@ fn total_time(time: u32, speed: f32) -> i32 { time as i32 } } + +/// `AbstractFurnaceBlockEntity.createExperience`: `count * experience`, floored, and one +/// more with the chance of what is left over (a level random draw only when there is some). +pub fn create_experience( + count: u32, + experience: f32, + random: &mut impl horizon_core::random::RandomSource, +) -> u32 { + let total = count as f32 * experience; + let whole = total.floor(); + let fraction = total - whole; + let mut xp = whole.max(0.0) as u32; + if fraction != 0.0 && random.next_float() < fraction { + xp += 1; + } + xp +} + +#[cfg(test)] +mod tests { + use super::*; + use horizon_core::random::{LegacyRandom, RandomSource}; + + /// A whole amount pays itself without a draw; a fraction is a chance of one more. + #[test] + fn create_experience_rounds_the_fraction_by_chance() { + let mut random = LegacyRandom::new(7); + let mut untouched = LegacyRandom::new(7); + assert_eq!(create_experience(4, 0.5, &mut random), 2); + assert_eq!(create_experience(3, 0.0, &mut random), 0); + assert_eq!(random.next_long(), untouched.next_long()); + let paid: u32 = (0..1000) + .map(|_| create_experience(1, 0.35, &mut random)) + .sum(); + assert!((250..450).contains(&paid), "{paid}"); + assert!((0..100).all(|_| (1..=2).contains(&create_experience(1, 1.7, &mut random)))); + } +} diff --git a/crates/horizon-client/src/crafting.rs b/crates/horizon-client/src/crafting.rs index d0b8906ee..690383963 100644 --- a/crates/horizon-client/src/crafting.rs +++ b/crates/horizon-client/src/crafting.rs @@ -1610,6 +1610,14 @@ impl Recipes { } out } + /// The `experience` of the cooking recipe with this file name, and 0 for one this data + /// pack does not have (a save from another pack can name it). + pub fn cooking_experience(&self, id: &str) -> f32 { + self.cooking + .iter() + .find(|c| c.id == id) + .map_or(0.0, |c| c.experience) + } pub fn cook(&self, kind: &str, input: &ItemStack) -> Option<&Cooking> { let index = self.cook_index.get_or_init(|| { let mut index = CookIndex::new(); diff --git a/crates/horizon-client/src/creative.rs b/crates/horizon-client/src/creative.rs index e1d1c262e..249e40b11 100644 --- a/crates/horizon-client/src/creative.rs +++ b/crates/horizon-client/src/creative.rs @@ -71,10 +71,52 @@ impl Names { .cloned() .unwrap_or_else(|| path.replace('_', " ")) } + /// An item's display name in the selected language: `item.*` then + /// `block.*`, then the pinned English map (`Names::item`). + pub fn display_item(&self, id: &str) -> String { + let (namespace, path) = id.split_once(':').unwrap_or(("minecraft", id)); + if let Some(lang) = crate::lang::current() { + for prefix in ["item", "block"] { + if let Some(name) = lang.get(&format!("{prefix}.{namespace}.{path}")) { + return name.to_owned(); + } + } + } + self.item(id) + } + /// A translation key's text in the selected language, else the pinned + /// English map (`HoverName` and tooltip keys such as potion names). + pub fn display_get(&self, key: &str) -> Option { + crate::lang::current() + .and_then(|lang| lang.get(key).map(str::to_owned)) + .or_else(|| self.get(key).map(str::to_owned)) + } pub fn get(&self, key: &str) -> Option<&str> { self.map.get(key).map(String::as_str) } } + +/// `CreativeModeTabs`' key for an exported tab id (`itemGroup.*`); the +/// export's `title` is the English fallback. +pub fn item_group_key(id: &str) -> Option<&'static str> { + Some(match id.trim_start_matches("minecraft:") { + "building_blocks" => "itemGroup.buildingBlocks", + "colored_blocks" => "itemGroup.coloredBlocks", + "natural_blocks" => "itemGroup.natural", + "functional_blocks" => "itemGroup.functional", + "redstone_blocks" => "itemGroup.redstone", + "hotbar" => "itemGroup.hotbar", + "search" => "itemGroup.search", + "tools_and_utilities" => "itemGroup.tools", + "combat" => "itemGroup.combat", + "food_and_drinks" => "itemGroup.foodAndDrink", + "ingredients" => "itemGroup.ingredients", + "spawn_eggs" => "itemGroup.spawnEggs", + "op_blocks" => "itemGroup.op", + "inventory" => "itemGroup.inventory", + _ => return None, + }) +} impl Creative { pub const WIDTH: f32 = 195.0; pub const HEIGHT: f32 = 136.0; @@ -303,7 +345,17 @@ impl Creative { .collect(); } // FullTextSearchTree: tooltip lines, and the item id after a colon. + // The export's lines are English (they come from the vanilla jars); + // the hover name in the selected language is matched too, so a + // localized search finds the item. It is the whole name (a tipped + // arrow's potion, the ominous banner), as the first line is. let text = java_lower(term); + let localized = |i: usize| { + java_lower(&crate::item_tooltip::name_line( + &self.search_items[i], + &|k| names.display_get(k), + )) + }; all.filter(|&i| { let e = &self.search_index[i]; let fallback; @@ -314,11 +366,15 @@ impl Creative { &e.lines }; match text.split_once(':') { - None => any(lines.iter().map(String::as_str), &text), + None => { + any(lines.iter().map(String::as_str), &text) || localized(i).contains(&text) + } Some((ns, path)) => { let (ns, path) = (java_trim(ns), java_trim(path)); e.namespace.contains(ns) - && (e.path.contains(path) || any(lines.iter().map(String::as_str), path)) + && (e.path.contains(path) + || any(lines.iter().map(String::as_str), path) + || localized(i).contains(path)) } } }) @@ -759,6 +815,24 @@ mod tests { assert_eq!(q("#craft"), Vec::::new()); } + #[test] + fn search_matches_the_display_name_in_the_selected_language() { + let c = creative(0); + let names = Names { + map: HashMap::from([ + ("item.minecraft.anvil".to_owned(), "铁砧".to_owned()), + ("item.minecraft.wooden_hoe".to_owned(), "木锄".to_owned()), + ]), + }; + let q = |s: &str| c.search_results(s, &names); + assert_eq!(q("铁砧"), [0]); + assert_eq!(q("木锄"), [2]); + // The English tooltip lines the export indexed still match. + assert_eq!(q("anvil"), [0]); + // A colon's path searches the display name too. + assert_eq!(q("minecraft:铁砧"), [0]); + } + #[test] fn scrollbar_drag_follows_the_mouse_while_held() { let mut c = creative(90); @@ -794,6 +868,20 @@ mod tests { assert!(!c.scrolling); } + #[test] + fn creative_tab_titles_have_vanilla_keys() { + assert_eq!( + item_group_key("minecraft:building_blocks"), + Some("itemGroup.buildingBlocks") + ); + assert_eq!( + item_group_key("minecraft:food_and_drinks"), + Some("itemGroup.foodAndDrink") + ); + assert_eq!(item_group_key("minecraft:hotbar"), Some("itemGroup.hotbar")); + assert_eq!(item_group_key("minecraft:nonsense"), None); + } + #[test] fn wheel_moves_one_row_per_notch() { let mut c = creative(90); diff --git a/crates/horizon-client/src/entity_nbt.rs b/crates/horizon-client/src/entity_nbt.rs index bf0764a6b..00576ea02 100644 --- a/crates/horizon-client/src/entity_nbt.rs +++ b/crates/horizon-client/src/entity_nbt.rs @@ -617,9 +617,23 @@ pub fn mob_from_nbt(tag: &Tag, types: &HashMap>, id: u32) - m.head_pitch = pitch; m.on_ground = flag(tag, "OnGround"); m.fall_distance = float(tag.get("fall_distance")).unwrap_or(0.0); - if let Some(h) = float(tag.get("Health")) { - m.health = h as f32; + // LivingEntity.readAdditionalSaveData: the saved attribute bases, then + // the health, the max health when there is none. + if let Some(Tag::List(attrs)) = tag.get("attributes") { + for a in attrs { + let (Some(id), Some(Tag::Double(bits))) = (text(a.get("id")), a.get("base")) else { + continue; + }; + let v = f64::from_bits(*bits); + match id.as_str() { + "minecraft:max_health" => m.attrs.max_health = v as f32, + "minecraft:movement_speed" => m.attrs.speed = v, + "minecraft:attack_damage" => m.attrs.attack_damage = v as f32, + _ => {} + } + } } + m.health = float(tag.get("Health")).map_or(m.attrs.max_health, |h| h as f32); m.hurt_time = number(tag.get("HurtTime")).unwrap_or(0) as i32; // What it holds (equipment.mainhand / offhand), when saved. if let Some(eq) = tag.get("equipment") { @@ -735,12 +749,8 @@ pub fn mob_from_nbt(tag: &Tag, types: &HashMap>, id: u32) - let (Some(id), Some(Tag::Double(bits))) = (text(a.get("id")), a.get("base")) else { continue; }; - let v = f64::from_bits(*bits); - match id.as_str() { - "minecraft:max_health" => m.attrs.max_health = v as f32, - "minecraft:movement_speed" => m.attrs.speed = v, - "minecraft:jump_strength" => m.extra.vector[0] = v, - _ => {} + if id == "minecraft:jump_strength" { + m.extra.vector[0] = f64::from_bits(*bits); } } } diff --git a/crates/horizon-client/src/font.rs b/crates/horizon-client/src/font.rs index 801a892d5..1903cab7e 100644 --- a/crates/horizon-client/src/font.rs +++ b/crates/horizon-client/src/font.rs @@ -891,8 +891,11 @@ impl Font { } s.chars().map(|c| self.glyph(c).advance).sum::().ceil() } + /// Font.width of a component: `§` codes in the spans are formatting, + /// not glyphs. pub fn width_text(&self, t: &Text) -> f32 { - t.chars() + t.resolved() + .chars() .map(|(c, s)| self.advance(c, s)) .sum::() .ceil() diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 31bfe74b6..595a4ce66 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -20,6 +20,7 @@ mod bossbars; mod breezes; mod cartography; mod chat; +mod client_ambient; mod clipboard; mod combat; mod command_blocks; @@ -541,6 +542,8 @@ struct GameLevel<'w> { seed: i64, behaviours: &'w Behaviours, items: &'w Items, + /// Cooking recipes' experience, which a removed furnace pays out. + recipes: &'w horizon_client::crafting::Recipes, /// Container loot tables (`unpackLootTable`). loot: Option>, registry: &'static horizon_world::registry::BlockRegistry, @@ -1172,6 +1175,8 @@ struct Game<'a> { carried: Option, craft_grid: Vec>, craft_size: usize, + /// The crafting table the 3x3 grid belongs to (`CraftingMenu.access`). + craft_table: Option<[i32; 3]>, /// A quick craft drag over inventory slots: (button, game slots so far). inv_drag: Option<(i32, Vec)>, /// `AbstractContainerScreen`'s quick craft and skip-release state. @@ -1572,6 +1577,8 @@ struct Game<'a> { cmds: commands::extra::CommandExtras, ui: chat::UiState, combat: combat::CombatTracker, + /// Each guest's own `CombatTracker`, out of `combat` between turns. + guest_combat: std::collections::HashMap, mount: mounts::MountState, stats: stats::Stats, score: scoreboard::ScoreState, @@ -1841,6 +1848,19 @@ impl Game<'_> { .is_some_and(|i| i.glider && (i.max_damage == 0 || s.damage + 1 < i.max_damage)) }) } + /// `LivingEntity.canFreeze`'s armor half: any worn piece in + /// `freeze_immune_wearables`. + pub(crate) fn wears_freeze_immune(&self) -> bool { + let immune = self + .res + .items() + .item_tags + .get("minecraft:freeze_immune_wearables"); + self.armor + .iter() + .flatten() + .any(|s| immune.is_some_and(|t| t.contains(&*s.item))) + } /// What the window decides for its player's body every tick, in frame and /// on a server alike: the look (Player.travel steers sprint-swimming by /// the pitch), Auto Jump (LocalPlayer.sendPosition), the slowdown of an @@ -1867,16 +1887,7 @@ impl Game<'_> { let leather_boots = self.armor[3] .as_ref() .is_some_and(|s| &*s.item == "minecraft:leather_boots"); - let immune = self - .res - .items() - .item_tags - .get("minecraft:freeze_immune_wearables"); - let freeze_immune = self - .armor - .iter() - .flatten() - .any(|s| immune.is_some_and(|t| t.contains(&*s.item))); + let freeze_immune = self.wears_freeze_immune(); let Some(player) = self.player.as_mut() else { return; }; @@ -2177,6 +2188,11 @@ impl Game<'_> { // Player.drop: the drop statistics. self.stats.add("dropped", &thrown.item, thrown.count as i32); self.stats.custom("drop", 1); + self.throw_from_eyes(thrown, eye); + } + /// `Player.createItemStackToDrop`: an item entity thrown from the eyes + /// along the view direction. + fn throw_from_eyes(&mut self, thrown: ItemStack, eye: [f64; 3]) { let (yaw, pitch) = ( f64::from(self.yaw).to_radians(), f64::from(self.pitch).to_radians(), @@ -2424,6 +2440,7 @@ impl Game<'_> { seed: self.level.seed, behaviours: self.res.behaviours(), items: self.res.items(), + recipes: self.res.recipes(), loot: horizon_client::structure_blocks::loot_data(&self.options.resources), registry: self.pack.assets.registry, rules, @@ -2920,6 +2937,7 @@ impl Game<'_> { } if &*name == "minecraft:crafting_table" { self.craft_size = 3; + self.craft_table = Some(pos); } else if &*name == "minecraft:ender_chest" { self.open_ender_chest(key); self.container_opener_event(key, true); @@ -4293,6 +4311,7 @@ impl Game<'_> { MotionEvent::Snowflake { position, velocity } => self.snowflake(position, velocity), MotionEvent::EnterBlock { state } => self.trigger_enter_block(state), MotionEvent::InsidePortal { pos } => self.player_inside_nether_portal(pos), + MotionEvent::InsideWitherRose => self.wither_rose_contact(), MotionEvent::SmashLanding { state, position, @@ -4339,7 +4358,8 @@ impl Game<'_> { // Out of the world. let _ = player.set_health(0.0, false); } - if player.health.dead_or_dying() && self.screen != Screen::Dead { + let dead = player.health.dead_or_dying(); + if dead && self.screen != Screen::Dead { // Inventory.dropAll: every stack thrown in a random direction. let feet = [player.position.x, player.position.y, player.position.z]; let at = [feet[0], feet[1] + 1.32, feet[2]]; @@ -4373,6 +4393,18 @@ impl Game<'_> { } self.screen = Screen::Dead; self.capture(false); + self.ui.death_time = 0; + } else if dead { + // LivingEntity.tickDeath: 20 ticks after dying the player is + // removed, and Player.remove's inventoryMenu.removed drops what + // the cursor held. + self.ui.death_time += 1; + if self.ui.death_time >= 20 + && let Some(stack) = self.carried.take() + && let Some(eye) = self.eye_position() + { + self.throw_from_eyes(stack, eye); + } } self.tick_world(running) } @@ -4451,6 +4483,7 @@ impl Game<'_> { self.tick_block_entity_anims() }); horizon_client::spans::time("tick_particles", || self.tick_particles()); + self.tick_client_ambient(); } // ServerBossEvent broadcasts, after this level's entities have ticked. self.publish_boss_bars(); @@ -5310,22 +5343,32 @@ impl Game<'_> { Some(BlockEntity::Chest(_)) if self.draw_machine_screen(batch, ox, oy) => {} Some(BlockEntity::Chest(_)) => self.draw_chest_screen(batch, ox, oy), Some(BlockEntity::Furnace(f)) => { - let (tex, title) = match f.kind { + let (tex, key, fallback) = match f.kind { horizon_client::containers::FurnaceKind::Furnace => { - ("gui/container/furnace", "Furnace") - } - horizon_client::containers::FurnaceKind::BlastFurnace => { - ("gui/container/blast_furnace", "Blast Furnace") + ("gui/container/furnace", "container.furnace", "Furnace") } + horizon_client::containers::FurnaceKind::BlastFurnace => ( + "gui/container/blast_furnace", + "container.blast_furnace", + "Blast Furnace", + ), horizon_client::containers::FurnaceKind::Smoker => { - ("gui/container/smoker", "Smoker") + ("gui/container/smoker", "container.smoker", "Smoker") } }; part(batch, tex, 0.0, 0.0, 176.0, 166.0, ox, oy); - let title = &self.container_title(title.to_owned()); - let tw = HudBatch::text_width(atlas, title); - batch.text(atlas, title, ox + (176.0 - tw) / 2.0, oy + 6.0, grey, false); - batch.text(atlas, "Inventory", ox + 8.0, oy + 72.0, grey, false); + let title = self.container_title(self.translate(key, fallback)); + let tw = HudBatch::text_width(atlas, &title); + batch.text( + atlas, + &title, + ox + (176.0 - tw) / 2.0, + oy + 6.0, + grey, + false, + ); + let inventory = self.translate("container.inventory", "Inventory"); + batch.text(atlas, &inventory, ox + 8.0, oy + 72.0, grey, false); if f.lit_remaining > 0 { // AbstractFurnaceScreen: flame height ceil(lit * 13) + 1. let total = if f.lit_total == 0 { 200 } else { f.lit_total }; @@ -5379,14 +5422,12 @@ impl Game<'_> { "gui/container/inventory" }; part(batch, texture, 0.0, 0.0, 176.0, 166.0, ox, oy); - let (title, tx) = if self.craft_size == 3 { - ("Crafting", 29.0) - } else { - ("Crafting", 97.0) - }; - batch.text(atlas, title, ox + tx, oy + 6.0, grey, false); + let title = self.translate("container.crafting", "Crafting"); + let tx = if self.craft_size == 3 { 29.0 } else { 97.0 }; + batch.text(atlas, &title, ox + tx, oy + 6.0, grey, false); if self.craft_size == 3 { - batch.text(atlas, "Inventory", ox + 8.0, oy + 72.0, grey, false); + let inventory = self.translate("container.inventory", "Inventory"); + batch.text(atlas, &inventory, ox + 8.0, oy + 72.0, grey, false); } } } @@ -5431,6 +5472,7 @@ impl Game<'_> { } } self.craft_size = 2; + self.craft_table = None; if let Some(key) = self.container.take() { self.container_opener_event(key, false); self.post_block_game_event( @@ -5942,6 +5984,12 @@ impl Game<'_> { // Attached after the slots were set: setting them is not a click. menu.merchant = session.map(|(s, _)| s); } + // `FurnaceResultSlot.checkTakeAchievements` pays out on what left the output slot. + let furnace_before = if matches!(kind, Kind::Furnace(_)) { + menu.slot(2).cloned().map_or(0, |s| s.count) + } else { + 0 + }; let outcome = run(&mut menu, &env, &to_menu); let made = maps.made.take(); match outcome { @@ -5959,6 +6007,12 @@ impl Game<'_> { self.merchant_sync_out(s); } let after: Vec> = (0..total).map(|i| menu.slot(i).cloned()).collect(); + // Only a furnace has a result slot to compare: an enchanting table has two slots. + let furnace_after = if matches!(kind, Kind::Furnace(_)) { + after[2].as_ref().map_or(0, |s| s.count) + } else { + 0 + }; let carried = menu.carried.take(); let dropped = std::mem::take(&mut menu.dropped); let sounds = std::mem::take(&mut menu.sounds); @@ -5983,6 +6037,13 @@ impl Game<'_> { self.player_drop(stack); } self.award_crafted(&crafted); + if furnace_before > furnace_after + && self.remote.is_none() + && let (Some(pos), Some(p)) = (self.container, self.player.as_ref()) + { + let at = [p.position.x, p.position.y, p.position.z]; + self.award_furnace_recipes(pos, at); + } } if kind == Kind::Loom { if let Some(s) = self.station.as_mut() { @@ -7885,8 +7946,12 @@ impl Game<'_> { (0..width).all(|i| frame([left[0] + d[0] * i, left[1] + height, left[2] + d[2] * i])); ((3..=21).contains(&height) && top_ok).then_some((left, width, height)) } - /// BaseFireBlock.onPlace: lighting fire inside a frame creates a portal. + /// BaseFireBlock.onPlace: lighting fire inside a frame creates a portal, + /// only in the Overworld and the Nether (`inPortalDimension`). fn try_light_portal(&mut self, at: [i32; 3]) -> Result { + if !matches!(self.dim, Dim::Overworld | Dim::Nether) { + return Ok(false); + } for x_axis in [true, false] { if let Some((origin, width, height)) = self.portal_shape(at, x_axis) { let d = if x_axis { [1, 0, 0] } else { [0, 0, 1] }; @@ -8774,6 +8839,27 @@ impl Game<'_> { self.give_orb_points(gained); } } + /// `FurnaceResultSlot.checkTakeAchievements`: the player took from the output of the + /// furnace at `pos`, which pops the experience of what it made at them + /// (`awardUsedRecipesAndPopExperience`) and forgets it. + pub(super) fn award_furnace_recipes(&mut self, pos: (i32, i32, i32), at: [f64; 3]) { + let used = match self.world.block_entities.get_mut(&pos) { + Some(BlockEntity::Furnace(f)) => { + let used = f.recipe_experience(self.res.recipes()); + f.recipes_used.clear(); + used + } + _ => return, + }; + for (count, experience) in used { + let xp = horizon_client::containers::create_experience( + count, + experience, + &mut self.world.random, + ); + self.spawn_orbs(at, xp); + } + } /// `Player.giveExperiencePoints`, and `giveExperienceLevels`' level-up /// sound every fifth level (at most every 100 ticks). pub(super) fn give_orb_points(&mut self, gained: u32) { @@ -9417,6 +9503,11 @@ impl Game<'_> { fn creative_origin(batch: &HudBatch) -> (f32, f32) { batch.centered_origin(Creative::WIDTH, Creative::HEIGHT) } + /// A creative tab's title: `itemGroup.*`, else the export's English title. + fn creative_tab_title(&self, tab: &horizon_client::creative::Tab) -> String { + horizon_client::creative::item_group_key(&tab.id) + .map_or_else(|| tab.title.clone(), |key| self.translate(key, &tab.title)) + } /// Vanilla tooltip: dark box with a purple gradient border. fn tooltip(&self, batch: &mut HudBatch, text: &str, x: f32, y: f32) { let atlas = &self.gui.atlas; @@ -9486,9 +9577,10 @@ impl Game<'_> { } let atlas = &self.gui.atlas; if !tab.inventory { + let title = self.creative_tab_title(&tab); batch.text( atlas, - &tab.title, + &title, ox + 8.0, oy + 6.0, rgba(64, 64, 64, 255), @@ -9599,7 +9691,12 @@ impl Game<'_> { && self.cursor.1 >= oy + ty && self.cursor.1 < oy + ty + 32.0 { - self.tooltip(batch, &t.title, self.cursor.0, self.cursor.1); + self.tooltip( + batch, + &self.creative_tab_title(t), + self.cursor.0, + self.cursor.1, + ); } } self.draw_carried(batch); @@ -11286,9 +11383,10 @@ impl ApplicationHandler for Game<'_> { // The hovered slot took the key (`ignoreTextInput`). } else if let Some(shortcut) = self.edit_shortcut(&event) { self.creative_search_shortcut(shortcut); + } else if event.physical_key == PhysicalKey::Code(KeyCode::Backspace) { + self.creative_search_backspace(self.has_control_down()); } else { - let backspace = event.physical_key == PhysicalKey::Code(KeyCode::Backspace); - self.creative_search_edit(event.text.as_deref(), backspace); + self.creative_search_edit(event.text.as_deref()); } } WindowEvent::KeyboardInput { event, .. } @@ -11723,7 +11821,11 @@ impl<'a> Game<'a> { let inventory = vec![None; 36]; let now = Instant::now(); let look = options.look; - let particles = horizon_client::particles::ParticleEngine::new(&root, &gui.atlas.sprites); + let particles = horizon_client::particles::ParticleEngine::new( + &root, + &gui.atlas.sprites, + &gui.atlas.translucent, + ); Ok(Game { res: res.clone(), rest_loader: None, @@ -11760,6 +11862,7 @@ impl<'a> Game<'a> { carried: None, craft_grid: vec![None; 9], craft_size: 2, + craft_table: None, inv_drag: None, tweaks: Default::default(), inv_clicks: Default::default(), @@ -12015,6 +12118,7 @@ impl<'a> Game<'a> { cmds: Default::default(), ui: Default::default(), combat: Default::default(), + guest_combat: Default::default(), mount: Default::default(), stats: Default::default(), score: Default::default(), @@ -12112,6 +12216,8 @@ enum PendingWorldEntry { Server { address: String, name: String, + /// A transfer's carried-over server cookies (`TransferState`). + cookies: Option, }, } @@ -12186,8 +12292,12 @@ impl Game<'_> { }) => { self.open_world(&folder, name.as_deref(), seed, creative, create.as_deref())?; } - Some(PendingWorldEntry::Server { address, name }) => { - self.connect_server(&address, &name)?; + Some(PendingWorldEntry::Server { + address, + name, + cookies, + }) => { + self.connect_server_with_cookies(&address, &name, cookies)?; } None => {} } diff --git a/crates/horizon-client/src/game/agent.rs b/crates/horizon-client/src/game/agent.rs index bed331c32..0d2d218ab 100644 --- a/crates/horizon-client/src/game/agent.rs +++ b/crates/horizon-client/src/game/agent.rs @@ -470,7 +470,8 @@ impl Game<'_> { "flying": p.flying, "fall_flying": p.fall_flying, "sneaking": self.keys.contains(&KeyCode::ShiftLeft), - "sprinting": self.keys.contains(&KeyCode::ControlLeft), + "sprinting": p.sprinting, + "fov_modifier": self.view.fov_modifier, "health": p.health.health(), "absorption": p.health.absorption(), // The hurt animation's ticks left (the camera's hurt tilt). @@ -1599,8 +1600,12 @@ impl Game<'_> { if self.screen != Screen::Dead { return Err("respawn needs the death screen".into()); } - let label = - horizon_client::text::Text::translate("deathScreen.respawn", &[]).plain(); + let key = if self.level.hardcore { + "deathScreen.spectate" + } else { + "deathScreen.respawn" + }; + let label = horizon_client::text::Text::translate(key, &[]).plain(); let w = widgets::find(&widgets::last(), &label)?; if !w.enabled { // DeathScreen enables its buttons after 20 ticks. diff --git a/crates/horizon-client/src/game/beacon.rs b/crates/horizon-client/src/game/beacon.rs index c8a0bcba6..24ead3a3d 100644 --- a/crates/horizon-client/src/game/beacon.rs +++ b/crates/horizon-client/src/game/beacon.rs @@ -519,7 +519,12 @@ impl Game<'_> { self.draw_tooltip_lines(batch, &lines, self.cursor.0, self.cursor.1); } else if let Some(key) = tooltip { let text = self.translate(&key, &key); - self.draw_tooltip_lines(batch, &[(text, u32::MAX)], self.cursor.0, self.cursor.1); + self.draw_tooltip_lines( + batch, + &[(horizon_client::text::Text::literal(text), u32::MAX)], + self.cursor.0, + self.cursor.1, + ); } } /// `BeaconRenderer`: each section's rotating solid beam and its glow. diff --git a/crates/horizon-client/src/game/block_entity_ticks.rs b/crates/horizon-client/src/game/block_entity_ticks.rs index 094aed6ef..e48c8f398 100644 --- a/crates/horizon-client/src/game/block_entity_ticks.rs +++ b/crates/horizon-client/src/game/block_entity_ticks.rs @@ -15,6 +15,8 @@ use horizon_world::registry::BlockStateId; impl Game<'_> { pub(super) fn tick_block_entities(&mut self) -> Result<()> { self.tick_mount_menu(); + self.tick_craft_table(); + self.tick_container_reach(); self.load_entity_types(); if !self.world.block_entities.has_states() { let states = self.ticking_states(); diff --git a/crates/horizon-client/src/game/boat_passengers.rs b/crates/horizon-client/src/game/boat_passengers.rs index 96686f69b..7055cccfd 100644 --- a/crates/horizon-client/src/game/boat_passengers.rs +++ b/crates/horizon-client/src/game/boat_passengers.rs @@ -13,7 +13,7 @@ use horizon_client::{ use horizon_core::geometry::{Aabb, Vec3}; /// `Entity.boardingCooldown` once a mob leaves its boat. -const BOARDING_COOLDOWN: i32 = 60; +pub(super) const BOARDING_COOLDOWN: i32 = 60; /// `Player.DEFAULT_VEHICLE_ATTACHMENT`: how far below the seat a player's /// feet are. const PLAYER_RIDING_OFFSET: f64 = 0.6; diff --git a/crates/horizon-client/src/game/chat.rs b/crates/horizon-client/src/game/chat.rs index ec85732ae..12d6e72f1 100644 --- a/crates/horizon-client/src/game/chat.rs +++ b/crates/horizon-client/src/game/chat.rs @@ -18,7 +18,7 @@ fn chat_tree<'a>( mods: &'a super::mods::ModsState, ) -> &'a horizon_client::command::Dispatcher { match remote.as_ref().and_then(|r| r.commands.as_ref()) { - Some(tree) => tree, + Some(tree) => mods.remote_dispatcher().unwrap_or(tree), None => mods.dispatcher(), } } @@ -99,6 +99,8 @@ pub struct UiState { /// Death screen cause and when the player died (buttons wait 20 ticks). pub death_text: Option, pub died_at: Option, + /// `LivingEntity.deathTime` of the window's player: ticks since it died. + pub death_time: u32, } impl Game<'_> { @@ -586,7 +588,7 @@ impl Game<'_> { } else { let line = Text::translate( "chat.type.text", - &[Text::literal(PLAYER_NAME), Text::literal(message)], + &[self.player_display_name(), Text::literal(message)], ); let focused = self.screen == Screen::Chat; let heard = self.mods.running().then(|| line.plain()); diff --git a/crates/horizon-client/src/game/client_ambient.rs b/crates/horizon-client/src/game/client_ambient.rs new file mode 100644 index 000000000..a10c6da49 --- /dev/null +++ b/crates/horizon-client/src/game/client_ambient.rs @@ -0,0 +1,37 @@ +//! The window's own ambient effects of entities, which a client makes for +//! itself (`level().isClientSide()` branches): endermen's and endermites' +//! portal particles, blazes' smoke and burn, eyes of ender's trail. They run +//! on the game that has the window, in-frame or following a server, never on +//! a server: nothing of them goes over the network. +use super::Game; +use horizon_client::mobs::{MobEvent, ambient}; +use horizon_client::particles::{ParticleOptions, ParticleType}; + +impl Game<'_> { + pub(super) fn tick_client_ambient(&mut self) { + if self.serving.is_some() || self.options.dedicated.is_some() { + return; + } + let mut events = Vec::new(); + for m in self.mobs.iter_mut().filter(|m| !m.removed) { + ambient::client_ai_step(m, &mut events); + } + for event in events { + match event { + MobEvent::ParticleAt { kind, at, velocity } => { + if let Some(ty) = ParticleType::from_name(kind) { + self.particles + .add(&ParticleOptions::simple(ty), at, velocity); + } + } + // Each guest of a LAN host makes its own. + MobEvent::LocalSound(name, at, volume, pitch) => { + self.audio + .play_unshared(&format!("minecraft:{name}"), Some(at), volume, pitch) + } + _ => {} + } + } + self.eye_particles(); + } +} diff --git a/crates/horizon-client/src/game/clipboard.rs b/crates/horizon-client/src/game/clipboard.rs index b9558ddfd..c87aa3681 100644 --- a/crates/horizon-client/src/game/clipboard.rs +++ b/crates/horizon-client/src/game/clipboard.rs @@ -97,15 +97,25 @@ impl Game<'_> { self.creative_screen.scroll = 0.0; } } - /// Typing or a backspace in the creative search box. - pub(super) fn creative_search_edit(&mut self, typed: Option<&str>, backspace: bool) { + /// A backspace in the creative search box: with Ctrl, the whole word + /// before the caret (`EditBox.deleteWords(-1)`), otherwise one character. + /// A Select All selection is what it deletes, as in `EditBox.keyPressed`. + pub(super) fn creative_search_backspace(&mut self, ctrl: bool) { let search = &mut self.creative_screen.search; - let cleared = self.text_selection.take(CREATIVE_SEARCH, search); - if backspace { - if !cleared { + if !self.text_selection.take(CREATIVE_SEARCH, search) { + if ctrl { + horizon_client::ui::delete_words(search, -1); + } else { search.pop(); } - } else if let Some(text) = typed { + } + self.creative_screen.scroll = 0.0; + } + /// Typing in the creative search box. + pub(super) fn creative_search_edit(&mut self, typed: Option<&str>) { + let search = &mut self.creative_screen.search; + self.text_selection.take(CREATIVE_SEARCH, search); + if let Some(text) = typed { clipboard::append_limited(search, text, CREATIVE_SEARCH_CHARS); } self.creative_screen.scroll = 0.0; diff --git a/crates/horizon-client/src/game/combat.rs b/crates/horizon-client/src/game/combat.rs index 70ae082aa..227aa0332 100644 --- a/crates/horizon-client/src/game/combat.rs +++ b/crates/horizon-client/src/game/combat.rs @@ -191,8 +191,12 @@ impl Game<'_> { } /// `CombatTracker.getDeathMessage`. pub(super) fn combat_death_message(&mut self) -> Text { + self.combat_death_message_for(Text::literal(PLAYER_NAME)) + } + /// `CombatTracker.getDeathMessage` for a player whose display name is + /// `me`: a server's guest is not `PLAYER_NAME`. + pub(super) fn combat_death_message_for(&mut self, me: Text) -> Text { self.recheck_combat(); - let me = Text::literal(PLAYER_NAME); let entries = std::mem::take(&mut self.combat.entries); self.combat.in_combat = false; let Some(killing) = entries.last() else { diff --git a/crates/horizon-client/src/game/commands.rs b/crates/horizon-client/src/game/commands.rs index 19a16e261..69c06d97e 100644 --- a/crates/horizon-client/src/game/commands.rs +++ b/crates/horizon-client/src/game/commands.rs @@ -214,7 +214,7 @@ impl Game<'_> { } /// Player.getDisplayName: the name (team-formatted), suggesting a /// `/tell`, showing the player on hover, inserting the name. - fn player_display_name(&self) -> Text { + pub(super) fn player_display_name(&self) -> Text { let name = self.acting_player_name().to_owned(); let Some(uuid) = self.lan_acting_uuid else { return self.target_name(Target::Player); @@ -444,7 +444,7 @@ impl Game<'_> { .find(|i| i.id == id) .map(|i| i.stack.item.to_string()) .unwrap_or_default(); - Text::literal(self.pack.names.item(&item)) + Text::literal(self.pack.names.display_item(&item)) } Target::Mob(id) if self @@ -2809,7 +2809,7 @@ impl Game<'_> { let name = match self.command_source.map(|s| s.entity) { Some(Some(t)) => self.target_name(t), Some(None) => Text::literal("Server"), - None => Text::literal(self.acting_player_name()), + None => self.player_display_name(), }; let line = match id { "say" => Text::translate("chat.type.announcement", &[name, message]), @@ -2819,14 +2819,14 @@ impl Game<'_> { // Whispering to yourself: the incoming and outgoing lines. let out = Text::translate( "commands.message.display.outgoing", - &[Text::literal(self.acting_player_name()), message.clone()], + &[self.player_display_name(), message.clone()], ) .with_color(text::GRAY) .italic(); self.system_message(out); Text::translate( "commands.message.display.incoming", - &[Text::literal(self.acting_player_name()), message], + &[self.player_display_name(), message], ) .with_color(text::GRAY) .italic() diff --git a/crates/horizon-client/src/game/commands/execute.rs b/crates/horizon-client/src/game/commands/execute.rs index 463cd542e..3f7e4c2a0 100644 --- a/crates/horizon-client/src/game/commands/execute.rs +++ b/crates/horizon-client/src/game/commands/execute.rs @@ -606,10 +606,7 @@ impl Game<'_> { .collect(), _ => Vec::new(), }, - Some("vehicle") => match entity { - Target::Player => self.riding.map(Target::Mob).into_iter().collect(), - _ => Vec::new(), - }, + Some("vehicle") => self.ride_vehicle(entity).into_iter().collect(), Some("controller") | Some("passengers") => match entity { Target::Mob(id) if self.mobs.iter().any(|m| m.id == id && m.rider.is_some()) => { vec![Target::Player] diff --git a/crates/horizon-client/src/game/commands/world_cmds.rs b/crates/horizon-client/src/game/commands/world_cmds.rs index 2f3792a57..30f019997 100644 --- a/crates/horizon-client/src/game/commands/world_cmds.rs +++ b/crates/horizon-client/src/game/commands/world_cmds.rs @@ -1074,15 +1074,15 @@ impl Game<'_> { }; let name = self.target_name(target); if id == "ride.dismount" { - let vehicle = match target { - Target::Player => self.riding.map(Target::Mob), - _ => None, - }; - let Some(v) = vehicle else { + let Some(v) = self.ride_vehicle(target) else { return Err(err("commands.ride.not_riding", &[&name.plain()])); }; let vehicle_name = self.target_name(v); - self.dismount_mount(); + match target { + Target::Player => self.dismount_mount(), + Target::Mob(rider) => self.dismount_mob(rider), + _ => {} + } self.feedback(Text::translate( "commands.ride.dismount.success", &[name, vehicle_name], @@ -1094,11 +1094,8 @@ impl Game<'_> { return Ok(0); }; let vehicle_name = self.target_name(vehicle); - if target == Target::Player && self.riding.is_some() { - let current = self - .riding - .map(Target::Mob) - .map_or_else(Text::empty, |v| self.target_name(v)); + if let Some(current) = self.ride_vehicle(target) { + let current = self.target_name(current); return Err(err( "commands.ride.already_riding", &[&name.plain(), ¤t.plain()], @@ -1107,22 +1104,42 @@ impl Game<'_> { if vehicle == Target::Player { return Err(err("commands.ride.mount.failure.cant_ride_players", &[])); } - if target == vehicle { - return Err(err("commands.ride.mount.failure.loop", &[])); + // getSelfAndPassengers: the vehicle is the target or rides it. + let mut up = Some(vehicle); + while let Some(v) = up { + if v == target { + return Err(err("commands.ride.mount.failure.loop", &[])); + } + up = self.ride_vehicle(v); } - // The player can ride mobs that carry a rider. + // Players and mobs can ride mobs. let Target::Mob(mob_id) = vehicle else { return Err(err( "commands.ride.mount.failure.generic", &[&name.plain(), &vehicle_name.plain()], )); }; - if target != Target::Player || !self.mobs.iter().any(|m| m.id == mob_id && m.alive()) { + let rider = match target { + Target::Mob(id) => Some(id), + _ => None, + }; + if !(target == Target::Player || rider.is_some()) + || !self.mobs.iter().any(|m| m.id == mob_id && m.alive()) + { return Err(err( "commands.ride.mount.failure.generic", &[&name.plain(), &vehicle_name.plain()], )); } + if let Some(rider) = rider { + // Entity.startRiding. + self.mount_mob(rider, mob_id); + self.feedback(Text::translate( + "commands.ride.mount.success", + &[name, vehicle_name], + )); + return Ok(1); + } if let Some(m) = self.mobs.iter_mut().find(|m| m.id == mob_id) { m.extra.flag = false; m.extra.counter = 0; @@ -1138,6 +1155,47 @@ impl Game<'_> { Ok(1) } + /// Entity.getVehicle: the player's mount, or a mob's. + pub(super) fn ride_vehicle(&self, t: Target) -> Option { + let id = match t { + Target::Player => return self.riding.map(Target::Mob), + Target::Mob(id) => self.mobs.iter().find(|m| m.id == id)?.extra.mount?, + _ => return None, + }; + Some(if self.boats.iter().any(|b| b.id == id) { + Target::Boat(id) + } else { + Target::Mob(id) + }) + } + /// LivingEntity.stopRiding for a mob: off its vehicle + /// (Entity.removePassenger), then to where the vehicle lets it out + /// (LivingEntity.dismountVehicle). + fn dismount_mob(&mut self, rider: u32) { + let Some(i) = self.mobs.iter().position(|m| m.id == rider) else { + return; + }; + let Some(v) = self.mobs[i].extra.mount.take() else { + return; + }; + let (width, yaw) = (f64::from(self.mobs[i].width()), self.mobs[i].yaw); + let at = if let Some(b) = self.boats.iter().position(|b| b.id == v) { + self.boat_dismount_target(b, width, yaw) + } else if let Some(m) = self.mobs.iter_mut().find(|m| m.id == v) { + m.extra.rider = None; + // Entity.getDismountLocationForPassenger: on top of the vehicle. + let p = m.position; + horizon_core::geometry::Vec3::new(p.x, p.y + f64::from(m.height()), p.z) + } else { + self.mobs[i].position + }; + let m = &mut self.mobs[i]; + m.extra.passenger = false; + m.extra.boarding_cooldown = super::super::boat_passengers::BOARDING_COOLDOWN; + m.position = at; + m.previous = at; + } + // ---- /spreadplayers ----------------------------------------------------------------------- pub(super) fn cmd_spreadplayers(&mut self, ctx: &Context) -> R { diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index ffe05b518..2e18c2df7 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -569,7 +569,7 @@ impl Game<'_> { } Screen::Creative if self.creative_screen.tab().search => { // As typed: replaces a selected query, keeps the length limit. - self.creative_search_edit(Some(text), false) + self.creative_search_edit(Some(text)) } _ => { return Err( diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 0bb7bbe85..f027a04fc 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -321,7 +321,7 @@ impl Game<'_> { .iter() .filter(|m| m.distance_sqr(center) <= radius * radius) .map(|m| { - serde_json::json!({ + let mut state = serde_json::json!({ "id": m.id, "kind": m.kind.id, "pos": [m.position.x, m.position.y, m.position.z], @@ -370,7 +370,11 @@ impl Game<'_> { "patrol_target": s.patrol_target, "vehicle": m.extra.mount, })), - }) + }); + // Drawn seated (Entity.isPassenger); set apart because the + // json! above is at the macro recursion limit. + state["passenger"] = m.extra.passenger.into(); + state }) .collect(); serde_json::json!({ "count": self.mobs.len(), "near": list, "projectiles": self.creatures.projectiles.len() }) @@ -1558,6 +1562,16 @@ impl Game<'_> { id, ticks, amplifier, )); } + /// `WitherRoseBlock.entityInside` for the player: on a server level that + /// isn't peaceful, `getBeeInteractionEffect` (Wither, 40 ticks) unless the + /// player is invulnerable to wither damage (creative, spectator). A + /// server's player gets it from the server. + pub(super) fn wither_rose_contact(&mut self) { + if self.remote.is_some() || self.ui.difficulty == 0 { + return; + } + self.give_player_effect("minecraft:wither", 40, 0); + } /// Burning applied to the player by mobs. fn tick_player_effects(&mut self) { self.creatures.wind_cooldown = self.creatures.wind_cooldown.saturating_sub(1); @@ -1694,7 +1708,26 @@ impl Game<'_> { targets: targets_guests, }; let guest_entities = self.lan_guest_entities(); + // ThrownEnderpearl.tick: a dead player's pearl is discarded in + // place of its tick while ender_pearls_vanish_on_death is on. + let vanish = self.ui.rules.bool("ender_pearls_vanish_on_death"); + let host_dead = self.player_view().is_some_and(|p| !p.alive); + let dead_guests: Vec = self.lan.as_ref().map_or_else(Vec::new, |h| { + h.hub + .guests + .iter() + .filter(|g| g.dead) + .map(|g| g.conn) + .collect() + }); projectiles.retain_mut(|p| { + if vanish + && matches!(p.kind, ProjectileKind::EnderPearl) + && p.owner == Some(Who::Player) + && p.guest.map_or(host_dead, |c| dead_guests.contains(&c)) + { + return false; + } let mob_shot = !matches!(p.owner, Some(Who::Player)); // A player's arrows, snowballs and eggs meet every player; a // guest's flies from that guest (Projectile.getOwner), not diff --git a/crates/horizon-client/src/game/dragon.rs b/crates/horizon-client/src/game/dragon.rs index 54b260c67..ddd5a716b 100644 --- a/crates/horizon-client/src/game/dragon.rs +++ b/crates/horizon-client/src/game/dragon.rs @@ -336,6 +336,12 @@ enum StrafeRequest { /// Anything else: the player nearest the crystal. Nearest(Vec3), } +/// Whether `EndCrystal.hurtServer` creates a blast before notifying the fight. +#[derive(Clone, Copy, PartialEq)] +enum CrystalDamage { + Direct, + Explosion, +} /// What `EnderDragon.hurt` made of a hit. #[derive(Clone, Copy, Debug, PartialEq)] pub(super) enum Hurt { @@ -867,27 +873,29 @@ impl Game<'_> { } } } - /// A crystal took damage: `EndCrystal.hurtServer` blows it up (power 6). - fn destroy_crystal(&mut self, index: usize, by: Option) { + /// `EndCrystal.hurtServer`: remove it, blast only for non-explosion damage, + /// then notify the dragon fight even when no blast was created. + fn destroy_crystal(&mut self, index: usize, by: Option, damage: CrystalDamage) { if index >= self.crystals.len() || self.crystals[index].invulnerable { return; } let c = self.crystals.remove(index); - if self.fight.respawn.is_some() + // Read before the blast, which can take other respawn crystals. + let respawn_crystal = self.fight.respawn.is_some() && self .fight .respawn_crystals .iter() - .any(|p| p.distance_squared(c.position) < 0.01) - { + .any(|p| p.distance_squared(c.position) < 0.01); + if damage == CrystalDamage::Direct { let at = [c.position.x, c.position.y, c.position.z]; let _ = self.server_explode(at, 6.0, super::explosion::BlockInteraction::Destroy, false); + } + if respawn_crystal { self.abort_respawn(); return; } - let at = [c.position.x, c.position.y, c.position.z]; - let _ = self.server_explode(at, 6.0, super::explosion::BlockInteraction::Destroy, false); // EnderDragonFight.onCrystalDestroyed. self.count_crystals(); if let Some(d) = self.dragon.as_mut() { @@ -962,7 +970,7 @@ impl Game<'_> { return Hurt::Refused; } let by = by_player.then(|| self.acting_player()); - self.destroy_crystal(index, by); + self.destroy_crystal(index, by, CrystalDamage::Direct); Hurt::Landed { full: true } } else if id >= SPECIAL { self.hurt_dragon((id - SPECIAL) as usize, amount, by_player, arrow) @@ -2634,8 +2642,8 @@ impl Crystal { } } impl Game<'_> { - /// `ServerExplosion.hurtEntities` on the end crystals (each in reach - /// blows up in turn) and the dragon (explosions always hurt dragons). + /// `ServerExplosion.hurtEntities`: crystals in reach are removed without + /// another blast; explosions always hurt dragons. pub(super) fn explosion_hits_special(&mut self, center: [f64; 3], radius: f32) { let diameter = f64::from(radius * 2.0); let in_reach = |p: Vec3| { @@ -2651,9 +2659,9 @@ impl Game<'_> { .map(|c| c.position) .collect(); for at in crystals { - // One blowing up may already have taken the next. + // Look up the current index after each removal. if let Some(i) = self.crystals.iter().position(|c| c.position == at) { - self.destroy_crystal(i, None); + self.destroy_crystal(i, None, CrystalDamage::Explosion); } } let Some(d) = self.dragon.as_ref() else { diff --git a/crates/horizon-client/src/game/end.rs b/crates/horizon-client/src/game/end.rs index 599158e3e..655971a5c 100644 --- a/crates/horizon-client/src/game/end.rs +++ b/crates/horizon-client/src/game/end.rs @@ -4,7 +4,12 @@ //! inactive until the dragon dies. use super::{Dim, Game}; use horizon_client::{ - Result, environment::Environment, items::ItemStack, mesher::Vertex, motion::PlayerMotion, + Result, + environment::Environment, + items::ItemStack, + mesher::Vertex, + motion::PlayerMotion, + particles::{ParticleOptions, ParticleType}, vanillagen::VanillaTerrain, }; use horizon_core::{ @@ -1122,6 +1127,35 @@ impl Game<'_> { self.end.eyes = eyes; } + /// `EyeOfEnder.tick` on a client: behind each eye (a quarter of its last + /// move back), four bubbles in water, else a portal particle. + pub(super) fn eye_particles(&mut self) { + for i in 0..self.end.eyes.len() { + let e = &self.end.eyes[i]; + let movement = (e.position - e.previous).to_array(); + let origin = (e.position - (e.position - e.previous) * 0.25).to_array(); + let block = origin.map(|v| v.floor() as i32); + let water = self + .res + .facts() + .get(self.world.block(block[0], block[1], block[2])) + .fluid + == horizon_client::blocks::Fluid::Water; + if water { + let bubble = ParticleOptions::simple(ParticleType::Bubble); + for _ in 0..4 { + self.particles.add(&bubble, origin, movement); + } + } else { + let r = self.particles.random(); + let x = origin[0] + r.next_double() * 0.6 - 0.3; + let z = origin[2] + r.next_double() * 0.6 - 0.3; + let portal = ParticleOptions::simple(ParticleType::Portal); + self.particles + .add(&portal, [x, origin[1] - 0.5, z], movement); + } + } + } /// ThrownItemRenderer: the ender eye item facing the camera. fn eye_geometry(&self, camera: glam::DVec3, out: &mut Vec) { let Some(r) = self.gui.atlas.sprites.get("item/ender_eye").copied() else { diff --git a/crates/horizon-client/src/game/fireworks.rs b/crates/horizon-client/src/game/fireworks.rs index 85fccc2de..7838c9590 100644 --- a/crates/horizon-client/src/game/fireworks.rs +++ b/crates/horizon-client/src/game/fireworks.rs @@ -25,9 +25,15 @@ pub(super) struct Rocket { pub(super) velocity: Vec3, life: i32, lifetime: i32, - explosions: Vec, + pub(super) explosions: Vec, /// Boosting the player (attachedToEntity). pub(super) attached: bool, + /// On a server, the player it boosts when not this game's own: a guest's + /// connection. + pub(super) guest: Option, + /// `DATA_ATTACHED_TO_TARGET`: the server entity the rocket follows; + /// boosting happens when it is the local player. + pub(super) attached_to: Option, /// `DATA_SHOT_AT_ANGLE` (dispensers): constant velocity, exploding on /// the first block it hits. pub(super) angled: bool, @@ -48,6 +54,8 @@ impl Rocket { lifetime: i32::MAX, explosions, attached: false, + guest: None, + attached_to: None, angled: false, item, } @@ -63,7 +71,7 @@ impl Rocket { (self.position, self.velocity) } } -fn fireworks_of(stack: &ItemStack) -> (u8, Vec) { +pub(super) fn fireworks_of(stack: &ItemStack) -> (u8, Vec) { // The item's default component: flight 1, no explosions. stack .data() @@ -89,6 +97,8 @@ impl Game<'_> { lifetime, explosions, attached, + guest: None, + attached_to: None, angled: false, item: stack.clone(), } @@ -127,7 +137,8 @@ impl Game<'_> { return false; } let at = self.player.as_ref().map_or(Vec3::ZERO, |p| p.position); - let rocket = self.new_rocket(at, stack, true); + let mut rocket = self.new_rocket(at, stack, true); + rocket.guest = self.lan_acting; self.rockets.push(rocket); if !self.creative { self.consume_held(); @@ -140,7 +151,18 @@ impl Game<'_> { while i < self.rockets.len() { let mut r = self.rockets.remove(i); r.previous = r.position; - if r.attached { + if r.attached + && let Some(conn) = r.guest + { + // A guest's rocket follows them; their own client boosts them. + let guest = self + .lan + .as_ref() + .and_then(|host| host.hub.guests.iter().find(|g| g.conn == conn)); + if let Some(g) = guest { + r.position = Vec3::new(g.position[0], g.position[1], g.position[2]); + } + } else if r.attached { if let Some(p) = self.player.as_mut() { if p.fall_flying { let look = view_vector(self.yaw, self.pitch); @@ -229,8 +251,19 @@ impl Game<'_> { let at = [r.position.x, r.position.y, r.position.z]; if !r.explosions.is_empty() { let damage = 5.0 + r.explosions.len() as f32 * 2.0; + // The player it boosts takes the full damage, a guest as well. if r.attached { - self.hurt_player_by("minecraft:fireworks", damage); + match r.guest { + None => self.hurt_player_by("minecraft:fireworks", damage), + Some(conn) => { + if let Some(mut host) = self.lan.take() { + self.as_guest(&mut host, conn, |g| { + g.hurt_player_by("minecraft:fireworks", damage); + }); + self.lan = Some(host); + } + } + } } let from = r.position; let hits: Vec<(u32, f32)> = { diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index 8460f54e6..9efe63baa 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -55,8 +55,8 @@ pub(super) struct LanHost { pub play: HashMap, /// Blocks broken this tick (their sound is a level event for the guests). pub broken: Vec<[f64; 3]>, - /// Hurt and death state of the mobs guests see. - mob_state: HashMap>, + /// Hurt time, death and swing time (while swinging) of the mobs guests see. + mob_state: HashMap)>>, /// The guest each mob sees as its player, when not the host (game/lan_mobs.rs). pub mob_players: HashMap, /// An integrated server's owner (game/integrated.rs). @@ -162,17 +162,28 @@ fn mob_meta( meta.push((3, serializer::BOOLEAN, Value::Bool(true))); } } + // LivingEntity.DATA_LIVING_ENTITY_FLAGS: using an item 1, in the off + // hand 2 (a skeleton draws its bow, a witch raises her potion). + let using = i8::from(m.extra.using_item > 0) | i8::from(m.extra.using_off_hand) << 1; + if using != 0 { + meta.push((8, serializer::BYTE, Value::Byte(using))); + } + defaults.push((8, serializer::BYTE, Value::Byte(0))); // LivingEntity.DATA_HEALTH_ID: defined as 1, then set to the max health. let health = m.health.max(0.0); if health != 1.0 { meta.push((9, serializer::FLOAT, Value::Float(health))); } defaults.push((9, serializer::FLOAT, Value::Float(1.0))); - // Mob.DATA_MOB_FLAGS_ID: no AI 1, left-handed 2. - let flags = i8::from(m.no_ai) | i8::from(m.extra.left_handed) << 1; + // Mob.DATA_MOB_FLAGS_ID: no AI 1, left-handed 2, aggressive 4 (what + // draws a vindicator's axe, a skeleton's drawn bow, a piglin's raised + // weapon). + let flags = + i8::from(m.no_ai) | i8::from(m.extra.left_handed) << 1 | i8::from(m.ai.aggressive) << 2; if flags != 0 { meta.push((15, serializer::BYTE, Value::Byte(flags))); } + defaults.push((15, serializer::BYTE, Value::Byte(0))); // AgeableMob.DATA_BABY_ID (and Zombie's, Piglin's). if let Some(index) = baby_index(kind) { if m.is_baby() { @@ -289,6 +300,21 @@ fn mob_meta( meta.push((18, serializer::BOOLEAN, Value::Bool(true))); } } + // Pillager.IS_CHARGING_CROSSBOW: the charge pose and the held + // item's loading animation. + "minecraft:pillager" => { + if e.flag { + meta.push((17, serializer::BOOLEAN, Value::Bool(true))); + } + defaults.push((17, serializer::BOOLEAN, Value::Bool(false))); + } + // SpellcasterIllager.DATA_SPELL_CASTING_ID: the casting pose. + "minecraft:evoker" | "minecraft:illusioner" => { + if e.variant2 != 0 { + meta.push((17, serializer::BYTE, Value::Byte(e.variant2 as i8))); + } + defaults.push((17, serializer::BYTE, Value::Byte(0))); + } // Enderman.DATA_CARRY_STATE. "minecraft:enderman" => { if let Some(state) = e.carried { @@ -979,6 +1005,7 @@ impl Game<'_> { self.save_guest(&host, &guest); host.models.remove(&guest.conn); host.play.remove(&guest.conn); + self.guest_combat.remove(&guest.conn); if self.options.dedicated.is_some() && guest.placed { super::dedicated::log( "INFO", @@ -1193,7 +1220,17 @@ impl Game<'_> { if dead && !old.is_some_and(|o| o.1) { host.hub.entity_event(m.id as i32, 3, at); } - seen.insert(m.id, (m.hurt_time, dead)); + // LivingEntity.swing on a server (a melee hit, a thrown potion): + // ClientboundSwingAnimationPacket for the players around. A swing + // started since the last flush, or restarted (its time went back). + let swing = m.ai.swinging.then_some(m.ai.swing_time); + if let Some(time) = swing + && !dead + && old.and_then(|o| o.2).is_none_or(|before| time < before) + { + host.hub.swing(m.id as i32, 0, at, None); + } + seen.insert(m.id, (m.hurt_time, dead, swing)); } host.mob_state.insert(self.dim, seen); } diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 2c7019c73..9d4d868e0 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -117,6 +117,7 @@ pub(super) struct GuestSwap { host_command_block: Option, host_mount_menu: super::mount_menu::MountMenu, craft_size: usize, + craft_table: Option<[i32; 3]>, craft_grid: Vec>, container: Option<(i32, i32, i32)>, mining: Option, @@ -143,6 +144,8 @@ pub(super) struct GuestSwap { adv_loaded: bool, last_inventory: u64, death_cause: Option, + /// The guest's own damage record, in place while they act. + combat: super::combat::CombatTracker, /// The guest whose turn this one nests in, if any. acting: (Option, Option, Option), /// The packets queued for that guest. @@ -256,6 +259,14 @@ impl Game<'_> { host.play.insert(conn, play); return None; }; + // The guest's own CombatTracker: damage to them is not the host's. + // A new body starts with no record of the last one. + let guest_combat = if fresh { + Default::default() + } else { + self.guest_combat.remove(&conn).unwrap_or_default() + }; + let host_combat = std::mem::replace(&mut self.combat, guest_combat); self.potions.swap_player(&mut g.effects, &mut play.potions); motion.position = Vec3::new(g.position[0], g.position[1], g.position[2]); motion.ground.on_ground = g.on_ground; @@ -264,6 +275,8 @@ impl Game<'_> { motion.sprinting = g.sprinting; motion.may_fly = g.creative() || g.game_mode == SPECTATOR; motion.flying = g.flying; + // FireworkRocketItem.use boosts only a gliding player. + motion.fall_flying = g.fall_flying; motion.invulnerable = motion.may_fly; let _ = motion.set_health(g.health, false); motion.health.food.food = g.food; @@ -307,6 +320,7 @@ impl Game<'_> { // The host's crafting grid stays theirs; a crafting table the guest // opens shows as the 3x3 size. let craft_size = std::mem::replace(&mut self.craft_size, 2); + let craft_table = self.craft_table.take(); let craft_grid = std::mem::replace(&mut self.craft_grid, vec![None; 9]); let container = self.container.take(); let mining = std::mem::replace(&mut self.mining, play.mining.take()); @@ -390,6 +404,7 @@ impl Game<'_> { host_command_block, host_mount_menu, craft_size, + craft_table, craft_grid, container, mining, @@ -418,6 +433,7 @@ impl Game<'_> { swings, projectiles, modifiers, + combat: host_combat, }) } @@ -445,6 +461,7 @@ impl Game<'_> { host_command_block, host_mount_menu, craft_size, + craft_table, craft_grid, container, mining, @@ -468,12 +485,16 @@ impl Game<'_> { adv_loaded, last_inventory, death_cause, + combat, acting, packets, swings, projectiles, modifiers, } = swap; + // The guest's damage record back into their slot, the host's in place. + let guest_combat = std::mem::replace(&mut self.combat, combat); + self.guest_combat.insert(conn, guest_combat); play.modifiers = std::mem::replace(&mut self.applied_modifiers, modifiers); // What the guest shot is theirs (Projectile.setOwner). for p in self.creatures.projectiles.iter_mut().skip(projectiles) { @@ -495,7 +516,10 @@ impl Game<'_> { // What the action opened: handed to the guest instead of the host. let opened = self.container.take(); - let opened_table = std::mem::replace(&mut self.craft_size, craft_size) == 3; + let guest_table = std::mem::replace(&mut self.craft_table, craft_table); + let opened_table = (std::mem::replace(&mut self.craft_size, craft_size) == 3) + .then_some(guest_table) + .flatten(); self.craft_grid = craft_grid; let opened_station = std::mem::replace(&mut self.station, host_station); let bed = std::mem::replace(&mut self.sleeping, sleeping); @@ -671,8 +695,8 @@ impl Game<'_> { if let Some(key) = guest_lectern { self.lan_open_lectern(host, conn, key); } - if opened_table { - self.lan_open_crafting(host, conn); + if let Some(pos) = opened_table { + self.lan_open_crafting(host, conn, pos); } if let Some(mob) = guest_mount { self.lan_open_mount(host, conn, mob); @@ -812,6 +836,31 @@ impl Game<'_> { } } + /// `ServerPlayer.tick`'s `stillValid`: a guest's block container closes + /// once the guest is out of reach of its block (a removed block closes it + /// in the hub's `sync_containers`). + fn lan_containers_tick(&mut self, host: &mut LanHost) { + let far: Vec = host + .hub + .guests + .iter() + .filter(|g| host.hub.here(g)) + .filter(|g| { + g.menu.as_ref().is_some_and(|m| { + m.slots.valid(&self.world) && !m.slots.still_valid(&self.world, g, m.pos) + }) + }) + .map(|g| g.conn) + .collect(); + for conn in far { + self.guest_container_closed(host, conn); + host.hub.close_container(conn); + if let Some(gi) = host.hub.guests.iter().position(|g| g.conn == conn) { + self.guest_carried_back(host, gi); + } + } + } + /// A packet from a guest that the hub does not handle itself. pub(super) fn lan_guest_packet(&mut self, host: &mut LanHost, conn: u32, packet: S) { let Some(gi) = host.hub.guests.iter().position(|g| g.conn == conn) else { @@ -1075,7 +1124,20 @@ impl Game<'_> { } S::ContainerClick(p) => { let rules = ItemRules(self.res.items()); + let slots = host.hub.guests[gi] + .menu + .as_ref() + .filter(|m| m.window == p.container) + .map(|m| m.slots); + let before = slots.and_then(|s| s.read(&self.world, &host.hub.guests[gi])); let drops = host.hub.container_click(conn, &p, &mut self.world, &rules); + // A click that changed the container's slots: BlockEntity.setChanged, + // so comparators read it again (a double chest's other half follows). + if let Some(Slots::Block(key) | Slots::Double(key, _)) = slots + && slots.and_then(|s| s.read(&self.world, &host.hub.guests[gi])) != before + { + self.container_changed(key); + } self.drop_guest_items(host, gi, drops); } S::ContainerClose(p) => { @@ -1216,29 +1278,35 @@ impl Game<'_> { host: &LanHost, stack: &Option, ) -> Option { - use horizon_protocol::item::{Component, component}; let s = stack.as_ref()?; - let name = host - .hub - .item_name(s.item) - .unwrap_or_else(|| "minecraft:air".into()); - let mut out = ItemStack::new(&name, s.count.max(1) as u32); - out.damage = s.damage().max(0) as u32; - if let Some(Component::Enchantments(list)) = s.component(component::ENCHANTMENTS) { - for (id, level) in list { - if let Some(e) = host.hub.enchantment_name(*id) { - out.enchant(&e, *level as u32); - } - } - } + let mut out = host.hub.stack_from_network(s); + out.count = out.count.max(1); Some(out) } + /// `LivingEntity.tickDeath`: 20 ticks after dying the player is removed, + /// and `Player.remove`'s `inventoryMenu.removed` drops what the cursor + /// held (`dropOrPlaceInInventory` for a removed player). Respawning + /// sooner removes it too. + fn guest_tick_death(&mut self, host: &mut LanHost, gi: usize) { + let g = &mut host.hub.guests[gi]; + g.death_time += 1; + if g.death_time >= 20 { + self.guest_drop_carried(host, gi); + } + } + + fn guest_drop_carried(&mut self, host: &mut LanHost, gi: usize) { + if let Some(c) = host.hub.guests[gi].carried.take() { + self.drop_guest_items(host, gi, vec![c]); + } + } + /// `AbstractContainerMenu.removed`: what a closing player held on the /// cursor goes back into their inventory (`placeItemBackInInventory`, /// dropped only if it doesn't fit), and their client's cursor empties /// with the inventory it is sent. - fn guest_carried_back(&mut self, host: &mut LanHost, gi: usize) { + pub(super) fn guest_carried_back(&mut self, host: &mut LanHost, gi: usize) { if let Some(c) = host.hub.guests[gi].carried.take() { self.lan_place_back(host, gi, vec![c]); host.hub.send_inventory(gi); @@ -1573,6 +1641,8 @@ impl Game<'_> { if !host.hub.guests[i].dead { return; } + // PlayerList.respawn removes the dead player (Player.remove). + self.guest_drop_carried(host, i); // PlayerList.respawn: a server's player comes back in its respawn // point's level: the Overworld, where the world spawn and beds are, // or another one's anchor or forced point, which its block resolves @@ -1613,6 +1683,7 @@ impl Game<'_> { } let g = &mut host.hub.guests[i]; g.dead = false; + g.death_time = 0; g.health = 20.0; g.food = 20; g.saturation = 5.0; @@ -1649,6 +1720,11 @@ impl Game<'_> { border: &self.border, }; host.hub.respawn(i, spawn, &ctx); + // ServerGamePacketListenerImpl.handleClientCommand: a hardcore world's + // respawned player only watches. + if self.level.hardcore { + host.hub.set_game_mode(conn, SPECTATOR); + } if let Some(config) = elsewhere { self.respawn_trip(conn, config); } @@ -1680,6 +1756,8 @@ impl Game<'_> { pub(super) fn tick_guests(&mut self, host: &mut LanHost) { self.lan_merchant_tick(host); self.lan_stations_tick(host); + self.lan_tables_tick(host); + self.lan_containers_tick(host); self.tick_lan_mount_menus(host); self.announce_guest_advancements(host); let conns: Vec = host @@ -1694,7 +1772,11 @@ impl Game<'_> { continue; }; self.guest_sleep_tick(host, gi); - if host.hub.guests[gi].dead || self.on_server_trip(conn) { + if host.hub.guests[gi].dead { + self.guest_tick_death(host, gi); + continue; + } + if self.on_server_trip(conn) { continue; } let play = host.play.entry(conn).or_default(); @@ -1792,31 +1874,34 @@ impl Game<'_> { let drops = (g.game_mode != SPECTATOR) .then(|| [at[0], at[1] + g.eye_height() - 0.3, at[2]]); let cause = host.play.get_mut(&conn).and_then(|p| p.death_cause.take()); - let message = match cause { - // ServerPlayer.die: the death message, to everyone unless - // the player's team hides it. - Some(key) if self.ui.rules.bool("show_death_messages") => { - let text = horizon_client::text::Text::translate( - &key, - &[self.player_text(&name, uuid)], - ); - let message = horizon_client::lan::data::component_nbt(&text.component()); - let team = self.score.board.players_team(&name); - if horizon_client::scoreboard::own_death_message_shown(team) { - host.hub.system_message(message.clone()); - if self.options.dedicated.is_some() { - super::dedicated::log( - "INFO", - &format!("System chat: {}", text.plain()), - ); - } else if self.player.is_some() { - self.system_message(text); - } + // ServerPlayer.die: with showDeathMessages, the message goes to + // the dying player's death screen and to everyone the team lets + // see it; without it, the death screen gets an empty one. + let message = if self.ui.rules.bool("show_death_messages") { + let me = self.player_text(&name, uuid); + let text = match cause { + Some(key) => horizon_client::text::Text::translate(&key, &[me]), + None if at[1] < f64::from(horizon_client::chunk::MIN_Y - 64) => { + horizon_client::text::Text::translate("death.attack.outOfWorld", &[me]) + } + None => self.guest_death_message(conn, me), + }; + let message = horizon_client::lan::data::component_nbt(&text.component()); + let team = self.score.board.players_team(&name); + if horizon_client::scoreboard::own_death_message_shown(team) { + host.hub.system_message(message.clone()); + if self.options.dedicated.is_some() { + super::dedicated::log( + "INFO", + &format!("System chat: {}", text.plain()), + ); + } else if self.player.is_some() { + self.system_message(text); } - message } - Some(_) => literal(""), - None => literal(&format!("{name} died")), + message + } else { + literal("") }; host.hub.send_to( conn, @@ -1832,6 +1917,21 @@ impl Game<'_> { } } + /// `ServerPlayer.die`'s message from a guest's own `CombatTracker`, which + /// sits in `guest_combat` between turns. + fn guest_death_message( + &mut self, + conn: u32, + me: horizon_client::text::Text, + ) -> horizon_client::text::Text { + let mut tracker = self.guest_combat.remove(&conn).unwrap_or_default(); + std::mem::swap(&mut self.combat, &mut tracker); + let text = self.combat_death_message_for(me); + std::mem::swap(&mut self.combat, &mut tracker); + self.guest_combat.insert(conn, tracker); + text + } + /// `Player.tick`'s sleep counter, and `LivingEntity.tick`'s wake-up of a /// sleeper who died or whose bed is gone. fn guest_sleep_tick(&mut self, host: &mut LanHost, gi: usize) { @@ -1942,6 +2042,7 @@ impl Game<'_> { let events = self.as_guest(host, conn, |game| { let (rules, motion) = (game.world.health_rules, game.motion); let health = &motion.health; + let freeze_immune = game.wears_freeze_immune(); let Some(player) = game.player.as_mut() else { return (Vec::new(), None); }; @@ -2005,11 +2106,18 @@ impl Game<'_> { // Entity.applyEffectsFromBlocks along those moves: magma, cactus, // lit campfires and grown sweet berry bushes hurt (#1305). w.player.shift_key_down = sneaking; + w.player.freeze_immune = freeze_immune; let _ = w.server_block_damage(start); let _ = w.tick_environment_damage(); let _ = w.apply_fire_contact(); let _ = w.server_hazards_tick(in_wall); let mut events = std::mem::take(&mut w.player.events); + // WitherRoseBlock.entityInside along those moves. + for event in &events { + if matches!(event, MotionEvent::InsideWitherRose) { + game.wither_rose_contact(); + } + } // ServerGamePacketListenerImpl.handleMovePlayer: // Player.checkMovementStatistics for each move. for d in travelled { @@ -2033,10 +2141,14 @@ impl Game<'_> { resources: game.motion, } .server_travel(); + // Landing damage from this travel reaches the tracker too. + events.append(&mut player.events); // A hurt since the last tick marked the velocity (Entity.markHurt): // ServerEntity.sendChanges sends it as it is after this travel. let marked = std::mem::take(&mut player.health.sync_velocity); - (events, marked.then_some(player.velocity)) + let velocity = marked.then_some(player.velocity); + game.record_combat(&events); + (events, velocity) }); let Some((events, marked)) = events else { return; diff --git a/crates/horizon-client/src/game/lan_crafting.rs b/crates/horizon-client/src/game/lan_crafting.rs index cb3b0a2c9..f74935522 100644 --- a/crates/horizon-client/src/game/lan_crafting.rs +++ b/crates/horizon-client/src/game/lan_crafting.rs @@ -19,8 +19,8 @@ use horizon_protocol::packets::play::{self, Clientbound as C}; pub(super) struct GuestCrafting { /// The inventory menu; its grid lives in the hub's guest. inventory: Option>, - /// The open crafting table: its window, state id and menu. - table: Option<(i32, i32, Box)>, + /// The open crafting table: its window, state id, menu and block. + table: Option<(i32, i32, Box, [i32; 3])>, /// The open furnace's or brewing stand's window and menu (`AbstractFurnaceMenu`, /// `BrewingStandMenu`); its slots live in the block entity and the hub's /// container menu keeps the client in sync. @@ -225,6 +225,7 @@ impl Game<'_> { Some(BlockEntity::Crafter(c)) => (Kind::CrafterBlock, c.items.to_vec()), _ => return false, }; + let furnace = matches!(kind, Kind::Furnace(_)); // CrafterBlockEntity.containerData: the disabled slots, then triggered. let crafter_data = match self.world.block_entities.get(&pos) { Some(BlockEntity::Crafter(c)) => Some((c.disabled, c.triggered)), @@ -284,6 +285,12 @@ impl Game<'_> { } _ => {} } + // FurnaceResultSlot.checkTakeAchievements: the guest took from the output. + let count = |s: Option<&ItemStack>| s.map_or(0, |s| s.count); + if furnace && count(menu.slot(2)) < count(items[2].as_ref()) { + let at = host.hub.guests[gi].position; + self.award_furnace_recipes(pos, at); + } host.play.entry(conn).or_default().crafting.machine = Some((p.container, menu)); host.hub.inventory_clicked(conn, p, &self.world, &changed); true @@ -383,17 +390,17 @@ impl Game<'_> { host.hub.send_inventory(gi); placed } else { - let Some((window, state, mut menu)) = host + let Some((window, state, mut menu, pos)) = host .play .get_mut(&conn) - .and_then(|play| play.crafting.table.take_if(|(w, _, _)| *w == p.container)) + .and_then(|play| play.crafting.table.take_if(|(w, ..)| *w == p.container)) else { return; }; let placed = self.guest_menu_op(host, gi, &mut menu, |menu, env| { menu.place_recipe(env, &id, p.use_max, creative, None) }); - host.play.entry(conn).or_default().crafting.table = Some((window, state, menu)); + host.play.entry(conn).or_default().crafting.table = Some((window, state, menu, pos)); self.lan_send_table_content(host, conn); placed }; @@ -403,7 +410,7 @@ impl Game<'_> { } /// The guest opened a crafting table (`CraftingTableBlock.getMenuProvider`). - pub(super) fn lan_open_crafting(&mut self, host: &mut LanHost, conn: u32) { + pub(super) fn lan_open_crafting(&mut self, host: &mut LanHost, conn: u32, pos: [i32; 3]) { self.lan_crafting_closed(host, conn, None); self.lan_station_closed(host, conn, None); // The container it replaces loses its opener. @@ -413,7 +420,7 @@ impl Game<'_> { return; }; let menu = Box::new(Menu::new(Kind::Crafting)); - host.play.entry(conn).or_default().crafting.table = Some((window, 0, menu)); + host.play.entry(conn).or_default().crafting.table = Some((window, 0, menu, pos)); self.lan_send_table_content(host, conn); } @@ -427,31 +434,72 @@ impl Game<'_> { let Some(play) = host.play.get_mut(&conn) else { return false; }; - let Some((window, state, mut menu)) = play + let Some((window, state, mut menu, pos)) = play .crafting .table - .take_if(|(window, _, _)| *window == p.container) + .take_if(|(window, ..)| *window == p.container) else { return false; }; + // A table that is no longer valid takes no clicks (handleContainerClick). if let Some(gi) = host.hub.guests.iter().position(|g| g.conn == conn) && !host.hub.guests[gi].spectator() + && self.lan_table_valid(&host.hub.guests[gi], pos) { self.guest_menu_op(host, gi, &mut menu, |menu, env| { let _ = menu.click(env, i32::from(p.slot), i32::from(p.button), input(p.input)); }); } - host.play.entry(conn).or_default().crafting.table = Some((window, state, menu)); + host.play.entry(conn).or_default().crafting.table = Some((window, state, menu, pos)); self.lan_send_table_content(host, conn); true } + /// `CraftingMenu.stillValid`: the block is still a crafting table and the + /// guest is within reach of it. + fn lan_table_valid(&self, g: &Guest, pos: [i32; 3]) -> bool { + let name = &self + .res + .facts() + .get(self.world.block(pos[0], pos[1], pos[2])) + .name; + &**name == "minecraft:crafting_table" && g.within_block_range(pos, 4.0) + } + + /// `ServerPlayer.tick`'s `stillValid`: a guest's crafting table closes + /// once it is gone or out of reach, giving its grid back. + pub(super) fn lan_tables_tick(&mut self, host: &mut LanHost) { + let invalid: Vec<(u32, i32)> = host + .play + .iter() + .filter_map(|(c, p)| p.crafting.table.as_ref().map(|t| (*c, t.0, t.3))) + .filter(|&(c, _, pos)| { + host.hub + .guests + .iter() + .find(|g| g.conn == c) + .is_some_and(|g| host.hub.here(g) && !self.lan_table_valid(g, pos)) + }) + .map(|(c, window, _)| (c, window)) + .collect(); + for (conn, window) in invalid { + self.lan_crafting_closed(host, conn, Some(window)); + host.hub.send_to( + conn, + &C::ContainerClose(play::ContainerClose { container: window }), + ); + if let Some(gi) = host.hub.guests.iter().position(|g| g.conn == conn) { + self.guest_carried_back(host, gi); + } + } + } + /// The whole crafting table menu for the client (`sendAllDataToRemote`). fn lan_send_table_content(&mut self, host: &mut LanHost, conn: u32) { let Some(gi) = host.hub.guests.iter().position(|g| g.conn == conn) else { return; }; - let Some((window, state, menu)) = host + let Some((window, state, menu, _)) = host .play .get_mut(&conn) .and_then(|p| p.crafting.table.as_mut()) @@ -499,9 +547,9 @@ impl Game<'_> { } else { match crafting .table - .take_if(|(w, _, _)| window.is_none_or(|window| *w == window)) + .take_if(|(w, ..)| window.is_none_or(|window| *w == window)) { - Some((_, _, menu)) => (1..10).filter_map(|s| menu.slot(s).cloned()).collect(), + Some((_, _, menu, _)) => (1..10).filter_map(|s| menu.slot(s).cloned()).collect(), None => return false, } }; @@ -527,7 +575,7 @@ impl Game<'_> { let mut grid: Vec = g.craft_grid.iter_mut().filter_map(Option::take).collect(); g.craft_result = None; if let Some(play) = play { - if let Some((_, _, menu)) = play.crafting.table { + if let Some((_, _, menu, _)) = play.crafting.table { grid.extend((1..10).filter_map(|s| menu.slot(s).cloned())); } // A workstation's inputs too. diff --git a/crates/horizon-client/src/game/lan_entities.rs b/crates/horizon-client/src/game/lan_entities.rs index dd272719a..cd4ba2fb8 100644 --- a/crates/horizon-client/src/game/lan_entities.rs +++ b/crates/horizon-client/src/game/lan_entities.rs @@ -404,11 +404,19 @@ impl Game<'_> { Value::Item(hub.stack(&Some(r.item.clone()))), )); } + let boosted = match r.guest { + Some(conn) => hub + .guests + .iter() + .find(|g| g.conn == conn) + .map(|g| g.entity_id), + None => host_entity, + }; if r.attached - && let Some(host) = host_entity + && let Some(player) = boosted { e.meta - .push((9, serializer::OPTIONAL_UNSIGNED_INT, Value::Id(host + 1))); + .push((9, serializer::OPTIONAL_UNSIGNED_INT, Value::Id(player + 1))); } if r.angled { e.meta.push((10, serializer::BOOLEAN, Value::Bool(true))); diff --git a/crates/horizon-client/src/game/lan_stations.rs b/crates/horizon-client/src/game/lan_stations.rs index 2efcfc705..418ed9c2a 100644 --- a/crates/horizon-client/src/game/lan_stations.rs +++ b/crates/horizon-client/src/game/lan_stations.rs @@ -588,7 +588,7 @@ impl Game<'_> { let [x, y, z] = gs.pos; let name = &self.res.facts().get(self.world.block(x, y, z)).name; let block = StationKind::from_block(name) == Some(gs.kind); - if !(block && within_reach(g, gs.pos)) { + if !(block && g.within_block_range(gs.pos, 4.0)) { let window = gs.window; self.lan_station_closed(host, conn, Some(window)); host.hub.send_to( @@ -631,16 +631,7 @@ impl Game<'_> { Some(BlockEntity::Lectern(l)) if l.book.is_some() ); // Container.stillValidBlockEntity: the interaction range and its buffer. - let eye = [g.position[0], g.position[1] + g.eye_height(), g.position[2]]; - let reach = if g.creative() { 5.0 } else { 4.5 } + 4.0; - let pos = [key.0, key.1, key.2]; - let near = (0..3) - .map(|k| { - let lo = f64::from(pos[k]); - (lo - eye[k]).max(eye[k] - lo - 1.0).max(0.0).powi(2) - }) - .sum::() - < reach * reach; + let near = g.within_block_range([key.0, key.1, key.2], 4.0); if has_book && near { self.lan_send_lectern(host, conn, false); } else { @@ -660,18 +651,3 @@ pub(super) fn station_inputs(gs: &GuestStation) -> Vec { .map(|(_, _, inputs)| inputs.filter_map(|s| gs.menu.slot(s).cloned()).collect()) .unwrap_or_default() } - -/// `Player.isWithinBlockInteractionRange(pos, 4)`: the guest's eyes within -/// its block interaction range (4.5, creative 5) and a 4-block buffer of the -/// block's box. -pub(super) fn within_reach(g: &horizon_client::lan::guest::Guest, pos: [i32; 3]) -> bool { - let eye = [g.position[0], g.position[1] + g.eye_height(), g.position[2]]; - let reach = if g.creative() { 5.0 } else { 4.5 } + 4.0; - (0..3) - .map(|k| { - let lo = f64::from(pos[k]); - (lo - eye[k]).max(eye[k] - lo - 1.0).max(0.0).powi(2) - }) - .sum::() - < reach * reach -} diff --git a/crates/horizon-client/src/game/loom.rs b/crates/horizon-client/src/game/loom.rs index ef49c8cf1..3d69aaca6 100644 --- a/crates/horizon-client/src/game/loom.rs +++ b/crates/horizon-client/src/game/loom.rs @@ -218,7 +218,7 @@ impl Game<'_> { oy: f32, draws: &[ScreenDraw], background: bool, - ) -> Option> { + ) -> Option> { let atlas = &self.gui.atlas; let mut tooltip = None; let mut sheet: Option = None; @@ -261,7 +261,10 @@ impl Game<'_> { .. } => self.draw_banner_result(batch, ox + *x0 as f32, oy + *y0 as f32, base, layers), ScreenDraw::Tooltip { key } => { - tooltip = Some(vec![(self.translate(key, key), u32::MAX)]); + tooltip = Some(vec![( + horizon_client::text::Text::literal(self.translate(key, key)), + u32::MAX, + )]); } ScreenDraw::SpriteOf { id } => sheet = Some(id.clone()), ScreenDraw::Fill { @@ -314,7 +317,7 @@ impl Game<'_> { ox: f32, oy: f32, local: (f32, f32), - ) -> Option> { + ) -> Option> { let s = self.station.as_ref()?; let l = &s.loom; let dye = s.inputs[1] diff --git a/crates/horizon-client/src/game/machines.rs b/crates/horizon-client/src/game/machines.rs index 8ce1940c9..6866552c1 100644 --- a/crates/horizon-client/src/game/machines.rs +++ b/crates/horizon-client/src/game/machines.rs @@ -889,13 +889,31 @@ impl Game<'_> { let key_is_cart = self .container .is_some_and(|k| k.1 == super::minecarts::CART_KEY_Y); - let (texture, height, title) = match screen { - Screen::Hopper if key_is_cart => { - ("gui/container/hopper", 133.0, "Minecart with Hopper") - } - Screen::Hopper => ("gui/container/hopper", 133.0, "Item Hopper"), - Screen::Dispenser => ("gui/container/dispenser", 166.0, "Dispenser"), - Screen::Dropper => ("gui/container/dispenser", 166.0, "Dropper"), + let (texture, height, key, fallback) = match screen { + Screen::Hopper if key_is_cart => ( + "gui/container/hopper", + 133.0, + "entity.minecraft.hopper_minecart", + "Minecart with Hopper", + ), + Screen::Hopper => ( + "gui/container/hopper", + 133.0, + "container.hopper", + "Item Hopper", + ), + Screen::Dispenser => ( + "gui/container/dispenser", + 166.0, + "container.dispenser", + "Dispenser", + ), + Screen::Dropper => ( + "gui/container/dispenser", + 166.0, + "container.dropper", + "Dropper", + ), }; if let Some(mut r) = atlas.sprites.get(texture).copied() { let dv = (r.v1 - r.v0) / r.h as f32; @@ -905,23 +923,17 @@ impl Game<'_> { batch.rect_uv(ox, oy, 176.0, height, r, u32::MAX); } let grey = horizon_client::hud::rgba(64, 64, 64, 255); - let title = &self + let title = self .remote_menu_title() - .unwrap_or_else(|| self.container_title(title.to_owned())); + .unwrap_or_else(|| self.container_title(self.translate(key, fallback))); let title_x = if screen == Screen::Hopper { 8.0 } else { - (176.0 - horizon_client::hud::HudBatch::text_width(atlas, title)) / 2.0 + (176.0 - horizon_client::hud::HudBatch::text_width(atlas, &title)) / 2.0 }; - batch.text(atlas, title, ox + title_x, oy + 6.0, grey, false); - batch.text( - atlas, - "Inventory", - ox + 8.0, - oy + height - 94.0, - grey, - false, - ); + batch.text(atlas, &title, ox + title_x, oy + 6.0, grey, false); + let inventory = self.translate("container.inventory", "Inventory"); + batch.text(atlas, &inventory, ox + 8.0, oy + height - 94.0, grey, false); true } } diff --git a/crates/horizon-client/src/game/machines/crafter.rs b/crates/horizon-client/src/game/machines/crafter.rs index 0fe66f7c8..b76bafd64 100644 --- a/crates/horizon-client/src/game/machines/crafter.rs +++ b/crates/horizon-client/src/game/machines/crafter.rs @@ -256,7 +256,8 @@ impl Game<'_> { grey, false, ); - batch.text(atlas, "Inventory", ox + 8.0, oy + 72.0, grey, false); + let inventory = self.translate("container.inventory", "Inventory"); + batch.text(atlas, &inventory, ox + 8.0, oy + 72.0, grey, false); self.paint_screen(batch, ox, oy, rest, true); } /// `gui.togglable_slot` over an empty, enabled grid slot. diff --git a/crates/horizon-client/src/game/mob_blocks.rs b/crates/horizon-client/src/game/mob_blocks.rs index a9e854bfb..083c96a66 100644 --- a/crates/horizon-client/src/game/mob_blocks.rs +++ b/crates/horizon-client/src/game/mob_blocks.rs @@ -259,7 +259,7 @@ impl Game<'_> { // ItemInHandLayer submits with OverlayTexture.NO_OVERLAY: no hurt tint. for (item, off_hand) in [(&mob.extra.main_hand, false), (&mob.extra.off_hand, true)] { let Some(item) = item else { continue }; - let left = off_hand != mob.extra.main_hand_left; + let left = off_hand != mob.main_arm_left(); let Some(t) = render::held_item_transform(mob, alpha as f32, camera, self.res.mob_data(), left) else { @@ -305,7 +305,7 @@ impl Game<'_> { .player_model .as_ref() .is_some_and(|p| std::ptr::eq(p, mob)); - owner.main_arm_left = mob.extra.main_hand_left; + owner.main_arm_left = mob.main_arm_left(); if off_hand == mob.extra.using_off_hand && mob.extra.using_item > 0 { owner.using_this_item = true; owner.use_duration = 72_000; diff --git a/crates/horizon-client/src/game/mods.rs b/crates/horizon-client/src/game/mods.rs index 2a07cb940..4a708f2cb 100644 --- a/crates/horizon-client/src/game/mods.rs +++ b/crates/horizon-client/src/game/mods.rs @@ -45,6 +45,14 @@ pub(super) struct ModsState { /// The chat's command tree while a running mod owns commands. #[cfg(feature = "mods")] tree: Option, + /// `command.run` lines for this window's own world, run on the next tick: + /// run at once, their chat lines would reach the mods while their output + /// is still being shown, and a mod answering its own lines would recurse. + #[cfg(feature = "mods")] + commands: Vec, + /// The server's command tree with the running mods' commands added. + #[cfg(feature = "mods")] + remote_tree: Option, /// A `ui` mod's screen or settings page while it is up. #[cfg(feature = "mods")] screen: Option, @@ -72,6 +80,14 @@ impl ModsState { } vanilla::game_dispatcher() } + /// The server's tree plus the mods' commands, when a mod owns any. + pub(super) fn remote_dispatcher(&self) -> Option<&Dispatcher> { + #[cfg(feature = "mods")] + if let Some(tree) = &self.remote_tree { + return Some(tree); + } + None + } /// Whether a mod runs: the chat keeps its lines for them. pub(super) fn running(&self) -> bool { #[cfg(feature = "mods")] @@ -181,6 +197,7 @@ impl Game<'_> { ); } } + pub(super) fn merge_remote_tree(&mut self) {} pub(super) fn sync_mods(&mut self) {} pub(super) fn tick_mods(&mut self) {} pub(super) fn mods_frame(&mut self, _elapsed: Duration) {} @@ -270,7 +287,20 @@ impl Game<'_> { .peek() .is_some() .then(|| vanilla::game_dispatcher().with_mod_commands(names)); + self.merge_remote_tree(); } + + /// The server's command tree with the mods' commands, for the chat. + pub(super) fn merge_remote_tree(&mut self) { + let names: Vec = self.mods.mods.commands().map(str::to_owned).collect(); + self.mods.remote_tree = self + .remote + .as_ref() + .and_then(|r| r.commands.as_ref()) + .filter(|_| !names.is_empty()) + .map(|t| t.with_mod_commands(names.iter().map(String::as_str))); + } + /// After the options changed: restart the mods if the list of the ones /// turned on did. pub(super) fn sync_mods(&mut self) { @@ -319,6 +349,11 @@ impl Game<'_> { } /// A game tick. pub(super) fn tick_mods(&mut self) { + // Here, at the player's level: shown inside a console call (a control + // socket command set the hook off), they would run as the console. + for line in std::mem::take(&mut self.mods.commands) { + self.run_command(&line); + } if !self.mods.mods.is_running() { return; } @@ -378,6 +413,19 @@ impl Game<'_> { } Output::Feedback { text, error: false } => Text::literal(text), Output::Feedback { text, error: true } => Text::literal(text).with_color(text::RED), + // Vanilla's commands only: a mod running a mod command could loop. + Output::Command { line } => { + let name = line.split(' ').next().unwrap_or_default(); + if !vanilla_command(name) { + // Dropped. + } else if self.remote.is_some() { + // The server checks the player's permissions. + self.send_chat(&format!("/{line}")); + } else { + self.mods.commands.push(line); + } + continue; + } Output::Stopped { name, reason } => { // Its screen goes with it: the entry's run is gone, so // `mod_ui_view` finds no widgets and the screen closes. diff --git a/crates/horizon-client/src/game/mount_menu.rs b/crates/horizon-client/src/game/mount_menu.rs index e9e1e380b..c80c900f9 100644 --- a/crates/horizon-client/src/game/mount_menu.rs +++ b/crates/horizon-client/src/game/mount_menu.rs @@ -470,7 +470,8 @@ impl Game<'_> { None => String::new(), }; batch.text(atlas, &title, ox + 8.0, oy + 6.0, grey, false); - batch.text(atlas, "Inventory", ox + 8.0, oy + 72.0, grey, false); + let inventory = self.translate("container.inventory", "Inventory"); + batch.text(atlas, &inventory, ox + 8.0, oy + 72.0, grey, false); if let Some(m) = self.mobs.iter().find(|m| m.id == open.mob) { self.draw_mob_preview( batch, diff --git a/crates/horizon-client/src/game/names.rs b/crates/horizon-client/src/game/names.rs index be829f1bb..7655fb489 100644 --- a/crates/horizon-client/src/game/names.rs +++ b/crates/horizon-client/src/game/names.rs @@ -151,10 +151,13 @@ impl Game<'_> { .clone() .unwrap_or_else(|| Text::literal(&name)) }; + // StringDecomposer.iterateFormatted: `§` codes a server leaves + // inside the team prefix or the name itself apply at draw time. let shown = match team { Some(t) => t.formatted_name(member), None => member, - }; + } + .resolve_legacy(); let mut lines = Vec::new(); // EntityRenderer.extractNameTags: the score shows within the // below-name distance (10), under the name. @@ -162,7 +165,7 @@ impl Game<'_> { if distance_sqr < 10.0 * 10.0 && let Some(score) = holder.and_then(|h| self.below_name_text(&h)) { - lines.push((score, 0.0)); + lines.push((score.resolve_legacy(), 0.0)); lift = f64::from(9.0f32 * 1.15 * 0.025); } lines.push((shown, lift)); @@ -213,14 +216,15 @@ impl Game<'_> { let shown = match team { Some(t) => t.formatted_name(name.clone()), None => name.clone(), - }; + } + .resolve_legacy(); let mut lines = Vec::new(); let mut lift = 0.0; let holder = self.persist.uuids.get(&a.id).map(|u| uuid_text(*u)); if distance_sqr < 10.0 * 10.0 && let Some(score) = holder.and_then(|h| self.below_name_text(&h)) { - lines.push((score, 0.0)); + lines.push((score.resolve_legacy(), 0.0)); lift = f64::from(9.0f32 * 1.15 * 0.025); } lines.push((shown, lift)); diff --git a/crates/horizon-client/src/game/options.rs b/crates/horizon-client/src/game/options.rs index 812c9ce07..0e385e25c 100644 --- a/crates/horizon-client/src/game/options.rs +++ b/crates/horizon-client/src/game/options.rs @@ -247,6 +247,7 @@ impl Game<'_> { pub(super) fn load_language(&mut self, code: &str) { let root = self.options.resources.clone(); lang::set(lang::Language::load(&[&root], code)); + self.refresh_remote_language(); self.ui.chat.rescale(&self.gui.atlas); } /// Settings that Horizon applies beyond render distance, volume and GUI scale. diff --git a/crates/horizon-client/src/game/packs.rs b/crates/horizon-client/src/game/packs.rs index 7629a5d25..733b19649 100644 --- a/crates/horizon-client/src/game/packs.rs +++ b/crates/horizon-client/src/game/packs.rs @@ -203,6 +203,7 @@ impl Game<'_> { horizon_client::mobs::villager_hats::VillagerHats::load(root); horizon_client::ui::load_metadata(root); lang::set(lang::Language::load(&[&root.to_path_buf()], &lang::code())); + self.refresh_remote_language(); self.audio.reload(root); self.clear_lod(); self.streaming.reload_resources(&mut self.world, context); diff --git a/crates/horizon-client/src/game/pad_menu.rs b/crates/horizon-client/src/game/pad_menu.rs index 32acdd256..30f2e7337 100644 --- a/crates/horizon-client/src/game/pad_menu.rs +++ b/crates/horizon-client/src/game/pad_menu.rs @@ -921,8 +921,10 @@ impl Game<'_> { } else if self.book.is_some() { self.book_input(key, text); } else if self.screen == Screen::Creative && self.creative_screen.tab().search { - if key != Some(KeyCode::Enter) { - self.creative_search_edit(text, backspace); + if backspace { + self.creative_search_backspace(false); + } else if key != Some(KeyCode::Enter) { + self.creative_search_edit(text); } } else if self.recipe_search_focused() { if key != Some(KeyCode::Enter) { diff --git a/crates/horizon-client/src/game/passengers.rs b/crates/horizon-client/src/game/passengers.rs index b20c1560b..8f205c484 100644 --- a/crates/horizon-client/src/game/passengers.rs +++ b/crates/horizon-client/src/game/passengers.rs @@ -147,8 +147,16 @@ impl Game<'_> { } } Species::Strider if !baby => self.spawn_strider_jockey(id, at), - // ZombieHorse.finalizeSpawn: a zombie with an iron spear. - Species::ZombieHorse if natural => { + // ZombieHorse.finalizeSpawn: a zombie with an iron spear. The rider + // comes from EntityType.create, which makes no zombie in peaceful + // (canSpawn), so a peaceful zombie horse spawns without one. + Species::ZombieHorse + if natural + && (self.ui.difficulty != 0 + || horizon_client::mobs::spawn_rules::allowed_in_peaceful( + Species::Zombie, + )) => + { if let Some(zombie) = self.spawn_mob("minecraft:zombie", at, false) { if let Some(z) = self.mobs.iter_mut().find(|m| m.id == zombie) { z.extra.main_hand = Some("minecraft:iron_spear".into()); diff --git a/crates/horizon-client/src/game/persist.rs b/crates/horizon-client/src/game/persist.rs index d3afbf6ce..45ef8801f 100644 --- a/crates/horizon-client/src/game/persist.rs +++ b/crates/horizon-client/src/game/persist.rs @@ -76,15 +76,14 @@ impl Game<'_> { let pos = ChunkPos::new(chunk.x, chunk.z); let time = self.world.game_time; let mut extras: ChunkExtras = chunk.saved.as_deref().cloned().unwrap_or_default(); + // A chunk the scheduler hasn't registered yet (one loaded and + // unloaded again before the next tick) still holds its saved ticks: + // `register_chunk_ticks` empties that list once it takes them. if let Some(t) = self.block_ticks.blocks.chunk(pos) { extras.block_ticks = t.pack(time); - } else { - extras.block_ticks.clear(); } if let Some(t) = self.block_ticks.fluids.chunk(pos) { extras.fluid_ticks = t.pack(time); - } else { - extras.fluid_ticks.clear(); } extras.last_update = time; // TheEndGatewayBlockEntity saves its Age (the server copy's). diff --git a/crates/horizon-client/src/game/postprocess.rs b/crates/horizon-client/src/game/postprocess.rs index e896a77ab..77cba6832 100644 --- a/crates/horizon-client/src/game/postprocess.rs +++ b/crates/horizon-client/src/game/postprocess.rs @@ -27,6 +27,11 @@ impl Game<'_> { if ready.is_empty() { return Ok(()); } + // The ticks the marks schedule go to the chunks' containers, which a + // chunk that arrived since the last tick doesn't have yet + // (`LevelChunk.registerTickContainerInLevel` comes before + // `postProcessGeneration`). + self.sync_tick_chunks(); for key in ready { let Some(chunk) = self.world.chunks.get_mut(&key) else { continue; diff --git a/crates/horizon-client/src/game/potions.rs b/crates/horizon-client/src/game/potions.rs index b17316e08..b28c2fb4d 100644 --- a/crates/horizon-client/src/game/potions.rs +++ b/crates/horizon-client/src/game/potions.rs @@ -755,7 +755,7 @@ impl Game<'_> { image_width, &self.potions.effects.list, self.clock.rate(), - &|key| self.pack.names.get(key).map(str::to_owned), + &|key| self.pack.names.display_get(key), &|text| self.gui.atlas.font.width(text).ceil() as i32, ); if let Some(lines) = inventory_effects::paint( diff --git a/crates/horizon-client/src/game/recipe_book.rs b/crates/horizon-client/src/game/recipe_book.rs index d3c14ffc7..96e12d79b 100644 --- a/crates/horizon-client/src/game/recipe_book.rs +++ b/crates/horizon-client/src/game/recipe_book.rs @@ -502,7 +502,7 @@ impl Game<'_> { horizon_client::item_tooltip::lines( stack, self.res.items().get(&stack.item), - &|k| self.pack.names.get(k).map(str::to_owned), + &|k| self.pack.names.display_get(k), ) }) { @@ -1163,8 +1163,12 @@ impl Game<'_> { } /// Paints recipe book calls; returns the first tooltip set (vanilla keeps /// the first one a frame asks for). - fn paint_book(&mut self, batch: &mut HudBatch, calls: &[Drawn]) -> Option> { - let mut tooltip: Option> = None; + fn paint_book( + &mut self, + batch: &mut HudBatch, + calls: &[Drawn], + ) -> Option> { + let mut tooltip: Option> = None; for d in calls { let start = batch.vertices.len(); let atlas = &self.gui.atlas; @@ -1259,7 +1263,14 @@ impl Game<'_> { tooltip = Some( lines .iter() - .map(|l| (l.iter().map(|r| r.text.as_str()).collect(), u32::MAX)) + .map(|l| { + ( + horizon_client::text::Text::literal( + l.iter().map(|r| r.text.as_str()).collect::(), + ), + u32::MAX, + ) + }) .collect(), ); } @@ -1269,10 +1280,10 @@ impl Game<'_> { let mut lines = self.stack_tooltip(stack); if *more { lines.push(( - self.translate( + horizon_client::text::Text::literal(self.translate( "gui.recipebook.moreRecipes", "Right Click for More", - ), + )), u32::MAX, )); } diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 044e43835..03b48df3d 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -193,6 +193,9 @@ pub(super) struct Remote { /// the oldest of them first waited (`poll_remote`). backlog: VecDeque, backlog_since: Option, + /// `ClientboundTransferPacket`'s target: when the connection ends the + /// game connects here, the stored cookies carried over (`TransferState`). + transfer: Option, } /// The time a frame spends on chunk packets before it leaves the rest of @@ -220,30 +223,6 @@ fn face_direction(face: [i32; 3]) -> u8 { } } -/// A `minecraft:profile` component as the item data JSON the model reads. -fn profile_tag(p: &horizon_protocol::item::Profile) -> Tag { - let properties = p - .properties - .iter() - .map(|(name, value, signature)| { - let mut entries: Vec<(&'static str, Tag)> = vec![ - ("name", Tag::String(name.as_str().into())), - ("value", Tag::String(value.as_str().into())), - ]; - if let Some(signature) = signature { - entries.push(("signature", Tag::String(signature.as_str().into()))); - } - Tag::compound(entries) - }) - .collect::>(); - let mut entries: Vec<(&'static str, Tag)> = Vec::new(); - if let Some(name) = &p.name { - entries.push(("name", Tag::String(name.as_str().into()))); - } - entries.push(("properties", Tag::List(properties))); - Tag::compound(entries) -} - /// A protocol number format as the scoreboard's. fn number_format(f: &play::NumberFormat) -> horizon_client::scoreboard::NumberFormat { use horizon_client::scoreboard::{NumberFormat, tag_to_json}; @@ -257,14 +236,25 @@ fn number_format(f: &play::NumberFormat) -> horizon_client::scoreboard::NumberFo impl Game<'_> { /// Leave the title for a server (ConnectScreen): an empty world fed by the connection. pub(super) fn connect_server(&mut self, address: &str, name: &str) -> Result<()> { + self.connect_server_with_cookies(address, name, None) + } + /// `connect_server` carrying a transfer's stored server cookies + /// (`TransferState`); None on an ordinary join. + pub(super) fn connect_server_with_cookies( + &mut self, + address: &str, + name: &str, + cookies: Option, + ) -> Result<()> { if self.defer_world_entry(super::PendingWorldEntry::Server { address: address.to_owned(), name: name.to_owned(), + cookies: cookies.clone(), })? { return Ok(()); } self.join_world_rest()?; - if let Err(error) = self.join_server(address, name, None) { + if let Err(error) = self.join_server(address, name, None, cookies) { log::warn!("Horizon: could not join server: {error}"); if self.title.is_none() { self.title = Some(horizon_client::title::Title::new( @@ -285,13 +275,14 @@ impl Game<'_> { pipe: horizon_protocol::connection::Pipe, name: &str, ) -> Result<()> { - self.join_server("local", name, Some(pipe)) + self.join_server("local", name, Some(pipe), None) } fn join_server( &mut self, address: &str, name: &str, local: Option, + cookies: Option, ) -> Result<()> { self.perf.world_opening(); let root = self.options.resources.clone(); @@ -323,7 +314,17 @@ impl Game<'_> { Some(pipe) => Session::connect_local(pipe, name, view, lang.clone()), None => { self.extras_connecting(address, name); - Session::connect(address, name, auth.clone(), view, lang.clone()) + match cookies { + Some(cookies) => Session::connect_transferred( + address, + name, + auth.clone(), + view, + lang.clone(), + cookies, + ), + None => Session::connect(address, name, auth.clone(), view, lang.clone()), + } } }; let player_type = super::perspective::player_type(&root); @@ -446,6 +447,7 @@ impl Game<'_> { chunk_center: (0, 0), backlog: VecDeque::new(), backlog_since: None, + transfer: None, }); // The level's TickRateManager follows the server's ticking packets. self.clock = horizon_client::clock::FixedStep::client(); @@ -584,164 +586,32 @@ impl Game<'_> { .and_then(|i| i.id(name)) .unwrap_or(0) } + /// A stack from the server, decoded as the saved and command item NBT + /// are, so tooltips and models see every field. pub(super) fn to_stack(&self, s: &Option) -> Option { - let s = s.as_ref()?; - let mut out = ItemStack { - item: self.item_name(s.item), - count: s.count.max(0) as u32, - damage: 0, - extra: None, - }; - // A vanilla `components` compound, then the same decoder the saved - // and command item NBT use, so tooltips and models see every field. - let components = self.protocol_components(s); - let (damage, data) = horizon_client::item_data::decode_components(components.as_ref()); - out.damage = damage; - if let Some(data) = data { - *out.data_mut() = *data; - } - out.tidy(); - Some(out) - } - /// The item's patched components as vanilla's `components` compound. - fn protocol_components(&self, s: &Stack) -> Option { - use horizon_protocol::item::{Component, component}; let r = self.remote.as_ref()?; - let name = |id: i32| r.builtin.get("minecraft:data_component_type")?.name(id); - let mut entries: Vec<(&str, Tag)> = Vec::new(); - for (kind, c) in &s.added { - let Some(key) = name(*kind) else { - continue; - }; - let value = match c { - // Read past, not decoded: the item keeps its prototype's value. - Component::Raw(_) => continue, - // Trim materials and patterns are synced registries, as enchantments are. - Component::Trim { material, pattern } => { - let (Some(material), Some(pattern)) = ( - r.registries.name("minecraft:trim_material", *material), - r.registries.name("minecraft:trim_pattern", *pattern), - ) else { - continue; - }; - Tag::compound([ - ("material", Tag::String(material.into())), - ("pattern", Tag::String(pattern.into())), - ]) - } - Component::Int(v) if *kind == component::RARITY => { - let rarity = ["common", "uncommon", "rare", "epic"]; - Tag::String(rarity.get(*v as usize).copied().unwrap_or("common").into()) - } - Component::Int(v) => Tag::Int(*v), - Component::Unit => Tag::Compound(Default::default()), - Component::Bool(v) => Tag::Byte(i8::from(*v)), - Component::Text(t) | Component::Nbt(t) => t.clone(), - Component::Ident(v) => Tag::String(v.as_str().into()), - Component::Lore(lines) => Tag::List(lines.clone()), - Component::WritableBook(pages) => Tag::compound([( - "pages", - Tag::List( - pages - .iter() - .map(|p| Tag::String(p.as_str().into())) - .collect(), - ), - )]), - Component::WrittenBook(b) => Tag::compound([ - ("title", Tag::String(b.title.as_str().into())), - ("author", Tag::String(b.author.as_str().into())), - ("generation", Tag::Int(b.generation)), - ("pages", Tag::List(b.pages.clone())), - ("resolved", Tag::Byte(i8::from(b.resolved))), - ]), - Component::Enchantments(list) => { - // Enchantments are a synced (data-driven) registry: the - // ids index the server's RegistryData, not the builtin - // report, so a datapack enchantment names correctly. - let levels = list.iter().filter_map(|(id, level)| { - let name = r.registries.name("minecraft:enchantment", *id)?; - Some((name, Tag::Int(*level))) - }); - Tag::compound(levels) - } - Component::AttackRange(v) => Tag::compound([ - ("min_reach", Tag::Float(v[0].to_bits())), - ("max_reach", Tag::Float(v[1].to_bits())), - ("min_attack_momentum", Tag::Float(v[2].to_bits())), - ("max_attack_momentum", Tag::Float(v[3].to_bits())), - ("hitbox_margin", Tag::Float(v[4].to_bits())), - ("mob_factor", Tag::Float(v[5].to_bits())), - ]), - Component::Profile(p) => profile_tag(p), - // ItemStackTemplate.CODEC: id, count and components. - Component::Stacks(stacks) => Tag::List( - stacks - .iter() - .map(|t| { - let mut entries = vec![ - ("id", Tag::String(self.item_name(t.item).as_ref().into())), - ("count", Tag::Int(t.count)), - ]; - if let Some(c) = self.protocol_components(t) { - entries.push(("components", c)); - } - Tag::compound(entries) - }) - .collect(), - ), - Component::CustomModelData { - floats, - flags, - strings, - colors, - } => Tag::compound([ - ( - "floats", - Tag::List(floats.iter().map(|f| Tag::Float(f.to_bits())).collect()), - ), - ( - "flags", - Tag::List(flags.iter().map(|b| Tag::Byte(i8::from(*b))).collect()), - ), - ( - "strings", - Tag::List( - strings - .iter() - .map(|s| Tag::String(s.as_str().into())) - .collect(), - ), - ), - ( - "colors", - Tag::List(colors.iter().map(|c| Tag::Int(*c)).collect()), - ), - ]), - Component::TooltipDisplay { hide, hidden } => { - let hidden = hidden - .iter() - .filter_map(|id| Some(Tag::String(name(*id)?.into()))); - Tag::compound([ - ("hide_tooltip", Tag::Byte(i8::from(*hide))), - ("hidden_components", Tag::List(hidden.collect())), - ]) - } - }; - entries.push((key, value)); - } - Some(Tag::compound(entries)) + Some(horizon_client::item_sync::from_network( + s.as_ref()?, + &r.builtin, + &r.registries, + )) } + /// A stack to the server (`SetCreativeModeSlot`): the same component + /// patch a Horizon server sends its clients, with the registries' ids, so + /// the server's echo doesn't hand back a bare item. fn to_network(&self, s: &Option) -> Option { - let s = s.as_ref()?; - let mut stack = Stack::new(self.item_id(&s.item), s.count as i32); - if s.damage > 0 { - stack.added.push(( - horizon_protocol::item::component::DAMAGE, - horizon_protocol::item::Component::Int(s.damage as i32), - )); - } - Some(stack) + let r = self.remote.as_ref()?; + horizon_client::lan::guest::stack_to_network( + |name| self.item_id(name), + // Built-in first, as a hub's `SyncData::id` does: potions are one. + |registry, name| { + r.builtin + .get(registry) + .and_then(|ids| ids.id(name)) + .or_else(|| r.registries.id(registry, name)) + }, + s, + ) } /// A line in the connection's chat, with its styles, click and hover /// events (ChatComponent.addMessage). @@ -867,6 +737,15 @@ impl Game<'_> { } } Event::Reconfigured(registries) => self.set_registries(registries), + // handleTransfer: "Cannot transfer to server from + // singleplayer" (no ServerData), so the world just closes. + Event::Transfer { host, port } => { + if let Some(r) = self.remote.as_mut() + && r.session.address != "local" + { + r.transfer = Some(format!("{host}:{port}")); + } + } Event::PackPush(p) => self.handle_pack_push(p), Event::PackPop(id) => self.handle_pack_pop(id), Event::DecodeError(e) => { @@ -882,6 +761,20 @@ impl Game<'_> { from_server, } => { log::info!("Horizon: disconnected: {reason}"); + // ClientboundTransferPacket left a target: connect there + // instead of showing the disconnect, the stored cookies + // carried over (ClientCommonPacketListenerImpl. + // handleTransfer → ConnectScreen.startConnecting). + let transfer = self.remote.as_mut().and_then(|r| { + r.transfer.take().map(|address| { + (address, r.session.name.clone(), r.session.cookies.clone()) + }) + }); + if let Some((address, name, cookies)) = transfer { + self.leave_server(None)?; + self.connect_server_with_cookies(&address, &name, Some(cookies))?; + return Ok(()); + } let retry = self.reconnect_after(&reason, from_server); self.leave_server(Some(reason))?; self.offer_reconnect(retry); @@ -1015,6 +908,9 @@ impl Game<'_> { r.stepped.clear(); r.head_lerps.clear(); r.pending_items.clear(); + // The entity ids are the old world's; the new dimension's + // player entities re-map to their profiles on AddEntity. + r.level_players.clear(); // A new ClientLevel: a new chunk storage, centred on 0, 0. r.chunk_center = (0, 0); } @@ -1524,10 +1420,10 @@ impl Game<'_> { self.turn_remote_projectile(id, rotation); } fn remove_entity(&mut self, id: i32) { - // The vehicle the player rode is gone (Entity.removeVehicle). - if self.riding == Some(id as u32) { - self.riding = None; - } + // Entity.setRemoved detaches every passenger, even when no empty + // SetPassengers packet preceded the vehicle's removal. Otherwise + // mobs and other players keep their passenger pose after it is gone. + self.remote_set_passengers(id, &[]); self.mobs.retain(|m| m.id != id as u32); self.orbs.retain(|o| o.id != id as u32); self.dropped.retain(|d| d.id != id as u32); @@ -1994,15 +1890,6 @@ impl Game<'_> { } } } - if let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) - && m.species() == horizon_client::mobs::Species::Rabbit - { - for (index, _, value) in &p.values { - if let (15, Value::Byte(flags)) = (index, value) { - m.no_ai = flags & 1 != 0; - } - } - } self.remote_mob_data(p.id, &p.values); if let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) && m.species() == horizon_client::mobs::Species::Creeper @@ -2017,25 +1904,12 @@ impl Game<'_> { } } } - // LivingEntity.onSyncedDataUpdated(DATA_LIVING_ENTITY_FLAGS): - // another player starts using the item in a hand (eating, a - // bow drawn, a shield up), or stops. + // LivingEntity.DATA_LIVING_ENTITY_FLAGS: for another player + // too, remote_mob_data's apply reads index 8 above. let other = self .remote .as_ref() .is_some_and(|r| r.level_players.contains_key(&p.id) && r.entity_id != p.id); - if other && let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) { - for (index, _, value) in &p.values { - if let (8, Value::Byte(flags)) = (index, value) { - m.extra.using_off_hand = flags & 2 != 0; - if flags & 1 == 0 { - m.extra.using_item = 0; - } else if m.extra.using_item == 0 { - m.extra.using_item = 1; - } - } - } - } // LivingEntity.onSyncedDataUpdated(SLEEPING_POS_ID): another // player lies down in their bed (drawn lying in it), or gets up. if other { @@ -2285,12 +2159,10 @@ impl Game<'_> { } } C::PlayerCombatKill(p) => { - let lang = self - .remote - .as_ref() - .map(|r| r.lang.clone()) - .unwrap_or_default(); - let message = plain(&p.message, &lang); + // The component resolves in the client's current language, as + // the chat line does, not in the language the session joined + // with (`Remote.lang` is the vanilla server-language table). + let message = remote_text(&p.message).plain(); let me = self.remote.as_ref().is_some_and(|r| r.entity_id == p.id); if let Some(r) = self.remote.as_mut() { r.death_message = Some(message); @@ -2493,6 +2365,7 @@ impl Game<'_> { if let Some(r) = self.remote.as_mut() { r.commands = Some(tree); } + self.merge_remote_tree(); } C::SystemChat(p) => { // The component as sent, translated in the client's own @@ -3187,6 +3060,7 @@ impl Game<'_> { self.tick_block_entity_anims(); self.tick_hand(); self.tick_particles(); + self.tick_client_ambient(); self.tick_client_effects(); // Player.tick's sleep counter (the darkening and the wake-up fade). self.sleep_timer = super::tick_sleep_timer(self.sleep_timer, self.sleeping.is_some()); @@ -3520,25 +3394,28 @@ impl Game<'_> { m.head_yaw += wrap_degrees(*yaw - m.head_yaw) / *steps as f32; *steps -= 1; } - // LivingEntity.updatingUsingItem for another player: the ticks the - // item has been used before this one. - if player && m.extra.using_item > 0 { - let item = if m.extra.using_off_hand { - m.extra.off_hand.clone() - } else { - m.extra.main_hand.clone() - }; - if let Some(item) = item { - let eye = if m.extra.crouching { 1.27 } else { 1.62 }; - let at = [m.position.x, m.position.y, m.position.z]; - using.push(( - item, - m.extra.using_item as u32 - 1, - at, - eye, - m.head_pitch, - m.yaw, - )); + // LivingEntity.updatingUsingItem: the ticks the item has been + // used before this one. Another player's use particles; a mob's + // drawn bow only needs the count (its pull follows it). + if m.extra.using_item > 0 { + if player { + let item = if m.extra.using_off_hand { + m.extra.off_hand.clone() + } else { + m.extra.main_hand.clone() + }; + if let Some(item) = item { + let eye = if m.extra.crouching { 1.27 } else { 1.62 }; + let at = [m.position.x, m.position.y, m.position.z]; + using.push(( + item, + m.extra.using_item as u32 - 1, + at, + eye, + m.head_pitch, + m.yaw, + )); + } } m.extra.using_item += 1; } @@ -4009,8 +3886,14 @@ impl Game<'_> { } if reacts { self.predict_use(&changes, seq); - } else if let Some(stack) = held.as_ref() { - self.predict_item_on_block(stack, &hit); + } else if let Some(stack) = held.as_ref() + && self.predict_item_on_block(stack, &hit) + { + // Item.useOn succeeded here (InteractionResult.SUCCESS): + // this hand swings and the click ends, so the other hand + // (a shield, a torch) is never tried. + self.with_hand(hand, Game::swing_arm); + return true; } if held_block { let placed = if reacts { @@ -4040,7 +3923,8 @@ impl Game<'_> { /// `playSound(player, ...)`: the server sends them to the others only): /// honeycomb waxing copper, and an axe, hoe or shovel transforming a /// block (stripping, scraping, removing wax, tilling, making a path). - fn predict_item_on_block(&mut self, stack: &ItemStack, hit: &super::Hit) { + /// Whether the item's use succeeded, which ends the click. + fn predict_item_on_block(&mut self, stack: &ItemStack, hit: &super::Hit) -> bool { let at = [ f64::from(hit.pos[0]) + 0.5, f64::from(hit.pos[1]) + 0.5, @@ -4059,11 +3943,15 @@ impl Game<'_> { self.audio .play("minecraft:item.honeycomb.wax_on", Some(at), 1.0, 1.0); } + waxes } else if let Some((_, sound, _, _, particle)) = self.block_transform(stack, hit.pos, hit.face) { self.transform_particle(&particle, hit.pos); self.audio.play(&sound, Some(at), 1.0, 1.0); + true + } else { + false } } @@ -4796,6 +4684,11 @@ impl Game<'_> { self.mount_menu_reset(); // The client's menu has no level access: what it holds is the server's. self.station = None; + // AbstractContainerMenu.removed: the server gives the grid and the + // carried stack back to the inventory and syncs it; the copies here + // are stale the moment the window closes. + self.craft_grid.fill(None); + self.carried = None; self.craft_size = 2; if let Some(key) = self.container.take() { // Server containers stay renderable but lose their copied slots. @@ -4919,6 +4812,7 @@ impl Game<'_> { cooking_timer: 0, cooking_total: 0, speed: 1.0, + recipes_used: std::collections::HashMap::new(), })) } // HopperMenu, and DispenserMenu (MenuType.GENERIC_3x3: dispensers @@ -5003,6 +4897,15 @@ impl Game<'_> { pub(super) fn remote_menu_title(&self) -> Option { self.remote.as_ref()?.menu_title.clone() } + /// A language change reaches the server-sent components too: their titles + /// are resolved when the packet arrives, with the session's language. + pub(super) fn refresh_remote_language(&mut self) { + if let Some(lang) = horizon_client::lang::current() + && let Some(r) = self.remote.as_mut() + { + r.lang = Arc::new(lang.map().clone()); + } + } /// A server's chest-like menu (`MenuType.GENERIC_9xN`): its rows and title. pub(super) fn remote_chest_menu(&self) -> Option<(usize, String)> { let r = self.remote.as_ref()?; diff --git a/crates/horizon-client/src/game/remote_mob_data.rs b/crates/horizon-client/src/game/remote_mob_data.rs index 5573d0f87..da786c1b9 100644 --- a/crates/horizon-client/src/game/remote_mob_data.rs +++ b/crates/horizon-client/src/game/remote_mob_data.rs @@ -20,9 +20,28 @@ fn apply(m: &mut Mob, values: &[(u8, i32, Value)], builtin: &Builtin) { // client, which draws the flames. A client doesn't count the // fire down: the flag goes off when the server's fire is out. (0, Value::Byte(flags)) => m.fire_ticks = i32::from(flags & 1), + // LivingEntity.DATA_LIVING_ENTITY_FLAGS: using an item 1, in the + // off hand 2. The flag starts and stops the use; the client ticks + // the count itself (a bow's pull follows it). + (8, Value::Byte(flags)) => { + m.extra.using_off_hand = flags & 2 != 0; + if flags & 1 == 0 { + m.extra.using_item = 0; + } else if m.extra.using_item == 0 { + m.extra.using_item = 1; + } + } // LivingEntity.DATA_HEALTH_ID: the boss bars and the death // animation follow it. (9, Value::Float(health)) => m.health = *health, + // Mob.DATA_MOB_FLAGS_ID: no AI 1, left-handed 2, aggressive 4. + // Mob.isAggressive draws the illagers' and skeletons' attack + // poses and the vindicator's axe. + (15, Value::Byte(flags)) => { + m.no_ai = flags & 1 != 0; + m.extra.left_handed = flags & 2 != 0; + m.ai.aggressive = flags & 4 != 0; + } (i, Value::Bool(b)) if Some(i) == baby => m.extra.age = if *b { -1 } else { 0 }, _ => {} } @@ -45,6 +64,13 @@ fn apply(m: &mut Mob, values: &[(u8, i32, Value)], builtin: &Builtin) { (Species::Slime | Species::MagmaCube, 18, Value::Int(size)) => { m.extra.size = (*size).clamp(1, 127); } + // Pillager.IS_CHARGING_CROSSBOW: the charge pose (Raider's + // IS_CELEBRATING takes 16). + (Species::Pillager, 17, Value::Bool(charging)) => m.extra.flag = *charging, + // SpellcasterIllager.DATA_SPELL_CASTING_ID: the casting pose. + (Species::Evoker | Species::Illusioner, 17, Value::Byte(spell)) => { + m.extra.variant2 = (*spell).max(0) as u8; + } // Villager.DATA_VILLAGER_DATA and ZombieVillager's (after // Zombie's three): what VillagerProfessionLayer draws. (Species::Villager, 19, Value::Ids(data)) @@ -178,6 +204,19 @@ mod tests { assert_eq!(m.fire_ticks, 0); } + #[test] + fn mob_flags_turn_aggression_on_and_off() { + let Some(mut m) = mob("minecraft:vindicator") else { + return; + }; + apply(&mut m, &[(15, s::BYTE, Value::Byte(4 | 2))]); + assert!(m.ai.aggressive && m.extra.left_handed && !m.no_ai); + assert!(horizon_client::mobs::illager::shows_item(&m)); + apply(&mut m, &[(15, s::BYTE, Value::Byte(0))]); + assert!(!m.ai.aggressive && !m.extra.left_handed); + assert!(!horizon_client::mobs::illager::shows_item(&m)); + } + #[test] fn a_villager_takes_its_type_profession_and_level() { let Some(mut m) = mob("minecraft:villager") else { @@ -218,4 +257,42 @@ mod tests { ); assert_eq!(name(&z), ("desert", "farmer", 3)); } + + #[test] + fn the_mob_flags_drive_the_aggressive_poses() { + let Some(mut m) = mob("minecraft:skeleton") else { + return; + }; + apply(&mut m, &[(15, s::BYTE, Value::Byte(2 | 4))]); + assert!(m.ai.aggressive && m.extra.left_handed && !m.no_ai); + apply(&mut m, &[(15, s::BYTE, Value::Byte(0))]); + assert!(!m.ai.aggressive && !m.extra.left_handed); + } + + #[test] + fn the_living_flags_start_and_stop_a_mob_using_an_item() { + let Some(mut m) = mob("minecraft:skeleton") else { + return; + }; + apply(&mut m, &[(8, s::BYTE, Value::Byte(1))]); + assert_eq!(m.extra.using_item, 1); + assert!(!m.extra.using_off_hand); + // Using in the off hand. + apply(&mut m, &[(8, s::BYTE, Value::Byte(3))]); + assert!(m.extra.using_off_hand); + apply(&mut m, &[(8, s::BYTE, Value::Byte(0))]); + assert_eq!(m.extra.using_item, 0); + } + + #[test] + fn a_pillager_charges_and_a_spellcaster_casts() { + let Some(mut p) = mob("minecraft:pillager") else { + return; + }; + apply(&mut p, &[(17, s::BOOLEAN, Value::Bool(true))]); + assert!(p.extra.flag); + let mut e = mob("minecraft:evoker").unwrap(); + apply(&mut e, &[(17, s::BYTE, Value::Byte(2))]); + assert_eq!(e.extra.variant2, 2); + } } diff --git a/crates/horizon-client/src/game/remote_projectiles.rs b/crates/horizon-client/src/game/remote_projectiles.rs index bc6d0b13e..5c494b9ae 100644 --- a/crates/horizon-client/src/game/remote_projectiles.rs +++ b/crates/horizon-client/src/game/remote_projectiles.rs @@ -2,7 +2,7 @@ //! end crystals on a server's client (`ClientPacketListener.handleAddEntity` //! and `handleSetEntityData`): the server simulates them, the client draws //! them where it says and interpolates their moves. -use super::{FallStep, FallingBlock, Game}; +use super::{FallStep, FallingBlock, Game, fireworks::fireworks_of, view_vector}; use horizon_client::projectiles::{Projectile, ProjectileKind}; use horizon_core::{geometry::Vec3, position::BlockPos}; use horizon_protocol::entity_data::Value; @@ -125,6 +125,37 @@ impl Game<'_> { pub(super) fn remote_projectile_data(&mut self, id: i32, values: &[(u8, i32, Value)]) { let id = id as u32; self.remote_hook_data(id, values); + if self.rockets.iter().any(|r| r.id == id) { + for (index, _, value) in values { + match (index, value) { + // FireworkRocketEntity.DATA_ID_FIREWORKS_ITEM: its + // explosions for `createFireworks`. + (8, Value::Item(stack)) => { + if let Some(stack) = self.to_stack(stack) + && let Some(r) = self.rockets.iter_mut().find(|r| r.id == id) + { + let (_, explosions) = fireworks_of(&stack); + r.explosions = explosions; + r.item = stack; + } + } + // DATA_ATTACHED_TO_TARGET (OptionalInt: the id plus 1). + (9, Value::Id(v)) => { + let v = (*v > 0).then(|| (*v - 1) as u32); + if let Some(r) = self.rockets.iter_mut().find(|r| r.id == id) { + r.attached_to = v; + } + } + // DATA_SHOT_AT_ANGLE. + (10, Value::Bool(a)) => { + if let Some(r) = self.rockets.iter_mut().find(|r| r.id == id) { + r.angled = *a; + } + } + _ => {} + } + } + } if self.potions.has_thrown(id) { // ThrowableItemProjectile.DATA_ITEM_STACK: the potion it is. for (index, _, value) in values { @@ -213,9 +244,30 @@ impl Game<'_> { let id = t.id; t.remote_step(|at| step(id, at)); } + // FireworkRocketEntity.tick: a rocket attached to the gliding local + // player boosts it client-side too (attachedToEntity.isFallFlying). + let me = self + .remote + .as_ref() + .and_then(|r| u32::try_from(r.entity_id).ok()); for r in &mut self.rockets { let id = r.id; r.remote_step(|at| step(id, at)); + if me.is_some() + && r.attached_to == me + && let Some(p) = self.player.as_mut() + && p.fall_flying + { + let look = view_vector(self.yaw, self.pitch); + let m = p.velocity; + p.velocity = Vec3::new( + m.x + look[0] * 0.1 + (look[0] * 1.5 - m.x) * 0.5, + m.y + look[1] * 0.1 + (look[1] * 1.5 - m.y) * 0.5, + m.z + look[2] * 0.1 + (look[2] * 1.5 - m.z) * 0.5, + ); + r.position = p.position; + r.velocity = p.velocity; + } } for c in &mut self.crystals { c.time += 1; diff --git a/crates/horizon-client/src/game/scoreboard.rs b/crates/horizon-client/src/game/scoreboard.rs index 227a09a6c..81c03e288 100644 --- a/crates/horizon-client/src/game/scoreboard.rs +++ b/crates/horizon-client/src/game/scoreboard.rs @@ -332,6 +332,7 @@ impl Game<'_> { return; } self.load_scoreboard(); + host.hub.teams.clone_from(&self.score.board.teams); let view = horizon_client::lan::scoreboard::View::of(&self.score.board); for g in host.hub.guests.iter_mut().filter(|g| g.placed) { if g.scoreboard != view { diff --git a/crates/horizon-client/src/game/screens.rs b/crates/horizon-client/src/game/screens.rs index 44f3fbd51..f8885f83a 100644 --- a/crates/horizon-client/src/game/screens.rs +++ b/crates/horizon-client/src/game/screens.rs @@ -2,6 +2,7 @@ use super::{Game, Screen}; use horizon_client::{ Result, + command::args::GameMode, hud::{HudBatch, rgba}, text::{self, Text}, ui, @@ -82,9 +83,15 @@ fn pause_icons(w: f32, h: f32) -> [(f32, f32, &'static str); 4] { (x0 + 72.0, y, "gui/sprites/pause_menu/player_reporting"), ] } -fn death_buttons(w: f32, h: f32) -> [(f32, f32, &'static str, u8); 2] { +/// DeathScreen.init: a hardcore player can only spectate. +fn death_buttons(w: f32, h: f32, hardcore: bool) -> [(f32, f32, &'static str, u8); 2] { + let respawn = if hardcore { + "deathScreen.spectate" + } else { + "deathScreen.respawn" + }; [ - (w / 2.0 - 100.0, h / 4.0 + 72.0, "deathScreen.respawn", 0), + (w / 2.0 - 100.0, h / 4.0 + 72.0, respawn, 0), ( w / 2.0 - 100.0, h / 4.0 + 96.0, @@ -224,7 +231,12 @@ impl Game<'_> { ), ); } - let title = Text::translate("deathScreen.title", &[]); + let title = if self.level.hardcore { + "deathScreen.title.hardcore" + } else { + "deathScreen.title" + }; + let title = Text::translate(title, &[]); let tw = atlas.font.width_text(&title); ui::text_scaled( batch, @@ -259,7 +271,7 @@ impl Game<'_> { .ui .died_at .is_none_or(|t| t.elapsed().as_millis() >= 1000); - for (x, y, key, _) in death_buttons(w, h) { + for (x, y, key, _) in death_buttons(w, h, self.level.hardcore) { let hover = active && ui::hovered(self.cursor, x, y, 200.0, 20.0); ui::button( batch, @@ -287,7 +299,7 @@ impl Game<'_> { .as_ref() .map_or([1, 1], |r| [r.config.width, r.config.height]); let b = HudBatch::new(size); - let hit = death_buttons(b.width, b.height) + let hit = death_buttons(b.width, b.height, self.level.hardcore) .into_iter() .find(|d| ui::hovered(self.cursor, d.0, d.1, 200.0, 20.0)) .map(|d| d.3); @@ -331,6 +343,11 @@ impl Game<'_> { Some(1) => self.close_world()?, _ => {} } + // ServerGamePacketListenerImpl.handleClientCommand: a hardcore world's + // respawned player only watches. + if hit == Some(0) && self.remote.is_none() && self.level.hardcore { + self.set_mode(GameMode::Spectator); + } if hit.is_some() { self.audio.play_button_click(); } diff --git a/crates/horizon-client/src/game/sign_edit.rs b/crates/horizon-client/src/game/sign_edit.rs index 5a26e3e69..2f50ad9ab 100644 --- a/crates/horizon-client/src/game/sign_edit.rs +++ b/crates/horizon-client/src/game/sign_edit.rs @@ -78,7 +78,7 @@ impl Game<'_> { let Some(g) = host.hub.guest(conn) else { return; }; - let near = super::lan_stations::within_reach(g, p.pos); + let near = g.within_block_range(p.pos, 4.0); let Some(play) = host.play.get_mut(&conn) else { return; }; @@ -248,13 +248,14 @@ impl Game<'_> { let atlas = &self.gui.atlas; let (w, h) = (batch.width, batch.height); batch.fill(atlas, 0.0, 0.0, w, h, rgba(16, 16, 16, 160)); - let title = if sign.hanging { - "Edit Hanging Sign Message" + let (key, fallback) = if sign.hanging { + ("hanging_sign.edit", "Edit Hanging Sign Message") } else { - "Edit Sign Message" + ("sign.edit", "Edit Sign Message") }; - let tw = HudBatch::text_width(atlas, title); - batch.text(atlas, title, (w - tw) / 2.0, 40.0, u32::MAX, true); + let title = self.translate(key, fallback); + let tw = HudBatch::text_width(atlas, &title); + batch.text(atlas, &title, (w - tw) / 2.0, 40.0, u32::MAX, true); let name = self .res .facts() diff --git a/crates/horizon-client/src/game/spill.rs b/crates/horizon-client/src/game/spill.rs index 20dcdf0de..b7283c57b 100644 --- a/crates/horizon-client/src/game/spill.rs +++ b/crates/horizon-client/src/game/spill.rs @@ -373,12 +373,29 @@ impl GameLevel<'_> { let mut neighbour_updates = 0; if side_effects { match kind { - PreRemove::Contents { slots, .. } => { + PreRemove::Contents { slots, furnace } => { let items = entity.as_ref().map_or(&[][..], BlockEntity::slots); for slot in 0..slots { let stack = items.get(slot).cloned().flatten(); self.drop_item_stack(pos, stack); } + // AbstractFurnaceBlockEntity.preRemoveSideEffects: then the + // experience of what it made, at the block's centre. + if furnace && let Some(BlockEntity::Furnace(f)) = &entity { + let at = [ + f64::from(pos.x) + 0.5, + f64::from(pos.y) + 0.5, + f64::from(pos.z) + 0.5, + ]; + for (count, experience) in f.recipe_experience(self.recipes) { + let xp = horizon_client::containers::create_experience( + count, + experience, + &mut self.world.random, + ); + self.award_orbs(at, xp); + } + } } PreRemove::Campfire => { for _ in 0..4 { diff --git a/crates/horizon-client/src/game/startup_menu.rs b/crates/horizon-client/src/game/startup_menu.rs index 94771f9ac..a5d658a4d 100644 --- a/crates/horizon-client/src/game/startup_menu.rs +++ b/crates/horizon-client/src/game/startup_menu.rs @@ -33,6 +33,7 @@ impl StartupMenu { Some(PendingWorldEntry::Server { address: address.clone(), name: options.player_name(), + cookies: None, }) } else { options @@ -125,6 +126,7 @@ impl StartupMenu { Action::Connect { address } => PendingWorldEntry::Server { address, name: player_name, + cookies: None, }, _ => return, }; @@ -764,7 +766,7 @@ mod tests { "Guest".into(), ); assert!( - matches!(menu.pending.take(), Some(PendingWorldEntry::Server { address, name }) + matches!(menu.pending.take(), Some(PendingWorldEntry::Server { address, name, .. }) if address == "localhost:25565" && name == "Guest") ); assert!(menu.pending.take().is_none(), "an entry is consumed once"); diff --git a/crates/horizon-client/src/game/stations.rs b/crates/horizon-client/src/game/stations.rs index ffe56d699..171f860f4 100644 --- a/crates/horizon-client/src/game/stations.rs +++ b/crates/horizon-client/src/game/stations.rs @@ -277,15 +277,10 @@ fn argb(c: i32) -> u32 { } impl Game<'_> { - fn data(&self) -> &stations::StationData { - self.res.stations() - } + /// `Component.translatable`: the selected language (which `lang` layers + /// over `en_us`), the caller's English text last. pub(super) fn translate(&self, key: &str, fallback: &str) -> String { - self.data() - .enchantments - .translate(key) - .unwrap_or(fallback) - .to_owned() + horizon_client::lang::get_or(key, fallback) } fn brewing_open(&self) -> bool { matches!(self.container_kind(), Some(BlockEntity::Brewing(_))) @@ -421,7 +416,8 @@ impl Game<'_> { pub(super) fn anvil_item_name(&self, s: &Station) -> String { match &s.inputs[0] { Some(input) - if input.custom_name().is_none() && s.name == self.pack.names.item(&input.item) => + if input.custom_name().is_none() + && s.name == self.pack.names.display_item(&input.item) => { String::new() } @@ -462,7 +458,7 @@ impl Game<'_> { let name = self.anvil_item_name(&s); let hover = s.inputs[0] .as_ref() - .map(|i| stations::hover_name(&|id| self.pack.names.item(id), i)) + .map(|i| stations::hover_name(&|id| self.pack.names.display_item(id), i)) .unwrap_or_default(); s.anvil = stations::anvil_result( &data.enchantments, @@ -895,7 +891,7 @@ impl Game<'_> { s.anvil_last = s.inputs[0].clone(); s.name = s.inputs[0] .as_ref() - .map(|i| stations::hover_name(&|id| self.pack.names.item(id), i)) + .map(|i| stations::hover_name(&|id| self.pack.names.display_item(id), i)) .unwrap_or_default(); } self.station_changed(); @@ -1096,7 +1092,7 @@ impl Game<'_> { } let (ox, oy) = self.inventory_origin(batch); let local = (self.cursor.0 - ox, self.cursor.1 - oy); - let mut tooltip: Option> = None; + let mut tooltip: Option> = None; { let atlas = &self.gui.atlas; batch.fill( @@ -1345,16 +1341,19 @@ impl Game<'_> { let clue = self .translate("container.enchant.clue", "%s . . . ?") .replace("%s", &name); - let mut lines = vec![(clue, u32::MAX)]; + let mut lines = + vec![(horizon_client::text::Text::literal(clue), u32::MAX)]; if !self.creative { - lines.push((String::new(), u32::MAX)); + lines.push((horizon_client::text::Text::literal(""), u32::MAX)); if level < cost { lines.push(( - self.translate( - "container.enchant.level.requirement", - "Level Requirement: %s", - ) - .replace("%s", &cost.to_string()), + horizon_client::text::Text::literal( + self.translate( + "container.enchant.level.requirement", + "Level Requirement: %s", + ) + .replace("%s", &cost.to_string()), + ), rgba(255, 85, 85, 255), )); } else { @@ -1372,7 +1371,7 @@ impl Game<'_> { .replace("%s", &n.to_string()) }; lines.push(( - lapis, + horizon_client::text::Text::literal(lapis), if gold >= n as i32 { rgba(170, 170, 170, 255) } else { @@ -1391,7 +1390,10 @@ impl Game<'_> { ) .replace("%s", &n.to_string()) }; - lines.push((levels, rgba(170, 170, 170, 255))); + lines.push(( + horizon_client::text::Text::literal(levels), + rgba(170, 170, 170, 255), + )); } } tooltip = Some(lines); @@ -1500,10 +1502,10 @@ impl Game<'_> { ); if inside(local, 65.0, 46.0, 28.0, 21.0) { tooltip = Some(vec![( - self.translate( + horizon_client::text::Text::literal(self.translate( "container.upgrade.error_tooltip", "Item can't be upgraded this way", - ), + )), u32::MAX, )]); } @@ -1630,9 +1632,12 @@ impl Game<'_> { // ---------------------------------------------------------------- tooltips - /// `ItemStack.getTooltipLines` subset: name in rarity colour, potion - /// effects, enchantments (gray, curses red), stored enchantments, trims. - pub(super) fn stack_tooltip(&self, stack: &ItemStack) -> Vec<(String, u32)> { + /// `ItemStack.getTooltipLines` subset: name in rarity colour, lore, + /// potion effects, enchantments (gray, curses red), stored enchantments, trims. + pub(super) fn stack_tooltip( + &self, + stack: &ItemStack, + ) -> Vec<(horizon_client::text::Text, u32)> { let e: &Enchantments = &self.res.stations().enchantments; let props = e.props(&stack.item); let rarity = horizon_client::item_tooltip::rarity( @@ -1648,39 +1653,43 @@ impl Game<'_> { let name = if let Some(n) = stack.custom_name() { n.to_owned() } else if stack.data().is_some_and(|d| d.ominous_banner) { - e.translate("block.minecraft.ominous_banner") - .map_or_else(|| "Ominous Banner".to_owned(), str::to_owned) + self.translate("block.minecraft.ominous_banner", "Ominous Banner") } else if stack.data().and_then(|d| d.potion.as_ref()).is_some() || stack.item.ends_with("potion") || &*stack.item == "minecraft:tipped_arrow" { let key = brewing::potion_name_key(&stack.item, stack.potion()); - e.translate(&key) - .map_or_else(|| self.pack.names.item(&stack.item), str::to_owned) + self.pack + .names + .display_get(&key) + .unwrap_or_else(|| self.pack.names.display_item(&stack.item)) } else { - self.pack.names.item(&stack.item) + self.pack.names.display_item(&stack.item) }; let display = horizon_client::item_tooltip::TooltipDisplay::for_stack(stack); let mut lines = Vec::new(); if display.name_visible(self.screen == Screen::Creative) { - lines.push((name, color)); + lines.push((horizon_client::text::Text::legacy(&name), color)); } - let translate = |k: &str| e.translate(k).map(str::to_owned); + let translate = |k: &str| self.pack.names.display_get(k); + let push = |lines: &mut Vec<(horizon_client::text::Text, u32)>, s: String, c: u32| { + lines.push((horizon_client::text::Text::legacy(&s), c)); + }; // ItemContainerContents.addToTooltip. let hover_name = |s: &ItemStack| { s.custom_name() - .map_or_else(|| self.pack.names.item(&s.item), str::to_owned) + .map_or_else(|| self.pack.names.display_item(&s.item), str::to_owned) }; if display.shows("minecraft:container") { - for line in + for text in horizon_client::item_tooltip::container_lines(stack, &hover_name, &translate) { - lines.push((line, u32::MAX)); + push(&mut lines, text, u32::MAX); } } // Fireworks / FireworkExplosion.addToTooltip. - for line in horizon_client::item_tooltip::firework_lines(stack, &translate) { - lines.push((line, rgba(170, 170, 170, 255))); + for text in horizon_client::item_tooltip::firework_lines(stack, &translate) { + push(&mut lines, text, rgba(170, 170, 170, 255)); } if display.shows("minecraft:potion_contents") && (stack.item.ends_with("potion") || &*stack.item == "minecraft:tipped_arrow") @@ -1690,7 +1699,7 @@ impl Game<'_> { "minecraft:tipped_arrow" => 0.125, _ => 1.0, }; - for (line, cat) in brewing::tooltip(stack.potion(), scale, &|k| { + for (text, cat) in brewing::tooltip(stack.potion(), scale, &|k| { e.translate(k).map(str::to_owned) }) { let c = match cat { @@ -1698,18 +1707,19 @@ impl Game<'_> { brewing::Category::Harmful => rgba(255, 85, 85, 255), brewing::Category::Neutral => rgba(170, 170, 170, 255), }; - lines.push((line, c)); + push(&mut lines, text, c); } } if display.shows("minecraft:trim") && let Some((material, pattern)) = stack.data().and_then(|d| d.trim.clone()) { - lines.push(( + push( + &mut lines, e.translate("item.minecraft.smithing_template.upgrade") .unwrap_or("Upgrade:") .to_owned(), rgba(170, 170, 170, 255), - )); + ); let short = |s: &str| s.trim_start_matches("minecraft:").to_owned(); let p = e .translate(&format!("trim_pattern.minecraft.{}", short(&pattern))) @@ -1717,38 +1727,51 @@ impl Game<'_> { let m = e .translate(&format!("trim_material.minecraft.{}", short(&material))) .map_or_else(|| short(&material), str::to_owned); - lines.push((format!(" {p}"), rgba(170, 170, 170, 255))); - lines.push((format!(" {m}"), rgba(170, 170, 170, 255))); + push(&mut lines, format!(" {p}"), rgba(170, 170, 170, 255)); + push(&mut lines, format!(" {m}"), rgba(170, 170, 170, 255)); } if display.shows("minecraft:stored_enchantments") { for (text, curse) in e.tooltip(stack.stored_enchantments()) { - lines.push(( + push( + &mut lines, text, if curse { rgba(255, 85, 85, 255) } else { rgba(170, 170, 170, 255) }, - )); + ); } } if display.shows("minecraft:enchantments") { for (text, curse) in e.tooltip(stack.enchantments()) { - lines.push(( + push( + &mut lines, text, if curse { rgba(255, 85, 85, 255) } else { rgba(170, 170, 170, 255) }, - )); + ); } } // BannerPatternLayers.addToTooltip: up to six layer names. if display.shows("minecraft:banner_patterns") { for layer in horizon_client::banner::tooltip_layers(stack).iter().take(6) { let key = self.res.stations().banners.layer_key(layer); - lines.push((self.translate(&key, &key), rgba(170, 170, 170, 255))); + push( + &mut lines, + self.translate(&key, &key), + rgba(170, 170, 170, 255), + ); + } + } + // ItemLore.addToTooltip: after the enchantments, before the attributes + // (ItemStack.addDetailsToTooltip). + if display.shows("minecraft:lore") { + for text in horizon_client::item_tooltip::lore_texts(stack) { + lines.push((text, u32::MAX)); } } // Spawner.appendHoverText: what a spawner or trial spawner item spawns. @@ -1762,15 +1785,15 @@ impl Game<'_> { match block_entity_item::spawner_entity(stack) { Some(id) => { let key = format!("entity.{}", id.replacen(':', ".", 1)); - lines.push((self.translate(&key, &id), gray)); + push(&mut lines, self.translate(&key, &id), gray); } None => { - lines.push((String::new(), u32::MAX)); + push(&mut lines, String::new(), u32::MAX); let desc1 = self.translate("block.minecraft.spawner.desc1", "Interact with Spawn Egg:"); - lines.push((desc1, gray)); + push(&mut lines, desc1, gray); let desc2 = self.translate("block.minecraft.spawner.desc2", "Sets Mob Type"); - lines.push((format!(" {desc2}"), rgba(85, 85, 255, 255))); + push(&mut lines, format!(" {desc2}"), rgba(85, 85, 255, 255)); } } } @@ -1798,11 +1821,13 @@ impl Game<'_> { horizon_client::attack_strength::BASE_ATTACK_SPEED, ), ]; - lines.extend(horizon_client::item_tooltip::attribute_lines( + for (text, c) in horizon_client::item_tooltip::attribute_lines( &info.modifiers, Some(player_base), &translate, - )); + ) { + push(&mut lines, text, c); + } } if display.shows("minecraft:damage") && let Some(info) = self.res.items().get(&stack.item) @@ -1810,14 +1835,15 @@ impl Game<'_> { && stack.damage > 0 && self.options_advanced_tooltips() { - lines.push(( + push( + &mut lines, format!( "Durability: {} / {}", info.max_damage - stack.damage, info.max_damage ), u32::MAX, - )); + ); } // ItemStack.OP_NBT_WARNING, for a player with game master permission. if self.ui.registries.permission >= 2 && block_entity_item::op_warning(stack) { @@ -1832,7 +1858,11 @@ impl Game<'_> { "Do not use unless you know the exact contents!", ), ] { - lines.push((self.translate(key, fallback), rgba(255, 85, 85, 255))); + push( + &mut lines, + self.translate(key, fallback), + rgba(255, 85, 85, 255), + ); } } lines @@ -1863,20 +1893,27 @@ impl Game<'_> { self.settings.file.bool("advancedItemTooltips") } /// `TooltipRenderUtil`: dark box with the purple border, first line spaced. - pub(super) fn draw_tooltip_lines( + pub(super) fn draw_tooltip_lines>( &self, batch: &mut HudBatch, - lines: &[(String, u32)], + lines: &[(T, u32)], x: f32, y: f32, ) { if lines.is_empty() { return; } + let lines: Vec<_> = lines + .iter() + .map(|(text, color)| { + let text: horizon_client::text::Text = text.clone().into(); + (text.resolve_legacy(), *color) + }) + .collect(); let atlas = &self.gui.atlas; let w = lines .iter() - .map(|(t, _)| HudBatch::text_width(atlas, t)) + .map(|(t, _)| atlas.font.width_text(t)) .fold(0.0, f32::max); let h = if lines.len() == 1 { 8.0 @@ -1929,7 +1966,7 @@ impl Game<'_> { ); let mut ty = y; for (i, (text, color)) in lines.iter().enumerate() { - batch.text(atlas, text, x, ty, *color, true); + batch.text_styled(atlas, text, x, ty, *color, true); ty += if i == 0 { 12.0 } else { 10.0 }; } } @@ -1946,7 +1983,12 @@ impl Game<'_> { let lines = self.stack_tooltip(&stack); self.draw_tooltip_lines(batch, &lines, self.cursor.0, self.cursor.1); } else if let Some(text) = self.crafter_tooltip(slot) { - self.draw_tooltip_lines(batch, &[(text, u32::MAX)], self.cursor.0, self.cursor.1); + self.draw_tooltip_lines( + batch, + &[(horizon_client::text::Text::literal(text), u32::MAX)], + self.cursor.0, + self.cursor.1, + ); } } diff --git a/crates/horizon-client/src/game/storage.rs b/crates/horizon-client/src/game/storage.rs index 9b28ce26b..bc9b1a6fb 100644 --- a/crates/horizon-client/src/game/storage.rs +++ b/crates/horizon-client/src/game/storage.rs @@ -127,6 +127,52 @@ impl Game<'_> { None => vec![own], } } + /// `Container.stillValidBlockEntity`: the open block container closes once + /// the player's eyes are out of reach of any of its blocks (a 4-block buffer). + pub(super) fn tick_container_reach(&mut self) { + // A server's menu ends when the server closes it. + if self.remote.is_some() { + return; + } + let Some(eye) = self.eye_position() else { + return; + }; + if self + .container + .is_none_or(|k| k.1 == super::minecarts::CART_KEY_Y) + { + return; + } + let range = self.reach() + 4.0; + let near = self.container_parts().iter().all(|&((x, y, z), _)| { + horizon_client::lan::guest::within_block_range(eye, range, [x, y, z]) + }); + if !near { + self.close_container(); + self.screen = super::Screen::Playing; + self.capture(true); + } + } + /// `CraftingMenu.stillValid`: the open crafting table closes once its + /// block is no longer a crafting table or the player's eyes are out of + /// reach (a 4-block buffer). + pub(super) fn tick_craft_table(&mut self) { + // A server's menu ends when the server closes it. + if self.remote.is_some() || self.craft_size != 3 { + return; + } + let (Some(pos), Some(eye)) = (self.craft_table, self.eye_position()) else { + return; + }; + let state = self.world.block(pos[0], pos[1], pos[2]); + let table = &*self.res.facts().get(state).name == "minecraft:crafting_table"; + let range = self.reach() + 4.0; + if !(table && horizon_client::lan::guest::within_block_range(eye, range, pos)) { + self.close_container(); + self.screen = super::Screen::Playing; + self.capture(true); + } + } /// The number of slots of the open container. pub(super) fn container_size(&self) -> usize { self.container_parts().iter().map(|p| p.1).sum() @@ -227,7 +273,8 @@ impl Game<'_> { ); let title = self.translate("container.shulkerBox", "Shulker Box"); batch.text(atlas, &title, ox + 8.0, oy + 6.0, grey, false); - batch.text(atlas, "Inventory", ox + 8.0, oy + 73.0, grey, false); + let inventory = self.translate("container.inventory", "Inventory"); + batch.text(atlas, &inventory, ox + 8.0, oy + 73.0, grey, false); return; } let rows = self.chest_rows().unwrap_or(3); @@ -255,9 +302,10 @@ impl Game<'_> { grey, false, ); + let inventory = self.translate("container.inventory", "Inventory"); batch.text( atlas, - "Inventory", + &inventory, ox + 8.0, oy + 114.0 + rows as f32 * 18.0 - 94.0, grey, diff --git a/crates/horizon-client/src/game/titles.rs b/crates/horizon-client/src/game/titles.rs index 70ade5595..cbae1f6b4 100644 --- a/crates/horizon-client/src/game/titles.rs +++ b/crates/horizon-client/src/game/titles.rs @@ -30,7 +30,7 @@ impl Game<'_> { Some(HeldName { item: stack.item.to_string(), hover_name: Text::literal(item_tooltip::name_line(stack, &|key| { - self.pack.names.get(key).map(str::to_owned) + self.pack.names.display_get(key) })), rarity_color: match rarity { "uncommon" => text::YELLOW, diff --git a/crates/horizon-client/src/game/tweaks.rs b/crates/horizon-client/src/game/tweaks.rs index 681368cab..cec83e248 100644 --- a/crates/horizon-client/src/game/tweaks.rs +++ b/crates/horizon-client/src/game/tweaks.rs @@ -399,7 +399,7 @@ impl Game<'_> { } let views: Vec> = slots.iter().map(|&s| self.slot_view(s)).collect(); let clicks = { - let names = |id: &str| self.pack.names.item(id); + let names = |id: &str| self.pack.names.display_item(id); let rank = |s: &ItemStack| { let order = self.creative_screen.order_of(s).unwrap_or(usize::MAX); (order, hover_name(&names, s)) diff --git a/crates/horizon-client/src/game/wither.rs b/crates/horizon-client/src/game/wither.rs index 91fe91b00..8084425ca 100644 --- a/crates/horizon-client/src/game/wither.rs +++ b/crates/horizon-client/src/game/wither.rs @@ -92,6 +92,8 @@ impl Game<'_> { match who { Some(Who::Player) => { if !self.creative { + // WitherSkull.onHitEntity's source: the wither launched it. + self.combat.projectile_owner = owner; self.mob_hits_player_kind( amount, [p.previous.x, p.previous.y, p.previous.z], diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 31853ad92..cd40ea22e 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -35,7 +35,7 @@ fn entity_shader() -> String { mod entity_batch; mod weather; use entity_batch::EntityBatch; -pub use entity_batch::{SHADOW_OVERLAY, WATER_MASK_OVERLAY}; +pub use entity_batch::{SHADOW_OVERLAY, TRANSLUCENT_PARTICLE_OVERLAY, WATER_MASK_OVERLAY}; /// Window.getAppropriateLineWidth, in framebuffer pixels. pub fn outline_width(width: u32) -> f32 { @@ -516,6 +516,18 @@ fn sprite_split() -> u32 { } /// Sprite arrays terrain.wgsl binds (`sprites`, `sprites_1`...). const SPRITE_ARRAYS: u32 = 8; +/// The layers to give a square texture of `count` layers viewed as a 2D +/// array. wgpu's OpenGL backend picks a texture's target from its layer +/// count when it is created: one layer makes a 2D texture and a multiple of +/// six a cube map, neither of which can be viewed as an array. +fn array_layers(count: u32, gl: bool) -> u32 { + match count { + _ if !gl => count, + 0 | 1 => 2, + n if n % 6 == 0 => n + 1, + n => n, + } +} /// The views of terrain.wgsl's sprite arrays, the unused ones a 1-layer /// stand-in. fn sprite_views( @@ -683,25 +695,31 @@ fn instance_descriptor(backends: wgpu::Backends) -> wgpu::InstanceDescriptor { } /// The backends to open a device on, each tried in turn until one gives a /// device. `api` is the player's choice (`GraphicsApi::with_env` applied); -/// `wgpu_backend`: whether `WGPU_BACKEND` is set, which picks the backends -/// itself. Only Windows has two: Auto tries Vulkan, then DirectX 12 when -/// Vulkan has no adapter (no Vulkan driver) or no device. A native crash in -/// the driver can't be caught in the game; the launcher switches the setting -/// to DirectX 12 after one at the renderer's start (#746). -fn backend_order(api: GraphicsApi, windows: bool, wgpu_backend: bool) -> Vec { - if !windows || wgpu_backend { - return vec![wgpu::Backends::default()]; +/// `os` is `std::env::consts::OS`; `wgpu_backend`: whether `WGPU_BACKEND` is set, +/// which picks the backends itself. Auto tries Vulkan, then DirectX 12 on Windows, +/// then OpenGL (Windows and Linux) for GPUs with neither driver, each when the one +/// before has no surface, adapter or device. A native crash in the driver can't be +/// caught in the game; the launcher switches the setting to DirectX 12 after one at +/// the renderer's start (#746). macOS has Metal only. +fn backend_order(api: GraphicsApi, os: &str, wgpu_backend: bool) -> Vec { + use wgpu::Backends; + if os == "macos" || wgpu_backend { + return vec![Backends::default()]; } + let windows = os == "windows"; match api { - GraphicsApi::Auto => vec![wgpu::Backends::VULKAN, wgpu::Backends::DX12], - GraphicsApi::Vulkan => vec![wgpu::Backends::VULKAN], - GraphicsApi::Dx12 => vec![wgpu::Backends::DX12], + GraphicsApi::Auto if windows => vec![Backends::VULKAN, Backends::DX12, Backends::GL], + GraphicsApi::Auto => vec![Backends::VULKAN, Backends::GL], + GraphicsApi::Dx12 if windows => vec![Backends::DX12], + GraphicsApi::Vulkan | GraphicsApi::Dx12 => vec![Backends::VULKAN], + GraphicsApi::OpenGl => vec![Backends::GL], } } fn backends_name(backends: wgpu::Backends) -> &'static str { match backends { wgpu::Backends::VULKAN => "Vulkan", wgpu::Backends::DX12 => "DirectX 12", + wgpu::Backends::GL => "OpenGL", _ => "graphics", } } @@ -780,7 +798,7 @@ async fn open_device( ) -> Result { let order = backend_order( api.with_env(), - cfg!(windows), + std::env::consts::OS, std::env::var_os("WGPU_BACKEND").is_some(), ); let mut failures = Vec::new(); @@ -793,7 +811,13 @@ async fn open_device( let surface = if offscreen { None } else { - Some(instance.create_surface(window.clone())?) + match instance.create_surface(window.clone()) { + Ok(surface) => Some(surface), + Err(e) => { + failures.push(format!("{name} surface: {e}")); + continue; + } + } }; let adapter = match instance .request_adapter(&wgpu::RequestAdapterOptions { @@ -863,14 +887,14 @@ async fn open_device( /// Vulkan is loaded at run time (the game links no graphics library), so a /// missing driver shows here rather than as a missing-DLL dialog. #[cfg(target_os = "windows")] -const NO_GPU_DRIVER: &str = "No graphics driver with Vulkan or DirectX 12 found: Horizon needs a GPU \ - driver with one of them. Install the latest driver for your NVIDIA, AMD or Intel GPU and \ +const NO_GPU_DRIVER: &str = "No graphics driver with Vulkan, DirectX 12 or OpenGL found: Horizon needs \ + a GPU driver with one of them. Install the latest driver for your NVIDIA, AMD or Intel GPU and \ start the game again"; #[cfg(target_os = "macos")] const NO_GPU_DRIVER: &str = "No Metal graphics device found"; #[cfg(not(any(target_os = "windows", target_os = "macos")))] -const NO_GPU_DRIVER: &str = "No Vulkan graphics driver found: Horizon needs the Vulkan loader \ - (libvulkan.so.1) and a Vulkan driver for your GPU (Mesa's, or NVIDIA's)"; +const NO_GPU_DRIVER: &str = "No Vulkan or OpenGL graphics driver found: Horizon needs the Vulkan loader \ + (libvulkan.so.1) and a Vulkan driver for your GPU (Mesa's, or NVIDIA's), or OpenGL through EGL"; /// Logs a step of the renderer's start, for a native crash in the graphics /// driver: it leaves no panic or crash report, so the log's last line is the /// only sign of where the game stopped. The log file is written unbuffered; @@ -1300,13 +1324,17 @@ impl Renderer { PreparedTextures::new(sprite_pixels, sprite_size, layers, sprite_mips, forcefield) }); let mut mip_levels = 1; + let gl = adapter.get_info().backend == wgpu::Backend::Gl; let sprite_textures: Vec = prepared .sprites .into_iter() .enumerate() - .map(|(array, (mips, levels))| { + .map(|(array, (mut mips, levels))| { let first = array as u32 * split; - let count = layers.saturating_sub(first).clamp(1, split); + let used = layers.saturating_sub(first).clamp(1, split); + let count = array_layers(used, gl); + // The extra layers are empty. + mips.resize(mips.len() / used as usize * count as usize, 0); mip_levels = levels; device.create_texture_with_data( &queue, @@ -1340,6 +1368,7 @@ impl Renderer { sprite_mips, &sprite_textures, split, + animate::SpriteAnimator::compute(&adapter, &device), ); let sprite_unused = device .create_texture(&wgpu::TextureDescriptor { @@ -1347,7 +1376,7 @@ impl Renderer { size: wgpu::Extent3d { width: 1, height: 1, - depth_or_array_layers: 1, + depth_or_array_layers: array_layers(1, gl), }, mip_level_count: 1, sample_count: 1, @@ -2057,16 +2086,15 @@ impl Renderer { // One per blend and `.png.mcmeta` sampling (screen_effects::SAMPLE_*): // the shader is specialised, so a pixel pays for none of the others. let screen_pipelines: [wgpu::RenderPipeline; 8] = std::array::from_fn(|i| { - let (entry, blend) = if i & 4 == 0 { - ("fs_screen", alpha_blend) - } else { - ("fs_vignette", vignette_blend) + let blur = i as u32 & crate::screen_effects::SAMPLE_BLUR != 0; + let (entry, blend) = match (i & 4 == 0, blur) { + (true, false) => ("fs_screen", alpha_blend), + (true, true) => ("fs_screen_blur", alpha_blend), + (false, false) => ("fs_vignette", vignette_blend), + (false, true) => ("fs_vignette_blur", vignette_blend), }; - let on = |bit: u32| f64::from(u8::from(i as u32 & bit != 0)); - let constants = [ - ("BLUR", on(crate::screen_effects::SAMPLE_BLUR)), - ("CLAMP", on(crate::screen_effects::SAMPLE_CLAMP)), - ]; + let clamp = i as u32 & crate::screen_effects::SAMPLE_CLAMP != 0; + let constants = [("CLAMP", f64::from(u8::from(clamp)))]; let compilation_options = wgpu::PipelineCompilationOptions { constants: &constants, ..Default::default() @@ -2154,15 +2182,26 @@ impl Renderer { ) }); let sorted_indices = arena::Arena::indices(&device, 1 << 16, flat.is_some()); + let multi_draw = device + .features() + .contains(wgpu::Features::INDIRECT_FIRST_INSTANCE) + && adapter + .get_downlevel_capabilities() + .flags + .contains(wgpu::DownlevelFlags::INDIRECT_EXECUTION); + // Without indirect draws (OpenGL before 4.3 without compute shaders) + // even creating an indirect buffer fails; one draw per section reads + // `draws` on the CPU instead. let indirect = device.create_buffer(&wgpu::BufferDescriptor { label: Some("Section draws"), size: 1024 * 20, - usage: wgpu::BufferUsages::INDIRECT | wgpu::BufferUsages::COPY_DST, + usage: if multi_draw { + wgpu::BufferUsages::INDIRECT | wgpu::BufferUsages::COPY_DST + } else { + wgpu::BufferUsages::COPY_DST + }, mapped_at_creation: false, }); - let multi_draw = device - .features() - .contains(wgpu::Features::INDIRECT_FIRST_INSTANCE); // HORIZON_GPU_CULL=0 turns occlusion culling off, `all` culls every // frame however few its draws and little it hides, `shader` tests in // the shader as on Metal (A/B checks). @@ -3439,6 +3478,7 @@ impl Renderer { translucent_crumbling_quads, water_mask_quads, shadow_quads, + translucent_particle_quads, ] = self.entity_batch.update( frame.entities, frame.additive, @@ -3450,7 +3490,8 @@ impl Renderer { + crumbling_quads + translucent_crumbling_quads + water_mask_quads - + shadow_quads; + + shadow_quads + + translucent_particle_quads; let cloud_quads = if let Some(input) = &frame.clouds { let (rebuilt, uniform) = crate::spans::time("clouds", || self.cloud_mesh.prepare(input)); @@ -3849,7 +3890,7 @@ impl Renderer { pass.set_vertex_buffer(0, self.entity_buffer.slice(..)); pass.draw_indexed( 0..shadow_quads * 6, - ((world_quads - shadow_quads) * 4) as i32, + ((world_quads - translucent_particle_quads - shadow_quads) * 4) as i32, 0..1, ); pass.set_bind_group(0, &self.group, &[]); @@ -3917,6 +3958,22 @@ impl Renderer { ); } } + // LevelRenderer.executeTranslucentAfterTerrain: vanilla's + // translucent particle layer draws after translucent terrain — + // the quads write depth, so before it they would hide the glass + // or water behind them. + if translucent_particle_quads > 0 { + pass.set_pipeline(&self.entity_pipeline); + pass.set_bind_group(0, &self.entity_group, &[]); + pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); + pass.set_vertex_buffer(0, self.entity_buffer.slice(..)); + pass.draw_indexed( + 0..translucent_particle_quads * 6, + ((world_quads - translucent_particle_quads) * 4) as i32, + 0..1, + ); + pass.set_bind_group(0, &self.group, &[]); + } // LevelRenderer.executeClassicTransparency: translucent targets' // outlines follow terrain, so its blending cannot wash them out. if outline_edges > 0 && frame.outline_after_terrain { @@ -4423,14 +4480,23 @@ mod tests { normal: [0; 4], }; 4] }; - let entities: Vec<_> = [(0, 0), (14, 9), (4, 1), (11, 2), (7, 3), (4, 4), (12, 7)] - .into_iter() - .flat_map(|(layer, id)| quad(layer, id)) - .collect(); + let entities: Vec<_> = [ + (0, 0), + (14, 9), + (4, 1), + (11, 2), + (15, 10), + (7, 3), + (4, 4), + (12, 7), + ] + .into_iter() + .flat_map(|(layer, id)| quad(layer, id)) + .collect(); let mut batch = super::EntityBatch::default(); let counts = batch.update(&entities, &quad(3, 5), &quad(0, 6), &quad(0, 8)); let vertices = &batch.combined; - assert_eq!(counts, [4, 1, 1, 0, 1]); + assert_eq!(counts, [4, 1, 1, 0, 1, 1]); assert_eq!( vertices .as_chunks::<4>() @@ -4438,29 +4504,31 @@ mod tests { .iter() .map(|q| q[0].light) .collect::>(), - [0, 1, 3, 4, 2, 7, 9, 5, 6, 8] + [0, 1, 3, 4, 2, 7, 9, 10, 5, 6, 8] ); } #[test] - fn windows_tries_vulkan_then_directx_12() { + fn auto_tries_vulkan_then_directx_12_then_opengl() { use super::{GraphicsApi, backend_order}; use wgpu::Backends; + let order = |api, os| backend_order(api, os, false); assert_eq!( - backend_order(GraphicsApi::Auto, true, false), - [Backends::VULKAN, Backends::DX12] + order(GraphicsApi::Auto, "windows"), + [Backends::VULKAN, Backends::DX12, Backends::GL] ); + assert_eq!(order(GraphicsApi::Vulkan, "windows"), [Backends::VULKAN]); + assert_eq!(order(GraphicsApi::Dx12, "windows"), [Backends::DX12]); + assert_eq!(order(GraphicsApi::OpenGl, "windows"), [Backends::GL]); assert_eq!( - backend_order(GraphicsApi::Vulkan, true, false), - [Backends::VULKAN] + order(GraphicsApi::Auto, "linux"), + [Backends::VULKAN, Backends::GL] ); - assert_eq!( - backend_order(GraphicsApi::Dx12, true, false), - [Backends::DX12] - ); - // WGPU_BACKEND picks the backends itself; other systems have one. + assert_eq!(order(GraphicsApi::Dx12, "linux"), [Backends::VULKAN]); + assert_eq!(order(GraphicsApi::OpenGl, "linux"), [Backends::GL]); + // WGPU_BACKEND picks the backends itself; macOS has Metal only. for api in GraphicsApi::ALL { - assert_eq!(backend_order(api, true, true), [Backends::default()]); - assert_eq!(backend_order(api, false, false), [Backends::default()]); + assert_eq!(backend_order(api, "windows", true), [Backends::default()]); + assert_eq!(order(api, "macos"), [Backends::default()]); } } /// Every WGSL shader file translates to HLSL as wgpu's DirectX 12 backend @@ -4520,6 +4588,13 @@ mod tests { } } #[test] + fn gl_sprite_arrays_are_neither_2d_textures_nor_cube_maps() { + for (count, gl) in [(1, 2), (2, 2), (5, 5), (6, 7), (12, 13), (2048, 2048)] { + assert_eq!(super::array_layers(count, true), gl); + assert_eq!(super::array_layers(count, false), count); + } + } + #[test] fn entity_shader_validates_with_its_overlay_constants() { let module = naga::front::wgsl::parse_str(&super::entity_shader()).unwrap(); naga::valid::Validator::new( diff --git a/crates/horizon-client/src/gfx/animate.rs b/crates/horizon-client/src/gfx/animate.rs index b7c44429b..5a70f3b0f 100644 --- a/crates/horizon-client/src/gfx/animate.rs +++ b/crates/horizon-client/src/gfx/animate.rs @@ -10,6 +10,10 @@ //! frames. Blending on the CPU at the array's size and copying every changed //! sprite level by level (a copy command each, a blit each in Mesa) was about //! a fifth of the render thread on an integrated GPU. +//! +//! Without compute shaders (OpenGL before 4.3, the last fallback) the same +//! jobs run on the CPU, the same integer mix as `animate.wgsl`, and each +//! changed level is copied into its layer. use super::upload::Uploads; use crate::mipmap::Settings; use crate::sprites::{FrameBlend, SpriteAnimation}; @@ -38,17 +42,32 @@ struct Sprite { struct Group { /// Where its jobs start in `SpriteAnimator::jobs`, in bytes. offset: u64, - /// One bind group per mip level of the array. + /// One bind group per mip level of the array (none on the CPU). levels: Vec, /// This tick's jobs. jobs: Vec, + /// Its sprite array and its frames, for the CPU. + array: usize, + buffer: usize, +} + +/// Where the jobs run. +enum Writer { + Gpu { + jobs: wgpu::Buffer, + pipeline: wgpu::ComputePipeline, + }, + /// Every frames buffer's words, and the sprite arrays to copy into. + Cpu { + frames: Vec>, + textures: Vec, + }, } pub(super) struct SpriteAnimator { sprites: Vec, groups: Vec, - jobs: wgpu::Buffer, - pipeline: wgpu::ComputePipeline, + writer: Writer, /// Edge of the arrays' layers. size: u32, /// The layers the last `animate` rewrote, counted over all arrays. @@ -57,7 +76,8 @@ pub(super) struct SpriteAnimator { impl SpriteAnimator { /// `textures` are the sprite arrays, `split` layers each, `size` pixels - /// wide; `animations` hold their frames at that size. + /// wide; `animations` hold their frames at that size. `compute`: whether + /// the device says it runs compute shaders writing storage textures. pub fn new( device: &wgpu::Device, animations: &[SpriteAnimation], @@ -65,11 +85,18 @@ impl SpriteAnimator { settings: &[Settings], textures: &[wgpu::Texture], split: u32, + compute: bool, ) -> Self { + let pipeline = if compute { pipeline(device) } else { None }; let limits = device.limits(); - let limit = limits - .max_storage_buffer_binding_size - .min(limits.max_buffer_size); + let limit = if pipeline.is_some() { + limits + .max_storage_buffer_binding_size + .min(limits.max_buffer_size) + } else { + // Word offsets. + u64::from(u32::MAX) * 4 + }; let mut frames: Vec> = Vec::new(); // Each group's sprite array and frames buffer. let mut keys: Vec<(u32, usize)> = Vec::new(); @@ -119,6 +146,29 @@ impl SpriteAnimator { group, }); } + let Some(pipeline) = pipeline else { + let groups = keys + .iter() + .zip(&counts) + .map(|(&(array, buffer), &count)| Group { + offset: 0, + levels: Vec::new(), + jobs: Vec::with_capacity(count * JOB_BYTES), + array: array as usize, + buffer, + }) + .collect(); + return Self { + sprites, + groups, + writer: Writer::Cpu { + frames, + textures: textures.to_vec(), + }, + size, + changed: Vec::new(), + }; + }; let frames: Vec<_> = frames .iter() .map(|data| { @@ -142,18 +192,6 @@ impl SpriteAnimator { usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false, }); - let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { - label: Some("Animated sprites"), - source: wgpu::ShaderSource::Wgsl(include_str!("animate.wgsl").into()), - }); - let pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { - label: Some("Animated sprites"), - layout: None, - module: &module, - entry_point: Some("animate"), - compilation_options: Default::default(), - cache: None, - }); let layout = pipeline.get_bind_group_layout(0); let groups = keys .iter() @@ -198,19 +236,33 @@ impl SpriteAnimator { offset, levels, jobs: Vec::with_capacity(count * JOB_BYTES), + array: array as usize, + buffer, } }) .collect(); Self { sprites, groups, - jobs, - pipeline, + writer: Writer::Gpu { jobs, pipeline }, size, changed: Vec::new(), } } + /// Whether the jobs can run on the GPU: compute shaders writing a + /// storage texture and reading two storage buffers (not OpenGL before + /// 4.3). + pub fn compute(adapter: &wgpu::Adapter, device: &wgpu::Device) -> bool { + let limits = device.limits(); + adapter + .get_downlevel_capabilities() + .flags + .contains(wgpu::DownlevelFlags::COMPUTE_SHADERS) + && limits.max_storage_textures_per_shader_stage >= 1 + && limits.max_storage_buffers_per_shader_stage >= 2 + } + /// The layers the last `animate` rewrote (for the shader pack atlas). pub fn changed(&self) -> &[u32] { &self.changed @@ -227,14 +279,47 @@ impl SpriteAnimator { if self.collect(tick) == 0 { return; } + let (jobs, pipeline) = match &self.writer { + Writer::Gpu { jobs, pipeline } => (jobs, pipeline), + Writer::Cpu { frames, textures } => { + for group in &mut self.groups { + let (frames, texture) = (&frames[group.buffer], &textures[group.array]); + for job in group.jobs.as_chunks::().0 { + let (words, _) = job.as_chunks::<4>(); + let job: [u32; 5] = std::array::from_fn(|i| u32::from_le_bytes(words[i])); + for level in 0..texture.mip_level_count() { + let width = self.size >> level; + uploads.texture( + encoder, + wgpu::TexelCopyTextureInfo { + texture, + mip_level: level, + origin: wgpu::Origin3d { + x: 0, + y: 0, + z: job[0], + }, + aspect: wgpu::TextureAspect::All, + }, + width, + width, + &mix_level(frames, job, width), + ); + } + } + group.jobs.clear(); + } + return; + } + }; for group in &self.groups { - uploads.buffer(encoder, &self.jobs, group.offset, &group.jobs); + uploads.buffer(encoder, jobs, group.offset, &group.jobs); } let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor { label: Some("Animated sprites"), timestamp_writes: None, }); - pass.set_pipeline(&self.pipeline); + pass.set_pipeline(pipeline); for group in &mut self.groups { let count = (group.jobs.len() / JOB_BYTES) as u32; if count == 0 { @@ -278,6 +363,62 @@ impl SpriteAnimator { } } +/// `animate.wgsl`'s pipeline, unless the device fails to make it: an OpenGL +/// driver can list compute shaders among its extensions while wgpu writes +/// the GLSL version of its core version, which has none (Mesa's +/// `MESA_GL_VERSION_OVERRIDE`, older drivers). +fn pipeline(device: &wgpu::Device) -> Option { + let validation = device.push_error_scope(wgpu::ErrorFilter::Validation); + let internal = device.push_error_scope(wgpu::ErrorFilter::Internal); + let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some("Animated sprites"), + source: wgpu::ShaderSource::Wgsl(include_str!("animate.wgsl").into()), + }); + let pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { + label: Some("Animated sprites"), + layout: None, + module: &module, + entry_point: Some("animate"), + compilation_options: Default::default(), + cache: None, + }); + let internal = pollster::block_on(internal.pop()); + match internal.or(pollster::block_on(validation.pop())) { + None => Some(pipeline), + Some(e) => { + log::warn!("Horizon renderer: animated sprites drawn on the CPU ({e})"); + None + } + } +} + +/// `animate.wgsl` on the CPU: one `width` wide level of a job +/// (`[layer, current, next, millis, edge]`) from `frames`' chains. +fn mix_level(frames: &[u8], [_, current, next, millis, edge]: [u32; 5], width: u32) -> Vec { + let mut out = Vec::with_capacity((width * width * 4) as usize); + for y in 0..width { + for x in 0..width { + let texel = if width >= edge { + let scale = width / edge; + y / scale * edge + x / scale + } else { + 4 * (edge * edge - width * width) / 3 + y * width + x + }; + let at = |chain: u32| ((chain + texel) * 4) as usize; + let a = &frames[at(current)..at(current) + 4]; + if millis == 0 { + out.extend_from_slice(a); + continue; + } + let b = &frames[at(next)..at(next) + 4]; + out.extend(a.iter().zip(b).map(|(&a, &b)| { + ((u32::from(a) * (1000 - millis) + u32::from(b) * millis + 500) / 1000) as u8 + })); + } + } + out +} + /// The mip chain a frame's layer is made of, and its first level's edge. /// `mipmap::generate` makes a sprite scaled up by whole pixels to the /// array's size from its own size's chain, its first level repeated into the @@ -512,6 +653,16 @@ mod tests { #[test] fn gpu_layers_match_the_cpu_chains_every_tick() { + layers_match_the_cpu_chains_every_tick(true); + } + + /// Without compute shaders (OpenGL before 4.3): the same layers. + #[test] + fn copied_layers_match_the_cpu_chains_every_tick() { + layers_match_the_cpu_chains_every_tick(false); + } + + fn layers_match_the_cpu_chains_every_tick(compute: bool) { let Some((device, queue)) = device() else { return; }; @@ -552,12 +703,15 @@ mod tests { sample_count: 1, dimension: wgpu::TextureDimension::D2, format: wgpu::TextureFormat::Rgba8Unorm, - usage: wgpu::TextureUsages::STORAGE_BINDING | wgpu::TextureUsages::COPY_SRC, + usage: wgpu::TextureUsages::STORAGE_BINDING + | wgpu::TextureUsages::COPY_SRC + | wgpu::TextureUsages::COPY_DST, view_formats: &[], }) }) .collect(); - let mut animator = SpriteAnimator::new(&device, &animations, size, &mips, &textures, split); + let mut animator = + SpriteAnimator::new(&device, &animations, size, &mips, &textures, split, compute); let mut uploads = Uploads::new(&device); let mut writes = Vec::new(); for tick in [0, 0, 1, 2, 3, 4, 7, 7, 12, 30] { diff --git a/crates/horizon-client/src/gfx/entity_batch.rs b/crates/horizon-client/src/gfx/entity_batch.rs index 56ffb04b9..8153699fc 100644 --- a/crates/horizon-client/src/gfx/entity_batch.rs +++ b/crates/horizon-client/src/gfx/entity_batch.rs @@ -3,13 +3,16 @@ use super::Vertex; /// Entity materials by a quad's overlay (`Vertex::layer`), in draw order: /// ordinary entities, breaking cracks (11), translucent breaking cracks (12), -/// the boat water mask (13, `RenderPipelines.WATER_MASK`) and entity shadows -/// (14, `RenderPipelines.ENTITY_SHADOW`). -const MATERIALS: usize = 5; +/// the boat water mask (13, `RenderPipelines.WATER_MASK`), entity shadows +/// (14, `RenderPipelines.ENTITY_SHADOW`) and translucent-layer particles (15, +/// drawn after translucent terrain: `SubmitNodeCollection.submitQuadParticleGroup`). +const MATERIALS: usize = 6; /// The overlay of depth-only quads that hide water (`RenderTypes.waterMask`). pub const WATER_MASK_OVERLAY: u32 = 13; /// The overlay of entity shadow quads (`RenderTypes.entityShadow`). pub const SHADOW_OVERLAY: u32 = 14; +/// The overlay of translucent-layer particles (`SingleQuadParticle.Layer.translucent`). +pub const TRANSLUCENT_PARTICLE_OVERLAY: u32 = 15; #[derive(Default)] pub(super) struct EntityBatch { @@ -41,6 +44,7 @@ impl EntityBatch { 12 => 2, WATER_MASK_OVERLAY => 3, SHADOW_OVERLAY => 4, + TRANSLUCENT_PARTICLE_OVERLAY => 5, _ => 0, }; self.buckets[material][counts[material]] = index; @@ -92,13 +96,14 @@ mod tests { ) -> (Vec, [u32; MATERIALS]) { let mut combined = Vec::with_capacity(entities.len() + additive.len() + hand.len()); let mut counts = [0; MATERIALS]; - for (i, layer) in [0, 11, 12, 13, 14].into_iter().enumerate() { + for (i, layer) in [0, 11, 12, 13, 14, 15].into_iter().enumerate() { for quad in entities.as_chunks::<4>().0 { let material = match quad[0].layer { 11 => 11, 12 => 12, 13 => 13, 14 => 14, + 15 => 15, _ => 0, }; if material == layer { @@ -118,7 +123,7 @@ mod tests { pos: [index as f32, 2.0, 3.0], uv: [index as f32, 5.0], layer: if mixed { - [7, 11, 14, 12, 4, 13, 0][index / 4 % 7] + [7, 11, 14, 12, 4, 13, 15, 0][index / 4 % 8] } else { 0 }, diff --git a/crates/horizon-client/src/graphics.rs b/crates/horizon-client/src/graphics.rs index 3cf1402d2..d3318aab2 100644 --- a/crates/horizon-client/src/graphics.rs +++ b/crates/horizon-client/src/graphics.rs @@ -98,19 +98,27 @@ impl PresentMode { /// macOS). Applies at the next start. #[derive(Clone, Copy, Debug, PartialEq, Eq, Default)] pub enum GraphicsApi { - /// Vulkan, and DirectX 12 when Vulkan has no adapter or device. + /// Vulkan, then DirectX 12 (Windows), then OpenGL when neither has a surface, adapter or device. #[default] Auto, Vulkan, Dx12, + /// OpenGL, for GPUs with no Vulkan or DirectX 12 driver (Windows and Linux). + OpenGl, } impl GraphicsApi { - pub const ALL: [GraphicsApi; 3] = [GraphicsApi::Auto, GraphicsApi::Vulkan, GraphicsApi::Dx12]; + pub const ALL: [GraphicsApi; 4] = [ + GraphicsApi::Auto, + GraphicsApi::Vulkan, + GraphicsApi::Dx12, + GraphicsApi::OpenGl, + ]; pub fn name(self) -> &'static str { match self { GraphicsApi::Auto => "auto", GraphicsApi::Vulkan => "vulkan", GraphicsApi::Dx12 => "dx12", + GraphicsApi::OpenGl => "opengl", } } pub fn label(self) -> &'static str { @@ -118,6 +126,7 @@ impl GraphicsApi { GraphicsApi::Auto => "Auto", GraphicsApi::Vulkan => "Vulkan", GraphicsApi::Dx12 => "DirectX 12", + GraphicsApi::OpenGl => "OpenGL", } } /// The API a name stands for, ignoring case; `None` for anything else. @@ -126,7 +135,7 @@ impl GraphicsApi { .into_iter() .find(|api| api.name().eq_ignore_ascii_case(s.trim())) } - /// `HORIZON_GRAPHICS_API` (auto, vulkan or dx12) if set, else the setting. + /// `HORIZON_GRAPHICS_API` (auto, vulkan, dx12 or opengl) if set, else the setting. pub fn with_env(self) -> Self { std::env::var("HORIZON_GRAPHICS_API") .ok() @@ -376,14 +385,18 @@ impl Graphics { if self.workers > 0 { return self.workers as usize; } - // The pool never takes every core. The render thread and the - // integrated server thread (single player runs on its own server - // thread by default) each need one, and a pool that fills every core - // starves them: the frame rate collapses to single digits while the - // workers saturate the CPU and its memory bandwidth. + // Vanilla's count (Util.maxAllowedExecutorThreads: every core but + // one). The pool never takes every core: a pool that fills them all + // starves the render and server threads, and the frame rate collapses + // while the workers saturate the CPU and its memory bandwidth. One + // core left is enough with the workers behind those two threads + // (horizon_threads: niced on Linux; on macOS and Windows the server + // and render threads are raised above four cores, where vanilla + // raises its server thread). Leaving two idled a core of a 4-thread + // laptop while a world loaded: a teleport there took ~15% longer. let auto = std::thread::available_parallelism() .map_or(2, |n| n.get()) - .saturating_sub(2) + .saturating_sub(1) .max(1); if self.battery { auto.div_ceil(2) } else { auto } } @@ -546,11 +559,11 @@ mod tests { } #[test] - fn automatic_workers_leave_cores_for_the_render_and_server_threads() { + fn automatic_workers_leave_a_core_as_vanilla_does() { let g = Graphics::default(); assert_eq!(g.workers, 0); let cores = std::thread::available_parallelism().map_or(2, |n| n.get()); - assert_eq!(g.worker_threads(), cores.saturating_sub(2).max(1)); + assert_eq!(g.worker_threads(), cores.saturating_sub(1).max(1)); // A manual count is the player's own choice, kept as asked. let manual = Graphics { workers: 3, ..g }; assert_eq!(manual.worker_threads(), 3); diff --git a/crates/horizon-client/src/hud.rs b/crates/horizon-client/src/hud.rs index c3a165792..2b6acda41 100644 --- a/crates/horizon-client/src/hud.rs +++ b/crates/horizon-client/src/hud.rs @@ -3,7 +3,7 @@ //! GUI scale is applied when drawing). use crate::Result; use std::{ - collections::HashMap, + collections::{HashMap, HashSet}, fs, path::{Path, PathBuf}, }; @@ -58,6 +58,9 @@ pub struct HudAtlas { pub width: u32, pub height: u32, pub sprites: HashMap, + /// Sprites with a 0 < alpha < 255 texel (`SpriteContents.transparency().hasTranslucent`), + /// which puts their particles on vanilla's translucent layer. + pub translucent: HashSet, /// Sprites whose `.png.mcmeta` sets `blur` or `clamp`; the others /// sample as vanilla's defaults (nearest, repeat). pub flags: HashMap, @@ -78,7 +81,10 @@ pub const MAP_SLOTS: usize = 256; /// The atlas slot the minimap extra draws into. pub const MINIMAP_SPRITE: &str = "horizon/minimap"; /// Atlas slots for downloaded player skins (`skins/`), 64x64 each. -pub const SKIN_SLOTS: usize = 32; +/// Vanilla registers a texture per skin. Here a skin evicted from its slot +/// is not stored again, and its player falls back to the default skin, so +/// the slots must cover a busy server's player list. +pub const SKIN_SLOTS: usize = 128; /// Reused by the visible rows of the classic world selector. pub const WORLD_ICON_SLOTS: usize = 32; /// Atlas slots for the pack icons on the pack screen, one per icon drawn @@ -459,7 +465,16 @@ impl HudAtlas { let height = (glyph_region + GLYPH_ROWS).next_multiple_of(1024); let mut pixels = vec![0u8; (width * height * 4) as usize]; let mut sprites = HashMap::new(); + let mut translucent = HashSet::new(); for ((name, w, h, data), (px, py)) in images.iter().zip(placements) { + if data + .as_chunks::<4>() + .0 + .iter() + .any(|p| p[3] != 0 && p[3] != 255) + { + translucent.insert(name.clone()); + } for row in 0..*h { let src = &data[(row * w * 4) as usize..((row + 1) * w * 4) as usize]; let dst = (((py + row) * width + px) * 4) as usize; @@ -524,6 +539,7 @@ impl HudAtlas { width, height, sprites, + translucent, flags, fire_animations, font, @@ -893,6 +909,9 @@ impl HudBatch { color: u32, shadow: bool, ) -> f32 { + // StringDecomposer.iterateFormatted inside Font.drawInternal: + // `§` codes inside a span restyle what follows and are not drawn. + let text = text.resolved(); let font = &atlas.font; let alpha = (color >> 24) as u8; let mut glyphs = Vec::new(); diff --git a/crates/horizon-client/src/item_sync.rs b/crates/horizon-client/src/item_sync.rs new file mode 100644 index 000000000..930304ec4 --- /dev/null +++ b/crates/horizon-client/src/item_sync.rs @@ -0,0 +1,355 @@ +//! Item stacks on the wire (`ItemStack.OPTIONAL_STREAM_CODEC`): the component +//! patch read back as vanilla's `components` compound, so the decoder the saved +//! and command item NBT use fills every field. Both sides use it: a client for +//! what its server sends, a server for a creative client's +//! `SetCreativeModeSlot`. The other direction is `lan::guest::stack_to_network`. +use crate::items::ItemStack; +use horizon_protocol::item::{Component, Profile, Stack, component}; +use horizon_protocol::registry::{Builtin, Dynamic}; +use horizon_world::nbt::Tag; +use std::sync::Arc; + +/// A stack from the wire: its item by the built-in registry's id, its +/// components by the synced registries' (enchantments, trims). +pub fn from_network(s: &Stack, builtin: &Builtin, synced: &Dynamic) -> ItemStack { + let mut out = ItemStack { + item: item_name(builtin, s.item), + count: s.count.max(0) as u32, + damage: 0, + extra: None, + }; + let components = components_tag(s, builtin, synced); + let (damage, data) = crate::item_data::decode_components(components.as_ref()); + out.damage = damage; + if let Some(data) = data { + *out.data_mut() = *data; + } + out.tidy(); + out +} + +fn item_name(builtin: &Builtin, id: i32) -> Arc { + Arc::from( + builtin + .get("minecraft:item") + .and_then(|i| i.name(id)) + .unwrap_or("minecraft:air"), + ) +} + +/// The item's patched components as vanilla's `components` compound. +fn components_tag(s: &Stack, builtin: &Builtin, synced: &Dynamic) -> Option { + let name = |id: i32| builtin.get("minecraft:data_component_type")?.name(id); + let mut entries: Vec<(&str, Tag)> = Vec::new(); + for (kind, c) in &s.added { + let Some(key) = name(*kind) else { + continue; + }; + let value = match c { + // Read past, not decoded: the item keeps its prototype's value. + Component::Raw(_) => continue, + // Trim materials and patterns are synced registries, as enchantments are. + Component::Trim { material, pattern } => { + let (Some(material), Some(pattern)) = ( + synced.name("minecraft:trim_material", *material), + synced.name("minecraft:trim_pattern", *pattern), + ) else { + continue; + }; + Tag::compound([ + ("material", Tag::String(material.into())), + ("pattern", Tag::String(pattern.into())), + ]) + } + Component::Int(v) if *kind == component::RARITY => { + let rarity = ["common", "uncommon", "rare", "epic"]; + Tag::String(rarity.get(*v as usize).copied().unwrap_or("common").into()) + } + Component::Int(v) => Tag::Int(*v), + Component::Unit => Tag::Compound(Default::default()), + Component::Bool(v) => Tag::Byte(i8::from(*v)), + Component::Text(t) | Component::Nbt(t) => t.clone(), + Component::Ident(v) => Tag::String(v.as_str().into()), + Component::Lore(lines) => Tag::List(lines.clone()), + Component::WritableBook(pages) => Tag::compound([( + "pages", + Tag::List( + pages + .iter() + .map(|p| Tag::String(p.as_str().into())) + .collect(), + ), + )]), + Component::WrittenBook(b) => Tag::compound([ + ("title", Tag::String(b.title.as_str().into())), + ("author", Tag::String(b.author.as_str().into())), + ("generation", Tag::Int(b.generation)), + ("pages", Tag::List(b.pages.clone())), + ("resolved", Tag::Byte(i8::from(b.resolved))), + ]), + Component::Fireworks { flight, explosions } => Tag::compound([ + ("flight_duration", Tag::Byte(*flight as i8)), + ( + "explosions", + Tag::List(explosions.iter().map(fireworks_tag).collect()), + ), + ]), + Component::FireworkExplosion(e) => fireworks_tag(e), + Component::Enchantments(list) => { + // Enchantments are a synced (data-driven) registry: the + // ids index the server's RegistryData, not the builtin + // report, so a datapack enchantment names correctly. + let levels = list.iter().filter_map(|(id, level)| { + let name = synced.name("minecraft:enchantment", *id)?; + Some((name, Tag::Int(*level))) + }); + Tag::compound(levels) + } + Component::AttackRange(v) => Tag::compound([ + ("min_reach", Tag::Float(v[0].to_bits())), + ("max_reach", Tag::Float(v[1].to_bits())), + ("min_attack_momentum", Tag::Float(v[2].to_bits())), + ("max_attack_momentum", Tag::Float(v[3].to_bits())), + ("hitbox_margin", Tag::Float(v[4].to_bits())), + ("mob_factor", Tag::Float(v[5].to_bits())), + ]), + Component::Profile(p) => profile_tag(p), + // PotionContents' NBT: the potion by name and the custom colour. + Component::Potion { potion, color, .. } => { + let potion = potion.and_then(|id| builtin.get("minecraft:potion")?.name(id)); + Tag::compound( + potion + .map(|name| ("potion", Tag::String(name.into()))) + .into_iter() + .chain(color.map(|c| ("custom_color", Tag::Int(c)))), + ) + } + // ItemStackTemplate.CODEC: id, count and components. + Component::Stacks(stacks) => Tag::List( + stacks + .iter() + .map(|t| { + let mut entries = vec![ + ( + "id", + Tag::String(item_name(builtin, t.item).as_ref().into()), + ), + ("count", Tag::Int(t.count)), + ]; + if let Some(c) = components_tag(t, builtin, synced) { + entries.push(("components", c)); + } + Tag::compound(entries) + }) + .collect(), + ), + Component::CustomModelData { + floats, + flags, + strings, + colors, + } => Tag::compound([ + ( + "floats", + Tag::List(floats.iter().map(|f| Tag::Float(f.to_bits())).collect()), + ), + ( + "flags", + Tag::List(flags.iter().map(|b| Tag::Byte(i8::from(*b))).collect()), + ), + ( + "strings", + Tag::List( + strings + .iter() + .map(|s| Tag::String(s.as_str().into())) + .collect(), + ), + ), + ( + "colors", + Tag::List(colors.iter().map(|c| Tag::Int(*c)).collect()), + ), + ]), + Component::TooltipDisplay { hide, hidden } => { + let hidden = hidden + .iter() + .filter_map(|id| Some(Tag::String(name(*id)?.into()))); + Tag::compound([ + ("hide_tooltip", Tag::Byte(i8::from(*hide))), + ("hidden_components", Tag::List(hidden.collect())), + ]) + } + }; + entries.push((key, value)); + } + Some(Tag::compound(entries)) +} + +/// A wire `FireworkExplosion` as vanilla's NBT codec writes it +/// (`FireworkExplosion.Shape`'s BY_ID order). +fn fireworks_tag(e: &horizon_protocol::item::FireworkExplosion) -> Tag { + const SHAPES: [&str; 5] = ["small_ball", "large_ball", "star", "creeper", "burst"]; + let ints = |c: &[i32]| Tag::List(c.iter().map(|c| Tag::Int(*c)).collect()); + Tag::compound([ + ( + "shape", + Tag::String( + SHAPES + .get(e.shape.max(0) as usize) + .copied() + .unwrap_or("small_ball") + .into(), + ), + ), + ("colors", ints(&e.colors)), + ("fade_colors", ints(&e.fade)), + ("has_trail", Tag::Byte(i8::from(e.trail))), + ("has_twinkle", Tag::Byte(i8::from(e.twinkle))), + ]) +} + +/// A `minecraft:profile` component as the item data JSON the model reads. +fn profile_tag(p: &Profile) -> Tag { + let properties = p + .properties + .iter() + .map(|(name, value, signature)| { + let mut entries: Vec<(&'static str, Tag)> = vec![ + ("name", Tag::String(name.as_str().into())), + ("value", Tag::String(value.as_str().into())), + ]; + if let Some(signature) = signature { + entries.push(("signature", Tag::String(signature.as_str().into()))); + } + Tag::compound(entries) + }) + .collect::>(); + let mut entries: Vec<(&'static str, Tag)> = Vec::new(); + if let Some(name) = &p.name { + entries.push(("name", Tag::String(name.as_str().into()))); + } + entries.push(("properties", Tag::List(properties))); + Tag::compound(entries) +} + +#[cfg(test)] +mod tests { + use super::*; + use horizon_protocol::registry::Ids; + + /// What a creative client sends (`stack_to_network`) comes back whole on + /// the server: damage, both enchantment lists, repair cost, name, trim. + #[test] + fn a_creative_stack_keeps_its_components_across_the_wire() { + let mut components = vec![String::new(); 60]; + for (id, name) in [ + (component::DAMAGE, "minecraft:damage"), + (component::CUSTOM_NAME, "minecraft:custom_name"), + (component::ENCHANTMENTS, "minecraft:enchantments"), + (component::POTION_CONTENTS, "minecraft:potion_contents"), + (component::REPAIR_COST, "minecraft:repair_cost"), + ( + component::STORED_ENCHANTMENTS, + "minecraft:stored_enchantments", + ), + (component::TRIM, "minecraft:trim"), + ] { + components[id as usize] = name.to_owned(); + } + let names = |n: &[&str]| n.iter().map(|s| s.to_string()).collect::>(); + let builtin = Builtin { + registries: [ + ( + "minecraft:item".to_owned(), + Ids::from_names(names(&[ + "minecraft:air", + "minecraft:diamond_chestplate", + "minecraft:splash_potion", + ])), + ), + ( + "minecraft:data_component_type".to_owned(), + Ids::from_names(components), + ), + ( + "minecraft:potion".to_owned(), + Ids::from_names(names(&["minecraft:water", "minecraft:swiftness"])), + ), + ] + .into(), + }; + let synced = |n: &[&str]| n.iter().map(|s| (s.to_string(), None)).collect::>(); + let dynamic = Dynamic { + registries: [ + ( + "minecraft:enchantment".to_owned(), + synced(&["minecraft:protection", "minecraft:sharpness"]), + ), + ( + "minecraft:trim_material".to_owned(), + synced(&["minecraft:gold"]), + ), + ( + "minecraft:trim_pattern".to_owned(), + synced(&["minecraft:sentry"]), + ), + ] + .into(), + }; + let mut stack = ItemStack::new("minecraft:diamond_chestplate", 1); + stack.damage = 5; + stack.enchant("minecraft:protection", 4); + let d = stack.data_mut(); + d.stored_enchantments + .push((Arc::from("minecraft:sharpness"), 2)); + d.repair_cost = 9; + d.custom_name = Some("Keeper".to_owned()); + d.trim = Some((Arc::from("minecraft:gold"), Arc::from("minecraft:sentry"))); + let id = |registry: &str, name: &str| { + builtin + .get(registry) + .and_then(|ids| ids.id(name)) + .or_else(|| dynamic.id(registry, name)) + }; + let wire = crate::lan::guest::stack_to_network( + |name| { + builtin + .get("minecraft:item") + .and_then(|i| i.id(name)) + .unwrap_or(0) + }, + id, + &Some(stack.clone()), + ) + .unwrap(); + let back = from_network(&wire, &builtin, &dynamic); + assert_eq!(back.item, stack.item); + assert_eq!(back.damage, 5); + let d = back.data().expect("components"); + assert_eq!(d.enchantments, vec![(Arc::from("minecraft:protection"), 4)]); + assert_eq!( + d.stored_enchantments, + vec![(Arc::from("minecraft:sharpness"), 2)] + ); + assert_eq!(d.repair_cost, 9); + assert_eq!(d.custom_name.as_deref(), Some("Keeper")); + assert_eq!( + d.trim, + Some((Arc::from("minecraft:gold"), Arc::from("minecraft:sentry"))) + ); + // PotionContents: the potion is a built-in registry, as a hub's + // `SyncData::id` treats it, so the id has to come from there. + let potion = ItemStack::new("minecraft:splash_potion", 1).with_potion("swiftness"); + let wire = crate::lan::guest::stack_to_network( + |name| id("minecraft:item", name).unwrap_or(0), + id, + &Some(potion), + ) + .unwrap(); + assert_eq!( + from_network(&wire, &builtin, &dynamic).potion(), + Some("minecraft:swiftness") + ); + } +} diff --git a/crates/horizon-client/src/item_tooltip.rs b/crates/horizon-client/src/item_tooltip.rs index ca6ff481b..6e51de391 100644 --- a/crates/horizon-client/src/item_tooltip.rs +++ b/crates/horizon-client/src/item_tooltip.rs @@ -487,23 +487,31 @@ pub fn lines( ) -> Vec { let mut lines = vec![name_line(stack, translate)]; if TooltipDisplay::for_stack(stack).shows("minecraft:lore") { - lines.extend(lore_lines(stack)); + lines.extend(lore_texts(stack).into_iter().map(|t| t.plain())); } lines.extend(detail_lines(stack, info, translate)); lines } -/// `LoreComponent.addToTooltip`: the saved component lines, as plain text. -fn lore_lines(stack: &ItemStack) -> Vec { +/// `ItemLore.addToTooltip`: each lore component under dark purple italic, with +/// the component's own styles (including explicit `false`) winning on children. +pub fn lore_texts(stack: &ItemStack) -> Vec { let Some(Tag::List(entries)) = stack.data().and_then(|d| d.other("minecraft:lore")) else { return Vec::new(); }; + let base = crate::text::Style { + color: Some(crate::text::DARK_PURPLE), + italic: Some(true), + ..Default::default() + }; entries .iter() .map(|entry| { - let json = crate::scoreboard::tag_to_json(entry); - let text = crate::component::to_text(&json, &mut |s| crate::text::Text::literal(s)); - text.plain() + let mut text = crate::entity_nbt::name_text(entry).resolve_legacy(); + for span in &mut text.spans { + span.style.inherit(&base); + } + text }) .collect() } @@ -512,6 +520,42 @@ fn lore_lines(stack: &ItemStack) -> Vec { mod tests { use super::*; + #[test] + fn lore_keeps_component_colours_under_dark_purple_italic() { + let mut stack = ItemStack::new("minecraft:stick", 1); + stack.data_mut().set_other( + "minecraft:lore", + Some(Tag::List(vec![ + Tag::compound([ + ("text", Tag::String("Gold line".into())), + ("color", Tag::String("gold".into())), + ("italic", Tag::Byte(0)), + ]), + Tag::String("Plain lore".into()), + ])), + ); + let lines = lore_texts(&stack); + assert_eq!(lines.len(), 2); + assert_eq!(lines[0].plain(), "Gold line"); + assert_eq!(lines[0].spans[0].style.color, Some(crate::text::GOLD)); + assert!(!lines[0].spans[0].style.is_italic()); + assert_eq!(lines[1].plain(), "Plain lore"); + assert_eq!( + lines[1].spans[0].style.color, + Some(crate::text::DARK_PURPLE) + ); + assert!(lines[1].spans[0].style.is_italic()); + assert!(TooltipDisplay::for_stack(&stack).shows("minecraft:lore")); + stack.data_mut().set_other( + "minecraft:tooltip_display", + Some(Tag::compound([( + "hidden_components", + Tag::List(vec![Tag::String("minecraft:lore".into())]), + )])), + ); + assert!(!TooltipDisplay::for_stack(&stack).shows("minecraft:lore")); + } + #[test] fn tooltip_display_controls_name_and_component_visibility() { let mut stack = ItemStack::new("minecraft:diamond_sword", 1); diff --git a/crates/horizon-client/src/lan/containers.rs b/crates/horizon-client/src/lan/containers.rs index d43cb5c27..0c9e3af54 100644 --- a/crates/horizon-client/src/lan/containers.rs +++ b/crates/horizon-client/src/lan/containers.rs @@ -343,7 +343,21 @@ impl Hub { if !window_ok || self.guests[i].spectator() { return Vec::new(); } + // A menu that is no longer valid takes no clicks. + if let Some(m) = self.guests[i].menu.as_ref().filter(|_| in_menu) + && !m.slots.still_valid(world, &self.guests[i], m.pos) + { + return Vec::new(); + } let source = self.guests[i].menu.as_ref().map(|m| m.slots); + let shulker_box = self + .sync + .builtin + .get("minecraft:menu") + .and_then(|m| m.id("minecraft:shulker_box")); + let shulker = in_menu + && shulker_box.is_some() + && self.guests[i].menu.as_ref().map(|m| m.kind) == shulker_box; let mut drag = if in_menu { self.guests[i].menu.as_mut().and_then(|m| m.drag.take()) } else { @@ -357,6 +371,7 @@ impl Hub { let mut view = View { guest, container: items.as_deref_mut(), + shulker, }; let (drops, changed) = menu::click( &mut view, diff --git a/crates/horizon-client/src/lan/guest.rs b/crates/horizon-client/src/lan/guest.rs index 440b58284..aa1994257 100644 --- a/crates/horizon-client/src/lan/guest.rs +++ b/crates/horizon-client/src/lan/guest.rs @@ -223,6 +223,8 @@ pub struct Guest { /// Whether the guest was on the ground before the first of `moves`. pub moves_from_ground: bool, pub dead: bool, + /// `LivingEntity.deathTime`: ticks since the guest died. + pub death_time: u32, /// The join packets were sent (PlayerList.placeNewPlayer ran). pub placed: bool, /// The level the player is in (`ServerPlayer.level().dimension()`): the @@ -349,6 +351,7 @@ impl Guest { moves: Vec::new(), moves_from_ground: false, dead: false, + death_time: 0, placed: false, dimension: "minecraft:overworld".into(), menu: None, @@ -404,6 +407,17 @@ impl Guest { pub fn eye_height(&self) -> f64 { if self.sneaking { 1.27 } else { 1.62 } } + /// `Player.isWithinBlockInteractionRange(pos, buffer)` for the guest: + /// its block interaction range is 4.5, 5 in creative. + pub fn within_block_range(&self, pos: [i32; 3], buffer: f64) -> bool { + let eye = [ + self.position[0], + self.position[1] + self.eye_height(), + self.position[2], + ]; + let range = if self.creative() { 5.0 } else { 4.5 }; + within_block_range(eye, range + buffer, pos) + } /// The inventory menu's 46 slots (ServerPlayer.inventoryMenu): 0 crafting /// result, 1..=4 crafting grid, 5..=8 armor, 9..=35 main, 36..=44 hotbar, /// 45 off hand. @@ -431,6 +445,43 @@ impl Guest { } } +/// `FireworkExplosion.STREAM_CODEC`'s fields (`Shape` by its BY_ID order). +fn network_explosion( + e: &crate::particles::FireworkExplosion, +) -> horizon_protocol::item::FireworkExplosion { + horizon_protocol::item::FireworkExplosion { + shape: e.shape as i32, + colors: e.colors.iter().map(|c| *c as i32).collect(), + fade: e.fade.iter().map(|c| *c as i32).collect(), + trail: e.trail, + twinkle: e.twinkle, + } +} + +/// A rocket's `minecraft:fireworks` and a star's `minecraft:firework_explosion`. +pub fn firework_components( + d: &crate::item_data::StackData, +) -> Vec<(i32, horizon_protocol::item::Component)> { + use horizon_protocol::item::{Component, component}; + let mut out = Vec::new(); + if let Some((flight, explosions)) = &d.fireworks { + out.push(( + component::FIREWORKS, + Component::Fireworks { + flight: i32::from(*flight), + explosions: explosions.iter().map(network_explosion).collect(), + }, + )); + } + if let Some(e) = &d.firework_explosion { + out.push(( + component::FIREWORK_EXPLOSION, + Component::FireworkExplosion(network_explosion(e)), + )); + } + out +} + /// The off hand slot is conventionally 40 in saves and 45 in the menu. /// `synced_id` names an entry of a synced registry (enchantments, trim /// materials and patterns) by its id on this connection. @@ -521,6 +572,23 @@ fn network_stack( horizon_protocol::item::Component::Int(i32::from(mode)), )); } + stack.added.extend(firework_components(d)); + // PotionContents: the potion by its `minecraft:potion` id. + if let Some(id) = (d.potion.as_deref()).and_then(|potion| { + synced_id( + "minecraft:potion", + &horizon_world::datafix::dynamic::namespaced(potion), + ) + }) { + stack.added.push(( + horizon_protocol::item::component::POTION_CONTENTS, + horizon_protocol::item::Component::Potion { + potion: Some(id), + color: None, + rest: Vec::new(), + }, + )); + } // A loaded crossbow's ammunition. if !d.charged.is_empty() { let charged = d @@ -568,11 +636,41 @@ fn network_stack( Some(stack) } +/// `Player.isWithinBlockInteractionRange`: `eye` closer than `range` to the +/// box of the block at `pos` (`AABB.distanceToSqr`). +pub fn within_block_range(eye: [f64; 3], range: f64, pos: [i32; 3]) -> bool { + (0..3) + .map(|k| { + let lo = f64::from(pos[k]); + (lo - eye[k]).max(eye[k] - lo - 1.0).max(0.0).powi(2) + }) + .sum::() + < range * range +} + #[cfg(test)] mod tests { use super::*; use horizon_protocol::item::{Component, component}; + #[test] + fn potions_go_as_potion_registry_ids() { + let synced_id = |registry: &str, name: &str| match (registry, name) { + ("minecraft:potion", "minecraft:swiftness") => Some(14), + _ => None, + }; + let potion = ItemStack::new("minecraft:splash_potion", 1).with_potion("swiftness"); + let stack = stack_to_network(|_| 7, synced_id, &Some(potion)).unwrap(); + assert_eq!( + stack.component(component::POTION_CONTENTS), + Some(&Component::Potion { + potion: Some(14), + color: None, + rest: Vec::new(), + }) + ); + } + #[test] fn trims_go_as_synced_registry_references() { let mut chestplate = ItemStack::new("minecraft:diamond_chestplate", 1); diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index c2a395899..b462536b9 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -149,6 +149,9 @@ pub struct Hub { pub singleplayer_owner: Option, /// Who rides what, set by the game before each tick. pub riding: super::entity::Riding, + /// The scoreboard's teams, set by the game each tick, for players' + /// display names. + pub teams: Vec, last_clock: i64, last_rate: f32, ticking: (f32, bool), @@ -206,6 +209,24 @@ pub fn display_name(uuid: u128, name: &str) -> Tag { ]) } +impl Hub { + /// Player.getDisplayName: the name formatted for the player's team + /// (PlayerTeam.formatNameForTeam), suggesting `/tell NAME `, showing the + /// player on hover and inserting the name. + pub fn player_display_name(&self, uuid: u128, name: &str) -> Tag { + let Some(team) = self.teams.iter().find(|t| t.players.contains(name)) else { + return display_name(uuid, name); + }; + let mut c = team.formatted_name_comp(&crate::scoreboard::literal(name)); + c["click_event"] = + serde_json::json!({"action": "suggest_command", "command": format!("/tell {name} ")}); + c["hover_event"] = serde_json::json!({"action": "show_entity", "id": "minecraft:player", + "uuid": horizon_protocol::crypto::uuid_string(uuid), "name": name}); + c["insertion"] = name.into(); + crate::scoreboard::component_tag(&c) + } +} + /// A yellow translation of player names (`multiplayer.player.joined`). fn yellow_names(key: &str, names: Vec) -> Tag { Tag::compound([ @@ -315,6 +336,7 @@ impl Hub { entity_types: Default::default(), entities: Default::default(), entity_out: Vec::new(), + teams: Vec::new(), } } @@ -1984,7 +2006,9 @@ impl Hub { S::PlayerInput(p) => { self.guests[i].input = p.flags; self.guests[i].sneaking = p.flags & 32 != 0; - self.guests[i].sprinting = p.flags & 64 != 0 && p.flags & 1 != 0; + // handlePlayerInput only records keys and updates crouching. + // Sprinting persists after the sprint key is released (or + // starts with a double tap); PlayerCommand changes it. } // OPEN_INVENTORY (the ridden mount's inventory) and // START_FALL_FLYING (checked against the guest's chest slot @@ -2291,6 +2315,11 @@ impl Hub { .name("minecraft:enchantment", id) .map(str::to_owned) } + /// A stack a client sent (`SetCreativeModeSlot`), read with this + /// server's registries. + pub fn stack_from_network(&self, s: &horizon_protocol::item::Stack) -> crate::items::ItemStack { + crate::item_sync::from_network(s, &self.sync.builtin, &self.sync.dynamic) + } /// ServerLevel.removePlayerImmediately, then addRespawnedPlayer: the other players /// lose the dead player's entity (ServerEntity.removePairing) and are sent the diff --git a/crates/horizon-client/src/lan/hub/chat.rs b/crates/horizon-client/src/lan/hub/chat.rs index 7e75c84c5..bdc7c3fb2 100644 --- a/crates/horizon-client/src/lan/hub/chat.rs +++ b/crates/horizon-client/src/lan/hub/chat.rs @@ -2,7 +2,7 @@ //! handle it: chat sessions validated against the services' keys, signed //! messages checked link by link, the acknowledged window, and messages //! relayed with each connection's numbering and signature cache. -use super::{HostEvent, Hub, display_name}; +use super::{HostEvent, Hub}; use horizon_protocol::{ packets::play::{self, Clientbound as C}, secure_chat::{self, Body, Chain, ChatSession, DecodeError, ProfileKey, Signature, Unpacked}, @@ -214,7 +214,7 @@ impl Hub { } let bound = play::ChatTypeBound { chat_type: self.chat_type, - name: display_name(uuid, &name), + name: self.player_display_name(uuid, &name), target: None, }; let line = format!("<{name}> {}", message.content); @@ -427,7 +427,7 @@ impl Hub { let (uuid, name) = (self.guests[i].uuid, self.guests[i].name.clone()); let bound = play::ChatTypeBound { chat_type: self.chat_type_id(chat_type), - name: display_name(uuid, &name), + name: self.player_display_name(uuid, &name), target: None, }; let line = match chat_type { @@ -467,15 +467,15 @@ impl Hub { self.chat_type_id("msg_command_incoming"), ); for &t in targets { - let target = display_name(self.guests[t].uuid, &self.guests[t].name); + let target = self.player_display_name(self.guests[t].uuid, &self.guests[t].name); let out = play::ChatTypeBound { chat_type: outgoing, - name: display_name(uuid, &name), + name: self.player_display_name(uuid, &name), target: Some(target), }; let inc = play::ChatTypeBound { chat_type: incoming, - name: display_name(uuid, &name), + name: self.player_display_name(uuid, &name), target: None, }; match &message { diff --git a/crates/horizon-client/src/lan/menu.rs b/crates/horizon-client/src/lan/menu.rs index de9fb9608..4185dadc4 100644 --- a/crates/horizon-client/src/lan/menu.rs +++ b/crates/horizon-client/src/lan/menu.rs @@ -64,6 +64,19 @@ impl Slots { .iter() .all(|k| world.block_entities.contains_key(k)) } + /// `Container.stillValid` for the guest: the block entities are there + /// and the guest is within reach of each (`stillValidBlockEntity`). The + /// ender chest's block is `at` (`PlayerEnderChestContainer.activeChest`). + pub fn still_valid(self, world: &World, guest: &Guest, at: Option<(i32, i32, i32)>) -> bool { + let blocks = match self { + Slots::Ender => at.into_iter().collect(), + _ => self.halves(), + }; + self.valid(world) + && blocks + .iter() + .all(|k| guest.within_block_range([k.0, k.1, k.2], 4.0)) + } /// Container slot `n`. pub fn get(self, world: &World, guest: &Guest, mut n: usize) -> Option { if self == Slots::Ender { @@ -111,6 +124,8 @@ pub struct View<'a> { pub guest: &'a mut Guest, /// The container's slots, or None for the inventory menu. pub container: Option<&'a mut [Option]>, + /// The container is a shulker box (`ShulkerBoxSlot`). + pub shulker: bool, } impl View<'_> { @@ -153,6 +168,12 @@ impl View<'_> { if self.container.is_none() && (5..=8).contains(&i) { return rules.armor_index(&stack.item) == Some(i - 5); } + if self.shulker + && self.container.as_ref().is_some_and(|c| i < c.len()) + && !crate::menu::fits_inside_container_items(stack) + { + return false; + } self.enabled(i) } fn slot_max(&self, i: usize, stack: &ItemStack, rules: &dyn Rules) -> u32 { @@ -665,6 +686,24 @@ mod tests { ) } + /// The prepared resources' items, or None (the test is skipped) without them. + fn rules() -> Option { + let root = std::env::var_os("HORIZON_RESOURCES") + .map(std::path::PathBuf::from) + .unwrap_or_else(|| { + std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("../../.local/horizon-client") + .join(horizon_core::VANILLA_VERSION) + }); + if !root.join("items.json").exists() { + eprintln!("skipping resource-backed menu check: {}", root.display()); + return None; + } + let registry = horizon_world::level::data::load_registry(&root).unwrap(); + let facts = crate::blocks::BlockFacts::load(&root, ®istry).unwrap(); + Some(ItemRules(Items::load(&root, ®istry, &facts).unwrap())) + } + fn contents(stack: &Option) -> Vec<(String, u32)> { let inside = stack.as_ref().and_then(|s| s.data()); inside.map_or(Vec::new(), |d| { @@ -680,20 +719,9 @@ mod tests { /// slot; a stack clicked onto a bundle goes in, as far as it fits. #[test] fn guest_chest_clicks_fill_and_empty_bundles() { - let root = std::env::var_os("HORIZON_RESOURCES") - .map(std::path::PathBuf::from) - .unwrap_or_else(|| { - std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .join("../../.local/horizon-client") - .join(horizon_core::VANILLA_VERSION) - }); - if !root.join("items.json").exists() { - eprintln!("skipping resource-backed bundle check: {}", root.display()); + let Some(rules) = rules() else { return; - } - let registry = horizon_world::level::data::load_registry(&root).unwrap(); - let facts = crate::blocks::BlockFacts::load(&root, ®istry).unwrap(); - let rules = ItemRules(Items::load(&root, ®istry, &facts).unwrap()); + }; let mut g = guest(); let mut chest = vec![ Some(ItemStack::new("minecraft:stone", 48)), @@ -704,6 +732,7 @@ mod tests { let mut view = View { guest: &mut g, container: Some(&mut chest), + shulker: false, }; view.guest.carried = Some(ItemStack::new("minecraft:bundle", 1)); // Left click with the bundle: the stone goes in. @@ -736,4 +765,37 @@ mod tests { ); assert_eq!(contents(&view.get(2)), []); } + + #[test] + fn guest_shulker_box_menu_refuses_shulker_boxes() { + let Some(rules) = rules() else { + return; + }; + for shulker in [true, false] { + let mut g = guest(); + let mut container = vec![None; 27]; + let mut drag = None; + let mut view = View { + guest: &mut g, + container: Some(&mut container), + shulker, + }; + // Carried onto an empty slot, shift-clicked from the hotbar + // (menu slot 27 + 27) and swapped in with the number key 1. + view.guest.carried = Some(ItemStack::new("minecraft:red_shulker_box", 1)); + click(&mut view, &mut drag, 0, 0, input::PICKUP, &rules); + assert_eq!(view.get(0).is_none(), shulker); + view.guest.carried = None; + view.guest.inventory[0] = Some(ItemStack::new("minecraft:shulker_box", 1)); + click(&mut view, &mut drag, 54, 0, input::QUICK_MOVE, &rules); + assert_eq!(view.guest.inventory[0].is_some(), shulker); + view.guest.inventory[0] = Some(ItemStack::new("minecraft:blue_shulker_box", 1)); + click(&mut view, &mut drag, 5, 0, input::SWAP, &rules); + assert_eq!(view.get(5).is_none(), shulker); + // Anything else goes in. + view.guest.carried = Some(ItemStack::new("minecraft:diamond", 1)); + click(&mut view, &mut drag, 9, 0, input::PICKUP, &rules); + assert!(view.get(9).is_some()); + } + } } diff --git a/crates/horizon-client/src/lang.rs b/crates/horizon-client/src/lang.rs index 94ec4b7ab..cb96cc373 100644 --- a/crates/horizon-client/src/lang.rs +++ b/crates/horizon-client/src/lang.rs @@ -138,6 +138,11 @@ impl Language { pub fn get(&self, key: &str) -> Option<&str> { self.map.get(key).map(String::as_str) } + /// The resolved map, for `horizon_protocol::text::plain`: a server's + /// components carry keys, and the network layer resolves them with this. + pub fn map(&self) -> &HashMap { + &self.map + } pub fn len(&self) -> usize { self.map.len() } diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index ae95e1bdb..ad05153eb 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -96,6 +96,7 @@ pub mod inv_sort; pub mod inventory_effects; pub mod item_data; pub mod item_render; +pub mod item_sync; pub mod item_tooltip; pub mod items; pub mod java_text; diff --git a/crates/horizon-client/src/menu.rs b/crates/horizon-client/src/menu.rs index 07e4ba2b9..3b2b8fd31 100644 --- a/crates/horizon-client/src/menu.rs +++ b/crates/horizon-client/src/menu.rs @@ -247,6 +247,12 @@ pub fn beacon_required_levels(effect: Option<&str>) -> i32 { .map_or(i32::MAX, |i| i as i32 + 1) } +/// `Item.canFitInsideContainerItems` (`ShulkerBoxSlot.mayPlace`): shulker +/// boxes cannot nest. +pub fn fits_inside_container_items(stack: &ItemStack) -> bool { + !stack.item.ends_with("shulker_box") +} + /// The containers a menu spans. const PLAYER: usize = 0; const MAIN: usize = 1; @@ -995,7 +1001,7 @@ impl Menu { fn anvil_result(&mut self, env: &Env) { let hover = self.containers[MAIN][0] .as_ref() - .map(|i| crate::stations::hover_name(&|id| env.names.item(id), i)) + .map(|i| crate::stations::hover_name(&|id| env.names.display_item(id), i)) .unwrap_or_default(); let out = crate::stations::anvil_result( env.enchantments, @@ -1210,11 +1216,6 @@ impl Menu { env.items.get(&stack.item).is_some_and(|i| i.fuel.is_some()) } - /// `Item.canFitInsideContainerItems`: shulker boxes cannot nest. - fn fits_inside(stack: &ItemStack) -> bool { - !(stack.item.ends_with("shulker_box")) - } - // ---- Slot ---- fn may_place(&self, env: &Env, s: usize, stack: &ItemStack) -> bool { @@ -1247,7 +1248,7 @@ impl Menu { .as_deref() .is_some_and(|m| i.equip_allowed.as_ref().is_none_or(|a| a.contains(m))) }), - Role::Shulker => Self::fits_inside(stack), + Role::Shulker => fits_inside_container_items(stack), Role::Fuel => Self::is_fuel(env, stack) || Self::is_bucket(stack), Role::Output | Role::CraftResult | Role::Inert => false, Role::CrafterIn => self.data.get(self.slots[s].index) != Some(&1), diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 4e3879fe5..46834e9d1 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -7,6 +7,7 @@ //! behaviour, models, animations and sounds. pub mod ai; pub mod allay; +pub mod ambient; pub mod anim; pub mod animal_brain; pub mod aquatic; @@ -1646,6 +1647,11 @@ impl Mob { pub fn species(&self) -> Species { self.kind.species } + /// `Mob.getMainArm`: the left arm when the mob is left-handed, a player's + /// `mainHand` option for a player mob. + pub fn main_arm_left(&self) -> bool { + self.extra.main_hand_left || self.extra.left_handed + } /// The player hit this mob (melee): LivingEntity.hurt with knockback. pub fn hurt( &mut self, diff --git a/crates/horizon-client/src/mobs/ambient.rs b/crates/horizon-client/src/mobs/ambient.rs new file mode 100644 index 000000000..cb4146e08 --- /dev/null +++ b/crates/horizon-client/src/mobs/ambient.rs @@ -0,0 +1,55 @@ +//! What a client's copy of a mob makes on its own, in the +//! `level().isClientSide()` branches of `aiStep`: nothing of it reaches the +//! server or goes over the network, so the window's game runs it, whether it +//! simulates the world (in-frame) or follows a server. +use super::{Mob, MobEvent, Species}; +use horizon_core::random::RandomSource; + +/// `Enderman.aiStep`, `Endermite.aiStep` and `Blaze.aiStep` on a client: +/// two portal particles a tick around endermen and endermites; two large +/// smoke particles a tick around blazes and, one tick in 24, their burn. +pub fn client_ai_step(m: &mut Mob, out: &mut Vec) { + let species = m.species(); + if !matches!( + species, + Species::Enderman | Species::Endermite | Species::Blaze + ) { + return; + } + let (w, h) = (f64::from(m.width()), f64::from(m.height())); + let p = m.position; + if species == Species::Blaze && m.random.next_int_bound(24).unwrap_or(1) == 0 && !m.silent { + let volume = 1.0 + m.random.next_float(); + let pitch = m.random.next_float() * 0.7 + 0.3; + out.push(MobEvent::LocalSound( + "entity.blaze.burn", + [p.x + 0.5, p.y + 0.5, p.z + 0.5], + volume, + pitch, + )); + } + for _ in 0..2 { + // Entity.getRandomX(0.5), getRandomY(), getRandomZ(0.5). + let r = &mut m.random; + let x = p.x + w * (2.0 * r.next_double() - 1.0) * 0.5; + let y = p.y + h * r.next_double(); + let z = p.z + w * (2.0 * r.next_double() - 1.0) * 0.5; + let (kind, at, velocity) = if species == Species::Blaze { + ("large_smoke", [x, y, z], [0.0; 3]) + } else { + // The enderman's are a quarter block lower. + let y = if species == Species::Enderman { + y - 0.25 + } else { + y + }; + let velocity = [ + (r.next_double() - 0.5) * 2.0, + -r.next_double(), + (r.next_double() - 0.5) * 2.0, + ]; + ("portal", [x, y, z], velocity) + }; + out.push(MobEvent::ParticleAt { kind, at, velocity }); + } +} diff --git a/crates/horizon-client/src/mobs/anim.rs b/crates/horizon-client/src/mobs/anim.rs index aa0d8fb2a..b2158132b 100644 --- a/crates/horizon-client/src/mobs/anim.rs +++ b/crates/horizon-client/src/mobs/anim.rs @@ -642,7 +642,7 @@ pub fn setup( } else { ArmPose::Empty }; - let mut h = if m.extra.main_hand_left { + let mut h = if m.main_arm_left() { Hum::new(ArmPose::Empty, main) } else { Hum::new(main, ArmPose::Empty) @@ -693,7 +693,7 @@ pub fn setup( } else { ArmPose::Empty }; - let mut h = if m.extra.main_hand_left { + let mut h = if m.main_arm_left() { Hum::new(off, pose) } else { Hum::new(pose, off) diff --git a/crates/horizon-client/src/mobs/undead.rs b/crates/horizon-client/src/mobs/undead.rs index 197321dea..e77a37641 100644 --- a/crates/horizon-client/src/mobs/undead.rs +++ b/crates/horizon-client/src/mobs/undead.rs @@ -621,8 +621,10 @@ impl BowAttack { } } } +/// `LivingEntity.isHolding(Items.BOW)`: the bow can sit in either hand. fn holding_bow(m: &Mob) -> bool { m.extra.main_hand.as_deref() == Some("minecraft:bow") + || m.extra.off_hand.as_deref() == Some("minecraft:bow") } impl Goal for BowAttack { goal_clone!(); @@ -715,7 +717,10 @@ impl Goal for BowAttack { } else { self.attack_time -= 1; if self.attack_time <= 0 && self.see_time >= -60 { + // mob.startUsingItem(ProjectileUtil.getWeaponHoldingHand): + // the off hand when the main one doesn't hold the bow. m.extra.using_item = 1; + m.extra.using_off_hand = m.extra.main_hand.as_deref() != Some("minecraft:bow"); } } } diff --git a/crates/horizon-client/src/motion.rs b/crates/horizon-client/src/motion.rs index 560cadc80..5918bccd9 100644 --- a/crates/horizon-client/src/motion.rs +++ b/crates/horizon-client/src/motion.rs @@ -167,6 +167,9 @@ pub enum MotionEvent { InsidePortal { pos: BlockPos, }, + /// `WitherRoseBlock.entityInside`: the player's box swept through a + /// wither rose; the server adds its Wither unless peaceful. + InsideWitherRose, /// `ServerPlayer.checkFallDamage` after a mace smash: block particles /// from the landing block, more the further the fall. SmashLanding { @@ -1083,11 +1086,26 @@ impl MotionWorld<'_, '_, '_> { } /// What `applyEffectsFromBlocks` does to a server's player along the /// moves its client sent since `from` (in its history): the damage of - /// magma, cactus, lit campfires and grown sweet berry bushes. Its client - /// moves it, so the slowing and the rest are the client's. + /// magma, cactus, lit campfires and grown sweet berry bushes, and powder + /// snow's FREEZE, then `LivingEntity.aiStep`'s freezing and its damage. + /// Its client moves it, so the slowing and the rest are the client's. pub fn server_block_damage(&mut self, from: Vec3) -> Result<()> { self.player.old_position = from; - self.effects_from_blocks(true) + // Entity.baseTick + self.player.was_in_powder_snow = self.player.in_powder_snow; + self.player.in_powder_snow = false; + self.effects_from_blocks(true)?; + // LivingEntity.aiStep "freezing" (server). + if !self.player.in_powder_snow || !self.player.can_freeze() { + self.player.ticks_frozen = (self.player.ticks_frozen - 2).max(0); + } + if self.player.health.tick_count % 40 == 0 + && self.player.is_fully_frozen() + && self.player.can_freeze() + { + self.freeze_damage()?; + } + Ok(()) } fn effects_from_blocks(&mut self, damage_only: bool) -> Result<()> { use horizon_world::entity_move::MovementRecord; @@ -1191,7 +1209,7 @@ impl MotionWorld<'_, '_, '_> { groups += 1; last_step = step; } - if groups == 1 { + if groups == 1 && !damage_only { // A later group finds the fire already out. self.player.powder_snow_entered.push(p); } @@ -1259,21 +1277,22 @@ impl MotionWorld<'_, '_, '_> { continue; } let powder_snow = name == "minecraft:powder_snow"; + let lava_cauldron = name == "minecraft:lava_cauldron"; // getEntityInsideCollisionShape is the whole block but for powder // snow (its collision shape, the whole block when it is empty), // filled cauldrons, end portals and tripwires; Horizon doesn't - // follow the player into those last three. + // follow the player into the water and powder snow cauldrons, end + // portals or tripwires yet. if matches!( name, "minecraft:water_cauldron" - | "minecraft:lava_cauldron" | "minecraft:powder_snow_cauldron" | "minecraft:end_portal" | "minecraft:tripwire" ) { continue; } - let inside = !powder_snow || { + let inside = !(powder_snow || lava_cauldron) || { let context = self.context(); let binding = self.collision_binding; let shape = binding.collision_shape_with_context( @@ -1282,21 +1301,33 @@ impl MotionWorld<'_, '_, '_> { p, &context, )?; - shape.is_empty() || shape.boxes() == VoxelShape::block().boxes() || { - let boxes: Vec<_> = shape - .moved(Vec3::new(f64::from(p.x), f64::from(p.y), f64::from(p.z))) - .boxes() - .to_vec(); - self.player - .dimensions - .bounds(from) - .collided_along_vector(to.subtract(from), &boxes) - } + powder_snow && (shape.is_empty() || shape.boxes() == VoxelShape::block().boxes()) + || { + let offset = Vec3::new(f64::from(p.x), f64::from(p.y), f64::from(p.z)); + let mut boxes: Vec<_> = shape.moved(offset).boxes().to_vec(); + if lava_cauldron { + // LavaCauldronBlock.FILLED_SHAPE: the cauldron's + // shape or its lava, Block.column(12, 4, 15). + boxes.push(Aabb::new( + Vec3::new(2.0 / 16.0, 4.0 / 16.0, 2.0 / 16.0).add_vector(offset), + Vec3::new(14.0 / 16.0, 15.0 / 16.0, 14.0 / 16.0).add_vector(offset), + )); + } + self.player + .dimensions + .bounds(from) + .collided_along_vector(to.subtract(from), &boxes) + } }; if inside && visited.insert(p.pack()) { - // CactusBlock and CampfireBlock.entityInside (`entity.hurt`). + // CactusBlock, CampfireBlock and LavaCauldronBlock.entityInside + // (`entity.hurt`), and WitherRoseBlock's server-side effect. match name { "minecraft:cactus" => self.environment_hurt("minecraft:cactus", 1.0)?, + "minecraft:lava_cauldron" => self.lava_cauldron_inside()?, + "minecraft:wither_rose" => { + self.player.events.push(MotionEvent::InsideWitherRose); + } "minecraft:campfire" | "minecraft:soul_campfire" => { let lit = self .assets @@ -1316,6 +1347,8 @@ impl MotionWorld<'_, '_, '_> { "minecraft:sweet_berry_bush" if damage_only => { self.sweet_berry_bush_damage(state)?; } + // PowderSnowBlock.entityInside's FREEZE. + "minecraft:powder_snow" if damage_only => entered.push((step, p)), _ => {} } if damage_only { @@ -1354,6 +1387,14 @@ impl MotionWorld<'_, '_, '_> { } self.player.impulse.try_reset(); } + /// `LavaCauldronBlock.entityInside`: CLEAR_FREEZE, LAVA_IGNITE + /// (`Entity.lavaIgnite`, 15 seconds) and then `Entity.lavaHurt` (lava, + /// 4), as ordinary lava does to the player. + fn lava_cauldron_inside(&mut self) -> Result<()> { + self.player.ticks_frozen = 0; + self.player.ignite_for_ticks(300)?; + self.environment_hurt("minecraft:lava", 4.0) + } /// `WebBlock.entityInside`: stuck at a quarter of the speed, half with /// the Weaving effect. fn web_inside(&mut self) { @@ -2368,6 +2409,124 @@ mod tests { assert_eq!(player.position.y, 4.0); } + #[test] + fn a_lava_cauldron_burns_the_player_inside_it() { + let root = std::env::var_os("HORIZON_TEST_RESOURCES") + .map(std::path::PathBuf::from) + .unwrap_or_else(|| { + Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../../.local/horizon-client") + .join(horizon_core::VANILLA_VERSION) + }); + if !root.join("movement-properties.nbt").exists() { + return; + } + let assets = Assets::load(&root).unwrap(); + let render = assets.reload(&root).unwrap(); + let binding = crate::collisions::load(&root, assets.registry).unwrap(); + let resources = MotionResources::load(&root, &assets).unwrap(); + let mut scene = World::test_scene(&assets, &render, 1).unwrap(); + let cauldron = assets + .registry + .default_state("minecraft:lava_cauldron") + .unwrap(); + scene.set(20, 4, 20, cauldron).unwrap(); + let mut tick = |player: &mut PlayerMotion| { + MotionWorld { + entities: &[], + player, + scene: &mut scene, + assets: &assets, + collision_binding: &binding, + resources: &resources, + } + .controlled_simulation_tick(Default::default()) + .unwrap(); + }; + // Standing on the cauldron's floor, in its lava. + let mut player = PlayerMotion::new(&resources, Vec3::new(20.5, 4.25, 20.5)).unwrap(); + player.ticks_frozen = 40; + tick(&mut player); + assert!(player.health.health() < 20.0, "the lava must hurt"); + assert!(player.fire_ticks > 0, "the lava must ignite"); + assert_eq!(player.ticks_frozen, 0); + // Beside it, nothing. + let mut away = PlayerMotion::new(&resources, Vec3::new(22.5, 4.0, 20.5)).unwrap(); + tick(&mut away); + assert_eq!(away.health.health(), 20.0); + assert!(away.fire_ticks <= 0); + } + + #[test] + fn standing_in_a_wither_rose_asks_for_its_effect() { + let root = std::env::var_os("HORIZON_TEST_RESOURCES") + .map(std::path::PathBuf::from) + .unwrap_or_else(|| { + Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../../.local/horizon-client") + .join(horizon_core::VANILLA_VERSION) + }); + if !root.join("movement-properties.nbt").exists() { + return; + } + let assets = Assets::load(&root).unwrap(); + let render = assets.reload(&root).unwrap(); + let binding = crate::collisions::load(&root, assets.registry).unwrap(); + let resources = MotionResources::load(&root, &assets).unwrap(); + let mut scene = World::test_scene(&assets, &render, 1).unwrap(); + let rose = assets + .registry + .default_state("minecraft:wither_rose") + .unwrap(); + scene.set(20, 4, 20, rose).unwrap(); + let roses = |player: &PlayerMotion| { + player + .events + .iter() + .filter(|e| matches!(e, MotionEvent::InsideWitherRose)) + .count() + }; + let mut player = PlayerMotion::new(&resources, Vec3::new(20.5, 4.0, 20.5)).unwrap(); + MotionWorld { + entities: &[], + player: &mut player, + scene: &mut scene, + assets: &assets, + collision_binding: &binding, + resources: &resources, + } + .controlled_simulation_tick(Default::default()) + .unwrap(); + assert_eq!(roses(&player), 1); + // A server's player: its client moved it, the server applies the effect. + player.events.clear(); + let from = player.position; + MotionWorld { + entities: &[], + player: &mut player, + scene: &mut scene, + assets: &assets, + collision_binding: &binding, + resources: &resources, + } + .server_block_damage(from) + .unwrap(); + assert_eq!(roses(&player), 1); + // Two blocks away, nothing. + let mut away = PlayerMotion::new(&resources, Vec3::new(22.5, 4.0, 20.5)).unwrap(); + MotionWorld { + entities: &[], + player: &mut away, + scene: &mut scene, + assets: &assets, + collision_binding: &binding, + resources: &resources, + } + .controlled_simulation_tick(Default::default()) + .unwrap(); + assert_eq!(roses(&away), 0); + } + #[test] fn server_travel_moves_only_the_velocity() { let root = Path::new(env!("CARGO_MANIFEST_DIR")) diff --git a/crates/horizon-client/src/net.rs b/crates/horizon-client/src/net.rs index 9cce9e1c6..1986c5b1d 100644 --- a/crates/horizon-client/src/net.rs +++ b/crates/horizon-client/src/net.rs @@ -18,6 +18,7 @@ use horizon_protocol::{ text::{Language, plain, translatable}, }; use std::{ + collections::HashMap, sync::{ Arc, Mutex, atomic::{AtomicBool, Ordering}, @@ -27,6 +28,12 @@ use std::{ time::Duration, }; +/// `CommonListenerCookie.serverCookies`: cookies the server asked the client +/// to store (`ClientboundStoreCookiePacket`), answered to +/// `ClientboundCookieRequestPacket`. A `transfer` carries them to the next +/// connection (`TransferState`), so a proxy's backend switch keeps them. +pub type Cookies = Arc>>>; + /// What the network thread reports to the game. pub enum Event { /// Progress text for the connecting screen. @@ -48,6 +55,13 @@ pub enum Event { PackPush(cfg::ResourcePackPush), /// ClientboundResourcePackPopPacket: one pack, or every pack. PackPop(Option), + /// `ClientboundTransferPacket`: the server sent us to another address. + /// The connection ends right after; the game reconnects there with the + /// stored cookies. + Transfer { + host: String, + port: i32, + }, /// The connection ended. `from_server`: the server gave the reason (or /// the connection was refused for good), rather than the connection /// failing (auto-reconnect retries only failures and shutdowns). @@ -67,6 +81,8 @@ pub struct Session { pub address: String, pub name: String, pub counters: Arc>>>, + /// The cookies this server stored on us (`ClientboundStoreCookiePacket`). + pub cookies: Cookies, /// HORIZON_NET_DEBUG=1 logs every packet sent from the game thread. debug: bool, } @@ -79,7 +95,27 @@ impl Session { view_distance: i8, lang: Arc, ) -> Self { - Self::start(address, name, auth, view_distance, lang, None) + Self::start(address, name, auth, view_distance, lang, None, None) + } + /// `ConnectScreen.startConnecting` for a `transfer`: the cookies the + /// server stored carry to the new connection (`TransferState`). + pub fn connect_transferred( + address: &str, + name: &str, + auth: Option>, + view_distance: i8, + lang: Arc, + cookies: Cookies, + ) -> Self { + Self::start( + address, + name, + auth, + view_distance, + lang, + None, + Some(cookies), + ) } /// Join an integrated server through memory (`Connection.connectToLocalServer`). pub fn connect_local( @@ -88,7 +124,7 @@ impl Session { view_distance: i8, lang: Arc, ) -> Self { - Self::start("local", name, None, view_distance, lang, Some(pipe)) + Self::start("local", name, None, view_distance, lang, Some(pipe), None) } fn start( address: &str, @@ -97,12 +133,18 @@ impl Session { view_distance: i8, lang: Arc, local: Option, + cookies: Option, ) -> Self { let (send, events) = channel(); let writer = Arc::new(Mutex::new(None)); let configuring = Arc::new(AtomicBool::new(true)); let counters = Arc::new(Mutex::new(None)); - let (thread_writer, thread_counters) = (writer.clone(), counters.clone()); + // Connection.initiateServerboundPlayConnection: a transfer's + // handshake says so (ClientIntent.TRANSFER). + let transferred = cookies.is_some(); + let cookies = cookies.unwrap_or_default(); + let (thread_writer, thread_counters, thread_cookies) = + (writer.clone(), counters.clone(), cookies.clone()); let thread_configuring = configuring.clone(); let (address_owned, name_owned) = (address.to_owned(), name.to_owned()); let thread = std::thread::Builder::new() @@ -119,6 +161,8 @@ impl Session { &thread_writer, &thread_configuring, &thread_counters, + &thread_cookies, + transferred, ); let from_server = result.is_ok(); let reason = result.unwrap_or_else(|e| e); @@ -140,6 +184,7 @@ impl Session { address: address.to_owned(), name: name.to_owned(), counters, + cookies, debug, } } @@ -291,6 +336,8 @@ fn run( shared_writer: &Mutex>, configuring: &AtomicBool, shared_counters: &Mutex>>, + cookies: &Cookies, + transferred: bool, ) -> Result { let (host, port, mut c) = if let Some(pipe) = local { (address.to_owned(), 0, Connection::local(pipe)) @@ -321,7 +368,11 @@ fn run( protocol: PROTOCOL_VERSION, host: host.clone(), port, - intent: handshake::LOGIN, + intent: if transferred { + handshake::TRANSFER + } else { + handshake::LOGIN + }, }) .encode(), ) @@ -391,11 +442,14 @@ fn run( ) .map_err(e)?; } + // ClientHandshakePacketListenerImpl.handleRequestCookie: the + // cookies a transfer carried over (none on a plain join). login::Clientbound::CookieRequest(q) => { + let payload = cookies.lock().unwrap().get(&q.key).cloned(); c.send( &login::Serverbound::CookieResponse(login::CookieResponse { key: q.key, - payload: None, + payload, }) .encode(), ) @@ -419,13 +473,13 @@ fn run( .map_err(e)?; let (mut reader, writer) = c.split(); *shared_writer.lock().unwrap() = Some(writer); - let registries = configure(&mut reader, shared_writer, configuring, lang, send)?; + let registries = configure(&mut reader, shared_writer, configuring, lang, send, cookies)?; let _ = send.send(Event::Joined { registries: Arc::new(registries), uuid: profile.uuid, name: profile.name, }); - receive_play(&mut reader, shared_writer, configuring, lang, send) + receive_play(&mut reader, shared_writer, configuring, lang, send, cookies) } fn receive_play( @@ -434,6 +488,7 @@ fn receive_play( configuring: &AtomicBool, lang: &Language, send: &Sender, + cookies: &Cookies, ) -> Result { loop { let (id, payload) = reader.recv().map_err(|e| e.to_string())?; @@ -469,9 +524,31 @@ fn receive_play( let _ = w.send(&play::Serverbound::ConfigurationAcknowledged(Empty).encode()); configuring.store(true, Ordering::Relaxed); } - let registries = configure(reader, shared_writer, configuring, lang, send)?; + let registries = + configure(reader, shared_writer, configuring, lang, send, cookies)?; let _ = send.send(Event::Reconfigured(Arc::new(registries))); } + // ClientCommonPacketListenerImpl.handleRequestCookie: the stored + // cookie (or none) goes straight back. + play::Clientbound::CookieRequest(q) => { + let payload = cookies.lock().unwrap().get(&q.key).cloned(); + reply(play::Serverbound::CookieResponse(cfg::CookieResponse { + key: q.key, + payload, + })); + } + play::Clientbound::StoreCookie(p) => { + cookies.lock().unwrap().insert(p.key, p.payload); + } + // ClientCommonPacketListenerImpl.handleTransfer: the connection + // ends and the game connects to the other address. + play::Clientbound::Transfer(p) => { + let _ = send.send(Event::Transfer { + host: p.host, + port: p.port, + }); + return Ok(tr(lang, "disconnect.transfer")); + } play::Clientbound::Disconnect(d) => return Ok(plain(&d.reason, lang)), play::Clientbound::CustomPayload(p) if p.channel == "minecraft:brand" => { receive_brand(&p, send); @@ -493,6 +570,7 @@ fn configure( configuring: &AtomicBool, lang: &Language, send: &Sender, + cookies: &Cookies, ) -> Result { let mut registries = Dynamic::default(); let reply = |p: cfg::Serverbound| -> Result<(), String> { @@ -525,11 +603,22 @@ fn configure( let _ = send.send(Event::PackPop(p.id)); } cfg::Clientbound::CookieRequest(q) => { + let payload = cookies.lock().unwrap().get(&q.key).cloned(); reply(cfg::Serverbound::CookieResponse(cfg::CookieResponse { key: q.key, - payload: None, + payload, }))? } + cfg::Clientbound::StoreCookie(p) => { + cookies.lock().unwrap().insert(p.key, p.payload); + } + cfg::Clientbound::Transfer(p) => { + let _ = send.send(Event::Transfer { + host: p.host, + port: p.port, + }); + return Err(tr(lang, "disconnect.transfer")); + } cfg::Clientbound::Disconnect(d) => return Err(plain(&d.reason, lang)), cfg::Clientbound::FinishConfiguration(_) => { let mut w = writer.lock().unwrap(); @@ -597,8 +686,14 @@ pub fn json_text(v: &serde_json::Value, lang: &Language) -> String { } } -/// en_us.json from the prepared resources. +/// The language a session resolves server-sent components with: the selected +/// one as `lang` loaded it (`en_us` under it, packs included), so a menu title +/// the server sends as a key follows the language screen. A process with no +/// language set (a dedicated server's log) reads en_us.json. pub fn load_language(resources: &std::path::Path) -> Language { + if let Some(lang) = crate::lang::current() { + return lang.map().clone(); + } std::fs::read(resources.join("assets/minecraft/lang/en_us.json")) .ok() .and_then(|b| serde_json::from_slice(&b).ok()) @@ -659,6 +754,7 @@ mod tests { &AtomicBool::new(false), &Language::new(), &send, + &Cookies::default(), ) }); let mut server = Connection::local(server); @@ -708,6 +804,7 @@ mod tests { address: "local".into(), name: "Player".into(), counters: Arc::new(Mutex::new(None)), + cookies: Cookies::default(), debug: false, }; let (thread_writer, thread_configuring) = @@ -719,6 +816,7 @@ mod tests { &thread_configuring, &Language::new(), &send, + &Cookies::default(), ) }); let mut server = Connection::local(server); @@ -749,6 +847,68 @@ mod tests { assert!(thread.join().unwrap().is_err()); } + #[test] + fn play_answers_cookie_requests_from_stored_cookies_and_follows_transfers() { + let (client, server) = horizon_protocol::connection::Pipe::pair(); + let (mut reader, writer) = Connection::local(client).split(); + let writer = Arc::new(Mutex::new(Some(writer))); + let (send, events) = channel(); + let cookies = Cookies::default(); + let (thread_writer, thread_cookies) = (writer.clone(), cookies.clone()); + let thread = std::thread::spawn(move || { + receive_play( + &mut reader, + &thread_writer, + &AtomicBool::new(false), + &Language::new(), + &send, + &thread_cookies, + ) + }); + let mut server = Connection::local(server); + let ask = |server: &mut Connection, key: &str| { + let request = play::Clientbound::CookieRequest(cfg::CookieRequest { key: key.into() }); + server.send(&request.encode()).unwrap(); + let (id, body) = server.recv().unwrap(); + play::Serverbound::decode(id, &body).unwrap() + }; + // handleRequestCookie answers an unknown key with no payload. + assert_eq!( + ask(&mut server, "proxy:session"), + play::Serverbound::CookieResponse(cfg::CookieResponse { + key: "proxy:session".into(), + payload: None, + }) + ); + let store = play::Clientbound::StoreCookie(cfg::StoreCookie { + key: "proxy:session".into(), + payload: vec![1, 2, 3], + }); + server.send(&store.encode()).unwrap(); + assert_eq!( + ask(&mut server, "proxy:session"), + play::Serverbound::CookieResponse(cfg::CookieResponse { + key: "proxy:session".into(), + payload: Some(vec![1, 2, 3]), + }) + ); + let transfer = play::Clientbound::Transfer(cfg::Transfer { + host: "example.net".into(), + port: 25566, + }); + server.send(&transfer.encode()).unwrap(); + assert!(thread.join().unwrap().is_ok()); + assert!(matches!( + events.try_iter().last(), + Some(Event::Transfer { host, port: 25566 }) if host == "example.net" + )); + // The stored cookie stays for the next connection (TransferState). + assert_eq!( + cookies.lock().unwrap().get("proxy:session"), + Some(&vec![1, 2, 3]) + ); + } + #[test] fn server_brand_payload_is_decoded_and_invalid_or_unrelated_payloads_are_ignored() { let (send, events) = channel(); diff --git a/crates/horizon-client/src/particles.rs b/crates/horizon-client/src/particles.rs index 0d4f4d623..be7f32551 100644 --- a/crates/horizon-client/src/particles.rs +++ b/crates/horizon-client/src/particles.rs @@ -18,7 +18,10 @@ use horizon_core::{ random::{LegacyRandom, RandomSource}, }; use horizon_world::block_collision::BlockCollisionRegistry; -use std::{collections::HashMap, path::Path}; +use std::{ + collections::{HashMap, HashSet}, + path::Path, +}; macro_rules! particle_types { ($($variant:ident = $name:literal),* $(,)?) => { @@ -196,6 +199,35 @@ impl ParticleType { | Self::OminousSpawning ) } + /// `SingleQuadParticle.Layer.translucent`: types whose vanilla particle class + /// hardcodes the translucent layer, drawn after translucent terrain. + /// `TerrainParticle`, `BreakingItemParticle` and `BlockMarker` resolve their + /// layer from the sprite instead (`Layer.bySprite`); see the `translucent` + /// sprite set on the engine. + pub fn translucent_layer(self) -> bool { + match self { + // CampfireSmokeParticle. + Self::CampfireCosySmoke | Self::CampfireSignalSmoke + // PlayerCloudParticle. + | Self::Cloud | Self::Sneeze + // SpellParticle. + | Self::Effect | Self::InstantEffect | Self::EntityEffect + | Self::Witch | Self::Infested | Self::TrialOmen | Self::RaidOmen + // EmissiveRisingParticle. + | Self::Soul | Self::SculkSoul + // SculkChargeParticle, SculkChargePopParticle, ShriekParticle, + // VibrationSignalParticle, FireflyParticle, OverlayParticle. + | Self::SculkCharge | Self::SculkChargePop | Self::Shriek + | Self::Vibration | Self::Firefly | Self::Flash + // SimpleAnimatedParticle and its subclasses: EndRodParticle, + // SparkParticle (firework), SquidInkParticle, TotemParticle. + | Self::EndRod | Self::Firework | Self::SquidInk | Self::GlowSquidInk + | Self::TotemOfUndying + // FlyTowardsPositionParticle: its LifetimeAlpha(0, 0.6, 0.25, 1) is not opaque. + | Self::VaultConnection => true, + _ => false, + } + } } /// `ParticleOptions` with the parameters of the option classes used here. @@ -1147,7 +1179,9 @@ pub struct ParticleEngine { pub requested: Option>, names: Vec, rects: Vec>, - atlas: HashMap, + /// Each named sprite, and whether `SingleQuadParticle.Layer.bySprite` puts + /// it on the translucent layer (a texel with 0 < alpha < 255). + atlas: HashMap, sets: HashMap, set_sprites: Vec>, /// `TrackingEmitter`s: particle type, entity feet position, width, height, ticks left. @@ -1167,7 +1201,7 @@ pub struct ParticleEngine { /// `ParticleGroup.MAX_PARTICLES` (per render group; one group here). const MAX_PARTICLES: usize = 16384; impl ParticleEngine { - pub fn new(root: &Path, atlas: &HashMap) -> Self { + pub fn new(root: &Path, atlas: &HashMap, translucent: &HashSet) -> Self { let mut engine = Self { particles: Vec::new(), pending: Vec::new(), @@ -1178,7 +1212,10 @@ impl ParticleEngine { requested: None, names: Vec::new(), rects: Vec::new(), - atlas: atlas.clone(), + atlas: atlas + .iter() + .map(|(name, &rect)| (name.clone(), (rect, translucent.contains(name)))) + .collect(), sets: HashMap::new(), set_sprites: Vec::new(), emitters: Vec::new(), @@ -1220,7 +1257,7 @@ impl ParticleEngine { return i as u32; } self.names.push(name.to_owned()); - self.rects.push(self.atlas.get(name).copied()); + self.rects.push(self.atlas.get(name).map(|&(rect, _)| rect)); (self.names.len() - 1) as u32 } pub fn len(&self) -> usize { @@ -3760,9 +3797,18 @@ impl ParticleEngine { { continue; } - let rect = match &p.sprite { - Sprite::Index(i) => self.rects.get(*i as usize).copied().flatten(), - Sprite::Named(name) => self.atlas.get(name).copied(), + // `SubmitNodeCollection.submitQuadParticleGroup`: the translucent + // layer draws after translucent terrain, so a crumb of stained + // glass cannot hide the glass behind it (`getLayer`). + let (rect, layer) = match &p.sprite { + Sprite::Index(i) => ( + self.rects.get(*i as usize).copied().flatten(), + p.ty.translucent_layer(), + ), + Sprite::Named(name) => match self.atlas.get(name) { + Some(&(rect, translucent)) => (Some(rect), translucent), + None => (None, false), + }, }; let Some(r) = rect else { continue }; let a = f64::from(partial); @@ -3867,7 +3913,11 @@ impl ParticleEngine { normal: [0; 4], pos: v.to_array(), uv, - layer: 0, + layer: if layer { + crate::gfx::TRANSLUCENT_PARTICLE_OVERLAY + } else { + 0 + }, color, light, }); @@ -3891,7 +3941,7 @@ mod tests { #[test] fn egg_teleport_unpacks_offsets_and_preserves_seeded_draw_order() { - let mut engine = ParticleEngine::new(Path::new(""), &HashMap::new()); + let mut engine = ParticleEngine::new(Path::new(""), &HashMap::new(), &HashSet::new()); engine.seed_randoms(0, 0, 0); // Destination offset (-15, 7, 15); the unused top byte is ignored. engine.block_teleportation([4, 12, -5], 0xff01_0f1fu32 as i32, [16, 8, 16], 0); @@ -3916,7 +3966,7 @@ mod tests { #[test] fn consume_teleport_unpacks_offsets_within_127() { - let mut engine = ParticleEngine::new(Path::new(""), &HashMap::new()); + let mut engine = ParticleEngine::new(Path::new(""), &HashMap::new(), &HashSet::new()); engine.seed_randoms(0, 0, 0); // TeleportRandomlyConsumeEffect: BlockUtil.clampedPackDifferenceInPosition // of the move (-7, -3, 9) with radii 127, from (4, 12, -5) to (-3, 9, 4). @@ -3941,7 +3991,8 @@ mod tests { (2, 0, 99_413_408_083_437), ] { for distant in [false, true] { - let mut engine = ParticleEngine::new(Path::new(""), &HashMap::new()); + let mut engine = + ParticleEngine::new(Path::new(""), &HashMap::new(), &HashSet::new()); engine.seed_randoms(0, 0, 0); if distant { engine.camera = [1000.0; 3]; diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index da5735c17..937519da9 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -488,7 +488,12 @@ pub fn encode_block_entity( entries.push(("cooking_time_spent", Tag::Int(f.cooking_timer))); entries.push(("cooking_total_time", Tag::Int(f.cooking_total))); entries.push(("speed_multiplier", Tag::Float(f.speed.to_bits()))); - entries.push(("RecipesUsed", Tag::Compound(Compound::new()))); + let used: Compound = f + .recipes_used + .iter() + .map(|(id, count)| (id.as_str().into(), Tag::Int(*count as i32))) + .collect(); + entries.push(("RecipesUsed", Tag::Compound(used))); } BlockEntity::Brewing(b) => { entries.push(("Items", encode_items(&b.items))); @@ -640,6 +645,17 @@ pub fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::contain if let Some(speed) = float(tag.get("speed_multiplier")) { f.speed = speed as f32; } + // A save without it owes nothing: the field is newer than some worlds. + if let Some(Tag::Compound(used)) = tag.get("RecipesUsed") { + for (id, count) in used { + let count = match count { + Tag::Int(n) => *n, + _ => continue, + }; + f.recipes_used + .insert(id.to_string_lossy(), count.max(0) as u32); + } + } } BlockEntity::Brewing(b) => { let items = decode_items(tag.get("Items"), 5); @@ -4276,4 +4292,21 @@ mod tests { .unwrap_or(0); assert_eq!(flying_survival, 0); } + + /// `RecipesUsed` is what a furnace owes in experience, so a save that drops it pays the + /// player nothing after a reload. + #[test] + fn a_furnaces_recipes_used_survive_a_save() { + use crate::containers::BlockEntity; + let Some(BlockEntity::Furnace(mut f)) = BlockEntity::for_block("minecraft:furnace") else { + panic!("a furnace"); + }; + f.recipes_used + .insert("minecraft:cooked_porkchop".to_owned(), 3); + let tag = encode_block_entity((1, 64, 2), &BlockEntity::Furnace(f), "minecraft:furnace"); + let Some((_, BlockEntity::Furnace(back))) = decode_block_entity(&tag) else { + panic!("a furnace back"); + }; + assert_eq!(back.recipes_used.get("minecraft:cooked_porkchop"), Some(&3)); + } } diff --git a/crates/horizon-client/src/screen.wgsl b/crates/horizon-client/src/screen.wgsl index e70aeb896..1497b36fa 100644 --- a/crates/horizon-client/src/screen.wgsl +++ b/crates/horizon-client/src/screen.wgsl @@ -8,10 +8,12 @@ // atlas, wrapped as repeat reads it (hud.rs), so the hardware's bilinear // sample is GL_LINEAR with repeat; clamp-to-edge keeps the sample point half // a texel inside. Each sampling has its own pipeline (gfx.rs), so a pixel -// costs what it did with nearest and repeat only: one sample. +// costs what it did with nearest and repeat only: one sample. Each entry +// point reads `atlas` through one sampler only: GLSL (wgpu's OpenGL backend) +// combines a texture with its sampler, and an override would not remove the +// other sampler's branch. @group(0) @binding(2) var nearest_sampler: sampler; @group(0) @binding(4) var linear_sampler: sampler; -override BLUR: bool = false; override CLAMP: bool = false; struct Out { @builtin(position) clip: vec4, @@ -32,28 +34,36 @@ struct Out { out.edges = vec4(rect.xy + half, rect.zw - half); return out; } -fn sample(in: Out) -> vec4 { +// The sprite's atlas coordinates of this pixel. +fn atlas_uv(in: Out) -> vec2 { var f = in.uv; if !CLAMP { // Repeat: one period of the sprite. f = fract(f); } - var uv = in.rect.xy + f * (in.rect.zw - in.rect.xy); + let uv = in.rect.xy + f * (in.rect.zw - in.rect.xy); if CLAMP { - uv = clamp(uv, in.edges.xy, in.edges.zw); + return clamp(uv, in.edges.xy, in.edges.zw); } - if BLUR { - return textureSampleLevel(atlas, linear_sampler, uv, 0.0) * in.color; - } - return textureSampleLevel(atlas, nearest_sampler, uv, 0.0) * in.color; + return uv; } -@fragment fn fs_screen(in: Out) -> @location(0) vec4 { - let c = sample(in); +fn screen(c: vec4) -> vec4 { if c.a <= 0.0 { discard; } return c; } +@fragment fn fs_screen(in: Out) -> @location(0) vec4 { + return screen(textureSampleLevel(atlas, nearest_sampler, atlas_uv(in), 0.0) * in.color); +} +@fragment fn fs_screen_blur(in: Out) -> @location(0) vec4 { + return screen(textureSampleLevel(atlas, linear_sampler, atlas_uv(in), 0.0) * in.color); +} // RenderPipelines.VIGNETTE blends ZERO, ONE_MINUS_SRC_COLOR: output the // texture colour; the blend state darkens the frame by it. @fragment fn fs_vignette(in: Out) -> @location(0) vec4 { - return vec4(sample(in).rgb, 1.0); + let c = textureSampleLevel(atlas, nearest_sampler, atlas_uv(in), 0.0) * in.color; + return vec4(c.rgb, 1.0); +} +@fragment fn fs_vignette_blur(in: Out) -> @location(0) vec4 { + let c = textureSampleLevel(atlas, linear_sampler, atlas_uv(in), 0.0) * in.color; + return vec4(c.rgb, 1.0); } diff --git a/crates/horizon-client/src/screen_draw.rs b/crates/horizon-client/src/screen_draw.rs index 572ee202f..03f979a68 100644 --- a/crates/horizon-client/src/screen_draw.rs +++ b/crates/horizon-client/src/screen_draw.rs @@ -619,26 +619,7 @@ impl TextField { /// `getWordPosition(dir, from)`: past the next space run, or back to the /// start of the word. pub fn word_position(&self, dir: isize, from: usize) -> usize { - let v: Vec = self.value.chars().collect(); - let mut at = from; - if dir > 0 { - at = v - .iter() - .skip(at) - .position(|c| *c == ' ') - .map_or(v.len(), |p| p + at); - while at < v.len() && v[at] == ' ' { - at += 1; - } - } else { - while at > 0 && v[at - 1] == ' ' { - at -= 1; - } - while at > 0 && v[at - 1] != ' ' { - at -= 1; - } - } - at + crate::ui::word_position(&self.value, dir, from) } /// `keyPressed` of a focused, editable box: `Some(changed)` when handled. pub fn key( diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index 55a2f0086..13b19fefc 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -2148,8 +2148,13 @@ mod cover_tests { } /// Every question streaming asks gets the same answer with the grid. + /// + /// A debug build checks every fourth cell of each row, shifted by row and case so the cases still cover every + /// cell's position relative to the areas: the full check took 20 of the ~30 s of the crate's tests. + /// `cargo test --release -p horizon-client --lib the_grid_answers_as_the_scan` checks every cell. #[test] fn the_grid_answers_as_the_scan() { + let stride = if cfg!(debug_assertions) { 4 } else { 1 }; let mut seed = 0x9E37_79B9_7F4A_7C15u64; let forced: HashSet<(i32, i32)> = [(40, -40)].into(); for case in 0..60 { @@ -2165,7 +2170,8 @@ mod cover_tests { let (scan, grid) = (ranges(None), ranges(Some(&cover))); let span = [4, 30, 120][case % 3] + 40; for z in -span..=span { - for x in -span..=span { + let first = -span + (z + case as i32).rem_euclid(stride); + for x in (first..=span).step_by(stride as usize) { for m in 0..=ExtraCover::REACH + 2 { assert_eq!( scan.in_extra(x, z, m), diff --git a/crates/horizon-client/src/text.rs b/crates/horizon-client/src/text.rs index 1254d32f6..4c04c9da5 100644 --- a/crates/horizon-client/src/text.rs +++ b/crates/horizon-client/src/text.rs @@ -428,6 +428,15 @@ impl Text { } out } + /// `resolve_legacy` borrowing `self` when no span holds a `§` code (the + /// common case). + pub fn resolved(&self) -> std::borrow::Cow<'_, Text> { + if self.spans.iter().any(|s| s.text.contains('§')) { + std::borrow::Cow::Owned(self.resolve_legacy()) + } else { + std::borrow::Cow::Borrowed(self) + } + } /// The text without its legacy `§` codes (ChatFormatting.stripFormatting: /// a `§` and a code character, `0`-`9`, `a`-`f`, `k`-`o` or `r`). pub fn strip_legacy(&self) -> Text { @@ -695,4 +704,20 @@ mod tests { assert_eq!(t.spans[1].style.color, Some(GOLD)); assert_eq!(t.slice(1, 2).plain(), "y"); } + #[test] + fn resolved_borrows_clean_text_and_restyles_codes() { + let clean = Text::colored("Steve", GOLD); + assert!(matches!(clean.resolved(), std::borrow::Cow::Borrowed(_))); + // A colour code inside a span clears its decorations (a bold + // "§cSteve" draws "Steve" red, not bold). + let styled = Style { + bold: Some(true), + ..Style::default() + }; + let styled = Text::styled("§cSteve", styled); + let resolved = styled.resolved(); + assert_eq!(resolved.plain(), "Steve"); + assert_eq!(resolved.spans[0].style.color, Some(RED)); + assert_eq!(resolved.spans[0].style.bold, Some(false)); + } } diff --git a/crates/horizon-client/src/ui.rs b/crates/horizon-client/src/ui.rs index 1c6e93067..8987020bf 100644 --- a/crates/horizon-client/src/ui.rs +++ b/crates/horizon-client/src/ui.rs @@ -279,9 +279,17 @@ pub fn centered_text( w: f32, color: u32, ) { - let width = atlas.font.width_text(text); + let text = text.resolved(); + let width = atlas.font.width_text(&text); if width <= w { - batch.text_styled(atlas, text, x + ((w - width) / 2.0).floor(), y, color, true); + batch.text_styled( + atlas, + &text, + x + ((w - width) / 2.0).floor(), + y, + color, + true, + ); } else { // Clip by characters (vanilla scrolls; we show the part that fits). let mut shown = Text::empty(); @@ -821,6 +829,40 @@ fn byte_index(s: &str, chars: usize) -> usize { pub fn char_len(s: &str) -> usize { s.chars().count() } +/// `EditBox.getWordPosition(dir, from)`: the character index `dir` words away +/// from `from`. Forward, past the rest of the word and its spaces; backward, +/// over any spaces to the start of the word. +pub fn word_position(value: &str, dir: isize, from: usize) -> usize { + let chars: Vec = value.chars().collect(); + let mut pos = from.min(chars.len()); + if dir < 0 { + while pos > 0 && chars[pos - 1] == ' ' { + pos -= 1; + } + while pos > 0 && chars[pos - 1] != ' ' { + pos -= 1; + } + } else { + while pos < chars.len() && chars[pos] != ' ' { + pos += 1; + } + while pos < chars.len() && chars[pos] == ' ' { + pos += 1; + } + } + pos +} +/// `EditBox.deleteWords(dir)` for an append-only field, whose caret is at the +/// end: removes the word in that direction. True when the value changed. +pub fn delete_words(value: &mut String, dir: isize) -> bool { + let len = char_len(value); + let to = word_position(value, dir, len); + if to == len { + return false; + } + value.truncate(byte_index(value, to)); + true +} impl crate::clipboard::Editable for EditBox { fn select_all(&mut self) { self.move_cursor_to_end(false); @@ -919,25 +961,7 @@ impl EditBox { } /// EditBox.getWordPosition(dir, from). fn word_position_from(&self, dir: isize, from: usize) -> usize { - let chars: Vec = self.value.chars().collect(); - let mut pos = from.min(chars.len()); - if dir < 0 { - while pos > 0 && chars[pos - 1] == ' ' { - pos -= 1; - } - while pos > 0 && chars[pos - 1] != ' ' { - pos -= 1; - } - } else { - let len = chars.len(); - while pos < len && chars[pos] != ' ' { - pos += 1; - } - while pos < len && chars[pos] == ' ' { - pos += 1; - } - } - pos + word_position(&self.value, dir, from) } pub fn set_cursor(&mut self, pos: usize, select: bool) { self.cursor = pos.min(char_len(&self.value)); @@ -1451,4 +1475,36 @@ mod tests { assert_eq!(e.value, "éx worl"); assert_eq!(e.cursor, 2); } + #[test] + fn word_position_steps_over_a_word_and_its_spaces() { + assert_eq!(word_position("hello world", -1, 11), 6); + assert_eq!(word_position("hello world", -1, 6), 0); + assert_eq!(word_position("hello world", -1, 13), 8); + assert_eq!(word_position("hello world", 1, 5), 8); + // From inside a word: back to its start, forward past it and its spaces. + assert_eq!(word_position("hello world", -1, 8), 6); + assert_eq!(word_position("hello world", 1, 2), 6); + // The ends, and a position past the end. + assert_eq!(word_position("hello", -1, 0), 0); + assert_eq!(word_position("hello", 1, 5), 5); + assert_eq!(word_position("hi", -1, 99), 0); + assert_eq!(word_position("hi", 1, 99), 2); + } + #[test] + fn delete_words_removes_the_word_before_the_end() { + let mut v = "stone bricks".to_string(); + assert!(delete_words(&mut v, -1)); + assert_eq!(v, "stone "); + assert!(delete_words(&mut v, -1)); + assert_eq!(v, ""); + assert!(!delete_words(&mut v, -1)); + // Characters, not bytes. + let mut v = "a é§x".to_string(); + assert!(delete_words(&mut v, -1)); + assert_eq!(v, "a "); + // Nothing after the end to delete. + let mut v = "stone".to_string(); + assert!(!delete_words(&mut v, 1)); + assert_eq!(v, "stone"); + } } diff --git a/crates/horizon-client/src/zip.rs b/crates/horizon-client/src/zip.rs index 22cab132d..7f8c2a614 100644 --- a/crates/horizon-client/src/zip.rs +++ b/crates/horizon-client/src/zip.rs @@ -23,7 +23,8 @@ impl Zip { pub fn open(path: &Path) -> io::Result { Self::from_bytes(fs::read(path)?) } - pub fn from_bytes(data: Vec) -> io::Result { + pub fn from_bytes(mut data: Vec) -> io::Result { + normalize_end(&mut data); let archive = ZipArchive::new(Cursor::new(Arc::from(data))).map_err(invalid)?; let entries = (0..archive.len()) .filter_map(|i| Some((archive.name_for_index(i)?.replace('\\', "/"), i))) @@ -59,6 +60,59 @@ impl Zip { } } +/// The 22 fixed bytes of the end-of-central-directory record. +const END_HEADER: usize = 22; + +/// Java's `ZipFile` ignores the END record's disk numbers and file counts, +/// reading the central directory from `cenpos` to `cenpos + cenlen` whatever +/// they say; the `zip` crate insists the record agrees with the directory. +/// Packs written while streaming (Nexo serves them for gommehd.net, for one) +/// leave every file's local header a stub and the END counts and disk number +/// at 0 and 0xFFFF: archives Java opens but the crate refuses. Count the real +/// directory entries and normalize the record so it does not look multi-disk +/// or empty. +fn normalize_end(data: &mut [u8]) { + if data.len() < END_HEADER { + return; + } + let u16_at = |data: &[u8], at: usize| u16::from_le_bytes(data[at..at + 2].try_into().unwrap()); + let u32_at = |data: &[u8], at: usize| u32::from_le_bytes(data[at..at + 4].try_into().unwrap()); + // The END record's comment can hide the signature: scan back through the + // last 64 KiB, like every zip reader. + let start = data.len().saturating_sub(END_HEADER + u16::MAX as usize); + let mut end = None; + for pos in (start..=data.len().saturating_sub(END_HEADER)).rev() { + if data[pos..pos + 4] == [0x50, 0x4b, 0x05, 0x06] + && pos + END_HEADER + u16_at(data, pos + 20) as usize <= data.len() + { + end = Some(pos); + break; + } + } + let Some(end) = end else { return }; + // The directory's real position and length, as Java derives them; the + // recorded offset is relative to the archive's start. + let cenlen = u32_at(data, end + 12) as usize; + let cenpos = end.saturating_sub(cenlen); + let mut count = 0u16; + let mut p = cenpos; + while p + 46 <= end && data[p..p + 4] == [0x50, 0x4b, 0x01, 0x02] { + count = count.saturating_add(1); + p += 46 + + u16_at(data, p + 28) as usize + + u16_at(data, p + 30) as usize + + u16_at(data, p + 32) as usize; + } + let disk = u16_at(data, end + 4); + let with_directory = u16_at(data, end + 6); + if disk == with_directory && u16_at(data, end + 8) == count && u16_at(data, end + 10) == count { + return; + } + data[end + 4..end + 10].fill(0); + data[end + 8..end + 10].copy_from_slice(&count.to_le_bytes()); + data[end + 10..end + 12].copy_from_slice(&count.to_le_bytes()); +} + #[cfg(test)] mod tests { use super::*; @@ -207,4 +261,61 @@ mod tests { cut.truncate(cut.len() / 2); assert!(Zip::from_bytes(cut).is_err()); } + + /// A pack as a streaming writer serves it (Nexo/gommehd.net): every + /// local header is a 30-byte stub and the END record's disk number and + /// counts are wrong. Java 25's `ZipFile` opens it, with each size either + /// in the directory or as 0xFFFFFFFF plus its Zip64 extra field (without + /// that field it refuses the archive: "invalid zip64 extra len size"). + #[test] + fn reads_a_streamed_archive() { + for zip64 in [false, true] { + let files: [(&str, &[u8]); 2] = [ + ("pack.mcmeta", b"{\"pack\":{}}"), + ("a/b.txt", b"streamed entry"), + ]; + let mut out = Vec::new(); + let mut central = Vec::new(); + for (name, body) in files { + let data = miniz_oxide::deflate::compress_to_vec(body, 6); + let offset = out.len() as u32; + out.extend(0x0403_4b50u32.to_le_bytes()); + out.extend([0; 26]); + out.extend(&data); + let extra: Vec = if zip64 { + [1u16, 8] + .iter() + .flat_map(|v| v.to_le_bytes()) + .chain((body.len() as u64).to_le_bytes()) + .collect() + } else { + Vec::new() + }; + central.extend(0x0201_4b50u32.to_le_bytes()); + central.extend([20, 0, 20, 0, 0, 0]); + central.extend(8u16.to_le_bytes()); + central.extend([0; 8]); + central.extend((data.len() as u32).to_le_bytes()); + central.extend(if zip64 { u32::MAX } else { body.len() as u32 }.to_le_bytes()); + central.extend((name.len() as u16).to_le_bytes()); + central.extend((extra.len() as u16).to_le_bytes()); + central.extend([0; 10]); + central.extend(offset.to_le_bytes()); + central.extend(name.as_bytes()); + central.extend(extra); + } + let start = out.len() as u32; + out.extend(¢ral); + out.extend(0x0605_4b50u32.to_le_bytes()); + out.extend(u16::MAX.to_le_bytes()); + out.extend([0; 6]); + out.extend((central.len() as u32).to_le_bytes()); + out.extend(start.to_le_bytes()); + out.extend([0; 2]); + let zip = Zip::from_bytes(out).unwrap(); + assert_eq!(zip.names().collect::>(), ["a/b.txt", "pack.mcmeta"]); + assert_eq!(zip.read("pack.mcmeta").unwrap(), b"{\"pack\":{}}"); + assert_eq!(zip.read("a/b.txt").unwrap(), b"streamed entry"); + } + } } diff --git a/crates/horizon-client/tests/lan_bot.rs b/crates/horizon-client/tests/lan_bot.rs index 6dd1b8258..34e8265a1 100644 --- a/crates/horizon-client/tests/lan_bot.rs +++ b/crates/horizon-client/tests/lan_bot.rs @@ -278,6 +278,65 @@ fn run( handle.join().unwrap().unwrap() } +#[test] +fn sprint_commands_survive_keyboard_input_changes() { + let Some(resources) = resources() else { + return; + }; + let mut s = server(&resources); + let port = s.hub.lan.port; + let _bot = run(&mut s, move || connect(port, "Sprinter", true)); + let deadline = Instant::now() + Duration::from_secs(5); + while s.hub.guests.is_empty() { + s.tick(&[]); + assert!(Instant::now() < deadline, "guest did not join"); + thread::yield_now(); + } + let id = s.hub.guests[0].entity_id; + s.hub.guests[0].loaded = true; + s.hub.guests[0] + .inbox + .push_back(S::PlayerCommand(play::PlayerCommand { + id, + action: 1, + data: 0, + })); + s.tick(&[]); + assert!(s.hub.guests[0].sprinting); + // LocalPlayer keeps sprinting after Ctrl is released while W remains + // down. Double-tap sprint also sends forward without the sprint key. + for flags in [65, 1, 0, 33] { + s.hub.guests[0] + .inbox + .push_back(S::PlayerInput(play::PlayerInput { flags })); + s.tick(&[]); + let g = &s.hub.guests[0]; + assert!( + g.sprinting, + "keyboard input {flags} cleared the sprint flag" + ); + assert_eq!(g.input, flags); + assert_eq!(g.sneaking, flags & 32 != 0); + } + s.hub.guests[0] + .inbox + .push_back(S::PlayerCommand(play::PlayerCommand { + id, + action: 2, + data: 0, + })); + s.tick(&[]); + assert!(!s.hub.guests[0].sprinting); + s.hub.guests[0] + .inbox + .push_back(S::PlayerInput(play::PlayerInput { flags: 65 })); + s.tick(&[]); + assert!( + !s.hub.guests[0].sprinting, + "keyboard input started a sprint" + ); +} + #[test] fn status_and_join() { let Some(resources) = resources() else { diff --git a/crates/horizon-core/src/paletted.rs b/crates/horizon-core/src/paletted.rs index 802eac225..9aa4aec6a 100644 --- a/crates/horizon-core/src/paletted.rs +++ b/crates/horizon-core/src/paletted.rs @@ -106,14 +106,16 @@ enum Palette { List(Vec), Global, } -/// `n / per_word` for `n < 4096` by multiplication: `MAGIC[bits] = ceil(2^20 / -/// (64 / bits))` is exact while `n * (64 / bits) < 2^20`. -const MAGIC: [u32; 33] = { - let mut table = [0; 33]; +/// `n / per_word` for `n < 4096` by multiplication: `MAGIC[bits].0 = ceil(2^20 / +/// (64 / bits))` is exact while `n * (64 / bits) < 2^20`. `.1` is the bits a +/// word's entries fill (`64 / bits * bits`): looked up, not divided for, as a +/// division by a run-time value costs tens of cycles on x86. +const MAGIC: [(u32, u32); 33] = { + let mut table = [(0, 0); 33]; let mut bits = 1; while bits <= 32 { let per = 64 / bits as u32; - table[bits] = (1u32 << 20).div_ceil(per); + table[bits] = ((1u32 << 20).div_ceil(per), per * bits as u32); bits += 1; } table @@ -134,9 +136,9 @@ fn locate(bits: u8, i: usize) -> (usize, u32) { let word = i >> (6 - log); (word, ((i & ((64 >> log) - 1)) as u32) << log) } else { - let per = 64 / u32::from(bits); - let word = (i as u32 * MAGIC[usize::from(bits)]) >> 20; - (word as usize, (i as u32 - word * per) * u32::from(bits)) + let (magic, filled) = MAGIC[usize::from(bits)]; + let word = (i as u32 * magic) >> 20; + (word as usize, i as u32 * u32::from(bits) - word * filled) } } #[inline] @@ -720,6 +722,17 @@ mod tests { use super::*; const BLOCKS: Strategy = Strategy::blocks(16); const BIOMES: Strategy = Strategy::biomes(7); + + #[test] + fn locate_matches_division_for_every_width() { + for bits in 1..=32u8 { + let per = 64 / usize::from(bits); + for i in 0..4096 { + let expected = (i / per, ((i % per) * usize::from(bits)) as u32); + assert_eq!(locate(bits, i), expected, "{bits} bits, entry {i}"); + } + } + } fn rng(state: &mut u64) -> u64 { *state ^= *state << 13; *state ^= *state >> 7; diff --git a/crates/horizon-dualsense/Cargo.toml b/crates/horizon-dualsense/Cargo.toml index fe05b4f59..feb25026b 100644 --- a/crates/horizon-dualsense/Cargo.toml +++ b/crates/horizon-dualsense/Cargo.toml @@ -7,10 +7,15 @@ rust-version = "1.85" description = "The DualSense's output over HID: rumble, adaptive triggers, lightbar and player LEDs" [dependencies] +log = "0.4" + +# hidapi has no iOS backend and its build script stops there, so the output's two +# dependencies go only to the targets that have one. On iOS the crate builds without +# output: the Link never opens. +[target.'cfg(not(target_os = "ios"))'.dependencies] # MIT; bundles the C hidapi under its BSD licence option. hidraw on Linux (libudev, as gilrs), # IOKit on macOS (opened shared, so gilrs keeps reading the pad), the HID API on Windows. hidapi = { version = "2.6.7", default-features = false, features = ["linux-static-hidraw", "macos-shared-device"] } -log = "0.4" # HD haptics through the pad's own audio device (USB). cpal = "0.16" diff --git a/crates/horizon-dualsense/src/device.rs b/crates/horizon-dualsense/src/device.rs index 97c6ec72b..f8e544ea6 100644 --- a/crates/horizon-dualsense/src/device.rs +++ b/crates/horizon-dualsense/src/device.rs @@ -1,14 +1,21 @@ //! The pad over hidapi, and the thread that writes to it: writes block (a Bluetooth report can //! take milliseconds), so the game only hands over what the pad should do. -use crate::{ - DEFAULT_LIGHTBAR, Output, PLAYER_1, TRIGGER_OFF, TriggerEffect, Vibration, is_dualsense, -}; +#[cfg(not(target_os = "ios"))] +use crate::{DEFAULT_LIGHTBAR, Output, PLAYER_1, Vibration, is_dualsense}; +use crate::{TRIGGER_OFF, TriggerEffect}; +#[cfg(not(target_os = "ios"))] use hidapi::{BusType, HidApi, HidDevice, HidResult}; use std::sync::Arc; -use std::sync::atomic::{AtomicBool, AtomicU8, Ordering}; -use std::sync::mpsc::{self, RecvTimeoutError, Sender}; +#[cfg(not(target_os = "ios"))] +use std::sync::atomic::AtomicBool; +use std::sync::atomic::{AtomicU8, Ordering}; +use std::sync::mpsc::Sender; +#[cfg(not(target_os = "ios"))] +use std::sync::mpsc::{self, RecvTimeoutError}; use std::thread::JoinHandle; -use std::time::{Duration, Instant}; +use std::time::Duration; +#[cfg(not(target_os = "ios"))] +use std::time::Instant; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Transport { @@ -17,6 +24,7 @@ pub enum Transport { } /// An open DualSense. +#[cfg(not(target_os = "ios"))] pub struct DualSense { device: HidDevice, transport: Transport, @@ -34,6 +42,7 @@ pub fn bluetooth_allowed() -> bool { cfg!(target_os = "linux") || std::env::var_os("HORIZON_DUALSENSE_BT").is_some_and(|v| v == "1") } +#[cfg(not(target_os = "ios"))] impl DualSense { /// The first DualSense hidapi lists, USB before Bluetooth (as SDL); `None` when there is /// none, an error when it can't be opened (Linux without access to its hidraw node). @@ -119,13 +128,17 @@ impl Default for State { /// A vibration: the motors' levels (left, right), each for a while, in order. pub type Rumble = Vec<([u8; 2], Duration)>; +// On iOS no thread reads the queue (the Link never opens); the variants stay so the +// Link's API is the same everywhere. +#[cfg_attr(target_os = "ios", allow(dead_code))] enum Message { State(State), Rumble(Rumble), Scan, } -/// The writer thread. Dropping it frees the triggers and stops the motors before it ends. +/// The output thread. Dropping it frees the triggers and stops the motors before it ends; on +/// iOS there is none (no hidapi backend) and the Link stays closed. pub struct Link { tx: Option>, /// 0: no pad open, else 1 + the transport. @@ -135,6 +148,7 @@ pub struct Link { impl Link { /// Start the thread and look for a DualSense. + #[cfg(not(target_os = "ios"))] pub fn start() -> Link { let (tx, rx) = mpsc::channel(); let open = Arc::new(AtomicU8::new(0)); @@ -151,6 +165,16 @@ impl Link { } } + /// No output on iOS: hidapi has no backend there. The Link never opens. + #[cfg(target_os = "ios")] + pub fn start() -> Link { + Link { + tx: None, + open: Arc::new(AtomicU8::new(0)), + thread: None, + } + } + /// The pad's transport while one is open and taking reports. pub fn open(&self) -> Option { match self.open.load(Ordering::Relaxed) { @@ -194,8 +218,10 @@ impl Drop for Link { /// Looks for the pad this many more times, a second apart, after a scan finds nothing (its /// hidraw node may appear after the controller does). +#[cfg(not(target_os = "ios"))] const SCAN_TRIES: u32 = 5; +#[cfg(not(target_os = "ios"))] struct Writer { api: Option, pad: Option, @@ -213,6 +239,7 @@ struct Writer { next_scan: Instant, } +#[cfg(not(target_os = "ios"))] #[derive(Clone, Copy, Debug, PartialEq)] enum Lights { /// Over Bluetooth, until the pad's clock says its animation is over (or this long after @@ -225,11 +252,13 @@ enum Lights { /// The pad's sensor clock once its Bluetooth connection animation is over (SDL_hidapi_ps5.c's /// `HIDAPI_DriverPS5_CheckPendingLEDReset`). +#[cfg(not(target_os = "ios"))] const ANIMATION_OVER: u32 = 10_200_000; /// The sensor clock of a Bluetooth input report 0x31: its state block starts at byte 2 and /// holds the clock at 27..31, little-endian (SDL's `PS5StatePacketCommon_t`, the kernel's /// `dualsense_input_report`). +#[cfg(not(target_os = "ios"))] fn bluetooth_clock(report: &[u8]) -> Option { match report { [0x31, _, state @ ..] if state.len() >= 31 => { @@ -239,6 +268,7 @@ fn bluetooth_clock(report: &[u8]) -> Option { } } +#[cfg(not(target_os = "ios"))] impl Writer { fn new(open: Arc) -> Self { Writer { @@ -451,7 +481,7 @@ impl Writer { } } -#[cfg(test)] +#[cfg(all(test, not(target_os = "ios")))] mod tests { use super::*; diff --git a/crates/horizon-dualsense/src/haptics_stub.rs b/crates/horizon-dualsense/src/haptics_stub.rs new file mode 100644 index 000000000..dc4e61667 --- /dev/null +++ b/crates/horizon-dualsense/src/haptics_stub.rs @@ -0,0 +1,20 @@ +//! No HD haptics on iOS: they play through the pad's own four-channel audio device over USB, +//! which needs cpal, and cpal is left out there with hidapi (see Cargo.toml). + +/// The pad's audio output, playing the queued samples (left, right actuator). +pub struct Haptics; + +impl Haptics { + /// The DualSense's audio device, if one is there with its four channels (USB). A phone + /// has none. + pub fn open() -> Option { + None + } + + /// Play a vibration (heavy and light levels, 0..1, each for a while), replacing the one + /// playing. Nothing plays on iOS. + pub fn play(&self, _steps: &[(f32, f32, std::time::Duration)]) {} + + /// Stop at once. Nothing plays on iOS. + pub fn stop(&self) {} +} diff --git a/crates/horizon-dualsense/src/lib.rs b/crates/horizon-dualsense/src/lib.rs index 438d8f6fe..4ae55de9a 100644 --- a/crates/horizon-dualsense/src/lib.rs +++ b/crates/horizon-dualsense/src/lib.rs @@ -1,12 +1,17 @@ //! The DualSense's output report, built by hand (docs/controllers.md "DualSense output"): the //! compatible-vibration motors, the adaptive triggers, the lightbar and the player LEDs. Gilrs //! keeps reading the pad; this crate only writes to it, over hidapi ([`device`]). +//! On iOS the crate builds without hidapi, which has no backend there: [`Link`] never opens. //! //! The byte layout is the one documented by the Linux kernel's `hid-playstation.c` and SDL's //! `SDL_hidapi_ps5.c`: a 47-byte block shared by both transports, sent as report 0x02 over USB //! and as report 0x31 over Bluetooth, where a sequence tag leads it and a CRC-32 (seeded with //! the byte 0xA2) closes it. pub mod device; +#[cfg(not(target_os = "ios"))] +pub mod haptics; +#[cfg(target_os = "ios")] +#[path = "haptics_stub.rs"] pub mod haptics; pub mod trigger; diff --git a/crates/horizon-mod-sdk/examples/predator-missile/Cargo.toml b/crates/horizon-mod-sdk/examples/predator-missile/Cargo.toml new file mode 100644 index 000000000..a3a77ce0b --- /dev/null +++ b/crates/horizon-mod-sdk/examples/predator-missile/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "predator-missile" +version = "0.1.0" +edition.workspace = true +license.workspace = true +publish = false +description = "Example Horizon mod: a missile you launch with /missile and steer with the mouse" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +horizon-mod-sdk = { path = "../.." } + +[lints] +workspace = true diff --git a/crates/horizon-mod-sdk/examples/predator-missile/horizon-mod.toml b/crates/horizon-mod-sdk/examples/predator-missile/horizon-mod.toml new file mode 100644 index 000000000..3baaf7d87 --- /dev/null +++ b/crates/horizon-mod-sdk/examples/predator-missile/horizon-mod.toml @@ -0,0 +1,8 @@ +id = "predator_missile" +name = "Predator Missile" +version = "0.1.0" +description = "/missile launches a guided missile 60 blocks above you. Steer it with the mouse; it explodes on whatever it hits. Needs cheats (it runs vanilla commands as you)." +authors = ["Horizon"] +api = "0.1" +side = "client" +capabilities = ["game-read", "hud", "chat", "command"] diff --git a/crates/horizon-mod-sdk/examples/predator-missile/src/lib.rs b/crates/horizon-mod-sdk/examples/predator-missile/src/lib.rs new file mode 100644 index 000000000..75ebbbdec --- /dev/null +++ b/crates/horizon-mod-sdk/examples/predator-missile/src/lib.rs @@ -0,0 +1,161 @@ +//! Example Horizon mod (`docs/mods.md`): `/missile` turns you into a guided +//! missile 60 blocks up. You fly along your own look, so the mouse steers; +//! on a block or a mob a stack of primed TNT goes off, and you are put back +//! where you launched. Built from vanilla commands (capability `command`), +//! so it needs cheats. +use horizon_mod_sdk::{ + Mod, Registration, + command::run, + game::{self, Player}, + hud::{self, Corner, Line}, +}; + +/// Blocks per tick (a fired arrow does about 3). +const SPEED: f64 = 1.5; +const ALTITUDE: i32 = 60; +/// Ticks of flight before the missile detonates on its own. +const FUEL: u32 = 30 * 20; +/// Ticks to watch the blast before going back. +const AFTERMATH: u32 = 40; +/// Primed TNT per blast: the stack's explosions add up. +const WARHEAD: usize = 4; +/// What the impact checks print; the mod hears it through the chat hook. +const IMPACT: &str = "Predator: impact"; + +enum State { + Idle, + Flying { from: Player, ticks: u32 }, + Aftermath { from: Player, ticks: u32 }, +} + +struct Predator { + state: State, +} + +fn launch() { + run("gamerule send_command_feedback false"); + run("execute if entity @s[gamemode=creative] run tag @s add pm_creative"); + run("gamemode spectator"); + run(&format!("tp @s ~ ~{ALTITUDE} ~ ~ 60")); + run("effect give @s night_vision 60 0 true"); + // The thermal camera: every mob outlined. + run("effect give @e[type=!player,distance=..256] glowing 60 0 true"); +} + +/// One tick of flight: stop at the surface (the next step would enter a +/// block or a mob), else move one step along the look. +fn fly() { + run(&format!( + "execute at @s unless block ^ ^ ^{SPEED} #minecraft:air run tellraw @s \"{IMPACT}\"" + )); + run(&format!( + "execute at @s positioned ^ ^ ^{SPEED} if entity @e[type=!player,distance=..2] run tellraw @s \"{IMPACT}\"" + )); + run(&format!( + "execute at @s if block ^ ^ ^{SPEED} #minecraft:air run tp @s ^ ^ ^{SPEED}" + )); +} + +fn detonate() { + for _ in 0..WARHEAD { + run("execute at @s run summon tnt ^ ^ ^1 {fuse:0s}"); + } +} + +fn land(from: &Player) { + run(&format!( + "tp @s {} {} {} {} {}", + from.x, from.y, from.z, from.yaw, from.pitch + )); + run("execute if entity @s[tag=pm_creative] run gamemode creative"); + run("execute unless entity @s[tag=pm_creative] run gamemode survival"); + run("execute if entity @s[tag=pm_creative] run tag @s remove pm_creative"); + run("effect clear @s night_vision"); + // shortcut: assumes feedback was on before launch, as in a new world. + run("gamerule send_command_feedback true"); +} + +fn panel(p: &Player, ticks: u32) -> Vec { + let green = 0x7CFF7C; + let left = (FUEL - ticks.min(FUEL)) / 20; + [ + "PREDATOR".to_string(), + format!("ALT {:.0}", p.y), + format!("FUEL {left}s"), + "Mouse to steer, /missile to detonate".to_string(), + ] + .into_iter() + .map(|text| Line { text, color: green }) + .collect() +} + +impl Predator { + fn impact(&mut self) { + if let State::Flying { from, .. } = &self.state { + detonate(); + self.state = State::Aftermath { + from: from.clone(), + ticks: 0, + }; + } + } +} + +impl Mod for Predator { + fn init() -> Result<(Self, Registration), String> { + let hooks = Registration::new().command("missile").on_tick().on_chat(); + Ok((Predator { state: State::Idle }, hooks)) + } + + fn tick(&mut self, _tick: u64) { + match &mut self.state { + State::Idle => {} + State::Flying { ticks, .. } => { + *ticks += 1; + let ticks = *ticks; + if ticks >= FUEL { + return self.impact(); + } + // The first tick waits for the launch teleport. + if ticks > 1 { + fly(); + } + if let Some(p) = game::get_player() { + hud::set_panel(Corner::TopLeft, &panel(&p, ticks)); + } + } + State::Aftermath { from, ticks } => { + *ticks += 1; + if *ticks >= AFTERMATH { + land(from); + hud::clear(); + self.state = State::Idle; + } + } + } + } + + fn chat(&mut self, text: &str) { + if text.contains(IMPACT) { + self.impact(); + } + } + + fn command(&mut self, _name: &str, _args: &str) -> Result, String> { + match self.state { + State::Idle => { + let from = game::get_player().ok_or("Not in a world")?; + launch(); + self.state = State::Flying { from, ticks: 0 }; + Ok(Some("Missile away".into())) + } + State::Flying { .. } => { + self.impact(); + Ok(None) + } + State::Aftermath { .. } => Err("Wait for the blast to clear".into()), + } + } +} + +horizon_mod_sdk::export_mod!(Predator); diff --git a/crates/horizon-mod-sdk/src/lib.rs b/crates/horizon-mod-sdk/src/lib.rs index 8708fb5be..a0afd01cb 100644 --- a/crates/horizon-mod-sdk/src/lib.rs +++ b/crates/horizon-mod-sdk/src/lib.rs @@ -21,7 +21,7 @@ //! ``` //! //! Every interface but `log` is a capability: list the ones the mod uses in -//! `horizon-mod.toml` (`game-read`, `chat`, `hud`, `storage`, `ui`). +//! `horizon-mod.toml` (`game-read`, `chat`, `hud`, `storage`, `command`, `ui`). //! //! Widgets (capability `ui`): a mod shows a screen it owns with //! [`ui::open_screen`] and a page in Options > Mods with @@ -38,7 +38,7 @@ wit_bindgen::generate!({ pub use exports::horizon::mods::events::{Guest as __Guest, Registration}; pub use exports::horizon::mods::ui_events::Guest as __UiEventsGuest; -pub use horizon::mods::{chat, game, hud, log, render, storage, ui}; +pub use horizon::mods::{chat, command, game, hud, log, render, storage, ui}; /// The game's calls into a mod. Only the hooks [`Registration`] asks for /// are called; the others may stay as their defaults. diff --git a/crates/horizon-mod-sdk/wit/horizon.wit b/crates/horizon-mod-sdk/wit/horizon.wit index 64bbac592..7e9395069 100644 --- a/crates/horizon-mod-sdk/wit/horizon.wit +++ b/crates/horizon-mod-sdk/wit/horizon.wit @@ -86,6 +86,14 @@ interface storage { delete: func(key: string); } +/// Capability `command`: vanilla commands run as the player, with the +/// player's permissions, after the hook call returns (8 per call). A mod's +/// own commands are not run this way. +interface command { + /// `line` without its slash; its feedback goes to the chat as if typed. + run: func(line: string); +} + /// Capability `ui`: widgets the game draws with its own menus' widgets, for /// a screen the mod owns and a settings page in Options > Mods. Layout and /// drawing are the game's: a mod says what its widgets hold, never where @@ -270,6 +278,7 @@ world horizon-mod { import chat; import hud; import storage; + import command; export events; } diff --git a/crates/horizon-mods/src/manifest.rs b/crates/horizon-mods/src/manifest.rs index 1880b9f3e..04290b7d4 100644 --- a/crates/horizon-mods/src/manifest.rs +++ b/crates/horizon-mods/src/manifest.rs @@ -78,6 +78,8 @@ pub enum Capability { Chat, Hud, Storage, + /// Runs vanilla commands as the player, with the player's permissions. + Command, /// New blocks and items from the mod's `data/` (API 0.2): no WIT /// interface, the game reads them before the world loads. Registries, @@ -89,11 +91,12 @@ pub enum Capability { Render, } impl Capability { - pub const ALL: [Capability; 7] = [ + pub const ALL: [Capability; 8] = [ Capability::GameRead, Capability::Chat, Capability::Hud, Capability::Storage, + Capability::Command, Capability::Registries, Capability::Ui, Capability::Render, @@ -105,6 +108,7 @@ impl Capability { Capability::Chat => "chat", Capability::Hud => "hud", Capability::Storage => "storage", + Capability::Command => "command", Capability::Registries => "registries", Capability::Ui => "ui", Capability::Render => "render", @@ -122,6 +126,7 @@ impl Capability { Capability::Chat => "show you chat messages", Capability::Hud => "draw text on your screen", Capability::Storage => "save its own settings to disk", + Capability::Command => "run commands as you, with your permissions", Capability::Registries => "add blocks and items to your worlds", Capability::Ui => "show its own screens and a settings page", Capability::Render => "draw on your screen and shake the camera", @@ -154,6 +159,7 @@ impl fmt::Display for Capability { Capability::Chat => "chat", Capability::Hud => "hud", Capability::Storage => "storage", + Capability::Command => "command", Capability::Registries => "registries", Capability::Ui => "ui", Capability::Render => "render", @@ -404,6 +410,10 @@ capabilities = ["game-read", "hud"] storage, Err("uses capability storage, missing from horizon-mod.toml".into()) ); + assert_eq!( + m.check_imports(["horizon:mods/command@0.1.0"]), + Err("uses capability command, missing from horizon-mod.toml".into()) + ); let with_storage = EXAMPLE.replace("\"hud\"", "\"hud\", \"storage\""); let m = Manifest::parse(&with_storage).unwrap(); assert_eq!(m.check_imports(["horizon:mods/storage@0.1.0"]), Ok(())); diff --git a/crates/horizon-mods/src/runtime.rs b/crates/horizon-mods/src/runtime.rs index e19bfbe3f..4be9ce322 100644 --- a/crates/horizon-mods/src/runtime.rs +++ b/crates/horizon-mods/src/runtime.rs @@ -32,7 +32,7 @@ pub use bindings::horizon::mods::{ use bindings::{ HorizonMod, exports::horizon::mods::events::Registration, - horizon::mods::{chat, game, hud, log as mod_log, storage}, + horizon::mods::{chat, command, game, hud, log as mod_log, storage}, }; /// The widget model a `ui` screen or settings page shows (WIT `interface /// ui`), for the game to draw with its menus' own widgets. @@ -94,6 +94,9 @@ pub enum Output { Chat { name: String, text: String }, /// A mod command's feedback, red when `error`. Feedback { text: String, error: bool }, + /// `command.run`: a vanilla command line, without its slash, to run as + /// the player. + Command { line: String }, /// The mod was turned off: it trapped, overran or failed to start. Stopped { name: String, reason: String }, /// `ui.open-screen`: the mod's entry in `entries()`; what it shows is @@ -749,6 +752,18 @@ impl render::Host for State { } } +impl command::Host for State { + fn run(&mut self, line: String) { + let queued = self + .out + .iter() + .filter(|o| matches!(o, Output::Command { .. })) + .count(); + if queued < CHAT_PER_CALL { + self.out.push(Output::Command { line: clip(line) }); + } + } +} impl hud::Host for State { fn set_panel(&mut self, corner: Corner, lines: Vec) { let lines = lines diff --git a/crates/horizon-mods/tests/sandbox.rs b/crates/horizon-mods/tests/sandbox.rs index dac333fe6..6107cdff7 100644 --- a/crates/horizon-mods/tests/sandbox.rs +++ b/crates/horizon-mods/tests/sandbox.rs @@ -1,4 +1,5 @@ -//! Loads real mods built with the SDK: the coords-hud example and a guest +//! Loads real mods built with the SDK: the coords-hud and predator-missile +//! examples and a guest //! that misbehaves on command. Needs `rustup target add wasm32-unknown-unknown`. use horizon_mods::{ Clock, Corner, Mods, Output, Package, Player, Shape, Snapshot, Status, manifest::componentize, @@ -26,7 +27,7 @@ capabilities = ["chat", "ui", "render"] experimental = ["ui", "render"] "#; -/// Builds both mods once and writes their `.hmod` files; returns the folder. +/// Builds the mods once and writes their `.hmod` files; returns the folder. fn mods_dir() -> &'static Path { static DIR: OnceLock = OnceLock::new(); DIR.get_or_init(|| { @@ -39,6 +40,8 @@ fn mods_dir() -> &'static Path { "-p", "coords-hud", "-p", + "predator-missile", + "-p", "horizon-mods-test-guest", "-p", "horizon-mods-test-ui-guest", @@ -54,9 +57,13 @@ fn mods_dir() -> &'static Path { let built = target.join("wasm32-unknown-unknown/release"); let dir = target.join("mods"); std::fs::create_dir_all(&dir).unwrap(); - let example = root.join("crates/horizon-mod-sdk/examples/coords-hud/horizon-mod.toml"); + let example = |name: &str| { + let path = format!("crates/horizon-mod-sdk/examples/{name}/horizon-mod.toml"); + std::fs::read_to_string(root.join(path)).unwrap() + }; let mods = [ - ("coords_hud", std::fs::read_to_string(example).unwrap()), + ("coords_hud", example("coords-hud")), + ("predator_missile", example("predator-missile")), ("horizon_mods_test_guest", GUEST_MANIFEST.to_string()), ("horizon_mods_test_ui_guest", UI_MANIFEST.to_string()), ]; @@ -93,6 +100,36 @@ fn player() -> Snapshot { } } +/// Capability `command`: `/missile` queues vanilla commands, and the chat +/// line its impact check prints sets off the warhead. +#[test] +fn predator_missile_runs_commands() { + let mut mods = load("predator_missile.hmod"); + assert_eq!(mods.entries()[0].status, Status::Running); + mods.set_snapshot(player()); + let commands = |mods: &mut Mods| -> Vec { + mods.drain() + .into_iter() + .filter_map(|o| match o { + Output::Command { line } => Some(line), + _ => None, + }) + .collect() + }; + assert!(mods.command("missile")); + let launch = commands(&mut mods); + assert_eq!(launch.len(), 6); + assert!(launch.contains(&"gamemode spectator".to_string())); + mods.tick(1); + mods.tick(2); + let flight = commands(&mut mods); + assert!(flight.iter().any(|l| l.ends_with("run tp @s ^ ^ ^1.5"))); + mods.chat("Predator: impact"); + let blast = commands(&mut mods); + assert_eq!(blast.len(), 4); + assert!(blast.iter().all(|l| l.contains("summon tnt"))); +} + #[test] fn coords_hud_draws_and_answers() { let mut mods = load("coords_hud.hmod"); diff --git a/crates/horizon-prepare/output-versions.txt b/crates/horizon-prepare/output-versions.txt new file mode 100644 index 000000000..71a531fab --- /dev/null +++ b/crates/horizon-prepare/output-versions.txt @@ -0,0 +1,3 @@ +# OUTPUT_VERSION (src/lib.rs) and the digest of what it prepares (python3 tools/prepare_output.py digest), +# one line per version, never edited: a change to the prepared files appends the next version. +1 0a608bf4115a8ed2e2c4f5f2b2609dc10c54e63479af8e56fabaa10af1060389 diff --git a/crates/horizon-prepare/src/lib.rs b/crates/horizon-prepare/src/lib.rs index c7e443c7d..5ea1121df 100644 --- a/crates/horizon-prepare/src/lib.rs +++ b/crates/horizon-prepare/src/lib.rs @@ -31,6 +31,15 @@ pub type Log<'a> = &'a dyn Fn(&str); /// The manifest's `schema`: the prepared files' layout, which the launcher compares. pub const SCHEMA: i64 = 7; +/// What this preparer makes. The launcher copies a prepared version's resources to a newly installed +/// version whose preparer has the same number (and the same pinned jars), so a release that changes +/// this crate without changing its output asks nobody to prepare again. Bump it, and append the new +/// digest to output-versions.txt, when the prepared files change: CI prepares with every change to +/// the preparer and fails when the output is not the one output-versions.txt gives this number +/// (`python3 tools/prepare_output.py check`). The launcher reads this line from the version +/// folder's sources: keep it on one line, as it is. +pub const OUTPUT_VERSION: u32 = 1; + /// Digest of this crate's sources (build.rs): the launcher prepares a version in-process when /// that version ships these same sources, and runs the version's own binary otherwise. pub const SOURCES_SHA256: &str = env!("HORIZON_PREPARE_SOURCES_SHA256"); @@ -1209,6 +1218,29 @@ mod tests { fs::remove_dir_all(dir).unwrap(); } + #[test] + fn output_versions_end_at_the_output_version() { + let text = include_str!("../output-versions.txt"); + let versions: Vec<(u32, &str)> = text + .lines() + .filter(|l| !l.trim().is_empty() && !l.starts_with('#')) + .map(|l| { + let (n, digest) = l.split_once(' ').unwrap(); + (n.parse().unwrap(), digest.trim()) + }) + .collect(); + let numbers: Vec = versions.iter().map(|v| v.0).collect(); + assert_eq!(numbers, (1..=OUTPUT_VERSION).collect::>()); + for (i, (_, digest)) in versions.iter().enumerate() { + assert_eq!(digest.len(), 64); + assert!(!versions[..i].iter().any(|(_, d)| d == digest)); + } + // The launcher finds the number with this line in a version folder's sources. + let lib = include_str!("lib.rs"); + let line = format!("pub const OUTPUT_VERSION: u32 = {OUTPUT_VERSION};"); + assert!(lib.lines().any(|l| l == line)); + } + #[test] fn the_source_digest_is_the_one_build_rs_computed() { assert_eq!( diff --git a/crates/horizon-protocol/src/item.rs b/crates/horizon-protocol/src/item.rs index 6b6b845f8..abc530278 100644 --- a/crates/horizon-protocol/src/item.rs +++ b/crates/horizon-protocol/src/item.rs @@ -35,9 +35,12 @@ pub mod component { pub const MAP_ID: i32 = 48; pub const MAP_POST_PROCESSING: i32 = 50; pub const CHARGED_PROJECTILES: i32 = 51; + pub const POTION_CONTENTS: i32 = 53; pub const WRITABLE_BOOK_CONTENT: i32 = 56; pub const WRITTEN_BOOK_CONTENT: i32 = 57; pub const TRIM: i32 = 58; + pub const FIREWORK_EXPLOSION: i32 = 70; + pub const FIREWORKS: i32 = 71; } /// A written book's content (`WrittenBookContent.STREAM_CODEC`): the @@ -65,6 +68,13 @@ pub enum Component { Lore(Vec), Enchantments(Vec<(i32, i32)>), AttackRange([f32; 6]), + /// `PotionContents`: the potion's `minecraft:potion` id and the custom + /// colour; the custom effects and name stay their encoded bytes. + Potion { + potion: Option, + color: Option, + rest: Vec, + }, Profile(Profile), CustomModelData { floats: Vec, @@ -81,6 +91,13 @@ pub enum Component { WrittenBook(WrittenBook), /// `ChargedProjectiles`: a loaded crossbow's ammunition, as templates. Stacks(Vec), + /// `Fireworks`: flight duration and its explosions. + Fireworks { + flight: i32, + explosions: Vec, + }, + /// `FireworkExplosion` (a firework star). + FireworkExplosion(FireworkExplosion), /// `ArmorTrim` naming its material and pattern by registry id (`trim_material`, /// `trim_pattern`); a trim with a direct holder stays [`Component::Raw`]. Trim { @@ -92,6 +109,41 @@ pub enum Component { Raw(Vec), } +/// `FireworkExplosion.STREAM_CODEC`: shape id, colors, fade colors, trail +/// and twinkle (FireworkExplosion.Shape's BY_ID order). +#[derive(Clone, Debug, PartialEq)] +pub struct FireworkExplosion { + pub shape: i32, + pub colors: Vec, + pub fade: Vec, + pub trail: bool, + pub twinkle: bool, +} +impl FireworkExplosion { + fn read(r: &mut Reader) -> Result { + let ints = |r: &mut Reader| -> Result> { + let n = r.count(1024)?; + (0..n).map(|_| r.i32()).collect() + }; + Ok(Self { + shape: r.varint()?, + colors: ints(r)?, + fade: ints(r)?, + trail: r.bool()?, + twinkle: r.bool()?, + }) + } + fn write(&self, w: &mut Writer) { + w.varint(self.shape); + w.varint(self.colors.len() as i32); + self.colors.iter().for_each(|c| w.i32(*c)); + w.varint(self.fade.len() as i32); + self.fade.iter().for_each(|c| w.i32(*c)); + w.bool(self.trail); + w.bool(self.twinkle); + } +} + /// `Filterable.streamCodec`: the raw value, then an optional filtered one /// (dropped). fn filterable(r: &mut Reader, value: impl Fn(&mut Reader) -> Result) -> Result { @@ -162,6 +214,22 @@ impl Component { .collect::>()?, ) } + POTION_CONTENTS => { + let potion = if r.bool()? { Some(r.varint()?) } else { None }; + let color = if r.bool()? { Some(r.i32()?) } else { None }; + let start = r.position(); + skip::potion_effects_and_name(r)?; + // No custom effects and no name: an empty tail. + let rest = match r.since(start) { + [0, 0] => Vec::new(), + bytes => bytes.to_vec(), + }; + Self::Potion { + potion, + color, + rest, + } + } ATTACK_RANGE => { let mut values = [0.0f32; 6]; for v in &mut values { @@ -236,6 +304,17 @@ impl Component { hidden: (0..n).map(|_| r.varint()).collect::>()?, } } + FIREWORKS => { + let flight = r.varint()?; + let n = r.count(256)?; + Self::Fireworks { + flight, + explosions: (0..n) + .map(|_| FireworkExplosion::read(r)) + .collect::>()?, + } + } + FIREWORK_EXPLOSION => Self::FireworkExplosion(FireworkExplosion::read(r)?), // ArmorTrim.STREAM_CODEC: two holders, each a reference's id plus // one. A direct material or pattern has no registry name: read past. TRIM => { @@ -283,6 +362,27 @@ impl Component { } } Self::AttackRange(values) => values.iter().for_each(|v| w.f32(*v)), + Self::Potion { + potion, + color, + rest, + } => { + w.bool(potion.is_some()); + if let Some(id) = potion { + w.varint(*id); + } + w.bool(color.is_some()); + if let Some(color) = color { + w.i32(*color); + } + // An empty tail is no custom effects and no name. + if rest.is_empty() { + w.varint(0); + w.bool(false); + } else { + w.bytes(rest); + } + } Self::Profile(p) => { w.u8(0); w.bool(p.name.is_some()); @@ -330,6 +430,12 @@ impl Component { w.varint(stacks.len() as i32); stacks.iter().for_each(|s| Template::write(s, w)); } + Self::Fireworks { flight, explosions } => { + w.varint(*flight); + w.varint(explosions.len() as i32); + explosions.iter().for_each(|e| e.write(w)); + } + Self::FireworkExplosion(e) => e.write(w), Self::WritableBook(pages) => { w.varint(pages.len() as i32); for page in pages { @@ -532,6 +638,51 @@ mod tests { assert_eq!(r.remaining(), 0); } + #[test] + fn potion_contents_round_trip() { + let mut arrow = Stack::new(7, 3); + arrow.added.push(( + component::POTION_CONTENTS, + Component::Potion { + potion: Some(31), + color: None, + rest: Vec::new(), + }, + )); + round::, Slot>(Some(arrow)); + // A custom colour, one custom effect (speed II for 600 ticks, shown, + // no hidden effect) and a custom name survive as they came. + let mut w = Writer::new(); + w.bool(false); + w.bool(true); + w.i32(0x00FF_8800); + w.varint(1); + for v in [1, 1, 600] { + w.varint(v); + } + for b in [false, true, true, false] { + w.bool(b); + } + w.bool(true); + w.string("Tea"); + let mut r = Reader::new(&w.data); + let read = Component::read(component::POTION_CONTENTS, &mut r).unwrap(); + assert_eq!(r.remaining(), 0); + let Component::Potion { + potion, + color, + rest, + } = &read + else { + panic!("{read:?}"); + }; + assert_eq!((*potion, *color), (None, Some(0x00FF_8800))); + assert!(!rest.is_empty()); + let mut again = Writer::new(); + read.write(component::POTION_CONTENTS, &mut again); + assert_eq!(again.data, w.data); + } + #[test] fn stacks_round_trip() { round::, Slot>(None); @@ -580,6 +731,44 @@ mod tests { round::, HashedSlot>(None); } + #[test] + fn fireworks_components_round_trip() { + let mut rocket = Stack::new(42, 1); + rocket.added.push(( + component::FIREWORKS, + Component::Fireworks { + flight: 3, + explosions: vec![ + FireworkExplosion { + shape: 4, + colors: vec![0xff0000, 0x00ff00], + fade: vec![0x0000ff], + trail: true, + twinkle: false, + }, + FireworkExplosion { + shape: 0, + colors: vec![], + fade: vec![], + trail: false, + twinkle: true, + }, + ], + }, + )); + rocket.added.push(( + component::FIREWORK_EXPLOSION, + Component::FireworkExplosion(FireworkExplosion { + shape: 2, + colors: vec![7], + fade: vec![], + trail: false, + twinkle: false, + }), + )); + round::, Slot>(Some(rocket)); + } + #[test] fn charged_projectiles_are_item_stack_templates() { // A crossbow (id 7) loaded with one arrow (id 9): ChargedProjectiles diff --git a/crates/horizon-protocol/src/item/skip.rs b/crates/horizon-protocol/src/item/skip.rs index 152d0f0f1..aa436cc31 100644 --- a/crates/horizon-protocol/src/item/skip.rs +++ b/crates/horizon-protocol/src/item/skip.rs @@ -87,7 +87,6 @@ pub(super) fn component(kind: i32, r: &mut Reader) -> Result<()> { } // BundleContents: templates. 52 => list(r, template), - 53 => potion_contents(r), // SuspiciousStewEffects: effect, duration. 55 => list(r, |r| { varint(r)?; @@ -410,10 +409,9 @@ fn kinetic_weapon(r: &mut Reader) -> Result<()> { optional(r, sound)?; optional(r, sound) } -/// PotionContents: potion, customColor, customEffects, customName. -fn potion_contents(r: &mut Reader) -> Result<()> { - optional(r, varint)?; - optional(r, i32)?; +/// PotionContents' customEffects and customName (`Component::read` reads +/// the potion and colour before them). +pub(super) fn potion_effects_and_name(r: &mut Reader) -> Result<()> { list(r, mob_effect)?; optional(r, string) } diff --git a/crates/horizon-protocol/src/packets/play.rs b/crates/horizon-protocol/src/packets/play.rs index 26bb96316..67373cf4b 100644 --- a/crates/horizon-protocol/src/packets/play.rs +++ b/crates/horizon-protocol/src/packets/play.rs @@ -13,8 +13,9 @@ use crate::{ packets::{ Empty, configuration::{ - ClientInformation, CustomPayload, Disconnect, KeepAlive, Ping, ResourcePackPop, - ResourcePackPush, ResourcePackResponse, UpdateTags, + ClientInformation, CookieRequest, CookieResponse, CustomPayload, Disconnect, KeepAlive, + Ping, ResourcePackPop, ResourcePackPush, ResourcePackResponse, StoreCookie, Transfer, + UpdateTags, }, login::Property, }, @@ -1759,6 +1760,7 @@ packet_enum!(Clientbound, crate::ids::play::clientbound { ContainerSetContent(ContainerSetContent) = CONTAINER_SET_CONTENT, ContainerSetData(ContainerSetData) = CONTAINER_SET_DATA, ContainerSetSlot(ContainerSetSlot) = CONTAINER_SET_SLOT, + CookieRequest(CookieRequest) = COOKIE_REQUEST, CustomPayload(CustomPayload) = CUSTOM_PAYLOAD, Disconnect(Disconnect) = DISCONNECT, DisguisedChat(DisguisedChat) = DISGUISED_CHAT, @@ -1836,6 +1838,7 @@ packet_enum!(Clientbound, crate::ids::play::clientbound { SoundEntity(SoundEntity) = SOUND_ENTITY, StopSound(StopSound) = STOP_SOUND, StartConfiguration(Empty) = START_CONFIGURATION, + StoreCookie(StoreCookie) = STORE_COOKIE, SwingAnimation(SwingAnimation) = SWING_ANIMATION, SystemChat(SystemChat) = SYSTEM_CHAT, TabList(TabList) = TAB_LIST, @@ -1843,6 +1846,7 @@ packet_enum!(Clientbound, crate::ids::play::clientbound { TeleportEntity(TeleportEntity) = TELEPORT_ENTITY, TickingState(TickingState) = TICKING_STATE, TickingStep(TickingStep) = TICKING_STEP, + Transfer(Transfer) = TRANSFER, UpdateAdvancements(UpdateAdvancements) = UPDATE_ADVANCEMENTS, UpdateAttributes(UpdateAttributes) = UPDATE_ATTRIBUTES, LevelParticles(LevelParticles) = LEVEL_PARTICLES, @@ -2223,6 +2227,7 @@ packet_enum!(Serverbound, crate::ids::play::serverbound { ContainerButtonClick(ContainerButtonClick) = CONTAINER_BUTTON_CLICK, ContainerClick(ContainerClick) = CONTAINER_CLICK, ContainerClose(ContainerClose) = CONTAINER_CLOSE, + CookieResponse(CookieResponse) = COOKIE_RESPONSE, CustomPayload(CustomPayload) = CUSTOM_PAYLOAD, EditBook(EditBook) = EDIT_BOOK, Interact(Interact) = INTERACT, diff --git a/crates/horizon-protocol/src/registry.rs b/crates/horizon-protocol/src/registry.rs index f97401670..400c21a37 100644 --- a/crates/horizon-protocol/src/registry.rs +++ b/crates/horizon-protocol/src/registry.rs @@ -102,6 +102,12 @@ impl Dynamic { .get(i) .map(|(n, _)| n.as_str()) } + /// The id of an entry by name, the reverse of `name`. A registry has one + /// entry per id, so the first match is the only one. + pub fn id(&self, registry: &str, name: &str) -> Option { + let list = self.registries.get(registry)?; + list.iter().position(|(n, _)| n == name).map(|i| i as i32) + } pub fn data(&self, registry: &str, id: i32) -> Option<&Tag> { let i = usize::try_from(id).ok()?; self.registries.get(registry)?.get(i)?.1.as_ref() diff --git a/crates/horizon-server/src/supervisor.rs b/crates/horizon-server/src/supervisor.rs index 4de011362..f2e811700 100644 --- a/crates/horizon-server/src/supervisor.rs +++ b/crates/horizon-server/src/supervisor.rs @@ -849,7 +849,17 @@ mod tests { let first = lock(&dir).unwrap(); assert!(lock(&dir).unwrap_err().contains("already running")); drop(first); - lock(&dir).unwrap(); + // A process another test spawns holds a copy of the lock's descriptor between its fork and exec: + // the lock comes back once that child has exec'd. + let mut again = lock(&dir); + for _ in 0..50 { + if again.is_ok() { + break; + } + std::thread::sleep(std::time::Duration::from_millis(20)); + again = lock(&dir); + } + again.unwrap(); std::fs::remove_dir_all(&dir).unwrap(); } } diff --git a/crates/horizon-telemetry-server/Cargo.lock b/crates/horizon-telemetry-server/Cargo.lock index e4e7d060e..9b5fc960d 100644 --- a/crates/horizon-telemetry-server/Cargo.lock +++ b/crates/horizon-telemetry-server/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "adler2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" + [[package]] name = "atomic-waker" version = "1.1.2" @@ -92,6 +98,15 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" +[[package]] +name = "crc32fast" +version = "1.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ba106e844045ddc02c20768ac4b0b7cb45f2cc2ecbd833ac9f5dacb183c2055" +dependencies = [ + "cfg-if", +] + [[package]] name = "displaydoc" version = "0.2.7" @@ -131,6 +146,17 @@ version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aedcfb3409746eddb02b9e19ebda1c3394f759a152e48ee875a0844d1b955484" +[[package]] +name = "flate2" +version = "1.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e634e2e0ebac1ee034020da1ca582e17ffe4e0f5e985823721e168928136dcb" +dependencies = [ + "crc32fast", + "miniz_oxide", + "zlib-rs", +] + [[package]] name = "foldhash" version = "0.1.5" @@ -235,6 +261,7 @@ dependencies = [ "serde_json", "tokio", "tower", + "ureq", ] [[package]] @@ -473,6 +500,16 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" +[[package]] +name = "miniz_oxide" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b63fbc4a50860e98e7b2aa7804ded1db5cbc3aff9193adaff57a6931bf7c4b4c" +dependencies = [ + "adler2", + "simd-adler32", +] + [[package]] name = "mio" version = "1.2.4" @@ -701,6 +738,12 @@ dependencies = [ "libc", ] +[[package]] +name = "simd-adler32" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a219298ac11a56ea9a6d2120044824d6f01aeb034955e7af7bc16858527deea" + [[package]] name = "slab" version = "0.4.12" @@ -851,10 +894,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "02d1a66277ed75f640d608235660df48c8e3c19f3b4edb6a263315626cc3c01d" dependencies = [ "base64", + "flate2", "log", "once_cell", "rustls", "rustls-pki-types", + "serde", + "serde_json", "url", "webpki-roots 0.26.11", ] @@ -1099,6 +1145,12 @@ dependencies = [ "syn", ] +[[package]] +name = "zlib-rs" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b268e58e7c693d7c271f93ffc4ba3b380412554231c85bf61ca7af91042a4112" + [[package]] name = "zmij" version = "1.0.23" diff --git a/crates/horizon-telemetry-server/Cargo.toml b/crates/horizon-telemetry-server/Cargo.toml index 8dbb0b7fd..486648b5f 100644 --- a/crates/horizon-telemetry-server/Cargo.toml +++ b/crates/horizon-telemetry-server/Cargo.toml @@ -19,6 +19,8 @@ rusqlite = { version = "0.37", features = ["bundled"] } serde = { version = "1", features = ["derive"] } serde_json = "1" ruzstd = "0.9" +# Opens the hub's bug reports as GitHub issues (blocking, run off the async workers; rustls). +ureq = { version = "2.12", features = ["json"] } [dev-dependencies] # The uploader the launcher uses, against this server, end to end. diff --git a/crates/horizon-telemetry-server/README.md b/crates/horizon-telemetry-server/README.md index ed54024a1..a401a72b0 100644 --- a/crates/horizon-telemetry-server/README.md +++ b/crates/horizon-telemetry-server/README.md @@ -1,6 +1,6 @@ # horizon-telemetry-server -This server receives what players choose to send (performance summaries, crash and bug reports, benchmark uploads: [docs/telemetry.md](../../docs/telemetry.md)) and stores it in one SQLite file. It serves anonymous totals to the website. It also keeps the live player count from the games' anonymous "I'm playing" pings, in memory only. It is one small Rust process, about 7 MB of memory when idle. +This server receives what players choose to send (performance summaries, crash and bug reports, benchmark uploads: [docs/telemetry.md](../../docs/telemetry.md)) and stores it in one SQLite file. It serves anonymous totals to the website. It also keeps the live player count from the games' anonymous "I'm playing" pings, in memory only, and takes what players send from the hub (playhorizon.dev/hub): votes, bug reports and suggestions. It is one small Rust process, about 7 MB of memory when idle. cargo run --release # in this folder: its own workspace, like the launcher's PORT=8080 HORIZON_TELEMETRY_DB=/data/telemetry.sqlite3 horizon-telemetry-server @@ -17,8 +17,15 @@ It runs at **https://telemetry.playhorizon.dev** (`DEFAULT_ENDPOINT` in `crates/ | `GET /v1/benchmarks/summary` | Players' benchmark uploads grouped by GPU, CPU, render distance and game, for the website's Benchmarks page (below). Cached 10 minutes, same `Access-Control-Allow-Origin`. | | `POST /v1/live` | A game's "I'm playing" ping (JSON, at most 512 bytes). Returns 204. Returns 400 for a bad ping, 413 for one too large, 429 over the address's limit, 503 when it holds `LIVE_MAX_SESSIONS` already. | | `GET /v1/live` | How many play now, rounded: `playing`, `lan`, `versions`, `platforms`. Made again at most every 15 seconds, with the same `Access-Control-Allow-Origin`. | +| `POST /hub/vote` | `{"item": "", "voter": "", "up": true}`: a 👍 on a roadmap item, one per voter; `"up": false` takes it back. Returns `{"item", "votes"}`. 400 for a bad id, 429 over the address's limit. | +| `GET /hub/votes` | `{"votes": {"": n}}`. Made again at most every minute. | +| `POST /hub/report` | A bug report from the hub's form: `{"title", "did", "happened", "expected", "version"?, "system", "name"?}`. Opens a GitHub issue at once (labels `bug` and `from-website`, the `#horizon-bugs` template, "Reported on playhorizon.dev") and returns 201 `{"id", "issue": ""}`. If GitHub fails: 202 `{"id", "issue": null}`, and the upkeep tries again for a week. The same report twice in a day returns the first one's issue. 400 for an empty or too long field, 429 over the address's limit, 503 past the day's cap or without `GITHUB_TOKEN`. | +| `POST /hub/suggest` | `{"text", "item"?, "name"?}` into the suggestion queue. Returns 202 `{"id"}`. Never published: Kira relays it to the maintainers' private channel. | +| `GET /hub/suggestions?after=&limit=100` | The queue, oldest first, for Kira: `{"suggestions": [{"id", "text", "item"?, "name"?, "created_at"}]}`. Needs `Authorization: Bearer `: 401 without it, 404 when the server has none. | | `GET /healthz` | `ok` | +The hub's posts take JSON from the website (`OPTIONS` preflights answer with its origin). A form's hidden `website` field filled in (a bot) gets 202 and is dropped. + The totals are grouped per version, OS, GPU vendor and render distance. Each group has its sessions, install-days, hours played, the 14 frame-time buckets summed, and the mean p50 and p99 frame times. Groups with fewer than 5 install-days are left out. The totals also list crash counts per version. Benchmark uploads are published only through the summary below, never one by one. ## Benchmarks summary @@ -56,6 +63,8 @@ The live count is a table in memory: a session ID, when it last pinged, its vers The day stored is the day the server received an item. The sender's IP address is never written down. The per-address rate limit keeps addresses in memory for an hour at most. +The hub keeps votes as an item ID and the browser's random ID, and reports and suggestions as their text, for `HORIZON_TELEMETRY_HUB_KEEP_DAYS` (90). Its rate limits key their buckets by a hash of the address with a key drawn at each start, in memory. + Raw rows are deleted after `HORIZON_TELEMETRY_RETENTION_DAYS` (180). The daily totals stay; they hold no install ID. ## Settings @@ -73,6 +82,14 @@ All are environment variables: - `HORIZON_TELEMETRY_LIVE_PER_HOUR` per address, pings and reads (600, bursts of 60: ten players behind one address) - `HORIZON_TELEMETRY_LIVE_WINDOW_SECONDS` (120) - `HORIZON_TELEMETRY_LIVE_MAX_SESSIONS` (200 000) +- `HORIZON_TELEMETRY_HUB_PER_HOUR` votes and vote reads per address (600, bursts of 60: a page load is one read, and several players can share an address) +- `HORIZON_TELEMETRY_HUB_POSTS_PER_HOUR` reports and suggestions per address (20, bursts of 2) +- `HORIZON_TELEMETRY_HUB_REPORTS_PER_DAY` from everyone (30): each opens an issue +- `HORIZON_TELEMETRY_HUB_SUGGESTIONS_PER_DAY` from everyone (200) +- `HORIZON_TELEMETRY_HUB_KEEP_DAYS` (90) +- `HORIZON_TELEMETRY_HUB_TOKEN`: the token Kira reads the suggestions with (unset: `GET /hub/suggestions` is off) +- `HORIZON_TELEMETRY_GITHUB_TOKEN`: a fine-grained token with Issues read and write on the repository only (unset: `POST /hub/report` answers 503 and the form points to GitHub and Discord) +- `HORIZON_TELEMETRY_GITHUB_REPO` (`ludovic111/horizon`) ## Hosting diff --git a/crates/horizon-telemetry-server/src/github.rs b/crates/horizon-telemetry-server/src/github.rs new file mode 100644 index 000000000..a800442e7 --- /dev/null +++ b/crates/horizon-telemetry-server/src/github.rs @@ -0,0 +1,128 @@ +//! A website bug report as a GitHub issue: the text made inert (no mentions, no cross-references, no HTML), +//! written as the template Kira's `/report` and `#horizon-bugs` use, and the call that opens it. +use std::time::Duration; + +use serde_json::{Map, Value, json}; + +/// Opens an issue; its URL, or why not. The server's is [`Filer::github`]; tests give their own. +pub type Filer = dyn Fn(&Issue) -> Result + Send + Sync; + +pub struct Issue { + pub title: String, + pub body: String, + pub labels: Vec, +} + +/// Labels every website report gets. +pub const LABELS: [&str; 2] = ["bug", "from-website"]; + +/// Text people wrote, safe in an issue: no HTML, no `@` mentions, no `#12` or issue-URL cross-references, +/// no closing keywords, no code spans to break out of. The same rules as Kira's `neutralise`. +pub fn neutralise(text: &str) -> String { + let mut out = String::with_capacity(text.len()); + let chars: Vec = text.chars().collect(); + let mut i = 0; + while i < chars.len() { + let c = chars[i]; + let next = chars.get(i + 1).copied(); + let word_start = i == 0 || !chars[i - 1].is_alphanumeric(); + match c { + '<' => out.push_str("<"), + '>' => out.push_str(">"), + '`' => out.push('\''), + // A zero-width space after `@` or `#` keeps the text and drops the mention or the link. + '@' | '#' if next.is_some_and(|n| n.is_alphanumeric()) => { + out.push(c); + out.push('\u{200b}'); + } + _ if word_start && closing_keyword(&chars[i..]) => { + out.push(c); + out.push('\u{200b}'); + } + _ => out.push(c), + } + i += 1; + } + // github.com/owner/repo/issues/12 links cross-reference the issue: shown, not linked. + out.replace("github.com/", "github.com\u{200b}/") +} + +fn closing_keyword(rest: &[char]) -> bool { + let word: String = rest + .iter() + .take_while(|c| c.is_alphabetic()) + .flat_map(|c| c.to_lowercase()) + .collect(); + [ + "close", "closes", "closed", "fix", "fixes", "fixed", "resolve", "resolves", "resolved", + ] + .contains(&word.as_str()) +} + +/// Text as a markdown quote, every line prefixed. +fn quote(text: &str) -> String { + text.lines() + .map(|l| format!("> {l}").trim_end().to_owned()) + .collect::>() + .join("\n") +} + +/// The issue for a report's fields (`hub::REPORT`'s names, already checked). +pub fn issue(fields: &Map) -> Issue { + let field = |name: &str| { + fields + .get(name) + .and_then(Value::as_str) + .map(neutralise) + .filter(|s| !s.is_empty()) + .unwrap_or_else(|| "not given".into()) + }; + let title = neutralise(&field("title").replace(['\n', '\r'], " ")); + let text = [ + format!("**What I did:**\n{}", field("did")), + format!("**What happened:**\n{}", field("happened")), + format!("**What I expected:**\n{}", field("expected")), + format!("**Horizon version:** {}", field("version")), + format!("**OS, CPU, GPU:** {}", field("system")), + ] + .join("\n"); + let by = fields + .get("name") + .and_then(Value::as_str) + .map(|n| neutralise(n).replace(['*', '_', '~', '|', '[', ']'], "")) + .filter(|n| !n.is_empty()) + .map(|n| format!(" by **{n}**")) + .unwrap_or_default(); + let body = format!( + "{}\n\nReported on playhorizon.dev{by}\n\nOpened by the bug form on playhorizon.dev/hub. The reporter has this issue's link; they don't get GitHub notifications.", + quote(&text) + ); + Issue { + title, + body, + labels: LABELS.iter().map(|l| (*l).to_owned()).collect(), + } +} + +/// Opens issues in `repo` (`owner/name`) with `token` (a fine-grained token with Issues: write on that repo +/// only). Errors never carry the token or GitHub's answer body. +pub fn github(repo: String, token: String) -> Box { + Box::new(move |issue: &Issue| { + let response = ureq::post(&format!("https://api.github.com/repos/{repo}/issues")) + .set("Authorization", &format!("Bearer {token}")) + .set("Accept", "application/vnd.github+json") + .set("X-GitHub-Api-Version", "2022-11-28") + .set("User-Agent", "horizon-telemetry-server") + .timeout(Duration::from_secs(20)) + .send_json(json!({ "title": issue.title, "body": issue.body, "labels": issue.labels })); + match response { + Ok(response) => response + .into_json::() + .ok() + .and_then(|v| v["html_url"].as_str().map(str::to_owned)) + .ok_or_else(|| "GitHub's answer had no issue URL".into()), + Err(ureq::Error::Status(code, _)) => Err(format!("GitHub answered {code}")), + Err(ureq::Error::Transport(e)) => Err(format!("could not reach GitHub: {}", e.kind())), + } + }) +} diff --git a/crates/horizon-telemetry-server/src/hub.rs b/crates/horizon-telemetry-server/src/hub.rs new file mode 100644 index 000000000..eb92cd692 --- /dev/null +++ b/crates/horizon-telemetry-server/src/hub.rs @@ -0,0 +1,242 @@ +//! The hub's votes: a 👍 on a roadmap item at playhorizon.dev/hub, and the helpers the hub's other +//! endpoints (`intake`) share. roadmap/README.md documents them for Kira and the website. +//! +//! - `POST /hub/vote`: a 👍 on a roadmap item, one per browser (`voter`, a random id the page makes and +//! keeps); `"up": false` takes it back. Answers the item's count. +//! - `GET /hub/votes`: every item's count, cached a minute. +//! +//! Stored: an item id and the browser's random id per vote. Never an IP address: the rate limits key their +//! buckets by a hash of it with a key drawn at start, in memory. +use std::collections::HashMap; +use std::hash::BuildHasher; +use std::sync::Arc; +use std::time::{Duration, Instant}; + +use axum::extract::{Request, State}; +use axum::http::{HeaderValue, StatusCode, header}; +use axum::response::Response; +use rusqlite::params; +use serde_json::{Map, Value, json}; + +use crate::store::Store; +use crate::{App, json_response, peer, take}; + +pub const SCHEMA: &str = " +CREATE TABLE IF NOT EXISTS hub_votes (item TEXT NOT NULL, voter TEXT NOT NULL, PRIMARY KEY (item, voter)) WITHOUT ROWID; +"; + +/// Items with votes, at most: a vote for a new id past this is refused (ids are only checked for shape). +const MAX_ITEMS: i64 = 1000; +/// The largest body accepted: a report's fields at their limits, as JSON, fit. +const MAX_BODY: usize = 24 * 1024; +const VOTES_FOR: Duration = Duration::from_secs(60); + +/// The hub's buckets: keyed by a keyed hash of the address, never the address itself. +pub type HashedBuckets = HashMap; + +/// A roadmap item id's shape: lowercase words joined by dashes (tools/roadmap.py), at most 48 characters. +pub fn item_id(id: &str) -> bool { + (1..=48).contains(&id.len()) + && !id.starts_with('-') + && !id.ends_with('-') + && !id.contains("--") + && id + .bytes() + .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-') +} + +/// A voter id's shape: what `crypto.randomUUID()` and similar make. +fn voter_id(id: &str) -> bool { + (16..=64).contains(&id.len()) + && id + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_') +} + +impl Store { + /// Adds or takes back a vote; the item's count after it. None when the item would be one too many. + pub fn vote(&mut self, item: &str, voter: &str, up: bool) -> rusqlite::Result> { + let tx = self.db.transaction()?; + if up { + let known = tx + .prepare("SELECT 1 FROM hub_votes WHERE item = ?1 LIMIT 1")? + .exists([item])?; + let items: i64 = + tx.query_row("SELECT count(DISTINCT item) FROM hub_votes", [], |r| { + r.get(0) + })?; + if !known && items >= MAX_ITEMS { + return Ok(None); + } + tx.execute( + "INSERT OR IGNORE INTO hub_votes (item, voter) VALUES (?1, ?2)", + params![item, voter], + )?; + } else { + tx.execute( + "DELETE FROM hub_votes WHERE item = ?1 AND voter = ?2", + params![item, voter], + )?; + } + let count = tx.query_row( + "SELECT count(*) FROM hub_votes WHERE item = ?1", + [item], + |r| r.get(0), + )?; + tx.commit()?; + Ok(Some(count)) + } + + /// Every item's count. + pub fn votes(&self) -> rusqlite::Result> { + let mut statement = self + .db + .prepare("SELECT item, count(*) FROM hub_votes GROUP BY item ORDER BY item")?; + let rows = + statement.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, i64>(1)?)))?; + rows.map(|row| row.map(|(item, n)| (item, Value::from(n)))) + .collect() + } +} + +impl App { + pub(crate) fn hashed(&self, req: &Request) -> u64 { + self.hub_key.hash_one(self.client(req.headers(), peer(req))) + } + + /// Takes a token from the address's hub bucket (votes and reads of the counts). + fn allow_hub(&self, req: &Request) -> bool { + take( + &self.hub_buckets, + self.config.hub_per_hour, + self.hashed(req), + ) + } + + /// The hub's share of `maintain`: idle buckets are forgotten. + pub(crate) fn forget_hub_buckets(&self) { + for buckets in [&self.hub_buckets, &self.hub_post_buckets] { + buckets + .lock() + .unwrap_or_else(|e| e.into_inner()) + .retain(|_, (_, at)| at.elapsed() < Duration::from_secs(3600)); + } + } +} + +/// A JSON answer to the website: its origin may read it, nothing caches it. +pub(crate) fn answer(app: &App, code: StatusCode, body: Value) -> Response { + let mut response = json_response(app, body.to_string(), 0); + *response.status_mut() = code; + response + .headers_mut() + .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-store")); + response +} + +pub(crate) fn refuse(app: &App, code: StatusCode, message: &str) -> Response { + answer(app, code, json!({ "error": message })) +} + +/// A browser's preflight for the JSON posts: the website may post them. +pub async fn preflight(State(app): State>) -> Response { + let mut response = answer(&app, StatusCode::NO_CONTENT, Value::Null); + *response.body_mut() = axum::body::Body::empty(); + let headers = response.headers_mut(); + headers.remove(header::CONTENT_TYPE); + headers.insert( + header::ACCESS_CONTROL_ALLOW_METHODS, + HeaderValue::from_static("POST"), + ); + headers.insert( + header::ACCESS_CONTROL_ALLOW_HEADERS, + HeaderValue::from_static("content-type"), + ); + headers.insert( + header::ACCESS_CONTROL_MAX_AGE, + HeaderValue::from_static("86400"), + ); + response +} + +pub(crate) async fn json_body(req: Request) -> Result { + let bytes = axum::body::to_bytes(req.into_body(), MAX_BODY) + .await + .map_err(|_| "too large")?; + serde_json::from_slice(&bytes).map_err(|_| "send JSON") +} + +/// `POST /hub/vote` `{"item", "voter", "up"}`: 200 `{"item", "votes"}`. +pub async fn vote(State(app): State>, req: Request) -> Response { + if !app.allow_hub(&req) { + return refuse(&app, StatusCode::TOO_MANY_REQUESTS, "too many requests"); + } + let body = match json_body(req).await { + Ok(body) => body, + Err(e) => return refuse(&app, StatusCode::BAD_REQUEST, e), + }; + let item = body["item"].as_str().unwrap_or(""); + let voter = body["voter"].as_str().unwrap_or(""); + if !item_id(item) || !voter_id(voter) { + return refuse(&app, StatusCode::BAD_REQUEST, "an item id and a voter id"); + } + let up = body["up"].as_bool().unwrap_or(true); + let counted = app.store().vote(item, voter, up); + match counted { + Ok(Some(votes)) => { + *app.votes_cache.lock().unwrap_or_else(|e| e.into_inner()) = None; + answer( + &app, + StatusCode::OK, + json!({ "item": item, "votes": votes }), + ) + } + Ok(None) => refuse(&app, StatusCode::BAD_REQUEST, "no such item"), + Err(e) => { + eprintln!("horizon-telemetry-server: could not store a vote: {e}"); + refuse(&app, StatusCode::SERVICE_UNAVAILABLE, "try again later") + } + } +} + +/// `GET /hub/votes`: `{"votes": {"": n}}`, made again at most every minute. +pub async fn votes(State(app): State>, req: Request) -> Response { + if !app.allow_hub(&req) { + return refuse(&app, StatusCode::TOO_MANY_REQUESTS, "too many requests"); + } + let json = { + let mut cache = app.votes_cache.lock().unwrap_or_else(|e| e.into_inner()); + match &*cache { + Some((at, json)) if at.elapsed() < VOTES_FOR => json.clone(), + _ => { + let counts = app.store().votes(); + match counts { + Ok(counts) => { + let json = json!({ "votes": counts }).to_string(); + *cache = Some((Instant::now(), json.clone())); + json + } + Err(e) => { + eprintln!("horizon-telemetry-server: could not read the votes: {e}"); + return refuse(&app, StatusCode::SERVICE_UNAVAILABLE, "try again later"); + } + } + } + } + }; + json_response(&app, json, VOTES_FOR.as_secs()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn ids_are_checked_for_shape() { + assert!(item_id("smooth-frames") && item_id("v2")); + for bad in ["", "-a", "a-", "a--b", "Smooth", "a b", &"a".repeat(49)] { + assert!(!item_id(bad), "{bad}"); + } + assert!(voter_id("0b7c3c9e-3f0a-4d8e-9a39-4c1f5a6b7d21") && !voter_id("short")); + } +} diff --git a/crates/horizon-telemetry-server/src/intake.rs b/crates/horizon-telemetry-server/src/intake.rs new file mode 100644 index 000000000..59ce57141 --- /dev/null +++ b/crates/horizon-telemetry-server/src/intake.rs @@ -0,0 +1,370 @@ +//! What players send from the hub (playhorizon.dev/hub) for the maintainers: bug reports and suggestions. +//! roadmap/README.md documents it for Kira and the website. +//! +//! - `POST /hub/report`: a bug report, opened at once as a GitHub issue (labels `bug`, `from-website`) in +//! the template Kira's `/report` uses (`github`). Kept and tried again by the upkeep if GitHub fails. +//! - `POST /hub/suggest`: a suggestion, into the queue. Never published: Kira relays it to the +//! maintainers' private channel. +//! - `GET /hub/suggestions?after=`: the queue for Kira, with the `HORIZON_TELEMETRY_HUB_TOKEN` bearer. +//! +//! Stored: reports and suggestions as their text, for `hub_keep_days`. Never an IP address. +use std::sync::Arc; + +use axum::extract::{Query, Request, State}; +use axum::http::{HeaderMap, HeaderValue, StatusCode, header}; +use axum::response::{IntoResponse, Response}; +use rusqlite::params; +use serde::Deserialize; +use serde_json::{Map, Value, json}; + +use crate::hub::{answer, item_id, json_body, refuse}; +use crate::store::Store; +use crate::{App, github, take, utc_now}; + +pub const SCHEMA: &str = " +CREATE TABLE IF NOT EXISTS hub_queue ( + id INTEGER PRIMARY KEY, kind TEXT NOT NULL, day INTEGER NOT NULL, created TEXT NOT NULL, + state TEXT NOT NULL DEFAULT 'new', issue TEXT NOT NULL DEFAULT '', body TEXT NOT NULL +); +CREATE INDEX IF NOT EXISTS hub_queue_kind ON hub_queue (kind, state, id); +"; + +/// Days the upkeep keeps trying to open a report's issue. +const RETRY_DAYS: u32 = 7; + +/// A field's rules: its name, whether it is needed, its length in characters, whether it may hold lines. +struct Field(&'static str, bool, std::ops::RangeInclusive, bool); + +/// A bug report: the `#horizon-bugs` template's fields. +const REPORT: &[Field] = &[ + Field("title", true, 5..=120, false), + Field("did", true, 3..=2000, true), + Field("happened", true, 3..=2000, true), + Field("expected", true, 3..=2000, true), + Field("version", false, 0..=60, false), + Field("system", true, 3..=200, false), + Field("name", false, 0..=64, false), +]; +const SUGGESTION: &[Field] = &[ + Field("text", true, 10..=1000, true), + Field("item", false, 0..=48, false), + Field("name", false, 0..=64, false), +]; + +/// Text as stored: no control characters but line breaks (where allowed), trimmed. +fn clean(text: &str, lines: bool) -> String { + let text = text.replace("\r\n", "\n"); + let kept: String = text + .chars() + .map(|c| if !lines && c == '\n' { ' ' } else { c }) + .filter(|c| !c.is_control() || (lines && *c == '\n')) + .collect(); + kept.trim().to_owned() +} + +/// The request's fields checked against `rules`: the cleaned object, or what is wrong. +fn fields(body: &Value, rules: &[Field]) -> Result, String> { + let object = body.as_object().ok_or("send a JSON object")?; + let mut out = Map::new(); + for Field(name, needed, length, lines) in rules { + let text = match object.get(*name) { + None | Some(Value::Null) => String::new(), + Some(Value::String(s)) => clean(s, *lines), + Some(_) => return Err(format!("{name} is text")), + }; + if text.is_empty() && !needed { + continue; + } + if !length.contains(&text.chars().count()) { + return Err(format!( + "{name}: {} to {} characters", + length.start(), + length.end() + )); + } + out.insert((*name).to_owned(), Value::String(text)); + } + Ok(out) +} + +impl Store { + /// How many entries of `kind` came in on `day`. + pub fn queued_on(&self, kind: &str, day: u32) -> rusqlite::Result { + self.db.query_row( + "SELECT count(*) FROM hub_queue WHERE kind = ?1 AND day = ?2", + params![kind, day], + |r| r.get(0), + ) + } + + /// Queues an entry: its id, and its issue when the same text already came in since `since` and was + /// opened (a double click, a form sent twice). + pub fn enqueue( + &mut self, + kind: &str, + day: u32, + since: u32, + body: &Map, + ) -> rusqlite::Result<(i64, Option)> { + let body = Value::Object(body.clone()).to_string(); + let same = self + .db + .prepare( + "SELECT id, issue FROM hub_queue WHERE kind = ?1 AND body = ?2 AND day >= ?3 LIMIT 1", + )? + .query_row(params![kind, body, since], |r| { + Ok((r.get::<_, i64>(0)?, r.get::<_, String>(1)?)) + }); + match same { + Ok((id, issue)) => return Ok((id, Some(issue).filter(|i| !i.is_empty()))), + Err(rusqlite::Error::QueryReturnedNoRows) => {} + Err(e) => return Err(e), + } + self.db.execute( + "INSERT INTO hub_queue (kind, day, created, body) VALUES (?1, ?2, ?3, ?4)", + params![kind, day, utc_now(), body], + )?; + Ok((self.db.last_insert_rowid(), None)) + } + + /// Suggestions after `after`, oldest first: `{id, text, item?, name?, created_at}`. + pub fn suggestions(&self, after: i64, limit: u32) -> rusqlite::Result> { + let mut statement = self.db.prepare( + "SELECT id, created, body FROM hub_queue WHERE kind = 'suggestion' AND id > ?1 ORDER BY id LIMIT ?2", + )?; + let rows = statement.query_map(params![after, limit], |r| { + let mut entry: Map = + serde_json::from_str(&r.get::<_, String>(2)?).unwrap_or_default(); + entry.insert("id".into(), r.get::<_, i64>(0)?.into()); + entry.insert("created_at".into(), r.get::<_, String>(1)?.into()); + Ok(Value::Object(entry)) + })?; + rows.collect() + } + + /// Reports whose issue isn't open yet, from `since` on: id and fields. + pub fn unfiled(&self, since: u32) -> rusqlite::Result)>> { + let mut statement = self.db.prepare( + "SELECT id, body FROM hub_queue WHERE kind = 'report' AND state = 'new' AND day >= ?1 ORDER BY id", + )?; + let rows = statement.query_map([since], |r| { + Ok(( + r.get::<_, i64>(0)?, + serde_json::from_str(&r.get::<_, String>(1)?).unwrap_or_default(), + )) + })?; + rows.collect() + } + + /// Marks a report as opened: `issue` is its URL. + pub fn filed(&mut self, id: i64, issue: &str) -> rusqlite::Result<()> { + self.db.execute( + "UPDATE hub_queue SET state = 'filed', issue = ?2 WHERE id = ?1", + params![id, issue], + )?; + Ok(()) + } + + /// Reports from before `retry_from` that never got an issue are given up; entries before `oldest_day` + /// go. + pub fn expire_queue(&mut self, oldest_day: u32, retry_from: u32) -> rusqlite::Result { + self.db.execute( + "UPDATE hub_queue SET state = 'failed' WHERE kind = 'report' AND state = 'new' AND day < ?1", + [retry_from], + )?; + self.db + .execute("DELETE FROM hub_queue WHERE day < ?1", [oldest_day]) + } +} + +impl App { + /// The same for reports and suggestions, a much smaller bucket. + fn allow_post(&self, req: &Request) -> bool { + take( + &self.hub_post_buckets, + self.config.hub_posts_per_hour, + self.hashed(req), + ) + } + + /// Opens the reports whose issue isn't open yet (the upkeep calls it off the async workers; it blocks). + /// Stops at the first failure: GitHub is down or the token is wrong, the next round tries again. + pub fn file_pending(&self) { + let Some(filer) = &self.filer else { return }; + let since = (self.today)().saturating_sub(RETRY_DAYS); + let pending = self.store().unfiled(since).unwrap_or_default(); + for (id, fields) in pending { + match filer(&github::issue(&fields)) { + Ok(url) => self.mark_filed(id, &url), + Err(e) => { + eprintln!("horizon-telemetry-server: report {id} still not opened: {e}"); + return; + } + } + } + } + + fn mark_filed(&self, id: i64, url: &str) { + let marked = self.store().filed(id, url); + if let Err(e) = marked { + eprintln!("horizon-telemetry-server: report {id} opened as {url}, not marked: {e}"); + } + } + + /// The intake's share of `maintain`: entries past `hub_keep_days` go, reports never opened after + /// [`RETRY_DAYS`] are given up. + pub(crate) fn expire_intake(&self) -> rusqlite::Result { + let today = (self.today)(); + self.store().expire_queue( + today.saturating_sub(self.config.hub_keep_days), + today.saturating_sub(RETRY_DAYS), + ) + } +} + +/// What `receive` keeps: the entry's id, its issue if already opened, its fields. +type Received = (i64, Option, Map); + +/// Checks a report or a suggestion and queues it. +async fn receive( + app: &App, + req: Request, + kind: &str, + rules: &[Field], + per_day: u32, +) -> Result> { + let no = |code: StatusCode, message: &str| Box::new(refuse(app, code, message)); + if !app.allow_post(&req) { + return Err(no(StatusCode::TOO_MANY_REQUESTS, "too many requests")); + } + let body = json_body(req) + .await + .map_err(|e| no(StatusCode::BAD_REQUEST, e))?; + // The form's hidden field, which people leave empty and form-filling bots don't: thanked, dropped. + if body["website"].as_str().is_some_and(|s| !s.is_empty()) { + let thanks = answer(app, StatusCode::ACCEPTED, json!({ "id": 0 })); + return Err(Box::new(thanks)); + } + let entry = fields(&body, rules).map_err(|e| no(StatusCode::BAD_REQUEST, &e))?; + if let Some(item) = entry.get("item").and_then(Value::as_str) + && !item_id(item) + { + return Err(no(StatusCode::BAD_REQUEST, "item: a roadmap item id")); + } + let today = (app.today)(); + let mut store = app.store(); + if store.queued_on(kind, today).unwrap_or(u32::MAX) >= per_day { + let full = "too many today: please try again tomorrow"; + return Err(no(StatusCode::SERVICE_UNAVAILABLE, full)); + } + match store.enqueue(kind, today, today.saturating_sub(1), &entry) { + Ok((id, issue)) => Ok((id, issue, entry)), + Err(e) => { + eprintln!("horizon-telemetry-server: could not queue a {kind}: {e}"); + Err(no(StatusCode::SERVICE_UNAVAILABLE, "try again later")) + } + } +} + +/// `POST /hub/report` `{"title", "did", "happened", "expected", "version"?, "system", "name"?}`: 201 +/// `{"id", "issue"}` once the issue is open, 202 `{"id", "issue": null}` when GitHub failed and the upkeep +/// will try again; 503 when reports are off (no GitHub token). +pub async fn report(State(app): State>, req: Request) -> Response { + let Some(filer) = app.filer.clone() else { + return refuse( + &app, + StatusCode::SERVICE_UNAVAILABLE, + "reports are closed here: use GitHub or Discord", + ); + }; + let per_day = app.config.hub_reports_per_day; + let (id, issue, fields) = match receive(&app, req, "report", REPORT, per_day).await { + Ok(received) => received, + Err(response) => return *response, + }; + if let Some(issue) = issue { + return answer( + &app, + StatusCode::CREATED, + json!({ "id": id, "issue": issue }), + ); + } + let opened = tokio::task::spawn_blocking(move || filer(&github::issue(&fields))) + .await + .map_err(|e| e.to_string()) + .and_then(|opened| opened); + match opened { + Ok(url) => { + app.mark_filed(id, &url); + answer(&app, StatusCode::CREATED, json!({ "id": id, "issue": url })) + } + Err(e) => { + eprintln!("horizon-telemetry-server: report {id} not opened yet: {e}"); + answer( + &app, + StatusCode::ACCEPTED, + json!({ "id": id, "issue": null }), + ) + } + } +} + +/// `POST /hub/suggest` `{"text", "item"?, "name"?}`: 202 `{"id"}`. +pub async fn suggest(State(app): State>, req: Request) -> Response { + let per_day = app.config.hub_suggestions_per_day; + match receive(&app, req, "suggestion", SUGGESTION, per_day).await { + Ok((id, ..)) => answer(&app, StatusCode::ACCEPTED, json!({ "id": id })), + Err(response) => *response, + } +} + +#[derive(Deserialize)] +pub struct After { + after: Option, + limit: Option, +} + +/// `GET /hub/suggestions?after=&limit=100` with `Authorization: Bearer `: +/// `{"suggestions": [{"id", "text", "item"?, "name"?, "created_at"}]}`, oldest first. 404 when no token is +/// set, 401 for a wrong one. +pub async fn suggestions( + State(app): State>, + headers: HeaderMap, + Query(after): Query, +) -> Response { + let Some(token) = app.config.hub_token.as_deref() else { + return StatusCode::NOT_FOUND.into_response(); + }; + let sent = headers + .get(header::AUTHORIZATION) + .and_then(|v| v.to_str().ok()) + .and_then(|v| v.strip_prefix("Bearer ")) + .unwrap_or(""); + if !same(sent.as_bytes(), token.as_bytes()) { + return StatusCode::UNAUTHORIZED.into_response(); + } + let limit = after.limit.unwrap_or(100).clamp(1, 500); + let listed = app.store().suggestions(after.after.unwrap_or(0), limit); + match listed { + Ok(list) => ( + [ + ( + header::CONTENT_TYPE, + HeaderValue::from_static("application/json"), + ), + (header::CACHE_CONTROL, HeaderValue::from_static("no-store")), + ], + json!({ "suggestions": list }).to_string(), + ) + .into_response(), + Err(e) => { + eprintln!("horizon-telemetry-server: could not read the suggestions: {e}"); + StatusCode::SERVICE_UNAVAILABLE.into_response() + } + } +} + +/// Equal, in a time that doesn't depend on where they differ. +fn same(a: &[u8], b: &[u8]) -> bool { + a.len() == b.len() && a.iter().zip(b).fold(0, |diff, (x, y)| diff | (x ^ y)) == 0 +} diff --git a/crates/horizon-telemetry-server/src/lib.rs b/crates/horizon-telemetry-server/src/lib.rs index 1a8e750e6..3ab923969 100644 --- a/crates/horizon-telemetry-server/src/lib.rs +++ b/crates/horizon-telemetry-server/src/lib.rs @@ -7,6 +7,7 @@ //! least 3 installs only (`summary`), cached; the website's Benchmarks page reads it. //! - `POST /v1/live`: a game's anonymous "I'm playing" ping (`live`, in memory only). //! - `GET /v1/live`: how many play now, rounded, cached briefly. +//! - `/hub/…`: the hub's votes (`hub`), bug reports and suggestions (`intake`). //! - `GET /healthz`. //! //! Abuse limits: a token bucket per IP address (kept in memory for an hour, never stored), a @@ -15,6 +16,7 @@ //! rows expire after `retention_days`; the daily totals stay. One small process and one SQLite //! file: see `README.md` for running it. use std::collections::HashMap; +use std::hash::{Hash, RandomState}; use std::net::{IpAddr, SocketAddr}; use std::sync::{Arc, Mutex}; use std::time::{Duration, Instant}; @@ -27,10 +29,14 @@ use axum::routing::{delete, get, post}; use horizon_telemetry::batch; pub mod classes; +pub mod github; +pub mod hub; +pub mod intake; pub mod live; pub mod store; pub mod summary; +use github::Filer; use horizon_telemetry::live::{MAX_PING_BYTES, Ping}; use live::Live; use store::Store; @@ -56,6 +62,22 @@ pub struct Config { pub live_window: Duration, /// Sessions the live count holds at most; a new one past that is refused. pub live_max_sessions: usize, + /// The hub's votes and reads of their counts an IP address may make an hour. + pub hub_per_hour: f64, + /// Bug reports and suggestions together an IP address may send an hour. + pub hub_posts_per_hour: f64, + /// Bug reports taken a day, from everyone: each one opens a GitHub issue. + pub hub_reports_per_day: u32, + /// Suggestions taken a day, from everyone. + pub hub_suggestions_per_day: u32, + /// Days reports and suggestions are kept (Kira has relayed them long before). + pub hub_keep_days: u32, + /// The bearer token `GET /hub/suggestions` wants (Kira's); unset, it answers 404. + pub hub_token: Option, + /// Opens the website's bug reports as issues; unset, `POST /hub/report` answers 503. + pub github_token: Option, + /// Where those issues go, `owner/name`. + pub github_repo: String, } impl Default for Config { @@ -70,6 +92,14 @@ impl Default for Config { live_per_hour: 600.0, live_window: horizon_telemetry::live::WINDOW, live_max_sessions: 200_000, + hub_per_hour: 600.0, + hub_posts_per_hour: 20.0, + hub_reports_per_day: 30, + hub_suggestions_per_day: 200, + hub_keep_days: 90, + hub_token: None, + github_token: None, + github_repo: "ludovic111/horizon".into(), } } } @@ -77,9 +107,16 @@ impl Default for Config { impl Config { /// The defaults, with `HORIZON_TELEMETRY_RETENTION_DAYS`, `…_MIN_INSTALL_DAYS`, /// `…_REQUESTS_PER_HOUR`, `…_ITEMS_PER_DAY`, `…_TRUST_PROXY` (1), `…_ALLOW_ORIGIN`, - /// `…_LIVE_PER_HOUR`, `…_LIVE_WINDOW_SECONDS` and `…_LIVE_MAX_SESSIONS`. + /// `…_LIVE_PER_HOUR`, `…_LIVE_WINDOW_SECONDS`, `…_LIVE_MAX_SESSIONS`, `…_HUB_PER_HOUR`, + /// `…_HUB_POSTS_PER_HOUR`, `…_HUB_REPORTS_PER_DAY`, `…_HUB_SUGGESTIONS_PER_DAY`, `…_HUB_KEEP_DAYS`, + /// `…_HUB_TOKEN`, `…_GITHUB_TOKEN` and `…_GITHUB_REPO`. Empty tokens count as unset. pub fn from_env() -> Self { let var = |k: &str| std::env::var(format!("HORIZON_TELEMETRY_{k}")).ok(); + let secret = |k: &str| { + var(k) + .map(|v| v.trim().to_owned()) + .filter(|v| !v.is_empty()) + }; let d = Self::default(); Self { retention_days: var("RETENTION_DAYS") @@ -105,6 +142,24 @@ impl Config { live_max_sessions: var("LIVE_MAX_SESSIONS") .and_then(|v| v.parse().ok()) .unwrap_or(d.live_max_sessions), + hub_per_hour: var("HUB_PER_HOUR") + .and_then(|v| v.parse().ok()) + .unwrap_or(d.hub_per_hour), + hub_posts_per_hour: var("HUB_POSTS_PER_HOUR") + .and_then(|v| v.parse().ok()) + .unwrap_or(d.hub_posts_per_hour), + hub_reports_per_day: var("HUB_REPORTS_PER_DAY") + .and_then(|v| v.parse().ok()) + .unwrap_or(d.hub_reports_per_day), + hub_suggestions_per_day: var("HUB_SUGGESTIONS_PER_DAY") + .and_then(|v| v.parse().ok()) + .unwrap_or(d.hub_suggestions_per_day), + hub_keep_days: var("HUB_KEEP_DAYS") + .and_then(|v| v.parse().ok()) + .unwrap_or(d.hub_keep_days), + hub_token: secret("HUB_TOKEN"), + github_token: secret("GITHUB_TOKEN"), + github_repo: var("GITHUB_REPO").unwrap_or(d.github_repo), } } } @@ -126,6 +181,16 @@ pub struct App { summary_cache: Mutex>, /// Today, in days since 1970 (a test moves it). today: Box u32 + Send + Sync>, + /// The key the hub's buckets hash addresses with, drawn at start and never stored. + hub_key: RandomState, + /// The hub's votes and vote counts, per hashed address. + hub_buckets: Mutex, + /// The hub's reports and suggestions, per hashed address. + hub_post_buckets: Mutex, + /// `GET /hub/votes`'s answer and when it was made. + votes_cache: Mutex>, + /// Opens bug reports as GitHub issues; None without a token. + filer: Option>, } type Buckets = HashMap; @@ -145,6 +210,20 @@ impl App { store: Store, config: Config, today: Box u32 + Send + Sync>, + ) -> Arc { + let filer = config + .github_token + .clone() + .map(|token| Arc::from(github::github(config.github_repo.clone(), token))); + Self::with_filer(store, config, today, filer) + } + + /// [`Self::with_clock`] with the hub's bug reports opened by `filer` (tests give their own). + pub fn with_filer( + store: Store, + config: Config, + today: Box u32 + Send + Sync>, + filer: Option>, ) -> Arc { let live = Live::new(config.live_window, config.live_max_sessions); Arc::new(Self { @@ -157,6 +236,11 @@ impl App { cache: Mutex::default(), summary_cache: Mutex::default(), today, + hub_key: RandomState::new(), + hub_buckets: Mutex::default(), + hub_post_buckets: Mutex::default(), + votes_cache: Mutex::default(), + filer, }) } @@ -217,10 +301,13 @@ impl App { } /// Daily upkeep: totals rebuilt, expired rows removed, idle rate-limit entries and live - /// sessions forgotten. + /// sessions forgotten, the hub's old reports and suggestions removed. Reports still waiting for + /// their issue are [`Self::file_pending`]'s. pub fn maintain(&self) -> rusqlite::Result { let oldest = (self.today)().saturating_sub(self.config.retention_days); let removed = self.store().expire(oldest)?; + self.expire_intake()?; + self.forget_hub_buckets(); for buckets in [&self.buckets, &self.live_buckets] { buckets .lock() @@ -235,14 +322,14 @@ impl App { } } -/// Takes a token for `ip` from its bucket in `buckets` (`per_hour`, bursts of a tenth of it); +/// Takes a token for `key` from its bucket in `buckets` (`per_hour`, bursts of a tenth of it); /// false when it has none left. -fn take(buckets: &Mutex, per_hour: f64, ip: IpAddr) -> bool { +fn take(buckets: &Mutex>, per_hour: f64, key: K) -> bool { let rate = per_hour / 3600.0; let burst = (per_hour / 10.0).max(1.0); let mut buckets = buckets.lock().unwrap_or_else(|e| e.into_inner()); let now = Instant::now(); - let (tokens, at) = buckets.entry(ip).or_insert((burst, now)); + let (tokens, at) = buckets.entry(key).or_insert((burst, now)); *tokens = (*tokens + now.duration_since(*at).as_secs_f64() * rate).min(burst); *at = now; if *tokens < 1.0 { @@ -482,6 +569,14 @@ pub fn router(app: Arc) -> Router { .route("/v1/installs/{id}", delete(forget)) .route("/v1/aggregate", get(aggregate)) .route("/v1/benchmarks/summary", get(benchmarks)) + .route("/hub/vote", post(hub::vote).options(hub::preflight)) + .route("/hub/votes", get(hub::votes)) + .route("/hub/report", post(intake::report).options(hub::preflight)) + .route( + "/hub/suggest", + post(intake::suggest).options(hub::preflight), + ) + .route("/hub/suggestions", get(intake::suggestions)) .route("/healthz", get(|| async { "ok" })) .with_state(app) } diff --git a/crates/horizon-telemetry-server/src/main.rs b/crates/horizon-telemetry-server/src/main.rs index 9b9c702c3..728846a0f 100644 --- a/crates/horizon-telemetry-server/src/main.rs +++ b/crates/horizon-telemetry-server/src/main.rs @@ -22,6 +22,9 @@ async fn main() { if let Err(e) = upkeep.maintain() { eprintln!("horizon-telemetry-server: upkeep failed: {e}"); } + // The hub's reports GitHub refused before: their issues, off the async workers. + let filing = upkeep.clone(); + let _ = tokio::task::spawn_blocking(move || filing.file_pending()).await; } }); let listener = tokio::net::TcpListener::bind(SocketAddr::from(([0, 0, 0, 0], port))) diff --git a/crates/horizon-telemetry-server/src/store.rs b/crates/horizon-telemetry-server/src/store.rs index 02c8803ae..9286dd36a 100644 --- a/crates/horizon-telemetry-server/src/store.rs +++ b/crates/horizon-telemetry-server/src/store.rs @@ -123,13 +123,15 @@ pub fn gpu_vendor(gpu: &str) -> &'static str { } pub struct Store { - db: Connection, + pub(crate) db: Connection, } impl Store { pub fn open(path: &str) -> rusqlite::Result { let mut db = Connection::open(path)?; db.execute_batch(SCHEMA)?; + db.execute_batch(crate::hub::SCHEMA)?; + db.execute_batch(crate::intake::SCHEMA)?; migrate(&mut db)?; Ok(Self { db }) } diff --git a/crates/horizon-telemetry-server/tests/github.rs b/crates/horizon-telemetry-server/tests/github.rs new file mode 100644 index 000000000..81f480b58 --- /dev/null +++ b/crates/horizon-telemetry-server/tests/github.rs @@ -0,0 +1,41 @@ +//! A website bug report as the issue GitHub gets: the template, and people's text made inert. +use horizon_telemetry_server::github::{issue, neutralise}; +use serde_json::{Map, Value, json}; + +#[test] +fn people_text_mentions_and_references_nothing() { + let text = + neutralise("@octocat see #12 and https://github.com/a/b/issues/3 `x` Fixes it"); + assert!(!text.contains("@o") && !text.contains("#1") && !text.contains("github.com/")); + assert!(!text.contains('<') && !text.contains('`')); + assert!(text.contains("F\u{200b}ixes")); + // Words that only start like a keyword stay as they are. + assert_eq!(neutralise("closet fixture"), "closet fixture"); +} + +#[test] +fn a_report_reads_as_the_template() { + let fields: Map = serde_json::from_value(json!({ + "title": "Water is black\non AMD", "did": "Opened a world", "happened": "Black water", + "expected": "Blue water", "system": "Windows 11, RX 6600", "name": "steve_" + })) + .unwrap(); + let issue = issue(&fields); + assert_eq!(issue.title, "Water is black on AMD"); + assert!( + issue + .body + .starts_with("> **What I did:**\n> Opened a world\n> **What happened:**") + ); + assert!( + issue + .body + .contains("> **Horizon version:** not given\n> **OS, CPU, GPU:** Windows 11, RX 6600") + ); + assert!( + issue + .body + .contains("Reported on playhorizon.dev by **steve**") + ); + assert_eq!(issue.labels, ["bug", "from-website"]); +} diff --git a/crates/horizon-telemetry-server/tests/hub.rs b/crates/horizon-telemetry-server/tests/hub.rs new file mode 100644 index 000000000..056978212 --- /dev/null +++ b/crates/horizon-telemetry-server/tests/hub.rs @@ -0,0 +1,398 @@ +//! The hub's intake through its HTTP interface: votes, bug reports (opened as issues by a fake GitHub), +//! suggestions and the queue Kira reads. +use std::sync::atomic::{AtomicU32, Ordering}; +use std::sync::{Arc, Mutex}; + +use axum::body::Body; +use axum::http::{Request, StatusCode}; +use horizon_telemetry_server::github::{Filer, Issue}; +use horizon_telemetry_server::store::Store; +use horizon_telemetry_server::{App, Config, router}; +use http_body_util::BodyExt; +use serde_json::{Value, json}; +use tower::ServiceExt; + +const DAY: u32 = 20_741; +const VOTER: &str = "0b7c3c9e-3f0a-4d8e-9a39-4c1f5a6b7d21"; + +/// The issues the fake GitHub opened, and whether it is up. +#[derive(Default)] +struct GitHub { + opened: Mutex>, + down: Mutex, +} + +struct Hub { + app: Arc, + github: Arc, + day: Arc, +} + +fn hub(config: Config) -> Hub { + let github = Arc::new(GitHub::default()); + let fake = github.clone(); + let filer: Arc = Arc::new(move |issue: &Issue| { + if *fake.down.lock().unwrap() { + return Err("GitHub answered 502".into()); + } + let mut opened = fake.opened.lock().unwrap(); + opened.push(Issue { + title: issue.title.clone(), + body: issue.body.clone(), + labels: issue.labels.clone(), + }); + Ok(format!( + "https://github.com/ludovic111/horizon/issues/{}", + 1000 + opened.len() + )) + }); + let day = Arc::new(AtomicU32::new(DAY)); + let clock = day.clone(); + let app = App::with_filer( + Store::open(":memory:").unwrap(), + config, + Box::new(move || clock.load(Ordering::SeqCst)), + Some(filer), + ); + Hub { app, github, day } +} + +fn config() -> Config { + Config { + hub_token: Some("kira-secret".into()), + hub_posts_per_hour: 1000.0, + ..Config::default() + } +} + +impl Hub { + async fn send(&self, req: Request) -> (StatusCode, Value) { + let res = router(self.app.clone()).oneshot(req).await.unwrap(); + let code = res.status(); + let body = res.into_body().collect().await.unwrap().to_bytes(); + (code, serde_json::from_slice(&body).unwrap_or(Value::Null)) + } + + async fn post(&self, path: &str, body: Value) -> (StatusCode, Value) { + self.send( + Request::post(path) + .header("content-type", "application/json") + .body(Body::from(body.to_string())) + .unwrap(), + ) + .await + } + + async fn get(&self, path: &str, token: Option<&str>) -> (StatusCode, Value) { + let mut req = Request::get(path); + if let Some(token) = token { + req = req.header("authorization", format!("Bearer {token}")); + } + self.send(req.body(Body::empty()).unwrap()).await + } +} + +fn report(title: &str) -> Value { + json!({ + "title": title, + "did": "Opened a new world\nWalked to the sea", + "happened": "The water is black", + "expected": "Blue water, as in vanilla", + "version": "v0.2.57", + "system": "Windows 11, Ryzen 5 5600, RX 6600", + "website": "", + }) +} + +#[tokio::test] +async fn votes_count_once_per_browser_and_can_be_taken_back() { + let hub = hub(config()); + let vote = |up: bool, voter: &str| json!({ "item": "smooth-frames", "voter": voter, "up": up }); + assert_eq!( + hub.post("/hub/vote", vote(true, VOTER)).await, + ( + StatusCode::OK, + json!({ "item": "smooth-frames", "votes": 1 }) + ) + ); + assert_eq!( + hub.post("/hub/vote", vote(true, VOTER)).await.1["votes"], + 1, + "a second vote changes nothing" + ); + let other = "f".repeat(32); + assert_eq!( + hub.post("/hub/vote", vote(true, &other)).await.1["votes"], + 2 + ); + assert_eq!( + hub.get("/hub/votes", None).await, + (StatusCode::OK, json!({ "votes": { "smooth-frames": 2 } })) + ); + assert_eq!( + hub.post("/hub/vote", vote(false, VOTER)).await.1["votes"], + 1 + ); + assert_eq!( + hub.get("/hub/votes", None).await.1["votes"]["smooth-frames"], + 1, + "a vote drops the cached counts" + ); + for bad in [ + json!({ "item": "Not An Id", "voter": VOTER }), + json!({ "item": "a", "voter": "short" }), + json!([]), + ] { + assert_eq!(hub.post("/hub/vote", bad).await.0, StatusCode::BAD_REQUEST); + } +} + +#[tokio::test] +async fn a_report_becomes_an_issue_in_the_bug_template() { + let hub = hub(config()); + let (code, body) = hub + .post("/hub/report", report("Water is black on AMD")) + .await; + assert_eq!(code, StatusCode::CREATED); + assert_eq!( + body["issue"], + "https://github.com/ludovic111/horizon/issues/1001" + ); + let issue = hub.github.opened.lock().unwrap().remove(0); + assert_eq!(issue.title, "Water is black on AMD"); + assert_eq!(issue.labels, ["bug", "from-website"]); + for line in [ + "> **What I did:**\n> Opened a new world\n> Walked to the sea", + "> **What happened:**\n> The water is black", + "> **What I expected:**\n> Blue water, as in vanilla", + "> **Horizon version:** v0.2.57", + "> **OS, CPU, GPU:** Windows 11, Ryzen 5 5600, RX 6600", + "Reported on playhorizon.dev", + ] { + assert!(issue.body.contains(line), "{line}\n---\n{}", issue.body); + } + // The same report again (a double click) answers the same issue and opens nothing. + let (code, again) = hub + .post("/hub/report", report("Water is black on AMD")) + .await; + assert_eq!( + (code, &again["issue"]), + (StatusCode::CREATED, &body["issue"]) + ); + assert!(hub.github.opened.lock().unwrap().is_empty()); +} + +#[tokio::test] +async fn reports_need_every_template_field_and_skip_bots() { + let hub = hub(config()); + for empty in ["title", "did", "happened", "expected", "system"] { + let mut body = report("Water is black on AMD"); + body[empty] = json!(" "); + let (code, answer) = hub.post("/hub/report", body).await; + assert_eq!(code, StatusCode::BAD_REQUEST, "{empty}"); + assert!(answer["error"].as_str().unwrap().starts_with(empty)); + } + let mut bot = report("Cheap watches"); + bot["website"] = json!("https://spam.example"); + assert_eq!( + hub.post("/hub/report", bot).await, + (StatusCode::ACCEPTED, json!({ "id": 0 })) + ); + assert!(hub.github.opened.lock().unwrap().is_empty()); +} + +#[tokio::test] +async fn a_report_github_refused_is_opened_by_the_upkeep() { + let hub = hub(config()); + *hub.github.down.lock().unwrap() = true; + let (code, body) = hub + .post("/hub/report", report("Crash when opening a chest")) + .await; + assert_eq!((code, &body["issue"]), (StatusCode::ACCEPTED, &Value::Null)); + hub.app.file_pending(); + assert_eq!(hub.github.opened.lock().unwrap().len(), 0); + *hub.github.down.lock().unwrap() = false; + hub.app.file_pending(); + assert_eq!(hub.github.opened.lock().unwrap().len(), 1); + hub.app.file_pending(); + assert_eq!(hub.github.opened.lock().unwrap().len(), 1, "opened once"); +} + +#[tokio::test] +async fn reports_are_closed_without_a_github_token() { + let app = App::with_clock( + Store::open(":memory:").unwrap(), + Config::default(), + Box::new(|| DAY), + ); + let res = router(app) + .oneshot( + Request::post("/hub/report") + .header("content-type", "application/json") + .body(Body::from(report("Water is black").to_string())) + .unwrap(), + ) + .await + .unwrap(); + assert_eq!(res.status(), StatusCode::SERVICE_UNAVAILABLE); +} + +#[tokio::test] +async fn suggestions_wait_in_the_queue_for_kira() { + let hub = hub(config()); + let first = json!({ "text": " Add a waypoint mod\r\nto the\u{7} browser ", "item": "mod-registry", "name": "alex", "extra": 1 }); + let (code, body) = hub.post("/hub/suggest", first).await; + assert_eq!(code, StatusCode::ACCEPTED); + let first_id = body["id"].as_i64().unwrap(); + hub.post( + "/hub/suggest", + json!({ "text": "Bigger render distance on the Steam Deck" }), + ) + .await; + assert_eq!( + hub.post("/hub/suggest", json!({ "text": "too short" })) + .await + .0, + StatusCode::BAD_REQUEST + ); + assert_eq!( + hub.post( + "/hub/suggest", + json!({ "text": "A long enough idea", "item": "Bad Id" }) + ) + .await + .0, + StatusCode::BAD_REQUEST + ); + + assert_eq!( + hub.get("/hub/suggestions", None).await.0, + StatusCode::UNAUTHORIZED + ); + assert_eq!( + hub.get("/hub/suggestions", Some("wrong-secret")).await.0, + StatusCode::UNAUTHORIZED + ); + let (code, all) = hub.get("/hub/suggestions", Some("kira-secret")).await; + assert_eq!(code, StatusCode::OK); + let list = all["suggestions"].as_array().unwrap(); + assert_eq!(list.len(), 2); + assert_eq!( + list[0]["text"], "Add a waypoint mod\nto the browser", + "trimmed, no control characters" + ); + assert!(list[0].get("extra").is_none()); + assert_eq!(list[0]["item"], "mod-registry"); + assert_eq!(list[0]["name"], "alex"); + assert!(list[0]["created_at"].as_str().unwrap().ends_with('Z')); + assert!(list[1].get("name").is_none()); + let (_, after) = hub + .get( + &format!("/hub/suggestions?after={first_id}"), + Some("kira-secret"), + ) + .await; + assert_eq!(after["suggestions"].as_array().unwrap().len(), 1); + + let closed = hub_without_token(); + assert_eq!( + closed.get("/hub/suggestions", Some("anything")).await.0, + StatusCode::NOT_FOUND + ); +} + +fn hub_without_token() -> Hub { + hub(Config::default()) +} + +#[tokio::test] +async fn posts_are_rate_limited_per_address_and_capped_per_day() { + let hub = hub(Config { + hub_posts_per_hour: 20.0, + hub_suggestions_per_day: 3, + ..config() + }); + // A burst of a tenth of the hourly rate: two, then refused. + let idea = |n: u32| json!({ "text": format!("Idea number {n}, long enough") }); + assert_eq!( + hub.post("/hub/suggest", idea(1)).await.0, + StatusCode::ACCEPTED + ); + assert_eq!( + hub.post("/hub/suggest", idea(2)).await.0, + StatusCode::ACCEPTED + ); + assert_eq!( + hub.post("/hub/suggest", idea(3)).await.0, + StatusCode::TOO_MANY_REQUESTS + ); + + let busy = self::hub(Config { + hub_posts_per_hour: 1000.0, + hub_suggestions_per_day: 3, + ..config() + }); + for n in 0..3 { + assert_eq!( + busy.post("/hub/suggest", idea(n)).await.0, + StatusCode::ACCEPTED + ); + } + assert_eq!( + busy.post("/hub/suggest", idea(9)).await.0, + StatusCode::SERVICE_UNAVAILABLE + ); + busy.day.store(DAY + 1, Ordering::SeqCst); + assert_eq!( + busy.post("/hub/suggest", idea(9)).await.0, + StatusCode::ACCEPTED, + "a new day" + ); +} + +#[tokio::test] +async fn old_entries_go_and_reports_never_opened_are_given_up() { + let hub = hub(config()); + *hub.github.down.lock().unwrap() = true; + let (_, body) = hub + .post("/hub/report", report("Crash when opening a chest")) + .await; + assert!(body["id"].as_i64().unwrap() > 0); + hub.post("/hub/suggest", json!({ "text": "An idea from long ago" })) + .await; + hub.day.store(DAY + 8, Ordering::SeqCst); + hub.app.maintain().unwrap(); + *hub.github.down.lock().unwrap() = false; + hub.app.file_pending(); + assert!( + hub.github.opened.lock().unwrap().is_empty(), + "past a week a report is given up" + ); + hub.day.store(DAY + 91, Ordering::SeqCst); + hub.app.maintain().unwrap(); + let (_, left) = hub.get("/hub/suggestions", Some("kira-secret")).await; + assert!(left["suggestions"].as_array().unwrap().is_empty()); +} + +#[tokio::test] +async fn the_website_may_post_from_the_browser() { + let hub = hub(config()); + let res = router(hub.app.clone()) + .oneshot( + Request::options("/hub/report") + .header("origin", "https://playhorizon.dev") + .header("access-control-request-method", "POST") + .body(Body::empty()) + .unwrap(), + ) + .await + .unwrap(); + assert_eq!(res.status(), StatusCode::NO_CONTENT); + let header = |name: &str| res.headers()[name].to_str().unwrap().to_owned(); + assert_eq!( + header("access-control-allow-origin"), + "https://playhorizon.dev" + ); + assert_eq!(header("access-control-allow-methods"), "POST"); + assert_eq!(header("access-control-allow-headers"), "content-type"); +} diff --git a/crates/horizon-world/src/worldgen/material_condition.rs b/crates/horizon-world/src/worldgen/material_condition.rs index 6433b09a4..d0b6c4290 100644 --- a/crates/horizon-world/src/worldgen/material_condition.rs +++ b/crates/horizon-world/src/worldgen/material_condition.rs @@ -23,6 +23,7 @@ use horizon_core::{ pub use library::MaterialRuleLibrary; pub use noise::MaterialNoiseSources; use rule::CompiledRule; +pub use rule::SurfaceVariants; pub use rule::{MaterialRule, MaterialRuleError}; use std::{ collections::{BTreeMap, BTreeSet}, @@ -299,7 +300,81 @@ enum Kind { }, Not(Box>), } +/// What a specialised rule may assume about every position it is applied to +/// (`CompiledRule::specialize`): its y lies in `low..=high`, and whether it is +/// at or above the column's preliminary surface level, when known. +#[derive(Clone, Copy, Debug)] +pub struct Facts { + pub low: i32, + pub high: i32, + pub above_surface: Option, +} +impl Clone for Kind +where + R::Positional: Clone, +{ + fn clone(&self) -> Self { + match self { + Self::Simple(condition) => Self::Simple(condition.clone()), + Self::Biome(names) => Self::Biome(names.clone()), + Self::Constant(value) => Self::Constant(*value), + Self::Noise { sampler, min, max } => Self::Noise { + sampler: *sampler, + min: *min, + max: *max, + }, + Self::Gradient { low, high, random } => Self::Gradient { + low: *low, + high: *high, + random: random.clone(), + }, + Self::Not(child) => Self::Not(child.clone()), + } + } +} +impl Clone for Compiled +where + R::Positional: Clone, +{ + fn clone(&self) -> Self { + Self { + kind: self.kind.clone(), + revision: self.revision, + result: self.result, + } + } +} impl Compiled { + /// The value `facts` decide for this condition at every position they + /// cover. Conditions read the position and the world, never change them, + /// so one decided in advance gives what testing it would. + fn decided(&self, facts: Facts) -> Option { + match &self.kind { + Kind::Constant(value) => Some(*value), + Kind::Simple(MaterialCondition::AbovePreliminarySurface) => facts.above_surface, + Kind::Gradient { low, high, .. } => { + if facts.high <= *low { + Some(true) + } else if facts.low >= *high { + Some(false) + } else { + None + } + } + Kind::Not(child) => child.decided(facts).map(|value| !value), + _ => None, + } + } + /// The vertical gradients' bounds, and whether the preliminary surface + /// is asked for. + fn inputs(&self, gradients: &mut Vec<(i32, i32)>, surface: &mut bool) { + match &self.kind { + Kind::Gradient { low, high, .. } => gradients.push((*low, *high)), + Kind::Simple(MaterialCondition::AbovePreliminarySurface) => *surface = true, + Kind::Not(child) => child.inputs(gradients, surface), + _ => {} + } + } fn constant_result(&self) -> Option { match &self.kind { Kind::Constant(value) => Some(*value), diff --git a/crates/horizon-world/src/worldgen/material_condition/rule.rs b/crates/horizon-world/src/worldgen/material_condition/rule.rs index cf4fc8547..f3c2ae60d 100644 --- a/crates/horizon-world/src/worldgen/material_condition/rule.rs +++ b/crates/horizon-world/src/worldgen/material_condition/rule.rs @@ -1,5 +1,7 @@ //! Compiled material rule composition, sharing the condition worker's context. -use super::{Compiled, Context, MaterialCondition, MaterialConditions, MaterialSurfaceQueries}; +use super::{ + Compiled, Context, Facts, MaterialCondition, MaterialConditions, MaterialSurfaceQueries, +}; use crate::{registry::BlockStateId, worldgen::DecodeError}; use horizon_core::{ density::DensitySampler, @@ -209,6 +211,161 @@ impl CompiledRule { } } } +impl CompiledRule +where + R::Positional: Clone, +{ + /// This rule where `facts` hold: conditions they decide are folded away, + /// with what can no longer produce a block, and a sequence stops at its + /// first child that always produces one. For every position the facts + /// cover it gives the block the whole rule gives. + fn specialize(&self, facts: Facts) -> Self { + match self { + Self::Empty => Self::Empty, + Self::Block(block) => Self::Block(*block), + Self::Bandlands => Self::Bandlands, + Self::OreVein { + blocks, + raw_ore_chance, + densities, + random, + } => Self::OreVein { + blocks: *blocks, + raw_ore_chance: *raw_ore_chance, + densities: *densities, + random: random.clone(), + }, + Self::Condition { condition, rule } => match condition.decided(facts) { + Some(false) => Self::Empty, + Some(true) => rule.specialize(facts), + None => match rule.specialize(facts) { + Self::Empty => Self::Empty, + rule => Self::Condition { + condition: condition.clone(), + rule: Box::new(rule), + }, + }, + }, + Self::Sequence(children) => { + let mut result = Vec::new(); + for child in children { + let child = child.specialize(facts); + if matches!(child, Self::Empty) { + continue; + } + let last = child.always_produces(); + result.push(child); + if last { + break; + } + } + match result.len() { + 0 => Self::Empty, + 1 => result.pop().unwrap(), + _ => Self::Sequence(result), + } + } + } + } + /// Whether applying this rule never gives `None` (only an error). + fn always_produces(&self) -> bool { + match self { + Self::Block(_) | Self::Bandlands => true, + Self::Sequence(children) => children.iter().any(Self::always_produces), + Self::Empty | Self::OreVein { .. } | Self::Condition { .. } => false, + } + } + fn inputs(&self, gradients: &mut Vec<(i32, i32)>, surface: &mut bool) { + match self { + Self::Condition { condition, rule } => { + condition.inputs(gradients, surface); + rule.inputs(gradients, surface); + } + Self::Sequence(children) => { + for child in children { + child.inputs(gradients, surface); + } + } + _ => {} + } + } +} +/// A rule specialised (`CompiledRule::specialize`) for each band of y between +/// its vertical gradients' bounds, and below and at or above the preliminary +/// surface when the rule asks for it: most blocks of a column, deep stone, +/// then evaluate only the few rules that can apply there. +pub struct SurfaceVariants { + /// The first y of each band after the first, ascending. + starts: Vec, + /// The rule index per band (two per band, below then above the surface, + /// when `surface`). + rules: Vec, + pub surface: bool, +} +impl SurfaceVariants { + /// The rule for a block at `y`, `above` the preliminary surface (ignored + /// unless `surface`). + #[inline] + pub fn rule(&self, y: i32, above: bool) -> usize { + let band = self.starts.partition_point(|&start| start <= y); + if self.surface { + self.rules[band * 2 + usize::from(above)] + } else { + self.rules[band] + } + } +} +impl MaterialConditions +where + R::Positional: Clone, +{ + /// Add the specialised variants of rule `index` (`SurfaceVariants`). + pub fn surface_variants(&mut self, index: usize) -> Result { + let rule = self + .rules + .get(index) + .ok_or(MaterialRuleError::UnknownRule)?; + let (mut gradients, mut surface) = (Vec::new(), false); + rule.inputs(&mut gradients, &mut surface); + // A gradient is true at and below `low`, random between, false at + // and above `high`: it is decided within each band these bound. + let mut starts: Vec = gradients + .iter() + .flat_map(|&(low, high)| [low.saturating_add(1), high]) + .collect(); + starts.sort_unstable(); + starts.dedup(); + let mut variants = Vec::new(); + for band in 0..=starts.len() { + let low = if band == 0 { + i32::MIN + } else { + starts[band - 1] + }; + let high = starts.get(band).map_or(i32::MAX, |&next| next - 1); + let sides: &[Option] = if surface { + &[Some(false), Some(true)] + } else { + &[None] + }; + for &above_surface in sides { + let facts = Facts { + low, + high, + above_surface, + }; + variants.push(self.rules[index].specialize(facts)); + } + } + let first = self.rules.len(); + self.rules.extend(variants); + Ok(SurfaceVariants { + starts, + rules: (first..self.rules.len()).collect(), + surface, + }) + } +} impl MaterialConditions { /// Compile all children in order, even after an unconditional block rule. /// Failed compilation never adds a partial rule to this worker. diff --git a/crates/horizon-world/src/worldgen/material_stage.rs b/crates/horizon-world/src/worldgen/material_stage.rs index 0763c4e63..fd51d3716 100644 --- a/crates/horizon-world/src/worldgen/material_stage.rs +++ b/crates/horizon-world/src/worldgen/material_stage.rs @@ -196,7 +196,10 @@ pub fn build_surface, extensions: &MaterialExtensions, -) -> Result<(), SurfaceError> { +) -> Result<(), SurfaceError> +where + R::Positional: Clone, +{ let min = chunk.min_y(); let max = min + chunk.height() - 1; if inputs.write_min_y < min @@ -240,9 +243,13 @@ fn build_columns, write_min_y: i32, write_max_y: i32, -) -> Result<(), SurfaceError> { +) -> Result<(), SurfaceError> +where + R::Positional: Clone, +{ let min = chunk.min_y(); let rule = worker.add_rule(rule)?; + let variants = worker.surface_variants(rule)?; let cx = chunk.position().x.wrapping_shl(4); let cz = chunk.position().z.wrapping_shl(4); for lx in 0..16 { @@ -321,6 +328,8 @@ fn build_columns= write_min_y && y <= write_max_y { + let above = variants.surface && y >= worker.minimum_surface_level()?; + let rule = variants.rule(y, above); if let Some(state) = worker.try_apply(rule, BlockPos::new(x, y, z))? { column.set_block(y, state)?; } diff --git a/docs/README.md b/docs/README.md index 609580d91..19117ab37 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,6 +1,6 @@ # Horizon documentation -To play, use [the launcher](../launcher/README.md); the project site is [playhorizon.dev](https://playhorizon.dev). These pages are for developers. +To play, use [the launcher](../launcher/README.md); the project site is [playhorizon.dev](https://playhorizon.dev). These pages are for developers. What is being done now and next, the work board and how to help: [the hub](https://playhorizon.dev/hub) ([ROADMAP.md](../ROADMAP.md), [roadmap/README.md](../roadmap/README.md) for keeping it current). ## Build and run diff --git a/docs/api/README.md b/docs/api/README.md index ad49ff609..ef3649105 100644 --- a/docs/api/README.md +++ b/docs/api/README.md @@ -51,5 +51,6 @@ Every page has the same parts: **Overview** (what you can build), **Guide** (whe **Examples**, **Stability** (the level of each part, per [stability.md](stability.md)), **Changelog** and **Not there yet** (what is planned, so nobody builds on a guess). -The website's summary for players and authors is [playhorizon.dev/docs/apis](https://playhorizon.dev/docs/apis) +The website shows these pages at [playhorizon.dev/docs/api](https://playhorizon.dev/docs/api), built from this +folder, and a summary for players and authors at [playhorizon.dev/docs/apis](https://playhorizon.dev/docs/apis) (`website/docs/apis.md`). diff --git a/docs/api/mods-reference.md b/docs/api/mods-reference.md index ef889d599..f3bd0d1e4 100644 --- a/docs/api/mods-reference.md +++ b/docs/api/mods-reference.md @@ -11,20 +11,21 @@ The contract between Horizon and its mods: the WIT package `horizon:mods@0.1.0`, | `chat` | [`chat`](#chat) | show you chat messages | | `hud` | [`hud`](#hud) | draw text on your screen | | `storage` | [`storage`](#storage) | save its own settings to disk | +| `command` | [`command`](#command) | run commands as you, with your permissions | | `registries` | data files (`assets/`, `data/`), no interface | add blocks and items to your worlds | | `ui` | [`ui`](#ui) | show its own screens and a settings page | | `render` | [`render`](#render) | draw on your screen and shake the camera | ## World `horizon-mod` -- Imports (what the game offers): `log`, `game`, `chat`, `hud`, `storage` +- Imports (what the game offers): `log`, `game`, `chat`, `hud`, `storage`, `command` - Exports (what the mod provides): `events` ## World `horizon-ui-mod` A mod with widgets (capability `ui`): the SDK builds every mod against this world. Mods built for `horizon-mod` alone (no `ui-events` export) still load and simply hear nothing of their widgets. -- Imports (what the game offers): `ui`, `render`, `log`, `game`, `chat`, `hud`, `storage` +- Imports (what the game offers): `ui`, `render`, `log`, `game`, `chat`, `hud`, `storage`, `command` - Exports (what the mod provides): `ui-events`, `events` ## World `ui-host` @@ -140,6 +141,16 @@ Capability `storage`: a per-mod folder of small key/value files, quota'd (docs/m | `set` | `func(key: string, value: list) -> result<_, string>` | Errs on a malformed key or over quota (bytes for the whole mod). | | `delete` | `func(key: string)` | No error if `key` was already absent. | +## `command` + +Access: capability `command`. + +Capability `command`: vanilla commands run as the player, with the player's permissions, after the hook call returns (8 per call). A mod's own commands are not run this way. + +| Function | Signature | What it does | +| --- | --- | --- | +| `run` | `func(line: string)` | `line` without its slash; its feedback goes to the chat as if typed. | + ## `ui` Access: capability `ui`. diff --git a/docs/api/mods.md b/docs/api/mods.md index cda484c31..88dbcc56b 100644 --- a/docs/api/mods.md +++ b/docs/api/mods.md @@ -19,11 +19,14 @@ the player before the mod is turned on, and checked against the component's real | Capabilities | 5: `game-read`, `chat`, `hud`, `storage`, `registries` | | Manifest `api` lines served | 2: `0.1` (code), `0.2` (content) | | SDK | the `Mod` trait (5 methods), `Registration` (4 builders), `export_mod!`, log helpers | -| `cargo horizon-mod` | `new`, `build`, `run` | +| `cargo horizon-mod` | `new` (templates `code` and `content`), `build`, `run`, `check` | | Example mods | `coords-hud` (code), `ruby-content` (content) | ## Guide +The step-by-step guide, from installing the tools to publishing on the registry, is +[Make a mod](https://playhorizon.dev/docs/make-a-mod) on the website. In short: + 1. `rustup target add wasm32-unknown-unknown`, `cargo install --git https://github.com/ludovic111/horizon cargo-horizon-mod`, then `cargo horizon-mod new my-mod` ([Making a mod](../mods.md#making-a-mod)). 2. Implement `Mod` for your type, list the capabilities you import in `horizon-mod.toml` @@ -55,6 +58,7 @@ scripting runtime in the game. | Way in | For | Status | Template and example | | --- | --- | --- | --- | | Rust | most mods; the fastest | first-class: the SDK, `cargo horizon-mod` | `cargo horizon-mod new`, `coords-hud` | +| Data only | blocks and items with no code | first-class: content mods (API 0.2) | `cargo horizon-mod new --template content`, `ruby-content` | | Lua scripts | people who don't want a compiler | planned: a script mod whose code is a `.lua` file, run by Horizon's own interpreter mod | planned, with the same example as Rust | | C, C++, Zig | authors of those languages | to evaluate: bindings generated from the WIT, built without WASI | one each if it passes | | Go (TinyGo), AssemblyScript, JavaScript / TypeScript | authors of those languages | to evaluate: each needs its toolchain to emit a component without WASI imports, or an interpreter mod as for Lua | one each if it passes | diff --git a/docs/api/stability.md b/docs/api/stability.md index 76c3c4e51..d35c5d173 100644 --- a/docs/api/stability.md +++ b/docs/api/stability.md @@ -86,7 +86,7 @@ Deprecations and breaking changes are in bold. The release notes of the game rel | Part | Level | Version | | --- | --- | --- | -| Mod API: `log`, `game`, `chat`, `hud`, `storage`, `events` | preview | 0.1.0 | +| Mod API: `log`, `game`, `chat`, `hud`, `storage`, `command`, `events` | preview | 0.1.0 | | Mod API: `ui`, `ui-events` (a mod's screens and settings page) | experimental | manifest `experimental = ["ui"]` | | Mod API: `render` (HUD shapes, camera shake; drawn by the game, no mod shaders) | experimental | manifest `experimental = ["render"]` | | Content mods (`registries`: data-driven blocks and items) | preview | manifest `api = "0.2"` | diff --git a/docs/mods.md b/docs/mods.md index 672d6c7fd..6daf06501 100644 --- a/docs/mods.md +++ b/docs/mods.md @@ -12,11 +12,11 @@ folder, loads no runtime and plays exactly like vanilla 26.3. | Part | Where | State | |---|---|---| -| The contract (WIT) | `crates/horizon-mod-sdk/wit/horizon.wit` | 0.1: log, game-read, chat, hud, storage, ui; tick, frame, chat and command hooks | +| The contract (WIT) | `crates/horizon-mod-sdk/wit/horizon.wit` | 0.1: log, game-read, chat, hud, storage, command, ui; tick, frame, chat and command hooks | | SDK for mod authors | `crates/horizon-mod-sdk` | 0.1 | -| Example mod | `crates/horizon-mod-sdk/examples/coords-hud` | coordinates, facing and time HUD, `/where`, `/coords` | +| Example mods | `crates/horizon-mod-sdk/examples/coords-hud`, `examples/predator-missile` | coordinates, facing and time HUD, `/where`, `/coords`; `/missile`, a guided missile from vanilla commands | | Runtime | `crates/horizon-mods` | wasmtime 36 LTS, component model | -| Authoring tool | `crates/cargo-horizon-mod` | `cargo horizon-mod new / build / run / check`, and the registry's CI (`registry build / review / list`) | +| Authoring tool | `crates/cargo-horizon-mod` | `cargo horizon-mod new [--template code\|content] / build / run / check`, and the registry's CI (`registry build / review / list`) | | In the game | `crates/horizon-client/src/game/mods.rs`, Options > Mods | client mods | | Launcher mod browser (Modrinth, CurseForge with a key) | [mod-browser.md](mod-browser.md) | resource packs, modpacks | | Registry index (`index.json`, `.hpack`), signatures | `horizon_mods::index`, [api/mod-index.md](api/mod-index.md) | schema 1; launcher and website next | @@ -86,6 +86,7 @@ folder, loads no runtime and plays exactly like vanilla 26.3. | `chat` | `chat` | `show(text)`: a line only the local player sees, after the mod's name (8 per call) | | `hud` | `hud` | `set-panel(corner, lines)`, `clear()`: up to 16 lines of 256 characters | | `storage` | `storage` | `get(key)`, `set(key, value)`, `delete(key)`: a per-mod folder of up to 64 files, 4 MiB total, at most 1 MiB written per call | +| `command` | `command` | `run(line)`: a vanilla command run as the player, with the player's permissions, after the hook returns (8 per call); mods' own commands are not run | | `ui` | `ui` | `open-screen`, `close-screen`, `set-settings`: a screen the mod owns and a page in Options > Mods, of text, item and list widgets the game lays out and draws with its own menus' code. **Experimental:** the manifest needs `experimental = ["ui"]` | | `render` | `render` | `set-hud(shapes)`: up to 1024 fills, four-cornered shapes, GUI sprites, items and text, kept on the HUD (over the hotbar, under chat and screens, hidden with F1) until replaced; `gui-size()`; `shake(strength, seconds)`: the view turns, the player doesn't, scaled by Distortion Effects. The game's own HUD renderer draws them: a mod never supplies shader code. **Experimental:** `experimental = ["render"]` | @@ -216,6 +217,7 @@ Benchmarks (`--benchmark`) never load mods. rustup target add wasm32-unknown-unknown cargo install --git https://github.com/ludovic111/horizon cargo-horizon-mod cargo horizon-mod new my-mod # Cargo.toml, horizon-mod.toml, src/lib.rs + # (--template content: a content mod, no code) cd my-mod cargo horizon-mod run # build, then start the game with it cargo horizon-mod build # again after an edit: the running game reloads it @@ -296,7 +298,10 @@ permission. An experimental capability (today `ui` and `render`) may change in a ### Content mods (API 0.2) A mod with `api = "0.2"` and capability `registries` may carry `assets/` and -`data/` in the `.hmod`, and needs no `mod.wasm` if it has no code. It adds +`data/` in the `.hmod`, and needs no `mod.wasm` if it has no code +(`cargo horizon-mod new --template content` starts one; `build` in a folder +with no `Cargo.toml` packs the manifest, `assets/` and `data/`, and `check` +reads it without a trial run). It adds blocks and items by declaring them in `data//horizon/block/*.json` and `data//horizon/item/*.json`, each copying a vanilla template ([modpacks.md](modpacks.md#registries-02-data-driven-blocks-and-items)): diff --git a/docs/parity-workflow.md b/docs/parity-workflow.md index f9f222071..d64ec3087 100644 --- a/docs/parity-workflow.md +++ b/docs/parity-workflow.md @@ -20,6 +20,8 @@ python3 tools/parity.py --changed origin/main # those a branch's changes ca python3 tools/parity.py --changed-files paths.txt # the same for listed paths, in a copy without Git (tools/pr_ready.py) ``` +On CI, a pull request runs the parity job only when it changes a harness, an oracle, a probe or the runner; everything else is checked on main by `parity-main`, a batch of merges at a time, which bisects a failure to the merge that caused it (`tools/parity_main.py`; [release-flow.md](release-flow.md#parity-on-main)). Game releases wait for it. + Each harness runs unmodified under a tracer that records the files it depended on: its Python modules, its Java oracle and the oracle sources javac pulled in, other repository files it read, and the Rust sources of the probes it ran (from Cargo's `.d` dep-info files). A pass stands until one of those files, the pinned vanilla artifacts, `Cargo.lock` or the Rust/Java toolchains change. State and logs are in `.local/parity-runner/`; each harness's own outputs stay in its `.local//` directory. Vanilla oracle runs are reused as well. `tools/parity_shim.py` stands in for `java` and `javac`: it records each oracle run's outputs and the content of every file under the paths its arguments name, and replays the run while those are unchanged. After a Rust-only change a harness pays only for its Rust side (`block_update`: 53 s to 5 s). Files an oracle writes outside its named paths aren't tracked, so a harness that fails while reusing oracle results is rerun fresh, and stops reusing them if it then passes. `--all` and `--fresh` never reuse them. @@ -49,6 +51,10 @@ A harness can declare, at the top level of its script: - `PARITY_SHARDS = N`: it runs as `NAME#1` to `NAME#N`, in parallel, each told its share in `PARITY_SHARD=i/N` (`test_datafix_parity.py` upgrades every sixth world). `NAME` on the command line or in `--known-failures` means all of them. - `PARITY_RUST_SCOPE = [...]`: the Rust sources its probes can reach, as path prefixes. Cargo's dep-info names every file of every crate a probe links, so without it a probe of `horizon-world` depends on all 350 of its files. The runner keeps only the scope's files if the scope is closed: nothing in it names a module, crate-root re-export or macro outside it (`crate::`, `super::`, other workspace crates; comments and strings ignored), declares a module it doesn't cover, or includes source, and nothing outside it implements anything for a type it defines. Crate roots then count without their declarations of other modules, so adding a module elsewhere doesn't rerun the harness. If the scope isn't closed the runner says why and keeps every file. `datafix` depends on 41 files this way, not 354. +### Which harnesses a change affects + +`--changed` and `--changed-files` narrow each harness's Rust sources to those its probes can reach (`tools/parity_reach.py`), so a change to one module of `horizon-client` no longer reruns every harness whose probe links the crate. The narrowing works on items, not files: from the probe modules a harness ran (or the binary it built), a free item (fn, type, const, static) is reached when a reached item names it in its own module, through a path or an import of a module that is named, or through a glob import; an inherent impl's methods count one by one once their type is reached and their name is mentioned; a trait impl counts once its trait and its type are reached (std's traits and types always are); a macro counts once invoked. Names match by spelling, so the result only over-approximates. A reached file's parent modules count through their `mod` declarations of it, compared with the merge base; Rust files a change adds take part, a dependency that was deleted counts as changed, code under `#[cfg(test)]` never counts, and a harness whose probe can't be found keeps every source. Parses are cached by content in `.local/parity-runner/reach-cache.json`. `--by-crate` turns the narrowing off. On the last 20 pull requests merged on 2026-10-25, the harnesses picked went from 2255 to 1061 (2384 to 1592 harness-minutes; game-binary harnesses, which link all of `game/`, set the floor), and every harness a CI parity run failed in the week before was still picked; every source file a sample of probes executed (LLVM `-C instrument-coverage`) is among those kept. Building the graph takes 15 s cold, under a second warm, and up to a minute and a half on a change that reaches many harnesses. + ## The gate `tools/gate.py` is what a branch must pass before it is integrated: the public-tree check, rustfmt, clippy with warnings denied, the Rust and Python tests, then the harnesses whose inputs changed. It stops at the first failure and writes `.local/gate.json`. `--known-failures a,b` lets harnesses that already fail on the integration branch keep failing. `--quick` checks formatting, lints and Python tests only; `--no-parity` skips the harnesses. diff --git a/docs/release-flow.md b/docs/release-flow.md index 4fb0209d9..a3955c7ca 100644 --- a/docs/release-flow.md +++ b/docs/release-flow.md @@ -13,7 +13,7 @@ reopened; labels never start it): | Job | Runs on | What | Time | | --- | --- | --- | --- | | `verify` | GitHub `ubuntu-latest` | Public-tree check, Python tests, rustfmt, clippy with warnings denied, Rust tests. Dependencies and the workspace build come from a cache saved by main's runs | ~4 min cold, less warm | -| `parity / parity` | The Mac mini's runner (`ludovics-mac-mini`, label `horizon-game`) | The vanilla harnesses the change can affect (`tools/parity.py --changed `) | seconds when nothing is affected; minutes per affected harness | +| `parity / parity` | The Mac mini's runner (`ludovics-mac-mini`, label `horizon-game`) | Only for a change to a harness, an oracle, a probe or the runner (or the `run-parity` label): the vanilla harnesses the change can affect (`tools/parity.py --changed `). Other pull requests skip it: [parity-main](#parity-on-main) checks them on main | seconds when nothing is affected; minutes per affected harness | Pushes to main run `verify` only; the nightly audit covers main's harnesses. @@ -39,10 +39,17 @@ copy is: the Mac mini. Its runner runs as the maintainer's account and keeps its depend only on the Minecraft version, are kept once in `shared-local/sounds//` (copied from a folder `horizon-prepare sounds` made from the maintainer's own game; the Mac mini has no official launcher of its own) and cloned into each preparation (not linked: vanilla's packs refuse symbolic links), for the audio harnesses. + `tools/prepare_output.py check` then compares what was prepared with `crates/horizon-prepare/output-versions.txt`: + launchers keep a player's prepared resources across releases whose preparer declares the same `OUTPUT_VERSION` + (`crates/horizon-prepare/src/lib.rs`), so a change to the prepared files fails here until it bumps the number + and appends the new digest (the error gives both lines). 3. `tools/parity.py --changed ` picks the harnesses whose traced dependencies (their script, Python modules, Java oracles and every Rust source their probes compile) changed since the merge base, plus any harness nothing is recorded about yet, and runs those that have no pass for their current inputs. A change to `Cargo.lock` or `research-lock.json` affects every harness. Each harness has 25 minutes, the job 45. + The selection runs with main's `tools/parity.py` and `tools/parity_reach.py` (`--changed --list`), so a + branch made before a better selection landed gets it too, and the head's own `parity.py` then runs the named + harnesses. A branch that changes either file selects with its own code. Failed harnesses' logs are uploaded as the `parity-logs` artifact. @@ -80,10 +87,34 @@ gh api -X POST repos/ludovic111/horizon/branches/main/protection/required_status A skipped `parity` (a fork's pull request without the label) counts as passed, so requiring it never blocks a fork's pull request; the maintainer decides whether to run it. +## Parity on main + +Most pull requests merge on `verify` alone and leave the vanilla harnesses to `.github/workflows/parity-main.yml`, +which runs them on main a batch of merges at a time, on the Mac mini's runner: + +- It starts on every push to main, one run at a time (`concurrency: parity-main`). Merges that land while a run + goes on wait in one pending run: GitHub keeps only the newest and cancels the others, so the next run takes them + all. +- `tools/parity_main.py base` gives the newest commit main contains whose parity-main passed (else the last + merge's parent), and the run is `tools/parity.py --changed `: the union of the harnesses every merge + since then can affect, once, with every earlier pass reused. +- On a failure, `tools/parity_main.py bisect` checks out the batch's first-parent merges by halves and runs only + the failing harnesses, names the first merge they fail on in the run's summary and comments on its pull request + (`🤖 Agent: parity-main found that this merge breaks ...`). Fix it with a pull request as usual; the next + run checks from the last green commit again. +- Game releases ship only a commit both `checks` and `parity-main` passed on (`tools/release_train.py`, see + below), so a broken merge holds releases instead of reaching players. `release.yml` also starts when a + parity-main run finishes. A forced release (`force=true`) doesn't wait for parity-main. + +A pull request still runs its own parity job when it changes a harness script, an oracle +(`mods/horizon/src/test/`), a probe (`crates/*/examples/`, `game/*_fixture.rs`, `game/*_harness.rs`), the runner +(`tools/parity*.py`, `tools/lab.py`, `tools/parity_hosts.json`, `parity*.yml`), or carries the `run-parity` label +(`run-parity.yml` runs it when the label is added). + ## Game releases -`.github/workflows/release.yml` releases the game by itself. It starts after every green `checks` run of a push -to main, every 10 minutes (so a release waiting for merges to settle goes out without another merge), and by hand. +`.github/workflows/release.yml` releases the game by itself. It starts after every green `checks` or `parity-main` +run of a push to main (it releases a commit only once both passed on it), every 10 minutes (so a release waiting for merges to settle goes out without another merge), and by hand. ```mermaid flowchart LR diff --git a/launcher/Cargo.lock b/launcher/Cargo.lock index a416a39ff..1e8a5aee2 100644 --- a/launcher/Cargo.lock +++ b/launcher/Cargo.lock @@ -2681,8 +2681,10 @@ dependencies = [ "horizon-theme", "image", "keyring", + "objc2-app-kit", "oo7", "pulldown-cmark", + "raw-window-handle", "self-replace", "serde", "serde_json", diff --git a/launcher/Cargo.toml b/launcher/Cargo.toml index cfe9787d6..a97f128b8 100644 --- a/launcher/Cargo.toml +++ b/launcher/Cargo.toml @@ -50,6 +50,10 @@ oo7 = { version = "0.5", default-features = false, features = ["async-std", "nat futures-lite = "2" [target.'cfg(target_os = "macos")'.dependencies] keyring = { version = "3", features = ["apple-native"] } +# Painting the window's title bar dark or light (titlebar::apply_appearance): crates already in +# the lockfile through gpui and wgpu. +objc2-app-kit = { version = "0.3", default-features = false, features = ["NSAppearance", "NSView", "NSWindow"] } +raw-window-handle = "0.6" [target.'cfg(target_os = "windows")'.dependencies] keyring = { version = "3", features = ["windows-native"] } diff --git a/launcher/src/backend.rs b/launcher/src/backend.rs index 9d1eeb111..7e5c4babb 100644 --- a/launcher/src/backend.rs +++ b/launcher/src/backend.rs @@ -552,7 +552,7 @@ pub fn install(release: &Release, progress: impl Fn(f32)) -> Result<(), String> fs::write(target.join(COMPLETE), "").map_err(|e| e.to_string())?; let _ = fs::remove_dir_all(&partial); reuse_prepared(&root, tag); - complete_packs(tag); + complete_assets(tag); progress(1.0); Ok(()) } @@ -774,13 +774,13 @@ fn reuse_prepared(versions: &Path, tag: &str) -> Option { /// `reuse_prepared` for every installed version that is not prepared, oldest first, so a /// version prepared this way can serve the next. Launchers before 0.1.4 installed new versions /// without it; this catches those up. Returns each (version, where its resources came from). -/// Prepared versions also get the run-time reports (`complete_reports`) and built-in packs -/// (`complete_packs`) they lack. +/// Prepared versions also get the run-time reports (`complete_reports`) and the built-in packs, +/// panorama and sounds (`complete_assets`) they lack. pub fn reuse_prepared_everywhere() -> Vec<(String, String)> { let versions = versions_dir(); let reused = reuse_prepared_in(&versions); for tag in installed_in(&versions) { - complete_packs(&tag); + complete_assets(&tag); } reused } @@ -856,19 +856,77 @@ fn complete_reports(versions: &Path, tag: &str) -> bool { complete } -/// Adds vanilla's built-in Programmer Art and High Contrast packs to a prepared version that -/// lacks them, as `horizon-prepare packs` does: preparers before #1538 left them out, and a new -/// version reuses the resources of the one before (`reuse_prepared`). They come from the player's -/// own Minecraft asset store; without them there, one line in the log, once per launcher run, and -/// the version plays as before. Nothing is prepared again. -pub fn complete_packs(tag: &str) { - complete_packs_in(&versions_dir(), tag, minecraft_folders, &|line| { +/// What a prepared version may lack that the launcher adds without preparing again, each copied +/// from the player's own Minecraft asset store as `horizon-prepare ` does: what the +/// player misses without it, whether the resources lack it, and the copy. +type Completion = ( + &'static str, + fn(&Path) -> bool, + fn(&Path, Option<&Path>, horizon_prepare::Log) -> Result, +); + +const COMPLETIONS: [Completion; 5] = [ + // Preparers before #1538 left the built-in packs out. + ( + "Programmer Art and High Contrast", + lacks_packs, + horizon_prepare::packs, + ), + // A preparation that could not read the asset store keeps the client jar's 1x1 grey + // placeholders, its English alone, and no sounds (#1867: Prism Launcher installs prepared + // since 0.2.48). + ( + "The title panorama", + lacks_panorama, + horizon_prepare::panorama, + ), + ("The Unifont glyphs", lacks_unifont, horizon_prepare::fonts), + ("The languages", lacks_languages, horizon_prepare::languages), + ( + "The sounds", + |resources| !sounds_in(resources), + horizon_prepare::sounds, + ), +]; + +fn lacks_unifont(resources: &Path) -> bool { + !resources + .join("assets/minecraft/font/unifont.zip") + .is_file() +} + +/// Whether `en_us`, the one language the client jar has, is the only one. +fn lacks_languages(resources: &Path) -> bool { + fs::read_dir(resources.join("assets/minecraft/lang")).map_or(true, |dir| dir.count() <= 1) +} + +fn lacks_packs(resources: &Path) -> bool { + horizon_prepare::BUILT_IN_PACKS + .iter() + .any(|pack| !resources.join("assets").join(pack).is_file()) +} + +/// Whether the first panorama face is missing or the client jar's 1x1 placeholder (a PNG's +/// width is the big-endian word at byte 16, in the IHDR chunk). +fn lacks_panorama(resources: &Path) -> bool { + let face = resources.join("assets/minecraft/textures/gui/title/background/panorama_0.png"); + fs::read(face).map_or(true, |png| { + png.get(16..20).is_none_or(|width| width == [0, 0, 0, 1]) + }) +} + +/// Adds what a prepared version lacks of `COMPLETIONS`: a new version reuses the resources of the +/// one before (`reuse_prepared`), which may come from a preparer that left them out. Without them +/// in any Minecraft folder, one line each in the log, once per launcher run, and the version plays +/// as before. Nothing is prepared again. +pub fn complete_assets(tag: &str) { + complete_assets_in(&versions_dir(), tag, minecraft_folders, &|line| { eprintln!("{line}") }); } -/// `folders`: where to look, asked only when the version lacks a pack. -fn complete_packs_in( +/// `folders`: where to look, asked only when the version lacks something. +fn complete_assets_in( versions: &Path, tag: &str, folders: impl Fn() -> Vec, @@ -879,37 +937,41 @@ fn complete_packs_in( static TRIED: Mutex> = Mutex::new(Vec::new()); let root = versions.join(tag); let resources = resources_at(&root); - let lacking = || { - resources.join(RESOURCES_MANIFEST).exists() - && horizon_prepare::BUILT_IN_PACKS - .iter() - .any(|pack| !resources.join("assets").join(pack).is_file()) + let lacking = || -> Vec<&Completion> { + if !resources.join(RESOURCES_MANIFEST).exists() { + return Vec::new(); + } + COMPLETIONS + .iter() + .filter(|(_, lacks, _)| lacks(&resources)) + .collect() }; - if !lacking() { + if lacking().is_empty() { return; } let mut tried = TRIED.lock().unwrap_or_else(|e| e.into_inner()); - if tried.contains(&root) || !lacking() { + let lacking = lacking(); + if tried.contains(&root) || lacking.is_empty() { return; } tried.push(root.clone()); - let mut first_error = None; - for folder in folders() { - let copied = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { - horizon_prepare::packs(&root, Some(&folder), &|line| log(line.to_string())) - })) - .unwrap_or_else(|_| Err("the copy stopped unexpectedly".into())); - match copied { - Ok(_) => return, - Err(e) => { - first_error.get_or_insert(e); - } + let folders = folders(); + for (what, _, copy) in lacking { + let mut first_error = None; + let added = folders.iter().any(|folder| { + let copied = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + copy(&root, Some(folder), &|line| log(line.to_string())) + })) + .unwrap_or_else(|_| Err("the copy stopped unexpectedly".into())); + copied.map_err(|e| first_error.get_or_insert(e)).is_ok() + }); + if !added { + log(format!( + "{tag}: {what} not added: {}", + first_error.unwrap_or_else(|| "no Minecraft folder found".into()) + )); } } - log(format!( - "{tag}: Programmer Art and High Contrast not added: {}", - first_error.unwrap_or_else(|| "no Minecraft folder found".into()) - )); } /// The Minecraft folders whose asset store can hold what a prepared version lacks: the one @@ -1103,6 +1165,34 @@ fn with_java(cmd: &mut Command, home: &Path) { } } +/// The Minecraft folder a preparation whose game files are imported already reads: the sounds, +/// the title panorama, the fonts and the languages come from its asset store. It is the one +/// `resolve` found, linked into the official layout for another launcher's files; without one, the +/// chosen folder, and the preparation goes without them, saying so. Before, such a preparation +/// read the chosen folder as it was, which a Prism Launcher or Modrinth App install does not +/// keep its assets in: the game was silent and its title screen grey. +fn assets_folder( + resolved: Result, + chosen: &Path, + version: &str, + log: &dyn Fn(String), +) -> PathBuf { + match resolved { + Ok(sources::Resolved::Folder(dir)) => dir, + Ok(sources::Resolved::NotFound(_)) => { + log(format!( + "Minecraft {version}'s assets were not found in any launcher folder: the game \ + will be silent and the title screen plain" + )); + chosen.to_path_buf() + } + Err(e) => { + log(format!("Minecraft {version}'s assets: {e}")); + chosen.to_path_buf() + } + } +} + /// Checks what the preparation needs and picks how to run it. PLAY.txt describes the same /// steps: the player's own launcher install, the server jar from that version's own json, and /// Java (and Python, for versions before horizon-prepare). Also returns the Minecraft folder @@ -1117,9 +1207,8 @@ pub fn preparer( let dir = version_dir(tag); let reused = horizon_prepare::artifacts::reuse_imported(&dir, &|line| log(line.to_string()))?; let chosen = minecraft.to_path_buf(); - let minecraft = if reused { - minecraft.to_path_buf() - } else { + // Resolved even when the game files are reused: the assets still come from the install. + let minecraft = { let pins = read_json(&dir.join("research-lock.json")) .and_then(|lock| sources::Pins::read(&lock)) .ok_or("The version's research-lock.json doesn't pin the Minecraft files")?; @@ -1139,12 +1228,17 @@ pub fn preparer( Ok(body) }; let linked = data_dir().join("linked-minecraft"); - match sources::resolve(minecraft, &pins, &known, &linked, &fetch, log)? { - sources::Resolved::Folder(dir) => dir, - sources::Resolved::NotFound(checked) => { - let not_found = sources::not_found(&version, &checked, minecraft); - download_for_owner(&pins, &own, &fetch, log).map_err(|e| not_found + &e)?; - own + let resolved = sources::resolve(minecraft, &pins, &known, &linked, &fetch, log); + if reused { + assets_folder(resolved, &chosen, &version, log) + } else { + match resolved? { + sources::Resolved::Folder(dir) => dir, + sources::Resolved::NotFound(checked) => { + let not_found = sources::not_found(&version, &checked, minecraft); + download_for_owner(&pins, &own, &fetch, log).map_err(|e| not_found + &e)?; + own + } } } }; @@ -1635,7 +1729,7 @@ pub fn need_prepared(tag: &str) -> Result<(), String> { /// The client's command, once its resources are prepared. pub fn client(tag: &str) -> Result { need_prepared(tag)?; - complete_packs(tag); + complete_assets(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. @@ -2353,6 +2447,53 @@ mod tests { use super::*; use std::cmp::Ordering::*; + /// A preparation that reuses the imported game files still reads the assets from the + /// launcher that has them: a Prism Launcher instance's are in the launcher root, linked into + /// the official layout (jan.sonki on Discord: silent, grey title screen since 0.2.48). + #[test] + fn reused_imports_take_the_assets_from_the_launcher_that_has_them() { + let dir = + std::env::temp_dir().join(format!("horizon-reused-assets-{}", std::process::id())); + let _ = fs::remove_dir_all(&dir); + let client = b"fake client jar"; + let manifest = br#"{"id": "26.3"}"#; + let write = |path: &str, data: &[u8]| { + let path = dir.join(path); + fs::create_dir_all(path.parent().unwrap()).unwrap(); + fs::write(path, data).unwrap(); + }; + let prism = "PrismLauncher"; + write( + &format!("{prism}/libraries/com/mojang/minecraft/26.3/minecraft-26.3-client.jar"), + client, + ); + write(&format!("{prism}/assets/indexes/34.json"), b"{}"); + let instance = dir.join(prism).join("instances/26.3/minecraft"); + fs::create_dir_all(&instance).unwrap(); + let pins = sources::Pins::read(&serde_json::json!({ "minecraft": { + "version": "26.3", + "client_sha1": sources::sha1_hex(client), + "manifest_sha1": sources::sha1_hex(manifest), + }})) + .unwrap(); + let linked = dir.join("linked"); + let fetch = |_: &str| Ok(manifest.to_vec()); + let lines = std::cell::RefCell::new(Vec::new()); + let log = |line: String| lines.borrow_mut().push(line); + let resolved = sources::resolve(&instance, &pins, &[], &linked, &fetch, &log); + let folder = assets_folder(resolved, &instance, "26.3", &log); + assert_eq!(folder, linked); + assert!(folder.join("assets/indexes/34.json").is_file()); + + // Nothing found: the chosen folder, and a line saying what the game will lack. + let empty = dir.join("empty"); + fs::create_dir_all(&empty).unwrap(); + let resolved = sources::resolve(&empty, &pins, &[], &linked, &fetch, &log); + assert_eq!(assets_folder(resolved, &empty, "26.3", &log), empty); + assert!(lines.borrow().last().unwrap().contains("will be silent")); + fs::remove_dir_all(&dir).unwrap(); + } + #[test] fn resources_without_sounds_are_reported() { let dir = std::env::temp_dir().join(format!("horizon-no-sounds-{}", std::process::id())); @@ -3069,7 +3210,7 @@ mod tests { } #[test] - fn prepared_versions_get_the_built_in_packs_without_preparing_again() { + fn prepared_versions_get_what_the_asset_store_adds_without_preparing_again() { let dir = std::env::temp_dir().join(format!( "horizon-launcher-packs-test-{}", std::process::id() @@ -3080,17 +3221,42 @@ mod tests { fs::create_dir_all(path.parent().unwrap()).unwrap(); fs::write(path, data).unwrap(); }; - // An official launcher folder whose asset store holds the two packs. - let packs = horizon_prepare::BUILT_IN_PACKS.map(|name| (name, name.as_bytes())); + // An official launcher folder whose asset store holds the two packs, a panorama (fake + // 2x2 PNG headers), Unifont, a language and the sound list. + let background = "minecraft/textures/gui/title/background"; + let png = |n: u8| { + let mut data = b"\x89PNG\r\n\x1a\n\0\0\0\x0dIHDR\0\0\0\x02\0\0\0\x02".to_vec(); + data.push(n); + data + }; + let mut assets: Vec<(String, Vec)> = horizon_prepare::BUILT_IN_PACKS + .iter() + .map(|name| (name.to_string(), name.as_bytes().to_vec())) + .collect(); + for face in ["0", "1", "2", "3", "4", "5", "overlay"] { + assets.push(( + format!("{background}/panorama_{face}.png"), + png(face.as_bytes()[0]), + )); + } + for name in [ + "minecraft/font/include/unifont.json", + "minecraft/font/unifont.zip", + "pack.mcmeta", + "minecraft/lang/fr_fr.json", + "minecraft/sounds.json", + ] { + assets.push((name.into(), format!("{{\"{name}\": 1}}").into_bytes())); + } let mut index = serde_json::Map::new(); - for (name, data) in packs { + for (name, data) in &assets { let hash = sources::sha1_hex(data); write( store.join("assets/objects").join(&hash[..2]).join(&hash), data, ); index.insert( - name.into(), + name.clone(), serde_json::json!({"hash": hash, "size": data.len()}), ); } @@ -3105,6 +3271,8 @@ mod tests { .to_string(); write(store.join("versions/9.9/9.9.json"), manifest.as_bytes()); fs::create_dir_all(&empty).unwrap(); + // The client jar's 1x1 placeholder. + let placeholder = b"\x89PNG\r\n\x1a\n\0\0\0\x0dIHDR\0\0\0\x01\0\0\0\x01"; let release = |tag: &str, prepared: bool| { let root = versions.join(tag); write(root.join(COMPLETE), b""); @@ -3112,27 +3280,27 @@ mod tests { "client_sha1": "c", "server_sha1": "s", "manifest_sha1": sources::sha1_hex(manifest.as_bytes())}}); write(root.join("research-lock.json"), lock.to_string().as_bytes()); if prepared { - write(resources_at(&root).join(RESOURCES_MANIFEST), b"{}"); + let resources = resources_at(&root); + write(resources.join(RESOURCES_MANIFEST), b"{}"); + write(resources.join("assets/minecraft/lang/en_us.json"), b"{}"); write( - resources_at(&root).join("assets/minecraft/lang/en_us.json"), - b"{}", + resources + .join("assets") + .join(background) + .join("panorama_0.png"), + placeholder, ); } }; let lines = std::cell::RefCell::new(Vec::new()); let log = |line: String| lines.borrow_mut().push(line); - let pack = |tag: &str, i: usize| { - fs::read( - resources_at(&versions.join(tag)) - .join("assets") - .join(packs[i].0), - ) - .ok() + let asset = |tag: &str, name: &str| { + fs::read(resources_at(&versions.join(tag)).join("assets").join(name)).ok() }; - // Resources prepared before the packs were: a folder without an asset store is skipped. + // Resources prepared without any of them: a folder without an asset store is skipped. release("v0.2.1", true); - complete_packs_in( + complete_assets_in( &versions, "v0.2.1", || vec![empty.clone(), store.clone()], @@ -3140,37 +3308,55 @@ mod tests { ); assert_eq!( *lines.borrow(), - ["Built-in pack assets ready: 2 files, 0.0 MB"] + [ + "Built-in pack assets ready: 2 files, 0.0 MB", + "Panorama assets ready: 7 files, 0.0 MB", + "Font assets ready: 2 files, 0.0 MB", + "Language assets ready: 2 files, 0.0 MB", + "Sound assets ready: 1 files, 0.0 MB", + ] ); - for (i, (_, data)) in packs.iter().enumerate() { - assert_eq!(pack("v0.2.1", i).as_deref(), Some(*data)); + for (name, data) in &assets { + assert_eq!(asset("v0.2.1", name).as_ref(), Some(data), "{name}"); } // Nothing else is prepared again. let resources = resources_at(&versions.join("v0.2.1")); assert_eq!(fs::read(resources.join(RESOURCES_MANIFEST)).unwrap(), b"{}"); - assert!(!resources.join("assets/minecraft/sounds.json").exists()); - // No asset store anywhere: one line, the version stays prepared, and no second try. + // No asset store anywhere: one line each, the version stays prepared, and no second try. lines.borrow_mut().clear(); release("v0.2.2", true); - complete_packs_in(&versions, "v0.2.2", || vec![empty.clone()], &log); - complete_packs_in(&versions, "v0.2.2", || vec![empty.clone()], &log); - assert_eq!(lines.borrow().len(), 1); - assert!( - lines.borrow()[0].starts_with("v0.2.2: Programmer Art and High Contrast not added: "), - "{lines:?}" + complete_assets_in(&versions, "v0.2.2", || vec![empty.clone()], &log); + complete_assets_in(&versions, "v0.2.2", || vec![empty.clone()], &log); + let what = lines + .borrow() + .iter() + .map(|line| line.split(" not added: ").next().unwrap().to_string()) + .collect::>(); + assert_eq!( + what, + [ + "v0.2.2: Programmer Art and High Contrast", + "v0.2.2: The title panorama", + "v0.2.2: The Unifont glyphs", + "v0.2.2: The languages", + "v0.2.2: The sounds", + ] ); assert!(resources_at(&versions.join("v0.2.2")) .join(RESOURCES_MANIFEST) .exists()); - assert_eq!(pack("v0.2.2", 0), None); + assert_eq!( + asset("v0.2.2", &format!("{background}/panorama_0.png")).as_deref(), + Some(&placeholder[..]) + ); // Not prepared, or complete: nothing to do, without even looking for a Minecraft folder. lines.borrow_mut().clear(); release("v0.2.3", false); let unasked = || -> Vec { unreachable!("no folder is needed") }; - complete_packs_in(&versions, "v0.2.3", unasked, &log); - complete_packs_in(&versions, "v0.2.1", unasked, &log); + complete_assets_in(&versions, "v0.2.3", unasked, &log); + complete_assets_in(&versions, "v0.2.1", unasked, &log); assert!(lines.borrow().is_empty()); assert!(!resources_at(&versions.join("v0.2.3")).exists()); let _ = fs::remove_dir_all(&dir); diff --git a/launcher/src/browser/horizon.rs b/launcher/src/browser/horizon.rs index e56f31e09..66553ab50 100644 --- a/launcher/src/browser/horizon.rs +++ b/launcher/src/browser/horizon.rs @@ -343,6 +343,7 @@ fn fetch( ) -> Result { match file.url.strip_prefix("file://").filter(|_| local) { Some(path) => { + let path = local_path(path); let reader = fs::File::open(path).map_err(|e| format!("{path}: {e}"))?; download::save_from_anywhere(file, reader, folder, progress) } @@ -350,6 +351,14 @@ fn fetch( } } +/// The path of a `file:///` URL: on Windows `/C:/x` is `C:/x`. +fn local_path(path: &str) -> &str { + match path.strip_prefix('/') { + Some(rest) if cfg!(windows) && rest.get(1..2) == Some(":") => rest, + _ => path, + } +} + /// What the registry adds to an installed entry. fn trust_label(trust: Trust) -> &'static str { match trust { diff --git a/launcher/src/browser/horizon/pack.rs b/launcher/src/browser/horizon/pack.rs index 73dcd0cc1..3cb6d7c9b 100644 --- a/launcher/src/browser/horizon/pack.rs +++ b/launcher/src/browser/horizon/pack.rs @@ -208,6 +208,16 @@ mod tests { zip.write_all(b"renderDistance:8\n").unwrap(); zip.finish().unwrap(); let size = |p: &Path| fs::metadata(p).unwrap().len(); + // A `file:///` URL: forward slashes (a Windows path's backslashes would be escapes in + // the JSON) and a leading slash before a Windows drive. + let url = |p: &Path| { + let path = p.display().to_string().replace('\\', "/"); + if path.starts_with('/') { + path + } else { + format!("/{path}") + } + }; let text = format!( r#"{{"schema": 1, "projects": [ {{"id": "coords_hud", "kind": "mod", "name": "Coordinates HUD", "summary": "", "license": "MIT", @@ -219,9 +229,9 @@ mod tests { "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(), + url(&hmod), size(&hmod), - hpack.display(), + url(&hpack), size(&hpack), idx::sha256_file(&hpack).unwrap() ); diff --git a/launcher/src/main.rs b/launcher/src/main.rs index d3a32467b..9105f71a2 100644 --- a/launcher/src/main.rs +++ b/launcher/src/main.rs @@ -267,6 +267,9 @@ impl Launcher { { settings.selected = installed.first().cloned(); } + // Pin the window's chrome (macOS title bar) to the choice before the theme reads + // `window.appearance()`, so both draw the same look. + titlebar::apply_appearance(window, settings.appearance); let mut this = Self { theme: Theme::for_appearance(settings.appearance, window.appearance()), page: Page::from_env(), @@ -828,13 +831,14 @@ impl Launcher { // A little earlier than the spawn: file times can be coarse. self.client_started = Some(std::time::SystemTime::now() - std::time::Duration::from_secs(2)); - // The built-in packs the resources lack and the session (a network round trip when the - // token needs renewing) come first; `drain` starts the game when the session arrives. + // What the resources lack (built-in packs, panorama, sounds) and the session (a network + // round trip when the token needs renewing) come first; `drain` starts the game when the + // session arrives. self.starting_client = Some(tag.clone()); self.play_pressed = Some(Instant::now()); let tx = self.tx.clone(); std::thread::spawn(move || { - backend::complete_packs(&tag); + backend::complete_assets(&tag); let session = (|| { let mut accounts = Accounts::load(); let Some((a, secrets)) = account::session_for_launch( @@ -2485,6 +2489,7 @@ impl Launcher { let _ = this.update(cx, |this, cx| { this.settings.appearance = choice; this.settings.save(); + titlebar::apply_appearance(window, choice); this.theme = Theme::for_appearance(choice, window.appearance()); cx.notify(); }); diff --git a/launcher/src/sources.rs b/launcher/src/sources.rs index b27b8417b..50953067e 100644 --- a/launcher/src/sources.rs +++ b/launcher/src/sources.rs @@ -191,6 +191,24 @@ fn client_jars(base: &Path, version: &str) -> Vec { jars.push(jar); } } + jars.extend(loader_jars(base, version)); + jars +} + +/// The client jars the Modrinth App keeps for a mod loader's profile: `versions/-/`, +/// the vanilla jar under the loader's name, when only a Fabric or Quilt profile of the version +/// was ever played (#1783). The jar is checked against the pin like the others. +fn loader_jars(base: &Path, version: &str) -> Vec { + let prefix = format!("{version}-"); + let mut jars: Vec = fs::read_dir(base.join("versions")) + .into_iter() + .flatten() + .flatten() + .filter_map(|entry| entry.file_name().into_string().ok()) + .filter(|name| name.starts_with(&prefix)) + .map(|name| format!("versions/{name}/{name}.jar")) + .collect(); + jars.sort(); jars } diff --git a/launcher/src/sources_tests.rs b/launcher/src/sources_tests.rs index 6d882f032..396f50881 100644 --- a/launcher/src/sources_tests.rs +++ b/launcher/src/sources_tests.rs @@ -289,6 +289,29 @@ fn the_modrinth_app_atlauncher_and_gdlauncher_are_found() { } } +/// A Modrinth App that only ever played a Fabric profile of 26.3 keeps the vanilla client jar +/// under the loader's name (#1783); a profile of another version beside it is not taken. +#[test] +fn a_modrinth_loader_profile_has_the_client_jar() { + let s = Scratch::new("modrinth-fabric"); + s.write( + "ModrinthApp/meta/versions/26.3.1-0.18.1/26.3.1-0.18.1.jar", + b"other", + ); + let jar = "versions/26.3-0.18.1/26.3-0.18.1.jar"; + let dir = s.launcher("ModrinthApp", "meta", jar); + s.write( + "ModrinthApp/meta/versions/26.3-0.18.1/26.3-0.18.1.json", + br#"{"id":"26.3-0.18.1","inheritsFrom":"26.3"}"#, + ); + let profile = dir.join("profiles/Fabric 26.3"); + fs::create_dir_all(&profile).unwrap(); + let linked = s.0.join("linked"); + let got = resolve_with(&profile, &[], &linked, &Mojang::new(MANIFEST)).unwrap(); + assert_eq!(got, linked); + assert_linked(&linked, &dir.join("meta"), jar); +} + #[test] fn an_instance_folder_may_be_chosen() { let s = Scratch::new("instance"); diff --git a/launcher/src/theme.rs b/launcher/src/theme.rs index 60b796709..9d495a598 100644 --- a/launcher/src/theme.rs +++ b/launcher/src/theme.rs @@ -74,22 +74,33 @@ fn c(hex: u32) -> Hsla { rgb(hex).into() } +/// Whether the launcher is pinned dark (`Some(true)`) or light, or follows the system (`None`): +/// `HORIZON_LAUNCHER_APPEARANCE=light` or `dark` (for screenshots and tests), else the player's +/// choice in Settings. +pub fn pinned_dark(choice: Appearance) -> Option { + match std::env::var("HORIZON_LAUNCHER_APPEARANCE").as_deref() { + Ok("light") => Some(false), + Ok("dark") => Some(true), + _ => match choice { + Appearance::Dark => Some(true), + Appearance::Light => Some(false), + Appearance::System => None, + }, + } +} + impl Theme { - /// The player's choice in Settings (dark unless they picked another), the system's when they - /// asked to follow it. `HORIZON_LAUNCHER_APPEARANCE=light` or `dark` overrides both (for - /// screenshots and tests). + /// The pinned look (`pinned_dark`: dark unless the player picked another), the system's when + /// they asked to follow it. pub fn for_appearance(choice: Appearance, system: WindowAppearance) -> Self { - match std::env::var("HORIZON_LAUNCHER_APPEARANCE").as_deref() { - Ok("light") => return Self::light(), - Ok("dark") => return Self::dark(), - _ => {} - } - match (choice, system) { - (Appearance::Light, _) => Self::light(), - (Appearance::System, WindowAppearance::Light | WindowAppearance::VibrantLight) => { - Self::light() - } - _ => Self::dark(), + let system_dark = !matches!( + system, + WindowAppearance::Light | WindowAppearance::VibrantLight + ); + if pinned_dark(choice).unwrap_or(system_dark) { + Self::dark() + } else { + Self::light() } } diff --git a/launcher/src/titlebar.rs b/launcher/src/titlebar.rs index 3c2a78ea4..a023f7d7d 100644 --- a/launcher/src/titlebar.rs +++ b/launcher/src/titlebar.rs @@ -1,6 +1,8 @@ -//! Custom Windows chrome. Other platforms keep their native window decorations. +//! Custom Windows chrome. Other platforms keep their native window decorations; on macOS, +//! `apply_appearance` repaints that chrome (the title bar) to match the chosen look. use crate::theme::Theme; use gpui::{Div, TitlebarOptions, Window}; +use horizon_launcher::backend::Appearance; /// Whether the launcher draws its own title bar (`render`) rather than the system. pub const DRAWN: bool = cfg!(target_os = "windows"); @@ -18,6 +20,44 @@ pub fn render(_theme: &Theme, _window: &Window) -> Option
{ None } +/// macOS keeps the window's appearance on the system setting, so a launcher set to Dark still +/// draws a light title bar on a light desktop. Pin the NSWindow's appearance to the choice +/// instead (`theme::pinned_dark`: `System` clears the pin, handing the chrome back to the +/// system). Elsewhere the chrome is ours (Windows) or the system's (Linux). +#[cfg(target_os = "macos")] +pub fn apply_appearance(window: &Window, choice: Appearance) { + use objc2_app_kit::{ + NSAppearance, NSAppearanceCustomization, NSAppearanceName, NSAppearanceNameAqua, + NSAppearanceNameDarkAqua, NSView, + }; + use raw_window_handle::{HasWindowHandle, RawWindowHandle}; + + // AppKit's name constants are extern statics: reading them is `unsafe` to rustc only. + let name: Option<&'static NSAppearanceName> = match crate::theme::pinned_dark(choice) { + Some(true) => Some(unsafe { NSAppearanceNameDarkAqua }), + Some(false) => Some(unsafe { NSAppearanceNameAqua }), + None => None, + }; + // The trait, not `Window::window_handle` (which is gpui's own `AnyWindowHandle`). + let Ok(handle) = HasWindowHandle::window_handle(window) else { + return; + }; + let RawWindowHandle::AppKit(appkit) = handle.as_raw() else { + return; + }; + // The handle is the window's content view; its `window` is the NSWindow itself. Both outlive + // this call. + let view = unsafe { appkit.ns_view.cast::().as_ref() }; + let Some(native) = view.window() else { + return; + }; + let appearance = name.and_then(NSAppearance::appearanceNamed); + native.setAppearance(appearance.as_deref()); +} + +#[cfg(not(target_os = "macos"))] +pub fn apply_appearance(_: &Window, _: Appearance) {} + #[cfg(target_os = "windows")] mod windows { use super::*; diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java b/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java index 76a951894..ec2c08012 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java @@ -84,7 +84,12 @@ import net.minecraft.world.phys.Vec3; * still), {@code sync NAME} (runs {@code time query gametime}, whose reply is * not recorded, and prints {@code synced NAME} once it is back: the server has * handled everything this player sent before, and everything the server sent - * this player before the reply has been recorded), {@code quit NAME}, {@code + * this player before the reply has been recorded), {@code respawn NAME} (the + * death screen's button, ServerboundClientCommandPacket PERFORM_RESPAWN), + * {@code use NAME PITCH} (the main hand's item used looking that way, + * ServerboundUseItemPacket), {@code pickup NAME SLOT} (a left click on that + * slot of the player's inventory menu, claiming nothing), {@code quit NAME}, + * {@code * players on|off} (whether each player also records the other players added * to and removed from its level, and its own respawns into a dimension), * {@code chat on|off} (whether the system chat each player receives is @@ -130,6 +135,12 @@ public final class LocatorBarClient { players.get(w[1]).send(new net.minecraft.network.protocol.game.ServerboundPlayerAbilitiesPacket(abilities)); } case "still" -> players.get(w[1]).still(); + case "respawn" -> players.get(w[1]).send(new net.minecraft.network.protocol.game.ServerboundClientCommandPacket( + net.minecraft.network.protocol.game.ServerboundClientCommandPacket.Action.PERFORM_RESPAWN)); + case "use" -> players.get(w[1]).use(Float.parseFloat(w[2])); + case "pickup" -> players.get(w[1]).send(new net.minecraft.network.protocol.game.ServerboundContainerClickPacket( + 0, 0, Short.parseShort(w[2]), (byte) 0, net.minecraft.world.inventory.ContainerInput.PICKUP, + new it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap<>(), net.minecraft.network.HashedStack.EMPTY)); case "sync" -> { try { players.get(w[1]).sync(); @@ -250,6 +261,13 @@ public final class LocatorBarClient { } } + private int sequence; + + void use(float pitch) throws Exception { + send(new net.minecraft.network.protocol.game.ServerboundUseItemPacket( + net.minecraft.world.InteractionHand.MAIN_HAND, ++sequence, position.yRot(), pitch)); + } + void still() throws Exception { PositionMoveRotation at = position; // A tick of its own: the server takes one position per client tick (receivedPositionThisTick). diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaLoreTooltipOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaLoreTooltipOracle.java new file mode 100644 index 000000000..462a8016a --- /dev/null +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaLoreTooltipOracle.java @@ -0,0 +1,58 @@ +package dev.horizonlab.parity; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Optional; +import net.minecraft.core.component.DataComponents; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.Style; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.TooltipFlag; +import net.minecraft.world.item.component.ItemLore; +import net.minecraft.world.item.component.TooltipDisplay; + +/** + * The styled lines {@code ItemLore.addToTooltip} adds, one stack (SNBT) per input + * line. Each line is its visited spans ({@code text, colour, bold, italic, + * underlined, strikethrough, obfuscated}); lines are separated by {@code ||}. + */ +public final class VanillaLoreTooltipOracle { + public static void main(String[] args) throws Exception { + ParityData.init(); + var context = Item.TooltipContext.of(ParityData.lookup); + var output = new ArrayList(); + for (String line : Files.readAllLines(Path.of(args[0]))) { + if (line.isBlank() || line.startsWith("#")) continue; + var stack = ParityData.stack(line); + var display = stack.getOrDefault(DataComponents.TOOLTIP_DISPLAY, TooltipDisplay.DEFAULT); + ItemLore lore = stack.get(DataComponents.LORE); + if (lore == null || !display.shows(DataComponents.LORE)) { + output.add("-"); + continue; + } + var lines = new ArrayList(); + lore.addToTooltip(context, component -> lines.add(spans(component)), TooltipFlag.NORMAL, stack); + output.add(lines.isEmpty() ? "-" : String.join(" || ", lines)); + } + Files.write(Path.of(args[1]), output); + } + + /** {@code Component.visit} from {@code Style.EMPTY}: the style a tooltip draws. */ + static String spans(Component component) { + var out = new StringBuilder(); + component.visit((style, text) -> { + if (text.isEmpty()) return Optional.empty(); + if (out.length() > 0) out.append(" | "); + int color = style.getColor() == null ? -1 : style.getColor().getValue(); + out.append(text).append('\t').append(color).append('\t') + .append(style.isBold()).append('\t') + .append(style.isItalic()).append('\t') + .append(style.isUnderlined()).append('\t') + .append(style.isStrikethrough()).append('\t') + .append(style.isObfuscated()); + return Optional.empty(); + }, Style.EMPTY); + return out.toString(); + } +} diff --git a/roadmap/README.md b/roadmap/README.md index 55cf9d09f..22cde1a7d 100644 --- a/roadmap/README.md +++ b/roadmap/README.md @@ -54,8 +54,8 @@ Whoever moves the work (the orchestrator on each heartbeat, or anyone in their p `2026-43.md`): `# Title`, a paragraph or two for players, then `## Technical` and the same week for contributors, with its pull requests. `build` puts it into website/data/roadmap.json; the hub's Journal tab shows it among the releases, and `/data/journal.json` carries it to Kira's #updates and to X. -6. Suggestions from the website and Discord are read and triaged by the maintainers. A good one becomes an - item in a normal pull request; none is published automatically. +6. Suggestions from the website and Discord reach the maintainers' private channel through Kira (below). A good + one becomes an item in a normal pull request; none is published automatically. CI runs `tests/test_roadmap.py` (in the Python tests), which fails when ROADMAP.md is not what `build` writes or when the file breaks a rule; `python3 tools/roadmap.py check --offline` runs the same rules without GitHub. @@ -89,3 +89,28 @@ merged pull request, release, shipped item and weekly summary, newest first, the - `announce: true` (the item's `announce` key) asks Kira to post the shipped item on X too, with `post`. - `https://playhorizon.dev/data/roadmap.json` is `roadmap.toml` as JSON (`areas`, `items` with every key above, `overview`, the weekly summaries); an item's page is `https://playhorizon.dev/hub#`. + +## The intake: votes, bug reports, suggestions + +The hub's forms and vote buttons talk to the telemetry server (`https://telemetry.playhorizon.dev`, +`crates/horizon-telemetry-server`, `src/hub.rs` and `src/intake.rs`; its README lists the limits and settings). +Kira uses the same endpoints, so a bug or an idea goes the same way from the site and from Discord. + +| Endpoint | Body | Answer | +| --- | --- | --- | +| `POST /hub/vote` | `{"item": "", "voter": "", "up": true}` | `{"item", "votes"}`; `"up": false` takes the vote back | +| `GET /hub/votes` | | `{"votes": {"": n}}`, a minute old at most | +| `POST /hub/report` | `{"title", "did", "happened", "expected", "version"?, "system", "name"?}` | 201 `{"id", "issue": ""}`; 202 `{"id", "issue": null}` when GitHub was down (opened later) | +| `POST /hub/suggest` | `{"text", "item"?, "name"?}` | 202 `{"id"}` | +| `GET /hub/suggestions?after=` | `Authorization: Bearer ` | `{"suggestions": [{"id", "text", "item"?, "name"?, "created_at"}]}`, oldest first | + +- A report becomes an issue at once, labels `bug` and `from-website`, in the `#horizon-bugs` template (What I did, + What happened, What I expected, Horizon version, OS, CPU, GPU) with "Reported on playhorizon.dev". Every field + but the version and the name is required; mentions, `#123` references and HTML in people's text are made inert. +- Suggestions are never published. Kira polls `/hub/suggestions` with the last id it relayed and posts each one + to `#roadmap-suggestions`. +- Errors are `{"error": "…"}`: 400 names the field (`"did: 3 to 2000 characters"`), 429 means too many from that + address, 503 a full day or reports turned off (no GitHub token on the server). +- The page shows the forms and the buttons only once `GET /hub/votes` answers. Until then it points to GitHub and + Discord. It keeps a random voter id and the items it voted for in `localStorage` (`hub:voter`, `hub:voted`). + diff --git a/roadmap/roadmap.toml b/roadmap/roadmap.toml index 5ad4d8846..669596fba 100644 --- a/roadmap/roadmap.toml +++ b/roadmap/roadmap.toml @@ -49,10 +49,22 @@ name = "Rendering" id = "launcher" name = "Launcher" +[[area]] +id = "platforms" +name = "Platforms" + [[area]] id = "extras" name = "Horizon extras" +[[area]] +id = "ai" +name = "AI and agents" + +[[area]] +id = "community" +name = "Community" + [[area]] id = "project" name = "Code and tools" @@ -206,6 +218,75 @@ dev = """A review of the whole tree: dead code and stubs removed (#1432, #1658), feature to its files and harnesses.""" links = ["#1432", "#1658", "#1453", "#1454", "#1448", "#1707", "docs/architecture.md"] +[[item]] +id = "native-shaders" +title = "Horizon's own shader format and kit" +status = "now" +area = "rendering" +plain = """Horizon's own way to make shader packs, with tools to write and test them. Every pack is checked before it \ +runs, and the registry will list them.""" +dev = """`horizon-shader-api` (API 0.1, experimental): WGSL stages with typed inputs inside the renderer's passes, \ +options as WGSL override constants, and the `horizon-shader new|check|pack` tool with templates and the \ +example pack Golden Hour. Merged: the check every pack passes (#1820) and the tool, templates and guide \ +(#1822). In review: the post stage running in the game with hot reload (#1861), the sky stage (#1863) and \ +registry checks that use the game's own check (#1855). Left: the material, lighting and shadow stages, a \ +last-good fallback on reload, a GPU-time overlay per stage and budgets (docs/shaders.md, \ +docs/api/shaders.md). The Iris/OptiFine loader is a separate item, 'Shader packs, written again from \ +scratch'.""" +links = ["#1820", "#1822", "#1861", "#1863", "#1855", "docs/shaders.md", "docs/api/shaders.md"] + +[[item]] +id = "server-mods" +title = "Mods that run on the server" +status = "now" +area = "mods" +plain = """Mods that run on the server side of a world, so a dedicated server or a LAN world can have mods that react \ +to players joining, chat and blocks being broken or placed, and that add commands.""" +dev = """API 0.3, experimental first (#1833, in review): the same sandboxed component runs on the server thread and \ +the dedicated server, with world events (block broken or placed, entity spawned or killed, player joined or \ +left), game rules and server commands with typed arguments. The world, item and entity interfaces come after \ +(see 'Mods that read and change the world'). docs/api/mods.md, docs/mods.md.""" +links = ["#1833", "docs/api/mods.md", "docs/mods.md"] + +[[item]] +id = "mod-screens" +title = "Screens, input and drawing for mods" +status = "now" +area = "mods" +plain = """Mods will be able to open their own screens and settings pages and react to keys and item use, so bigger \ +mods, such as the Orbital Laser, can run on the normal game.""" +dev = """Capability `ui` is in review (#1864): mod-owned screens and a settings page under Options > Mods, built from \ +vanilla's widgets. Still to design: `input` (an item's use, key binds, the crosshair target), `render` (a \ +budgeted draw list on the native shader API) and `world` (changing blocks). Orbital Laser is the acceptance \ +test: it needs all four today through its author's fork (see 'Orbital Laser on the normal game').""" +links = ["#1864", "docs/mods.md"] + +[[item]] +id = "modder-tools" +title = "Tools for people who make mods" +status = "now" +area = "mods" +plain = """Better tools for mod makers: a test runner that needs no game window, templates for each kind of mod, and \ +clear promises about what keeps working from one version to the next.""" +dev = """`cargo horizon-mod test` runs a mod's test scripts against a fake game snapshot (#1816) and, for server \ +mods, scripted server events (#1837); the content template and the guides are merged (#1859, #1810, \ +docs/api/stability.md). Left: templates for hud, server and machine mods, typed wrappers in the SDK, a \ +frozen 0.1 compatibility mod that every runtime must load, a CI check that fails when a stable reference \ +entry is removed without a new major version, and the SDK on crates.io once the API is stable.""" +links = ["#1816", "#1837", "docs/api/stability.md", "website/docs/test-a-mod.md"] + +[[item]] +id = "pregen" +title = "Pre-generate an area with /pregen" +status = "now" +area = "servers" +plain = """A /pregen command that builds an area of the world ahead of time, so players don't wait for new terrain when \ +they explore.""" +dev = """Generates, lights and saves an area before anyone walks there (#1805, a contributor's pull request, in \ +review). Useful on servers, and it moves the generation cost away from the first visit; the generation \ +speed-ups are under 'Steady frames at long render distances'.""" +links = ["#1805"] + # ---------------------------------------------------------------------------------------------------- Next [[item]] @@ -239,11 +320,14 @@ id = "benchmarks" title = "Fresh benchmarks against vanilla" status = "next" area = "performance" -plain = """New public numbers comparing Horizon with vanilla on many kinds of computers, measured the same way \ -every time, on the website's Benchmarks page.""" +plain = """New public numbers comparing Horizon with vanilla on many kinds of computers, measured the same way every \ +time, with a history that shows what each release changed.""" dev = """Redo the comparison after the current performance work (#1073); reliability, coverage and presentation \ -(#1346); a scene must count as loaded only once its first chunk arrives (#1749). Players' reports come in through \ -`--benchmark` and `horizon-launcher data benchmark` (docs/benchmark.md).""" +(#1346); a scene must count as loaded only once its first chunk arrives (#1749). Next: a performance history \ +across releases (an automatic comparison of each release with the last, on a timeline), more report fields \ +(server TPS and MSPT, power, battery drain, temperature, throttling) and more hardware (a MacBook on \ +battery, low-end laptops, community reports). Players' reports come in through `--benchmark` and \ +`horizon-launcher data benchmark` (docs/benchmark.md).""" links = ["#1073", "#1346", "#1749", "docs/benchmark.md"] [[item]] @@ -262,36 +346,661 @@ links = ["#1496", "#1609", "#1608", "#1704", "#1758"] id = "intel-mac" title = "Horizon on Intel Macs" status = "next" -area = "launcher" -plain = """Builds of the game and the launcher for Macs with an Intel processor.""" +area = "platforms" +plain = "Builds of the game and the launcher for Macs with an Intel processor." dev = """x86_64 game and launcher builds, picked per processor (#1171). Before it ships it needs a run on a real \ Intel Mac: Rosetta on Apple Silicon only proves the CPU code. Requests: #1161, #1792.""" links = ["#1171", "#1161", "#1792"] +[[item]] +id = "world-entry" +title = "Faster world creation and entry on small CPUs" +status = "next" +area = "performance" +plain = """Creating a world and joining it should be quick even on a four-thread laptop, where a fresh world at 32 \ +chunks can make you wait minutes for the first terrain.""" +dev = """Cut the per-column generation cost (noise sampling, surface rules, aquifers) and the lock convoy on \ +structure starts, and leave one core free for the rest of the game as vanilla does (#1760 and #1778, in \ +review, are listed under 'Steady frames at long render distances'). Still to profile on small CPUs: the \ +first seconds of a fresh world deliver no chunk, and fluids remesh for about a minute after loading. \ +Measured with `tools/perf_bench.py` (docs/benchmark.md, docs/profiling.md).""" +links = ["docs/benchmark.md", "docs/profiling.md"] + +[[item]] +id = "server-tick" +title = "Servers that hold 20 TPS with 50 to 100 players" +status = "next" +area = "performance" +plain = """Servers that keep 20 ticks per second with 50 to 100 players. Today Horizon holds it up to 50 bots and drops \ +to about 10 at 100.""" +dev = """Measured against vanilla on equal work (docs/server.md, Scale): Horizon is ahead with few players and when \ +exploring, but vanilla's tick is shorter with 25 to 50 players near spawn. Ideas on the list: send entity \ +changes instead of rebuilding every entity each tick (about 15 percent), fewer memory reads in random ticks \ +and collision scans, cheaper item collision checks, mob AI and natural spawning. The bot benchmark \ +(`horizon-bots`) is the proof.""" +links = ["docs/server.md"] + +[[item]] +id = "low-end-mode" +title = "Low-end computers: Steam Deck, old Macs, 8 GB" +status = "next" +area = "performance" +plain = """Horizon should run well on small machines: integrated graphics, the Steam Deck, older Macs and 8 GB of \ +memory, with a battery mode that saves power on laptops.""" +dev = """Partly done: an opt-in 60 FPS cap with fewer workers on battery, Far Detail Reduced for integrated GPUs and \ +animated textures on the GPU (both shipped). Left: measured targets and fixes per class of machine (memory \ +use, worker counts, startup) on a Steam Deck, old Intel and Apple Silicon Macs and 8 GB computers, reported \ +on the Benchmarks page.""" +links = ["docs/benchmark.md", "website/docs/performance.md"] + +[[item]] +id = "upscaling" +title = "Native upscaling" +status = "next" +area = "performance" +plain = """Draw the game at a lower resolution and sharpen it to your screen's size, in the way of FSR or DLSS, to gain \ +frames on weaker graphics cards. An optional graphics setting.""" +dev = """A render-scale setting below the window size with an upscaler of our own that runs on every wgpu backend. \ +Proof: frame time against native resolution on integrated GPUs, and screenshots for image quality. Issue \ +#1403.""" +links = ["#1403"] + +[[item]] +id = "targeted-optimisations" +title = "A batch of targeted optimisations" +status = "next" +area = "performance" +plain = """Several smaller speed-ups, each measured: idle block entities that sleep, cheaper particles, faster \ +structure placement, high-rate mice and very large texture packs.""" +dev = """Block entities and hoppers that skip work when idle (the hopper item scan is already skipped when no item is \ +near); a particle frustum test first (also a parity gap), then particle quads built on the GPU; jigsaw \ +shortcuts that skip blocked connectors and pool elements known not to fit, with layouts unchanged (trial \ +chambers and ancient cities first); batched raw mouse input on Windows for 8000 Hz mice; a better atlas \ +packer for huge resource packs (a 64x pack builds a 704 MiB atlas for 452 MiB of textures); sign-heavy \ +scenes (#1461). Profile first, keep what pays, show before and after numbers.""" +links = ["#1461", "docs/profiling.md"] + +[[item]] +id = "convert-to-rust" +title = "A Convert to Rust button for what isn't rewritten yet" +status = "next" +area = "modpacks" +plain = """When a mod, shader pack or modpack hasn't been rewritten for Horizon, one button converts what can be \ +converted, so you don't have to wait for the rewrite.""" +dev = """Generalise the mod browser's Port step (`horizon-port-pack`: the data, assets, worldgen and block and item \ +definitions of Java mods, .mrpack and CurseForge zips) to every kind of content: code mods where the API \ +allows it (see 'Mods with code') and shader packs, converting an Iris/OptiFine pack into Horizon's format. \ +The browser says what works before the install. Java code itself does not run (docs/modpacks.md, \ +docs/mod-browser.md).""" +links = ["docs/modpacks.md", "docs/mod-browser.md"] + +[[item]] +id = "code-mods" +title = "Mods with code: machines, magic, mobs" +status = "next" +area = "modpacks" +plain = """Making mods that add machines, magic and mobs work in Horizon, in steps, starting with machines. They are \ +most of what a big modpack is made of.""" +dev = """333 of the 496 mods in a big pack (ATM10) contain code. Issue #1854 ranks the API gaps (ticking block \ +entities, networking, rendering, menus, inventories) that unlock them in tiers, with machines first as the \ +recommended path and a furnace ported tick for tick as proof. The first API step is a draft (#1189, see \ +'Machines and blocks with inventories'). The path isn't started yet (docs/modpacks.md).""" +links = ["#1854", "docs/modpacks.md"] + +[[item]] +id = "datapack-browser" +title = "Data packs in the mod browser" +status = "next" +area = "modpacks" +plain = """Search and install data packs from the launcher's mod browser, for the world you choose, as you do mods and \ +resource packs.""" +dev = """A Datapacks tab beside Mods and Resource packs: install into a chosen world's datapacks folder, with the \ +same checks and 'works in Horizon' notes as the other kinds (docs/mod-browser.md).""" +links = ["docs/mod-browser.md"] + +[[item]] +id = "prepare-without-java" +title = "Prepare a version without Java" +status = "next" +area = "servers" +plain = """Set up Horizon, a server first, without Java on your computer: published tables are combined with your own \ +copy of the game.""" +dev = """Today `horizon-prepare` runs Mojang's exporters with Java. Issue #1750 proposes publishing the computed \ +version tables once from CI and assembling the rest locally from the player's own jar and assets, provided \ +the tables hold no Mojang content; a contributor offered a pull request, and it would help phones too. A \ +server-only prepare from `server.jar` alone is the first step (docs/server.md).""" +links = ["#1750", "docs/server.md"] + +[[item]] +id = "server-admin" +title = "Server console, bans and a full admin API" +status = "next" +area = "servers" +plain = """Manage a server from a console, ban and pardon players, and drive it from other tools through a full admin \ +API and an MCP server for AI agents.""" +dev = """`horizon-server console` (a text interface), `ban`, `pardon`, `ban-ip` and `mcp` over stdio. Admin API \ +additions: the player list, kick, ban, op and whitelist, an SSE stream of logs and events, a text metrics \ +endpoint, config GET and PATCH, backup delete and download. The panel's later features (bans, worlds, \ +server.properties import, logs and crash reports, modpacks) and vanilla's management protocol (the JSON-RPC \ +over WebSocket that the `management-server-*` keys configure), so vanilla's tools can manage a Horizon \ +server. docs/server.md, docs/api/server.md.""" +links = ["docs/server.md", "docs/api/server.md"] + +[[item]] +id = "hosting-polish" +title = "Hosting polish: Windows service, safe updates" +status = "next" +area = "servers" +plain = """Smoother hosting: a real Windows service, a backup before every update, backups that keep the world and each \ +player's state, and a changed view distance or message without a restart.""" +dev = """A Windows service wrapper instead of the logon scheduled task; `backups.before_update`; hot reload of config \ +for view distance and the message of the day (needs engine support); backups that keep world and player \ +state (#1808, a contributor's draft, in review). docs/hosting.md, docs/server.md.""" +links = ["#1808", "docs/hosting.md"] + +[[item]] +id = "tailscale-hosting" +title = "Guided hosting with Tailscale and an invite button" +status = "next" +area = "servers" +plain = """In the launcher, a guide and a button to host a server and invite friends over Tailscale, so nobody has to \ +open ports on a router.""" +dev = """On the Servers page (create, start, back up, update and invite are shipped) add a Tailscale setup that \ +explains and checks each step, and an invite button that hands friends what they need to join over the \ +tailnet. Buttons first, no docs needed, with the details in an Advanced section. Remote access over \ +Tailscale is already described in docs/server.md.""" +links = ["docs/hosting.md", "docs/server.md"] + +[[item]] +id = "docker-image" +title = "An official Docker image for servers" +status = "next" +area = "servers" +plain = """An official image to run a Horizon dedicated server in Docker with one command, your own copy of the game \ +files mounted in.""" +dev = """The repository has the Dockerfile (`packaging/server/Dockerfile`: a multi-stage build on Debian \ +bookworm-slim, user `horizon`, volume `/data`, port 25565, game files mounted read-only, `HORIZON_EULA` and \ +friends answering the first-start questions), and anyone can build it (docs/hosting.md, section 9). Left: \ +publish the image to a public registry from the release workflow, tagged per release, so it follows the same \ +checks, and update the docs and the launcher. The image never holds Mojang's files.""" +links = ["docs/hosting.md", "docs/server.md"] + +[[item]] +id = "one-single-player-path" +title = "One single-player path: remove the in-frame world" +status = "next" +area = "gameplay" +plain = """Single player will have one way to run, the server thread, so every fix works everywhere and there are fewer \ +bugs. The older in-frame mode goes once everything passes without it.""" +dev = """`--in-frame` (also `--no-server-thread` and `HORIZON_SERVER_THREAD=0`) still ticks the world inside the \ +window's `Game`, and most harnesses test that path. Delete it once `lan_e2e --server-thread` and the parity \ +harnesses pass on the server thread, which also halves the bug surface. See also 'Single player as a server' \ +and docs/server-levels.md.""" +links = ["docs/server-levels.md"] + +[[item]] +id = "villages-combat-parity" +title = "Villages and combat, exactly as in vanilla" +status = "next" +area = "gameplay" +plain = """Villages and fights behaving as in vanilla: iron golems defending villages, villagers remembering their \ +homes, mace smashes and knockback.""" +dev = """Villages: iron golem village goals, villager memories across saves, villager age, job-site claiming on \ +dedicated servers, flower offers (#1639 is one report). Combat: the mobs' `attack_knockback` attribute, mace \ +smash logic, the hit knockback source on the host, the fire burnout biome tag. Each is checked against \ +vanilla by a harness (docs/rust-parity.md).""" +links = ["#1639", "docs/rust-parity.md"] + +[[item]] +id = "audio-device" +title = "Choose the audio output device" +status = "next" +area = "gameplay" +plain = "Pick the speakers or headphones Horizon plays through, in Music & Sounds." +dev = """An output device list in Music & Sounds, falling back to the system default. #1035 started it: a player on \ +an older Windows computer had no sound at all, and a device choice is how such cases are found and fixed.""" +links = ["#1035"] + +[[item]] +id = "mod-world-items" +title = "Mods that read and change the world" +status = "next" +area = "mods" +plain = """Mods that can see and change blocks, read inventories and items, and find and affect entities, all through \ +the server so every player sees the same thing.""" +dev = """After server mods: read blocks, states and biomes in loaded chunks and change them through the server's \ +block log, so clients and the parity harnesses see vanilla's updates; the player's inventory, held item, \ +open container and item stacks with components; entity queries near a position, a safe subset of entity data \ +and data-driven mod entity types. Snapshots and batched calls per tick, never per-block host calls in hot \ +paths. docs/api/mods.md ('Not there yet').""" +links = ["docs/api/mods.md", "docs/modpacks.md"] + +[[item]] +id = "machines-api" +title = "Machines and blocks with inventories (API 0.4)" +status = "next" +area = "mods" +plain = """Mods that add machines: blocks that tick, hold items and have their own menus. This unlocks the technology \ +mods of big modpacks.""" +dev = """Block entities and menus: ticking machines with slots, server menu logic and the menu protocol to clients, \ +energy, item and fluid transfer, data attachments, accessory slots and custom recipe types, with a typed \ +client and server channel and typed config beside them. A draft (#1189) proves a furnace ported tick for \ +tick and holds the ATM10 code-mods plan; the design is redone through the API policy before it merges \ +(docs/modpacks.md, docs/api/stability.md).""" +links = ["#1189", "docs/modpacks.md"] + +[[item]] +id = "mod-languages" +title = "Lua and other languages for mods" +status = "next" +area = "mods" +plain = """Write mods as Lua scripts, in the style of KubeJS, with no compiler, and later in other languages that pass \ +the tests. Every language runs in the same sandbox.""" +dev = """Lua first: Horizon ships a Lua interpreter that is itself a WebAssembly mod, and a script mod's manifest \ +names it and the capabilities it uses, so a script can do exactly what a compiled mod with the same manifest \ +can, with hot reload of `main.lua`. Then C, C++, Zig, Go (TinyGo), AssemblyScript and JavaScript or \ +TypeScript are measured against Rust on the same tick-heavy example, and each is supported only with a \ +template, an example and a CI test. docs/api/mods.md ('Ways in').""" +links = ["docs/api/mods.md"] + +[[item]] +id = "shader-pack-examples" +title = "Well-known shader looks, rewritten for Horizon" +status = "next" +area = "rendering" +plain = """Popular shader packs rewritten in Horizon's format, so good packs exist from the start. Only packs whose \ +licence is open source and compatible with ours are rewritten, never anything else.""" +dev = """The first candidates are Photon, Chocapic13 and MakeUp Ultra Fast, each only after its licence is read and \ +recorded: a pack is rewritten only if it is open source and compatible with Horizon's licence, and the \ +rewrite is written from the shader format's documentation and the look on screen, not from anyone's code. \ +Today only the Golden Hour example exists (`crates/horizon-shader-api/packs/golden_hour`).""" +links = ["docs/shaders.md", "crates/horizon-shader-api/packs/golden_hour"] + +[[item]] +id = "beta-label" +title = "A visible beta label" +status = "next" +area = "launcher" +plain = """A clear 'beta' label in the launcher and the game, so nobody is surprised that Horizon is still being built.""" +dev = """Show Beta in the launcher's header and about page, on the game's title screen and on the website, beside the \ +version number (docs/versioning.md).""" +links = ["docs/versioning.md"] + +[[item]] +id = "wayland-polish" +title = "Wayland and Hyprland polish" +status = "next" +area = "platforms" +plain = """Fixes for Linux desktops that use Wayland and Hyprland: fullscreen, the clipboard, the GUI scale after a \ +restart and the like.""" +dev = """Open: the GUI Scale is clamped to 2 after a restart under Hyprland, while XWayland keeps it (#1866). Earlier \ +reports covered fullscreen dropping on menu clicks, the clipboard and a white overlay (#1007, closed). \ +Checked on a real Wayland session.""" +links = ["#1866"] + +[[item]] +id = "agent-control-protocol" +title = "A versioned agent-control protocol" +status = "next" +area = "ai" +plain = """A stable, documented way for AI agents and scripts to drive the game and the launcher, with versions and an \ +event stream so tools keep working as Horizon changes.""" +dev = """`hello` answers with the API version, the release and the features; `horizon-ctl --api`; MCP serverInfo; \ +reply schemas generated into the reference; a `test.` namespace for hooks; transcript compatibility tests; \ +`subscribe` as a stream of events on one connection; Horizon option files by key and saved-server commands. \ +Agent control itself (`horizon-ctl`, both MCP servers, the No AI switch) is done: docs/agent-control.md, \ +docs/api/agent-control.md.""" +links = ["docs/agent-control.md", "docs/api/agent-control.md", "docs/api/stability.md"] + +[[item]] +id = "own-agents-porting" +title = "Port mods with your own AI agent" +status = "next" +area = "ai" +plain = """You can port a mod to Horizon with your own AI agent on your own computer. Horizon documents how and gives \ +you the tools, but provides no hosted agents.""" +dev = """A player runs their own agent locally with `horizon-ctl` and the MCP servers (docs/agent-control.md), `cargo \ +horizon-mod`, `horizon-port-pack` and the parity harnesses as the check. Horizon writes the guide \ +(website/docs/agents.md, docs/modpacks.md) and runs no agents, accounts or keys for players. The No AI \ +switch turns all of it off.""" +links = ["website/docs/agents.md", "docs/modpacks.md", "docs/agent-control.md"] + +[[item]] +id = "x-account" +title = "Horizon on X (coming soon)" +status = "next" +area = "community" +plain = "Coming soon: an official Horizon account on X, used to announce releases and the weekly summary." +dev = """Kira already posts the roadmap items marked `announce = true` when they ship, and the weekly summary, as \ +soon as the account's credentials exist (roadmap/README.md, 'The journal'). Waiting for the account itself.""" +links = ["roadmap/README.md"] + +[[item]] +id = "agents-app" +title = "Pipeline protection and a GitHub App for agents" +status = "next" +area = "project" +plain = """Safer releases: changes to the release machinery need the owner's review, and AI agents use a smaller \ +identity of their own instead of a personal account.""" +dev = """`.github/CODEOWNERS` lists the workflows, the release and signing tools, the update-checking code and the \ +installers (#1015); the branch protection that makes it count waits for the `horizon-agent` GitHub App, \ +which reads and writes code, pull requests and issues but can't change settings, workflows, secrets or \ +members, and whose tokens last an hour. It needs the owner to create the App and switch the protection on \ +(docs/security.md).""" +links = ["docs/security.md"] + +[[item]] +id = "signed-installers" +title = "Signed updates everywhere, installers included" +status = "next" +area = "project" +plain = """Every download Horizon makes is checked against the project's signature, including the first install with \ +install.sh and install.ps1.""" +dev = """Releases carry signed checksums, and the launcher and `horizon-server update` refuse anything unsigned \ +(#1011, #1014). Left: the first install through `install.sh` and `install.ps1` still trusts only the \ +checksum GitHub publishes, because verifying Ed25519 signatures needs OpenSSL 3, which macOS doesn't ship \ +(docs/security.md).""" +links = ["docs/security.md"] + # ---------------------------------------------------------------------------------------------------- Later [[item]] id = "ai-companion" title = "An optional AI teammate" status = "later" -area = "extras" -plain = """An AI teammate that plays beside you and that you can talk to. It is off unless you turn it on, and the \ -No AI switch hides it completely.""" -dev = """Draft pull requests: the companion (#1068), its safety modes and their evaluation (#1069), and a route \ -that finishes the game (#1070). Local models first; it ships only once the safety evaluation passes.""" +area = "ai" +plain = """An optional AI teammate you can talk to by voice. It plays beside you, helps or finishes the game with you, \ +runs on your computer first, and exists only if you turn it on. The No AI switch hides it.""" +dev = """Draft pull requests: the companion (#1068), its safety modes and their evaluation (#1069) and a route that \ +finishes the game (#1070). In the drafts: a companion that follows, fights, mines, crafts, sleeps and \ +respawns; chat and push-to-talk voice, with speech recognition and a choice of voices; presets (very strong, \ +helper, beat the game); a guard model with Clean, Kid and Salty modes; perception levels from Fair to \ +All-knowing; hardware detection with a simple mode and an Advanced tab; local models first, cloud keys \ +optional. The No AI switch is already in the game. Left: the speedrun route doesn't finish the game reliably \ +yet, voices for more languages, and the safety evaluation, which must pass before it ships.""" links = ["#1068", "#1069", "#1070"] [[item]] id = "new-versions" -title = "Following new Minecraft versions" +title = "Every Minecraft version, prepared automatically" status = "later" area = "gameplay" -plain = """When Mojang releases a new version of the game, Horizon follows it, and every system is checked against \ -the new version again.""" -dev = """The port pipeline detects a release, diffs its classes against 26.3 and writes a port report \ -(docs/version-ports.md); the harnesses then run against the new version.""" +plain = """Every past and future Minecraft version prepared for Horizon automatically. When Mojang releases one, \ +Horizon follows it, and every system is checked against it again.""" +dev = """The port pipeline (docs/version-ports.md, `tools/version_port.py`) detects a release, prepares it, diffs its \ +classes against the pinned 26.3, regenerates the data-driven tables, runs every parity harness on it and \ +writes a port report that becomes issues. The goal is as little human work as possible for any version, past \ +or future. Detection and the first slices are merged; the scheduled agent that runs it every day isn't \ +installed yet. Two versions in one build are out of scope.""" links = ["docs/version-ports.md"] +[[item]] +id = "exact-far-terrain" +title = "Exact far terrain from real chunks" +status = "later" +area = "performance" +plain = """Far terrain drawn from the world you really built and explored, caves and builds included, not only a \ +preview from the seed.""" +dev = """Keep a 3D mip store on disk built from real chunks, drawn by the GPU beside the seed preview for areas \ +nobody has visited. Written from scratch and measured before it is kept. Builds on Far Detail and the \ +far-terrain levels of detail already shipped; the open work on them is #718.""" +links = ["#718"] + +[[item]] +id = "older-jars" +title = "Older Fabric and Forge mods" +status = "later" +area = "modpacks" +plain = """Recognise mods built for versions before 26.1 in the mod browser, so they can be checked and carried over \ +like newer ones.""" +dev = """A mapping table from intermediary and SRG names so pre-26.1 jars can be classified, and a rename for mod ids \ +with dashes (`fabric-api`); the port step then treats them like newer jars (docs/mod-browser.md).""" +links = ["docs/mod-browser.md"] + +[[item]] +id = "modpack-own-packs" +title = "A modpack's own resource and shader packs" +status = "later" +area = "modpacks" +plain = """Importing a modpack also brings the resource packs and shader packs it ships with, where Horizon can use \ +them.""" +dev = """Porting a pack carries data, assets and worldgen today; the resource packs and shader packs bundled with it \ +are left out. Resource packs can be carried as they are; shader packs go through the converter \ +(docs/modpacks.md).""" +links = ["docs/modpacks.md"] + +[[item]] +id = "whole-modpacks" +title = "Whole modpacks, such as ATM10, rewritten" +status = "later" +area = "modpacks" +plain = """Rewriting a complete big modpack for Horizon so it runs far better than the original. Not a priority now: \ +the work above comes first.""" +dev = """The project's first goal for modpacks: support them ten times better than vanilla and port any of them. It \ +was set aside in favour of compatibility through the browser. What exists: ATM10's world generates and 42 of \ +its 496 mods are carried at least in part (docs/modpacks.md). It depends on 'Mods with code' and the native \ +mod API.""" +links = ["docs/modpacks.md"] + +[[item]] +id = "java-mod-bridge" +title = "Running Java mods that aren't rewritten" +status = "later" +area = "modpacks" +plain = """Run Java mods that nobody has rewritten for Horizon yet, through a bridge or a converter. Hard and not \ +started: the Convert button and native rewrites come first.""" +dev = """Earlier research (#527, closed) found that Java mods can't simply run, because they call Minecraft's own \ +classes. Options to look at again: a jar-to-WebAssembly backend, or a bridge for simple Fabric mods. \ +Whatever is chosen has to run inside the same sandbox and capability checks as native mods.""" +links = ["#527"] + +[[item]] +id = "alt-accounts" +title = "Ely.by and other sign-in providers" +status = "later" +area = "servers" +plain = """Sign in with other account providers, such as Ely.by or any Yggdrasil-compatible one, beside Microsoft.""" +dev = """Issue #1270 asks for Ely.by and Yggdrasil-compatible servers with an account selector in the launcher. It \ +builds on the Microsoft sign-in (docs/microsoft-account.md).""" +links = ["#1270", "docs/microsoft-account.md"] + +[[item]] +id = "friends-online-mode" +title = "Online-mode checks for Play with Friends" +status = "later" +area = "servers" +plain = """Friends who join through an invite code could prove who they are with their Microsoft account, as on \ +online-mode servers.""" +dev = """Play with Friends joins over an encrypted tunnel with an invite code (docs/play-with-friends.md, \ +'Identity'). Online-mode checks against Mojang's session servers can be added on top of the same tunnel; \ +they need Microsoft sign-in first, which is coming soon. Play with Friends stays self-hosted between \ +players, with no relay run by the project.""" +links = ["docs/play-with-friends.md"] + +[[item]] +id = "orbital-laser" +title = "Orbital Laser on the normal game" +status = "later" +area = "mods" +plain = """The community mod Orbital Laser, made for a fork of Horizon, running on the normal game and listed in the \ +registry as the showcase of the mod API.""" +dev = """A pull request on https://github.com/Stink-O/orbital-laser (MIT) once the input, ui, render and world \ +capabilities exist (see 'Screens, input and drawing for mods'): today the mod needs them through its \ +author's fork. Tested in the game, and listed in the registry.""" +links = ["https://github.com/Stink-O/orbital-laser"] + +[[item]] +id = "client-mod-natives" +title = "Native versions of popular client mods" +status = "later" +area = "mods" +plain = """Built-in versions of the client mods players ask for: block info on hover, a recipe viewer like JEI, a world \ +map and schematic overlays.""" +dev = """Done as extras: a mod list and menu, a minimap and a zoom (docs/extras.md). Left: block info tooltips, a \ +recipe viewer (a contributor offered to port JEI if it becomes a native feature), a world map and a \ +schematic overlay. They need the read-only interfaces `inventory-read`, `recipes`, `world-read` and `render` \ +in the mod API, or they go into the game directly.""" +links = ["docs/extras.md", "docs/mods.md"] + +[[item]] +id = "create-physics" +title = "Create-style physics as a native mod" +status = "later" +area = "mods" +plain = """Contraptions and physics like the Create and Aeronautics mods, as a mod for Horizon rather than part of the \ +base game.""" +dev = """Issue #805 (porting QOL mods) closed with this answer: it would come as a native Horizon mod on the mod API. \ +It needs server mods, entities and rendering first.""" +links = ["#805"] + +[[item]] +id = "proximity-voice" +title = "Proximity voice chat as a native mod" +status = "later" +area = "mods" +plain = """Hear nearby players talk, as with the voice chat mods of other Minecraft versions, as a Horizon mod.""" +dev = """Needs server mods, mod networking and an audio interface in the mod API before anything can be built \ +(docs/api/mods.md).""" +links = ["docs/api/mods.md"] + +[[item]] +id = "light-default-pack" +title = "A light default shader pack" +status = "later" +area = "rendering" +plain = """A small built-in look (tone mapping, colour grade, a cheap vignette) that costs almost nothing, for players \ +who want a nicer picture without a heavy pack.""" +dev = """An opt-in, composite-only pack on the native shader API, under about 0.3 ms at 1080p, shown on the Shader \ +Packs page. Pack option sliders and profiles, and a Failed row with the full error, belong with it.""" +links = ["docs/shaders.md"] + +[[item]] +id = "ray-tracing" +title = "Optional ray tracing" +status = "later" +area = "rendering" +plain = """An optional ray-tracing mode for graphics cards that support it (RTX and recent AMD): shadows, water \ +reflections and global lighting on top of the vanilla look.""" +dev = """A spike with ray-traced sun shadows, off by default, is a draft (#1180). Reflections, global illumination, \ +Apple M3 and newer if it is cheap, and a performance report per card come after.""" +links = ["#1180"] + +[[item]] +id = "hdr" +title = "HDR monitors" +status = "later" +area = "rendering" +plain = "Output for HDR monitors: brighter highlights and richer colour where your screen supports it." +dev = """Issue #1441. Needs a surface format and a tone-mapping path per backend, and tests on real HDR screens on \ +Windows, macOS and Linux.""" +links = ["#1441"] + +[[item]] +id = "msaa-fullscreen" +title = "MSAA, anisotropic filtering, exclusive fullscreen" +status = "later" +area = "rendering" +plain = """More graphics options: multisample anti-aliasing, anisotropic filtering and an exclusive fullscreen mode. \ +Today Horizon has FXAA and borderless fullscreen.""" +dev = """Not implemented (docs/graphics.md). Anisotropic filtering needs linear magnification in wgpu, which blurs \ +close blocks, so close-up sharpness has to be kept first; MSAA would sit beside FXAA; exclusive fullscreen \ +where the backend supports it.""" +links = ["docs/graphics.md"] + +[[item]] +id = "video-studio" +title = "A complete video studio inside the game" +status = "later" +area = "extras" +plain = """Record, replay and edit videos without leaving the game: record in a keypress, replay a session, cut and \ +finish the video. One of Horizon's original goals.""" +dev = """Three stages. Recording: capture gameplay in a keypress with an encoder thread and little frame-time cost. \ +Replay: a replay mode for recorded sessions. Editing: cuts, camera paths, titles and export. None of it \ +exists yet; the agent-control socket (docs/agent-control.md) already lets tools drive the game for scripted \ +shots.""" +links = ["docs/agent-control.md"] + +[[item]] +id = "controllers" +title = "Controllers: tested on real pads, and in the launcher" +status = "later" +area = "gameplay" +plain = """Make controller support solid: tested with real PlayStation and Xbox pads and a Steam Deck, and usable in \ +the launcher too.""" +dev = """The game side is merged (docs/controllers.md): pads read with gilrs, menus, rumble, glyphs, remapping and \ +DualSense adaptive triggers, lightbar and haptics, but not yet confirmed on real pads or a Steam Deck \ +(DualSense haptics stay behind a switch until then). Left: a test pass on real hardware, and the same focus \ +navigation in the GPUI launcher.""" +links = ["docs/controllers.md"] + +[[item]] +id = "debug-screen" +title = "The full debug screen" +status = "later" +area = "gameplay" +plain = """Vanilla's complete F3 screen: debug options, a profiler chart, ten-second captures and an atlas dump.""" +dev = """Today F3 shows position, chunk, light, biome and FPS (docs/native-client.md). Vanilla also has F3+F6 debug \ +options with profiles, F3+1 the profiler pie, F3+L a ten-second capture and F3+S the atlas dump (#562 asked \ +for the F3 editor).""" +links = ["#562", "docs/native-client.md"] + +[[item]] +id = "lean-installs" +title = "Lean installs that use less disk" +status = "later" +area = "launcher" +plain = """Smaller installs: Horizon keeps one shared copy of the prepared game files and removes what only the \ +preparation needed.""" +dev = """A shared resource store keyed by the preparer's digest, no prepare intermediates left behind, the current \ +and the previous version kept, and an automatic clean-up. Not started.""" + +[[item]] +id = "linux-arm64" +title = "Horizon on Linux ARM64" +status = "later" +area = "platforms" +plain = "Game and launcher builds for 64-bit ARM Linux computers." +dev = """Issue #1709: aarch64 builds of the game and the launcher in the release workflow, tested on a real ARM \ +machine with its Vulkan driver. The server build already drops audio and controllers \ +(`--no-default-features`).""" +links = ["#1709"] + +[[item]] +id = "opengl-fallback" +title = "OpenGL for old computers" +status = "shipped" +shipped_in = "v0.2.63" +area = "platforms" +plain = """Horizon now starts on old PCs whose graphics card has no Vulkan or DirectX 12 driver: it switches to OpenGL \ +by itself, and Options > Graphics can pick OpenGL by hand.""" +dev = """wgpu's GL backend (WGL on Windows, EGL on Linux) as the last fallback after Vulkan and DirectX 12 (#1917), \ +sprite arrays and CPU sprite animation for drivers without compute (#1920), no indirect draws below GL 4.3 \ +(#1936). Minimum OpenGL 3.3 core. Shader packs don't run on it yet.""" +links = ["#1917", "#1920", "#1936"] + +[[item]] +id = "phones-and-browser" +title = "Android, iOS and the browser" +status = "later" +area = "platforms" +plain = """Versions of Horizon for Android, iOS and web browsers. Nothing official yet; contributors have started the \ +first pieces.""" +dev = """No official build. Android was first closed as not planned, with touch controls, a mobile interface, \ +packaging and loading the player's own assets all missing; the roadmap now includes it, iOS and a \ +WebAssembly build for the browser as long-term goals. First pieces from contributors: `horizon-dualsense` \ +compiling on iOS (#1813) and helper-thread limits on Android (#1751). Preparing without Java (#1750) matters \ +here too. Horizon ships none of Mojang's files on any platform.""" +links = ["#1813", "#1751"] + +[[item]] +id = "video-series" +title = "A video series about Horizon" +status = "later" +area = "community" +plain = """A series of videos about how Horizon is made and what is new, with each upload announced on Discord.""" +dev = """Kira already shows new uploads in a read-only Discord channel (done). The series follows each stage of the \ +project: a script, edits, titles and a thumbnail per video.""" + # ---------------------------------------------------------------------------------------------------- Help wanted [[item]] @@ -480,3 +1189,38 @@ plain = """The launcher's Servers page creates, starts, backs up and updates a H invites friends.""" dev = """The launcher's Servers page (#1026) over `horizon-server` (docs/hosting.md).""" links = ["#1026", "docs/hosting.md"] + +[[item]] +id = "cli-server-install" +title = "A server you install with one command" +status = "next" +area = "servers" +plain = """Install and run a Horizon dedicated server from a terminal with a single command, no launcher needed, for \ +VPSes and headless machines.""" +dev = """A player's idea (#horizon-ideas). `horizon-server` already runs headless; left: an install script \ +(Linux/macOS shell, Windows PowerShell) that fetches the signed release, checks it, asks for or points at the \ +player's own game files and starts the server, documented in docs/hosting.md.""" +links = ["docs/hosting.md"] + +[[item]] +id = "launcher-look-menus" +title = "Game menus in the launcher's style" +status = "later" +area = "extras" +plain = """An optional look for the game's own menus and options that matches the Horizon launcher. Off by default: \ +the vanilla menus stay as they are.""" +dev = """A player's idea (#horizon-ideas). A built-in resource pack or a Horizon setting that restyles the title, \ +pause and options screens with the launcher's design tokens (crates/horizon-theme), following \ +docs/design-system.md; off by default because it changes the vanilla look.""" +links = ["docs/design-system.md"] + +[[item]] +id = "linux-packages" +title = "AppImage and a Nix flake" +status = "later" +area = "platforms" +plain = """Install Horizon on any Linux distribution as an AppImage, and on NixOS through a Nix flake.""" +dev = """A player's idea (#horizon-ideas). An AppImage of the launcher and game from the release workflow, and a \ +flake.nix packaging the release binaries with their runtime libraries (Vulkan loader, X11/Wayland, ALSA), \ +the way horizon-parity's NixOS setup runs them today.""" +links = [] diff --git a/tests/test_parity_main.py b/tests/test_parity_main.py new file mode 100644 index 000000000..7a72ecae5 --- /dev/null +++ b/tests/test_parity_main.py @@ -0,0 +1,43 @@ +"""tools/parity_main.py: the base of a parity-main run, and the bisect after a failure.""" +from pathlib import Path +import tempfile +import unittest +from unittest import mock + +from tools import parity_main + + +class ParityMainTests(unittest.TestCase): + def test_first_bad_finds_the_first_failing_merge(self): + commits = list("abcdefgh") # h failed; the base before a passed + for culprit in commits: + tried = [] + fails = lambda commit: tried.append(commit) or commit >= culprit + self.assertEqual(parity_main.first_bad(commits, fails), culprit) + self.assertLessEqual(len(tried), 3, "log2 of 8 merges") + self.assertEqual(parity_main.first_bad(["a"], lambda commit: self.fail("nothing to try")), "a") + + def test_base_is_the_newest_green_run_head_contains(self): + with tempfile.TemporaryDirectory() as tmp, mock.patch.object(parity_main, "ROOT", Path(tmp)): + git = lambda *arguments: parity_main.git(*arguments).stdout.strip() + git("init", "-q") + for n in range(3): + git("-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "--allow-empty", "-m", str(n)) + head, parent, grandparent = git("rev-parse", "HEAD"), git("rev-parse", "HEAD^1"), git("rev-parse", "HEAD~2") + git("checkout", "-q", "-b", "side", grandparent) + git("-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "--allow-empty", "-m", "side") + side = git("rev-parse", "HEAD") + git("checkout", "-q", head) + self.assertEqual(parity_main.base(runs=[side, grandparent, parent]), grandparent, + "a run on a commit HEAD doesn't contain is skipped") + self.assertEqual(parity_main.base(runs=[]), parent, "no green run yet: the last merge") + self.assertEqual(parity_main.base(runs=[head]), head) + + def test_a_harness_a_commit_does_not_have_cannot_fail_there(self): + with mock.patch.object(parity_main.subprocess, "run") as run: + self.assertFalse(parity_main.run_harnesses(["not_a_harness_yet"], 60)) + run.assert_not_called() + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_parity_reach.py b/tests/test_parity_reach.py new file mode 100644 index 000000000..9b7464b47 --- /dev/null +++ b/tests/test_parity_reach.py @@ -0,0 +1,111 @@ +"""tools/parity_reach.py: which workspace sources a harness's probe can reach.""" +from pathlib import Path +import tempfile +import textwrap +import unittest + +from tools import parity, parity_reach + +PROBE = [["example", "world_probe", "release", ["-p", "a"], True]] + + +def workspace(root, files): + for path, text in files.items(): + (root / path).parent.mkdir(parents=True, exist_ok=True) + (root / path).write_text(textwrap.dedent(text), encoding="utf-8") + + +class ReachTests(unittest.TestCase): + def setUp(self): + self.directory = tempfile.TemporaryDirectory() + self.root = Path(self.directory.name) + workspace(self.root, { + "crates/a/Cargo.toml": '[package]\nname = "a"\n\n[[example]]\nname = "world_probe"\npath = "examples/probe/main.rs"\n', + "crates/a/src/lib.rs": """ + pub mod used; + pub mod unused; + mod methods; + mod display; + mod table; + #[cfg(test)] + mod tests; + """, + "crates/a/src/used.rs": """ + pub struct Thing; + pub fn helper() -> u32 { 1 } + """, + # Same names as used.rs, but nothing names this module. + "crates/a/src/unused.rs": "pub struct Thing;\npub fn helper() -> u32 { 2 }\n", + "crates/a/src/methods.rs": """ + use crate::used::Thing; + impl Thing { + pub fn method(&self) -> u32 { 3 } + pub fn never_called(&self) -> u32 { 4 } + } + """, + "crates/a/src/display.rs": """ + impl std::fmt::Display for crate::used::Thing { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "thing") } + } + """, + "crates/a/src/table.rs": """ + macro_rules! table { () => { pub struct Table; }; } + table!(); + """, + "crates/a/src/tests.rs": "use crate::used::Thing;\nimpl Thing { pub fn method_in_tests(&self) {} }\n", + "crates/a/examples/probe/main.rs": "mod dispatch;\nmod p;\n", + "crates/a/examples/probe/dispatch.rs": "pub fn args() {}\n", + "crates/a/examples/probe/p.rs": """ + use a::used::{helper, Thing}; + pub fn main() { println!("{} {} {}", helper(), Thing.method(), Thing); } + """, + }) + self.dependencies = sorted(str(path.relative_to(self.root)) for path in self.root.rglob("*") if path.is_file()) + self.dependencies.remove("crates/a/examples/probe/main.rs") # parity.only_probes_run leaves it out + + def tearDown(self): + self.directory.cleanup() + + def narrowed(self, changed=(), artifacts=PROBE): + graph = parity_reach.SourceGraph(self.root, parity.rust_code) + return parity_reach.narrow(self.dependencies, artifacts, graph, set(changed)) + + def test_keeps_what_the_probe_reaches(self): + kept = {path for path, (whole, _) in self.narrowed().items() if whole} + self.assertIn("crates/a/src/used.rs", kept) + self.assertIn("crates/a/src/methods.rs", kept) # Thing.method() + self.assertIn("crates/a/src/display.rs", kept) # std's Display for a reached type + self.assertIn("crates/a/Cargo.toml", kept) + self.assertNotIn("crates/a/src/unused.rs", kept) # its names match, but nothing names its module + self.assertNotIn("crates/a/src/table.rs", kept) + self.assertNotIn("crates/a/src/tests.rs", kept) # compiled into tests only + + def test_module_parents_count_through_their_declarations(self): + self.assertEqual(self.narrowed()["crates/a/src/lib.rs"], (False, {"used", "methods", "display"})) + text = (self.root / "crates/a/src/lib.rs").read_text() + self.assertEqual(parity_reach.declarations(text, {"used"}, False), "pub mod used;") + self.assertEqual(parity_reach.declarations(text, {"tests"}, False), "#[cfg(test)]\nmod tests;") + # Adding or dropping a module beside the reached ones changes neither view. + other = text.replace("mod table;", "mod table2;\nmod more;") + self.assertEqual(parity_reach.declarations(other, {"used"}, False), "pub mod used;") + self.assertEqual(parity_reach.declarations(other, {"used"}, True), parity_reach.declarations(text, {"used"}, True)) + self.assertNotEqual(parity_reach.declarations(text + "pub fn f() {}\n", {"used"}, True), + parity_reach.declarations(text, {"used"}, True)) + + def test_a_new_file_counts_once_it_is_reached(self): + (self.root / "crates/a/src/lib.rs").write_text("pub mod used;\nmod extra;\n") + workspace(self.root, {"crates/a/src/extra.rs": "impl crate::used::Thing { pub fn method(&self) -> u32 { 5 } }\n"}) + self.assertIn("crates/a/src/extra.rs", self.narrowed(changed={"crates/a/src/extra.rs"})) + + def test_a_deleted_dependency_counts(self): + (self.root / "crates/a/src/unused.rs").unlink() + self.assertIn("crates/a/src/unused.rs", self.narrowed()) + + def test_unknown_probes_keep_everything(self): + self.assertEqual(set(self.narrowed(artifacts=[])), set(self.dependencies)) + self.assertEqual(set(self.narrowed(artifacts=[["example", "missing", "release", ["-p", "a"], True]])), + set(self.dependencies)) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_parity_runner.py b/tests/test_parity_runner.py index d10fde854..2d16e7179 100644 --- a/tests/test_parity_runner.py +++ b/tests/test_parity_runner.py @@ -300,3 +300,17 @@ class LargeOutputTests(unittest.TestCase): self.assertFalse((own / "dump.txt").exists()) # chunk_generation may still be writing there. self.assertTrue((shared / "dump.txt").exists()) + + def test_rust_named_directories_and_big_oracle_outputs_go_too(self): + with tempfile.TemporaryDirectory() as tmp: + local, state = Path(tmp) / ".local", Path(tmp) / "state" + own = local / "rust-state-provider-parity" + own.mkdir(parents=True) + for name, size in (("rust.txt", 16), ("server.txt", 16), ("client.txt", 64), ("report.json", 4)): + (own / name).write_bytes(b"x" * size) + trace = {"commands": [["java", "Oracle", str(own)], ["probe", "fixture", str(own)]]} + with mock.patch.multiple(parity, LOCAL=local, STATE_DIR=state, LARGE_OUTPUT=8, KEPT_ORACLE_OUTPUT=32): + listing = parity.outputs_listing("state_provider#2", fresh=True) + listing.write_text(f"{own / 'server.txt'}\n{own / 'client.txt'}\n") + parity.clear_large_outputs("state_provider#2", trace) + self.assertEqual(sorted(path.name for path in own.iterdir()), ["report.json", "server.txt"]) diff --git a/tests/test_prepare_output.py b/tests/test_prepare_output.py new file mode 100644 index 000000000..00c489fbb --- /dev/null +++ b/tests/test_prepare_output.py @@ -0,0 +1,64 @@ +import json +import tempfile +import unittest +from pathlib import Path + +from tools import prepare_output + + +class PrepareOutputTests(unittest.TestCase): + def preparation(self, root: Path, files: dict[str, bytes]) -> Path: + for rel, data in files.items(): + path = root / rel + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(data) + return root + + def test_what_varies_between_runs_does_not_count(self): + manifest = {'schema': 7, 'data_version': 5023, 'render_files_sha256': {'a.json': 'x'}} + a = { + 'horizon-resources.json': json.dumps(manifest).encode(), + 'render-definitions.json': b'{"a": 1, "b": [1, 2]}', + 'creative-inventory.json': b'{"tags": [["b", "a"]]}', + 'block-behaviours.json': b'{"c": "Foo$$Lambda/0x0000012a"}', + 'export.log': b'run 1', + 'assets/minecraft/sounds/a.ogg': b'one', + } + manifest['render_files_sha256'] = {'a.json': 'y'} + b = { + 'horizon-resources.json': json.dumps(manifest).encode(), + 'render-definitions.json': b'{"b": [1, 2], "a": 1}', + 'creative-inventory.json': b'{"tags": [["a", "b"]]}', + 'block-behaviours.json': b'{"c": "Foo$$Lambda/0x00000ff0"}', + 'export.log': b'run 2', + 'assets/minecraft/sounds/a.ogg': b'two', + } + with tempfile.TemporaryDirectory() as tmp: + first = prepare_output.digest(self.preparation(Path(tmp) / 'a', a)) + second = prepare_output.digest(self.preparation(Path(tmp) / 'b', b)) + self.assertEqual(first, second) + # The data does, and so do the manifest's schema and the files there are. + (Path(tmp) / 'b/render-definitions.json').write_bytes(b'{"b": [2, 1], "a": 1}') + self.assertNotEqual(first, prepare_output.digest(Path(tmp) / 'b')) + (Path(tmp) / 'b/render-definitions.json').write_bytes(b'{"b": [1, 2], "a": 1}') + (Path(tmp) / 'b/horizon-resources.json').write_text(json.dumps({**manifest, 'schema': 8})) + self.assertNotEqual(first, prepare_output.digest(Path(tmp) / 'b')) + (Path(tmp) / 'b/horizon-resources.json').write_text(json.dumps(manifest)) + (Path(tmp) / 'b/new.nbt').write_bytes(b'') + self.assertNotEqual(first, prepare_output.digest(Path(tmp) / 'b')) + + def test_the_versions_file_ends_at_the_constant(self): + versions = prepare_output.parse_versions('# comment\n1 aa\n2 bb\n') + self.assertEqual(versions, [(1, 'aa'), (2, 'bb')]) + self.assertEqual(prepare_output.problems(versions, 2), []) + self.assertTrue(prepare_output.problems(versions, 1)) + self.assertTrue(prepare_output.problems([(1, 'aa'), (3, 'bb')], 3)) + self.assertTrue(prepare_output.problems([(1, 'aa'), (2, 'aa')], 2)) + root = Path(prepare_output.__file__).resolve().parent.parent + current = prepare_output.parse_versions((root / prepare_output.VERSIONS).read_text()) + constant = prepare_output.CONSTANT.search((root / prepare_output.LIB).read_text()) + self.assertEqual(prepare_output.problems(current, int(constant.group(1))), []) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_release_train.py b/tests/test_release_train.py index 56064fa42..ee1fbcae5 100644 --- a/tests/test_release_train.py +++ b/tests/test_release_train.py @@ -2,6 +2,7 @@ import datetime as dt from pathlib import Path import tempfile import unittest +from unittest import mock from tools import release_train as train @@ -147,6 +148,26 @@ class ReleaseTrainTests(unittest.TestCase): self.assertFalse(train.decision("game", [], NOW, new_line="0.3", blockers=[7])[0]) self.assertTrue(train.decision("game", [pr(1)], NOW, blockers=[])[0]) + def test_game_releases_ship_only_what_parity_main_passed(self): + def runs(parity, checks): + def gh_json(*arguments): + table = parity if "parity-main" in arguments else checks + if "--commit" in arguments: + sha = arguments[arguments.index("--commit") + 1] + return [{"status": s, "conclusion": c} for h, s, c in table if h == sha] + return [{"headSha": h, "status": s, "conclusion": c} for h, s, c in table] + return mock.patch.object(train, "gh_json", gh_json) + checks = [("c", "in_progress", ""), ("b", "completed", "success"), ("a", "completed", "success")] + with runs([("c", "queued", ""), ("b", "completed", "cancelled"), ("a", "completed", "success")], checks): + self.assertEqual(train.green_main_head(), "b", "checks alone: the newest green checks") + self.assertEqual(train.green_main_head(parity=True), "a", "a superseded parity-main run counts for nothing") + self.assertTrue(train.passed_on("parity-main", "a")) + self.assertFalse(train.passed_on("parity-main", "b")) + with runs([("b", "completed", "failure"), ("a", "completed", "success")], checks): + self.assertIsNone(train.green_main_head(parity=True), "a failed batch holds releases until fixed") + with runs([("c", "completed", "success")], checks): + self.assertIsNone(train.green_main_head(parity=True), "its checks must pass too") + def test_a_release_is_published_only_after_its_smoke_test_and_with_no_blocker(self): drew = {"error": None, "frames": 12710, "chunks": 81, "meshed_sections": 550, "player": [160.5, 71.0, 156.5]} self.assertIsNone(train.gate(drew, [])) diff --git a/tools/command_exec_corpus.py b/tools/command_exec_corpus.py index 0e376e260..4bc202f78 100644 --- a/tools/command_exec_corpus.py +++ b/tools/command_exec_corpus.py @@ -291,6 +291,19 @@ def entities(): cmd('/data get entity @e[tag=root,limit=1] Passengers[0].Health'), cmd('/data get entity @e[tag=root,limit=1] Passengers[0].id'), cmd('/kill @e[tag=root]'), + # Saved attribute bases are read for every mob, then Health or the max + # health when there is none (LivingEntity.readAdditionalSaveData, #1483). + cmd('/summon minecraft:cow 4.5 210 8.5 {Tags:["attr"],NoAI:1b,NoGravity:1b,' + 'attributes:[{id:"minecraft:max_health",base:40.0d},{id:"minecraft:movement_speed",base:0.5d}]}'), + cmd('/data get entity @e[tag=attr,limit=1] Health'), + cmd('/attribute @e[tag=attr,limit=1] minecraft:max_health base get'), + cmd('/attribute @e[tag=attr,limit=1] minecraft:movement_speed base get'), + cmd('/kill @e[tag=attr]'), + cmd('/summon minecraft:zombie 4.5 210 8.5 {Tags:["attr"],NoAI:1b,NoGravity:1b,Health:7.0f,' + 'attributes:[{id:"minecraft:attack_damage",base:9.0d}]}'), + cmd('/data get entity @e[tag=attr,limit=1] Health'), + cmd('/attribute @e[tag=attr,limit=1] minecraft:attack_damage base get'), + cmd('/kill @e[tag=attr]'), *setup('/kill @e[type=minecraft:armor_stand]'), {'op': 'tick', 'n': 25}, cmd('/execute if entity @e[tag=h]'), @@ -327,6 +340,31 @@ def riding_teleport(): '/tp @s 8.5 230 8.5 0 0') +def mob_riding(): + """Mobs mount and dismount mobs with /ride, NBT passengers included (#1520).""" + rider, vehicle = '@e[tag=ride_r,limit=1]', '@e[tag=ride_v,limit=1]' + return [ + *setup('/summon minecraft:cow 4.5 220 12.5 {NoAI:1b,NoGravity:1b,Tags:["ride_v"]}', + '/summon minecraft:pig 6.5 220 12.5 {NoAI:1b,NoGravity:1b,Tags:["ride_r"]}'), + cmd(f'/ride {rider} dismount'), + cmd(f'/ride {rider} mount @s'), + cmd(f'/ride {rider} mount {rider}'), + cmd(f'/ride {rider} mount {vehicle}'), + cmd(f'/ride {rider} mount {vehicle}'), + cmd(f'/ride {vehicle} mount {rider}'), + cmd('/execute as @e[tag=ride_r] on vehicle run say carrying'), + cmd(f'/ride {rider} dismount'), + cmd(f'/data get entity {rider} Pos'), + cmd('/execute as @e[tag=ride_r] on vehicle run say still carrying'), + *setup('/kill @e[tag=ride_r]', '/kill @e[tag=ride_v]'), + *setup('/summon minecraft:cow 4.5 220 12.5 {NoAI:1b,NoGravity:1b,Tags:["ride_v"],' + 'Passengers:[{id:"minecraft:pig",NoAI:1b,NoGravity:1b,Tags:["ride_r"]}]}'), + cmd(f'/ride {rider} dismount'), + cmd(f'/data get entity {rider} Pos'), + *setup('/kill @e[tag=ride_r]', '/kill @e[tag=ride_v]'), + ] + + def scores(): return [ cmd('/scoreboard objectives add harness dummy'), @@ -1017,6 +1055,7 @@ def groups(): ('blocks and border', world_blocks()), ('entities', entities()), ('riding teleport', riding_teleport()), + ('mob riding', mob_riding()), ('scores, teams and bossbars', scores()), ('execute', execute()), ('levels', levels()), diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index 320a9c56a..0ec7218d8 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -6252,5 +6252,151 @@ CHECKS += ['crystals'] CHECK_FNS.update(crystals=check_crystals) +def check_crystal_blast(host, guest, out): + """#1533: TNT removes a crystal without a blast; a punched crystal blasts once. + Both clients must see the surviving distant and invulnerable crystals.""" + spots = apart(host, guest) + seen = {} + for label, player, other in (('host', host, guest), ('guest', guest, host)): + x, y, z = spots[label] + y += 32 + player('command', command='gamemode creative') + for offset, cause in enumerate(('tnt', 'melee')): + # Leave survivors in separate arenas; the scratch world is discarded. + z = spots[label][2] + (0 if label == 'host' else 80) + offset * 40 + case = f'{label}-{cause}' + host('command', command=f'fill {x - 3} {y - 1} {z - 3} {x + 25} {y - 1} {z + 5} minecraft:bedrock') + host('command', command=f'fill {x - 3} {y} {z - 3} {x + 25} {y + 4} {z + 5} minecraft:air') + positions = [[x + dx, y, z + dz] for dx, dz in ((2.5, 0.5), (12.5, 0.5), (22.5, 0.5), (4.5, 3.5))] + + def remaining(game): + crystals = [p for p in game('status')['projectiles'] if p['kind'] == 'minecraft:end_crystal'] + return [i for i, at in enumerate(positions) + if any(math.dist(p['position'], at) < 0.01 for p in crystals)] + + for i, at in enumerate(positions): + host('command', command=f'summon minecraft:end_crystal {at[0]} {at[1]} {at[2]} {{Invulnerable:{1 if i == 3 else 0}b}}') + player('command', command=f'tp @s {x + 0.5} {y} {z + 0.5}') + other('command', command='gamemode creative') + other('command', command=f'tp @s {x + 0.5} {y} {z + 4.5}') + player('command', command='clear @s') + player.wait(20) + for game in (host, guest): + assert remaining(game) == [0, 1, 2, 3], f'{case}: crystals not tracked on {game.name}' + if cause == 'tnt': + player('command', command=f'summon minecraft:tnt {x + 0.5} {y} {z + 0.5} {{fuse:10s}}') + else: + player.raw('look-at', positions[0][0], y + 1, positions[0][2]) + player.raw('mouse', 'left', 'click') + player.wait(80) + result = {game.name: remaining(game) for game in (host, guest)} + seen[case] = result + # TNT reaches the first crystal only. A direct crystal blast reaches + # the second too, but that crystal must not detonate the third. + expected = [1, 2, 3] if cause == 'tnt' else [2, 3] + assert all(ids == expected for ids in result.values()), f'{case}: expected {expected}, got {result}' + return seen + + +CHECKS += ['crystal_blast'] +CHECK_FNS.update(crystal_blast=check_crystal_blast) + + +def check_boat_dismount_pose(host, guest, out): + """Removing a boat detaches its mobs on the server and every observing client.""" + x, y, z = map(math.floor, pos_of(host)) + bx, bz = x + 3.5, z + 3.5 + tag = 'boat_dismount_pose' + host('command', command='gamemode creative') + host('command', command=f'fill {x - 2} {y - 1} {z - 2} {x + 8} {y - 1} {z + 8} minecraft:stone') + host('command', command=f'fill {x - 2} {y} {z - 2} {x + 8} {y + 4} {z + 8} minecraft:air') + host('command', command=f'summon minecraft:oak_boat {bx} {y} {bz} {{Tags:["{tag}"]}}') + for _ in range(2): + host('command', command=f'summon minecraft:husk {bx} {y} {bz} {{NoAI:1b,Tags:["{tag}"]}}') + host.wait(5) + try: + host.wait(20) + boat = next(o for o in host('status')['objects'] + if o['kind'].endswith('oak_boat') and math.dist(o['position'], [bx, y, bz]) < 2) + riders = [m['id'] for m in host.raw('mobs', 64)['near'] + if m['kind'] == 'minecraft:husk' and m['mount'] == boat['id']] + assert len(riders) == 2, f'the boat did not collect both husks: {riders}' + for game in (host, guest): + game.wait(10) + for rider in riders: + m = mob_seen(game, rider) + assert m and m['mount'] == boat['id'] and m['passenger'], f'{game.name} did not see seated husk {rider}: {m}' + game.raw('look-at', bx, y + 1, bz) + game.shot(out, 'boat-passengers-seated') + host('command', command=f'tp @s {bx - 3} {y} {bz} 0 0') + host.wait(5) + # Aim at the near rim, below the riders, so their boxes cannot + # intercept the creative player's one hit that destroys the boat. + host.raw('look-at', bx - 0.55, y + 0.1, bz) + host.raw('mouse', 'left', 'click') + seen = {} + for game in (host, guest): + game.wait(20) + assert all(o['id'] != boat['id'] for o in game('status')['objects']), f'{game.name} still sees the boat' + seen[game.name] = [mob_seen(game, rider) for rider in riders] + game.raw('look-at', bx, y + 1, bz) + game.shot(out, 'boat-passengers-dismounted') + for m in seen[game.name]: + assert m and m['mount'] is None and not m['passenger'], f'{game.name} still draws a removed boat\'s passenger: {m}' + return {name: [{k: m[k] for k in ('id', 'mount', 'passenger')} for m in mobs] for name, mobs in seen.items()} + finally: + host('command', command=f'kill @e[tag={tag}]') + + +CHECKS += ['boat-dismount-pose'] +CHECK_FNS['boat-dismount-pose'] = check_boat_dismount_pose + + +def check_sprint_fov(host, guest, out): + """Releasing Ctrl must preserve a sprint until W is released, then reset FOV. + + PlayerInput reports keys; PlayerCommand starts/stops the sprint. Confusing + them clears the client's sprint flag while leaving its speed modifier on. + """ + seen = {} + for game in (host, guest): + game('command', command='gamemode creative') + game('command', command='effect clear @s') + game.raw('options-set', 'fovEffectScale', '1.0') + x, y, z = map(math.floor, pos_of(game)) + game('command', command=f'fill {x - 2} {y} {z - 2} {x + 2} {y + 3} {z + 20} minecraft:air') + game('command', command=f'fill {x - 2} {y - 1} {z - 2} {x + 2} {y - 1} {z + 20} minecraft:stone') + game('command', command=f'tp @s {x + 0.5} {y} {z + 0.5} 0 0') + game.wait(20) + try: + game.raw('key', 'w', 'down') + game.raw('key', 'ctrl', 'down') + game.wait(10) + started = game('state')['player'] + game.raw('key', 'ctrl', 'up') + game.wait(10) + released_ctrl = game('state')['player'] + game.raw('key', 'w', 'up') + game.wait(20) + stopped = game('state')['player'] + finally: + game.raw('key', 'ctrl', 'up') + game.raw('key', 'w', 'up') + seen[game.name] = states = { + label: {k: p[k] for k in ('sprinting', 'fov_modifier')} + for label, p in [('started', started), ('released_ctrl', released_ctrl), ('stopped', stopped)] + } + for label in ('started', 'released_ctrl'): + assert states[label]['sprinting'], f'{game.name} {label}: {states}' + assert abs(states[label]['fov_modifier'] - 1.15) < 0.002, f'{game.name} {label}: {states}' + assert not stopped['sprinting'], f'{game.name}: {states}' + assert abs(stopped['fov_modifier'] - 1.0) < 0.002, f'{game.name}: {states}' + return seen + + +CHECKS += ['sprint-fov'] +CHECK_FNS['sprint-fov'] = check_sprint_fov + + if __name__ == '__main__': main() diff --git a/tools/parity.py b/tools/parity.py index 503a5f3bf..3f18b922d 100644 --- a/tools/parity.py +++ b/tools/parity.py @@ -45,6 +45,11 @@ import subprocess import sys import time +try: + from . import parity_reach +except ImportError: + import parity_reach + ROOT = Path(__file__).resolve().parents[1] LOCAL = ROOT / ".local" STATE_DIR = LOCAL / "parity-runner" @@ -652,6 +657,9 @@ def prebuild(artifacts, jobs): LARGE_OUTPUT = 64 << 20 +# Vanilla outputs above this are deleted after a pass too: rerunning those few oracles (12 harnesses on the Mac +# mini's runner) costs less than the 26 GB they held in every checkout; smaller ones stay for oracle reuse. +KEPT_ORACLE_OUTPUT = 256 << 20 OWN_DIRECTORIES = {"parity-target", "parity-runner", "rea", "logs", "gate"} @@ -667,11 +675,12 @@ def clear_large_outputs(name, trace): """After a pass, delete the large files a harness regenerates on every run. Harness outputs reach gigabytes and used to stay in every worktree. Vanilla - oracle outputs are kept, since reusing an oracle run needs them in place; - everything else large under the harness's .local directories is rebuilt on - its next run.""" + oracle outputs under KEPT_ORACLE_OUTPUT are kept, since reusing an oracle + run needs them in place; everything else large under the harness's .local + directories (`...` or `rust--parity`) is rebuilt on its next run.""" local = LOCAL.resolve() keep = {os.path.realpath(line) for line in outputs_listing(name).read_text(encoding="utf-8").splitlines() if line} + own = name.split("#")[0].replace("_", "-") directories = set() for command in trace["commands"]: for argument in command[1:]: @@ -683,12 +692,13 @@ def clear_large_outputs(name, trace): # Only the harness's own directory: another harness running beside it may be writing # large files in a directory this one merely read (decorated_region compiles its oracle # into chunk-generation-parity while chunk_generation writes its dumps there). - if top not in OWN_DIRECTORIES and top.startswith(name.replace("_", "-")): + if top not in OWN_DIRECTORIES and (top.startswith(own) or top == f"rust-{own}-parity"): directories.add(local / top) for directory in directories: for file in (directory.rglob("*") if directory.is_dir() else []): try: - if file.is_file() and not file.is_symlink() and file.stat().st_size >= LARGE_OUTPUT and str(file) not in keep: + size = file.stat().st_size if file.is_file() and not file.is_symlink() else 0 + if size >= LARGE_OUTPUT and (str(file) not in keep or size > KEPT_ORACLE_OUTPUT): file.unlink() except OSError: pass @@ -823,8 +833,11 @@ class LocalExecutor: environment.pop("PARITY_SHARD", None) if shard: environment["PARITY_SHARD"] = shard + # stdin from /dev/null: tools/parity_shim.py never reuses an oracle run that reads a pipe, and CI runners + # (and --worker's ssh) give the runner one, which kept oracle reuse off on CI. process = subprocess.Popen([sys.executable, str(Path(__file__)), "--trace", str(trace), str(script)], - cwd=ROOT, env=environment, stdout=log, stderr=subprocess.STDOUT, start_new_session=True) + cwd=ROOT, env=environment, stdin=subprocess.DEVNULL, stdout=log, + stderr=subprocess.STDOUT, start_new_session=True) self.running[name] = {"process": process, "started": time.monotonic(), "log": log, "expected": expected, "slot": slot, "script": script} return True @@ -1224,20 +1237,44 @@ def changed_harnesses(found, records, ref): return found_changed -def affected_harnesses(found, records, changed): +def affected_harnesses(found, records, changed, reach=True, base=None): """Harnesses the changed files can affect: their script or a recorded dependency (here, or in the shared - dependency lists) changed, a file every harness depends on changed, or nothing is recorded about them yet.""" + dependency lists) changed, a file every harness depends on changed, or nothing is recorded about them yet. + With reach, a harness's workspace Rust sources count only if its probes can reach them (tools/parity_reach.py); + a harness whose probes can't be placed keeps them all. A changed file counts unless, compared with base (a + commit), only declarations of modules the harness doesn't reach changed (tools/parity_reach.py).""" if changed & SHARED_INPUTS: return set(found) + graph = None + if reach and any(path.startswith("crates/") for path in changed): + cache_path = STATE_DIR / "reach-cache.json" + try: + cache = json.loads(cache_path.read_text(encoding="utf-8")) + except (OSError, ValueError): + cache = {} + graph = parity_reach.SourceGraph(ROOT, rust_code, cache) affected = set() for name, script in found.items(): - dependencies = records.get(name, {}).get("dependencies") + record = records.get(name) or {} + dependencies = record.get("dependencies") template = SHARED / "dependencies" / f"{name}.json" if dependencies is None and template.exists(): - dependencies = json.loads(template.read_text(encoding="utf-8")).get("dependencies") - if dependencies is None or Path(script).relative_to(ROOT).as_posix() in changed or \ - any(path.split(KEEP)[0] in changed for path in dependencies): + record = json.loads(template.read_text(encoding="utf-8")) + dependencies = record.get("dependencies") + if dependencies is None or Path(script).relative_to(ROOT).as_posix() in changed: affected.add(name) + continue + plain = {path.split(KEEP)[0] for path in dependencies} + if graph is not None and plain & changed: + narrowed = parity_reach.narrow(dependencies, recorded_artifacts(record), graph, changed) + if any(path.split(KEEP)[0] in changed and (children is None or parity_reach.declarations_changed( + ROOT, path.split(KEEP)[0], children, base, whole)) for path, (whole, children) in narrowed.items()): + affected.add(name) + elif plain & changed: + affected.add(name) + if graph is not None: + STATE_DIR.mkdir(parents=True, exist_ok=True) + cache_path.write_text(json.dumps(graph.saved_cache()), encoding="utf-8") return affected @@ -1280,6 +1317,8 @@ def main(): parser.add_argument("--flake-rounds", type=int, default=3, help="runs required by --flake-check (default 3)") parser.add_argument("--changed", metavar="REF", help="only harnesses the changes since the merge base with REF can " "affect (and that have not passed with their current inputs): the pull-request check") + parser.add_argument("--by-crate", action="store_true", help="with --changed: count every Rust source a probe " + "links, not only those it can reach (tools/parity_reach.py)") parser.add_argument("--changed-files", metavar="FILE", help="like --changed, for the changed paths listed in FILE, " "one per line: for a copy without Git history (tools/pr_ready.py on another machine)") args = parser.parse_args() @@ -1316,7 +1355,11 @@ def main(): changed, since = changed_files(args.changed), f" since the merge base with {args.changed}" if changed is None: parser.error(f"no merge base with {args.changed}") - affected = affected_harnesses(found, records, changed) + base = None + if args.changed: + merge_base = subprocess.run(["git", "merge-base", "HEAD", args.changed], cwd=ROOT, capture_output=True, text=True) + base = merge_base.stdout.strip() or None + affected = affected_harnesses(found, records, changed, reach=not args.by_crate, base=base) chosen = [name for name in found if name in affected and stale(name, state, hasher, environment)] print(f"{len(changed)} files changed{since}: {len(affected)} harnesses " f"depend on them, {len(chosen)} without a pass for their current inputs.", flush=True) diff --git a/tools/parity_main.py b/tools/parity_main.py new file mode 100644 index 000000000..ff319aed0 --- /dev/null +++ b/tools/parity_main.py @@ -0,0 +1,129 @@ +#!/usr/bin/env python3 +"""parity-main (.github/workflows/parity-main.yml): the vanilla harnesses run on main, a batch of merges at a time. + + python3 tools/parity_main.py base # the commit this run checks from: main's last green parity-main + python3 tools/parity_main.py bisect BASE # after a failure: the first merge since BASE that broke a harness + +`base` prints the newest commit on main whose parity-main run passed and that HEAD contains, else HEAD's first parent, +so a run tests every merge since the last green one (`tools/parity.py --changed `), however many landed while +the previous run went on. `bisect` reads the harnesses that failed (.local/parity-runner/last-run.json), checks out +the first-parent merges between BASE and HEAD by halves and runs only those harnesses, then names the first merge they +fail on (written to $GITHUB_STEP_SUMMARY and, with --comment, on its pull request). HEAD is checked out again at the end. +""" + +import argparse +import json +import os +from pathlib import Path +import re +import subprocess +import sys + +ROOT = Path(__file__).resolve().parents[1] +REPO = os.environ.get("GITHUB_REPOSITORY", "ludovic111/horizon") +LAST_RUN = ROOT / ".local" / "parity-runner" / "last-run.json" + + +def git(*arguments, check=True): + result = subprocess.run(["git", *arguments], cwd=ROOT, capture_output=True, text=True) + if check and result.returncode: + sys.exit(f"git {' '.join(arguments)}: {result.stderr.strip()}") + return result + + +def green_runs(): + """Head commits of main's passing parity-main runs, newest first.""" + result = subprocess.run(["gh", "run", "list", "--repo", REPO, "--workflow", "parity-main", "--branch", "main", + "--status", "success", "--limit", "30", "--json", "headSha"], capture_output=True, text=True) + if result.returncode: + print(f"note: no parity-main history ({result.stderr.strip()})", file=sys.stderr) + return [] + return [run["headSha"] for run in json.loads(result.stdout or "[]")] + + +def base(head="HEAD", runs=None): + for sha in green_runs() if runs is None else runs: + if git("merge-base", "--is-ancestor", sha, head, check=False).returncode == 0: + return git("rev-parse", sha).stdout.strip() + return git("rev-parse", f"{head}^1").stdout.strip() + + +def first_bad(commits, fails): + """The first of commits (oldest first, the last known to fail; the one before them known to pass) where fails().""" + good, bad = -1, len(commits) - 1 + while bad - good > 1: + middle = (good + bad) // 2 + if fails(commits[middle]): + bad = middle + else: + good = middle + return commits[bad] + + +def harness_exists(name): + return (ROOT / "tools" / f"test_{name.split('#')[0]}_parity.py").exists() + + +def run_harnesses(names, timeout): + """Whether any of the harnesses fails at the checked-out commit (those it doesn't have yet can't).""" + present = [name for name in names if harness_exists(name)] + if not present: + return False + return subprocess.run([sys.executable, "tools/parity.py", *present, "--timeout", str(timeout)], cwd=ROOT).returncode != 0 + + +def bisect(since, timeout, comment): + failed = [] + if LAST_RUN.exists(): + failed = json.loads(LAST_RUN.read_text(encoding="utf-8")).get("failed") or [] + if not failed: + print("No failed harness recorded: nothing to bisect.") + return 0 + head = git("rev-parse", "HEAD").stdout.strip() + commits = git("rev-list", "--first-parent", "--reverse", f"{since}..{head}").stdout.split() + if not commits: + print("No merge since the base.") + return 0 + + def fails(commit): + print(f"--- {commit[:9]} {git('log', '-1', '--format=%s', commit).stdout.strip()}", flush=True) + git("checkout", "-q", "--detach", commit) + return run_harnesses(failed, timeout) + + try: + culprit = first_bad(commits, fails) if len(commits) > 1 else commits[0] + finally: + git("checkout", "-q", "--detach", head) + subject = git("log", "-1", "--format=%s", culprit).stdout.strip() + number = re.search(r"Merge pull request #(\d+)", subject) + line = (f"parity-main: {', '.join(failed)} first fail at {culprit[:9]} ({subject}), " + f"of {len(commits)} merges since {since[:9]}.") + print(line) + if os.environ.get("GITHUB_STEP_SUMMARY"): + with open(os.environ["GITHUB_STEP_SUMMARY"], "a", encoding="utf-8") as summary: + summary.write(f"### {line}\n") + if comment and number: + url = f"{os.environ.get('GITHUB_SERVER_URL', 'https://github.com')}/{REPO}/actions/runs/{os.environ.get('GITHUB_RUN_ID', '')}" + body = (f"🤖 Agent: parity-main found that this merge breaks {', '.join(f'`{name}`' for name in failed)} on main " + f"(bisected over {len(commits)} merges: {url}). Releases wait until parity-main passes again.") + subprocess.run(["gh", "pr", "comment", number.group(1), "--repo", REPO, "--body", body]) + return 0 + + +def main(): + parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + commands = parser.add_subparsers(dest="command", required=True) + commands.add_parser("base") + bisecting = commands.add_parser("bisect") + bisecting.add_argument("since", help="the last commit known to pass (the run's base)") + bisecting.add_argument("--timeout", type=int, default=2400, help="seconds per harness") + bisecting.add_argument("--comment", action="store_true", help="comment on the breaking merge's pull request") + args = parser.parse_args() + if args.command == "base": + print(base()) + return 0 + return bisect(args.since, args.timeout, args.comment) + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tools/parity_reach.py b/tools/parity_reach.py new file mode 100644 index 000000000..0c3051b86 --- /dev/null +++ b/tools/parity_reach.py @@ -0,0 +1,768 @@ +"""Which of a harness's Rust sources its probes can reach, for `parity.py --changed`. + +Cargo's dep-info names every source file of every crate a probe links, so a change anywhere in horizon-client +selected every harness whose probe links horizon-client. This module narrows a harness's recorded dependencies to +the files holding an item its entry points (the probe modules it ran, the binaries it built) can reach, over a graph +of the workspace's current sources whose nodes are items: + +- everything in an entry is reached, and so is everything in a Rust file the module tree doesn't place (an + `include!`d source); +- a module is open when it is a crate root, holds reached code, or is named in a path (`name::`, `use ...name`) by + reached code while its parent is open; +- a free item (fn, struct, enum, union, trait, type, const, static, an item-level macro call's items) is reached when + reached code in its own module or below it mentions its name, or when its module is open and reached code names + it in a path or an import, or when its module is glob-imported (`use m::*`) and its name is mentioned; +- an inherent impl's methods and constants are reached one by one, by name, once their type is; a trait impl once + its trait and its type are (a std or outside name always is, a generic parameter needs nothing); a macro once + invoked; a `pub use` like a free item named by everything it imports; +- "a crate's reached code" covers the crates it names (`horizon_world::`), directly or through one that does, and + code under `#[cfg(test)]` or `#[test]` is left out (probes are never test builds). + +Names match by spelling, so a file can be kept for a name it never meant: the graph only over-approximates. A +reached file's parent modules count through their `mod` declarations of it (parity.py compares those with the merge +base). Everything that isn't a workspace Rust source stays a dependency, except a crate's non-Rust files (shaders, +assets, fonts) that only unreached sources name; a dependency that no longer exists counts as changed, and Rust files +a change adds take part. +""" + +import hashlib +from pathlib import Path, PurePosixPath +import re +import subprocess + +IDENT = r"[A-Za-z_]\w*" +WORD = re.compile(rf"\b({IDENT})\b") +MOD_DECLARATION = re.compile(rf"\bmod\s+({IDENT})\s*;") +INLINE_MOD = re.compile(rf"\bmod\s+({IDENT})\s*\{{") +PATH_ATTRIBUTE = re.compile(r'#\[\s*path\s*=\s*"([^"]+)"\s*\]\s*(?:#\[[^\]]*\]\s*)*(?:pub(?:\([^)]*\))?\s+)?mod\s+\w+\s*;$') +BRACE = re.compile(r"[{}]") +TEST_ONLY = re.compile(r"#\[\s*(?:cfg\s*\(\s*test\s*\)|test)\s*\]") +TEST_MODULE = re.compile(r"#\[\s*cfg\s*\(\s*test\s*\)\s*\]\s*(?:#\[[^\]]*\]\s*)*(?:pub(?:\([^)]*\))?\s+)?mod\s+\w+\s*;$") +SPLIT = re.compile(r"[{}()\[\];]") +ITEM = re.compile(rf"(?:pub(?:\s*\([^)]*\))?\s+)?(?:(?:unsafe|async|const|extern|default|auto)\s+)*" + rf"(fn|struct|enum|union|trait|type|const|static|impl|mod|use|macro_rules\s*!|extern\s+crate)" + rf"(?!\w)\s*(?:mut\s+)?({IDENT})?") +PATH_BEFORE = re.compile(rf"\b({IDENT})\s*::") +PATH_AFTER = re.compile(rf"::\s*({IDENT})") +MACRO_USE = re.compile(rf"\b({IDENT})\s*!\s*[(\[{{]") +MACRO_CALL = re.compile(rf"({IDENT}(?:\s*::\s*{IDENT})*)\s*!") +DEFINED_INSIDE = re.compile(rf"\b(?:fn|struct|enum|union|trait|type|const|static)\s+({IDENT})") +NESTED_IMPL = re.compile(r"^[ \t]*(?:unsafe\s+)?impl\b([^{;]*)\{", re.MULTILINE) +QUALIFIED = re.compile(rf"{IDENT}(?:\s*::\s*{IDENT})*") +GLOB_IMPORT = re.compile(r"\buse\s[^;]*::\s*\*") +# File names a source may name in a literal (include_str!, include_bytes!, a path it reads). A file of another kind +# is never "named", so it always counts. +FILE_NAME = re.compile(r"[\w-]+\.(?:wgsl|glsl|json|png|ttf|otf|svg|txt|toml|ogg|wav|nbt|bin|dat|csv|mcmeta|wit|wasm)\b") +TYPE_KINDS = {"struct", "enum", "union", "trait", "type", "macro_call"} +# Names an impl header can take from outside the workspace: std's prelude and primitive types, and the std and serde +# traits a glob import may bring. A glob-imported name outside this list is taken to be the workspace's. +STD_NAMES = {"Display", "Error", "Read", "Write", "Seek", "BufRead", "Deref", "DerefMut", "Index", "IndexMut", "Add", + "Sub", "Mul", "Div", "Rem", "Neg", "Not", "BitAnd", "BitOr", "BitXor", "Shl", "Shr", "AddAssign", + "SubAssign", "MulAssign", "DivAssign", "RemAssign", "BitAndAssign", "BitOrAssign", "BitXorAssign", + "Borrow", "BorrowMut", "Future", "Any", "FromStr", "Hasher", "BuildHasher", "Formatter", "Ordering", + "Serialize", "Deserialize", "Serializer", "Deserializer", "Visitor", "Termination", "Sum", "Product", + "Wake", "Cow", "Rc", "Arc", "Mutex", "RwLock", "Cell", "RefCell", "HashMap", "HashSet", "BTreeMap", + "BTreeSet", "VecDeque"} +PRELUDE = {"Option", "Some", "None", "Result", "Ok", "Err", "Vec", "String", "Box", "ToString", "ToOwned", "Clone", + "Copy", "Send", "Sync", "Sized", "Unpin", "Drop", "Fn", "FnMut", "FnOnce", "From", "Into", "TryFrom", + "TryInto", "AsRef", "AsMut", "Default", "Eq", "PartialEq", "Ord", "PartialOrd", "Iterator", "Extend", + "IntoIterator", "DoubleEndedIterator", "ExactSizeIterator", "FromIterator", "Debug", "Hash", + "bool", "char", "str", "u8", "u16", "u32", "u64", "u128", "usize", "i8", "i16", "i32", "i64", "i128", + "isize", "f32", "f64"} +STD_NAMES |= PRELUDE +KEYWORDS = {"crate", "self", "super", "Self", "pub", "in", "as", "use", "for", "where", "dyn", "mut", "const", "fn", + "unsafe", "impl", "std", "core", "alloc", "let", "if", "else", "match", "return", "struct", "enum", "mod", + "type", "static", "trait", "true", "false", "loop", "while", "move", "ref", "break", "continue", "async", + "await", "extern", "union"} + + +def segments(code, start, end): + """The item-level pieces of code[start:end]: each ends at a `;` or a `}` outside any bracket.""" + found, begin, depth = [], start, 0 + for match in SPLIT.finditer(code, start, end): + character = match.group() + if character in "{([": + depth += 1 + elif character in "})]": + depth -= 1 + if character == "}" and depth == 0: + found.append((begin, match.end())) + begin = match.end() + elif depth == 0: + found.append((begin, match.end())) + begin = match.end() + if code[begin:end].strip(): + found.append((begin, end)) + return found + + +def skip_attributes(code, index, end): + """The offset of the first non-space character after the attributes (`#[...]`, `#![...]`) starting at index.""" + while True: + while index < end and code[index].isspace(): + index += 1 + if not code.startswith("#", index) or not re.match(r"#!?\[", code[index:index + 3]): + return index + depth = 0 + for position in range(code.index("[", index), end): + depth += {"[": 1, "]": -1}.get(code[position], 0) + if depth == 0: + index = position + 1 + break + else: + return end + + +def body_of(code, start, end): + """The range inside an item's outermost `{ }` block, or None.""" + opening = code.find("{", start, end) + closing = code.rfind("}", start, end) + return (opening + 1, closing) if 0 <= opening < closing else None + + +def words(text): + return set(WORD.findall(text)) - KEYWORDS + + +def qualified_words(text): + """Names a piece of code reaches through a path (`a::b`: b), the way items of other modules are named.""" + return set(PATH_AFTER.findall(text)) - KEYWORDS + + +def path_words(text): + """Names a piece of code uses as path segments (`a::`, `::b`), where module names appear.""" + return set(PATH_BEFORE.findall(text)) | set(PATH_AFTER.findall(text)) + + +def parse_items(code, start, end, items): + """Append the items of code[start:end] (a file or an inline module's body) to items, as dicts.""" + for begin, finish in segments(code, start, end): + head = skip_attributes(code, begin, finish) + text = code[head:finish] + if not text.strip(" \n\t;") or TEST_ONLY.search(code[begin:head]): + continue # compiled into tests only, never into a probe + match = ITEM.match(text) + kind = re.sub(r"\s+", "", match.group(1)) if match else None + name = match.group(2) if match else None + if kind == "mod": + body = body_of(code, head, finish) + if body: + parse_items(code, body[0], body[1], items) + continue + if kind in ("externcrate", None) and not MACRO_CALL.match(text): + if kind is None: + items.append({"kind": "glue", "mentions": sorted(words(text)), "paths": sorted(path_words(text)), "qualified": sorted(qualified_words(text)), "invokes": sorted(set(MACRO_USE.findall(text)))}) + continue + if kind is None: # an item-level macro call: it may define items + items.append({"kind": "macro_call", "names": sorted(set(DEFINED_INSIDE.findall(text))), + "macro": MACRO_CALL.match(text).group(1).split("::")[-1].strip(), + "mentions": sorted(words(text)), "paths": sorted(path_words(text)), "qualified": sorted(qualified_words(text)), "invokes": sorted(set(MACRO_USE.findall(text)))}) + elif kind == "use": + head_word = re.match(rf"\s*(?:::\s*)?({IDENT})", text[match.end(1):]) + items.append({"kind": "use", "public": text.startswith("pub"), "names": sorted(words(text)), + "head": head_word.group(1) if head_word else "", "glob": bool(re.search(r"::\s*\*", text)), + "mentions": sorted(words(text)), "paths": sorted(words(text)), "qualified": sorted(words(text)), + "invokes": []}) + elif kind == "impl": + body = body_of(code, head, finish) + subject = code[head + match.end(1) - match.start(): body[0] - 1 if body else finish] + inherent = not re.search(r"\bfor\b", impl_subject(subject)[1]) + if inherent and body: + members = [] + for member_begin, member_end in segments(code, body[0], body[1]): + member = code[skip_attributes(code, member_begin, member_end):member_end] + found = ITEM.match(member) + members.append({"name": found.group(2) if found else None, "mentions": sorted(words(member)), + "paths": sorted(path_words(member)), "qualified": sorted(qualified_words(member)), "invokes": sorted(set(MACRO_USE.findall(member)))}) + items.append({"kind": "impl", "subject": subject, "inherent": True, "members": members, + "mentions": sorted(words(subject)), "paths": sorted(path_words(subject)), "qualified": sorted(qualified_words(subject)), "invokes": sorted(set(MACRO_USE.findall(subject)))}) + else: + items.append({"kind": "impl", "subject": subject, "inherent": False, "mentions": sorted(words(text)), + "paths": sorted(path_words(text)), "qualified": sorted(qualified_words(text)), "invokes": sorted(set(MACRO_USE.findall(text)))}) + else: + kind = "macro" if kind.startswith("macro_rules") else kind + items.append({"kind": kind, "names": [name] if name else [], "mentions": sorted(words(text)), + "defines": sorted(set(DEFINED_INSIDE.findall(text))) if kind == "macro" else [], + "paths": sorted(path_words(text)), "qualified": sorted(qualified_words(text)), "invokes": sorted(set(MACRO_USE.findall(text)))}) + if kind in ("fn", "const", "static"): + # An impl inside a function body still holds for the whole program. + for header in NESTED_IMPL.findall(text): + items.append({"kind": "impl", "subject": header, "inherent": False, + "mentions": sorted(words(text)), "paths": sorted(path_words(text)), "qualified": sorted(qualified_words(text)), "invokes": sorted(set(MACRO_USE.findall(text)))}) + + +def crate_roots(crate_dir): + """A crate's root files: its library, binaries and examples.""" + roots = [crate_dir / "src" / "lib.rs", crate_dir / "src" / "main.rs"] + for folder in (crate_dir / "src" / "bin", crate_dir / "examples"): + if folder.is_dir(): + roots += sorted(folder.glob("*.rs")) + sorted(folder.glob("*/main.rs")) + return [root for root in roots if root.is_file()] + + +class SourceGraph: + """The workspace's Rust sources, parsed once: what each file names, defines and declares.""" + + def __init__(self, root, rust_code, cache=None): + """cache: a dict of earlier parses by content digest, which the graph reuses and adds to (parity.py keeps it + in .local/parity-runner/reach-cache.json).""" + self.root = Path(root) + self.rust_code = rust_code + self.cache = {} if cache is None else cache + self.used = set() + self.files = {} + self.parent = {} # module file -> the file declaring it + self.module_name = {} + self.roots = set() + self.target_cache = {} + self.workspace_heads = None + self.test_only = set() # modules compiled into tests only + self.tables = None + self.root_of = {} # source -> the root of its target (lib.rs, a bin's or an example's main file) + self.results = {} # (entries, universe) -> reachable files + for manifest in sorted((self.root / "crates").glob("*/Cargo.toml")): + for crate_root in crate_roots(manifest.parent): + path = self.relative(crate_root) + self.roots.add(path) + self.root_of[path] = path + self.place(path, crate_root.parent, set()) + + def entries(self, artifacts, rust): + """The root files of the probes and binaries a harness built (its record's artifacts), among its Rust + dependencies: for a crate's probe binary, the probe modules it ran (parity.only_probes_run kept only those). + None when an artifact can't be placed.""" + found = set() + for kind, name, *rest in artifacts: + selection = rest[1] if len(rest) > 1 else [] + packages = [value for flag, value in zip(selection, selection[1:]) if flag == "-p"] or \ + [manifest.parent.name for manifest in sorted((self.root / "crates").glob("*/Cargo.toml"))] + roots = set() + for package in packages: + crate = self.root / "crates" / package + targets = self.targets(crate) + if kind in ("example", "examples"): + candidates = [path for target, (target_kind, path) in targets.items() + if target_kind == "example" and (not name or target == name)] + else: + candidates = [path for target, (target_kind, path) in targets.items() + if target_kind == "bin" and (not name or target == name)] + for path in candidates: + if path.endswith("/examples/probe/main.rs"): + # The probes it ran, and the dispatch and support modules they use (only_probes_run kept + # those); the binary's root, which only declares them, so their own submodules are placed. + folder = path.removesuffix("main.rs") + roots |= {dependency for dependency in rust if dependency.startswith(folder) + and "/" not in dependency.removeprefix(folder).removeprefix("support/")} + if roots: + roots.add(path) + elif path in rust: + roots.add(path) + if not roots: + return None + found |= roots + return found or None + + def targets(self, crate): + """A crate's bins and examples: {target name: (kind, root file)}, from its Cargo.toml and Cargo's defaults.""" + if crate in self.target_cache: + return self.target_cache[crate] + found = {} + manifest = (crate / "Cargo.toml").read_text(encoding="utf-8") if (crate / "Cargo.toml").is_file() else "" + package = re.search(r'^\[package\][^\[]*?^name\s*=\s*"([^"]+)"', manifest, re.MULTILINE | re.DOTALL) + if (crate / "src" / "main.rs").is_file() and package: + found[package.group(1)] = ("bin", self.relative(crate / "src" / "main.rs")) + for kind, folder in (("bin", crate / "src" / "bin"), ("example", crate / "examples")): + for path in sorted(folder.glob("*.rs")) + sorted(folder.glob("*/main.rs")): + found[path.stem if path.name != "main.rs" else path.parent.name] = (kind, self.relative(path)) + for kind, table in re.findall(r"^\[\[(bin|example)\]\]([^\[]*)", manifest, re.MULTILINE): + name = re.search(r'^name\s*=\s*"([^"]+)"', table, re.MULTILINE) + path = re.search(r'^path\s*=\s*"([^"]+)"', table, re.MULTILINE) + if name and path and (crate / path.group(1)).is_file(): + found[name.group(1)] = (kind, self.relative(crate / path.group(1))) + self.target_cache[crate] = found + return found + + def target(self, path): + """The Cargo target a source belongs to: the root file of the library, bin or example whose module tree + holds it (a file nothing places is its own).""" + return self.root_of.get(path, path) + + def is_library(self, path): + return self.target(path).endswith("/src/lib.rs") + + def relative(self, path): + return Path(path).resolve().relative_to(self.root.resolve()).as_posix() + + def place(self, path, module_dir, seen): + """Walk the module tree from a file: record each declared module's file, parent and name.""" + if path in seen: + return + seen.add(path) + parsed = self.parse(path) + if parsed is None: + return + for name, nesting, attribute, test in parsed["declarations"]: + if attribute is not None: + if nesting: + continue # a #[path] inside an inline module: unplaced, so always kept + candidates = [(self.root / path).parent / attribute] + else: + folder = module_dir.joinpath(*nesting) + candidates = [folder / f"{name}.rs", folder / name / "mod.rs"] + for candidate in candidates: + if candidate.is_file(): + child = self.relative(candidate) + self.parent.setdefault(child, path) + self.module_name.setdefault(child, name) + self.root_of.setdefault(child, self.root_of[path]) + if test or path in self.test_only: + self.test_only.add(child) + child_dir = candidate.parent if candidate.name == "mod.rs" or attribute is not None \ + else candidate.parent / candidate.stem + self.place(child, child_dir, seen) + break + + def parse(self, path): + if path in self.files: + return self.files[path] + try: + data = (self.root / path).read_bytes() + except OSError: + self.files[path] = None + return None + digest = hashlib.sha256(data).hexdigest() + self.used.add(digest) + if digest in self.cache: + self.files[path] = parsed = self.cache[digest] + return parsed + raw = data.decode("utf-8", errors="replace") + code = self.rust_code(raw) # same length as raw: literals and comments blanked + opening = {match.end() - 1: match.group(1) for match in INLINE_MOD.finditer(code)} + events = sorted([(match.start(), match) for match in BRACE.finditer(code)] + + [(match.start(), match) for match in MOD_DECLARATION.finditer(code)], key=lambda event: event[0]) + declarations, stack, depth = [], [], 0 + for index, match in events: + if match.group() == "{": + depth += 1 + if index in opening: + stack.append((opening[index], depth)) + elif match.group() == "}": + if stack and stack[-1][1] == depth: + stack.pop() + depth -= 1 + else: + window = " ".join(raw[max(0, index - 300):match.end()].split()) + attribute = PATH_ATTRIBUTE.search(window) + declarations.append([match.group(1), [name for name, _ in stack], attribute.group(1) if attribute else None, + bool(TEST_MODULE.search(window))]) + items = [] + parse_items(code, 0, len(code), items) + self.files[path] = parsed = { + "declarations": declarations, + "items": items, + "glob": bool(GLOB_IMPORT.search(code)), + "file_names": sorted(set(FILE_NAME.findall(raw))), + } + self.cache[digest] = parsed + return parsed + + def saved_cache(self): + """The parses of the files this graph read, to keep for the next run.""" + return {digest: self.cache[digest] for digest in self.used if digest in self.cache} + + def requirement(self, path, item): + """For an impl: for its trait and for its type, [names the file defines, other workspace names], one of + which must be a reached type or trait (of this file, of a crate it sees) before the impl can matter; both + empty: nothing of the workspace is needed. A name is the workspace's when the path naming it starts in the + workspace, or the file defines it, imports it from the workspace or may glob-import it; otherwise (std's, + another crate's, the prelude's) it needs nothing.""" + if "requires" in item: + return item["requires"] + if self.workspace_heads is None: + self.workspace_heads = {"crate", "self", "super"} | set(self.module_name.values()) | { + manifest.parent.name.replace("-", "_") for manifest in (self.root / "crates").glob("*/Cargo.toml")} + parsed = self.parse(path) + defined = {name for other in parsed["items"] for name in other.get("names", ()) if other["kind"] != "use"} + workspace_imports = {name for other in parsed["items"] if other["kind"] == "use" + and other["head"] in self.workspace_heads for name in other["names"]} + parameters, subject = impl_subject(item["subject"]) + parts = [] + for part in re.split(r"\bfor\b", subject, maxsplit=1): + names, own = set(), set() + for match in QUALIFIED.finditer(part): + segments_ = [segment.strip() for segment in match.group().split("::")] + name = segments_[-1] + if name in parameters or name in KEYWORDS: + continue + if len(segments_) > 1: + if segments_[0] in self.workspace_heads: + names.add(name) + elif name in defined: + own.add(name) + elif name in workspace_imports or (parsed["glob"] and name not in STD_NAMES): + names.add(name) + parts.append([sorted(own), sorted(names)]) + item["requires"] = parts + return parts + + def index(self): + """Lookup tables over every placed source, built once: which crates each crate sees, the nodes (items, an + inherent impl's members) by the names they define and the types they need, the modules by name.""" + if self.tables is not None: + return self.tables + idents = {manifest.parent.name.replace("-", "_"): manifest.parent.name + for manifest in (self.root / "crates").glob("*/Cargo.toml")} + paths = [path for path in self.files if self.files[path] is not None and path not in self.test_only] + sees = {} + for path in paths: + named = {word for item in self.files[path]["items"] for word in item["mentions"]} & idents.keys() + sees.setdefault(crate_of(path), {crate_of(path)}).update(idents[name] for name in named) + changed = True + while changed: + changed = False + for crate, seen in sees.items(): + wider = set().union(*(sees.get(other, {other}) for other in seen)) + if not wider <= seen: + seen |= wider + changed = True + seen_by = {} # crate -> the crates that see it + for crate, seen in sees.items(): + for other in seen: + seen_by.setdefault(other, set()).add(crate) + # What a call of a workspace macro defines: what its definition's body defines. + expands = {} + for path in paths: + for item in self.files[path]["items"]: + if item["kind"] == "macro": + for name in item["names"]: + expands.setdefault(name, set()).update(item["defines"]) + names_of, by_name, by_file_name, by_type, by_module, children, seeds = {}, {}, {}, {}, {}, {}, [] + for path in paths: + crate = crate_of(path) + if path in self.module_name: + by_module.setdefault((crate, self.module_name[path]), []).append(path) + children.setdefault(self.parent[path], []).append(path) + for index, item in enumerate(self.files[path]["items"]): + node = (path, index, None) + names = set(item.get("names", ())) + if item["kind"] == "macro_call": + names |= expands.get(item["macro"], set()) + names_of[node] = names + if item["kind"] == "glue": + seeds.append(node) + elif item["kind"] == "macro_call" and not names: + # A macro call defining nothing by name may still expand to impls: it counts once a type or + # trait it mentions is reached. + for name in item["mentions"]: + by_type.setdefault(name, []).append(node) + elif item["kind"] == "impl": + required = {name for own, foreign in self.requirement(path, item) for name in own + foreign} + members = [(path, index, member) for member in range(len(item["members"]))] if item["inherent"] else [] + for member_node in members: + name = item["members"][member_node[2]]["name"] + names_of[member_node] = set() + if name: + by_name.setdefault(name, []).append(member_node) + for name in required: + for other in [node] + members: + by_type.setdefault(name, []).append(other) + if not required: + seeds.append(node) + seeds += [member_node for member_node in members if not item["members"][member_node[2]]["name"]] + else: + for name in names: + by_name.setdefault(name, []).append(node) + by_file_name.setdefault((path, name), []).append(node) + self.tables = dict(idents=idents, sees=sees, seen_by=seen_by, names_of=names_of, by_name=by_name, + by_file_name=by_file_name, by_type=by_type, by_module=by_module, children=children, + seeds=seeds, node_sets={}) + return self.tables + + def reachable(self, entries, universe, reasons=None): + """The files of universe (workspace Rust sources) holding an item reachable from entries (by the rules in + this module's doc), and the entries. reasons, a dict, gets why each file was reached (for debugging).""" + # Searching every source of the crates involved (not only those Cargo listed) only adds reach, and lets + # harnesses with the same entries (the game binary's) share one search. + crates = frozenset(crate_of(path) for path in universe) + key = (frozenset(entries), crates) + if reasons is None and key in self.results: + return self.results[key] & set(universe) + everything = {path for path in self.files if crate_of(path) in crates} | set(universe) + files = self.search(entries, everything, reasons) + self.results[key] = files + return files & set(universe) + + def search(self, entries, universe, reasons): + targets = {self.target(path) for path in entries} + universe = {path for path in universe if (self.is_library(path) or self.target(path) in targets) + and path not in self.test_only} + tables = self.index() + idents, sees, seen_by, names_of = tables["idents"], tables["sees"], tables["seen_by"], tables["names_of"] + node_sets = tables["node_sets"] + by_name, by_file_name, by_type = tables["by_name"], tables["by_file_name"], tables["by_type"] + by_module, children = tables["by_module"], tables["children"] + seeds = [node for node in tables["seeds"] if node[0] in universe] + for path in universe: + if (path in entries or (path not in self.parent and path not in self.roots)) and self.parse(path): + seeds += [(path, index, None) for index in range(len(self.parse(path)["items"]))] + + def names(node): + return names_of.get(node, set()) + + def sets_of(node, item): + """A node's mentions, path words, qualified names and macro calls, as sets (parses keep lists).""" + if node not in node_sets: + source = item["members"][node[2]] if node[2] is not None else item + node_sets[node] = {key: set(source.get(key, ())) for key in ("mentions", "paths", "qualified", "invokes")} + return node_sets[node] + + def item_of(node): + return self.parse(node[0])["items"][node[1]] + + reached, opened, globbed, files, queue = set(), set(), set(), set(), [] + # name -> the crates whose reached code mentions it plainly, names it in a path or an import, invokes it. + mentioned, qualified, invoked = {}, {}, {} + done = {"mentions": {}, "qualified": {}, "invokes": {}, "paths": {}} # crate -> names already handled + path_names, types = {}, {} # by crate + local = {} # file -> plain mentions by reached code in it or below it (child modules may `use super::*`) + local_types = {} # file -> its reached types and traits + + def heard(table, name, crate): + """Whether a crate that can see crate's items has name in table.""" + sources = table.get(name) + return bool(sources) and not sources.isdisjoint(seen_by.get(crate, {crate})) + + def ready(node): + path, index, member = node + item = item_of(node) + crate = crate_of(path) + if item["kind"] == "impl": + visible = visible_types(crate) + here = local_types.get(path, set()) + if not all((not own and not foreign) or here.intersection(own) or visible.intersection(foreign) + for own, foreign in self.requirement(path, item)): + return False + name = item["members"][member]["name"] if member is not None else None + return name is None or heard(mentioned, name, crate) # a method: called by its name + if item["kind"] == "macro_call" and not names(node): + return not visible_types(crate).isdisjoint(item["mentions"]) + names_ = names(node) + if item["kind"] == "macro": + return any(heard(invoked, name, crate) for name in names_) + if item["kind"] == "use" and item["glob"]: + return path in opened + nearby = local.get(path, ()) + if any(name in nearby for name in names_): + return True # named in its own module, or below it + if item["kind"] == "use" and not item["public"]: + return False + if path in opened and any(heard(qualified, name, crate) for name in names_): + return True # named through a path, or imported + return path in globbed and any(heard(mentioned, name, crate) for name in names_) + + visible_cache = {} + + def visible_types(crate): + """The reached types and traits of the crates a crate sees (cached until a type is reached).""" + if crate not in visible_cache: + visible_cache[crate] = set().union(*(types.get(other, set()) for other in sees.get(crate, {crate}))) + return visible_cache[crate] + + def wake(nodes, why): + for other in nodes: + if other not in reached and other[0] in universe and ready(other): + reach(other, why) + + def open_module(path, why): + pending = [path] + while pending: + current = pending.pop() + if current in opened: + continue + opened.add(current) + crate = crate_of(current) + pending += [child for child in children.get(current, ()) + if child in universe and self.module_name[child] in path_names.get(crate, ())] + parsed = self.parse(current) + wake([(current, index, None) for index, item in enumerate(parsed["items"] if parsed else ()) + if item["kind"] != "impl"], f"{current} opened ({why})") + + def reach(node, why): + if node in reached: + return + reached.add(node) + queue.append(node) + path, _, member = node + item = item_of(node) + if (item["kind"] == "use" and not item["public"]) or (item["kind"] == "impl" and item["inherent"] and member is None): + return + if path not in files: + files.add(path) + if reasons is not None: + reasons[path] = why + + for path in universe: + if path.endswith("/src/lib.rs") or path in entries or path not in self.parent: + open_module(path, "crate root or entry") + for node in seeds: + reach(node, "entry" if node[0] in entries else "always counted") + for path in entries & universe: + files.add(path) + if reasons is not None: + reasons.setdefault(path, "entry") + while queue: + node = queue.pop() + path, index, member = node + item = item_of(node) + crate = crate_of(path) + label = (item.get("names") or [item["kind"]])[0] if member is None else item["members"][member]["name"] + source = sets_of(node, item) + why = f"{path} ({label})" + # Plain mentions reach items of this file and of the modules above it. + ancestor, new = path, source["mentions"] + while ancestor is not None and new: + known = local.setdefault(ancestor, set()) + new = new - known + known |= new + for name in new: + wake(by_file_name.get((ancestor, name), ()), f"{why} mentions {name}") + ancestor = self.parent.get(ancestor) + if item["kind"] == "use" and item["glob"]: + for name in item["names"]: + for target in sees.get(crate, {crate}): + for module in list(by_module.get((target, name), ())) + \ + ([f"crates/{idents[name]}/src/lib.rs"] if name in idents else []): + if module not in globbed and module in universe: + globbed.add(module) + wake([(module, index, None) for index in range(len(self.parse(module)["items"]))] + if self.parse(module) else [], f"{why} glob-imports {name}") + visible_to = sees.get(crate, {crate}) + new = source["paths"] - done["paths"].setdefault(crate, set()) + done["paths"][crate] |= new + for name in new: + for target in visible_to: + path_names.setdefault(target, set()).add(name) + for module in by_module.get((target, name), ()): + if module in universe and self.parent[module] in opened: + open_module(module, f"{why} names {name}") + for key, table, wanted in (("qualified", qualified, lambda other: True), + ("invokes", invoked, lambda other: item_of(other)["kind"] == "macro"), + ("mentions", mentioned, lambda other: other[2] is not None or other[0] in globbed)): + new = source[key] - done[key].setdefault(crate, set()) + done[key][crate] |= new + for name in new: + table.setdefault(name, set()).add(crate) + wake([other for other in by_name.get(name, ()) if crate_of(other[0]) in visible_to and wanted(other)], + f"{why} {key} {name}") + if item["kind"] in TYPE_KINDS and member is None: + own = types.setdefault(crate, set()) + local_types.setdefault(path, set()).update(names(node)) + if not names(node) <= own: + own |= names(node) + visible_cache.clear() + seeing = seen_by.get(crate, {crate}) + for name in names(node): # also when the crate has one by that name already: impls of this one + wake([other for other in by_type.get(name, ()) if crate_of(other[0]) in seeing], + f"implements for {name}, defined in {path}") + return files + + +def impl_subject(header): + """An impl header (the text after `impl`): its generic parameters' names, and the rest without its where clause + (the trait and the type it is for).""" + header, parameters = header.strip(), set() + if header.startswith("<"): + depth = 0 + for index, character in enumerate(header): + depth += {"<": 1, ">": -1}.get(character, 0) + if depth == 0: + generics, header = header[1:index], header[index + 1:] + parameters = {match.group(1) for match in re.finditer(rf"(?:^|,)\s*(?:const\s+)?({IDENT})", generics)} + break + return parameters, re.split(r"\bwhere\b", header)[0] + + +def crate_of(path): + return path.split("/", 2)[1] + + +def is_workspace_rust(path): + parts = PurePosixPath(path).parts + return len(parts) > 2 and parts[0] == "crates" and path.endswith(".rs") + + +DECLARATION_LINE = re.compile(rf"\s*(?:pub(?:\([^)]*\))?\s+)?mod\s+({IDENT})\s*;") + + +def declarations(text, names, keep_rest): + """A Rust file's `mod` declarations of names, each with the attribute lines just above it; with keep_rest, the + whole text except the declarations of other modules instead.""" + lines, found, attributes = text.splitlines(), [], [] + for line in lines: + if line.strip().startswith("#[") and not line.strip().startswith("#!["): + attributes.append(line.strip()) + continue + match = DECLARATION_LINE.match(line) + if match and match.group(1) in names: + found += attributes + [line.strip()] + elif keep_rest and not match: + found += attributes + [line] + attributes = [] + return "\n".join(found + attributes) + + +def declarations_changed(root, path, children, base, whole): + """Whether a changed file differs from the commit base in what a harness depends on: for a module parent, its + `mod` declarations of the reached children; for a whole file (whole), everything but its declarations of other + modules (adding a module beside reached ones changes nothing). True without a base, or if either side is + missing.""" + if base is None: + return True + try: + head = (Path(root) / path).read_text(encoding="utf-8", errors="replace") + before = subprocess.run(["git", "show", f"{base}:{path}"], cwd=root, capture_output=True, text=True, check=True).stdout + except (OSError, subprocess.CalledProcessError): + return True + return declarations(head, children, whole) != declarations(before, children, whole) + + +def narrow(dependencies, artifacts, graph, changed=()): + """A harness's dependencies narrowed to what its entry points can reach: {dependency: (whole, children)}, where + whole says the file counts for its own content (else only through its `mod` declarations of children, as a + module ancestor of a reached file) and children are its reached child modules. Without known entry points (a + probe or binary the harness built that isn't found here), every dependency is kept whole.""" + plain = [dependency.split("#keep:")[0] for dependency in dependencies] + crates = {PurePosixPath(path).parts[1] for path in plain if path.startswith("crates/")} + rust = {path for path in plain if is_workspace_rust(path)} + added = {path for path in changed if is_workspace_rust(path) and PurePosixPath(path).parts[1] in crates + and path not in rust and (graph.root / path).is_file()} + entries = graph.entries(artifacts, rust) + if entries is None: + return {dependency: (True, None) for dependency in dependencies} + missing = {path for path in rust if graph.parse(path) is None} + reached = graph.reachable(entries, rust | added | entries) | missing + named, named_in_crate = set(), {} + for path in rust | added: + parsed = graph.parse(path) + file_names = set(parsed["file_names"]) if parsed else set() + if path in reached: + named |= file_names + named_in_crate.setdefault(PurePosixPath(path).parts[1], set()).update(file_names) + kept = {} + for dependency, path in zip(dependencies, plain): + if is_workspace_rust(path): + if path in reached: + kept[dependency] = (True, set()) + elif not path.startswith("crates/") or path.endswith(("Cargo.toml", "build.rs")) \ + or PurePosixPath(path).name in named \ + or PurePosixPath(path).name not in named_in_crate.get(PurePosixPath(path).parts[1], ()): + kept[dependency] = (True, None) + for path in sorted(added & reached): + kept[path] = (True, set()) + for path in sorted(reached): + child = path + while child in graph.parent: + parent = graph.parent[child] + kept.setdefault(parent, (False, set()))[1].add(graph.module_name[child]) + child = parent + return kept diff --git a/tools/prepare_output.py b/tools/prepare_output.py new file mode 100755 index 000000000..81ed156cb --- /dev/null +++ b/tools/prepare_output.py @@ -0,0 +1,176 @@ +#!/usr/bin/env python3 +"""The prepared output's version: what keeps `OUTPUT_VERSION` (crates/horizon-prepare/src/lib.rs) honest. + +The launcher copies a prepared version's resources (`.local/horizon-client/`) to a newly installed +one when both pin the same Minecraft jars and their preparers declare the same `OUTPUT_VERSION`, so a release +that changes the preparer's code without changing what it makes asks nobody to prepare again. The number is +bumped by hand; this tool catches a change to the output without a bump. + +crates/horizon-prepare/output-versions.txt lists every `OUTPUT_VERSION` with the digest of what it prepares. +It only grows: the last line is the current version, and a change to the output appends a line. + + python3 tools/prepare_output.py digest [RESOURCES] the digest of a preparation + python3 tools/prepare_output.py check [--base SHA] [--cache] [RESOURCES] + fails when RESOURCES (default: this checkout's .local/horizon-client/, as CI prepares it) is + not what the last line says, or when the file changed other than by appending since SHA. --cache keeps + the digest beside RESOURCES (CI: a preparation never changes once made; it takes ~30 s to compute) + +The digest covers every file of the preparation, canonicalised where the preparer is not deterministic between +runs (verified across the CI runners): JSON is compared as data (key order, the order of each `tags` list of +creative-inventory.json, and the addresses in `$$Lambda/0x...` class names don't count); logs, compiled classes, +the sounds (copied from the player's launcher by another step) and the manifest's file hashes (raw bytes) don't +count. The manifest's own scalar fields (schema, data version, jar hashes, file count) do. +""" + +from __future__ import annotations + +import argparse +import hashlib +import json +import os +import re +import subprocess +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parent.parent +VERSIONS = 'crates/horizon-prepare/output-versions.txt' +LIB = 'crates/horizon-prepare/src/lib.rs' +MANIFEST = 'horizon-resources.json' +LAMBDA = re.compile(r'\$\$Lambda/0x[0-9a-fA-F]+') +CONSTANT = re.compile(r'^pub const OUTPUT_VERSION: u32 = (\d+);', re.M) + + +def canonical_json(value, key=''): + if isinstance(value, dict): + return {k: canonical_json(v, k) for k, v in value.items()} + if isinstance(value, list): + items = [canonical_json(v, key) for v in value] + if key == 'tags' and all(isinstance(x, list) and all(isinstance(t, str) for t in x) for x in items): + items = [sorted(x) for x in items] + return items + if isinstance(value, str): + return LAMBDA.sub('$$Lambda', value) + return value + + +def canonical(rel: str, data: bytes) -> bytes: + if rel == MANIFEST: + manifest = json.loads(data) + scalars = {k: v for k, v in manifest.items() if not isinstance(v, (dict, list))} + return json.dumps(scalars, sort_keys=True).encode() + if rel.endswith('.json'): + try: + value = json.loads(data) + except ValueError: + return data + return json.dumps(canonical_json(value), sort_keys=True).encode() + return data + + +def skipped(rel: str) -> bool: + parts = rel.split('/') + return rel.endswith(('.log', '.class')) or parts[:3] == ['assets', 'minecraft', 'sounds'] \ + or rel == 'assets/minecraft/sounds.json' + + +def digest(resources: Path) -> tuple[str, int]: + """The digest of a preparation, and how many files it covers.""" + if not (resources / MANIFEST).is_file(): + raise SystemExit(f'{resources} is not a preparation: no {MANIFEST}') + files = [] + for dirpath, dirs, names in os.walk(resources): + dirs.sort() + for name in names: + path = Path(dirpath) / name + rel = path.relative_to(resources).as_posix() + if path.is_symlink() or skipped(rel): + continue + files.append(rel) + total = hashlib.sha256() + for rel in sorted(files): + data = hashlib.sha256(canonical(rel, (resources / rel).read_bytes())).hexdigest() + total.update(f'{rel}\0{data}\n'.encode()) + return total.hexdigest(), len(files) + + +def parse_versions(text: str) -> list[tuple[int, str]]: + out = [] + for line in text.splitlines(): + line = line.strip() + if not line or line.startswith('#'): + continue + number, value = line.split() + out.append((int(number), value)) + return out + + +def problems(versions: list[tuple[int, str]], constant: int | None) -> list[str]: + found = [] + if not versions: + return [f'{VERSIONS} lists no version'] + if [n for n, _ in versions] != list(range(1, len(versions) + 1)): + found.append(f'{VERSIONS}: the versions must be 1, 2, 3... in order') + if len({d for _, d in versions}) != len(versions): + found.append(f'{VERSIONS}: two versions with the same digest') + if constant != versions[-1][0]: + found.append(f'{LIB} says OUTPUT_VERSION = {constant}, {VERSIONS} ends at {versions[-1][0]}') + return found + + +def default_resources() -> Path: + lock = json.loads((ROOT / 'research-lock.json').read_text()) + return ROOT / '.local/horizon-client' / lock['minecraft']['version'] + + +def main() -> int: + parser = argparse.ArgumentParser(description=__doc__.split('\n\n')[0]) + parser.add_argument('command', choices=['digest', 'check']) + parser.add_argument('resources', nargs='?', type=Path) + parser.add_argument('--base', help='the commit the change is based on (check: the file only grew since)') + parser.add_argument('--cache', action='store_true', help='keep the digest beside the preparation') + args = parser.parse_args() + resources = (args.resources or default_resources()).resolve() + tool = hashlib.sha256(Path(__file__).read_bytes()).hexdigest()[:16] + cache = resources.parent / f'.output-digest-{resources.name}-{tool}' + if args.cache and cache.is_file(): + value, count = cache.read_text().split() + else: + value, count = digest(resources) + if args.cache: + cache.write_text(f'{value} {count}\n') + if args.command == 'digest': + print(value) + return 0 + + versions = parse_versions((ROOT / VERSIONS).read_text()) + match = CONSTANT.search((ROOT / LIB).read_text()) + found = problems(versions, int(match.group(1)) if match else None) + if args.base: + shown = subprocess.run(['git', 'show', f'{args.base}:{VERSIONS}'], cwd=ROOT, capture_output=True, text=True) + if shown.returncode == 0: + before = parse_versions(shown.stdout) + if versions[:len(before)] != before: + found.append(f'{VERSIONS} changed lines it already had (since {args.base[:12]}): ' + 'a new output gets a new version, appended') + current = versions[-1] if versions else (0, '') + if value != current[1]: + known = [n for n, d in versions if d == value] + if known: + found.append(f'the preparation is OUTPUT_VERSION {known[0]}\'s output, but OUTPUT_VERSION is {current[0]}') + else: + found.append( + f'the prepared output changed ({count} files, digest {value}) but OUTPUT_VERSION is still ' + f'{current[0]}. Players would keep resources prepared by the old preparer. Bump it:\n' + f' set `pub const OUTPUT_VERSION: u32 = {current[0] + 1};` in {LIB}\n' + f' append `{current[0] + 1} {value}` to {VERSIONS}\n' + ' (if the change is only a nondeterminism this tool should ignore, teach canonical() instead)') + for problem in found: + print(f'::error::{problem}' if os.environ.get('GITHUB_ACTIONS') else f'error: {problem}', file=sys.stderr) + if not found: + print(f'prepared output: OUTPUT_VERSION {current[0]} ({count} files, {value[:16]})') + return 1 if found else 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/tools/release_train.py b/tools/release_train.py index 48d4d46cf..72d81ce57 100644 --- a/tools/release_train.py +++ b/tools/release_train.py @@ -414,13 +414,26 @@ def gather(kind, head, releases=None): return last, prs -def green_main_head(): - """The newest commit on main whose checks passed: what an automatic release builds.""" - runs = gh_json("run", "list", "--repo", REPO, "--workflow", "checks", "--branch", "main", "--event", "push", +def passed_on(workflow, sha): + """Whether the newest finished push run of workflow on main for commit sha passed.""" + runs = gh_json("run", "list", "--repo", REPO, "--workflow", workflow, "--branch", "main", "--event", "push", + "--commit", sha, "--limit", "5", "--json", "status,conclusion") + done = [entry for entry in runs if entry["status"] == "completed" and entry["conclusion"] != "cancelled"] + return bool(done) and done[0]["conclusion"] == "success" + + +def green_main_head(parity=False): + """The newest commit on main whose checks passed: what an automatic release builds. With parity (game + releases), the newest commit parity-main passed on, if its checks passed too: parity-main runs on batches of + merges, so the commits a run skipped are covered by the next one, and a superseded run counts for nothing.""" + workflow = "parity-main" if parity else "checks" + runs = gh_json("run", "list", "--repo", REPO, "--workflow", workflow, "--branch", "main", "--event", "push", "--limit", "10", "--json", "headSha,status,conclusion") for entry in runs: - if entry["status"] == "completed": - return entry["headSha"] if entry["conclusion"] == "success" else None + if entry["status"] == "completed" and entry["conclusion"] != "cancelled": + if entry["conclusion"] != "success": + return None + return entry["headSha"] if not parity or passed_on("checks", entry["headSha"]) else None return None @@ -437,7 +450,7 @@ def main(): parser.add_argument("--kind", choices=["game", "launcher"], required=True) parser.add_argument("--head", help="the commit to release (default: the newest commit on main whose checks passed)") parser.add_argument("--tag", help="notes: the release's tag") - parser.add_argument("--force", action="store_true", help="release even without player-facing changes, before merges settle or while paused") + parser.add_argument("--force", action="store_true", help="release even without player-facing changes, before merges settle, while paused or before parity-main passed") parser.add_argument("--out", type=Path, help="notes: write title.txt and body.md here") parser.add_argument("--now", help="the time to decide at (ISO 8601; default now)") parser.add_argument("--smoke", type=Path, help="gate: the release smoke test's report (smoke-test.json)") @@ -456,9 +469,12 @@ def main(): summary.write(f"### {line}\n") return 1 if problem else 0 - head = args.head or green_main_head() + # Game releases ship only a commit the vanilla harnesses passed on (parity-main, docs/release-flow.md). + parity = args.kind == "game" and args.command == "decide" and not args.force + head = args.head or green_main_head(parity) if not head: - result = {"release": False, "reason": "main's latest checks did not pass"} + result = {"release": False, "reason": "main's latest checks did not pass" + ( + " or parity-main did not pass on them" if parity else "")} print(json.dumps(result, indent=1)) write_outputs({"release": "false", "reason": result["reason"]}) return 0 @@ -482,6 +498,8 @@ def main(): blockers = open_blockers() if args.kind == "game" else [] release, reason = decision(args.kind, prs, now, os.environ.get("RELEASES_PAUSED") == "1", args.force, new_line, blockers) + if release and args.head and parity and not (passed_on("parity-main", head) and passed_on("checks", head)): + release, reason = False, f"waiting for checks and parity-main to pass on {head[:9]}" minor = any("feature" in pr["labels"] for pr in prs if pr["paths"]) tag = next_game_tag(tags, now.date(), line) if args.kind == "game" else next_launcher_tag(tags, minor, line) result = { diff --git a/tools/roadmap.py b/tools/roadmap.py index fe97285ff..4bc439e4d 100644 --- a/tools/roadmap.py +++ b/tools/roadmap.py @@ -56,7 +56,8 @@ ITEM_KEYS = {"id", "title", "status", "area", "plain", "dev", "links", "focus", ISSUE = re.compile(r"#(\d+)$") ID = re.compile(r"[a-z0-9]+(-[a-z0-9]+)*$") # An item is the anchor playhorizon.dev/hub#: it can't take the name of a tab, a section or a heading. -RESERVED = {"main", "overview", "roadmap", "board", "journal", "contribute", "hub-data", *STATUSES} +RESERVED = {"main", "overview", "roadmap", "board", "journal", "contribute", "report", "suggest", "contributors", + "hub-data", *STATUSES} RESERVED_PREFIXES = ("h-", "tab-", "col-", "week-", "journal-") RELEASE = re.compile(r"(v\d[\w.]*|launcher@\d[\w.]*|launcher-v\d[\w.]*|website)$") MONTHS = ("January|February|March|April|June|July|August|September|October|November|December") diff --git a/tools/test_collision_parity.py b/tools/test_collision_parity.py index ad99ab960..26f7f25df 100644 --- a/tools/test_collision_parity.py +++ b/tools/test_collision_parity.py @@ -5,6 +5,7 @@ import json import os from pathlib import Path import platform +import shutil import subprocess import tempfile @@ -37,6 +38,8 @@ def main(): if args.prepare: lab.prepare() lock, jars, libraries = test_rust_parity.verify_artifacts() + for earlier in directory.glob("run-*"): # one run per run used to stay behind: 5 GB here after a few weeks + shutil.rmtree(earlier, ignore_errors=True) base = Path(tempfile.mkdtemp(prefix="run-", dir=directory)) oracle = lab.ROOT / "mods/horizon/src/test/java/dev/horizonlab/parity/VanillaCollisionOracle.java" sources = [oracle, Path(__file__).resolve(), lab.ROOT / "tools/test_rust_parity.py", lab.ROOT / "Cargo.toml", lab.ROOT / "Cargo.lock", lab.ROOT / "crates/horizon-core/Cargo.toml", lab.ROOT / "crates/horizon-world/Cargo.toml", *sorted((lab.ROOT / "crates/horizon-core/src").rglob("*.rs")), *sorted((lab.ROOT / "crates/horizon-world/src").rglob("*.rs")), lab.ROOT / "crates/horizon-world/examples/probe/collision_fixture.rs"] diff --git a/tools/test_cursor_death_parity.py b/tools/test_cursor_death_parity.py new file mode 100644 index 000000000..f64f2d506 --- /dev/null +++ b/tools/test_cursor_death_parity.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +"""What a player holds on the cursor when they die, on a dedicated server, against vanilla's, seen by a vanilla client. + +A vanilla dedicated server and Horizon's (`horizon-client --dedicated --nogui`) each serve a copy of one flat world +vanilla generated first, with Alice an operator in survival and keep_inventory off. LocatorBarClient (a player made +of vanilla's protocol code) is given seven diamonds, picks them up onto its inventory menu's cursor (a PICKUP click) +ten blocks from the spawn, runs /kill on itself, waits on the death screen and respawns (PERFORM_RESPAWN). Twenty +ticks after dying vanilla removes the dead player (LivingEntity.tickDeath), and Player.remove's +inventoryMenu.removed drops the cursor's stack where it died. Alice then asks whether an item lies around the death +site; the reply, and the inventory slots, respawns and teleports she is sent from the death on, are compared +exactly. +""" +import sys + +try: + from . import test_locator_bar_parity as locator +except ImportError: + import test_locator_bar_parity as locator + +NAME = 'cursor-death-parity' +# server_dir and generate_world write these server.properties. +locator.PROPERTIES = dict(locator.PROPERTIES, gamemode='survival') +SCRIPT = [ + ('setup', ['cmd Alice gamerule keep_inventory false', 'cmd Alice clear @s', + 'cmd Alice give @s minecraft:diamond 7', 'cmd Alice tp @s 10.5 -60 0.5']), + # Hotbar slot 0 is the inventory menu's slot 36. + ('pickup', ['pickup Alice 36']), + ('death', ['cmd Alice kill @s']), + ('respawn', ['respawn Alice']), + ('check', ['chat on', 'cmd Alice execute if entity @e[type=minecraft:item,x=10.5,y=-60,z=0.5,distance=..4]']), +] + + +def shown(lines): + """Alice's slots, respawns, teleports and chat from the death on.""" + return [line for line in lines if len(parts := line.split(' ', 3)) >= 3 + and parts[1] in ('step=death', 'step=respawn', 'step=check') + and parts[2] in ('slot', 'inventory', 'respawn', 'teleport', 'chat')] + + +def check(vanilla, _): + if not any(l.startswith('Alice step=check chat') for l in vanilla): + return 'vanilla answered no check command' + return None + + +def main(): + lines = locator.one_player(NAME, SCRIPT, shown, check, 'A player dying with a stack on the inventory menu cursor ' + '(keep_inventory off) and respawning on a dedicated server: the stack is dropped ' + 'where it died, not kept.') + print(f'cursor death: {len(lines)} lines identical') + + +if __name__ == '__main__': + try: + main() + except AssertionError as error: + sys.exit(f'{NAME}: {error}') diff --git a/tools/test_geometry_parity.py b/tools/test_geometry_parity.py index 54ebc21ae..fa58bf66f 100644 --- a/tools/test_geometry_parity.py +++ b/tools/test_geometry_parity.py @@ -9,6 +9,7 @@ from pathlib import Path import platform import random import struct +import shutil import subprocess import tempfile @@ -273,6 +274,8 @@ def main(): if args.prepare: lab.prepare() lock, jars, libraries = test_rust_parity.verify_artifacts() + for earlier in directory.glob("run-*"): # one run per run used to stay behind: 5 GB here after a few weeks + shutil.rmtree(earlier, ignore_errors=True) base = Path(tempfile.mkdtemp(prefix="run-", dir=directory)) commands = list(corpus()) inputs = base / "inputs.txt" diff --git a/tools/test_hardcore_respawn_parity.py b/tools/test_hardcore_respawn_parity.py new file mode 100644 index 000000000..8c7a5394d --- /dev/null +++ b/tools/test_hardcore_respawn_parity.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 +"""Respawning in a hardcore world on a dedicated server, against vanilla's, seen by a vanilla client. + +A vanilla dedicated server and Horizon's (`horizon-client --dedicated --nogui`) each serve a copy of one hardcore +flat world vanilla generated first, with Alice an operator in survival. LocatorBarClient (a player made of +vanilla's protocol code) joins, runs /kill on itself and presses the death screen's button +(ServerboundClientCommandPacket PERFORM_RESPAWN). Vanilla's ServerGamePacketListenerImpl.handleClientCommand +respawns the player, then makes it a spectator because the server is hardcore. The game mode changes and respawns +Alice is sent in the death and respawn steps are compared exactly. +""" +import sys + +try: + from . import test_locator_bar_parity as locator +except ImportError: + import test_locator_bar_parity as locator + +NAME = 'hardcore-respawn-parity' +# server_dir and generate_world write these server.properties. +locator.PROPERTIES = dict(locator.PROPERTIES, hardcore='true', gamemode='survival') +SCRIPT = [ + ('death', ['cmd Alice kill @s']), + ('respawn', ['respawn Alice']), +] + + +def shown(lines): + """Alice's game mode changes and respawns in the death and respawn steps.""" + return [line for line in lines if len(parts := line.split(' ', 3)) >= 3 + and parts[1] in ('step=death', 'step=respawn') and parts[2] in ('gamemode', 'respawn')] + + +def check(vanilla, _): + if 'Alice step=respawn gamemode 3' not in vanilla: + return 'vanilla did not make the respawned player a spectator' + return None + + +def main(): + lines = locator.one_player(NAME, SCRIPT, shown, check, 'A player dying and respawning in a hardcore world on a ' + 'dedicated server: the respawn and the game mode it is sent (spectator).') + print(f'hardcore respawn: {len(lines)} lines identical') + + +if __name__ == '__main__': + try: + main() + except AssertionError as error: + sys.exit(f'{NAME}: {error}') diff --git a/tools/test_locator_bar_parity.py b/tools/test_locator_bar_parity.py index bc7d21750..985b87a35 100644 --- a/tools/test_locator_bar_parity.py +++ b/tools/test_locator_bar_parity.py @@ -205,12 +205,13 @@ def run_side(label, server_command, client_command, build, world): return out.read_text(encoding='utf-8').splitlines() -def main(): +def sides(name): + """The vanilla and Horizon dedicated servers and the LocatorBarClient that joins them, built for harness + `name`: (lock, build directory, vanilla server, Horizon server, client) commands.""" lock, jars, libraries = base.verify_artifacts() - build = lab.LOCAL / NAME + build = lab.LOCAL / name build.mkdir(parents=True, exist_ok=True) - report = build / 'report.json' - report.unlink(missing_ok=True) + (build / 'report.json').unlink(missing_ok=True) oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java' classes = build / 'classes' shutil.rmtree(classes, ignore_errors=True) @@ -228,7 +229,74 @@ def main(): vanilla_server = ['java', '-Xmx2g', '-Duser.timezone=UTC', '-cp', dependencies, 'net.minecraft.server.Main', 'nogui'] horizon_server = [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui'] client = ['java', '-Xmx1g', '-cp', str(classes) + os.pathsep + dependencies, 'dev.horizonlab.parity.LocatorBarClient'] + return lock, build, vanilla_server, horizon_server, client + +def one_player(name, script, shown, check, scope, step_seconds=4.0): + """A harness of one player, Alice: on each server she joins, then runs `script`, a list of (step, client + actions) `step_seconds` apart. The lines `shown(transcript)` keeps must match vanilla's exactly, and + `check(vanilla's shown lines, vanilla's transcript)` returns why vanilla's run is no proof (the script did + not run), or None.""" + lock, build, vanilla_server, horizon_server, client = sides(name) + + def run(label, server_command, world): + directory = build / label + port = server_dir(directory, world) + out = build / f'{label}.txt' + out.unlink(missing_ok=True) + server = Server(server_command, directory, label) + try: + server.wait_for(r'Done \(') + with (build / f'{label}-client.log').open('w') as log: + process = subprocess.Popen([*map(str, client), '127.0.0.1', str(port), str(out)], cwd=lab.ROOT, + stdin=subprocess.PIPE, stdout=log, stderr=subprocess.STDOUT, text=True) + + def tell(line): + process.stdin.write(line + '\n') + process.stdin.flush() + try: + for line in ['players on', 'state on', 'step joins', 'join Alice']: + tell(line) + server.wait_for(r'Alice joined the game', timeout=120) + time.sleep(step_seconds) + for step, actions in script: + tell(f'step {step}') + for action in actions: + tell(action) + time.sleep(step_seconds) + tell('end') + process.wait(timeout=60) + finally: + if process.poll() is None: + process.kill() + finally: + server.stop() + if not out.exists(): + raise AssertionError(f'{label}: the client wrote no transcript (see {build}/{label}-client.log)') + return out.read_text(encoding='utf-8').splitlines() + + world = generate_world(vanilla_server, build) + vanilla_all = run('vanilla', vanilla_server, world) + horizon_all = run('horizon', horizon_server, world) + errors = [l for l in vanilla_all + horizon_all if ' error ' in f' {l} ' or 'disconnected' in l] + vanilla, horizon = shown(vanilla_all), shown(horizon_all) + if errors or vanilla != horizon: + import difflib + diff = '\n'.join(difflib.unified_diff(vanilla, horizon, 'vanilla', 'horizon', n=1, lineterm='')) + raise AssertionError(f'{len(errors)} errors, transcripts differ (see {build}/vanilla.txt and horizon.txt):\n' + + '\n'.join(errors[:10]) + '\n' + diff[:6000]) + if (why := check(vanilla, vanilla_all)) is not None: + raise AssertionError(f'{why}: the script did not run (see {build})') + lab.write_json(build / 'report.json', dict( + schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(), + java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], + lines=len(vanilla), output_sha256=lab.digest(build / 'horizon.txt', 'sha256'), scope=scope)) + return vanilla + + +def main(): + lock, build, vanilla_server, horizon_server, client = sides(NAME) + report = build / 'report.json' world = generate_world(vanilla_server, build) vanilla = run_side('vanilla', vanilla_server, client, build, world) horizon = run_side('horizon', horizon_server, client, build, world) diff --git a/tools/test_lore_tooltip_parity.py b/tools/test_lore_tooltip_parity.py new file mode 100644 index 000000000..d262a3542 --- /dev/null +++ b/tools/test_lore_tooltip_parity.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 +"""Compare item lore tooltip styles with the pinned vanilla client. + +Vanilla prints what ``ItemLore.addToTooltip`` adds after ``ComponentUtils.mergeStyles`` +with dark purple italic (VanillaLoreTooltipOracle). Horizon prints what +``item_tooltip::lore_texts`` gives (examples/probe/lore_tooltip_fixture). +""" +try: + from . import lab, oracle_harness as harness +except ImportError: + import lab, oracle_harness as harness + +# SNBT stacks. A lore line is a text component; italic:0b is an explicit false. +CASES = [ + '{id:"minecraft:stick",count:1}', + '{id:"minecraft:stick",count:1,components:{"minecraft:lore":[\'"Plain"\']}}', + '{id:"minecraft:stick",count:1,components:{"minecraft:lore":[\'{"text":"Gold","color":"gold","italic":0b}\']}}', + '{id:"minecraft:stick",count:1,components:{"minecraft:lore":[\'{"text":"A","extra":[{"text":"B","color":"aqua","italic":0b}]}\']}}', + '{id:"minecraft:stick",count:1,components:{"minecraft:lore":[\'"One"\',\'{"text":"Two","bold":1b}\']}}', + '{id:"minecraft:stick",count:1,components:{"minecraft:lore":[\'"Hidden"\'],"minecraft:tooltip_display":{hidden_components:["minecraft:lore"]}}}', + '{id:"minecraft:stick",count:1,components:{"minecraft:lore":[\'{"text":"Line","underlined":1b,"strikethrough":1b}\']}}', +] + + +def cases(build): + path = build / 'cases.txt' + path.write_text(''.join(line + '\n' for line in CASES)) + return path, len(CASES) + + +if __name__ == '__main__': + harness.differential( + 'lore-tooltip-parity', 'VanillaLoreTooltipOracle', 'lore_tooltip_fixture', cases, + 'Item lore tooltip lines: dark purple italic, child colours, explicit non-italic, and tooltip_display', + sources=[ + lab.ROOT / 'crates/horizon-client/src/item_tooltip.rs', + lab.ROOT / 'crates/horizon-client/src/item_data.rs', + lab.ROOT / 'crates/horizon-client/src/text.rs', + lab.ROOT / 'crates/horizon-client/src/component.rs', + ], + expected_lines=len(CASES)) diff --git a/tools/test_pearl_death_parity.py b/tools/test_pearl_death_parity.py new file mode 100644 index 000000000..81714c5d4 --- /dev/null +++ b/tools/test_pearl_death_parity.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 +"""An ender pearl in flight when its thrower dies, on a dedicated server, against vanilla's, seen by a vanilla client. + +A vanilla dedicated server and Horizon's (`horizon-client --dedicated --nogui`) each serve a copy of one flat world +vanilla generated first, with Alice an operator in survival. LocatorBarClient (a player made of vanilla's protocol +code) throws an ender pearl straight up (ServerboundUseItemPacket), runs /kill on itself while the pearl flies, +presses the death screen's button (PERFORM_RESPAWN) and waits for the pearl to come down, with the rule +ender_pearls_vanish_on_death true: ThrownEnderpearl.tick discards a dead owner's pearl, so the respawned player is +never teleported. The teleports, game mode changes and respawns Alice is sent from the throw on are compared +exactly. +""" +import sys + +try: + from . import test_locator_bar_parity as locator +except ImportError: + import test_locator_bar_parity as locator + +NAME = 'pearl-death-parity' +# server_dir and generate_world write these server.properties. +locator.PROPERTIES = dict(locator.PROPERTIES, gamemode='survival') +SCRIPT = [ + ('setup', ['cmd Alice gamerule ender_pearls_vanish_on_death true', 'cmd Alice give @s minecraft:ender_pearl']), + # Straight up: it comes down about four seconds later, near where it was thrown. + ('throw', ['use Alice -90', 'cmd Alice kill @s']), + ('respawn', ['respawn Alice']), + ('landed', []), +] + + +def shown(lines): + """Alice's teleports, game mode changes and respawns from the throw on.""" + steps = {f'step={step}' for step, _ in SCRIPT if step != 'setup'} + return [line for line in lines if len(parts := line.split(' ', 3)) >= 3 and parts[1] in steps + and parts[2] in ('teleport', 'gamemode', 'respawn')] + + +def check(vanilla, vanilla_all): + killed = any(l.startswith('Alice step=throw killed') for l in vanilla_all) + if not killed or 'Alice step=respawn respawn minecraft:overworld' not in vanilla: + return 'vanilla did not kill and respawn Alice' + return None + + +def main(): + lines = locator.one_player(NAME, SCRIPT, shown, check, 'An ender pearl in flight when its thrower dies and ' + 'respawns on a dedicated server, with ender_pearls_vanish_on_death true: the pearl ' + 'vanishes and never teleports the player.') + print(f'pearl death: {len(lines)} lines identical') + + +if __name__ == '__main__': + try: + main() + except AssertionError as error: + sys.exit(f'{NAME}: {error}') diff --git a/tools/test_world_parity.py b/tools/test_world_parity.py index 0b4a3deb7..9344a47b0 100644 --- a/tools/test_world_parity.py +++ b/tools/test_world_parity.py @@ -5,6 +5,7 @@ import json import os from pathlib import Path import platform +import shutil import subprocess import tempfile @@ -35,6 +36,8 @@ def main(): if args.prepare: lab.prepare() lock, jars, libraries = test_rust_parity.verify_artifacts() + for earlier in directory.glob("run-*"): # one run per run used to stay behind: 5 GB here after a few weeks + shutil.rmtree(earlier, ignore_errors=True) base = Path(tempfile.mkdtemp(prefix="run-", dir=directory)) classes = base / "classes" classes.mkdir() diff --git a/website/Caddyfile b/website/Caddyfile index 5295b5e9e..88390ffdc 100644 --- a/website/Caddyfile +++ b/website/Caddyfile @@ -38,6 +38,10 @@ redir /roadmap /hub#roadmap permanent @doc path_regexp doc ^/docs/([a-z-]+)$ rewrite @doc /docs/{re.doc.1}.html + # The API portal: /docs/api is its index, /docs/api/ its pages. + redir /docs/api/ /docs/api permanent + @apidoc path_regexp apidoc ^/docs/api/([a-z-]+)$ + rewrite @apidoc /docs/api/{re.apidoc.1}.html # 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/* { diff --git a/website/Dockerfile b/website/Dockerfile index ea9cc9afc..b075f9b58 100644 --- a/website/Dockerfile +++ b/website/Dockerfile @@ -5,6 +5,7 @@ WORKDIR /site COPY build.mjs ./ COPY src ./src COPY docs ./docs +COPY data ./data COPY public ./public COPY LEGAL.md ./ ARG GITHUB_TOKEN diff --git a/website/README.md b/website/README.md index 338e25863..ccf54b59b 100644 --- a/website/README.md +++ b/website/README.md @@ -81,8 +81,13 @@ mark merged-but-unreleased things "next release". `{{HARNESSES}}` and `{{PERCENT the README. A line holding only `![caption](/media/...)` becomes a figure (the file must be in `public/media/`); links to other pages are `/docs/#heading`. The build checks every link between pages and every image, and fails with the list when one is broken. `/server` -redirects to `/docs/hosting`. The developer docs in the repository's `docs/` stay as they are and are linked from -the Contributing page. +redirects to `/docs/hosting`. + +The API portal is the repository's `docs/api/`, not a copy: `nav.json` lists its README as `api` (`/docs/api`) and +its pages as `api/` (`/docs/api/`), read from the checkout or from `main` on GitHub like the README. +Links between its pages become the site's, every other relative link goes to the file on GitHub, and the same link +check applies. Markdown tables render as tables. The other developer docs in `docs/` stay on GitHub and are linked +from the Contributing page. ## Run it locally diff --git a/website/build.mjs b/website/build.mjs index 27949fe85..b979f83f8 100644 --- a/website/build.mjs +++ b/website/build.mjs @@ -3,12 +3,13 @@ // docs/nav.json), plus public/ and fingerprinted CSS and JS. No dependencies. // // Railway builds only website/, so the README and links.json come from the repo when they -// are next to it (a local checkout) and from main on GitHub otherwise. The documentation lives in website/. The latest releases and +// are next to it (a local checkout) and from main on GitHub otherwise. The documentation lives in website/, except the +// API portal (docs/api/), read the same way as the README. The latest releases and // the contributors are baked in when GitHub answers, so the page reads well before (or without) JavaScript. import { readFile, writeFile, mkdir, rm, cp } from "node:fs/promises"; import { existsSync } from "node:fs"; import { createHash } from "node:crypto"; -import { join, dirname } from "node:path"; +import { join, dirname, posix } from "node:path"; import { fileURLToPath } from "node:url"; import { markdown, inline, section, escape } from "./src/markdown.js"; import { recentHtml, releasesPageHtml } from "./src/releases.js"; @@ -16,7 +17,7 @@ import { machinesHtml, playersHtml } from "./src/bench.js"; 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, boardFilterHtml, boardFrom, boardHtml, helpWantedHtml, journalFrom, journalHtml, journalJson, overviewHtml, roadmapHtml, slimIssue } from "./src/hub.js"; +import { areaFilterHtml, boardFilterHtml, boardFrom, boardHtml, creditFrom, creditHtml, helpWantedHtml, journalFrom, journalHtml, journalJson, overviewHtml, roadmapHtml, slimIssue, suggestItemsHtml } from "./src/hub.js"; import { INDEX_URL, KINDS, SUBMIT, listHtml, projectHtml, readIndex, submitHtml } from "./src/mods.js"; const site = dirname(fileURLToPath(import.meta.url)); @@ -135,6 +136,18 @@ const hubLinked = new Map(hubLive.filter((i) => linked.has(i.n)).map((i) => [i.n const board = boardFrom(hubLive, roadmap); const journal = journalFrom(releases, hubLive, roadmap); +// The hub's server (crates/horizon-telemetry-server: votes, bug reports, suggestions), and Kira's list of the +// contributors who linked their Discord account and agreed to be credited (kira's /contributors.json). +const hubApi = process.env.HUB_API_URL || "https://telemetry.playhorizon.dev"; +let kiraCredit = {}; +try { + const res = await fetch(process.env.KIRA_CONTRIBUTORS_URL || "https://kira-production-500e.up.railway.app/contributors.json", { signal: AbortSignal.timeout(10000) }); + if (!res.ok) throw new Error(`${res.status} from Kira`); + kiraCredit = await res.json(); +} catch (e) { + console.warn(`hub: contributors' Discord names: ${e.message}`); +} + // --- The parity table ------------------------------------------------------------------ const status = section(readme, "Project status") ?? ""; @@ -348,22 +361,44 @@ const values = { HUB_BOARD: hubLive.length ? boardHtml(board, roadmap) : `

The board could not be loaded here.

`, HUB_BOARD_FILTER: boardFilterHtml(board, roadmap), HUB_JOURNAL: journalHtml(journal, roadmap) || unavailable, - HUB_DATA: JSON.stringify({ roadmap, live: [...hubLinked.values()] }).replace(/.md: "# Title", a one-paragraph lede, then the body. docs/nav.json orders them in groups. +// The API portal is the repository's docs/api/ (the pages and their generated references): nav.json lists it as +// `api` (its README, /docs/api) and `api/` (/docs/api/). Its links are written for GitHub, so links +// between its pages become the site's and every other relative link goes to the file on GitHub. const docsNav = JSON.parse(await readFile(join(site, "docs", "nav.json"), "utf8")); +const docSlugs = new Set(docsNav.flatMap((g) => g.pages)); +const apiSource = (slug) => `docs/api/${slug === "api" ? "README" : slug.slice(4)}.md`; +function apiLinks(text, from) { + return text.replace(/\]\(([^)\s]+)\)/g, (all, url) => { + if (url.startsWith("https://playhorizon.dev/")) return `](${url.slice(23)})`; + if (/^([a-z]+:|#|\/)/i.test(url)) return all; + const [file, id] = url.split("#"); + const path = posix.normalize(posix.join(posix.dirname(from), file)); + const page = path.match(/^docs\/api\/([\w-]+)\.md$/)?.[1]; + const slug = page === "README" ? "api" : page && `api/${page}`; + const to = docSlugs.has(slug) ? `/docs/${slug}` : `https://github.com/${REPO}/blob/main/${path}`; + return `](${to}${id ? `#${id}` : ""})`; + }); +} const docs = []; for (const { group, pages: slugs } of docsNav) { for (const slug of slugs) { - const file = `${slug}.md`; - const text = (await readFile(join(site, "docs", file), "utf8")).replace(/\r\n?/g, "\n"); + const api = slug === "api" || slug.startsWith("api/"); + const file = api ? apiSource(slug) : `website/docs/${slug}.md`; + let text = (api ? (await repoFile(file))[0] : await readFile(join(site, "docs", `${slug}.md`), "utf8")).replace(/\r\n?/g, "\n"); + if (api) text = apiLinks(text.replace(/^\n/, ""), file); const m = text.match(/^# (.+)\n\n((?:[^\n]+\n)+)\n/); - if (!m) throw new Error(`docs/${file}: needs "# Title", a blank line, a lede paragraph and a blank line`); - docs.push({ slug, file, group, title: m[1].trim(), lede: m[2].trim().replace(/\n/g, " "), body: text.slice(m[0].length), path: slug === "index" ? "/docs/" : `/docs/${slug}` }); + if (!m) throw new Error(`${file}: needs "# Title", a blank line, a lede paragraph and a blank line`); + docs.push({ slug, file, group, title: m[1].replace(/`/g, "").trim(), lede: m[2].trim().replace(/\n/g, " "), body: text.slice(m[0].length), path: slug === "index" ? "/docs/" : `/docs/${slug}` }); } } const plain = (md) => md.replace(/`/g, "").replace(/\[([^\]]+)\]\([^)]*\)/g, "$1").replace(/\*\*/g, ""); @@ -403,7 +438,7 @@ const docNav = (current) => .join("\n"); const pagerLink = (d, rel) => (d ? `${rel === "next" ? "Next" : "Previous"}${escape(d.title)}` : ""); -await mkdir(join(dist, "docs"), { recursive: true }); +await mkdir(join(dist, "docs", "api"), { recursive: true }); const docPageFiles = []; const docPages = renderedDocs.map((d, i) => { const toc = d.headings.filter((h) => h.level === 2); @@ -442,7 +477,7 @@ const modsTemplate = await readFile(join(site, "src", "mods.html"), "utf8"); const modsList = `

ModsMade for Horizon, reviewed

Horizon mods

Mods, modpacks and resource packs made for Horizon. Each one was reviewed before it was listed, and the launcher checks every file before it installs it. Mods run in a sandbox and can only do what their page says.

-

Submit yours Make a mod

+

Submit yours Make a mod

diff --git a/website/data/roadmap.json b/website/data/roadmap.json index 1fcbdd773..58d697257 100644 --- a/website/data/roadmap.json +++ b/website/data/roadmap.json @@ -59,10 +59,22 @@ "id": "launcher", "name": "Launcher" }, + { + "id": "platforms", + "name": "Platforms" + }, { "id": "extras", "name": "Horizon extras" }, + { + "id": "ai", + "name": "AI and agents" + }, + { + "id": "community", + "name": "Community" + }, { "id": "project", "name": "Code and tools" @@ -255,6 +267,73 @@ ], "ongoing": true }, + { + "id": "native-shaders", + "title": "Horizon's own shader format and kit", + "status": "now", + "area": "rendering", + "plain": "Horizon's own way to make shader packs, with tools to write and test them. Every pack is checked before it runs, and the registry will list them.", + "dev": "`horizon-shader-api` (API 0.1, experimental): WGSL stages with typed inputs inside the renderer's passes, options as WGSL override constants, and the `horizon-shader new|check|pack` tool with templates and the example pack Golden Hour. Merged: the check every pack passes (#1820) and the tool, templates and guide (#1822). In review: the post stage running in the game with hot reload (#1861), the sky stage (#1863) and registry checks that use the game's own check (#1855). Left: the material, lighting and shadow stages, a last-good fallback on reload, a GPU-time overlay per stage and budgets (docs/shaders.md, docs/api/shaders.md). The Iris/OptiFine loader is a separate item, 'Shader packs, written again from scratch'.", + "links": [ + "#1820", + "#1822", + "#1861", + "#1863", + "#1855", + "docs/shaders.md", + "docs/api/shaders.md" + ] + }, + { + "id": "server-mods", + "title": "Mods that run on the server", + "status": "now", + "area": "mods", + "plain": "Mods that run on the server side of a world, so a dedicated server or a LAN world can have mods that react to players joining, chat and blocks being broken or placed, and that add commands.", + "dev": "API 0.3, experimental first (#1833, in review): the same sandboxed component runs on the server thread and the dedicated server, with world events (block broken or placed, entity spawned or killed, player joined or left), game rules and server commands with typed arguments. The world, item and entity interfaces come after (see 'Mods that read and change the world'). docs/api/mods.md, docs/mods.md.", + "links": [ + "#1833", + "docs/api/mods.md", + "docs/mods.md" + ] + }, + { + "id": "mod-screens", + "title": "Screens, input and drawing for mods", + "status": "now", + "area": "mods", + "plain": "Mods will be able to open their own screens and settings pages and react to keys and item use, so bigger mods, such as the Orbital Laser, can run on the normal game.", + "dev": "Capability `ui` is in review (#1864): mod-owned screens and a settings page under Options > Mods, built from vanilla's widgets. Still to design: `input` (an item's use, key binds, the crosshair target), `render` (a budgeted draw list on the native shader API) and `world` (changing blocks). Orbital Laser is the acceptance test: it needs all four today through its author's fork (see 'Orbital Laser on the normal game').", + "links": [ + "#1864", + "docs/mods.md" + ] + }, + { + "id": "modder-tools", + "title": "Tools for people who make mods", + "status": "now", + "area": "mods", + "plain": "Better tools for mod makers: a test runner that needs no game window, templates for each kind of mod, and clear promises about what keeps working from one version to the next.", + "dev": "`cargo horizon-mod test` runs a mod's test scripts against a fake game snapshot (#1816) and, for server mods, scripted server events (#1837); the content template and the guides are merged (#1859, #1810, docs/api/stability.md). Left: templates for hud, server and machine mods, typed wrappers in the SDK, a frozen 0.1 compatibility mod that every runtime must load, a CI check that fails when a stable reference entry is removed without a new major version, and the SDK on crates.io once the API is stable.", + "links": [ + "#1816", + "#1837", + "docs/api/stability.md", + "website/docs/test-a-mod.md" + ] + }, + { + "id": "pregen", + "title": "Pre-generate an area with /pregen", + "status": "now", + "area": "servers", + "plain": "A /pregen command that builds an area of the world ahead of time, so players don't wait for new terrain when they explore.", + "dev": "Generates, lights and saves an area before anyone walks there (#1805, a contributor's pull request, in review). Useful on servers, and it moves the generation cost away from the first visit; the generation speed-ups are under 'Steady frames at long render distances'.", + "links": [ + "#1805" + ] + }, { "id": "microsoft-sign-in", "title": "Microsoft sign-in (coming soon)", @@ -286,8 +365,8 @@ "title": "Fresh benchmarks against vanilla", "status": "next", "area": "performance", - "plain": "New public numbers comparing Horizon with vanilla on many kinds of computers, measured the same way every time, on the website's Benchmarks page.", - "dev": "Redo the comparison after the current performance work (#1073); reliability, coverage and presentation (#1346); a scene must count as loaded only once its first chunk arrives (#1749). Players' reports come in through `--benchmark` and `horizon-launcher data benchmark` (docs/benchmark.md).", + "plain": "New public numbers comparing Horizon with vanilla on many kinds of computers, measured the same way every time, with a history that shows what each release changed.", + "dev": "Redo the comparison after the current performance work (#1073); reliability, coverage and presentation (#1346); a scene must count as loaded only once its first chunk arrives (#1749). Next: a performance history across releases (an automatic comparison of each release with the last, on a timeline), more report fields (server TPS and MSPT, power, battery drain, temperature, throttling) and more hardware (a MacBook on battery, low-end laptops, community reports). Players' reports come in through `--benchmark` and `horizon-launcher data benchmark` (docs/benchmark.md).", "links": [ "#1073", "#1346", @@ -314,7 +393,7 @@ "id": "intel-mac", "title": "Horizon on Intel Macs", "status": "next", - "area": "launcher", + "area": "platforms", "plain": "Builds of the game and the launcher for Macs with an Intel processor.", "dev": "x86_64 game and launcher builds, picked per processor (#1171). Before it ships it needs a run on a real Intel Mac: Rosetta on Apple Silicon only proves the CPU code. Requests: #1161, #1792.", "links": [ @@ -323,13 +402,328 @@ "#1792" ] }, + { + "id": "world-entry", + "title": "Faster world creation and entry on small CPUs", + "status": "next", + "area": "performance", + "plain": "Creating a world and joining it should be quick even on a four-thread laptop, where a fresh world at 32 chunks can make you wait minutes for the first terrain.", + "dev": "Cut the per-column generation cost (noise sampling, surface rules, aquifers) and the lock convoy on structure starts, and leave one core free for the rest of the game as vanilla does (#1760 and #1778, in review, are listed under 'Steady frames at long render distances'). Still to profile on small CPUs: the first seconds of a fresh world deliver no chunk, and fluids remesh for about a minute after loading. Measured with `tools/perf_bench.py` (docs/benchmark.md, docs/profiling.md).", + "links": [ + "docs/benchmark.md", + "docs/profiling.md" + ] + }, + { + "id": "server-tick", + "title": "Servers that hold 20 TPS with 50 to 100 players", + "status": "next", + "area": "performance", + "plain": "Servers that keep 20 ticks per second with 50 to 100 players. Today Horizon holds it up to 50 bots and drops to about 10 at 100.", + "dev": "Measured against vanilla on equal work (docs/server.md, Scale): Horizon is ahead with few players and when exploring, but vanilla's tick is shorter with 25 to 50 players near spawn. Ideas on the list: send entity changes instead of rebuilding every entity each tick (about 15 percent), fewer memory reads in random ticks and collision scans, cheaper item collision checks, mob AI and natural spawning. The bot benchmark (`horizon-bots`) is the proof.", + "links": [ + "docs/server.md" + ] + }, + { + "id": "low-end-mode", + "title": "Low-end computers: Steam Deck, old Macs, 8 GB", + "status": "next", + "area": "performance", + "plain": "Horizon should run well on small machines: integrated graphics, the Steam Deck, older Macs and 8 GB of memory, with a battery mode that saves power on laptops.", + "dev": "Partly done: an opt-in 60 FPS cap with fewer workers on battery, Far Detail Reduced for integrated GPUs and animated textures on the GPU (both shipped). Left: measured targets and fixes per class of machine (memory use, worker counts, startup) on a Steam Deck, old Intel and Apple Silicon Macs and 8 GB computers, reported on the Benchmarks page.", + "links": [ + "docs/benchmark.md", + "website/docs/performance.md" + ] + }, + { + "id": "upscaling", + "title": "Native upscaling", + "status": "next", + "area": "performance", + "plain": "Draw the game at a lower resolution and sharpen it to your screen's size, in the way of FSR or DLSS, to gain frames on weaker graphics cards. An optional graphics setting.", + "dev": "A render-scale setting below the window size with an upscaler of our own that runs on every wgpu backend. Proof: frame time against native resolution on integrated GPUs, and screenshots for image quality. Issue #1403.", + "links": [ + "#1403" + ] + }, + { + "id": "targeted-optimisations", + "title": "A batch of targeted optimisations", + "status": "next", + "area": "performance", + "plain": "Several smaller speed-ups, each measured: idle block entities that sleep, cheaper particles, faster structure placement, high-rate mice and very large texture packs.", + "dev": "Block entities and hoppers that skip work when idle (the hopper item scan is already skipped when no item is near); a particle frustum test first (also a parity gap), then particle quads built on the GPU; jigsaw shortcuts that skip blocked connectors and pool elements known not to fit, with layouts unchanged (trial chambers and ancient cities first); batched raw mouse input on Windows for 8000 Hz mice; a better atlas packer for huge resource packs (a 64x pack builds a 704 MiB atlas for 452 MiB of textures); sign-heavy scenes (#1461). Profile first, keep what pays, show before and after numbers.", + "links": [ + "#1461", + "docs/profiling.md" + ] + }, + { + "id": "convert-to-rust", + "title": "A Convert to Rust button for what isn't rewritten yet", + "status": "next", + "area": "modpacks", + "plain": "When a mod, shader pack or modpack hasn't been rewritten for Horizon, one button converts what can be converted, so you don't have to wait for the rewrite.", + "dev": "Generalise the mod browser's Port step (`horizon-port-pack`: the data, assets, worldgen and block and item definitions of Java mods, .mrpack and CurseForge zips) to every kind of content: code mods where the API allows it (see 'Mods with code') and shader packs, converting an Iris/OptiFine pack into Horizon's format. The browser says what works before the install. Java code itself does not run (docs/modpacks.md, docs/mod-browser.md).", + "links": [ + "docs/modpacks.md", + "docs/mod-browser.md" + ] + }, + { + "id": "code-mods", + "title": "Mods with code: machines, magic, mobs", + "status": "next", + "area": "modpacks", + "plain": "Making mods that add machines, magic and mobs work in Horizon, in steps, starting with machines. They are most of what a big modpack is made of.", + "dev": "333 of the 496 mods in a big pack (ATM10) contain code. Issue #1854 ranks the API gaps (ticking block entities, networking, rendering, menus, inventories) that unlock them in tiers, with machines first as the recommended path and a furnace ported tick for tick as proof. The first API step is a draft (#1189, see 'Machines and blocks with inventories'). The path isn't started yet (docs/modpacks.md).", + "links": [ + "#1854", + "docs/modpacks.md" + ] + }, + { + "id": "datapack-browser", + "title": "Data packs in the mod browser", + "status": "next", + "area": "modpacks", + "plain": "Search and install data packs from the launcher's mod browser, for the world you choose, as you do mods and resource packs.", + "dev": "A Datapacks tab beside Mods and Resource packs: install into a chosen world's datapacks folder, with the same checks and 'works in Horizon' notes as the other kinds (docs/mod-browser.md).", + "links": [ + "docs/mod-browser.md" + ] + }, + { + "id": "prepare-without-java", + "title": "Prepare a version without Java", + "status": "next", + "area": "servers", + "plain": "Set up Horizon, a server first, without Java on your computer: published tables are combined with your own copy of the game.", + "dev": "Today `horizon-prepare` runs Mojang's exporters with Java. Issue #1750 proposes publishing the computed version tables once from CI and assembling the rest locally from the player's own jar and assets, provided the tables hold no Mojang content; a contributor offered a pull request, and it would help phones too. A server-only prepare from `server.jar` alone is the first step (docs/server.md).", + "links": [ + "#1750", + "docs/server.md" + ] + }, + { + "id": "server-admin", + "title": "Server console, bans and a full admin API", + "status": "next", + "area": "servers", + "plain": "Manage a server from a console, ban and pardon players, and drive it from other tools through a full admin API and an MCP server for AI agents.", + "dev": "`horizon-server console` (a text interface), `ban`, `pardon`, `ban-ip` and `mcp` over stdio. Admin API additions: the player list, kick, ban, op and whitelist, an SSE stream of logs and events, a text metrics endpoint, config GET and PATCH, backup delete and download. The panel's later features (bans, worlds, server.properties import, logs and crash reports, modpacks) and vanilla's management protocol (the JSON-RPC over WebSocket that the `management-server-*` keys configure), so vanilla's tools can manage a Horizon server. docs/server.md, docs/api/server.md.", + "links": [ + "docs/server.md", + "docs/api/server.md" + ] + }, + { + "id": "hosting-polish", + "title": "Hosting polish: Windows service, safe updates", + "status": "next", + "area": "servers", + "plain": "Smoother hosting: a real Windows service, a backup before every update, backups that keep the world and each player's state, and a changed view distance or message without a restart.", + "dev": "A Windows service wrapper instead of the logon scheduled task; `backups.before_update`; hot reload of config for view distance and the message of the day (needs engine support); backups that keep world and player state (#1808, a contributor's draft, in review). docs/hosting.md, docs/server.md.", + "links": [ + "#1808", + "docs/hosting.md" + ] + }, + { + "id": "tailscale-hosting", + "title": "Guided hosting with Tailscale and an invite button", + "status": "next", + "area": "servers", + "plain": "In the launcher, a guide and a button to host a server and invite friends over Tailscale, so nobody has to open ports on a router.", + "dev": "On the Servers page (create, start, back up, update and invite are shipped) add a Tailscale setup that explains and checks each step, and an invite button that hands friends what they need to join over the tailnet. Buttons first, no docs needed, with the details in an Advanced section. Remote access over Tailscale is already described in docs/server.md.", + "links": [ + "docs/hosting.md", + "docs/server.md" + ] + }, + { + "id": "docker-image", + "title": "An official Docker image for servers", + "status": "next", + "area": "servers", + "plain": "An official image to run a Horizon dedicated server in Docker with one command, your own copy of the game files mounted in.", + "dev": "The repository has the Dockerfile (`packaging/server/Dockerfile`: a multi-stage build on Debian bookworm-slim, user `horizon`, volume `/data`, port 25565, game files mounted read-only, `HORIZON_EULA` and friends answering the first-start questions), and anyone can build it (docs/hosting.md, section 9). Left: publish the image to a public registry from the release workflow, tagged per release, so it follows the same checks, and update the docs and the launcher. The image never holds Mojang's files.", + "links": [ + "docs/hosting.md", + "docs/server.md" + ] + }, + { + "id": "one-single-player-path", + "title": "One single-player path: remove the in-frame world", + "status": "next", + "area": "gameplay", + "plain": "Single player will have one way to run, the server thread, so every fix works everywhere and there are fewer bugs. The older in-frame mode goes once everything passes without it.", + "dev": "`--in-frame` (also `--no-server-thread` and `HORIZON_SERVER_THREAD=0`) still ticks the world inside the window's `Game`, and most harnesses test that path. Delete it once `lan_e2e --server-thread` and the parity harnesses pass on the server thread, which also halves the bug surface. See also 'Single player as a server' and docs/server-levels.md.", + "links": [ + "docs/server-levels.md" + ] + }, + { + "id": "villages-combat-parity", + "title": "Villages and combat, exactly as in vanilla", + "status": "next", + "area": "gameplay", + "plain": "Villages and fights behaving as in vanilla: iron golems defending villages, villagers remembering their homes, mace smashes and knockback.", + "dev": "Villages: iron golem village goals, villager memories across saves, villager age, job-site claiming on dedicated servers, flower offers (#1639 is one report). Combat: the mobs' `attack_knockback` attribute, mace smash logic, the hit knockback source on the host, the fire burnout biome tag. Each is checked against vanilla by a harness (docs/rust-parity.md).", + "links": [ + "#1639", + "docs/rust-parity.md" + ] + }, + { + "id": "audio-device", + "title": "Choose the audio output device", + "status": "next", + "area": "gameplay", + "plain": "Pick the speakers or headphones Horizon plays through, in Music & Sounds.", + "dev": "An output device list in Music & Sounds, falling back to the system default. #1035 started it: a player on an older Windows computer had no sound at all, and a device choice is how such cases are found and fixed.", + "links": [ + "#1035" + ] + }, + { + "id": "mod-world-items", + "title": "Mods that read and change the world", + "status": "next", + "area": "mods", + "plain": "Mods that can see and change blocks, read inventories and items, and find and affect entities, all through the server so every player sees the same thing.", + "dev": "After server mods: read blocks, states and biomes in loaded chunks and change them through the server's block log, so clients and the parity harnesses see vanilla's updates; the player's inventory, held item, open container and item stacks with components; entity queries near a position, a safe subset of entity data and data-driven mod entity types. Snapshots and batched calls per tick, never per-block host calls in hot paths. docs/api/mods.md ('Not there yet').", + "links": [ + "docs/api/mods.md", + "docs/modpacks.md" + ] + }, + { + "id": "machines-api", + "title": "Machines and blocks with inventories (API 0.4)", + "status": "next", + "area": "mods", + "plain": "Mods that add machines: blocks that tick, hold items and have their own menus. This unlocks the technology mods of big modpacks.", + "dev": "Block entities and menus: ticking machines with slots, server menu logic and the menu protocol to clients, energy, item and fluid transfer, data attachments, accessory slots and custom recipe types, with a typed client and server channel and typed config beside them. A draft (#1189) proves a furnace ported tick for tick and holds the ATM10 code-mods plan; the design is redone through the API policy before it merges (docs/modpacks.md, docs/api/stability.md).", + "links": [ + "#1189", + "docs/modpacks.md" + ] + }, + { + "id": "mod-languages", + "title": "Lua and other languages for mods", + "status": "next", + "area": "mods", + "plain": "Write mods as Lua scripts, in the style of KubeJS, with no compiler, and later in other languages that pass the tests. Every language runs in the same sandbox.", + "dev": "Lua first: Horizon ships a Lua interpreter that is itself a WebAssembly mod, and a script mod's manifest names it and the capabilities it uses, so a script can do exactly what a compiled mod with the same manifest can, with hot reload of `main.lua`. Then C, C++, Zig, Go (TinyGo), AssemblyScript and JavaScript or TypeScript are measured against Rust on the same tick-heavy example, and each is supported only with a template, an example and a CI test. docs/api/mods.md ('Ways in').", + "links": [ + "docs/api/mods.md" + ] + }, + { + "id": "shader-pack-examples", + "title": "Well-known shader looks, rewritten for Horizon", + "status": "next", + "area": "rendering", + "plain": "Popular shader packs rewritten in Horizon's format, so good packs exist from the start. Only packs whose licence is open source and compatible with ours are rewritten, never anything else.", + "dev": "The first candidates are Photon, Chocapic13 and MakeUp Ultra Fast, each only after its licence is read and recorded: a pack is rewritten only if it is open source and compatible with Horizon's licence, and the rewrite is written from the shader format's documentation and the look on screen, not from anyone's code. Today only the Golden Hour example exists (`crates/horizon-shader-api/packs/golden_hour`).", + "links": [ + "docs/shaders.md", + "crates/horizon-shader-api/packs/golden_hour" + ] + }, + { + "id": "beta-label", + "title": "A visible beta label", + "status": "next", + "area": "launcher", + "plain": "A clear 'beta' label in the launcher and the game, so nobody is surprised that Horizon is still being built.", + "dev": "Show Beta in the launcher's header and about page, on the game's title screen and on the website, beside the version number (docs/versioning.md).", + "links": [ + "docs/versioning.md" + ] + }, + { + "id": "wayland-polish", + "title": "Wayland and Hyprland polish", + "status": "next", + "area": "platforms", + "plain": "Fixes for Linux desktops that use Wayland and Hyprland: fullscreen, the clipboard, the GUI scale after a restart and the like.", + "dev": "Open: the GUI Scale is clamped to 2 after a restart under Hyprland, while XWayland keeps it (#1866). Earlier reports covered fullscreen dropping on menu clicks, the clipboard and a white overlay (#1007, closed). Checked on a real Wayland session.", + "links": [ + "#1866" + ] + }, + { + "id": "agent-control-protocol", + "title": "A versioned agent-control protocol", + "status": "next", + "area": "ai", + "plain": "A stable, documented way for AI agents and scripts to drive the game and the launcher, with versions and an event stream so tools keep working as Horizon changes.", + "dev": "`hello` answers with the API version, the release and the features; `horizon-ctl --api`; MCP serverInfo; reply schemas generated into the reference; a `test.` namespace for hooks; transcript compatibility tests; `subscribe` as a stream of events on one connection; Horizon option files by key and saved-server commands. Agent control itself (`horizon-ctl`, both MCP servers, the No AI switch) is done: docs/agent-control.md, docs/api/agent-control.md.", + "links": [ + "docs/agent-control.md", + "docs/api/agent-control.md", + "docs/api/stability.md" + ] + }, + { + "id": "own-agents-porting", + "title": "Port mods with your own AI agent", + "status": "next", + "area": "ai", + "plain": "You can port a mod to Horizon with your own AI agent on your own computer. Horizon documents how and gives you the tools, but provides no hosted agents.", + "dev": "A player runs their own agent locally with `horizon-ctl` and the MCP servers (docs/agent-control.md), `cargo horizon-mod`, `horizon-port-pack` and the parity harnesses as the check. Horizon writes the guide (website/docs/agents.md, docs/modpacks.md) and runs no agents, accounts or keys for players. The No AI switch turns all of it off.", + "links": [ + "website/docs/agents.md", + "docs/modpacks.md", + "docs/agent-control.md" + ] + }, + { + "id": "x-account", + "title": "Horizon on X (coming soon)", + "status": "next", + "area": "community", + "plain": "Coming soon: an official Horizon account on X, used to announce releases and the weekly summary.", + "dev": "Kira already posts the roadmap items marked `announce = true` when they ship, and the weekly summary, as soon as the account's credentials exist (roadmap/README.md, 'The journal'). Waiting for the account itself.", + "links": [ + "roadmap/README.md" + ] + }, + { + "id": "agents-app", + "title": "Pipeline protection and a GitHub App for agents", + "status": "next", + "area": "project", + "plain": "Safer releases: changes to the release machinery need the owner's review, and AI agents use a smaller identity of their own instead of a personal account.", + "dev": "`.github/CODEOWNERS` lists the workflows, the release and signing tools, the update-checking code and the installers (#1015); the branch protection that makes it count waits for the `horizon-agent` GitHub App, which reads and writes code, pull requests and issues but can't change settings, workflows, secrets or members, and whose tokens last an hour. It needs the owner to create the App and switch the protection on (docs/security.md).", + "links": [ + "docs/security.md" + ] + }, + { + "id": "signed-installers", + "title": "Signed updates everywhere, installers included", + "status": "next", + "area": "project", + "plain": "Every download Horizon makes is checked against the project's signature, including the first install with install.sh and install.ps1.", + "dev": "Releases carry signed checksums, and the launcher and `horizon-server update` refuse anything unsigned (#1011, #1014). Left: the first install through `install.sh` and `install.ps1` still trusts only the checksum GitHub publishes, because verifying Ed25519 signatures needs OpenSSL 3, which macOS doesn't ship (docs/security.md).", + "links": [ + "docs/security.md" + ] + }, { "id": "ai-companion", "title": "An optional AI teammate", "status": "later", - "area": "extras", - "plain": "An AI teammate that plays beside you and that you can talk to. It is off unless you turn it on, and the No AI switch hides it completely.", - "dev": "Draft pull requests: the companion (#1068), its safety modes and their evaluation (#1069), and a route that finishes the game (#1070). Local models first; it ships only once the safety evaluation passes.", + "area": "ai", + "plain": "An optional AI teammate you can talk to by voice. It plays beside you, helps or finishes the game with you, runs on your computer first, and exists only if you turn it on. The No AI switch hides it.", + "dev": "Draft pull requests: the companion (#1068), its safety modes and their evaluation (#1069) and a route that finishes the game (#1070). In the drafts: a companion that follows, fights, mines, crafts, sleeps and respawns; chat and push-to-talk voice, with speech recognition and a choice of voices; presets (very strong, helper, beat the game); a guard model with Clean, Kid and Salty modes; perception levels from Fair to All-knowing; hardware detection with a simple mode and an Advanced tab; local models first, cloud keys optional. The No AI switch is already in the game. Left: the speedrun route doesn't finish the game reliably yet, voices for more languages, and the safety evaluation, which must pass before it ships.", "links": [ "#1068", "#1069", @@ -338,15 +732,269 @@ }, { "id": "new-versions", - "title": "Following new Minecraft versions", + "title": "Every Minecraft version, prepared automatically", "status": "later", "area": "gameplay", - "plain": "When Mojang releases a new version of the game, Horizon follows it, and every system is checked against the new version again.", - "dev": "The port pipeline detects a release, diffs its classes against 26.3 and writes a port report (docs/version-ports.md); the harnesses then run against the new version.", + "plain": "Every past and future Minecraft version prepared for Horizon automatically. When Mojang releases one, Horizon follows it, and every system is checked against it again.", + "dev": "The port pipeline (docs/version-ports.md, `tools/version_port.py`) detects a release, prepares it, diffs its classes against the pinned 26.3, regenerates the data-driven tables, runs every parity harness on it and writes a port report that becomes issues. The goal is as little human work as possible for any version, past or future. Detection and the first slices are merged; the scheduled agent that runs it every day isn't installed yet. Two versions in one build are out of scope.", "links": [ "docs/version-ports.md" ] }, + { + "id": "exact-far-terrain", + "title": "Exact far terrain from real chunks", + "status": "later", + "area": "performance", + "plain": "Far terrain drawn from the world you really built and explored, caves and builds included, not only a preview from the seed.", + "dev": "Keep a 3D mip store on disk built from real chunks, drawn by the GPU beside the seed preview for areas nobody has visited. Written from scratch and measured before it is kept. Builds on Far Detail and the far-terrain levels of detail already shipped; the open work on them is #718.", + "links": [ + "#718" + ] + }, + { + "id": "older-jars", + "title": "Older Fabric and Forge mods", + "status": "later", + "area": "modpacks", + "plain": "Recognise mods built for versions before 26.1 in the mod browser, so they can be checked and carried over like newer ones.", + "dev": "A mapping table from intermediary and SRG names so pre-26.1 jars can be classified, and a rename for mod ids with dashes (`fabric-api`); the port step then treats them like newer jars (docs/mod-browser.md).", + "links": [ + "docs/mod-browser.md" + ] + }, + { + "id": "modpack-own-packs", + "title": "A modpack's own resource and shader packs", + "status": "later", + "area": "modpacks", + "plain": "Importing a modpack also brings the resource packs and shader packs it ships with, where Horizon can use them.", + "dev": "Porting a pack carries data, assets and worldgen today; the resource packs and shader packs bundled with it are left out. Resource packs can be carried as they are; shader packs go through the converter (docs/modpacks.md).", + "links": [ + "docs/modpacks.md" + ] + }, + { + "id": "whole-modpacks", + "title": "Whole modpacks, such as ATM10, rewritten", + "status": "later", + "area": "modpacks", + "plain": "Rewriting a complete big modpack for Horizon so it runs far better than the original. Not a priority now: the work above comes first.", + "dev": "The project's first goal for modpacks: support them ten times better than vanilla and port any of them. It was set aside in favour of compatibility through the browser. What exists: ATM10's world generates and 42 of its 496 mods are carried at least in part (docs/modpacks.md). It depends on 'Mods with code' and the native mod API.", + "links": [ + "docs/modpacks.md" + ] + }, + { + "id": "java-mod-bridge", + "title": "Running Java mods that aren't rewritten", + "status": "later", + "area": "modpacks", + "plain": "Run Java mods that nobody has rewritten for Horizon yet, through a bridge or a converter. Hard and not started: the Convert button and native rewrites come first.", + "dev": "Earlier research (#527, closed) found that Java mods can't simply run, because they call Minecraft's own classes. Options to look at again: a jar-to-WebAssembly backend, or a bridge for simple Fabric mods. Whatever is chosen has to run inside the same sandbox and capability checks as native mods.", + "links": [ + "#527" + ] + }, + { + "id": "alt-accounts", + "title": "Ely.by and other sign-in providers", + "status": "later", + "area": "servers", + "plain": "Sign in with other account providers, such as Ely.by or any Yggdrasil-compatible one, beside Microsoft.", + "dev": "Issue #1270 asks for Ely.by and Yggdrasil-compatible servers with an account selector in the launcher. It builds on the Microsoft sign-in (docs/microsoft-account.md).", + "links": [ + "#1270", + "docs/microsoft-account.md" + ] + }, + { + "id": "friends-online-mode", + "title": "Online-mode checks for Play with Friends", + "status": "later", + "area": "servers", + "plain": "Friends who join through an invite code could prove who they are with their Microsoft account, as on online-mode servers.", + "dev": "Play with Friends joins over an encrypted tunnel with an invite code (docs/play-with-friends.md, 'Identity'). Online-mode checks against Mojang's session servers can be added on top of the same tunnel; they need Microsoft sign-in first, which is coming soon. Play with Friends stays self-hosted between players, with no relay run by the project.", + "links": [ + "docs/play-with-friends.md" + ] + }, + { + "id": "orbital-laser", + "title": "Orbital Laser on the normal game", + "status": "later", + "area": "mods", + "plain": "The community mod Orbital Laser, made for a fork of Horizon, running on the normal game and listed in the registry as the showcase of the mod API.", + "dev": "A pull request on https://github.com/Stink-O/orbital-laser (MIT) once the input, ui, render and world capabilities exist (see 'Screens, input and drawing for mods'): today the mod needs them through its author's fork. Tested in the game, and listed in the registry.", + "links": [ + "https://github.com/Stink-O/orbital-laser" + ] + }, + { + "id": "client-mod-natives", + "title": "Native versions of popular client mods", + "status": "later", + "area": "mods", + "plain": "Built-in versions of the client mods players ask for: block info on hover, a recipe viewer like JEI, a world map and schematic overlays.", + "dev": "Done as extras: a mod list and menu, a minimap and a zoom (docs/extras.md). Left: block info tooltips, a recipe viewer (a contributor offered to port JEI if it becomes a native feature), a world map and a schematic overlay. They need the read-only interfaces `inventory-read`, `recipes`, `world-read` and `render` in the mod API, or they go into the game directly.", + "links": [ + "docs/extras.md", + "docs/mods.md" + ] + }, + { + "id": "create-physics", + "title": "Create-style physics as a native mod", + "status": "later", + "area": "mods", + "plain": "Contraptions and physics like the Create and Aeronautics mods, as a mod for Horizon rather than part of the base game.", + "dev": "Issue #805 (porting QOL mods) closed with this answer: it would come as a native Horizon mod on the mod API. It needs server mods, entities and rendering first.", + "links": [ + "#805" + ] + }, + { + "id": "proximity-voice", + "title": "Proximity voice chat as a native mod", + "status": "later", + "area": "mods", + "plain": "Hear nearby players talk, as with the voice chat mods of other Minecraft versions, as a Horizon mod.", + "dev": "Needs server mods, mod networking and an audio interface in the mod API before anything can be built (docs/api/mods.md).", + "links": [ + "docs/api/mods.md" + ] + }, + { + "id": "light-default-pack", + "title": "A light default shader pack", + "status": "later", + "area": "rendering", + "plain": "A small built-in look (tone mapping, colour grade, a cheap vignette) that costs almost nothing, for players who want a nicer picture without a heavy pack.", + "dev": "An opt-in, composite-only pack on the native shader API, under about 0.3 ms at 1080p, shown on the Shader Packs page. Pack option sliders and profiles, and a Failed row with the full error, belong with it.", + "links": [ + "docs/shaders.md" + ] + }, + { + "id": "ray-tracing", + "title": "Optional ray tracing", + "status": "later", + "area": "rendering", + "plain": "An optional ray-tracing mode for graphics cards that support it (RTX and recent AMD): shadows, water reflections and global lighting on top of the vanilla look.", + "dev": "A spike with ray-traced sun shadows, off by default, is a draft (#1180). Reflections, global illumination, Apple M3 and newer if it is cheap, and a performance report per card come after.", + "links": [ + "#1180" + ] + }, + { + "id": "hdr", + "title": "HDR monitors", + "status": "later", + "area": "rendering", + "plain": "Output for HDR monitors: brighter highlights and richer colour where your screen supports it.", + "dev": "Issue #1441. Needs a surface format and a tone-mapping path per backend, and tests on real HDR screens on Windows, macOS and Linux.", + "links": [ + "#1441" + ] + }, + { + "id": "msaa-fullscreen", + "title": "MSAA, anisotropic filtering, exclusive fullscreen", + "status": "later", + "area": "rendering", + "plain": "More graphics options: multisample anti-aliasing, anisotropic filtering and an exclusive fullscreen mode. Today Horizon has FXAA and borderless fullscreen.", + "dev": "Not implemented (docs/graphics.md). Anisotropic filtering needs linear magnification in wgpu, which blurs close blocks, so close-up sharpness has to be kept first; MSAA would sit beside FXAA; exclusive fullscreen where the backend supports it.", + "links": [ + "docs/graphics.md" + ] + }, + { + "id": "video-studio", + "title": "A complete video studio inside the game", + "status": "later", + "area": "extras", + "plain": "Record, replay and edit videos without leaving the game: record in a keypress, replay a session, cut and finish the video. One of Horizon's original goals.", + "dev": "Three stages. Recording: capture gameplay in a keypress with an encoder thread and little frame-time cost. Replay: a replay mode for recorded sessions. Editing: cuts, camera paths, titles and export. None of it exists yet; the agent-control socket (docs/agent-control.md) already lets tools drive the game for scripted shots.", + "links": [ + "docs/agent-control.md" + ] + }, + { + "id": "controllers", + "title": "Controllers: tested on real pads, and in the launcher", + "status": "later", + "area": "gameplay", + "plain": "Make controller support solid: tested with real PlayStation and Xbox pads and a Steam Deck, and usable in the launcher too.", + "dev": "The game side is merged (docs/controllers.md): pads read with gilrs, menus, rumble, glyphs, remapping and DualSense adaptive triggers, lightbar and haptics, but not yet confirmed on real pads or a Steam Deck (DualSense haptics stay behind a switch until then). Left: a test pass on real hardware, and the same focus navigation in the GPUI launcher.", + "links": [ + "docs/controllers.md" + ] + }, + { + "id": "debug-screen", + "title": "The full debug screen", + "status": "later", + "area": "gameplay", + "plain": "Vanilla's complete F3 screen: debug options, a profiler chart, ten-second captures and an atlas dump.", + "dev": "Today F3 shows position, chunk, light, biome and FPS (docs/native-client.md). Vanilla also has F3+F6 debug options with profiles, F3+1 the profiler pie, F3+L a ten-second capture and F3+S the atlas dump (#562 asked for the F3 editor).", + "links": [ + "#562", + "docs/native-client.md" + ] + }, + { + "id": "lean-installs", + "title": "Lean installs that use less disk", + "status": "later", + "area": "launcher", + "plain": "Smaller installs: Horizon keeps one shared copy of the prepared game files and removes what only the preparation needed.", + "dev": "A shared resource store keyed by the preparer's digest, no prepare intermediates left behind, the current and the previous version kept, and an automatic clean-up. Not started." + }, + { + "id": "linux-arm64", + "title": "Horizon on Linux ARM64", + "status": "later", + "area": "platforms", + "plain": "Game and launcher builds for 64-bit ARM Linux computers.", + "dev": "Issue #1709: aarch64 builds of the game and the launcher in the release workflow, tested on a real ARM machine with its Vulkan driver. The server build already drops audio and controllers (`--no-default-features`).", + "links": [ + "#1709" + ] + }, + { + "id": "opengl-fallback", + "title": "OpenGL for old computers", + "status": "shipped", + "area": "platforms", + "plain": "Horizon now starts on old PCs whose graphics card has no Vulkan or DirectX 12 driver: it switches to OpenGL by itself, and Options > Graphics can pick OpenGL by hand.", + "dev": "wgpu's GL backend (WGL on Windows, EGL on Linux) as the last fallback after Vulkan and DirectX 12 (#1917), sprite arrays and CPU sprite animation for drivers without compute (#1920), no indirect draws below GL 4.3 (#1936). Minimum OpenGL 3.3 core. Shader packs don't run on it yet.", + "links": [ + "#1917", + "#1920", + "#1936" + ], + "shipped_in": "v0.2.63" + }, + { + "id": "phones-and-browser", + "title": "Android, iOS and the browser", + "status": "later", + "area": "platforms", + "plain": "Versions of Horizon for Android, iOS and web browsers. Nothing official yet; contributors have started the first pieces.", + "dev": "No official build. Android was first closed as not planned, with touch controls, a mobile interface, packaging and loading the player's own assets all missing; the roadmap now includes it, iOS and a WebAssembly build for the browser as long-term goals. First pieces from contributors: `horizon-dualsense` compiling on iOS (#1813) and helper-thread limits on Android (#1751). Preparing without Java (#1750) matters here too. Horizon ships none of Mojang's files on any platform.", + "links": [ + "#1813", + "#1751" + ] + }, + { + "id": "video-series", + "title": "A video series about Horizon", + "status": "later", + "area": "community", + "plain": "A series of videos about how Horizon is made and what is new, with each upload announced on Discord.", + "dev": "Kira already shows new uploads in a read-only Discord channel (done). The series follows each stage of the project: a script, edits, titles and a thumbnail per video." + }, { "id": "benchmark-your-pc", "title": "Benchmark your computer", @@ -557,6 +1205,37 @@ "docs/hosting.md" ], "shipped_in": "launcher-v0.15.0" + }, + { + "id": "cli-server-install", + "title": "A server you install with one command", + "status": "next", + "area": "servers", + "plain": "Install and run a Horizon dedicated server from a terminal with a single command, no launcher needed, for VPSes and headless machines.", + "dev": "A player's idea (#horizon-ideas). `horizon-server` already runs headless; left: an install script (Linux/macOS shell, Windows PowerShell) that fetches the signed release, checks it, asks for or points at the player's own game files and starts the server, documented in docs/hosting.md.", + "links": [ + "docs/hosting.md" + ] + }, + { + "id": "launcher-look-menus", + "title": "Game menus in the launcher's style", + "status": "later", + "area": "extras", + "plain": "An optional look for the game's own menus and options that matches the Horizon launcher. Off by default: the vanilla menus stay as they are.", + "dev": "A player's idea (#horizon-ideas). A built-in resource pack or a Horizon setting that restyles the title, pause and options screens with the launcher's design tokens (crates/horizon-theme), following docs/design-system.md; off by default because it changes the vanilla look.", + "links": [ + "docs/design-system.md" + ] + }, + { + "id": "linux-packages", + "title": "AppImage and a Nix flake", + "status": "later", + "area": "platforms", + "plain": "Install Horizon on any Linux distribution as an AppImage, and on NixOS through a Nix flake.", + "dev": "A player's idea (#horizon-ideas). An AppImage of the launcher and game from the release workflow, and a flake.nix packaging the release binaries with their runtime libraries (Vulkan loader, X11/Wayland, ALSA), the way horizon-parity's NixOS setup runs them today.", + "links": [] } ], "journal": [ diff --git a/website/docs/apis.md b/website/docs/apis.md index f948b1d54..fbed5fe53 100644 --- a/website/docs/apis.md +++ b/website/docs/apis.md @@ -1,4 +1,4 @@ -# Horizon's APIs +# APIs at a glance Horizon is built to be built on. Mods, shaders, agents, server panels, launchers' scripts and tools all get a documented API with a version, a stability level and a reference generated from the code, so it never says something the game doesn't do. @@ -13,7 +13,7 @@ Horizon is built to be built on. Mods, shaders, agents, server panels, launchers | **Launcher** | your agent, pack makers | `horizon-launcher`'s 46 commands and its MCP server | preview | | **Data formats** | tools that read Horizon's files | benchmark reports, telemetry, settings, release checksums; worlds are vanilla's format | per format | -The full pages, one per surface, are in the repository: [docs/api](https://github.com/ludovic111/horizon/tree/main/docs/api). Mods for players: [Mods](/docs/mods). Agents: [Control Horizon with your AI agent](/docs/agents). Hosting: [Host a server](/docs/hosting). +The full pages, one per surface, with their references: [the API portal](/docs/api), starting with [the Mod API](/docs/api/mods). Mods for players: [Mods](/docs/mods). Agents: [Control Horizon with your AI agent](/docs/agents). Hosting: [Host a server](/docs/hosting). ## What the levels mean diff --git a/website/docs/content-mods.md b/website/docs/content-mods.md new file mode 100644 index 000000000..9504bceb6 --- /dev/null +++ b/website/docs/content-mods.md @@ -0,0 +1,124 @@ +# Content mods + +A content mod adds blocks and items to the game with data files only: no Rust, no compiler. Each new block or item copies a vanilla one (its template) and changes what differs, and comes with its recipes, loot table, tags, model, textures and name, in the same formats as vanilla's data packs and resource packs. + +## What a content mod is + +A folder (while you work on it) or a `.hmod` file (to share it) holding a manifest, `assets/` and `data/`. This is the SDK's [`ruby-content`](https://github.com/ludovic111/horizon/tree/main/crates/horizon-mod-sdk/examples/ruby-content) example, a ruby gem and a block of ruby: + +``` +ruby/ +├── horizon-mod.toml +├── data/ruby/horizon/block/ruby_block.json the block: a template and what differs +├── data/ruby/horizon/item/ruby.json the item +├── data/ruby/recipe/ruby_block.json recipes, as in a data pack +├── data/ruby/recipe/ruby_from_ruby_block.json +├── data/ruby/loot_table/blocks/ruby_block.json what the block drops +├── data/minecraft/tags/block/mineable/pickaxe.json added to vanilla's tags +├── data/minecraft/tags/block/needs_iron_tool.json +├── assets/ruby/blockstates/ruby_block.json models and textures, as in a resource pack +├── assets/ruby/models/block/ruby_block.json +├── assets/ruby/models/item/ruby.json +├── assets/ruby/items/ruby.json +├── assets/ruby/items/ruby_block.json +├── assets/ruby/textures/block/ruby_block.png +├── assets/ruby/textures/item/ruby.png +└── assets/ruby/lang/en_us.json the names players see +``` + +Its manifest asks for API `0.2` and the `registries` capability. It has no code, so it needs no `Cargo.toml` and no `mod.wasm`: + +```toml +id = "ruby" +name = "Ruby" +version = "0.1.0" +description = "A ruby gem and a block of ruby" +authors = ["You"] +api = "0.2" +side = "both" +capabilities = ["registries"] +``` + +## Blocks + +A block is a file `data//horizon/block/.json`. It becomes the block `:`, with the template's behaviour, states and shapes: + +```json +{ + "template": "minecraft:iron_block", + "properties": { "hardness": 4.0, "light": 3, "sounds": "minecraft:amethyst_block", "map_color": 28 } +} +``` + +| Key | Meaning | +| --- | --- | +| `template` | The vanilla block it copies, such as `minecraft:iron_block`, `minecraft:oak_door` or `minecraft:diamond_ore`. A door copies a door's states and shape, an ore an ore's. | +| `properties.hardness` | How long it takes to mine (vanilla's destroy time; `-1` cannot be broken). | +| `properties.explosion_resistance` | How well it stands up to explosions. | +| `properties.light` | The light it gives, 0 to 15. | +| `properties.requires_tool` | `true`: it drops nothing unless mined with the right tool (the tags say which). | +| `properties.friction` | How slippery it is (ice is `0.98`). | +| `properties.map_color` | Its colour on maps, a vanilla map colour id from 0 to 61. | +| `properties.sounds` | A vanilla block whose sounds it uses. | +| `item` | `false` for a block with no item. By default the block gets an item of the same id. | + +Every property you leave out is the template's. + +## Items + +An item is a file `data//horizon/item/.json`, copying a vanilla item: + +```json +{ "template": "minecraft:diamond", "rarity": "uncommon" } +``` + +It may change `max_stack_size` (1 to 99) and `rarity` (`common`, `uncommon`, `rare` or `epic`, the colour of its name). + +## Recipes, loot, tags, models and names + +Everything else uses vanilla 26.3's own formats, so the [Minecraft Wiki's](https://minecraft.wiki/w/Data_pack) pages on data packs and resource packs apply: + +- **Recipes** in `data//recipe/`, **loot tables** in `data//loot_table/`. +- **Tags**: your own in `data//tags/`. To add to vanilla's (which pickaxe mines the block, for example), put a tag file in `data/minecraft/tags/`; to add to the convention tags NeoForge and Fabric mods share, in `data/c/tags/`. They add to the tag as a data pack does; `"replace": true` drops what came before it, and the log says so. +- **Block states, models, item models and textures** in `assets//`, as in a resource pack. Models can use vanilla's parents, such as `minecraft:block/cube_all`. +- **Names** in `assets//lang/en_us.json`: `"block.ruby.ruby_block": "Block of Ruby"`, `"item.ruby.ruby": "Ruby"`. + +**Structures.** A content mod can also carry structures, template pools and structure sets in its namespace, and add its own pieces to vanilla's villages or to another mod's pools. The [mod design page](https://github.com/ludovic111/horizon/blob/main/docs/mods.md#content-mods-api-02) has the details. + +## Namespaces + +Your files go under your mod's id (`assets/ruby/`, `data/ruby/`), plus vanilla's and the convention tags (`data/minecraft/tags/`, `data/c/tags/`). Tags are the only files a mod may put under `minecraft`. + +A mod made from a Java mod whose files use other names lists them in its manifest: `namespaces = ["kaisyn", "towns_and_towers"]`. A listed namespace is the mod's own, as its id is, but blocks and items are still declared under `data//horizon/`. `minecraft`, `c` and `horizon` can't be listed, and two mods that are on can't own the same namespace: the game keeps the first and logs the clash. + +## Limits + +- A mod's files use vanilla's resource-name characters: at most 65,536 files, 16 MiB each and 512 MiB in all. +- A file the game can't read (a block model that doesn't parse, a recipe with a typo) is logged and skipped, as vanilla does: the rest of the mod still loads. Read the log while you work. +- Unknown keys in a block or item file are refused, and the mod stays off with the reason on its row. +- Content mods work in single player today. Playing them together on a server is planned. + +## Start one + +```sh +cargo horizon-mod new my-blocks --template content +``` + +makes a content mod with one block, Glowing Amethyst: an amethyst block that gives light, with its block state, model (using vanilla's amethyst texture), item model, name, loot table, a recipe (an amethyst block and glowstone dust) and the pickaxe tag. Rename and copy its files to add your own. + +## Try it and share it + +While you work, give the game the folder as a dev mod ([how](/docs/make-a-mod#see-it-in-the-game)): `--dev-mod "/path/to/ruby"`. The first start with a new set of mods takes a little longer while the game adds their content to its tables; after a change to the files, start the game again. + +To share it, build the `.hmod` in the mod's folder: + +```sh +cargo horizon-mod build +cargo horizon-mod check target/horizon-mod/ruby.hmod +``` + +A folder without a `Cargo.toml` is a content mod: `build` checks its files as the game will and packs the manifest, `assets/` and `data/` into `target/horizon-mod/.hmod`, with no compiler involved. `check` reads it as the registry does and says "no code: content only, nothing to run". Players put that file in their `horizon-mods` folder and turn it on in **Options, Mods...**, and you can [publish it](/docs/publish-a-mod) like any mod. + +A mod can have both: a `Cargo.toml` with code, and `assets/` and `data/` with content. `build` packs both. + +Next: [test it](/docs/test-a-mod). diff --git a/website/docs/index.md b/website/docs/index.md index 2b16c4db9..24f969840 100644 --- a/website/docs/index.md +++ b/website/docs/index.md @@ -30,4 +30,5 @@ There is no build for Intel Macs, 32-bit systems, or ARM Linux and Windows. The - **Playing:** [install and first launch](/docs/install), [settings worth knowing](/docs/settings), [multiplayer](/docs/multiplayer) and [what to do when something breaks](/docs/troubleshooting). - **Hosting:** [host a server](/docs/hosting), from the launcher or the command line. - **Going further:** [modpacks](/docs/modpacks), [mods and data packs](/docs/mods) and [performance](/docs/performance). +- **Making mods:** [make a mod](/docs/make-a-mod), from the first line of code to the registry, and the [Mod API](/docs/api/mods). - **Helping out:** [contribute](/docs/contributing) and the [FAQ](/docs/faq). diff --git a/website/docs/make-a-mod.md b/website/docs/make-a-mod.md new file mode 100644 index 000000000..89f2d8bcd --- /dev/null +++ b/website/docs/make-a-mod.md @@ -0,0 +1,88 @@ +# Make a mod + +This guide takes you from nothing to a mod that other players can install from [playhorizon.dev/mods](/mods). You write the mod in Rust. It is compiled to WebAssembly and runs in a sandbox, so a player can see what it may do before turning it on. You need no Minecraft modding experience. + +## The path + +1. **Set up** (this page): install Rust and `cargo horizon-mod`, make your first mod and see it in the game. +2. **[The manifest and capabilities](/docs/mod-manifest)**: the file that names your mod and lists what it may do. +3. **[Write the code](/docs/mod-code)**: the hooks the game calls, what your code can call, and a worked example. +4. **[Content mods](/docs/content-mods)**: blocks, items, recipes and textures from data files, with no code at all. +5. **[Test it](/docs/test-a-mod)**: unit tests, the game's dev mode and the registry's own checks. +6. **[Publish it](/docs/publish-a-mod)**: package, sign and submit it to the registry. + +The [Mod API](/docs/api/mods) lists everything a mod can do today, and its [reference](/docs/api/mods-reference) has every function and type. + +## What a mod can do today + +- **Client mods** run in the player's game. They read what the F3 screen shows (position, facing, health, food, dimension, the time), draw a text panel on the screen, show chat lines only that player sees, add chat commands, and keep small files of their own. They work in single player and on any server, and a server cannot see them. +- **Content mods** add blocks and items copied from vanilla templates, with their recipes, loot tables, tags, models, textures and names. They need no code. +- **Server mods** (rules, world events, server commands) are being built. The [Mod API](/docs/api/mods#not-there-yet) says what comes next. + +A Horizon mod is not a Java mod. Fabric, NeoForge and Forge jars don't run in Horizon, and Horizon mods don't run in Java Minecraft. + +## Install the tools + +You need [Rust](https://rustup.rs) (the stable toolchain), its WebAssembly target and `cargo horizon-mod`, the tool that makes, builds and checks mods. On Windows, macOS or Linux: + +```sh +rustup target add wasm32-unknown-unknown +cargo install --git https://github.com/ludovic111/horizon cargo-horizon-mod +``` + +The second command builds the tool from source, which takes a few minutes the first time. Run it again later to update the tool. + +You also need Horizon itself, installed with the launcher ([Install](/docs/install)), and prepared at least once so the game has its files. + +## Make your first mod + +```sh +cargo horizon-mod new my-mod +cd my-mod +``` + +`new` makes a folder with a working mod in it: a `/hello` command that answers in chat. `--name "My Mod"` sets the name players see, and `--template content` makes a [content mod](/docs/content-mods) instead, with no code. The folder holds: + +``` +my-mod/ +├── Cargo.toml the Rust crate (a cdylib depending on horizon-mod-sdk) +├── horizon-mod.toml the manifest: id, name, version, what the mod may do +├── .gitignore +└── src/lib.rs the code +``` + +Build it: + +```sh +cargo horizon-mod build +``` + +This compiles the crate, checks that the code uses nothing its manifest doesn't declare, and writes `target/horizon-mod/my_mod.hmod`. A `.hmod` is a zip file holding the manifest and the compiled code (`mod.wasm`). It is the one file you give players: the same file runs on Windows, macOS and Linux. + +## See it in the game + +A **dev mod** is a mod the game loads from a path you give it. It is always on, and the game looks at its file once a second: build again and the game reloads it, saying "Mods reloaded" in chat. Its memory starts over; what it saved to its storage stays. + +**From the launcher.** Open the launcher's **Settings** and put this in **Game arguments**, with the full path to your file: + +``` +--dev-mod "/path/to/my-mod/target/horizon-mod/my_mod.hmod" +``` + +Then press **Play**, open a world and type `/hello world` in chat. Leave the setting there while you work on the mod, and clear it when you are done. The launcher's command line does the same: `horizon-launcher play --arg --dev-mod --arg /path/to/my_mod.hmod`. + +**With `cargo horizon-mod run`.** If you have a `horizon-client` you built from source, `cargo horizon-mod run` builds the mod and starts that game with it. It looks for `horizon-client` on your `PATH`, or takes `--game ` or the `HORIZON_CLIENT` variable; anything after `--` goes to the game: + +```sh +cargo horizon-mod run --game ../horizon/target/fast/horizon-client -- --resources ../horizon/.local/horizon-client/26.3 +``` + +Either way, edit `src/lib.rs`, run `cargo horizon-mod build`, and the game picks up the new file. + +## Where things go + +- **Your mod's messages** go to the game log as `[mod my_mod] …` (the `logs/latest.log` file in the game folder), and the game prints them in its terminal too. +- **Mods players install** go in the `horizon-mods` folder of the game folder (Linux `~/.local/share/horizon`, macOS `~/Library/Application Support/horizon`, Windows `%APPDATA%\horizon`). **Options, Mods...** in the game lists them, each with what it may do and a switch. They start off. +- **A mod that fails** to load, or that breaks while running, is switched off with the reason in red on its row of the Mods screen. It never takes the game down with it. + +Next: [the manifest and capabilities](/docs/mod-manifest). diff --git a/website/docs/mod-code.md b/website/docs/mod-code.md new file mode 100644 index 000000000..42cc6f04b --- /dev/null +++ b/website/docs/mod-code.md @@ -0,0 +1,190 @@ +# Write the code + +A mod is a Rust type that implements the `Mod` trait from `horizon-mod-sdk`. The game calls its methods, the hooks, at fixed points, and the mod calls back into the game through the parts of the API its capabilities allow. This page explains both, then builds a complete mod step by step. + +## The shape of a mod + +```rust +use horizon_mod_sdk::{Mod, Registration, chat}; + +struct Hello { + greeted: u32, +} + +impl Mod for Hello { + fn init() -> Result<(Self, Registration), String> { + Ok((Hello { greeted: 0 }, Registration::new().command("hello"))) + } + + fn command(&mut self, _name: &str, args: &str) -> Result, String> { + self.greeted += 1; + chat::show(&format!("Hello {args}! ({} so far)", self.greeted)); + Ok(None) + } +} + +horizon_mod_sdk::export_mod!(Hello); +``` + +- **The state lives in your type.** `init` creates it, and every hook gets `&mut self`. +- **`export_mod!`** connects the type to the game. A mod has exactly one. +- **The game calls only the hooks you ask for.** `init` returns a `Registration` that lists them. The other methods keep their default, empty bodies, and a hook you didn't ask for costs nothing. + +## Hooks + +| Hook | Ask for it with | When the game calls it | +| --- | --- | --- | +| `init() -> Result<(Self, Registration), String>` | always called | Once, when the mod loads. An `Err` keeps the mod off and shows the message on its row. | +| `tick(&mut self, tick: u64)` | `.on_tick()` | Every game tick while the player is in a world: 20 times a second. | +| `frame(&mut self, seconds: f32)` | `.on_frame()` | Every frame the game draws, with the time since the last one. | +| `command(&mut self, name: &str, args: &str)` | `.command("name")` | When the player types `/name …` in chat. `args` is everything after the name. | +| `chat(&mut self, text: &str)` | `.on_chat()` | For each chat line the player receives, as plain text. | + +**Commands.** Ask for as many as you like with `.command("…")`, without the slash. The chat box colours them and completes them with Tab, like vanilla's commands. Vanilla's names win: a mod can't take `/give`, and when two mods ask for the same name, the first one loaded keeps it. The game logs both cases. The return value says what happens next: + +- `Ok(None)`: nothing more. Use it when the mod already answered with `chat::show`. +- `Ok(Some(text))`: the game shows `text` to the player. +- `Err(text)`: the game shows `text` in red, as a failed command. + +**Prefer ticks to frames.** A tick happens 20 times a second whatever the frame rate. The HUD panel is kept by the game and drawn every frame for you, so most mods never need `frame`. + +## What your code can call + +Each module of the SDK is one part of the API, and each needs its capability in the manifest ([the list](/docs/mod-manifest#capabilities)): + +| Module | Capability | Calls | +| --- | --- | --- | +| `game` | `game-read` | `get_player() -> Option` (`x`, `y`, `z`, `yaw`, `pitch`, `health`, `food`, `dimension`), `get_clock() -> Option` (`game_time`, `day_time`). `None` outside a world. | +| `chat` | `chat` | `show(text)` | +| `hud` | `hud` | `set_panel(Corner, &[Line])` (`Line { text, color }`, colour as `0xRRGGBB`; corners `TopLeft`, `TopRight`, `BottomLeft`, `BottomRight`), `clear()` | +| `storage` | `storage` | `get(key) -> Option>`, `set(key, &[u8]) -> Result<(), String>`, `delete(key)` | +| `log` | none | `horizon_mod_sdk::info(text)` and `warn(text)`: lines in the game log | + +What the game reads for you (`game`) is a snapshot taken once a tick, and again before a command runs. It never changes in the middle of a call. Everything else is plain Rust: your own types, maths and text, and the crates you add to `Cargo.toml`, as long as they build for `wasm32-unknown-unknown` without the operating system (no files, threads, network or clock). + +## A worked example: Playtime + +This mod counts how long you play, shows it in the top-right corner, and keeps the count when you quit. `/playtime` says it in chat; `/playtime hide`, `show` and `reset` do what they say. It uses three capabilities (`hud`, `chat`, `storage`) and two hooks (`tick`, `command`). + +**1. Make the mod.** + +```sh +cargo horizon-mod new playtime --name "Playtime" +cd playtime +``` + +**2. Declare what it may do.** In `horizon-mod.toml`, set the description and the capabilities: + +```toml +description = "How long you have played, in a corner of the screen" +capabilities = ["hud", "chat", "storage"] +``` + +**3. The state and a helper.** Replace `src/lib.rs` with the code below, in parts. First the imports, the state and a function that turns ticks into hours and minutes. It touches no API, so an ordinary unit test can check it (step 7). + +```rust +use horizon_mod_sdk::{ + Mod, Registration, chat, + hud::{self, Corner, Line}, + storage, +}; + +/// Ticks played, kept across sessions in the mod's storage. +struct Playtime { + ticks: u64, + shown: bool, +} + +/// `1h 05m` from a number of ticks (20 a second). +fn format(ticks: u64) -> String { + let minutes = ticks / 20 / 60; + format!("{}h {:02}m", minutes / 60, minutes % 60) +} +``` + +**4. Saving and drawing.** `storage::set` writes bytes under a key of your choice; it can fail when the mod is over its quota, so the error goes to the log. The panel is set once and the game draws it until it changes, so redraw only when there is something new to show. + +```rust +impl Playtime { + fn save(&self) { + if let Err(e) = storage::set("ticks", &self.ticks.to_le_bytes()) { + horizon_mod_sdk::warn(&format!("could not save: {e}")); + } + } + + fn draw(&self) { + if self.shown { + let line = Line { text: format!("Played {}", format(self.ticks)), color: 0xFFFF55 }; + hud::set_panel(Corner::TopRight, &[line]); + } else { + hud::clear(); + } + } +} +``` + +**5. The hooks.** `init` reads what an earlier session saved, draws the panel and asks for one command and the tick hook. `tick` counts, and once a minute redraws and saves. `command` reads its arguments. + +```rust +impl Mod for Playtime { + fn init() -> Result<(Self, Registration), String> { + // What an earlier session saved, or 0 the first time. + let ticks = storage::get("ticks") + .and_then(|bytes| bytes.try_into().ok()) + .map_or(0, u64::from_le_bytes); + let state = Playtime { ticks, shown: true }; + state.draw(); + Ok((state, Registration::new().command("playtime").on_tick())) + } + + fn tick(&mut self, _tick: u64) { + self.ticks += 1; + // Once a minute: redraw the panel and save. + if self.ticks % (20 * 60) == 0 { + self.draw(); + self.save(); + } + } + + fn command(&mut self, _name: &str, args: &str) -> Result, String> { + match args.trim() { + "" => chat::show(&format!("You have played {}", format(self.ticks))), + "hide" | "show" => { + self.shown = args.trim() == "show"; + self.draw(); + } + "reset" => { + self.ticks = 0; + self.save(); + self.draw(); + return Ok(Some("Playtime reset".into())); + } + other => return Err(format!("Unknown option {other}: use hide, show or reset")), + } + Ok(None) + } +} + +horizon_mod_sdk::export_mod!(Playtime); +``` + +**6. Try it.** `cargo horizon-mod build`, then start the game with it as a dev mod ([how](/docs/make-a-mod#see-it-in-the-game)). Open a world: "Played 0h 00m" sits in the top-right corner and goes up each minute. Type `/playtime`, then `/playtime hide`. `/playtime foo` answers in red. Quit and come back: the count goes on from where it was. + +**7. Test the helper.** At the end of `src/lib.rs`: + +```rust +#[cfg(test)] +mod tests { + #[test] + fn formats_hours_and_minutes() { + assert_eq!(super::format(0), "0h 00m"); + assert_eq!(super::format(20 * 60 * 65), "1h 05m"); + } +} +``` + +`cargo test` runs it on your computer, with no game. [Test it](/docs/test-a-mod) covers the rest. + +**Where to go from here.** The SDK's examples are complete mods to read: [`coords-hud`](https://github.com/ludovic111/horizon/tree/main/crates/horizon-mod-sdk/examples/coords-hud) (coordinates, facing and the time on the HUD, `/where` and `/coords`) and [`ruby-content`](https://github.com/ludovic111/horizon/tree/main/crates/horizon-mod-sdk/examples/ruby-content) (a content mod). The [reference](/docs/api/mods-reference) lists every type and function, with its limits. + +Next: [content mods](/docs/content-mods), or straight to [testing](/docs/test-a-mod) if your mod has no blocks or items. diff --git a/website/docs/mod-manifest.md b/website/docs/mod-manifest.md new file mode 100644 index 000000000..c0e0e4b51 --- /dev/null +++ b/website/docs/mod-manifest.md @@ -0,0 +1,60 @@ +# The manifest and capabilities + +Every mod has a `horizon-mod.toml` at its root. It names the mod, says which version of the mod API it was made for, and lists its capabilities: what it may do. Players read that list before they turn the mod on, and the game holds the mod to it. + +## The manifest + +```toml +id = "playtime" +name = "Playtime" +version = "0.1.0" +description = "How long you have played, in a corner of the screen" +authors = ["You"] +api = "0.1" +side = "client" +capabilities = ["hud", "chat", "storage"] +``` + +| Key | What it is | +| --- | --- | +| `id` | The mod's id: 1 to 64 characters of `a-z`, `0-9` and `_`. It names the mod's setting, its log lines, its storage folder and, for content, its namespace. Keep it once you have published the mod. | +| `name` | The name players see. | +| `version` | Your mod's version, such as `0.1.0`. Each version you publish needs a new one. | +| `description` | One or two sentences for the Mods screen. | +| `authors` | A list of names. | +| `api` | The mod API line the mod was made for: `"0.1"` for code, `"0.2"` for content. The game refuses a line it doesn't serve, with a message that says so. | +| `side` | `"client"` for a mod that runs in the player's game. Content mods use `"both"`. | +| `capabilities` | What the mod may do: the list below. | +| `namespaces` | Content mods only: extra namespaces the mod's files use (see [Content mods](/docs/content-mods#namespaces)). | + +Unknown keys are refused, so a typo is an error, not a permission that silently does nothing. + +## Capabilities + +A capability is a part of the API the mod may use. Each one shows on the Mods screen as a sentence that starts with "May". Writing to the game log needs no capability. + +| Capability | The player reads | What the code can do | +| --- | --- | --- | +| `game-read` | May see your position, health and the time | `game::get_player()`: position, facing (yaw and pitch), health, food and dimension. `game::get_clock()`: the game time and the time of day. | +| `chat` | May show you chat messages | `chat::show(text)`: a chat line only this player sees, after the mod's name. At most 8 per call into the mod. | +| `hud` | May draw text on your screen | `hud::set_panel(corner, lines)` and `hud::clear()`: a panel of up to 16 coloured lines of 256 characters in one corner. The game keeps drawing it until you change it. | +| `storage` | May save its own settings to disk | `storage::get`, `set` and `delete`: small files in a folder of the mod's own. Up to 64 files and 4 MiB in all, and at most 1 MiB written per call. Keys are 1 to 64 characters of `a-z`, `0-9`, `_`, `.` and `-`. | +| `registries` | May add blocks and items to your worlds | Content mods: blocks and items from the mod's data files. No code. | + +Ask only for what the mod uses: players are more likely to turn on a mod that asks for little. + +**The game checks.** When you build, and again when a game loads the mod, the compiled code's imports are compared with the manifest. A mod whose code calls `chat::show` without `chat` in its capabilities is refused (`uses capability chat, missing from horizon-mod.toml`). The list players read is what the code can do, not a promise. + +## What no mod can do + +A mod runs in a WebAssembly sandbox. Whatever its capabilities: + +- It cannot open files (apart from its own storage), the network, other programs or the clock. +- It sees the game only through the snapshot the game gives it once a tick, and changes it only through the calls above. It cannot patch the game's code. +- It has 64 MiB of memory and a time budget for each call. A mod that runs past its budget, runs out of memory or crashes is switched off, with the reason on its row of the Mods screen. The game goes on. + +## API versions + +The mod API is versioned apart from the game. A mod made for API `0.1` keeps loading on every game that serves `0.1`, and new functions arrive as additions. A change that would break mods comes as a new line, served beside the old one for at least one release cycle. The [stability page](/docs/api/stability) has the whole policy, and the [Mod API's changelog](/docs/api/mods#changelog) what each line added. + +Next: [write the code](/docs/mod-code). diff --git a/website/docs/mods.md b/website/docs/mods.md index 0c125cf73..405c62976 100644 --- a/website/docs/mods.md +++ b/website/docs/mods.md @@ -6,7 +6,7 @@ Horizon has its own mod system, written for Horizon and not compatible with Java Horizon mods are written in Rust, compiled to WebAssembly and run in a sandbox. A mod can only do what it asks for and you allow, and a mod that crashes is switched off without taking the game down. -> **Early.** The mod system is new. There is no store for Horizon mods yet: they are files to put in a folder. The launcher's **Mods** page (see below) finds resource packs and modpacks, not `.hmod` mods. +> **Early.** The mod system is new. Reviewed mods are listed at [playhorizon.dev/mods](/mods), and any `.hmod` file can be put in the folder by hand. **Playing with mods** @@ -22,15 +22,17 @@ Horizon mods are written in Rust, compiled to WebAssembly and run in a sandbox. **Making a mod** +[Make a mod](/docs/make-a-mod) is the full guide: the tools, your first mod in the game, the manifest and capabilities, the code with a worked example, content mods, testing, and publishing on [the registry](/mods). In short: + ```sh rustup target add wasm32-unknown-unknown cargo install --git https://github.com/ludovic111/horizon cargo-horizon-mod cargo horizon-mod new my-mod cd my-mod -cargo horizon-mod run +cargo horizon-mod build ``` -`run` builds the mod and starts the game with it. After an edit, `cargo horizon-mod build` rebuilds it and the running game reloads it. The full guide, with the API, the manifest and the roadmap, is [docs/mods.md](https://github.com/ludovic111/horizon/blob/main/docs/mods.md). Java mod jars don't run in Horizon: their code calls and patches Minecraft's Java classes, and Horizon has none. What carries over is the data and assets inside a jar ([modpacks](https://github.com/ludovic111/horizon/blob/main/docs/modpacks.md)). +The [Mod API](/docs/api/mods) lists what a mod can do today and what comes next, and its [reference](/docs/api/mods-reference) every function. Java mod jars don't run in Horizon: their code calls and patches Minecraft's Java classes, and Horizon has none. What carries over is the data and assets inside a jar ([modpacks](/docs/modpacks)). ## Finding resource packs and modpacks diff --git a/website/docs/nav.json b/website/docs/nav.json index 3bc2ec8d2..76c06dc47 100644 --- a/website/docs/nav.json +++ b/website/docs/nav.json @@ -3,6 +3,8 @@ { "group": "Playing", "pages": ["settings", "multiplayer", "troubleshooting"] }, { "group": "Hosting", "pages": ["hosting"] }, { "group": "Going further", "pages": ["modpacks", "mods", "performance", "agents"] }, - { "group": "Build on Horizon", "pages": ["apis"] }, + { "group": "Make a mod", "pages": ["make-a-mod", "mod-manifest", "mod-code", "content-mods", "test-a-mod", "publish-a-mod"] }, + { "group": "Build on Horizon", "pages": ["apis", "api", "api/stability"] }, + { "group": "API reference", "pages": ["api/mods", "api/mods-reference", "api/mod-index", "api/server", "api/server-reference", "api/agent-control", "api/launcher", "api/data-formats", "api/shaders"] }, { "group": "Project", "pages": ["contributing", "faq"] } ] diff --git a/website/docs/publish-a-mod.md b/website/docs/publish-a-mod.md new file mode 100644 index 000000000..c24a77a2f --- /dev/null +++ b/website/docs/publish-a-mod.md @@ -0,0 +1,57 @@ +# Publish a mod + +Horizon's registry lists mods, modpacks and resource packs made for Horizon. Players find them at [playhorizon.dev/mods](/mods) and on the launcher's Mods page, which checks every file before it installs it. Each project is reviewed by a person before it is published. This page takes your mod from a file on your computer to the registry. + +## Before you submit + +1. **Choose a version.** Set `version` in `horizon-mod.toml` (such as `1.0.0`). Every version you publish needs a new one, and a published file never changes. +2. **Build it for release.** `cargo horizon-mod build` (release is the default; `--debug` is for quick local builds) writes `target/horizon-mod/.hmod`. That file is what you submit. +3. **Check it.** `cargo horizon-mod check target/horizon-mod/.hmod` must pass ([Test a mod](/docs/test-a-mod#check-it-as-the-registry-does)). +4. **Publish the source.** Put the mod's source in a public repository, under a licence that lets players use it (MIT, Apache-2.0, GPL and so on, as an [SPDX](https://spdx.org/licenses/) name). Reviewers read it, and the file must be built from it. + +## Sign it + +Signing is optional and recommended. When your project has a public key, every later version must be signed with the matching secret key, so nobody else can publish an update in your name, even with access to your GitHub account. + +Horizon uses [minisign](https://jedisct1.github.io/minisign/) signatures. Install minisign (`brew install minisign`, `scoop install minisign`, or your Linux distribution's package), then make a key pair once: + +```sh +minisign -G -p horizon-mods.pub -s horizon-mods.key +``` + +Keep `horizon-mods.key` and its password private and backed up: losing it means changing the project's key by hand, through the registry's maintainers. The public key is the last line of `horizon-mods.pub` (it starts with `RW`). + +Sign each file you publish, and check the signature as the registry will: + +```sh +minisign -S -s horizon-mods.key -m target/horizon-mod/playtime.hmod +cargo horizon-mod check target/horizon-mod/playtime.hmod --key "RW…your public key…" --signature target/horizon-mod/playtime.hmod.minisig +``` + +The report then says "signature verified". + +## Submit it + +Open [playhorizon.dev/mods/submit](/mods/submit) and sign in with GitHub. The page reads only your public profile. Then fill in: + +- **The file**: the kind (mod), the `.hmod`, its id (the manifest's `id`), its version, the Minecraft version (26.3) and what changed. +- **Its page**: the name, a one-line summary for the list, a description (Markdown), the authors, the licence, the source address and, optionally, a square PNG icon (at most 256 KiB). +- **Signature**: your public key (for a new project) and the file's `.minisig`. + +**Submit for review** checks the file again and opens a pull request in [ludovic111/horizon-mods](https://github.com/ludovic111/horizon-mods). It adds `mods/.toml`, with you as the project's owner. The repository's checks run the file in the sandbox and post their report on the pull request. + +## Review and publication + +A reviewer reads the pull request, your source and the checks' report. Questions and requests for changes come as comments on the pull request: answer them there, and submit a new version from the form when the file must change. + +When the reviewer merges it, the file becomes a release of horizon-mods within minutes, and the project gets its page at `playhorizon.dev/mods/` and shows in the launcher. The page lists what the mod may do, as the game's Mods screen does, with your source, licence and the file's SHA-256. + +## Updates + +An update is the same form with a new version: it adds the version to your project's entry. The project's owners publish updates without a reviewer's extra look, and if the project has a key, the new file must be signed with it. + +Without the form, you can open the pull request by hand: the [horizon-mods README](https://github.com/ludovic111/horizon-mods) says how. The registry's file formats are in [the mod index reference](/docs/api/mod-index). + +## Reports and takedowns + +Anyone can report a project from its page (**Report it**). A project that does harm is taken down: the launcher then refuses its files and turns installed copies off, telling players why. diff --git a/website/docs/test-a-mod.md b/website/docs/test-a-mod.md new file mode 100644 index 000000000..650810963 --- /dev/null +++ b/website/docs/test-a-mod.md @@ -0,0 +1,59 @@ +# Test a mod + +Three kinds of checks catch most mistakes before players see them: unit tests for your own logic, the game's dev mode for how the mod looks and feels, and `cargo horizon-mod check`, which runs the same checks as the registry. + +## Unit tests for your logic + +Code that doesn't call the game (formatting, maths, parsing a command's arguments) is ordinary Rust. Put it in plain functions and test it with `#[test]`, as the [Playtime example](/docs/mod-code#a-worked-example-playtime) does with its `format` function: + +```sh +cargo test +``` + +The tests run on your computer, with no game and no WebAssembly. Keeping the hooks thin and the logic in functions makes most of a mod testable this way. + +## In the game, as a dev mod + +Start the game with your mod as a dev mod ([how](/docs/make-a-mod#see-it-in-the-game)) and keep it running. After each edit: + +```sh +cargo horizon-mod build +``` + +The game sees the new file within a second, says "Mods reloaded" in chat and starts the mod over. What it saved with `storage` stays. Things to try each time: + +- Every command, with no arguments, with good ones and with wrong ones. An `Err` should say what to type instead. +- Leaving the world and opening another: `game::get_player()` is `None` on the title screen. +- Quitting and starting again, for a mod that saves. +- **Options, Mods...**: the mod's row lists what it may do. If it says the mod stopped, the reason is there. + +The game log (`logs/latest.log` in the game folder) has your mod's `info` and `warn` lines as `[mod ]`, and the reason when the game refused or stopped the mod. + +**Why a mod stops.** A mod that panics, runs past its time budget for one call (an endless loop, for example) or uses more than 64 MiB of memory is switched off. The rest of the game carries on. Fix the cause and build again: a dev mod starts over with the new file. + +## Check it as the registry does + +Before you publish, run what the registry runs on every submission: + +```sh +cargo horizon-mod check target/horizon-mod/playtime.hmod +``` + +``` +playtime 0.1.0 (mod, API 0.1, client) + 12424 bytes, SHA-256 6561…ac13, unsigned + may draw text on your screen + may show you chat messages + may save its own settings to disk + trial run: started, 40 ticks, 120 frames, a chat line, /playtime: ok +``` + +It reads the manifest, refuses code that uses a capability the manifest doesn't list, then plays the mod in the sandbox, as the game does: 40 ticks with a moving player, 120 frames, a chat line and each of its commands once with no arguments. The mod must start and must never crash or overrun its budget. It fails with the reason when one of those goes wrong. + +- `--json` prints the report as JSON, for scripts. +- `--key --signature .minisig` also checks your signature ([Publish](/docs/publish-a-mod#sign-it)). +- It checks modpacks (`.hpack`) and resource packs (`.zip`) too; `--kind mod|modpack|resourcepack` when the file's extension doesn't say which. + +A mod that passes `check` passes the registry's automatic checks. A person still reviews it (see [Publish](/docs/publish-a-mod)). + +Next: [publish it](/docs/publish-a-mod). diff --git a/website/railway.json b/website/railway.json index 0f84cf952..3bbe9dfb3 100644 --- a/website/railway.json +++ b/website/railway.json @@ -3,7 +3,7 @@ "build": { "builder": "DOCKERFILE", "dockerfilePath": "Dockerfile", - "watchPatterns": ["/website/**", "/README.md", "/launcher/links.json"] + "watchPatterns": ["/website/**", "/README.md", "/launcher/links.json", "/docs/api/**"] }, "deploy": { "healthcheckPath": "/", diff --git a/website/serve.mjs b/website/serve.mjs index 0f862714e..553a3ff9b 100644 --- a/website/serve.mjs +++ b/website/serve.mjs @@ -11,6 +11,10 @@ const port = Number(process.env.PORT) || 4173; createServer(async (req, res) => { let path = normalize(decodeURIComponent(new URL(req.url, "http://x").pathname)).replace(/^(\.\.[/\\])+/, ""); + if (path === "/docs/api/") { + res.writeHead(301, { Location: "/docs/api" }); + return res.end(); + } if (path.endsWith("/")) path += "index.html"; // The server guide is part of the docs now, as the Caddyfile redirects it. if (path === "/server" || path === "/server.html") { @@ -23,7 +27,7 @@ createServer(async (req, res) => { return res.end(); } if (path === "/docs") path = "/docs/index.html"; - // /privacy and /docs/ are .html files, as the Caddyfile rewrites them. + // /privacy, /docs/ and /docs/api/ are .html files, as the Caddyfile rewrites them. if (!extname(path)) path += ".html"; let file = join(root, path); let status = 200; diff --git a/website/src/contributors.js b/website/src/contributors.js index 5d2030f5e..12bce9457 100644 --- a/website/src/contributors.js +++ b/website/src/contributors.js @@ -21,7 +21,7 @@ export function contributorsFrom(list) { } // GitHub serves avatars at any size: 96 px is twice what the page draws, for sharp screens. -function avatar(url) { +export function avatar(url) { if (!url.startsWith(AVATARS)) return ""; const u = new URL(url); u.searchParams.set("s", "96"); diff --git a/website/src/docs.html b/website/src/docs.html index 1277803b5..56ba89c09 100644 --- a/website/src/docs.html +++ b/website/src/docs.html @@ -46,7 +46,7 @@ -

Edit this page on GitHub · Spotted something wrong? Open an issue or ask on Discord.

+

Edit this page on GitHub · Spotted something wrong? Open an issue or ask on Discord.

Not an official Minecraft product. Not approved by or associated with Mojang or Microsoft.

diff --git a/website/src/hub-page.js b/website/src/hub-page.js index 748ddbab4..9115c5ca4 100644 --- a/website/src/hub-page.js +++ b/website/src/hub-page.js @@ -132,6 +132,7 @@ async function refresh() { document.querySelector("[data-overview]").innerHTML = overviewHtml(data.roadmap, live); document.querySelector("[data-roadmap]").innerHTML = roadmapHtml(data.roadmap, live); filter(area); + showVotes(); const board = boardFrom(fresh, data.roadmap); const chosen = boardSelect.value; boardSelect.innerHTML = boardFilterHtml(board, data.roadmap); @@ -144,7 +145,126 @@ async function refresh() { if (note) note.textContent = "Pull request and issue states are live from GitHub."; } +// --- Votes, bug reports and suggestions: the hub's server (crates/horizon-telemetry-server) -------------------- + +const saved = (key, fallback) => { + try { + return JSON.parse(localStorage.getItem(key)) ?? fallback; + } catch { + return fallback; + } +}; +const save = (key, value) => { + try { + localStorage.setItem(key, JSON.stringify(value)); + } catch {} +}; +const post = (path, body) => fetch(`${data.api}${path}`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }); + +// This browser's random id (one vote per item per browser) and the items it voted for. Nothing else identifies it. +function voter() { + let id = saved("hub:voter", ""); + if (typeof id !== "string" || id.length < 16) { + id = crypto.randomUUID ? crypto.randomUUID() : [...crypto.getRandomValues(new Uint8Array(16))].map((b) => b.toString(16).padStart(2, "0")).join(""); + save("hub:voter", id); + } + return id; +} +const mine = new Set(saved("hub:voted", [])); +let counts = null; // { item: votes } once the server answered; until then the buttons stay hidden + +function showVotes() { + if (!counts) return; + for (const button of document.querySelectorAll("[data-vote]")) { + const id = button.dataset.vote; + button.hidden = false; + button.setAttribute("aria-pressed", String(mine.has(id))); + button.querySelector(".hub-vote-label").textContent = mine.has(id) ? "Wanted" : "Want it"; + button.querySelector("[data-votes]").textContent = counts[id] ? String(counts[id]) : ""; + } +} + +async function vote(id) { + const up = !mine.has(id); + const apply = (on, step) => { + if (on) mine.add(id); + else mine.delete(id); + counts[id] = Math.max(0, (counts[id] || 0) + step); + save("hub:voted", [...mine]); + showVotes(); + }; + apply(up, up ? 1 : -1); + try { + const res = await post("/hub/vote", { item: id, voter: voter(), up }); + if (!res.ok) throw new Error(res.status); + counts[id] = (await res.json()).votes; + showVotes(); + } catch { + apply(!up, up ? -1 : 1); + } +} +document.addEventListener("click", (e) => { + const button = e.target.closest("[data-vote]"); + if (button && counts) vote(button.dataset.vote); +}); + +const LABELS = { title: "the title", did: "what you did", happened: "what happened", expected: "what you expected", version: "the version", system: "OS, CPU, GPU", text: "your idea", name: "your name" }; +// The server's answer in words: "did: 3 to 2000 characters" becomes "Check what you did: 3 to 2000 characters." +function trouble(code, answer) { + if (code === 429) return "Too many sent from your connection: try again in a few minutes."; + const said = typeof answer.error === "string" ? answer.error : ""; + const field = said.match(/^(\w+): (.+)$/); + if (code === 400 && field) return `Check ${LABELS[field[1]] || field[1]}: ${field[2]}.`; + return said ? `${said[0].toUpperCase()}${said.slice(1)}.` : "That didn't go through: try again in a moment."; +} + +function setUpForm(form, path, thanks) { + const status = form.querySelector("[data-status]"); + form.hidden = false; + form.parentElement.querySelector("[data-closed]").hidden = true; + form.addEventListener("submit", async (e) => { + e.preventDefault(); + if (!form.reportValidity()) return; + const button = form.querySelector('[type="submit"]'); + button.disabled = true; + status.textContent = "Sending…"; + try { + const res = await post(path, Object.fromEntries(new FormData(form))); + const answer = await res.json().catch(() => ({})); + if (res.ok) { + thanks(status, answer); + // A new report starts with the version filled in again; the name stays for the next one. + for (const field of form.querySelectorAll("textarea, input:not([name='version']):not([name='name'])")) field.value = ""; + } else status.textContent = trouble(res.status, answer); + } catch { + status.textContent = "Couldn't reach the server: try again, or use GitHub or Discord."; + } + button.disabled = false; + }); +} + +// The forms and the votes show once the server answers; without it the page points to GitHub and Discord. +async function connect() { + try { + // Revalidated: the server keeps the counts a minute, a browser's copy would miss this browser's own vote. + const res = await fetch(`${data.api}/hub/votes`, { cache: "no-cache" }); + if (!res.ok) return; + counts = (await res.json()).votes || {}; + } catch { + return; + } + showVotes(); + setUpForm(document.querySelector('[data-form="report"]'), "/hub/report", (status, answer) => { + const issue = typeof answer.issue === "string" && answer.issue.match(/^https:\/\/github\.com\/ludovic111\/horizon\/issues\/(\d+)$/); + status.innerHTML = issue ? `Thanks! It's issue #${issue[1]} on GitHub: follow it there.` : "Thanks! It will be on GitHub in a few minutes."; + }); + setUpForm(document.querySelector('[data-form="suggest"]'), "/hub/suggest", (status) => { + status.textContent = "Thanks! The maintainers read every suggestion."; + }); +} + setUpTheme(); setUpMenu(); +connect(); follow(); refresh().then(() => data.roadmap.items.some((i) => `#${i.id}` === location.hash) && follow()); diff --git a/website/src/hub.html b/website/src/hub.html index d4ba4600c..167ffc15e 100644 --- a/website/src/hub.html +++ b/website/src/hub.html @@ -90,7 +90,7 @@

Take a task

@@ -109,6 +109,41 @@
+
+

Report a bug

+

One bug per report: something that breaks, or doesn't play like vanilla. It becomes an issue on GitHub, public, and you get its link to follow it. Search the open bugs first.

+ +

Report it on GitHub or in #horizon-bugs on Discord, where Kira turns your post into an issue.

+
+
+

Suggest something

+

An idea for Horizon, or about something on the roadmap. The maintainers read every one, and the good ones become roadmap items. Nothing you send here is published.

+ +

Post it in #horizon-ideas on Discord, or send it to Kira with /suggest.

+

The rules

    @@ -119,6 +154,12 @@

The contributing guideCONTRIBUTING.md

+
+

Contributors

+

Everyone with commits in Horizon. On Discord, /link-github <your login> credit:True adds your Discord name next to yours.

+
    {{HUB_CREDIT}}
+

All contributors on GitHub

+

Not an official Minecraft product. Not approved by or associated with Mojang or Microsoft.

diff --git a/website/src/hub.js b/website/src/hub.js index d293b7250..f11668d8c 100644 --- a/website/src/hub.js +++ b/website/src/hub.js @@ -6,6 +6,7 @@ import { escape, inline } from "./markdown.js"; import { parts, plainItem, sections } from "./releases.js"; import { isGame, isLauncher } from "./versions.js"; +import { avatar } from "./contributors.js"; export const REPO = "ludovic111/horizon"; const issueUrl = (n) => `https://github.com/${REPO}/issues/${n}`; @@ -85,11 +86,16 @@ export function progress(item, live) { const areaName = (roadmap, id) => roadmap.areas.find((a) => a.id === id)?.name || id; +// An item's vote button: hidden until the page has the counts from the hub's server (hub-page.js). +export function voteHtml(item) { + return ``; +} + export function itemHtml(item, roadmap, live) { const done = item.status === "shipped"; const state = done ? "" : progress(item, live); return `
-

${escape(areaName(roadmap, item.area))}${done ? `${escape(item.shipped_in)}` : ""}${item.ongoing ? `Ongoing` : ""}

+

${escape(areaName(roadmap, item.area))}${done ? `${escape(item.shipped_in)}` : ""}${item.ongoing ? `Ongoing` : ""}${done ? "" : voteHtml(item)}

${escape(item.title)}

${inline(item.plain)}

${devHtml(item)}

@@ -160,7 +166,7 @@ export function overviewHtml(roadmap, live = new Map()) {

How to help

${inline(o.help_plain)}

${inline(o.help_dev)}

-

ContributeHelp wanted

+

ContributeHelp wantedReport a bugSuggest something

`; } @@ -418,3 +424,34 @@ export function journalHtml(entries, roadmap) { }) .join("\n"); } + +// --- Contribute: the suggestion form's items and the contributors ------------------------------------------------ + +// The suggestion form's "About" choices: the items still to come. +export function suggestItemsHtml(roadmap) { + const open = roadmap.items.filter((i) => i.status !== "shipped"); + return [``] + .concat(open.map((i) => ``)) + .join(""); +} + +// The contributors, from GitHub (contributors.js's list), with the Discord name of those who linked their accounts +// on Kira and agreed to be credited (Kira's /contributors.json: { contributors: [{ github, discord }] }). +export function creditFrom(github, kira) { + const discord = new Map( + (Array.isArray(kira?.contributors) ? kira.contributors : []) + .filter((c) => c && typeof c.github === "string" && typeof c.discord === "string") + .map((c) => [c.github.toLowerCase(), c.discord]), + ); + return github.map((c) => ({ ...c, discord: discord.get(c.login.toLowerCase()) || "" })); +} + +export function creditHtml(list) { + return list + .map((c) => { + const src = avatar(c.avatar_url); + const count = `${c.contributions.toLocaleString("en-US")} ${c.contributions === 1 ? "commit" : "commits"}`; + return `
  • ${src ? `` : ``}${escape(c.login)}${count}${c.discord ? ` · ${escape(c.discord)} on Discord` : ""}
  • `; + }) + .join(""); +} diff --git a/website/src/hub.test.mjs b/website/src/hub.test.mjs index d688489e9..4213d8a87 100644 --- a/website/src/hub.test.mjs +++ b/website/src/hub.test.mjs @@ -1,7 +1,7 @@ import { test } from "node:test"; import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; -import { boardFrom, boardHtml, fixesIn, itemOf, journalFrom, journalHtml, journalJson, overviewHtml, progress, roadmapHtml, slimIssue } from "./hub.js"; +import { boardFrom, boardHtml, creditFrom, creditHtml, fixesIn, itemOf, journalFrom, journalHtml, journalJson, overviewHtml, progress, roadmapHtml, slimIssue, suggestItemsHtml } from "./hub.js"; const roadmap = JSON.parse(readFileSync(new URL("../data/roadmap.json", import.meta.url), "utf8")); @@ -137,3 +137,23 @@ test("journal.json is the flat feed Kira reads: merges, releases, shipped items, assert.match(e.url, /^https:\/\/(github\.com|playhorizon\.dev)\//, e.id); } }); + +test("items still to come take votes and suggestions; shipped ones don't", () => { + const html = roadmapHtml(roadmap); + for (const item of roadmap.items) { + assert.equal(html.includes(`data-vote="${item.id}"`), item.status !== "shipped", item.id); + assert.equal(suggestItemsHtml(roadmap).includes(`value="${item.id}"`), item.status !== "shipped", item.id); + } + // Hidden until the hub's server answers. + assert.ok(html.includes('aria-pressed="false" hidden>')); +}); + +test("the contributors carry the Discord name of those who agreed on Kira", () => { + const github = [{ login: "Alex", html_url: "https://github.com/Alex", avatar_url: "", contributions: 3 }, { login: "sam", html_url: "https://github.com/sam", avatar_url: "", contributions: 1 }]; + const kira = { contributors: [{ github: "alex", discord: "Alex " }, { github: 5 }, null] }; + const credit = creditFrom(github, kira); + assert.deepEqual(credit.map((c) => c.discord), ["Alex ", ""]); + const html = creditHtml(credit); + assert.ok(html.includes("3 commits · Alex <b> on Discord") && html.includes("1 commit")); + assert.deepEqual(creditFrom(github, "not json").map((c) => c.discord), ["", ""]); +}); diff --git a/website/src/markdown.js b/website/src/markdown.js index 252e554ec..1735f7304 100644 --- a/website/src/markdown.js +++ b/website/src/markdown.js @@ -1,5 +1,5 @@ -// A small Markdown renderer for release notes and the README: headings, paragraphs, -// lists (nested by indentation), fenced code, quotes, rules, and inline code, bold, +// A small Markdown renderer for release notes, the README and the docs: headings, paragraphs, +// lists (nested by indentation), fenced code, quotes, rules, tables, and inline code, bold, // italic and links. Everything is escaped first, so its output never carries raw HTML. // Shared by build.mjs (Node) and main.js (browser). @@ -38,12 +38,41 @@ export function inline(text, site = false) { out = out.replace(/\u0001(\d+)\u0001/g, (_, i) => { const [label, url] = links[i]; const target = href(url, site); - const body = inline(label, site); + // The label's code spans are already placeholders: put them back for its own pass. + const body = inline(label.replace(/\u0000(\d+)\u0000/g, (_, c) => `\`${codes[c]}\``), site); return target ? `${body}` : body; }); return out.replace(/\u0000(\d+)\u0000/g, (_, i) => `${escape(codes[i])}`); } +// A table: a row of cells, a rule row (`| --- | :-: |`), then body rows, until a line that isn't a row. +const ROW = /^\s*\|.*\|\s*$/; +const RULE = /^\s*\|(\s*:?-+:?\s*\|)+\s*$/; + +/** A row's cells: split on `|` outside code spans; `\|` is a literal bar. */ +function cells(row) { + const s = row.trim().slice(1, -1); + const out = [""]; + let code = false; + for (let i = 0; i < s.length; i++) { + if (s[i] === "\\" && s[i + 1] === "|") out[out.length - 1] += s[++i]; + else if (s[i] === "|" && !code) out.push(""); + else { + if (s[i] === "`") code = !code; + out[out.length - 1] += s[i]; + } + } + return out.map((c) => c.trim()); +} + +function table(lines, start, site) { + const row = (line, tag) => `${cells(line).map((c) => `<${tag}>${inline(c, site)}`).join("")}`; + let i = start + 2; + const body = []; + while (i < lines.length && ROW.test(lines[i])) body.push(row(lines[i++], "td")); + return [`
    ${row(lines[start], "th")}${body.join("")}
    `, i]; +} + const indentOf = (line) => line.match(/^ */)[0].length; const ITEM = /^(\s*)([-*+]|\d+[.)])\s+(.*)$/; @@ -131,6 +160,10 @@ export function markdown(source, { shift = 0, site = false, headings = null } = } else if (/^\s*([-*_])(\s*\1){2,}\s*$/.test(line)) { out.push("
    "); i++; + } else if (ROW.test(line) && RULE.test(lines[i + 1] || "")) { + const [html, end] = table(lines, i, site); + out.push(html); + i = end; } else if (ITEM.test(line)) { const [html, end] = list(lines, i, site); out.push(html); diff --git a/website/src/markdown.test.mjs b/website/src/markdown.test.mjs new file mode 100644 index 000000000..171348e45 --- /dev/null +++ b/website/src/markdown.test.mjs @@ -0,0 +1,25 @@ +// node --test src/: the Markdown the docs pages and release notes are rendered from. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { markdown } from "./markdown.js"; + +test("a table renders with its header, cells and inline Markdown", () => { + const html = markdown("| Way in | Status |\n| --- | :-: |\n| Rust | `cargo horizon-mod` |\n| Lua | planned \\| soon |\n\nAfter."); + assert.equal( + html, + '
    ' + + "
    Way inStatus
    Rustcargo horizon-mod
    Luaplanned | soon
    \n

    After.

    ", + ); +}); + +test("a bar inside a code span stays in its cell", () => { + assert.match(markdown("| a | b |\n|---|---|\n| `x | y` | z |"), /x \| y<\/code><\/td>z<\/td>/); +}); + +test("rows without a rule line are a paragraph", () => { + assert.equal(markdown("| a | b |\n| c | d |"), "

    | a | b | | c | d |

    "); +}); + +test("a link whose label is code keeps it", () => { + assert.equal(markdown("See [`AGENTS.md`](/docs/agents).", { site: true }), '

    See AGENTS.md.

    '); +}); diff --git a/website/src/mods.js b/website/src/mods.js index acaee41f0..414db7396 100644 --- a/website/src/mods.js +++ b/website/src/mods.js @@ -134,6 +134,7 @@ export function submitHtml() { return `

    ModsSubmit

    Submit to the registry

    A mod, a modpack of registry mods, or a resource pack made for Horizon. The form checks the file and opens a pull request; a reviewer reads it, its source and the checks' report, and merging publishes it to this site and the launcher.

    +

    First time? Publish a mod walks through building, checking and signing the file, and Make a mod starts from the beginning.

    The form needs JavaScript. You can also open a pull request by hand.

    diff --git a/website/src/style.css b/website/src/style.css index 07423ff5c..1de2a3d3d 100644 --- a/website/src/style.css +++ b/website/src/style.css @@ -645,7 +645,7 @@ code { border-radius: 0; background: var(--well); box-shadow: 0 0 0 1px var(--ou /* Releases: a filter, then every release as a panel that opens. */ .release-filter { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 32px; } -.release-filter .button[aria-pressed="true"], .hub-detail .button[aria-pressed="true"] { background: var(--face); color: var(--on-face); box-shadow: inset 0 var(--block-bevel) 0 var(--face-bevel), inset 0 calc(-1 * var(--block-lip)) 0 var(--face-lip); } +.release-filter .button[aria-pressed="true"], .hub-detail .button[aria-pressed="true"], .button.hub-vote[aria-pressed="true"] { background: var(--face); color: var(--on-face); box-shadow: inset 0 var(--block-bevel) 0 var(--face-bevel), inset 0 calc(-1 * var(--block-lip)) 0 var(--face-lip); } .release-count { margin: 16px 0 12px; } .release-list { display: grid; gap: 8px; } .release-list .release + .release { margin-top: 0; } @@ -687,6 +687,10 @@ code { border-radius: 0; background: var(--well); box-shadow: 0 0 0 1px var(--ou .bench-table td.num { font-variant-numeric: tabular-nums; white-space: nowrap; } .bench-table td.wins { color: var(--ink); font-weight: 600; } .bench-table td.vs { font-size: var(--text-caption); white-space: nowrap; } +.md-table { width: 100%; border-collapse: collapse; font-size: var(--text-ui); } +.md-table th, .md-table td { padding: 8px 12px 8px 0; text-align: left; border-bottom: 1px solid var(--rule); vertical-align: baseline; } +.md-table th { font-weight: 500; color: var(--ink); } +.md-table td code, .md-table th code { overflow-wrap: anywhere; } .vs-horizon { color: var(--success); } .vs-java { color: var(--danger); } .vs-tie, .vs-none { color: var(--muted); } @@ -827,6 +831,29 @@ code { border-radius: 0; background: var(--well); box-shadow: 0 0 0 1px var(--ou .hub-rows .row-meta { grid-column: 2; text-align: left; } } +/* --- Hub: votes, the report and suggestion forms, the contributors ---------------------- */ +.hub-vote { margin-left: auto; } +.hub-vote-count { margin-left: -2px; } +.hub-vote-count:empty { display: none; } +.hub-form { display: grid; gap: 16px; max-width: 46rem; margin-top: 8px; } +.hub-form-pair { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; } +.hub-field { display: grid; gap: 6px; font-size: var(--text-label); color: var(--ink-soft); } +.hub-field input, .hub-field textarea, .hub-field select { + width: 100%; min-height: 36px; padding: 8px 10px; font: var(--text-ui) / 1.4 var(--font); color: var(--ink); + background: var(--well); border: 0; box-shadow: 0 0 0 var(--block-outline) var(--outline); +} +.hub-field textarea { resize: vertical; } +.hub-field input:focus-visible, .hub-field textarea:focus-visible, .hub-field select:focus-visible { outline: 1px solid var(--accent); outline-offset: 2px; } +.hub-field input::placeholder, .hub-field textarea::placeholder { color: var(--muted); } +.hub-trap { position: absolute; left: -10000px; width: 1px; height: 1px; overflow: hidden; } +.hub-form-foot { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 16px; } +.hub-form-foot [data-status] { color: var(--ink); } +.hub-form-foot .button:disabled { opacity: 0.4; cursor: default; } +.hub-form-closed { color: var(--ink-soft); } +@media (max-width: 720px) { + .hub-form-pair { grid-template-columns: minmax(0, 1fr); } +} + /* The registry (/mods): a search and the kind filter, rows as panels, then a page per project. */ .mod-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 24px; } .mod-tools { display: flex; flex-wrap: wrap; align-items: center; gap: 16px; margin-top: 32px; }