mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +00:00
fix(steam): the last change to a game's compatibility tool wins (#1347)
Refs #1248 ## Problem Users report that changing a game's compatibility tool in Steam's game properties doesn't stick: it goes back to DroidDeck's default. #1248 was closed by #1250, but its reporter still saw it after clearing the game's DroidDeck profile. A game's tool can be chosen in two places, DroidDeck's per-game Proton (`games.json`, written by `ProtonDefault.requestGame`) and Steam. `plan_mapping` treats every `games.json` entry as an override that always wins, so the live helper (`droiddeck-steam-compat`, on `CompatManager.NotifyStateChanged`) sets it back within a second, and `steam-compatibility` does again at every session start. A `null` entry ("use the default", also written by a profile reset) is forced as the default, so resetting the profile made it worse. Reproduced with the real `plan_mapping` (game 220, Steam set to `droiddeck-fex`): | `games.json` entry | before | |---|---| | none | kept | | `null` (profile reset / "Default") | reverted to default | | a DroidDeck Proton | reverted to it | ## Change Whichever side changed a game's tool last wins: - A DroidDeck choice is applied once (as now). Once applied, a different pick in Steam stands: - a pick of one of DroidDeck's Protons becomes the DroidDeck choice too (`games.json` updated, so the app shows it); - any other pick (FEX, a Steam Linux Runtime, clearing the forced tool) removes the game from `games.json`. - `null` is applied once and then dropped, so the game follows the default like one DroidDeck never managed. - What was applied is kept in `state.json` as `games_applied`. Choices an earlier build already applied count as applied (when `state.json` is from an earlier build), so upgrading respects the Steam picks users already made. - `games.json` is rewritten only if the app didn't replace it after it was read, so a newer app choice is never overwritten. - An x86-only Valve Proton picked in Steam is still replaced (`unrunnable`), unchanged. Both paths share the logic: `register_default` (session start, reads config.vdf; with the client stopped, an app missing from the mapping counts as cleared) and the live helper's `reconcile`. ## Tests - `test_steam_compat_live.py` + `test_game_environment.py`: 109 pass locally (103 existing + 6 new). The 6 new tests fail on the old scripts. - Steam pick after an app choice is kept live, and a new app choice applies again - Steam pick of a DroidDeck Proton becomes the app's choice - "use the default" applies once, then Steam picks stand - upgrade: choices an earlier build applied give way to Steam picks - an app choice written during a sync is not overwritten - session start keeps a Steam pick from config.vdf Not device-tested. ## Side note: `test_authorship` writes into the global git hooks `InstalledHooksTest.setUp` runs `tools/release/install_authorship_hooks.py` in a temp repo, but the installer reads `core.hooksPath` from the **global** config. On a machine with a global hooks path, running the suite replaced the user's global `commit-msg`/`prepare-commit-msg`/`pre-push` with hooks pointing into the deleted temp repo (every later commit failed), and the saved originals were deleted with the temp repo. The test should isolate `HOME`/`GIT_CONFIG_GLOBAL`. Not changed in this PR.
This commit is contained in:
3 files changed
+201
-10
No files matched your search
@@ -583,9 +583,8 @@ class Helper:
|
||||
picked = COMPAT["picked_apps"](state)
|
||||
runnable = {t["name"].lower() for t in live}
|
||||
apps = sorted(set(installed) | set(picks) | set(prior_auto) | native_choices, key=COMPAT["app_order"])
|
||||
overrides = COMPAT["requested_games"](
|
||||
COMPAT["game_requests"](), catalog, [t["name"] for t in live])
|
||||
apps = sorted(set(apps) | set(overrides), key=COMPAT["app_order"])
|
||||
asked = [app for app, request in COMPAT["game_requests"]().items() if COMPAT["valid_game_request"](app, request)]
|
||||
apps = sorted(set(apps) | set(asked), key=COMPAT["app_order"])
|
||||
current = dict(page.compat("many(%s)" % json.dumps(apps)) or {})
|
||||
refused = set(apps) - set(current)
|
||||
refused_native = native_choices & refused
|
||||
@@ -597,7 +596,6 @@ class Helper:
|
||||
if isinstance(state.get("auto"), dict):
|
||||
state["auto"] = prior_auto
|
||||
native_choices -= refused
|
||||
overrides = {app: tool for app, tool in overrides.items() if app not in refused}
|
||||
current["0"] = top["selected"]
|
||||
native = COMPAT["native_targets"](self.steam_root, apps)
|
||||
raw_mapping = COMPAT["read_config_mapping"](
|
||||
@@ -619,6 +617,9 @@ class Helper:
|
||||
current[app] = ""
|
||||
elif not current.get(app):
|
||||
current.pop(app, None)
|
||||
requests = COMPAT["settled_game_requests"](state, current, catalog)
|
||||
overrides = COMPAT["requested_games"](requests or {}, catalog, [t["name"] for t in live])
|
||||
overrides = {app: tool for app, tool in overrides.items() if app not in refused}
|
||||
changes, auto = COMPAT["plan_mapping"](
|
||||
current, installed, [t["name"] for t in live], default,
|
||||
COMPAT["followers"](state, current), native, picks, overrides,
|
||||
@@ -630,7 +631,8 @@ class Helper:
|
||||
state.update(
|
||||
auto={app: tool for app, tool in auto.items() if tool is not COMPAT["NATIVE_CHOICE"]},
|
||||
native_choices=sorted(refused_native | {app for app, tool in auto.items() if tool is COMPAT["NATIVE_CHOICE"]}),
|
||||
picked=COMPAT["picked_after"](current, changes), tools=catalog, live=True)
|
||||
picked=COMPAT["picked_after"](current, changes), tools=catalog, live=True,
|
||||
games_applied=COMPAT["applied_game_requests"](requests, overrides, state))
|
||||
state.pop("native", None)
|
||||
if waiting:
|
||||
state["pending"] = wanted
|
||||
|
||||
@@ -1113,10 +1113,89 @@ def requested_tool(request, catalog):
|
||||
|
||||
|
||||
def game_requests(directory=None):
|
||||
value = read_json(state_path("games.json", directory))
|
||||
if value.get("version") != 1 or not isinstance(value.get("games"), dict):
|
||||
return {}
|
||||
return value["games"]
|
||||
return read_game_requests(directory)[0]
|
||||
|
||||
|
||||
def read_game_requests(directory=None):
|
||||
try:
|
||||
with open(state_path("games.json", directory), encoding="utf-8") as f:
|
||||
text = f.read()
|
||||
value = json.loads(text)
|
||||
except (OSError, ValueError):
|
||||
return {}, None
|
||||
if not isinstance(value, dict) or value.get("version") != 1 or not isinstance(value.get("games"), dict):
|
||||
return {}, text
|
||||
return value["games"], text
|
||||
|
||||
|
||||
def write_game_requests(requests, original, directory=None):
|
||||
path = state_path("games.json", directory)
|
||||
try:
|
||||
with open(path, encoding="utf-8") as f:
|
||||
if f.read() != original:
|
||||
return False
|
||||
except OSError:
|
||||
if original is not None:
|
||||
return False
|
||||
return write_json(path, {"version": 1, "games": requests})
|
||||
|
||||
|
||||
def valid_game_request(app, request):
|
||||
return (isinstance(app, str) and re.fullmatch(r"[1-9][0-9]*", app) is not None and int(app) <= 0xFFFFFFFF and
|
||||
(request is None or isinstance(request, dict) and isinstance(request.get("dir"), str) and
|
||||
request["dir"] != "" and isinstance(request.get("valve"), bool)))
|
||||
|
||||
|
||||
def settle_game_requests(requests, state, current, catalog, missing_is_cleared=False):
|
||||
recorded = state.get("games_applied")
|
||||
if isinstance(recorded, dict):
|
||||
applied = dict(recorded)
|
||||
else:
|
||||
applied = dict(requests) if "tools" in state else {}
|
||||
by_name = {t["name"]: t for t in catalog}
|
||||
out = dict(requests)
|
||||
for app, request in requests.items():
|
||||
if not valid_game_request(app, request) or app not in applied or applied[app] != request:
|
||||
continue
|
||||
if request is None:
|
||||
del out[app]
|
||||
continue
|
||||
wanted = requested_tool(request, catalog)
|
||||
if wanted is None:
|
||||
continue
|
||||
if app in current:
|
||||
picked = current[app]
|
||||
elif missing_is_cleared:
|
||||
picked = ""
|
||||
else:
|
||||
continue
|
||||
if picked == wanted:
|
||||
continue
|
||||
tool = by_name.get(picked)
|
||||
if tool and tool["dir"]:
|
||||
out[app] = {"valve": tool["valve"], "dir": tool["dir"]}
|
||||
else:
|
||||
del out[app]
|
||||
return out, out != requests
|
||||
|
||||
|
||||
def settled_game_requests(state, current, catalog, missing_is_cleared=False, directory=None):
|
||||
for _ in range(2):
|
||||
requests, text = read_game_requests(directory)
|
||||
settled, moved = settle_game_requests(requests, state, current, catalog, missing_is_cleared)
|
||||
if not moved or write_game_requests(settled, text, directory):
|
||||
return settled
|
||||
return None
|
||||
|
||||
|
||||
def applied_game_requests(requests, overrides, state):
|
||||
recorded = state.get("games_applied")
|
||||
before = recorded if isinstance(recorded, dict) else {}
|
||||
if requests is None:
|
||||
return dict(before)
|
||||
return {app: request if app in overrides and overrides[app] is not DEFERRED_GAME_TOOL else before[app]
|
||||
for app, request in requests.items()
|
||||
if app in overrides and overrides[app] is not DEFERRED_GAME_TOOL or app in before}
|
||||
|
||||
|
||||
def requested_games(requests, catalog, runnable=None):
|
||||
@@ -1306,7 +1385,8 @@ def register_default(config_path, apps=(), protect=(), default=None, catalog=Non
|
||||
else:
|
||||
default = state.get("default") if state.get("default") in names else TOOL
|
||||
steam_root = os.path.dirname(os.path.dirname(os.path.abspath(config_path)))
|
||||
overrides = requested_games(game_requests(directory), catalog)
|
||||
requests = settled_game_requests(state, current, catalog, not steam_running(), directory)
|
||||
overrides = requested_games(requests or {}, catalog)
|
||||
managed = state.get("auto", {}).keys() if isinstance(state.get("auto"), dict) else ()
|
||||
native_choices = state.get("native_choices", ())
|
||||
native = native_targets(steam_root, set(apps) | set(picks) | set(managed) | set(native_choices))
|
||||
@@ -1325,6 +1405,7 @@ def register_default(config_path, apps=(), protect=(), default=None, catalog=Non
|
||||
state["auto"] = {app: tool for app, tool in auto.items() if tool is not NATIVE_CHOICE}
|
||||
state["native_choices"] = sorted(app for app, tool in auto.items() if tool is NATIVE_CHOICE)
|
||||
state["picked"] = picked_after(current, changes)
|
||||
state["games_applied"] = applied_game_requests(requests, overrides, state)
|
||||
state.pop("native", None)
|
||||
state["tools"] = catalog
|
||||
state.setdefault("default", default)
|
||||
|
||||
@@ -641,6 +641,114 @@ class ReconcileTest(unittest.TestCase):
|
||||
self.assertEqual(page.mapping["10"], "GE-Proton9-1")
|
||||
self.assertEqual(state["auto"], {"11": TOOL})
|
||||
|
||||
def games(self):
|
||||
return json.loads((self.state / "games.json").read_text())["games"]
|
||||
|
||||
def write_games(self, games):
|
||||
(self.state / "games.json").write_text(json.dumps({"version": 1, "games": games}))
|
||||
|
||||
def test_a_steam_pick_after_the_app_choice_is_kept_live(self):
|
||||
page = FakePage({"0": TOOL, "10": TOOL, "11": TOOL}, [TOOL, TOOL_11, COMPAT["FEX_TOOL"]])
|
||||
self.write_games({"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
|
||||
state = self.reconcile(page)
|
||||
self.assertEqual(page.sets, [("10", TOOL_11)])
|
||||
self.assertEqual(state["games_applied"], {"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
|
||||
|
||||
page.sets.clear()
|
||||
page.mapping["10"] = COMPAT["FEX_TOOL"]
|
||||
state = self.reconcile(page)
|
||||
self.assertEqual(page.sets, [])
|
||||
self.assertEqual(page.mapping["10"], COMPAT["FEX_TOOL"])
|
||||
self.assertEqual(self.games(), {})
|
||||
self.assertEqual(state["games_applied"], {})
|
||||
|
||||
self.write_games({"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
|
||||
self.reconcile(page)
|
||||
self.assertEqual(page.sets, [("10", TOOL_11)])
|
||||
|
||||
def test_a_steam_pick_of_our_proton_becomes_the_app_choice_live(self):
|
||||
page = FakePage({"0": TOOL, "10": TOOL, "11": TOOL}, [TOOL, TOOL_11])
|
||||
self.write_games({"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
|
||||
self.reconcile(page)
|
||||
page.sets.clear()
|
||||
page.mapping["10"] = TOOL
|
||||
state = self.reconcile(page)
|
||||
self.assertEqual(page.sets, [])
|
||||
self.assertEqual(self.games(), {"10": {"valve": True, "dir": "Proton Experimental (ARM64)"}})
|
||||
self.assertEqual(state["games_applied"], self.games())
|
||||
|
||||
def test_use_the_default_applies_once_then_steam_picks_stand_live(self):
|
||||
page = FakePage({"0": TOOL, "10": TOOL_11, "11": TOOL}, [TOOL, TOOL_11, COMPAT["FEX_TOOL"]])
|
||||
self.write_games({"10": None})
|
||||
self.reconcile(page)
|
||||
self.assertEqual(page.sets, [("10", TOOL)])
|
||||
|
||||
page.sets.clear()
|
||||
self.reconcile(page)
|
||||
self.assertEqual(self.games(), {})
|
||||
page.mapping["10"] = COMPAT["FEX_TOOL"]
|
||||
self.reconcile(page)
|
||||
self.assertEqual(page.sets, [])
|
||||
self.assertEqual(page.mapping["10"], COMPAT["FEX_TOOL"])
|
||||
|
||||
def test_choices_an_earlier_build_applied_give_way_to_steam_picks_live(self):
|
||||
(self.state / "state.json").write_text(json.dumps({"default": TOOL, "auto": {"11": TOOL}, "tools": []}))
|
||||
self.write_games({"10": None, "11": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
|
||||
page = FakePage({"0": TOOL, "10": COMPAT["FEX_TOOL"], "11": "steamlinuxruntime_sniper"},
|
||||
[TOOL, TOOL_11, COMPAT["FEX_TOOL"]])
|
||||
self.reconcile(page)
|
||||
self.assertEqual(page.sets, [])
|
||||
self.assertEqual(self.games(), {})
|
||||
|
||||
def test_an_app_choice_written_during_a_sync_is_not_overwritten(self):
|
||||
self.write_games({"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
|
||||
requests, text = COMPAT["read_game_requests"]()
|
||||
self.write_games({"10": {"valve": True, "dir": "Proton Experimental (ARM64)"}})
|
||||
self.assertFalse(COMPAT["write_game_requests"]({}, text))
|
||||
self.assertEqual(self.games(), {"10": {"valve": True, "dir": "Proton Experimental (ARM64)"}})
|
||||
|
||||
def test_an_app_the_client_briefly_refuses_keeps_its_applied_choice_live(self):
|
||||
page = FakePage({"0": TOOL, "10": TOOL, "11": TOOL}, [TOOL, TOOL_11, COMPAT["FEX_TOOL"]])
|
||||
self.write_games({"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
|
||||
self.reconcile(page)
|
||||
page.refused = {"10"}
|
||||
state = self.reconcile(page)
|
||||
self.assertEqual(state["games_applied"], {"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
|
||||
page.refused = set()
|
||||
page.sets.clear()
|
||||
page.mapping["10"] = COMPAT["FEX_TOOL"]
|
||||
self.reconcile(page)
|
||||
self.assertEqual(page.sets, [])
|
||||
self.assertEqual(page.mapping["10"], COMPAT["FEX_TOOL"])
|
||||
|
||||
def test_an_app_rewriting_its_choices_during_both_tries_applies_none_of_them_live(self):
|
||||
page = FakePage({"0": TOOL, "10": TOOL, "11": TOOL}, [TOOL, TOOL_11, COMPAT["FEX_TOOL"]])
|
||||
self.write_games({"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
|
||||
self.reconcile(page)
|
||||
page.sets.clear()
|
||||
page.mapping["10"] = COMPAT["FEX_TOOL"]
|
||||
self.write_games({"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}, "11": None})
|
||||
with mock.patch.dict(COMPAT["settled_game_requests"].__globals__, write_game_requests=lambda *_: False):
|
||||
state = self.reconcile(page)
|
||||
self.assertEqual(page.sets, [])
|
||||
self.assertEqual(state["games_applied"], {"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
|
||||
|
||||
def test_a_steam_pick_is_kept_at_the_next_start(self):
|
||||
config = self.steam / "config/config.vdf"
|
||||
config.parent.mkdir(parents=True)
|
||||
vdf = '"InstallConfigStore" { "Software" { "Valve" { "Steam" { "CompatToolMapping" { %s } } } } }'
|
||||
entry = '"%s" { "name" "%s" "config" "" "priority" "250" }'
|
||||
catalog = COMPAT["tool_catalog"](str(self.steam))
|
||||
self.write_games({"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
|
||||
config.write_text(vdf % (entry % ("0", TOOL) + entry % ("10", TOOL)))
|
||||
with mock.patch.dict(COMPAT["register_default"].__globals__, steam_running=lambda: False):
|
||||
COMPAT["register_default"](str(config), ["10"], catalog=catalog, directory=str(self.state), settle=True)
|
||||
self.assertIn(entry % ("10", TOOL_11), " ".join(config.read_text().split()))
|
||||
config.write_text(vdf % (entry % ("0", TOOL) + entry % ("10", COMPAT["FEX_TOOL"])))
|
||||
COMPAT["register_default"](str(config), ["10"], catalog=catalog, directory=str(self.state), settle=True)
|
||||
self.assertIn(entry % ("10", COMPAT["FEX_TOOL"]), " ".join(config.read_text().split()))
|
||||
self.assertEqual(self.games(), {})
|
||||
|
||||
def test_file_mode_defers_to_the_live_helper(self):
|
||||
(self.state / "live.pid").write_text("%d\n" % os.getpid())
|
||||
self.assertTrue(COMPAT["live_helper_running"]())
|
||||
|
||||
Reference in new issue
Block a user