Add ursa client
This commit is contained in:
73
src/main/kotlin/moe/nea/firmament/apis/UrsaManager.kt
Normal file
73
src/main/kotlin/moe/nea/firmament/apis/UrsaManager.kt
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
*/
|
||||||
|
|
||||||
|
package moe.nea.firmament.apis
|
||||||
|
|
||||||
|
import io.ktor.client.request.*
|
||||||
|
import io.ktor.client.statement.*
|
||||||
|
import io.ktor.http.*
|
||||||
|
import java.time.Duration
|
||||||
|
import java.time.Instant
|
||||||
|
import java.util.*
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.sync.Mutex
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
import net.minecraft.client.MinecraftClient
|
||||||
|
import moe.nea.firmament.Firmament
|
||||||
|
|
||||||
|
object UrsaManager {
|
||||||
|
private data class Token(
|
||||||
|
val validUntil: Instant,
|
||||||
|
val token: String,
|
||||||
|
val obtainedFrom: String,
|
||||||
|
) {
|
||||||
|
fun isValid(host: String) = Instant.now().plusSeconds(60) < validUntil && obtainedFrom == host
|
||||||
|
}
|
||||||
|
|
||||||
|
private var currentToken: Token? = null
|
||||||
|
private val lock = Mutex()
|
||||||
|
private fun getToken(host: String) = currentToken?.takeIf { it.isValid(host) }
|
||||||
|
|
||||||
|
suspend fun request(path: List<String>): HttpResponse {
|
||||||
|
var didLock = false
|
||||||
|
try {
|
||||||
|
val host = "ursa.notenoughupdates.org"
|
||||||
|
var token = getToken(host)
|
||||||
|
if (token == null) {
|
||||||
|
lock.lock()
|
||||||
|
didLock = true
|
||||||
|
token = getToken(host)
|
||||||
|
}
|
||||||
|
val response = Firmament.httpClient.get {
|
||||||
|
url {
|
||||||
|
this.host = host
|
||||||
|
appendPathSegments(path, encodeSlash = true)
|
||||||
|
}
|
||||||
|
if (token == null) {
|
||||||
|
withContext(Dispatchers.IO) {
|
||||||
|
val mc = MinecraftClient.getInstance()
|
||||||
|
val serverId = UUID.randomUUID().toString()
|
||||||
|
mc.sessionService.joinServer(mc.session.profile, mc.session.accessToken, serverId)
|
||||||
|
header("x-ursa-username", mc.session.profile.name)
|
||||||
|
header("x-ursa-serverid", serverId)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
header("x-ursa-token", token.token)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val savedToken = response.headers["x-ursa-token"]
|
||||||
|
if (savedToken != null) {
|
||||||
|
val validUntil = response.headers["x-ursa-expires"]?.toLongOrNull()?.let { Instant.ofEpochMilli(it) }
|
||||||
|
?: (Instant.now() + Duration.ofMinutes(55))
|
||||||
|
currentToken = Token(validUntil, savedToken, host)
|
||||||
|
}
|
||||||
|
return response
|
||||||
|
} finally {
|
||||||
|
if (didLock)
|
||||||
|
lock.unlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,6 +16,9 @@ import java.lang.reflect.ParameterizedType
|
|||||||
import java.lang.reflect.Type
|
import java.lang.reflect.Type
|
||||||
import java.lang.reflect.TypeVariable
|
import java.lang.reflect.TypeVariable
|
||||||
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource
|
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import moe.nea.firmament.Firmament
|
||||||
|
import moe.nea.firmament.util.MinecraftDispatcher
|
||||||
import moe.nea.firmament.util.iterate
|
import moe.nea.firmament.util.iterate
|
||||||
|
|
||||||
|
|
||||||
@@ -105,9 +108,11 @@ fun <T : ArgumentBuilder<DefaultSource, T>> T.thenLiteral(
|
|||||||
fun <T : ArgumentBuilder<DefaultSource, T>> T.then(node: ArgumentBuilder<DefaultSource, *>, block: T.() -> Unit): T =
|
fun <T : ArgumentBuilder<DefaultSource, T>> T.then(node: ArgumentBuilder<DefaultSource, *>, block: T.() -> Unit): T =
|
||||||
then(node).also(block)
|
then(node).also(block)
|
||||||
|
|
||||||
fun <T : ArgumentBuilder<DefaultSource, T>> T.thenExecute(block: CommandContext<DefaultSource>.() -> Unit): T =
|
fun <T : ArgumentBuilder<DefaultSource, T>> T.thenExecute(block: suspend CommandContext<DefaultSource>.() -> Unit): T =
|
||||||
executes {
|
executes {
|
||||||
block(it)
|
Firmament.coroutineScope.launch(MinecraftDispatcher) {
|
||||||
|
block(it)
|
||||||
|
}
|
||||||
1
|
1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,8 +8,10 @@ package moe.nea.firmament.commands
|
|||||||
|
|
||||||
import com.mojang.brigadier.CommandDispatcher
|
import com.mojang.brigadier.CommandDispatcher
|
||||||
import com.mojang.brigadier.arguments.StringArgumentType.string
|
import com.mojang.brigadier.arguments.StringArgumentType.string
|
||||||
|
import io.ktor.client.statement.*
|
||||||
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource
|
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource
|
||||||
import net.minecraft.text.Text
|
import net.minecraft.text.Text
|
||||||
|
import moe.nea.firmament.apis.UrsaManager
|
||||||
import moe.nea.firmament.features.inventory.storageoverlay.StorageOverlayScreen
|
import moe.nea.firmament.features.inventory.storageoverlay.StorageOverlayScreen
|
||||||
import moe.nea.firmament.features.world.FairySouls
|
import moe.nea.firmament.features.world.FairySouls
|
||||||
import moe.nea.firmament.gui.config.AllConfigsGui
|
import moe.nea.firmament.gui.config.AllConfigsGui
|
||||||
@@ -135,6 +137,15 @@ fun firmamentCommand() = literal("firmament") {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
thenLiteral("callUrsa") {
|
||||||
|
thenArgument("path", string()) { path ->
|
||||||
|
thenExecute {
|
||||||
|
source.sendFeedback(Text.translatable("firmament.ursa.debugrequest.start"))
|
||||||
|
val text = UrsaManager.request(this[path].split("/")).bodyAsText()
|
||||||
|
source.sendFeedback(Text.translatable("firmament.ursa.debugrequest.result", text))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,23 +6,7 @@
|
|||||||
|
|
||||||
package moe.nea.firmament.util
|
package moe.nea.firmament.util
|
||||||
|
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.asCoroutineDispatcher
|
||||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
|
||||||
import kotlinx.coroutines.Runnable
|
|
||||||
import kotlin.coroutines.CoroutineContext
|
|
||||||
import net.minecraft.client.MinecraftClient
|
import net.minecraft.client.MinecraftClient
|
||||||
|
|
||||||
object MinecraftDispatcher : CoroutineDispatcher() {
|
val MinecraftDispatcher by lazy { MinecraftClient.getInstance().asCoroutineDispatcher() }
|
||||||
@ExperimentalCoroutinesApi
|
|
||||||
override fun limitedParallelism(parallelism: Int): CoroutineDispatcher {
|
|
||||||
throw UnsupportedOperationException("limitedParallelism is not supported for MinecraftDispatcher")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun isDispatchNeeded(context: CoroutineContext): Boolean =
|
|
||||||
!MinecraftClient.getInstance().isOnThread
|
|
||||||
|
|
||||||
|
|
||||||
override fun dispatch(context: CoroutineContext, block: Runnable) {
|
|
||||||
MinecraftClient.getInstance().execute(block)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -28,6 +28,8 @@
|
|||||||
"firmament.config.repo.branch": "Repo Branch",
|
"firmament.config.repo.branch": "Repo Branch",
|
||||||
"firmament.config.repo.branch.hint": "dangerous",
|
"firmament.config.repo.branch.hint": "dangerous",
|
||||||
"firmament.config.repo.reset": "Reset",
|
"firmament.config.repo.reset": "Reset",
|
||||||
|
"firmament.ursa.debugrequest.start": "Ursa request launched",
|
||||||
|
"firmament.ursa.debugrequest.result": "Ursa request succeeded: %s",
|
||||||
"firmament.sbinfo.nolocraw": "No locraw data available",
|
"firmament.sbinfo.nolocraw": "No locraw data available",
|
||||||
"firmament.sbinfo.profile": "Current profile cutename: %s",
|
"firmament.sbinfo.profile": "Current profile cutename: %s",
|
||||||
"firmament.sbinfo.server": "Locraw Server: %s",
|
"firmament.sbinfo.server": "Locraw Server: %s",
|
||||||
|
|||||||
Reference in New Issue
Block a user