From 0789bf071926d6cf9ced552f916f1d4bc712f9c7 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 04:06:23 +0200 Subject: [PATCH] Keep the behaviours snapshot in the game folder, keyed to the build The resource folder is shared: worktrees and parity harnesses link one copy, and the launcher copies a prepared folder to each new compatible version. A snapshot there would be read by builds that resolve the tables differently while the VERSION constant stays the same. Keep it in the game's own .horizon-cache (set by the client; tools, tests and servers don't cache), add the running executable to its key, and skip hashing the resources when there is no snapshot. Postcard without default features drops heapless and the unmaintained atomic-polyfill from the lockfile. Co-Authored-By: Claude Opus 5.5 --- .gitignore | 1 + Cargo.lock | 54 ----------- crates/horizon-client/src/game.rs | 3 + crates/horizon-world/Cargo.toml | 2 +- crates/horizon-world/src/level/data.rs | 1 + crates/horizon-world/src/level/data/cache.rs | 94 ++++++++++++++++---- crates/horizon-world/src/level/mod.rs | 6 ++ 7 files changed, 88 insertions(+), 73 deletions(-) diff --git a/.gitignore b/.gitignore index eb29ad10d..7fffd6a99 100644 --- a/.gitignore +++ b/.gitignore @@ -28,5 +28,6 @@ /horizon-player.txt /servers.dat /screenshots/ +/.horizon-cache/ /crash-reports/ /debug/ diff --git a/Cargo.lock b/Cargo.lock index 9726316b6..ea6681a2c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -196,15 +196,6 @@ dependencies = [ "libloading", ] -[[package]] -name = "atomic-polyfill" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cf2bce30dfe09ef0bfaef228b9d414faaf7e563035494d7fe092dba54b300f4" -dependencies = [ - "critical-section", -] - [[package]] name = "atomic-waker" version = "1.1.2" @@ -331,12 +322,6 @@ dependencies = [ "syn 3.0.6", ] -[[package]] -name = "byteorder" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" - [[package]] name = "bytes" version = "1.12.1" @@ -757,12 +742,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "critical-section" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b" - [[package]] name = "crossbeam-utils" version = "0.8.23" @@ -1328,15 +1307,6 @@ dependencies = [ "zerocopy", ] -[[package]] -name = "hash32" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0c35f58762feb77d74ebe43bdbc3210f09be9fe6742234d573bacc26ed92b67" -dependencies = [ - "byteorder", -] - [[package]] name = "hashbrown" version = "0.15.5" @@ -1367,20 +1337,6 @@ dependencies = [ "foldhash 0.2.0", ] -[[package]] -name = "heapless" -version = "0.7.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdc6457c0eb62c71aac4bc17216026d8410337c4126773b9c5daba343f17964f" -dependencies = [ - "atomic-polyfill", - "hash32", - "rustc_version", - "serde", - "spin", - "stable_deref_trait", -] - [[package]] name = "heck" version = "0.5.0" @@ -2919,7 +2875,6 @@ dependencies = [ "cobs", "embedded-io 0.4.0", "embedded-io 0.6.1", - "heapless", "serde", ] @@ -3506,15 +3461,6 @@ dependencies = [ "sha2", ] -[[package]] -name = "spin" -version = "0.9.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3763264f6b73151db08c50ff20d7d8a0b8796e021cdea7ceedad07b80155fa0e" -dependencies = [ - "lock_api", -] - [[package]] name = "spirv" version = "0.4.0+sdk-1.4.341.0" diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index e552eb118..6dbaba66d 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -10144,6 +10144,9 @@ pub fn run() -> Result<()> { horizon_client::spans::enable(options.spikes.is_some()); let vanilla_root = options.resources.clone(); let early_settings = Settings::load(&std::env::current_dir()?.join("options.txt")); + // Snapshots of derived tables live in the game folder, never beside the + // shared resources. + Behaviours::set_cache_dir(std::env::current_dir()?.join(".horizon-cache")); options.resources = packs::startup_root(&vanilla_root, &early_settings.resource_packs); options.resources = mods::content_root( &options.resources, diff --git a/crates/horizon-world/Cargo.toml b/crates/horizon-world/Cargo.toml index 3e5f9466d..737edd289 100644 --- a/crates/horizon-world/Cargo.toml +++ b/crates/horizon-world/Cargo.toml @@ -11,7 +11,7 @@ libm = "=0.2.16" serde_json = { version = "1.0.151", features = ["raw_value"] } serde = { version = "1", features = ["derive"] } sha2 = "0.10" -postcard = { version = "=1.1.3", features = ["alloc"] } +postcard = { version = "=1.1.3", default-features = false, features = ["alloc"] } horizon-core = { path = "../horizon-core" } flate2 = { version = "1.1.10", default-features = false, features = ["rust_backend"] } lz4_flex = { version = "0.14", default-features = false, features = ["std", "safe-encode", "safe-decode", "checked-decode"] } diff --git a/crates/horizon-world/src/level/data.rs b/crates/horizon-world/src/level/data.rs index 83349232c..ef47294a8 100644 --- a/crates/horizon-world/src/level/data.rs +++ b/crates/horizon-world/src/level/data.rs @@ -23,6 +23,7 @@ use std::{ }; #[path = "data/cache.rs"] mod cache; +pub(super) use cache::set_dir as set_cache_dir; /// Per-state flags read from vanilla accessors over an empty level. pub mod state_flags { diff --git a/crates/horizon-world/src/level/data/cache.rs b/crates/horizon-world/src/level/data/cache.rs index f143cf6cd..59bc1f8d8 100644 --- a/crates/horizon-world/src/level/data/cache.rs +++ b/crates/horizon-world/src/level/data/cache.rs @@ -2,21 +2,36 @@ //! //! The source resources remain authoritative. Failed reads, unsupported //! versions, corrupt payloads and write failures always use the normal loader. +//! The snapshot lives in the game's own cache folder ([`set_dir`]), never in the +//! resource folder: that one is shared by worktrees and copied between installed +//! versions by the launcher. Its key includes the running executable, so a build +//! that resolves the tables differently never reads another build's snapshot. use super::*; use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; use std::{ io::Write, path::PathBuf, - sync::atomic::{AtomicU64, Ordering}, + sync::{ + OnceLock, + atomic::{AtomicU64, Ordering}, + }, + time::UNIX_EPOCH, }; const MAGIC: &[u8; 8] = b"HZBEHAV\0"; const VERSION: u32 = 2; -const FILE_NAME: &str = ".horizon-behaviours-v2.bin"; +const FILE_NAME: &str = "behaviours-v2.bin"; const MAX_FILE_BYTES: usize = 128 * 1024 * 1024; const MAX_DECODED_BYTES: usize = 256 * 1024 * 1024; static NEXT_TEMP: AtomicU64 = AtomicU64::new(0); +static DIR: OnceLock = OnceLock::new(); + +/// Keep snapshots in `dir` (the client passes `/.horizon-cache`). +/// Without it (tests, tools, servers), `Behaviours::load` always uses the source loader. +pub(in crate::level) fn set_dir(dir: PathBuf) { + let _ = DIR.set(dir); +} #[derive(Serialize, Deserialize)] struct Snapshot { @@ -123,6 +138,17 @@ struct Item { fn fingerprint(registry: &BlockRegistry, root: &Path) -> io::Result<[u8; 32]> { let mut hasher = Sha256::new(); hasher.update(b"horizon resolved behaviours 2"); + // The build that resolved the tables: the version constant alone misses + // loader changes that keep the snapshot layout. + let exe = std::env::current_exe()?; + let built = fs::metadata(&exe)?; + hash_str(&mut hasher, &exe.to_string_lossy()); + hasher.update(built.len().to_le_bytes()); + let modified = built + .modified()? + .duration_since(UNIX_EPOCH) + .map_err(|e| invalid(e.to_string()))?; + hasher.update(modified.as_nanos().to_le_bytes()); for relative in [ "facts.json", "block-behaviours.json", @@ -635,8 +661,12 @@ impl Snapshot { } pub(super) fn load(registry: &BlockRegistry, root: &Path) -> Option { + load_from(DIR.get()?, registry, root) +} + +fn load_from(dir: &Path, registry: &BlockRegistry, root: &Path) -> Option { + let bytes = fs::read(dir.join(FILE_NAME)).ok()?; let fingerprint = fingerprint(registry, root).ok()?; - let bytes = fs::read(root.join(FILE_NAME)).ok()?; // Validate bounds before decoding untrusted cache contents. if bytes.len() < 8 + 4 + 32 + 32 || bytes.len() > MAX_FILE_BYTES { return None; @@ -661,10 +691,17 @@ pub(super) fn load(registry: &BlockRegistry, root: &Path) -> Option } pub(super) fn store(registry: &BlockRegistry, root: &Path, behaviours: &Behaviours) { - let _ = write(registry, root, behaviours); + if let Some(dir) = DIR.get() { + let _ = write(dir, registry, root, behaviours); + } } -fn write(registry: &BlockRegistry, root: &Path, behaviours: &Behaviours) -> io::Result<()> { +fn write( + dir: &Path, + registry: &BlockRegistry, + root: &Path, + behaviours: &Behaviours, +) -> io::Result<()> { let fingerprint = fingerprint(registry, root)?; let snapshot = Snapshot::capture(behaviours); let serialized = postcard::to_allocvec(&snapshot).map_err(|e| invalid(e.to_string()))?; @@ -678,8 +715,9 @@ fn write(registry: &BlockRegistry, root: &Path, behaviours: &Behaviours) -> io:: bytes.extend_from_slice(&fingerprint); bytes.extend_from_slice(&Sha256::digest(&body)); bytes.extend_from_slice(&body); - let path = root.join(FILE_NAME); - let tmp = root.join(format!( + fs::create_dir_all(dir)?; + let path = dir.join(FILE_NAME); + let tmp = dir.join(format!( "{FILE_NAME}.tmp-{}-{}", std::process::id(), NEXT_TEMP.fetch_add(1, Ordering::Relaxed) @@ -773,11 +811,25 @@ mod tests { let Some(root) = fixture() else { return; }; + // The game's cache folder, apart from the resources. + let dir = root.0.join("game/.horizon-cache"); let registry = super::super::load_registry(&root.0).unwrap(); - assert!(load(®istry, &root.0).is_none()); - let initial = super::super::load(®istry, &root.0).unwrap(); - assert!(root.0.join(FILE_NAME).is_file()); - let hit = load(®istry, &root.0).expect("cache hit"); + // What `data::load` does once `set_dir` gave it a folder. + let load_or_build = || { + load_from(&dir, ®istry, &root.0).unwrap_or_else(|| { + let built = super::super::load_uncached(®istry, &root.0).unwrap(); + write(&dir, ®istry, &root.0, &built).unwrap(); + built + }) + }; + assert!(load_from(&dir, ®istry, &root.0).is_none()); + let initial = load_or_build(); + assert!(dir.join(FILE_NAME).is_file()); + assert!( + !root.0.join(FILE_NAME).exists(), + "the resource folder stays untouched" + ); + let hit = load_from(&dir, ®istry, &root.0).expect("cache hit"); assert_eq!(initial.table.state_count(), hit.table.state_count()); let tags = root.0.join("block-tags.json"); fs::OpenOptions::new() @@ -787,23 +839,29 @@ mod tests { .write_all(b"\n") .unwrap(); assert!( - load(®istry, &root.0).is_none(), + load_from(&dir, ®istry, &root.0).is_none(), "changed resource must invalidate" ); - super::super::load(®istry, &root.0).expect("source fallback"); + load_or_build(); assert!( - load(®istry, &root.0).is_some(), + load_from(&dir, ®istry, &root.0).is_some(), "fallback should replace cache" ); - let cache_file = root.0.join(FILE_NAME); + let cache_file = dir.join(FILE_NAME); let mut bytes = fs::read(&cache_file).unwrap(); bytes[80] ^= 0x80; fs::write(&cache_file, bytes).unwrap(); assert!( - load(®istry, &root.0).is_none(), + load_from(&dir, ®istry, &root.0).is_none(), "checksum failure must invalidate" ); - super::super::load(®istry, &root.0).expect("corrupt cache fallback"); - assert!(load(®istry, &root.0).is_some()); + load_or_build(); + assert!(load_from(&dir, ®istry, &root.0).is_some()); + // A snapshot written under another key (another build of the game, + // other resources) is never trusted. + let mut other = fs::read(&cache_file).unwrap(); + other[12] ^= 1; + fs::write(&cache_file, other).unwrap(); + assert!(load_from(&dir, ®istry, &root.0).is_none()); } } diff --git a/crates/horizon-world/src/level/mod.rs b/crates/horizon-world/src/level/mod.rs index 00dd115db..cc561993a 100644 --- a/crates/horizon-world/src/level/mod.rs +++ b/crates/horizon-world/src/level/mod.rs @@ -456,6 +456,12 @@ impl Behaviours { pub fn load(registry: &BlockRegistry, resources_dir: &Path) -> io::Result { data::load(registry, resources_dir) } + /// Keep a snapshot of the resolved tables in `dir` (a game folder's + /// `.horizon-cache`), so later launches of the same build restore it instead + /// of resolving the resources again. Without it, [`Self::load`] never caches. + pub fn set_cache_dir(dir: std::path::PathBuf) { + data::set_cache_dir(dir); + } pub fn facts(&self, state: BlockStateId) -> &StateFacts { &self.facts[state.0 as usize] }