Add mod folder to mod info when initializing mods so we don't have files pretending to be mods everywhere we try and find mod folders

This commit is contained in:
Yair Morgenstern
2022-02-03 12:15:28 +02:00
parent 89958e27ff
commit 6925fc6909
5 changed files with 16 additions and 13 deletions

View File

@ -66,6 +66,7 @@ class ModOptions : IHasUniques {
class Ruleset {
private val jsonParser = JsonParser()
var folderLocation:FileHandle?=null
var name = ""
val beliefs = LinkedHashMap<String, Belief>()
@ -754,6 +755,7 @@ object RulesetCache : HashMap<String,Ruleset>() {
val modRuleset = Ruleset()
modRuleset.load(modFolder.child("jsons"), printOutput)
modRuleset.name = modFolder.name()
modRuleset.folderLocation = modFolder
this[modRuleset.name] = modRuleset
if (printOutput) {
println("Mod loaded successfully: " + modRuleset.name)

View File

@ -4,6 +4,7 @@ import com.badlogic.gdx.Gdx
import com.badlogic.gdx.files.FileHandle
import com.unciv.JsonParser
import com.unciv.UncivGame
import com.unciv.models.ruleset.RulesetCache
import com.unciv.ui.utils.ImageGetter
object TileSetCache : HashMap<String, TileSetConfig>() {
@ -64,8 +65,8 @@ object TileSetCache : HashMap<String, TileSetConfig>() {
//load mod TileSets
val modsHandles =
if (consoleMode) FileHandle("mods").list()
else Gdx.files.local("mods").list()
if (consoleMode) FileHandle("mods").list().toList()
else RulesetCache.values.mapNotNull { it.folderLocation }
for (modFolder in modsHandles) {
val modName = modFolder.name()

View File

@ -2,6 +2,7 @@ package com.unciv.models.translations
import com.badlogic.gdx.Gdx
import com.unciv.UncivGame
import com.unciv.models.ruleset.RulesetCache
import com.unciv.models.ruleset.unique.Unique
import com.unciv.models.stats.Stat
import com.unciv.models.stats.Stats
@ -93,7 +94,7 @@ class Translations : LinkedHashMap<String, TranslationEntry>(){
}
// try to load the translations from the mods
for (modFolder in Gdx.files.local("mods").list()) {
for (modFolder in RulesetCache.values.mapNotNull { it.folderLocation }) {
val modTranslationFile = modFolder.child(translationFileName)
if (modTranslationFile.exists()) {
var translationsForMod = modsWithTranslations[modFolder.name()]

View File

@ -26,8 +26,8 @@ class MapOptionsTable(private val newGameScreen: NewGameScreen): Table() {
private val mapFilesSequence = sequence<FileHandleWrapper> {
yieldAll(MapSaver.getMaps().asSequence().map { FileHandleWrapper(it) })
for (mod in Gdx.files.local("mods").list()) {
val mapsFolder = mod.child("maps")
for (modFolder in RulesetCache.values.mapNotNull { it.folderLocation }) {
val mapsFolder = modFolder.child("maps")
if (mapsFolder.exists())
yieldAll(mapsFolder.list().asSequence().map { FileHandleWrapper(it) })
}

View File

@ -234,7 +234,7 @@ class ModManagementScreen(
}
if (installedMod.modOptions.author.isEmpty()) {
rewriteModOptions(repo, Gdx.files.local("mods").child(repo.name))
rewriteModOptions(repo, installedMod.folderLocation!!)
installedMod.modOptions.author = repo.owner.login
installedMod.modOptions.modSize = repo.size
}
@ -520,7 +520,8 @@ class ModManagementScreen(
syncInstalledSelected(mod.name, mod.button)
refreshInstalledModActions(mod.ruleset!!)
rightSideButton.setText("Delete [${mod.name}]".tr())
rightSideButton.isEnabled = true
// Don't let the player think he can delete Vanilla and G&K rulesets
rightSideButton.isEnabled = mod.ruleset.folderLocation!=null
showModDescription(mod.name)
removeRightSideClickListeners()
rightSideButton.onClick {
@ -528,7 +529,7 @@ class ModManagementScreen(
YesNoPopup(
question = "Are you SURE you want to delete this mod?",
action = {
deleteMod(mod.name)
deleteMod(mod.ruleset)
modActionTable.clear()
rightSideButton.setText("[${mod.name}] was deleted.".tr())
},
@ -539,12 +540,10 @@ class ModManagementScreen(
}
/** Delete a Mod, refresh ruleset cache and update installed mod table */
private fun deleteMod(modName: String) {
val modFileHandle = Gdx.files.local("mods").child(modName)
if (modFileHandle.isDirectory) modFileHandle.deleteDirectory()
else modFileHandle.delete() // This should never happen
private fun deleteMod(mod: Ruleset) {
mod.folderLocation!!.deleteDirectory()
RulesetCache.loadRulesets()
installedModInfo.remove(modName)
installedModInfo.remove(mod.name)
refreshInstalledModTable()
}