diff --git a/core/src/com/unciv/JsonParser.kt b/core/src/com/unciv/JsonParser.kt index d3af7c41f9..794e6f9b7d 100644 --- a/core/src/com/unciv/JsonParser.kt +++ b/core/src/com/unciv/JsonParser.kt @@ -1,14 +1,17 @@ package com.unciv import com.badlogic.gdx.Gdx +import com.badlogic.gdx.files.FileHandle import com.badlogic.gdx.utils.Json class JsonParser { private val json = Json().apply { ignoreUnknownFields = true } - fun getFromJson(tClass: Class, filePath: String): T { - val jsonText = Gdx.files.internal(filePath).readString(Charsets.UTF_8.name()) + fun getFromJson(tClass: Class, filePath: String): T = getFromJson(tClass, Gdx.files.internal(filePath)) + + fun getFromJson(tClass: Class, file: FileHandle): T { + val jsonText = file.readString(Charsets.UTF_8.name()) return json.fromJson(tClass, jsonText) } } \ No newline at end of file diff --git a/core/src/com/unciv/models/ruleset/Ruleset.kt b/core/src/com/unciv/models/ruleset/Ruleset.kt index 8e60b0d7fe..d86ef88554 100644 --- a/core/src/com/unciv/models/ruleset/Ruleset.kt +++ b/core/src/com/unciv/models/ruleset/Ruleset.kt @@ -1,5 +1,7 @@ package com.unciv.models.ruleset +import com.badlogic.gdx.Gdx +import com.badlogic.gdx.files.FileHandle import com.unciv.JsonParser import com.unciv.models.ruleset.tech.TechColumn import com.unciv.models.ruleset.tech.Technology @@ -36,7 +38,7 @@ class Ruleset(load: Boolean = true) { init { if (load) { - load() + load(Gdx.files.internal("jsons")) } } @@ -75,46 +77,69 @@ class Ruleset(load: Boolean = true) { units.clear() } - fun load(folderPath: String = "jsons") { + fun load(folderHandle :FileHandle ) { val gameBasicsStartTime = System.currentTimeMillis() - val techColumns = jsonParser.getFromJson(Array::class.java, "$folderPath/Techs.json") - for (techColumn in techColumns) { - for (tech in techColumn.techs) { - if (tech.cost == 0) tech.cost = techColumn.techCost - tech.column = techColumn - technologies[tech.name] = tech + + val techFile =folderHandle.child("Techs.json") + if(techFile.exists()) { + val techColumns = jsonParser.getFromJson(Array::class.java, techFile) + for (techColumn in techColumns) { + for (tech in techColumn.techs) { + if (tech.cost == 0) tech.cost = techColumn.techCost + tech.column = techColumn + technologies[tech.name] = tech + } } } - buildings += createHashmap(jsonParser.getFromJson(Array::class.java, "$folderPath/Buildings.json")) - for (building in buildings.values) { - if (building.requiredTech == null) continue - val column = technologies[building.requiredTech!!]!!.column - if (building.cost == 0) - building.cost = if (building.isWonder || building.isNationalWonder) column!!.wonderCost else column!!.buildingCost - } - - terrains += createHashmap(jsonParser.getFromJson(Array::class.java, "$folderPath/Terrains.json")) - tileResources += createHashmap(jsonParser.getFromJson(Array::class.java, "$folderPath/TileResources.json")) - tileImprovements += createHashmap(jsonParser.getFromJson(Array::class.java, "$folderPath/TileImprovements.json")) - units += createHashmap(jsonParser.getFromJson(Array::class.java, "$folderPath/Units.json")) - unitPromotions += createHashmap(jsonParser.getFromJson(Array::class.java, "$folderPath/UnitPromotions.json")) - - policyBranches += createHashmap(jsonParser.getFromJson(Array::class.java, "$folderPath/Policies.json")) - for (branch in policyBranches.values) { - branch.requires = ArrayList() - branch.branch = branch - for (policy in branch.policies) { - policy.branch = branch - if (policy.requires == null) policy.requires = arrayListOf(branch.name) + val buildingsFile = folderHandle.child("Buildings.json") + if(buildingsFile.exists()) { + buildings += createHashmap(jsonParser.getFromJson(Array::class.java, buildingsFile)) + for (building in buildings.values) { + if (building.requiredTech == null) continue + val column = technologies[building.requiredTech!!]!!.column + if (building.cost == 0) + building.cost = if (building.isWonder || building.isNationalWonder) column!!.wonderCost else column!!.buildingCost } - branch.policies.last().name = branch.name + " Complete" } - nations += createHashmap(jsonParser.getFromJson(Array::class.java, "$folderPath/Nations/Nations.json")) - for (nation in nations.values) nation.setTransients() + val terrainsFile = folderHandle.child("Terrains.json") + if(terrainsFile.exists()) terrains += createHashmap(jsonParser.getFromJson(Array::class.java, terrainsFile)) - difficulties += createHashmap(jsonParser.getFromJson(Array::class.java, "$folderPath/Difficulties.json")) + val resourcesFile = folderHandle.child("TileResources.json") + if(resourcesFile.exists()) tileResources += createHashmap(jsonParser.getFromJson(Array::class.java, resourcesFile)) + + val improvementsFile = folderHandle.child("TileImprovements.json") + if(improvementsFile.exists()) tileImprovements += createHashmap(jsonParser.getFromJson(Array::class.java, improvementsFile)) + + val unitsFile = folderHandle.child("Units.json") + if(unitsFile.exists()) units += createHashmap(jsonParser.getFromJson(Array::class.java, unitsFile)) + + val promotionsFile = folderHandle.child("UnitPromotions.json") + if(promotionsFile.exists()) unitPromotions += createHashmap(jsonParser.getFromJson(Array::class.java, promotionsFile)) + + val policiesFile = folderHandle.child("Policies.json") + if(policiesFile.exists()) { + policyBranches += createHashmap(jsonParser.getFromJson(Array::class.java, policiesFile)) + for (branch in policyBranches.values) { + branch.requires = ArrayList() + branch.branch = branch + for (policy in branch.policies) { + policy.branch = branch + if (policy.requires == null) policy.requires = arrayListOf(branch.name) + } + branch.policies.last().name = branch.name + " Complete" + } + } + + val nationsFile = folderHandle.child("Nations/Nations.json") + if(nationsFile.exists()) { + nations += createHashmap(jsonParser.getFromJson(Array::class.java, nationsFile)) + for (nation in nations.values) nation.setTransients() + } + + val difficultiesFile = folderHandle.child("Difficulties.json") + if(difficultiesFile.exists()) difficulties += createHashmap(jsonParser.getFromJson(Array::class.java, difficultiesFile)) val gameBasicsLoadTime = System.currentTimeMillis() - gameBasicsStartTime println("Loading game basics - " + gameBasicsLoadTime + "ms") diff --git a/core/src/com/unciv/ui/newgamescreen/NewGameScreenOptionsTable.kt b/core/src/com/unciv/ui/newgamescreen/NewGameScreenOptionsTable.kt index 1313df5e39..871bab0054 100644 --- a/core/src/com/unciv/ui/newgamescreen/NewGameScreenOptionsTable.kt +++ b/core/src/com/unciv/ui/newgamescreen/NewGameScreenOptionsTable.kt @@ -38,7 +38,7 @@ class NewGameScreenOptionsTable(val newGameScreen: NewGameScreen, val updatePlay addOneCityChallengeCheckbox() addIsOnlineMultiplayerCheckbox() - // addModCheckboxes() + addModCheckboxes() pack() } @@ -222,21 +222,18 @@ class NewGameScreenOptionsTable(val newGameScreen: NewGameScreen, val updatePlay fun addModCheckboxes() { - add("{Mods}:".tr()).colspan(2).row() - - val modCheckboxTable = Table().apply { defaults().pad(10f) } - val modFolders = Gdx.files.local("mods") val loadableMods = ArrayList() for (modFolder in modFolders.list()) { if (modFolder.list().any { it.name() == "jsons" }) { - val ruleSet = Ruleset(false) + val modRuleset = Ruleset(false) try { - ruleSet.load(modFolder.path() + "/jsons") - ruleSet.name = modFolder.nameWithoutExtension() - loadableMods.add(ruleset) + modRuleset.load(modFolder.child("jsons")) + modRuleset.name = modFolder.nameWithoutExtension() + loadableMods.add(modRuleset) + } catch (ex: Exception) { print(ex.message) } @@ -252,6 +249,11 @@ class NewGameScreenOptionsTable(val newGameScreen: NewGameScreen, val updatePlay } } + if(loadableMods.isEmpty()) return + + + add("{Mods}:".tr()).colspan(2).row() + val modCheckboxTable = Table().apply { defaults().pad(10f) } for(mod in loadableMods){ val checkBox = CheckBox(mod.name,CameraStageBaseScreen.skin) checkBox.addListener(object : ChangeListener() { @@ -262,21 +264,10 @@ class NewGameScreenOptionsTable(val newGameScreen: NewGameScreen, val updatePlay updatePlayerPickerTable() } }) + modCheckboxTable.add(checkBox) } add(modCheckboxTable).colspan(2).row() } } - -// -//class Mod(val name:String){ -// val ruleSet=Ruleset(false) -// -// fun tryLoadRuleset(){ -// val folderPath="mods/$name" -// val jsonsFolderLocation = folderPath+"/jsons" -// if(Gdx.files.local(jsonsFolderLocation).exists()) -// -// } -//} \ No newline at end of file diff --git a/desktop/build.gradle b/desktop/build.gradle index 2440114d1c..e111ceb67b 100644 --- a/desktop/build.gradle +++ b/desktop/build.gradle @@ -51,8 +51,8 @@ for(platform in PackrConfig.Platform.values()) { PackrConfig config = new PackrConfig() config.platform = platform - // For Travis - if(true) { // change to false for local build + def forTravis = true // change to false for local build + if(forTravis) { if (platform == PackrConfig.Platform.Linux32 || platform == PackrConfig.Platform.Linux64) config.jdk = System.env.'JAVA_HOME' // take the jdk straight from the building linux computer