diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index e83908171..0c13caf7f 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -4856,8 +4856,8 @@ impl Game<'_> { creative: bool, ) -> Result<()> { let save_root = std::env::current_dir()?.join("saves").join(folder); + horizon_client::save::upgrade_older_world(&save_root)?; horizon_client::save::migrate_legacy(&save_root)?; - horizon_client::save::check_data_version(&save_root)?; self.session_lock = None; self.session_lock = Some(horizon_client::save::SessionLock::acquire(&save_root)?); self.persist = Default::default(); diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index 0211673ab..32f904b5b 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -111,10 +111,13 @@ impl SessionLock { Ok(Self(file)) } } -/// Refuse a world last saved by an older game version: chunks are only read -/// at the current DataVersion, and the DataFixerUpper chain is not ported. -/// `tools/upgrade_world.py` runs vanilla's own upgrader (`--forceUpgrade`). -pub fn check_data_version(world: &Path) -> Result<()> { +/// Bring a world last saved by an older game version to the current format, as +/// 26.3 does when it opens one: the file fixes and level.dat first +/// (`FileFixerUpper`), then the DataFixers upgrades of every chunk, entity chunk, +/// POI chunk, player, saved data, advancements and statistics file. Vanilla runs +/// the latter as it reads each file; Horizon runs them all on opening, which reads +/// the same data. Worlds older than the ported fixes (1.20) are refused. +pub fn upgrade_older_world(world: &Path) -> Result<()> { let Some(root) = read_dat(&world.join("level.dat")) else { return Ok(()); }; @@ -122,14 +125,20 @@ pub fn check_data_version(world: &Path) -> Result<()> { .get("Data") .and_then(|d| number(d.get("DataVersion"))) .unwrap_or(i64::from(DATA_VERSION)); - if version < i64::from(DATA_VERSION) { + if version >= i64::from(DATA_VERSION) { + return Ok(()); + } + if version < i64::from(horizon_world::datafix::OLDEST_SUPPORTED) { return Err(format!( - "{} was saved with DataVersion {version} (this game is {DATA_VERSION}); \ - upgrade it first with tools/upgrade_world.py", - world.display() + "{} was saved with DataVersion {version}; worlds older than DataVersion {} (1.20) \ + cannot be upgraded yet (tools/upgrade_world.py runs vanilla's own upgrader)", + world.display(), + horizon_world::datafix::OLDEST_SUPPORTED ) .into()); } + horizon_world::datafix::files::upgrade_world(world) + .map_err(|e| format!("upgrading {}: {e}", world.display()))?; Ok(()) } /// Move a world saved by an older Horizon build (pre-26.3 layout with a @@ -2181,14 +2190,24 @@ impl ChunkGenerator for Persistent { #[cfg(test)] mod tests { #[test] - fn old_data_version_is_refused() { + fn older_worlds_are_upgraded_on_opening() { let dir = std::env::temp_dir().join(format!("horizon-dv-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); let level = |v: i32| Tag::compound([("Data", Tag::compound([("DataVersion", Tag::Int(v))]))]); + let version = |dir: &std::path::Path| { + super::read_dat(&dir.join("level.dat")) + .and_then(|r| super::number(r.get("Data")?.get("DataVersion"))) + }; + // Older than the ported fixes: refused, left as it is. + super::write_dat(&dir.join("level.dat"), level(3000)).unwrap(); + assert!(super::upgrade_older_world(&dir).is_err()); + assert_eq!(version(&dir), Some(3000)); + // 1.21.1: upgraded to the current version. super::write_dat(&dir.join("level.dat"), level(3955)).unwrap(); - assert!(super::check_data_version(&dir).is_err()); - super::write_dat(&dir.join("level.dat"), level(super::DATA_VERSION)).unwrap(); - assert!(super::check_data_version(&dir).is_ok()); + super::upgrade_older_world(&dir).unwrap(); + assert_eq!(version(&dir), Some(i64::from(super::DATA_VERSION))); + super::upgrade_older_world(&dir).unwrap(); let _ = std::fs::remove_dir_all(&dir); } #[test] diff --git a/docs/world-data.md b/docs/world-data.md index cbd068131..57543de8f 100644 --- a/docs/world-data.md +++ b/docs/world-data.md @@ -68,10 +68,7 @@ The registry loader checks data version, duplicate states, default IDs and block-name coverage. The local oracle exports all 35,723 state identities and defaults from vanilla. This imports identities, not block implementations, shapes or behavior. Registry exports and game-derived fixtures stay in `.local/`. -Chunk access requires data version 5023. The DataFixerUpper chain is not ported: -opening a world whose `level.dat` is older fails with a message pointing at -`tools/upgrade_world.py`, which runs vanilla's own `--forceUpgrade` on a copy -(exercised only against a current-version world; no older server jar is local). `BlockRegistry::resolve` rejects invalid property descriptors. The separate +Chunk access requires data version 5023. A world saved by an older version is upgraded when it is opened, as 26.3 does: the file fixes (`FileFixerUpper`) and level.dat, then vanilla's DataFixers over every chunk, entity chunk, POI chunk, player, saved data, advancements and statistics file (`horizon_world::datafix`; vanilla upgrades each file as it reads it, Horizon all of them on opening). `tools/test_datafix_parity.py` compares the result with vanilla's own upgrade over worlds written by older server jars. Worlds older than the ported fixes are refused with a message pointing at `tools/upgrade_world.py`, which runs vanilla's `--forceUpgrade` on a copy. `BlockRegistry::resolve` rejects invalid property descriptors. The separate [block-state JSON codec](block-state-codecs.md) reproduces vanilla property fallback for compact and full JSON forms. diff --git a/tools/test_save_roundtrip_parity.py b/tools/test_save_roundtrip_parity.py index 126d39b01..a3798b28c 100644 --- a/tools/test_save_roundtrip_parity.py +++ b/tools/test_save_roundtrip_parity.py @@ -547,7 +547,10 @@ def main(): failures += 1 volatile = re.compile(r'(cooking_time_spent|lit_time_remaining|ticks_since_song_started|Age|BrewTime|' r'ticks_in_hive|triggered|state_updating_resumes_at|LastExecution|last_vibration_frequency|warning_level): -?[0-9]+[a-zA-Z]?') - volatile2 = re.compile(r'(RecipesUsed: \{[^}]*\}|listener: \{.*\}(?=, x))') + # A lit campfire keeps cooking while each server runs before `data get`: its + # progress counters advance by however many ticks that took (stage 3 checks + # that Horizon saves them exactly as loaded). + volatile2 = re.compile(r'(RecipesUsed: \{[^}]*\}|listener: \{.*\}(?=, x)|CookingTimes: \[I;[^\]]*\])') # The lit furnace keeps smelting while each game runs: its slots move on. volatile_items = re.compile(r'Items: \[[^\]]*\]') for label in views0: