diff --git a/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/components/EntityObservers.kt b/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/components/EntityObservers.kt index 6e67756c..e5845481 100644 --- a/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/components/EntityObservers.kt +++ b/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/components/EntityObservers.kt @@ -28,7 +28,9 @@ class EventBind( class CachedEvent(val componentId: ComponentId, val data: Any?) @Transient - val emitEvents = emit.map { - if (using != null) ReEmitEvent(SerializableComponentId(using.id), it) else it + val emitEvents = emit.flatMap { emitter -> + emitter.map { component -> + if (using != null) ReEmitEvent(SerializableComponentId(using.id), component) else component + } }.map { CachedEvent(componentId(it::class), it) } } diff --git a/addons/geary-prefabs/src/jvmTest/kotlin/com/mineinabyss/geary/prefabs/observers/ConfigEntityObserversTests.kt b/addons/geary-prefabs/src/jvmTest/kotlin/com/mineinabyss/geary/prefabs/observers/ConfigEntityObserversTests.kt new file mode 100644 index 00000000..6a96e7d9 --- /dev/null +++ b/addons/geary-prefabs/src/jvmTest/kotlin/com/mineinabyss/geary/prefabs/observers/ConfigEntityObserversTests.kt @@ -0,0 +1,74 @@ +package com.mineinabyss.geary.prefabs.observers + +import com.mineinabyss.geary.modules.TestEngineModule +import com.mineinabyss.geary.modules.geary +import com.mineinabyss.geary.prefabs.Prefabs +import com.mineinabyss.geary.prefabs.configuration.components.EntityObservers +import com.mineinabyss.geary.serialization.dsl.serialization +import com.mineinabyss.geary.serialization.dsl.withCommonComponentNames +import com.mineinabyss.geary.serialization.formats.YamlFormat +import com.mineinabyss.geary.serialization.serializableComponents +import com.mineinabyss.geary.serialization.serializers.GearyEntitySerializer +import com.mineinabyss.geary.systems.builders.observeWithData +import com.mineinabyss.idofront.di.DI +import io.kotest.assertions.print.Print +import io.kotest.matchers.shouldBe +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable +import kotlinx.serialization.builtins.serializer +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test + +class ConfigEntityObserversTests { + @Serializable + @SerialName("geary:print") + data class Print(val string: String) + + @Serializable + @SerialName("geary:my_comp") + class MyComp() + + @BeforeEach + fun createEngine() { + DI.clear() + geary(TestEngineModule) { + install(Prefabs) + + serialization { + withCommonComponentNames() + + components { + component(String.serializer()) + component(Print.serializer()) + component(EntityObservers.serializer()) + component(MyComp.serializer()) + } + } + } + geary.pipeline.runStartupTasks() + } + + @Test + fun `should correctly add temporary and persisting components with CopyToInstances`() { + // arrange + val entityDef = """ + geary:observe: + - event: geary:onSet + involving: [ geary:myComp ] + emit: + - geary:print: + string: "Hello World" + """.trimIndent() + + val format = YamlFormat(serializableComponents.serializers.module) + val entity = format.decodeFromString(GearyEntitySerializer, entityDef) + val printed = mutableListOf() + geary.observeWithData().exec { printed += event.string } + + // act + entity.set(MyComp()) + + // assert + printed shouldBe listOf("Hello World") + } +} diff --git a/addons/geary-serialization/src/commonMain/kotlin/com/mineinabyss/geary/serialization/formats/Format.kt b/addons/geary-serialization/src/commonMain/kotlin/com/mineinabyss/geary/serialization/formats/Format.kt index 302e9cd3..c90e3144 100644 --- a/addons/geary-serialization/src/commonMain/kotlin/com/mineinabyss/geary/serialization/formats/Format.kt +++ b/addons/geary-serialization/src/commonMain/kotlin/com/mineinabyss/geary/serialization/formats/Format.kt @@ -8,6 +8,13 @@ import okio.Path interface Format { val ext: String + fun decodeFromString( + deserializer: DeserializationStrategy, + string: String, + overrideSerializersModule: SerializersModule? = null, + configType: ConfigType = ConfigType.REGULAR, + ): T + fun decodeFromFile( deserializer: DeserializationStrategy, path: Path, diff --git a/addons/geary-serialization/src/jvmMain/kotlin/com/mineinabyss/geary/serialization/formats/YamlFormat.kt b/addons/geary-serialization/src/jvmMain/kotlin/com/mineinabyss/geary/serialization/formats/YamlFormat.kt index 2feca5f3..c0ab4a82 100644 --- a/addons/geary-serialization/src/jvmMain/kotlin/com/mineinabyss/geary/serialization/formats/YamlFormat.kt +++ b/addons/geary-serialization/src/jvmMain/kotlin/com/mineinabyss/geary/serialization/formats/YamlFormat.kt @@ -52,11 +52,12 @@ class YamlFormat( return Yaml(module, config).decodeFromStream(deserializer, inputStream()) } - fun decodeFromString( + + override fun decodeFromString( deserializer: DeserializationStrategy, @Language("yaml") string: String, - overrideSerializersModule: SerializersModule? = null, - configType: ConfigType = ConfigType.REGULAR, + overrideSerializersModule: SerializersModule?, + configType: ConfigType ): T { return decodeFromStream(deserializer, overrideSerializersModule, configType) { string.byteInputStream() } }