Merge origin/main into fix/vehicle-passenger-pose

Keeps main's crystals check and this branch's boat-dismount-pose check in tools/lan_e2e.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-25 14:29:29 +01:00
102 changed files with 10890 additions and 477 deletions
+287
View File
@@ -0,0 +1,287 @@
#!/usr/bin/env python3
"""End-to-end check of Discord Rich Presence: real games talk to a fake Discord.
Each game gets its own fake Discord IPC server (a Unix socket in a scratch folder, pointed to by
HORIZON_DISCORD_IPC_DIR) that answers the handshake with READY and records every SET_ACTIVITY.
The game goes title -> world -> Nether -> quit through horizon-ctl, and the activities must say
"In the menus", "Singleplayer" in the Overworld, then the Nether, and be cleared on quit:
single single player on its server thread (the default)
in-frame single player ticked in the window's frame (--in-frame)
lan a host opens its world to LAN and a guest joins: the guest shows "Multiplayer",
the host "Hosting a world" with a party of 2 of 8 (lan-in-frame: the host in-frame)
setting off in horizon-extras.txt: nothing reaches Discord; the Horizon Settings switch
turns it on (the activity appears) and off (it is cleared)
late Discord starts after the game: found within the 15 s retry
python3 tools/discord_presence_e2e.py # all of them
python3 tools/discord_presence_e2e.py --check lan --bin-dir target/fast
macOS and Linux (Discord's Windows pipes aren't faked here). Games render offscreen, have no audio
and use scratch homes (lab.game_env). Needs the prepared resources in .local/horizon-client.
"""
import argparse
import json
import os
import shutil
import socket
import struct
import subprocess
import sys
import tempfile
import threading
import time
from pathlib import Path
try:
from . import lab
except ImportError:
import lab
ROOT = Path(__file__).resolve().parents[1]
HANDSHAKE, FRAME, CLOSE = 0, 1, 2
class FakeDiscord:
"""discord-ipc-0 in a scratch folder: READY on handshake, a reply to each command."""
def __init__(self):
# Short: Unix socket paths are limited to ~100 bytes.
self.dir = Path(tempfile.mkdtemp(prefix='hzdc-', dir='/tmp'))
self.server = None
self.activities = [] # (seconds since start, activity or None)
self.handshakes = []
self.started = time.time()
self.lock = threading.Lock()
def start(self):
"""Discord starts: its socket appears."""
self.server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.server.bind(str(self.dir / 'discord-ipc-0'))
self.server.listen()
threading.Thread(target=self.accept, daemon=True).start()
def accept(self):
while True:
try:
conn, _ = self.server.accept()
except OSError:
return
threading.Thread(target=self.serve, args=(conn,), daemon=True).start()
def serve(self, conn):
def read():
header = conn.recv(8, socket.MSG_WAITALL)
if len(header) < 8:
raise EOFError
op, length = struct.unpack('<II', header)
return op, json.loads(conn.recv(length, socket.MSG_WAITALL) or b'null')
def send(op, payload):
body = json.dumps(payload).encode()
conn.sendall(struct.pack('<II', op, len(body)) + body)
try:
while True:
op, msg = read()
if op == HANDSHAKE:
with self.lock:
self.handshakes.append(msg)
send(FRAME, {'cmd': 'DISPATCH', 'evt': 'READY', 'data': {'v': 1}})
elif op == FRAME and msg.get('cmd') == 'SET_ACTIVITY':
with self.lock:
self.activities.append((round(time.time() - self.started, 1), msg['args']['activity']))
send(FRAME, {'cmd': 'SET_ACTIVITY', 'nonce': msg['nonce'], 'data': msg['args']['activity']})
elif op == CLOSE:
return
except (EOFError, OSError, ValueError):
pass
finally:
conn.close()
def last(self):
with self.lock:
return self.activities[-1][1] if self.activities else 'nothing yet'
def wait(self, what, check, timeout=60):
"""Waits until the latest activity passes `check` (Discord's limit can hold one back 20 s)."""
deadline = time.time() + timeout
while time.time() < deadline:
if check(self.last()):
print(f' ok: {what}: {json.dumps(self.last())}')
return
time.sleep(0.25)
raise AssertionError(f'{what}: the last activity was {self.last()!r}; all: {self.activities}')
def close(self):
if self.server:
self.server.close()
shutil.rmtree(self.dir, ignore_errors=True)
def shows(details, state=None):
return lambda a: isinstance(a, dict) and a.get('details') == details and (state is None or a.get('state') == state)
class Game:
"""One game started by horizon-ctl in a scratch home, with its own fake Discord."""
def __init__(self, bins, name, args=(), extras=None, discord_running=True):
self.ctl = bins / 'horizon-ctl'
self.home = Path(tempfile.mkdtemp(prefix=f'hzdp-{name}-'))
if extras:
(self.home / 'horizon-extras.txt').write_text(extras)
self.socket = self.home / 'c.sock'
self.discord = FakeDiscord()
if discord_running:
self.discord.start()
extra = {'HORIZON_HOME': self.home, 'HORIZON_TELEMETRY_URL': 'off', 'HORIZON_DISCORD_IPC_DIR': self.discord.dir}
self.env = lab.game_env(**extra)
self.ctl_run('launch', '--offscreen', '--game', str(bins / 'horizon-client'), '--resources',
str(lab.RESOURCES.resolve()), *args)
def ctl_run(self, *words, timeout=600):
r = subprocess.run([str(self.ctl), '--socket', str(self.socket), *words], cwd=self.home, env=self.env,
capture_output=True, text=True, timeout=timeout)
out = r.stdout.strip().splitlines()
reply = json.loads(out[-1]) if out else {'error': r.stderr.strip()}
if r.returncode or 'error' in reply:
raise AssertionError(f'horizon-ctl {" ".join(words)}: exit {r.returncode}: {reply}')
return reply
def quit(self):
try:
self.ctl_run('quit', timeout=120)
except Exception as e: # the reply can be lost as the game exits
print(f' quit: {e}')
def close(self):
self.discord.close()
shutil.rmtree(self.home, ignore_errors=True)
def single(bins, args=()):
game = Game(bins, 'single', args=args)
d = game.discord
try:
d.wait('title screen', shows('In the menus'))
assert d.handshakes[0]['v'] == 1 and d.handshakes[0]['client_id'], d.handshakes
game.ctl_run('open', 'Presence', '5', 'creative')
game.ctl_run('wait-for', 'loaded', '--timeout', '300')
d.wait('in the world', shows('Singleplayer', 'In the Overworld'))
start = d.last()['timestamps']['start']
assert abs(start - time.time()) < 120 and 'party' not in d.last(), d.last()
game.ctl_run('command', 'execute in minecraft:the_nether run tp @s 0 80 0')
d.wait('in the Nether', shows('Singleplayer', 'In the Nether'))
assert d.last()['timestamps']['start'] == start, 'a dimension change restarted the clock'
text = json.dumps(d.activities)
assert 'Presence' not in text, f'the world name reached Discord: {text}'
game.quit()
d.wait('cleared on quit', lambda a: a is None)
finally:
game.close()
def lan(bins, host_args=()):
host = Game(bins, 'host', args=host_args)
guest = None
try:
host.ctl_run('open', 'Hosted', '5', 'creative')
host.ctl_run('wait-for', 'loaded', '--timeout', '300')
port = lab.free_port()
host.ctl_run('lan', 'open', str(port))
host.discord.wait('host, alone', lambda a: shows('Hosting a world')(a) and a['party']['size'] == [1, 8])
guest = Game(bins, 'guest')
guest.discord.wait('guest title screen', shows('In the menus'))
guest.ctl_run('connect', f'127.0.0.1:{port}', 'Guest')
guest.ctl_run('wait-for', 'loaded', '--timeout', '300')
guest.discord.wait('guest in the world', shows('Multiplayer', 'In the Overworld'))
assert '127.0.0.1' not in json.dumps(guest.discord.activities), guest.discord.activities
host.discord.wait('host with a guest', lambda a: shows('Hosting a world')(a) and a['party']['size'] == [2, 8])
guest.quit()
guest.discord.wait('guest cleared on quit', lambda a: a is None)
host.quit()
host.discord.wait('host cleared on quit', lambda a: a is None)
finally:
for g in (guest, host):
if g:
g.close()
def setting(bins):
"""Off in horizon-extras.txt: Discord never hears from the game. Turned on in Horizon Settings: the
activity appears; off again: it is cleared."""
game = Game(bins, 'setting', extras='discordPresence:false\n')
d = game.discord
try:
game.ctl_run('wait-for', 'screen', 'title')
time.sleep(3)
assert not d.handshakes and not d.activities, (d.handshakes, d.activities)
game.ctl_run('options', 'horizon')
switch = next(w for w in game.ctl_run('ui')['widgets'] if w.get('label', '').startswith('Discord Rich'))
assert switch['label'] == 'Discord Rich Presence: Off', switch
game.ctl_run('click', switch['id'])
d.wait('turned on', shows('In the menus'))
game.ctl_run('click', switch['id'])
d.wait('turned off', lambda a: a is None)
assert 'discordPresence:false' in (game.home / 'horizon-extras.txt').read_text()
print(' ok: saved off')
game.quit()
finally:
game.close()
def late(bins):
"""Discord started after the game: it is found at the next retry (every 15 s)."""
game = Game(bins, 'late', discord_running=False)
try:
game.ctl_run('wait-for', 'screen', 'title')
time.sleep(2)
game.discord.start()
started = time.time()
game.discord.wait('found Discord', shows('In the menus'), timeout=30)
print(f' ok: after {time.time() - started:.1f} s')
game.quit()
finally:
game.close()
CHECKS = {'single': single, 'in-frame': lambda b: single(b, ['--in-frame']), 'lan': lan,
'lan-in-frame': lambda b: lan(b, ['--in-frame']), 'setting': setting,
'late': late}
def main():
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('--check', choices=[*CHECKS, 'all'], default='all')
parser.add_argument('--bin-dir', help='prebuilt horizon-client and horizon-ctl (default: build with the fast profile)')
args = parser.parse_args()
if sys.platform == 'win32':
sys.exit('the fake Discord is a Unix socket: macOS or Linux only')
if args.bin_dir:
bins = Path(args.bin_dir).resolve()
else:
subprocess.run(['cargo', 'build', '--locked', '--profile', 'fast', '-p', 'horizon-client', '--bins'],
cwd=ROOT, check=True)
meta = json.loads(subprocess.run(['cargo', 'metadata', '--format-version', '1', '--no-deps'], cwd=ROOT,
capture_output=True, text=True, check=True).stdout)
bins = Path(meta['target_directory']) / 'fast'
if not (lab.RESOURCES / 'horizon-resources.json').exists():
sys.exit(f'no prepared resources in {lab.RESOURCES}')
results = {}
for name in CHECKS if args.check == 'all' else [args.check]:
print(f'[{name}]')
started = time.time()
try:
CHECKS[name](bins)
results[name] = True
print(f'[{name}] PASS in {time.time() - started:.0f} s')
except (AssertionError, KeyError, StopIteration, subprocess.TimeoutExpired) as e:
results[name] = False
print(f'[{name}] FAIL: {e}')
print(json.dumps(results))
sys.exit(0 if all(results.values()) else 1)
if __name__ == '__main__':
main()
+4 -1
View File
@@ -72,7 +72,7 @@ def _remove_scratch_dirs():
def game_env(base=None, **extra):
"""The environment for every run of the game, the launcher or horizon-ctl from a tool or a harness: no audio,
and fresh scratch folders for the game's home (logs, saves, options), the launcher's home and the XDG folders,
no Discord presence, and fresh scratch folders for the game's home (logs, saves, options), the launcher's home and the XDG folders,
so a run never writes into a real player's folders (the game falls back to them when HORIZON_HOME is unset).
A folder the caller already has in `base` (default os.environ) or passes in `extra` is kept. The scratch folders
are removed when this process exits."""
@@ -80,6 +80,9 @@ def game_env(base=None, **extra):
env.update({key: str(value) for key, value in extra.items()})
env["HORIZON_NO_AUDIO"] = "1"
env["HORIZON_LAUNCHER_NO_SELF_UPDATE"] = "1"
# Never a real Discord's Rich Presence; tools/discord_presence_e2e.py points the game at a fake one.
if not env.get("HORIZON_DISCORD_IPC_DIR"):
env["HORIZON_NO_DISCORD"] = "1"
for key, prefix in (("HORIZON_HOME", "horizon-home-"), ("HORIZON_LAUNCHER_HOME", "horizon-launcher-home-"),
("XDG_DATA_HOME", "horizon-xdg-data-"), ("XDG_CONFIG_HOME", "horizon-xdg-config-")):
if not env.get(key):
+90
View File
@@ -117,6 +117,8 @@ address. The script then checks, from both sides, what a guest's game used to ge
the host has them; a falling block lands on both sides.
- shulker_bullets: every shot a shulker fires has its own id and flies on both sides, following
the same trajectory; the bullets disappear on both sides when they hit blocks (#1494).
- crystals: the host and the guest punch placed End crystals in survival and creative; the blast
kills the survival player, and attacks respect reach and blocks in front (#1532).
- thrown: a splash potion and an eye of ender the host and the guest each throw fly on both
sides (#1363, #1391);
- sparks: level events' particles on both sides: the host's and the guest's bone meal on wheat
@@ -6162,6 +6164,94 @@ CHECKS += ['falling']
CHECK_FNS.update(falling=check_falling)
def check_crystals(host, guest, out):
"""#1532: a real left click detonates a placed End crystal in survival and creative,
on the host and the guest; reach, block occlusion and passing right clicks still apply."""
spots = apart(host, guest)
seen = {}
def crystals(game, at):
return [p for p in game('status')['projectiles'] if p['kind'] == 'minecraft:end_crystal'
and math.dist(p['position'], at) < 0.01]
for label, player, other in (('host', host, guest), ('guest', guest, host)):
x, y, z = spots[label]
y += 32 # Above the terrain and its entities: EndCrystalItem requires an empty box.
at = [x + 2.5, y, z + 0.5]
other('command', command='gamemode creative')
for mode in ('creative', 'survival'):
case = f'{label}-{mode}'
try:
host('command', command=f'fill {x - 5} {y - 1} {z - 3} {x + 5} {y - 1} {z + 3} minecraft:obsidian')
host('command', command=f'fill {x - 5} {y} {z - 3} {x + 5} {y + 3} {z + 3} minecraft:air')
player('command', command=f'gamemode {mode}')
player('command', command='clear @s')
player('command', command='effect clear @s')
player('command', command='effect give @s minecraft:instant_health 1 5 true')
player('command', command=f'tp @s {x + 0.5} {y} {z + 0.5}')
player('command', command='give @s minecraft:end_crystal 1')
player.raw('select', 1)
player.wait(10)
player.raw('look-at', at[0], y - 0.01, at[2])
player.raw('mouse', 'right', 'click')
player.wait(10)
placed = crystals(player, at)
assert len(placed) == 1, f'{case}: placing the crystal: {placed}'
crystal_id = placed[0]['id']
tracked = crystals(other, at)
assert crystal_id != 0 and [p['id'] for p in tracked] == [crystal_id], f'{case}: crystal not tracked on both sides'
# The nearest face is six blocks away, beyond both modes' attack reach.
player('command', command=f'tp @s {x - 4.5} {y} {z + 0.5}')
player.wait(5)
player.raw('look-at', at[0], y + 1, at[2])
player.raw('mouse', 'left', 'click')
player.wait(10)
assert crystals(player, at), f'{case}: attacked a crystal out of reach'
player('command', command=f'tp @s {x + 0.5} {y} {z + 0.5}')
host('command', command=f'fill {x + 1} {y} {z} {x + 1} {y + 2} {z} minecraft:obsidian')
player.wait(5)
player.raw('look-at', at[0], y + 1, at[2])
player.raw('mouse', 'left', 'click')
player.wait(10)
assert crystals(player, at), f'{case}: attacked a crystal through a block'
host('command', command=f'fill {x + 1} {y} {z} {x + 1} {y + 2} {z} minecraft:air')
player('command', command='item replace entity @s weapon.mainhand with minecraft:shield')
player.wait(10)
player.raw('look-at', at[0], y + 1, at[2])
player.raw('mouse', 'right', 'down')
player.wait(10)
assert player('state').get('using') and server_using(host, player), f'{case}: crystal consumed right click'
player.raw('mouse', 'right', 'up')
player('command', command='clear @s')
player.wait(5)
player.shot(out, f'crystals-{mode}-before')
player.raw('mouse', 'left', 'click')
player.wait(40)
remaining = {g.name: len(crystals(g, at)) for g in (player, other)}
health = server_player(host, player)['health']
screen = player('state').get('screen')
seen[case] = {'id': crystal_id, 'remaining': remaining, 'health': health, 'screen': screen}
player.shot(out, f'crystals-{mode}-after')
assert not any(remaining.values()), f'{case}: left click left the crystal intact: {seen[case]}'
assert health == (20 if mode == 'creative' else 0), f'{case}: explosion damage: {seen[case]}'
assert screen == ('playing' if mode == 'creative' else 'dead'), f'{case}: explosion death: {seen[case]}'
finally:
if player('state').get('screen') == 'dead':
respawn_when_ready(player)
player.wait(30)
else:
player.raw('mouse', 'right', 'up')
player('command', command='gamemode creative')
return seen
CHECKS += ['crystals']
CHECK_FNS.update(crystals=check_crystals)
def check_boat_dismount_pose(host, guest, out):
"""Removing a boat detaches its mobs on the server and every observing client."""
x, y, z = map(math.floor, pos_of(host))