Merge branch 'parity/old-saves-flat' (1.12.2 to 1.13.2 fixes)

This commit is contained in:
Ludovic Marie
2026-10-13 00:48:14 +02:00
13 changed files with 7509 additions and 5 deletions
@@ -0,0 +1,199 @@
//! `BlockStateData`: the table that maps the 1.12 block states (`blockId << 4 | data`)
//! to 1.13 block states, and the 1.12 `Name` + `Properties` tags to those ids.
//! The table itself is `block_state_data.txt`, generated from vanilla's source by
//! `tools/gen_block_state_data.py`.
//!
//! Vanilla keeps one tag object per `register` call (and per defaulted id) and
//! `ChunkPalettedStorageFix` compares them by identity, so a state is an
//! *instance* here: `register` calls make instances, ids without a call share
//! the instance of the first call for their block.
use std::collections::HashMap;
use std::sync::OnceLock;
use super::dynamic::{as_str, key, string};
use crate::nbt::{Compound, Tag};
/// A block state: name and properties sorted by key.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct State {
pub name: String,
pub props: Vec<(String, String)>,
}
impl State {
pub fn new(name: &str, props: &[(&str, &str)]) -> Self {
let mut props: Vec<(String, String)> = props
.iter()
.map(|(k, v)| ((*k).to_owned(), (*v).to_owned()))
.collect();
props.sort();
Self {
name: name.to_owned(),
props,
}
}
pub fn property(&self, name: &str) -> &str {
self.props
.iter()
.find(|(k, _)| k == name)
.map_or("", |(_, v)| v)
}
/// The NBT tag: `{Name}` or `{Name, Properties}`.
pub fn to_tag(&self) -> Tag {
let mut tag = Tag::compound([("Name", string(&self.name))]);
if !self.props.is_empty() {
let mut props = Compound::new();
for (k, v) in &self.props {
props.insert(key(k), string(v));
}
super::dynamic::set(&mut tag, "Properties", Tag::Compound(props));
}
tag
}
/// A tag read back as a state (`Name` and an optional `Properties` of strings only).
pub fn from_tag(tag: &Tag) -> Option<Self> {
let Tag::Compound(c) = tag else {
return None;
};
let name = as_str(c.get(&key("Name")))?;
let mut props = Vec::new();
let mut expected = 1;
if let Some(p) = c.get(&key("Properties")) {
expected += 1;
let Tag::Compound(p) = p else {
return None;
};
for (k, v) in p {
props.push((k.to_string_lossy(), as_str(Some(v))?));
}
}
if c.len() != expected {
return None;
}
props.sort();
Some(Self { name, props })
}
}
pub struct Data {
/// One entry per `register` call.
pub instances: Vec<State>,
/// `MAP`: the instance of each id (after the defaulting of `finalizeMaps`).
map: Vec<Option<u32>>,
id_by_old: HashMap<State, i32>,
id_by_old_name: HashMap<String, i32>,
}
fn parse_props(s: &str) -> Vec<(String, String)> {
if s.is_empty() {
return Vec::new();
}
s.split(';')
.map(|kv| {
let (k, v) = kv.split_once('=').unwrap_or((kv, ""));
(k.to_owned(), v.to_owned())
})
.collect()
}
pub fn data() -> &'static Data {
static DATA: OnceLock<Data> = OnceLock::new();
DATA.get_or_init(|| {
let mut map: Vec<Option<u32>> = vec![None; 4096];
let mut defaults: Vec<Option<u32>> = vec![None; 256];
let mut instances = Vec::new();
let mut id_by_old = HashMap::new();
let mut id_by_old_name: HashMap<String, i32> = HashMap::new();
for line in include_str!("block_state_data.txt").lines() {
if line.starts_with('#') || line.is_empty() {
continue;
}
let f: Vec<&str> = line.split('\t').collect();
let id: usize = f[0].parse().expect("id");
let instance = instances.len() as u32;
instances.push(State {
name: f[1].to_owned(),
props: parse_props(f.get(2).copied().unwrap_or("")),
});
map[id] = Some(instance);
if defaults[id >> 4].is_none() {
defaults[id >> 4] = Some(instance);
}
for pair in f[3..].chunks(2) {
let old = State {
name: pair[0].to_owned(),
props: parse_props(pair.get(1).copied().unwrap_or("")),
};
id_by_old_name.entry(old.name.clone()).or_insert(id as i32);
id_by_old.insert(old, id as i32);
}
}
for (i, slot) in map.iter_mut().enumerate() {
if slot.is_none() {
*slot = defaults[i >> 4];
}
}
Data {
instances,
map,
id_by_old,
id_by_old_name,
}
})
}
impl Data {
/// `getTag(id)` as an instance.
pub fn instance(&self, id: i32) -> u32 {
let slot = usize::try_from(id)
.ok()
.filter(|i| *i < 4096)
.and_then(|i| self.map[i]);
slot.or(self.map[0]).expect("air is registered")
}
pub fn state(&self, instance: u32) -> &State {
&self.instances[instance as usize]
}
}
/// `BlockStateData.getTag`.
pub fn get_tag(id: i32) -> Tag {
let d = data();
d.state(d.instance(id)).to_tag()
}
/// `BlockStateData.upgradeBlockStateTag`.
pub fn upgrade_block_state_tag(old: &Tag) -> Tag {
let d = data();
match State::from_tag(old).and_then(|s| d.id_by_old.get(&s).copied()) {
Some(id) if (0..4096).contains(&id) => match d.map[id as usize] {
Some(i) => d.state(i).to_tag(),
None => old.clone(),
},
_ => old.clone(),
}
}
/// `BlockStateData.upgradeBlock(String)`.
pub fn upgrade_block_name(old: &str) -> String {
let d = data();
match d.id_by_old_name.get(old) {
Some(&id) if (0..4096).contains(&id) => match d.map[id as usize] {
Some(i) => d.state(i).name.clone(),
None => old.to_owned(),
},
_ => old.to_owned(),
}
}
/// `BlockStateData.upgradeBlock(int)`.
pub fn upgrade_block_id(id: i32) -> String {
let d = data();
match usize::try_from(id).ok().filter(|i| *i < 4096) {
Some(i) => match d.map[i] {
Some(inst) => d.state(inst).name.clone(),
None => "minecraft:air".to_owned(),
},
None => "minecraft:air".to_owned(),
}
}
File diff suppressed because one or more lines are too long
@@ -0,0 +1,771 @@
//! `ChunkPalettedStorageFix` (key 1451.1): the chunk sections of the 1.12 format
//! (`Blocks`, `Data`, `Add` byte arrays) become a palette and packed `BlockStates`,
//! with the block entities that carried block state (note blocks, flower pots,
//! beds, banners, skulls) folded into the states, as vanilla does.
//!
//! Vanilla compares block states by identity (`CrudeIncrementalIntIdentityHashBiMap`
//! and an identity set): states are instances here, numbered in one space with the
//! instances of the `BlockStateData` table.
use std::collections::{HashMap, HashSet};
use std::sync::OnceLock;
use super::block_state_data::{self as bsd, State};
use super::dynamic::{as_bool, as_byte_buffer, as_int, as_str, ceillog2, key, set};
use crate::nbt::Tag;
/// `MappingConstants`.
struct Consts {
/// Instances: the `BlockStateData` ones first, then the constants below.
states: Vec<State>,
air: u32,
pumpkin: u32,
snowy_podzol: u32,
snowy_grass: u32,
snowy_mycelium: u32,
upper: HashMap<&'static str, u32>,
flower_pot: HashMap<String, u32>,
skull: HashMap<String, u32>,
door: HashMap<String, u32>,
note_block: HashMap<String, u32>,
bed: HashMap<String, u32>,
banner: HashMap<String, u32>,
}
const DYE_COLORS: [&str; 16] = [
"white",
"orange",
"magenta",
"light_blue",
"yellow",
"lime",
"pink",
"gray",
"light_gray",
"cyan",
"purple",
"blue",
"brown",
"green",
"red",
"black",
];
fn consts() -> &'static Consts {
static C: OnceLock<Consts> = OnceLock::new();
C.get_or_init(|| {
let mut states = bsd::data().instances.clone();
let mut add = |name: &str, props: &[(&str, &str)]| -> u32 {
states.push(State::new(name, props));
(states.len() - 1) as u32
};
let air = add("minecraft:air", &[]);
let pumpkin = add("minecraft:pumpkin", &[]);
let snowy_podzol = add("minecraft:podzol", &[("snowy", "true")]);
let snowy_grass = add("minecraft:grass_block", &[("snowy", "true")]);
let snowy_mycelium = add("minecraft:mycelium", &[("snowy", "true")]);
let mut upper = HashMap::new();
for name in [
"minecraft:sunflower",
"minecraft:lilac",
"minecraft:tall_grass",
"minecraft:large_fern",
"minecraft:rose_bush",
"minecraft:peony",
] {
upper.insert(name, add(name, &[("half", "upper")]));
}
let mut flower_pot = HashMap::new();
let pots: [(&str, &str); 22] = [
("minecraft:air0", "minecraft:flower_pot"),
("minecraft:red_flower0", "minecraft:potted_poppy"),
("minecraft:red_flower1", "minecraft:potted_blue_orchid"),
("minecraft:red_flower2", "minecraft:potted_allium"),
("minecraft:red_flower3", "minecraft:potted_azure_bluet"),
("minecraft:red_flower4", "minecraft:potted_red_tulip"),
("minecraft:red_flower5", "minecraft:potted_orange_tulip"),
("minecraft:red_flower6", "minecraft:potted_white_tulip"),
("minecraft:red_flower7", "minecraft:potted_pink_tulip"),
("minecraft:red_flower8", "minecraft:potted_oxeye_daisy"),
("minecraft:yellow_flower0", "minecraft:potted_dandelion"),
("minecraft:sapling0", "minecraft:potted_oak_sapling"),
("minecraft:sapling1", "minecraft:potted_spruce_sapling"),
("minecraft:sapling2", "minecraft:potted_birch_sapling"),
("minecraft:sapling3", "minecraft:potted_jungle_sapling"),
("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:deadbush0", "minecraft:potted_dead_bush"),
("minecraft:tallgrass2", "minecraft:potted_fern"),
("minecraft:cactus0", "minecraft:potted_cactus"),
];
for (k, name) in pots {
flower_pot.insert(k.to_owned(), add(name, &[]));
}
let mut skull = HashMap::new();
for (i, name, ty) in [
(0, "skeleton", "skull"),
(1, "wither_skeleton", "skull"),
(2, "zombie", "head"),
(3, "player", "head"),
(4, "creeper", "head"),
(5, "dragon", "head"),
] {
for facing in ["north", "east", "south", "west"] {
skull.insert(
format!("{i}{facing}"),
add(
&format!("minecraft:{name}_wall_{ty}"),
&[("facing", facing)],
),
);
}
for rot in 0..16 {
skull.insert(
format!("{i}{rot}"),
add(
&format!("minecraft:{name}_{ty}"),
&[("rotation", &rot.to_string())],
),
);
}
}
let mut door = HashMap::new();
for ty in [
"oak_door",
"iron_door",
"spruce_door",
"birch_door",
"jungle_door",
"acacia_door",
"dark_oak_door",
] {
let id = format!("minecraft:{ty}");
for facing in ["east", "north", "south", "west"] {
for half in ["lower", "upper"] {
for hinge in ["left", "right"] {
for open in ["false", "true"] {
for powered in ["false", "true"] {
door.insert(
format!("{id}{facing}{half}{hinge}{open}{powered}"),
add(
&id,
&[
("facing", facing),
("half", half),
("hinge", hinge),
("open", open),
("powered", powered),
],
),
);
}
}
}
}
}
}
let mut note_block = HashMap::new();
for i in 0..26 {
for powered in ["true", "false"] {
note_block.insert(
format!("{powered}{i}"),
add(
"minecraft:note_block",
&[("powered", powered), ("note", &i.to_string())],
),
);
}
}
let mut bed = HashMap::new();
for (id, color) in DYE_COLORS.iter().enumerate() {
if *color == "red" {
continue;
}
let name = format!("minecraft:{color}_bed");
for (facing, occupied, part) in [
("south", "false", "foot"),
("west", "false", "foot"),
("north", "false", "foot"),
("east", "false", "foot"),
("south", "false", "head"),
("west", "false", "head"),
("north", "false", "head"),
("east", "false", "head"),
("south", "true", "head"),
("west", "true", "head"),
("north", "true", "head"),
("east", "true", "head"),
] {
bed.insert(
format!("{facing}{occupied}{part}{id}"),
add(
&name,
&[("facing", facing), ("occupied", occupied), ("part", part)],
),
);
}
}
let mut banner = HashMap::new();
for (id, color) in DYE_COLORS.iter().enumerate() {
if *color == "white" {
continue;
}
let color_id = 15 - id;
for i in 0..16 {
banner.insert(
format!("{i}_{color_id}"),
add(
&format!("minecraft:{color}_banner"),
&[("rotation", &i.to_string())],
),
);
}
for facing in ["north", "south", "west", "east"] {
banner.insert(
format!("{facing}_{color_id}"),
add(
&format!("minecraft:{color}_wall_banner"),
&[("facing", facing)],
),
);
}
}
Consts {
states,
air,
pumpkin,
snowy_podzol,
snowy_grass,
snowy_mycelium,
upper,
flower_pot,
skull,
door,
note_block,
bed,
banner,
}
})
}
/// The block ids (`id >> 4`) whose blocks need a second look.
const FIX: [usize; 18] = [
2, 3, 110, 140, 144, 25, 86, 26, 176, 177, 175, 64, 71, 193, 194, 195, 196, 197,
];
const VIRTUAL: &[usize] = &[
54, 146, 25, 26, 51, 53, 67, 108, 109, 114, 128, 134, 135, 136, 156, 163, 164, 180, 203, 55,
85, 113, 188, 189, 190, 191, 192, 93, 94, 101, 102, 160, 106, 107, 183, 184, 185, 186, 187,
132, 139, 199,
];
fn is_virtual(block: usize) -> bool {
VIRTUAL.contains(&block)
}
/// `getSideMask`.
fn side_mask(west: bool, east: bool, north: bool, south: bool) -> i32 {
let mut s = 0;
if north {
if east {
s |= 2;
} else if west {
s |= 128;
} else {
s |= 1;
}
} else if south {
if west {
s |= 32;
} else if east {
s |= 8;
} else {
s |= 16;
}
} else if east {
s |= 4;
} else if west {
s |= 64;
}
s
}
/// A `DataLayer`: 4 bits per block.
struct Nibbles(Vec<u8>);
impl Nibbles {
fn get(&self, x: usize, y: usize, z: usize) -> i32 {
let position = y << 8 | z << 4 | x;
let byte = self.0[position >> 1];
if position & 1 == 0 {
i32::from(byte & 15)
} else {
i32::from(byte >> 4 & 15)
}
}
}
struct Section {
y: i32,
has_data: bool,
/// The `listTag`: instances in the order they were first seen.
list: Vec<u32>,
seen: HashSet<u32>,
/// `palette`: instance by palette id.
palette: Vec<u32>,
palette_ids: HashMap<u32, i32>,
to_fix: Vec<(usize, Vec<usize>)>,
update: Vec<i32>,
buffer: Vec<i32>,
raw: Tag,
}
impl Section {
fn new(raw: Tag) -> Self {
let y = as_int(raw.get("Y"), 0);
let has_data = raw.get("Blocks").is_some();
Self {
y,
has_data,
list: Vec::new(),
seen: HashSet::new(),
palette: Vec::new(),
palette_ids: HashMap::new(),
to_fix: Vec::new(),
update: Vec::new(),
buffer: vec![0; 4096],
raw,
}
}
fn get_block(&self, pos: usize) -> u32 {
if pos <= 4095 {
self.palette
.get(self.buffer[pos] as usize)
.copied()
.unwrap_or_else(|| consts().air)
} else {
consts().air
}
}
fn set_block(&mut self, idx: usize, state: u32) {
if self.seen.insert(state) {
self.list.push(state);
}
let id = match self.palette_ids.get(&state) {
Some(id) => *id,
None => {
self.palette.push(state);
let id = (self.palette.len() - 1) as i32;
self.palette_ids.insert(state, id);
id
}
};
self.buffer[idx] = id;
}
fn add_fix(&mut self, block: usize, position: usize) {
match self.to_fix.iter_mut().find(|(b, _)| *b == block) {
Some((_, list)) => list.push(position),
None => self.to_fix.push((block, vec![position])),
}
}
fn upgrade(&mut self, mut sides: i32) -> i32 {
if !self.has_data {
return sides;
}
let blocks = as_byte_buffer(self.raw.get("Blocks")).unwrap_or_default();
let nibbles = |name: &str| {
as_byte_buffer(self.raw.get(name))
.map(Nibbles)
.unwrap_or_else(|| Nibbles(vec![0; 2048]))
};
let data = nibbles("Data");
let add_blocks = nibbles("Add");
let c = consts();
self.seen.insert(c.air);
self.palette.push(c.air);
self.palette_ids.insert(c.air, 0);
self.list.push(c.air);
let d = bsd::data();
for idx in 0..4096usize {
let (xx, yy, zz) = (idx & 15, idx >> 8 & 15, idx >> 4 & 15);
let id = add_blocks.get(xx, yy, zz) << 12
| i32::from(blocks.get(idx).copied().unwrap_or(0)) << 4
| data.get(xx, yy, zz);
let block = (id >> 4) as usize;
if FIX.contains(&block) {
self.add_fix(block, idx);
}
if is_virtual(block) {
let s = side_mask(xx == 0, xx == 15, zz == 0, zz == 15);
if s == 0 {
self.update.push(idx as i32);
} else {
sides |= s;
}
}
self.set_block(idx, d.instance(id));
}
sides
}
fn write(&self) -> Tag {
let mut section = self.raw.clone();
if !self.has_data {
return section;
}
let c = consts();
let palette: Vec<Tag> = self
.list
.iter()
.map(|s| {
let state = &c.states[*s as usize];
if state.name == "%%FILTER_ME%%" {
c.states[c.air as usize].to_tag()
} else {
state.to_tag()
}
})
.collect();
set(&mut section, "Palette", Tag::List(palette));
let bits = ceillog2(self.seen.len()).max(4) as usize;
let mut storage = vec![0i64; (4096 * bits).div_ceil(64)];
let mask = (1i64 << bits) - 1;
for (index, value) in self.buffer.iter().enumerate() {
let value = i64::from(*value);
let position = index * bits;
let start = position >> 6;
let end = ((index + 1) * bits - 1) >> 6;
let start_bit = position ^ (start << 6);
storage[start] = storage[start] & !(mask << start_bit) | (value & mask) << start_bit;
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;
}
}
set(&mut section, "BlockStates", Tag::LongArray(storage));
for f in ["Blocks", "Data", "Add"] {
super::dynamic::remove(&mut section, f);
}
section
}
}
#[derive(Clone, Copy)]
enum Direction {
Down,
Up,
}
fn relative(pos: i64, direction: Direction) -> i64 {
let y = (pos >> 8)
+ if matches!(direction, Direction::Up) {
1
} else {
-1
};
if (0..=255).contains(&y) {
pos & 0xFF | y << 8
} else {
-1
}
}
struct UpgradeChunk {
sides: i32,
sections: Vec<Option<Section>>,
/// `Int2ObjectLinkedOpenHashMap`: insertion order, a repeated key keeps its place.
block_entities: Vec<(i64, Tag)>,
}
impl UpgradeChunk {
fn block_entity(&self, pos: i64) -> Option<&Tag> {
self.block_entities
.iter()
.find(|(k, _)| *k == pos)
.map(|(_, v)| v)
}
fn remove_block_entity(&mut self, pos: i64) -> Option<Tag> {
let i = self.block_entities.iter().position(|(k, _)| *k == pos)?;
Some(self.block_entities.remove(i).1)
}
fn section(&self, pos: i64) -> Option<&Section> {
self.sections.get((pos >> 12) as usize)?.as_ref()
}
fn get_block(&self, pos: i64) -> u32 {
if (0..=65535).contains(&pos) {
match self.section(pos) {
Some(s) => s.get_block((pos & 4095) as usize),
None => consts().air,
}
} else {
consts().air
}
}
fn set_block(&mut self, pos: i64, block: u32) {
if (0..=65535).contains(&pos)
&& let Some(Some(s)) = self.sections.get_mut((pos >> 12) as usize)
{
s.set_block((pos & 4095) as usize, block);
}
}
fn name(&self, pos: i64) -> &str {
&consts().states[self.get_block(pos) as usize].name
}
fn state(&self, pos: i64) -> &State {
&consts().states[self.get_block(pos) as usize]
}
}
/// `ChunkPalettedStorageFix.fix` over a chunk (`{Level: ...}`).
pub(super) fn fix(chunk: &mut Tag) {
let Some(level) = chunk.get_mut("Level") else {
return;
};
if !matches!(level.get("Sections"), Some(Tag::List(_))) {
return;
}
let new_level = upgrade_level(level);
*level = new_level;
}
fn upgrade_level(level: &Tag) -> Tag {
let c = consts();
let x0 = as_int(level.get("xPos"), 0) << 4;
let z0 = as_int(level.get("zPos"), 0) << 4;
let mut chunk = UpgradeChunk {
sides: 0,
sections: (0..16).map(|_| None).collect(),
block_entities: Vec::new(),
};
if let Some(Tag::List(entities)) = level.get("TileEntities") {
for entity in entities {
let x = i64::from(as_int(entity.get("x"), 0).wrapping_sub(x0) & 15);
let y = i64::from(as_int(entity.get("y"), 0));
let z = i64::from(as_int(entity.get("z"), 0).wrapping_sub(z0) & 15);
let k = (y << 8) | (z << 4) | x;
match chunk.block_entities.iter_mut().find(|(key, _)| *key == k) {
Some((_, v)) => *v = entity.clone(),
None => chunk.block_entities.push((k, entity.clone())),
}
}
}
let converted_from_alpha = as_bool(level.get("convertedFromAlphaFormat"), false);
if let Some(Tag::List(sections)) = level.get("Sections") {
for raw in sections {
let mut section = Section::new(raw.clone());
chunk.sides = section.upgrade(chunk.sides);
let y = section.y;
if (0..16).contains(&y) {
chunk.sections[y as usize] = Some(section);
}
}
}
for si in 0..16 {
let Some(section) = &chunk.sections[si] else {
continue;
};
let to_fix = section.to_fix.clone();
let section_y = section.y as i64;
for (block, positions) in to_fix {
let dy = section_y << 12;
for pos in positions {
let pos = pos as i64 | dy;
fix_block(&mut chunk, c, block, pos, converted_from_alpha);
}
}
}
// write()
let mut out = level.clone();
if chunk.block_entities.is_empty() {
super::dynamic::remove(&mut out, "TileEntities");
} else {
set(
&mut out,
"TileEntities",
Tag::List(
chunk
.block_entities
.iter()
.map(|(_, v)| v.clone())
.collect(),
),
);
}
let mut indices = crate::nbt::Compound::new();
let mut sections = Vec::new();
for section in chunk.sections.iter().flatten() {
sections.push(section.write());
indices.insert(
key(&section.y.to_string()),
Tag::IntArray(section.update.clone()),
);
}
let upgrade_data = Tag::compound([
("Sides", Tag::Byte(chunk.sides as i8)),
("Indices", Tag::Compound(indices)),
]);
set(&mut out, "UpgradeData", upgrade_data);
set(&mut out, "Sections", Tag::List(sections));
out
}
fn fix_block(chunk: &mut UpgradeChunk, c: &Consts, block: usize, pos: i64, alpha: bool) {
let up = relative(pos, Direction::Up);
let down = relative(pos, Direction::Down);
let is_snow = |n: &str| n == "minecraft:snow" || n == "minecraft:snow_layer";
match block {
2 => {
if chunk.name(pos) == "minecraft:grass_block" && is_snow(chunk.name(up)) {
chunk.set_block(pos, c.snowy_grass);
}
}
3 => {
if chunk.name(pos) == "minecraft:podzol" && is_snow(chunk.name(up)) {
chunk.set_block(pos, c.snowy_podzol);
}
}
25 => {
if let Some(entity) = chunk.remove_block_entity(pos) {
let note = as_int(entity.get("note"), 0).clamp(0, 24) as i8;
let k = format!("{}{}", as_bool(entity.get("powered"), false), note);
let state = c
.note_block
.get(&k)
.or_else(|| c.note_block.get("false0"))
.copied()
.expect("note block");
chunk.set_block(pos, state);
}
}
26 => {
if let Some(entity) = chunk.block_entity(pos) {
let color = as_int(entity.get("color"), 0);
let state = chunk.state(pos);
if color != 14 && (0..16).contains(&color) {
let k = format!(
"{}{}{}{}",
state.property("facing"),
state.property("occupied"),
state.property("part"),
color
);
if let Some(s) = c.bed.get(&k).copied() {
chunk.set_block(pos, s);
}
}
}
}
64 | 71 | 193..=197 => {
if chunk.name(pos).ends_with("_door") {
let lower = chunk.state(pos).clone();
if lower.property("half") == "lower" {
let above = up;
let upper = chunk.state(above).clone();
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 name = &lower.name;
let lo = c
.door
.get(&format!("{name}{facing}lower{hinge}{open}{powered}"))
.copied();
let hi = c
.door
.get(&format!("{name}{facing}upper{hinge}{open}{powered}"))
.copied();
if let (Some(lo), Some(hi)) = (lo, hi) {
chunk.set_block(pos, lo);
chunk.set_block(above, hi);
}
}
}
}
}
86 => {
if chunk.name(pos) == "minecraft:carved_pumpkin" {
let below = chunk.name(down);
if below == "minecraft:grass_block" || below == "minecraft:dirt" {
chunk.set_block(pos, c.pumpkin);
}
}
}
110 => {
if chunk.name(pos) == "minecraft:mycelium" && is_snow(chunk.name(up)) {
chunk.set_block(pos, c.snowy_mycelium);
}
}
140 => {
if let Some(entity) = chunk.remove_block_entity(pos) {
let item = as_str(entity.get("Item")).unwrap_or_default();
let k = format!("{}{}", item, as_int(entity.get("Data"), 0));
let state = c
.flower_pot
.get(&k)
.or_else(|| c.flower_pot.get("minecraft:air0"))
.copied()
.expect("flower pot");
chunk.set_block(pos, state);
}
}
144 => {
if let Some(entity) = chunk.block_entity(pos) {
let ty = as_int(entity.get("SkullType"), 0).to_string();
let facing = chunk.state(pos).property("facing").to_owned();
let k = if facing != "up" && facing != "down" {
format!("{ty}{facing}")
} else {
format!("{ty}{}", as_int(entity.get("Rot"), 0))
};
// `entity.remove(...)` on an immutable Dynamic changes nothing.
let state = c
.skull
.get(&k)
.or_else(|| c.skull.get("0north"))
.copied()
.expect("skull");
chunk.set_block(pos, state);
}
}
175 => {
if chunk.state(pos).property("half") == "upper" {
let variant = chunk.name(down).to_owned();
if let Some(s) = c.upper.get(variant.as_str()).copied() {
chunk.set_block(pos, s);
}
}
}
176 | 177 => {
if let Some(entity) = chunk.block_entity(pos) {
let color = as_int(entity.get("Base"), 0);
let state = chunk.state(pos);
if color != 15 && (0..16).contains(&color) {
let prop = state.property(if block == 176 { "rotation" } else { "facing" });
let k = format!("{prop}_{color}");
if let Some(s) = c.banner.get(&k).copied() {
chunk.set_block(pos, s);
}
}
}
}
_ => {}
}
}
@@ -0,0 +1,513 @@
//! `LeavesFix` (key 1496) and `TrappedChestBlockEntityFix` (key 1624): fixes that
//! read the packed block states of the sections of a chunk.
//!
//! `LeavesFix` visits blocks in the iteration order of fastutil hash sets, and the
//! order decides in which order its new palette entries appear: the sets are
//! 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, set};
use crate::nbt::Tag;
/// `PackedBitStorage`.
#[derive(Clone)]
struct Packed {
data: Vec<i64>,
bits: usize,
mask: i64,
}
impl Packed {
fn new(bits: usize, data: Vec<i64>) -> Option<Self> {
(data.len() == (4096 * bits).div_ceil(64)).then(|| Self {
data,
bits,
mask: (1i64 << bits) - 1,
})
}
fn zeroed(bits: usize) -> Self {
Self::new(bits, vec![0; (4096 * bits).div_ceil(64)]).expect("length")
}
fn set(&mut self, index: usize, value: i32) {
let position = index * self.bits;
let start = position >> 6;
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;
if start != end {
let shift = 64 - start_bit;
let wanted = self.bits - shift;
self.data[end] =
((self.data[end] as u64) >> wanted << wanted) as i64 | (value & self.mask) >> shift;
}
}
fn get(&self, index: usize) -> i32 {
let position = index * self.bits;
let start = position >> 6;
let end = ((index + 1) * self.bits - 1) >> 6;
let start_bit = position ^ (start << 6);
if start == end {
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
}
}
// ------------------------------------------------------------------ fastutil
fn array_size(expected: usize) -> usize {
// arraySize(expected, 0.75f): nextPowerOfTwo(ceil(expected / f)), at least 2
let need = (expected as f64 / 0.75f32 as f64).ceil() as usize;
need.next_power_of_two().max(2)
}
fn max_fill(n: usize) -> usize {
((n as f64 * 0.75f32 as f64).ceil() as usize).min(n - 1)
}
fn mix(x: i32) -> usize {
let h = x.wrapping_mul(0x9E37_79B9u32 as i32);
(h ^ ((h as u32) >> 16) as i32) as u32 as usize
}
/// `IntOpenHashSet`: add and iteration order.
struct IntSet {
key: Vec<i32>,
n: usize,
max_fill: usize,
size: usize,
contains_null: bool,
}
impl IntSet {
/// `new IntOpenHashSet()`.
fn new() -> Self {
Self::sized(16)
}
fn sized(expected: usize) -> Self {
let n = array_size(expected);
Self {
key: vec![0; n + 1],
n,
max_fill: max_fill(n),
size: 0,
contains_null: false,
}
}
fn add(&mut self, k: i32) -> bool {
if k == 0 {
if self.contains_null {
return false;
}
self.contains_null = true;
} else {
let mask = self.n - 1;
let mut pos = mix(k) & mask;
while self.key[pos] != 0 {
if self.key[pos] == k {
return false;
}
pos = (pos + 1) & mask;
}
self.key[pos] = k;
}
let grow = self.size >= self.max_fill;
self.size += 1;
if grow {
self.rehash(array_size(self.size + 1));
}
true
}
fn rehash(&mut self, new_n: usize) {
let mask = new_n - 1;
let mut new_key = vec![0; new_n + 1];
let real = if self.contains_null {
self.size - 1
} else {
self.size
};
let mut i = self.n;
for _ in 0..real {
loop {
i -= 1;
if self.key[i] != 0 {
break;
}
}
let mut pos = mix(self.key[i]) & mask;
while new_key[pos] != 0 {
pos = (pos + 1) & mask;
}
new_key[pos] = self.key[i];
}
self.n = new_n;
self.max_fill = max_fill(new_n);
self.key = new_key;
}
/// The elements in iteration order: the null key first, then the table from its end.
fn order(&self) -> Vec<i32> {
let mut out = Vec::with_capacity(self.size);
if self.contains_null {
out.push(0);
}
for pos in (0..self.n).rev() {
if self.key[pos] != 0 {
out.push(self.key[pos]);
}
}
out
}
}
/// The section indices in the iteration order of
/// `new Int2ObjectOpenHashMap<>(hashMapOfTheSections)`.
fn section_order(indices: &[i32]) -> Vec<i32> {
let mut sorted = indices.to_vec();
sorted.sort_unstable();
let mut set = IntSet::sized(sorted.len());
for i in sorted {
set.add(i);
}
set.order()
}
// ------------------------------------------------------------------ sections
fn leaf_index(name: &str) -> Option<i32> {
Some(match name {
"minecraft:acacia_leaves" => 0,
"minecraft:birch_leaves" => 1,
"minecraft:dark_oak_leaves" => 2,
"minecraft:jungle_leaves" => 3,
"minecraft:oak_leaves" => 4,
"minecraft:spruce_leaves" => 5,
_ => return None,
})
}
const LOGS: [&str; 18] = [
"minecraft:acacia_bark",
"minecraft:birch_bark",
"minecraft:dark_oak_bark",
"minecraft:jungle_bark",
"minecraft:oak_bark",
"minecraft:spruce_bark",
"minecraft:acacia_log",
"minecraft:birch_log",
"minecraft:dark_oak_log",
"minecraft:jungle_log",
"minecraft:oak_log",
"minecraft:spruce_log",
"minecraft:stripped_acacia_log",
"minecraft:stripped_birch_log",
"minecraft:stripped_dark_oak_log",
"minecraft:stripped_jungle_log",
"minecraft:stripped_oak_log",
"minecraft:stripped_spruce_log",
];
/// A section as `LeavesFix.Section` reads it.
struct Section {
index: i32,
palette: Vec<Tag>,
storage: Option<Packed>,
leaf_ids: HashSet<i32>,
log_ids: HashSet<i32>,
state_to_id: HashMap<i32, i32>,
}
fn name_of(state: &Tag) -> String {
as_str(state.get("Name")).unwrap_or_default()
}
fn state_id(name: &str, persistent: bool, distance: i32) -> i32 {
leaf_index(name).unwrap_or(0) << 5 | if persistent { 16 } else { 0 } | distance
}
fn leaf_tag(name: &str, persistent: bool, distance: i32) -> Tag {
Tag::compound([
(
"Properties",
Tag::compound([
(
"persistent",
super::dynamic::string(if persistent { "true" } else { "false" }),
),
("distance", super::dynamic::string(&distance.to_string())),
]),
),
("Name", super::dynamic::string(name)),
])
}
impl Section {
/// `LeavesSection` constructor: reads the palette, and rewrites its leaf states.
fn read_leaves(section: &Tag) -> Self {
let mut s = Self::read(section);
// skippable()
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");
s.leaf_ids.insert(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()) {
s.log_ids.insert(i as i32);
}
}
s.finish_read(section, s.leaf_ids.is_empty() && s.log_ids.is_empty());
s
}
fn read(section: &Tag) -> Self {
let palette = match section.get("Palette") {
Some(Tag::List(l)) => l.clone(),
_ => Vec::new(),
};
Self {
index: as_int(section.get("Y"), 0),
palette,
storage: None,
leaf_ids: HashSet::new(),
log_ids: HashSet::new(),
state_to_id: HashMap::new(),
}
}
fn finish_read(&mut self, section: &Tag, skippable: bool) {
if skippable {
self.storage = None;
} else {
let states = as_long_stream(section.get("BlockStates")).unwrap_or_default();
let size = ceillog2(self.palette.len()).max(4) as usize;
self.storage = Packed::new(size, states);
}
}
fn is_skippable(&self) -> bool {
self.storage.is_none()
}
fn block(&self, pos: usize) -> i32 {
self.storage.as_ref().map_or(0, |s| s.get(pos))
}
fn is_leaf(&self, block: i32) -> bool {
self.leaf_ids.contains(&block)
}
fn is_log(&self, block: i32) -> bool {
self.log_ids.contains(&block)
}
fn distance(&self, block: i32) -> i32 {
if self.is_log(block) {
return 0;
}
self.palette
.get(block as usize)
.and_then(|p| p.get("Properties"))
.and_then(|p| as_str(p.get("distance")))
.and_then(|d| d.parse().ok())
.unwrap_or(0)
}
fn set_distance(&mut self, pos: usize, block: i32, distance: i32) {
let base = self.palette[block as usize].clone();
let name = name_of(&base);
let persistent = as_str(base.get("Properties").and_then(|p| p.get("persistent")))
.is_some_and(|p| p == "true");
let id_key = state_id(&name, persistent, distance);
if !self.state_to_id.contains_key(&id_key) {
let id = self.palette.len() as i32;
self.leaf_ids.insert(id);
self.state_to_id.insert(id_key, id);
self.palette.push(leaf_tag(&name, persistent, distance));
}
let id = self.state_to_id[&id_key];
let storage = self.storage.as_mut().expect("storage");
if 1i64 << storage.bits <= i64::from(id) {
let mut grown = Packed::zeroed(storage.bits + 1);
for i in 0..4096 {
grown.set(i, storage.get(i));
}
*storage = grown;
}
storage.set(pos, id);
}
fn write(&self, section: &mut Tag) {
let Some(storage) = &self.storage else {
return;
};
set(section, "BlockStates", Tag::LongArray(storage.data.clone()));
set(section, "Palette", Tag::List(self.palette.clone()));
}
}
fn get_index(x: i32, y: i32, z: i32) -> i32 {
y << 8 | z << 4 | x
}
fn side_mask(west: bool, east: bool, north: bool, south: bool) -> i32 {
let mut s = 0;
if north {
if east {
s |= 2;
} else if west {
s |= 128;
} else {
s |= 1;
}
} else if south {
if west {
s |= 32;
} else if east {
s |= 8;
} else {
s |= 16;
}
} else if east {
s |= 4;
} else if west {
s |= 64;
}
s
}
/// `LeavesFix` over a chunk (`{Level: ...}`).
pub(super) fn leaves_fix(chunk: &mut Tag) {
let Some(level) = chunk.get_mut("Level") else {
return;
};
let Some(Tag::List(raw_sections)) = level.get("Sections").cloned() else {
return;
};
let mut sections: Vec<Section> = raw_sections.iter().map(Section::read_leaves).collect();
if sections.iter().all(Section::is_skippable) {
return;
}
let mut sides = 0;
let mut queue: Vec<IntSet> = (0..7).map(|_| IntSet::new()).collect();
let indices: Vec<i32> = sections.iter().map(|s| s.index).collect();
for index in section_order(&indices) {
let Some(section) = sections.iter().find(|s| s.index == index) else {
continue;
};
if section.is_skippable() {
continue;
}
for i in 0..4096usize {
let block = section.block(i);
if section.is_log(block) {
queue[0].add(section.index << 12 | i as i32);
} else if section.is_leaf(block) {
let x = (i & 15) as i32;
let z = (i >> 4 & 15) as i32;
sides |= side_mask(x == 0, x == 15, z == 0, z == 15);
}
}
}
const DIRECTIONS: [[i32; 3]; 6] = [
[-1, 0, 0],
[1, 0, 0],
[0, -1, 0],
[0, 1, 0],
[0, 0, -1],
[0, 0, 1],
];
for i in 1..7 {
let current = queue[i - 1].order();
for pos_chunk in current {
let x = pos_chunk & 15;
let y = pos_chunk >> 8 & 0xFF;
let z = pos_chunk >> 4 & 15;
for d in DIRECTIONS {
let (nx, ny, nz) = (x + d[0], y + d[1], z + d[2]);
if !(0..=15).contains(&nx) || !(0..=15).contains(&nz) || !(0..=255).contains(&ny) {
continue;
}
let Some(section) = sections.iter_mut().find(|s| s.index == ny >> 4) else {
continue;
};
if section.is_skippable() {
continue;
}
let pos_section = get_index(nx, ny & 15, nz) as usize;
let block = section.block(pos_section);
if section.is_leaf(block) && section.distance(block) > i as i32 {
section.set_distance(pos_section, block, i as i32);
queue[i].add(get_index(nx, ny, nz));
}
}
}
}
if let Some(Tag::List(list)) = level.get_mut("Sections") {
for raw in list.iter_mut() {
let y = as_int(raw.get("Y"), 0);
if let Some(section) = sections.iter().find(|s| s.index == y) {
section.write(raw);
}
}
}
if sides != 0 {
let mut upgrade = level
.get("UpgradeData")
.cloned()
.unwrap_or_else(super::dynamic::empty_map);
let old = as_int(upgrade.get("Sides"), 0) as i8;
set(&mut upgrade, "Sides", Tag::Byte(old | sides as i8));
set(level, "UpgradeData", upgrade);
}
}
/// `TrappedChestBlockEntityFix` over a chunk (`{Level: ...}`).
pub(super) fn trapped_chest_fix(chunk: &mut Tag) {
let Some(level) = chunk.get_mut("Level") else {
return;
};
let Some(Tag::List(raw_sections)) = level.get("Sections").cloned() else {
return;
};
let mut chests: HashSet<i32> = HashSet::new();
for raw in &raw_sections {
let mut section = Section::read(raw);
let ids: HashSet<i32> = section
.palette
.iter()
.enumerate()
.filter(|(_, p)| name_of(p) == "minecraft:trapped_chest")
.map(|(i, _)| i as i32)
.collect();
section.finish_read(raw, ids.is_empty());
if section.is_skippable() {
continue;
}
for i in 0..4096usize {
if ids.contains(&section.block(i)) {
chests.insert(section.index << 12 | i as i32);
}
}
}
let chunk_x = as_int(level.get("xPos"), 0);
let chunk_z = as_int(level.get("zPos"), 0);
if let Some(Tag::List(entities)) = level.get_mut("TileEntities") {
for be in entities {
if !super::schema::block_entity_known(be, super::schema::key(1624, 0) - 1) {
continue;
}
let x = as_int(be.get("x"), 0).wrapping_sub(chunk_x << 4);
let y = as_int(be.get("y"), 0);
let z = as_int(be.get("z"), 0).wrapping_sub(chunk_z << 4);
if chests.contains(&get_index(x, y, z)) {
set(be, "id", super::dynamic::string("minecraft:trapped_chest"));
}
}
}
}
+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())
@@ -398,6 +398,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")) {
@@ -473,6 +608,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)?,
@@ -695,10 +831,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
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+17 -1
View File
@@ -9,14 +9,19 @@
//! with the schema just below its own key ([`schema`]). Data older than
//! [`OLDEST_SUPPORTED`] needs fixes that are not ported. The fixes of client
//! files (options, hotbars, debug profiles) are not ported either.
mod block_state_data;
mod chunk_flatten;
mod chunk_leaves;
pub mod dynamic;
pub mod files;
mod fixes_a;
mod fixes_b;
mod fixes_c;
mod fixes_cliffs;
mod fixes_flat;
mod fixes_mid;
mod fixes_old;
mod flat_tables;
mod items;
pub mod json;
pub mod schema;
@@ -178,6 +183,7 @@ type FixFn = fn(&mut Cx);
/// Every ported fix (from the 1.19 schemas on), in DataFixers order.
fn fixes() -> Vec<(Key, FixFn)> {
let mut out = Vec::new();
fixes_flat::register(&mut out);
fixes_mid::register(&mut out);
fixes_cliffs::register(&mut out);
fixes_old::register(&mut out);
@@ -221,7 +227,17 @@ fn read_hooks(root: Ty, data: &mut Tag, k: Key) {
*stack = Tag::compound([("id", stack.clone()), ("count", Tag::Int(1))]);
}
});
walk(root, data, k, Ty::Objective, &mut fixes_a::unpack_objective);
if k >= key(1451, 6) {
walk(root, data, k, Ty::Objective, &mut fixes_a::unpack_objective);
}
// Namespaced name types read their strings with `ensureNamespaced`.
for ty in [Ty::Biome, Ty::EntityName] {
walk(root, data, k, ty, &mut |name| {
if let Some(n) = as_str(Some(name)) {
*name = string(&dynamic::namespaced(&n));
}
});
}
}
fn write_hooks(root: Ty, data: &mut Tag, k: Key) {
@@ -8,6 +8,9 @@
use super::dynamic::{as_str, namespaced};
use crate::nbt::Tag;
#[path = "schema_flat.rs"]
mod flat;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Ty {
Level,
@@ -144,6 +147,9 @@ impl Walker<'_> {
fn children(&mut self, ty: Ty, v: &mut Tag) {
let k = self.k;
if k < flat::END && self.legacy_children(ty, v) {
return;
}
match ty {
Ty::Level => {
if k < key(4771, 0) {
@@ -837,6 +843,9 @@ impl Walker<'_> {
/// Block entity ids the schema at `k` knows (others are read as remainder).
pub fn block_entity_known(be: &Tag, k: Key) -> bool {
let id = namespaced(&as_str(be.get("id")).unwrap_or_default());
if k < flat::END {
return flat::legacy_block_entity_known(&id, k);
}
let since = match id.as_str() {
"minecraft:sculk_catalyst" => key(3076, 0),
"minecraft:sculk_shrieker" => key(3078, 0),
@@ -0,0 +1,170 @@
//! The schemas of before 1.14 (keys below 1800): V99 to V1510 as far as the fixes
//! of those versions look. [`Walker::legacy_children`] answers for the types whose
//! shape differs from the general code of [`super`]; the other types fall through.
use super::{Key, Ty, Walker, as_str, key, namespaced};
use crate::nbt::Tag;
/// The schemas below this key are described here.
pub(super) const END: Key = key(1800, 0);
impl Walker<'_> {
/// `children` for a schema below [`END`]; false when the general code applies.
pub(super) fn legacy_children(&mut self, ty: Ty, v: &mut Tag) -> bool {
let k = self.k;
match ty {
Ty::Entity => {
self.legacy_entity(v);
true
}
// STATS is a plain map until V1451_6 turns it into typed counters.
Ty::Stats if k < key(1451, 6) => true,
Ty::Objective if k < key(1451, 6) => {
self.field(Ty::TextComponent, v, "DisplayName");
true
}
_ => false,
}
}
/// `ENTITY` of the schemas V705 to V1510 (the ids are `minecraft:...` since V704).
fn legacy_entity(&mut self, v: &mut Tag) {
let k = self.k;
self.list(Ty::ItemStack, v, "ArmorItems");
self.list(Ty::ItemStack, v, "HandItems");
self.field(Ty::TextComponent, v, "CustomName");
let id = namespaced(&as_str(v.get("id")).unwrap_or_default());
// V1451_3 flattens the blocks of entities; V1510 renames some entities.
let blocks_as_names = k < key(1451, 3);
let renamed = k >= key(1510, 0);
let block = |w: &mut Self, v: &mut Tag, old: &str, new: &str| {
if blocks_as_names {
w.field(Ty::BlockName, v, old);
} else {
w.field(Ty::BlockState, v, new);
}
};
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:egg"
| "minecraft:ender_pearl"
| "minecraft:fireball"
| "minecraft:small_fireball"
| "minecraft:snowball"
| "minecraft:wither_skull" => {
if blocks_as_names {
self.field(Ty::BlockName, v, "inTile");
}
}
"minecraft:xp_bottle" if !renamed => {
if blocks_as_names {
self.field(Ty::BlockName, v, "inTile");
}
}
"minecraft:potion" => {
self.field(Ty::ItemStack, v, "Potion");
if blocks_as_names {
self.field(Ty::BlockName, v, "inTile");
}
}
"minecraft:trident" => {
self.field(Ty::BlockState, v, "inBlockState");
self.field(Ty::ItemStack, v, "Trident");
}
"minecraft:chest_minecart" | "minecraft:hopper_minecart" => {
block(self, v, "DisplayTile", "DisplayState");
self.list(Ty::ItemStack, v, "Items");
}
"minecraft:minecart" | "minecraft:furnace_minecart" | "minecraft:tnt_minecart" => {
block(self, v, "DisplayTile", "DisplayState");
}
"minecraft:commandblock_minecart" if !renamed => {
block(self, v, "DisplayTile", "DisplayState");
self.field(Ty::TextComponent, v, "LastOutput");
}
"minecraft:command_block_minecart" if renamed => {
block(self, v, "DisplayTile", "DisplayState");
self.field(Ty::TextComponent, v, "LastOutput");
}
"minecraft:spawner_minecart" => {
block(self, v, "DisplayTile", "DisplayState");
self.spawner(v);
}
"minecraft:enderman" => block(self, v, "carried", "carriedBlockState"),
"minecraft:falling_block" => {
block(self, v, "Block", "BlockState");
self.field(Ty::BlockEntity, v, "TileEntityData");
}
"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" => {
self.field(Ty::ItemStack, v, "ArmorItem");
self.field(Ty::ItemStack, v, "SaddleItem");
}
"minecraft:donkey" | "minecraft:mule" => {
self.list(Ty::ItemStack, v, "Items");
self.field(Ty::ItemStack, v, "SaddleItem");
}
"minecraft:llama" => {
self.list(Ty::ItemStack, v, "Items");
self.field(Ty::ItemStack, v, "SaddleItem");
self.field(Ty::ItemStack, v, "DecorItem");
}
"minecraft:skeleton_horse" | "minecraft:zombie_horse" => {
self.field(Ty::ItemStack, v, "SaddleItem");
}
"minecraft:villager" => {
self.list(Ty::ItemStack, v, "Inventory");
if let Some(o) = v.get_mut("Offers") {
self.list(Ty::VillagerTrade, o, "Recipes");
}
}
"minecraft:zombie_villager" => {
if let Some(o) = v.get_mut("Offers") {
self.list(Ty::VillagerTrade, o, "Recipes");
}
}
_ => {}
}
}
}
/// Whether the block entity id is registered in the schema at `k` (before 1.14).
pub(super) fn legacy_block_entity_known(id: &str, k: Key) -> bool {
match id {
"minecraft:furnace"
| "minecraft:chest"
| "minecraft:ender_chest"
| "minecraft:jukebox"
| "minecraft:dispenser"
| "minecraft:dropper"
| "minecraft:sign"
| "minecraft:mob_spawner"
| "minecraft:piston"
| "minecraft:brewing_stand"
| "minecraft:enchanting_table"
| "minecraft:end_portal"
| "minecraft:beacon"
| "minecraft:skull"
| "minecraft:daylight_detector"
| "minecraft:hopper"
| "minecraft:comparator"
| "minecraft:banner"
| "minecraft:structure_block"
| "minecraft:end_gateway"
| "minecraft:command_block"
| "minecraft:shulker_box" => true,
// Registered by V1125 and dropped by V1451_5.
"minecraft:bed" => k >= key(1125, 0),
"minecraft:noteblock" | "minecraft:flower_pot" => k < key(1451, 5),
"minecraft:trapped_chest" => k >= key(1451, 0),
"DUMMY" => k >= key(1466, 0),
"minecraft:conduit" => k >= key(1481, 0),
_ => false,
}
}
@@ -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());
+333
View File
@@ -0,0 +1,333 @@
#!/usr/bin/env python3
"""Convert vanilla's BlockStateData.java into a plain data file.
Reads .local/minecraft/26.3/sources/server/net/minecraft/util/datafix/fixes/BlockStateData.java
Writes crates/horizon-world/src/datafix/block_state_data.txt
Output: UTF-8, '#' header lines, then one line per register() call, in the order the
calls execute (the static initializer order of the bootstrap* methods), tab separated:
ID <TAB> newName <TAB> newProps <TAB> legacyName1 <TAB> legacyProps1 <TAB> legacyName2 <TAB> legacyProps2 ...
Props are `key=value` pairs joined by `;`, sorted by key; empty string when there are none
(including an explicit `Map.of()`).
Usage: tools/gen_block_state_data.py [path/to/BlockStateData.java] [-o output.txt]
Re-runnable: the output only depends on the Java file.
"""
import argparse
import re
import sys
from pathlib import Path
REPO = Path(__file__).resolve().parent.parent
DEFAULT_JAVA = REPO / ".local/minecraft/26.3/sources/server/net/minecraft/util/datafix/fixes/BlockStateData.java"
DEFAULT_OUT = REPO / "crates/horizon-world/src/datafix/block_state_data.txt"
TOKEN_RE = re.compile(
r"""
(?P<ws>\s+)
| (?P<lcomment>//[^\n]*)
| (?P<bcomment>/\*.*?\*/)
| (?P<string>"(?:[^"\\]|\\.)*")
| (?P<ident>[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*)
| (?P<number>-?[0-9]+)
| (?P<punct>[(),;{}=<>?\[\].@!\-+*/%|&~:^'])
""",
re.X | re.S,
)
def tokenize(text):
pos = 0
out = []
while pos < len(text):
m = TOKEN_RE.match(text, pos)
if not m:
raise SystemExit(f"tokenize error at offset {pos}: {text[pos:pos+60]!r}")
pos = m.end()
kind = m.lastgroup
if kind in ("ws", "lcomment", "bcomment"):
continue
out.append((kind, m.group()))
return out
def unquote(s):
inner = s[1:-1]
if "\\" in inner:
raise SystemExit(f"unexpected escape in string {s}")
return inner
class Parser:
"""Parses a token list holding a call expression tree: ident(args) / strings / numbers / idents."""
def __init__(self, toks, consts):
self.toks = toks
self.i = 0
self.consts = consts
def peek(self):
return self.toks[self.i] if self.i < len(self.toks) else (None, None)
def next(self):
t = self.toks[self.i]
self.i += 1
return t
def expect(self, val):
k, v = self.next()
if v != val:
raise SystemExit(f"expected {val!r} got {v!r} at token {self.i}")
def parse_args(self):
"""After '(' consumed: return list of parsed args up to ')'."""
args = []
if self.peek()[1] == ")":
self.next()
return args
while True:
args.append(self.parse_expr())
k, v = self.next()
if v == ")":
return args
if v != ",":
raise SystemExit(f"expected ',' or ')' got {v!r} at token {self.i}")
def parse_expr(self):
k, v = self.next()
if k == "string":
return ("str", unquote(v))
if k == "number":
return ("int", int(v))
if k == "ident":
if self.peek()[1] == "(":
self.next()
args = self.parse_args()
return ("call", v, args)
return ("name", v)
raise SystemExit(f"unexpected token {v!r} at {self.i}")
def eval_map(expr, consts):
"""Evaluate a Map.of(...) call or a constant name into a dict."""
if expr[0] == "name":
if expr[1] not in consts:
raise SystemExit(f"unresolved map constant {expr[1]}")
return dict(consts[expr[1]])
if expr[0] == "call" and expr[1] == "Map.of":
args = expr[2]
if len(args) % 2:
raise SystemExit("odd Map.of args")
d = {}
for a in range(0, len(args), 2):
k, v = args[a], args[a + 1]
if k[0] != "str" or v[0] != "str":
raise SystemExit(f"non-string Map.of entry {k} {v}")
if k[1] in d:
raise SystemExit(f"duplicate key {k[1]}")
d[k[1]] = v[1]
return d
raise SystemExit(f"cannot evaluate map expr {expr}")
def fmt_props(d):
for k, v in d.items():
if not re.fullmatch(r"[a-z0-9_]+", k) or not re.fullmatch(r"[A-Za-z0-9_]+", v):
raise SystemExit(f"unexpected prop chars {k}={v}")
return ";".join(f"{k}={d[k]}" for k in sorted(d))
def eval_create(expr, consts):
if expr[0] != "call" or expr[1] != "create":
raise SystemExit(f"expected create(...) got {expr}")
args = expr[2]
if not args or args[0][0] != "str":
raise SystemExit(f"create without name: {expr}")
name = args[0][1]
if not re.fullmatch(r"[a-z0-9_:]+", name) and name != "%%FILTER_ME%%":
raise SystemExit(f"bad block name {name!r}")
if len(args) == 1:
return name, ""
if len(args) == 2:
return name, fmt_props(eval_map(args[1], consts))
raise SystemExit(f"create with {len(args)} args")
def matching_brace(toks, i):
"""toks[i] is '{'; return index of matching '}'."""
depth = 0
for j in range(i, len(toks)):
if toks[j][1] == "{":
depth += 1
elif toks[j][1] == "}":
depth -= 1
if depth == 0:
return j
raise SystemExit("unbalanced braces")
def main():
ap = argparse.ArgumentParser()
ap.add_argument("java", nargs="?", default=str(DEFAULT_JAVA))
ap.add_argument("-o", "--output", default=str(DEFAULT_OUT))
a = ap.parse_args()
java_path = Path(a.java)
text = java_path.read_text(encoding="utf-8")
toks = tokenize(text)
# ---- map constants: `private static final Map<String, String> NAME = <expr>;`
consts = {}
i = 0
n = len(toks)
while i < n:
if (
toks[i][1] == "Map"
and toks[i + 1][1] == "<"
and i >= 3
and toks[i - 1][1] == "final"
and toks[i - 2][1] == "static"
):
# Map < String , String > NAME = expr ;
j = i + 1
while toks[j][1] != ">":
j += 1
name = toks[j + 1][1]
assert toks[j + 2][1] == "=", toks[j : j + 4]
p = Parser(toks[j + 3 :], consts)
expr = p.parse_expr()
assert toks[j + 3 + p.i][1] == ";", toks[j + 3 + p.i]
if name in consts:
raise SystemExit(f"duplicate const {name}")
consts[name] = eval_map(expr, consts)
i = j + 3 + p.i
else:
i += 1
# ---- methods: find `void bootstrapX ( ) {` bodies
bodies = {}
for i in range(n - 5):
if (
toks[i][1] == "void"
and toks[i + 1][1].startswith("bootstrap")
and toks[i + 2][1] == "("
and toks[i + 3][1] == ")"
and toks[i + 4][1] == "{"
):
end = matching_brace(toks, i + 4)
bodies[toks[i + 1][1]] = toks[i + 5 : end]
# ---- static initializer order
order = None
for i in range(n - 1):
if toks[i][1] == "static" and toks[i + 1][1] == "{":
end = matching_brace(toks, i + 1)
seq = []
k = i + 2
while k < end:
if toks[k][0] == "ident" and toks[k][1].startswith("bootstrap") and toks[k + 1][1] == "(":
seq.append(toks[k][1])
k += 3
else:
k += 1
order = seq
if not order:
raise SystemExit("static initializer not found")
if set(order) != set(bodies) or len(order) != len(set(order)):
raise SystemExit(f"bootstrap methods {sorted(bodies)} vs called {order}")
# ---- parse register calls
lines = []
total_register = 0
total_create = 0
total_legacy = 0
max_id = -1
for m in order:
body = bodies[m]
k = 0
while k < len(body):
t = body[k]
if t[0] == "ident" and t[1] == "register" and body[k + 1][1] == "(":
p = Parser(body[k + 2 :], consts)
args = p.parse_args()
k += 2 + p.i
if body[k][1] != ";":
raise SystemExit(f"{m}: register not followed by ';'")
total_register += 1
if args[0][0] != "int":
raise SystemExit(f"{m}: register id not int: {args[0]}")
rid = args[0][1]
if len(args) < 2:
raise SystemExit(f"{m}: register({rid}) without new tag")
fields = [str(rid)]
for c in args[1:]:
name, props = eval_create(c, consts)
total_create += 1
fields += [name, props]
total_legacy += len(args) - 2
max_id = max(max_id, rid)
lines.append("\t".join(fields))
else:
# anything other than register(...) ; in a bootstrap body is unexpected
raise SystemExit(f"{m}: unexpected token {t} in body")
k += 1
k += 1 # skip ';'
# ---- independent validation: count `register(` occurrences inside bootstrap bodies (text based)
src_lines = text.split("\n")
in_body = False
textual = 0
for ln in src_lines:
if re.match(r"\s*private static void bootstrap\w*\(\)\s*\{", ln):
in_body = True
continue
if in_body:
if re.match(r" \}\s*$", ln):
in_body = False
continue
textual += len(re.findall(r"\bregister\(", ln))
if textual != total_register or textual != len(lines):
raise SystemExit(f"register count mismatch: textual {textual}, parsed {total_register}, lines {len(lines)}")
textual_create = 0
in_body = False
for ln in src_lines:
if re.match(r"\s*private static void bootstrap\w*\(\)\s*\{", ln):
in_body = True
continue
if in_body:
if re.match(r" \}\s*$", ln):
in_body = False
continue
textual_create += len(re.findall(r"\bcreate\(", ln))
if textual_create != total_create:
raise SystemExit(f"create count mismatch: textual {textual_create}, parsed {total_create}")
header = [
"# Generated by tools/gen_block_state_data.py from vanilla 26.3 BlockStateData.java. Do not edit.",
"# One line per register(ID, new, legacy...) call, in execution order of the static initializer",
"# (bootstrap0, 1, 2, 3_1, 3_2, 4 .. 9, A .. F).",
"# Format (tab separated): ID <TAB> newName <TAB> newProps [<TAB> legacyName <TAB> legacyProps]...",
"# ID is the decimal 1.12 (blockId << 4 | data) id; newName/newProps is the 1.13 block state;",
"# each legacy pair is a 1.12 Name + Properties tag that maps to ID.",
"# Props: key=value pairs joined by ';', sorted by key; empty string when there are none.",
"# newName may be the literal %%FILTER_ME%% (vanilla FILTER_ME marker, the skull ids 2304..2317).",
]
out = Path(a.output)
out.parent.mkdir(parents=True, exist_ok=True)
out.write_text("\n".join(header + lines) + "\n", encoding="utf-8")
print(f"java: {java_path}")
print(f"output: {out}")
print(f"map constants: {len(consts)}")
print(f"bootstrap order: {' '.join(order)}")
print(f"register calls: {total_register} (grep count {textual})")
print(f"data lines: {len(lines)}")
print(f"create() calls: {total_create} (all resolved; grep count {textual_create})")
print(f"legacy tags: {total_legacy}")
print(f"highest ID: {max_id}")
if __name__ == "__main__":
sys.exit(main())
+146
View File
@@ -0,0 +1,146 @@
#!/usr/bin/env python3
"""Copy the lookup tables of vanilla's flattening-era DataFixers (1.12.2 -> 1.13.2) into
crates/horizon-world/src/datafix/flat_tables.rs, so that the Rust fixes use exactly vanilla's
data. Run from the repository root; the vanilla sources are the ones under .local/minecraft.
gen_flat_tables.py [-o OUT]
"""
import argparse
import re
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
FIXES = ROOT / '.local' / 'minecraft' / '26.3' / 'sources' / 'server' / 'net' / 'minecraft' / 'util' / 'datafix' / 'fixes'
OUT = ROOT / 'crates' / 'horizon-world' / 'src' / 'datafix' / 'flat_tables.rs'
STR = r'"((?:[^"\\]|\\.)*)"'
def declaration(text, var):
"""The source of `NAME = ...;` for a field called `var`."""
m = re.search(r'\b' + re.escape(var) + r'\s*=', text)
if not m:
sys.exit(f'declaration {var} not found')
depth = 0
i = m.end()
while True:
c = text[i]
if c in '({[':
depth += 1
elif c in ')}]':
depth -= 1
elif c == '"':
i = text.index('"', i + 1)
while text[i - 1] == '\\':
i = text.index('"', i + 1)
elif c == ';' and depth == 0:
return text[m.end():i]
i += 1
def pairs(path, var, value_int=False, key_int=False):
body = declaration((FIXES / path).read_text(), var)
key = r'(\d+)' if key_int else STR
value = r'(\d+)' if value_int else STR
out = re.findall(r'put\(\s*' + key + r',\s*' + value + r'\s*\)', body)
if not out:
sys.exit(f'{path} {var}: no entries')
return out
def strings(path, var, call):
body = declaration((FIXES / path).read_text(), var)
out = re.findall(call + r'\(\s*' + STR + r'\s*\)', body) if call else re.findall(STR, body)
if not out:
sys.exit(f'{path} {var}: no entries')
return out
def rs_str(s):
return '"' + s.replace('\\', '\\\\').replace('"', '\\"') + '"'
def emit_pairs(name, entries, int_key=False, int_value=False, doc=''):
lines = [f'/// {doc}'] if doc else []
kt = 'i32' if int_key else '&str'
vt = 'i32' if int_value else '&str'
lines.append(f'pub static {name}: &[({kt}, {vt})] = &[')
for k, v in entries:
a = k if int_key else rs_str(k)
b = v if int_value else rs_str(v)
lines.append(f' ({a}, {b}),')
lines.append('];')
return '\n'.join(lines)
def emit_strs(name, entries, doc=''):
lines = [f'/// {doc}'] if doc else []
lines.append(f'pub static {name}: &[&str] = &[')
lines += [f' {rs_str(s)},' for s in entries]
lines.append('];')
return '\n'.join(lines)
def structure_renames():
text = (FIXES / 'ChunkStructuresTemplateRenameFix.java').read_text()
out = []
for m in re.finditer(r'\.put\(\s*' + STR + r',\s*Pair\.of\(\s*' + STR + r',\s*ImmutableMap\.<String, String>builder\(\)((?:\s*\.put\(' + STR + r',\s*' + STR + r'\))*)\s*\.build\(\)', text):
structure, child = m.group(1), m.group(2)
entries = re.findall(r'\.put\(' + STR + r',\s*' + STR + r'\)', m.group(3))
out.append((structure, child, entries))
if len(out) != 4:
sys.exit(f'expected 4 structure renames, found {len(out)}')
lines = ['/// `ChunkStructuresTemplateRenameFix.RENAMES`: structure id, child id, template renames.',
'pub static STRUCTURE_TEMPLATE_RENAMES: &[(&str, &str, &[(&str, &str)])] = &[']
for structure, child, entries in out:
lines.append(f' ({rs_str(structure)}, {rs_str(child)}, &[')
lines += [f' ({rs_str(a)}, {rs_str(b)}),' for a, b in entries]
lines.append(' ]),')
lines.append('];')
return '\n'.join(lines)
def main():
p = argparse.ArgumentParser()
p.add_argument('-o', '--out', default=str(OUT))
args = p.parse_args()
parts = [
'//! Lookup tables of the DataFixers of 1.13 (DataVersion 1343 to 1631), copied from vanilla\'s\n'
'//! sources by `tools/gen_flat_tables.py`. Do not edit.\n',
emit_pairs('ITEM_FLATTENING', pairs('ItemStackTheFlatteningFix.java', 'MAP'),
doc='`ItemStackTheFlatteningFix.MAP`: `id.damage` to the flattened item.'),
emit_strs('ITEM_DAMAGE_IDS', strings('ItemStackTheFlatteningFix.java', 'DAMAGE_IDS', ''),
doc='`ItemStackTheFlatteningFix.DAMAGE_IDS`: items that keep their damage in `tag.Damage`.'),
emit_pairs('BLOCK_IDS', pairs('EntityBlockStateFix.java', 'MAP', value_int=True), int_value=True,
doc='`EntityBlockStateFix.MAP`: 1.12 block name to numeric id.'),
emit_pairs('ITEM_IDS', pairs('ItemIdFix.java', 'ITEM_NAMES', key_int=True), int_key=True,
doc='`ItemIdFix.ITEM_NAMES`: numeric item id to name.'),
emit_strs('SPECIAL_OBJECTIVE_CRITERIA', strings('StatsCounterFix.java', 'SPECIAL_OBJECTIVE_CRITERIA', '')),
emit_strs('STATS_SKIP', strings('StatsCounterFix.java', 'SKIP', 'add')),
emit_pairs('STATS_CUSTOM', pairs('StatsCounterFix.java', 'CUSTOM_MAP')),
emit_pairs('STATS_ITEM_KEYS', pairs('StatsCounterFix.java', 'ITEM_KEYS')),
emit_pairs('STATS_ENTITY_KEYS', pairs('StatsCounterFix.java', 'ENTITY_KEYS')),
emit_pairs('STATS_ENTITIES', pairs('StatsCounterFix.java', 'ENTITIES')),
emit_pairs('RECIPES', pairs('RecipesFix.java', 'RECIPES')),
emit_pairs('RECIPES_RENAMENING', pairs('RecipesRenameningFix.java', 'RECIPES')),
emit_pairs('ADVANCEMENTS', pairs('AdvancementsFix.java', 'RENAMES')),
emit_pairs('GENERATOR_BIOMES', pairs('LevelDataGeneratorOptionsFix.java', 'MAP')),
emit_pairs('SPAWN_EGGS', pairs('ItemStackSpawnEggFix.java', 'MAP')),
emit_pairs('ENCHANTMENTS', pairs('ItemStackEnchantmentNamesFix.java', 'MAP', key_int=True), int_key=True),
emit_pairs('CORAL', pairs('RenamedCoralFix.java', 'RENAMED_IDS')),
emit_pairs('CORAL_FANS', pairs('RenamedCoralFansFix.java', 'RENAMED_IDS')),
emit_pairs('THE_RENAMENING_ENTITIES', pairs('EntityTheRenameningFix.java', 'RENAMED_IDS')),
emit_pairs('THE_RENAMENING_BLOCKS', pairs('EntityTheRenameningFix.java', 'RENAMED_BLOCKS')),
emit_pairs('COD_SALMON', pairs('EntityCodSalmonFix.java', 'RENAMED_IDS')),
emit_pairs('COD_SALMON_EGGS', pairs('EntityCodSalmonFix.java', 'RENAMED_EGG_IDS')),
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)
if __name__ == '__main__':
main()