@@ -228,6 +228,12 @@ class Downloader(
228228 }
229229
230230 file.close()
231+
232+ // Wait for the file to be found (some random issue on macOS cause the file not to be found immediately)
233+ while (! File (" temp/$name " ).exists()) {
234+ Thread .sleep(100 )
235+ }
236+
231237 onComplete?.invoke()
232238 }
233239 }
@@ -617,19 +623,71 @@ fun unpackGame(version: GameVersion) {
617623 }
618624}
619625
620- fun unpack (path : String , dest : String ) {
621- if (System .getProperty(" os.name" ).startsWith(" Windows" )) {
622- Runtime .getRuntime().exec(arrayOf(" powershell" , " Expand-Archive" , " -Path" , path, " -DestinationPath" , dest)).waitFor()
626+ fun unpack (path : String , dest : String ): Int {
627+ Thread .sleep(1000 )
628+
629+ if (! File (path).absoluteFile.exists()) {
630+ println (" Failed to find $path " )
631+ return - 1
632+ }
633+
634+ if (! File (dest).absoluteFile.exists()) {
635+ if (! File (dest).absoluteFile.mkdirs()) {
636+ println (" Failed to create $dest " )
637+ return - 1
638+ } else {
639+ println (" Created $dest " )
640+ }
641+ } else {
642+ println (" Found $dest " )
643+ }
644+ return if (System .getProperty(" os.name" ).startsWith(" Windows" )) {
645+ val exec = ProcessBuilder (
646+ " powershell" ,
647+ " Expand-Archive" ,
648+ " -Path" ,
649+ File (path).absolutePath,
650+ " -DestinationPath" ,
651+ File (dest).absolutePath
652+ ).inheritIO().start()
653+
654+ val waitFor =
655+ exec.waitFor()
656+
657+ if (waitFor != 0 ) {
658+ for (i in 0 until exec.errorStream.available()) {
659+ print (exec.errorStream.read().toChar())
660+ }
661+ println (" Failed to unpack ${path} " )
662+
663+ JOptionPane .showMessageDialog(null , " Failed to unpack $path " , " Error" , JOptionPane .ERROR_MESSAGE )
664+ Main .playButton.text = " Play"
665+ Main .playButton.enabled = true
666+ }
667+
668+ waitFor
623669 } else if (System .getProperty(" os.name" ).startsWith(" Linux" )) {
624- if (! File (dest).exists()) {
625- File (dest).mkdirs()
670+ val exec = ProcessBuilder (" tar" , " -xvf" , File (path).absolutePath, " -C" , File (dest).absolutePath).inheritIO().start()
671+
672+ val waitFor =
673+ exec.waitFor()
674+
675+ if (waitFor != 0 ) {
676+ println (" Failed to unpack $path " )
626677 }
627- Runtime .getRuntime().exec(arrayOf(" tar" , " -xf" , path, " -C" , dest)).waitFor()
678+
679+ waitFor
628680 } else if (System .getProperty(" os.name" ).startsWith(" Mac" )) {
629- if (! File (dest).exists()) {
630- File (dest).mkdirs()
681+ val exec = ProcessBuilder (" tar" , " -xvf" , File (path).absolutePath, " -C" , File (dest).absolutePath).inheritIO().start()
682+
683+ val waitFor =
684+ exec.waitFor()
685+
686+ if (waitFor != 0 ) {
687+ println (" Failed to unpack $path " )
631688 }
632- Runtime .getRuntime().exec(arrayOf(" tar" , " -xf" , path, " -C" , dest)).waitFor()
689+
690+ waitFor
633691 } else {
634692 throw UnsupportedOperationException ()
635693 }
@@ -748,10 +806,10 @@ object Main : ApplicationAdapter() {
748806 playButton.text = " Play"
749807 if (! File (" jdk" ).exists()) {
750808 playButton.enabled = false
751- download(JDK_URL , " jdk" + if (System .getProperty(" os.name" ).startsWith(" Windows" )) " .zip" else " .tar.gz " , onProgress = {
809+ download(JDK_URL , " jdk" + if (System .getProperty(" os.name" ).startsWith(" Windows" )) " .zip" else " " , onProgress = {
752810 playButton.text = " Downloading JDK (${(it * 100 ).toInt()} %)"
753811 }) {
754- unpack(" temp/jdk" + if (System .getProperty(" os.name" ).startsWith(" Windows" )) " .zip" else " .tar.gz " , " jdk" )
812+ unpack(" temp/jdk" + if (System .getProperty(" os.name" ).startsWith(" Windows" )) " .zip" else " " , " jdk" )
755813
756814 playButton.enabled = true
757815 playButton.text = " Play"
0 commit comments