-
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.
Merge pull request #95 from MineInAbyss/develop
Archetype cleanup, Prefab addon improvements
- Loading branch information
Showing
18 changed files
with
429 additions
and
72 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
80 changes: 80 additions & 0 deletions
80
...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,80 @@ | ||
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.ListSerializer | ||
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 namespaces = mutableListOf<String>() | ||
val components = mutableListOf<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) | ||
when (key) { | ||
"namespaces" -> { | ||
val valueSerializer = ListSerializer(String.serializer()) | ||
val newIndex = | ||
compositeDecoder.decodeElementIndex(MapSerializer(keySerializer, valueSerializer).descriptor) | ||
val namespacesList = compositeDecoder.decodeSerializableElement(descriptor, newIndex, valueSerializer) | ||
namespaces.addAll(namespacesList) | ||
} | ||
else -> { | ||
val foundValueSerializer = | ||
serializableComponents.serializers.getSerializerFor(key, GearyComponent::class, namespaces) 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
7 changes: 7 additions & 0 deletions
7
geary-core/src/commonMain/kotlin/com/mineinabyss/geary/components/KeepArchetype.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,7 @@ | ||
package com.mineinabyss.geary.components | ||
|
||
/** | ||
* An entity's archetype with this component may stay even if empty. | ||
* Useful for entities that are created and removed often, ex events. | ||
*/ | ||
sealed class KeepArchetype |
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
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
Oops, something went wrong.