diff --git a/app/src/main/java/com/steamdeck/launcher/MainActivity.kt b/app/src/main/java/com/steamdeck/launcher/MainActivity.kt index 2283dcc096..3f894964bb 100644 --- a/app/src/main/java/com/steamdeck/launcher/MainActivity.kt +++ b/app/src/main/java/com/steamdeck/launcher/MainActivity.kt @@ -22,6 +22,7 @@ import com.steamdeck.launcher.runtime.LinuxRuntimeInstaller import com.steamdeck.launcher.session.SessionService import com.steamdeck.launcher.ui.DesktopAppsDialog import com.steamdeck.launcher.ui.PackageRow +import com.steamdeck.launcher.session.OfflineMode import com.steamdeck.launcher.session.ProtonExtras import com.steamdeck.launcher.ui.ProtonDialog import com.steamdeck.launcher.ui.ProtonRow @@ -59,6 +60,8 @@ class MainActivity : ComponentActivity() { private var pkgStage by mutableStateOf(null) private var pkgPercent by mutableIntStateOf(-1) private var desktopInstalled by mutableStateOf(false) + private var offlineAccount by mutableStateOf(null) + private var offline by mutableStateOf(false) private var protonRows by mutableStateOf>(emptyList()) override fun onCreate(savedInstanceState: Bundle?) { @@ -73,6 +76,7 @@ class MainActivity : ComponentActivity() { busy = busy, stage = stage, percent = percent, failed = failed, frameGenLabel = frameGenLabel, desktopInstalled = desktopInstalled, + offlineAccount = offlineAccount, offline = offline, ), onPlay = { startActivity(Intent(this, SessionActivity::class.java)) }, onDesktop = { @@ -80,6 +84,10 @@ class MainActivity : ComponentActivity() { .putExtra(SessionService.EXTRA_MODE, SessionService.MODE_DESKTOP)) }, onApps = { openApps() }, + onOffline = { + OfflineMode.setEnabled(this, !OfflineMode.enabled(this)) + offline = OfflineMode.enabled(this) + }, onRuntime = { onRuntimeButton() }, onFrameGen = { showFrameGen = true }, onProtons = { refreshProtons(); showProtons = true }, @@ -172,6 +180,8 @@ class MainActivity : ComponentActivity() { private fun refresh() { desktopInstalled = DesktopCatalog.desktopInstalled(this) + offlineAccount = OfflineMode.account(this) + offline = OfflineMode.enabled(this) installed = LinuxRuntimeInstaller.installedVersion(this) ready = LinuxRuntime.isInstalled(this) frameGenLabel = FrameGen.label(this) diff --git a/app/src/main/java/com/steamdeck/launcher/session/OfflineMode.kt b/app/src/main/java/com/steamdeck/launcher/session/OfflineMode.kt new file mode 100644 index 0000000000..2e3e2914b6 --- /dev/null +++ b/app/src/main/java/com/steamdeck/launcher/session/OfflineMode.kt @@ -0,0 +1,109 @@ +package com.steamdeck.launcher.session + +import android.content.Context +import android.util.Log +import com.steamdeck.launcher.runtime.LinuxRuntime +import java.io.File + +/** + * Starting the Steam client signed out of Valve's servers. + * + * The client can run without a connection — installed games launch, Proton and the frame-gen are + * all local — but only if it has credentials from an earlier successful sign-in. Two keys in + * loginusers.vdf decide what it does when it cannot reach a CM: WantsOfflineMode makes it go + * offline on its own, SkipOfflineModeWarning stops it asking first. Both are read once, while the + * client starts, which is why this belongs to the main screen and not to the in-session drawer: + * changing it during a session would write the file and change nothing until the next launch. + * + * The client rewrites this file when it exits, so the request is kept as our own preference and + * written into the file again at every session start. + */ +object OfflineMode { + private const val TAG = "OfflineMode" + private const val KEY = "steam_offline" + private val WANTS = Regex("""^(\s*"WantsOfflineMode"\s*")([01])(".*)$""") + private val SKIP = Regex("""^(\s*"SkipOfflineModeWarning"\s*")([01])(".*)$""") + private val REMEMBER = Regex(""""RememberPassword"\s*"1"""") + private val PERSONA = Regex(""""PersonaName"\s*"([^"]*)"""") + + private fun prefs(context: Context) = context.getSharedPreferences("session", Context.MODE_PRIVATE) + + private fun loginUsers(root: File) = File(root, "root/.local/share/Steam/config/loginusers.vdf") + + /** Whether the client has an account it could sign in as offline. */ + fun account(context: Context): String? { + val file = loginUsers(LinuxRuntime.rootDir(context)) + val text = try { + if (file.isFile) file.readText() else return null + } catch (e: Exception) { + Log.w(TAG, "could not read loginusers.vdf", e); return null + } + if (!REMEMBER.containsMatchIn(text)) return null + return PERSONA.find(text)?.groupValues?.get(1)?.takeIf { it.isNotBlank() } ?: "signed in" + } + + fun enabled(context: Context): Boolean = prefs(context).getBoolean(KEY, false) + + fun setEnabled(context: Context, on: Boolean) { + prefs(context).edit().putBoolean(KEY, on).apply() + apply(context, LinuxRuntime.rootDir(context)) + } + + /** + * Write the request into loginusers.vdf. Every account block gets the same answer: the client + * signs in as one of them and we have no say in which. Keys the file does not carry are added + * beside the account's name; a file without an account block is left alone, since there is + * nothing to be offline as. + */ + fun apply(context: Context, root: File) { + val file = loginUsers(root) + if (!file.isFile) return + val want = if (enabled(context)) "1" else "0" + try { + val lines = file.readLines() + val out = ArrayList(lines.size + 4) + var sawWants = false + var sawSkip = false + var changed = false + for (line in lines) { + val wants = WANTS.matchEntire(line) + val skip = SKIP.matchEntire(line) + when { + wants != null -> { + sawWants = true + if (wants.groupValues[2] != want) changed = true + out.add(wants.groupValues[1] + want + wants.groupValues[3]) + } + skip != null -> { + sawSkip = true + if (skip.groupValues[2] != want) changed = true + out.add(skip.groupValues[1] + want + skip.groupValues[3]) + } + else -> { + out.add(line) + if (line.contains("\"AccountName\"")) { + val indent = line.takeWhile { it == ' ' || it == '\t' } + if (!lines.any { WANTS.matchEntire(it) != null }) { + out.add("$indent\"WantsOfflineMode\"\t\t\"$want\""); changed = true + } + if (!lines.any { SKIP.matchEntire(it) != null }) { + out.add("$indent\"SkipOfflineModeWarning\"\t\t\"$want\""); changed = true + } + } + } + } + } + if (!changed) return + val staged = File(file.parentFile, "loginusers.vdf.staged") + staged.writeText(out.joinToString("\n", postfix = "\n")) + if (!staged.renameTo(file)) { + staged.delete() + Log.w(TAG, "could not replace loginusers.vdf") + return + } + Log.i(TAG, "offline=$want (wants=$sawWants skip=$sawSkip)") + } catch (e: Exception) { + Log.w(TAG, "could not write loginusers.vdf", e) + } + } +} diff --git a/app/src/main/java/com/steamdeck/launcher/session/SessionService.kt b/app/src/main/java/com/steamdeck/launcher/session/SessionService.kt index b612d98a74..dec7d6c892 100644 --- a/app/src/main/java/com/steamdeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/steamdeck/launcher/session/SessionService.kt @@ -173,6 +173,10 @@ class SessionService : Service() { val prootLibs = LinuxRuntime.prootLibraryPath(this) if (prootLibs.isNotEmpty()) hostEnv.put("LD_LIBRARY_PATH", prootLibs) + // Whether the client signs in to Valve or starts offline: read once, while it starts, and + // rewritten by the client when it exits, so it is set again here at every session start. + if (SessionState.mode == MODE_STEAM) OfflineMode.apply(this, root) + val networkLink = LinuxNetworkLinkComponent(this, root) networkLink.setContext(this) networkLink.publish() diff --git a/app/src/main/java/com/steamdeck/launcher/ui/MainScreen.kt b/app/src/main/java/com/steamdeck/launcher/ui/MainScreen.kt index 6e1e2f5269..f460ab8946 100644 --- a/app/src/main/java/com/steamdeck/launcher/ui/MainScreen.kt +++ b/app/src/main/java/com/steamdeck/launcher/ui/MainScreen.kt @@ -43,6 +43,9 @@ class MainUiState( val failed: Boolean, val frameGenLabel: String, val desktopInstalled: Boolean, + /** The account the client would sign in as offline, or null if it has never signed in. */ + val offlineAccount: String?, + val offline: Boolean, ) /** @@ -63,6 +66,7 @@ fun MainScreen( onRuntime: () -> Unit, onFrameGen: () -> Unit, onProtons: () -> Unit, + onOffline: () -> Unit, onCredits: () -> Unit, ) { BoxWithConstraints( @@ -136,6 +140,21 @@ fun MainScreen( onClick = onDesktop, enabled = state.ready && !state.busy && state.desktopInstalled, modifier = Modifier.fillMaxWidth().padding(top = 5.dp * k).height(buttonHeight + 5.dp * k), ) { Text(if (state.desktopInstalled) "Desktop" else "Desktop — install it under Desktop & apps", fontSize = 13.sp * k) } + // Offline is read while the client starts, so it is decided here rather than in the + // session's drawer, and it needs credentials from an earlier sign-in to be possible. + OutlinedButton( + onClick = onOffline, enabled = state.offlineAccount != null, + modifier = Modifier.fillMaxWidth().padding(top = 5.dp * k).height(buttonHeight + 5.dp * k), + ) { + Text( + when { + state.offlineAccount == null -> "Start offline — sign in once first" + state.offline -> "Start offline: on · ${state.offlineAccount}" + else -> "Start offline: off" + }, + fontSize = 13.sp * k, + ) + } val runtimeLabel = when { state.busy -> "Working…" !state.ready -> "Install Linux runtime"