mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +00:00
* feat: Epic Games Launcher for Linux Install Epic's official Linux launcher (EGLv2) from Epic's CDN with Valve's Steam Runtime 4 and run it under FEX, signed in with the linked Epic account. Epic games launch through a hidden launcher instance so they get the launcher's own arguments, with the exchange-code launch as the fallback, and DroidDeck installs show up in the launcher as installed. In the launcher, touch scrolls like a phone app and the gamepad drives the UI: D-pad and left stick move the selection, A opens, B goes back, the right stick and triggers scroll proportionally, X opens the options menu, Y the keyboard and LB/RB switch tabs. * feat: Epic Games Launcher for Linux Install Epic's official Linux launcher (EGLv2) from Epic's CDN with Valve's Steam Runtime 4 and run it under FEX, signed in with the linked Epic account. Epic games launch through a hidden launcher instance so they get the launcher's own arguments, with the exchange-code launch as the fallback, and DroidDeck installs show up in the launcher as installed. In the launcher, touch scrolls like a phone app and the gamepad drives the UI: D-pad and left stick move the selection, A opens, B goes back, the right stick and triggers scroll proportionally, X opens the options menu, Y the keyboard and LB/RB switch tabs. * Route Epic games through Steam, import launcher installs, offer the second library - Every Epic launch goes Steam -> Epic Games Launcher -> game; Play inside the open launcher hands the game to Steam - Games installed from inside the launcher get a Games-tab entry and launch through Steam; the launcher installs to the last Stores target - Stores install picker offers the Steam second library, with side-by-side choices - uruntime xattr-only failures no longer fail the launcher unpack (app-extract) * Hand launcher Play and Steam links to Steam, wait for FEX on first run Play on a game the launcher just installed is matched to its install record and handed to Steam. Steam games listed in the launcher open steam://rungameid links, which now start the game in a Steam session. Opening the launcher before Steam has downloaded FEX asks Steam for it and starts Steam instead of failing with no-fex. * Address review: FEX check, play-in-Steam action, touch route thread, scroll scale fexReady needs a FEX binary in Steam's FEX tool, not just its folder. The play-in-Steam action stays until the link is handled, so Run Steam at startup can't start a session first. The pad touch route reads the mouse mode without side effects off the main thread and posts the pad update there. Vertical scroll uses the output height. A status-2 unpack counts only when AppRun is there. The xdg-open command is tested end to end for Steam links. * Open the launcher through Steam, recover dropped shortcuts, sign out of both The Epic Games Launcher now runs as its own Steam shortcut, so opening it starts Steam, which opens the launcher, and Play inside it goes to the running Steam. Steam dropped a running shortcut from its library when the game exited after a sleep/wake replay or a server re-logon, and launching it then waited forever at ShowInterstitials. login1 only replays sleep/wake when Steam is asleep. The session restarts Steam when a requested shortcut is missing, and when a game exits with its shortcut gone and nothing else runs. Resuming after a long pause no longer reports failure while traced processes are still waking, so "Steam is paused" goes away on the first tap. Signing out of the launcher signs DroidDeck's Epic store out, and signing out in DroidDeck restarts the launcher signed out and clears the Epic web cookies. * Address review: one session-switch poll, Epic setup can't block a session A new switch confirmation replaces the pending poll instead of adding a second one that could start the session again. Epic library sync and install-folder setup at session start are each guarded, so a bad preference no longer stops the guest from launching. * Uninstall the Epic Games Launcher from the Games tab An Uninstall button next to Launch asks first, closes a running session, and removes the launcher and its runtime. The launcher's sign-in and install records stay, so its games keep their Games-tab entries and a reinstall comes back signed in. Its Steam shortcut goes away on the next Steam start. * Address review: uninstall only after the session has ended The launcher's files are removed only once the session is not running and its phase has ended; a shutdown still in progress after the wait reports failure instead. The confirmation reads the session state from the front-end state. * Find FEX that Steam installed on the second library Steam's tools on a second library live in DroidDeck's private copy, which the session binds over the library. The launcher's FEX check only looked on the shared storage, so with FEX there it always asked Steam for FEX and only Steam opened. It now checks the private copy too. * Start games without Steam, per-game Settings menu with Wine sync "Start without Steam" now really leaves the Steam client out: the game runs in its own Proton/FEX through droiddeck-proton-run in a run session, and the Epic Games Launcher opens on its own with its games playing in it. Run sessions for these games are not pinned to the big cores and get DirectAudio and the added-games list like Steam. The Games tab card is now "Settings", titled "<game> Settings": Start without Steam, FEX preset, Wine sync (ntsync, fsync, esync, wineserver or the app default) and Default, which puts everything changed for the game back to the app's settings. The app-wide Wine sync choice is shown in the Steam settings Session tab. * Address review: uninstall guard, routed games, shortcut matching Opening or reinstalling the Epic Games Launcher waits while it is being uninstalled. Added games that are Steam installs keep their Start without Steam setting under their Steam app id and start as that Steam game. Live shortcut lookups wait for Steam's library to load and match a same-name shortcut by its executable, so no duplicate is added. The session treats the open launcher as itself even when it could not tell its Steam app id. * Address review: drop a queued launcher switch when it is uninstalled A confirmed switch to the Epic Games Launcher that is still waiting for the old session to end is cancelled when the launcher is uninstalled, and never starts while the uninstall runs. The launcher id test sets its environment for its own runs only.
417 lines
22 KiB
Python
417 lines
22 KiB
Python
"""The guest agent: its request files, exec, errors and the DevTools client, without a session."""
|
|
import base64
|
|
import hashlib
|
|
import importlib.machinery
|
|
import importlib.util
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import socket
|
|
import struct
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import threading
|
|
import time
|
|
import unittest
|
|
from unittest.mock import patch
|
|
import tracemalloc
|
|
|
|
SCRIPT = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin/droiddeck-agent"
|
|
loader = importlib.machinery.SourceFileLoader("droiddeck_agent", str(SCRIPT))
|
|
spec = importlib.util.spec_from_loader(loader.name, loader)
|
|
agent = importlib.util.module_from_spec(spec)
|
|
loader.exec_module(agent)
|
|
|
|
|
|
class FakeDevTools(threading.Thread):
|
|
"""GET /json lists one target; its WebSocket answers Runtime.evaluate the way Chromium does."""
|
|
|
|
def __init__(self, answer):
|
|
super().__init__(daemon=True)
|
|
self.answer = answer
|
|
self.server = socket.socket()
|
|
self.server.bind(("127.0.0.1", 0))
|
|
self.server.listen(4)
|
|
self.port = self.server.getsockname()[1]
|
|
self.seen = []
|
|
|
|
def run(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):
|
|
head = b""
|
|
while b"\r\n\r\n" not in head:
|
|
head += conn.recv(4096)
|
|
request = head.decode()
|
|
if request.startswith("GET /json"):
|
|
body = json.dumps([{"title": "SharedJSContext", "url": "https://steamloopback.host/", "type": "page",
|
|
"webSocketDebuggerUrl": "ws://127.0.0.1:%d/devtools/page/1" % self.port}]).encode()
|
|
conn.sendall(b"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: %d\r\n\r\n" % len(body) + body)
|
|
conn.close()
|
|
return
|
|
key = [l.split(":", 1)[1].strip() for l in request.split("\r\n") if l.lower().startswith("sec-websocket-key")][0]
|
|
accept = base64.b64encode(hashlib.sha1((key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").encode()).digest()).decode()
|
|
conn.sendall(("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n"
|
|
"Sec-WebSocket-Accept: %s\r\n\r\n" % accept).encode())
|
|
b0, b1 = conn.recv(2)
|
|
n = b1 & 0x7F
|
|
if n == 126:
|
|
n = struct.unpack(">H", conn.recv(2))[0]
|
|
mask = conn.recv(4)
|
|
data = b""
|
|
while len(data) < n:
|
|
data += conn.recv(n - len(data))
|
|
message = json.loads(bytes(b ^ mask[i % 4] for i, b in enumerate(data)))
|
|
self.seen.append(message)
|
|
for reply in ({"method": "Runtime.consoleAPICalled", "params": {}}, dict(self.answer, id=message["id"])):
|
|
payload = json.dumps(reply).encode()
|
|
header = bytes([0x81, 126]) + struct.pack(">H", len(payload)) if len(payload) > 125 else bytes([0x81, len(payload)])
|
|
conn.sendall(header + payload)
|
|
time.sleep(0.2)
|
|
conn.close()
|
|
|
|
|
|
class AgentTest(unittest.TestCase):
|
|
def setUp(self):
|
|
self.temp = tempfile.TemporaryDirectory()
|
|
self.addCleanup(self.temp.cleanup)
|
|
self.dir = Path(self.temp.name)
|
|
(self.dir / "resp").mkdir()
|
|
|
|
def ask(self, request):
|
|
path = self.dir / "req.json"
|
|
path.write_text(json.dumps(request))
|
|
agent.answer(self.dir, path)
|
|
self.assertFalse(path.exists(), "the request file is consumed")
|
|
return json.loads((self.dir / "resp" / "req.json").read_text())
|
|
|
|
def vr_helper(self, body):
|
|
helper = self.dir / "droiddeck-steamvr"
|
|
helper.write_text("#!/bin/sh\n" + body)
|
|
old = os.environ.get("DROIDDECK_VR_HELPER")
|
|
os.environ["DROIDDECK_VR_HELPER"] = str(helper)
|
|
self.addCleanup(lambda: os.environ.pop("DROIDDECK_VR_HELPER") if old is None
|
|
else os.environ.__setitem__("DROIDDECK_VR_HELPER", old))
|
|
return helper
|
|
|
|
def test_vr_status_is_parsed(self):
|
|
self.vr_helper('echo "installed=1 running=0 installing=0 driver=1"\n')
|
|
answer = self.ask({"kind": "vr", "action": "status"})
|
|
self.assertTrue(answer["ok"])
|
|
self.assertEqual((True, False, False, True),
|
|
(answer["installed"], answer["running"], answer["installing"], answer["driver"]))
|
|
|
|
def test_vr_runs_only_its_own_actions(self):
|
|
log = self.dir / "args"
|
|
self.vr_helper('echo "$@" > %s\n' % log)
|
|
self.assertTrue(self.ask({"kind": "vr", "action": "start"})["ok"])
|
|
self.assertEqual("start", log.read_text().strip())
|
|
answer = self.ask({"kind": "vr", "action": "rm -rf /"})
|
|
self.assertFalse(answer["ok"])
|
|
self.assertEqual("INVALID_REQUEST", answer["error"]["code"])
|
|
|
|
def test_vr_without_a_helper_is_unavailable(self):
|
|
os.environ["DROIDDECK_VR_HELPER"] = str(self.dir / "missing")
|
|
self.addCleanup(os.environ.pop, "DROIDDECK_VR_HELPER")
|
|
answer = self.ask({"kind": "vr", "action": "status"})
|
|
self.assertEqual("UNAVAILABLE", answer["error"]["code"])
|
|
|
|
def test_exec_output_and_status(self):
|
|
answer = self.ask({"kind": "exec", "argv": ["sh", "-c", "echo out; echo err >&2; exit 3"], "env": {"X": "1"}})
|
|
self.assertTrue(answer["ok"])
|
|
self.assertEqual(3, answer["exitCode"])
|
|
self.assertEqual("out\n", answer["stdout"]["text"])
|
|
self.assertEqual("err\n", answer["stderr"]["text"])
|
|
self.assertFalse(answer["timedOut"])
|
|
|
|
def test_exec_environment_stdin_and_cap(self):
|
|
answer = self.ask({"kind": "exec", "argv": ["sh", "-c", "printf %s \"$X\"; cat"], "env": {"X": "a"}, "stdin": "b"})
|
|
self.assertEqual("ab", answer["stdout"]["text"])
|
|
big = self.ask({"kind": "exec", "argv": ["sh", "-c", "head -c 300000 /dev/zero"]})
|
|
self.assertTrue(big["stdout"]["truncated"])
|
|
self.assertEqual(agent.OUTPUT_CAP, len(big["stdout"]["text"]))
|
|
|
|
def test_exec_timeout_kills_the_process_group(self):
|
|
started = time.monotonic()
|
|
answer = self.ask({"kind": "exec", "argv": ["sh", "-c", "sleep 30 & sleep 30"], "timeout": 0.5})
|
|
self.assertTrue(answer["timedOut"])
|
|
self.assertLess(time.monotonic() - started, 10)
|
|
|
|
def test_exec_memory_is_bounded_while_draining_large_output(self):
|
|
tracemalloc.start()
|
|
try:
|
|
answer = agent.run_exec({"argv": [sys.executable, "-c", "import os; [os.write(1, b'x' * 65536) for _ in range(1024)]"]})
|
|
_, peak = tracemalloc.get_traced_memory()
|
|
finally:
|
|
tracemalloc.stop()
|
|
self.assertEqual(0, answer["exitCode"])
|
|
self.assertEqual(agent.OUTPUT_CAP, len(answer["stdout"]["text"]))
|
|
self.assertTrue(answer["stdout"]["truncated"])
|
|
self.assertLess(peak, 6 * 1024 * 1024)
|
|
|
|
def test_exec_drains_both_streams_while_writing_stdin(self):
|
|
program = "import os, sys; os.write(1, b'x' * 300000); print(len(sys.stdin.read()), file=sys.stderr)"
|
|
answer = self.ask({"kind": "exec", "argv": [sys.executable, "-c", program], "stdin": "y" * 100000})
|
|
self.assertFalse(answer["timedOut"])
|
|
self.assertTrue(answer["stdout"]["truncated"])
|
|
self.assertEqual("100000\n", answer["stderr"]["text"])
|
|
|
|
def test_flooding_both_streams_still_times_out(self):
|
|
program = "import os\nwhile True:\n os.write(1, b'o' * 65536)\n os.write(2, b'e' * 65536)"
|
|
answer = self.ask({"kind": "exec", "argv": [sys.executable, "-c", program], "timeout": 0.5})
|
|
self.assertTrue(answer["timedOut"])
|
|
for name in ("stdout", "stderr"):
|
|
self.assertEqual(agent.OUTPUT_CAP, len(answer[name]["text"]))
|
|
self.assertTrue(answer[name]["truncated"])
|
|
|
|
def test_timeout_applies_when_parent_exits_with_inherited_pipes(self):
|
|
started = time.monotonic()
|
|
answer = self.ask({"kind": "exec", "argv": ["sh", "-c", "sleep 30 & exit 0"], "timeout": 0.5})
|
|
self.assertTrue(answer["timedOut"])
|
|
self.assertLess(time.monotonic() - started, 5)
|
|
|
|
def test_invalid_timeout_and_input_are_refused_before_exec(self):
|
|
for value in (0, -1, 601, "NaN", "Infinity", "invalid"):
|
|
answer = self.ask({"kind": "exec", "argv": ["true"], "timeout": value})
|
|
self.assertEqual("INVALID_REQUEST", answer["error"]["code"])
|
|
for fields in ({"stdin": 1}, {"env": []}, {"env": {"BAD=KEY": "value"}}, {"argv": ["true\0"]}):
|
|
answer = self.ask({"kind": "exec", "argv": ["true"], **fields})
|
|
self.assertEqual("INVALID_REQUEST", answer["error"]["code"])
|
|
|
|
def test_errors_are_answers(self):
|
|
self.assertEqual("INVALID_REQUEST", self.ask({"kind": "exec", "argv": []})["error"]["code"])
|
|
self.assertEqual("EXEC_FAILED", self.ask({"kind": "exec", "argv": ["/no/such/program"]})["error"]["code"])
|
|
self.assertEqual("UNKNOWN_REQUEST", self.ask({"kind": "nope"})["error"]["code"])
|
|
self.assertEqual("INVALID_APP_ID", self.ask({"kind": "quit", "appId": "x"})["error"]["code"])
|
|
(self.dir / "req.json").write_text("{not json")
|
|
agent.answer(self.dir, self.dir / "req.json")
|
|
self.assertEqual("INVALID_REQUEST", json.loads((self.dir / "resp" / "req.json").read_text())["error"]["code"])
|
|
|
|
def test_request_shape_and_size_are_bounded(self):
|
|
self.assertEqual("INVALID_REQUEST", self.ask([])["error"]["code"])
|
|
self.assertEqual("INVALID_REQUEST", self.ask({"kind": "ping", "unused": "x" * agent.REQUEST_CAP})["error"]["code"])
|
|
|
|
def test_replies_are_owner_only_even_when_replacing_a_public_file(self):
|
|
path = self.dir / "private.json"
|
|
path.write_text("old")
|
|
path.chmod(0o644)
|
|
agent.write_json(path, {"ok": True})
|
|
self.assertEqual(0o600, path.stat().st_mode & 0o777)
|
|
|
|
def test_oversized_websocket_frames_are_refused_before_reading_payload(self):
|
|
ws = agent.WebSocket.__new__(agent.WebSocket)
|
|
ws.buffer = bytes([0x81, 127]) + struct.pack(">Q", agent.RESPONSE_CAP + 1)
|
|
with self.assertRaises(agent.AgentError) as caught:
|
|
ws.recv()
|
|
self.assertEqual("RESPONSE_TOO_LARGE", caught.exception.code)
|
|
|
|
def test_devtools_targets_cannot_connect_to_remote_hosts(self):
|
|
for url in ("ws://example.com/devtools", "ws://127.0.0.1@example.com/devtools", "wss://127.0.0.1/devtools"):
|
|
with patch.object(agent.socket, "create_connection") as connect:
|
|
with self.assertRaises(agent.AgentError) as caught:
|
|
agent.WebSocket(url, 1)
|
|
self.assertEqual("INVALID_TARGET", caught.exception.code)
|
|
connect.assert_not_called()
|
|
|
|
def test_cdp_without_a_port(self):
|
|
os.environ.pop("BL_CDP_PORT", None)
|
|
self.assertEqual("NO_DEVTOOLS", self.ask({"kind": "cdp", "expression": "1"})["error"]["code"])
|
|
|
|
def test_cdp_eval(self):
|
|
server = FakeDevTools({"result": {"result": {"type": "number", "value": 42}}})
|
|
server.start()
|
|
self.addCleanup(server.server.close)
|
|
(self.dir / "cdp-port").write_text("%d\n" % server.port)
|
|
answer = self.ask({"kind": "cdp", "expression": "6 * 7"})
|
|
self.assertTrue(answer["ok"], answer)
|
|
self.assertEqual(42, answer["value"])
|
|
self.assertEqual("SharedJSContext", answer["target"])
|
|
self.assertEqual("Runtime.evaluate", server.seen[0]["method"])
|
|
self.assertEqual("6 * 7", server.seen[0]["params"]["expression"])
|
|
targets = self.ask({"kind": "cdp", "op": "targets"})
|
|
self.assertEqual("SharedJSContext", targets["targets"][0]["title"])
|
|
missing = self.ask({"kind": "cdp", "expression": "1", "target": "Nope"})
|
|
self.assertEqual("NO_TARGET", missing["error"]["code"])
|
|
|
|
def test_steam_selection(self):
|
|
with patch.object(agent, "cdp_eval", return_value={"value": "3180310"}) as evaluate:
|
|
answer = self.ask({"kind": "steam-selection"})
|
|
self.assertEqual(3180310, answer["appId"])
|
|
self.assertIn("document.activeElement", evaluate.call_args.args[1])
|
|
self.assertEqual("Steam Big Picture Mode", evaluate.call_args.kwargs["target"])
|
|
|
|
def test_steam_selection_allows_no_highlight(self):
|
|
with patch.object(agent, "cdp_eval", return_value={"value": None}):
|
|
self.assertIsNone(self.ask({"kind": "steam-selection"})["appId"])
|
|
|
|
def test_steam_selection_rejects_invalid_app_id(self):
|
|
with patch.object(agent, "cdp_eval", return_value={"value": "4294967296"}):
|
|
answer = self.ask({"kind": "steam-selection"})
|
|
self.assertEqual("INVALID_SELECTION", answer["error"]["code"])
|
|
|
|
def test_shortcut_cgameid_waits_for_exact_steam_overview_mapping(self):
|
|
game_id = str((3551182975 << 32) | 0x02000000)
|
|
self.assertEqual(3551182975, agent.steam_shortcut_app_id(game_id))
|
|
self.assertIsNone(agent.steam_shortcut_app_id("413150"))
|
|
self.assertIsNone(agent.steam_shortcut_app_id(str((3551182975 << 32) | 0x03000000)))
|
|
clock = [0.0]
|
|
with patch.object(agent, "cdp_eval", side_effect=[{"value": None}, {"value": "3551182975"}]) as evaluate, \
|
|
patch.object(agent.time, "monotonic", side_effect=lambda: clock[0]), \
|
|
patch.object(agent.time, "sleep", side_effect=lambda delay: clock.__setitem__(0, clock[0] + delay)):
|
|
result = agent.wait_for_steam_shortcut_overview(self.dir, game_id, timeout=1)
|
|
self.assertEqual({"supported": True, "ready": True, "appId": 3551182975}, result)
|
|
expression = evaluate.call_args_list[0].args[1]
|
|
self.assertIn("GetAppOverviewByGameID", expression)
|
|
self.assertIn(json.dumps(game_id), expression)
|
|
self.assertNotIn("RequestAppDetails", expression)
|
|
self.assertFalse(evaluate.call_args_list[0].kwargs["await_promise"])
|
|
|
|
def test_wrong_overview_app_id_keeps_shortcut_url_held_until_timeout(self):
|
|
game_id = str((3537741571 << 32) | 0x02000000)
|
|
clock = [0.0]
|
|
with patch.object(agent, "cdp_eval", return_value={"value": "646570"}) as evaluate, \
|
|
patch.object(agent.time, "monotonic", side_effect=lambda: clock[0]), \
|
|
patch.object(agent.time, "sleep", side_effect=lambda delay: clock.__setitem__(0, clock[0] + delay)):
|
|
with self.assertRaises(agent.AgentError) as raised:
|
|
agent.wait_for_steam_shortcut_overview(self.dir, game_id, timeout=0.5)
|
|
self.assertEqual("STEAM_OVERVIEW_TIMEOUT", raised.exception.code)
|
|
self.assertGreater(evaluate.call_count, 0)
|
|
self.assertTrue(all("RequestAppDetails" not in call.args[1] for call in evaluate.call_args_list))
|
|
|
|
def test_normal_steam_ids_and_unknown_cgameids_skip_overview_wait(self):
|
|
for game_id in ("413150", str((123456789 << 32) | 0x03000000), "18446744073709551616"):
|
|
with self.subTest(game_id=game_id), patch.object(agent, "cdp_eval") as evaluate:
|
|
result = agent.wait_for_steam_shortcut_overview(self.dir, game_id)
|
|
self.assertFalse(result["supported"])
|
|
evaluate.assert_not_called()
|
|
|
|
def test_shortcut_state_tells_a_lost_shortcut_from_a_loading_library(self):
|
|
game_id = str((3333273269 << 32) | 0x02000000)
|
|
self.assertEqual("not-shortcut", agent.steam_shortcut_state(self.dir, "413150"))
|
|
for values, wanted, calls in ((["loading", "ready"], "ready", 2), (["loading", "missing"], "missing", 3),
|
|
([None], "loading", 3)):
|
|
clock = [0.0]
|
|
replies = [{"value": v} for v in values] + [{"value": values[-1]}] * 10
|
|
with self.subTest(values=values), patch.object(agent, "cdp_eval", side_effect=replies) as evaluate, \
|
|
patch.object(agent.time, "monotonic", side_effect=lambda: clock[0]), \
|
|
patch.object(agent.time, "sleep", side_effect=lambda delay: clock.__setitem__(0, clock[0] + delay)):
|
|
self.assertEqual(wanted, agent.steam_shortcut_state(self.dir, game_id, timeout=1))
|
|
self.assertEqual(calls, evaluate.call_count)
|
|
self.assertIn("GetAppOverviewByGameID(%s)" % json.dumps(game_id), evaluate.call_args_list[0].args[1])
|
|
|
|
def test_shortcut_state_waits_out_an_unreachable_client_but_not_other_errors(self):
|
|
game_id = str((3333273269 << 32) | 0x02000000)
|
|
clock = [0.0]
|
|
with patch.object(agent, "cdp_eval", side_effect=agent.AgentError("NO_DEVTOOLS", "down")), \
|
|
patch.object(agent.time, "monotonic", side_effect=lambda: clock[0]), \
|
|
patch.object(agent.time, "sleep", side_effect=lambda delay: clock.__setitem__(0, clock[0] + delay)):
|
|
self.assertEqual("loading", agent.steam_shortcut_state(self.dir, game_id, timeout=1))
|
|
with patch.object(agent, "cdp_eval", side_effect=agent.AgentError("CDP_EXCEPTION", "boom")):
|
|
with self.assertRaises(agent.AgentError):
|
|
agent.steam_shortcut_state(self.dir, game_id, timeout=1)
|
|
|
|
def test_shortcut_state_command_exits_with_the_state(self):
|
|
game_id = str((3333273269 << 32) | 0x02000000)
|
|
launch = self.dir.parent
|
|
with patch.dict(os.environ, {"BL_LAUNCH_DIR": str(launch)}):
|
|
for state, code in (("ready", 0), ("missing", 4), ("not-shortcut", 3), ("loading", 1)):
|
|
with self.subTest(state=state), patch.object(agent, "steam_shortcut_state", return_value=state) as ask:
|
|
self.assertEqual(code, agent.main(["--steam-shortcut-state", game_id, "2"]))
|
|
self.assertEqual((launch / "agent", game_id, "2"), ask.call_args.args)
|
|
with patch.object(agent, "steam_shortcut_state", side_effect=agent.AgentError("CDP_EXCEPTION", "boom")):
|
|
self.assertEqual(1, agent.main(["--steam-shortcut-state", game_id]))
|
|
self.assertEqual(2, agent.main(["--steam-shortcut-state"]))
|
|
|
|
def test_asleep_command_reports_steams_own_sleep_state(self):
|
|
with patch.dict(os.environ, {"BL_LAUNCH_DIR": str(self.dir.parent)}):
|
|
for value, code in (("yes", 0), ("no", 5), ("unknown", 1), (None, 1)):
|
|
with self.subTest(value=value), patch.object(agent, "cdp_eval", return_value={"value": value}) as ask:
|
|
self.assertEqual(code, agent.main(["--steam-asleep"]))
|
|
self.assertIn("m_bSuspending", ask.call_args.args[1])
|
|
self.assertFalse(ask.call_args.kwargs["await_promise"])
|
|
with patch.object(agent, "cdp_eval", side_effect=agent.AgentError("NO_DEVTOOLS", "down")):
|
|
self.assertEqual(1, agent.main(["--steam-asleep"]))
|
|
|
|
def test_cdp_exception(self):
|
|
server = FakeDevTools({"result": {"result": {"type": "object"}, "exceptionDetails": {
|
|
"text": "Uncaught", "exception": {"description": "ReferenceError: SteamClient is not defined"}}}})
|
|
server.start()
|
|
self.addCleanup(server.server.close)
|
|
(self.dir / "cdp-port").write_text(str(server.port))
|
|
answer = self.ask({"kind": "cdp", "expression": "SteamClient"})
|
|
self.assertEqual("JS_EXCEPTION", answer["error"]["code"])
|
|
self.assertIn("ReferenceError", answer["error"]["message"])
|
|
|
|
def test_serve_answers_request_files(self):
|
|
server = subprocess.Popen([sys.executable, str(SCRIPT), "--no-focus"],
|
|
env=dict(os.environ, BL_LAUNCH_DIR=str(self.dir)),
|
|
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
def stop_server():
|
|
server.terminate()
|
|
server.wait(timeout=5)
|
|
self.addCleanup(stop_server)
|
|
agent_dir = self.dir / "agent"
|
|
for _ in range(100):
|
|
if (agent_dir / "hello.json").exists():
|
|
break
|
|
time.sleep(0.02)
|
|
hello = json.loads((agent_dir / "hello.json").read_text())
|
|
self.assertEqual(server.pid, hello["pid"])
|
|
self.assertFalse(hello["focus"])
|
|
(agent_dir / "req" / "a.json.tmp").write_text(json.dumps({"kind": "ping"}))
|
|
os.replace(agent_dir / "req" / "a.json.tmp", agent_dir / "req" / "a.json")
|
|
for _ in range(200):
|
|
if (agent_dir / "resp" / "a.json").exists():
|
|
break
|
|
time.sleep(0.02)
|
|
self.assertEqual(agent.VERSION, json.loads((agent_dir / "resp" / "a.json").read_text())["version"])
|
|
|
|
def test_game_pids_reads_the_steam_environment(self):
|
|
import subprocess
|
|
if not Path("/proc").is_dir():
|
|
self.skipTest("needs /proc")
|
|
child = subprocess.Popen(["sleep", "30"], env=dict(os.environ, SteamAppId="424242"))
|
|
self.addCleanup(lambda: (child.kill(), child.wait()))
|
|
time.sleep(0.2)
|
|
self.assertIn(child.pid, agent.game_pids(424242))
|
|
|
|
|
|
class VrFocusTest(unittest.TestCase):
|
|
def test_scene_dashboard_and_external_focus_changes(self):
|
|
focus = agent.VrSceneFocus()
|
|
self.assertEqual(focus.order([9, 7], [7, 42], 42), [42, 9, 7])
|
|
self.assertIsNone(focus.order([42, 9, 7], [7, 42], 42))
|
|
self.assertEqual(focus.order([42, 9, 7], [7, 42], None), [9, 7])
|
|
self.assertIsNone(focus.order([9, 7], [7], 42))
|
|
self.assertEqual(focus.order([9, 7], [7, 42], 42), [42, 9, 7])
|
|
# Do not overwrite a different focus order installed after our last write.
|
|
self.assertIsNone(focus.order([123, 7], [7, 42], None))
|
|
|
|
def test_scene_requires_live_server_and_game_environment(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = Path(directory)
|
|
state = root / 'scene.json'
|
|
for pid in (100, 200):
|
|
(root / str(pid)).mkdir()
|
|
(root / '100/comm').write_text('vrserver\n')
|
|
(root / '200/environ').write_bytes(b'SteamAppId=42\0Other=value\0')
|
|
state.write_text(json.dumps(dict(serverPid=100, scenePid=200, dashboard=False)))
|
|
self.assertEqual(agent.vr_scene_app(state, root), 42)
|
|
state.write_text(json.dumps(dict(serverPid=100, scenePid=200, dashboard=True)))
|
|
self.assertIsNone(agent.vr_scene_app(state, root))
|
|
state.write_text(json.dumps(dict(serverPid=100, scenePid=200, dashboard=False)))
|
|
(root / '100/comm').write_text('other\n')
|
|
self.assertIsNone(agent.vr_scene_app(state, root))
|
|
(root / '100/comm').unlink()
|
|
self.assertIsNone(agent.vr_scene_app(state, root))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|