Merge branch 'parity/saves' into parity-base

This commit is contained in:
Ludovic Marie
2026-10-12 14:06:53 +02:00
25 changed files with 10086 additions and 37 deletions
+2 -2
View File
@@ -37,7 +37,7 @@ round-trip harness has not been rerun.
| Rendering | 0.75 | Entity models match over 150 layers; block entity renderers and HUD overlays not compared |
| Lighting | 1 | None: saved and relit light matches vanilla, slabs and stairs included |
| World generation | 1 | None: 2,352 whole chunks over 225 structure sites, in all three dimensions, identical to vanilla |
| Region files and saves | 0.75 | Save round-trip harness fails; older DataVersions are not upgraded |
| Region files and saves | 0.75 | Saves round-trip with vanilla, and worlds from 1.18.2 to 26.2 upgrade as vanilla's DataFixers do over 22 sample worlds; worlds older than 1.18.2 are not upgraded |
| Entities and physics | 0.75 | Attributes, dimensions, death loot, spawn rules and natural spawning (15,500 NaturalSpawner cases) match; AI not compared; drowned, villagers, traders and the wither unverified |
| Interaction and inventory | 1 | None: menu clicks match over 6,000 click sequences |
| Menus, HUD and chat | 0.75 | Commands parse as vanilla over 39,685 inputs, but /item modify, /spectate, /waypoint and /dialog do not run; no tab list or world border rendering |
@@ -46,7 +46,7 @@ round-trip harness has not been rerun.
### Next steps
1. Rerun the save round-trip harness and upgrade saves from older DataVersions.
1. Upgrade saves older than 1.18.2 (the DataFixers before DataVersion 2970).
2. Draw every item through the item model pipeline, and add the resource pack screen.
3. An audio harness: sound engine attenuation, weighted sound events, music and ambience.
4. A mob AI harness, and an oracle for drowned, villagers, traders and the wither.
+1 -1
View File
@@ -4866,8 +4866,8 @@ impl Game<'_> {
creative: bool,
) -> Result<()> {
let save_root = std::env::current_dir()?.join("saves").join(folder);
horizon_client::save::upgrade_older_world(&save_root)?;
horizon_client::save::migrate_legacy(&save_root)?;
horizon_client::save::check_data_version(&save_root)?;
self.session_lock = None;
self.session_lock = Some(horizon_client::save::SessionLock::acquire(&save_root)?);
self.persist = Default::default();
+31 -12
View File
@@ -111,10 +111,13 @@ impl SessionLock {
Ok(Self(file))
}
}
/// Refuse a world last saved by an older game version: chunks are only read
/// at the current DataVersion, and the DataFixerUpper chain is not ported.
/// `tools/upgrade_world.py` runs vanilla's own upgrader (`--forceUpgrade`).
pub fn check_data_version(world: &Path) -> Result<()> {
/// Bring a world last saved by an older game version to the current format, as
/// 26.3 does when it opens one: the file fixes and level.dat first
/// (`FileFixerUpper`), then the DataFixers upgrades of every chunk, entity chunk,
/// POI chunk, player, saved data, advancements and statistics file. Vanilla runs
/// the latter as it reads each file; Horizon runs them all on opening, which reads
/// the same data. Worlds older than the ported fixes (1.18.2) are refused.
pub fn upgrade_older_world(world: &Path) -> Result<()> {
let Some(root) = read_dat(&world.join("level.dat")) else {
return Ok(());
};
@@ -122,14 +125,20 @@ pub fn check_data_version(world: &Path) -> Result<()> {
.get("Data")
.and_then(|d| number(d.get("DataVersion")))
.unwrap_or(i64::from(DATA_VERSION));
if version < i64::from(DATA_VERSION) {
if version >= i64::from(DATA_VERSION) {
return Ok(());
}
if version < i64::from(horizon_world::datafix::OLDEST_SUPPORTED) {
return Err(format!(
"{} was saved with DataVersion {version} (this game is {DATA_VERSION}); \
upgrade it first with tools/upgrade_world.py",
world.display()
"{} was saved with DataVersion {version}; worlds older than DataVersion {} (1.18.2) \
cannot be upgraded yet (tools/upgrade_world.py runs vanilla's own upgrader)",
world.display(),
horizon_world::datafix::OLDEST_SUPPORTED
)
.into());
}
horizon_world::datafix::files::upgrade_world(world)
.map_err(|e| format!("upgrading {}: {e}", world.display()))?;
Ok(())
}
/// Move a world saved by an older Horizon build (pre-26.3 layout with a
@@ -2181,14 +2190,24 @@ impl<G: ChunkGenerator> ChunkGenerator for Persistent<G> {
#[cfg(test)]
mod tests {
#[test]
fn old_data_version_is_refused() {
fn older_worlds_are_upgraded_on_opening() {
let dir = std::env::temp_dir().join(format!("horizon-dv-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let level =
|v: i32| Tag::compound([("Data", Tag::compound([("DataVersion", Tag::Int(v))]))]);
let version = |dir: &std::path::Path| {
super::read_dat(&dir.join("level.dat"))
.and_then(|r| super::number(r.get("Data")?.get("DataVersion")))
};
// 1.18, older than the ported fixes: refused, left as it is.
super::write_dat(&dir.join("level.dat"), level(2860)).unwrap();
assert!(super::upgrade_older_world(&dir).is_err());
assert_eq!(version(&dir), Some(2860));
// 1.21.1: upgraded to the current version.
super::write_dat(&dir.join("level.dat"), level(3955)).unwrap();
assert!(super::check_data_version(&dir).is_err());
super::write_dat(&dir.join("level.dat"), level(super::DATA_VERSION)).unwrap();
assert!(super::check_data_version(&dir).is_ok());
super::upgrade_older_world(&dir).unwrap();
assert_eq!(version(&dir), Some(i64::from(super::DATA_VERSION)));
super::upgrade_older_world(&dir).unwrap();
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
@@ -0,0 +1,45 @@
//! Upgrade a copy of a world saved by 1.20 to 26.2 to the 26.3 format, as
//! vanilla 26.3 does when it opens it and reads every file:
//! `datafix_world IN_WORLD OUT_WORLD` (used by tools/test_datafix_parity.py).
use horizon_world::datafix::files;
use std::{env, fs, io, path::Path, process::ExitCode};
fn copy(from: &Path, to: &Path) -> io::Result<()> {
fs::create_dir_all(to)?;
for entry in fs::read_dir(from)? {
let entry = entry?;
let target = to.join(entry.file_name());
if entry.file_type()?.is_dir() {
copy(&entry.path(), &target)?;
} else if entry.file_name() != "session.lock" {
fs::copy(entry.path(), target)?;
}
}
Ok(())
}
fn main() -> ExitCode {
let args: Vec<String> = env::args().skip(1).collect();
let [input, output] = args.as_slice() else {
eprintln!("usage: datafix_world IN_WORLD OUT_WORLD");
return ExitCode::from(2);
};
let output = Path::new(output);
let result = (|| {
if output.exists() {
fs::remove_dir_all(output)?;
}
copy(Path::new(input), output)?;
files::upgrade_world(output)
})();
match result {
Ok(version) => {
println!("upgraded {} from DataVersion {version}", output.display());
ExitCode::SUCCESS
}
Err(e) => {
eprintln!("datafix_world: {e}");
ExitCode::FAILURE
}
}
}
+241
View File
@@ -0,0 +1,241 @@
//! What DataFixerUpper's `Dynamic` over `NbtOps` does with values: number and
//! string views, maps and lists, field edits. Fixes use these to stay close to
//! the Java they port.
use crate::nbt::{Compound, JavaString, Tag};
pub fn key(s: &str) -> JavaString {
JavaString::from(s)
}
pub fn string(s: &str) -> Tag {
Tag::String(JavaString::from(s))
}
pub fn empty_map() -> Tag {
Tag::Compound(Compound::new())
}
pub fn boolean(b: bool) -> Tag {
Tag::Byte(i8::from(b))
}
pub fn int_list(values: &[i32]) -> Tag {
Tag::IntArray(values.to_vec())
}
/// A Java `Number` from a numeric tag (`Tag.asNumber`).
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Number {
Byte(i8),
Short(i16),
Int(i32),
Long(i64),
Float(f32),
Double(f64),
}
impl Number {
pub fn int_value(self) -> i32 {
match self {
Self::Byte(v) => i32::from(v),
Self::Short(v) => i32::from(v),
Self::Int(v) => v,
Self::Long(v) => v as i32,
Self::Float(v) => v as i32,
Self::Double(v) => v as i32,
}
}
pub fn long_value(self) -> i64 {
match self {
Self::Byte(v) => i64::from(v),
Self::Short(v) => i64::from(v),
Self::Int(v) => i64::from(v),
Self::Long(v) => v,
Self::Float(v) => v as i64,
Self::Double(v) => v as i64,
}
}
pub fn double_value(self) -> f64 {
match self {
Self::Byte(v) => f64::from(v),
Self::Short(v) => f64::from(v),
Self::Int(v) => f64::from(v),
Self::Long(v) => v as f64,
Self::Float(v) => f64::from(v),
Self::Double(v) => v,
}
}
pub fn float_value(self) -> f32 {
match self {
Self::Double(v) => v as f32,
Self::Long(v) => v as f32,
other => other.double_value() as f32,
}
}
pub fn byte_value(self) -> i8 {
match self {
Self::Byte(v) => v,
Self::Short(v) => v as i8,
Self::Int(v) => v as i8,
Self::Long(v) => v as i8,
Self::Float(v) => (v as i32) as i8,
Self::Double(v) => (v as i32) as i8,
}
}
/// `Number.toString` of the boxed value.
pub fn java_string(self) -> String {
match self {
Self::Byte(v) => v.to_string(),
Self::Short(v) => v.to_string(),
Self::Int(v) => v.to_string(),
Self::Long(v) => v.to_string(),
Self::Float(v) => crate::datafix::json::java_float_string(v),
Self::Double(v) => crate::datafix::json::java_double_string(v),
}
}
}
pub fn as_number(tag: &Tag) -> Option<Number> {
Some(match tag {
Tag::Byte(v) => Number::Byte(*v),
Tag::Short(v) => Number::Short(*v),
Tag::Int(v) => Number::Int(*v),
Tag::Long(v) => Number::Long(*v),
Tag::Float(v) => Number::Float(f32::from_bits(*v)),
Tag::Double(v) => Number::Double(f64::from_bits(*v)),
_ => return None,
})
}
pub fn as_int(tag: Option<&Tag>, default: i32) -> i32 {
tag.and_then(as_number).map_or(default, Number::int_value)
}
pub fn as_long(tag: Option<&Tag>, default: i64) -> i64 {
tag.and_then(as_number).map_or(default, Number::long_value)
}
pub fn as_double(tag: Option<&Tag>, default: f64) -> f64 {
tag.and_then(as_number)
.map_or(default, Number::double_value)
}
pub fn as_float(tag: Option<&Tag>, default: f32) -> f32 {
tag.and_then(as_number).map_or(default, Number::float_value)
}
/// `NbtOps.getBooleanValue`: a number that is not zero.
pub fn as_bool_opt(tag: Option<&Tag>) -> Option<bool> {
tag.and_then(as_number).map(|n| n.double_value() != 0.0)
}
pub fn as_bool(tag: Option<&Tag>, default: bool) -> bool {
as_bool_opt(tag).unwrap_or(default)
}
pub fn as_str(tag: Option<&Tag>) -> Option<String> {
match tag {
Some(Tag::String(s)) => Some(s.to_string_lossy()),
_ => None,
}
}
pub fn as_string(tag: Option<&Tag>, default: &str) -> String {
as_str(tag).unwrap_or_else(|| default.to_owned())
}
/// `NbtOps.getStream`: the elements of a list or of a number array.
pub fn stream(tag: Option<&Tag>) -> Vec<Tag> {
match tag {
Some(Tag::List(v)) => v.clone(),
Some(Tag::ByteArray(v)) => v.iter().map(|b| Tag::Byte(*b as i8)).collect(),
Some(Tag::IntArray(v)) => v.iter().map(|i| Tag::Int(*i)).collect(),
Some(Tag::LongArray(v)) => v.iter().map(|l| Tag::Long(*l)).collect(),
_ => Vec::new(),
}
}
pub fn is_list(tag: Option<&Tag>) -> bool {
matches!(
tag,
Some(Tag::List(_) | Tag::ByteArray(_) | Tag::IntArray(_) | Tag::LongArray(_))
)
}
pub fn get<'a>(tag: &'a Tag, field: &str) -> Option<&'a Tag> {
tag.get(field)
}
pub fn map(tag: &Tag) -> Option<&Compound> {
match tag {
Tag::Compound(c) => Some(c),
_ => None,
}
}
pub fn map_mut(tag: &mut Tag) -> Option<&mut Compound> {
match tag {
Tag::Compound(c) => Some(c),
_ => None,
}
}
/// `Dynamic.set`: only maps change.
pub fn set(tag: &mut Tag, field: &str, value: Tag) {
if let Tag::End = tag {
*tag = empty_map();
}
if let Tag::Compound(c) = tag {
c.insert(key(field), value);
}
}
pub fn remove(tag: &mut Tag, field: &str) -> Option<Tag> {
match tag {
Tag::Compound(c) => c.remove(&key(field)),
_ => None,
}
}
pub fn has(tag: &Tag, field: &str) -> bool {
tag.get(field).is_some()
}
/// `Dynamic.renameField`.
pub fn rename(tag: &mut Tag, from: &str, to: &str) {
if let Some(v) = remove(tag, from) {
set(tag, to, v);
}
}
/// `Dynamic.update`: apply `f` to a field that is present.
pub fn update(tag: &mut Tag, field: &str, f: impl FnOnce(Tag) -> Tag) {
if let Some(v) = remove(tag, field) {
set(tag, field, f(v));
}
}
/// `Dynamic.update` in place.
pub fn update_mut(tag: &mut Tag, field: &str, f: impl FnOnce(&mut Tag)) {
if let Some(v) = tag.get_mut(field) {
f(v);
}
}
/// `Dynamic.renameAndFixField`: the fixed value under the new name, removed when
/// the fix gives nothing.
pub fn rename_and_fix(tag: &mut Tag, from: &str, to: &str, f: impl FnOnce(Tag) -> Option<Tag>) {
if let Some(v) = remove(tag, from)
&& let Some(v) = f(v)
{
set(tag, to, v);
}
}
/// `Dynamic.copyField`.
pub fn copy_field(from: &Tag, field: &str, to: &mut Tag, to_field: &str) {
if let Some(v) = from.get(field) {
set(to, to_field, v.clone());
}
}
/// `Dynamic.equals(emptyMap())`: the map a record remembers it was read from
/// (see `datafix::mark_read`) is not part of the value.
pub fn is_empty_map(tag: &Tag) -> bool {
matches!(tag, Tag::Compound(c) if c.keys().all(|k| *k == key(super::LAST_READ)))
}
/// `Dynamic.orElseEmptyMap` of an optional field.
pub fn or_empty_map(tag: Option<&Tag>) -> Tag {
tag.cloned().unwrap_or_else(empty_map)
}
/// Apply `f` to every element of a list field (`Dynamic.createList(asStream().map(f))`).
pub fn map_list(tag: &mut Tag, field: &str, mut f: impl FnMut(Tag) -> Tag) {
if let Some(v) = tag.get(field)
&& is_list(Some(v))
{
let list = stream(Some(v)).into_iter().map(&mut f).collect();
set(tag, field, Tag::List(list));
}
}
/// `NamespacedSchema.ensureNamespaced`.
pub fn namespaced(id: &str) -> String {
match crate::datafix::identifier(id) {
Some((ns, path)) => format!("{ns}:{path}"),
None => id.to_owned(),
}
}
+680
View File
@@ -0,0 +1,680 @@
//! Opening an older world as 26.3 does: the file fixes of `FileFixerUpper`
//! (`net.minecraft.util.filefix`, from DataVersion 4772 on) that move the
//! files into the 26.x layout, then level.dat brought to the current version,
//! and the upgrades vanilla runs as it reads each file ([`upgrade_world`]
//! does them all at once, as a converter).
use super::dynamic::{as_bool, as_int, as_str, int_list, remove, set, string};
use super::json::{self, Json};
use super::schema::Ty;
use super::{CURRENT, update};
use crate::compression::Compression;
use crate::nbt::{self, Compound, JavaString, Limits, NamedTag, Tag};
use crate::region::{ChunkRecord, RegionFile, RegionWriter};
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
/// The name `SharedConstants.getCurrentVersion().name()` writes in level.dat.
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];
fn invalid(message: impl Into<String>) -> io::Error {
io::Error::new(io::ErrorKind::InvalidData, message.into())
}
/// `NbtIo.readCompressed`: the root compound of a gzip NBT file, if it exists.
pub fn read_dat(path: &Path) -> io::Result<Option<Tag>> {
let bytes = match fs::read(path) {
Ok(b) => b,
Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(None),
Err(e) => return Err(e),
};
let limits = Limits::default();
let raw = Compression::Gzip.decode(&bytes, limits.max_bytes)?;
let (root, _) = nbt::read_named(&raw, limits).map_err(|e| invalid(e.to_string()))?;
Ok(Some(root.tag))
}
/// `NbtIo.writeCompressed`, creating the parent folders.
pub fn write_dat(path: &Path, tag: &Tag) -> io::Result<()> {
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
let root = NamedTag {
name: JavaString::default(),
tag: tag.clone(),
};
let raw = nbt::write_named(&root, Limits::default()).map_err(|e| invalid(e.to_string()))?;
fs::write(path, Compression::Gzip.encode(&raw)?)
}
/// `NbtUtils.getDataVersion`.
pub fn data_version(tag: &Tag, default: i32) -> i32 {
match tag.get("DataVersion") {
Some(Tag::Int(v)) => *v,
_ => default,
}
}
/// `FileFixerUpper.requiresFileFixing`.
pub fn requires_file_fixing(level_version: i32) -> bool {
let from = if level_version < FILE_FIXER_INTRODUCTION {
0
} else {
level_version
};
from < *FILE_FIXES.last().unwrap()
}
// ------------------------------------------------------------------ file operations
fn resolve(base: &Path, relative: &str) -> PathBuf {
base.join(relative.trim_start_matches('/'))
}
/// `FileFixUtil.moveFile`: nothing when the source is missing, an error when
/// the target exists.
fn move_file(base: &Path, from: &str, to: &str) -> io::Result<()> {
let from = resolve(base, from);
if !from.exists() {
return Ok(());
}
let to = resolve(base, to);
if to.exists() {
return Err(io::Error::new(
io::ErrorKind::AlreadyExists,
format!(
"cannot move {} to {}: target already exists",
from.display(),
to.display()
),
));
}
if let Some(parent) = to.parent() {
fs::create_dir_all(parent)?;
}
fs::rename(from, to)
}
/// `FileFixUtil.deleteFileOrEmptyDirectory`.
fn delete(base: &Path, relative: &str) -> io::Result<()> {
let path = resolve(base, relative);
if path.is_dir() {
let entries: Vec<_> = fs::read_dir(&path)?.collect::<Result<_, _>>()?;
if entries.len() == 1 && entries[0].file_name() == ".DS_Store" {
let _ = fs::remove_file(entries[0].path());
}
if fs::read_dir(&path)?.next().is_some() {
return Ok(());
}
fs::remove_dir(&path)
} else if path.exists() {
fs::remove_file(&path)
} else {
Ok(())
}
}
fn subdirectories(path: &Path) -> Vec<PathBuf> {
let mut out: Vec<PathBuf> = fs::read_dir(path)
.map(|r| {
r.filter_map(Result::ok)
.map(|e| e.path())
.filter(|p| p.is_dir())
.collect()
})
.unwrap_or_default();
out.sort();
out
}
/// `FileRelation.DIMENSIONS`.
fn dimensions(base: &Path) -> Vec<PathBuf> {
let root = base.join("dimensions");
let defaults = || {
["overworld", "the_nether", "the_end"]
.map(|d| base.join("dimensions/minecraft").join(d))
.to_vec()
};
if !root.exists() {
return defaults();
}
let found: Vec<PathBuf> = subdirectories(&root)
.iter()
.flat_map(|ns| subdirectories(ns))
.collect();
if found.is_empty() { defaults() } else { found }
}
// ------------------------------------------------------------------ file accessors
/// `LevelDat.read`: level.dat's `Data` brought to `version`.
fn read_level(base: &Path, version: i32) -> io::Result<Option<Tag>> {
Ok(read_dat(&base.join("level.dat"))?.map(|root| {
let mut data = root
.get("Data")
.cloned()
.unwrap_or_else(|| Tag::Compound(Compound::new()));
let from = data_version(&data, -1);
update(Ty::Level, &mut data, from, version);
data
}))
}
fn write_level(base: &Path, mut data: Tag, version: i32) -> io::Result<()> {
set(&mut data, "DataVersion", Tag::Int(version));
write_dat(&base.join("level.dat"), &Tag::compound([("Data", data)]))
}
/// `SavedDataNbt.write`.
fn write_saved_data(path: &Path, data: Tag, version: i32) -> io::Result<()> {
write_dat(
path,
&Tag::compound([("data", data), ("DataVersion", Tag::Int(version))]),
)
}
/// `SavedDataNbt.read`: the `data` of a saved data file brought to `version`.
fn read_saved_data(path: &Path, ty: Ty, version: i32) -> io::Result<Option<Tag>> {
Ok(read_dat(path)?.map(|mut tag| {
let from = data_version(&tag, -1);
update(ty, &mut tag, from, version);
tag.get("data")
.cloned()
.unwrap_or_else(|| Tag::Compound(Compound::new()))
}))
}
// ------------------------------------------------------------------ the file fixes
/// 4772: `ResourcePackLocationFileFix`, `DimensionStorageFileFix`,
/// `PlayerStorageFileFix`, `LevelDatToSavedDataFileFix`, `RemoveObsoleteFilesFileFix`.
fn fix_4772(base: &Path) -> io::Result<()> {
move_file(base, "resources.zip", "resourcepacks/resources.zip")?;
for dim in dimensions(base) {
let data = dim.join("data");
move_file(&data, "chunks.dat", "minecraft/chunk_tickets.dat")?;
move_file(&data, "raids.dat", "minecraft/raids.dat")?;
move_file(&data, "world_border.dat", "minecraft/world_border.dat")?;
}
for (from, to) in [
("data", "dimensions/minecraft/overworld/data/minecraft"),
(
"DIM-1/data",
"dimensions/minecraft/the_nether/data/minecraft",
),
("DIM1/data", "dimensions/minecraft/the_end/data/minecraft"),
] {
for (a, b) in [
("chunks.dat", "chunk_tickets.dat"),
("raids.dat", "raids.dat"),
("raids_end.dat", "raids.dat"),
("world_border.dat", "world_border.dat"),
] {
move_file(base, &format!("{from}/{a}"), &format!("{to}/{b}"))?;
}
}
delete(base, "DIM1/data")?;
delete(base, "DIM-1/data")?;
let data = base.join("data");
for (a, b) in [
("scoreboard.dat", "minecraft/scoreboard.dat"),
("stopwatches.dat", "minecraft/stopwatches.dat"),
] {
move_file(&data, a, b)?;
}
regex_move(&data, |name| {
let ns = name
.strip_prefix("command_storage_")?
.strip_suffix(".dat")?;
(!ns.is_empty()
&& ns
.bytes()
.all(|b| matches!(b, b'a'..=b'z' | b'0'..=b'9' | b'_' | b'.' | b'-')))
.then(|| format!("{ns}/command_storage.dat"))
})?;
move_file(&data, "idcounts.dat", "minecraft/maps/last_id.dat")?;
regex_move(&data, |name| {
let id = name.strip_prefix("map_")?.strip_suffix(".dat")?;
(!id.is_empty() && id.bytes().all(|b| b.is_ascii_digit()))
.then(|| format!("minecraft/maps/{id}.dat"))
})?;
move_file(
&data,
"random_sequences.dat",
"minecraft/random_sequences.dat",
)?;
for (from, to) in [
("", "dimensions/minecraft/overworld"),
("DIM-1", "dimensions/minecraft/the_nether"),
("DIM1", "dimensions/minecraft/the_end"),
] {
for f in ["region", "entities", "poi"] {
move_file(base, &format!("{from}/{f}"), &format!("{to}/{f}"))?;
}
}
delete(base, "DIM-1")?;
delete(base, "DIM1")?;
move_file(base, "advancements", "players/advancements")?;
move_file(base, "playerdata", "players/data")?;
move_file(base, "stats", "players/stats")?;
level_dat_to_saved_data(base)?;
for f in [
"data/villages.dat",
"data/villages_end.dat",
"data/villages_nether.dat",
"data/advancements",
"data/functions",
] {
delete(base, f)?;
}
for s in [
"Monument",
"Stronghold",
"Mineshaft",
"Temple",
"Mansion",
"Village",
"Fortress",
"EndCity",
] {
delete(base, &format!("data/{s}.dat"))?;
delete(base, &format!("data/{s}_index.dat"))?;
}
Ok(())
}
/// `RegexMove` over the files of a folder.
fn regex_move(folder: &Path, rename: impl Fn(&str) -> Option<String>) -> io::Result<()> {
if !folder.is_dir() {
return Ok(());
}
let mut names: Vec<String> = fs::read_dir(folder)?
.filter_map(Result::ok)
.map(|e| e.file_name().to_string_lossy().into_owned())
.collect();
names.sort();
for name in names {
if let Some(new) = rename(&name) {
move_file(folder, &name, &new)?;
}
}
Ok(())
}
fn level_dat_to_saved_data(base: &Path) -> io::Result<()> {
const V: i32 = 4772;
let Some(mut content) = read_level(base, V)? else {
return Ok(());
};
let data = base.join("data/minecraft");
let dim_data = |dim: &str, file: &str| {
base.join(format!("dimensions/minecraft/{dim}/data/minecraft/{file}"))
};
let extract = |content: &mut Tag, key: &str, path: PathBuf| -> io::Result<()> {
if let Some(tag) = remove(content, key) {
write_saved_data(&path, tag, V)?;
}
Ok(())
};
extract(
&mut content,
"dragon_fight",
dim_data("the_end", "ender_dragon_fight.dat"),
)?;
if let Some(mut player) = remove(&mut content, "Player") {
let from = data_version(&player, -1);
update(Ty::Player, &mut player, from, V);
let uuid = match player.get("UUID") {
Some(u) => u.clone(),
None => {
set(&mut player, "DataVersion", Tag::Int(V));
write_dat(
&base.join("players/data/00000000-0000-0000-0000-000000000000.dat"),
&player,
)?;
int_list(&[0, 0, 0, 0])
}
};
set(&mut content, "singleplayer_uuid", uuid);
}
extract(
&mut content,
"wandering_trader_migration_data",
data.join("wandering_trader.dat"),
)?;
extract(
&mut content,
"CustomBossEvents",
data.join("custom_boss_events.dat"),
)?;
extract(&mut content, "weather_data", data.join("weather.dat"))?;
extract(
&mut content,
"scheduled_events",
data.join("scheduled_events.dat"),
)?;
if let Some(border) = content.get("world_border").cloned() {
for (dim, divider) in [("overworld", 1.0), ("the_nether", 8.0), ("the_end", 1.0)] {
let mut b = border.clone();
for k in ["center_x", "center_z"] {
if let Some(v) = b.get(k) {
let v = super::dynamic::as_double(Some(v), 0.0) / divider;
set(&mut b, k, Tag::Double(v.to_bits()));
}
}
write_saved_data(&dim_data(dim, "world_border.dat"), b, V)?;
}
}
remove(&mut content, "world_border");
extract(&mut content, "game_rules", data.join("game_rules.dat"))?;
if let Some(mut settings) = remove(&mut content, "world_gen_settings") {
let from = data_version(&content, -1);
update(Ty::WorldGenSettings, &mut settings, from, V);
write_saved_data(&data.join("world_gen_settings.dat"), settings, V)?;
}
extract(&mut content, "world_clocks", data.join("world_clocks.dat"))?;
write_level(base, content, V)
}
/// 4773: `GeneratedStructuresRenameFileFix`.
fn fix_4773(base: &Path) -> io::Result<()> {
for ns in subdirectories(&base.join("generated")) {
move_file(&ns, "structures", "structure")?;
}
Ok(())
}
/// 4899: `ReenableSpectatorsGenerateChunksInHardcoreWorldsFileFix`.
fn fix_4899(base: &Path) -> io::Result<()> {
const V: i32 = 4899;
let Some(level) = read_level(base, V)? else {
return Ok(());
};
let hardcore = level
.get("difficulty_settings")
.is_some_and(|d| as_bool(d.get("hardcore"), false));
if level.get("singleplayer_uuid").is_some() && hardcore {
let path = base.join("data/minecraft/game_rules.dat");
if let Some(mut rules) = read_saved_data(&path, Ty::SavedGameRules, V)? {
if rules.get("minecraft:spectators_generate_chunks").is_some() {
set(
&mut rules,
"minecraft:spectators_generate_chunks",
Tag::Byte(1),
);
}
write_saved_data(&path, rules, V)?;
}
}
Ok(())
}
/// `FileFixerUpper.addVersionsToLevelData`.
fn add_versions(data: &mut Tag) {
set(data, "DataVersion", Tag::Int(CURRENT));
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |d| d.as_millis() as i64);
set(data, "LastPlayed", Tag::Long(now));
set(
data,
"Version",
Tag::compound([
("Name", string(VERSION_NAME)),
("Id", Tag::Int(CURRENT)),
("Snapshot", Tag::Byte(0)),
("Series", string("main")),
]),
);
}
/// `FileFixerUpper.fix`: the file fixes a world saved at an older version
/// needs, then its level.dat at the current version. Returns the DataVersion
/// the world had.
pub fn fix_world_files(base: &Path) -> io::Result<i32> {
let root = read_dat(&base.join("level.dat"))?.ok_or_else(|| invalid("missing level.dat"))?;
let mut data = root
.get("Data")
.cloned()
.unwrap_or_else(|| Tag::Compound(Compound::new()));
let loaded = data_version(&data, -1);
if loaded >= CURRENT {
return Ok(loaded);
}
let mut from = loaded;
if requires_file_fixing(loaded) {
let start = if loaded < FILE_FIXER_INTRODUCTION {
0
} else {
loaded
};
for version in FILE_FIXES {
if version > start && version <= CURRENT {
match version {
4772 => fix_4772(base)?,
4773 => fix_4773(base)?,
_ => fix_4899(base)?,
}
}
}
// `writeUpdatedLevelData`, then the world swapped in and read again.
let root =
read_dat(&base.join("level.dat"))?.ok_or_else(|| invalid("missing level.dat"))?;
data = root
.get("Data")
.cloned()
.unwrap_or_else(|| Tag::Compound(Compound::new()));
from = data_version(&data, -1);
let _ = fs::remove_file(base.join("level.dat_old"));
let _ = fs::remove_file(base.join("session.lock"));
}
update(Ty::Level, &mut data, from, CURRENT);
add_versions(&mut data);
write_dat(&base.join("level.dat"), &Tag::compound([("Data", data)]))?;
Ok(loaded)
}
// ------------------------------------------------------------------ the read-time upgrades
/// Chunk generator types per dimension from `world_gen_settings.dat`.
fn generators(base: &Path) -> io::Result<Vec<(String, String)>> {
let Some(tag) = read_dat(&base.join("data/minecraft/world_gen_settings.dat"))? else {
return Ok(Vec::new());
};
let mut out = Vec::new();
if let Some(Tag::Compound(dims)) = tag.get("data").and_then(|d| d.get("dimensions")) {
for (id, dim) in dims {
if let Some(ty) = dim.get("generator").and_then(|g| as_str(g.get("type"))) {
out.push((id.to_string_lossy(), ty));
}
}
}
Ok(out)
}
fn files_with_suffix(folder: &Path, suffix: &str) -> Vec<PathBuf> {
let mut out: Vec<PathBuf> = fs::read_dir(folder)
.map(|r| {
r.filter_map(Result::ok)
.map(|e| e.path())
.filter(|p| p.is_file() && p.to_string_lossy().ends_with(suffix))
.collect()
})
.unwrap_or_default();
out.sort();
out
}
/// Rewrite every chunk of the region files of a folder with `fix`.
fn upgrade_regions(folder: &Path, mut fix: impl FnMut(&mut Tag)) -> io::Result<()> {
let limits = Limits::default();
for path in files_with_suffix(folder, ".mca") {
let mut region = RegionFile::open(&path)?;
let positions: Vec<_> = region.chunks().map(|(p, _)| p).collect();
let mut records = Vec::new();
for pos in positions {
let Some(record) = region.read(pos)? else {
continue;
};
let mut root = record.nbt(limits)?;
fix(&mut root.tag);
records.push((
pos,
ChunkRecord::from_nbt(&root, record.compression, record.timestamp, limits)?,
));
}
drop(region);
let staging = folder.join(".datafix");
fs::create_dir_all(&staging)?;
let temporary = staging.join(path.file_name().unwrap_or_default());
let _ = fs::remove_file(&temporary);
let mut writer = RegionWriter::create(&temporary)?;
for (pos, record) in &records {
writer.write(*pos, record)?;
}
writer.finish()?;
fs::rename(&temporary, &path)?;
fs::remove_dir(&staging)?;
}
Ok(())
}
/// `SavedDataStorage.readTagFromDisk` over every known file of a data folder.
fn upgrade_saved_data(data: &Path) -> io::Result<()> {
if !data.is_dir() {
return Ok(());
}
let mut files = Vec::new();
let mut stack = vec![data.to_path_buf()];
while let Some(dir) = stack.pop() {
for entry in fs::read_dir(&dir)?.filter_map(Result::ok) {
let p = entry.path();
if p.is_dir() {
stack.push(p);
} else if p.to_string_lossy().ends_with(".dat") {
files.push(p);
}
}
}
files.sort();
for file in files {
let relative = file
.strip_prefix(data)
.unwrap_or(&file)
.to_string_lossy()
.replace('\\', "/");
let Some(ty) = super::saved_data_type(&relative) else {
continue;
};
if let Some(mut tag) = read_dat(&file)? {
super::update_saved_data(ty, &mut tag);
set(&mut tag, "DataVersion", Tag::Int(CURRENT));
write_dat(&file, &tag)?;
}
}
Ok(())
}
/// A JSON file of the player folders as NBT for the fixes (`JsonOps` and
/// `NbtOps` see the same structure), booleans kept apart.
fn json_to_tag(j: &Json) -> Tag {
match j {
Json::Bool(b) => Tag::compound([("\u{0}bool", Tag::Byte(i8::from(*b)))]),
Json::Arr(items) => Tag::List(items.iter().map(json_to_tag).collect()),
Json::Obj(entries) => Tag::Compound(
entries
.iter()
.map(|(k, v)| (JavaString(k.clone()), json_to_tag(v)))
.collect(),
),
other => json::to_nbt(other).unwrap_or(Tag::End),
}
}
fn tag_to_json(t: &Tag) -> Json {
match t {
Tag::Compound(c) if c.len() == 1 && c.contains_key(&JavaString::from("\u{0}bool")) => {
Json::Bool(as_int(t.get("\u{0}bool"), 0) != 0)
}
Tag::List(items) => Json::Arr(items.iter().map(tag_to_json).collect()),
Tag::Compound(c) => Json::Obj(
c.iter()
.map(|(k, v)| (k.0.clone(), tag_to_json(v)))
.collect(),
),
other => json::from_nbt(other),
}
}
fn upgrade_json(path: &Path, ty: Ty) -> io::Result<()> {
let text = fs::read_to_string(path)?;
let units: Vec<u16> = text.encode_utf16().collect();
let parsed = json::parse_lenient(&units)
.ok_or_else(|| invalid(format!("bad JSON in {}", path.display())))?;
let mut tag = json_to_tag(&parsed);
let from = super::dynamic::as_int(tag.get("DataVersion"), 1343);
if ty == Ty::Advancements {
// `PlayerAdvancements`: the version field is not part of the data.
remove(&mut tag, "DataVersion");
}
update(ty, &mut tag, from, CURRENT);
set(&mut tag, "DataVersion", Tag::Int(CURRENT));
let out = json::to_stable_string(&tag_to_json(&tag));
fs::write(path, String::from_utf16_lossy(&out))
}
/// Everything vanilla upgrades as it reads a world, done at once: every chunk,
/// entity chunk and POI chunk, saved data, player, advancements and
/// statistics file. Run after [`fix_world_files`].
pub fn upgrade_world_data(base: &Path) -> io::Result<()> {
let generators = generators(base)?;
for ns in subdirectories(&base.join("dimensions")) {
for dim in subdirectories(&ns) {
let id = format!(
"{}:{}",
ns.file_name().unwrap_or_default().to_string_lossy(),
dim.file_name().unwrap_or_default().to_string_lossy()
);
let generator = generators
.iter()
.find(|(d, _)| *d == id)
.map(|(_, g)| g.clone());
upgrade_regions(&dim.join("region"), |tag| {
super::update_chunk(tag, &id, generator.as_deref())
})?;
upgrade_regions(&dim.join("entities"), |tag| {
super::update_region_tag(Ty::EntityChunk, tag)
})?;
upgrade_regions(&dim.join("poi"), |tag| {
super::update_region_tag(Ty::PoiChunk, tag)
})?;
upgrade_saved_data(&dim.join("data"))?;
}
}
upgrade_saved_data(&base.join("data"))?;
for file in files_with_suffix(&base.join("players/data"), ".dat") {
if let Some(mut tag) = read_dat(&file)? {
super::update_player(&mut tag);
set(&mut tag, "DataVersion", Tag::Int(CURRENT));
write_dat(&file, &tag)?;
}
}
for file in files_with_suffix(&base.join("players/advancements"), ".json") {
upgrade_json(&file, Ty::Advancements)?;
}
for file in files_with_suffix(&base.join("players/stats"), ".json") {
upgrade_json(&file, Ty::Stats)?;
}
Ok(())
}
/// Bring a whole world saved by 1.20 to 26.2 to the 26.3 format in place.
pub fn upgrade_world(base: &Path) -> io::Result<i32> {
let version = fix_world_files(base)?;
upgrade_world_data(base)?;
Ok(version)
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,714 @@
//! Fixes of 1.19 to 1.20 (keys 3076 to 3459): what worlds saved by 1.18.2 to
//! 1.19.4 need before the fixes of [`super::fixes_a`]. The options fixes of
//! this range (3201, 3214, 3319) are client settings, not world data.
use super::dynamic::{
as_number, as_str, is_list, namespaced, remove, rename, set, stream, string, update,
};
use super::fixes_a::fix_block_pos;
use super::fixes_b::feature_flag_remove;
use super::schema::{Key, Ty, key};
use super::{Cx, FixFn, id_is, renamer, reread_as};
use crate::nbt::{Compound, Tag};
pub(super) fn register(out: &mut Vec<(Key, FixFn)>) {
out.push((key(3077, 0), chunk_delete_ignored_light));
out.push((key(3084, 0), |cx| {
cx.rename(Ty::GameEventName, renamer(GAME_EVENTS))
}));
out.push((key(3086, 0), |cx| {
entity_variant(cx, "minecraft:cat", "CatType", cat_type)
}));
out.push((key(3086, 0), |cx| {
criteria_rename(cx, "minecraft:husbandry/complete_catalogue", &|s| {
CAT_TEXTURES
.iter()
.find(|(from, _)| *from == s)
.map(|(_, to)| (*to).to_owned())
});
}));
out.push((key(3087, 0), |cx| {
entity_variant(cx, "minecraft:frog", "Variant", |n| match n {
0 => Some("minecraft:temperate"),
1 => Some("minecraft:warm"),
2 => Some("minecraft:cold"),
_ => None,
})
}));
out.push((key(3090, 0), |cx| {
cx.entity("minecraft:painting", |e| {
rename(e, "Motive", "variant");
rename(e, "Facing", "facing");
})
}));
out.push((key(3093, 0), |cx| {
cx.entity("minecraft:goat", |e| {
set(e, "HasLeftHorn", Tag::Byte(1));
set(e, "HasRightHorn", Tag::Byte(1));
})
}));
out.push((key(3094, 0), goat_horn_id));
out.push((key(3097, 0), filtered_books));
out.push((key(3097, 0), filtered_signs));
out.push((key(3097, 0), |cx| {
cx.entity("minecraft:cat", |e| {
if as_str(e.get("variant")).as_deref() == Some("minecraft:british") {
set(e, "variant", string("minecraft:british_shorthair"));
}
})
}));
out.push((key(3097, 0), |cx| {
criteria_rename(cx, "minecraft:husbandry/complete_catalogue", &|s| {
(s == "minecraft:british").then(|| "minecraft:british_shorthair".to_owned())
});
}));
out.push((key(3097, 0), |cx| {
poi_records(cx, |r| {
!matches!(
as_str(r.get("type")).as_deref(),
Some("minecraft:unemployed" | "minecraft:nitwit") | None
)
})
}));
out.push((key(3108, 0), |cx| {
cx.each_out(Ty::Chunk, |c| {
let dimension = c.get("__context").and_then(|x| as_str(x.get("dimension")));
if dimension.as_deref() != Some("minecraft:overworld") {
remove(c, "blending_data");
}
})
}));
out.push((key(3209, 0), pig_spawn_egg));
out.push((key(3322, 0), effect_duration));
out.push((key(3438, 0), |cx| {
cx.each(Ty::BlockEntity, |be| {
if id_is(be, "minecraft:suspicious_sand") {
set(be, "id", string("minecraft:brushable_block"));
}
})
}));
out.push((key(3438, 0), |cx| {
cx.each_out(Ty::BlockEntity, |be| {
if id_is(be, "minecraft:brushable_block") {
rename(be, "loot_table", "LootTable");
rename(be, "loot_table_seed", "LootTableSeed");
}
})
}));
out.push((key(3438, 0), |cx| {
cx.rename(Ty::ItemName, renamer(POTTERY_SHARDS))
}));
out.push((key(3439, 0), |cx| sign_double_sided(cx, "minecraft:sign")));
out.push((key(3439, 1), |cx| {
sign_double_sided(cx, "minecraft:hanging_sign")
}));
out.push((key(3440, 0), multi_noise_preset));
out.push((key(3440, 0), |cx| {
feature_flag_remove(cx, &["minecraft:update_1_20"])
}));
out.push((key(3447, 0), |cx| {
cx.rename(Ty::ItemName, |id| {
let id = namespaced(id);
POTTERY_SHERDS
.iter()
.find(|(from, _)| *from == id)
.map(|(_, to)| *to)
})
}));
// DecoratedPotFieldRenameFix converts unchecked: the typed shards are written
// as sherds, and the raw shards come back from the block entity's remainder.
out.push((key(3448, 0), |cx| {
cx.each(Ty::BlockEntity, |be| {
if id_is(be, "minecraft:decorated_pot")
&& let Some(shards) = be.get("shards").cloned()
{
set(be, "sherds", shards);
super::drop_typed(be, "shards");
}
})
}));
out.push((key(3450, 0), remap_chunk_status));
out.push((key(3451, 0), chunk_delete_light));
out.push((key(3459, 0), legacy_dragon_fight));
}
// ------------------------------------------------------------------ 3077, 3451
/// `ChunkDeleteIgnoredLightDataFix`.
fn chunk_delete_ignored_light(cx: &mut Cx) {
cx.each(Ty::Chunk, |c| {
let light_on = c
.get("isLightOn")
.and_then(as_number)
.is_some_and(|n| n.double_value() != 0.0);
if !light_on && let Some(Tag::List(sections)) = c.get_mut("sections") {
for s in sections {
remove(s, "BlockLight");
remove(s, "SkyLight");
}
}
});
}
/// `ChunkDeleteLightFix`.
fn chunk_delete_light(cx: &mut Cx) {
cx.each(Ty::Chunk, |c| {
remove(c, "isLightOn");
if let Some(Tag::List(sections)) = c.get_mut("sections") {
for s in sections {
remove(s, "BlockLight");
remove(s, "SkyLight");
}
}
});
}
// ------------------------------------------------------------------ 3084
const GAME_EVENTS: &[(&str, &str)] = &[
("minecraft:block_press", "minecraft:block_activate"),
("minecraft:block_switch", "minecraft:block_activate"),
("minecraft:block_unpress", "minecraft:block_deactivate"),
("minecraft:block_unswitch", "minecraft:block_deactivate"),
("minecraft:drinking_finish", "minecraft:drink"),
("minecraft:elytra_free_fall", "minecraft:elytra_glide"),
("minecraft:entity_damaged", "minecraft:entity_damage"),
("minecraft:entity_dying", "minecraft:entity_die"),
("minecraft:entity_killed", "minecraft:entity_die"),
("minecraft:mob_interact", "minecraft:entity_interact"),
("minecraft:ravager_roar", "minecraft:entity_roar"),
("minecraft:ring_bell", "minecraft:block_change"),
("minecraft:shulker_close", "minecraft:container_close"),
("minecraft:shulker_open", "minecraft:container_open"),
("minecraft:wolf_shaking", "minecraft:entity_shake"),
];
// ------------------------------------------------------------------ 3086, 3087, 3097
fn cat_type(n: i32) -> Option<&'static str> {
Some(match n {
1 => "minecraft:black",
2 => "minecraft:red",
3 => "minecraft:siamese",
4 => "minecraft:british",
5 => "minecraft:calico",
6 => "minecraft:persian",
7 => "minecraft:ragdoll",
8 => "minecraft:white",
9 => "minecraft:jellie",
10 => "minecraft:all_black",
// The map's default return value.
_ => "minecraft:tabby",
})
}
/// `EntityVariantFix`: the numeric variant written to `variant`; the old field
/// stays (`updateAndRename` only sets the new key). An id the map has no entry
/// for gives Java's `null`, which `createString` cannot take: such a variant is
/// left as the number.
fn entity_variant(cx: &mut Cx, id: &str, field: &str, ids: fn(i32) -> Option<&'static str>) {
cx.entity(id, |e| {
let Some(value) = e.get(field).cloned() else {
return;
};
let new = match as_number(&value).map(|n| ids(n.int_value())) {
Some(Some(name)) => string(name),
_ => value,
};
set(e, "variant", new);
});
}
const CAT_TEXTURES: &[(&str, &str)] = &[
("textures/entity/cat/tabby.png", "minecraft:tabby"),
("textures/entity/cat/black.png", "minecraft:black"),
("textures/entity/cat/red.png", "minecraft:red"),
("textures/entity/cat/siamese.png", "minecraft:siamese"),
(
"textures/entity/cat/british_shorthair.png",
"minecraft:british",
),
("textures/entity/cat/calico.png", "minecraft:calico"),
("textures/entity/cat/persian.png", "minecraft:persian"),
("textures/entity/cat/ragdoll.png", "minecraft:ragdoll"),
("textures/entity/cat/white.png", "minecraft:white"),
("textures/entity/cat/jellie.png", "minecraft:jellie"),
("textures/entity/cat/all_black.png", "minecraft:all_black"),
];
/// `CriteriaRenameFix`: the criteria keys of one advancement.
fn criteria_rename(cx: &mut Cx, advancement: &str, conversions: &dyn Fn(&str) -> Option<String>) {
cx.each(Ty::Advancements, |v| {
let Some(criteria) = v.get_mut(advancement).and_then(|a| a.get_mut("criteria")) else {
return;
};
if let Tag::Compound(c) = criteria {
let old = std::mem::take(c);
for (k, value) in old {
let name = k.to_string_lossy();
let k = conversions(&name).map_or(k, |n| n.as_str().into());
c.insert(k, value);
}
}
});
}
// ------------------------------------------------------------------ 3094, 3097
/// The `tag` of every stack whose id passes `filter` (`ItemStackTagFix`).
fn item_tag(cx: &mut Cx, filter: impl Fn(&str) -> bool, mut f: impl FnMut(&mut Tag)) {
cx.item(filter, |s| {
if let Some(tag) = s.get_mut("tag") {
f(tag);
}
});
}
const INSTRUMENTS: [&str; 8] = [
"minecraft:ponder_goat_horn",
"minecraft:sing_goat_horn",
"minecraft:seek_goat_horn",
"minecraft:feel_goat_horn",
"minecraft:admire_goat_horn",
"minecraft:call_goat_horn",
"minecraft:yearn_goat_horn",
"minecraft:dream_goat_horn",
];
/// `GoatHornIdFix`.
fn goat_horn_id(cx: &mut Cx) {
item_tag(
cx,
|id| id == "minecraft:goat_horn",
|tag| {
let variant = super::dynamic::as_int(tag.get("SoundVariant"), 0);
let sound = INSTRUMENTS[if (0..8).contains(&variant) {
variant as usize
} else {
0
}];
remove(tag, "SoundVariant");
set(tag, "instrument", string(sound));
},
);
}
/// `FilteredBooksFix`.
fn filtered_books(cx: &mut Cx) {
item_tag(
cx,
|id| id == "minecraft:writable_book" || id == "minecraft:written_book",
|tag| {
remove(tag, "filtered_title");
remove(tag, "filtered_pages");
},
);
}
/// `FilteredSignsFix` (a `NamedEntityWriteReadFix` over signs only).
fn filtered_signs(cx: &mut Cx) {
let k = cx.k;
cx.block_entity("minecraft:sign", |be| {
for i in 1..=4 {
remove(be, &format!("FilteredText{i}"));
}
reread_as(Ty::BlockEntity, k, be);
});
}
/// `AbstractPoiSectionFix`: the records of every POI section kept by `keep`.
fn poi_records(cx: &mut Cx, keep: impl Fn(&Tag) -> bool) {
cx.each(Ty::PoiChunk, |poi| {
let Some(Tag::Compound(sections)) = poi.get_mut("Sections") else {
return;
};
for section in sections.values_mut() {
if let Some(records) = section.get("Records")
&& is_list(Some(records))
{
let kept: Vec<Tag> = stream(Some(records))
.into_iter()
.filter(|r| keep(r))
.collect();
set(section, "Records", Tag::List(kept));
}
}
});
}
// ------------------------------------------------------------------ 3209
/// `ItemStackSpawnEggFix` for `minecraft:pig_spawn_egg`: an egg whose entity has
/// an id becomes that entity's egg.
fn pig_spawn_egg(cx: &mut Cx) {
cx.item(
|id| id == "minecraft:pig_spawn_egg",
|s| {
let entity = s
.get("tag")
.and_then(|t| t.get("EntityTag"))
.and_then(|e| as_str(e.get("id")));
if let Some(entity) = entity {
let egg = SPAWN_EGGS
.iter()
.find(|(e, _)| *e == namespaced(&entity))
.map_or("minecraft:pig_spawn_egg", |(_, egg)| egg);
set(s, "id", string(egg));
}
},
);
}
const SPAWN_EGGS: &[(&str, &str)] = &[
("minecraft:bat", "minecraft:bat_spawn_egg"),
("minecraft:blaze", "minecraft:blaze_spawn_egg"),
("minecraft:cave_spider", "minecraft:cave_spider_spawn_egg"),
("minecraft:chicken", "minecraft:chicken_spawn_egg"),
("minecraft:cow", "minecraft:cow_spawn_egg"),
("minecraft:creeper", "minecraft:creeper_spawn_egg"),
("minecraft:donkey", "minecraft:donkey_spawn_egg"),
(
"minecraft:elder_guardian",
"minecraft:elder_guardian_spawn_egg",
),
("minecraft:ender_dragon", "minecraft:ender_dragon_spawn_egg"),
("minecraft:enderman", "minecraft:enderman_spawn_egg"),
("minecraft:endermite", "minecraft:endermite_spawn_egg"),
(
"minecraft:evocation_illager",
"minecraft:evocation_illager_spawn_egg",
),
("minecraft:ghast", "minecraft:ghast_spawn_egg"),
("minecraft:guardian", "minecraft:guardian_spawn_egg"),
("minecraft:horse", "minecraft:horse_spawn_egg"),
("minecraft:husk", "minecraft:husk_spawn_egg"),
("minecraft:iron_golem", "minecraft:iron_golem_spawn_egg"),
("minecraft:llama", "minecraft:llama_spawn_egg"),
("minecraft:magma_cube", "minecraft:magma_cube_spawn_egg"),
("minecraft:mooshroom", "minecraft:mooshroom_spawn_egg"),
("minecraft:mule", "minecraft:mule_spawn_egg"),
("minecraft:ocelot", "minecraft:ocelot_spawn_egg"),
("minecraft:pufferfish", "minecraft:pufferfish_spawn_egg"),
("minecraft:parrot", "minecraft:parrot_spawn_egg"),
("minecraft:pig", "minecraft:pig_spawn_egg"),
("minecraft:polar_bear", "minecraft:polar_bear_spawn_egg"),
("minecraft:rabbit", "minecraft:rabbit_spawn_egg"),
("minecraft:sheep", "minecraft:sheep_spawn_egg"),
("minecraft:shulker", "minecraft:shulker_spawn_egg"),
("minecraft:silverfish", "minecraft:silverfish_spawn_egg"),
("minecraft:skeleton", "minecraft:skeleton_spawn_egg"),
(
"minecraft:skeleton_horse",
"minecraft:skeleton_horse_spawn_egg",
),
("minecraft:slime", "minecraft:slime_spawn_egg"),
("minecraft:snow_golem", "minecraft:snow_golem_spawn_egg"),
("minecraft:spider", "minecraft:spider_spawn_egg"),
("minecraft:squid", "minecraft:squid_spawn_egg"),
("minecraft:stray", "minecraft:stray_spawn_egg"),
("minecraft:turtle", "minecraft:turtle_spawn_egg"),
("minecraft:vex", "minecraft:vex_spawn_egg"),
("minecraft:villager", "minecraft:villager_spawn_egg"),
(
"minecraft:vindication_illager",
"minecraft:vindication_illager_spawn_egg",
),
("minecraft:witch", "minecraft:witch_spawn_egg"),
("minecraft:wither", "minecraft:wither_spawn_egg"),
(
"minecraft:wither_skeleton",
"minecraft:wither_skeleton_spawn_egg",
),
("minecraft:wolf", "minecraft:wolf_spawn_egg"),
("minecraft:zombie", "minecraft:zombie_spawn_egg"),
("minecraft:zombie_horse", "minecraft:zombie_horse_spawn_egg"),
(
"minecraft:zombie_pigman",
"minecraft:zombie_pigman_spawn_egg",
),
(
"minecraft:zombie_villager",
"minecraft:zombie_villager_spawn_egg",
),
];
// ------------------------------------------------------------------ 3322
const POTION_ITEMS: [&str; 4] = [
"minecraft:potion",
"minecraft:splash_potion",
"minecraft:lingering_potion",
"minecraft:tipped_arrow",
];
/// `EffectDurationFix.fix`: every effect's factor data counts ticks active.
fn effect_list(list: Tag) -> Tag {
if !is_list(Some(&list)) {
return list;
}
Tag::List(
stream(Some(&list))
.into_iter()
.map(|mut effect| {
let duration = super::dynamic::as_int(effect.get("Duration"), -1);
update(&mut effect, "FactorCalculationData", |mut data| {
let timestamp =
super::dynamic::as_int(data.get("effect_changed_timestamp"), -1);
remove(&mut data, "effect_changed_timestamp");
set(
&mut data,
"ticks_active",
Tag::Int(timestamp.wrapping_sub(duration)),
);
data
});
effect
})
.collect(),
)
}
fn effect_duration(cx: &mut Cx) {
let living = |e: &mut Tag| {
for field in ["Effects", "ActiveEffects", "CustomPotionEffects"] {
update(e, field, effect_list);
}
};
cx.each(Ty::Entity, living);
cx.each(Ty::Player, living);
item_tag(
cx,
|id| POTION_ITEMS.contains(&id),
|tag| update(tag, "CustomPotionEffects", effect_list),
);
}
// ------------------------------------------------------------------ 3438, 3447
const POTTERY_SHARDS: &[(&str, &str)] = &[
(
"minecraft:pottery_shard_archer",
"minecraft:archer_pottery_shard",
),
(
"minecraft:pottery_shard_prize",
"minecraft:prize_pottery_shard",
),
(
"minecraft:pottery_shard_arms_up",
"minecraft:arms_up_pottery_shard",
),
(
"minecraft:pottery_shard_skull",
"minecraft:skull_pottery_shard",
),
];
const POTTERY_SHERDS: &[(&str, &str)] = &[
(
"minecraft:angler_pottery_shard",
"minecraft:angler_pottery_sherd",
),
(
"minecraft:archer_pottery_shard",
"minecraft:archer_pottery_sherd",
),
(
"minecraft:arms_up_pottery_shard",
"minecraft:arms_up_pottery_sherd",
),
(
"minecraft:blade_pottery_shard",
"minecraft:blade_pottery_sherd",
),
(
"minecraft:brewer_pottery_shard",
"minecraft:brewer_pottery_sherd",
),
(
"minecraft:burn_pottery_shard",
"minecraft:burn_pottery_sherd",
),
(
"minecraft:danger_pottery_shard",
"minecraft:danger_pottery_sherd",
),
(
"minecraft:explorer_pottery_shard",
"minecraft:explorer_pottery_sherd",
),
(
"minecraft:friend_pottery_shard",
"minecraft:friend_pottery_sherd",
),
(
"minecraft:heart_pottery_shard",
"minecraft:heart_pottery_sherd",
),
(
"minecraft:heartbreak_pottery_shard",
"minecraft:heartbreak_pottery_sherd",
),
(
"minecraft:howl_pottery_shard",
"minecraft:howl_pottery_sherd",
),
(
"minecraft:miner_pottery_shard",
"minecraft:miner_pottery_sherd",
),
(
"minecraft:mourner_pottery_shard",
"minecraft:mourner_pottery_sherd",
),
(
"minecraft:plenty_pottery_shard",
"minecraft:plenty_pottery_sherd",
),
(
"minecraft:prize_pottery_shard",
"minecraft:prize_pottery_sherd",
),
(
"minecraft:sheaf_pottery_shard",
"minecraft:sheaf_pottery_sherd",
),
(
"minecraft:shelter_pottery_shard",
"minecraft:shelter_pottery_sherd",
),
(
"minecraft:skull_pottery_shard",
"minecraft:skull_pottery_sherd",
),
(
"minecraft:snort_pottery_shard",
"minecraft:snort_pottery_sherd",
),
];
// ------------------------------------------------------------------ 3439
/// `LegacyComponentDataFixUtils.createEmptyComponent`.
fn empty_component() -> Tag {
string("{\"text\":\"\"}")
}
/// `BlockEntitySignDoubleSidedEditableTextFix` (a `NamedEntityWriteReadFix`).
fn sign_double_sided(cx: &mut Cx, id: &str) {
let k = cx.k;
cx.block_entity(id, |be| {
let lines: Vec<Tag> = (1..=4)
.map(|i| {
be.get(&format!("Text{i}"))
.cloned()
.unwrap_or_else(empty_component)
})
.collect();
let filtered: Vec<Option<Tag>> = (1..=4)
.map(|i| be.get(&format!("FilteredText{i}")).cloned())
.collect();
let mut front = Compound::new();
front.insert("messages".into(), Tag::List(lines.clone()));
front.insert(
"color".into(),
be.get("Color").cloned().unwrap_or_else(|| string("black")),
);
front.insert(
"has_glowing_text".into(),
be.get("GlowingText").cloned().unwrap_or(Tag::Byte(0)),
);
if filtered.iter().any(Option::is_some) {
let messages = filtered
.into_iter()
.zip(&lines)
.map(|(f, line)| f.unwrap_or_else(|| line.clone()))
.collect();
front.insert("filtered_messages".into(), Tag::List(messages));
}
set(be, "front_text", Tag::Compound(front));
let back = Tag::compound([
("messages", Tag::List(vec![empty_component(); 4])),
("color", string("black")),
("has_glowing_text", Tag::Byte(0)),
]);
set(be, "back_text", back);
set(be, "is_waxed", Tag::Byte(0));
set(be, "_filtered_correct", Tag::Byte(1));
for field in super::fixes_a::SIGN_FIELDS_TO_DROP {
remove(be, field);
}
reread_as(Ty::BlockEntity, k, be);
});
}
// ------------------------------------------------------------------ 3440
/// `NamespacedTypeRenameFix` over `MULTI_NOISE_BIOME_SOURCE_PARAMETER_LIST`: the
/// preset of a multi-noise biome source in the world generation settings.
fn multi_noise_preset(cx: &mut Cx) {
cx.each(Ty::WorldGenSettings, |v| {
let Some(Tag::Compound(dims)) = v.get_mut("dimensions") else {
return;
};
for dim in dims.values_mut() {
let Some(g) = dim.get_mut("generator") else {
continue;
};
if as_str(g.get("type")).as_deref() != Some("minecraft:noise") {
continue;
}
let Some(source) = g.get_mut("biome_source") else {
continue;
};
if as_str(source.get("type")).as_deref() != Some("minecraft:multi_noise") {
continue;
}
if let Some(preset) = as_str(source.get("preset"))
&& namespaced(&preset) == "minecraft:overworld_update_1_20"
{
set(source, "preset", string("minecraft:overworld"));
}
}
});
}
// ------------------------------------------------------------------ 3450, 3459
/// `RemapChunkStatusFix`: statuses namespaced, then mapped.
fn remap_chunk_status(cx: &mut Cx) {
let fix = |v: Tag| match as_str(Some(&v)) {
Some(s) => {
let s = namespaced(&s);
string(match s.as_str() {
"minecraft:liquid_carvers" => "minecraft:carvers",
"minecraft:heightmaps" => "minecraft:spawn",
other => other,
})
}
None => v,
};
cx.each(Ty::Chunk, |c| {
update(c, "Status", fix);
if let Some(retrogen) = c.get_mut("below_zero_retrogen") {
update(retrogen, "target_status", fix);
}
});
}
/// `LegacyDragonFightFix`.
fn legacy_dragon_fight(cx: &mut Cx) {
cx.each(Ty::Level, |level| {
if level.get("DragonFight").is_some() {
return;
}
let legacy = level
.get("DimensionData")
.and_then(|d| d.get("1"))
.and_then(|d| d.get("DragonFight"))
.cloned()
.unwrap_or_else(super::dynamic::empty_map);
let mut fight = legacy;
update(&mut fight, "ExitPortalLocation", fix_block_pos);
set(level, "DragonFight", fight);
});
}
File diff suppressed because it is too large Load Diff
+644
View File
@@ -0,0 +1,644 @@
//! The JSON that DataFixers read and write: Gson's `JsonReader` in strict and
//! lenient mode, `GsonHelper.toStableString`, and `JsonOps.convertTo` into NBT.
//! Strings stay UTF-16 like Java's; numbers keep their literal like Gson's
//! `LazilyParsedNumber`.
use crate::nbt::{Compound, JavaString, Tag};
#[derive(Clone, Debug, PartialEq)]
pub enum Json {
Null,
Bool(bool),
/// The literal as written (`LazilyParsedNumber`) or `Number.toString`.
Num(String),
Str(Vec<u16>),
Arr(Vec<Json>),
/// Insertion order, a repeated key replacing the value in place.
Obj(Vec<(Vec<u16>, Json)>),
}
impl Json {
pub fn get(&self, key: &str) -> Option<&Json> {
let key: Vec<u16> = key.encode_utf16().collect();
match self {
Json::Obj(entries) => entries.iter().find(|(k, _)| *k == key).map(|(_, v)| v),
_ => None,
}
}
fn insert(entries: &mut Vec<(Vec<u16>, Json)>, key: Vec<u16>, value: Json) {
if let Some(e) = entries.iter_mut().find(|(k, _)| *k == key) {
e.1 = value;
} else {
entries.push((key, value));
}
}
pub fn object(entries: Vec<(&str, Json)>) -> Json {
let mut out = Vec::new();
for (k, v) in entries {
Json::insert(&mut out, k.encode_utf16().collect(), v);
}
Json::Obj(out)
}
pub fn string(s: &str) -> Json {
Json::Str(s.encode_utf16().collect())
}
}
#[derive(Clone, Copy, PartialEq)]
enum Mode {
Strict,
Lenient,
}
struct Reader<'a> {
s: &'a [u16],
pos: usize,
mode: Mode,
}
type R<T> = Result<T, ()>;
const fn u(c: char) -> u16 {
c as u16
}
impl Reader<'_> {
fn lenient(&self) -> bool {
self.mode == Mode::Lenient
}
fn peek_char(&self) -> Option<u16> {
self.s.get(self.pos).copied()
}
/// `JsonReader.nextNonWhitespace`, with comments when lenient.
fn skip_ws(&mut self) -> R<Option<u16>> {
loop {
let Some(c) = self.peek_char() else {
return Ok(None);
};
match c {
0x20 | 0x0A | 0x09 | 0x0D => self.pos += 1,
0x2F if self.s.get(self.pos + 1) == Some(&u('*'))
|| self.s.get(self.pos + 1) == Some(&u('/')) =>
{
if !self.lenient() {
return Err(());
}
if self.s[self.pos + 1] == u('*') {
let start = self.pos + 2;
let end = (start..self.s.len().saturating_sub(1))
.find(|&i| self.s[i] == u('*') && self.s[i + 1] == u('/'))
.ok_or(())?;
self.pos = end + 2;
} else {
self.skip_line();
}
}
0x23 => {
if !self.lenient() {
return Err(());
}
self.skip_line();
}
_ => return Ok(Some(c)),
}
}
}
fn skip_line(&mut self) {
while let Some(c) = self.peek_char() {
self.pos += 1;
if c == 0x0A || c == 0x0D {
break;
}
}
}
fn value(&mut self, depth: usize) -> R<Json> {
if depth > 255 {
return Err(());
}
let c = self.skip_ws()?.ok_or(())?;
match c {
0x7B => self.object(depth),
0x5B => self.array(depth),
0x22 => {
self.pos += 1;
Ok(Json::Str(self.quoted(0x22)?))
}
0x27 => {
if !self.lenient() {
return Err(());
}
self.pos += 1;
Ok(Json::Str(self.quoted(0x27)?))
}
_ => self.literal(),
}
}
fn object(&mut self, depth: usize) -> R<Json> {
self.pos += 1;
let mut entries = Vec::new();
if self.skip_ws()? == Some(u('}')) {
self.pos += 1;
return Ok(Json::Obj(entries));
}
loop {
let c = self.skip_ws()?.ok_or(())?;
let name = match c {
0x22 => {
self.pos += 1;
self.quoted(0x22)?
}
0x27 if self.lenient() => {
self.pos += 1;
self.quoted(0x27)?
}
_ if self.lenient() => {
let lit = self.unquoted();
if lit.is_empty() {
return Err(());
}
lit
}
_ => return Err(()),
};
match self.skip_ws()?.ok_or(())? {
0x3A => self.pos += 1,
0x3D if self.lenient() => {
self.pos += 1;
if self.peek_char() == Some(u('>')) {
self.pos += 1;
}
}
_ => return Err(()),
}
let value = self.value(depth + 1)?;
Json::insert(&mut entries, name, value);
match self.skip_ws()?.ok_or(())? {
0x7D => {
self.pos += 1;
return Ok(Json::Obj(entries));
}
0x2C => self.pos += 1,
0x3B if self.lenient() => self.pos += 1,
_ => return Err(()),
}
}
}
fn array(&mut self, depth: usize) -> R<Json> {
self.pos += 1;
let mut out = Vec::new();
if self.skip_ws()? == Some(u(']')) {
self.pos += 1;
return Ok(Json::Arr(out));
}
loop {
let c = self.skip_ws()?.ok_or(())?;
// Lenient: an empty element between separators is a null.
if self.lenient() && (c == u(',') || c == u(';')) {
out.push(Json::Null);
self.pos += 1;
continue;
}
if self.lenient() && c == u(']') {
out.push(Json::Null);
self.pos += 1;
return Ok(Json::Arr(out));
}
out.push(self.value(depth + 1)?);
match self.skip_ws()?.ok_or(())? {
0x5D => {
self.pos += 1;
return Ok(Json::Arr(out));
}
0x2C => self.pos += 1,
0x3B if self.lenient() => self.pos += 1,
_ => return Err(()),
}
}
}
fn quoted(&mut self, quote: u16) -> R<Vec<u16>> {
let mut out = Vec::new();
loop {
let c = self.peek_char().ok_or(())?;
self.pos += 1;
if c == quote {
return Ok(out);
}
if c == u('\\') {
let e = self.peek_char().ok_or(())?;
self.pos += 1;
match e {
0x75 => {
let hex = self.s.get(self.pos..self.pos + 4).ok_or(())?;
let text = String::from_utf16(hex).map_err(|_| ())?;
out.push(u16::from_str_radix(&text, 16).map_err(|_| ())?);
self.pos += 4;
}
0x74 => out.push(u('\t')),
0x62 => out.push(0x08),
0x6E => out.push(u('\n')),
0x72 => out.push(u('\r')),
0x66 => out.push(0x0C),
0x0A | 0x22 | 0x5C | 0x2F => out.push(e),
0x27 => out.push(e),
_ if self.lenient() => out.push(e),
_ => return Err(()),
}
} else {
if !self.lenient() && c < 0x20 {
return Err(());
}
out.push(c);
}
}
}
fn unquoted(&mut self) -> Vec<u16> {
let start = self.pos;
while let Some(c) = self.peek_char() {
if matches!(
c,
0x2F | 0x5C
| 0x3B
| 0x23
| 0x3D
| 0x7B
| 0x7D
| 0x5B
| 0x5D
| 0x3A
| 0x2C
| 0x20
| 0x09
| 0x0C
| 0x0D
| 0x0A
) {
break;
}
self.pos += 1;
}
self.s[start..self.pos].to_vec()
}
/// A keyword, a number, or (lenient) an unquoted string.
fn literal(&mut self) -> R<Json> {
let lit = self.unquoted();
if lit.is_empty() {
return Err(());
}
let text = String::from_utf16_lossy(&lit);
for (word, value) in [
("true", Json::Bool(true)),
("false", Json::Bool(false)),
("null", Json::Null),
] {
if text.len() == word.len()
&& text
.chars()
.zip(word.chars())
.all(|(a, b)| a == b || self.lenient() && a == b.to_ascii_uppercase())
{
return Ok(value);
}
}
if is_json_number(&text) {
return Ok(Json::Num(text));
}
if self.lenient() {
return Ok(Json::Str(lit));
}
Err(())
}
}
/// `JsonReader.peekNumber`.
fn is_json_number(s: &str) -> bool {
let b = s.as_bytes();
let mut i = 0;
if b.get(i) == Some(&b'-') {
i += 1;
}
match b.get(i) {
Some(b'0') => {
i += 1;
if b.get(i).is_some_and(u8::is_ascii_digit) {
return false;
}
}
Some(c) if c.is_ascii_digit() => {
while b.get(i).is_some_and(u8::is_ascii_digit) {
i += 1;
}
}
_ => return false,
}
if b.get(i) == Some(&b'.') {
i += 1;
if !b.get(i).is_some_and(u8::is_ascii_digit) {
return false;
}
while b.get(i).is_some_and(u8::is_ascii_digit) {
i += 1;
}
}
if matches!(b.get(i), Some(b'e' | b'E')) {
i += 1;
if matches!(b.get(i), Some(b'+' | b'-')) {
i += 1;
}
if !b.get(i).is_some_and(u8::is_ascii_digit) {
return false;
}
while b.get(i).is_some_and(u8::is_ascii_digit) {
i += 1;
}
}
i == b.len()
}
fn parse_mode(s: &[u16], mode: Mode) -> Option<Json> {
let mut r = Reader { s, pos: 0, mode };
let value = r.value(0).ok()?;
// JsonParser.parseReader: the whole document must be consumed.
if r.skip_ws().ok()?.is_some() {
if mode == Mode::Lenient && value == Json::Null {
return Some(value);
}
return None;
}
Some(value)
}
/// `LenientJsonParser.parse` (Gson `JsonParser.parseString`).
pub fn parse_lenient(s: &[u16]) -> Option<Json> {
parse_mode(s, Mode::Lenient)
}
/// `StrictJsonParser.parse`.
pub fn parse_strict(s: &[u16]) -> Option<Json> {
parse_mode(s, Mode::Strict)
}
/// `GsonHelper.toStableString`: keys in natural (UTF-16) order, Gson escaping.
pub fn to_stable_string(json: &Json) -> Vec<u16> {
let mut out = Vec::new();
write(json, &mut out);
out
}
fn write(json: &Json, out: &mut Vec<u16>) {
let push = |out: &mut Vec<u16>, s: &str| out.extend(s.encode_utf16());
match json {
Json::Null => push(out, "null"),
Json::Bool(b) => push(out, if *b { "true" } else { "false" }),
Json::Num(n) => push(out, n),
Json::Str(s) => write_string(s, out),
Json::Arr(items) => {
out.push(u('['));
for (i, item) in items.iter().enumerate() {
if i > 0 {
out.push(u(','));
}
write(item, out);
}
out.push(u(']'));
}
Json::Obj(entries) => {
let mut sorted: Vec<&(Vec<u16>, Json)> = entries.iter().collect();
sorted.sort_by(|a, b| a.0.cmp(&b.0));
out.push(u('{'));
for (i, (k, v)) in sorted.into_iter().enumerate() {
if i > 0 {
out.push(u(','));
}
write_string(k, out);
out.push(u(':'));
write(v, out);
}
out.push(u('}'));
}
}
}
/// `JsonWriter.string` without HTML escaping.
fn write_string(s: &[u16], out: &mut Vec<u16>) {
out.push(u('"'));
for &c in s {
match c {
0x22 => out.extend("\\\"".encode_utf16()),
0x5C => out.extend("\\\\".encode_utf16()),
0x09 => out.extend("\\t".encode_utf16()),
0x08 => out.extend("\\b".encode_utf16()),
0x0A => out.extend("\\n".encode_utf16()),
0x0D => out.extend("\\r".encode_utf16()),
0x0C => out.extend("\\f".encode_utf16()),
c if c < 0x20 || c == 0x2028 || c == 0x2029 => {
out.extend(format!("\\u{c:04x}").encode_utf16());
}
c => out.push(c),
}
}
out.push(u('"'));
}
/// `JsonOps.convertTo(NbtOps)`. Nulls give `None` (an end tag in Java).
pub fn to_nbt(json: &Json) -> Option<Tag> {
Some(match json {
Json::Null => return None,
Json::Bool(b) => Tag::Byte(i8::from(*b)),
Json::Str(s) => Tag::String(JavaString(s.clone())),
Json::Num(n) => number_to_nbt(n),
Json::Arr(items) => Tag::List(items.iter().filter_map(to_nbt).collect()),
Json::Obj(entries) => {
let mut c = Compound::new();
for (k, v) in entries {
if let Some(v) = to_nbt(v) {
c.insert(JavaString(k.clone()), v);
}
}
Tag::Compound(c)
}
})
}
/// `JsonOps.convertTo` for a number: the smallest integer tag that holds an
/// exact integral value, else float when exact, else double.
fn number_to_nbt(literal: &str) -> Tag {
if let Some(l) = exact_long(literal) {
if let Ok(b) = i8::try_from(l) {
return Tag::Byte(b);
}
if let Ok(s) = i16::try_from(l) {
return Tag::Short(s);
}
if let Ok(i) = i32::try_from(l) {
return Tag::Int(i);
}
return Tag::Long(l);
}
let d: f64 = literal.parse().unwrap_or(0.0);
let f = d as f32;
if f64::from(f) == d {
Tag::Float(f.to_bits())
} else {
Tag::Double(d.to_bits())
}
}
/// `BigDecimal.longValueExact` of a JSON number literal.
fn exact_long(literal: &str) -> Option<i64> {
let (mantissa, exponent) = match literal.find(['e', 'E']) {
Some(i) => (&literal[..i], literal[i + 1..].parse::<i64>().ok()?),
None => (literal, 0),
};
let negative = mantissa.starts_with('-');
let mantissa = mantissa.trim_start_matches('-');
let (int_part, frac) = match mantissa.find('.') {
Some(i) => (&mantissa[..i], &mantissa[i + 1..]),
None => (mantissa, ""),
};
let mut digits: String = format!("{int_part}{frac}");
let mut scale = frac.len() as i64 - exponent;
// Trailing zeros of the unscaled value do not change the value.
while scale > 0 && digits.ends_with('0') {
digits.pop();
scale -= 1;
}
if scale > 0 {
return None;
}
let digits = digits.trim_start_matches('0');
if digits.is_empty() {
return Some(0);
}
if digits.len() as i64 - scale > 19 {
return None;
}
let mut value: i128 = digits.parse().ok()?;
for _ in 0..-scale {
value = value.checked_mul(10)?;
}
let value = if negative { -value } else { value };
i64::try_from(value).ok()
}
/// `NbtOps.convertTo(JsonOps)`.
pub fn from_nbt(tag: &Tag) -> Json {
match tag {
Tag::End => Json::Null,
Tag::Byte(v) => Json::Num(v.to_string()),
Tag::Short(v) => Json::Num(v.to_string()),
Tag::Int(v) => Json::Num(v.to_string()),
Tag::Long(v) => Json::Num(v.to_string()),
Tag::Float(v) => Json::Num(java_float_string(f32::from_bits(*v))),
Tag::Double(v) => Json::Num(java_double_string(f64::from_bits(*v))),
Tag::ByteArray(v) => Json::Arr(
v.iter()
.map(|b| Json::Num((*b as i8).to_string()))
.collect(),
),
Tag::IntArray(v) => Json::Arr(v.iter().map(|i| Json::Num(i.to_string())).collect()),
Tag::LongArray(v) => Json::Arr(v.iter().map(|l| Json::Num(l.to_string())).collect()),
Tag::String(s) => Json::Str(s.0.clone()),
Tag::List(items) => Json::Arr(items.iter().map(from_nbt).collect()),
Tag::Compound(c) => Json::Obj(c.iter().map(|(k, v)| (k.0.clone(), from_nbt(v))).collect()),
}
}
/// Java `Double.toString`.
pub fn java_double_string(d: f64) -> String {
java_fp_string(d, format!("{d:e}"))
}
/// Java `Float.toString`.
pub fn java_float_string(f: f32) -> String {
java_fp_string(f64::from(f), format!("{f:e}"))
}
fn java_fp_string(d: f64, sci: String) -> String {
if d.is_nan() {
return "NaN".into();
}
if d.is_infinite() {
return if d > 0.0 { "Infinity" } else { "-Infinity" }.into();
}
if d == 0.0 {
return if d.is_sign_negative() { "-0.0" } else { "0.0" }.into();
}
// `sci` is Rust's shortest round-trip form: d.ddde<exp>.
let (mantissa, exp) = sci.split_once('e').unwrap_or((&sci, "0"));
let exp: i32 = exp.parse().unwrap_or(0);
let negative = mantissa.starts_with('-');
let digits: String = mantissa.chars().filter(char::is_ascii_digit).collect();
let sign = if negative { "-" } else { "" };
let a = d.abs();
if (1e-3..1e7).contains(&a) {
if exp >= 0 {
let e = exp as usize;
let (int, frac) = if digits.len() > e + 1 {
(digits[..=e].to_string(), digits[e + 1..].to_string())
} else {
(format!("{digits:0<width$}", width = e + 1), String::new())
};
let frac = if frac.is_empty() { "0".into() } else { frac };
format!("{sign}{int}.{frac}")
} else {
let zeros = "0".repeat((-exp - 1) as usize);
format!("{sign}0.{zeros}{digits}")
}
} else {
let (first, rest) = digits.split_at(1);
let rest = if rest.is_empty() { "0" } else { rest };
format!("{sign}{first}.{rest}E{exp}")
}
}
/// A string value as a JSON text component (`{"text": s}`), stable string.
pub fn text_component_json(text: &[u16]) -> Vec<u16> {
to_stable_string(&Json::Obj(vec![(
"text".encode_utf16().collect(),
Json::Str(text.to_vec()),
)]))
}
pub fn translatable_component_json(key: &str) -> Vec<u16> {
to_stable_string(&Json::object(vec![("translate", Json::string(key))]))
}
#[cfg(test)]
mod tests {
use super::*;
fn w(s: &str) -> Vec<u16> {
s.encode_utf16().collect()
}
#[test]
fn numbers_become_the_smallest_tag() {
let tag = |s: &str| to_nbt(&parse_strict(&w(s)).unwrap()).unwrap();
assert_eq!(tag("1"), Tag::Byte(1));
assert_eq!(tag("1.0"), Tag::Byte(1));
assert_eq!(tag("1e2"), Tag::Byte(100));
assert_eq!(tag("300"), Tag::Short(300));
assert_eq!(tag("1.5"), Tag::Float(1.5f32.to_bits()));
assert_eq!(tag("0.1"), Tag::Double(0.1f64.to_bits()));
assert_eq!(tag("3000000000"), Tag::Long(3_000_000_000));
}
#[test]
fn lenient_reads_what_strict_refuses() {
assert!(parse_strict(&w("hello")).is_none());
assert_eq!(parse_lenient(&w("hello")), Some(Json::string("hello")));
assert!(parse_lenient(&w("hello world")).is_none());
assert_eq!(
parse_lenient(&w("{a:'b'}")),
Some(Json::object(vec![("a", Json::string("b"))]))
);
}
#[test]
fn stable_strings_sort_keys() {
let j = parse_strict(&w(r#"{"text":"a","color":"red"}"#)).unwrap();
assert_eq!(
String::from_utf16(&to_stable_string(&j)).unwrap(),
r#"{"color":"red","text":"a"}"#
);
}
#[test]
fn java_floating_point_strings() {
assert_eq!(java_double_string(1.0), "1.0");
assert_eq!(java_double_string(0.001), "0.001");
assert_eq!(java_double_string(1e7), "1.0E7");
assert_eq!(java_double_string(123456.789), "123456.789");
assert_eq!(java_double_string(1e-4), "1.0E-4");
assert_eq!(java_float_string(0.1), "0.1");
assert_eq!(java_float_string(1.5e10), "1.5E10");
}
}
+299
View File
@@ -0,0 +1,299 @@
//! Vanilla's DataFixers from the 1.19 schemas on (`net.minecraft.util.datafix`):
//! the data of worlds saved by 1.18.2 to 26.2 brought to the 26.3 format exactly
//! as 26.3 does when it reads them, and the file fixes it runs when it opens
//! such a world (`net.minecraft.util.filefix`).
//!
//! DataFixerUpper reads the data once with the schema of its version (running
//! the schema's read hooks), applies every fix with a key above that version
//! in order, and writes it with the current schema. Each fix finds its values
//! 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.
pub mod dynamic;
pub mod files;
mod fixes_a;
mod fixes_b;
mod fixes_c;
mod fixes_old;
mod items;
pub mod json;
pub mod schema;
mod text;
use crate::nbt::Tag;
use dynamic::{as_str, string};
use schema::{Key, Ty, key, walk};
/// The DataVersion of the game.
pub const CURRENT: i32 = crate::DATA_VERSION;
/// Data saved before this DataVersion (1.18.2 is 2975) needs fixes that are not
/// ported (2967 and 2970, and everything before).
pub const OLDEST_SUPPORTED: i32 = 2970;
/// `Identifier.tryParse` validity, as `NamespacedSchema.ensureNamespaced` uses it.
pub fn identifier(id: &str) -> Option<(String, String)> {
let (ns, path) = match id.split_once(':') {
Some((ns, path)) => (if ns.is_empty() { "minecraft" } else { ns }, path),
None => ("minecraft", id),
};
let ns_ok = ns
.bytes()
.all(|c| matches!(c, b'a'..=b'z' | b'0'..=b'9' | b'_' | b'-' | b'.'));
let path_ok = path
.bytes()
.all(|c| matches!(c, b'a'..=b'z' | b'0'..=b'9' | b'_' | b'-' | b'.' | b'/'));
(ns_ok && path_ok).then(|| (ns.to_owned(), path.to_owned()))
}
/// What a fix works on: the root value, its type and the fix's key.
pub struct Cx<'a> {
root: Ty,
data: &'a mut Tag,
k: Key,
}
impl Cx<'_> {
/// Every value of `ty`, read with the fix's input schema.
pub fn each(&mut self, ty: Ty, mut f: impl FnMut(&mut Tag)) {
walk(self.root, self.data, self.k - 1, ty, &mut f);
}
/// Every value of `ty` read with the fix's output schema (fixes declared
/// on `getOutputSchema().getType(...)`).
pub fn each_out(&mut self, ty: Ty, mut f: impl FnMut(&mut Tag)) {
walk(self.root, self.data, self.k, ty, &mut f);
}
/// Every entity of one type (`NamedEntityFix` and friends).
pub fn entity(&mut self, id: &str, mut f: impl FnMut(&mut Tag)) {
self.each(Ty::Entity, |e| {
if id_is(e, id) {
f(e);
}
});
}
/// Every block entity of one type.
pub fn block_entity(&mut self, id: &str, mut f: impl FnMut(&mut Tag)) {
self.each(Ty::BlockEntity, |e| {
if id_is(e, id) {
f(e);
}
});
}
/// Every item stack whose id passes `filter`.
pub fn item(&mut self, filter: impl Fn(&str) -> bool, mut f: impl FnMut(&mut Tag)) {
self.each(Ty::ItemStack, |s| {
if let Some(id) = as_str(s.get("id"))
&& filter(&dynamic::namespaced(&id))
{
f(s);
}
});
}
/// A name type renamed in place (`fixTypeEverywhere` over a namespaced string).
pub fn rename(&mut self, ty: Ty, rename: impl Fn(&str) -> Option<&'static str>) {
self.each(ty, |v| {
if let Some(name) = as_str(Some(v))
&& let Some(new) = rename(&name)
{
*v = string(new);
}
});
}
}
/// 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.
pub(crate) const LAST_READ: &str = "\u{0}last_read";
/// Remember a record'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) {
reread_as(Ty::Entity, k, e);
}
/// [`reread`] for a value of another type (a block entity).
pub fn reread_as(ty: Ty, k: Key, v: &mut Tag) {
walk(ty, v, k, Ty::BlockEntity, &mut mark_read);
walk(ty, v, 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)
}
/// `createRenamer`: the id namespaced, looked up in a table.
pub fn renamer<'a>(
table: &'a [(&'a str, &'static str)],
) -> impl Fn(&str) -> Option<&'static str> + 'a {
move |id: &str| {
let id = dynamic::namespaced(id);
table
.iter()
.find(|(from, _)| *from == id)
.map(|(_, to)| *to)
}
}
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_old::register(&mut out);
fixes_a::register(&mut out);
fixes_b::register(&mut out);
fixes_c::register(&mut out);
out
}
/// `DataFixer.update(type, data, from, to)` for a root value.
pub fn update(root: Ty, data: &mut Tag, from: i32, to: i32) {
if from >= to {
return;
}
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::BlockEntity, &mut mark_read);
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() {
if k > from_key && k / 10 <= to_version {
fix(&mut Cx { root, data, k });
}
}
// ...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.
fn read_hooks(root: Ty, data: &mut Tag, k: Key) {
walk(root, data, k, Ty::ItemStack, &mut |stack| {
if k < key(3818, 5) {
items::add_names(stack);
} else if let Tag::String(_) = stack {
// V3818_5 UNPACK_PLAIN_ID: a bare item id is a stack of one.
*stack = Tag::compound([("id", stack.clone()), ("count", Tag::Int(1))]);
}
});
walk(root, data, k, Ty::Objective, &mut fixes_a::unpack_objective);
}
fn write_hooks(root: Ty, data: &mut Tag, k: Key) {
walk(root, data, k, Ty::Objective, &mut fixes_a::repack_objective);
}
/// `SimpleRegionStorage.upgradeChunkTag` for a region chunk (`ChunkMap`):
/// the dimension and generator are passed to the fixes as `__context`.
pub fn update_chunk(tag: &mut Tag, dimension: &str, generator: Option<&str>) {
let version = dynamic::as_int(tag.get("DataVersion"), -1);
if version >= CURRENT {
return;
}
let mut context = Tag::compound([("dimension", string(dimension))]);
if let Some(g) = generator {
dynamic::set(&mut context, "generator", string(g));
}
dynamic::set(tag, "__context", context);
update(Ty::Chunk, tag, version, CURRENT);
dynamic::remove(tag, "__context");
dynamic::set(tag, "DataVersion", Tag::Int(CURRENT));
}
/// `SimpleRegionStorage.upgradeChunkTag` for entity chunks (default version -1)
/// and POI chunks (default 1945).
pub fn update_region_tag(root: Ty, tag: &mut Tag) {
let default = if root == Ty::PoiChunk { 1945 } else { -1 };
let version = dynamic::as_int(tag.get("DataVersion"), default);
if version >= CURRENT {
return;
}
update(root, tag, version, CURRENT);
dynamic::set(tag, "DataVersion", Tag::Int(CURRENT));
}
/// `PlayerDataStorage.load`: the DataVersion field stays as loaded.
pub fn update_player(tag: &mut Tag) {
let version = dynamic::as_int(tag.get("DataVersion"), -1);
update(Ty::Player, tag, version, CURRENT);
}
/// The saved data type of a file below a `data` folder (`namespace/path.dat`).
pub fn saved_data_type(relative: &str) -> Option<Ty> {
let name = relative.strip_suffix(".dat").unwrap_or(relative);
if name.ends_with("/command_storage") {
return Some(Ty::SavedCommandStorage);
}
if let Some(n) = name.strip_prefix("minecraft/maps/")
&& !n.is_empty()
&& n.bytes().all(|b| b.is_ascii_digit())
{
return Some(Ty::SavedMapData);
}
Some(match name {
"minecraft/custom_boss_events" => Ty::SavedCustomBossEvents,
"minecraft/scoreboard" => Ty::SavedScoreboard,
"minecraft/chunk_tickets" => Ty::SavedTickets,
"minecraft/world_border" => Ty::SavedWorldBorder,
"minecraft/game_rules" => Ty::SavedGameRules,
"minecraft/ender_dragon_fight" => Ty::SavedEnderDragonFight,
"minecraft/wandering_trader" => Ty::SavedWanderingTrader,
"minecraft/maps/last_id" => Ty::SavedMapIndex,
"minecraft/weather" => Ty::SavedWeather,
"minecraft/scheduled_events" => Ty::SavedScheduledEvents,
"minecraft/world_gen_settings" => Ty::SavedWorldGenSettings,
"minecraft/random_sequences" => Ty::SavedRandomSequences,
"minecraft/stopwatches" => Ty::SavedStopwatches,
"minecraft/raids" => Ty::SavedRaids,
"minecraft/world_clocks" => Ty::SavedWorldClocks,
_ => return None,
})
}
/// `SavedDataStorage.readTagFromDisk`: default version 1343, field kept.
pub fn update_saved_data(ty: Ty, tag: &mut Tag) {
let version = dynamic::as_int(tag.get("DataVersion"), 1343);
update(ty, tag, version, CURRENT);
}
#[cfg(test)]
mod tests;
+860
View File
@@ -0,0 +1,860 @@
//! Where DataFixerUpper finds each type inside the data, schema by schema
//! (`net.minecraft.util.datafix.schemas`), from the 1.20 schemas on.
//!
//! `walk` visits every value of the target type under a root of a given type,
//! children before parents (DataFixerUpper rewrites recursive types bottom-up).
//! Keys are `version * 10 + sub-version` as in `DataFixUtils.makeKey`, and a
//! fix with key K reads its input with the schema just below K.
use super::dynamic::{as_str, namespaced};
use crate::nbt::Tag;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Ty {
Level,
/// `LIGHTWEIGHT_LEVEL`: the level data apart from its typed fields (the same value).
LightweightLevel,
Player,
Chunk,
EntityChunk,
PoiChunk,
BlockEntity,
EntityTree,
Entity,
ItemStack,
DataComponents,
TextComponent,
VillagerTrade,
Particle,
BlockState,
FlatBlockState,
BlockName,
ItemName,
EntityName,
Biome,
Recipe,
StructureFeature,
GameEventName,
Advancements,
Stats,
Hotbar,
Objective,
Team,
WorldGenSettings,
SavedCustomBossEvents,
SavedMapData,
SavedScoreboard,
SavedWorldGenSettings,
SavedStructureFeatureIndices,
/// Saved data that has no typed fields (`DSL.remainder`).
SavedRemainder,
/// Each saved data kind still needs its own identity for fixes that target it.
SavedEnderDragonFight,
SavedGameRules,
SavedTickets,
SavedRaids,
SavedRandomSequences,
SavedWorldBorder,
SavedWeather,
SavedWorldClocks,
SavedWanderingTrader,
SavedScheduledEvents,
SavedMapIndex,
SavedCommandStorage,
SavedStopwatches,
Options,
DebugProfile,
}
/// A schema key: `version * 10 + sub`.
pub type Key = u32;
pub const fn key(version: u32, sub: u32) -> Key {
version * 10 + sub
}
pub struct Walker<'f> {
/// The schema the data is read with.
pub k: Key,
pub target: Ty,
pub f: &'f mut dyn FnMut(&mut Tag),
}
impl Walker<'_> {
pub fn walk(&mut self, ty: Ty, v: &mut Tag) {
self.children(ty, v);
if ty == self.target {
(self.f)(v);
}
}
fn field(&mut self, ty: Ty, v: &mut Tag, name: &str) {
if let Some(child) = v.get_mut(name) {
self.walk(ty, child);
}
}
fn list(&mut self, ty: Ty, v: &mut Tag, name: &str) {
if let Some(Tag::List(items)) = v.get_mut(name) {
for item in items {
self.walk(ty, item);
}
}
}
fn list_in(&mut self, ty: Ty, items: &mut Tag) {
if let Tag::List(items) = items {
for item in items {
self.walk(ty, item);
}
}
}
/// `DSL.compoundList(type)`: every value of a map.
fn values(&mut self, ty: Ty, v: &mut Tag, name: &str) {
if let Some(Tag::Compound(c)) = v.get_mut(name) {
for value in c.values_mut() {
self.walk(ty, value);
}
}
}
/// A list of compounds, each with a typed field.
fn list_of_field(&mut self, ty: Ty, v: &mut Tag, list: &str, field: &str) {
if let Some(Tag::List(items)) = v.get_mut(list) {
for item in items {
self.field(ty, item, field);
}
}
}
/// `DSL.compoundList(keyType, valueType)` with typed string keys: keys are
/// renamed in place by the callback (as a string tag).
fn keys(&mut self, ty: Ty, v: &mut Tag, name: &str) {
if ty != self.target {
return;
}
if let Some(Tag::Compound(c)) = v.get_mut(name) {
let entries: Vec<_> = std::mem::take(c).into_iter().collect();
for (k, value) in entries {
let mut key_tag = Tag::String(k.clone());
(self.f)(&mut key_tag);
let k = match key_tag {
Tag::String(s) => s,
_ => k,
};
c.insert(k, value);
}
}
}
fn children(&mut self, ty: Ty, v: &mut Tag) {
let k = self.k;
match ty {
Ty::Level => {
if k < key(4771, 0) {
if let Some(Tag::Compound(c)) = v.get_mut("CustomBossEvents") {
for e in c.values_mut() {
self.field(Ty::TextComponent, e, "Name");
}
}
}
self.walk(Ty::LightweightLevel, v);
}
Ty::Player => {
if k >= key(4312, 0) {
self.equipment(v);
}
if let Some(rv) = v.get_mut("RootVehicle") {
self.field(Ty::EntityTree, rv, "Entity");
}
self.list(Ty::EntityTree, v, "ender_pearls");
self.list(Ty::ItemStack, v, "Inventory");
self.list(Ty::ItemStack, v, "EnderItems");
self.field(Ty::EntityTree, v, "ShoulderEntityLeft");
self.field(Ty::EntityTree, v, "ShoulderEntityRight");
if let Some(book) = v.get_mut("recipeBook") {
self.list(Ty::Recipe, book, "recipes");
self.list(Ty::Recipe, book, "toBeDisplayed");
}
}
Ty::Chunk => {
self.list(Ty::EntityTree, v, "entities");
if let Some(Tag::List(items)) = v.get_mut("block_entities") {
for be in items {
// DSL.or(BLOCK_ENTITY, remainder): unknown ids stay as they are.
if block_entity_known(be, k) {
self.walk(Ty::BlockEntity, be);
}
}
}
self.list_of_field(Ty::BlockName, v, "block_ticks", "i");
if let Some(Tag::List(sections)) = v.get_mut("sections") {
for s in sections {
if let Some(b) = s.get_mut("biomes") {
self.list(Ty::Biome, b, "palette");
}
if let Some(b) = s.get_mut("block_states") {
self.list(Ty::BlockState, b, "palette");
}
}
}
if let Some(s) = v.get_mut("structures") {
self.values(Ty::StructureFeature, s, "starts");
}
}
Ty::EntityChunk => self.list(Ty::EntityTree, v, "Entities"),
Ty::EntityTree => {
self.list(Ty::EntityTree, v, "Passengers");
self.walk(Ty::Entity, v);
}
Ty::Entity => self.entity(v),
Ty::BlockEntity => self.block_entity(v),
Ty::ItemStack => self.item_stack(v),
Ty::DataComponents => self.components(v),
Ty::TextComponent => self.text(v),
Ty::VillagerTrade => {
self.field(Ty::ItemStack, v, "buy");
self.field(Ty::ItemStack, v, "buyB");
self.field(Ty::ItemStack, v, "sell");
}
Ty::Particle => {
if k >= key(3818, 4) {
self.field(Ty::ItemStack, v, "item");
self.field(Ty::BlockState, v, "block_state");
}
}
Ty::Hotbar => {
if let Tag::Compound(c) = v {
for row in c.values_mut() {
self.list_in(Ty::ItemStack, row);
}
}
}
Ty::Stats => {
if let Some(stats) = v.get_mut("stats") {
self.keys(Ty::BlockName, stats, "minecraft:mined");
for s in ["crafted", "used", "broken", "picked_up", "dropped"] {
self.keys(Ty::ItemName, stats, &format!("minecraft:{s}"));
}
self.keys(Ty::EntityName, stats, "minecraft:killed");
self.keys(Ty::EntityName, stats, "minecraft:killed_by");
}
}
Ty::Advancements => {
for (adv, kind) in [
("minecraft:adventure/adventuring_time", Ty::Biome),
("minecraft:adventure/kill_a_mob", Ty::EntityName),
("minecraft:adventure/kill_all_mobs", Ty::EntityName),
("minecraft:husbandry/bred_all_animals", Ty::EntityName),
] {
if let Some(a) = v.get_mut(adv) {
self.keys(kind, a, "criteria");
}
}
}
Ty::Objective => {
// The hook unpacked CriteriaName into CriteriaType {type, id}.
if let Some(ct) = v.get_mut("CriteriaType") {
let kind = match as_str(ct.get("type")).as_deref() {
Some("minecraft:mined") => Some(Ty::BlockName),
Some(
"minecraft:crafted"
| "minecraft:used"
| "minecraft:broken"
| "minecraft:picked_up"
| "minecraft:dropped",
) => Some(Ty::ItemName),
Some("minecraft:killed" | "minecraft:killed_by") => Some(Ty::EntityName),
_ => None,
};
if let Some(kind) = kind {
self.field(kind, ct, "id");
}
}
self.field(Ty::TextComponent, v, "DisplayName");
}
Ty::Team => {
for f in ["MemberNamePrefix", "MemberNameSuffix", "DisplayName"] {
self.field(Ty::TextComponent, v, f);
}
}
Ty::SavedCustomBossEvents => {
if let Some(Tag::Compound(c)) = v.get_mut("data") {
for e in c.values_mut() {
self.field(Ty::TextComponent, e, "Name");
}
}
}
Ty::SavedMapData => {
if let Some(d) = v.get_mut("data") {
let name = if k >= key(3813, 0) { "name" } else { "Name" };
self.list_of_field(Ty::TextComponent, d, "banners", name);
}
}
Ty::SavedScoreboard => {
if let Some(d) = v.get_mut("data") {
self.list(Ty::Objective, d, "Objectives");
self.list(Ty::Team, d, "Teams");
self.list_of_field(Ty::TextComponent, d, "PlayerScores", "display");
}
}
Ty::SavedWorldGenSettings => self.field(Ty::WorldGenSettings, v, "data"),
Ty::SavedStructureFeatureIndices => {
if let Some(d) = v.get_mut("data") {
self.values(Ty::StructureFeature, d, "Features");
}
}
Ty::WorldGenSettings => self.world_gen(v),
_ => {}
}
}
fn equipment(&mut self, v: &mut Tag) {
if self.k >= key(4301, 0) {
if let Some(eq) = v.get_mut("equipment") {
for slot in [
"mainhand", "offhand", "feet", "legs", "chest", "head", "body", "saddle",
] {
self.field(Ty::ItemStack, eq, slot);
}
}
} else {
self.list(Ty::ItemStack, v, "ArmorItems");
self.list(Ty::ItemStack, v, "HandItems");
self.field(Ty::ItemStack, v, "body_armor_item");
self.field(Ty::ItemStack, v, "saddle");
}
}
fn entity(&mut self, v: &mut Tag) {
let k = self.k;
self.equipment(v);
self.field(Ty::TextComponent, v, "CustomName");
let id = namespaced(&as_str(v.get("id")).unwrap_or_default());
let id = id.as_str();
let listener = |w: &mut Self, v: &mut Tag| {
if let Some(l) = v.get_mut("listener")
&& let Some(e) = l.get_mut("event")
{
w.field(Ty::GameEventName, e, "game_event");
}
};
match id {
"minecraft:area_effect_cloud" => {
let f = if k >= key(4420, 0) {
"custom_particle"
} else {
"Particle"
};
self.field(Ty::Particle, v, f);
}
"minecraft:arrow" | "minecraft:spectral_arrow" => {
self.field(Ty::BlockState, v, "inBlockState");
if k >= key(3685, 0) {
self.field(Ty::ItemStack, v, "item");
}
if k >= key(3938, 0) {
self.field(Ty::ItemStack, v, "weapon");
}
}
"minecraft:trident" => {
self.field(Ty::BlockState, v, "inBlockState");
let f = if k >= key(3685, 0) { "item" } else { "Trident" };
self.field(Ty::ItemStack, v, f);
}
"minecraft:chest_minecart" | "minecraft:hopper_minecart" => {
self.field(Ty::BlockState, v, "DisplayState");
self.list(Ty::ItemStack, v, "Items");
}
"minecraft:minecart" | "minecraft:furnace_minecart" | "minecraft:tnt_minecart" => {
self.field(Ty::BlockState, v, "DisplayState");
}
"minecraft:command_block_minecart" => {
self.field(Ty::BlockState, v, "DisplayState");
self.field(Ty::TextComponent, v, "LastOutput");
}
"minecraft:spawner_minecart" => {
self.field(Ty::BlockState, v, "DisplayState");
self.spawner(v);
}
"minecraft:donkey" | "minecraft:mule" => {
self.list(Ty::ItemStack, v, "Items");
if k < key(4300, 0) {
self.field(Ty::ItemStack, v, "SaddleItem");
}
}
"minecraft:llama" | "minecraft:trader_llama" => {
self.list(Ty::ItemStack, v, "Items");
if k < key(4300, 0) {
self.field(Ty::ItemStack, v, "SaddleItem");
}
let split = if id == "minecraft:llama" {
key(3808, 1)
} else {
key(3808, 2)
};
if k < split {
self.field(Ty::ItemStack, v, "DecorItem");
}
}
"minecraft:horse" => {
if k < key(4300, 0) {
self.field(Ty::ItemStack, v, "SaddleItem");
}
if k < key(3808, 0) {
self.field(Ty::ItemStack, v, "ArmorItem");
}
}
"minecraft:skeleton_horse" | "minecraft:zombie_horse" => {
if k < key(4300, 0) {
self.field(Ty::ItemStack, v, "SaddleItem");
}
}
"minecraft:enderman" => self.field(Ty::BlockState, v, "carriedBlockState"),
"minecraft:falling_block" => {
self.field(Ty::BlockState, v, "BlockState");
self.field(Ty::BlockEntity, v, "TileEntityData");
}
"minecraft:firework_rocket" => self.field(Ty::ItemStack, v, "FireworksItem"),
"minecraft:item" | "minecraft:item_frame" | "minecraft:glow_item_frame" => {
self.field(Ty::ItemStack, v, "Item");
}
"minecraft:potion" if k < key(4306, 0) => self.field(Ty::ItemStack, v, "Item"),
"minecraft:splash_potion" | "minecraft:lingering_potion" if k >= key(4306, 0) => {
self.field(Ty::ItemStack, v, "Item");
}
"minecraft:villager" | "minecraft:wandering_trader" => {
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");
}
}
"minecraft:pillager" | "minecraft:piglin" => self.list(Ty::ItemStack, v, "Inventory"),
"minecraft:allay" => {
self.list(Ty::ItemStack, v, "Inventory");
listener(self, v);
}
"minecraft:warden" => listener(self, v),
"minecraft:tnt" if k >= key(3683, 0) => self.field(Ty::BlockState, v, "block_state"),
"minecraft:chest_boat" if k < key(4067, 0) => self.list(Ty::ItemStack, v, "Items"),
"minecraft:item_display" => self.field(Ty::ItemStack, v, "item"),
"minecraft:block_display" => self.field(Ty::BlockState, v, "block_state"),
"minecraft:text_display" => self.field(Ty::TextComponent, v, "text"),
"minecraft:ominous_item_spawner" if k >= key(3825, 0) => {
self.field(Ty::ItemStack, v, "item")
}
_ if k >= key(4067, 0)
&& (id.ends_with("_chest_boat") || id == "minecraft:bamboo_chest_raft") =>
{
self.list(Ty::ItemStack, v, "Items");
}
_ => {}
}
}
/// `UNTAGGED_SPAWNER` (V2831).
fn spawner(&mut self, v: &mut Tag) {
if let Some(Tag::List(items)) = v.get_mut("SpawnPotentials") {
for p in items {
if let Some(d) = p.get_mut("data") {
self.field(Ty::EntityTree, d, "entity");
}
}
}
if let Some(d) = v.get_mut("SpawnData") {
self.field(Ty::EntityTree, d, "entity");
}
}
fn block_entity(&mut self, v: &mut Tag) {
let k = self.k;
self.field(Ty::DataComponents, v, "components");
let id = namespaced(&as_str(v.get("id")).unwrap_or_default());
let named_inventory = |w: &mut Self, v: &mut Tag| {
w.list(Ty::ItemStack, v, "Items");
w.field(Ty::TextComponent, v, "CustomName");
};
match id.as_str() {
"minecraft:furnace" | "minecraft:smoker" | "minecraft:blast_furnace" => {
named_inventory(self, v);
self.keys(Ty::Recipe, v, "RecipesUsed");
}
"minecraft:chest"
| "minecraft:trapped_chest"
| "minecraft:dispenser"
| "minecraft:dropper"
| "minecraft:hopper"
| "minecraft:shulker_box"
| "minecraft:brewing_stand"
| "minecraft:barrel" => {
named_inventory(self, v);
}
"minecraft:crafter" if k >= key(3682, 0) => named_inventory(self, v),
"minecraft:jukebox" => self.field(Ty::ItemStack, v, "RecordItem"),
"minecraft:sign" | "minecraft:hanging_sign" => {
for side in ["front_text", "back_text"] {
if let Some(s) = v.get_mut(side) {
self.list(Ty::TextComponent, s, "messages");
self.list(Ty::TextComponent, s, "filtered_messages");
}
}
}
"minecraft:mob_spawner" => self.spawner(v),
"minecraft:piston" => self.field(Ty::BlockState, v, "blockState"),
"minecraft:enchanting_table" | "minecraft:beacon" | "minecraft:banner" => {
self.field(Ty::TextComponent, v, "CustomName");
}
"minecraft:skull" => self.field(Ty::TextComponent, v, "custom_name"),
"minecraft:command_block" => {
self.field(Ty::TextComponent, v, "CustomName");
self.field(Ty::TextComponent, v, "LastOutput");
}
"minecraft:jigsaw" => self.field(Ty::FlatBlockState, v, "final_state"),
"minecraft:campfire" | "minecraft:chiseled_bookshelf" => {
self.list(Ty::ItemStack, v, "Items")
}
"minecraft:shelf" if k >= key(4533, 0) => self.list(Ty::ItemStack, v, "Items"),
"minecraft:lectern" => self.field(Ty::ItemStack, v, "Book"),
"minecraft:beehive" => {
if k >= key(3818, 0) {
self.list_of_field(Ty::EntityTree, v, "bees", "entity_data");
} else {
self.list_of_field(Ty::EntityTree, v, "Bees", "EntityData");
}
}
"minecraft:sculk_sensor" | "minecraft:sculk_shrieker" => {
if let Some(l) = v.get_mut("listener")
&& let Some(e) = l.get_mut("event")
{
self.field(Ty::GameEventName, e, "game_event");
}
}
"minecraft:decorated_pot" => {
if k >= key(4996, 1) {
if let Some(s) = v.get_mut("sherds") {
for side in ["back", "left", "right", "front"] {
self.field(Ty::ItemStack, s, side);
}
}
} else if k >= key(3448, 0) {
self.list(Ty::ItemName, v, "sherds");
} else {
self.list(Ty::ItemName, v, "shards");
}
self.field(Ty::ItemStack, v, "item");
}
"minecraft:brushable_block" | "minecraft:suspicious_sand" => {
self.field(Ty::ItemStack, v, "item")
}
"minecraft:trial_spawner" if k >= key(3689, 0) => {
if let Some(Tag::List(items)) = v.get_mut("spawn_potentials") {
for p in items {
if let Some(d) = p.get_mut("data") {
self.field(Ty::EntityTree, d, "entity");
}
}
}
if let Some(d) = v.get_mut("spawn_data") {
self.field(Ty::EntityTree, d, "entity");
}
}
"minecraft:vault" if k >= key(3807, 0) => {
if let Some(c) = v.get_mut("config") {
self.field(Ty::ItemStack, c, "key_item");
}
if let Some(c) = v.get_mut("server_data") {
self.list(Ty::ItemStack, c, "items_to_eject");
}
if let Some(c) = v.get_mut("shared_data") {
self.field(Ty::ItemStack, c, "display_item");
}
}
"minecraft:test_instance_block" if k >= key(4302, 0) => {
if let Some(d) = v.get_mut("data") {
self.field(Ty::TextComponent, d, "error_message");
}
self.list_of_field(Ty::TextComponent, v, "errors", "text");
}
_ => {}
}
}
fn item_stack(&mut self, v: &mut Tag) {
if self.k >= key(3818, 5) {
self.field(Ty::ItemName, v, "id");
self.field(Ty::DataComponents, v, "components");
return;
}
self.field(Ty::ItemName, v, "id");
let Some(tag) = v.get_mut("tag") else {
return;
};
self.field(Ty::EntityTree, tag, "EntityTag");
self.field(Ty::BlockEntity, tag, "BlockEntityTag");
self.list(Ty::BlockName, tag, "CanDestroy");
self.list(Ty::BlockName, tag, "CanPlaceOn");
self.list(Ty::ItemStack, tag, "Items");
self.list(Ty::ItemStack, tag, "ChargedProjectiles");
self.list(Ty::TextComponent, tag, "pages");
self.values(Ty::TextComponent, tag, "filtered_pages");
if let Some(d) = tag.get_mut("display") {
self.field(Ty::TextComponent, d, "Name");
self.list(Ty::TextComponent, d, "Lore");
}
}
fn components(&mut self, v: &mut Tag) {
let k = self.k;
if k < key(3818, 3) {
return;
}
let Tag::Compound(_) = v else {
return;
};
self.list_of_field(Ty::EntityTree, v, "minecraft:bees", "entity_data");
self.field(Ty::BlockEntity, v, "minecraft:block_entity_data");
self.list(Ty::ItemStack, v, "minecraft:bundle_contents");
for c in ["minecraft:can_break", "minecraft:can_place_on"] {
let Some(p) = v.get_mut(c) else {
continue;
};
if k >= key(4307, 0) {
// A predicate or a list of them.
if let Tag::List(items) = p {
for item in items {
self.blocks_predicate(item);
}
} else {
self.blocks_predicate(p);
}
} else if let Some(Tag::List(items)) = p.get_mut("predicates") {
for item in items {
self.blocks_predicate(item);
}
}
}
self.list(Ty::ItemStack, v, "minecraft:charged_projectiles");
self.list_of_field(Ty::ItemStack, v, "minecraft:container", "item");
self.field(Ty::EntityTree, v, "minecraft:entity_data");
if k >= key(4996, 0) {
if let Some(p) = v.get_mut("minecraft:pot_decorations") {
for side in ["back", "left", "right", "front"] {
self.field(Ty::ItemStack, p, side);
}
}
} else {
self.list(Ty::ItemName, v, "minecraft:pot_decorations");
}
if k < key(4059, 0) {
if let Some(f) = v.get_mut("minecraft:food") {
self.field(Ty::ItemStack, f, "using_converts_to");
}
} else {
self.field(Ty::ItemStack, v, "minecraft:use_remainder");
if let Some(e) = v.get_mut("minecraft:equippable")
&& let Some(a) = e.get_mut("allowed_entities")
{
if let Tag::List(_) = a {
self.list_in(Ty::EntityName, a);
} else {
self.walk(Ty::EntityName, a);
}
}
self.field(Ty::ItemStack, v, "minecraft:sulfur_cube_content");
}
self.field(Ty::TextComponent, v, "minecraft:custom_name");
self.field(Ty::TextComponent, v, "minecraft:item_name");
self.list(Ty::TextComponent, v, "minecraft:lore");
if let Some(b) = v.get_mut("minecraft:written_book_content")
&& let Some(Tag::List(pages)) = b.get_mut("pages")
{
for page in pages {
// DSL.or({raw, filtered}, TEXT_COMPONENT): any compound is the filterable form.
if let Tag::Compound(_) = page {
self.field(Ty::TextComponent, page, "raw");
self.field(Ty::TextComponent, page, "filtered");
} else {
self.walk(Ty::TextComponent, page);
}
}
}
}
fn blocks_predicate(&mut self, p: &mut Tag) {
if let Some(b) = p.get_mut("blocks") {
if let Tag::List(_) = b {
self.list_in(Ty::BlockName, b);
} else {
self.walk(Ty::BlockName, b);
}
}
}
fn text(&mut self, v: &mut Tag) {
let k = self.k;
if k < key(4290, 0) {
return;
}
match v {
Tag::List(_) => self.list_in(Ty::TextComponent, v),
Tag::Compound(_) => {
self.list(Ty::TextComponent, v, "extra");
self.field(Ty::TextComponent, v, "separator");
if k >= key(4292, 0) {
if let Some(h) = v.get_mut("hover_event") {
match as_str(h.get("action")).as_deref() {
Some("show_text") => self.field(Ty::TextComponent, h, "value"),
Some("show_item") => self.walk(Ty::ItemStack, h),
Some("show_entity") => {
self.field(Ty::EntityName, h, "id");
self.field(Ty::TextComponent, h, "name");
}
_ => {}
}
}
} else if let Some(h) = v.get_mut("hoverEvent") {
match as_str(h.get("action")).as_deref() {
Some("show_text") => self.field(Ty::TextComponent, h, "contents"),
Some("show_item") => {
if let Some(c) = h.get_mut("contents") {
// DSL.or(ITEM_STACK, ITEM_NAME)
if let Tag::Compound(_) = c {
self.walk(Ty::ItemStack, c);
} else {
self.walk(Ty::ItemName, c);
}
}
}
Some("show_entity") => {
if let Some(c) = h.get_mut("contents") {
self.field(Ty::EntityName, c, "type");
self.field(Ty::TextComponent, c, "name");
}
}
_ => {}
}
}
}
_ => {}
}
}
/// `WORLD_GEN_SETTINGS` (V2832).
fn world_gen(&mut self, v: &mut Tag) {
let Some(Tag::Compound(dims)) = v.get_mut("dimensions") else {
return;
};
for dim in dims.values_mut() {
let Some(g) = dim.get_mut("generator") else {
continue;
};
match as_str(g.get("type")).as_deref() {
Some("minecraft:flat") => {
if let Some(s) = g.get_mut("settings") {
self.field(Ty::Biome, s, "biome");
self.list_of_field(Ty::BlockName, s, "layers", "block");
}
}
Some("minecraft:noise") => {
if let Some(b) = g.get_mut("biome_source") {
match as_str(b.get("type")).as_deref() {
Some("minecraft:fixed") => self.field(Ty::Biome, b, "biome"),
Some("minecraft:checkerboard") => self.list(Ty::Biome, b, "biomes"),
_ => {}
}
}
if let Some(s) = g.get_mut("settings")
&& let Tag::Compound(_) = s
{
self.field(Ty::BlockName, s, "default_block");
self.field(Ty::BlockName, s, "default_fluid");
}
}
_ => {}
}
}
}
}
/// 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());
let since = match id.as_str() {
"minecraft:sculk_catalyst" => key(3076, 0),
"minecraft:sculk_shrieker" => key(3078, 0),
"minecraft:hanging_sign" => key(3202, 0),
"minecraft:chiseled_bookshelf" => key(3204, 0),
"minecraft:decorated_pot" => key(3327, 0),
"minecraft:suspicious_sand" => return (key(3327, 0)..key(3438, 0)).contains(&k),
"minecraft:crafter" => key(3682, 0),
"minecraft:trial_spawner" => key(3689, 0),
"minecraft:vault" => key(3807, 0),
"minecraft:creaking_heart" => key(4071, 0),
"minecraft:test_block" | "minecraft:test_instance_block" => key(4302, 0),
"minecraft:copper_golem_statue" => key(4532, 0),
"minecraft:shelf" => key(4533, 0),
"minecraft:potent_sulfur" => key(4881, 0),
"minecraft:bed" => return k < key(4885, 0),
"minecraft:brushable_block" | "minecraft:calibrated_sculk_sensor" => key(3438, 0),
_ => 0,
};
k >= since && BLOCK_ENTITIES.contains(&id.as_str())
}
/// Block entity types of the schemas from 1.20 on.
const BLOCK_ENTITIES: &[&str] = &[
"minecraft:furnace",
"minecraft:chest",
"minecraft:trapped_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",
"minecraft:bed",
"minecraft:conduit",
"minecraft:barrel",
"minecraft:smoker",
"minecraft:blast_furnace",
"minecraft:lectern",
"minecraft:bell",
"minecraft:jigsaw",
"minecraft:campfire",
"minecraft:beehive",
"minecraft:sculk_sensor",
"minecraft:sculk_catalyst",
"minecraft:sculk_shrieker",
"minecraft:hanging_sign",
"minecraft:chiseled_bookshelf",
"minecraft:decorated_pot",
"minecraft:suspicious_sand",
"minecraft:brushable_block",
"minecraft:calibrated_sculk_sensor",
"minecraft:crafter",
"minecraft:trial_spawner",
"minecraft:vault",
"minecraft:creaking_heart",
"minecraft:test_block",
"minecraft:test_instance_block",
"minecraft:copper_golem_statue",
"minecraft:shelf",
"minecraft:potent_sulfur",
];
/// Visit every value of `target` under a `root`-typed value read with schema `k`.
pub fn walk(root: Ty, v: &mut Tag, k: Key, target: Ty, f: &mut dyn FnMut(&mut Tag)) {
Walker { k, target, f }.walk(root, v);
}
+80
View File
@@ -0,0 +1,80 @@
use super::*;
use crate::nbt::parse_snbt;
fn snbt(s: &str) -> Tag {
parse_snbt(s).unwrap().0
}
#[test]
fn current_data_is_left_alone() {
let mut tag = snbt("{DataVersion: 5023, Status: \"minecraft:full\"}");
let before = tag.clone();
update_chunk(&mut tag, "minecraft:overworld", Some("minecraft:noise"));
assert_eq!(tag, before);
}
#[test]
fn item_stacks_of_1_20_take_components() {
let mut player = snbt(
"{DataVersion: 3465, Inventory: [{Slot: 0b, id: \"minecraft:diamond_sword\", Count: 1b, \
tag: {Damage: 5, display: {Name: '{\"text\":\"Blade\"}'}}}]}",
);
update_player(&mut player);
let stack = &dynamic::stream(player.get("Inventory"))[0];
assert_eq!(
dynamic::as_str(stack.get("id")).as_deref(),
Some("minecraft:diamond_sword")
);
assert_eq!(stack.get("count"), Some(&Tag::Int(1)));
let components = stack.get("components").expect("components");
assert_eq!(components.get("minecraft:damage"), Some(&Tag::Int(5)));
assert_eq!(
components.get("minecraft:custom_name"),
Some(&Tag::compound([("text", dynamic::string("Blade"))]))
);
}
#[test]
fn level_data_of_1_20_moves_to_the_new_fields() {
let mut level = snbt(
"{DataVersion: 3465, Difficulty: 3b, DifficultyLocked: 1b, hardcore: 0b, SpawnX: 1, SpawnY: 64, SpawnZ: -3, \
SpawnAngle: 90.0f, GameRules: {doFireTick: \"false\", keepInventory: \"true\", randomTickSpeed: \"5\"}}",
);
update(Ty::Level, &mut level, 3465, CURRENT);
let settings = level
.get("difficulty_settings")
.expect("difficulty settings");
assert_eq!(
dynamic::as_str(settings.get("difficulty")).as_deref(),
Some("hard")
);
assert_eq!(settings.get("locked"), Some(&Tag::Byte(1)));
let rules = level.get("game_rules").expect("game rules");
assert_eq!(rules.get("minecraft:keep_inventory"), Some(&Tag::Byte(1)));
assert_eq!(rules.get("minecraft:random_tick_speed"), Some(&Tag::Int(5)));
assert_eq!(
rules.get("minecraft:fire_spread_radius_around_player"),
Some(&Tag::Int(0))
);
assert_eq!(
level.get("spawn").and_then(|s| s.get("pos")),
Some(&Tag::IntArray(vec![1, 64, -3]))
);
}
#[test]
fn block_states_take_their_new_field_names() {
let mut chunk = snbt(
"{DataVersion: 4903, Status: \"minecraft:full\", sections: [{Y: 0b, block_states: \
{palette: [{Name: \"minecraft:chain\", Properties: {axis: \"y\"}}]}}]}",
);
update_chunk(&mut chunk, "minecraft:the_nether", Some("minecraft:noise"));
let section = &dynamic::stream(chunk.get("sections"))[0];
let state = &dynamic::stream(section.get("block_states").and_then(|b| b.get("palette")))[0];
assert_eq!(
dynamic::as_str(state.get("id")).as_deref(),
Some("minecraft:chain")
);
assert!(state.get("properties").is_some());
assert_eq!(chunk.get("DataVersion"), Some(&Tag::Int(CURRENT)));
}
+252
View File
@@ -0,0 +1,252 @@
//! The text component fixes of 1.21.5 (keys 4290 to 4292): JSON strings become
//! NBT components, legacy hover events are replaced, stringified style flags
//! become booleans and hover/click events take their new field names.
use super::FixFn;
use super::dynamic::{
as_number, as_str, as_string, boolean, empty_map, remove, rename, rename_and_fix, set, string,
};
use super::json;
use super::schema::{Key, Ty, key};
use crate::nbt::Tag;
pub(super) fn register(out: &mut Vec<(Key, FixFn)>) {
out.push((key(4290, 0), |cx| cx.each(Ty::TextComponent, unflatten)));
out.push((key(4291, 0), |cx| {
cx.each(Ty::TextComponent, legacy_hover_event)
}));
out.push((key(4291, 0), |cx| {
cx.each(Ty::TextComponent, stringified_flags)
}));
out.push((key(4292, 0), |cx| {
cx.each(Ty::TextComponent, hover_and_click_event)
}));
}
/// `UnflattenTextComponentFix`: the JSON of a string component as NBT.
fn unflatten(v: &mut Tag) {
let Tag::String(s) = v else {
return;
};
if let Some(parsed) = json::parse_lenient(&s.0)
&& let Some(tag) = json::to_nbt(&parsed)
{
*v = tag;
}
}
fn is_full(v: &Tag) -> bool {
matches!(v, Tag::Compound(_))
}
/// The hover event a schema of 4290/4291 reads as typed (a known action).
fn typed_hover(v: &Tag) -> Option<String> {
let h = v.get("hoverEvent")?;
let action = as_str(h.get("action"))?;
matches!(action.as_str(), "show_text" | "show_item" | "show_entity").then_some(action)
}
/// `LegacyHoverEventFix`.
fn legacy_hover_event(v: &mut Tag) {
if !is_full(v) {
return;
}
let Some(hover) = v.get("hoverEvent").cloned() else {
return;
};
if hover.get("value").is_none() {
return;
}
let action = typed_hover(v).unwrap_or_default();
let new = if action == "show_text" {
let mut h = hover;
rename(&mut h, "value", "contents");
h
} else {
let mut text = "Legacy hoverEvent: ".encode_utf16().collect::<Vec<u16>>();
text.extend(json::to_stable_string(&json::from_nbt(&hover)));
Tag::compound([
("action", string("show_text")),
(
"contents",
Tag::compound([("text", Tag::String(crate::nbt::JavaString(text)))]),
),
])
};
set(v, "hoverEvent", new);
}
/// `TextComponentStringifiedFlagsFix`: `Boolean.parseBoolean` of string flags.
fn stringified_flags(v: &mut Tag) {
if !is_full(v) {
return;
}
for flag in [
"bold",
"italic",
"underlined",
"strikethrough",
"obfuscated",
] {
if let Some(s) = as_str(v.get(flag)) {
set(v, flag, boolean(s.eq_ignore_ascii_case("true")));
}
}
}
/// `TextComponentHoverAndClickEventFix`.
fn hover_and_click_event(v: &mut Tag) {
if !is_full(v) {
return;
}
if typed_hover(v).is_none() && v.get("clickEvent").is_none() {
return;
}
rename_and_fix(v, "hoverEvent", "hover_event", |h| Some(fix_hover_event(h)));
rename_and_fix(v, "clickEvent", "click_event", fix_click_event);
}
fn copy_fields(target: &mut Tag, source: &Tag, fields: &[&str]) {
for f in fields {
if let Some(value) = source.get(f) {
set(target, f, value.clone());
}
}
}
fn fix_hover_event(mut h: Tag) -> Tag {
match as_string(h.get("action"), "").as_str() {
"show_text" => rename(&mut h, "contents", "value"),
"show_item" => {
let contents = h.get("contents").cloned().unwrap_or_else(empty_map);
remove(&mut h, "contents");
copy_fields(&mut h, &contents, &["id", "count", "components"]);
}
"show_entity" => {
let contents = h.get("contents").cloned().unwrap_or_else(empty_map);
remove(&mut h, "contents");
copy_fields(&mut h, &contents, &["id", "type", "name"]);
rename(&mut h, "id", "uuid");
rename(&mut h, "type", "id");
}
_ => {}
}
h
}
fn fix_click_event(mut c: Tag) -> Option<Tag> {
let action = as_string(c.get("action"), "");
let value = as_string(c.get("value"), "");
match action.as_str() {
"open_url" => {
if !valid_uri(&value) {
return None;
}
rename(&mut c, "value", "url");
}
"open_file" => rename(&mut c, "value", "path"),
"run_command" | "suggest_command" => {
if !valid_chat(&value) {
return None;
}
rename(&mut c, "value", "command");
}
"change_page" => {
let old = c.get("value").and_then(|v| match as_number(v) {
Some(n) => Some(n.int_value()),
None => parse_int(&as_string(Some(v), "")),
})?;
remove(&mut c, "value");
set(&mut c, "page", Tag::Int(old.max(1)));
}
_ => {}
}
Some(c)
}
/// `Integer.parseInt`.
fn parse_int(s: &str) -> Option<i32> {
let digits = s.strip_prefix(['+', '-']).unwrap_or(s);
if digits.is_empty() || !digits.bytes().all(|b| b.is_ascii_digit()) {
return None;
}
s.parse().ok()
}
/// `new URI(uri)` succeeds with an http or https scheme.
fn valid_uri(uri: &str) -> bool {
let Some((scheme, rest)) = uri.split_once(':') else {
return false;
};
let mut chars = scheme.chars();
let scheme_ok = chars.next().is_some_and(|c| c.is_ascii_alphabetic())
&& chars.all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '.'));
if !scheme_ok || rest.is_empty() {
return false;
}
// Characters java.net.URI refuses anywhere, and escapes it checks.
let bytes: Vec<char> = rest.chars().collect();
let mut hashes = 0;
let mut i = 0;
while i < bytes.len() {
let c = bytes[i];
if c == '%' {
if !bytes.get(i + 1).is_some_and(char::is_ascii_hexdigit)
|| !bytes.get(i + 2).is_some_and(char::is_ascii_hexdigit)
{
return false;
}
i += 3;
continue;
}
if c == '#' {
hashes += 1;
}
if c.is_control()
|| c.is_whitespace()
|| matches!(c, '"' | '<' | '>' | '\\' | '^' | '`' | '{' | '|' | '}')
{
return false;
}
i += 1;
}
hashes <= 1 && matches!(scheme.to_ascii_lowercase().as_str(), "http" | "https")
}
/// `StringUtil.isAllowedChatCharacter` for every character.
fn valid_chat(s: &str) -> bool {
s.encode_utf16().all(|c| c != 167 && c >= 32 && c != 127)
}
#[cfg(test)]
mod tests {
use super::*;
fn j(s: &str) -> Tag {
json::to_nbt(&json::parse_lenient(&s.encode_utf16().collect::<Vec<_>>()).unwrap()).unwrap()
}
#[test]
fn click_events_take_their_new_fields() {
let mut c = j(r#"{"text":"a","clickEvent":{"action":"change_page","value":"0"}}"#);
hover_and_click_event(&mut c);
// The page is written as an int (`createInt`), not as JSON's narrowest number.
let mut expected = j(r#"{"text":"a","click_event":{"action":"change_page"}}"#);
if let Some(click) = expected.get_mut("click_event") {
super::super::dynamic::set(click, "page", Tag::Int(1));
}
assert_eq!(c, expected);
let mut c = j(r#"{"text":"a","clickEvent":{"action":"open_url","value":"ftp://x"}}"#);
hover_and_click_event(&mut c);
assert_eq!(c, j(r#"{"text":"a"}"#));
}
#[test]
fn legacy_hover_events_become_text() {
let mut c = j(r#"{"text":"a","hoverEvent":{"action":"show_item","value":"{id:stone}"}}"#);
legacy_hover_event(&mut c);
let expected = j(
r#"{"text":"a","hoverEvent":{"action":"show_text","contents":{"text":"Legacy hoverEvent: {\"action\":\"show_item\",\"value\":\"{id:stone}\"}"}}}"#,
);
assert_eq!(c, expected);
}
}
+1
View File
@@ -34,6 +34,7 @@ pub mod consume_effect;
pub mod damage_immunity;
pub mod damage_reduction;
pub mod damage_source;
pub mod datafix;
pub mod dynamic_support;
pub mod edge_backoff;
pub mod effect_attribute_modifiers;
+13 -14
View File
@@ -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,
+12 -3
View File
@@ -120,12 +120,21 @@ fn malformed_region(path: &Path, first: u32, second: u32, payload_len: u32, code
}
#[test]
fn corrupt_allocations_lengths_codecs_and_missing_external_files_fail() {
fn corrupt_allocations_are_dropped_and_bad_payloads_fail() {
let dir = Directory::new();
for (first, second) in [(257, 0), (512, 0), (1025, 0), (513, 513)] {
// RegionFile's constructor drops (and logs) entries whose sector overlaps the
// header, that have no sectors or that start past the end of the file; it
// does not check overlaps between chunks.
for (first, second, chunks) in [(257, 0, 0), (512, 0, 0), (1025, 0, 0), (513, 513, 2)] {
malformed_region(&dir.region(), first, second, 1, 3);
assert!(RegionFile::open(dir.region()).is_err());
let reader = RegionFile::open(dir.region()).unwrap();
assert_eq!(reader.chunks().count(), chunks);
}
// An empty or truncated file reads as a region without chunks.
fs::write(dir.region(), b"").unwrap();
assert_eq!(RegionFile::open(dir.region()).unwrap().chunks().count(), 0);
fs::write(dir.region(), [0, 0, 2, 1]).unwrap();
assert_eq!(RegionFile::open(dir.region()).unwrap().chunks().count(), 0);
for (len, codec) in [
(0, 3),
(4093, 3),
+1 -4
View File
@@ -68,10 +68,7 @@ The registry loader checks data version, duplicate states, default IDs and
block-name coverage. The local oracle exports all 35,723 state identities and
defaults from vanilla. This imports identities, not block implementations,
shapes or behavior. Registry exports and game-derived fixtures stay in `.local/`.
Chunk access requires data version 5023. The DataFixerUpper chain is not ported:
opening a world whose `level.dat` is older fails with a message pointing at
`tools/upgrade_world.py`, which runs vanilla's own `--forceUpgrade` on a copy
(exercised only against a current-version world; no older server jar is local). `BlockRegistry::resolve` rejects invalid property descriptors. The separate
Chunk access requires data version 5023. A world saved by an older version is upgraded when it is opened, as 26.3 does: the file fixes (`FileFixerUpper`) and level.dat, then vanilla's DataFixers over every chunk, entity chunk, POI chunk, player, saved data, advancements and statistics file (`horizon_world::datafix`; vanilla upgrades each file as it reads it, Horizon all of them on opening). `tools/test_datafix_parity.py` compares the result with vanilla's own upgrade over worlds written by older server jars. Worlds older than the ported fixes are refused with a message pointing at `tools/upgrade_world.py`, which runs vanilla's `--forceUpgrade` on a copy. `BlockRegistry::resolve` rejects invalid property descriptors. The separate
[block-state JSON codec](block-state-codecs.md) reproduces vanilla property
fallback for compact and full JSON forms.
@@ -0,0 +1,246 @@
package dev.horizonlab.parity;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.mojang.serialization.Dynamic;
import com.mojang.serialization.JsonOps;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.*;
import java.util.stream.Stream;
import net.minecraft.SharedConstants;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.*;
import net.minecraft.resources.Identifier;
import net.minecraft.resources.ResourceKey;
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.worldupdate.UpgradeProgress;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.chunk.storage.RegionFile;
import net.minecraft.world.level.chunk.storage.RegionStorageInfo;
import net.minecraft.world.level.storage.LevelStorageSource;
/**
* Opens a world saved by an older game version the way 26.3 does and writes out
* what 26.3 reads: the file fixes (FileFixerUpper) on the folder, level.dat fixed
* to the current DataVersion, then every chunk, entity chunk and POI chunk
* (SimpleRegionStorage.upgradeChunkTag with ChunkMap's context), player file
* (PlayerDataStorage.load), saved data file (SavedDataStorage.readTagFromDisk),
* advancements and statistics through the real DataFixers.
*
* VanillaDataFixOracle IN_WORLD OUT_WORLD
*
* Files the game rewrites after loading get the current DataVersion so that the
* output compares with what the game would save. Nothing is answered by hand.
*/
public final class VanillaDataFixOracle {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
public static void main(String[] args) throws Exception {
SharedConstants.tryDetectVersion();
Bootstrap.bootStrap();
Path in = Path.of(args[0]);
Path out = Path.of(args[1]);
copy(in, out);
int current = SharedConstants.getCurrentVersion().dataVersion().version();
var fixer = DataFixers.getDataFixer();
// FileFixerUpper.fix through a real level storage access, as the game opens a
// world: the file fixes on a copy-on-write view swapped in, then level.dat
// fixed to the current version and stamped (DataVersion, LastPlayed, Version).
LevelStorageSource source = LevelStorageSource.createDefault(out.getParent());
try (LevelStorageSource.LevelStorageAccess access = source.createAccess(out.getFileName().toString())) {
Dynamic<?> levelData = access.getUnfixedDataTag(false);
Dynamic<?> fixed = DataFixers.getFileFixer().fix(access, levelData, new UpgradeProgress());
access.saveLevelData(fixed);
}
Files.deleteIfExists(out.resolve("level.dat_old"));
Map<String, String> generators = generators(out);
Path dimensions = out.resolve("dimensions");
if (Files.isDirectory(dimensions)) {
List<Path> dims;
try (Stream<Path> s = Files.walk(dimensions, 2)) {
dims = s.filter(p -> dimensions.relativize(p).getNameCount() == 2).sorted().toList();
}
for (Path dim : dims) {
String id = dim.getParent().getFileName() + ":" + dim.getFileName();
ResourceKey<Level> key = ResourceKey.create(Registries.DIMENSION, Identifier.parse(id));
String generator = generators.get(id);
CompoundTag context = ChunkMap.getChunkDataFixContextTag(
key, Optional.ofNullable(generator).map(Identifier::parse));
upgradeRegions(dim.resolve("region"), key, "chunk", DataFixTypes.CHUNK, -1, context, current);
upgradeRegions(dim.resolve("entities"), key, "entities", DataFixTypes.ENTITY_CHUNK, -1, null, current);
upgradeRegions(dim.resolve("poi"), key, "poi", DataFixTypes.POI_CHUNK, 1945, null, current);
upgradeSavedData(dim.resolve("data"), current);
}
}
upgradeSavedData(out.resolve("data"), current);
for (Path file : list(out.resolve("players/data"), ".dat")) {
CompoundTag tag = NbtIo.readCompressed(file, NbtAccounter.unlimitedHeap());
tag = DataFixTypes.PLAYER.updateToCurrentVersion(fixer, tag, NbtUtils.getDataVersion(tag));
NbtIo.writeCompressed(NbtUtils.addDataVersion(tag, current), file);
}
for (Path file : list(out.resolve("players/advancements"), ".json")) {
// PlayerAdvancements: DataFixTypes.ADVANCEMENTS.wrapCodec, default version 1343.
JsonElement json = JsonParser.parseString(Files.readString(file, StandardCharsets.UTF_8));
var dynamic = new Dynamic<>(JsonOps.INSTANCE, json);
int from = dynamic.get("DataVersion").asInt(1343);
var fixed = DataFixTypes.ADVANCEMENTS.updateToCurrentVersion(fixer, dynamic.remove("DataVersion"), from);
fixed = fixed.set("DataVersion", fixed.createInt(current));
Files.writeString(file, GSON.toJson(fixed.getValue()), StandardCharsets.UTF_8);
}
for (Path file : list(out.resolve("players/stats"), ".json")) {
// ServerStatsCounter.parse: the version field stays in the data.
JsonElement json = JsonParser.parseString(Files.readString(file, StandardCharsets.UTF_8));
var dynamic = new Dynamic<>(JsonOps.INSTANCE, json);
var fixed = DataFixTypes.STATS.updateToCurrentVersion(fixer, dynamic, NbtUtils.getDataVersion(dynamic, 1343));
fixed = fixed.set("DataVersion", fixed.createInt(current));
Files.writeString(file, GSON.toJson(fixed.getValue()), StandardCharsets.UTF_8);
}
}
/** Chunk generator type per dimension, from the saved world generation settings. */
private static Map<String, String> generators(Path world) throws Exception {
Map<String, String> out = new HashMap<>();
Path file = world.resolve("data/minecraft/world_gen_settings.dat");
if (!Files.exists(file)) {
return out;
}
CompoundTag tag = NbtIo.readCompressed(file, NbtAccounter.unlimitedHeap());
CompoundTag dims = tag.getCompoundOrEmpty("data").getCompoundOrEmpty("dimensions");
for (String dim : dims.keySet()) {
dims.getCompoundOrEmpty(dim).getCompoundOrEmpty("generator").getString("type").ifPresent(t -> out.put(dim, t));
}
return out;
}
private static void upgradeRegions(Path folder, ResourceKey<Level> dimension, String type, DataFixTypes fixType,
int defaultVersion, CompoundTag context, int current) throws Exception {
if (!Files.isDirectory(folder)) {
return;
}
var fixer = DataFixers.getDataFixer();
for (Path file : list(folder, ".mca")) {
String[] parts = file.getFileName().toString().split("\\.");
int rx = Integer.parseInt(parts[1]);
int rz = Integer.parseInt(parts[2]);
RegionStorageInfo info = new RegionStorageInfo("oracle", dimension, type);
Map<ChunkPos, CompoundTag> chunks = new LinkedHashMap<>();
try (RegionFile region = new RegionFile(info, file, folder, false)) {
for (int z = 0; z < 32; z++) {
for (int x = 0; x < 32; x++) {
ChunkPos pos = new ChunkPos(rx * 32 + x, rz * 32 + z);
if (!region.hasChunk(pos)) {
continue;
}
try (DataInputStream in = region.getChunkDataInputStream(pos)) {
if (in != null) {
chunks.put(pos, NbtIo.read(in));
}
}
}
}
}
Files.delete(file);
try (RegionFile region = new RegionFile(info, file, folder, false)) {
for (var entry : chunks.entrySet()) {
CompoundTag tag = entry.getValue();
int version = NbtUtils.getDataVersion(tag, defaultVersion);
if (version < current) {
// SimpleRegionStorage.upgradeChunkTag
if (context != null) {
tag.put("__context", context.copy());
}
tag = fixType.update(fixer, tag, version, current);
tag.remove("__context");
NbtUtils.addDataVersion(tag, current);
}
try (DataOutputStream o = region.getChunkDataOutputStream(entry.getKey())) {
NbtIo.write(tag, o);
}
}
}
}
}
/** SavedDataStorage.readTagFromDisk for every known saved data file under a data folder. */
private static void upgradeSavedData(Path data, int current) throws Exception {
if (!Files.isDirectory(data)) {
return;
}
List<Path> files;
try (Stream<Path> s = Files.walk(data)) {
files = s.filter(p -> p.toString().endsWith(".dat")).sorted().toList();
}
var fixer = DataFixers.getDataFixer();
for (Path file : files) {
String rel = data.relativize(file).toString().replace('\\', '/');
DataFixTypes type = savedDataType(rel);
if (type == null) {
continue;
}
CompoundTag tag = NbtIo.readCompressed(file, NbtAccounter.unlimitedHeap());
int version = NbtUtils.getDataVersion(tag, 1343);
tag = type.update(fixer, tag, version, current);
NbtIo.writeCompressed(NbtUtils.addDataVersion(tag, current), file);
}
}
/** The SavedDataType registered for a file below data/ (namespace/path.dat). */
static DataFixTypes savedDataType(String rel) {
String name = rel.endsWith(".dat") ? rel.substring(0, rel.length() - 4) : rel;
if (name.endsWith("/command_storage")) {
return DataFixTypes.SAVED_DATA_COMMAND_STORAGE;
}
if (name.matches("minecraft/maps/\\d+")) {
return DataFixTypes.SAVED_DATA_MAP_DATA;
}
return switch (name) {
case "minecraft/custom_boss_events" -> DataFixTypes.SAVED_DATA_CUSTOM_BOSS_EVENTS;
case "minecraft/scoreboard" -> DataFixTypes.SAVED_DATA_SCOREBOARD;
case "minecraft/chunk_tickets" -> DataFixTypes.SAVED_DATA_FORCED_CHUNKS;
case "minecraft/world_border" -> DataFixTypes.SAVED_DATA_WORLD_BORDER;
case "minecraft/game_rules" -> DataFixTypes.SAVED_DATA_GAME_RULES;
case "minecraft/ender_dragon_fight" -> DataFixTypes.SAVED_DATA_ENDER_DRAGON_FIGHT;
case "minecraft/wandering_trader" -> DataFixTypes.SAVED_DATA_WANDERING_TRADER;
case "minecraft/maps/last_id" -> DataFixTypes.SAVED_DATA_MAP_INDEX;
case "minecraft/weather" -> DataFixTypes.SAVED_DATA_WEATHER;
case "minecraft/scheduled_events" -> DataFixTypes.SAVED_DATA_SCHEDULED_EVENTS;
case "minecraft/world_gen_settings" -> DataFixTypes.SAVED_DATA_WORLD_GEN_SETTINGS;
case "minecraft/random_sequences" -> DataFixTypes.SAVED_DATA_RANDOM_SEQUENCES;
case "minecraft/stopwatches" -> DataFixTypes.SAVED_DATA_STOPWATCHES;
case "minecraft/raids" -> DataFixTypes.SAVED_DATA_RAIDS;
case "minecraft/world_clocks" -> DataFixTypes.SAVED_DATA_WORLD_CLOCKS;
default -> null;
};
}
private static List<Path> list(Path folder, String suffix) throws Exception {
if (!Files.isDirectory(folder)) {
return List.of();
}
try (Stream<Path> s = Files.list(folder)) {
return s.filter(p -> p.getFileName().toString().endsWith(suffix)).sorted().toList();
}
}
private static void copy(Path from, Path to) throws Exception {
try (Stream<Path> s = Files.walk(from)) {
for (Path p : s.sorted().toList()) {
Path target = to.resolve(from.relativize(p).toString());
if (Files.isDirectory(p)) {
Files.createDirectories(target);
} else if (!p.getFileName().toString().equals("session.lock")) {
Files.copy(p, target, StandardCopyOption.REPLACE_EXISTING);
}
}
}
}
}
+418
View File
@@ -0,0 +1,418 @@
"""Scenes for tools/datafix_worlds.py: block entities, entities and items in the save
formats of older game versions, placed by console commands of that version.
Three syntax eras matter for the commands:
A before 1.20.5 (DataVersion < 3818): item `tag` NBT, `Count`, JSON text strings
B 1.20.5 to 1.21.4 (< 4325): item components, JSON text strings
C 1.21.5 and later: item components, SNBT text components, `equipment`
Every entry is (minimum DataVersion, command template). `{x} {y} {z}` are filled
with a free position; entities get a fixed UUID so outputs can be matched.
"""
A, B, C = 'A', 'B', 'C'
def era(version):
return A if version < 3818 else B if version < 4325 else C
def text(e, s):
"""A text component value for NBT: JSON string (A/B) or SNBT compound (C)."""
if e == C:
return '{text:"%s"}' % s
return "'{\"text\":\"%s\"}'" % s
# ------------------------------------------------------------------ items, era A
ITEMS_A = [
'{id:"minecraft:diamond_sword",Count:1b,tag:{Damage:5,RepairCost:3,Unbreakable:1b,HideFlags:255,'
'Enchantments:[{id:"minecraft:sharpness",lvl:3s},{id:"minecraft:sweeping",lvl:2s}],'
'display:{Name:\'{"text":"Blade","color":"red","italic":false}\',Lore:[\'{"text":"line one"}\',\'"plain"\']},'
'AttributeModifiers:[{AttributeName:"generic.attack_damage",Name:"x",Amount:2.5d,Operation:0,'
'UUID:[I;1,2,3,4],Slot:"mainhand"}],CustomModelData:7}}',
'{id:"minecraft:enchanted_book",Count:1b,tag:{StoredEnchantments:[{id:"minecraft:mending",lvl:1s}]}}',
'{id:"minecraft:written_book",Count:1b,tag:{title:"Title",author:"Me",generation:1,resolved:1b,'
'pages:[\'{"text":"page one","bold":true}\',\'{"text":"two","clickEvent":{"action":"change_page","value":"2"},'
'"hoverEvent":{"action":"show_text","value":"hi"}}\'],filtered_pages:{"1":\'"f"\'}}}',
'{id:"minecraft:writable_book",Count:1b,tag:{pages:["draft one","draft two"]}}',
'{id:"minecraft:potion",Count:1b,tag:{Potion:"minecraft:strong_healing",CustomPotionColor:255}}',
'{id:"minecraft:splash_potion",Count:1b,tag:{CustomPotionEffects:[{Id:1b,Amplifier:1b,Duration:200}]}}',
'{id:"minecraft:tipped_arrow",Count:5b,tag:{Potion:"minecraft:poison"}}',
'{id:"minecraft:suspicious_stew",Count:1b,tag:{Effects:[{EffectId:3,EffectDuration:160}]}}',
'{id:"minecraft:firework_rocket",Count:3b,tag:{Fireworks:{Flight:2b,Explosions:[{Type:1b,Colors:[I;255],'
'FadeColors:[I;16711680],Trail:1b,Flicker:0b}]}}}',
'{id:"minecraft:firework_star",Count:1b,tag:{Explosion:{Type:3b,Colors:[I;65280]}}}',
'{id:"minecraft:player_head",Count:1b,tag:{SkullOwner:{Name:"Notch",Id:[I;1,2,3,4],'
'Properties:{textures:[{Value:"abc",Signature:"sig"}]}}}}',
'{id:"minecraft:player_head",Count:1b,tag:{SkullOwner:"jeb_"}}',
'{id:"minecraft:filled_map",Count:1b,tag:{map:3,display:{MapColor:255},Decorations:[{id:"x",type:9b,x:1.0d,z:2.0d,rot:180.0d}]}}',
'{id:"minecraft:compass",Count:1b,tag:{LodestonePos:{X:1,Y:2,Z:3},LodestoneDimension:"minecraft:overworld",LodestoneTracked:0b}}',
'{id:"minecraft:crossbow",Count:1b,tag:{Charged:1b,ChargedProjectiles:[{id:"minecraft:arrow",Count:1b}]}}',
'{id:"minecraft:shield",Count:1b,tag:{BlockEntityTag:{Base:4,Patterns:[{Pattern:"cre",Color:15},{Pattern:"bs",Color:1}]}}}',
'{id:"minecraft:white_banner",Count:1b,tag:{BlockEntityTag:{Patterns:[{Pattern:"mr",Color:9}]},'
'display:{Name:\'{"translate":"block.minecraft.ominous_banner","color":"gold"}\'}}}',
'{id:"minecraft:red_shulker_box",Count:1b,tag:{BlockEntityTag:{Items:[{Slot:3b,id:"minecraft:stone",Count:9b},'
'{Slot:0b,id:"minecraft:golden_apple",Count:2b,tag:{display:{Name:\'"Gold"\'}}}],Lock:"key",'
'CustomName:\'{"text":"Box"}\'}}}',
'{id:"minecraft:chest",Count:1b,tag:{BlockEntityTag:{LootTable:"minecraft:chests/simple_dungeon",LootTableSeed:5L}}}',
'{id:"minecraft:pig_spawn_egg",Count:1b,tag:{EntityTag:{CustomName:\'"Babe"\',Age:-24000}}}',
'{id:"minecraft:armor_stand",Count:1b,tag:{EntityTag:{Small:1b}}}',
'{id:"minecraft:pig_spawn_egg",Count:1b,tag:{EntityTag:{id:"minecraft:cow"}}}',
'{id:"minecraft:leather_chestplate",Count:1b,tag:{display:{color:16711680},'
'Trim:{material:"minecraft:gold",pattern:"minecraft:coast"}}}',
'{id:"minecraft:goat_horn",Count:1b,tag:{instrument:"minecraft:sing_goat_horn"}}',
'{id:"minecraft:axolotl_bucket",Count:1b,tag:{Variant:2,Age:-100,Health:7.0f,NoAI:1b}}',
'{id:"minecraft:tropical_fish_bucket",Count:1b,tag:{BucketVariantTag:65536}}',
'{id:"minecraft:knowledge_book",Count:1b,tag:{Recipes:["minecraft:stone"]}}',
'{id:"minecraft:debug_stick",Count:1b,tag:{DebugProperty:{"minecraft:oak_stairs":"facing"}}}',
'{id:"minecraft:diamond_pickaxe",Count:1b,tag:{CanDestroy:["minecraft:stone","#minecraft:logs",'
'"minecraft:oak_stairs[facing=east]"],HideFlags:8}}',
'{id:"minecraft:stone",Count:4b,tag:{CanPlaceOn:["minecraft:dirt"],BlockStateTag:{}}}',
'{id:"minecraft:oak_slab",Count:1b,tag:{BlockStateTag:{type:"top",waterlogged:1b}}}',
'{id:"minecraft:grass",Count:2b}',
'{id:"minecraft:scute",Count:2b}',
'{id:"minecraft:chain",Count:1b}',
'{id:"minecraft:decorated_pot",Count:1b,tag:{BlockEntityTag:{sherds:["minecraft:brick","minecraft:angler_pottery_sherd",'
'"minecraft:brick","minecraft:arms_up_pottery_sherd"]}}}',
'{id:"minecraft:beehive",Count:1b,tag:{BlockEntityTag:{Bees:[{EntityData:{id:"minecraft:bee"},TicksInHive:3,'
'MinOccupationTicks:600}]}}}',
'{id:"minecraft:cooked_beef",Count:64b,tag:{custom:"data",nested:{a:1}}}',
'{id:"minecraft:iron_sword",Count:1b,tag:{Enchantments:[]}}',
'{id:"minecraft:bow",Count:1b,tag:{Enchantments:[{id:"minecraft:power",lvl:0s}],HideFlags:1}}',
]
# ------------------------------------------------------------------ items, era B/C
ITEMS_B = [
'{id:"minecraft:diamond_sword",count:1,components:{"minecraft:damage":5,"minecraft:repair_cost":3,'
'"minecraft:unbreakable":{show_in_tooltip:false},"minecraft:enchantments":{levels:{"minecraft:sharpness":3}},'
'"minecraft:custom_name":\'{"text":"Blade","color":"red"}\',"minecraft:lore":[\'"plain"\'],'
'"minecraft:attribute_modifiers":{modifiers:[{type:"minecraft:generic.attack_damage",id:"minecraft:x",'
'amount:2.5d,operation:"add_value",slot:"mainhand"}]}}}',
'{id:"minecraft:written_book",count:1,components:{"minecraft:written_book_content":{title:{raw:"T"},author:"Me",'
'pages:[{raw:\'{"text":"p1","clickEvent":{"action":"run_command","value":"/say hi"}}\'}]}}}',
'{id:"minecraft:potion",count:1,components:{"minecraft:potion_contents":{potion:"minecraft:healing"}}}',
'{id:"minecraft:white_banner",count:1,components:{"minecraft:banner_patterns":[{pattern:"minecraft:creeper",color:"black"}],'
'"minecraft:item_name":\'{"translate":"block.minecraft.ominous_banner"}\',"minecraft:rarity":"uncommon"}}',
'{id:"minecraft:red_shulker_box",count:1,components:{"minecraft:container":[{slot:0,item:{id:"minecraft:stone",count:3}}],'
'"minecraft:lock":"key"}}',
'{id:"minecraft:apple",count:2,components:{"minecraft:food":{nutrition:4,saturation:2.4f,can_always_eat:true,'
'eat_seconds:1.0f}}}',
'{id:"minecraft:netherite_sword",count:1,components:{"minecraft:fire_resistant":{}}}',
'{id:"minecraft:stick",count:1,components:{"minecraft:custom_model_data":5}}',
'{id:"minecraft:pig_spawn_egg",count:1,components:{"minecraft:entity_data":{id:"minecraft:pig",Age:-200}}}',
'{id:"minecraft:diamond_helmet",count:1,components:{"minecraft:hide_additional_tooltip":{},'
'"minecraft:trim":{material:"minecraft:gold",pattern:"minecraft:coast",show_in_tooltip:false}}}',
'{id:"minecraft:decorated_pot",count:1,components:{"minecraft:pot_decorations":["minecraft:brick",'
'"minecraft:angler_pottery_sherd"]}}',
'{id:"minecraft:chain",count:1}',
]
ITEMS_C = [
'{id:"minecraft:diamond_sword",count:1,components:{"minecraft:damage":5,'
'"minecraft:custom_name":{text:"Blade",color:"red"},"minecraft:tooltip_display":{hidden_components:["minecraft:damage"]}}}',
'{id:"minecraft:written_book",count:1,components:{"minecraft:written_book_content":{title:{raw:"T"},author:"Me",'
'pages:[{raw:{text:"p1",click_event:{action:"change_page",page:2}}}]}}}',
'{id:"minecraft:decorated_pot",count:1,components:{"minecraft:pot_decorations":["minecraft:brick",'
'"minecraft:angler_pottery_sherd"]}}',
'{id:"minecraft:filled_map",count:1,components:{"minecraft:map_id":2,"minecraft:map_color":255}}',
'{id:"minecraft:chain",count:1}',
'{id:"minecraft:name_tag",count:1,components:{"minecraft:custom_name":"Bob"}}',
]
def experiments(version):
"""Experimental data packs a world of `version` turns on, so that the blocks behind
them exist and the feature-flag fixes have flags to remove."""
packs = []
if 3218 <= version < 3440:
packs.append('update_1_20')
if 3698 <= version < 3939:
packs.append('update_1_21')
if 3700 <= version < 4067:
packs.append('bundle')
if 4080 <= version < 4180:
packs.append('winter_drop')
return packs
def items(e, version=None):
"""The sample stacks of an era, in the exact component formats `version` accepts."""
stacks = ITEMS_A if e == A else ITEMS_B if e == B else ITEMS_C
if version is None:
return stacks
return [v for v in (versioned(s, version) for s in stacks) if available(v, version)]
# Items era A worlds older than each DataVersion do not have.
ITEM_SINCE = {
'minecraft:goat_horn': 3105,
'minecraft:decorated_pot': 3327,
}
def available(stack, version):
return all(version >= since for item, since in ITEM_SINCE.items() if f'"{item}"' in stack)
def versioned(stack, version):
"""Rewrite the components whose format changed inside an era."""
if version < 3448: # 1.19.4: pots hold shards, sherds were pottery_shard_*
stack = (stack.replace('sherds:', 'shards:')
.replace('"minecraft:angler_pottery_sherd"', '"minecraft:pottery_shard_prize"')
.replace('"minecraft:arms_up_pottery_sherd"', '"minecraft:pottery_shard_arms_up"')
.replace('"minecraft:skull_pottery_sherd"', '"minecraft:pottery_shard_skull"'))
if version >= 4541: # chain renamed to iron_chain
stack = stack.replace('"minecraft:chain"', '"minecraft:iron_chain"')
if '"minecraft:attribute_modifiers"' in stack:
if version < 3945: # 1.20.5: modifiers are named and carry a UUID
stack = stack.replace('id:"minecraft:x",', 'uuid:[I;1,2,3,4],name:"x",')
if version >= 4055: # 1.21.2: attribute ids lose their generic. prefix
stack = stack.replace('minecraft:generic.attack_damage', 'minecraft:attack_damage')
if version >= 4068: # 1.21.2: a lock is an item predicate
stack = stack.replace('"minecraft:lock":"key"', '"minecraft:lock":{items:"minecraft:stick"}')
if version >= 4064: # 1.21.2: fire_resistant became damage_resistant
stack = stack.replace('"minecraft:fire_resistant":{}', '"minecraft:damage_resistant":{types:"#minecraft:is_fire"}')
if version >= 4059: # 1.21.2: food lost eat_seconds (now in consumable)
stack = stack.replace(',eat_seconds:1.0f', '')
if version >= 4175: # 1.21.4: custom model data holds lists
stack = stack.replace('"minecraft:custom_model_data":5', '"minecraft:custom_model_data":{floats:[5.0f]}')
return stack
def slotted(stacks):
out = []
for i, s in enumerate(stacks[:27]):
out.append(s[:1] + f'Slot:{i}b,' + s[1:])
return '[' + ','.join(out) + ']'
def chests(e, version=None):
"""Chests holding every sample item (27 per chest)."""
st = items(e, version)
return [st[i:i + 27] for i in range(0, len(st), 27)]
# ------------------------------------------------------------------ block entities
def block_entities(e, version):
name = lambda s: text(e, s)
out = []
for group in chests(e, version):
out.append(('chest', f'{{Items:{slotted(group)},CustomName:{name("Stash")}}}'))
sign_a = ('{front_text:{messages:[\'{"text":"one"}\',\'"two"\',\'""\',\'""\'],color:"red",has_glowing_text:1b},'
'back_text:{messages:[\'"b"\',\'""\',\'""\',\'""\'],filtered_messages:[\'"x"\',\'""\',\'""\',\'""\']},'
'is_waxed:1b}')
sign_c = ('{front_text:{messages:[{text:"one"},"two","",""],color:"red",has_glowing_text:1b},'
'back_text:{messages:["b","","",""]},is_waxed:1b}')
# Before 1.20 (3439) a sign has one side of loose Text fields.
sign_old = ('{Text1:\'{"text":"one"}\',Text2:\'"two"\',Text3:\'""\',Text4:\'""\',Color:"red",GlowingText:1b,'
'FilteredText1:\'"x"\'}')
sign = sign_c if e == C else sign_a if version >= 3439 else sign_old
out.append(('oak_sign', sign))
if version >= 3218: # hanging signs: 1.19.3 with the 1.20 experiment
out.append(('oak_hanging_sign', sign))
if e == A:
out += [
('white_banner', '{Patterns:[{Pattern:"cre",Color:15}],CustomName:\'{"translate":"block.minecraft.ominous_banner"}\'}'),
('player_head', '{SkullOwner:{Name:"Notch",Id:[I;1,2,3,4]}}'),
('decorated_pot', '{sherds:["minecraft:brick","minecraft:skull_pottery_sherd","minecraft:brick","minecraft:brick"]}'),
('beehive', '{Bees:[{EntityData:{id:"minecraft:bee",CustomName:\'"Bz"\'},TicksInHive:2,MinOccupationTicks:400}],'
'FlowerPos:{X:1,Y:2,Z:3}}'),
('furnace', '{BurnTime:100s,CookTime:20s,CookTimeTotal:200s,Items:[{Slot:0b,id:"minecraft:iron_ore",Count:3b}],'
'RecipesUsed:{"minecraft:iron_ingot":2}}'),
('jukebox', '{RecordItem:{id:"minecraft:music_disc_cat",Count:1b},RecordStartTick:100L,TickCount:250L,IsPlaying:1b}'),
('suspicious_sand', '{item:{id:"minecraft:emerald",Count:1b}}'),
('lectern', '{Book:{id:"minecraft:written_book",Count:1b,tag:{title:"L",author:"A",pages:[\'"x"\']}},Page:0}'),
('spawner', '{SpawnData:{entity:{id:"minecraft:zombie",ArmorItems:[{},{},{},{id:"minecraft:iron_helmet",Count:1b}]}},'
'SpawnPotentials:[{weight:1,data:{entity:{id:"minecraft:skeleton"}}}]}'),
('command_block', '{Command:"say hi",CustomName:\'"Cmd"\',LastOutput:\'{"text":"out"}\'}'),
('end_gateway', '{ExitPortal:{X:1,Y:70,Z:2},ExactTeleport:1b,Age:5L}'),
('chiseled_bookshelf', '{Items:[{Slot:1b,id:"minecraft:book",Count:1b}],last_interacted_slot:1}'),
('campfire', '{Items:[{Slot:0b,id:"minecraft:beef",Count:1b}],CookingTimes:[I;5,0,0,0],CookingTotalTimes:[I;600,0,0,0]}'),
('hopper', '{Items:[{Slot:0b,id:"minecraft:stone",Count:1b}],Lock:"key"}'),
('structure_block', '{name:"x:y",posX:1,posY:2,posZ:3,sizeX:4,sizeY:5,sizeZ:6,mode:"SAVE"}'),
('skeleton_skull', '{}'),
]
if version >= 3682:
out.append(('crafter', '{Items:[{Slot:0b,id:"minecraft:stone",Count:1b}],triggered:1,crafting_ticks_remaining:2}'))
# Blocks the 1.20 experiment brought in 1.19.3 and 1.19.4.
since = {'decorated_pot': 3327, 'suspicious_sand': 3327, 'chiseled_bookshelf': 3218}
out = [(b, versioned(nbt, version)) for b, nbt in out if version >= since.get(b, 0)]
else:
out += [
('white_banner', '{patterns:[{pattern:"minecraft:creeper",color:"black"}],CustomName:%s}' % name("Flag")),
('player_head', '{profile:{name:"Notch"}}'),
('decorated_pot', '{sherds:["minecraft:brick","minecraft:skull_pottery_sherd","minecraft:brick","minecraft:brick"]}'),
('furnace', '{BurnTime:100s,CookTime:20s,CookTimeTotal:200s,Items:[{Slot:0b,id:"minecraft:iron_ore",count:3}]}'
if version < 4181 else '{lit_time_remaining:100s,cooking_time_spent:20s,cooking_total_time:200s}'),
('jukebox', '{RecordItem:{id:"minecraft:music_disc_cat",count:1},ticks_since_song_started:5L}'),
('trial_spawner', '{required_player_range:10}'),
('vault', '{config:{key_item:{id:"minecraft:trial_key",count:1}}}'),
('crafter', '{Items:[{Slot:0b,id:"minecraft:stone",count:1}],triggered:1}'),
('spawner', '{SpawnData:{entity:{id:"minecraft:zombie"}}}'),
('hopper', '{Items:[{Slot:0b,id:"minecraft:stone",count:1}],lock:{items:"minecraft:stick"}}'
if version >= 4068 else '{Items:[{Slot:0b,id:"minecraft:stone",count:1}],Lock:"key"}'),
]
if version >= 4071:
out.append(('creaking_heart', ''))
if version >= 4533:
out.append(('oak_shelf', '{Items:[{Slot:0b,id:"minecraft:apple",count:1}]}'))
if version >= 4532:
out.append(('copper_golem_statue', ''))
return out
# ------------------------------------------------------------------ entities
def entities(e, version):
"""(id, SNBT fields) for summon; item fields in the era's form."""
if e == A:
sword = '{id:"minecraft:iron_sword",Count:1b,tag:{display:{Name:\'"S"\'}}}'
helmet = '{id:"minecraft:iron_helmet",Count:1b}'
out = [
('zombie', f'ArmorItems:[{{}},{{}},{{}},{helmet}],HandItems:[{sword},{{}}],ArmorDropChances:[0.1f,0.1f,0.1f,2.0f],'
'HandDropChances:[1.0f,0.085f],CustomName:\'"Zed"\''),
('villager', 'VillagerData:{profession:"minecraft:farmer",level:2,type:"minecraft:plains"},Xp:3,'
'Offers:{Recipes:[{buy:{id:"minecraft:wheat",Count:20b},buyB:{id:"minecraft:air",Count:1b},'
'sell:{id:"minecraft:emerald",Count:1b},maxUses:16,uses:1}]},Inventory:[{id:"minecraft:bread",Count:3b}]'),
('horse', 'Tame:1b,SaddleItem:{id:"minecraft:saddle",Count:1b},ArmorItem:{id:"minecraft:iron_horse_armor",Count:1b},'
'Attributes:[{Name:"minecraft:horse.jump_strength",Base:0.7d},{Name:"minecraft:generic.max_health",Base:20.0d,'
'Modifiers:[{Name:"m",Amount:1.0d,Operation:0,UUID:[I;5,6,7,8]}]}]'),
('llama', 'ChestedHorse:1b,Items:[{Slot:2b,id:"minecraft:apple",Count:1b}],DecorItem:{id:"minecraft:red_carpet",Count:1b}'),
('donkey', 'ChestedHorse:1b,Items:[{Slot:2b,id:"minecraft:apple",Count:1b}],SaddleItem:{id:"minecraft:saddle",Count:1b}'),
('wolf', 'Health:15.0f,Owner:[I;1,2,3,4],CollarColor:3b'),
('salmon', ''),
('armor_stand', f'ArmorItems:[{{}},{{}},{{}},{helmet}],HandItems:[{sword},{{}}],Small:1b'),
('item_frame', f'Item:{sword},Facing:3b'),
('item', 'Item:{id:"minecraft:written_book",Count:1b,tag:{pages:[\'"p"\'],title:"t",author:"a"}},PickupDelay:10s'),
('arrow', 'inGround:1b,Potion:"minecraft:poison",CustomPotionEffects:[{Id:19b,Duration:100}]'),
('trident', 'Trident:{id:"minecraft:trident",Count:1b},inGround:1b'),
('area_effect_cloud', 'Potion:"minecraft:healing",Particle:"entity_effect",Radius:2.0f,Duration:600'),
('potion', 'Item:{id:"minecraft:splash_potion",Count:1b,tag:{Potion:"minecraft:swiftness"}}'),
('falling_block', 'BlockState:{Name:"minecraft:sand"},Time:-1000,NoGravity:1b'),
('tnt', 'Fuse:80s'),
('tnt_minecart', 'TNTFuse:40'),
('chest_minecart', 'Items:[{Slot:0b,id:"minecraft:apple",Count:1b}]'),
('boat', 'Type:"spruce"'),
('chest_boat', 'Type:"birch",Items:[{Slot:0b,id:"minecraft:apple",Count:1b}]'),
('text_display', 'text:\'{"text":"Hi","bold":true}\''),
('item_display', 'item:{id:"minecraft:stone",Count:1b}'),
('pig', 'Saddle:1b'),
('cat', 'variant:"minecraft:black"'),
('fox', 'Type:"red"'),
('zombie_villager', 'VillagerData:{profession:"minecraft:none",level:1,type:"minecraft:plains"}'),
('wandering_trader', ''),
('piglin', 'Inventory:[{id:"minecraft:gold_ingot",Count:2b}]'),
('firework_rocket', 'FireworksItem:{id:"minecraft:firework_rocket",Count:1b,tag:{Fireworks:{Flight:1b}}}'),
('allay', 'Inventory:[{id:"minecraft:diamond",Count:1b}]'),
('goat', ''),
]
since = {'allay': 3105, 'chest_boat': 3105, 'text_display': 3325, 'item_display': 3325}
out = [(k, x) for k, x in out if version >= since.get(k, 0)]
if version < 3086: # cats before 1.19 have a numeric CatType
out = [(k, 'CatType:1' if k == 'cat' else x) for k, x in out]
if 3105 <= version < 3322: # darkness counts its factor from a timestamp before 1.19.4
out.append(('pig', 'ActiveEffects:[{Id:33b,Amplifier:0b,Duration:200,ShowParticles:1b,'
'FactorCalculationData:{padding_duration:22,factor_start:0.0f,factor_target:1.0f,'
'factor_current:0.0f,effect_changed_timestamp:5,factor_previous_frame:0.0f,'
'had_effect_last_tick:0b}}]'))
return out
sword = '{id:"minecraft:iron_sword",count:1}'
helmet = '{id:"minecraft:iron_helmet",count:1}'
name = lambda s: text(e, s)
if e == B:
out = [
('zombie', f'ArmorItems:[{{}},{{}},{{}},{helmet}],HandItems:[{sword},{{}}],ArmorDropChances:[0.1f,0.1f,0.1f,2.0f],'
f'CustomName:{name("Zed")}'),
('horse', 'Tame:1b,SaddleItem:{id:"minecraft:saddle",count:1},body_armor_item:{id:"minecraft:iron_horse_armor",count:1}'),
('llama', 'ChestedHorse:1b,Items:[{Slot:2b,id:"minecraft:apple",count:1}],body_armor_item:{id:"minecraft:red_carpet",count:1}'),
('area_effect_cloud', 'potion_contents:{potion:"minecraft:healing"},Particle:{type:"minecraft:entity_effect",color:255},Duration:600'),
('potion', 'Item:{id:"minecraft:lingering_potion",count:1}'),
('tnt_minecart', 'TNTFuse:40' if version < 4173 else 'fuse:40'),
('salmon', ''),
('item_frame', f'Item:{sword},Facing:3b'),
('arrow', 'inGround:1b,item:{id:"minecraft:arrow",count:1}'),
('text_display', f'text:{name("Hi")}'),
('villager', 'VillagerData:{profession:"minecraft:farmer",level:2,type:"minecraft:plains"}'),
('pig', 'Saddle:1b'),
('falling_block', 'BlockState:{Name:"minecraft:sand"},Time:-1000,NoGravity:1b'),
]
if version >= 4067:
out.append(('pale_oak_chest_boat' if version >= 4070 else 'oak_chest_boat', 'Items:[{Slot:0b,id:"minecraft:apple",count:1}]'))
else:
out.append(('chest_boat', 'Type:"birch",Items:[{Slot:0b,id:"minecraft:apple",count:1}]'))
return out
out = [
('zombie', f'equipment:{{head:{helmet},mainhand:{sword}}},drop_chances:{{head:2.0f}},CustomName:{name("Zed")}'),
('horse', 'Tame:1b,equipment:{saddle:{id:"minecraft:saddle",count:1},body:{id:"minecraft:iron_horse_armor",count:1}}'),
('area_effect_cloud', 'potion_contents:{potion:"minecraft:healing"},Duration:600'),
('splash_potion' if version >= 4306 else 'potion', 'Item:{id:"minecraft:splash_potion",count:1}'),
('tnt_minecart', 'fuse:40'),
('text_display', f'text:{name("Hi")}'),
('villager', 'VillagerData:{profession:"minecraft:farmer",level:2,type:"minecraft:plains"}'),
('falling_block', 'BlockState:{Name:"minecraft:sand"},Time:-1000,NoGravity:1b'),
('trident', 'item:{id:"minecraft:trident",count:1},inGround:1b'),
]
if version >= 4531:
out.append(('copper_golem', ''))
if version >= 4543:
out.append(('mannequin', ''))
return out
def player_commands(e, name, version=None):
"""Give the joined bot a varied inventory and ender chest."""
out = []
stacks = items(e, version)
for i, stack in enumerate(stacks[:30]):
out.append(f'item replace entity {name} container.{i} with {give_form(e, stack)}')
out.append(f'item replace entity {name} enderchest.0 with {give_form(e, stacks[0])}')
out.append(f'item replace entity {name} armor.head with minecraft:iron_helmet')
out.append(f'xp set {name} 5 levels')
return out
def give_form(e, stack):
"""`item replace ... with` argument from a stack SNBT: id plus tag/components."""
import re
m = re.match(r'\{id:"([^"]+)",(?:Count:(\d+)b|count:(\d+))(?:,(tag|components):(.*))?\}$', stack, re.S)
item, count = m.group(1), m.group(2) or m.group(3)
if m.group(4) == 'tag':
return f'{item}{m.group(5)} {count}'
if m.group(4) == 'components':
body = m.group(5)[1:-1]
parts = split_top(body)
comps = []
for p in parts:
if p.startswith('"'):
end = p.index('"', 1)
key, value = p[1:end], p[end + 1:].lstrip()[1:]
else:
key, value = p.split(':', 1)
comps.append(f'{key}={value.strip()}')
comps = ','.join(comps)
return f'{item}[{comps}] {count}'
return f'{item} {count}'
def split_top(s):
"""Split SNBT compound contents at top-level commas."""
out, depth, cur, q = [], 0, '', None
for ch in s:
if q:
cur += ch
if ch == q:
q = None
continue
if ch in '"\'':
q = ch
elif ch in '{[':
depth += 1
elif ch in '}]':
depth -= 1
elif ch == ',' and depth == 0:
out.append(cur)
cur = ''
continue
cur += ch
if cur:
out.append(cur)
return out
+342
View File
@@ -0,0 +1,342 @@
#!/usr/bin/env python3
"""Make sample worlds with older vanilla servers, for the DataFixer harness.
For each listed version, the official server jar (cached under
.local/datafix/servers/VERSION/server.jar, beside the shared vanilla data) generates a default world around spawn,
gets a scene of block entities, entities and items in that version's own save
format (tools/datafix_scene.py), and a bot player joins (with its inventory filled)
so that player data exists. The saved world is kept in datafix/worlds/VERSION beside the shared vanilla data
(.local/datafix in a plain checkout), where every worktree finds it.
datafix_worlds.py [--force] [VERSION...] (default: every version below)
"""
import argparse
import hashlib
import json
import os
import queue
import re
import shutil
import socket
import struct
import subprocess
import sys
import threading
import time
import urllib.request
import uuid
import zipfile
import zlib
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
import datafix_scene as scene # noqa: E402
ROOT = Path(__file__).resolve().parent.parent
LOCAL = ROOT / '.local' / 'datafix'
# Old server jars and their worlds are inputs, not outputs: keep them beside the
# vanilla data, which parallel worktrees share (.local/minecraft links there).
SHARED = (ROOT / '.local' / 'minecraft').resolve().parent / 'datafix'
VERSIONS = ['1.18.2', '1.19', '1.19.2', '1.19.3', '1.19.4', '1.20.1', '1.20.2', '1.20.4', '1.20.5', '1.20.6', '1.21.1',
'1.21.3', '1.21.4', '1.21.5', '1.21.6', '1.21.8', '1.21.9', '1.21.10', '1.21.11', '26.1', '26.1.2', '26.2']
MANIFEST = 'https://piston-meta.mojang.com/mc/game/version_manifest_v2.json'
BOT = 'Upgrader'
UUID_BASE = 0x6000
def server_jar(version):
path = SHARED / 'servers' / version / 'server.jar'
if not path.exists():
path.parent.mkdir(parents=True, exist_ok=True)
manifest = json.load(urllib.request.urlopen(MANIFEST))
entry = next(v for v in manifest['versions'] if v['id'] == version)
meta = json.load(urllib.request.urlopen(entry['url']))
download = meta['downloads']['server']
data = urllib.request.urlopen(download['url']).read()
if hashlib.sha1(data).hexdigest() != download['sha1']:
sys.exit(f'{version}: server.jar checksum mismatch')
path.write_bytes(data)
info = json.loads(zipfile.ZipFile(path).read('version.json'))
return path, info['world_version'], info['protocol_version']
class Server:
def __init__(self, directory, jar, port, packs=()):
directory.mkdir(parents=True, exist_ok=True)
(directory / 'eula.txt').write_text('eula=true\n')
props = {'server-port': port, 'online-mode': 'false', 'level-name': 'world', 'level-seed': '4242',
'view-distance': '4', 'simulation-distance': '4', 'spawn-protection': '0', 'sync-chunk-writes': 'true',
'enable-rcon': 'false', 'enable-query': 'false', 'max-tick-time': '-1'}
if packs:
props['initial-enabled-packs'] = ','.join(['vanilla', *packs])
(directory / 'server.properties').write_text(''.join(f'{k}={v}\n' for k, v in props.items()))
self.process = subprocess.Popen(['java', '-Xmx2G', '-jar', str(jar), 'nogui'], cwd=directory,
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
self.lines = []
self.queue = queue.Queue()
self.counter = 0
threading.Thread(target=self._pump, daemon=True).start()
self.wait('Done (', 600)
def _pump(self):
for line in self.process.stdout:
line = line.rstrip('\n')
self.lines.append(line)
self.queue.put(line)
def wait(self, pattern, timeout=60):
end = time.time() + timeout
seen = []
while time.time() < end:
try:
line = self.queue.get(timeout=1)
except queue.Empty:
if self.process.poll() is not None:
break
continue
seen.append(line)
if pattern in line:
return seen
raise TimeoutError(f'no "{pattern}" in {timeout}s; last lines: {seen[-5:]}')
def run(self, command, timeout=60):
self.counter += 1
marker = f'@@{self.counter}@@'
self.process.stdin.write(command + '\nsay ' + marker + '\n')
self.process.stdin.flush()
out = self.wait(marker, timeout)
return [re.sub(r'^\[[0-9:]+\] \[[^\]]*\]: ', '', l) for l in out[:-1]]
def stop(self):
try:
self.process.stdin.write('stop\n')
self.process.stdin.flush()
self.process.wait(120)
except Exception:
self.process.kill()
def failed(lines):
return [l for l in lines if re.search(r'(Unknown|Expected|Incorrect|Invalid|Could not|Unable|Failed|That block|'
r'error|Malformed|Can\'t)', l, re.I)]
# ------------------------------------------------------------------ minimal bot
def varint(v):
out = b''
v &= 0xFFFFFFFF
while True:
b = v & 0x7F
v >>= 7
out += bytes([b | (0x80 if v else 0)])
if not v:
return out
def mcstring(s):
data = s.encode()
return varint(len(data)) + data
def parse_varint(data, pos):
value = shift = 0
while True:
b = data[pos]
pos += 1
value |= (b & 0x7F) << shift
shift += 7
if not b & 0x80:
return pos, value
def offline_uuid(name):
digest = bytearray(hashlib.md5(('OfflinePlayer:' + name).encode()).digest())
digest[6] = (digest[6] & 0x0F) | 0x30
digest[8] = (digest[8] & 0x3F) | 0x80
return uuid.UUID(bytes=bytes(digest))
class Bot:
"""Logs in (offline mode) with the given protocol and stays until told to leave."""
def __init__(self, port, protocol, name):
self.sock = socket.create_connection(('127.0.0.1', port), timeout=30)
self.threshold = -1
self.buffer = b''
self.protocol = protocol
self.stop = threading.Event()
self.send(0, varint(protocol) + mcstring('127.0.0.1') + struct.pack('>H', port) + varint(2))
if protocol <= 758: # 1.18.2: the name only
self.send(0, mcstring(name))
elif protocol == 759: # 1.19: no signature data
self.send(0, mcstring(name) + b'\x00')
elif protocol == 760: # 1.19.2: no signature data, optional UUID
self.send(0, mcstring(name) + b'\x00' + b'\x01' + offline_uuid(name).bytes)
elif protocol <= 763: # 1.19.3 - 1.20.1: optional UUID
self.send(0, mcstring(name) + b'\x01' + offline_uuid(name).bytes)
else:
self.send(0, mcstring(name) + offline_uuid(name).bytes)
while True:
pid, data = self.receive()
if pid == 3:
self.threshold = parse_varint(data, 0)[1]
elif pid == 2:
break
elif pid == 0:
raise RuntimeError(f'login refused: {data!r}')
if protocol >= 764:
self.send(3) # login acknowledged -> configuration
self.configure()
threading.Thread(target=self.idle, daemon=True).start()
def configure(self):
old = self.protocol < 766 # 1.20.2 - 1.20.4 packet ids
finish, keep_alive, ping, disconnect = (2, 3, 4, 1) if old else (3, 4, 5, 2)
while True:
pid, data = self.receive()
if pid == finish:
self.send(2 if old else 3)
return
if pid == keep_alive:
self.send(3 if old else 4, data)
elif pid == ping:
self.send(4 if old else 5, data)
elif pid == disconnect:
raise RuntimeError(f'disconnected during configuration: {data!r}')
elif not old and self.known_packs(data):
self.send(7, varint(0)) # claim no packs: the server sends full registries
@staticmethod
def known_packs(data):
try:
pos, count = parse_varint(data, 0)
if count < 1:
return False
pos, n = parse_varint(data, pos)
return data[pos:pos + n] == b'minecraft'
except IndexError:
return False
def idle(self):
self.sock.settimeout(1.0)
while not self.stop.is_set():
try:
self.receive()
except socket.timeout:
continue
except (EOFError, OSError):
break
def leave(self):
self.stop.set()
time.sleep(1.5)
self.sock.close()
def read_exact(self, n):
while len(self.buffer) < n:
chunk = self.sock.recv(65536)
if not chunk:
raise EOFError('server closed the connection')
self.buffer += chunk
out, self.buffer = self.buffer[:n], self.buffer[n:]
return out
def read_varint(self):
value = shift = 0
while True:
b = self.read_exact(1)[0]
value |= (b & 0x7F) << shift
shift += 7
if not b & 0x80:
return value
def send(self, packet_id, payload=b''):
body = varint(packet_id) + payload
if self.threshold >= 0:
body = varint(len(body)) + zlib.compress(body) if len(body) >= self.threshold else varint(0) + body
self.sock.sendall(varint(len(body)) + body)
def receive(self):
length = self.read_varint()
body = self.read_exact(length)
if self.threshold >= 0:
pos, size = parse_varint(body, 0)
body = zlib.decompress(body[pos:]) if size else body[pos:]
pos, packet_id = parse_varint(body, 0)
return packet_id, body[pos:]
# ------------------------------------------------------------------ world
def uuid_nbt(i):
return f'[I;{UUID_BASE},0,0,{i}]'
def build(version, force=False):
jar, data_version, protocol = server_jar(version)
target = SHARED / 'worlds' / version
if target.exists() and not force:
print(f'{version}: kept {target}')
return []
era = scene.era(data_version)
work = LOCAL / 'gen' / version
shutil.rmtree(work, ignore_errors=True)
port = 30000 + (os.getpid() + data_version) % 20000
server = Server(work, jar, port, scene.experiments(data_version))
problems = []
def run(command):
out = server.run(command)
bad = failed(out)
if bad:
problems.append((command[:160], bad[:2]))
return out
run('forceload add -48 -48 79 79')
run('fill 0 99 0 47 99 47 minecraft:stone')
run('fill 0 100 0 47 112 47 minecraft:air')
for i, (block, nbt) in enumerate(scene.block_entities(era, data_version)):
x, z = 1 + 3 * (i % 15), 1 + 3 * (i // 15)
run(f'setblock {x} 100 {z} minecraft:{block}{nbt}')
for i, (kind, extra) in enumerate(scene.entities(era, data_version)):
x, z = 1.5 + 3 * (i % 15), 25.5 + 3 * (i // 15)
fields = f'UUID:{uuid_nbt(i)},NoAI:1b,Silent:1b,NoGravity:1b,Invulnerable:1b,PersistenceRequired:1b'
run(f'summon minecraft:{kind} {x} 100 {z} {{{fields}' + (',' + extra if extra else '') + '}')
run('setworldspawn 20 101 20')
bot = Bot(port, protocol, BOT)
time.sleep(3)
for command in scene.player_commands(era, BOT, data_version):
run(command)
time.sleep(3)
run('save-all flush')
time.sleep(2)
bot.leave()
time.sleep(2)
server.stop()
# Copy aside, then rename: an interrupted copy must not look like a finished world.
partial = target.with_name(target.name + '.partial')
shutil.rmtree(partial, ignore_errors=True)
target.parent.mkdir(parents=True, exist_ok=True)
shutil.copytree(work / 'world', partial)
(partial / 'datafix-source.json').write_text(json.dumps(
{'version': version, 'data_version': data_version, 'problems': problems}, indent=1))
shutil.rmtree(target, ignore_errors=True)
partial.rename(target)
for command, out in problems:
print(f'{version}: command failed: {command}\n {out}')
print(f'{version}: DataVersion {data_version}, world in {target}')
return problems
def main():
p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
p.add_argument('versions', nargs='*')
p.add_argument('--force', action='store_true')
args = p.parse_args()
for version in args.versions or VERSIONS:
build(version, args.force)
if __name__ == '__main__':
main()
+222
View File
@@ -0,0 +1,222 @@
#!/usr/bin/env python3
"""DataFixer differential test: worlds saved by older game versions, upgraded by
vanilla 26.3 and by Horizon, must give the same data.
For every sample world in datafix/worlds beside the shared vanilla data (made by
tools/datafix_worlds.py with the official server jars of 1.18.2 to 26.2, and
generated automatically when missing), two upgrades run on a copy:
vanilla VanillaDataFixOracle: FileFixerUpper, then DataFixers on level.dat, every
chunk, entity chunk and POI chunk, player, saved data, advancements and
statistics file, exactly as 26.3 does when it opens the world
horizon the `datafix_world` example of horizon-world, which does the same with
horizon_world::datafix
Then every file is compared: region files chunk by chunk, NBT files and JSON
files value by value (compound and object key order does not matter). The only
tolerated differences are listed in IGNORED (wall-clock time).
test_datafix_parity.py [--keep] [--skip-build] [VERSION...]
"""
import argparse
import json
import os
import re
import shutil
import subprocess
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
import anvil_dump # noqa: E402
ROOT = Path(__file__).resolve().parent.parent
LOCAL = ROOT / '.local' / 'datafix'
WORLDS = (ROOT / '.local' / 'minecraft').resolve().parent / 'datafix' / 'worlds'
RUN = LOCAL / 'run'
IGNORED = {
r'^level\.dat\.Data\.LastPlayed$': 'wall-clock time of the upgrade',
}
def java_classpath():
import oracle_harness as harness
import test_rust_parity as base
import test_model_baking_parity as baking
lock, jars, _ = base.verify_artifacts()
libraries = baking.client_libraries()
build = LOCAL / 'build'
build.mkdir(parents=True, exist_ok=True)
classes, dependencies, _ = harness.compile_oracle(build, 'VanillaDataFixOracle', jars, libraries,
lock['minecraft']['java_major'])
return str(classes) + os.pathsep + dependencies
def build_rust():
subprocess.run(['cargo', 'build', '--locked', '--release', '-p', 'horizon-world', '--example', 'datafix_world'],
cwd=ROOT, check=True)
meta = json.loads(subprocess.check_output(['cargo', 'metadata', '--locked', '--no-deps', '--format-version', '1'],
cwd=ROOT))
return Path(meta['target_directory']) / 'release' / 'examples' / 'datafix_world'
def run_vanilla(classpath, world, out):
shutil.rmtree(out, ignore_errors=True)
log = out.parent / (out.name + '.log')
with log.open('w') as f:
subprocess.run(['java', '-Xmx3g', '-Djava.awt.headless=true', '-cp', classpath,
'dev.horizonlab.parity.VanillaDataFixOracle', str(world), str(out)],
check=True, stdout=f, stderr=subprocess.STDOUT)
def run_horizon(binary, world, out):
shutil.rmtree(out, ignore_errors=True)
log = out.parent / (out.name + '.log')
with log.open('w') as f:
subprocess.run([str(binary), str(world), str(out)], check=True, stdout=f, stderr=subprocess.STDOUT)
# ---------------------------------------------------------------- comparison
def norm(v):
"""Python values from anvil_dump, with 26.3 mixed-list wrappers removed."""
if isinstance(v, dict):
return {k: norm(x) for k, x in v.items()}
if isinstance(v, list):
return [norm(x[''] if isinstance(x, dict) and list(x) == [''] else x) for x in v]
return v
def diff(a, b, path, out):
if type(a) is not type(b):
out.append((path, a, b))
elif isinstance(a, dict):
for k in sorted(set(a) | set(b), key=str):
if k not in a:
out.append((f'{path}.{k}', None, b[k]))
elif k not in b:
out.append((f'{path}.{k}', a[k], None))
else:
diff(a[k], b[k], f'{path}.{k}', out)
elif isinstance(a, list):
if len(a) != len(b):
out.append((f'{path}[len]', len(a), len(b)))
for i, (x, y) in enumerate(zip(a, b)):
diff(x, y, f'{path}[{i}]', out)
elif a != b:
out.append((path, a, b))
def read_json(path):
return json.loads(path.read_text(encoding='utf-8'))
def files(world):
out = set()
for p in world.rglob('*'):
if p.is_file() and p.name not in ('session.lock', 'datafix-source.json') and not p.name.endswith('_old'):
out.add(p.relative_to(world))
return out
def compare(vanilla, horizon):
report = []
a, b = files(vanilla), files(horizon)
for rel in sorted(a | b, key=str):
name = str(rel)
if rel not in b:
report.append((name, 'present', 'missing from Horizon'))
continue
if rel not in a:
report.append((name, 'missing from vanilla', 'present'))
continue
va, ha = vanilla / rel, horizon / rel
if name.endswith('.mca'):
vc, hc = anvil_dump.region_chunks(str(va)), anvil_dump.region_chunks(str(ha))
for key in sorted(set(vc) | set(hc)):
label = f'{name}{list(key)}'
if key not in hc:
report.append((label, 'present', 'missing from Horizon'))
elif key not in vc:
report.append((label, 'missing from vanilla', 'present'))
else:
out = []
diff(norm(vc[key]), norm(hc[key]), 'chunk', out)
report.extend((f'{p} @{label}', x, y) for p, x, y in out)
elif name.endswith('.dat'):
out = []
diff(norm(anvil_dump.read_dat(str(va))), norm(anvil_dump.read_dat(str(ha))), name, out)
report.extend(out)
elif name.endswith('.json'):
out = []
diff(read_json(va), read_json(ha), name, out)
report.extend(out)
elif va.read_bytes() != ha.read_bytes():
report.append((name, 'bytes differ', ''))
return report
def pattern_of(path):
return re.sub(r'\[\d+\]', '[]', path.split(' @')[0])
def summarize(report):
groups = {}
for path, a, b in report:
groups.setdefault(pattern_of(path), []).append((path, a, b))
failures = 0
for pattern, items in groups.items():
why = next((w for p, w in IGNORED.items() if re.search(p, pattern)), None)
if why:
print(f' ignored {pattern} x{len(items)} ({why})')
continue
failures += len(items)
path, a, b = items[0]
short = lambda v: (s := json.dumps(v, default=str))[:200] + ('...' if len(s) > 200 else '')
print(f' DIFF {pattern} x{len(items)}\n e.g. {path}\n vanilla {short(a)}\n horizon {short(b)}')
return failures
def main():
p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
p.add_argument('versions', nargs='*')
p.add_argument('--keep', action='store_true')
p.add_argument('--vanilla-only', action='store_true', help='only run the oracle')
p.add_argument('--binary', help='a prebuilt datafix_world example')
args = p.parse_args()
import datafix_worlds
import fcntl
WORLDS.parent.mkdir(parents=True, exist_ok=True)
with open(WORLDS.parent / 'generate.lock', 'w') as lock:
fcntl.flock(lock, fcntl.LOCK_EX) # another worktree may be generating them
missing = [v for v in datafix_worlds.VERSIONS if not (WORLDS / v / 'level.dat').exists()]
if missing:
print('generating sample worlds:', ' '.join(missing), flush=True)
subprocess.run([sys.executable, str(ROOT / 'tools' / 'datafix_worlds.py'), *missing], cwd=ROOT, check=True)
worlds = sorted(p.name for p in WORLDS.iterdir() if p.is_dir())
versions = args.versions or worlds
classpath = java_classpath()
binary = None if args.vanilla_only else Path(args.binary) if args.binary else build_rust()
total = 0
for version in versions:
world = WORLDS / version
source = json.loads((world / 'datafix-source.json').read_text())
print(f'{version} (DataVersion {source["data_version"]})')
RUN.mkdir(parents=True, exist_ok=True)
run_vanilla(classpath, world, RUN / f'{version}-vanilla')
if binary is None:
continue
run_horizon(binary, world, RUN / f'{version}-horizon')
failures = summarize(compare(RUN / f'{version}-vanilla', RUN / f'{version}-horizon'))
print(f' {version}: {"ok" if not failures else f"{failures} differences"}')
total += failures
if binary is not None:
print('datafix parity', 'FAILED' if total else 'ok', f'({total} differences over {len(versions)} worlds)')
if not total and not args.keep:
shutil.rmtree(RUN, ignore_errors=True)
sys.exit(1 if total else 0)
if __name__ == '__main__':
main()
+4 -1
View File
@@ -547,7 +547,10 @@ def main():
failures += 1
volatile = re.compile(r'(cooking_time_spent|lit_time_remaining|ticks_since_song_started|Age|BrewTime|'
r'ticks_in_hive|triggered|state_updating_resumes_at|LastExecution|last_vibration_frequency|warning_level): -?[0-9]+[a-zA-Z]?')
volatile2 = re.compile(r'(RecipesUsed: \{[^}]*\}|listener: \{.*\}(?=, x))')
# A lit campfire keeps cooking while each server runs before `data get`: its
# progress counters advance by however many ticks that took (stage 3 checks
# that Horizon saves them exactly as loaded).
volatile2 = re.compile(r'(RecipesUsed: \{[^}]*\}|listener: \{.*\}(?=, x)|CookingTimes: \[I;[^\]]*\])')
# The lit furnace keeps smelting while each game runs: its slots move on.
volatile_items = re.compile(r'Items: \[[^\]]*\]')
for label in views0: