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

com.utopiarise.serialization.godot.resource.ResourceDeserializer.kt Maven / Gradle / Ivy

The newest version!
package com.utopiarise.serialization.godot.resource

import com.utopiarise.serialization.godot.core.Tokenizer
import java.io.File

internal val pathMap = HashMap()

inline fun  fromGodotResource(resourcePath: String, resPathReplacement: String) =
    getOrPutResourceFromMap(resourcePath, ResourceDeserializer(T::class.java, resPathReplacement).deserialize(File(resourcePath))) as T

fun  fromGodotResource(type: Class, resourcePath: String, resPathReplacement: String): T {
    val resource = ResourceDeserializer(type, resPathReplacement).deserialize(File(resourcePath))
    return if (resource::class.java == type) {
        getOrPutResourceFromMap(resourcePath, resource) as T
    } else{
        throw IllegalArgumentException("$resource is not equal to $type")
    }
}

fun getOrPutResourceFromMap(resourcePath: String, any: Any): Any {
    if (!pathMap.containsKey(resourcePath)) pathMap[resourcePath] = any
    return pathMap[resourcePath]!!
}

@PublishedApi
internal class ResourceDeserializer(private val clazz: Class<*>, private val resPathReplacement: String) {
    fun deserialize(file: File): Any = DataInjector(clazz, resPathReplacement).inject(Tokenizer(file.readText()).tokenize())
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy