diff --git a/core/src/com/unciv/ui/pickerscreens/GitHub.kt b/core/src/com/unciv/ui/pickerscreens/GitHub.kt index 76d376d7fe..98d8b9a663 100644 --- a/core/src/com/unciv/ui/pickerscreens/GitHub.kt +++ b/core/src/com/unciv/ui/pickerscreens/GitHub.kt @@ -6,9 +6,18 @@ import com.unciv.json.fromJsonFile import com.unciv.json.json import com.unciv.logic.BackwardCompatibility.updateDeprecations import com.unciv.models.ruleset.ModOptions +import com.unciv.ui.pickerscreens.Github.RateLimit +import com.unciv.ui.pickerscreens.Github.download +import com.unciv.ui.pickerscreens.Github.downloadAndExtract +import com.unciv.ui.pickerscreens.Github.tryGetGithubReposWithTopic import com.unciv.utils.Log import com.unciv.utils.debug -import java.io.* +import java.io.BufferedInputStream +import java.io.BufferedOutputStream +import java.io.BufferedReader +import java.io.FileOutputStream +import java.io.InputStream +import java.io.InputStreamReader import java.net.HttpURLConnection import java.net.URL import java.util.zip.ZipEntry @@ -52,17 +61,16 @@ object Github { /** * Download a mod and extract, deleting any pre-existing version. - * @param gitRepoUrl Url of the repository as delivered by the Github search query - * @param defaultBranch Branch name as delivered by the Github search query * @param folderFileHandle Destination handle of mods folder - also controls Android internal/external * @author **Warning**: This took a long time to get just right, so if you're changing this, ***TEST IT THOROUGHLY*** on _both_ Desktop _and_ Phone * @return FileHandle for the downloaded Mod's folder or null if download failed */ fun downloadAndExtract( - gitRepoUrl: String, - defaultBranch: String, + repo: Repo, folderFileHandle: FileHandle ): FileHandle? { + val defaultBranch = repo.default_branch + val gitRepoUrl = repo.html_url // Initiate download - the helper returns null when it fails val zipUrl = "$gitRepoUrl/archive/$defaultBranch.zip" val inputStream = download(zipUrl) ?: return null @@ -299,8 +307,9 @@ object Github { } else { val matchRepo = Regex("""^.*/(.*)/(.*)/?$""").matchEntire(url) if (matchRepo != null && matchRepo.groups.size > 2) { - owner.login = matchRepo.groups[1]!!.value - name = matchRepo.groups[2]!!.value + val repoString = download("https://api.github.com/repos/${matchRepo.groups[1]!!.value}/${matchRepo.groups[2]!!.value}")!! + .bufferedReader().readText() + return json().fromJson(Repo::class.java, repoString) } } return this diff --git a/core/src/com/unciv/ui/pickerscreens/ModManagementScreen.kt b/core/src/com/unciv/ui/pickerscreens/ModManagementScreen.kt index 4c2a0e93bc..2683f34e11 100644 --- a/core/src/com/unciv/ui/pickerscreens/ModManagementScreen.kt +++ b/core/src/com/unciv/ui/pickerscreens/ModManagementScreen.kt @@ -425,7 +425,7 @@ class ModManagementScreen( private fun downloadMod(repo: Github.Repo, postAction: () -> Unit = {}) { Concurrency.run("DownloadMod") { // to avoid ANRs - we've learnt our lesson from previous download-related actions try { - val modFolder = Github.downloadAndExtract(repo.html_url, repo.default_branch, + val modFolder = Github.downloadAndExtract(repo, Gdx.files.local("mods")) ?: throw Exception() // downloadAndExtract returns null for 404 errors and the like -> display something! Github.rewriteModOptions(repo, modFolder) diff --git a/core/src/com/unciv/ui/saves/LoadGameScreen.kt b/core/src/com/unciv/ui/saves/LoadGameScreen.kt index be9f519447..59bf0d2201 100644 --- a/core/src/com/unciv/ui/saves/LoadGameScreen.kt +++ b/core/src/com/unciv/ui/saves/LoadGameScreen.kt @@ -7,8 +7,8 @@ import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.scenes.scene2d.ui.TextButton import com.badlogic.gdx.utils.SerializationException import com.unciv.Constants -import com.unciv.logic.UncivFiles import com.unciv.logic.MissingModsException +import com.unciv.logic.UncivFiles import com.unciv.logic.UncivShowableException import com.unciv.models.ruleset.RulesetCache import com.unciv.models.translations.tr @@ -240,7 +240,7 @@ class LoadGameScreen(previousScreen:BaseScreen) : LoadOrSaveScreen() { val repo = repos.items.firstOrNull { it.name.lowercase() == modName } ?: throw UncivShowableException("Could not find a mod named \"[$modName]\".") val modFolder = Github.downloadAndExtract( - repo.html_url, repo.default_branch, + repo, Gdx.files.local("mods") ) ?: throw Exception() // downloadAndExtract returns null for 404 errors and the like -> display something!