diff --git a/docs/development/steam-game-imports.md b/docs/development/steam-game-imports.md index 52d69934fc..e116a73b26 100644 --- a/docs/development/steam-game-imports.md +++ b/docs/development/steam-game-imports.md @@ -22,7 +22,9 @@ a bounded subprocess so library failures cannot crash the importer or client. The resulting snapshot is stored under that account's `userdata//config` and refreshed every 30 seconds while logged in. Title lookups retry every five minutes and when the source changes, so an early network failure does not end the watcher. Changed -status and probe failures are written to the session log. Startup and post-exit import +status and probe failures are written to the session log. While a game runs, the check still runs until it has +succeeded once for the current folders, so a game started right after the client does not keep its owned titles +unconfirmed for the whole session. Startup and post-exit import use the last successful snapshot for the selected account. The client reads manifests and shortcuts only at start, so when a snapshot confirms owned titles that are not yet imported, the watcher restarts the client once (not while a game runs or a download is in diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-games b/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-games index c618cadf35..3b4228141a 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-games +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-games @@ -236,7 +236,7 @@ def watch(steam_root, listing): candidates, sources = {}, {} next_lookup = 0 last_status = None - probed, next_probe, failures = None, 0, 0 + probed, next_probe, failures, confirmed = None, 0, 0, None pending, requested, account = [], [], None while True: now = time.monotonic() @@ -254,7 +254,7 @@ def watch(steam_root, listing): next_lookup = now + 300 status = "waiting for a matching Steam title" key = (dict(candidates), dict(sources)) - if candidates and (key != probed or now >= next_probe) and game_running(): + if candidates and (key != probed or now >= next_probe) and key == confirmed and game_running(): status = last_status elif candidates and (key != probed or now >= next_probe): probed, next_probe = key, now + 300 @@ -278,6 +278,7 @@ def watch(steam_root, listing): save_json(Path(steam_root) / "userdata" / snapshot["account"] / "config" / STATE, snapshot) pending = unimported(steam_root, snapshot, candidates) account = snapshot["account"] + confirmed = key status = "matched %d/%d games; %d owned" % (len(candidates), len(games), sum(snapshot["owned"].values())) except (OSError, ValueError, KeyError, StopIteration, subprocess.TimeoutExpired) as error: status = "ownership check unavailable: " + str(error) diff --git a/tools/tests/test_steam_games.py b/tools/tests/test_steam_games.py index 08ffc90935..e7c4e9f815 100644 --- a/tools/tests/test_steam_games.py +++ b/tools/tests/test_steam_games.py @@ -306,7 +306,7 @@ class ImportsTest(unittest.TestCase): snapshot = imports.read_json(self.acct / 'config' / imports.STATE, {}) self.assertTrue(snapshot['owned']['42']) - def run_watch(self, beats, routes=None, busy=False, answer=True, started=False): + def run_watch(self, beats, routes=None, busy=False, answer=True, started=False, running=False, times=None, results=None): listing = self.root / 'games.json' listing.write_text(json.dumps([self.game])) if routes is not None: @@ -317,19 +317,37 @@ class ImportsTest(unittest.TestCase): idle = imports.downloads_idle result = Mock(returncode=0, stdout='DROIDDECK_OWNERSHIP=' + json.dumps(dict(account='123', owned={'42': True}))) with patch.object(imports, 'identify', return_value=42), \ - patch.object(imports, 'game_running', return_value=False), \ + patch.object(imports, 'game_running', return_value=running), \ patch.object(imports, 'request_restart') as restart, \ patch.object(imports, 'ask_restart', return_value=answer) as asked, \ patch.object(imports, 'downloads_idle', side_effect=lambda root: idle(root) and not (started and asked.called)), \ - patch.object(imports, 'time', Mock(monotonic=Mock(side_effect=list(range(0, 30 * beats, 30))), + patch.object(imports, 'time', Mock(monotonic=Mock(side_effect=times or list(range(0, 30 * beats, 30))), sleep=Mock(side_effect=[None] * (beats - 1) + [RuntimeError('stop')]))), \ - patch.object(imports.subprocess, 'run', return_value=result), \ + patch.object(imports.subprocess, 'run', side_effect=results or [result] * beats) as probes, \ patch('builtins.print'): with self.assertRaisesRegex(RuntimeError, 'stop'): imports.watch(self.steam, listing) self.asked = asked + self.probes = probes return restart + def test_ownership_is_checked_while_a_game_runs_until_it_is_known(self): + unavailable = Mock(returncode=1, stderr='Steam pipe unavailable', stdout='') + owned = Mock(returncode=0, stdout='DROIDDECK_OWNERSHIP=' + json.dumps(dict(account='123', owned={'42': True}))) + restart = self.run_watch(3, running=True, times=[0, 30, 90], results=[unavailable, owned]) + self.assertEqual(2, self.probes.call_count) + self.assertTrue(imports.read_json(self.acct / 'config' / imports.STATE, {})['owned']['42']) + self.assertEqual(0, restart.call_count) + self.assertEqual(0, self.asked.call_count) + + def test_known_ownership_is_not_checked_again_while_a_game_runs(self): + self.run_watch(3, running=True, times=[0, 301, 602]) + self.assertEqual(1, self.probes.call_count) + + def test_known_ownership_is_checked_again_when_no_game_runs(self): + self.run_watch(3, answer=False, times=[0, 301, 602]) + self.assertEqual(3, self.probes.call_count) + def test_newly_owned_game_restarts_the_client_once(self): self.assertEqual(1, self.run_watch(3).call_count)