@@ -2,11 +2,9 @@ package com.mineinabyss.geary.prefabs.configuration.components
2
2
3
3
import com.mineinabyss.geary.datatypes.Component
4
4
import com.mineinabyss.geary.datatypes.Entity
5
- import com.mineinabyss.geary.modules.Geary
6
5
import com.mineinabyss.geary.serialization.SerializableComponents
7
6
import com.mineinabyss.geary.serialization.serializers.SerializedComponents
8
7
import com.mineinabyss.geary.serialization.setAllPersisting
9
- import kotlinx.serialization.Contextual
10
8
import kotlinx.serialization.Polymorphic
11
9
import kotlinx.serialization.SerialName
12
10
import kotlinx.serialization.Serializable
@@ -23,31 +21,20 @@ import kotlinx.serialization.Serializable
23
21
data class CopyToInstances (
24
22
private val temporary : SerializedComponents ? = null ,
25
23
private val persisting : SerializedComponents ? = null ,
26
- private val world : @Contextual Geary ,
27
24
) {
28
25
@Serializable
29
26
private data class DeepCopy (
30
27
val temporary : List <@Polymorphic Component >? ,
31
- val persisting : List <@Polymorphic Component >?
28
+ val persisting : List <@Polymorphic Component >? ,
32
29
)
33
30
34
- val formats get() = world.getAddon(SerializableComponents ).formats
31
+ fun decodeComponentsTo (entity : Entity ) {
32
+ val binaryFormat = entity.world.getAddon(SerializableComponents ).formats.binaryFormat
35
33
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)
43
37
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
51
38
if (instance != null ) {
52
39
entity.setAll(instance, override = false )
53
40
}
0 commit comments