From 21ed41c307d31a6b34dfd137fe4b4d41ad67c87b Mon Sep 17 00:00:00 2001 From: Graham Date: Sat, 19 Jun 2021 13:46:35 +0100 Subject: [PATCH] Use applet viewer config to find the hostname of a JS5 server Signed-off-by: Graham --- .../openrs2/archive/cache/CacheDownloader.kt | 20 +++++++++++++++---- .../kotlin/org/openrs2/archive/game/Game.kt | 3 +-- .../org/openrs2/archive/game/GameDatabase.kt | 15 +++++--------- .../archive/migrations/V2__game_url.sql | 8 ++++++++ 4 files changed, 30 insertions(+), 16 deletions(-) create mode 100644 archive/src/main/resources/org/openrs2/archive/migrations/V2__game_url.sql diff --git a/archive/src/main/kotlin/org/openrs2/archive/cache/CacheDownloader.kt b/archive/src/main/kotlin/org/openrs2/archive/cache/CacheDownloader.kt index 7836c5b8..e3ccde01 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/cache/CacheDownloader.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/cache/CacheDownloader.kt @@ -1,14 +1,18 @@ package org.openrs2.archive.cache import org.openrs2.archive.game.GameDatabase +import org.openrs2.archive.jav.JavConfig import org.openrs2.net.BootstrapFactory import org.openrs2.net.awaitSuspend +import java.net.URI +import java.net.http.HttpClient import javax.inject.Inject import javax.inject.Singleton import kotlin.coroutines.suspendCoroutine @Singleton public class CacheDownloader @Inject constructor( + private val client: HttpClient, private val bootstrapFactory: BootstrapFactory, private val gameDatabase: GameDatabase, private val importer: CacheImporter @@ -16,10 +20,13 @@ public class CacheDownloader @Inject constructor( public suspend fun download(gameName: String) { val game = gameDatabase.getGame(gameName) ?: throw Exception("Game not found") - val hostname = game.hostname ?: throw Exception("Hostname not set") - val port = game.port ?: throw Exception("Port not set") + val url = game.url ?: throw Exception("URL not set") val build = game.build ?: throw Exception("Current build not set") + val config = JavConfig.download(client, url) + val codebase = config.config[CODEBASE] ?: throw Exception("Codebase missing") + val hostname = URI(codebase).host ?: throw Exception("Hostname missing") + val group = bootstrapFactory.createEventLoopGroup() try { suspendCoroutine { continuation -> @@ -28,7 +35,7 @@ public class CacheDownloader @Inject constructor( bootstrap, game.id, hostname, - port, + PORT, build, game.lastMasterIndexId, continuation, @@ -36,10 +43,15 @@ public class CacheDownloader @Inject constructor( ) bootstrap.handler(Js5ChannelInitializer(handler)) - .connect(hostname, port) + .connect(hostname, PORT) } } finally { group.shutdownGracefully().awaitSuspend() } } + + private companion object { + private const val CODEBASE = "codebase" + private const val PORT = 443 + } } diff --git a/archive/src/main/kotlin/org/openrs2/archive/game/Game.kt b/archive/src/main/kotlin/org/openrs2/archive/game/Game.kt index fc316cbb..38ffc8fc 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/game/Game.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/game/Game.kt @@ -2,8 +2,7 @@ package org.openrs2.archive.game public data class Game( public val id: Int, - public val hostname: String?, - public val port: Int?, + public val url: String?, public val build: Int?, public val lastMasterIndexId: Int? ) diff --git a/archive/src/main/kotlin/org/openrs2/archive/game/GameDatabase.kt b/archive/src/main/kotlin/org/openrs2/archive/game/GameDatabase.kt index 595a4036..80c2686b 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/game/GameDatabase.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/game/GameDatabase.kt @@ -12,7 +12,7 @@ public class GameDatabase @Inject constructor( return database.execute { connection -> connection.prepareStatement( """ - SELECT id, hostname, port, build, last_master_index_id + SELECT id, url, build, last_master_index_id FROM games WHERE name = ? """.trimIndent() @@ -25,24 +25,19 @@ public class GameDatabase @Inject constructor( } val id = rows.getInt(1) - val hostname: String? = rows.getString(2) + val url: String? = rows.getString(2) - var port: Int? = rows.getInt(3) - if (rows.wasNull()) { - port = null - } - - var build: Int? = rows.getInt(4) + var build: Int? = rows.getInt(3) if (rows.wasNull()) { build = null } - var lastMasterIndexId: Int? = rows.getInt(5) + var lastMasterIndexId: Int? = rows.getInt(4) if (rows.wasNull()) { lastMasterIndexId = null } - return@execute Game(id, hostname, port, build, lastMasterIndexId) + return@execute Game(id, url, build, lastMasterIndexId) } } } diff --git a/archive/src/main/resources/org/openrs2/archive/migrations/V2__game_url.sql b/archive/src/main/resources/org/openrs2/archive/migrations/V2__game_url.sql new file mode 100644 index 00000000..29ac64d6 --- /dev/null +++ b/archive/src/main/resources/org/openrs2/archive/migrations/V2__game_url.sql @@ -0,0 +1,8 @@ +ALTER TABLE games + DROP COLUMN hostname, + DROP COLUMN port, + ADD COLUMN url TEXT NULL; + +UPDATE games +SET url = 'https://oldschool.runescape.com/jav_config.ws' +WHERE name = 'oldschool';