fix(input): close a truncated touch configset before adding the autosave entry

This commit is contained in:
The412Banner committed 2026-11-20 04:22:42 +00:00
1 parent 439d9afe5e
commit 02e7eff284
2 files changed
+17 -1

No files matched your search

@@ -438,7 +438,7 @@ object SteamTouchConfig {
private fun selectAutosave(dir: File, key: String) {
val file = File(dir, CONFIGSET)
val text = file.takeIf { it.isFile }?.readText()?.takeIf { it.contains('}') } ?: "\"controller_config\"\n{\n}\n"
val text = file.takeIf { it.isFile }?.readText()?.takeIf { it.contains('{') }?.let(::closeBraces) ?: "\"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" }
@@ -446,6 +446,11 @@ object SteamTouchConfig {
File(file.path + ".tmp").apply { writeText(next) }.renameTo(file)
}
private fun closeBraces(text: String): String {
val open = text.count { it == '{' } - text.count { it == '}' }
return if (open <= 0) text else text.trimEnd() + "\n}".repeat(open) + "\n"
}
fun decodeLayouts(bytes: ByteArray): Layouts {
val layouts = mutableListOf<Layout>()
@@ -116,6 +116,17 @@ class SteamTouchConfigFilesTest {
assertTrue(text.contains("\"413150\""))
}
@Test fun aConfigsetMissingItsRootBraceGetsTheEntryAtTheRoot() {
File(dir, "configset_controller_mobile_touch.vdf")
.writeText("\"controller_config\"\n{\n\t\"8400\"\n\t{\n\t\t\"workshop\"\t\t\"7\"\n\t}\n")
val saved = saveDefault(413150)
val root = KeyValues.parse(File(dir, "configset_controller_mobile_touch.vdf").readText()).child("controller_config")
assertEquals("1", root?.child("413150")?.string("autosave"))
assertEquals("7", root?.child("8400")?.string("workshop"))
assertEquals(null, root?.child("8400")?.child("413150"))
assertSavedA(413150, saved)
}
private fun shortcut(name: String): Int {
val shortcut = 2702387189L.toInt()
template.apply { parentFile!!.mkdirs(); writeText(touch("Gamepad with Joystick")) }