diff --git a/src/main/kotlin/cf/wayzer/contentsTweaker/CTNode.kt b/src/main/kotlin/cf/wayzer/contentsTweaker/CTNode.kt index eec6f06..de4b3ac 100644 --- a/src/main/kotlin/cf/wayzer/contentsTweaker/CTNode.kt +++ b/src/main/kotlin/cf/wayzer/contentsTweaker/CTNode.kt @@ -39,7 +39,7 @@ class CTNode private constructor() : ExtendableClass() { fun resolve(name: String): CTNode { val node = children[name] - ?: getAll().firstNotNullOfOrNull { it.resolve(name) } + ?: getAll().firstNotNullOfOrNull { it.resolve(name) } ?: error("Not found child $name") node.collectAll() return node @@ -92,12 +92,16 @@ class CTNode private constructor() : ExtendableClass() { override fun reset() = setValue(info.obj) } - fun interface Indexable : CTExtInfo { + fun interface IndexableRaw : CTExtInfo { + fun resolve(name: String): CTNode? + } + + fun interface Indexable : IndexableRaw { /** 解析索引,[key]已去除# */ fun resolveIndex(key: String): CTNode? /** 通用的[name]索引(不一定以#开头) */ - fun resolve(name: String): CTNode? { + override fun resolve(name: String): CTNode? { if (name.isEmpty() || name[0] != '#') return null return resolveIndex(name.substring(1)) } @@ -129,7 +133,7 @@ class CTNode private constructor() : ExtendableClass() { init { Nope.apply { +Modifier { } - +Indexable { Nope } + +IndexableRaw { Nope } } } } diff --git a/src/main/kotlin/cf/wayzer/contentsTweaker/resolvers/ArrayResolver.kt b/src/main/kotlin/cf/wayzer/contentsTweaker/resolvers/ArrayResolver.kt index 055a171..f829f71 100644 --- a/src/main/kotlin/cf/wayzer/contentsTweaker/resolvers/ArrayResolver.kt +++ b/src/main/kotlin/cf/wayzer/contentsTweaker/resolvers/ArrayResolver.kt @@ -20,11 +20,11 @@ object ArrayResolver : ContentsTweaker.NodeCollector { if (item != null) node.getOrCreate("#$index") += CTNode.ObjInfo(item) } - node += CTNode.Indexable { key -> + node += CTNode.IndexableRaw { key -> val i = key.toInt() - if (i >= list.size) return@Indexable null + if (i >= list.size) return@IndexableRaw null node.getOrCreate("#${i}").apply { - extendOnce>(CTNode.ObjInfo(list[i] ?: return@Indexable null)) + extendOnce>(CTNode.ObjInfo(list[i] ?: return@IndexableRaw null)) } } modifier("-") { json -> diff --git a/src/main/kotlin/cf/wayzer/contentsTweaker/resolvers/MindustryContentsResolver.kt b/src/main/kotlin/cf/wayzer/contentsTweaker/resolvers/MindustryContentsResolver.kt index f8b774a..33410f9 100644 --- a/src/main/kotlin/cf/wayzer/contentsTweaker/resolvers/MindustryContentsResolver.kt +++ b/src/main/kotlin/cf/wayzer/contentsTweaker/resolvers/MindustryContentsResolver.kt @@ -65,12 +65,9 @@ object MindustryContentsResolver : ContentsTweaker.NodeCollector { +healthReload } } - node += object : CTNode.Indexable { - override fun resolveIndex(key: String): CTNode? = null - override fun resolve(name: String): CTNode? { - val normalize = Strings.camelToKebab(name) - return node.children[normalize] - } + node += CTNode.IndexableRaw { + val normalize = Strings.camelToKebab(it) + node.children[normalize] } // if (type == ContentType.block) // node += CTNode.AfterHandler { diff --git a/src/main/kotlin/cf/wayzer/contentsTweaker/resolvers/UIExtResolver.kt b/src/main/kotlin/cf/wayzer/contentsTweaker/resolvers/UIExtResolver.kt index 05b61de..ddca6c1 100644 --- a/src/main/kotlin/cf/wayzer/contentsTweaker/resolvers/UIExtResolver.kt +++ b/src/main/kotlin/cf/wayzer/contentsTweaker/resolvers/UIExtResolver.kt @@ -73,26 +73,23 @@ object UIExtResolver : ContentsTweaker.NodeCollector { private fun CTNodeTypeChecked.extend() { val obj = objInfo.obj - node += object : CTNode.Indexable { - override fun resolveIndex(key: String): CTNode? = null - override fun resolve(name: String): CTNode? { - if (name.length < 2 || name[0] != '+') return null - val idStart = name.indexOf('#') - check(idStart > 0) { "Must provide element id" } - val type = name.substring(1, idStart) - val id = name.substring(idStart) - return node.children[id] ?: createUIElement(type).let { element -> - node.getOrCreate(id).apply { - +CTNode.ObjInfo(element) - when (obj) { - is Table -> { - val cell = obj.add(element) - getOrCreate("cell") += CTNode.ObjInfo(cell) - } - - is Group -> obj.addChild(element) - else -> error("Only Group can add child element") + node += CTNode.IndexableRaw { name -> + if (name.length < 2 || name[0] != '+') return@IndexableRaw null + val idStart = name.indexOf('#') + check(idStart > 0) { "Must provide element id" } + val type = name.substring(1, idStart) + val id = name.substring(idStart) + node.children[id] ?: createUIElement(type).let { element -> + node.getOrCreate(id).apply { + +CTNode.ObjInfo(element) + when (obj) { + is Table -> { + val cell = obj.add(element) + getOrCreate("cell") += CTNode.ObjInfo(cell) } + + is Group -> obj.addChild(element) + else -> error("Only Group can add child element") } } }