modules can now have their own config files

This commit is contained in:
minjaesong
2022-07-03 18:17:50 +09:00
parent 565323ef07
commit f2e690b936
5 changed files with 78 additions and 22 deletions

View File

@@ -0,0 +1,3 @@
{
"gameplay_max_crafting": 100
}

View File

@@ -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. * @return true on successful, false on failure.
@@ -1192,18 +1231,12 @@ public class App implements ApplicationListener {
// make config // make config
for (JsonValue entry = map.child; entry != null; entry = entry.next) { for (JsonValue entry = map.child; entry != null; entry = entry.next) {
gameConfig.set(entry.name, setToGameConfigForced(entry, null);
entry.isArray() ? entry.asDoubleArray() :
entry.isDouble() ? entry.asDouble() :
entry.isBoolean() ? entry.asBoolean() :
entry.isLong() ? entry.asInt() :
entry.asString()
);
} }
return true; 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. // write default config to game dir. Call th.is method again to read config from it.
try { try {
createConfigJson(); createConfigJson();

View File

@@ -111,9 +111,6 @@ object DefaultConfig {
"screenmagnifying" to 1.0, "screenmagnifying" to 1.0,
// TODO move to basegame module's default config!
"basegame:gameplay_max_crafting" to 100
// settings regarding debugger // settings regarding debugger
/*"buildingmakerfavs" to arrayOf( /*"buildingmakerfavs" to arrayOf(

View File

@@ -2,28 +2,26 @@ package net.torvald.terrarum
import com.badlogic.gdx.Gdx import com.badlogic.gdx.Gdx
import com.badlogic.gdx.files.FileHandle 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.App.*
import net.torvald.terrarum.blockproperties.BlockCodex import net.torvald.terrarum.blockproperties.BlockCodex
import net.torvald.terrarum.blockproperties.WireCodex import net.torvald.terrarum.blockproperties.WireCodex
import net.torvald.terrarum.gameitems.GameItem import net.torvald.terrarum.gameitems.GameItem
import net.torvald.terrarum.gameitems.ItemID import net.torvald.terrarum.gameitems.ItemID
import net.torvald.terrarum.itemproperties.CraftingCodex
import net.torvald.terrarum.itemproperties.ItemCodex import net.torvald.terrarum.itemproperties.ItemCodex
import net.torvald.terrarum.itemproperties.MaterialCodex import net.torvald.terrarum.itemproperties.MaterialCodex
import net.torvald.terrarum.langpack.Lang import net.torvald.terrarum.langpack.Lang
import net.torvald.terrarum.serialise.Common
import net.torvald.terrarum.utils.CSVFetcher import net.torvald.terrarum.utils.CSVFetcher
import net.torvald.terrarum.utils.JsonFetcher import net.torvald.terrarum.utils.JsonFetcher
import net.torvald.terrarum.utils.forEachSiblings
import org.apache.commons.codec.digest.DigestUtils import org.apache.commons.codec.digest.DigestUtils
import org.apache.commons.csv.CSVFormat import org.apache.commons.csv.CSVFormat
import org.apache.commons.csv.CSVParser import org.apache.commons.csv.CSVParser
import org.apache.commons.csv.CSVRecord import org.apache.commons.csv.CSVRecord
import java.io.File import java.io.File
import java.io.FileFilter
import java.io.FileInputStream import java.io.FileInputStream
import java.io.FileNotFoundException import java.io.FileNotFoundException
import java.io.FilenameFilter import java.io.IOException
import java.net.MalformedURLException import java.net.MalformedURLException
import java.net.URL import java.net.URL
import java.net.URLClassLoader import java.net.URLClassLoader
@@ -151,10 +149,21 @@ object ModMgr {
modMetadata.load(FileInputStream(file)) modMetadata.load(FileInputStream(file))
if (File("$modDir/$moduleName/$defaultConfigFilename").exists()) { if (File("$modDir/$moduleName/$defaultConfigFilename").exists()) {
val defaultConfig = JsonFetcher("$modDir/$moduleName/$defaultConfigFilename") try {
// read config and store it to the game 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()
}
} }

View File

@@ -11,7 +11,7 @@ object JsonFetcher {
private var jsonString: StringBuffer? = null private var jsonString: StringBuffer? = null
@Throws(java.nio.file.NoSuchFileException::class) @Throws(java.io.IOException::class)
operator fun invoke(jsonFilePath: String): JsonValue { operator fun invoke(jsonFilePath: String): JsonValue {
jsonString = StringBuffer() // reset buffer every time it called jsonString = StringBuffer() // reset buffer every time it called
readJsonFileAsString(jsonFilePath) readJsonFileAsString(jsonFilePath)
@@ -25,7 +25,7 @@ object JsonFetcher {
return JsonReader().parse(jsonString.toString()) return JsonReader().parse(jsonString.toString())
} }
@Throws(java.nio.file.NoSuchFileException::class) @Throws(java.io.IOException::class)
operator fun invoke(jsonFile: java.io.File): JsonValue { operator fun invoke(jsonFile: java.io.File): JsonValue {
jsonString = StringBuffer() // reset buffer every time it called jsonString = StringBuffer() // reset buffer every time it called
readJsonFileAsString(jsonFile.canonicalPath) readJsonFileAsString(jsonFile.canonicalPath)
@@ -39,7 +39,7 @@ object JsonFetcher {
return JsonReader().parse(jsonString.toString()) return JsonReader().parse(jsonString.toString())
} }
@Throws(java.nio.file.NoSuchFileException::class) @Throws(java.io.IOException::class)
private fun readJsonFileAsString(path: String) { private fun readJsonFileAsString(path: String) {
java.nio.file.Files.lines(java.nio.file.FileSystems.getDefault().getPath(path)).forEach( java.nio.file.Files.lines(java.nio.file.FileSystems.getDefault().getPath(path)).forEach(
{ jsonString!!.append(it) } { 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) { fun forEachSiblingsIndexed(map: JsonValue, action: (Int, String, JsonValue) -> Unit) {
var counter = 0 var counter = 0
var entry = map.child 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) 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) fun JsonValue.forEachSiblingsIndexed(action: (Int, String, JsonValue) -> Unit) = JsonFetcher.forEachSiblingsIndexed(this, action)