Delete ten tools/ scripts nothing references

command_parse_summary, structure_start_oracle, fetch_client_assets,
compare_structure_starts, profile_behaviour_cache, command_tree_diff,
compare_loot_fill, compare_structure_blocks, test_lan_lifecycle and
PROFILE_SHARED_JSON.md: no workflow, doc, harness, test or other tool
names them (review-plan.md, Dead code). History keeps them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-23 14:41:40 +02:00
parent 6be7daebc8
commit 7cc47e2b55
10 changed files with 0 additions and 822 deletions
-85
View File
@@ -1,85 +0,0 @@
# Reproducing PR #607 block-facts startup profiles
These are loader-only measurements. They include `Assets::load`, `BlockFacts`, and
`Behaviours` and exclude content root preparation, graphics, input setup, first
frame, and the rest of the client startup sequence. No full-client startup gain
has been measured. The prepared `26.3` resource root must match the revision.
## Historical release builds
From the PR branch checkout, use an absolute prepared resource-root path and a
single, explicit Cargo release profile for each detached historical worktree:
```sh
PR_ROOT="$(pwd)"
BENCH_ROOT="$(mktemp -d)"
mkdir -p "$BENCH_ROOT/bin"
CARGO_TARGET_DIR="$BENCH_ROOT/target"
export CARGO_TARGET_DIR
for entry in baseline:e422bd13 a01:a01d47f2 cf:cf837118 cd:cd61e11b; do
name="${entry%%:*}"
revision="${entry#*:}"
git worktree add --detach "$BENCH_ROOT/$name" "$revision"
done
# e422bd13 predates the profiler; add the same baseline example source used
# for the benchmark, without changing any production implementation.
cp "$PR_ROOT/tools/baseline_loader_profile.rs" \
"$BENCH_ROOT/baseline/crates/horizon-client/examples/shared_json_startup_profile.rs"
for name in baseline a01 cf cd; do
(cd "$BENCH_ROOT/$name" && \
cargo build --offline --release -p horizon-client --example shared_json_startup_profile)
cp "$CARGO_TARGET_DIR/release/examples/shared_json_startup_profile" \
"$BENCH_ROOT/bin/$name"
done
cargo build --offline --release -p horizon-client --example shared_json_startup_profile
cp "$CARGO_TARGET_DIR/release/examples/shared_json_startup_profile" "$BENCH_ROOT/bin/current"
python3 tools/profile_shared_json.py \
--resources /absolute/path/to/.local/horizon-client/26.3 \
--baseline-binary "$BENCH_ROOT/bin/baseline" \
--a01-binary "$BENCH_ROOT/bin/a01" \
--cf-binary "$BENCH_ROOT/bin/cf" \
--cd-binary "$BENCH_ROOT/bin/cd" \
--binary "$BENCH_ROOT/bin/current" \
--repeats 10 --output "$BENCH_ROOT/five-way.json"
```
The source tree of each historical revision is exact. The baseline profiler is
an added standalone measurement example, and therefore does not alter its
historical loader logic. Comparisons use alternating forward/reverse process
order, separate processes, Linux `wait4` CPU time and peak RSS, and saved raw
samples with 25th/75th percentiles, extrema, build hashes and system load.
Keep concurrent compilation and other CPU-intensive jobs idle if possible.
`ru_maxrss` records process peak residency and cannot establish the real game's
memory usage after rendering begins. If system load rises, preserve rather than
discard outliers and report the uncertainty.
## Decoding passes and buffer lifetime
```sh
cargo build --offline --release -p horizon-client --example gameplay_decode_profile
python3 tools/profile_gameplay_decode.py \
--resources /absolute/path/to/.local/horizon-client/26.3 \
--repeats 10 --output "$BENCH_ROOT/decode.json"
python3 tools/profile_shared_json.py \
--resources /absolute/path/to/.local/horizon-client/26.3 \
--variant released="$BENCH_ROOT/bin/current"@deferred \
--variant retained="$BENCH_ROOT/bin/current"@deferred-held \
--repeats 10 --output "$BENCH_ROOT/lifetime.json"
```
The decoder profile measures the `Validate` pass and typed `Gameplay` pass
separately, including the JSON read in `total_ms`. The primary facts loader
continues both passes to preserve JSON validation, error precedence and useful
diagnostics, including overwritten fields and raw geometry. The lifetime
comparison measures current process `VmRSS` after materialisation, plus peak
RSS across the process. An explicit `drop` releases owned Rust values, but the
allocator may retain physical pages, so immediate RSS savings are not implied.
When finished, `git worktree remove` each temporary historical worktree. Do
not `git clean` or reset the active PR working tree.
-36
View File
@@ -1,36 +0,0 @@
#!/usr/bin/env python3
"""Group the mismatches of tools/test_command_parse_parity.py by cause.
usage: command_parse_summary.py MISMATCHES.jsonl [ROOT]
"""
import collections
import json
import sys
from pathlib import Path
def describe(side, kind):
if kind == 'parse':
if side.get('ok'):
return 'OK'
return f"ERR {side.get('error')!r}@{'c' if side.get('cursor') else '0'}"
return f"SUGG[{len(side['list'])}]"
path = Path(sys.argv[1])
only = sys.argv[2] if len(sys.argv) > 2 else None
groups = collections.Counter()
examples = {}
for line in path.open(encoding='utf-8'):
m = json.loads(line)
root = m['in'].split(' ')[0]
if only and root != only:
continue
key = (m['kind'], describe(m['vanilla'], m['kind']), describe(m['rust'], m['kind']))
if m['kind'] == 'parse':
key = key[:1] + (key[1][:90], key[2][:90])
groups[key] += 1
examples.setdefault(key, m)
for key, count in sorted(groups.items(), key=lambda kv: -kv[1])[:int(sys.argv[3]) if len(sys.argv) > 3 else 40]:
ex = examples[key]
print(f'{count:6} {key[0]} vanilla={key[1]} rust={key[2]}\n e.g. {ex["in"]!r}')
-79
View File
@@ -1,79 +0,0 @@
#!/usr/bin/env python3
"""Structural diff between vanilla's exported command tree and Horizon's.
usage: command_tree_diff.py VANILLA_TREE.json RUST_TREE.json [ROOT]
Reports, for every shared node, the children Horizon lacks or adds, differing
argument parsers and executable/redirect flags. Missing subtrees are reported
once at their root.
"""
import json
import sys
LOOSE = {
# Horizon's coarse argument kinds that stand in for several vanilla parsers.
'brigadier:string': {'brigadier:string', 'minecraft:message', 'minecraft:component'},
}
def same_parser(vanilla, rust):
if vanilla == rust:
return True
return False
def keyed(children):
"""Literals by name; arguments by position (their names are internal)."""
out = {}
index = 0
for name, child in children.items():
if child['type'] == 'literal':
out[('l', name)] = (name, child)
else:
out[('a', index)] = (name, child)
index += 1
return out
def diff(path, v, r, out):
vc, rc = keyed(v.get('children', {})), keyed(r.get('children', {}))
for key, (name, node) in vc.items():
if key not in rc:
out.append(('missing', path + [name], node.get('parser') or 'literal', count(node)))
for key, (name, node) in rc.items():
if key not in vc:
out.append(('extra', path + [name], node.get('parser') or 'literal', count(node)))
for key, (name, a) in vc.items():
if key not in rc:
continue
_, b = rc[key]
if a['type'] == 'argument' and not same_parser(a['parser'], b['parser']):
out.append(('parser', path + [name], f"{a['parser']} vs {b['parser']}", 0))
if bool(a.get('executable')) != bool(b.get('executable')):
out.append(('executable', path + [name], f"vanilla={bool(a.get('executable'))}", 0))
if bool(a.get('redirect')) != bool(b.get('redirect')):
out.append(('redirect', path + [name], f"vanilla={bool(a.get('redirect'))}", 0))
diff(path + [name], a, b, out)
def count(node):
return 1 + sum(count(c) for c in node.get('children', {}).values())
def main():
vanilla = json.load(open(sys.argv[1]))
rust = json.load(open(sys.argv[2]))
only = sys.argv[3] if len(sys.argv) > 3 else None
if only:
vanilla = {'children': {only: vanilla['children'][only]}, 'type': 'root'}
rust = {'children': {only: rust['children'].get(only, {'children': {}, 'type': 'literal'})}, 'type': 'root'}
out = []
diff([], vanilla, rust, out)
for kind, path, detail, size in out:
print(f'{kind:10} {" ".join(path)} [{detail}]' + (f' ({size} nodes)' if size else ''))
missing = sum(size for kind, _, _, size in out if kind == 'missing')
print(f'-- {len(out)} differences, {missing} vanilla nodes missing in Horizon')
if __name__ == '__main__':
main()
-91
View File
@@ -1,91 +0,0 @@
#!/usr/bin/env python3
"""Compare Rust container loot (examples/loot_fill) with vanilla LootTable.fill.
usage: compare_loot_fill.py RESOURCES TABLES SEED...
TABLES is a comma-separated list or `chests` for every chest table. Builds the
loot_fill example, runs VanillaLootFillOracle and diffs slot contents
(enchantments compared as sets). Tables vanilla cannot fill without a level
(exploration maps) are reported and skipped.
"""
import collections
import os
import subprocess
import sys
from pathlib import Path
try:
from . import lab, test_rust_parity as base
except ImportError:
import lab
import test_rust_parity as base
def build_dir():
return Path(os.environ['HORIZON_ORACLE_BUILD']) if os.environ.get('HORIZON_ORACLE_BUILD') else lab.LOCAL/'loot-fill-oracle'
def run_oracle(out, tables, seeds):
lock, jars, libraries = base.verify_artifacts()
source = lab.ROOT/'mods/horizon/src/test/java/dev/horizonlab/parity/VanillaLootFillOracle.java'
build = build_dir()
classes = build/'classes'
classes.mkdir(parents=True, exist_ok=True)
dependencies = os.pathsep.join(map(str, [jars['server'], *libraries]))
compiled = subprocess.run(['javac', '--release', str(lock['minecraft']['java_major']), '-proc:none', '-cp', dependencies,
'-d', str(classes), str(source)], capture_output=True, text=True)
if compiled.returncode:
raise SystemExit('javac failed:\n' + compiled.stderr[-3000:])
result = subprocess.run(['java', '-Xmx2G', '-cp', str(classes)+os.pathsep+dependencies,
'dev.horizonlab.parity.VanillaLootFillOracle', str(out), tables, *seeds],
capture_output=True, text=True)
if result.returncode:
lines = [line for line in (result.stdout + result.stderr).splitlines() if 'Exception' in line or 'Caused' in line]
raise SystemExit('oracle failed: ' + ' | '.join(lines[:5]))
def parse(lines):
fills, errors = collections.defaultdict(list), set()
for line in lines:
t = line.split()
if not t:
continue
if t[0] == 'table':
fills[(t[1], t[2])]
elif t[0] == 'error':
errors.add((t[1], t[2]))
elif t[0] == 'fill':
fills[(t[1], t[2])].append((int(t[3]), t[4], t[5], t[6], tuple(sorted(t[7:]))))
return fills, errors
def main():
resources, tables, seeds = sys.argv[1], sys.argv[2], sys.argv[3:]
out = build_dir()/'vanilla.txt'
out.parent.mkdir(parents=True, exist_ok=True)
run_oracle(out, tables, seeds)
vanilla, errors = parse(out.read_text().splitlines())
names = sorted({k[0] for k in vanilla} | {k[0] for k in errors})
subprocess.run(['cargo', 'build', '--profile', 'fast', '-q', '-p', 'horizon-world', '--example', 'loot_fill'],
check=True, cwd=lab.ROOT)
rust_out = subprocess.run([str(lab.cargo_target()/'fast/examples/loot_fill'), resources, ','.join(names), *seeds],
check=True, capture_output=True, text=True).stdout
rust, _ = parse(rust_out.splitlines())
matched = differ = 0
for key in sorted(vanilla):
if key in errors:
continue
if vanilla[key] == rust.get(key):
matched += 1
continue
differ += 1
if differ <= 20:
print('DIFF', key, '\n vanilla', vanilla[key], '\n rust ', rust.get(key))
tables = collections.Counter(k[0] for k in vanilla if k not in errors and vanilla[k] != rust.get(k))
if tables:
print('differing tables:', dict(tables))
skipped = sorted({k[0] for k in errors})
print(f'{matched} of {matched + differ} fills match; skipped (needs a level): {skipped}')
return 1 if differ else 0
if __name__ == '__main__':
sys.exit(main())
-113
View File
@@ -1,113 +0,0 @@
#!/usr/bin/env python3
"""Compare structure placement (terrain + structure pieces, no placed features)
between vanilla and Rust for target chunks.
usage: compare_structure_blocks.py RESOURCES SEED DIMENSION CX,CZ...
Runs VanillaStructurePlacementOracle and the structure_blocks example and
reports, per chunk, the blocks and block entities that differ.
"""
import json
import os
import subprocess
import sys
from pathlib import Path
try:
from . import lab, test_rust_parity as base
except ImportError:
import lab
import test_rust_parity as base
def build_dir():
return Path(os.environ['HORIZON_ORACLE_BUILD']) if os.environ.get('HORIZON_ORACLE_BUILD') else lab.LOCAL/'structure-placement-oracle'
def run_oracle(out, args):
lock, jars, libraries = base.verify_artifacts()
source = lab.ROOT/'mods/horizon/src/test/java/dev/horizonlab/parity/VanillaStructurePlacementOracle.java'
classes = build_dir()/'classes'
classes.mkdir(parents=True, exist_ok=True)
dependencies = os.pathsep.join(map(str, [jars['server'], *libraries]))
compiled = subprocess.run(['javac', '--release', str(lock['minecraft']['java_major']), '-proc:none', '-cp', dependencies,
'-d', str(classes), str(source)], capture_output=True, text=True)
if compiled.returncode:
raise SystemExit('javac failed:\n' + compiled.stderr[-3000:])
result = subprocess.run(['java', '-Xmx4G', '-cp', str(classes)+os.pathsep+dependencies,
'dev.horizonlab.parity.VanillaStructurePlacementOracle', str(out), *args],
capture_output=True, text=True)
if result.returncode:
lines = [line for line in (result.stdout + result.stderr).splitlines() if 'Exception' in line or 'Error' in line or '\tat ' in line]
raise SystemExit('oracle failed: ' + ' | '.join(lines[:8]))
def parse(lines):
chunks, current = {}, None
for line in lines:
t = line.split()
if not t:
continue
if t[0] == 'chunk':
current = (int(t[1]), int(t[2]))
chunks[current] = {'rows': {}, 'be': []}
elif t[0] == 'y' and current:
chunks[current]['rows'][int(t[1])] = t[2:]
elif t[0] == 'be' and current:
# Position, loot table and seed; the block entity type follows
# from the (separately compared) block state.
chunks[current]['be'].append(' '.join(t[1:4] + t[5:]))
return chunks
def state_names(resources):
names = {}
report = lab.LOCAL/'datagen/out/reports/blocks.json'
if report.exists():
for block, info in json.loads(report.read_text()).items():
for state in info['states']:
props = ','.join(f'{k}={v}' for k, v in sorted(state.get('properties', {}).items()))
names[str(state['id'])] = block + (f'[{props}]' if props else '')
return names
def main():
resources, seed, dimension, targets = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4:]
out = build_dir()/'vanilla.txt'
out.parent.mkdir(parents=True, exist_ok=True)
run_oracle(out, [seed, dimension, *targets])
vanilla = parse(out.read_text().splitlines())
subprocess.run(['cargo', 'build', '--profile', 'fast', '-q', '-p', 'horizon-world', '--example', 'structure_blocks'],
check=True, cwd=lab.ROOT)
rust_out = subprocess.run([str(lab.cargo_target()/'fast/examples/structure_blocks'), resources, seed, dimension, *targets],
check=True, capture_output=True, text=True).stdout
(build_dir()/'rust.txt').write_text(rust_out)
rust = parse(rust_out.splitlines())
names = state_names(resources)
failed = 0
for key in sorted(vanilla):
v, r = vanilla[key], rust.get(key)
if r is None:
print(key, 'missing from Rust')
failed += 1
continue
diffs = []
for y, row in v['rows'].items():
other = r['rows'].get(y, [])
for i, (a, b) in enumerate(zip(row, other)):
if a != b:
diffs.append((key[0] * 16 + i % 16, y, key[1] * 16 + i // 16, a, b))
total = sum(len(row) for row in v['rows'].values())
be_v, be_r = set(v['be']), set(r['be'])
status = 'match' if not diffs and be_v == be_r else 'DIFF'
print(f'chunk {key}: {status}; {total - len(diffs)} of {total} blocks equal; block entities {len(be_v & be_r)} of {len(be_v)} equal')
for x, y, z, a, b in diffs[:12]:
print(f' {x} {y} {z} vanilla {names.get(a, a)} rust {names.get(b, b)}')
for e in sorted(be_v - be_r)[:6]:
print(' only vanilla be', e)
for e in sorted(be_r - be_v)[:6]:
print(' only rust be', e)
failed += status != 'match'
return 1 if failed else 0
if __name__ == '__main__':
sys.exit(main())
-55
View File
@@ -1,55 +0,0 @@
#!/usr/bin/env python3
"""Compare Rust structure starts (chunkgen_bench --starts) with the vanilla oracle.
usage: compare_structure_starts.py VANILLA RUST [--all]
Pieces are compared by type, bounding box and (for jigsaw pieces) position,
rotation, ground level delta and junction count. Without --all, vanilla
starts of structure types Rust does not generate yet are only counted.
"""
import collections
import sys
def load(path):
starts = {}
current = None
for line in open(path):
t = line.split()
if not t:
continue
if t[0] == 'start':
current = (int(t[1]), int(t[2]), t[3])
starts[current] = [' '.join(t[4:])]
elif t[0] == 'piece' and current:
starts[current].append(' '.join(t[1:14]))
return starts
def main():
vanilla, rust = load(sys.argv[1]), load(sys.argv[2])
generated = {k[2] for k in rust}
matched = 0
missing = collections.Counter()
for key in sorted(vanilla):
if key not in rust:
missing[key[2]] += 1
if key[2] in generated or '--all' in sys.argv:
print('missing', key)
continue
if vanilla[key] == rust[key]:
matched += 1
continue
v, r = vanilla[key], rust[key]
print('DIFF', key, 'pieces', len(v) - 1, len(r) - 1)
for i, (a, b) in enumerate(zip(v, r)):
if a != b:
print(' first difference at line', i, '\n vanilla', a, '\n rust ', b)
break
for key in sorted(set(rust) - set(vanilla)):
print('extra', key)
print(f'{matched} of {len(vanilla)} vanilla starts match exactly; not generated by Rust:', dict(missing))
return 0 if matched == len(rust) == sum(1 for k in vanilla if k[2] in generated) else 1
if __name__ == '__main__':
sys.exit(main())
-46
View File
@@ -1,46 +0,0 @@
#!/usr/bin/env python3
"""Copy the pinned version's indexed client assets that are not in the game
jar from your own official launcher install: language files and the language
list (pack.mcmeta), the unifont glyph archives and their font definitions, and
the built-in Programmer Art and High Contrast resource packs. Every object's
SHA-1 is verified. Nothing is downloaded; files go to the private client
resource directory (or --target) and are never committed."""
import argparse
from pathlib import Path
import shutil
import lab
def wanted(name):
return (name == 'pack.mcmeta'
or name.startswith('minecraft/lang/')
or name.startswith('minecraft/font/')
or name.startswith('minecraft/resourcepacks/'))
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--target', type=Path, default=lab.RESOURCES,
help='prepared resource directory (default: .local/horizon-client/<pinned version>)')
parser.add_argument('--minecraft-dir', type=Path,
help="official launcher game directory (default: the launcher's own)")
args = parser.parse_args()
lock, directory = lab.config()
manifest = lab.import_manifest(lock, directory, args.minecraft_dir)
store, index = lab.launcher_assets(manifest, args.minecraft_dir)
target = args.target
target.mkdir(parents=True, exist_ok=True)
shutil.copyfile(index, target / 'asset-index.json')
objects = lab.json.loads(index.read_text())['objects']
selected = {name: info for name, info in objects.items() if wanted(name)}
total = 0
for name, info in selected.items():
source = lab.launcher_object(store, info['hash'])
lab.import_copy(source, target / 'assets' / name, info['hash'])
total += info['size']
print(f'Client assets ready: {len(selected)} files, {total / 1e6:.1f} MB')
if __name__ == '__main__':
main()
-57
View File
@@ -1,57 +0,0 @@
#!/usr/bin/env python3
"""Measure the resource-dependent Behaviours::load portion of client startup.
Build the behaviour_load_profile example in the checkout to be measured first:
cargo build --release -p horizon-world --example behaviour_load_profile
Run the profiler from the repository root with the matching example binary.
This measures behaviour loading, not time to first rendered frame. With
--cache-dir, the example reads and refreshes the resolved snapshot there, as the
client does with its .horizon-cache folder: the first load of a run builds it
when it is missing, the next loads restore it.
"""
import argparse
import json
import re
import statistics
import subprocess
from pathlib import Path
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--binary", type=Path, default=Path("target/release/examples/behaviour_load_profile"))
parser.add_argument("--resources", type=Path, default=Path(".local/horizon-client/26.3"))
parser.add_argument("--cache-dir", type=Path,
help="snapshot folder (default: no snapshot, every load is uncached)")
parser.add_argument("--repeats", type=int, default=5)
parser.add_argument("--output", type=Path)
args = parser.parse_args()
if args.repeats < 1:
parser.error("--repeats must be at least one")
samples = []
for _ in range(args.repeats):
command = [str(args.binary.resolve()), str(args.resources.resolve())]
if args.cache_dir:
command.append(str(args.cache_dir.resolve()))
run = subprocess.run(command, text=True, capture_output=True, check=True)
values = {key: float(value) for key, value in
re.findall(r"^(registry_ms|behaviours_[0-2]_ms)=([\d.]+)$", run.stderr, re.M)}
if len(values) != 4:
raise RuntimeError(f"incomplete profile output: {run.stderr}")
samples.append(values)
summary = {key: {"median_ms": round(statistics.median(row[key] for row in samples), 3),
"samples_ms": [row[key] for row in samples]}
for key in samples[0]}
result = {"scope": "behaviour loader only; not full client startup",
"binary": str(args.binary), "resources": str(args.resources), "repeats": args.repeats,
"cache_dir": str(args.cache_dir) if args.cache_dir else None,
"metrics": summary}
print(json.dumps(result, indent=2))
if args.output:
args.output.parent.mkdir(parents=True, exist_ok=True)
args.output.write_text(json.dumps(result, indent=2) + "\n")
if __name__ == "__main__":
main()
-38
View File
@@ -1,38 +0,0 @@
#!/usr/bin/env python3
"""Run the vanilla structure-start oracle for a seed and chunk rectangle.
usage: structure_start_oracle.py OUT SEED X0 Z0 X1 Z1
"""
import os
import subprocess
from pathlib import Path
import sys
try:
from . import lab, test_rust_parity as base
except ImportError:
import lab
import test_rust_parity as base
def main():
out, seed, x0, z0, x1, z1 = sys.argv[1:7]
lock, jars, libraries = base.verify_artifacts()
source = lab.ROOT/'mods/horizon/src/test/java/dev/horizonlab/parity/VanillaStructureStartOracle.java'
build = Path(os.environ['HORIZON_ORACLE_BUILD']) if os.environ.get('HORIZON_ORACLE_BUILD') else lab.LOCAL/'structure-start-oracle'
classes = build/'classes'
classes.mkdir(parents=True, exist_ok=True)
dependencies = os.pathsep.join(map(str, [jars['server'], *libraries]))
compiled = subprocess.run(['javac', '--release', str(lock['minecraft']['java_major']), '-proc:none', '-cp', dependencies,
'-d', str(classes), str(source)], capture_output=True, text=True)
if compiled.returncode:
raise SystemExit('javac failed:\n' + compiled.stderr[-3000:])
result = subprocess.run(['java', '-Xmx2G', '-cp', str(classes)+os.pathsep+dependencies,
'dev.horizonlab.parity.VanillaStructureStartOracle', out, seed, x0, z0, x1, z1],
capture_output=True, text=True)
if result.returncode:
lines = [line for line in (result.stdout + result.stderr).splitlines() if 'Exception' in line or 'Caused' in line]
raise SystemExit('oracle failed: ' + ' | '.join(lines[:5]))
if __name__ == '__main__':
main()
-222
View File
@@ -1,222 +0,0 @@
#!/usr/bin/env python3
"""Check LAN shutdown when leaving a singleplayer world through the pause menu,
and that offline guests joining with their default names are not refused as the
host's profile (#195: everyone was "Player").
Requires a built horizon-client, prepared resources and a display on Unix:
xvfb-run -a python3 tools/test_lan_lifecycle.py --client target/fast/horizon-client
Worlds and settings are created in a temporary directory.
"""
import argparse
import json
import os
from pathlib import Path
import socket
import subprocess
import tempfile
import time
try:
from . import lab
except ImportError:
import lab
ROOT = Path(__file__).resolve().parents[1]
GROUP = '224.0.2.60'
DISCOVERY_PORT = 4445
def command(endpoint, line):
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as connection:
connection.settimeout(60)
connection.connect(str(endpoint))
connection.sendall((line + '\n').encode())
with connection.makefile('r') as reply:
result = json.loads(reply.readline())
assert 'error' not in result, (line, result)
return result
def wait_for(endpoint, line, predicate, timeout=60):
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
try:
result = command(endpoint, line)
except (FileNotFoundError, ConnectionRefusedError):
time.sleep(0.1)
continue
if predicate(result):
return result
time.sleep(0.1)
raise AssertionError(f'timed out waiting for {line}')
def announcement(scanner, port, timeout):
deadline = time.monotonic() + timeout
marker = f'[AD]{port}[/AD]'.encode()
while time.monotonic() < deadline:
scanner.settimeout(max(0.001, deadline - time.monotonic()))
try:
message, _ = scanner.recvfrom(4096)
except socket.timeout:
return False
if marker in message:
return True
return False
def scenario(endpoint, scanner):
wait_for(endpoint, 'screen', lambda value: value['screen'] == 'title')
command(endpoint, 'open LanLifecycle 0 creative')
wait_for(endpoint, 'status', lambda value: value['player'] is not None
and value['pending_chunks'] == 0)
assert command(endpoint, 'lan status') == {'open': False}
port = 0
for _ in range(2):
port = command(endpoint, f'lan open {port}')['port']
with socket.create_connection(('127.0.0.1', port), timeout=2):
pass
assert announcement(scanner, port, 5), 'LAN world was not advertised'
command(endpoint, 'key esc tap')
screen = wait_for(endpoint, 'screen', lambda value: any(
button['label'] == 'Save and Quit to Title' for button in value['buttons']))
assert screen['screen'] == 'paused', screen
command(endpoint, 'click-button Save and Quit to Title')
wait_for(endpoint, 'screen', lambda value: value['screen'] == 'title')
assert command(endpoint, 'lan status') == {'open': False}, 'LAN stayed open after leaving'
try:
connection = socket.create_connection(('127.0.0.1', port), timeout=2)
except ConnectionRefusedError:
pass
else:
connection.close()
raise AssertionError('LAN still accepts connections after leaving')
# Discard datagrams queued before shutdown, then listen for more than
# two announcement intervals (the pinger sends every 1.5 seconds).
scanner.setblocking(False)
try:
while True:
scanner.recvfrom(4096)
except BlockingIOError:
pass
assert not announcement(scanner, port, 3.5), 'LAN kept advertising after leaving'
command(endpoint, 'open LanLifecycle')
wait_for(endpoint, 'status', lambda value: value['player'] is not None
and value['pending_chunks'] == 0)
assert command(endpoint, 'lan status') == {'open': False}, 'LAN reopened with the world'
command(endpoint, 'lan open 0')
command(endpoint, 'quit')
def start_client(client, resources, home, endpoint, log, *extra):
return subprocess.Popen([
str(client), '--resources', str(resources), '--render-distance', '2',
'--test-terrain', '--control-socket', str(endpoint), '--timeout-seconds', '180', *extra,
], cwd=home, env=dict(os.environ, HORIZON_HOME=str(home), HORIZON_NO_AUDIO='1',
HORIZON_PLAYER_NAME=''),
stdout=log, stderr=subprocess.STDOUT)
def guest_names(endpoint, port, guests):
"""Two guests with default names join together; a guest named like the host is refused."""
names = []
for home, guest in guests:
wait_for(guest, 'screen', lambda value: value['screen'] == 'title')
command(guest, f'connect 127.0.0.1:{port}')
wait_for(guest, 'status', lambda value: (value.get('server') or {}).get('joined'))
kept = (home / 'horizon-player.txt').read_text()
assert kept.startswith('name:Player') and kept.strip() != 'name:Player', kept
names.append(kept.strip().removeprefix('name:'))
assert names[0] != names[1], names
status = wait_for(endpoint, 'lan status', lambda value: len(value['guests']) == 2)
assert sorted(g['name'] for g in status['guests']) == sorted(names), status
# The same home keeps its name: leave and join again as the same profile.
home, guest = guests[0]
command(guest, 'disconnect')
wait_for(endpoint, 'lan status', lambda value: len(value['guests']) == 1)
command(guest, f'connect 127.0.0.1:{port}')
wait_for(guest, 'status', lambda value: (value.get('server') or {}).get('joined'))
assert (home / 'horizon-player.txt').read_text().strip() == f'name:{names[0]}'
# A guest that really is the host's offline profile is refused, the others stay.
command(guest, 'disconnect')
command(guest, f'connect 127.0.0.1:{port} Player')
refused = wait_for(guest, 'screen', lambda value: (value.get('menu') or {}).get('disconnected'))
assert 'another location' in refused['menu']['disconnected'], refused
status = command(endpoint, 'lan status')
assert [g['name'] for g in status['guests']] == [names[1]], status
return names
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--client', type=Path, required=True)
parser.add_argument('--resources', type=Path, default=lab.RESOURCES)
args = parser.parse_args()
client, resources = args.client.resolve(), args.resources.resolve()
with tempfile.TemporaryDirectory(prefix='horizon-lan-') as directory:
work = Path(directory)
endpoint = work / 'control.sock'
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as scanner:
scanner.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
scanner.bind(('', DISCOVERY_PORT))
scanner.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP,
socket.inet_aton(GROUP) + socket.inet_aton('0.0.0.0'))
with (work / 'client.log').open('w+') as log:
process = start_client(client, resources, work, endpoint, log)
try:
scenario(endpoint, scanner)
assert process.wait(timeout=20) == 0, 'client exited unsuccessfully'
except Exception:
log.seek(0)
print('\n'.join(log.read().splitlines()[-20:]))
raise
finally:
stop(process)
names = guests_scenario(client, resources, work)
print('LAN lifecycle passed: leaving stops hosting and discovery; reopening stays private')
print(f'LAN guests passed: default names {names[0]} and {names[1]} joined together and kept; '
'"Player" refused as the host\'s profile')
def stop(process):
if process.poll() is None:
process.terminate()
try:
process.wait(timeout=10)
except subprocess.TimeoutExpired:
process.kill()
process.wait(timeout=10)
def guests_scenario(client, resources, work):
host_home = work / 'host'
homes = [work / 'guest-a', work / 'guest-b']
processes, logs = [], []
try:
for home in [host_home, *homes]:
home.mkdir()
log = (home / 'client.log').open('w+')
logs.append(log)
processes.append(start_client(client, resources, home, home / 'control.sock', log))
endpoint = host_home / 'control.sock'
wait_for(endpoint, 'screen', lambda value: value['screen'] == 'title')
command(endpoint, 'open LanGuests 0 creative')
wait_for(endpoint, 'status', lambda value: value['player'] is not None
and value['pending_chunks'] == 0)
port = command(endpoint, 'lan open 0')['port']
return guest_names(endpoint, port, [(home, home / 'control.sock') for home in homes])
except Exception:
for log in logs:
log.seek(0)
print(f'--- {log.name}')
print('\n'.join(log.read().splitlines()[-20:]))
raise
finally:
for process in processes:
stop(process)
for log in logs:
log.close()
if __name__ == '__main__':
main()