From 8de1bc62be10476fcebc724fca19cf4863530ff2 Mon Sep 17 00:00:00 2001 From: Taskeren Date: Sun, 25 Aug 2024 11:03:14 +0800 Subject: [PATCH] Fixed Mixin-caused product env Crashing --- .../cn/taskeren/op/mixin_plugin/OP_Mixin.kt | 46 +++++++++ .../op/mixin_plugin/OP_MixinPlugin.kt | 98 +++++++++++++++++++ src/main/resources/mixins.Overpowered.json | 3 +- 3 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/cn/taskeren/op/mixin_plugin/OP_Mixin.kt create mode 100644 src/main/kotlin/cn/taskeren/op/mixin_plugin/OP_MixinPlugin.kt diff --git a/src/main/kotlin/cn/taskeren/op/mixin_plugin/OP_Mixin.kt b/src/main/kotlin/cn/taskeren/op/mixin_plugin/OP_Mixin.kt new file mode 100644 index 0000000..1631c2d --- /dev/null +++ b/src/main/kotlin/cn/taskeren/op/mixin_plugin/OP_Mixin.kt @@ -0,0 +1,46 @@ +@file:Suppress("EnumEntryName") + +package cn.taskeren.op.mixin_plugin + +import java.nio.file.Path +import kotlin.io.path.extension +import kotlin.io.path.nameWithoutExtension + +enum class OP_Mixin(vararg targetModArg: OP_TargetMod, classNameArg: String? = null) { + + Insurance_BaseMetaTileEntity(OP_TargetMod.GregTech), + CrashProof_ModularGui_Mixin(OP_TargetMod.ModularUi), + + ; + + val className = "${classNameArg ?: name}_Mixin" + val targetMod = targetModArg.toList() + + init { + require(targetModArg.isNotEmpty()) { "Mixin must be targeting to at least 1 mod!" } + } + + fun shouldLoad(loadedMods: Collection): Boolean { + return loadedMods.containsAll(targetMod) + } + +} + +/** + * @see gregtech.mixin.TargetedMod + * @see com.kuba6000.mobsinfo.mixin.TargetedMod + * @see com.gtnewhorizons.modularui.mixinplugin.TargetedMod + */ +enum class OP_TargetMod(jarNamePrefixArg: String? = null, val shouldLoadInDev: Boolean = true) { + + GregTech, + ModularUi + + ; + + val jarNamePrefix = (jarNamePrefixArg ?: name).lowercase() + + fun isMatchingJar(path: Path): Boolean { + return path.extension == "jar" && path.nameWithoutExtension.lowercase().startsWith(jarNamePrefix) + } +} diff --git a/src/main/kotlin/cn/taskeren/op/mixin_plugin/OP_MixinPlugin.kt b/src/main/kotlin/cn/taskeren/op/mixin_plugin/OP_MixinPlugin.kt new file mode 100644 index 0000000..5973388 --- /dev/null +++ b/src/main/kotlin/cn/taskeren/op/mixin_plugin/OP_MixinPlugin.kt @@ -0,0 +1,98 @@ +package cn.taskeren.op.mixin_plugin + +import com.gtnewhorizon.gtnhmixins.MinecraftURLClassPath +import net.minecraft.launchwrapper.Launch +import org.apache.logging.log4j.LogManager +import org.spongepowered.asm.lib.tree.ClassNode +import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin +import org.spongepowered.asm.mixin.extensibility.IMixinInfo +import java.io.File +import kotlin.io.path.listDirectoryEntries + +class OP_MixinPlugin : IMixinConfigPlugin { + + companion object { + val LOGGER = LogManager.getLogger() + + val MODS_DIRECTORY_PATH = File(Launch.minecraftHome, "mods/").toPath() + + fun findJarOf(mod: OP_TargetMod): File? { + return MODS_DIRECTORY_PATH.listDirectoryEntries().firstOrNull { mod.isMatchingJar(it) }?.toFile() + } + + fun loadJarOf(mod: OP_TargetMod): Boolean { + try { + val foundJar = findJarOf(mod) + if(foundJar == null) { + LOGGER.error("Unable to find required jar of $mod") + return false + } + + LOGGER.info("Attempt to add jar $foundJar to classloader") + MinecraftURLClassPath.addJar(foundJar) + return true + } catch(e: Exception) { + LOGGER.error("Error occurred while loading jar $mod", e) + return false + } + } + } + + override fun onLoad(mixinPackage: String?) { + OP_MixinConfig.init() + } + + override fun getMixins(): List { + val isDev = Launch.blackboard["fml.deobfuscatedEnvironment"] as Boolean + + val loadedMods = OP_TargetMod.entries.filter { isDev && it.shouldLoadInDev || loadJarOf(it) } + + OP_TargetMod.entries.forEach { + if(loadedMods.contains(it)) { + LOGGER.info("Found ${it.name}, integrating") + } else { + LOGGER.info("Unable to find ${it.name}, integrating skipped") + } + } + + return OP_Mixin.entries.filter { it.shouldLoad(loadedMods) }.map { it.className } + } + + override fun getRefMapperConfig(): String? { + return null + } + + override fun shouldApplyMixin(targetClassName: String?, mixinClassName: String?): Boolean { + return true + } + + override fun acceptTargets( + myTargets: Set?, + otherTargets: Set?, + ) { + } + + override fun preApply( + s: String?, + classNode: ClassNode?, + s1: String?, + iMixinInfo: IMixinInfo?, + ) { + } + + override fun postApply( + s: String?, + classNode: ClassNode?, + s1: String?, + iMixinInfo: IMixinInfo?, + ) { + } +} + +private object OP_MixinConfig { + + fun init() { + + } + +} diff --git a/src/main/resources/mixins.Overpowered.json b/src/main/resources/mixins.Overpowered.json index 9c7cc46..05cc116 100644 --- a/src/main/resources/mixins.Overpowered.json +++ b/src/main/resources/mixins.Overpowered.json @@ -1,10 +1,9 @@ { "required": true, "package": "cn.taskeren.op.mixin", + "plugin": "cn.taskeren.op.mixin_plugin.OP_MixinPlugin", "mixins": [ - "Insurance_BaseMetaTileEntity_Mixin" ], "client": [ - "CrashProof_ModularGui_Mixin" ] }