All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.ru.casperix.spine.json.JsonSkeletonLoader.kt Maven / Gradle / Ivy

There is a newer version: 0.9.0
Show newest version
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
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy