Skip to content

Commit

Permalink
Backend: Add in event handler check to SkyHanni Events (#2755)
Browse files Browse the repository at this point in the history
  • Loading branch information
CalMWolfs authored Oct 26, 2024
1 parent 9436627 commit 697e9e2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
26 changes: 23 additions & 3 deletions src/main/java/at/hannibal2/skyhanni/api/event/EventHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package at.hannibal2.skyhanni.api.event

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.data.IslandType
import at.hannibal2.skyhanni.mixins.hooks.getValue
import at.hannibal2.skyhanni.mixins.hooks.setValue
import at.hannibal2.skyhanni.test.command.ErrorManager
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.LorenzUtils.inAnyIsland
import at.hannibal2.skyhanni.utils.StringUtils
import at.hannibal2.skyhanni.utils.chat.Text
import at.hannibal2.skyhanni.utils.system.PlatformUtils

class EventHandler<T : SkyHanniEvent> private constructor(
val name: String,
Expand All @@ -21,9 +24,24 @@ class EventHandler<T : SkyHanniEvent> private constructor(
constructor(event: Class<T>, listeners: List<EventListeners.Listener>) : this(
(event.name.split(".").lastOrNull() ?: event.name).replace("$", "."),
listeners.sortedBy { it.options.priority }.toList(),
listeners.any { it.options.receiveCancelled }
listeners.any { it.options.receiveCancelled },
)

companion object {
private var eventHandlerDepth by object : ThreadLocal<Int>() {
override fun initialValue(): Int {
return 0
}
}

/**
* Returns true if the current thread is in an event handler. This is because the event handler catches exceptions which means
* that we are free to throw exceptions in the event handler without crashing the game.
* We also return true if we are in a dev environment to alert the developer of any errors effectively.
*/
val isInEventHandler get() = eventHandlerDepth > 0 || PlatformUtils.isDevEnvironment
}

fun post(event: T, onError: ((Throwable) -> Unit)? = null): Boolean {
invokeCount++
if (this.listeners.isEmpty()) return false
Expand All @@ -32,6 +50,7 @@ class EventHandler<T : SkyHanniEvent> private constructor(

var errors = 0

eventHandlerDepth++
for (listener in listeners) {
if (!shouldInvoke(event, listener)) continue
try {
Expand All @@ -48,13 +67,14 @@ class EventHandler<T : SkyHanniEvent> private constructor(
}
if (event.isCancelled && !canReceiveCancelled) break
}
eventHandlerDepth--

if (errors > 3) {
val hiddenErrors = errors - 3
ChatUtils.chat(
Text.text(
"§c[SkyHanni/${SkyHanniMod.version}] $hiddenErrors more errors in $name are hidden!"
)
"§c[SkyHanni/${SkyHanniMod.version}] $hiddenErrors more errors in $name are hidden!",
),
)
}
return event.isCancelled
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package at.hannibal2.skyhanni.config

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.api.event.EventHandler
import at.hannibal2.skyhanni.config.core.config.Position
import at.hannibal2.skyhanni.config.core.config.PositionList
import at.hannibal2.skyhanni.data.jsonobjects.local.FriendsJson
Expand Down Expand Up @@ -96,7 +97,7 @@ class ConfigManager {
try {
findPositionLinks(features, mutableSetOf())
} catch (e: Exception) {
if (LorenzEvent.isInGuardedEventHandler) throw e
if (LorenzEvent.isInGuardedEventHandler || EventHandler.isInEventHandler) throw e
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/at/hannibal2/skyhanni/utils/ConfigUtils.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package at.hannibal2.skyhanni.utils

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.api.event.EventHandler
import at.hannibal2.skyhanni.config.ConfigGuiManager
import at.hannibal2.skyhanni.config.HasLegacyId
import at.hannibal2.skyhanni.events.LorenzEvent
Expand Down Expand Up @@ -86,7 +87,7 @@ object ConfigUtils {
if (tryJumpToEditor(ConfigGuiManager.getEditorInstance())) return

// TODO create utils function "crashIfInDevEnv"
if (LorenzEvent.isInGuardedEventHandler) {
if (LorenzEvent.isInGuardedEventHandler || EventHandler.isInEventHandler) {
throw Error("can not jump to editor $name")
}
ErrorManager.logErrorStateWithData(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package at.hannibal2.skyhanni.utils.repopatterns

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.api.event.EventHandler
import at.hannibal2.skyhanni.api.event.HandleEvent
import at.hannibal2.skyhanni.config.ConfigManager
import at.hannibal2.skyhanni.config.features.dev.RepoPatternConfig
Expand Down Expand Up @@ -60,11 +61,6 @@ object RepoPatternManager {
private var usedKeys: NavigableMap<String, CommonPatternInfo<*, *>> = TreeMap()

private var wasPreInitialized = false
private val isInDevEnv = try {
Launch.blackboard["fml.deobfuscatedEnvironment"] as Boolean
} catch (_: Exception) {
true
}

private val insideTest = Launch.blackboard == null

Expand All @@ -88,8 +84,9 @@ object RepoPatternManager {
* Crash if in a development environment, or if inside a guarded event handler.
*/
fun crash(reason: String) {
if (isInDevEnv || LorenzEvent.isInGuardedEventHandler)
if (LorenzEvent.isInGuardedEventHandler || EventHandler.isInEventHandler) {
throw RuntimeException(reason)
}
}

/**
Expand Down

0 comments on commit 697e9e2

Please sign in to comment.