diff --git a/docs/development/stores.md b/docs/development/stores.md index 9776ac8c4c..16fdf66bc5 100644 --- a/docs/development/stores.md +++ b/docs/development/stores.md @@ -197,6 +197,13 @@ The appid Steam derives (CRC32 of the quoted exe plus the name, high bit set) is listing carries, so the live add and the writer never disagree. Uninstall removes the folder and the listing drops the entry; the live client is asked to remove it too. +`droiddeck-steam-shortcuts` merges each entry against the one Steam holds: a Target or Start In +edited in Steam survives the client's restart. A target whose file is gone is not an edit to keep - +it can only fail before the game starts - so the listing's exe replaces it, checked in the guest +where the shortcut's paths live, as long as the folder holding it exists (a missing folder can be an +unmounted card). #694 was a hand-typed Target that outlived the game update that removed its file, +so every launch died with Wine's `Failed to create process ...: 2` before the game ran. + `Library.SteamGame.source` is `steam`, `gog`, `epic`, `amazon` or `added`; the Library shows it as a chip on every tile and in the hero, and filters by it. diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-shortcuts b/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-shortcuts index 4cb3301d41..fc35c0fec3 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-shortcuts +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-shortcuts @@ -125,6 +125,14 @@ RECORD = ".droiddeck-shortcuts.json" AWAY_LIMIT = 256 FIELDS = ("AppName", "Exe", "StartDir", "icon", "LaunchOptions") FOLLOWED = ("StartDir", "LaunchOptions") +# Guest mountpoints whose contents live on storage the user can unplug. The app creates +# the mountpoint before binding it, so it outlives a removed card; libraries, store +# volumes and added game folders get one mountpoint each under these families. +REMOVABLE_ROOTS = ("/mnt/droiddeck-sd", "/mnt/bannerlator-sd") +REMOVABLE_FAMILIES = ("/mnt/droiddeck-lib", "/mnt/droiddeck-stores", "/root/Games") +# Guest trees the app always has, whatever storage is in the device. A target that is +# missing from one of these is gone even when its whole folder was removed. +INTERNAL_ROOTS = ("/root/Games/Stores", "/root/.local/share/Steam") def unquoted(value): @@ -146,6 +154,46 @@ def read_record(path): } +def under(path, root): + return path == root or path.startswith(root + "/") + + +def internal_target(target): + path = os.path.normpath(target) + return any(under(path, root) for root in INTERNAL_ROOTS) + + +def removable_mount(target): + """The guest mountpoint holding target, when it is storage the user can unplug.""" + path = os.path.normpath(target) + for root in REMOVABLE_ROOTS: + if under(path, root): + return root + for family in REMOVABLE_FAMILIES: + prefix = family + "/" + if path.startswith(prefix): + return prefix + path[len(prefix):].split("/", 1)[0] + return None + + +def storage_mounted(target): + """Whether the storage holding target is there this session. + + The app creates a guest mountpoint before binding storage to it, so a mountpoint that + lists nothing is one a removed card or volume left behind. A target under it may work + again when that storage returns, so its absence is not proof the file is gone. + """ + if internal_target(target): + return True + mount = removable_mount(target) + if mount is None: + return True + try: + return bool(os.listdir(mount)) + except OSError: + return False + + def merge(fresh, current, state, seen): state = state if isinstance(state, dict) else {} rev = state.get("rev") if isinstance(state.get("rev"), int) else 0 @@ -158,7 +206,16 @@ def merge(fresh, current, state, seen): entry = dict(current) entry["appid"] = fresh["appid"] exe = unquoted(fresh["Exe"]) - if exe != unquoted(current.get("Exe")) and (exe != unquoted(base.get("Exe")) or seen == rev): + target = unquoted(current.get("Exe")) + # A target Steam still points at after the file is gone starts nothing, so it is not + # an edit to keep: the app's own exe replaces it (#694: a hand-typed Target outlived + # the game update that removed the file, and every launch died before the game ran). + # Only when its storage is mounted and it is missing from a tree the app always has or + # from a folder that is still there. An absent folder under a mountpoint can equally be + # unplugged storage, and that target may work again next session. + gone = (bool(target) and not os.path.isfile(target) and storage_mounted(target) and + (internal_target(target) or os.path.isdir(os.path.dirname(target)))) + if exe != target and (gone or exe != unquoted(base.get("Exe")) or seen == rev): entry["Exe"], entry["StartDir"] = fresh["Exe"], fresh["StartDir"] for field in ("AppName", "icon"): if current.get(field) == base.get(field): diff --git a/tools/tests/test_steam_shortcuts.py b/tools/tests/test_steam_shortcuts.py index a927d4db07..74396e3221 100644 --- a/tools/tests/test_steam_shortcuts.py +++ b/tools/tests/test_steam_shortcuts.py @@ -1,6 +1,7 @@ import importlib.machinery import importlib.util import json +import os from pathlib import Path import subprocess import tempfile @@ -20,7 +21,15 @@ def load(name): shortcuts = load('droiddeck-steam-shortcuts') APPID = 0x87654321 -GUEST = '/root/Games/PC/Example' +GUEST = os.path.join(tempfile.mkdtemp(prefix='droiddeck-shortcuts-'), 'Games/PC/Example') + + +def installed(*paths): + """Creates the files the writer checks, so a Steam edit to one of them is kept.""" + for path in paths: + Path(path).parent.mkdir(parents=True, exist_ok=True) + Path(path).touch() + return paths[0] class ShortcutsTest(unittest.TestCase): @@ -34,7 +43,7 @@ class ShortcutsTest(unittest.TestCase): self.listing = self.root / 'added-games.json' def game(self, exe='Example.exe', seen=0, appid=APPID, name='Example'): - path = GUEST + '/' + exe + path = installed(GUEST + '/' + exe) return dict(name=name, exe=path, folder=GUEST, dir=path.rsplit('/', 1)[0], appid=appid, seen=seen) def run_writer(self, *games): @@ -54,10 +63,32 @@ class ShortcutsTest(unittest.TestCase): if value['appid'] & 0xFFFFFFFF == appid: value.update(fields) path.write_bytes(shortcuts.write({'shortcuts': data})) + if fields.get('Exe'): + target = Path(shortcuts.unquoted(fields['Exe'])) + target.parent.mkdir(parents=True, exist_ok=True) + target.touch() def record(self): return json.loads((self.config / shortcuts.RECORD).read_text()) + def patched(self, **constants): + for name, value in constants.items(): + old = getattr(shortcuts, name) + setattr(shortcuts, name, value) + self.addCleanup(setattr, shortcuts, name, old) + + def merge_target(self, exe, **edits): + """Runs merge against a Steam-edited target, with the app's exe recorded as the base.""" + fresh = shortcuts.shortcut(self.game()) + current = dict(fresh, Exe=exe, **edits) + state = { + 'app': {field: fresh[field] for field in shortcuts.FIELDS}, + 'steam': {field: current.get(field, '') for field in ('AppName', 'Exe', 'StartDir', 'LaunchOptions')}, + 'rev': 1, + } + entry, _ = shortcuts.merge(fresh, current, state, 0) + return entry + def test_settings_changed_in_steam_survive_its_restart(self): self.run_writer(self.game()) edits = dict(Exe='"%s/bin/Other.exe"' % GUEST, StartDir='"%s"' % GUEST, LaunchOptions='DXVK_HUD=1 %command%', @@ -82,6 +113,68 @@ class ShortcutsTest(unittest.TestCase): self.run_writer(self.game()) self.assertEqual(1, self.record()['games'][str(APPID)]['rev']) + def test_a_steam_target_whose_file_is_gone_goes_back_to_the_apps(self): + self.run_writer(self.game()) + self.steam_edits(Exe='"%s/bin/Other.exe"' % GUEST, StartDir='"%s/data"' % GUEST) + Path(shortcuts.unquoted('"%s/bin/Other.exe"' % GUEST)).unlink() + entry = self.run_writer(self.game())[APPID] + self.assertEqual('"%s/Example.exe"' % GUEST, entry['Exe']) + self.assertEqual('"%s"' % GUEST, entry['StartDir']) + self.assertEqual('"%s/Example.exe"' % GUEST, self.record()['games'][str(APPID)]['steam']['Exe']) + + def test_a_steam_target_whose_internal_folder_is_gone_goes_back_to_the_apps(self): + games = self.root / 'games' + (games / 'Stores').mkdir(parents=True) + self.patched(INTERNAL_ROOTS=(str(games / 'Stores'),)) + entry = self.merge_target('"%s/old-version/Other.exe"' % (games / 'Stores'), StartDir='"%s/data"' % GUEST) + self.assertEqual('"%s/Example.exe"' % GUEST, entry['Exe']) + self.assertEqual('"%s"' % GUEST, entry['StartDir']) + + def test_a_steam_target_on_an_unmounted_card_is_kept(self): + card = self.root / 'card' + card.mkdir() + self.patched(REMOVABLE_ROOTS=(str(card),)) + entry = self.merge_target('"%s/steamapps/common/Other.exe"' % card) + self.assertEqual('"%s/steamapps/common/Other.exe"' % card, entry['Exe']) + self.assertEqual('"%s"' % GUEST, entry['StartDir']) + + def test_a_steam_target_under_a_bare_card_mountpoint_is_kept(self): + card = self.root / 'card' + card.mkdir() + self.patched(REMOVABLE_ROOTS=(str(card),)) + entry = self.merge_target('"%s/Other.exe"' % card, StartDir='"%s/data"' % GUEST) + self.assertEqual('"%s/Other.exe"' % card, entry['Exe']) + self.assertEqual('"%s/data"' % GUEST, entry['StartDir']) + + def test_a_steam_target_is_classified_by_its_resolved_path(self): + games = self.root / 'games' + stores = games / 'Stores' + stores.mkdir(parents=True) + card = self.root / 'card' + card.mkdir() + self.patched(INTERNAL_ROOTS=(str(stores),), REMOVABLE_ROOTS=(str(card),)) + target = '"%s/../../card/Other.exe"' % stores + entry = self.merge_target(target, StartDir='"%s/data"' % GUEST) + self.assertEqual(target, entry['Exe']) + self.assertEqual('"%s/data"' % GUEST, entry['StartDir']) + + def test_a_steam_target_under_a_bare_store_mountpoint_is_kept(self): + stores = self.root / 'stores' + mount = stores / 'abcd1234' + mount.mkdir(parents=True) + self.patched(REMOVABLE_FAMILIES=(str(stores),)) + entry = self.merge_target('"%s/Other.exe"' % mount, StartDir='"%s/data"' % GUEST) + self.assertEqual('"%s/Other.exe"' % mount, entry['Exe']) + self.assertEqual('"%s/data"' % GUEST, entry['StartDir']) + + def test_a_steam_target_under_a_mounted_card_goes_back_to_the_apps(self): + card = self.root / 'card' + (card / 'steamapps').mkdir(parents=True) + self.patched(REMOVABLE_ROOTS=(str(card),)) + entry = self.merge_target('"%s/Other.exe"' % card, StartDir='"%s/data"' % GUEST) + self.assertEqual('"%s/Example.exe"' % GUEST, entry['Exe']) + self.assertEqual('"%s"' % GUEST, entry['StartDir']) + def test_the_app_taking_steams_target_keeps_steams_start_in(self): self.run_writer(self.game()) self.steam_edits(Exe='"%s/bin/Other.exe"' % GUEST, StartDir='"%s/data"' % GUEST)