Resolved #7989 - Get entire repo data for manually downloaded mods

This commit is contained in:
Yair Morgenstern 2022-11-07 20:13:37 +02:00
parent b0c84f5eea
commit f779221f93
3 changed files with 19 additions and 10 deletions

View File

@ -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

View File

@ -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)

View File

@ -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!