commonMain.ru.casperix.spine.json.JsonSkeletonLoader.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spine-jvm Show documentation
Show all versions of spine-jvm Show documentation
Signals for all occasions
package ru.casperix.spine.json
import casperix.loader.ResourceLoadError
import casperix.loader.resourceLoader
import casperix.signals.concrete.EitherFuture
import casperix.signals.concrete.EitherSignal
import kotlinx.serialization.json.Json
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.polymorphic
import kotlinx.serialization.modules.subclass
import ru.casperix.atlas.Atlas
import ru.casperix.atlas.AtlasLoader
import ru.casperix.spine.SkeletonData
import ru.casperix.spine.json.component.*
import ru.casperix.spine.json.component.Curve
object JsonSkeletonLoader {
private val JSON = Json {
ignoreUnknownKeys = true
serializersModule = SerializersModule {
polymorphic(AttachmentJson::class) {
subclass(RegionAttachmentJson::class)
subclass(MeshAttachmentJson::class)
subclass(LinkedMeshAttachmentJson::class)
subclass(BoundingBoxAttachmentJson::class)
subclass(PathAttachmentJson::class)
subclass(PointAttachmentJson::class)
subclass(ClippingAttachmentJson::class)
defaultDeserializer { RegionAttachmentJson.serializer() }
}
}
}
fun load(skeletonFile: String, atlasFile:String): EitherFuture, ResourceLoadError> {
val atlasLoader = AtlasLoader.load(atlasFile)
val spineLoader = resourceLoader.loadText(skeletonFile)
val signal = EitherSignal, ResourceLoadError>()
spineLoader.thenAccept { rawSpine->
atlasLoader.thenAccept { atlas->
val json = JSON.decodeFromString(rawSpine)
val data = SkeletonDecoder(json, skeletonFile, atlas).output
signal.accept(Pair(data, atlas))
}
}
spineLoader.thenReject {
signal.reject(it)
}
atlasLoader.thenReject {
signal.reject(it)
}
return signal
}
}