From 99c798daad1bf05bcae6dd25c22d5ad0e94137a6 Mon Sep 17 00:00:00 2001 From: ryannathans Date: Tue, 26 Apr 2022 19:03:48 +1000 Subject: [PATCH 1/2] launcher now checks if there is an update before downloading it --- src/main/kotlin/Checksum.kt | 33 +++++++-------------------------- src/main/kotlin/Settings.kt | 1 + src/main/kotlin/Updater.kt | 2 +- 3 files changed, 9 insertions(+), 27 deletions(-) diff --git a/src/main/kotlin/Checksum.kt b/src/main/kotlin/Checksum.kt index 43be22b..e3fcdb7 100644 --- a/src/main/kotlin/Checksum.kt +++ b/src/main/kotlin/Checksum.kt @@ -9,18 +9,6 @@ import java.security.NoSuchAlgorithmException import kotlin.experimental.and object Checksum { - val localChecksum: String? - get() { - val local: File = File(Settings.SAVE_DIR + Settings.SAVE_NAME) - try { - FileInputStream(local).use { fis -> return calculateMd5(fis) } - } catch (e: Exception) { - e.printStackTrace() - MainWindow.loadingLabel.text = e.message - MainWindow.loadingLabel.repaint() - } - return null - } fun getLocalChecksum(file: URI?): String { val local = File(file!!) @@ -28,21 +16,14 @@ object Checksum { else FileInputStream(local).use { fis -> return calculateMd5(fis) } } - val remoteChecksum: String? - get() { - try { - URL(Settings.DOWNLOAD_URL).openStream().use { stream -> return calculateMd5(stream) } - } catch (e: Exception) { - e.printStackTrace() - MainWindow.loadingLabel.text = e.message - MainWindow.loadingLabel.repaint() - return null - } - } - - fun getRemoteChecksum(url: String?): String? { + fun getRemoteChecksum(url: String?, checksumFile: Boolean = false): String? { try { - URL(url).openStream().use { stream -> return calculateMd5(stream) } + if (checksumFile) { + URL(url).openStream().use { stream -> return stream.bufferedReader().use { it.readText().trim() } } + } + else { + URL(url).openStream().use { stream -> return calculateMd5(stream) } + } } catch (e: Exception) { e.printStackTrace() MainWindow.loadingLabel.text = e.message diff --git a/src/main/kotlin/Settings.kt b/src/main/kotlin/Settings.kt index d6958ba..9d2b23c 100644 --- a/src/main/kotlin/Settings.kt +++ b/src/main/kotlin/Settings.kt @@ -6,6 +6,7 @@ object Settings { } val SAVE_NAME = "2009scape.jar" val DOWNLOAD_URL = "http://play.2009scape.org/2009scape.jar" + val DOWNLOAD_MD5_URL = "http://play.2009scape.org/2009scape.md5sum" val LAUNCHER_URL = "https://gitlab.com/2009scape/09launcher/-/jobs/artifacts/master/raw/build/libs/2009scape.jar?job=build" var HAS_UPDATED = false var CHECK_FOR_UPDATES = true diff --git a/src/main/kotlin/Updater.kt b/src/main/kotlin/Updater.kt index adb5d24..0457c79 100644 --- a/src/main/kotlin/Updater.kt +++ b/src/main/kotlin/Updater.kt @@ -19,7 +19,7 @@ object Updater { val fileUri = File(Settings.SAVE_DIR + File.separator + Settings.SAVE_NAME).toURI() status = UpdateStatus.CHECKING localMD5 = Checksum.getLocalChecksum(fileUri) - remoteMD5 = Checksum.getRemoteChecksum(Settings.DOWNLOAD_URL) ?: "-1" + remoteMD5 = Checksum.getRemoteChecksum(Settings.DOWNLOAD_MD5_URL, checksumFile = true) ?: "-1" println("Local: $localMD5 || Remote: $remoteMD5") status = UpdateStatus.COMPLETE } From 4818225bc6df1592fb6740f152eeb1de5276c032 Mon Sep 17 00:00:00 2001 From: ryannathans Date: Tue, 26 Apr 2022 22:05:30 +1000 Subject: [PATCH 2/2] update client if fail to get md5 --- src/main/kotlin/MainWindow.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/MainWindow.kt b/src/main/kotlin/MainWindow.kt index f215f08..3ee6daf 100644 --- a/src/main/kotlin/MainWindow.kt +++ b/src/main/kotlin/MainWindow.kt @@ -104,7 +104,7 @@ object MainWindow : JFrame("2009scape Launcher") { Thread.sleep(50L) } Settings.HAS_UPDATED = true - if (Updater.remoteMD5 != "-1" && Updater.remoteMD5 != Updater.localMD5) { + if (Updater.remoteMD5 != Updater.localMD5) { println("Update required, running update...") loadingLabel.text = oldText Updater.runUpdate()