From 6ffbce4ee1c9cee616c57b314773975ca1aabcdc Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Wed, 21 Apr 2021 21:22:55 +0300 Subject: [PATCH] Added "Updated!" next to updated mods in mod management screen --- core/src/com/unciv/models/ruleset/Ruleset.kt | 3 + .../ui/pickerscreens/ModManagementScreen.kt | 57 ++++++++++++++----- .../unciv/ui/worldscreen/mainmenu/DropBox.kt | 11 ++-- 3 files changed, 52 insertions(+), 19 deletions(-) diff --git a/core/src/com/unciv/models/ruleset/Ruleset.kt b/core/src/com/unciv/models/ruleset/Ruleset.kt index 31eeca6bb5..910db69215 100644 --- a/core/src/com/unciv/models/ruleset/Ruleset.kt +++ b/core/src/com/unciv/models/ruleset/Ruleset.kt @@ -33,6 +33,9 @@ class ModOptions { var nationsToRemove = HashSet() var uniques = HashSet() val maxXPfromBarbarians = 30 + + var lastUpdated = "" + var modUrl = "" } class Ruleset { diff --git a/core/src/com/unciv/ui/pickerscreens/ModManagementScreen.kt b/core/src/com/unciv/ui/pickerscreens/ModManagementScreen.kt index 927688881b..83c00f1cb1 100644 --- a/core/src/com/unciv/ui/pickerscreens/ModManagementScreen.kt +++ b/core/src/com/unciv/ui/pickerscreens/ModManagementScreen.kt @@ -5,7 +5,10 @@ import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.scenes.scene2d.ui.TextArea import com.badlogic.gdx.scenes.scene2d.ui.TextButton +import com.badlogic.gdx.utils.Json +import com.unciv.JsonParser import com.unciv.MainMenuScreen +import com.unciv.models.ruleset.ModOptions import com.unciv.models.ruleset.Ruleset import com.unciv.models.ruleset.RulesetCache import com.unciv.models.translations.tr @@ -60,7 +63,16 @@ class ModManagementScreen: PickerScreen() { Gdx.app.postRunnable { for (repo in repoSearch.items) { repo.name = repo.name.replace('-', ' ') - val downloadButton = repo.name.toTextButton() + var downloadButtonText = repo.name + + val existingMod = RulesetCache.values.firstOrNull { it.name == repo.name } + if(existingMod!=null) { + if (existingMod.modOptions.lastUpdated != "" && existingMod.modOptions.lastUpdated != repo.updated_at) + downloadButtonText += " - Updated!" + } + + val downloadButton = downloadButtonText.toTextButton() + downloadButton.onClick { descriptionLabel.setText(repo.description + "\n" + "[${repo.stargazers_count}]✯".tr()) removeRightSideClickListeners() @@ -69,18 +81,13 @@ class ModManagementScreen: PickerScreen() { rightSideButton.onClick { rightSideButton.setText("Downloading...".tr()) rightSideButton.disable() - downloadMod(repo.svn_url, repo.default_branch) { + downloadMod(repo) { rightSideButton.setText("Downloaded!".tr()) } } modActionTable.clear() - modActionTable.add("Open Github page".toTextButton().onClick { - Gdx.net.openURI(repo.html_url) - }).row() - val updateString = "Last updated at: "+ LocalDateTime.parse(repo.updated_at, DateTimeFormatter.ISO_DATE_TIME) - .toLocalDate().toString() - modActionTable.add(updateString.toLabel()) + addModInfoToActionTable(repo.html_url, repo.updated_at) } downloadTable.add(downloadButton).row() } @@ -98,6 +105,20 @@ class ModManagementScreen: PickerScreen() { } } + fun addModInfoToActionTable(repoUrl: String, updatedAt: String) { + if (repoUrl != "") { + modActionTable.add("Open Github page".toTextButton().onClick { + Gdx.net.openURI(repoUrl) + }).row() + } + + if (updatedAt != "") { + val updateString = "Last updated at: " + LocalDateTime.parse(updatedAt, DateTimeFormatter.ISO_DATE_TIME) + .toLocalDate().toString() + modActionTable.add(updateString.toLabel()) + } + } + fun getDownloadButton(): TextButton { val downloadButton = "Download mod from URL".toTextButton() downloadButton.onClick { @@ -108,7 +129,7 @@ class ModManagementScreen: PickerScreen() { actualDownloadButton.onClick { actualDownloadButton.setText("Downloading...".tr()) actualDownloadButton.disable() - downloadMod(textArea.text, "master") { popup.close() } + downloadMod(Github.Repo().apply { html_url=textArea.text; default_branch= "master"}) { popup.close() } } popup.add(actualDownloadButton).row() popup.addCloseButton() @@ -117,22 +138,28 @@ class ModManagementScreen: PickerScreen() { return downloadButton } - fun downloadMod(gitRepoUrl:String, defaultBranch:String, postAction:()->Unit={}){ + fun downloadMod(repo:Github.Repo, postAction:()->Unit={}) { thread { // to avoid ANRs - we've learnt our lesson from previous download-related actions try { - Github.downloadAndExtract(gitRepoUrl, defaultBranch, + val modFolder = Github.downloadAndExtract(repo.html_url, repo.default_branch, Gdx.files.local("mods")) + if (modFolder == null) return@thread + // rewrite modOptions file + val modOptionsFile = modFolder.child("jsons/ModOptions.json") + val modOptions = if (modOptionsFile.exists()) JsonParser().getFromJson(ModOptions::class.java, modOptionsFile) else ModOptions() + modOptions.modUrl = repo.html_url + modOptions.lastUpdated = repo.updated_at + Json().toJson(modOptions, modOptionsFile) Gdx.app.postRunnable { ToastPopup("Downloaded!", this) RulesetCache.loadRulesets() refreshModTable() } - } catch (ex:Exception){ + } catch (ex: Exception) { Gdx.app.postRunnable { ToastPopup("Could not download mod", this) } - } - finally { + } finally { postAction() } } @@ -155,6 +182,8 @@ class ModManagementScreen: PickerScreen() { refreshModActions(mod) }) modActionTable.row() + + addModInfoToActionTable(mod.modOptions.modUrl, mod.modOptions.lastUpdated) } fun refreshModTable(){ diff --git a/core/src/com/unciv/ui/worldscreen/mainmenu/DropBox.kt b/core/src/com/unciv/ui/worldscreen/mainmenu/DropBox.kt index 9cb4f9418c..2fb1b57f4c 100644 --- a/core/src/com/unciv/ui/worldscreen/mainmenu/DropBox.kt +++ b/core/src/com/unciv/ui/worldscreen/mainmenu/DropBox.kt @@ -159,10 +159,10 @@ object Github { } // This took a long time to get just right, so if you're changing this, TEST IT THOROUGHLY on both Desktop and Phone - fun downloadAndExtract(gitRepoUrl:String, defaultBranch:String, folderFileHandle:FileHandle) { + fun downloadAndExtract(gitRepoUrl:String, defaultBranch:String, folderFileHandle:FileHandle): FileHandle? { val zipUrl = "$gitRepoUrl/archive/$defaultBranch.zip" val inputStream = download(zipUrl) - if (inputStream == null) return + if (inputStream == null) return null val tempZipFileHandle = folderFileHandle.child("tempZip.zip") tempZipFileHandle.write(inputStream, false) @@ -170,15 +170,17 @@ object Github { Zip.extractFolder(tempZipFileHandle, unzipDestination) val innerFolder = unzipDestination.list().first() // tempZip/-master/ - val finalDestinationName = innerFolder.name().replace("-$defaultBranch","").replace('-',' ') + val finalDestinationName = innerFolder.name().replace("-$defaultBranch", "").replace('-', ' ') val finalDestination = folderFileHandle.child(finalDestinationName) finalDestination.mkdirs() // If we don't create this as a directory, it will think this is a file and nothing will work. - for(innerFileOrFolder in innerFolder.list()){ + for (innerFileOrFolder in innerFolder.list()) { innerFileOrFolder.moveTo(finalDestination) } tempZipFileHandle.delete() unzipDestination.deleteDirectory() + + return finalDestination } @@ -196,7 +198,6 @@ object Github { class Repo { var name = "" var description = "" - var svn_url = "" var stargazers_count = 0 var default_branch = "" var html_url = ""