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) + } + } +}