Can now download mods from Github repos! :D

This commit is contained in:
Yair Morgenstern 2020-08-25 22:00:38 +03:00
parent 353b6e71f8
commit 247227bdaa
4 changed files with 75 additions and 7 deletions

View file

@ -2,22 +2,65 @@ package com.unciv.ui.pickerscreens
import com.badlogic.gdx.Gdx
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.scenes.scene2d.utils.ClickListener
import com.unciv.MainMenuScreen
import com.unciv.models.ruleset.Ruleset
import com.unciv.models.ruleset.RulesetCache
import com.unciv.models.translations.tr
import com.unciv.ui.utils.*
import com.unciv.ui.worldscreen.mainmenu.Zip
import kotlin.concurrent.thread
class ModManagementScreen: PickerScreen() {
val modTable = Table().apply { defaults().pad(10f) }
val downloadTable = Table()
init {
setDefaultCloseAction(MainMenuScreen())
refresh()
topTable.add(modTable)
topTable.add(modTable).pad(10f)
downloadTable.add(getDownloadButton())
topTable.add(downloadTable)
}
fun getDownloadButton(): TextButton {
val downloadButton = "Download mod".toTextButton()
downloadButton.onClick {
val popup = Popup(this)
val textArea = TextArea("https://github.com/...",skin)
popup.add(textArea).width(stage.width/2).row()
val downloadButton = "Download".toTextButton()
downloadButton.onClick {
thread { // to avoid ANRs - we've learnt our lesson from previous download-related actions
try {
downloadButton.setText("Downloading...")
downloadButton.disable()
Zip.downloadAndExtract(textArea.text+"/archive/master.zip",
Gdx.files.local("mods"))
Gdx.app.postRunnable {
RulesetCache.loadRulesets()
refresh()
popup.close()
}
} catch (ex:Exception){
Gdx.app.postRunnable {
ResponsePopup("Could not download mod", this)
popup.close()
}
}
}
}
popup.add(downloadButton).row()
popup.addCloseButton()
popup.open()
}
return downloadButton
}
fun refresh(){

View file

@ -1,6 +1,5 @@
package com.unciv.ui.worldscreen.mainmenu
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.files.FileHandle
import com.unciv.logic.GameInfo
import com.unciv.logic.GameSaver
@ -135,11 +134,36 @@ class OnlineMultiplayer {
object Zip {
fun downloadAndExtract(dropboxFileLocation:String, folderFileHandle:FileHandle){
val fileFromDropbox = DropBox.downloadFile(dropboxFileLocation)
fun downloadUrl(url:String): InputStream? {
with(URL(url).openConnection() as HttpURLConnection) {
// requestMethod = "POST" // default is GET
doOutput = true
try {
return inputStream
} catch (ex: Exception) {
println(ex.message)
val reader = BufferedReader(InputStreamReader(errorStream))
println(reader.readText())
return null
}
}
}
fun downloadAndExtract(url:String, folderFileHandle:FileHandle) {
val inputStream = downloadUrl(url)
if (inputStream == null) return
//DropBox.downloadFile(dropboxFileLocation)
val tempZipFileHandle = folderFileHandle.child("tempZip.zip")
tempZipFileHandle.write(fileFromDropbox, false)
tempZipFileHandle.write(inputStream, false)
extractFolder(tempZipFileHandle.path())
tempZipFileHandle.delete()
val extractedFolder = FileHandle(tempZipFileHandle.pathWithoutExtension())
val innerFolder = extractedFolder.list().first()
innerFolder.moveTo(folderFileHandle.child(innerFolder.name().replace("-master","")))
extractedFolder.deleteDirectory()
}
// I went through a lot of similar answers that didn't work until I got to this gem by NeilMonday

View file

@ -518,7 +518,7 @@ Unless otherwise specified, all the following are from [the Noun Project](https:
* [Puppet](https://thenounproject.com/search/?q=puppet&i=285735) By Ben Davis for puppeted cities
* [City](https://thenounproject.com/search/?q=city&i=1765370) By Muhajir ila Robbi in the Icon center
* [Lock](https://thenounproject.com/search/?q=lock&i=3217613) by Vadim Solomakhin for locked tiles
* [Turn](https://thenounproject.com/search/?q=turn&i=2829863) by Adrien Coquet for the 'Turn' icon
* [Hourglass](https://thenounproject.com/search/?q=hourglass&i=142268) by I Create Stuff for the 'Turn' icon
## Main menu

View file

@ -83,9 +83,10 @@ class BasicTests {
print(x)
}
// @Test // This should NOT run as part of the test suite!
@Test // This should NOT run as part of the test suite!
// fun tryUnzip(){
// Zip.extractFolder("""C:\Users\LENOVO\Downloads\Rebuild.rar""")
// Zip.downloadAndExtract("/Mods/Reasoures.zip", FileHandle("""C:\Users\LENOVO\Downloads"""))
// Zip.downloadAndExtract("https://github.com/yairm210/Unciv-IV-mod/archive/master.zip", FileHandle("""C:\Users\LENOVO\Downloads"""))
// }
}