diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 8fd6411fd6..0ad123b619 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -307,8 +307,8 @@ jobs: set -o pipefail ./gradlew assembleRelease --console=plain -PndkVersion="$NDK_VERSION" 2>&1 | tee /tmp/build.log - - name: Game environment launch tests - run: python3 -m unittest discover -s tools/tests -p test_game_environment.py + - name: Steam import and game environment tests + run: python3 -m unittest discover -s tools/tests -p 'test_*.py' - name: Unit tests run: ./gradlew testReleaseUnitTest --console=plain -PndkVersion="$NDK_VERSION" diff --git a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt index 9aa715fcfe..e10bf04d2c 100644 --- a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt @@ -991,14 +991,14 @@ class MainActivity : ComponentActivity() { val games = if (ready) Library.steamGames(this) + com.droiddeck.launcher.frontend.AddedGames.scan(this).map { g -> com.droiddeck.launcher.frontend.AddedGameArt.resolve(this, g).let { art -> Library.SteamGame( - g.appId.toInt(), g.name, art.portrait ?: art.header, "added", g.gameId, + g.steamAppId ?: g.appId.toInt(), g.name, art.portrait ?: art.header, "added", g.gameId, hero = art.hero ?: art.header, gameFiles = g.folder, - protonPrefix = Library.protonPrefix(this, g.appId), + protonPrefix = Library.protonPrefix(this, g.steamAppId?.toLong() ?: g.appId), ) } } else emptyList() val emus = Library.emulators(this) { id -> DesktopCatalog.installed(this, id) != null } - ui.post { steamGames = games; emulatorList = emus } + ui.post { steamGames = games.distinctBy { it.gameId }; emulatorList = emus } // Box art for the games that have none, fetched after the list is up; the list is // rebuilt once if any was found. if (!OfflineMode.enabled(this) && CoverArt.fetchMissing(this, emus.flatMap { it.games })) { diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/AddedGames.kt b/app/src/main/java/com/droiddeck/launcher/frontend/AddedGames.kt index bd5a759832..6d833d9318 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/AddedGames.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/AddedGames.kt @@ -1,24 +1,15 @@ package com.droiddeck.launcher.frontend import android.content.Context -import android.os.Environment import android.util.Log +import com.droiddeck.launcher.runtime.LinuxRuntime +import org.json.JSONObject import com.droiddeck.launcher.session.GameStorage import com.droiddeck.launcher.session.SessionPrefs import java.io.File import java.util.zip.CRC32 -/** - * The user's own Windows games, added to the Steam client's library. - * - * A Games folder is scanned one level deep: each subfolder is one game, and the program to launch - * is the .exe in it that looks most like the game - the one named after the folder, else the - * largest, never an installer, redistributable or crash reporter - unless the user picked one - * for that game. Each game becomes a non-Steam shortcut in the client, under the ARM64 Proton, - * with the appid the client itself would derive, so launching from the rail and from the - * client's own library are the same thing. Any number of Games folders, from anywhere on the - * device: each is bound into the session under /root/Games on its own. - */ +/** Windows game folders shared with the Steam session. */ object AddedGames { private const val TAG = "AddedGames" @@ -29,6 +20,7 @@ object AddedGames { /** What steam://rungameid/ takes for a shortcut. */ val gameId: Long, val candidates: List, + val steamAppId: Int? = null, ) { fun folderName(): String = folder.name } @@ -96,12 +88,15 @@ object AddedGames { fun scan(context: Context): List { val out = ArrayList() - for (root in roots(context)) { - val dir = root.host + val folders = roots(context).map { it.host } + listOfNotNull( + GameStorage.effective(context)?.let { File(it.path) }, + GameStorage.effective(context)?.let { File(it.path, "steamapps/common") }, + ) + for (dir in folders.distinctBy { it.absolutePath }) { if (!dir.isDirectory) { Log.w(TAG, "$dir is not a folder; skipped"); continue } for (folder in dir.listFiles { f -> f.isDirectory }?.sortedBy { it.name.lowercase() } ?: emptyList()) scanGame(context, folder, out) } - return out + return out.distinctBy { it.folder.canonicalPath } } private fun scanGame(context: Context, folder: File, out: MutableList) { @@ -115,10 +110,23 @@ object AddedGames { val name = folder.name val crc = CRC32().apply { update(("\"$guestExe\"" + name).toByteArray()) }.value val appId = crc or 0x80000000L - out.add(Game(folder, name, exe, guestExe, guestDir, appId, (appId shl 32) or 0x02000000L, candidates)) + val steamRoot = File(LinuxRuntime.rootDir(context), "root/.local/share/Steam") + val steamId = steamRoute(steamRoot, appId) + out.add(Game(folder, name, exe, guestExe, guestDir, appId, + steamId?.toLong() ?: ((appId shl 32) or 0x02000000L), candidates, steamId)) } } + private fun steamRoute(root: File, appId: Long): Int? = runCatching { + val users = File(root, "config/loginusers.vdf").readText() + val blocks = Regex(""""(\d{5,})"\s*\{([^}]*)\}""") + val recent = blocks.findAll(users).firstOrNull { Regex(""""MostRecent"\s*"1"""").containsMatchIn(it.groupValues[2]) } + ?: return@runCatching null + val account = recent.groupValues[1].toLong() - 76561197960265728L + JSONObject(File(root, "userdata/$account/config/.droiddeck-routes.json").readText()) + .optInt(appId.toString()).takeIf { it > 0 } + }.getOrNull() + /** The list the session hands the runtime's shortcuts writer; one file per session start. */ fun writeListing(context: Context, games: List): File { val file = File(context.filesDir, "session/added-games.json").apply { parentFile?.mkdirs() } @@ -126,6 +134,7 @@ object AddedGames { games.forEachIndexed { i, g -> if (i > 0) json.append(',') json.append("{\"name\":").append(quote(g.name)).append(",\"exe\":").append(quote(g.guestExe)) + .append(",\"folder\":").append(quote(guestPath(context, g.folder) ?: g.guestDir)) .append(",\"dir\":").append(quote(g.guestDir)).append(",\"appid\":").append(g.appId) // The art, as the session sees it: the app's cache is bound at its own path, a file in // the game's folder at the folder's guest path. @@ -145,5 +154,5 @@ object AddedGames { return guestPath(context, file) } - private fun quote(s: String) = "\"" + s.replace("\\", "\\\\").replace("\"", "\\\"") + "\"" + private fun quote(s: String): String = JSONObject.quote(s) } diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt index be31867ac3..2bb2f1010e 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt @@ -40,6 +40,7 @@ object SessionFiles { "usr/local/bin/bannerlator-desktop-games" to "usr/local/bin/bannerlator-desktop-games", "usr/local/bin/bannerlator-steam-shim" to "usr/local/bin/bannerlator-steam-shim", "usr/local/bin/bannerlator-steam-shortcuts" to "usr/local/bin/bannerlator-steam-shortcuts", + "usr/local/bin/bannerlator-steam-games" to "usr/local/bin/bannerlator-steam-games", "usr/local/bin/bannerlator-pad-defaults" to "usr/local/bin/bannerlator-pad-defaults", // The SteamOS helpers the client calls in Deck mode: the two Armada found it needs, plus // the three under /usr/bin, all no-ops that answer "nothing to do" (see each file). diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt index a071e2686f..0cc8bdedd9 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt @@ -309,6 +309,7 @@ class SessionService : Service() { val added = com.droiddeck.launcher.frontend.AddedGames.scan(this) val listing = com.droiddeck.launcher.frontend.AddedGames.writeListing(this, added) guest.add("BL_ADDED_GAMES=" + listing.path) + if (OfflineMode.enabled(this)) guest.add("BL_STEAM_OFFLINE=1") if (added.isNotEmpty()) Log.i(TAG, "added games: " + added.joinToString { "${it.name} (${it.exe.name})" }) } // Where the guest leaves a request for another session (the desktop's Steam launchers). diff --git a/app/src/main/java/com/droiddeck/launcher/ui/ModeSettingsDialog.kt b/app/src/main/java/com/droiddeck/launcher/ui/ModeSettingsDialog.kt index 0d6bb8003b..f7f8d32e1d 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/ModeSettingsDialog.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/ModeSettingsDialog.kt @@ -16,12 +16,12 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue +import androidx.compose.ui.res.stringResource +import com.droiddeck.launcher.R import androidx.compose.ui.Modifier import androidx.compose.ui.focus.focusRequester import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp -import androidx.compose.ui.res.stringResource -import com.droiddeck.launcher.R import com.droiddeck.launcher.core.FexPreset import com.droiddeck.launcher.runtime.DeckyManager import com.droiddeck.launcher.session.SessionPrefs @@ -349,13 +349,13 @@ fun ModeSettingsPage(s: ModeSettings, a: ModeSettingsActions) { for (dir in s.addedGamesDirs) { val n = s.addedGames.count { it.folderPath.startsWith("$dir/") } ActionRow( - dir.substringAfterLast('/').ifEmpty { dir }, dir + " · " + (if (n == 0) "no game folders with a .exe found" else "$n game${if (n == 1) "" else "s"}") + ". Forget: the games leave the client's library at the next session start; nothing on disk is touched.", + dir.substringAfterLast('/').ifEmpty { dir }, dir + " · " + (if (n == 0) "no game folders with a .exe found" else "$n game${if (n == 1) "" else "s"}") + ". " + stringResource(R.string.added_games_forget_hint), "Forget", onClick = { a.onForgetAddedGamesDir(dir) }, ) } ActionRow( if (s.addedGamesDirs.isEmpty()) "Games folder" else "Another games folder", - "Your own Windows games, one subfolder each, anywhere: internal storage, the SD card, a USB drive. As many folders as you like. Each game goes into the client's library as a non-Steam game under the ARM64 Proton, at the next session start.", + stringResource(R.string.added_games_import_hint), "Add…", onClick = a.onPickAddedGamesDir, ) ToggleRow( @@ -403,7 +403,7 @@ fun ModeSettingsPage(s: ModeSettings, a: ModeSettingsActions) { val open = host.open == "storage" SettingsRow( "Second library", - "Adds a library location in Steam. Applies next session.", + stringResource(R.string.second_library_import_hint), highlighted = open, ) { androidx.compose.foundation.layout.Box { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a0519dce57..10cf334af2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -19,6 +19,9 @@ Resume Stop session Frame generation + Choose Windows games with one subfolder per game. Steam checks ownership while signed in. Confirmed games use their Steam library entries after restarting Steam; other games stay as non-Steam shortcuts. Steam may verify or download files for imported games. + Adds a Steam library and scans Windows game folders here and in steamapps/common. After the ownership check and a Steam restart, owned games use Steam entries; other games use non-Steam shortcuts. + Forget removes non-Steam shortcuts next session. Manage Steam installations in Steam. Files stay on disk. Game environment Shared defaults and per-game overrides for games using Proton. Applies on the next game launch. Edit variables diff --git a/docs/development/steam-game-imports.md b/docs/development/steam-game-imports.md new file mode 100644 index 0000000000..4639afa5bd --- /dev/null +++ b/docs/development/steam-game-imports.md @@ -0,0 +1,62 @@ +# Steam game imports + +Second Library still registers the selected Steam library. It now also scans Windows +executables in its immediate game subfolders and `steamapps/common`, using the same +importer as Added Games. Existing Steam manifests are preserved. + +`bannerlator-steam-games watch` runs beside the client. It identifies a game by an +existing manifest's install directory, a `steam_appid.txt` beside the game or selected +executable, or a unique normalized title from Steam's store search with an explicit +country. Matching also accepts the terminal Windows Edition and INTERGRADE suffixes; +multiple matches still remain unresolved. Artwork's fuzzy matching is deliberately not +used for identity. Ambiguous or unresolved titles remain non-Steam games. + +Ownership comes from the running native ARM64 client's `BIsSubscribedApp`, through +versioned SteamClient020 / SteamUser021 / STEAMAPPS_INTERFACE_VERSION008 interfaces. The +helper gets the account from that connection, not from a public profile. No API key, +password, profile visibility change, or CEF debugging port is needed. Each probe runs in +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 +use the last successful snapshot for the selected account. A newly identified title +therefore starts as a shortcut and becomes a Steam entry after Steam exits and starts +again. An unavailable client or offline first launch falls back to shortcuts; existing +snapshots remain usable offline. Steam still enforces the current license at launch. + +For a confirmed game without a manifest, the importer links the source folder into the +internal Steam library and writes an installed manifest with `Universe=1` and +`StateFlags=4`. Steam ignores manifests without the universe field. This registers +existing files; it does not assert a verified build ID or depot manifest. Steam can +still require verification, updates or redistributables. Steam updates and uninstall +operate on the linked game files, so imported folders must be writable. Existing Steam +manifests and directories are preserved. Only the exact minimal manifests generated by +the earlier importer are migrated, with the matching source symlink still present. If +registration fails, the game remains a shortcut. Once Steam takes over an install, +manage it in Steam; forgetting an Added Games folder only removes its shortcuts. + +The account's `.droiddeck-routes.json` maps shortcut IDs to real Steam app IDs so the +Android launcher uses the same title and Proton prefix as Steam. Other accounts do not +inherit those routes. Managed shortcuts are replaced; user-created shortcuts and +unreadable shortcut files are preserved. Both routes use the existing ARM64 Proton +registration. + +## Validation + +Run `python3 -m unittest discover -s tools/tests -p 'test_*.py'` and Android unit tests. +Tests cover identity and edition matching, network recovery, account isolation, +owned/unowned/unknown routing, legacy manifest migration, manifest preservation, +filesystem collisions, shortcut preservation, and the versioned IPC call/cleanup +contract. On 2026-09-28, the ARM64 Steam client (1790545198) confirmed ownership and +installed status through ISteamApps for Final Fantasy VII Rebirth (2909400), Final +Fantasy VII Remake Intergrade (1462040), and Final Fantasy XV (637650), with all three +install paths resolving to the original second-library folders. The old managed +shortcuts were removed. This verifies library registration, not complete game launches. +Unowned fallback and account isolation are covered by automated tests. + +References: [Steam Apps +API](https://partner.steamgames.com/doc/api/ISteamApps#BIsSubscribedApp), [Valve's +versioned interfaces](https://github.com/ValveSoftware/Proton/tree/proton_11.0/lsteamcli +ent/steamworks_sdk_154). diff --git a/tools/linuxfs/overlay/usr/local/bin/bannerlator-session b/tools/linuxfs/overlay/usr/local/bin/bannerlator-session index c0adc95e78..1cb8472a39 100755 --- a/tools/linuxfs/overlay/usr/local/bin/bannerlator-session +++ b/tools/linuxfs/overlay/usr/local/bin/bannerlator-session @@ -539,6 +539,10 @@ BUSCONF # again on the branch it just wrote. steam_channel=${BL_STEAM_CHANNEL:-publicbeta} steam_beta=$steam_root/package/beta + if [ -n "${BL_ADDED_GAMES:-}" ] && [ -f "$BL_ADDED_GAMES" ]; then + bannerlator-steam-games watch "$steam_root" "$BL_ADDED_GAMES" & + games_watcher=$! + fi echo "== steam args: ${_bl_ui[*]} -clientbeta $steam_channel -no-cef-sandbox -cef-force-gpu -cef-ozone-platform=x11 -cef-use-gl=angle -cef-use-angle=vulkan $*" # An update that cannot install asks for a restart (rc=42) every time; the count is capped so a # session can never again loop in silence, which reads as a menu opening and closing by itself. @@ -556,6 +560,9 @@ BUSCONF rc=$? echo "== steam exited rc=$rc" collect_steam_logs + if [ -n "${BL_ADDED_GAMES:-}" ] && [ -f "$BL_ADDED_GAMES" ]; then + bannerlator-steam-shortcuts "$steam_root" "$BL_ADDED_GAMES" || true + fi # The session asked for this exit (the ARM64 Proton landed): start again, without the URL # arguments, which were acted on. Not counted against the update cap. if [ -f "$HOME/.bl-restart-once" ]; then @@ -572,6 +579,10 @@ BUSCONF # The URL arguments were acted on the first time; a restart must not repeat them. set -- done + if [ -n "${games_watcher:-}" ]; then + kill "$games_watcher" 2>/dev/null || true + wait "$games_watcher" 2>/dev/null || true + fi if [ -n "${BL_DESKTOP:-}" ]; then # Started from the desktop (bannerlator-steam-launch): the desktop's own programs share this # script's parent, so reap_orphans - right under gamescope, where everything is the client's - diff --git a/tools/linuxfs/overlay/usr/local/bin/bannerlator-steam-games b/tools/linuxfs/overlay/usr/local/bin/bannerlator-steam-games new file mode 100755 index 0000000000..1e6f2a1fd5 --- /dev/null +++ b/tools/linuxfs/overlay/usr/local/bin/bannerlator-steam-games @@ -0,0 +1,254 @@ +#!/usr/bin/env python3 +"""Resolve added games against the signed-in Steam client's licenses.""" +import ctypes as C +import json +import os +from pathlib import Path +import re +import subprocess +import sys +import time +import unicodedata +import urllib.parse +import urllib.request + +STATE = ".droiddeck-games.json" +BASE_ID = 76561197960265728 + + +def read_json(path, default): + try: + return json.loads(Path(path).read_text()) + except (OSError, ValueError): + return default + + +def save_json(path, value): + path = Path(path) + path.parent.mkdir(parents=True, exist_ok=True) + temp = path.with_suffix(".tmp") + temp.write_text(json.dumps(value)) + os.replace(temp, path) + + +def title_key(name): + return "".join(c for c in unicodedata.normalize("NFKC", name.replace("™", "").replace("®", "")).casefold() if c.isalnum()) + + +def edition_key(name): + name = re.sub(r"(?i)\s+(?:windows edition|intergrade)\s*$", "", name) + return title_key(name) + + +def source_stamp(game): + try: + exe = Path(game["exe"]) + stat = exe.stat() + hints = [] + for parent in sorted({Path(game["folder"]), exe.parent}): + hint = parent / "steam_appid.txt" + hints.append(hint.read_text().strip() if hint.is_file() else "") + return [game["name"], str(exe), stat.st_size, stat.st_mtime_ns, hints] + except (OSError, ValueError): + return None + + +def identify(game): + folder = Path(game["folder"]) + steamapps = folder.parent.parent + if folder.parent.name == "common" and steamapps.name == "steamapps": + matches = [] + for manifest in steamapps.glob("appmanifest_*.acf"): + try: + text = manifest.read_text() + install = re.search(r'"installdir"\s*"([^"\\]*)"', text, re.I) + if install and install[1] == folder.name: + matches.append(int(manifest.stem.removeprefix("appmanifest_"))) + except (OSError, ValueError): + pass + if matches: + return matches[0] if len(matches) == 1 else None + ids = set() + for parent in {folder, Path(game["exe"]).parent}: + try: + appid = int((parent / "steam_appid.txt").read_text().strip()) + if 0 < appid < 0x80000000: + ids.add(appid) + except (OSError, ValueError): + pass + if ids: + return next(iter(ids)) if len(ids) == 1 else None + url = "https://store.steampowered.com/api/storesearch/?l=english&cc=US&term=" + urllib.parse.quote(game["name"]) + try: + with urllib.request.urlopen(url, timeout=5) as response: + items = json.loads(response.read(1024 * 1024)).get("items", []) + for key in (title_key, edition_key): + ids = {int(item["id"]) for item in items + if key(item.get("name", "")) == key(game["name"]) and key(game["name"])} + if ids: + return next(iter(ids)) if len(ids) == 1 else None + return None + except (OSError, ValueError, KeyError): + return None + + +def method(obj, slot, result, *args): + if not obj: + raise RuntimeError("Steam interface unavailable") + table = C.cast(obj, C.POINTER(C.POINTER(C.c_void_p))).contents + return C.CFUNCTYPE(result, C.c_void_p, *args)(table[slot]) + + +def probe(steam_root, ids): + # Slots belong to Valve's SteamClient020, SteamUser021 and STEAMAPPS_INTERFACE_VERSION008. + library = C.CDLL(str(Path(steam_root) / "steamrtarm64/steamclient.so")) + library.CreateInterface.argtypes = [C.c_char_p, C.c_void_p] + library.CreateInterface.restype = C.c_void_p + client = library.CreateInterface(b"SteamClient020", None) + pipe = method(client, 0, C.c_int)(client) + if not pipe: + raise RuntimeError("Steam pipe unavailable") + user = 0 + try: + user = method(client, 2, C.c_int, C.c_int)(client, pipe) + if not user: + raise RuntimeError("No signed-in Steam user") + get_interface = lambda slot, version: method(client, slot, C.c_void_p, C.c_int, C.c_int, C.c_char_p)(client, user, pipe, version) + account = get_interface(5, b"SteamUser021") + if not method(account, 1, C.c_bool)(account): + raise RuntimeError("Steam is offline") + steamid = method(account, 2, C.c_uint64)(account) + apps = get_interface(15, b"STEAMAPPS_INTERFACE_VERSION008") + subscribed = method(apps, 6, C.c_bool, C.c_uint32) + return {"account": str(steamid - BASE_ID), "checked": time.time(), + "owned": {str(appid): bool(subscribed(apps, appid)) for appid in ids}} + finally: + if user: + method(client, 4, None, C.c_int, C.c_int)(client, pipe, user) + method(client, 1, C.c_bool, C.c_int)(client, pipe) + + +def watch(steam_root, listing): + games = read_json(listing, []) + if os.environ.get("BL_STEAM_OFFLINE") == "1" or not games: + return + candidates, sources = {}, {} + next_lookup = 0 + last_status = None + while True: + now = time.monotonic() + for game in games: + folder = game["folder"] + stamp = source_stamp(game) + if now >= next_lookup or stamp != sources.get(folder): + sources[folder] = stamp + appid = identify(game) + if appid: + candidates[folder] = appid + else: + candidates.pop(folder, None) + if now >= next_lookup: + next_lookup = now + 300 + status = "waiting for a matching Steam title" + if candidates: + try: + result = subprocess.run([sys.executable, __file__, "probe", steam_root, + *map(str, sorted(set(candidates.values())))], + capture_output=True, text=True, timeout=15) + if result.returncode != 0: + status = "ownership probe failed: " + (result.stderr.strip()[-300:] or str(result.returncode)) + else: + line = next(line for line in result.stdout.splitlines() if line.startswith("DROIDDECK_OWNERSHIP=")) + snapshot = json.loads(line.removeprefix("DROIDDECK_OWNERSHIP=")) + if not snapshot["account"].isdigit() or int(snapshot["account"]) <= 0: + raise ValueError("invalid Steam account") + snapshot["candidates"] = candidates + snapshot["sources"] = sources + save_json(Path(steam_root) / "userdata" / snapshot["account"] / "config" / STATE, snapshot) + 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) + if status != last_status: + print("bannerlator-steam-games: " + status, flush=True) + last_status = status + time.sleep(30) + + +def seed_install(steam_root, game, appid): + steamapps = Path(steam_root) / "steamapps" + common = steamapps / "common" + name = f"DroidDeck-{appid}" + target = common / name + source = Path(game["folder"]) + title = game["name"].replace("\\", "\\\\").replace('"', '\\"').replace("\n", " ").replace("\r", " ") + content = ('"AppState"\n{\n' + f' "appid" "{appid}"\n "Universe" "1"\n "name" "{title}"\n' + + f' "StateFlags" "4"\n "installdir" "{name}"\n' + '}\n') + legacy = content.replace(' "Universe" "1"\n', '') + manifest = steamapps / f"appmanifest_{appid}.acf" + for root in [Path(steam_root), Path("/mnt/bannerlator-sd")]: + existing = root / "steamapps" / manifest.name + if existing.is_file(): + if (existing == manifest and source.is_dir() and target.is_symlink() + and os.readlink(target) == str(source) + and existing.read_text() in (legacy, legacy.replace('"StateFlags" "4"', '"StateFlags" "2"'))): + temp = manifest.with_suffix(".droiddeck") + temp.write_text(content) + os.replace(temp, manifest) + return True + if not source.is_dir(): + return False + common.mkdir(parents=True, exist_ok=True) + if os.path.lexists(target): + if not target.is_symlink() or os.readlink(target) != str(source): + return False + else: + target.symlink_to(source, target_is_directory=True) + try: + with manifest.open("x") as output: + output.write(content) + except FileExistsError: + pass + return True + + +def route(steam_root, acct, games): + snapshot = read_json(Path(acct) / "config" / STATE, {}) + if not isinstance(snapshot, dict): + snapshot = {} + owned = snapshot.get("owned", {}) if snapshot.get("account") == Path(acct).name else {} + candidates = snapshot.get("candidates", {}) + sources = snapshot.get("sources", {}) + if not all(isinstance(value, dict) for value in (owned, candidates, sources)): + owned, candidates, sources = {}, {}, {} + shortcuts, routes = [], {} + for game in games: + appid = candidates.get(game.get("folder")) + stamp = source_stamp(game) + confirmed = (isinstance(appid, int) and 0 < appid < 0x80000000 + and owned.get(str(appid)) is True and stamp is not None + and sources.get(game.get("folder")) == stamp) + if confirmed: + try: + if seed_install(steam_root, game, appid): + routes[str(game["appid"])] = appid + continue + except OSError as error: + print(f"bannerlator-steam-games: import unavailable ({error})") + shortcuts.append(game) + save_json(Path(acct) / "config/.droiddeck-routes.json", routes) + return shortcuts, routes + + +if __name__ == "__main__": + if len(sys.argv) < 4: + sys.exit(2) + try: + if sys.argv[1] == "probe": + print("DROIDDECK_OWNERSHIP=" + json.dumps(probe(sys.argv[2], [int(value) for value in sys.argv[3:]])), flush=True) + elif sys.argv[1] == "watch": + watch(sys.argv[2], sys.argv[3]) + else: + sys.exit(2) + except (OSError, RuntimeError, ValueError) as error: + print(str(error), file=sys.stderr) + sys.exit(1) diff --git a/tools/linuxfs/overlay/usr/local/bin/bannerlator-steam-shortcuts b/tools/linuxfs/overlay/usr/local/bin/bannerlator-steam-shortcuts index 7f76069120..c3fcd4ae33 100755 --- a/tools/linuxfs/overlay/usr/local/bin/bannerlator-steam-shortcuts +++ b/tools/linuxfs/overlay/usr/local/bin/bannerlator-steam-shortcuts @@ -1,15 +1,7 @@ #!/usr/bin/env python3 -"""Puts the app's added games (Windows games in the user's own folders) into the Steam client's -library as non-Steam shortcuts, each mapped to the ARM64 Proton, before the client starts. +"""Import added games while Steam is closed, preserving user-created shortcuts. - bannerlator-steam-shortcuts - -The JSON is a list the app writes each session: {"name", "exe", "dir", "appid"} with guest paths. -The client keeps non-Steam games in userdata//config/shortcuts.vdf, a binary VDF it -reads at start-up and rewrites at exit, so this runs while it is down. Entries the app wrote -carry the tag "droiddeck-app" and are replaced wholesale; anything the user added inside the -client is kept as it is. The appid is the one the client itself derives (crc32 of exe+name with -the top bit set), so the launch URL the app uses matches what the client shows. +Usage: bannerlator-steam-shortcuts """ import importlib.machinery import importlib.util @@ -207,8 +199,15 @@ def main(): try: with open(path, "rb") as f: existing = parse(f.read()).get("shortcuts", {}) - except (ValueError, IndexError) as e: - print("bannerlator-steam-shortcuts: could not read the existing file (%s); starting over" % e) + except (ValueError, IndexError, struct.error) as e: + print("bannerlator-steam-shortcuts: could not read the existing file (%s); keeping it unchanged" % e) + return 1 + helper = os.path.join(os.path.dirname(os.path.abspath(__file__)), "bannerlator-steam-games") + loader = importlib.machinery.SourceFileLoader("steam_games", helper) + spec = importlib.util.spec_from_loader(loader.name, loader) + routing = importlib.util.module_from_spec(spec) + loader.exec_module(routing) + games, routes = routing.route(steam_root, acct, games) kept = [v for v in existing.values() if isinstance(v, dict) and TAG not in (v.get("tags") or {}).values()] ours = [shortcut(g) for g in games if g.get("exe") and g.get("name")] entries = {str(i): v for i, v in enumerate(kept + ours)} @@ -226,8 +225,8 @@ def main(): compat = importlib.util.module_from_spec(spec) spec.loader.exec_module(compat) cfg = os.path.join(steam_root, "config", "config.vdf") - if os.path.isfile(cfg) and ours: - compat.register_default(cfg, apps=[str(int(g["appid"]) & 0xFFFFFFFF) for g in games if g.get("exe")]) + if os.path.isfile(cfg) and (ours or routes): + compat.register_default(cfg, apps=[str(int(g["appid"]) & 0xFFFFFFFF) for g in games if g.get("exe")] + [str(appid) for appid in set(routes.values())]) print("bannerlator-steam-shortcuts: mapped them to %s" % compat.TOOL) except Exception as e: # noqa: BLE001 - the games still show; the client falls back to the default tool print("bannerlator-steam-shortcuts: compat mapping skipped (%s)" % e) diff --git a/tools/tests/test_steam_games.py b/tools/tests/test_steam_games.py new file mode 100644 index 0000000000..26220319d9 --- /dev/null +++ b/tools/tests/test_steam_games.py @@ -0,0 +1,231 @@ +import importlib.machinery +import importlib.util +import json +from pathlib import Path +import subprocess +import tempfile +import unittest +from unittest.mock import patch + +BIN = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin' + + +def load(name): + loader = importlib.machinery.SourceFileLoader(name, str(BIN / name)) + spec = importlib.util.spec_from_loader(name, loader) + module = importlib.util.module_from_spec(spec) + loader.exec_module(module) + return module + + +imports = load('bannerlator-steam-games') +shortcuts = load('bannerlator-steam-shortcuts') + + +class ImportsTest(unittest.TestCase): + def setUp(self): + self.temp = tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + self.root = Path(self.temp.name) + self.steam = self.root / 'Steam' + self.acct = self.steam / 'userdata/123' + self.folder = self.root / 'Games/Example' + self.folder.mkdir(parents=True) + self.exe = self.folder / 'Example.exe' + self.exe.write_bytes(b'game') + self.game = dict(name='Example', exe=str(self.exe), folder=str(self.folder), + dir=str(self.folder), appid=0x87654321) + + def snapshot(self, owned=True, account='123'): + imports.save_json(self.acct / 'config' / imports.STATE, + dict(account=account, owned={'42': owned}, + candidates={str(self.folder): 42}, + sources={str(self.folder): imports.source_stamp(self.game)})) + + def test_owned_uses_real_id_and_registers_installed_files(self): + self.snapshot() + games, routes = imports.route(self.steam, self.acct, [self.game]) + self.assertEqual([], games) + self.assertEqual({str(self.game['appid']): 42}, routes) + manifest = self.steam / 'steamapps/appmanifest_42.acf' + self.assertIn('"StateFlags" "4"', manifest.read_text()) + self.assertIn('"Universe" "1"', manifest.read_text()) + self.assertEqual(self.folder, (self.steam / 'steamapps/common/DroidDeck-42').resolve()) + self.assertEqual(b'game', self.exe.read_bytes()) + + def test_migrates_only_unchanged_legacy_import_manifests(self): + self.snapshot() + imports.route(self.steam, self.acct, [self.game]) + path = self.steam / 'steamapps/appmanifest_42.acf' + current = path.read_text() + legacy = current.replace(' "Universe" "1"\n', '').replace('"StateFlags" "4"', '"StateFlags" "2"') + path.write_text(legacy) + imports.route(self.steam, self.acct, [self.game]) + self.assertEqual(current, path.read_text()) + changed = legacy.replace('"StateFlags" "2"', '"StateFlags" "6"') + path.write_text(changed) + imports.route(self.steam, self.acct, [self.game]) + self.assertEqual(changed, path.read_text()) + + def test_unresolved_titles_are_retried_after_network_recovers(self): + listing = self.root / 'games.json' + listing.write_text(json.dumps([self.game])) + from unittest.mock import Mock + result = Mock(returncode=0, stdout='DROIDDECK_OWNERSHIP=' + json.dumps(dict(account='123', owned={'42': True}))) + with patch.object(imports, 'identify', side_effect=[None, 42]) as identify, \ + patch.object(imports.time, 'monotonic', side_effect=[0, 301]), \ + patch.object(imports.time, 'sleep', side_effect=[None, RuntimeError('stop')]), \ + patch.object(imports.subprocess, 'run', return_value=result), \ + patch('builtins.print'): + with self.assertRaisesRegex(RuntimeError, 'stop'): + imports.watch(self.steam, listing) + self.assertEqual(2, identify.call_count) + snapshot = imports.read_json(self.acct / 'config' / imports.STATE, {}) + self.assertTrue(snapshot['owned']['42']) + + def test_unowned_unknown_and_different_account_stay_shortcuts(self): + for owned, account in [(False, '123'), (True, '456')]: + self.snapshot(owned, account) + self.assertEqual(([self.game], {}), imports.route(self.steam, self.acct, [self.game])) + (self.acct / 'config' / imports.STATE).unlink() + self.assertEqual(([self.game], {}), imports.route(self.steam, self.acct, [self.game])) + self.assertFalse((self.steam / 'steamapps').exists()) + + def test_existing_manifest_is_never_overwritten(self): + manifest = self.steam / 'steamapps/appmanifest_42.acf' + manifest.parent.mkdir(parents=True) + manifest.write_text('Steam owns this file') + self.snapshot() + imports.route(self.steam, self.acct, [self.game]) + self.assertEqual('Steam owns this file', manifest.read_text()) + + def test_import_collision_falls_back_without_touching_files(self): + occupied = self.steam / 'steamapps/common/DroidDeck-42' + occupied.mkdir(parents=True) + self.snapshot() + self.assertEqual(([self.game], {}), imports.route(self.steam, self.acct, [self.game])) + self.assertTrue(occupied.is_dir()) + self.assertFalse(occupied.is_symlink()) + + def test_removed_folder_does_not_import(self): + self.snapshot() + self.exe.unlink() + self.folder.rmdir() + self.assertEqual(([self.game], {}), imports.route(self.steam, self.acct, [self.game])) + + def test_replaced_game_does_not_inherit_cached_ownership(self): + self.snapshot() + self.exe.write_bytes(b'a different game') + self.assertEqual(([self.game], {}), imports.route(self.steam, self.acct, [self.game])) + + def test_duplicate_owned_folders_share_one_steam_entry(self): + self.snapshot() + games, routes = imports.route(self.steam, self.acct, [self.game, self.game]) + self.assertEqual([], games) + self.assertEqual(1, len(routes)) + self.assertEqual(1, len(list((self.steam / 'steamapps').glob('*.acf')))) + + def test_explicit_appid_does_not_need_network(self): + (self.folder / 'steam_appid.txt').write_text('42\n') + with patch.object(imports.urllib.request, 'urlopen', side_effect=AssertionError): + self.assertEqual(42, imports.identify(self.game)) + + def test_manifest_identity_uses_install_directory(self): + folder = self.root / 'second/steamapps/common/Custom Name' + folder.mkdir(parents=True) + (folder.parent.parent / 'appmanifest_42.acf').write_text('"AppState" { "installdir" "Custom Name" }') + game = dict(self.game, folder=str(folder), exe=str(folder / 'game.exe')) + with patch.object(imports.urllib.request, 'urlopen', side_effect=AssertionError): + self.assertEqual(42, imports.identify(game)) + + def test_store_requires_unique_exact_title(self): + cases = [([{'id': 42, 'name': 'Example Deluxe'}], None), + ([{'id': 42, 'name': 'Example'}, {'id': 43, 'name': 'Example'}], None), + ([{'id': 42, 'name': 'EXAMPLE'}], 42)] + for items, expected in cases: + with patch.object(imports.urllib.request, 'urlopen') as request: + request.return_value.__enter__.return_value.read.return_value = json.dumps({'items': items}).encode() + self.assertEqual(expected, imports.identify(self.game)) + + def test_store_country_and_edition_suffixes(self): + for folder, store in [('FINAL FANTASY VII REMAKE', 'FINAL FANTASY VII REMAKE INTERGRADE'), + ('FINAL FANTASY XV', 'FINAL FANTASY XV WINDOWS EDITION')]: + with patch.object(imports.urllib.request, 'urlopen') as request: + request.return_value.__enter__.return_value.read.return_value = json.dumps({'items': [{'id': 42, 'name': store}]}).encode() + self.assertEqual(42, imports.identify(dict(self.game, name=folder))) + self.assertIn('cc=US&', request.call_args.args[0]) + + def test_edition_matching_rejects_ambiguous_titles_and_soundtracks(self): + with patch.object(imports.urllib.request, 'urlopen') as request: + request.return_value.__enter__.return_value.read.return_value = json.dumps({'items': [ + {'id': 42, 'name': 'Example WINDOWS EDITION'}, + {'id': 43, 'name': 'Example INTERGRADE'}, + {'id': 44, 'name': 'Example Soundtrack'}, + ]}).encode() + self.assertIsNone(imports.identify(self.game)) + + def test_corrupt_shortcuts_are_preserved(self): + path = self.acct / 'config/shortcuts.vdf' + path.parent.mkdir(parents=True) + original = b'corrupt' + path.write_bytes(original) + listing = self.root / 'games.json' + listing.write_text(json.dumps([self.game])) + run = subprocess.run(['python3', str(BIN / 'bannerlator-steam-shortcuts'), str(self.steam), str(listing)], capture_output=True) + self.assertEqual(1, run.returncode) + self.assertEqual(original, path.read_bytes()) + + def test_user_shortcuts_survive_owned_promotion(self): + self.snapshot() + path = self.acct / 'config/shortcuts.vdf' + manual = dict(shortcuts.shortcut(self.game), tags={'0': 'Personal'}, AppName='Manual') + path.write_bytes(shortcuts.write({'shortcuts': {'0': manual, '1': shortcuts.shortcut(self.game)}})) + listing = self.root / 'games.json' + listing.write_text(json.dumps([self.game])) + subprocess.run(['python3', str(BIN / 'bannerlator-steam-shortcuts'), str(self.steam), str(listing)], check=True, capture_output=True) + result = shortcuts.parse(path.read_bytes())['shortcuts'] + self.assertEqual([manual], list(result.values())) + self.assertEqual({str(self.game['appid']): 42}, json.loads((path.parent / '.droiddeck-routes.json').read_text())) + + +class SteamProbeTest(unittest.TestCase): + def test_probe_reads_account_and_licenses_and_releases_handles(self): + import ctypes as C + from unittest.mock import Mock + objects, callbacks, calls = [], [], [] + + def interface(slots): + table = (C.c_void_p * 24)() + for slot, result, args, callback in slots: + fn = C.CFUNCTYPE(result, C.c_void_p, *args)(callback) + callbacks.append(fn) + table[slot] = C.cast(fn, C.c_void_p).value + obj = C.pointer(C.cast(table, C.POINTER(C.c_void_p))) + objects.extend([table, obj]) + return C.cast(obj, C.c_void_p).value + + user = interface([ + (1, C.c_bool, [], lambda _: True), + (2, C.c_uint64, [], lambda _: imports.BASE_ID + 123), + ]) + apps = interface([(6, C.c_bool, [C.c_uint32], lambda _, appid: appid == 42)]) + client = interface([ + (0, C.c_int, [], lambda _: 7), + (1, C.c_bool, [C.c_int], lambda _, pipe: calls.append(('pipe', pipe)) or True), + (2, C.c_int, [C.c_int], lambda _, pipe: 9), + (4, None, [C.c_int, C.c_int], lambda _, pipe, handle: calls.append(('user', pipe, handle))), + (5, C.c_void_p, [C.c_int, C.c_int, C.c_char_p], lambda _, handle, pipe, version: user if version == b'SteamUser021' else 0), + (15, C.c_void_p, [C.c_int, C.c_int, C.c_char_p], lambda _, handle, pipe, version: apps if version == b'STEAMAPPS_INTERFACE_VERSION008' else 0), + ]) + library = Mock() + library.CreateInterface.return_value = client + with patch.object(imports.C, 'CDLL', return_value=library): + result = imports.probe('/Steam', [42, 43]) + self.assertEqual('123', result['account']) + self.assertEqual({'42': True, '43': False}, result['owned']) + self.assertEqual([('user', 7, 9), ('pipe', 7)], calls) + library.CreateInterface.assert_called_once_with(b'SteamClient020', None) + + +if __name__ == '__main__': + unittest.main()