From 04ef730ad93082a8dac1f0831cf3db8a1f707ee0 Mon Sep 17 00:00:00 2001 From: ThatGravyBoat Date: Thu, 6 Jun 2024 00:04:53 -0230 Subject: [PATCH 1/2] dev modules --- .../skyhannimodule/ModuleProcessor.kt | 17 ++++++++-- .../skyhanni/skyhannimodule/SkyHanniModule.kt | 7 ++++- .../skyhanni/config/ConfigManager.kt | 5 +-- .../at/hannibal2/skyhanni/data/ChatManager.kt | 6 ++-- .../hannibal2/skyhanni/events/LorenzEvent.kt | 4 +-- .../events/MessageSendToServerEvent.kt | 4 +-- .../at/hannibal2/skyhanni/utils/ChatUtils.kt | 2 +- .../hannibal2/skyhanni/utils/LorenzUtils.kt | 4 --- .../at/hannibal2/skyhanni/utils/NEUItems.kt | 3 +- .../skyhanni/utils/ReflectionUtils.kt | 16 ---------- .../utils/repopatterns/RepoPatternManager.kt | 4 +-- .../skyhanni/utils/system/PlatformUtils.kt | 31 +++++++++++++++++++ 12 files changed, 66 insertions(+), 37 deletions(-) create mode 100644 src/main/java/at/hannibal2/skyhanni/utils/system/PlatformUtils.kt diff --git a/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/ModuleProcessor.kt b/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/ModuleProcessor.kt index bc7d262b49d5..34d4a71124af 100644 --- a/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/ModuleProcessor.kt +++ b/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/ModuleProcessor.kt @@ -44,6 +44,12 @@ class ModuleProcessor(private val codeGenerator: CodeGenerator, private val logg return symbol } + //TODO remove when KMixins added as it contains KSP annotation helpers. + private fun isDevAnnotation(klass: KSClassDeclaration): Boolean { + val annotation = klass.annotations.find { it.shortName.asString() == "SkyHanniModule" } ?: return false + return annotation.arguments.find { it.name?.asString() == "inDevelopment" }?.value as? Boolean ?: false + } + private fun generateFile(symbols: List) { val dependencies = symbols.mapNotNull { it.containingFile }.toTypedArray() val deps = Dependencies(true, *dependencies) @@ -53,13 +59,18 @@ class ModuleProcessor(private val codeGenerator: CodeGenerator, private val logg OutputStreamWriter(file).use { it.write("package at.hannibal2.skyhanni.skyhannimodule\n\n") it.write("object LoadedModules {\n") - it.write(" val modules: List = listOf(\n") + it.write(" val isDev: Boolean = at.hannibal2.skyhanni.utils.system.PlatformUtils.isDevEnvironment\n") + it.write(" val modules: List = buildList {\n") symbols.forEach { symbol -> - it.write(" ${symbol.qualifiedName!!.asString()},\n") + if (isDevAnnotation(symbol)) { + it.write(" if (isDev) add(${symbol.qualifiedName!!.asString()})\n") + } else { + it.write(" add(${symbol.qualifiedName!!.asString()})\n") + } } - it.write(" )\n") + it.write(" }\n") it.write("}\n") } diff --git a/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/SkyHanniModule.kt b/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/SkyHanniModule.kt index cb6b0eae4c48..45207da0207e 100644 --- a/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/SkyHanniModule.kt +++ b/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/SkyHanniModule.kt @@ -2,4 +2,9 @@ package at.hannibal2.skyhanni.skyhannimodule @Target(AnnotationTarget.CLASS) @Retention(AnnotationRetention.SOURCE) -annotation class SkyHanniModule +annotation class SkyHanniModule( + /** + * If the module will only be loaded in a development environment. + */ + val inDevelopment: Boolean = false, +) diff --git a/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt b/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt index fc654983cfe4..5bee8eab3157 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 ef032f6fc360..860449ee8225 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/ChatManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/ChatManager.kt @@ -11,10 +11,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 @@ -83,10 +83,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 aaaa1dd2ed73..b2b5be984d3e 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 @@ -26,7 +26,7 @@ abstract class LorenzEvent : Event() { return 0 } } - val isInGuardedEventHandler get() = eventHandlerDepth > 0 || LorenzUtils.isInDevEnvironment() + val isInGuardedEventHandler get() = eventHandlerDepth > 0 || PlatformUtils.isDevEnvironment } fun postAndCatchAndBlock( diff --git a/src/main/java/at/hannibal2/skyhanni/events/MessageSendToServerEvent.kt b/src/main/java/at/hannibal2/skyhanni/events/MessageSendToServerEvent.kt index 49e51a30b603..6aa07cb54ca0 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 134f33154278..aff0e745a070 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 0dc0047a55b3..f400843c413e 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 9c35ddcba42a..9b44852a4db8 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 1fe8b882c797..f714ebc97f26 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 cc17fa17fd27..3935943f5d49 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/repopatterns/RepoPatternManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/repopatterns/RepoPatternManager.kt @@ -10,10 +10,10 @@ import at.hannibal2.skyhanni.events.RepositoryReloadEvent 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 @@ -76,7 +76,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 000000000000..4828bc2af825 --- /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) From ba6a9e20b1fc6224a4501a506c8f905c530958d0 Mon Sep 17 00:00:00 2001 From: ThatGravyBoat Date: Thu, 6 Jun 2024 00:05:45 -0230 Subject: [PATCH 2/2] change annotation property name --- .../at/hannibal2/skyhanni/skyhannimodule/ModuleProcessor.kt | 2 +- .../at/hannibal2/skyhanni/skyhannimodule/SkyHanniModule.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/ModuleProcessor.kt b/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/ModuleProcessor.kt index 34d4a71124af..8ed3e23a5c2a 100644 --- a/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/ModuleProcessor.kt +++ b/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/ModuleProcessor.kt @@ -47,7 +47,7 @@ class ModuleProcessor(private val codeGenerator: CodeGenerator, private val logg //TODO remove when KMixins added as it contains KSP annotation helpers. private fun isDevAnnotation(klass: KSClassDeclaration): Boolean { val annotation = klass.annotations.find { it.shortName.asString() == "SkyHanniModule" } ?: return false - return annotation.arguments.find { it.name?.asString() == "inDevelopment" }?.value as? Boolean ?: false + return annotation.arguments.find { it.name?.asString() == "devOnly" }?.value as? Boolean ?: false } private fun generateFile(symbols: List) { diff --git a/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/SkyHanniModule.kt b/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/SkyHanniModule.kt index 45207da0207e..c854a572ce65 100644 --- a/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/SkyHanniModule.kt +++ b/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/SkyHanniModule.kt @@ -6,5 +6,5 @@ annotation class SkyHanniModule( /** * If the module will only be loaded in a development environment. */ - val inDevelopment: Boolean = false, + val devOnly: Boolean = false, )