diff --git a/crates/horizon-client/src/flatgen.rs b/crates/horizon-client/src/flatgen.rs index d7764c30e..2ba62a56d 100644 --- a/crates/horizon-client/src/flatgen.rs +++ b/crates/horizon-client/src/flatgen.rs @@ -44,6 +44,105 @@ impl FlatTerrain { crate::chunk::MIN_Y + self.layers.len().min(crate::chunk::HEIGHT as usize) as i32 } } +/// DimensionType.Y_SIZE: the most layers FlatLevelGeneratorSettings takes. +const Y_SIZE: i64 = 4064; + +/// FlatLevelGeneratorSettings.CODEC reading a dedicated server's +/// `generator-settings` (DedicatedServerProperties.WorldDimensionData for +/// the `minecraft:flat` preset): the settings as `world_gen_settings` stores +/// them, and the error vanilla logs (`resultOrPartial`). No settings means +/// the preset's own layers; a layer that doesn't parse is left out, as the +/// codec's partial result leaves it. +/// `is_block` says whether a block id is registered; `biome` resolves a biome +/// id the way the lenient codec does (Plains when it isn't known). +pub fn settings_from_json( + json: &crate::server::json::Json, + is_block: impl Fn(&str) -> bool, + biome: impl Fn(&str) -> String, +) -> (Option, Option) { + use crate::server::json::Json; + let id = |s: &str| { + if s.contains(':') { + s.to_owned() + } else { + format!("minecraft:{s}") + } + }; + let Some(Json::Array(list)) = json.get("layers") else { + let error = match json.get("layers") { + None => format!("No key layers in MapLike[{}]", json.compact()), + Some(v) => format!("Not a list: {}", v.compact()), + }; + return (None, Some(error)); + }; + let mut errors = Vec::new(); + let mut layers = Vec::new(); + let mut total = 0; + for layer in list { + let height = layer.get("height").and_then(Json::as_string); + let height = height.as_deref().and_then(|h| h.parse::().ok()); + let block = layer.get("block").and_then(Json::as_string).map(|b| id(&b)); + match (height, block) { + (Some(h), _) if !(0..=Y_SIZE).contains(&h) => { + errors.push(format!("Value {h} outside of range [0:{Y_SIZE}]")); + } + (Some(h), Some(b)) if is_block(&b) => { + total += h; + layers.push(Tag::compound([ + ("height", Tag::Int(h as i32)), + ("block", Tag::String(b.as_str().into())), + ])); + } + (Some(_), Some(b)) => errors.push(format!( + "Unknown registry key in ResourceKey[minecraft:root / minecraft:block]: {b}" + )), + (None, _) => errors.push(format!("No key height in MapLike[{}]", layer.compact())), + (_, None) => errors.push(format!("No key block in MapLike[{}]", layer.compact())), + } + } + // validateHeight. + if total > Y_SIZE { + errors.push(format!("Sum of layer heights is > {Y_SIZE}")); + return (None, Some(errors.join("; "))); + } + let flag = |key: &str| match json.get(key) { + Some(Json::Bool(b)) => *b, + Some(Json::Number(n)) => n.parse::().is_ok_and(|v| v != 0.0), + _ => false, + }; + let biome = biome( + &json + .get("biome") + .and_then(Json::as_string) + .map_or_else(|| "minecraft:plains".to_owned(), |b| id(&b)), + ); + let mut settings = Vec::new(); + // RegistryCodecs.holderSet, lenient too: a tag or a list of ids. + match json.get("structure_overrides") { + Some(Json::String(s)) => { + settings.push(("structure_overrides", Tag::String(s.as_str().into()))) + } + Some(Json::Array(ids)) if ids.iter().all(|i| matches!(i, Json::String(_))) => settings + .push(( + "structure_overrides", + Tag::List( + ids.iter() + .filter_map(Json::as_string) + .map(|i| Tag::String(id(&i).as_str().into())) + .collect(), + ), + )), + _ => {} + } + settings.extend([ + ("layers", Tag::List(layers)), + ("lakes", Tag::Byte(i8::from(flag("lakes")))), + ("features", Tag::Byte(i8::from(flag("features")))), + ("biome", Tag::String(biome.as_str().into())), + ]); + let error = (!errors.is_empty()).then(|| errors.join("; ")); + (Some(Tag::compound(settings)), error) +} impl ChunkGenerator for FlatTerrain { type Worker = (); fn worker(&self) {} @@ -75,3 +174,73 @@ impl ChunkGenerator for FlatTerrain { *proto.downcast::().expect("flat terrain proto") } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::server::json::{self, Json}; + + fn read(text: &str) -> (Option, Option) { + let json: Json = json::parse(text, false).unwrap(); + settings_from_json( + &json, + |b| ["minecraft:stone", "minecraft:bedrock"].contains(&b), + |b| { + if b == "minecraft:desert" { + b + } else { + "minecraft:plains" + } + .to_owned() + }, + ) + } + + #[test] + fn generator_settings_read_as_flat_level_generator_settings() { + // server.properties' default: the preset's own layers, and the codec's error. + assert_eq!( + read("{}"), + (None, Some("No key layers in MapLike[{}]".into())) + ); + let (settings, error) = read( + r#"{"layers":[{"block":"bedrock","height":1},{"block":"minecraft:stone","height":3}],"biome":"desert","features":true}"#, + ); + assert_eq!(error, None); + let settings = settings.unwrap(); + let Some(Tag::List(layers)) = settings.get("layers") else { + panic!("{settings:?}") + }; + assert_eq!(layers.len(), 2); + assert_eq!( + crate::save::text(layers[0].get("block")).as_deref(), + Some("minecraft:bedrock") + ); + assert_eq!(crate::save::number(layers[1].get("height")), Some(3)); + assert_eq!( + crate::save::text(settings.get("biome")).as_deref(), + Some("minecraft:desert") + ); + assert_eq!(crate::save::number(settings.get("features")), Some(1)); + assert_eq!(crate::save::number(settings.get("lakes")), Some(0)); + // A layer that doesn't parse is left out; an unknown biome is Plains. + let (settings, error) = read( + r#"{"layers":[{"block":"nope","height":2},{"block":"stone","height":2}],"biome":"x"}"#, + ); + assert!(error.unwrap().contains("minecraft:nope")); + let settings = settings.unwrap(); + let Some(Tag::List(layers)) = settings.get("layers") else { + panic!() + }; + assert_eq!(layers.len(), 1); + assert_eq!( + crate::save::text(settings.get("biome")).as_deref(), + Some("minecraft:plains") + ); + // validateHeight. + assert_eq!( + read(r#"{"layers":[{"block":"stone","height":4000},{"block":"stone","height":100}]}"#), + (None, Some("Sum of layer heights is > 4064".into())) + ); + } +} diff --git a/crates/horizon-client/src/game/dedicated.rs b/crates/horizon-client/src/game/dedicated.rs index 33bedf696..3f4e28494 100644 --- a/crates/horizon-client/src/game/dedicated.rs +++ b/crates/horizon-client/src/game/dedicated.rs @@ -290,6 +290,78 @@ impl Lists { } } +/// The world presets of 26.3's `worldgen/world_preset`. +const WORLD_PRESETS: [&str; 6] = [ + "minecraft:normal", + "minecraft:flat", + "minecraft:large_biomes", + "minecraft:amplified", + "minecraft:single_biome_surface", + "minecraft:debug_all_block_states", +]; + +/// Identifier.tryParse: `namespace:path`, or a path in `minecraft`. +fn parse_identifier(text: &str) -> Option { + let (namespace, path) = text.split_once(':').unwrap_or(("minecraft", text)); + let valid = |s: &str, extra: &[char]| { + s.chars() + .all(|c| matches!(c, 'a'..='z' | '0'..='9' | '_' | '.' | '-') || extra.contains(&c)) + }; + (valid(namespace, &[]) && valid(path, &['/'])).then(|| format!("{namespace}:{path}")) +} + +impl Game<'_> { + /// DedicatedServerProperties.createDimensions for a world that doesn't + /// exist yet: `level-type`'s preset, a flat world's `generator-settings` + /// and `generate-structures`, written before the world opens so that + /// streaming reads them. Horizon generates the normal and flat presets. + fn write_world_gen(&self, dir: &std::path::Path, props: &ServerProperties) -> Result<()> { + let level_type = &props.level_type; + let preset = parse_identifier(level_type) + .filter(|id| WORLD_PRESETS.contains(&id.as_str())) + .unwrap_or_else(|| { + log_from( + "Worker-Main-1", + "WARN", + &format!( + "Failed to parse level-type {level_type}, defaulting to minecraft:normal" + ), + ); + "minecraft:normal".to_owned() + }); + let flat = preset == "minecraft:flat"; + if !flat && preset != "minecraft:normal" { + log_from( + "Worker-Main-1", + "WARN", + &format!( + "Horizon doesn't generate the {preset} world preset yet: using minecraft:normal" + ), + ); + } + let settings = if flat { + let biomes = &self.res.biomes; + let (settings, error) = horizon_client::flatgen::settings_from_json( + &props.generator_settings, + |b| self.pack.assets.registry.default_state(b).is_some(), + |b| biomes.get(biomes.id(b)).name.clone(), + ); + if let Some(e) = error { + log_from("Worker-Main-1", "ERROR", &e); + } + settings + } else { + None + }; + let options = horizon_client::save::WorldGen { + flat, + structures: props.generate_structures, + bonus_chest: false, + }; + horizon_client::save::write_world_gen_settings_with(dir, props.seed, options, settings) + } +} + /// PlayerList.BAN_DATE_FORMAT ("yyyy-MM-dd 'at' HH:mm:ss z"), in UTC. fn ban_date(millis: i64) -> String { let s = format_date(millis); @@ -345,6 +417,13 @@ impl Game<'_> { .get() .and_then(|m| m.lock().unwrap().take()) .ok_or("dedicated server not prepared")?; + // Main's WorldLoader: a new world's dimensions come from + // server.properties before the server starts. + let world_dir = std::env::current_dir()?.join(&universe).join(&level_name); + let new_world = !world_dir.join("level.dat").exists(); + if new_world { + self.write_world_gen(&world_dir, &props)?; + } // The console thread starts first, as in initServer. let (tx, console) = mpsc::channel(); std::thread::Builder::new() @@ -425,9 +504,7 @@ impl Game<'_> { lists.enforce_whitelist = props.enforce_whitelist; let lists = Arc::new(Mutex::new(lists)); log("INFO", &format!("Preparing level \"{level_name}\"")); - let world_dir = std::env::current_dir()?.join(&universe).join(&level_name); let folder = world_dir.to_string_lossy().into_owned(); - let new_world = !world_dir.join("level.dat").exists(); self.open_world( &folder, Some(&level_name), diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index 4411a353c..6eccf29bd 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -1980,40 +1980,51 @@ impl Default for WorldGen { } /// Writes a new world's `world_gen_settings` (seed, options and dimensions). pub fn write_world_gen_settings(dir: &Path, seed: i64, options: WorldGen) -> Result<()> { + write_world_gen_settings_with(dir, seed, options, None) +} +/// [`write_world_gen_settings`] with a flat overworld's own generator +/// settings (FlatLevelGeneratorSettings, as `flatgen::settings_from_json` +/// reads them) instead of the Superflat preset's classic layers. +pub fn write_world_gen_settings_with( + dir: &Path, + seed: i64, + options: WorldGen, + flat_settings: Option, +) -> Result<()> { fs::create_dir_all(dir)?; // worldgen/world_preset/flat.json's overworld: the classic flat layers. let layer = |block: &str, height: i32| { Tag::compound([("block", string(block)), ("height", Tag::Int(height))]) }; + let classic = || { + Tag::compound([ + ("biome", string("minecraft:plains")), + ("features", Tag::Byte(0)), + ("lakes", Tag::Byte(0)), + ( + "layers", + Tag::List(vec![ + layer("minecraft:bedrock", 1), + layer("minecraft:dirt", 2), + layer("minecraft:grass_block", 1), + ]), + ), + ( + "structure_overrides", + Tag::List(vec![ + string("minecraft:strongholds"), + string("minecraft:villages"), + ]), + ), + ]) + }; let flat = Tag::compound([ ("type", string("minecraft:overworld")), ( "generator", Tag::compound([ ("type", string("minecraft:flat")), - ( - "settings", - Tag::compound([ - ("biome", string("minecraft:plains")), - ("features", Tag::Byte(0)), - ("lakes", Tag::Byte(0)), - ( - "layers", - Tag::List(vec![ - layer("minecraft:bedrock", 1), - layer("minecraft:dirt", 2), - layer("minecraft:grass_block", 1), - ]), - ), - ( - "structure_overrides", - Tag::List(vec![ - string("minecraft:strongholds"), - string("minecraft:villages"), - ]), - ), - ]), - ), + ("settings", flat_settings.unwrap_or_else(classic)), ]), ), ]); diff --git a/tools/test_server_world_gen_parity.py b/tools/test_server_world_gen_parity.py new file mode 100644 index 000000000..0ad0ed25e --- /dev/null +++ b/tools/test_server_world_gen_parity.py @@ -0,0 +1,132 @@ +#!/usr/bin/env python3 +"""A new world's generator settings from server.properties, against vanilla's dedicated server. + +DedicatedServerProperties.WorldDimensionData makes a world that doesn't exist yet from `level-type` (a world +preset: `flat`, `minecraft:normal`, or a name it can't find, which it warns about and replaces with +`minecraft:normal`), a flat world's `generator-settings` (FlatLevelGeneratorSettings: its layers, biome, +lakes and features; a layer that doesn't parse is left out and logged, and settings without layers fall back +to the preset's classic ones) and `generate-structures`. + +Each case starts vanilla's server and Horizon's (`horizon-client --dedicated --nogui`) in a new folder, waits +for `Done` and stops them. Compared exactly: the world's `data/minecraft/world_gen_settings.dat`, tag types +included, and the warnings and errors logged while the world's settings are read (the `Worker-Main` lines; +the worker's number is left out). + +Horizon generates the normal and flat presets only: `large_biomes`, `amplified`, `single_biome_surface` and +`debug_all_block_states` aren't tested. +""" +import json +import os +from pathlib import Path +import platform +import re +import shutil +import subprocess +import sys + +try: + from . import anvil_dump, lab, test_rust_parity as base + from .test_dedicated_server_parity import Server, free_port +except ImportError: + import anvil_dump + import lab + import test_rust_parity as base + from test_dedicated_server_parity import Server, free_port + +NAME = 'server-world-gen-parity' +PROPERTIES = { + 'online-mode': 'false', + 'view-distance': '2', + 'simulation-distance': '2', + 'spawn-protection': '0', +} +CASES = { + # server.properties' default generator-settings ({}) has no layers: the preset's classic ones. + 'flat': {'level-type': 'flat', 'level-seed': '1'}, + 'flat-layers': { + 'level-type': 'minecraft:flat', + 'level-seed': '-7', + 'generator-settings': json.dumps({ + 'layers': [{'block': 'minecraft:bedrock', 'height': 1}, {'block': 'stone', 'height': 10}, + {'block': 'minecraft:no_such_block', 'height': 2}, {'block': 'sand', 'height': 3}], + 'biome': 'minecraft:desert', 'lakes': True, 'features': True}, separators=(',', ':')), + }, + 'flat-biome': { + 'level-type': 'FLAT', + 'level-seed': 'flat', + 'generator-settings': '{"layers":[{"block":"minecraft:glass","height":5}],"biome":"nowhere"}', + }, + 'normal': {'level-type': 'minecraft:normal', 'level-seed': 'abc', 'generate-structures': 'false'}, + 'unknown': {'level-type': 'Some Type', 'level-seed': '42'}, +} +WORKER = re.compile(r'\[Worker-Main-\d+/(WARN|ERROR)\]: (.*)') + + +def escape(value): + """java.util.Properties.store's escaping of a value.""" + return value.replace('\\', '\\\\').replace(':', '\\:').replace('=', '\\=').replace('#', '\\#') + + +def run_case(label, command, build, case): + directory = build / label / case + if directory.exists(): + shutil.rmtree(directory) + directory.mkdir(parents=True) + props = dict(PROPERTIES, **CASES[case], **{'server-port': str(free_port())}) + (directory / 'server.properties').write_text(''.join(f'{k}={escape(v)}\n' for k, v in props.items()), + encoding='iso-8859-1') + (directory / 'eula.txt').write_text('eula=true\n') + server = Server(command, directory, f'{label}-{case}') + try: + server.wait_for(r'Done \(') + finally: + server.stop() + out = [f'# {case}'] + out += [f'{m.group(1)}: {m.group(2)}' for l in server.lines if (m := WORKER.search(l))] + settings = anvil_dump.plain(anvil_dump.read_dat(directory / 'world/data/minecraft/world_gen_settings.dat'), + True, typed=True) + out += json.dumps(settings, indent=1, sort_keys=True).splitlines() + return out + + +def main(): + lock, jars, libraries = base.verify_artifacts() + build = lab.LOCAL / NAME + build.mkdir(parents=True, exist_ok=True) + report = build / 'report.json' + report.unlink(missing_ok=True) + subprocess.run(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT, + check=True, capture_output=True) + metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'], + cwd=lab.ROOT, text=True)) + binary = Path(metadata['target_directory']) / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client') + dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) + vanilla_server = ['java', '-Xmx2g', '-Duser.timezone=UTC', '-cp', dependencies, 'net.minecraft.server.Main', + 'nogui'] + horizon_server = [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui'] + vanilla, horizon = [], [] + for case in CASES: + vanilla += run_case('vanilla', vanilla_server, build, case) + horizon += run_case('horizon', horizon_server, build, case) + (build / 'vanilla.txt').write_text('\n'.join(vanilla) + '\n', encoding='utf-8') + (build / 'horizon.txt').write_text('\n'.join(horizon) + '\n', encoding='utf-8') + if vanilla != horizon: + import difflib + diff = '\n'.join(difflib.unified_diff(vanilla, horizon, 'vanilla', 'horizon', n=2, lineterm='')) + raise AssertionError(f'world settings differ (see {build}/vanilla.txt and horizon.txt):\n' + diff[:6000]) + if not any('minecraft:flat' in l for l in vanilla) or not any(l.startswith('ERROR: ') for l in vanilla): + raise AssertionError(f'vanilla made no flat world or logged no error (see {build})') + lab.write_json(report, dict( + schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(), + java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], + cases=list(CASES), lines=len(vanilla), output_sha256=lab.digest(build / 'horizon.txt', 'sha256'), + scope='a new dedicated-server world: level-type (flat, normal, unknown), a flat world\'s ' + 'generator-settings, generate-structures, as world_gen_settings.dat and the lines logged.')) + print(f'server world gen: {len(CASES)} cases, {len(vanilla)} lines identical') + + +if __name__ == '__main__': + try: + main() + except AssertionError as error: + sys.exit(f'{NAME}: {error}')