From cc5d0392b6a92b95067d629880b5e7a84ce78146 Mon Sep 17 00:00:00 2001 From: Aly Date: Sat, 13 Jun 2026 18:38:40 -0600 Subject: [PATCH] replace SpellContinuation recursive codec with more natural list form --- .../api/casting/eval/vm/SpellContinuation.kt | 45 ++++++------------- 1 file changed, 13 insertions(+), 32 deletions(-) diff --git a/Common/src/main/java/at/petrak/hexcasting/api/casting/eval/vm/SpellContinuation.kt b/Common/src/main/java/at/petrak/hexcasting/api/casting/eval/vm/SpellContinuation.kt index 774d9ea60d..359f94085a 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/casting/eval/vm/SpellContinuation.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/casting/eval/vm/SpellContinuation.kt @@ -20,42 +20,23 @@ sealed interface SpellContinuation { fun pushFrame(frame: ContinuationFrame): SpellContinuation = NotDone(frame, this) companion object { - // TODO port: maybe serialize to list like before? - // TODO port: maybe unit should be first @JvmStatic - val CODEC = Codec.recursive( - SpellContinuation::class.java.simpleName - ) { recursed: Codec -> - Codec.withAlternative( - Codec.unit(Done), - RecordCodecBuilder.create { inst -> - inst.group( - ContinuationFrame.Type.TYPED_CODEC.fieldOf("frame").forGetter { it.frame }, - recursed.fieldOf("next").forGetter { it.next } - ).apply(inst, ::NotDone) - } - ) - } + val CODEC = ContinuationFrame.Type.TYPED_CODEC.listOf().xmap(::fromList, ::toList) @JvmStatic - val STREAM_CODEC = StreamCodec.recursive { recursed -> - withAlternative( - StreamCodec.unit(Done), - StreamCodec.composite( - ContinuationFrame.Type.TYPED_STREAM_CODEC, NotDone::frame, - recursed, NotDone::next, - ::NotDone - ).map(Function.identity()) { it as NotDone } - ) + val STREAM_CODEC = ContinuationFrame.Type.TYPED_STREAM_CODEC.apply(ByteBufCodecs.list()).map(::fromList, ::toList) + + private fun fromList(list: List): SpellContinuation { + return list.foldRight(Done, ::NotDone) } - private fun withAlternative(primary: StreamCodec, alternative: StreamCodec): StreamCodec { - return ByteBufCodecs.either( - primary, - alternative - ).map( - Function { either: Either -> Either.unwrap(either) }, - Function { value: T -> Either.left(value) } - ) + private fun toList(sc: SpellContinuation): List { + val result = ArrayList() + var acc = sc + while(acc is NotDone) { + result.add(acc.frame) + acc = acc.next + } + return result } } }