From 8fd259183bba700d2dba5c2db784cd59448b907d Mon Sep 17 00:00:00 2001 From: ceikry Date: Fri, 16 Jul 2021 10:19:25 -0500 Subject: [PATCH] v1.1 - More UI responsiveness, increased download buffer --- build.gradle | 2 +- src/main/kotlin/Extensions.kt | 6 ------ src/main/kotlin/Globals.kt | 13 +++++++++++++ src/main/kotlin/MainWindow.kt | 26 ++++++++++++++++++++++++-- src/main/kotlin/Updater.kt | 27 ++++++++++++++++++--------- 5 files changed, 56 insertions(+), 18 deletions(-) delete mode 100644 src/main/kotlin/Extensions.kt create mode 100644 src/main/kotlin/Globals.kt diff --git a/build.gradle b/build.gradle index f45880c..f007f90 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ plugins { } group 'org.rs09' -version '1.0' +version '1.1' repositories { mavenCentral() diff --git a/src/main/kotlin/Extensions.kt b/src/main/kotlin/Extensions.kt deleted file mode 100644 index bcbb469..0000000 --- a/src/main/kotlin/Extensions.kt +++ /dev/null @@ -1,6 +0,0 @@ -import java.awt.Component - -fun Component.placeAt(x: Int, y: Int, width: Int, height: Int){ - this.setBounds(x,y,width,height) - if(this is ImgButton) this.scale(width,height) -} \ No newline at end of file diff --git a/src/main/kotlin/Globals.kt b/src/main/kotlin/Globals.kt new file mode 100644 index 0000000..2f142a6 --- /dev/null +++ b/src/main/kotlin/Globals.kt @@ -0,0 +1,13 @@ +import java.awt.Component +import java.io.File +import kotlin.system.exitProcess + +fun Component.placeAt(x: Int, y: Int, width: Int, height: Int){ + this.setBounds(x,y,width,height) + if(this is ImgButton) this.scale(width,height) +} + +fun launchClient() { + println("Launching client now.") + Runtime.getRuntime().exec("java -jar " + Settings.SAVE_DIR + File.separator + Settings.SAVE_NAME, null, File(System.getProperty("user.home"))).also { exitProcess(0) } +} \ No newline at end of file diff --git a/src/main/kotlin/MainWindow.kt b/src/main/kotlin/MainWindow.kt index 076e7a9..4a7d905 100644 --- a/src/main/kotlin/MainWindow.kt +++ b/src/main/kotlin/MainWindow.kt @@ -50,8 +50,30 @@ object MainWindow : JFrame("2009scape Launcher") { val loadingFrame = JLabel(ImageIcon(javaClass.getResource("/loadingFrame.png"))) playButton.isEnabled = true playButton.onClick { - if(Updater.checkUpdate()) Updater.runUpdate() - else Runtime.getRuntime().exec("java -jar " + Settings.SAVE_DIR + File.separator + Settings.SAVE_NAME, null, File(System.getProperty("user.home"))).also { exitProcess(0) } + val t = Thread { + val oldText = loadingLabel.text + loadingLabel.text = "Checking for updates" + playButton.isEnabled = false + var counter = 0 + var dotCounter = 0 + while(Updater.status == Updater.UpdateStatus.CHECKING){ + if(counter++ % 5 == 0){ + dotCounter++ + loadingLabel.text = "Checking for updates${".".repeat((dotCounter % 4) + 1)}" + } + Thread.sleep(50L) + } + if(Updater.remoteMD5 != "-1" && Updater.remoteMD5 != Updater.localMD5){ + println("Update required, running update...") + loadingLabel.text = oldText + Updater.runUpdate() + } else { + loadingLabel.text = oldText + playButton.isEnabled = true + launchClient() + } + }.start() + Thread { Updater.checkUpdate() }.start() } loadingFrame.placeAt(96,MainWindow.height - 35, 704, 35) loadingBar.placeAt(103, MainWindow.height - 33, 695, 31) diff --git a/src/main/kotlin/Updater.kt b/src/main/kotlin/Updater.kt index f036681..1a578ae 100644 --- a/src/main/kotlin/Updater.kt +++ b/src/main/kotlin/Updater.kt @@ -7,13 +7,16 @@ import javax.swing.ImageIcon object Updater { - private var status = UpdateStatus.DOWNLOADING + var status = UpdateStatus.CHECKING + var localMD5 = "-1" + var remoteMD5 = "-1" - fun checkUpdate(): Boolean{ - val localMD5 = Checksum.getLocalChecksum(Settings.SAVE_DIR + File.separator + Settings.SAVE_NAME) - val remoteMD5 = Checksum.getRemoteChecksum(Settings.DOWNLOAD_URL) - - return localMD5 != remoteMD5 + fun checkUpdate(){ + status = UpdateStatus.CHECKING + localMD5 = Checksum.getLocalChecksum(Settings.SAVE_DIR + File.separator + Settings.SAVE_NAME) + remoteMD5 = Checksum.getRemoteChecksum(Settings.DOWNLOAD_URL) ?: "-1" + println("Local: $localMD5 || Remote: $remoteMD5") + status = UpdateStatus.COMPLETE } fun runUpdate(){ @@ -42,9 +45,12 @@ object Updater { val stream = connection.getInputStream() + status = UpdateStatus.DOWNLOADING + var start = System.currentTimeMillis() + while(status == UpdateStatus.DOWNLOADING){ - val buffer = if(length - downloaded > 1024){ - ByteArray(1024) + val buffer = if(length - downloaded > 4096){ + ByteArray(4096) } else { ByteArray(length - downloaded) } @@ -74,12 +80,15 @@ object Updater { status = UpdateStatus.COMPLETE } } + println("Time taken: ${System.currentTimeMillis() - start}ms") MainWindow.loadingBar.icon = ImageIcon((MainWindow.loadingBar.icon as ImageIcon).image.getScaledInstance(695, 31, Image.SCALE_FAST)) MainWindow.loadingBar.placeAt(103, MainWindow.height - 33, 695, 31) + launchClient() }.start() } - internal enum class UpdateStatus{ + enum class UpdateStatus{ + CHECKING, DOWNLOADING, COMPLETE }