From 439d9afe5ee097677049fc25537020468d39529a Mon Sep 17 00:00:00 2001 From: The412Banner <205237651+The412Banner@users.noreply.github.com> Date: Mon, 16 Nov 2026 19:07:47 +0000 Subject: [PATCH] fix(input): save Steam touch layouts when the controller config folder or configset is missing --- .../launcher/input/SteamTouchConfig.kt | 20 +++++-- .../input/SteamTouchConfigFilesTest.kt | 59 ++++++++++++++++++- 2 files changed, 71 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/input/SteamTouchConfig.kt b/app/src/main/java/com/droiddeck/launcher/input/SteamTouchConfig.kt index dc9465222e..3f6a872990 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/SteamTouchConfig.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/SteamTouchConfig.kt @@ -166,14 +166,21 @@ object SteamTouchConfig { fun steamRoot(context: Context) = com.droiddeck.launcher.session.SessionPrefs.activeSteamRoot(context) + private fun controllerConfigs(context: Context) = File(steamRoot(context), "steamapps/common/Steam Controller Configs") + fun configDir(context: Context): File? { - val base = File(steamRoot(context), "steamapps/common/Steam Controller Configs") + val base = controllerConfigs(context) val account = accountId(context) if (account != null) File(base, "$account/config").takeIf { it.isDirectory }?.let { return it } return base.listFiles()?.filter { it.name.all(Char::isDigit) && File(it, "config").isDirectory } ?.maxByOrNull { it.lastModified() }?.let { File(it, "config") } } + private fun writableConfigDir(context: Context): File? { + val account = accountId(context) ?: return configDir(context) + return File(controllerConfigs(context), "$account/config").apply { mkdirs() }.takeIf { it.isDirectory } + } + private fun accountId(context: Context): Long? = try { val users = KeyValues.parse(File(steamRoot(context), "config/loginusers.vdf").readText()) val node = users.child("users") @@ -389,8 +396,9 @@ object SteamTouchConfig { bindings: Map SteamTouchBindings.Binding> = emptyMap(), options: Options? = null, ): File? { - val dir = configDir(context) ?: return null - val base = config.text ?: if (config.file == null && bindings.isEmpty()) "\"controller_mappings\"\n{\n\t\"controller_type\"\t\t\"controller_mobile_touch\"\n}\n" else return null + val dir = writableConfigDir(context) ?: return null.also { Log.w(TAG, "steam touch: no controller config folder to save app $appId in") } + val base = config.text ?: if (config.file == null && bindings.isEmpty()) "\"controller_mappings\"\n{\n\t\"controller_type\"\t\t\"controller_mobile_touch\"\n}\n" + else return null.also { Log.w(TAG, "steam touch: ${config.file?.path ?: "default layout"} has no readable text to save into") } val source = bindings.entries.fold(base) { text, (ref, change) -> SteamTouchBindings.rewrite(text, ref, change) ?: text } @@ -408,11 +416,11 @@ object SteamTouchConfig { val existing = Regex("""\n\s*"touch_layout"\s+"[0-9a-fA-F]*"""") val text = if (existing.containsMatchIn(source)) source.replace(existing, "\n$line") else source.replaceFirst(Regex("""("controller_type"\s+"controller_mobile_touch"[^\n]*)"""), "$1\n$line") - if (!text.contains("touch_layout")) return null + if (!text.contains("touch_layout")) return null.also { Log.w(TAG, "steam touch: ${config.file?.path} is no controller_mobile_touch config") } target.parentFile?.mkdirs() val tmp = File(target.path + ".tmp") tmp.writeText(text) - if (!tmp.renameTo(target)) return null + if (!tmp.renameTo(target)) return null.also { Log.w(TAG, "steam touch: could not replace ${target.path}") } selectAutosave(dir, key) dropDeviceEntry(dir, key) Log.i(TAG, "steam touch: saved layouts ${sets.keys} for app $appId to ${target.path}") @@ -430,7 +438,7 @@ object SteamTouchConfig { private fun selectAutosave(dir: File, key: String) { val file = File(dir, CONFIGSET) - val text = if (file.isFile) file.readText() else "\"controller_config\"\n{\n}\n" + val text = file.takeIf { it.isFile }?.readText()?.takeIf { it.contains('}') } ?: "\"controller_config\"\n{\n}\n" val entry = "\t\"$key\"\n\t{\n\t\t\"autosave\"\t\t\"1\"\n\t}" val block = Regex("""\n\s*"${Regex.escape(key)}"\s*\{[^}]*\}""", RegexOption.IGNORE_CASE) val next = if (block.containsMatchIn(text)) text.replace(block) { "\n$entry" } diff --git a/app/src/test/java/com/droiddeck/launcher/input/SteamTouchConfigFilesTest.kt b/app/src/test/java/com/droiddeck/launcher/input/SteamTouchConfigFilesTest.kt index 845104bf51..67fe8f85e3 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/SteamTouchConfigFilesTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/SteamTouchConfigFilesTest.kt @@ -60,6 +60,62 @@ class SteamTouchConfigFilesTest { private val template get() = File(steam, "controller_base/templates/controller_mobile_touch_gamepad_joystick.vdf") + private fun login() = File(steam, "config/loginusers.vdf").apply { parentFile!!.mkdirs() } + .writeText("\"users\"\n{\n\t\"76561197960265851\"\n\t{\n\t\t\"MostRecent\"\t\t\"1\"\n\t}\n}\n") + + private fun saveDefault(app: Int): File? { + val elements = listOf(SteamTouchConfig.Element(SteamTouchConfig.A, true, 0.5f, 0.6f)) + return SteamTouchConfig.saveLayouts(context, app, SteamTouchConfig.load(context, app), mapOf(1 to elements)) + } + + private fun assertSavedA(app: Int, saved: File?) { + val loaded = SteamTouchConfig.load(context, app) + assertEquals(saved, loaded.file) + val a = SteamTouchConfig.elementsFor(loaded, 1, emptyList()).first { it.type == SteamTouchConfig.A } + assertEquals(0.5f, a.x, 0.002f) + assertEquals(0.6f, a.y, 0.002f) + } + + @Test fun savesWhenSteamHasNotCreatedTheAccountsControllerFolder() { + File(steam, "steamapps/common/Steam Controller Configs").deleteRecursively() + login() + val saved = saveDefault(413150) + assertEquals(File(dir, "413150/controller_mobile_touch.vdf"), saved) + assertSavedA(413150, saved) + } + + @Test fun savesIntoTheSignedInAccountNotAnotherAccountsFolder() { + dir.parentFile!!.deleteRecursively() + val other = File(steam, "steamapps/common/Steam Controller Configs/456/config").apply { mkdirs() } + login() + val saved = saveDefault(413150) + assertEquals(File(dir, "413150/controller_mobile_touch.vdf"), saved) + assertFalse(File(other, "413150").exists()) + assertSavedA(413150, saved) + } + + @Test fun withoutAnAccountOrControllerFolderNothingIsWritten() { + File(steam, "steamapps/common/Steam Controller Configs").deleteRecursively() + assertEquals(null, saveDefault(413150)) + assertFalse(File(steam, "steamapps/common/Steam Controller Configs").exists()) + } + + @Test fun anEmptyConfigsetIsRebuiltInsteadOfFailingTheSave() { + File(dir, "configset_controller_mobile_touch.vdf").writeText("") + val saved = saveDefault(413150) + assertEquals(File(dir, "413150/controller_mobile_touch.vdf"), saved) + assertTrue(File(dir, "configset_controller_mobile_touch.vdf").readText().contains("\"413150\"")) + assertSavedA(413150, saved) + } + + @Test fun anExistingConfigsetKeepsItsOtherEntries() { + configset("configset_controller_mobile_touch.vdf", "8400" to "\"workshop\"\t\t\"7\"") + saveDefault(413150) + val text = File(dir, "configset_controller_mobile_touch.vdf").readText() + assertTrue(text.contains("\"8400\"")) + assertTrue(text.contains("\"413150\"")) + } + private fun shortcut(name: String): Int { val shortcut = 2702387189L.toInt() template.apply { parentFile!!.mkdirs(); writeText(touch("Gamepad with Joystick")) } @@ -67,8 +123,7 @@ class SteamTouchConfigFilesTest { "!! Controller 0 attributes:\n Type: 43\n" + "Loaded Config for Last Resort Path for App ID 2702387189, Controller 0: /root/.local/share/Steam//controller_base/templates/controller_mobile_touch_gamepad_joystick.vdf\n", ) - File(steam, "config/loginusers.vdf").apply { parentFile!!.mkdirs() } - .writeText("\"users\"\n{\n\t\"76561197960265851\"\n\t{\n\t\t\"MostRecent\"\t\t\"1\"\n\t}\n}\n") + login() val vdf = java.io.ByteArrayOutputStream().apply { write("\u0000shortcuts\u0000\u00000\u0000\u0002appid\u0000".toByteArray()) write(java.nio.ByteBuffer.allocate(4).order(java.nio.ByteOrder.LITTLE_ENDIAN).putInt(shortcut).array())