Skip to content

Commit

Permalink
Experimental | Update Item
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFloodDragon committed May 21, 2024
1 parent 09f3554 commit 3a6cbf5
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,33 @@

package cn.fd.ratziel.core.serialization

import cn.fd.ratziel.core.util.forEachDually
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.elementNames
import kotlinx.serialization.json.JsonNames

fun SerialDescriptor.getElementDescriptor(name:String) = this.getElementDescriptor(this.getElementIndex(name))
fun SerialDescriptor.getElementNames(name: String): Set<String> = this.getElementNames(this.getElementIndex(name))

fun SerialDescriptor.getElementAnnotations(name: String) = this.getElementAnnotations(this.getElementIndex(name))

fun SerialDescriptor.getElementDescriptor(name: String) = this.getElementDescriptor(this.getElementIndex(name))

/**
* 获取序列化器中使用的节点
* 即所有的元素名称和 [JsonNames] 注解中的所有名称
*/
val SerialDescriptor.usedNodes: List<String>
get() = buildList {
// 添加所有元素名称
addAll(elementNames)
val SerialDescriptor.elementAlias: List<String> get() = elementNames.flatMap { getElementNames(it) }

/**
* 获取序列化器中使用的节点
* 即的元素名称和 [JsonNames] 注解中的名称
*/
fun SerialDescriptor.getElementNames(index: Int): Set<String> =
buildSet {
// 添加元素名称
add(getElementName(index))
// 添加注解 [JsonNames] 中的名称
elementAnnotations.forEachDually { if (it is JsonNames) addAll(it.names) }
getElementAnnotations(index).forEach { if (it is JsonNames) addAll(it.names) }
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cn.fd.ratziel.module.item.api.common

import cn.fd.ratziel.function.argument.ArgumentFactory
import cn.fd.ratziel.function.argument.DefaultArgumentFactory
import cn.fd.ratziel.module.item.api.Resolver

/**
* ArgumentResolver
*
* @author TheFloodDragon
* @since 2024/5/21 22:54
*/
interface ArgumentResolver<E, T> : Resolver<E, T> {

/**
* 解析元素 (带参数)
*/
fun resolve(element: E, arguments: ArgumentFactory): T

/**
* 解析元素 (带空参数)
*/
override fun resolve(element: E): T = resolve(element, DefaultArgumentFactory())

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cn.fd.ratziel.module.item.api.common

/**
* NamedStringResolver
*
* @author TheFloodDragon
* @since 2024/5/21 22:55
*/
interface NamedStringResolver : StringResolver {

/**
* 解析器名称
*/
val name: String

/**
* 解析器别名
*/
val alias: Array<String>

}
Original file line number Diff line number Diff line change
@@ -1,35 +1,9 @@
package cn.fd.ratziel.module.item.api.common

import cn.fd.ratziel.function.argument.ArgumentFactory
import cn.fd.ratziel.function.argument.DefaultArgumentFactory
import cn.fd.ratziel.module.item.api.Resolver

/**
* StringResolver
*
* @author TheFloodDragon
* @since 2024/5/18 15:15
*/
interface StringResolver : Resolver<Iterable<String>, String?> {

/**
* 解析器名称
*/
val name: String

/**
* 解析器别名
*/
val alias: Array<String>

/**
* 解析元素 (带参数)
*/
fun resolve(element: Iterable<String>, arguments: ArgumentFactory): String?

/**
* 解析元素 (带空参数)
*/
override fun resolve(element: Iterable<String>): String? = resolve(element, DefaultArgumentFactory())

}
interface StringResolver : ArgumentResolver<Iterable<String>, String?>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import cn.fd.ratziel.core.util.splitNonEscaped
import cn.fd.ratziel.function.argument.ArgumentFactory
import cn.fd.ratziel.function.argument.popOrNull
import cn.fd.ratziel.module.item.api.builder.ItemResolver
import cn.fd.ratziel.module.item.api.common.NamedStringResolver
import cn.fd.ratziel.module.item.api.common.StringResolver
import cn.fd.ratziel.module.item.impl.builder.resolver.RandomResolver
import kotlinx.serialization.json.JsonElement
Expand Down Expand Up @@ -36,7 +37,7 @@ object DefaultItemResolver : ItemResolver {
/**
* 字符串解析器 (解析器名称-解析器)
*/
val resolvers = mutableListOf<StringResolver>(
val resolvers = mutableListOf<NamedStringResolver>(
RandomResolver
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cn.fd.ratziel.module.item.impl.builder

import cn.fd.ratziel.core.serialization.elementAlias
import cn.fd.ratziel.core.serialization.get
import cn.fd.ratziel.core.serialization.getElementDescriptor
import cn.fd.ratziel.core.serialization.getElementNames
import cn.fd.ratziel.core.serialization.handle
import cn.fd.ratziel.core.serialization.usedNodes
import cn.fd.ratziel.module.item.api.ItemMaterial
import cn.fd.ratziel.module.item.api.common.ItemKSerializer
import cn.fd.ratziel.module.item.impl.part.VItemDisplay
Expand Down Expand Up @@ -80,12 +80,12 @@ class DefaultItemSerializer(rawJson: Json) : ItemKSerializer<VItemMeta> {
VItemSundry.serializer(),
)

val NODES_MATERIAL = VItemMeta.serializer().descriptor.getElementDescriptor(VItemMeta::material.name).usedNodes
val NODES_MATERIAL = VItemMeta.serializer().descriptor.getElementNames(VItemMeta::material.name)

/**
* 占据的节点
*/
val occupiedNodes = serializers.flatMap { it.descriptor.usedNodes }
val occupiedNodes = serializers.flatMap { it.descriptor.elementAlias }

/**
* 结构化解析
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cn.fd.ratziel.module.item.impl.builder.resolver

import cn.fd.ratziel.function.argument.ArgumentFactory
import cn.fd.ratziel.module.item.api.common.StringResolver
import cn.fd.ratziel.module.item.api.common.NamedStringResolver
import taboolib.common.util.random

/**
Expand All @@ -10,7 +10,7 @@ import taboolib.common.util.random
* @author TheFloodDragon
* @since 2024/5/18 16:22
*/
object RandomResolver : StringResolver {
object RandomResolver : NamedStringResolver {

override val name = "random"

Expand Down

0 comments on commit 3a6cbf5

Please sign in to comment.