-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use map syntax for prefab file definitions
- Loading branch information
Showing
4 changed files
with
76 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...mmonMain/kotlin/com/mineinabyss/geary/prefabs/serializers/ComponentListAsMapSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.mineinabyss.geary.prefabs.serializers | ||
|
||
import com.mineinabyss.geary.datatypes.GearyComponent | ||
import com.mineinabyss.geary.serialization.dsl.serializableComponents | ||
import kotlinx.serialization.InternalSerializationApi | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.builtins.MapSerializer | ||
import kotlinx.serialization.builtins.serializer | ||
import kotlinx.serialization.descriptors.PolymorphicKind | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.descriptors.buildSerialDescriptor | ||
import kotlinx.serialization.encoding.CompositeDecoder | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
|
||
|
||
class GearyComponentSerializer : KSerializer<GearyComponent> { | ||
@OptIn(InternalSerializationApi::class) | ||
override val descriptor: SerialDescriptor = | ||
buildSerialDescriptor("GearyComponentSerializer", PolymorphicKind.SEALED) | ||
|
||
override fun deserialize(decoder: Decoder): GearyComponent { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: GearyComponent) { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
} | ||
|
||
class ComponentListAsMapSerializer : KSerializer<List<GearyComponent>> { | ||
val keySerializer = String.serializer() | ||
val valueSerializer = GearyComponentSerializer() | ||
|
||
override val descriptor: SerialDescriptor | ||
get() = MapSerializer(keySerializer, valueSerializer).descriptor | ||
|
||
override fun deserialize(decoder: Decoder): List<GearyComponent> { | ||
val components = mutableSetOf<GearyComponent>() | ||
val compositeDecoder = decoder.beginStructure(descriptor) | ||
while (true) { | ||
val index = compositeDecoder.decodeElementIndex(descriptor) | ||
if (index == CompositeDecoder.DECODE_DONE) break | ||
|
||
val startIndex = components.size * 2 | ||
val key: String = compositeDecoder.decodeSerializableElement(descriptor, startIndex + index, keySerializer) | ||
val foundValueSerializer = | ||
serializableComponents.serializers.getSerializerFor(key, GearyComponent::class) as? KSerializer<Any> | ||
?: error("No component serializer registered for $key") | ||
val newDescriptor = MapSerializer(keySerializer, foundValueSerializer).descriptor | ||
val newIndex = compositeDecoder.decodeElementIndex(newDescriptor) | ||
val decodedValue = compositeDecoder.decodeSerializableElement<Any>( | ||
descriptor = newDescriptor, | ||
index = newIndex, | ||
deserializer = foundValueSerializer, | ||
) | ||
components += decodedValue | ||
} | ||
compositeDecoder.endStructure(descriptor) | ||
return components.toList() | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: List<GearyComponent>) { | ||
TODO("Not implemented") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters