Port LegacyStructureFileFix (1493) and run the file fixes of the range in the band harness

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-13 00:35:54 +02:00
parent 748a80fedd
commit b5a6b6a99e
8 changed files with 1210 additions and 303 deletions
@@ -94,7 +94,10 @@ fn consts() -> &'static Consts {
("minecraft:sapling4", "minecraft:potted_acacia_sapling"),
("minecraft:sapling5", "minecraft:potted_dark_oak_sapling"),
("minecraft:red_mushroom0", "minecraft:potted_red_mushroom"),
("minecraft:brown_mushroom0", "minecraft:potted_brown_mushroom"),
(
"minecraft:brown_mushroom0",
"minecraft:potted_brown_mushroom",
),
("minecraft:deadbush0", "minecraft:potted_dead_bush"),
("minecraft:tallgrass2", "minecraft:potted_fern"),
("minecraft:cactus0", "minecraft:potted_cactus"),
@@ -444,8 +447,8 @@ impl Section {
if start != end {
let shift = 64 - start_bit;
let wanted = bits - shift;
storage[end] = ((storage[end] as u64) >> wanted << wanted) as i64
| (value & mask) >> shift;
storage[end] =
((storage[end] as u64) >> wanted << wanted) as i64 | (value & mask) >> shift;
}
}
set(&mut section, "BlockStates", Tag::LongArray(storage));
@@ -463,7 +466,12 @@ enum Direction {
}
fn relative(pos: i64, direction: Direction) -> i64 {
let y = (pos >> 8) + if matches!(direction, Direction::Up) { 1 } else { -1 };
let y = (pos >> 8)
+ if matches!(direction, Direction::Up) {
1
} else {
-1
};
if (0..=255).contains(&y) {
pos & 0xFF | y << 8
} else {
@@ -583,7 +591,13 @@ fn upgrade_level(level: &Tag) -> Tag {
set(
&mut out,
"TileEntities",
Tag::List(chunk.block_entities.iter().map(|(_, v)| v.clone()).collect()),
Tag::List(
chunk
.block_entities
.iter()
.map(|(_, v)| v.clone())
.collect(),
),
);
}
let mut indices = crate::nbt::Compound::new();
@@ -659,8 +673,16 @@ fn fix_block(chunk: &mut UpgradeChunk, c: &Consts, block: usize, pos: i64, alpha
if lower.name == upper.name {
let facing = lower.property("facing");
let open = lower.property("open");
let hinge = if alpha { "left" } else { upper.property("hinge") };
let powered = if alpha { "false" } else { upper.property("powered") };
let hinge = if alpha {
"left"
} else {
upper.property("hinge")
};
let powered = if alpha {
"false"
} else {
upper.property("powered")
};
let name = &lower.name;
let lo = c
.door
@@ -6,7 +6,7 @@
//! emulated here (`IntOpenHashSet` and the key order of an `Int2ObjectOpenHashMap`).
use std::collections::{HashMap, HashSet};
use super::dynamic::{as_int, as_long_stream, as_str, ceillog2, remove, set};
use super::dynamic::{as_int, as_long_stream, as_str, ceillog2, set};
use crate::nbt::Tag;
/// `PackedBitStorage`.
@@ -34,7 +34,8 @@ impl Packed {
let end = ((index + 1) * self.bits - 1) >> 6;
let start_bit = position ^ (start << 6);
let value = i64::from(value);
self.data[start] = self.data[start] & !(self.mask << start_bit) | (value & self.mask) << start_bit;
self.data[start] =
self.data[start] & !(self.mask << start_bit) | (value & self.mask) << start_bit;
if start != end {
let shift = 64 - start_bit;
let wanted = self.bits - shift;
@@ -51,7 +52,8 @@ impl Packed {
return (((self.data[start] as u64) >> start_bit) as i64 & self.mask) as i32;
}
let shift = 64 - start_bit;
((((self.data[start] as u64) >> start_bit) as i64 | self.data[end] << shift) & self.mask) as i32
((((self.data[start] as u64) >> start_bit) as i64 | self.data[end] << shift) & self.mask)
as i32
}
}
@@ -250,10 +252,15 @@ impl Section {
for i in 0..s.palette.len() {
let name = name_of(&s.palette[i]);
if leaf_index(&name).is_some() {
let persistent = as_str(s.palette[i].get("Properties").and_then(|p| p.get("decayable")))
.is_some_and(|d| d == "false");
let persistent = as_str(
s.palette[i]
.get("Properties")
.and_then(|p| p.get("decayable")),
)
.is_some_and(|d| d == "false");
s.leaf_ids.insert(i as i32);
s.state_to_id.insert(state_id(&name, persistent, 7), i as i32);
s.state_to_id
.insert(state_id(&name, persistent, 7), i as i32);
s.palette[i] = leaf_tag(&name, persistent, 7);
}
if LOGS.contains(&name.as_str()) {
@@ -503,5 +510,4 @@ pub(super) fn trapped_chest_fix(chunk: &mut Tag) {
}
}
}
let _ = remove;
}
+143 -4
View File
@@ -19,7 +19,7 @@ pub const VERSION_NAME: &str = "26.3";
const FILE_FIXER_INTRODUCTION: i32 = 4772;
/// The versions of the file fixes, in order.
const FILE_FIXES: [i32; 3] = [4772, 4773, 4899];
const FILE_FIXES: [i32; 4] = [1493, 4772, 4773, 4899];
fn invalid(message: impl Into<String>) -> io::Error {
io::Error::new(io::ErrorKind::InvalidData, message.into())
@@ -381,6 +381,141 @@ fn level_dat_to_saved_data(base: &Path) -> io::Result<()> {
write_level(base, content, V)
}
/// 1493: `LegacyStructureFileFix`. The structures of `data/*.dat` (1.12) become the
/// `Structures` of the chunks they reach, in the region folders of the old layout.
pub(crate) fn fix_1493(base: &Path) -> io::Result<()> {
use std::collections::{BTreeMap, HashMap};
const LEGACY_TO_CURRENT: [(&str, &str); 4] = [
("Iglu", "Igloo"),
("TeDP", "Desert_Pyramid"),
("TeJP", "Jungle_Pyramid"),
("TeSH", "Swamp_Hut"),
];
#[derive(Default)]
struct LegacyData {
starts: HashMap<String, Tag>,
indexes: BTreeMap<String, Vec<i64>>,
}
let pack = |x: i32, z: i32| i64::from(x) & 0xFFFF_FFFF | (i64::from(z) & 0xFFFF_FFFF) << 32;
let Some(level) = read_level(base, 1493)? else {
return Ok(());
};
let overworld_generator = match as_str(level.get("generatorName")).as_deref() {
Some("flat") => "minecraft:flat",
Some("debug_all_block_states") => "minecraft:debug",
_ => "minecraft:noise",
};
let dimensions: [(&[&str], &str, &str, &str); 3] = [
(
&["Monument", "Stronghold", "Mineshaft", "Temple", "Mansion"],
"",
"minecraft:overworld",
overworld_generator,
),
(
&["Fortress"],
"DIM-1",
"minecraft:the_nether",
"minecraft:noise",
),
(&["EndCity"], "DIM1", "minecraft:the_end", "minecraft:noise"),
];
for (ids, folder, dimension, generator) in dimensions {
let mut structures: BTreeMap<(i32, i32), LegacyData> = BTreeMap::new();
for id in ids {
let path = base.join("data").join(format!("{id}.dat"));
let Some(data) = read_saved_data(&path, Ty::SavedStructureFeatureIndices, 1493)? else {
continue;
};
let Some(Tag::Compound(features)) = data.get("Features") else {
continue;
};
let values: Vec<(String, Tag)> = features
.iter()
.map(|(k, v)| (k.to_string_lossy(), v.clone()))
.collect();
for (_, mut value) in super::items::java_hash_order(values, |(k, _)| k.clone()) {
let chunk_x = as_int(value.get("ChunkX"), 0);
let chunk_z = as_int(value.get("ChunkZ"), 0);
let children = super::dynamic::stream(value.get("Children"));
if let Some(first) = children.first()
&& let Some(current) = as_str(first.get("id"))
.and_then(|i| LEGACY_TO_CURRENT.iter().find(|(from, _)| *from == i))
{
set(&mut value, "id", string(current.1));
}
let Some(feature_id) = as_str(value.get("id")) else {
continue;
};
structures
.entry((chunk_x, chunk_z))
.or_default()
.starts
.insert(feature_id.clone(), value);
for nx in chunk_x - 8..=chunk_x + 8 {
for nz in chunk_z - 8..=chunk_z + 8 {
structures
.entry((nx, nz))
.or_default()
.indexes
.entry(feature_id.clone())
.or_default()
.push(pack(chunk_x, chunk_z));
}
}
}
}
if structures.is_empty() {
continue;
}
let region = if folder.is_empty() {
base.join("region")
} else {
base.join(folder).join("region")
};
upgrade_regions(&region, |tag| {
let version = as_int(tag.get("DataVersion"), -1);
let x = as_int(tag.get("Level").and_then(|l| l.get("xPos")), 0);
let z = as_int(tag.get("Level").and_then(|l| l.get("zPos")), 0);
let Some(legacy) = structures.get(&(x, z)) else {
return;
};
let _ = version;
super::update_chunk_to(tag, dimension, Some(generator), 1493);
let mut level = tag
.get("Level")
.cloned()
.unwrap_or_else(super::dynamic::empty_map);
let mut structure = level
.get("Structures")
.cloned()
.filter(|s| matches!(s, Tag::Compound(_)))
.unwrap_or_else(super::dynamic::empty_map);
let mut starts = structure
.get("Starts")
.cloned()
.filter(|s| matches!(s, Tag::Compound(_)))
.unwrap_or_else(super::dynamic::empty_map);
let mut references = structure
.get("References")
.cloned()
.filter(|s| matches!(s, Tag::Compound(_)))
.unwrap_or_else(super::dynamic::empty_map);
for (id, value) in &legacy.starts {
set(&mut starts, id, value.clone());
}
for (id, indexes) in &legacy.indexes {
set(&mut references, id, Tag::LongArray(indexes.clone()));
}
set(&mut structure, "Starts", starts);
set(&mut structure, "References", references);
set(&mut level, "Structures", structure);
set(tag, "Level", level);
})?;
}
Ok(())
}
/// 4773: `GeneratedStructuresRenameFileFix`.
fn fix_4773(base: &Path) -> io::Result<()> {
for ns in subdirectories(&base.join("generated")) {
@@ -456,6 +591,7 @@ pub fn fix_world_files(base: &Path) -> io::Result<i32> {
for version in FILE_FIXES {
if version > start && version <= CURRENT {
match version {
1493 => fix_1493(base)?,
4772 => fix_4772(base)?,
4773 => fix_4773(base)?,
_ => fix_4899(base)?,
@@ -678,10 +814,13 @@ pub fn upgrade_world_data(base: &Path) -> io::Result<()> {
/// A world in the layout of before 1.16 (dimension folders in the world folder,
/// `playerdata`, `stats`, `advancements`) brought to DataVersion `target` and
/// no further, without the file fixes: the harness checks the data fixes of a
/// range of versions on their own (the vanilla oracle does the same given a
/// target).
/// no further, with the file fixes of that range but not the ones that move the
/// files into the 26.x layout: the harness checks the fixes of a range of
/// versions on their own (the vanilla oracle does the same given a target).
pub fn upgrade_band(base: &Path, target: i32) -> io::Result<()> {
if target >= 1493 {
fix_1493(base)?;
}
let level = base.join("level.dat");
if let Some(mut root) = read_dat(&level)? {
let mut data = root
+51 -34
View File
@@ -11,7 +11,7 @@ use super::dynamic::{
};
use super::flat_tables as t;
use super::schema::{Key, Ty, key};
use super::{Cx, FixFn, id_is, items, mark_read, reread, reread_as};
use super::{Cx, FixFn, items, mark_read, reread, reread_as};
use crate::nbt::{Compound, JavaString, Tag};
pub(super) fn register(out: &mut Vec<(Key, FixFn)>) {
@@ -63,10 +63,16 @@ pub(super) fn register(out: &mut Vec<(Key, FixFn)>) {
})
}));
out.push((key(1474, 0), |cx| {
legacy_block_rename(cx, &only("minecraft:purple_shulker_box", "minecraft:shulker_box"))
legacy_block_rename(
cx,
&only("minecraft:purple_shulker_box", "minecraft:shulker_box"),
)
}));
out.push((key(1474, 0), |cx| {
item_rename(cx, &only("minecraft:purple_shulker_box", "minecraft:shulker_box"))
item_rename(
cx,
&only("minecraft:purple_shulker_box", "minecraft:shulker_box"),
)
}));
out.push((key(1475, 0), |cx| {
legacy_block_rename(
@@ -85,7 +91,10 @@ pub(super) fn register(out: &mut Vec<(Key, FixFn)>) {
out.push((key(1483, 0), |cx| {
item_rename(
cx,
&only("minecraft:puffer_fish_spawn_egg", "minecraft:pufferfish_spawn_egg"),
&only(
"minecraft:puffer_fish_spawn_egg",
"minecraft:pufferfish_spawn_egg",
),
)
}));
const SEAGRASS: &[(&str, &str)] = &[
@@ -96,7 +105,9 @@ pub(super) fn register(out: &mut Vec<(Key, FixFn)>) {
out.push((key(1484, 0), |cx| legacy_block_rename(cx, &table(SEAGRASS))));
out.push((key(1484, 0), heightmap_renaming));
out.push((key(1486, 0), |cx| entity_rename(cx, &table(t::COD_SALMON))));
out.push((key(1486, 0), |cx| item_rename(cx, &table(t::COD_SALMON_EGGS))));
out.push((key(1486, 0), |cx| {
item_rename(cx, &table(t::COD_SALMON_EGGS))
}));
const PRISMARINE: &[(&str, &str)] = &[
(
"minecraft:prismarine_bricks_slab",
@@ -108,7 +119,9 @@ pub(super) fn register(out: &mut Vec<(Key, FixFn)>) {
),
];
out.push((key(1487, 0), |cx| item_rename(cx, &table(PRISMARINE))));
out.push((key(1487, 0), |cx| legacy_block_rename(cx, &table(PRISMARINE))));
out.push((key(1487, 0), |cx| {
legacy_block_rename(cx, &table(PRISMARINE))
}));
out.push((key(1488, 0), |cx| {
legacy_block_rename(
cx,
@@ -133,13 +146,18 @@ pub(super) fn register(out: &mut Vec<(Key, FixFn)>) {
&table(&[
("minecraft:melon_block", "minecraft:melon"),
("minecraft:melon", "minecraft:melon_slice"),
("minecraft:speckled_melon", "minecraft:glistering_melon_slice"),
(
"minecraft:speckled_melon",
"minecraft:glistering_melon_slice",
),
]),
)
}));
out.push((key(1492, 0), structure_template_rename));
out.push((key(1494, 0), enchantment_names));
out.push((key(1496, 0), |cx| cx.each(Ty::Chunk, chunk_leaves::leaves_fix)));
out.push((key(1496, 0), |cx| {
cx.each(Ty::Chunk, chunk_leaves::leaves_fix)
}));
out.push((key(1500, 0), |cx| {
cx.block_entity("DUMMY", |be| set(be, "keepPacked", Tag::Byte(1)))
}));
@@ -155,9 +173,13 @@ pub(super) fn register(out: &mut Vec<(Key, FixFn)>) {
out.push((key(1510, 0), |cx| {
rename_names(cx, Ty::Recipe, &table(t::RECIPES_RENAMENING))
}));
out.push((key(1510, 0), |cx| entity_rename(cx, &the_renamening_entities)));
out.push((key(1510, 0), |cx| {
entity_rename(cx, &the_renamening_entities)
}));
out.push((key(1510, 0), swim_stats_rename));
out.push((key(1514, 0), |cx| display_name_to_component(cx, Ty::Objective)));
out.push((key(1514, 0), |cx| {
display_name_to_component(cx, Ty::Objective)
}));
out.push((key(1514, 0), |cx| display_name_to_component(cx, Ty::Team)));
out.push((key(1514, 0), objective_render_type));
out.push((key(1515, 0), |cx| {
@@ -264,12 +286,7 @@ fn the_renamening_items(name: &str) -> Option<String> {
)
}
/// `LegacyComponentDataFixUtils.createTextComponentJson`.
fn text_json(text: &str) -> Tag {
let units: Vec<u16> = text.encode_utf16().collect();
Tag::String(JavaString(super::json::text_component_json(&units)))
}
/// `LegacyComponentDataFixUtils.createTextComponentJson` of a string tag's text.
fn text_json_units(text: &JavaString) -> Tag {
Tag::String(JavaString(super::json::text_component_json(&text.0)))
}
@@ -346,7 +363,9 @@ fn entity_block_state(cx: &mut Cx) {
remove(e, f);
}
}
"minecraft:enderman" => block_to_state(e, "carried", "carriedData", "carriedBlockState"),
"minecraft:enderman" => {
block_to_state(e, "carried", "carriedData", "carriedBlockState")
}
"minecraft:arrow" | "minecraft:spectral_arrow" => {
block_to_state(e, "inTile", "inData", "inBlockState")
}
@@ -396,7 +415,8 @@ fn block_name_flattening(cx: &mut Cx) {
/// `ItemStackTheFlatteningFix.updateItem`.
fn update_item(name: &str, data: i32) -> Option<&'static str> {
static MAP: OnceLock<(HashMap<&'static str, &'static str>, Vec<&'static str>)> = OnceLock::new();
static MAP: OnceLock<(HashMap<&'static str, &'static str>, Vec<&'static str>)> =
OnceLock::new();
let (map, ids) = MAP.get_or_init(|| {
let map: HashMap<_, _> = t::ITEM_FLATTENING.iter().copied().collect();
let mut ids: Vec<&str> = t::ITEM_FLATTENING
@@ -550,9 +570,7 @@ fn flat_generator_string(options: &str) -> String {
to_int(block_string, 0)
};
let data_index = name_index + 1;
let data = block_parts
.get(data_index)
.map_or(0, |d| to_int(d, 0));
let data = block_parts.get(data_index).map_or(0, |d| to_int(d, 0));
let tag = bsd::get_tag(block << 4 | data);
let name = as_str(tag.get("Name")).unwrap_or_default();
format!(
@@ -579,7 +597,11 @@ fn flat_generator_info(cx: &mut Cx) {
return;
}
if let Some(options) = as_str(l.get("generatorOptions")) {
set(l, "generatorOptions", string(&flat_generator_string(&options)));
set(
l,
"generatorOptions",
string(&flat_generator_string(&options)),
);
}
});
}
@@ -608,10 +630,7 @@ fn unpack_legacy_key(k: &str) -> Option<(String, String)> {
let prefix = &k[..split];
let rest = k[split + 1..].replace('.', ":");
if prefix == "stat.mineBlock" {
return Some((
"minecraft:mined".to_owned(),
bsd::upgrade_block_name(&rest),
));
return Some(("minecraft:mined".to_owned(), bsd::upgrade_block_name(&rest)));
}
if let Some(item_key) = lookup(t::STATS_ITEM_KEYS, prefix) {
let new = update_item(&rest, 0).map_or(rest.clone(), str::to_owned);
@@ -1068,7 +1087,8 @@ fn flat_options(flat: &str) -> Tag {
continue;
}
let mut options = Vec::new();
if separated.len() > 1 && separated[1].ends_with(')') && separated[1].len() > 1 {
if separated.len() > 1 && separated[1].ends_with(')') && separated[1].len() > 1
{
let inner = &separated[1][..separated[1].len() - 1];
for part in java_split(inner, " ", 0) {
let split = java_split(part, "=", 2);
@@ -1105,7 +1125,10 @@ fn flat_options(flat: &str) -> Tag {
.into_iter()
.map(|(k, v)| (super::dynamic::key(&k), string(&v)))
.collect();
(super::dynamic::key(&name.to_lowercase()), Tag::Compound(options))
(
super::dynamic::key(&name.to_lowercase()),
Tag::Compound(options),
)
})
.collect();
Tag::compound([
@@ -1189,9 +1212,3 @@ fn objective_render_type(cx: &mut Cx) {
}
});
}
#[allow(dead_code)]
fn unused(_: &Tag) -> bool {
let _ = (text_json, id_is);
false
}
File diff suppressed because it is too large Load Diff
@@ -45,7 +45,9 @@ impl Walker<'_> {
};
match id.as_str() {
"minecraft:area_effect_cloud" => self.field(Ty::Particle, v, "Particle"),
"minecraft:arrow" | "minecraft:spectral_arrow" => block(self, v, "inTile", "inBlockState"),
"minecraft:arrow" | "minecraft:spectral_arrow" => {
block(self, v, "inTile", "inBlockState")
}
"minecraft:egg"
| "minecraft:ender_pearl"
| "minecraft:fireball"
@@ -95,7 +97,9 @@ impl Walker<'_> {
block(self, v, "Block", "BlockState");
self.field(Ty::BlockEntity, v, "TileEntityData");
}
"minecraft:fireworks_rocket" if !renamed => self.field(Ty::ItemStack, v, "FireworksItem"),
"minecraft:fireworks_rocket" if !renamed => {
self.field(Ty::ItemStack, v, "FireworksItem")
}
"minecraft:firework_rocket" if renamed => self.field(Ty::ItemStack, v, "FireworksItem"),
"minecraft:item" | "minecraft:item_frame" => self.field(Ty::ItemStack, v, "Item"),
"minecraft:horse" => {
@@ -21,6 +21,8 @@ import net.minecraft.server.Bootstrap;
import net.minecraft.server.level.ChunkMap;
import net.minecraft.util.datafix.DataFixTypes;
import net.minecraft.util.datafix.DataFixers;
import net.minecraft.util.filefix.FileFix;
import net.minecraft.util.filefix.FileFixerUpper;
import net.minecraft.util.worldupdate.UpgradeProgress;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;
@@ -123,6 +125,16 @@ public final class VanillaDataFixOracle {
*/
private static void band(Path out, int target) throws Exception {
var fixer = DataFixers.getDataFixer();
// The file fixes of the range (1493, LegacyStructureFileFix), as FileFixerUpper runs them
// (in place; the copy-on-write view and the level.dat rewrite are not what is checked).
var fileFixes = FileFixerUpper.class.getDeclaredField("fileFixes");
fileFixes.setAccessible(true);
for (Object o : (List<?>) fileFixes.get(DataFixers.getFileFixer())) {
FileFix fileFix = (FileFix) o;
if (fileFix.getVersion() <= target) {
fileFix.runFixOperations(out, new UpgradeProgress());
}
}
Path levelFile = out.resolve("level.dat");
if (Files.exists(levelFile)) {
CompoundTag root = NbtIo.readCompressed(levelFile, NbtAccounter.unlimitedHeap());
+2
View File
@@ -7,6 +7,7 @@ data. Run from the repository root; the vanilla sources are the ones under .loc
"""
import argparse
import re
import subprocess
import sys
from pathlib import Path
@@ -137,6 +138,7 @@ def main():
structure_renames(),
]
Path(args.out).write_text('\n\n'.join(parts) + '\n')
subprocess.run(['rustfmt', '--edition', '2024', args.out], check=True)
print('wrote', args.out)