fix(input): rewrite the touch configset from its parsed tree when selecting the saved layout

This commit is contained in:
The412Banner committed 2026-11-20 07:27:39 +00:00
1 parent b67a69542c
commit e9877e27f7
2 files changed
+32 -12

No files matched your search

@@ -438,17 +438,13 @@ object SteamTouchConfig {
private fun selectAutosave(dir: File, key: String) {
val file = File(dir, CONFIGSET)
val text = file.takeIf { it.isFile }?.readText()?.takeIf { it.contains('{') }?.let(::closeBraces)?.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" }
else text.substring(0, text.lastIndexOf('}')).trimEnd() + "\n$entry\n}\n"
File(file.path + ".tmp").apply { writeText(next) }.renameTo(file)
}
private fun closeBraces(text: String): String {
val open = KeyValues.unclosedBraces(text)
return if (open <= 0) text else text.trimEnd() + "\n}".repeat(open) + "\n"
val root = file.takeIf { it.isFile }?.readText()?.let(KeyValues::parse)?.takeIf { it.child("controller_config") != null }
?: KeyValues.Node(mutableListOf("controller_config" to KeyValues.Node()))
val config = root.child("controller_config") ?: return
val entry = key to KeyValues.Node(mutableListOf("autosave" to "1"))
val at = config.entries.indexOfFirst { it.first.equals(key, true) && it.second is KeyValues.Node }
if (at >= 0) config.entries[at] = entry else config.entries += entry
File(file.path + ".tmp").apply { writeText(KeyValues.write(root)) }.renameTo(file)
}
@@ -612,7 +608,14 @@ class KeyValues private constructor() {
}
companion object {
fun unclosedBraces(text: String): Int = tokenize(text).let { tokens -> tokens.count { it.opens } - tokens.count { it.closes } }
fun write(node: Node): String = write(node, "")
private fun write(node: Node, indent: String): String = node.entries.joinToString("") { (key, value) ->
if (value is Node) "$indent\"${escape(key)}\"\n$indent{\n${write(value, "$indent\t")}$indent}\n"
else "$indent\"${escape(key)}\"\t\t\"${escape(value.toString())}\"\n"
}
private fun escape(text: String) = text.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n").replace("\t", "\\t")
fun parse(text: String): Node {
val tokens = tokenize(text)
@@ -135,6 +135,23 @@ class SteamTouchConfigFilesTest {
assertSavedA(413150, saved)
}
@Test fun aConfigsetCutOffAfterAKeyGetsTheEntryAtTheRoot() {
File(dir, "configset_controller_mobile_touch.vdf").writeText("\"controller_config\"\n{\n\t\"8400\"\n\t{\n\t\t\"workshop\"")
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(null, root?.child("8400")?.child("413150"))
assertSavedA(413150, saved)
}
@Test fun aConfigsetWhoseRootIsAQuotedValueIsRebuilt() {
File(dir, "configset_controller_mobile_touch.vdf").writeText("\"controller_config\" \"{}\"\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"))
assertSavedA(413150, saved)
}
private fun shortcut(name: String): Int {
val shortcut = 2702387189L.toInt()
template.apply { parentFile!!.mkdirs(); writeText(touch("Gamepad with Joystick")) }