From 11a43242b96b65aa000181bdf1d8b3648d81df74 Mon Sep 17 00:00:00 2001 From: Graham Date: Sat, 19 Jun 2021 11:29:20 +0100 Subject: [PATCH] Add applet viewer config parser Signed-off-by: Graham --- .../org/openrs2/archive/jav/JavConfig.kt | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 archive/src/main/kotlin/org/openrs2/archive/jav/JavConfig.kt diff --git a/archive/src/main/kotlin/org/openrs2/archive/jav/JavConfig.kt b/archive/src/main/kotlin/org/openrs2/archive/jav/JavConfig.kt new file mode 100644 index 00000000..84786ac6 --- /dev/null +++ b/archive/src/main/kotlin/org/openrs2/archive/jav/JavConfig.kt @@ -0,0 +1,66 @@ +package org.openrs2.archive.jav + +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.future.await +import kotlinx.coroutines.withContext +import org.openrs2.http.checkStatusCode +import java.io.BufferedReader +import java.net.URI +import java.net.http.HttpClient +import java.net.http.HttpRequest +import java.net.http.HttpResponse + +public data class JavConfig( + public val config: Map, + public val params: Map, + public val messages: Map +) { + public companion object { + public suspend fun download(client: HttpClient, url: String): JavConfig { + val request = HttpRequest.newBuilder(URI(url)) + .GET() + .build() + + val response = client.sendAsync(request, HttpResponse.BodyHandlers.ofInputStream()).await() + response.checkStatusCode() + + return withContext(Dispatchers.IO) { + response.body().bufferedReader().use { reader -> + read(reader) + } + } + } + + public fun read(reader: BufferedReader): JavConfig { + val config = mutableMapOf() + val params = mutableMapOf() + val messages = mutableMapOf() + + reader.lineSequence().map(String::trim).forEach { line -> + when { + line.startsWith("//") || line.startsWith("#") -> Unit + line.startsWith("msg=") -> { + val parts = line.substring("msg=".length).split("=", limit = 2) + if (parts.size == 2) { + messages[parts[0]] = parts[1] + } + } + line.startsWith("param=") -> { + val parts = line.substring("param=".length).split("=", limit = 2) + if (parts.size == 2) { + params[parts[0]] = parts[1] + } + } + else -> { + val parts = line.split("=", limit = 2) + if (parts.size == 2) { + config[parts[0]] = parts[1] + } + } + } + } + + return JavConfig(config, params, messages) + } + } +}