diff --git a/.gitignore b/.gitignore index 7231c09a..9a4a5f3e 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ eclipse kotlin-js-store/ geary-benchmarks/.results +/truckconfig.json diff --git a/addons/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/ActionGroup.kt b/addons/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/ActionGroup.kt index 08247769..7610f4aa 100644 --- a/addons/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/ActionGroup.kt +++ b/addons/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/ActionGroup.kt @@ -2,6 +2,7 @@ package com.mineinabyss.geary.actions import com.mineinabyss.geary.actions.actions.EmitEventAction import com.mineinabyss.geary.actions.actions.EnsureAction +import com.mineinabyss.geary.actions.event_binds.ActionOnFail import com.mineinabyss.geary.actions.event_binds.ActionRegister import com.mineinabyss.geary.actions.event_binds.ActionWhen import com.mineinabyss.geary.modules.geary @@ -15,6 +16,7 @@ class ActionEntry( val action: Action, val conditions: List?, val register: String?, + val onFail: ActionGroup?, ) @Serializable(with = ActionGroup.Serializer::class) @@ -33,19 +35,21 @@ class ActionGroup( if (entry.register != null) context.register(entry.register, returned) } catch (e: ActionsCancelledException) { + entry.onFail?.execute(context) return } } } - object Serializer : InnerSerializer, ActionGroup>( + class Serializer : InnerSerializer, ActionGroup>( serialName = "geary:action_group", inner = ListSerializer( PolymorphicListAsMapSerializer.ofComponents( PolymorphicListAsMapSerializer.Config( customKeys = mapOf( - "when" to ActionWhen.serializer(), - "register" to ActionRegister.serializer() + "when" to { ActionWhen.serializer() }, + "register" to { ActionRegister.serializer() }, + "onFail" to { ActionOnFail.serializer() } ) ) ) @@ -56,10 +60,12 @@ class ActionGroup( var action: Action? = null var condition: List? = null var register: String? = null + var onFail: ActionGroup? = null components.forEach { comp -> when { comp is ActionWhen -> condition = comp.conditions comp is ActionRegister -> register = comp.register + comp is ActionOnFail -> onFail = comp.action action != null -> geary.logger.w { "Multiple actions defined in one block!" } else -> action = EmitEventAction.wrapIfNotAction(comp) } @@ -68,7 +74,8 @@ class ActionGroup( ActionEntry( action = action!!, conditions = condition, - register = register + register = register, + onFail = onFail ) } ActionGroup(actions) diff --git a/addons/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/actions/EnsureAction.kt b/addons/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/actions/EnsureAction.kt index c7031d6c..5b673a5e 100644 --- a/addons/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/actions/EnsureAction.kt +++ b/addons/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/actions/EnsureAction.kt @@ -1,10 +1,6 @@ package com.mineinabyss.geary.actions.actions -import com.mineinabyss.geary.actions.Action -import com.mineinabyss.geary.actions.ActionsCancelledException -import com.mineinabyss.geary.actions.ActionGroupContext -import com.mineinabyss.geary.actions.Condition -import com.mineinabyss.geary.actions.event_binds.EventBind +import com.mineinabyss.geary.actions.* import com.mineinabyss.geary.helpers.componentId import com.mineinabyss.geary.serialization.serializers.InnerSerializer import com.mineinabyss.geary.serialization.serializers.PolymorphicListAsMapSerializer @@ -23,7 +19,9 @@ class EnsureAction( flat.forEach { (id, data) -> when (data) { is Condition -> with(data) { - if(!execute()) throw ActionsCancelledException() + if(!execute()) { + throw ActionsCancelledException() + } } else -> entity.emit(id, data) //TODO use geary condition system if we get one } diff --git a/addons/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/event_binds/EntityObservers.kt b/addons/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/event_binds/EntityObservers.kt index 14286b36..6bb5574b 100644 --- a/addons/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/event_binds/EntityObservers.kt +++ b/addons/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/event_binds/EntityObservers.kt @@ -17,7 +17,7 @@ class EntityObservers( serialName = "geary:observe", inner = MapSerializer( SerializableComponentId.serializer(), - ActionGroup.Serializer + ActionGroup.serializer() ), inverseTransform = { TODO() }, transform = { @@ -33,7 +33,7 @@ class EntityObservers( @Serializable(with = ActionWhen.Serializer::class) class ActionWhen(val conditions: List) { - object Serializer : InnerSerializer, ActionWhen>( + class Serializer : InnerSerializer, ActionWhen>( serialName = "geary:when", inner = ListSerializer(EnsureAction.serializer()), inverseTransform = ActionWhen::conditions, @@ -44,3 +44,13 @@ class ActionWhen(val conditions: List) { @JvmInline @Serializable value class ActionRegister(val register: String) + +@Serializable(with = ActionOnFail.Serializer::class) +class ActionOnFail(val action: ActionGroup) { + class Serializer : InnerSerializer( + serialName = "geary:on_fail", + inner = ActionGroup.Serializer(), + inverseTransform = ActionOnFail::action, + transform = ::ActionOnFail + ) +} diff --git a/addons/geary-serialization/src/commonMain/kotlin/com/mineinabyss/geary/serialization/serializers/PolymorphicListAsMapSerializer.kt b/addons/geary-serialization/src/commonMain/kotlin/com/mineinabyss/geary/serialization/serializers/PolymorphicListAsMapSerializer.kt index 23243083..0b3187b1 100644 --- a/addons/geary-serialization/src/commonMain/kotlin/com/mineinabyss/geary/serialization/serializers/PolymorphicListAsMapSerializer.kt +++ b/addons/geary-serialization/src/commonMain/kotlin/com/mineinabyss/geary/serialization/serializers/PolymorphicListAsMapSerializer.kt @@ -49,7 +49,7 @@ open class PolymorphicListAsMapSerializer( } else -> { - val componentSerializer = config.customKeys[key] ?: findSerializerFor(compositeDecoder.serializersModule, namespaces, key) + val componentSerializer = config.customKeys[key]?.invoke() ?: findSerializerFor(compositeDecoder.serializersModule, namespaces, key) .getOrElse { if (config.onMissingSerializer != OnMissing.IGNORE) { config.whenComponentMalformed(key) @@ -122,7 +122,7 @@ open class PolymorphicListAsMapSerializer( val onMissingSerializer: OnMissing = OnMissing.WARN, val skipMalformedComponents: Boolean = true, val whenComponentMalformed: (String) -> Unit = {}, - val customKeys: Map> = mapOf(), + val customKeys: Map KSerializer> = mapOf(), ) companion object {