From bf3de4ef0564df5c33d68d8585990ae7a325fa6c Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 11 Oct 2026 14:26:49 +0200 Subject: [PATCH] WIP: saves parity work, paused while the harness tooling is sped up Uncommitted changes the agent had in progress when it was stopped; may not build or pass its harnesses yet. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-world/src/datafix/fixes_a.rs | 30 +++++++++++-- crates/horizon-world/src/datafix/fixes_b.rs | 21 ++++++++- crates/horizon-world/src/datafix/fixes_c.rs | 10 +++++ crates/horizon-world/src/datafix/mod.rs | 49 +++++++++++++++++++++ crates/horizon-world/src/region.rs | 27 ++++++------ 5 files changed, 117 insertions(+), 20 deletions(-) diff --git a/crates/horizon-world/src/datafix/fixes_a.rs b/crates/horizon-world/src/datafix/fixes_a.rs index 7781d6820..89a5bfee5 100644 --- a/crates/horizon-world/src/datafix/fixes_a.rs +++ b/crates/horizon-world/src/datafix/fixes_a.rs @@ -5,7 +5,7 @@ use super::dynamic::{ }; use super::json; use super::schema::{Key, Ty, key}; -use super::{Cx, FixFn, id_is, items, renamer}; +use super::{Cx, FixFn, id_is, items, renamer, reread}; use crate::nbt::Tag; pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { @@ -266,15 +266,18 @@ fn mob_effect_ids(cx: &mut Cx) { // ------------------------------------------------------------------ 3683, 3685 fn primed_tnt_block_state(cx: &mut Cx) { + let k = cx.k; cx.entity("minecraft:tnt", |e| { set(e, "block_state", Tag::compound([("Name", string("minecraft:tnt"))])); if let Some(fuse) = e.get("Fuse").cloned() { set(e, "fuse", fuse); } + reread(k, e); }); } fn projectile_stored_item(cx: &mut Cx) { + let k = cx.k; cx.each(Ty::Entity, |e| { let item = if id_is(e, "minecraft:arrow") { let potion = as_string(e.get("Potion"), "minecraft:empty"); @@ -282,9 +285,11 @@ fn projectile_stored_item(cx: &mut Cx) { } else if id_is(e, "minecraft:spectral_arrow") { "minecraft:spectral_arrow" } else { - // The trident's value is cast to the new type: its Trident field is `item`. + // The trident's value is cast to the new type: its typed Trident field + // is written as `item`, and the raw Trident comes back from the remainder. if id_is(e, "minecraft:trident") { rename(e, "Trident", "item"); + super::drop_typed(e, "Trident"); } return; }; @@ -294,6 +299,7 @@ fn projectile_stored_item(cx: &mut Cx) { if let Some(s) = e.get_mut("item") { items::add_names(s); } + reread(k, e); }); } @@ -390,7 +396,15 @@ fn map_banner_block_pos(cx: &mut Cx) { // ------------------------------------------------------------------ 3808, 3809, 3812 fn horse_body_armor(cx: &mut Cx, id: &str, field: &str, clear_armor_items: bool) { + let k = cx.k; cx.entity(id, |e| { + horse_body_armor_entity(e, field, clear_armor_items); + reread(k, e); + }); +} + +fn horse_body_armor_entity(e: &mut Tag, field: &str, clear_armor_items: bool) { + { let Some(armor) = e.get(field).cloned() else { return; }; @@ -417,7 +431,7 @@ fn horse_body_armor(cx: &mut Cx, id: &str, field: &str, clear_armor_items: bool) } set(e, "body_armor_item", armor); set(e, "body_armor_drop_chance", Tag::Float(2.0f32.to_bits())); - }); + } } fn chested_horse_slots(cx: &mut Cx) { @@ -682,7 +696,15 @@ fn banner_patterns(cx: &mut Cx) { } fn tipped_arrow_potion(cx: &mut Cx) { + let k = cx.k; cx.entity("minecraft:arrow", |e| { + tipped_arrow_potion_entity(e); + reread(k, e); + }); +} + +fn tipped_arrow_potion_entity(e: &mut Tag) { + { let potion = e.get("Potion").cloned(); let effects = e.get("custom_potion_effects").cloned(); let color = e.get("Color").cloned(); @@ -706,7 +728,7 @@ fn tipped_arrow_potion(cx: &mut Cx) { set(&mut stack, "tag", tag); stack }); - }); + } } fn particle_unflattening(cx: &mut Cx) { diff --git a/crates/horizon-world/src/datafix/fixes_b.rs b/crates/horizon-world/src/datafix/fixes_b.rs index 656bc5dde..32cb1646f 100644 --- a/crates/horizon-world/src/datafix/fixes_b.rs +++ b/crates/horizon-world/src/datafix/fixes_b.rs @@ -6,10 +6,19 @@ use super::dynamic::{ use super::fixes_a::{component_fix, dye_color, fix_inline_block_pos, translation_key}; use super::items::java_hash_order; use super::schema::{Key, Ty, key}; -use super::{Cx, FixFn, id_is}; +use super::{Cx, FixFn, id_is, reread}; use crate::nbt::{Compound, Tag}; pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { + // ProjectileStoredWeaponFix only writes and reads arrows with the new schema. + out.push((key(3938, 0), |cx| { + let k = cx.k; + cx.each(Ty::Entity, |e| { + if id_is(e, "minecraft:arrow") || id_is(e, "minecraft:spectral_arrow") { + reread(k, e); + } + }); + })); out.push((key(3939, 0), |cx| feature_flag_remove(cx, &["minecraft:update_1_21"]))); out.push((key(3945, 0), attribute_modifier_ids)); out.push((key(3945, 0), jukebox_ticks)); @@ -588,7 +597,8 @@ fn boat_split(cx: &mut Cx) { (w, false) => format!("minecraft:{w}_boat"), (w, true) => format!("minecraft:{w}_chest_boat"), }; - remove(e, "Type"); + // The remainder update runs on the value cast to the new type, whose choice + // no longer has the old boat id: it finds nothing, so Type stays. set(e, "id", string(&new_id)); }); } @@ -817,6 +827,7 @@ fn saddle_drop_chance(e: &mut Tag) { } fn saddle_equipment_slot(cx: &mut Cx) { + let k = cx.k; cx.each(Ty::Entity, |e| { let id = namespaced(&as_str(e.get("id")).unwrap_or_default()); match id.as_str() { @@ -826,6 +837,7 @@ fn saddle_equipment_slot(cx: &mut Cx) { rename(e, "SaddleItem", "saddle"); saddle_drop_chance(e); } + reread(k, e); } "minecraft:pig" | "minecraft:strider" => { let saddled = as_bool(e.get("Saddle"), false); @@ -834,6 +846,7 @@ fn saddle_equipment_slot(cx: &mut Cx) { set(e, "saddle", Tag::compound([("id", string("minecraft:saddle")), ("count", Tag::Int(1))])); saddle_drop_chance(e); } + reread(k, e); } _ => {} } @@ -864,6 +877,10 @@ pub(super) fn equipment_to_slots(e: &mut Tag) { ("body", body), ("saddle", saddle), ]; + // The old fields are typed: they come back from the entity's remainder. + for field in ["ArmorItems", "HandItems", "body_armor_item", "saddle"] { + super::drop_typed(e, field); + } if slots.iter().all(|(_, s)| s.is_none()) { return; } diff --git a/crates/horizon-world/src/datafix/fixes_c.rs b/crates/horizon-world/src/datafix/fixes_c.rs index fa8221a27..496ddee65 100644 --- a/crates/horizon-world/src/datafix/fixes_c.rs +++ b/crates/horizon-world/src/datafix/fixes_c.rs @@ -10,6 +10,16 @@ use super::{Cx, FixFn, id_is, renamer}; use crate::nbt::Tag; pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { + // NamedEntityConvertUncheckedFix: the typed Particle is written as + // custom_particle, the raw Particle comes back from the remainder. + out.push((key(4420, 0), |cx| { + cx.entity("minecraft:area_effect_cloud", |e| { + if let Some(p) = e.get("Particle").cloned() { + set(e, "custom_particle", p); + } + super::drop_typed(e, "Particle"); + }); + })); out.push((key(4424, 0), |cx| feature_flag_remove(cx, &["minecraft:locator_bar"]))); out.push((key(4424, 0), |cx| cx.each(Ty::Player, add_locator_bar_style))); out.push((key(4424, 0), |cx| cx.each(Ty::Entity, add_locator_bar_style))); diff --git a/crates/horizon-world/src/datafix/mod.rs b/crates/horizon-world/src/datafix/mod.rs index 24a6263aa..a29fc0f21 100644 --- a/crates/horizon-world/src/datafix/mod.rs +++ b/crates/horizon-world/src/datafix/mod.rs @@ -96,6 +96,53 @@ impl Cx<'_> { } } +/// DataFixerUpper reads a record's typed fields from its map without taking +/// them out: the record's remainder keeps the whole map it was read from, and +/// writing lays the typed fields over it. A typed field that a fix drops (by +/// casting to a type without it) therefore comes back on write with its value +/// from that read. Each entity keeps that map under this key while fixes run. +const LAST_READ: &str = "\u{0}last_read"; + +/// Remember an entity's current fields as the map it was read from. +pub fn mark_read(e: &mut Tag) { + if let Tag::Compound(_) = e { + let mut raw = e.clone(); + strip_last_read(&mut raw); + dynamic::set(e, LAST_READ, raw); + } +} + +/// `Util.writeAndReadTypedOrThrow` over an entity by the fix with key `k`: it is +/// written and read again with the output schema, so it (and every entity inside +/// it) now remembers its current fields as the map it was last read from. +pub fn reread(k: Key, e: &mut Tag) { + walk(Ty::Entity, e, k, Ty::Entity, &mut mark_read); +} + +/// A typed field dropped from an entity: what writing the entity gives back for +/// it (its value in the map the entity was last read from, if any). +pub fn drop_typed(e: &mut Tag, field: &str) { + match e.get(LAST_READ).and_then(|raw| raw.get(field)).cloned() { + Some(raw) => dynamic::set(e, field, raw), + None => { + dynamic::remove(e, field); + } + } +} + +fn strip_last_read(v: &mut Tag) { + match v { + Tag::Compound(c) => { + c.remove(&dynamic::key(LAST_READ)); + for value in c.values_mut() { + strip_last_read(value); + } + } + Tag::List(items) => items.iter_mut().for_each(strip_last_read), + _ => {} + } +} + /// The entity or block entity `id` field, namespaced. pub fn id_is(v: &Tag, id: &str) -> bool { as_str(v.get("id")).is_some_and(|s| dynamic::namespaced(&s) == id) @@ -128,6 +175,7 @@ pub fn update(root: Ty, data: &mut Tag, from: i32, to: i32) { let from_key = key(from.max(0) as u32, 0); let to_version = to as u32; // The data is read once with the schema of its version (its read hooks)... + walk(root, data, from_key, Ty::Entity, &mut mark_read); read_hooks(root, data, from_key); // ...then every fix above that key, up to the target version... for (k, fix) in fixes() { @@ -137,6 +185,7 @@ pub fn update(root: Ty, data: &mut Tag, from: i32, to: i32) { } // ...and written with the target schema (its write hooks). write_hooks(root, data, key(to_version, 9)); + strip_last_read(data); } /// The read hooks of the schema the data was saved with. diff --git a/crates/horizon-world/src/region.rs b/crates/horizon-world/src/region.rs index 48b6e132e..05bdc1a54 100644 --- a/crates/horizon-world/src/region.rs +++ b/crates/horizon-world/src/region.rs @@ -104,14 +104,18 @@ impl RegionFile { let position = position_from_path(&path)?; let mut file = File::open(&path)?; let len = file.metadata()?.len(); - if len < HEADER as u64 { - return Err(invalid("truncated region header")); - } + // RegionFile's constructor: a short header (an empty or truncated file) + // reads as far as it goes, the rest stays zero (no chunks). let mut header = [0; HEADER]; - file.read_exact(&mut header)?; + let mut read = 0; + while read < HEADER { + match file.read(&mut header[read..])? { + 0 => break, + n => read += n, + } + } let mut offsets = [0; 1024]; let mut timestamps = [0; 1024]; - let mut ranges = Vec::new(); for i in 0..1024 { offsets[i] = u32::from_be_bytes(header[i * 4..i * 4 + 4].try_into().unwrap()); timestamps[i] = u32::from_be_bytes( @@ -124,16 +128,11 @@ impl RegionFile { } let start = offsets[i] >> 8; let count = offsets[i] & 255; - if start < 2 || count == 0 || u64::from(start + count) > len.div_ceil(SECTOR as u64) { - return Err(invalid(format!( - "invalid sector allocation at region slot {i}" - ))); + // Vanilla drops entries that overlap the header, are empty or start past + // the end of the file (logged as invalid sectors). + if start < 2 || count == 0 || u64::from(start) * SECTOR as u64 > len { + offsets[i] = 0; } - ranges.push((start, start + count)); - } - ranges.sort_unstable(); - if ranges.windows(2).any(|r| r[0].1 > r[1].0) { - return Err(invalid("overlapping region sector allocations")); } Ok(Self { file,