From 244ec6d598ed757c7e925390f0317158b8fa6af3 Mon Sep 17 00:00:00 2001 From: joe Date: Fri, 8 Mar 2024 13:42:18 +0000 Subject: [PATCH] Change Fabric Mixin detection. NeoForge now uses Fabric Mixin --- .../platform/fabric/util/fabric-util.kt | 29 ------------------- .../injectionPoint/CtorHeadInjectionPoint.kt | 4 +-- .../injector/CtorHeadNoUnsafeInspection.kt | 6 ++-- .../InjectIntoConstructorInspection.kt | 6 ++-- .../injector/UnnecessaryUnsafeInspection.kt | 6 ++-- src/main/kotlin/platform/mixin/util/Mixin.kt | 3 ++ .../platform/mixin/util/MixinConstants.kt | 2 ++ 7 files changed, 16 insertions(+), 40 deletions(-) delete mode 100644 src/main/kotlin/platform/fabric/util/fabric-util.kt diff --git a/src/main/kotlin/platform/fabric/util/fabric-util.kt b/src/main/kotlin/platform/fabric/util/fabric-util.kt deleted file mode 100644 index 4ce58ea4b..000000000 --- a/src/main/kotlin/platform/fabric/util/fabric-util.kt +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Minecraft Development for IntelliJ - * - * https://mcdev.io/ - * - * Copyright (C) 2024 minecraft-dev - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, version 3.0 only. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -package com.demonwav.mcdev.platform.fabric.util - -import com.demonwav.mcdev.facet.MinecraftFacet -import com.demonwav.mcdev.platform.fabric.FabricModuleType -import com.demonwav.mcdev.util.findModule -import com.intellij.psi.PsiElement - -val PsiElement.isFabric: Boolean get() = - findModule()?.let { MinecraftFacet.getInstance(it) }?.isOfType(FabricModuleType) == true diff --git a/src/main/kotlin/platform/mixin/handlers/injectionPoint/CtorHeadInjectionPoint.kt b/src/main/kotlin/platform/mixin/handlers/injectionPoint/CtorHeadInjectionPoint.kt index a4f608958..bbcc42bb1 100644 --- a/src/main/kotlin/platform/mixin/handlers/injectionPoint/CtorHeadInjectionPoint.kt +++ b/src/main/kotlin/platform/mixin/handlers/injectionPoint/CtorHeadInjectionPoint.kt @@ -20,12 +20,12 @@ package com.demonwav.mcdev.platform.mixin.handlers.injectionPoint -import com.demonwav.mcdev.platform.fabric.util.isFabric import com.demonwav.mcdev.platform.mixin.inspection.injector.CtorHeadNoUnsafeInspection import com.demonwav.mcdev.platform.mixin.reference.MixinSelector import com.demonwav.mcdev.platform.mixin.util.findOrConstructSourceMethod import com.demonwav.mcdev.platform.mixin.util.findSuperConstructorCall import com.demonwav.mcdev.platform.mixin.util.isConstructor +import com.demonwav.mcdev.platform.mixin.util.isFabricMixin import com.demonwav.mcdev.util.createLiteralExpression import com.demonwav.mcdev.util.enumValueOfOrNull import com.demonwav.mcdev.util.findContainingClass @@ -67,7 +67,7 @@ class CtorHeadInjectionPoint : InjectionPoint() { // avoid adding unsafe = true when it's unnecessary on Fabric val noUnsafeInspection = project.findInspection(CtorHeadNoUnsafeInspection.SHORT_NAME) - if (reference.isFabric && noUnsafeInspection?.ignoreForFabric == true) { + if (reference.isFabricMixin && noUnsafeInspection?.ignoreForFabric == true) { return } diff --git a/src/main/kotlin/platform/mixin/inspection/injector/CtorHeadNoUnsafeInspection.kt b/src/main/kotlin/platform/mixin/inspection/injector/CtorHeadNoUnsafeInspection.kt index f3156c7d8..bea20a119 100644 --- a/src/main/kotlin/platform/mixin/inspection/injector/CtorHeadNoUnsafeInspection.kt +++ b/src/main/kotlin/platform/mixin/inspection/injector/CtorHeadNoUnsafeInspection.kt @@ -20,10 +20,10 @@ package com.demonwav.mcdev.platform.mixin.inspection.injector -import com.demonwav.mcdev.platform.fabric.util.isFabric import com.demonwav.mcdev.platform.mixin.inspection.MixinInspection import com.demonwav.mcdev.platform.mixin.inspection.fix.AnnotationAttributeFix import com.demonwav.mcdev.platform.mixin.util.MixinConstants +import com.demonwav.mcdev.platform.mixin.util.isFabricMixin import com.demonwav.mcdev.util.constantStringValue import com.demonwav.mcdev.util.constantValue import com.intellij.codeInspection.ProblemsHolder @@ -41,7 +41,7 @@ class CtorHeadNoUnsafeInspection : MixinInspection() { override fun createOptionsPanel(): JComponent { val panel = JPanel(FlowLayout(FlowLayout.LEFT)) - val checkbox = JCheckBox("Ignore in Fabric mods", ignoreForFabric) + val checkbox = JCheckBox("Ignore when Fabric Mixin is present", ignoreForFabric) checkbox.addActionListener { ignoreForFabric = checkbox.isSelected } @@ -53,7 +53,7 @@ class CtorHeadNoUnsafeInspection : MixinInspection() { override fun buildVisitor(holder: ProblemsHolder): PsiElementVisitor { if (ignoreForFabric) { - val isFabric = holder.file.isFabric + val isFabric = holder.file.isFabricMixin if (isFabric) { return PsiElementVisitor.EMPTY_VISITOR } diff --git a/src/main/kotlin/platform/mixin/inspection/injector/InjectIntoConstructorInspection.kt b/src/main/kotlin/platform/mixin/inspection/injector/InjectIntoConstructorInspection.kt index 578b9076d..d33954645 100644 --- a/src/main/kotlin/platform/mixin/inspection/injector/InjectIntoConstructorInspection.kt +++ b/src/main/kotlin/platform/mixin/inspection/injector/InjectIntoConstructorInspection.kt @@ -20,7 +20,6 @@ package com.demonwav.mcdev.platform.mixin.inspection.injector -import com.demonwav.mcdev.platform.fabric.util.isFabric import com.demonwav.mcdev.platform.mixin.handlers.InjectorAnnotationHandler import com.demonwav.mcdev.platform.mixin.handlers.MixinAnnotationHandler import com.demonwav.mcdev.platform.mixin.handlers.injectionPoint.AtResolver @@ -30,6 +29,7 @@ import com.demonwav.mcdev.platform.mixin.util.MethodTargetMember import com.demonwav.mcdev.platform.mixin.util.MixinConstants.Annotations.INJECT import com.demonwav.mcdev.platform.mixin.util.findSuperConstructorCall import com.demonwav.mcdev.platform.mixin.util.isConstructor +import com.demonwav.mcdev.platform.mixin.util.isFabricMixin import com.demonwav.mcdev.util.constantValue import com.demonwav.mcdev.util.findAnnotation import com.demonwav.mcdev.util.findAnnotations @@ -50,7 +50,7 @@ class InjectIntoConstructorInspection : MixinInspection() { override fun createOptionsPanel(): JComponent { val panel = JPanel(FlowLayout(FlowLayout.LEFT)) - val checkbox = JCheckBox("Always allow @Inject into constructors in Fabric", allowOnFabric) + val checkbox = JCheckBox("Allow @Inject into constructors when Fabric Mixin is present", allowOnFabric) checkbox.addActionListener { allowOnFabric = checkbox.isSelected } @@ -59,7 +59,7 @@ class InjectIntoConstructorInspection : MixinInspection() { } override fun buildVisitor(holder: ProblemsHolder): PsiElementVisitor { - val isFabric = holder.file.isFabric + val isFabric = holder.file.isFabricMixin return object : JavaElementVisitor() { override fun visitMethod(method: PsiMethod) { val injectAnnotation = method.findAnnotation(INJECT) ?: return diff --git a/src/main/kotlin/platform/mixin/inspection/injector/UnnecessaryUnsafeInspection.kt b/src/main/kotlin/platform/mixin/inspection/injector/UnnecessaryUnsafeInspection.kt index ce7ff7a87..7a3ee8dff 100644 --- a/src/main/kotlin/platform/mixin/inspection/injector/UnnecessaryUnsafeInspection.kt +++ b/src/main/kotlin/platform/mixin/inspection/injector/UnnecessaryUnsafeInspection.kt @@ -20,7 +20,6 @@ package com.demonwav.mcdev.platform.mixin.inspection.injector -import com.demonwav.mcdev.platform.fabric.util.isFabric import com.demonwav.mcdev.platform.mixin.handlers.MixinAnnotationHandler import com.demonwav.mcdev.platform.mixin.handlers.injectionPoint.AtResolver import com.demonwav.mcdev.platform.mixin.inspection.MixinInspection @@ -28,6 +27,7 @@ import com.demonwav.mcdev.platform.mixin.inspection.fix.AnnotationAttributeFix import com.demonwav.mcdev.platform.mixin.util.MethodTargetMember import com.demonwav.mcdev.platform.mixin.util.MixinConstants import com.demonwav.mcdev.platform.mixin.util.isConstructor +import com.demonwav.mcdev.platform.mixin.util.isFabricMixin import com.demonwav.mcdev.util.constantValue import com.demonwav.mcdev.util.findInspection import com.demonwav.mcdev.util.ifEmpty @@ -49,7 +49,7 @@ class UnnecessaryUnsafeInspection : MixinInspection() { override fun createOptionsPanel(): JComponent { val panel = JPanel(FlowLayout(FlowLayout.LEFT)) - val checkbox = JCheckBox("Always unnecessary in Fabric mods", alwaysUnnecessaryOnFabric) + val checkbox = JCheckBox("Always unnecessary when Fabric Mixin is present", alwaysUnnecessaryOnFabric) checkbox.addActionListener { alwaysUnnecessaryOnFabric = checkbox.isSelected } @@ -58,7 +58,7 @@ class UnnecessaryUnsafeInspection : MixinInspection() { } override fun buildVisitor(holder: ProblemsHolder): PsiElementVisitor { - val isFabric = holder.file.isFabric + val isFabric = holder.file.isFabricMixin val alwaysUnnecessary = isFabric && alwaysUnnecessaryOnFabric val requiresUnsafeForCtorHeadOnFabric = holder.project.findInspection(CtorHeadNoUnsafeInspection.SHORT_NAME) diff --git a/src/main/kotlin/platform/mixin/util/Mixin.kt b/src/main/kotlin/platform/mixin/util/Mixin.kt index a2ac0a5f9..b7bd17629 100644 --- a/src/main/kotlin/platform/mixin/util/Mixin.kt +++ b/src/main/kotlin/platform/mixin/util/Mixin.kt @@ -234,3 +234,6 @@ fun isMixinEntryPoint(element: PsiElement?): Boolean { } return false } + +val PsiElement.isFabricMixin: Boolean get() = + JavaPsiFacade.getInstance(project).findClass(MixinConstants.Classes.FABRIC_UTIL, resolveScope) != null diff --git a/src/main/kotlin/platform/mixin/util/MixinConstants.kt b/src/main/kotlin/platform/mixin/util/MixinConstants.kt index 937cf44bc..b0e6093c6 100644 --- a/src/main/kotlin/platform/mixin/util/MixinConstants.kt +++ b/src/main/kotlin/platform/mixin/util/MixinConstants.kt @@ -47,6 +47,8 @@ object MixinConstants { const val SERIALIZED_NAME = "com.google.gson.annotations.SerializedName" const val MIXIN_SERIALIZED_NAME = "org.spongepowered.include.$SERIALIZED_NAME" + + const val FABRIC_UTIL = "org.spongepowered.asm.mixin.FabricUtil" } object Annotations {