From 4cf4381f513b2c3d5d105e9fa0e56d9ee8325332 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Wed, 26 Aug 2020 12:39:07 +0300 Subject: [PATCH] Mod download works on Mobile as well! --- .../ui/pickerscreens/ModManagementScreen.kt | 2 +- .../unciv/ui/worldscreen/mainmenu/DropBox.kt | 34 +++++++++++-------- tests/src/com/unciv/testing/BasicTests.kt | 2 +- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/core/src/com/unciv/ui/pickerscreens/ModManagementScreen.kt b/core/src/com/unciv/ui/pickerscreens/ModManagementScreen.kt index f9b0191b7b..04ca870a22 100644 --- a/core/src/com/unciv/ui/pickerscreens/ModManagementScreen.kt +++ b/core/src/com/unciv/ui/pickerscreens/ModManagementScreen.kt @@ -33,7 +33,7 @@ class ModManagementScreen: PickerScreen() { val downloadButton = "Download mod".toTextButton() downloadButton.onClick { val popup = Popup(this) - val textArea = TextArea("https://github.com/...",skin) + val textArea = TextArea("https://github.com/yairm210/Unciv-IV-mod",skin) popup.add(textArea).width(stage.width/2).row() val downloadButton = "Download".toTextButton() downloadButton.onClick { diff --git a/core/src/com/unciv/ui/worldscreen/mainmenu/DropBox.kt b/core/src/com/unciv/ui/worldscreen/mainmenu/DropBox.kt index de9fa26632..19d58b328f 100644 --- a/core/src/com/unciv/ui/worldscreen/mainmenu/DropBox.kt +++ b/core/src/com/unciv/ui/worldscreen/mainmenu/DropBox.kt @@ -151,6 +151,7 @@ object Zip { } } + // 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(url:String, folderFileHandle:FileHandle) { val inputStream = downloadUrl(url) if (inputStream == null) return @@ -158,23 +159,29 @@ object Zip { val tempZipFileHandle = folderFileHandle.child("tempZip.zip") tempZipFileHandle.write(inputStream, false) - extractFolder(tempZipFileHandle.path()) + val unzipDestination = tempZipFileHandle.sibling("tempZip") // folder, not file + extractFolder(tempZipFileHandle, unzipDestination) + val innerFolder = unzipDestination.list().first() // tempZip/-master/ + + val finalDestination = folderFileHandle.child(innerFolder.name().replace("-master","")) + 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()){ + innerFileOrFolder.moveTo(finalDestination) + } + tempZipFileHandle.delete() - val extractedFolder = FileHandle(tempZipFileHandle.pathWithoutExtension()) - val innerFolder = extractedFolder.list().first() - innerFolder.moveTo(folderFileHandle.child(innerFolder.name().replace("-master",""))) - extractedFolder.deleteDirectory() + unzipDestination.deleteDirectory() } // I went through a lot of similar answers that didn't work until I got to this gem by NeilMonday + // (with mild changes to fit the FileHandles) // https://stackoverflow.com/questions/981578/how-to-unzip-files-recursively-in-java - fun extractFolder(zipFile: String) { + fun extractFolder(zipFile: FileHandle, unzipDestination: FileHandle) { println(zipFile) val BUFFER = 2048 - val file = File(zipFile) + val file = zipFile.file() val zip = ZipFile(file) - val newPath = zipFile.substring(0, zipFile.length - 4) - File(newPath).mkdir() + unzipDestination.mkdirs() val zipFileEntries = zip.entries() // Process each entry @@ -182,9 +189,8 @@ object Zip { // grab a zip file entry val entry = zipFileEntries.nextElement() as ZipEntry val currentEntry = entry.name - val destFile = File(newPath, currentEntry) - //destFile = new File(newPath, destFile.getName()); - val destinationParent = destFile.parentFile + val destFile = unzipDestination.child(currentEntry) + val destinationParent = destFile.parent() // create the parent directory structure if needed destinationParent.mkdirs() @@ -196,7 +202,7 @@ object Zip { val data = ByteArray(BUFFER) // write the current file to disk - val fos = FileOutputStream(destFile) + val fos = FileOutputStream(destFile.file()) val dest = BufferedOutputStream(fos, BUFFER) @@ -210,7 +216,7 @@ object Zip { } if (currentEntry.endsWith(".zip")) { // found a zip file, try to open - extractFolder(destFile.absolutePath) + extractFolder(destFile, unzipDestination) } } } diff --git a/tests/src/com/unciv/testing/BasicTests.kt b/tests/src/com/unciv/testing/BasicTests.kt index f48044645d..6dc449eadc 100644 --- a/tests/src/com/unciv/testing/BasicTests.kt +++ b/tests/src/com/unciv/testing/BasicTests.kt @@ -83,7 +83,7 @@ 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"""))