mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +00:00
feat(session): seed Steam defaults for battery percent, byte download rates and recent sort (#2599)
This commit is contained in:
1 parent
f502b8b3e6
commit
e816d79581
2 files changed
+154
-1
No files matched your search
@@ -355,7 +355,10 @@ object SessionFiles {
|
||||
"stage session=$startsSession pending=$force choice=${SessionPrefs.bootMovie(context)} movie=${startupMovie?.path} " +
|
||||
"staged=$bigPictureMovieInstalled done=$done steam=${home.path} config: $before -> ${startupMoviePath(config)}",
|
||||
)
|
||||
if (startsSession) disableIdleSleep(home)
|
||||
if (startsSession) {
|
||||
disableIdleSleep(home)
|
||||
seedUserDefaults(home)
|
||||
}
|
||||
if (!done) allDone = false
|
||||
if (!bigPictureMovieInstalled) allInstalled = false
|
||||
if (done && home.path.contains(".droiddeck-x64") == activeIsX64) seen = if (bigPictureMovieInstalled) BOOT_MOVIE_PATH else ""
|
||||
@@ -575,6 +578,85 @@ object SessionFiles {
|
||||
}
|
||||
}
|
||||
|
||||
private const val USER_DEFAULTS_MARKER = "droiddeck-user-defaults-initialized"
|
||||
private const val BATTERY_PREFERENCES = "CachedBatteryPreferences"
|
||||
private const val UI_STATE = "UIStoreLocalSteamUIState"
|
||||
private const val SORT_RECENT_OFF = "\\\"bSortedByRecent\\\":false"
|
||||
private const val SORT_RECENT_ON = "\\\"bSortedByRecent\\\":true"
|
||||
private const val BATTERY_PERCENT_ON = "{\\\"bShowBatteryPercentage\\\":true}"
|
||||
|
||||
/**
|
||||
* Seeds opinionated first-run defaults into each Steam account's localconfig.vdf: battery percentage shown and
|
||||
* download speeds in bytes rather than bits, and the library sorted by recently played. A value is written once per account and only when the key is
|
||||
* absent, so a later choice made in Steam is never overridden. Runs while Steam is stopped, as it keeps the
|
||||
* file in memory and rewrites it.
|
||||
*/
|
||||
internal fun seedUserDefaults(steamHome: File) {
|
||||
val users = File(steamHome, "userdata").listFiles { f -> f.isDirectory && f.name.all(Char::isDigit) && f.name != "0" } ?: return
|
||||
for (user in users) {
|
||||
val configDir = File(user, "config")
|
||||
val marker = File(configDir, USER_DEFAULTS_MARKER)
|
||||
val file = File(configDir, "localconfig.vdf")
|
||||
if (marker.exists() || !file.isFile) continue
|
||||
try {
|
||||
val text = file.readText()
|
||||
val updated = withUserDefaults(text) ?: continue
|
||||
if (updated != text) {
|
||||
val staged = File(configDir, file.name + ".staged")
|
||||
staged.writeText(updated)
|
||||
if (!staged.renameTo(file)) { staged.delete(); continue }
|
||||
}
|
||||
marker.writeText("1\n")
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "could not seed Steam user defaults", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal fun withUserDefaults(text: String): String? {
|
||||
val newline = if (text.contains("\r\n")) "\r\n" else "\n"
|
||||
val root = findObject(text, "UserLocalConfigStore", 0, text.length) ?: return null
|
||||
var out = text
|
||||
val system = findObject(out, "system", root.open + 1, root.close)
|
||||
if (system == null) {
|
||||
out = insertInto(out, root, listOf("\"system\"", "{", "\t\"displayratesasbits\"\t\t\"0\"", "}"), newline)
|
||||
} else if (!hasKey(out.substring(system.keyStart, system.close + 1), "displayratesasbits")) {
|
||||
val block = out.substring(system.keyStart, system.close + 1)
|
||||
out = out.replaceRange(system.keyStart, system.close + 1, setScalar(block, "displayratesasbits", "0", newline))
|
||||
}
|
||||
val reroot = findObject(out, "UserLocalConfigStore", 0, out.length) ?: return out
|
||||
val web = findObject(out, "WebStorage", reroot.open + 1, reroot.close)
|
||||
if (web == null) {
|
||||
out = insertInto(out, reroot, listOf("\"WebStorage\"", "{", "\t\"$BATTERY_PREFERENCES\"\t\t\"$BATTERY_PERCENT_ON\"", "}"), newline)
|
||||
} else if (!hasKey(out.substring(web.keyStart, web.close + 1), BATTERY_PREFERENCES)) {
|
||||
val block = out.substring(web.keyStart, web.close + 1)
|
||||
out = out.replaceRange(web.keyStart, web.close + 1, setScalar(block, BATTERY_PREFERENCES, BATTERY_PERCENT_ON, newline))
|
||||
}
|
||||
return seedRecentSort(out, newline)
|
||||
}
|
||||
|
||||
// The library sort lives inside a JSON string; Steam writes bSortedByRecent:false until the player picks a sort,
|
||||
// so the first seed flips it once and the marker file keeps a later choice.
|
||||
private fun seedRecentSort(text: String, newline: String): String {
|
||||
val root = findObject(text, "UserLocalConfigStore", 0, text.length) ?: return text
|
||||
val web = findObject(text, "WebStorage", root.open + 1, root.close) ?: return text
|
||||
val block = text.substring(web.keyStart, web.close + 1)
|
||||
if (!hasKey(block, UI_STATE)) {
|
||||
return text.replaceRange(web.keyStart, web.close + 1,
|
||||
setScalar(block, UI_STATE, "{\\\"nVersion\\\":1,$SORT_RECENT_ON}", newline))
|
||||
}
|
||||
return text.replaceRange(web.keyStart, web.close + 1, block.replace(SORT_RECENT_OFF, SORT_RECENT_ON))
|
||||
}
|
||||
|
||||
private fun hasKey(block: String, key: String) = Regex("(?m)^[\\t ]*\"${Regex.escape(key)}\"[\\t ]").containsMatchIn(block)
|
||||
|
||||
private fun insertInto(text: String, parent: VdfBlock, lines: List<String>, newline: String): String {
|
||||
val lineStart = text.lastIndexOf('\n', parent.close).let { if (it < 0) 0 else it + 1 }
|
||||
val indent = text.substring(lineStart, parent.close).takeWhile { it == '\t' || it == ' ' } + "\t"
|
||||
val section = lines.joinToString(newline) { indent + it } + newline
|
||||
return text.substring(0, lineStart) + section + text.substring(lineStart)
|
||||
}
|
||||
|
||||
private val IDLE_SLEEP_TIMERS = listOf("IdleSuspendBatterySeconds", "IdleSuspendACSeconds")
|
||||
|
||||
internal fun disableIdleSleep(steamHome: File): Boolean {
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.droiddeck.launcher.session
|
||||
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.rules.TemporaryFolder
|
||||
import java.io.File
|
||||
|
||||
class SteamUserDefaultsTest {
|
||||
@get:Rule val temp = TemporaryFolder()
|
||||
|
||||
private val battery = "\"CachedBatteryPreferences\"\t\t\"{\\\"bShowBatteryPercentage\\\":true}\""
|
||||
|
||||
private fun home(body: String): File = temp.newFolder().also {
|
||||
File(it, "userdata/123/config").mkdirs()
|
||||
File(it, "userdata/123/config/localconfig.vdf").writeText("\"UserLocalConfigStore\"\n{\n$body}\n")
|
||||
}
|
||||
|
||||
private fun text(home: File) = File(home, "userdata/123/config/localconfig.vdf").readText()
|
||||
private fun bits(text: String) = Regex("\"displayratesasbits\"\\s+\"([^\"]*)\"").find(text)?.groupValues?.get(1)
|
||||
|
||||
@Test fun freshAccountGetsBothDefaults() {
|
||||
val home = home("")
|
||||
SessionFiles.seedUserDefaults(home)
|
||||
assertEquals("0", bits(text(home)))
|
||||
assertTrue(text(home).contains(battery))
|
||||
assertTrue(File(home, "userdata/123/config/droiddeck-user-defaults-initialized").isFile)
|
||||
}
|
||||
|
||||
@Test fun existingBlocksGainTheKeysBesideOtherSettings() {
|
||||
val home = home("\t\"system\"\n\t{\n\t\t\"PushToTalkKey\"\t\t\"0\"\n\t}\n\t\"WebStorage\"\n\t{\n\t\t\"Other\"\t\t\"{\\\"a\\\":1}\"\n\t}\n")
|
||||
SessionFiles.seedUserDefaults(home)
|
||||
val t = text(home)
|
||||
assertEquals("0", bits(t))
|
||||
assertTrue(t.contains("\"PushToTalkKey\"\t\t\"0\""))
|
||||
assertTrue(t.contains("\"Other\"\t\t\"{\\\"a\\\":1}\""))
|
||||
assertTrue(t.contains(battery))
|
||||
}
|
||||
|
||||
@Test fun aChoiceAlreadyMadeIsKept() {
|
||||
val home = home("\t\"system\"\n\t{\n\t\t\"displayratesasbits\"\t\t\"1\"\n\t}\n\t\"WebStorage\"\n\t{\n\t\t\"CachedBatteryPreferences\"\t\t\"{\\\"bShowBatteryPercentage\\\":false}\"\n\t}\n")
|
||||
SessionFiles.seedUserDefaults(home)
|
||||
assertEquals("1", bits(text(home)))
|
||||
assertTrue(text(home).contains("false"))
|
||||
}
|
||||
|
||||
@Test fun laterChangesAreNotReseeded() {
|
||||
val home = home("")
|
||||
SessionFiles.seedUserDefaults(home)
|
||||
File(home, "userdata/123/config/localconfig.vdf").writeText(text(home).replace("\"displayratesasbits\"\t\t\"0\"", "\"displayratesasbits\"\t\t\"1\""))
|
||||
SessionFiles.seedUserDefaults(home)
|
||||
assertEquals("1", bits(text(home)))
|
||||
}
|
||||
|
||||
@Test fun missingLocalConfigIsRetriedLater() {
|
||||
val home = temp.newFolder()
|
||||
File(home, "userdata/123/config").mkdirs()
|
||||
SessionFiles.seedUserDefaults(home)
|
||||
assertFalse(File(home, "userdata/123/config/droiddeck-user-defaults-initialized").exists())
|
||||
}
|
||||
|
||||
@Test fun librarySortsByRecentlyPlayed() {
|
||||
val fresh = home("")
|
||||
SessionFiles.seedUserDefaults(fresh)
|
||||
assertTrue(text(fresh).contains("\\\"bSortedByRecent\\\":true"))
|
||||
val existing = home("\t\"WebStorage\"\n\t{\n\t\t\"UIStoreLocalSteamUIState\"\t\t\"{\\\"nVersion\\\":1,\\\"bSortedByRecent\\\":false,\\\"eGameListView\\\":1}\"\n\t}\n")
|
||||
SessionFiles.seedUserDefaults(existing)
|
||||
assertTrue(text(existing).contains("\\\"bSortedByRecent\\\":true,\\\"eGameListView\\\":1"))
|
||||
assertEquals(1, Regex("UIStoreLocalSteamUIState").findAll(text(existing)).count())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user