Skip to content

Commit

Permalink
fix Indexable相关问题
Browse files Browse the repository at this point in the history
  • Loading branch information
way-zer committed Nov 24, 2023
1 parent 258af3c commit ce8205e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 32 deletions.
12 changes: 8 additions & 4 deletions src/main/kotlin/cf/wayzer/contentsTweaker/CTNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class CTNode private constructor() : ExtendableClass<CTExtInfo>() {

fun resolve(name: String): CTNode {
val node = children[name]
?: getAll<Indexable>().firstNotNullOfOrNull { it.resolve(name) }
?: getAll<IndexableRaw>().firstNotNullOfOrNull { it.resolve(name) }
?: error("Not found child $name")
node.collectAll()
return node
Expand Down Expand Up @@ -92,12 +92,16 @@ class CTNode private constructor() : ExtendableClass<CTExtInfo>() {
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))
}
Expand Down Expand Up @@ -129,7 +133,7 @@ class CTNode private constructor() : ExtendableClass<CTExtInfo>() {
init {
Nope.apply {
+Modifier { }
+Indexable { Nope }
+IndexableRaw { Nope }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Any?>>(CTNode.ObjInfo(list[i] ?: return@Indexable null))
extendOnce<CTNode.ObjInfo<Any?>>(CTNode.ObjInfo(list[i] ?: return@IndexableRaw null))
}
}
modifier("-") { json ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,23 @@ object UIExtResolver : ContentsTweaker.NodeCollector {

private fun CTNodeTypeChecked<Element>.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")
}
}
}
Expand Down

0 comments on commit ce8205e

Please sign in to comment.