mirror of
https://github.com/curioustorvald/tsvm.git
synced 2026-06-09 06:34:04 +09:00
reading config from config.json
This commit is contained in:
102
tsvm_executable/src/net/torvald/terrarum/KVHashMap.kt
Normal file
102
tsvm_executable/src/net/torvald/terrarum/KVHashMap.kt
Normal file
@@ -0,0 +1,102 @@
|
||||
package net.torvald.terrarum
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 2015-12-30.
|
||||
*/
|
||||
open class KVHashMap {
|
||||
|
||||
constructor() {
|
||||
hashMap = HashMap<String, Any>()
|
||||
}
|
||||
|
||||
protected constructor(newMap: HashMap<String, Any>) {
|
||||
hashMap = newMap
|
||||
}
|
||||
|
||||
var hashMap: HashMap<String, Any>
|
||||
|
||||
/**
|
||||
* Add key-value pair to the configuration table.
|
||||
* If key does not exist on the table, new key will be generated.
|
||||
* If key already exists, the value will be overwritten.
|
||||
|
||||
* @param key case insensitive
|
||||
* *
|
||||
* @param value
|
||||
*/
|
||||
open operator fun set(key: String, value: Any) {
|
||||
hashMap.put(key.toLowerCase(), value)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get value using key from configuration table.
|
||||
|
||||
* @param key case insensitive
|
||||
* *
|
||||
* @return Object value
|
||||
*/
|
||||
operator fun get(key: String): Any? {
|
||||
return hashMap[key.toLowerCase()]
|
||||
}
|
||||
|
||||
fun getAsInt(key: String): Int? {
|
||||
val value = get(key)
|
||||
|
||||
if (value == null) return null
|
||||
|
||||
return value as Int
|
||||
}
|
||||
|
||||
fun getAsDouble(key: String): Double? {
|
||||
val value = get(key)
|
||||
|
||||
if (value == null) return null
|
||||
|
||||
if (value is Int)
|
||||
return value.toDouble()
|
||||
|
||||
return value as Double
|
||||
}
|
||||
|
||||
fun getAsFloat(key: String): Float? {
|
||||
val value = get(key)
|
||||
|
||||
if (value == null) return null
|
||||
|
||||
if (value is Float) return value as Float
|
||||
|
||||
return getAsDouble(key)?.toFloat()
|
||||
}
|
||||
|
||||
fun getAsString(key: String): String? {
|
||||
val value = get(key)
|
||||
|
||||
if (value == null) return null
|
||||
|
||||
return value as String
|
||||
}
|
||||
|
||||
fun getAsBoolean(key: String): Boolean? {
|
||||
val value = get(key)
|
||||
|
||||
if (value == null) return null
|
||||
|
||||
return value as Boolean
|
||||
}
|
||||
|
||||
fun hasKey(key: String) = hashMap.containsKey(key)
|
||||
|
||||
val keySet: Set<Any>
|
||||
get() = hashMap.keys
|
||||
|
||||
open fun remove(key: String) {
|
||||
if (hashMap[key] != null) {
|
||||
hashMap.remove(key, hashMap[key]!!)
|
||||
}
|
||||
}
|
||||
|
||||
open fun clone(): KVHashMap {
|
||||
val cloneOfMap = hashMap.clone() as HashMap<String, Any>
|
||||
return KVHashMap(cloneOfMap)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
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.KVHashMap
|
||||
import net.torvald.terrarum.utils.JsonFetcher
|
||||
import net.torvald.tsvm.TsvmEmulator
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 2021-09-19.
|
||||
*/
|
||||
object WriteConfig {
|
||||
|
||||
private val jsoner = Json(JsonWriter.OutputType.json)
|
||||
|
||||
init {
|
||||
jsoner.ignoreUnknownFields = true
|
||||
jsoner.setUsePrototypes(false)
|
||||
jsoner.setIgnoreDeprecated(false)
|
||||
|
||||
// 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.forEachSiblings(jsonData) { key, obj ->
|
||||
map[key] = json.readValue(null, obj)
|
||||
}
|
||||
return map
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
/*fun getJson(): String {
|
||||
val sb = StringBuilder()
|
||||
|
||||
App.gameConfig.hashMap.toSortedMap().forEach { (k, v) ->
|
||||
sb.append("$k:")
|
||||
|
||||
when (v) {
|
||||
is DoubleArray -> { sb.append("[${v.joinToString(",")}]") }
|
||||
is IntArray -> { sb.append("[${v.joinToString(",")}]") }
|
||||
is Array<*> -> { sb.append("[${v.joinToString(",")}]") }
|
||||
else -> { sb.append("$v") }
|
||||
}
|
||||
|
||||
sb.append("\n")
|
||||
}
|
||||
|
||||
return "{\n$sb}"
|
||||
}*/
|
||||
|
||||
operator fun invoke() {
|
||||
val writer = java.io.FileWriter(TsvmEmulator.configDir, false)
|
||||
//writer.write(getJson())
|
||||
writer.write(jsoner.prettyPrint(TsvmEmulator.gameConfig))
|
||||
writer.close()
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user