Skip to content

Commit

Permalink
Fixed Mixin-caused product env Crashing
Browse files Browse the repository at this point in the history
  • Loading branch information
Taskeren committed Aug 25, 2024
1 parent ae9092b commit 8de1bc6
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 2 deletions.
46 changes: 46 additions & 0 deletions src/main/kotlin/cn/taskeren/op/mixin_plugin/OP_Mixin.kt
Original file line number Diff line number Diff line change
@@ -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<OP_TargetMod>): 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)
}
}
98 changes: 98 additions & 0 deletions src/main/kotlin/cn/taskeren/op/mixin_plugin/OP_MixinPlugin.kt
Original file line number Diff line number Diff line change
@@ -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<String> {
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<String?>?,
otherTargets: Set<String?>?,
) {
}

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() {

}

}
3 changes: 1 addition & 2 deletions src/main/resources/mixins.Overpowered.json
Original file line number Diff line number Diff line change
@@ -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"
]
}

0 comments on commit 8de1bc6

Please sign in to comment.