Skip to content

Commit 33e9b48

Browse files
committed
fix: Don't pass world in CopyToInstances, we can get it from the entity in decodeComponentsTo
1 parent 364b694 commit 33e9b48

File tree

2 files changed

+6
-20
lines changed

2 files changed

+6
-20
lines changed

addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/components/CopyToInstances.kt

+6-19
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ package com.mineinabyss.geary.prefabs.configuration.components
22

33
import com.mineinabyss.geary.datatypes.Component
44
import com.mineinabyss.geary.datatypes.Entity
5-
import com.mineinabyss.geary.modules.Geary
65
import com.mineinabyss.geary.serialization.SerializableComponents
76
import com.mineinabyss.geary.serialization.serializers.SerializedComponents
87
import com.mineinabyss.geary.serialization.setAllPersisting
9-
import kotlinx.serialization.Contextual
108
import kotlinx.serialization.Polymorphic
119
import kotlinx.serialization.SerialName
1210
import kotlinx.serialization.Serializable
@@ -23,31 +21,20 @@ import kotlinx.serialization.Serializable
2321
data class CopyToInstances(
2422
private val temporary: SerializedComponents? = null,
2523
private val persisting: SerializedComponents? = null,
26-
private val world: @Contextual Geary,
2724
) {
2825
@Serializable
2926
private data class DeepCopy(
3027
val temporary: List<@Polymorphic Component>?,
31-
val persisting: List<@Polymorphic Component>?
28+
val persisting: List<@Polymorphic Component>?,
3229
)
3330

34-
val formats get() = world.getAddon(SerializableComponents).formats
31+
fun decodeComponentsTo(entity: Entity) {
32+
val binaryFormat = entity.world.getAddon(SerializableComponents).formats.binaryFormat
3533

36-
// This is the safest and cleanest way to deep-copy, even if a little performance intense.
37-
private val serializedComponents by lazy {
38-
formats.binaryFormat.encodeToByteArray(
39-
DeepCopy.serializer(),
40-
DeepCopy(temporary, persisting)
41-
)
42-
}
34+
// This is the safest and cleanest way to deep-copy, even if a little performance intense.
35+
val encoded = binaryFormat.encodeToByteArray(DeepCopy.serializer(), DeepCopy(temporary, persisting))
36+
val (instance, persist) = binaryFormat.decodeFromByteArray(DeepCopy.serializer(), encoded)
4337

44-
private fun getDeepCopied() = formats.binaryFormat.decodeFromByteArray(
45-
DeepCopy.serializer(), serializedComponents
46-
)
47-
48-
fun decodeComponentsTo(entity: Entity) {
49-
val (instance, persist) = getDeepCopied()
50-
//order of addition specifies that persisting components should override all
5138
if (instance != null) {
5239
entity.setAll(instance, override = false)
5340
}

addons/geary-prefabs/src/jvmTest/kotlin/com/mineinabyss/geary/prefabs/CopyToInstancesTest.kt

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class CopyToInstancesTest : GearyTest() {
3333
CopyToInstances(
3434
temporary = listOf(42),
3535
persisting = listOf("Hello world"),
36-
world = world,
3736
)
3837
)
3938
addRelation<NoInherit, CopyToInstances>()

0 commit comments

Comments
 (0)