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: 1.1.1
Show newest version
package ru.casperix.spine.json

import ru.casperix.signals.concrete.EitherFuture
import ru.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.multiplatform.loader.ResourceLoadError
import ru.casperix.multiplatform.loader.resourceLoader
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