writing config to disk

This commit is contained in:
minjaesong
2021-09-19 15:15:42 +09:00
parent b741d463bb
commit 66e77e0a01
8 changed files with 76 additions and 6 deletions

View File

@@ -297,6 +297,7 @@ public class App implements ApplicationListener {
// load configs
getDefaultDirectory();
createDirs();
initialiseConfig();
readConfigJson();
updateListOfSavegames();
@@ -988,7 +989,7 @@ public class App implements ApplicationListener {
// CONFIG //
private static KVHashMap gameConfig = new KVHashMap();
public static KVHashMap gameConfig = new KVHashMap();
private static void createConfigJson() throws IOException {
File configFile = new File(configDir);
@@ -998,6 +999,15 @@ public class App implements ApplicationListener {
}
}
/**
* Reads DefaultConfig to populate the gameConfig
*/
private static void initialiseConfig() {
for (Map.Entry<String, Object> entry : DefaultConfig.INSTANCE.getHashMap().entrySet()) {
gameConfig.set(entry.getKey(), entry.getValue());
}
}
/**
*
* @return true on successful, false on failure.

View File

@@ -76,7 +76,7 @@ object DefaultConfig {
"config_keyjump" to Input.Keys.SPACE,
"config_keyquickslots" to (Input.Keys.NUM_0..Input.Keys.NUM_9).toList(),
"config_keyquickslots" to (Input.Keys.NUM_0..Input.Keys.NUM_9).toList().toIntArray(),
"config_mouseprimary" to Input.Buttons.LEFT, // left mouse
"config_mousesecondary" to Input.Buttons.RIGHT, // right mouse

View File

@@ -15,7 +15,7 @@ open class KVHashMap {
hashMap = newMap
}
protected var hashMap: HashMap<String, Any>
var hashMap: HashMap<String, Any>
/**
* Add key-value pair to the configuration table.

View File

@@ -10,6 +10,7 @@ import net.torvald.terrarum.App.printdbgerr
import net.torvald.terrarum.QNDTreeNode
import net.torvald.terrarum.TitleScreen
import net.torvald.terrarum.Yaml
import net.torvald.terrarum.serialise.WriteConfig
import net.torvald.terrarum.ui.UICanvas
import net.torvald.terrarum.ui.UIItemTextButton
import net.torvald.terrarum.ui.UIItemTextButtonList
@@ -92,7 +93,13 @@ open class UIRemoCon(val parent: TitleScreen, treeRepresentation: QNDTreeNode<St
//System.exit(0)
Gdx.app.exit()
}
else if (it.labelText == "MENU_LABEL_RETURN") {
else if (it.labelText.startsWith("MENU_LABEL_RETURN")) {
val tag = it.labelText.substringAfter('+')
when (tag) {
"WRITETOCONFIG" -> WriteConfig()
}
if (currentRemoConContents.parent != null) {
remoConTray.consume()

View File

@@ -70,10 +70,12 @@ class UITitleLanguage : UICanvas() {
// attach listeners
textArea1.selectionChangeListener = { _, newSelectionIndex ->
App.GAME_LOCALE = localeList[newSelectionIndex]
App.setConfig("language", localeList[newSelectionIndex])
textArea2.deselect()
}
textArea2.selectionChangeListener = { _, newSelectionIndex ->
App.GAME_LOCALE = localeList[newSelectionIndex + localeFirstHalf.size]
App.setConfig("language", localeList[newSelectionIndex + localeFirstHalf.size])
textArea1.deselect()
}

View File

@@ -17,7 +17,7 @@ object UITitleRemoConYaml {
- MENU_OPTIONS_CONTROLS : net.torvald.terrarum.modulebasegame.ui.UIKeyboardControlPanel
- MENU_LABEL_LANGUAGE : net.torvald.terrarum.modulebasegame.ui.UITitleLanguage
- MENU_MODULES : net.torvald.terrarum.ModOptionsHost
- MENU_LABEL_RETURN
- MENU_LABEL_RETURN+WRITETOCONFIG
- MENU_LABEL_CREDITS
- MENU_LABEL_COPYRIGHT : net.torvald.terrarum.modulebasegame.ui.UITitleCredits
- MENU_CREDIT_GPL_DNT : net.torvald.terrarum.modulebasegame.ui.UITitleGPL3

View 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()
}
}

View File

@@ -51,7 +51,7 @@ object JsonFetcher {
var counter = 0
var entry = map.child
while (entry != null) {
action(entry.name ?: "(arrayindex $counter)", entry)
action(entry.name ?: "$counter", entry)
entry = entry.next
counter += 1
}