From 0b7d5ba223657f0a5de33862871a2355e7235c18 Mon Sep 17 00:00:00 2001 From: Brady Date: Sun, 9 Jun 2024 07:27:18 -0230 Subject: Backend: Dev Modules (#2011) Co-authored-by: Cal --- .../at/hannibal2/skyhanni/config/ConfigManager.kt | 5 ++-- .../java/at/hannibal2/skyhanni/data/ChatManager.kt | 6 ++--- .../at/hannibal2/skyhanni/events/LorenzEvent.kt | 4 +-- .../skyhanni/events/MessageSendToServerEvent.kt | 4 +-- .../java/at/hannibal2/skyhanni/utils/ChatUtils.kt | 2 +- .../at/hannibal2/skyhanni/utils/LorenzUtils.kt | 4 --- .../java/at/hannibal2/skyhanni/utils/NEUItems.kt | 3 ++- .../at/hannibal2/skyhanni/utils/ReflectionUtils.kt | 16 ----------- .../utils/repopatterns/RepoPatternManager.kt | 4 +-- .../skyhanni/utils/system/PlatformUtils.kt | 31 ++++++++++++++++++++++ 10 files changed, 46 insertions(+), 33 deletions(-) create mode 100644 src/main/java/at/hannibal2/skyhanni/utils/system/PlatformUtils.kt (limited to 'src') diff --git a/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt b/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt index fc654983c..5bee8eab3 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt @@ -16,6 +16,7 @@ import at.hannibal2.skyhanni.utils.LorenzLogger import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.SimpleTimeMark import at.hannibal2.skyhanni.utils.json.BaseGsonBuilder +import at.hannibal2.skyhanni.utils.system.PlatformUtils import com.google.gson.Gson import com.google.gson.GsonBuilder import com.google.gson.JsonObject @@ -127,7 +128,7 @@ class ConfigManager { } val configLink = field.getAnnotation(ConfigLink::class.java) if (configLink == null) { - if (LorenzUtils.isInDevEnvironment()) { + if (PlatformUtils.isDevEnvironment) { var name = "${field.declaringClass.name}.${field.name}" name = name.replace("at.hannibal2.skyhanni.config.", "") if (name !in ignoredMissingConfigLinks) { @@ -170,7 +171,7 @@ class ConfigManager { val jsonObject = lenientGson.fromJson(bufferedReader.readText(), JsonObject::class.java) val newJsonObject = ConfigUpdaterMigrator.fixConfig(jsonObject) val run = { lenientGson.fromJson(newJsonObject, defaultValue.javaClass) } - if (LorenzUtils.isInDevEnvironment()) { + if (PlatformUtils.isDevEnvironment) { try { run() } catch (e: Throwable) { diff --git a/src/main/java/at/hannibal2/skyhanni/data/ChatManager.kt b/src/main/java/at/hannibal2/skyhanni/data/ChatManager.kt index fdfaaf06b..965b4af57 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/ChatManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/ChatManager.kt @@ -12,10 +12,10 @@ import at.hannibal2.skyhanni.utils.IdentityCharacteristics import at.hannibal2.skyhanni.utils.LorenzLogger import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.ReflectionUtils.getClassInstance -import at.hannibal2.skyhanni.utils.ReflectionUtils.getModContainer import at.hannibal2.skyhanni.utils.ReflectionUtils.makeAccessible import at.hannibal2.skyhanni.utils.StringUtils.removeColor import at.hannibal2.skyhanni.utils.chat.Text.send +import at.hannibal2.skyhanni.utils.system.PlatformUtils.getModInstance import net.minecraft.client.Minecraft import net.minecraft.client.gui.ChatLine import net.minecraft.client.gui.GuiNewChat @@ -84,10 +84,10 @@ object ChatManager { val message = packet.message val component = ChatComponentText(message) val originatingModCall = event.findOriginatingModCall() - val originatingModContainer = originatingModCall?.getClassInstance()?.getModContainer() + val originatingModContainer = originatingModCall?.getClassInstance()?.getModInstance() val hoverInfo = listOf( "§7Message created by §a${originatingModCall?.toString() ?: "§cprobably minecraft"}", - "§7Mod id: §a${originatingModContainer?.modId}", + "§7Mod id: §a${originatingModContainer?.id}", "§7Mod name: §a${originatingModContainer?.name}" ) val stackTrace = diff --git a/src/main/java/at/hannibal2/skyhanni/events/LorenzEvent.kt b/src/main/java/at/hannibal2/skyhanni/events/LorenzEvent.kt index a7d957ace..f957b75ff 100644 --- a/src/main/java/at/hannibal2/skyhanni/events/LorenzEvent.kt +++ b/src/main/java/at/hannibal2/skyhanni/events/LorenzEvent.kt @@ -6,7 +6,7 @@ import at.hannibal2.skyhanni.mixins.hooks.setValue import at.hannibal2.skyhanni.mixins.transformers.AccessorEventBus import at.hannibal2.skyhanni.test.command.ErrorManager import at.hannibal2.skyhanni.utils.ChatUtils -import at.hannibal2.skyhanni.utils.LorenzUtils +import at.hannibal2.skyhanni.utils.system.PlatformUtils import net.minecraftforge.common.MinecraftForge import net.minecraftforge.fml.common.eventhandler.Event import net.minecraftforge.fml.common.eventhandler.IEventListener @@ -28,7 +28,7 @@ abstract class LorenzEvent : Event() { return 0 } } - val isInGuardedEventHandler get() = eventHandlerDepth > 0 || LorenzUtils.isInDevEnvironment() + val isInGuardedEventHandler get() = eventHandlerDepth > 0 || PlatformUtils.isDevEnvironment } @Deprecated("Use SkyHanniEvent instead", ReplaceWith("")) diff --git a/src/main/java/at/hannibal2/skyhanni/events/MessageSendToServerEvent.kt b/src/main/java/at/hannibal2/skyhanni/events/MessageSendToServerEvent.kt index 49e51a30b..6aa07cb54 100644 --- a/src/main/java/at/hannibal2/skyhanni/events/MessageSendToServerEvent.kt +++ b/src/main/java/at/hannibal2/skyhanni/events/MessageSendToServerEvent.kt @@ -1,11 +1,11 @@ package at.hannibal2.skyhanni.events -import net.minecraftforge.fml.common.ModContainer +import at.hannibal2.skyhanni.utils.system.ModInstance import net.minecraftforge.fml.common.eventhandler.Cancelable @Cancelable class MessageSendToServerEvent( val message: String, val splitMessage: List, - val originatingModContainer: ModContainer? + val originatingModContainer: ModInstance? ) : LorenzEvent() diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt index 134f33154..aff0e745a 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt @@ -260,7 +260,7 @@ object ChatUtils { fun MessageSendToServerEvent.isCommand(commandsWithSlash: Collection) = splitMessage.takeIf { it.isNotEmpty() }?.get(0) in commandsWithSlash - fun MessageSendToServerEvent.senderIsSkyhanni() = originatingModContainer?.modId == "skyhanni" + fun MessageSendToServerEvent.senderIsSkyhanni() = originatingModContainer?.id == "skyhanni" fun MessageSendToServerEvent.eventWithNewMessage(message: String) = MessageSendToServerEvent(message, message.split(" "), this.originatingModContainer) diff --git a/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt index 0c88e0bfe..f52f7f72b 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt @@ -26,7 +26,6 @@ import net.minecraft.client.entity.EntityPlayerSP import net.minecraft.client.gui.inventory.GuiEditSign import net.minecraft.entity.EntityLivingBase import net.minecraft.entity.SharedMonsterAttributes -import net.minecraft.launchwrapper.Launch import net.minecraft.util.AxisAlignedBB import net.minecraft.util.ChatComponentText import net.minecraftforge.fml.common.FMLCommonHandler @@ -327,9 +326,6 @@ object LorenzUtils { inline fun > T.isAnyOf(vararg array: T): Boolean = array.contains(this) - // TODO move to val by lazy - fun isInDevEnvironment() = ((Launch.blackboard ?: mapOf())["fml.deobfuscatedEnvironment"] as Boolean?) ?: true - fun shutdownMinecraft(reason: String? = null) { System.err.println("SkyHanni-${SkyHanniMod.version} forced the game to shutdown.") reason?.let { diff --git a/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt b/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt index d3a433e86..262b768a4 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt @@ -18,6 +18,7 @@ import at.hannibal2.skyhanni.utils.NumberUtil.isInt import at.hannibal2.skyhanni.utils.PrimitiveItemStack.Companion.makePrimitiveStack import at.hannibal2.skyhanni.utils.json.BaseGsonBuilder import at.hannibal2.skyhanni.utils.json.fromJson +import at.hannibal2.skyhanni.utils.system.PlatformUtils import com.google.gson.JsonObject import com.google.gson.JsonPrimitive import com.google.gson.TypeAdapter @@ -135,7 +136,7 @@ object NEUItems { name = name.removePrefix("§7[lvl 1➡100] ") if (name.contains("[lvl 1➡100]")) { - if (LorenzUtils.isInDevEnvironment()) { + if (PlatformUtils.isDevEnvironment) { error("wrong name: '$name'") } println("wrong name: '$name'") diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ReflectionUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/ReflectionUtils.kt index 1fe8b882c..f714ebc97 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ReflectionUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ReflectionUtils.kt @@ -1,7 +1,5 @@ package at.hannibal2.skyhanni.utils -import net.minecraftforge.fml.common.Loader -import net.minecraftforge.fml.common.ModContainer import java.lang.reflect.Constructor import java.lang.reflect.Field import java.lang.reflect.Modifier @@ -66,19 +64,5 @@ object ReflectionUtils { return Class.forName(this.className) } - private val packageLookup by lazy { - Loader.instance().modList - .flatMap { mod -> mod.ownedPackages.map { it to mod } } - .toMap() - } - - val Class<*>.shPackageName - get() = - canonicalName?.substringBeforeLast('.') - - fun Class<*>.getModContainer(): ModContainer? { - return packageLookup[shPackageName] - } - fun Class<*>.getDeclaredFieldOrNull(name: String): Field? = declaredFields.firstOrNull { it.name == name } } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/repopatterns/RepoPatternManager.kt b/src/main/java/at/hannibal2/skyhanni/utils/repopatterns/RepoPatternManager.kt index d52f8ccb4..80881bbce 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/repopatterns/RepoPatternManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/repopatterns/RepoPatternManager.kt @@ -11,10 +11,10 @@ import at.hannibal2.skyhanni.events.utils.PreInitFinishedEvent import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule import at.hannibal2.skyhanni.test.command.ErrorManager import at.hannibal2.skyhanni.utils.ConditionalUtils.afterChange -import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.RegexUtils.matches import at.hannibal2.skyhanni.utils.StringUtils import at.hannibal2.skyhanni.utils.StringUtils.substringBeforeLastOrNull +import at.hannibal2.skyhanni.utils.system.PlatformUtils import net.minecraft.launchwrapper.Launch import net.minecraftforge.fml.common.FMLCommonHandler import net.minecraftforge.fml.common.eventhandler.SubscribeEvent @@ -77,7 +77,7 @@ object RepoPatternManager { } } - val localLoading: Boolean get() = config.forceLocal.get() || (!insideTest && LorenzUtils.isInDevEnvironment()) + val localLoading: Boolean get() = config.forceLocal.get() || (!insideTest && PlatformUtils.isDevEnvironment) /** * Crash if in a development environment, or if inside a guarded event handler. diff --git a/src/main/java/at/hannibal2/skyhanni/utils/system/PlatformUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/system/PlatformUtils.kt new file mode 100644 index 000000000..4828bc2af --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/utils/system/PlatformUtils.kt @@ -0,0 +1,31 @@ +package at.hannibal2.skyhanni.utils.system + +import net.minecraft.launchwrapper.Launch +import net.minecraftforge.fml.common.Loader +import net.minecraftforge.fml.common.ModContainer + +/** + * This object contains utilities for all platform specific operations. + * i.e. operations that are specific to the mod loader or the environment the mod is running in. + */ +object PlatformUtils { + + private val modPackages: Map by lazy { + Loader.instance().modList + .flatMap { mod -> mod.ownedPackages.map { it to mod } } + .toMap() + } + + val isDevEnvironment: Boolean by lazy { + Launch.blackboard?.get("fml.deobfuscatedEnvironment") as? Boolean ?: true + } + + fun getModFromPackage(packageName: String?): ModInstance? = modPackages[packageName]?.let { + ModInstance(it.modId, it.name, it.version) + } + + fun Class<*>.getModInstance(): ModInstance? = getModFromPackage(canonicalName?.substringBeforeLast('.')) + +} + +data class ModInstance(val id: String, val name: String, val version: String) -- cgit