diff options
12 files changed, 66 insertions, 37 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 568164133..99d7904d0 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 @@ -76,6 +76,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() == "devOnly" }?.value as? Boolean ?: false + } + // TODO use Kotlin Poet once KMixins is merged private fun generateFile(symbols: List<KSClassDeclaration>) { @@ -92,13 +98,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<Any> = listOf(\n") + it.write(" val isDev: Boolean = at.hannibal2.skyhanni.utils.system.PlatformUtils.isDevEnvironment\n") + it.write(" val modules: List<Any> = 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 cb6b0eae4..c854a572c 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 devOnly: 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 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<String>, - 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<String>) = 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 <reified T : Enum<T>> 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<String, ModContainer> 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) |