diff --git a/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/components/CopyToInstances.kt b/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/components/CopyToInstances.kt index 595c6a2d7..adcb0f83b 100644 --- a/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/components/CopyToInstances.kt +++ b/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/components/CopyToInstances.kt @@ -8,8 +8,6 @@ import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable /** - * > geary:copy_to_instances - * * A component prefabs may use to specify a list of components which should be copied to instances of itself. * * These components are not active on the prefab itself and will be instantiated for every new instance of this class. @@ -17,7 +15,7 @@ import kotlinx.serialization.Serializable * in the Engine. */ @Serializable -@SerialName("geary:copy_to_instances") +@SerialName("geary:copyToInstances") data class CopyToInstances( private val temporary: Set<@Polymorphic Component> = setOf(), private val persisting: Set<@Polymorphic Component> = setOf(), diff --git a/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/serializers/ComponentListAsMapSerializer.kt b/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/serializers/ComponentListAsMapSerializer.kt index 21d913888..5e30279c6 100644 --- a/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/serializers/ComponentListAsMapSerializer.kt +++ b/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/serializers/ComponentListAsMapSerializer.kt @@ -30,15 +30,24 @@ class GearyComponentSerializer : KSerializer { } -class ComponentListAsMapSerializer : KSerializer> { +class ComponentListAsMapSerializer( + val namespaces: List = listOf(), + val prefix: String = "", +) : KSerializer> { val keySerializer = String.serializer() val valueSerializer = GearyComponentSerializer() override val descriptor: SerialDescriptor get() = MapSerializer(keySerializer, valueSerializer).descriptor + fun CompositeDecoder.decodeMapValue(valueSerializer: KSerializer): T { + val newDescriptor = MapSerializer(keySerializer, valueSerializer).descriptor + val newIndex = decodeElementIndex(newDescriptor) + return decodeSerializableElement(newDescriptor, newIndex, valueSerializer) + } + override fun deserialize(decoder: Decoder): List { - val namespaces = mutableListOf() + val namespaces = namespaces.toMutableList() val components = mutableListOf() val compositeDecoder = decoder.beginStructure(descriptor) while (true) { @@ -47,25 +56,24 @@ class ComponentListAsMapSerializer : KSerializer> { val startIndex = components.size * 2 val key: String = compositeDecoder.decodeSerializableElement(descriptor, startIndex + index, keySerializer) - when (key) { - "namespaces" -> { + when { + key == "namespaces" -> { val valueSerializer = ListSerializer(String.serializer()) - val newIndex = - compositeDecoder.decodeElementIndex(MapSerializer(keySerializer, valueSerializer).descriptor) - val namespacesList = compositeDecoder.decodeSerializableElement(descriptor, newIndex, valueSerializer) + val namespacesList = compositeDecoder.decodeMapValue(valueSerializer) namespaces.addAll(namespacesList) } + + key.endsWith("*") -> { + val innerSerializer = ComponentListAsMapSerializer(namespaces, key.removeSuffix("*")) + components.addAll(compositeDecoder.decodeMapValue(innerSerializer)) + } + else -> { - val foundValueSerializer = - serializableComponents.serializers.getSerializerFor(key, GearyComponent::class, namespaces) as? KSerializer - ?: error("No component serializer registered for $key") - val newDescriptor = MapSerializer(keySerializer, foundValueSerializer).descriptor - val newIndex = compositeDecoder.decodeElementIndex(newDescriptor) - val decodedValue = compositeDecoder.decodeSerializableElement( - descriptor = newDescriptor, - index = newIndex, - deserializer = foundValueSerializer, - ) + val foundValueSerializer = serializableComponents.serializers + .getSerializerFor("$prefix$key", GearyComponent::class, namespaces) as? KSerializer + ?: error("No component serializer registered for $key") + + val decodedValue = compositeDecoder.decodeMapValue(foundValueSerializer) components += decodedValue } } diff --git a/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/serializers/PrefabKeySerializer.kt b/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/serializers/PrefabKeySerializer.kt index d6f659706..0ec30d686 100644 --- a/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/serializers/PrefabKeySerializer.kt +++ b/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/serializers/PrefabKeySerializer.kt @@ -9,7 +9,7 @@ import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder object PrefabKeySerializer : KSerializer { - override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("geary:prefab_key", PrimitiveKind.STRING) + override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("geary:prefabKey", PrimitiveKind.STRING) override fun deserialize(decoder: Decoder): PrefabKey { return PrefabKey.of(decoder.decodeString()) diff --git a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/components/ComponentInfo.kt b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/components/ComponentInfo.kt index 7d88152d3..5e54925e7 100644 --- a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/components/ComponentInfo.kt +++ b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/components/ComponentInfo.kt @@ -8,7 +8,7 @@ import kotlin.reflect.KClassifier * Represents the [class][kClass] a component entity is responsible for. */ @Serializable -@SerialName("geary:component_info") +@SerialName("geary:componentInfo") data class ComponentInfo( val kClass: KClassifier )