mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-23 06:46:02 +00:00
Add per-game Steam Input overrides (#2407)
* feat: add per-game Steam Input override * fix(steam-input): keep overrides in Steam's root apps section and keep the panel in step with the session * fix(steam-input): reload the override after every session
This commit is contained in:
1 parent
259d5d184b
commit
946e9c41e0
6 files changed
+254
-15
No files matched your search
@@ -0,0 +1,39 @@
|
||||
package com.droiddeck.launcher.frontend
|
||||
|
||||
import android.content.Context
|
||||
import com.droiddeck.launcher.session.SessionPrefs
|
||||
import com.droiddeck.launcher.session.SessionState
|
||||
|
||||
object SteamInputOverride {
|
||||
private const val CONFIG_KEY = "UseSteamControllerConfig"
|
||||
private const val DEFAULT_VALUE = "1"
|
||||
|
||||
enum class Mode(val value: String?) {
|
||||
DEFAULT(null), ENABLED("2"), DISABLED("0");
|
||||
|
||||
companion object {
|
||||
fun from(value: String): Mode? = when (value) {
|
||||
DEFAULT_VALUE -> DEFAULT
|
||||
"2" -> ENABLED
|
||||
"0" -> DISABLED
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum class Saved { YES, NO, RUNNING }
|
||||
|
||||
fun current(context: Context, appId: Int): Mode? =
|
||||
SteamLaunchOptions.steamAppSetting(
|
||||
SessionPrefs.activeSteamRoot(context), appId, CONFIG_KEY, DEFAULT_VALUE,
|
||||
)?.let(Mode::from)
|
||||
|
||||
fun set(context: Context, appId: Int, mode: Mode, shown: Mode): Saved {
|
||||
if (SessionState.running) return Saved.RUNNING
|
||||
val saved = SteamLaunchOptions.writeSteamAppSetting(
|
||||
SessionPrefs.activeSteamRoot(context), appId, CONFIG_KEY,
|
||||
shown.value ?: DEFAULT_VALUE, mode.value, DEFAULT_VALUE,
|
||||
)
|
||||
return if (saved) Saved.YES else Saved.NO
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,8 @@ import java.nio.file.StandardCopyOption
|
||||
object SteamLaunchOptions {
|
||||
private const val TAG = "SteamLaunchOptions"
|
||||
private val STEAM = listOf("UserLocalConfigStore", "Software", "Valve", "Steam")
|
||||
// Steam keeps per-game settings such as UseSteamControllerConfig in the root apps section, and launch options under Software/Valve/Steam.
|
||||
private val ROOT = listOf("UserLocalConfigStore")
|
||||
|
||||
fun current(context: Context, appId: Int): String? =
|
||||
SteamLiveShortcuts.launchOptions(context, appId.toLong()) ?: steam(SessionPrefs.activeSteamRoot(context), appId)
|
||||
@@ -36,13 +38,38 @@ object SteamLaunchOptions {
|
||||
val config = AddedGames.accountConfig(steamHome) ?: return null
|
||||
val text = runCatching { File(config, "localconfig.vdf").readText() }.getOrNull() ?: return null
|
||||
if (section(text, STEAM) == null) return null
|
||||
return launchOptions(text, appId).orEmpty()
|
||||
return appValue(text, appId, "LaunchOptions").orEmpty()
|
||||
}
|
||||
|
||||
internal fun write(steamHome: File, appId: Int, options: String): Boolean {
|
||||
val config = AddedGames.accountConfig(steamHome)?.let { File(it, "localconfig.vdf") } ?: return false
|
||||
val text = runCatching { config.readText() }.getOrNull() ?: return false
|
||||
val updated = withLaunchOptions(text, appId, options) ?: return false
|
||||
return writeConfig(config, text, updated)
|
||||
}
|
||||
|
||||
internal fun steamAppSetting(steamHome: File, appId: Int, key: String, default: String): String? {
|
||||
val config = AddedGames.accountConfig(steamHome) ?: return null
|
||||
val text = runCatching { File(config, "localconfig.vdf").readText() }.getOrNull() ?: return null
|
||||
if (section(text, ROOT) == null) return null
|
||||
return settingValue(text, appId, key) ?: default
|
||||
}
|
||||
|
||||
private fun settingValue(text: String, appId: Int, key: String): String? =
|
||||
appValue(text, appId, key, ROOT) ?: appValue(text, appId, key, STEAM)
|
||||
|
||||
internal fun writeSteamAppSetting(
|
||||
steamHome: File, appId: Int, key: String, expected: String, value: String?, default: String,
|
||||
): Boolean {
|
||||
val config = AddedGames.accountConfig(steamHome)?.let { File(it, "localconfig.vdf") } ?: return false
|
||||
val text = runCatching { config.readText() }.getOrNull() ?: return false
|
||||
if (section(text, ROOT) == null || (settingValue(text, appId, key) ?: default) != expected) return false
|
||||
val updated = (if (value == null) withoutAppValue(text, appId, key, ROOT) else withAppValue(text, appId, key, value, ROOT))
|
||||
?.let { withoutAppValue(it, appId, key, STEAM) } ?: return false
|
||||
return writeConfig(config, text, updated)
|
||||
}
|
||||
|
||||
private fun writeConfig(config: File, text: String, updated: String): Boolean {
|
||||
if (updated == text) return true
|
||||
val staged = File(config.parentFile, config.name + ".staged")
|
||||
return try {
|
||||
@@ -55,36 +82,57 @@ object SteamLaunchOptions {
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
staged.delete()
|
||||
Log.w(TAG, "could not write launch options to ${config.path}", e)
|
||||
Log.w(TAG, "could not write Steam app settings to ${config.path}", e)
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
internal fun launchOptions(text: String, appId: Int): String? {
|
||||
val app = section(text, STEAM + "apps" + appId.toString()) ?: return null
|
||||
return entries(text, app.valueStart, app.end).firstOrNull { !it.section && it.key.equals("LaunchOptions", true) }
|
||||
return appValue(text, appId, "LaunchOptions")
|
||||
}
|
||||
|
||||
private fun appValue(text: String, appId: Int, key: String, base: List<String> = STEAM): String? {
|
||||
val app = section(text, base + "apps" + appId.toString()) ?: return null
|
||||
return entries(text, app.valueStart, app.end).firstOrNull { !it.section && it.key.equals(key, true) }
|
||||
?.let { unescape(text.substring(it.valueStart + 1, it.end)) }
|
||||
}
|
||||
|
||||
internal fun withLaunchOptions(text: String, appId: Int, options: String): String? {
|
||||
internal fun withLaunchOptions(text: String, appId: Int, options: String): String? =
|
||||
withAppValue(text, appId, "LaunchOptions", options)
|
||||
|
||||
private fun withAppValue(text: String, appId: Int, key: String, rawValue: String, base: List<String> = STEAM): String? {
|
||||
val newline = if (text.contains("\r\n")) "\r\n" else "\n"
|
||||
var updated = text
|
||||
var path = STEAM
|
||||
for (key in listOf("apps", appId.toString())) {
|
||||
updated = withSection(updated, section(updated, path) ?: return null, key, newline) ?: return null
|
||||
path = path + key
|
||||
var path = base
|
||||
for (pathPart in listOf("apps", appId.toString())) {
|
||||
updated = withSection(updated, section(updated, path) ?: return null, pathPart, newline) ?: return null
|
||||
path = path + pathPart
|
||||
}
|
||||
val app = section(updated, path) ?: return null
|
||||
val value = "\"" + escape(options) + "\""
|
||||
entries(updated, app.valueStart, app.end).firstOrNull { !it.section && it.key.equals("LaunchOptions", true) }?.let {
|
||||
val value = "\"" + escape(rawValue) + "\""
|
||||
entries(updated, app.valueStart, app.end).firstOrNull { !it.section && it.key.equals(key, true) }?.let {
|
||||
return updated.replaceRange(it.valueStart, it.end + 1, value)
|
||||
}
|
||||
val lineStart = closingLine(updated, app) ?: return null
|
||||
val indent = updated.substring(lineStart, app.end) + "\t"
|
||||
return updated.substring(0, lineStart) + indent + "\"LaunchOptions\"\t\t" + value + newline + updated.substring(lineStart)
|
||||
return updated.substring(0, lineStart) + indent + "\"${escape(key)}\"\t\t" + value + newline + updated.substring(lineStart)
|
||||
}
|
||||
|
||||
private class Entry(val key: String, val valueStart: Int, val end: Int, val section: Boolean)
|
||||
private fun withoutAppValue(text: String, appId: Int, key: String, base: List<String> = STEAM): String {
|
||||
val app = section(text, base + "apps" + appId.toString()) ?: return text
|
||||
val entry = entries(text, app.valueStart, app.end).firstOrNull { !it.section && it.key.equals(key, true) } ?: return text
|
||||
val lineStart = text.lastIndexOf('\n', entry.start - 1) + 1
|
||||
val lineEnd = text.indexOf('\n', entry.end + 1).let { if (it < 0) text.length else it }
|
||||
val onlyEntryOnLine = text.substring(lineStart, entry.start).all(Char::isWhitespace) &&
|
||||
text.substring(entry.end + 1, lineEnd).all(Char::isWhitespace)
|
||||
return if (onlyEntryOnLine) {
|
||||
text.removeRange(lineStart, if (lineEnd < text.length) lineEnd + 1 else lineEnd)
|
||||
} else {
|
||||
text.removeRange(entry.start, entry.end + 1)
|
||||
}
|
||||
}
|
||||
|
||||
private class Entry(val key: String, val start: Int, val valueStart: Int, val end: Int, val section: Boolean)
|
||||
|
||||
private fun section(text: String, path: List<String>): Entry? {
|
||||
var found: Entry? = null
|
||||
@@ -98,18 +146,27 @@ object SteamLaunchOptions {
|
||||
private fun entries(text: String, open: Int, close: Int): List<Entry> {
|
||||
val found = ArrayList<Entry>()
|
||||
var key: String? = null
|
||||
var keyStart = -1
|
||||
var i = open + 1
|
||||
while (i < close) {
|
||||
when {
|
||||
text[i] == '"' -> {
|
||||
val end = stringEnd(text, i) ?: return found
|
||||
if (key == null) key = unescape(text.substring(i + 1, end)) else { found += Entry(key, i, end, false); key = null }
|
||||
if (key == null) {
|
||||
key = unescape(text.substring(i + 1, end))
|
||||
keyStart = i
|
||||
} else {
|
||||
found += Entry(key, keyStart, i, end, false)
|
||||
key = null
|
||||
keyStart = -1
|
||||
}
|
||||
i = end + 1
|
||||
}
|
||||
text[i] == '{' -> {
|
||||
val end = sectionEnd(text, i) ?: return found
|
||||
key?.let { found += Entry(it, i, end, true) }
|
||||
key?.let { found += Entry(it, keyStart, i, end, true) }
|
||||
key = null
|
||||
keyStart = -1
|
||||
i = end + 1
|
||||
}
|
||||
text.startsWith("//", i) -> i = text.indexOf('\n', i).let { if (it < 0) close else it }
|
||||
|
||||
Reference in new issue
Block a user