mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-16 21:44:05 +09:00
writing config to disk
This commit is contained in:
51
src/net/torvald/terrarum/serialise/WriteConfig.kt
Normal file
51
src/net/torvald/terrarum/serialise/WriteConfig.kt
Normal file
@@ -0,0 +1,51 @@
|
||||
package net.torvald.terrarum.serialise
|
||||
|
||||
import com.badlogic.gdx.utils.Json
|
||||
import com.badlogic.gdx.utils.JsonValue
|
||||
import com.badlogic.gdx.utils.JsonWriter
|
||||
import net.torvald.terrarum.App
|
||||
import net.torvald.terrarum.KVHashMap
|
||||
import net.torvald.terrarum.utils.JsonFetcher
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 2021-09-19.
|
||||
*/
|
||||
object WriteConfig {
|
||||
|
||||
private val jsoner = Json(JsonWriter.OutputType.json)
|
||||
|
||||
init {
|
||||
// KVHashMap
|
||||
jsoner.setSerializer(KVHashMap::class.java, object : Json.Serializer<KVHashMap> {
|
||||
override fun write(json: Json, obj: KVHashMap, knownType: Class<*>?) {
|
||||
json.writeObjectStart()
|
||||
obj.hashMap.toSortedMap().forEach { (k, v) ->
|
||||
json.writeValue(k, v)
|
||||
}
|
||||
json.writeObjectEnd()
|
||||
}
|
||||
|
||||
override fun read(json: Json, jsonData: JsonValue, type: Class<*>?): KVHashMap {
|
||||
val map = KVHashMap()
|
||||
JsonFetcher.forEach(jsonData) { key, obj ->
|
||||
map[key] = json.readValue(null, obj)
|
||||
}
|
||||
return map
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
operator fun invoke() {
|
||||
println("Writing config...")
|
||||
App.gameConfig.hashMap.forEach { s, any ->
|
||||
println("config $s = $any")
|
||||
}
|
||||
|
||||
|
||||
val writer = java.io.FileWriter(App.configDir, false)
|
||||
writer.write(jsoner.prettyPrint(App.gameConfig))
|
||||
writer.close()
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user