-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement JUMP injection point (without source navigation)
- Loading branch information
1 parent
2293169
commit 3f2c121
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
src/main/kotlin/platform/mixin/handlers/injectionPoint/JumpInjectionPoint.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* 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 <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.demonwav.mcdev.platform.mixin.handlers.injectionPoint | ||
|
||
import com.demonwav.mcdev.platform.mixin.reference.MixinSelector | ||
import com.demonwav.mcdev.platform.mixin.util.findOrConstructSourceMethod | ||
import com.demonwav.mcdev.util.constantValue | ||
import com.intellij.codeInsight.lookup.LookupElementBuilder | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiAnnotation | ||
import com.intellij.psi.PsiClass | ||
import com.intellij.psi.PsiElement | ||
import org.objectweb.asm.Opcodes | ||
import org.objectweb.asm.tree.ClassNode | ||
import org.objectweb.asm.tree.JumpInsnNode | ||
import org.objectweb.asm.tree.MethodNode | ||
|
||
class JumpInjectionPoint : InjectionPoint<PsiElement>() { | ||
companion object { | ||
private val VALID_OPCODES = setOf( | ||
Opcodes.IFEQ, | ||
Opcodes.IFNE, | ||
Opcodes.IFLT, | ||
Opcodes.IFGE, | ||
Opcodes.IFGT, | ||
Opcodes.IFLE, | ||
Opcodes.IF_ICMPEQ, | ||
Opcodes.IF_ICMPNE, | ||
Opcodes.IF_ICMPLT, | ||
Opcodes.IF_ICMPGE, | ||
Opcodes.IF_ICMPGT, | ||
Opcodes.IF_ICMPLE, | ||
Opcodes.IF_ACMPEQ, | ||
Opcodes.IF_ACMPNE, | ||
Opcodes.GOTO, | ||
Opcodes.JSR, | ||
Opcodes.IFNULL, | ||
Opcodes.IFNONNULL, | ||
) | ||
} | ||
|
||
override fun createNavigationVisitor( | ||
at: PsiAnnotation, | ||
target: MixinSelector?, | ||
targetClass: PsiClass | ||
): NavigationVisitor? { | ||
// TODO: jump target source navigation? This would be extremely hard | ||
return null | ||
} | ||
|
||
override fun doCreateCollectVisitor( | ||
at: PsiAnnotation, | ||
target: MixinSelector?, | ||
targetClass: ClassNode, | ||
mode: CollectVisitor.Mode | ||
): CollectVisitor<PsiElement> { | ||
val opcode = (at.findDeclaredAttributeValue("opcode")?.constantValue as? Int) | ||
?.takeIf { it in VALID_OPCODES } ?: -1 | ||
return MyCollectVisitor(at.project, targetClass, mode, opcode) | ||
} | ||
|
||
override fun createLookup( | ||
targetClass: ClassNode, | ||
result: CollectVisitor.Result<PsiElement> | ||
): LookupElementBuilder? { | ||
return null | ||
} | ||
|
||
private class MyCollectVisitor( | ||
private val project: Project, | ||
private val clazz: ClassNode, | ||
mode: Mode, | ||
private val opcode: Int | ||
) : CollectVisitor<PsiElement>(mode) { | ||
override fun accept(methodNode: MethodNode) { | ||
val insns = methodNode.instructions ?: return | ||
insns.iterator().forEachRemaining { insn -> | ||
if (insn is JumpInsnNode && (opcode == -1 || insn.opcode == opcode)) { | ||
addResult(insn, methodNode.findOrConstructSourceMethod(clazz, project)) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters