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 2a83d57e5b..93ddf80ed1 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/AddedGames.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/AddedGames.kt @@ -387,11 +387,18 @@ object AddedGames { } } + private class LoginUser(val id: String, val mostRecent: Boolean, val autoLogin: Boolean, val timestamp: Long) + + /** The signed-in account's config, chosen as droiddeck-steam-shortcuts chooses it: MostRecent, else AutoLogin, else the latest timestamp. */ internal fun accountConfig(root: File): File? { val users = runCatching { File(root, "config/loginusers.vdf").readText() }.getOrDefault("") - val recent = Regex(""""(\d{5,})"\s*\{([^}]*)\}""").findAll(users) - .lastOrNull { Regex(""""MostRecent"\s*"1"""").containsMatchIn(it.groupValues[2]) } - val account = recent?.groupValues?.get(1)?.toLongOrNull()?.let { (it - 76561197960265728L).toString() } + val recent = Regex(""""(\d{5,})"\s*\{([^}]*)\}""").findAll(users).map { + val body = it.groupValues[2] + LoginUser(it.groupValues[1], Regex(""""MostRecent"\s*"1"""").containsMatchIn(body), Regex(""""AutoLogin"\s*"1"""").containsMatchIn(body), + Regex(""""timestamp"\s*"(\d+)"""").find(body)?.groupValues?.get(1)?.toLongOrNull() ?: 0L) + }.filter { it.mostRecent || it.autoLogin || it.timestamp > 0 }.toList().asReversed() + .maxWithOrNull(compareBy({ it.mostRecent }, { it.autoLogin }, { it.timestamp })) + val account = recent?.id?.toLongOrNull()?.let { (it - 76561197960265728L).toString() } ?: File(root, "userdata").list()?.filter { it.isNotEmpty() && it.all(Char::isDigit) && it != "0" }?.singleOrNull() return account?.let { File(root, "userdata/$it/config") } } diff --git a/app/src/test/java/com/droiddeck/launcher/frontend/AddedGamesAccountTest.kt b/app/src/test/java/com/droiddeck/launcher/frontend/AddedGamesAccountTest.kt new file mode 100644 index 0000000000..610f00bb4e --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/frontend/AddedGamesAccountTest.kt @@ -0,0 +1,43 @@ +package com.droiddeck.launcher.frontend + +import java.io.File +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder + +class AddedGamesAccountTest { + @get:Rule val tmp = TemporaryFolder() + + private fun steamRoot(vararg blocks: Pair>): File = tmp.newFolder("Steam").also { root -> + File(root, "config").mkdirs() + File(root, "userdata/123/config").mkdirs() + File(root, "userdata/456/config").mkdirs() + val body = blocks.joinToString("") { (account, keys) -> + "\t\"${76561197960265728L + account}\"\n\t{\n" + keys.entries.joinToString("") { "\t\t\"${it.key}\"\t\t\"${it.value}\"\n" } + "\t}\n" + } + File(root, "config/loginusers.vdf").writeText("\"users\"\n{\n$body}\n") + } + + @Test fun theAccountMarkedMostRecentIsUsed() { + val root = steamRoot(123L to mapOf("MostRecent" to "1", "AutoLogin" to "0", "timestamp" to "100"), + 456L to mapOf("MostRecent" to "0", "AutoLogin" to "1", "timestamp" to "200")) + assertEquals(File(root, "userdata/123/config"), AddedGames.accountConfig(root)) + } + + @Test fun withoutMostRecentTheAccountThatSignsInIsUsed() { + val root = steamRoot(123L to mapOf("AutoLogin" to "1", "timestamp" to "100"), 456L to mapOf("AutoLogin" to "0", "timestamp" to "200")) + assertEquals(File(root, "userdata/123/config"), AddedGames.accountConfig(root)) + } + + @Test fun withoutEitherMarkTheAccountUsedLastIsUsed() { + val root = steamRoot(456L to mapOf("AutoLogin" to "0", "timestamp" to "100"), 123L to mapOf("AutoLogin" to "0", "timestamp" to "200")) + assertEquals(File(root, "userdata/123/config"), AddedGames.accountConfig(root)) + } + + @Test fun twoAccountsWithoutAnyMarkGiveNoAccount() { + val root = steamRoot(123L to mapOf("AccountName" to "a"), 456L to mapOf("AccountName" to "b")) + assertNull(AddedGames.accountConfig(root)) + } +} diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-shortcuts b/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-shortcuts index 3764e4a92c..68cf8f775b 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-shortcuts +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-shortcuts @@ -16,15 +16,20 @@ TAG = "droiddeck-app" def account_dir(steam_root): - """The most recent account's userdata folder, else the only one that is not 0.""" + """The signed-in account's userdata folder, else the only one that is not 0. Clients that do not + write MostRecent still mark that account AutoLogin; the latest timestamp breaks the remaining ties.""" users = os.path.join(steam_root, "config", "loginusers.vdf") - recent = None + recent, best = None, None try: with open(users, encoding="utf-8", errors="replace") as f: text = f.read() for block in re.finditer(r'"(\d{5,})"\s*\{([^}]*)\}', text): - if re.search(r'"MostRecent"\s*"1"', block.group(2)): - recent = int(block.group(1)) + stamp = re.search(r'"timestamp"\s*"(\d+)"', block.group(2)) + rank = (bool(re.search(r'"MostRecent"\s*"1"', block.group(2))), + bool(re.search(r'"AutoLogin"\s*"1"', block.group(2))), + int(stamp.group(1)) if stamp else 0) + if any(rank) and (best is None or rank >= best): + recent, best = int(block.group(1)), rank except OSError: pass userdata = os.path.join(steam_root, "userdata") diff --git a/tools/tests/test_steam_shortcuts.py b/tools/tests/test_steam_shortcuts.py index 60a2998a60..a927d4db07 100644 --- a/tools/tests/test_steam_shortcuts.py +++ b/tools/tests/test_steam_shortcuts.py @@ -203,6 +203,30 @@ class ShortcutsTest(unittest.TestCase): self.assertEqual('-vulkan', steam['LaunchOptions']) self.assertEqual('Mine', steam['AppName']) + def users(self, *blocks): + body = ''.join('\t"%d"\n\t{\n%s\t}\n' % (76561197960265728 + acct, ''.join('\t\t"%s"\t\t"%s"\n' % kv for kv in keys.items())) + for acct, keys in blocks) + (self.steam / 'config').mkdir(exist_ok=True) + (self.steam / 'config/loginusers.vdf').write_text('"users"\n{\n%s}\n' % body) + (self.steam / 'userdata/456/config').mkdir(parents=True, exist_ok=True) + + def test_the_account_marked_most_recent_is_used(self): + self.users((123, dict(MostRecent='1', AutoLogin='0', timestamp='100')), (456, dict(MostRecent='0', AutoLogin='1', timestamp='200'))) + self.assertEqual(str(self.steam / 'userdata/123'), shortcuts.account_dir(str(self.steam))) + + def test_without_most_recent_the_account_that_signs_in_is_used(self): + self.users((123, dict(AutoLogin='1', timestamp='100')), (456, dict(AutoLogin='0', timestamp='200'))) + self.assertEqual(str(self.steam / 'userdata/123'), shortcuts.account_dir(str(self.steam))) + self.assertIn(APPID, self.run_writer(self.game())) + + def test_without_either_mark_the_account_used_last_is_used(self): + self.users((456, dict(AutoLogin='0', timestamp='100')), (123, dict(AutoLogin='0', timestamp='200'))) + self.assertEqual(str(self.steam / 'userdata/123'), shortcuts.account_dir(str(self.steam))) + + def test_two_accounts_without_any_mark_write_nothing(self): + self.users((123, dict(AccountName='a')), (456, dict(AccountName='b'))) + self.assertIsNone(shortcuts.account_dir(str(self.steam))) + def test_an_older_record_without_launch_options_still_takes_the_apps(self): self.run_writer(self.game()) record = self.record()