From f2e690b93635592e21e9a286db1fe78f4f43c358 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Sun, 3 Jul 2022 18:17:50 +0900 Subject: [PATCH] modules can now have their own config files --- assets/mods/basegame/default.json | 3 ++ src/net/torvald/terrarum/App.java | 49 ++++++++++++++++--- src/net/torvald/terrarum/DefaultConfig.kt | 3 -- src/net/torvald/terrarum/ModMgr.kt | 25 +++++++--- src/net/torvald/terrarum/utils/JsonFetcher.kt | 20 ++++++-- 5 files changed, 78 insertions(+), 22 deletions(-) create mode 100644 assets/mods/basegame/default.json diff --git a/assets/mods/basegame/default.json b/assets/mods/basegame/default.json new file mode 100644 index 000000000..44679b8e4 --- /dev/null +++ b/assets/mods/basegame/default.json @@ -0,0 +1,3 @@ +{ + "gameplay_max_crafting": 100 +} \ No newline at end of file diff --git a/src/net/torvald/terrarum/App.java b/src/net/torvald/terrarum/App.java index cc5996846..dd4b819ce 100644 --- a/src/net/torvald/terrarum/App.java +++ b/src/net/torvald/terrarum/App.java @@ -1181,6 +1181,45 @@ public class App implements ApplicationListener { } } + /** + * Will forcibly overwrite previously loaded config value. + * + * Key naming convention will be 'modName:propertyName'; if modName is null, the key will be just propertyName. + * + * @param value JsonValue (the key-value pair) + * @param modName module name, nullable + */ + public static void setToGameConfigForced(JsonValue value, String modName) { + gameConfig.set((modName == null) ? value.name : modName+":"+value.name, + value.isArray() ? value.asDoubleArray() : + value.isDouble() ? value.asDouble() : + value.isBoolean() ? value.asBoolean() : + value.isLong() ? value.asInt() : + value.asString() + ); + } + + /** + * Will not overwrite previously loaded config value. + * + * Key naming convention will be 'modName:propertyName'; if modName is null, the key will be just propertyName. + * + * @param value JsonValue (the key-value pair) + * @param modName module name, nullable + */ + public static void setToGameConfig(JsonValue value, String modName) { + String key = (modName == null) ? value.name : modName+":"+value.name; + if (gameConfig.get(key) == null) { + gameConfig.set(key, + value.isArray() ? value.asDoubleArray() : + value.isDouble() ? value.asDouble() : + value.isBoolean() ? value.asBoolean() : + value.isLong() ? value.asInt() : + value.asString() + ); + } + } + /** * * @return true on successful, false on failure. @@ -1192,18 +1231,12 @@ public class App implements ApplicationListener { // make config for (JsonValue entry = map.child; entry != null; entry = entry.next) { - gameConfig.set(entry.name, - entry.isArray() ? entry.asDoubleArray() : - entry.isDouble() ? entry.asDouble() : - entry.isBoolean() ? entry.asBoolean() : - entry.isLong() ? entry.asInt() : - entry.asString() - ); + setToGameConfigForced(entry, null); } return true; } - catch (java.nio.file.NoSuchFileException e) { + catch (IOException e) { // write default config to game dir. Call th.is method again to read config from it. try { createConfigJson(); diff --git a/src/net/torvald/terrarum/DefaultConfig.kt b/src/net/torvald/terrarum/DefaultConfig.kt index 4e90ec6eb..b5d1f28c8 100644 --- a/src/net/torvald/terrarum/DefaultConfig.kt +++ b/src/net/torvald/terrarum/DefaultConfig.kt @@ -111,9 +111,6 @@ object DefaultConfig { "screenmagnifying" to 1.0, - // TODO move to basegame module's default config! - "basegame:gameplay_max_crafting" to 100 - // settings regarding debugger /*"buildingmakerfavs" to arrayOf( diff --git a/src/net/torvald/terrarum/ModMgr.kt b/src/net/torvald/terrarum/ModMgr.kt index 66a291e45..5fca49d55 100644 --- a/src/net/torvald/terrarum/ModMgr.kt +++ b/src/net/torvald/terrarum/ModMgr.kt @@ -2,28 +2,26 @@ package net.torvald.terrarum import com.badlogic.gdx.Gdx import com.badlogic.gdx.files.FileHandle -import com.badlogic.gdx.graphics.g2d.SpriteBatch +import com.badlogic.gdx.utils.JsonValue import net.torvald.terrarum.App.* import net.torvald.terrarum.blockproperties.BlockCodex import net.torvald.terrarum.blockproperties.WireCodex import net.torvald.terrarum.gameitems.GameItem import net.torvald.terrarum.gameitems.ItemID -import net.torvald.terrarum.itemproperties.CraftingCodex import net.torvald.terrarum.itemproperties.ItemCodex import net.torvald.terrarum.itemproperties.MaterialCodex import net.torvald.terrarum.langpack.Lang -import net.torvald.terrarum.serialise.Common import net.torvald.terrarum.utils.CSVFetcher import net.torvald.terrarum.utils.JsonFetcher +import net.torvald.terrarum.utils.forEachSiblings import org.apache.commons.codec.digest.DigestUtils import org.apache.commons.csv.CSVFormat import org.apache.commons.csv.CSVParser import org.apache.commons.csv.CSVRecord import java.io.File -import java.io.FileFilter import java.io.FileInputStream import java.io.FileNotFoundException -import java.io.FilenameFilter +import java.io.IOException import java.net.MalformedURLException import java.net.URL import java.net.URLClassLoader @@ -151,10 +149,21 @@ object ModMgr { modMetadata.load(FileInputStream(file)) if (File("$modDir/$moduleName/$defaultConfigFilename").exists()) { - val defaultConfig = JsonFetcher("$modDir/$moduleName/$defaultConfigFilename") - // read config and store it to the game + try { + val defaultConfig = JsonFetcher("$modDir/$moduleName/$defaultConfigFilename") - // write to user's config file + // read config and store it to the game + var entry: JsonValue? = defaultConfig.child + while (entry != null) { + setToGameConfig(entry, moduleName) + entry = entry.next + } // copied from App.java + + // write to user's config file + } + catch (e: IOException) { + e.printStackTrace() + } } diff --git a/src/net/torvald/terrarum/utils/JsonFetcher.kt b/src/net/torvald/terrarum/utils/JsonFetcher.kt index 0aa4de7b7..3f770f281 100644 --- a/src/net/torvald/terrarum/utils/JsonFetcher.kt +++ b/src/net/torvald/terrarum/utils/JsonFetcher.kt @@ -11,7 +11,7 @@ object JsonFetcher { private var jsonString: StringBuffer? = null - @Throws(java.nio.file.NoSuchFileException::class) + @Throws(java.io.IOException::class) operator fun invoke(jsonFilePath: String): JsonValue { jsonString = StringBuffer() // reset buffer every time it called readJsonFileAsString(jsonFilePath) @@ -25,7 +25,7 @@ object JsonFetcher { return JsonReader().parse(jsonString.toString()) } - @Throws(java.nio.file.NoSuchFileException::class) + @Throws(java.io.IOException::class) operator fun invoke(jsonFile: java.io.File): JsonValue { jsonString = StringBuffer() // reset buffer every time it called readJsonFileAsString(jsonFile.canonicalPath) @@ -39,7 +39,7 @@ object JsonFetcher { return JsonReader().parse(jsonString.toString()) } - @Throws(java.nio.file.NoSuchFileException::class) + @Throws(java.io.IOException::class) private fun readJsonFileAsString(path: String) { java.nio.file.Files.lines(java.nio.file.FileSystems.getDefault().getPath(path)).forEach( { jsonString!!.append(it) } @@ -63,6 +63,12 @@ object JsonFetcher { } } + /** + * Iterates [JsonValue] over its siblings. + * + * @param map JsonValue to iterate over + * @param action A `function(index, `Name of the sibling or a stringified integer if the `map` is an array`, `JsonValue representation of the sibling`)` -> `Unit` + */ fun forEachSiblingsIndexed(map: JsonValue, action: (Int, String, JsonValue) -> Unit) { var counter = 0 var entry = map.child @@ -74,5 +80,13 @@ object JsonFetcher { } } +/** + * Iterates [JsonValue] over its siblings. + * @param action A function(`Name of the sibling or a stringified integer if the `map` is an array`, `JsonValue representation of the sibling`)` -> `Unit` + */ fun JsonValue.forEachSiblings(action: (String, JsonValue) -> Unit) = JsonFetcher.forEachSiblings(this, action) +/** + * Iterates [JsonValue] over its siblings. + * @param action A `function(index, `Name of the sibling or a stringified integer if the `map` is an array`, `JsonValue representation of the sibling`)` -> `Unit` + */ fun JsonValue.forEachSiblingsIndexed(action: (Int, String, JsonValue) -> Unit) = JsonFetcher.forEachSiblingsIndexed(this, action) \ No newline at end of file