diff --git a/core/src/com/unciv/ui/pickerscreens/ModManagementScreen.kt b/core/src/com/unciv/ui/pickerscreens/ModManagementScreen.kt index ba45148b..f9b0191b 100644 --- a/core/src/com/unciv/ui/pickerscreens/ModManagementScreen.kt +++ b/core/src/com/unciv/ui/pickerscreens/ModManagementScreen.kt @@ -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(){ diff --git a/core/src/com/unciv/ui/worldscreen/mainmenu/DropBox.kt b/core/src/com/unciv/ui/worldscreen/mainmenu/DropBox.kt index 750ed69d..de9fa266 100644 --- a/core/src/com/unciv/ui/worldscreen/mainmenu/DropBox.kt +++ b/core/src/com/unciv/ui/worldscreen/mainmenu/DropBox.kt @@ -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 diff --git a/docs/Credits.md b/docs/Credits.md index 5f2563ea..75c4e14b 100644 --- a/docs/Credits.md +++ b/docs/Credits.md @@ -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 diff --git a/tests/src/com/unciv/testing/BasicTests.kt b/tests/src/com/unciv/testing/BasicTests.kt index fd5d1af9..f4804464 100644 --- a/tests/src/com/unciv/testing/BasicTests.kt +++ b/tests/src/com/unciv/testing/BasicTests.kt @@ -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""")) // } } \ No newline at end of file