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 a5da595248..1017f72eba 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/SteamTouchConfig.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/SteamTouchConfig.kt @@ -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('{') }?.let(::closeBraces) ?: "\"controller_config\"\n{\n}\n" + 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" } @@ -447,7 +447,7 @@ object SteamTouchConfig { } private fun closeBraces(text: String): String { - val open = text.count { it == '{' } - text.count { it == '}' } + val open = KeyValues.unclosedBraces(text) return if (open <= 0) text else text.trimEnd() + "\n}".repeat(open) + "\n" } @@ -606,7 +606,14 @@ class KeyValues private constructor() { fun string(key: String): String? = entries.firstOrNull { it.first.equals(key, true) && it.second is String }?.second as String? } + private class Token(val text: String, val quoted: Boolean) { + val opens get() = !quoted && text == "{" + val closes get() = !quoted && text == "}" + } + companion object { + fun unclosedBraces(text: String): Int = tokenize(text).let { tokens -> tokens.count { it.opens } - tokens.count { it.closes } } + fun parse(text: String): Node { val tokens = tokenize(text) var i = 0 @@ -614,26 +621,26 @@ class KeyValues private constructor() { val n = Node() while (i < tokens.size) { val t = tokens[i++] - if (t == "}" ) return n - if (t == "{") continue + if (t.closes) return n + if (t.opens) continue if (i >= tokens.size) break val next = tokens[i] - if (next == "{") { i++; n.entries += t to node() } else { i++; n.entries += t to next } + if (next.opens) { i++; n.entries += t.text to node() } else { i++; n.entries += t.text to next.text } } return n } return node() } - private fun tokenize(text: String): List { - val out = mutableListOf() + private fun tokenize(text: String): List { + val out = mutableListOf() var i = 0 while (i < text.length) { val c = text[i] when { c.isWhitespace() -> i++ c == '/' && i + 1 < text.length && text[i + 1] == '/' -> while (i < text.length && text[i] != '\n') i++ - c == '{' || c == '}' -> { out += c.toString(); i++ } + c == '{' || c == '}' -> { out += Token(c.toString(), false); i++ } c == '"' -> { val sb = StringBuilder() i++ @@ -645,13 +652,13 @@ class KeyValues private constructor() { i++ } i++ - out += sb.toString() + out += Token(sb.toString(), true) } c == '[' -> { while (i < text.length && text[i] != ']') i++; i++ } else -> { val start = i while (i < text.length && !text[i].isWhitespace() && text[i] != '{' && text[i] != '}' && text[i] != '"') i++ - out += text.substring(start, i) + out += Token(text.substring(start, i), false) } } } 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 742fed72db..0da26262f1 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/SteamTouchConfigFilesTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/SteamTouchConfigFilesTest.kt @@ -108,6 +108,14 @@ class SteamTouchConfigFilesTest { assertSavedA(413150, saved) } + @Test fun aConfigsetWithOnlyAQuotedBraceIsRebuilt() { + 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) + } + @Test fun anExistingConfigsetKeepsItsOtherEntries() { configset("configset_controller_mobile_touch.vdf", "8400" to "\"workshop\"\t\t\"7\"") saveDefault(413150) @@ -155,6 +163,16 @@ class SteamTouchConfigFilesTest { assertEquals(saved, SteamTouchConfig.load(context, shortcut).file) } + @Test fun aSavedShortcutNameWithBracesDoesNotHideTheNextAppsEntry() { + val shortcut = shortcut("Game {") + SteamTouchConfig.saveLayouts(context, shortcut, SteamTouchConfig.load(context, shortcut), mapOf(1 to emptyList())) + val saved = saveDefault(413150) + val root = KeyValues.parse(File(dir, "configset_controller_mobile_touch.vdf").readText()).child("controller_config") + assertEquals("1", root?.child("game {")?.string("autosave")) + assertEquals("1", root?.child("413150")?.string("autosave")) + assertSavedA(413150, saved) + } + @Test fun aShortcutNameThatIsNoPlainFolderNameUsesTheAppId() { for (name in listOf("../escape", "a/b", "..", "Tab\tName")) { assertEquals(name, "2702387189", SteamTouchConfig.steamKey(context, shortcut(name)))