Merge branch 'parity-base' into parity/old-saves
This commit is contained in:
@@ -0,0 +1,392 @@
|
||||
#!/usr/bin/env python3
|
||||
"""The dedicated server against vanilla's, seen from a vanilla client.
|
||||
|
||||
The pinned server JAR first generates a world (offline mode, a fixed seed,
|
||||
respawn_radius 0) and stops; a vanilla dedicated server and Horizon's
|
||||
(`horizon-client --dedicated --nogui`) then serve copies of it with the same
|
||||
server.properties, server-icon.png and lists. VanillaProtocolClient, a
|
||||
headless client made of vanilla's own codecs (any byte vanilla would not
|
||||
read is an error), pings each server, joins it in offline mode through
|
||||
login, configuration and play, accepts the spawn teleport, receives the
|
||||
chunks around the spawn (their bytes are compared: the world is loaded from
|
||||
the same files), chats, runs /list and leaves. The console then runs `list`
|
||||
and `say` and `stop`. Compared, all of them exactly:
|
||||
|
||||
- the status response (MOTD, players, version, favicon) and the ping;
|
||||
- the legacy (pre-1.7, 0xFE) ping reply;
|
||||
- the login and configuration packets in order (compression, profile,
|
||||
brand, features, known packs, every registry, tags);
|
||||
- the play packets up to the first chunk batch, in order, with entity ids,
|
||||
keep-alive ids and times left out; the chunks sent and their bytes; the
|
||||
replies to the chat message and to /list;
|
||||
- the console lines about the player, the chat, `list`, `say` and `stop`;
|
||||
- the files the servers leave: server.properties (but the date line),
|
||||
eula.txt, ops.json, whitelist.json, banned-players.json, banned-ips.json.
|
||||
|
||||
Online mode needs Mojang's session servers and is not tested.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import platform
|
||||
import re
|
||||
import shutil
|
||||
import socket
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
import zlib
|
||||
|
||||
try:
|
||||
from . import lab, oracle_harness as oh, test_rust_parity as base
|
||||
except ImportError:
|
||||
import lab
|
||||
import oracle_harness as oh
|
||||
import test_rust_parity as base
|
||||
|
||||
NAME = 'dedicated-server-parity'
|
||||
SEED = 12345
|
||||
PLAYER = 'Tester'
|
||||
# Packets Horizon's server does not send yet, listed rather than compared:
|
||||
# the recipe property sets and stonecutter recipes (ClientboundUpdateRecipes)
|
||||
# and a player's advancements (ClientboundUpdateAdvancements, and the recipe
|
||||
# unlocks their first tick grants). Each is reported on every run.
|
||||
KNOWN_GAPS = {
|
||||
'minecraft:update_recipes': 'ClientboundUpdateRecipesPacket (recipe property sets, stonecutter recipes)',
|
||||
'minecraft:update_advancements': 'ClientboundUpdateAdvancementsPacket (player advancements)',
|
||||
'minecraft:recipe_book_add+': 'recipes unlocked by advancements on the first tick',
|
||||
}
|
||||
|
||||
|
||||
def gap(line):
|
||||
"""The known gap a vanilla play-start line belongs to, if any."""
|
||||
parts = line.split(' ')
|
||||
if len(parts) < 2 or parts[0] != 'play-start':
|
||||
return None
|
||||
if parts[1] in KNOWN_GAPS:
|
||||
return parts[1]
|
||||
if parts[1] == 'minecraft:recipe_book_add' and 'entries=[]' not in line:
|
||||
return 'minecraft:recipe_book_add+'
|
||||
return None
|
||||
|
||||
|
||||
PROPERTIES = {
|
||||
'online-mode': 'false',
|
||||
'white-list': 'false',
|
||||
'level-seed': str(SEED),
|
||||
'view-distance': '4',
|
||||
'simulation-distance': '4',
|
||||
'spawn-protection': '0',
|
||||
'max-players': '5',
|
||||
'motd': '\\u00a7aHorizon \\u00a7rparity \\u00e9t\\u00e9',
|
||||
'pause-when-empty-seconds': '-1',
|
||||
}
|
||||
|
||||
|
||||
def free_port():
|
||||
with socket.socket() as s:
|
||||
s.bind(('127.0.0.1', 0))
|
||||
return s.getsockname()[1]
|
||||
|
||||
|
||||
def icon_png():
|
||||
"""A 64x64 RGBA PNG (server-icon.png)."""
|
||||
rows = b''.join(b'\0' + b''.join(bytes((x * 4, y * 4, (x ^ y) * 4, 255)) for x in range(64)) for y in range(64))
|
||||
|
||||
def chunk(kind, data):
|
||||
return struct.pack('>I', len(data)) + kind + data + struct.pack('>I', zlib.crc32(kind + data))
|
||||
return (b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', struct.pack('>IIBBBBB', 64, 64, 8, 6, 0, 0, 0))
|
||||
+ chunk(b'IDAT', zlib.compress(rows)) + chunk(b'IEND', b''))
|
||||
|
||||
|
||||
def write_server_dir(directory, port, world=None):
|
||||
if directory.exists():
|
||||
shutil.rmtree(directory)
|
||||
directory.mkdir(parents=True)
|
||||
props = dict(PROPERTIES, **{'server-port': str(port)})
|
||||
(directory / 'server.properties').write_text(''.join(f'{k}={v}\n' for k, v in props.items()), encoding='iso-8859-1')
|
||||
(directory / 'eula.txt').write_text('eula=true\n')
|
||||
(directory / 'server-icon.png').write_bytes(icon_png())
|
||||
if world is not None:
|
||||
shutil.copytree(world, directory / 'world')
|
||||
for lock in (directory / 'world').glob('session.lock'):
|
||||
lock.unlink()
|
||||
|
||||
|
||||
class Server:
|
||||
"""A server process with its console on stdin and its log collected."""
|
||||
|
||||
def __init__(self, command, directory, log_name):
|
||||
self.log_path = directory.parent / f'{log_name}.log'
|
||||
self.log = self.log_path.open('w', encoding='utf-8')
|
||||
self.lines = []
|
||||
self.process = subprocess.Popen(list(map(str, command)), cwd=directory, stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
|
||||
encoding='utf-8', errors='replace')
|
||||
self.reader = threading.Thread(target=self._read, daemon=True)
|
||||
self.reader.start()
|
||||
|
||||
def _read(self):
|
||||
for line in self.process.stdout:
|
||||
self.lines.append(line.rstrip('\n'))
|
||||
self.log.write(line)
|
||||
self.log.flush()
|
||||
|
||||
def wait_for(self, pattern, timeout=300):
|
||||
deadline = time.time() + timeout
|
||||
regex = re.compile(pattern)
|
||||
while time.time() < deadline:
|
||||
if any(regex.search(l) for l in self.lines):
|
||||
return
|
||||
if self.process.poll() is not None:
|
||||
time.sleep(0.5)
|
||||
raise AssertionError(f'{self.log_path.name}: server exited ({self.process.returncode}):\n'
|
||||
+ '\n'.join(self.lines[-30:]))
|
||||
time.sleep(0.2)
|
||||
raise AssertionError(f'{self.log_path.name}: no line matching {pattern!r} in {timeout} s:\n' + '\n'.join(self.lines[-30:]))
|
||||
|
||||
def console(self, command):
|
||||
self.process.stdin.write(command + '\n')
|
||||
self.process.stdin.flush()
|
||||
|
||||
def stop(self, timeout=120):
|
||||
if self.process.poll() is None:
|
||||
try:
|
||||
self.console('stop')
|
||||
self.process.wait(timeout=timeout)
|
||||
except (BrokenPipeError, subprocess.TimeoutExpired):
|
||||
self.process.kill()
|
||||
self.process.wait()
|
||||
self.reader.join(timeout=10)
|
||||
self.log.close()
|
||||
|
||||
|
||||
def legacy_ping(port):
|
||||
"""The reply to a 1.6 client's 0xFE 0x01 0xFA MC|PingHost query (LegacyQueryHandler)."""
|
||||
host = 'localhost'
|
||||
data = (b'\xfe\x01\xfa' + struct.pack('>H', 11) + 'MC|PingHost'.encode('utf-16-be')
|
||||
+ struct.pack('>H', 7 + 2 * len(host)) + bytes([78]) + struct.pack('>H', len(host))
|
||||
+ host.encode('utf-16-be') + struct.pack('>I', port))
|
||||
with socket.create_connection(('127.0.0.1', port), timeout=10) as s:
|
||||
s.sendall(data)
|
||||
reply = b''
|
||||
while True:
|
||||
try:
|
||||
chunk = s.recv(4096)
|
||||
except TimeoutError:
|
||||
return reply.hex() + ' <no close>'
|
||||
if not chunk:
|
||||
break
|
||||
reply += chunk
|
||||
return reply.hex()
|
||||
|
||||
|
||||
CONSOLE_LINE = re.compile(r'^\[\d\d:\d\d:\d\d\] \[([^\]]+)\]: (.*)$')
|
||||
KEPT = re.compile(r'Tester|\[Server\]|\[Not Secure\]|There are|Stopping|Saving|ThreadedAnvilChunkStorage|All dimensions|Unknown or incomplete')
|
||||
|
||||
|
||||
def console_lines(lines):
|
||||
"""The console lines the comparison covers, timestamps and ports aside."""
|
||||
out = []
|
||||
for line in lines:
|
||||
m = CONSOLE_LINE.match(line)
|
||||
if not m or not KEPT.search(m.group(2)):
|
||||
continue
|
||||
text = re.sub(r'/127\.0\.0\.1:\d+', '/127.0.0.1:_', m.group(2))
|
||||
text = re.sub(r'entity id \d+', 'entity id _', text)
|
||||
out.append(f'[{m.group(1)}] {text}')
|
||||
# Vanilla saves its levels in the order of an identity-hashed map, which
|
||||
# changes between runs: each block of saves is compared sorted.
|
||||
i = 0
|
||||
while i < len(out):
|
||||
j = i
|
||||
while j < len(out) and 'Saving chunks for level' in out[j]:
|
||||
j += 1
|
||||
out[i:j] = sorted(out[i:j])
|
||||
i = j + 1 if j == i else j
|
||||
return out
|
||||
|
||||
|
||||
def server_files(directory):
|
||||
out = []
|
||||
for name in ['server.properties', 'eula.txt', 'ops.json', 'whitelist.json', 'banned-players.json', 'banned-ips.json']:
|
||||
path = directory / name
|
||||
if not path.exists():
|
||||
out.append(f'{name}: missing')
|
||||
continue
|
||||
text = path.read_bytes().decode('iso-8859-1')
|
||||
if name in ('server.properties', 'eula.txt'):
|
||||
# The second comment line is the date Properties.store wrote.
|
||||
text = re.sub(r'^(#[^\n]*\n)#[^\n]*\n', r'\1#<date>\n', text)
|
||||
# Random per server, and each side has its own port.
|
||||
text = re.sub(r'\n(management-server-secret|server-port)=[^\n]*', r'\n\1=<varies>', text)
|
||||
out.append(f'{name}: {text!r}')
|
||||
return out
|
||||
|
||||
|
||||
def run_side(label, command, directory, client):
|
||||
server = Server(command, directory, label)
|
||||
try:
|
||||
server.wait_for(r'Done \(')
|
||||
port = int(PROPERTIES_PORTS[label])
|
||||
legacy = legacy_ping(port)
|
||||
client(port, directory.parent / f'{label}-client.txt')
|
||||
time.sleep(1)
|
||||
server.console('list')
|
||||
server.console('say console says hi')
|
||||
server.wait_for(r'\[Server\] console says hi', timeout=30)
|
||||
finally:
|
||||
server.stop()
|
||||
transcript = (directory.parent / f'{label}-client.txt').read_text(encoding='utf-8').splitlines()
|
||||
return (['legacy-ping ' + legacy] + transcript + ['console ' + l for l in console_lines(server.lines)]
|
||||
+ ['file ' + l for l in server_files(directory)])
|
||||
|
||||
|
||||
PROPERTIES_PORTS = {}
|
||||
|
||||
|
||||
def horizon_client_joins(vanilla_server, binary, world, build, reference):
|
||||
"""Horizon's client joins a vanilla dedicated server headless: it must
|
||||
land where the vanilla client did, receive as many chunks, see its chat
|
||||
and /list reply, and leave as a vanilla client does (the server's log)."""
|
||||
label = 'vanilla-for-horizon-client'
|
||||
PROPERTIES_PORTS[label] = port = free_port()
|
||||
directory = build / label
|
||||
write_server_dir(directory, port, world)
|
||||
client_dir = build / 'horizon-client'
|
||||
shutil.rmtree(client_dir, ignore_errors=True)
|
||||
client_dir.mkdir()
|
||||
report = build / 'horizon-client-report.json'
|
||||
report.unlink(missing_ok=True)
|
||||
server = Server(vanilla_server, directory, label)
|
||||
try:
|
||||
server.wait_for(r'Done \(')
|
||||
env = dict(os.environ, HORIZON_NO_AUDIO='1', HORIZON_NET_REPORT=str(report))
|
||||
with (build / 'horizon-client-run.log').open('w') as log:
|
||||
subprocess.run([str(c) for c in [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--render-distance', 2,
|
||||
'--server', f'127.0.0.1:{port}', '--name', PLAYER]],
|
||||
cwd=client_dir, env=env, stdout=log, stderr=subprocess.STDOUT, timeout=300, check=True)
|
||||
server.wait_for(r'left the game', timeout=30)
|
||||
finally:
|
||||
server.stop()
|
||||
got = json.loads(report.read_text())
|
||||
move = next(l for l in reference if l.startswith('play-move'))
|
||||
want_position = [float(v) for v in re.match(r'play-move \(([^,]+), ([^,]+), ([^)]+)\)', move).groups()]
|
||||
want_chunks = int(next(l for l in reference if l.startswith('play-counts')).split()[-1])
|
||||
expected = {
|
||||
'connected throughout': None,
|
||||
'spawn position': want_position,
|
||||
'chunks received': want_chunks,
|
||||
'own chat shown': True,
|
||||
'/list reply shown': True,
|
||||
'server log': ['logged in', 'Tester joined the game', '[Not Secure] <Tester> hello from the horizon client',
|
||||
'Tester lost connection: Disconnected', 'Tester left the game'],
|
||||
}
|
||||
log = '\n'.join(server.lines)
|
||||
actual = {
|
||||
'connected throughout': got['error'],
|
||||
'spawn position': got['position'],
|
||||
'chunks received': got['chunks'],
|
||||
'own chat shown': any('<Tester> hello from the horizon client' in c for c in got['chat']),
|
||||
'/list reply shown': any('There are 1 of a max of 5 players online: Tester' in c for c in got['chat']),
|
||||
'server log': [k for k in expected['server log'] if k in log],
|
||||
}
|
||||
return [f'horizon-client {k}: {"ok" if actual[k] == v else f"expected {v!r}, got {actual[k]!r}"}'
|
||||
for k, v in expected.items()]
|
||||
|
||||
|
||||
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)
|
||||
oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java'
|
||||
classes = build / 'classes'
|
||||
shutil.rmtree(classes, ignore_errors=True)
|
||||
classes.mkdir()
|
||||
dependencies = os.pathsep.join(map(str, [jars['server'], *libraries]))
|
||||
oh.run_logged(['javac', '--release', lock['minecraft']['java_major'], '-proc:none', '-nowarn', '-sourcepath',
|
||||
lab.ROOT / 'mods/horizon/src/test/java', '-cp', dependencies, '-d', classes, oracle],
|
||||
build, 'compile-java')
|
||||
oh.run_logged(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], build,
|
||||
'compile-rust')
|
||||
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')
|
||||
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']
|
||||
|
||||
# The world, generated once by vanilla.
|
||||
generate = build / 'generate'
|
||||
write_server_dir(generate, free_port())
|
||||
server = Server(vanilla_server, generate, 'generate')
|
||||
try:
|
||||
server.wait_for(r'Done \(')
|
||||
server.console('gamerule respawn_radius 0')
|
||||
server.wait_for(r'respawn_radius', timeout=30)
|
||||
# Vanilla keeps no spawn chunks: force-load the area the client is
|
||||
# sent and its neighbours (the console stands at the world spawn) so
|
||||
# both servers load full chunks instead of finishing proto-chunks.
|
||||
server.console('forceload add ~-80 ~-80 ~80 ~80')
|
||||
server.wait_for(r'chunks in the Overworld are now force loaded|forceloaded|force loaded', timeout=60)
|
||||
time.sleep(20)
|
||||
server.console('forceload remove all')
|
||||
server.console('save-all flush')
|
||||
server.wait_for(r'Saved the game', timeout=120)
|
||||
finally:
|
||||
server.stop()
|
||||
world = generate / 'world'
|
||||
(world / 'playerdata').mkdir(exist_ok=True)
|
||||
|
||||
def client(port, out):
|
||||
oh.run_logged(['java', '-Xmx1g', '-Duser.timezone=UTC', '-cp', str(classes) + os.pathsep + dependencies,
|
||||
'dev.horizonlab.parity.VanillaProtocolClient', '127.0.0.1', port, PLAYER, out], build,
|
||||
out.stem, timeout=300)
|
||||
|
||||
results = {}
|
||||
for label, command in [('vanilla', vanilla_server), ('horizon', horizon_server)]:
|
||||
PROPERTIES_PORTS[label] = free_port()
|
||||
directory = build / label
|
||||
write_server_dir(directory, PROPERTIES_PORTS[label], world)
|
||||
results[label] = run_side(label, command, directory, client)
|
||||
(build / f'{label}.txt').write_text('\n'.join(results[label]) + '\n', encoding='utf-8')
|
||||
client_checks = horizon_client_joins(vanilla_server, binary, world, build, results['vanilla'])
|
||||
vanilla, horizon = results['vanilla'], results['horizon']
|
||||
gaps = sorted({gap(l) for l in vanilla if gap(l)})
|
||||
vanilla = [l for l in vanilla if not gap(l)]
|
||||
for g in gaps:
|
||||
print(f'known gap: {KNOWN_GAPS[g]}')
|
||||
errors = [l for l in vanilla + horizon if l.startswith('error')] + [l for l in client_checks if not l.endswith(': ok')]
|
||||
for line in client_checks:
|
||||
print(line)
|
||||
differences = []
|
||||
import difflib
|
||||
for line in difflib.unified_diff(vanilla, horizon, 'vanilla', 'horizon', n=0, lineterm=''):
|
||||
differences.append(line[:400])
|
||||
# Name the chunks that differ instead of the whole line.
|
||||
chunk_lines = [dict(re.findall(r'(-?\d+,-?\d+)=(\w+)', next((l for l in side if l.startswith('play-chunks')), '')))
|
||||
for side in (vanilla, horizon)]
|
||||
for key in sorted(set(chunk_lines[0]) | set(chunk_lines[1])):
|
||||
a, b = chunk_lines[0].get(key), chunk_lines[1].get(key)
|
||||
if a != b:
|
||||
differences.append(f'chunk {key}: vanilla {a} horizon {b}')
|
||||
if differences or errors:
|
||||
raise AssertionError(f'{len([d for d in differences if d[:1] in "+-" and d[:3] not in ("+++", "---")])} differing lines '
|
||||
f'(see {build}/vanilla.txt and horizon.txt):\n' + '\n'.join(differences[:80]))
|
||||
lab.write_json(report, dict(
|
||||
schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(),
|
||||
java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0],
|
||||
lines=len(vanilla), known_gaps=[KNOWN_GAPS[g] for g in gaps], output_sha256=lab.digest(build / 'horizon.txt', 'sha256'),
|
||||
scope='Dedicated server seen by a vanilla-codec client: status, legacy ping, login, configuration, play '
|
||||
'start, chunks, chat, /list, console list/say/stop, files written. Offline mode.'))
|
||||
print(f'dedicated server: {len(vanilla)} transcript lines identical')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
main()
|
||||
except AssertionError as error:
|
||||
sys.exit(f'{NAME}: {error}')
|
||||
@@ -0,0 +1,177 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Compare Mob.finalizeSpawn (spawned mob gear, enchantments and state) with vanilla.
|
||||
|
||||
Java (VanillaFinalizeSpawnOracle) creates each case's mobs on a stand-in
|
||||
ServerLevel and runs vanilla's finalizeSpawn with a seeded level random (what
|
||||
finalizeSpawn reads through level.getRandom()) and seeded entity randoms; Rust
|
||||
(finalize_spawn_fixture) runs horizon_client::mobs::finalize on the same case.
|
||||
Cases cover every mob type, every spawn reason, peaceful to hard, game times,
|
||||
inhabited times and moon phases (the local difficulty and its special
|
||||
multiplier), packs sharing group data, biomes (variants, villager types,
|
||||
sheep colours, rabbit and fox types), structures (black cats), roofs (camel
|
||||
husk room) and chickens standing by (zombie jockeys).
|
||||
|
||||
Each entity created in a case, the mobs and the jockeys finalizeSpawn makes,
|
||||
is compared: equipment with its enchantments, drop chances, CanPickUpLoot,
|
||||
LeftHanded, persistence, age, health, attribute values, effects, variants,
|
||||
the memories finalizeSpawn sets and the vehicle it rides; then the next draw
|
||||
of every entity random and of the level random, which pins every draw in
|
||||
between.
|
||||
"""
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import platform
|
||||
import random
|
||||
import subprocess
|
||||
try:
|
||||
from . import lab, test_rust_parity as base
|
||||
except ImportError:
|
||||
import lab
|
||||
import test_rust_parity as base
|
||||
|
||||
REASONS = ['NATURAL', 'CHUNK_GENERATION', 'SPAWNER', 'STRUCTURE', 'BREEDING', 'MOB_SUMMONED', 'JOCKEY', 'EVENT',
|
||||
'CONVERSION', 'REINFORCEMENT', 'TRIGGERED', 'BUCKET', 'SPAWN_ITEM_USE', 'COMMAND', 'DISPENSER', 'PATROL',
|
||||
'TRIAL_SPAWNER', 'LOAD', 'DIMENSION_TRAVEL']
|
||||
# Mobs whose finalizeSpawn draws, by how often the corpus picks them.
|
||||
GEARED = ['zombie', 'husk', 'drowned', 'zombie_villager', 'zombified_piglin', 'skeleton', 'stray', 'bogged', 'parched',
|
||||
'wither_skeleton', 'pillager', 'vindicator', 'piglin', 'piglin_brute', 'vex', 'fox', 'spider', 'illusioner',
|
||||
'evoker', 'ravager', 'witch']
|
||||
OTHERS = ['pig', 'cow', 'mooshroom', 'sheep', 'chicken', 'rabbit', 'horse', 'donkey', 'mule', 'llama', 'trader_llama',
|
||||
'goat', 'panda', 'polar_bear', 'wolf', 'cat', 'ocelot', 'parrot', 'bee', 'turtle', 'armadillo', 'frog',
|
||||
'axolotl', 'squid', 'glow_squid', 'cod', 'salmon', 'tropical_fish', 'pufferfish', 'dolphin', 'bat', 'villager',
|
||||
'wandering_trader', 'iron_golem', 'snow_golem', 'creeper', 'cave_spider', 'enderman', 'endermite',
|
||||
'silverfish', 'slime', 'magma_cube', 'phantom', 'hoglin', 'zoglin', 'ghast', 'blaze', 'strider', 'guardian',
|
||||
'elder_guardian', 'shulker', 'camel', 'allay', 'breeze', 'sniffer', 'happy_ghast', 'warden', 'creaking',
|
||||
'camel_husk', 'nautilus', 'zombie_nautilus', 'sulfur_cube', 'zombie_horse', 'skeleton_horse', 'tadpole',
|
||||
'giant', 'wither']
|
||||
BIOMES = ['minecraft:plains', 'minecraft:desert', 'minecraft:snowy_plains', 'minecraft:snowy_taiga', 'minecraft:taiga',
|
||||
'minecraft:jungle', 'minecraft:bamboo_jungle', 'minecraft:swamp', 'minecraft:mangrove_swamp',
|
||||
'minecraft:savanna', 'minecraft:badlands', 'minecraft:forest', 'minecraft:grove', 'minecraft:old_growth_pine_taiga',
|
||||
'minecraft:old_growth_spruce_taiga', 'minecraft:sparse_jungle', 'minecraft:windswept_savanna',
|
||||
'minecraft:mushroom_fields', 'minecraft:river', 'minecraft:frozen_river', 'minecraft:ocean',
|
||||
'minecraft:warm_ocean', 'minecraft:lukewarm_ocean', 'minecraft:cold_ocean', 'minecraft:frozen_ocean',
|
||||
'minecraft:ice_spikes', 'minecraft:cherry_grove', 'minecraft:pale_garden', 'minecraft:dripstone_caves',
|
||||
'minecraft:lush_caves', 'minecraft:deep_dark', 'minecraft:nether_wastes', 'minecraft:crimson_forest',
|
||||
'minecraft:soul_sand_valley', 'minecraft:the_end', 'minecraft:snowy_slopes', 'minecraft:frozen_peaks',
|
||||
'minecraft:wooded_badlands', 'minecraft:beach', 'minecraft:snowy_beach', 'minecraft:meadow',
|
||||
'minecraft:sulfur_caves']
|
||||
STRUCTURES = ['minecraft:swamp_hut', 'minecraft:village_plains', 'minecraft:pillager_outpost', 'minecraft:fortress',
|
||||
'minecraft:trial_chambers']
|
||||
FLOORS = ['minecraft:grass_block', 'minecraft:sand', 'minecraft:stone', 'minecraft:snow_block', 'minecraft:netherrack']
|
||||
|
||||
|
||||
def dimension(biome):
|
||||
if biome in ('minecraft:nether_wastes', 'minecraft:crimson_forest', 'minecraft:soul_sand_valley'):
|
||||
return 'minecraft:the_nether'
|
||||
if biome == 'minecraft:the_end':
|
||||
return 'minecraft:the_end'
|
||||
return 'minecraft:overworld'
|
||||
|
||||
|
||||
def corpus():
|
||||
rng = random.Random(0x46494E414C)
|
||||
cases = []
|
||||
for k in range(int(os.environ.get('FINALIZE_SPAWN_CASES', '24000'))):
|
||||
kind = rng.choice(GEARED) if rng.random() < 0.55 else rng.choice(OTHERS)
|
||||
reason = rng.choice(['NATURAL'] * 10 + ['CHUNK_GENERATION', 'SPAWNER', 'STRUCTURE', 'COMMAND', 'SPAWN_ITEM_USE',
|
||||
'TRIAL_SPAWNER', 'PATROL', 'EVENT', 'JOCKEY'] * 2 + REASONS)
|
||||
biome = rng.choice(BIOMES)
|
||||
kv = {
|
||||
'type': f'minecraft:{kind}',
|
||||
'reason': reason,
|
||||
'difficulty': rng.choice(['hard', 'hard', 'normal', 'normal', 'easy', 'peaceful']),
|
||||
'time': rng.choice([0, 0, 50000, 72000, 400000, 800000, 1512000, 3000000, rng.randrange(0, 4000000)]),
|
||||
'inhabited': rng.choice([0, 0, 0, 100000, 1000000, 3600000, 5000000, rng.randrange(0, 4000000)]),
|
||||
'phase': rng.randrange(8),
|
||||
'seed': rng.randrange(-2**63, 2**63),
|
||||
'eseed': rng.randrange(-2**47, 2**47),
|
||||
'pack': rng.choice([1, 1, 1, 2, 3, 4, 6]),
|
||||
'pos': f'{rng.randrange(-200, 200) + rng.choice([0.5, 0.25, 0.0]):.2f},{rng.randrange(-30, 120)},'
|
||||
f'{rng.randrange(-200, 200) + rng.choice([0.5, 0.75, 0.0]):.2f}',
|
||||
'biome': biome,
|
||||
'dim': dimension(biome),
|
||||
'floor': rng.choice(FLOORS),
|
||||
}
|
||||
if rng.random() < 0.15:
|
||||
kv['structure'] = rng.choice(STRUCTURES)
|
||||
if rng.random() < 0.2:
|
||||
kv['roof'] = rng.choice([1, 2, 3])
|
||||
if rng.random() < 0.15:
|
||||
kv['chickens'] = rng.choice([1, 2])
|
||||
if rng.random() < 0.05:
|
||||
kv['monsters'] = 0
|
||||
cases.append(f'case/{k} ' + ' '.join(f'{a}={b}' for a, b in kv.items()))
|
||||
return cases
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--prepare', action='store_true')
|
||||
parser.add_argument('--java-only', action='store_true', help='run the oracle and stop')
|
||||
args = parser.parse_args()
|
||||
build = lab.LOCAL / 'rust-finalize-spawn-parity'
|
||||
build.mkdir(parents=True, exist_ok=True)
|
||||
if args.prepare:
|
||||
lab.prepare()
|
||||
lock, jars, libraries = base.verify_artifacts()
|
||||
resources = lab.LOCAL / 'horizon-client/26.3'
|
||||
oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/VanillaFinalizeSpawnOracle.java'
|
||||
sources = [oracle, Path(__file__), lab.ROOT / 'crates/horizon-client/examples/finalize_spawn_fixture.rs',
|
||||
lab.ROOT / 'crates/horizon-client/src/mobs/finalize.rs']
|
||||
tested = base.source_hashes([s for s in sources if s.exists()])
|
||||
inputs = build / 'inputs.txt'
|
||||
cases = corpus()
|
||||
inputs.write_text(''.join(line + '\n' for line in cases), encoding='utf-8')
|
||||
classes = build / 'classes'
|
||||
classes.mkdir(exist_ok=True)
|
||||
deps = os.pathsep.join(map(str, libraries))
|
||||
|
||||
def run(command, name):
|
||||
path = build / (name + '.log')
|
||||
with path.open('w', encoding='utf-8') as log:
|
||||
try:
|
||||
base.run(list(map(str, command)), stdout=log, stderr=subprocess.STDOUT)
|
||||
except subprocess.CalledProcessError:
|
||||
log.flush()
|
||||
raise SystemExit(f'{name} failed:\n' + path.read_text(errors='replace')[-4000:])
|
||||
run(['javac', '--release', lock['minecraft']['java_major'], '-proc:none', '-sourcepath', str(lab.ROOT / 'mods/horizon/src/test/java'),
|
||||
'-cp', str(jars['server']) + os.pathsep + deps, '-d', classes, oracle], 'compile-java')
|
||||
run(['java', '-Xmx3G', '-cp', os.pathsep.join([str(classes), str(jars['server']), deps]),
|
||||
'dev.horizonlab.parity.VanillaFinalizeSpawnOracle', inputs, build / 'server.txt'], 'server')
|
||||
vanilla = (build / 'server.txt').read_text().splitlines()
|
||||
errors = [l for l in vanilla if ' error ' in l]
|
||||
if errors:
|
||||
raise SystemExit(f'oracle errors ({len(errors)}): {errors[:5]}')
|
||||
if args.java_only:
|
||||
print(f'{len(vanilla)} oracle lines')
|
||||
return
|
||||
halloween = vanilla[0].split()[1]
|
||||
run(['cargo', 'build', '--release', '-p', 'horizon-client', '--example', 'finalize_spawn_fixture'], 'compile-rust')
|
||||
metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'], cwd=lab.ROOT, text=True))
|
||||
binary = Path(metadata['target_directory']) / 'release/examples/finalize_spawn_fixture'
|
||||
run([binary, resources, inputs, build / 'rust.txt', halloween], 'rust')
|
||||
rust = (build / 'rust.txt').read_text().splitlines()
|
||||
bad = [(a, b) for a, b in zip(vanilla, rust) if a != b]
|
||||
if bad or len(vanilla) != len(rust):
|
||||
report = '\n'.join(f' vanilla {a[:2000]}\n rust {b[:2000]}' for a, b in bad[:12])
|
||||
raise SystemExit(f'{len(bad)} of {len(vanilla)} lines differ (rust {len(rust)} lines):\n{report}')
|
||||
base.ensure_sources_unchanged([s for s in sources if s.exists()], tested)
|
||||
entities = sum(line.count(' | ') for line in vanilla) // 2
|
||||
enchanted = sum(line.count('[minecraft:') for line in vanilla)
|
||||
lab.write_json(build / 'report.json', {
|
||||
'schema': 1, 'minecraft': lock['minecraft']['version'], 'platform': platform.platform(), 'cases': len(cases),
|
||||
'entities': entities, 'enchanted_items': enchanted, 'sources_sha256': tested,
|
||||
'scope': 'Mob.finalizeSpawn for every mob type and spawn reason: populateDefaultEquipmentSlots, '
|
||||
'populateDefaultEquipmentEnchantments through the mob_spawn_equipment and pillager_spawn_crossbow '
|
||||
'providers, drop chances, CanPickUpLoot, LeftHanded, the follow range spawn bonus, babies and '
|
||||
'group data, jockeys (chicken, spider, strider, zombie horse, camel husk, zombie nautilus), '
|
||||
'variants and sound variants, zombie attributes, spider effects, patrol leaders, villager types, '
|
||||
'cube sizes, horse and llama attributes and the memories finalizeSpawn sets, with every draw of the '
|
||||
'level random and of the entities\' own randoms.'})
|
||||
print(f'{len(cases):,} finalizeSpawn cases match vanilla ({entities:,} entities, {enchanted:,} enchanted items)')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,48 @@
|
||||
#!/usr/bin/env python3
|
||||
"""LAN world discovery against the pinned client's LanServerPinger and
|
||||
LanServerDetection: the announcement an opened world multicasts
|
||||
(`[MOTD]...[/MOTD][AD]port[/AD]` in UTF-8), and how the multiplayer screen
|
||||
parses what it hears (parseMotd, parseAddress, UTF-8 decoding of malformed
|
||||
bytes) and keeps its list (LanServerList.addServer: one entry per
|
||||
address, the first MOTD kept, never expired while the screen is open).
|
||||
VanillaLanOracle and the `lan_probe` example must print the same lines."""
|
||||
import random
|
||||
|
||||
try:
|
||||
from . import oracle_harness as oh
|
||||
except ImportError:
|
||||
import oracle_harness as oh
|
||||
|
||||
NAME = 'lan-parity'
|
||||
|
||||
|
||||
def make_input(build):
|
||||
rng = random.Random(4445)
|
||||
lines = []
|
||||
motds = ['Player - New World', 'Steve - §aGreen §lWorld', 'été 日本語 ✓', '', '[MOTD]', 'a[/MOTD]b', 'x' * 300,
|
||||
'tab\\there', '[AD]25565[/AD]']
|
||||
for motd in motds:
|
||||
for port in ['25565', '1024', '65535', '0', 'abc', '']:
|
||||
lines.append(f'ping\t{motd}\t{port}')
|
||||
fragments = [b'[MOTD]', b'[/MOTD]', b'[AD]', b'[/AD]', b'25565', b'1234', b'Steve - World', b'\xc3\xa9', b'\xe6\x97',
|
||||
b'\xff', b'\xc0\x80', b'\xed\xa0\x80', b'\xf0\x9f\x98\x80', b'\xf0\x9f', b'abc', b' ', b'\x00', b'-1']
|
||||
ips = ['192.168.1.10', '10.0.0.2', '127.0.0.1']
|
||||
good = lambda motd, port: f'[MOTD]{motd}[/MOTD][AD]{port}[/AD]'.encode()
|
||||
for i in range(400):
|
||||
if i % 50 == 0:
|
||||
lines.append('reset')
|
||||
if rng.random() < 0.4:
|
||||
data = good(rng.choice(['A - W', 'B - W', 'é', '']), rng.choice(['25565', '25566', 'x', '']))
|
||||
else:
|
||||
data = b''.join(rng.choice(fragments) for _ in range(rng.randint(0, 9)))
|
||||
lines.append(f'heard\t{rng.choice(ips)}\t{data.hex()}')
|
||||
path = build / 'cases.txt'
|
||||
path.write_text('\n'.join(lines) + '\n', encoding='utf-8')
|
||||
return path, len(lines)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
oh.differential(NAME, 'VanillaLanOracle', 'lan_probe', make_input,
|
||||
'LanServerPinger.createPingString/parseMotd/parseAddress, LanServerDetection decoding and '
|
||||
'LanServerList.addServer/takeDirtyServers',
|
||||
sources=[oh.lab.ROOT / 'crates/horizon-client/src/lan/discovery.rs'])
|
||||
@@ -77,6 +77,36 @@ def scenarios():
|
||||
'player 12.5 -60 0.5',
|
||||
'mob minecraft:villager 0.5 -60 0.5 0 51 persistent',
|
||||
])
|
||||
# Two villagers gossip (TradeWithVillager): what one heard of the player
|
||||
# passes to the other, weakened, once in 1200 ticks; idle and at the bell.
|
||||
gossips = ('gossip=player:minor_negative:100 gossip=player:major_positive:15 '
|
||||
'gossip=player:trading:20 gossip=player:minor_positive:3')
|
||||
for name, opts, extra in [('villager_gossip_idle', 'time=100', []),
|
||||
('villager_gossip_bell', 'time=9500', ['block 0 -60 -6 minecraft:bell'])]:
|
||||
add(name, 400, extra + [
|
||||
'player 14.5 -60 0.5',
|
||||
f'mob minecraft:villager 0.5 -60 0.5 0 61 persistent {gossips}',
|
||||
'mob minecraft:villager 2.5 -60 0.5 180 62 persistent gossip=player:minor_positive:50',
|
||||
], opts)
|
||||
# Two villagers with bread breed (VillagerMakeLove): they eat, a baby is
|
||||
# born and takes a free bed, the parents rest for 6000 ticks.
|
||||
# PoiManager.take hands out the free beds of a section in hash order:
|
||||
# the parents claim the two near them, the child gets the only one left.
|
||||
beds = []
|
||||
for x in (-6, 3, 6):
|
||||
beds += [f'block {x} -60 6 minecraft:red_bed[facing=north,part=foot]',
|
||||
f'block {x} -60 5 minecraft:red_bed[facing=north,part=head]']
|
||||
for name, seeds in [('villager_breed', (71, 72)), ('villager_breed_2', (73, 74))]:
|
||||
add(name, 900, beds + [
|
||||
'player 14.5 -60 0.5',
|
||||
f'mob minecraft:villager 0.5 -60 0.5 0 {seeds[0]} persistent inv=bread*3',
|
||||
f'mob minecraft:villager 2.5 -60 0.5 180 {seeds[1]} persistent inv=bread*3 food=4',
|
||||
], 'time=100')
|
||||
# Too little food: no breeding.
|
||||
add('villager_breed_hungry', 400, beds[:2] + [
|
||||
'mob minecraft:villager 0.5 -60 0.5 0 75 persistent inv=bread*2',
|
||||
'mob minecraft:villager 2.5 -60 0.5 180 76 persistent inv=bread*3',
|
||||
], 'time=100')
|
||||
# Breezes: idle, and fighting a player near, in, and beyond the inner jump circle.
|
||||
add('breeze_alone', 200, ['mob minecraft:breeze 0.5 -60 0.5 90 61 persistent'])
|
||||
add('breeze_player', 200, ['player 8.5 -60 0.5', 'mob minecraft:breeze 0.5 -60 0.5 0 62 persistent'])
|
||||
@@ -155,6 +185,8 @@ def write_scenarios(path, chosen):
|
||||
lines.append(f'origin {ox} {oz}')
|
||||
for line in body:
|
||||
f = line.split()
|
||||
# `at TICK block ...` changes blocks before that tick; `watch` boxes stay relative.
|
||||
head, f = (f[:2], f[2:]) if f[0] == 'at' else ([], f)
|
||||
if f[0] == 'player':
|
||||
f[1] = repr(float(f[1]) + ox)
|
||||
f[3] = repr(float(f[3]) + oz)
|
||||
@@ -169,7 +201,7 @@ def write_scenarios(path, chosen):
|
||||
f[k] = str(int(f[k]) + ox)
|
||||
for k in (3, 6):
|
||||
f[k] = str(int(f[k]) + oz)
|
||||
lines.append(' '.join(f))
|
||||
lines.append(' '.join(head + f))
|
||||
lines.append('end')
|
||||
path.write_text('\n'.join(lines) + '\n')
|
||||
|
||||
@@ -187,6 +219,9 @@ def parse(path):
|
||||
if parts[1] == 'sound':
|
||||
cur['sounds'].setdefault(t, []).append(parts[2])
|
||||
continue
|
||||
if parts[1] == 'block':
|
||||
cur.setdefault('blocks', {}).setdefault(t, []).append(parts[2])
|
||||
continue
|
||||
if parts[1] == 'trace':
|
||||
cur.setdefault('trace', {}).setdefault(t, []).append(parts[2])
|
||||
continue
|
||||
@@ -286,11 +321,14 @@ def compare(vanilla, rust, fields=None):
|
||||
report.append(f'{name}: hearts differ at tick {t}: vanilla {v.get("hearts", {}).get(t)} | horizon {r.get("hearts", {}).get(t)}')
|
||||
clean = False
|
||||
break
|
||||
for t in sorted(set(v['sounds']) | set(r['sounds'])):
|
||||
if v['sounds'].get(t, []) != r['sounds'].get(t, []):
|
||||
report.append(f'{name}: sounds differ at tick {t}: vanilla {v["sounds"].get(t, [])} | horizon {r["sounds"].get(t, [])}')
|
||||
clean = False
|
||||
break
|
||||
# Sounds and watched block changes: first differing tick.
|
||||
for kind in ('sounds', 'blocks'):
|
||||
va, ra = v.get(kind, {}), r.get(kind, {})
|
||||
for t in sorted(set(va) | set(ra)):
|
||||
if va.get(t, []) != ra.get(t, []):
|
||||
report.append(f'{name}: {kind} differ at tick {t}: vanilla {va.get(t, [])} | horizon {ra.get(t, [])}')
|
||||
clean = False
|
||||
break
|
||||
totals['clean'] += clean
|
||||
if clean:
|
||||
report.append(f'{name}: all {len(v["ticks"])} ticks match')
|
||||
|
||||
Reference in New Issue
Block a user