From f0285e44ed113bcf2a11b5d6635f6124deb7fc3b Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Mon, 23 Dec 2024 02:22:06 +0100 Subject: feat: Add in game updater --- src/main/kotlin/moe/nea/ledger/utils/ErrorUtil.kt | 9 ++++++ .../moe/nea/ledger/utils/MinecraftExecutor.kt | 10 ++++++ src/main/kotlin/moe/nea/ledger/utils/di/DI.kt | 37 +++++++++++++--------- 3 files changed, 41 insertions(+), 15 deletions(-) create mode 100644 src/main/kotlin/moe/nea/ledger/utils/MinecraftExecutor.kt (limited to 'src/main/kotlin/moe/nea/ledger/utils') diff --git a/src/main/kotlin/moe/nea/ledger/utils/ErrorUtil.kt b/src/main/kotlin/moe/nea/ledger/utils/ErrorUtil.kt index 344c8bc..e0c83f9 100644 --- a/src/main/kotlin/moe/nea/ledger/utils/ErrorUtil.kt +++ b/src/main/kotlin/moe/nea/ledger/utils/ErrorUtil.kt @@ -4,6 +4,7 @@ import moe.nea.ledger.utils.di.Inject import moe.nea.ledger.utils.telemetry.ContextValue import moe.nea.ledger.utils.telemetry.EventRecorder import moe.nea.ledger.utils.telemetry.Span +import java.util.concurrent.CompletableFuture class ErrorUtil { @@ -27,6 +28,14 @@ class ErrorUtil { return getOrNull() } + fun > listenToFuture(t: T): T { + t.handle { ignored, exception -> + if (exception != null) + report(exception, "Uncaught exception in completable future") + } + return t + } + inline fun catch( vararg pairs: Pair, crossinline function: () -> T diff --git a/src/main/kotlin/moe/nea/ledger/utils/MinecraftExecutor.kt b/src/main/kotlin/moe/nea/ledger/utils/MinecraftExecutor.kt new file mode 100644 index 0000000..affd86c --- /dev/null +++ b/src/main/kotlin/moe/nea/ledger/utils/MinecraftExecutor.kt @@ -0,0 +1,10 @@ +package moe.nea.ledger.utils + +import net.minecraft.client.Minecraft +import java.util.concurrent.Executor + +class MinecraftExecutor : Executor { + override fun execute(command: Runnable) { + Minecraft.getMinecraft().addScheduledTask(command) + } +} \ No newline at end of file diff --git a/src/main/kotlin/moe/nea/ledger/utils/di/DI.kt b/src/main/kotlin/moe/nea/ledger/utils/di/DI.kt index 133637a..a9061d7 100644 --- a/src/main/kotlin/moe/nea/ledger/utils/di/DI.kt +++ b/src/main/kotlin/moe/nea/ledger/utils/di/DI.kt @@ -9,25 +9,32 @@ class DI { private fun formatInjectionStack() = injectionStack.joinToString(" -> ") + fun getProvider(type: Class): BaseDIProvider { + val provider = providers[type] as BaseDIProvider? + ?: error("Could not find provider for type $type") + return provider + } + private fun internalProvide(type: Class, element: AnnotatedElement? = null): T { - val provider = providers[type] as BaseDIProvider - val context = if (element == null) provider.createEmptyContext() else provider.createContext(element) - val key = Pair(type, context) - val existingValue = values[key] - if (existingValue != null) return existingValue as T - if (type in injectionStack) { - error("Found injection cycle: ${formatInjectionStack()} -> $type") - } - injectionStack.push(type) - val value = try { - provider.provideWithContext(this, context) + try { + val provider = getProvider(type) as BaseDIProvider + val context = if (element == null) provider.createEmptyContext() else provider.createContext(element) + val key = Pair(type, context) + val existingValue = values[key] + if (existingValue != null) return existingValue as T + if (type in injectionStack) { + error("Found injection cycle: ${formatInjectionStack()} -> $type") + } + injectionStack.push(type) + val value = + provider.provideWithContext(this, context) + val cycleCheckCookie = injectionStack.pop() + require(cycleCheckCookie == type) { "Unbalanced stack cookie: $cycleCheckCookie != $type" } + values[key] = value + return value } catch (ex: Exception) { throw RuntimeException("Could not create instance for type $type (in stack ${formatInjectionStack()})", ex) } - val cycleCheckCookie = injectionStack.pop() - require(cycleCheckCookie == type) { "Unbalanced stack cookie: $cycleCheckCookie != $type" } - values[key] = value - return value } fun provide(type: Class, element: AnnotatedElement? = null): T { -- cgit