Skip to content

Commit

Permalink
Placeholder mappings API (#2135)
Browse files Browse the repository at this point in the history
  • Loading branch information
Earthcomputer authored Sep 26, 2023
1 parent 44be603 commit 2cc2f48
Show file tree
Hide file tree
Showing 28 changed files with 412 additions and 190 deletions.
1 change: 1 addition & 0 deletions src/main/kotlin/facet/MinecraftFacet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class MinecraftFacet(
}

newlyEnabled.forEach(AbstractModule::init)
modules.forEach(AbstractModule::refresh)

ProjectView.getInstance(module.project).refresh()
}
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/platform/AbstractModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,5 @@ abstract class AbstractModule(protected val facet: MinecraftFacet) {

open fun init() {}
open fun dispose() {}
open fun refresh() {}
}
68 changes: 67 additions & 1 deletion src/main/kotlin/platform/fabric/FabricModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,36 @@ import com.demonwav.mcdev.platform.AbstractModule
import com.demonwav.mcdev.platform.PlatformType
import com.demonwav.mcdev.platform.fabric.reference.EntryPointReference
import com.demonwav.mcdev.platform.fabric.util.FabricConstants
import com.demonwav.mcdev.platform.mcp.fabricloom.FabricLoomData
import com.demonwav.mcdev.platform.mcp.mappings.HardcodedYarnToMojmap
import com.demonwav.mcdev.platform.mcp.mappings.HasCustomNamedMappings
import com.demonwav.mcdev.platform.mcp.mappings.MappingsManager
import com.demonwav.mcdev.util.SourceType
import com.demonwav.mcdev.util.nullable
import com.demonwav.mcdev.util.runCatchingKtIdeaExceptions
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiMethod
import com.intellij.psi.search.searches.ReferencesSearch
import java.io.IOException
import net.fabricmc.mappingio.MappedElementKind
import net.fabricmc.mappingio.MappingReader
import net.fabricmc.mappingio.MappingVisitor
import org.jetbrains.plugins.gradle.util.GradleUtil
import org.jetbrains.uast.UClass
import org.jetbrains.uast.UIdentifier
import org.jetbrains.uast.UMethod
import org.jetbrains.uast.toUElementOfType

class FabricModule internal constructor(facet: MinecraftFacet) : AbstractModule(facet) {
class FabricModule internal constructor(facet: MinecraftFacet) : AbstractModule(facet), HasCustomNamedMappings {

var fabricJson by nullable { facet.findFile(FabricConstants.FABRIC_MOD_JSON, SourceType.RESOURCE) }
private set

private var namedToMojangManagerField: MappingsManager? = null
override val namedToMojangManager: MappingsManager?
get() = namedToMojangManagerField

override val moduleType = FabricModuleType
override val type = PlatformType.FABRIC
override val icon = PlatformAssets.FABRIC_ICON
Expand All @@ -65,6 +78,59 @@ class FabricModule internal constructor(facet: MinecraftFacet) : AbstractModule(
return ReferencesSearch.search(psiParent).anyMatch { EntryPointReference.isEntryPointReference(it) }
}

override fun refresh() {
namedToMojangManagerField = if (detectYarn()) {
MappingsManager.Immediate(HardcodedYarnToMojmap.createMappings())
} else {
null
}
}

private fun detectYarn(): Boolean {
val gradleData = GradleUtil.findGradleModuleData(facet.module) ?: return false
val loomData =
gradleData.children.find { it.key == FabricLoomData.KEY }?.data as? FabricLoomData ?: return false
val mappingsFile = loomData.tinyMappings ?: return false

var yarnDetected = false
val visitor = object : MappingVisitor {
private var namedIndex = -1

override fun visitNamespaces(srcNamespace: String?, dstNamespaces: List<String>) {
namedIndex = dstNamespaces.indexOf("named")
}

override fun visitContent() = namedIndex >= 0

override fun visitClass(srcName: String) = true

override fun visitField(srcName: String?, srcDesc: String?) = false

override fun visitMethod(srcName: String?, srcDesc: String?) = false

override fun visitMethodArg(argPosition: Int, lvIndex: Int, srcName: String?) = false

override fun visitMethodVar(lvtRowIndex: Int, lvIndex: Int, startOpIdx: Int, srcName: String?) = false

override fun visitDstName(targetKind: MappedElementKind?, namespace: Int, name: String) {
if (namespace == namedIndex && name == "net/minecraft/client/MinecraftClient") {
yarnDetected = true
}
}

override fun visitComment(targetKind: MappedElementKind?, comment: String?) {
}
}

try {
MappingReader.read(mappingsFile.toPath(), visitor)
} catch (e: IOException) {
return false
}

return yarnDetected
}

override fun dispose() {
super.dispose()
fabricJson = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ object ReflectedFieldReference : PsiReferenceProvider() {
?: return@withInsertHandler

val srgManager = literal.findModule()?.let { MinecraftFacet.getInstance(it) }
?.getModuleOfType(McpModuleType)?.srgManager
val srgMap = srgManager?.srgMapNow
val srgField = srgMap?.getSrgField(field.simpleQualifiedMemberReference)
?.getModuleOfType(McpModuleType)?.mappingsManager
val srgMap = srgManager?.mappingsNow
val srgField = srgMap?.getIntermediaryField(field.simpleQualifiedMemberReference)
?: return@withInsertHandler

context.setLaterRunnable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ abstract class ReflectedMemberReferenceBasePoly(element: PsiLiteral) : PsiRefere

val name = memberName
val srgManager = element.findModule()?.let { MinecraftFacet.getInstance(it) }
?.getModuleOfType(McpModuleType)?.srgManager
val srgMap = srgManager?.srgMapNow
val mcpName = srgMap?.mapMcpToSrgName(name) ?: name
?.getModuleOfType(McpModuleType)?.mappingsManager
val srgMap = srgManager?.mappingsNow
val mcpName = srgMap?.mapIntermediaryToMapped(name) ?: name

return typeClass.allFields.asSequence()
.filter { it.name == mcpName }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ object ReflectedMethodReference : PsiReferenceProvider() {
?: return@withInsertHandler
val params = literal.parent as? PsiExpressionList ?: return@withInsertHandler
val srgManager = literal.findModule()?.let { MinecraftFacet.getInstance(it) }
?.getModuleOfType(McpModuleType)?.srgManager
val srgMap = srgManager?.srgMapNow
?.getModuleOfType(McpModuleType)?.mappingsManager
val srgMap = srgManager?.mappingsNow

val signature = method.getSignature(PsiSubstitutor.EMPTY)
val returnType = method.returnType?.let { TypeConversionUtil.erasure(it).canonicalText }
Expand All @@ -84,7 +84,7 @@ object ReflectedMethodReference : PsiReferenceProvider() {
.map { it.canonicalText }

val memberRef = method.qualifiedMemberReference
val srgMethod = srgMap?.getSrgMethod(memberRef) ?: memberRef
val srgMethod = srgMap?.getIntermediaryMethod(memberRef) ?: memberRef

context.setLaterRunnable {
// Commit changes made by code completion
Expand Down
14 changes: 7 additions & 7 deletions src/main/kotlin/platform/mcp/McpModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ package com.demonwav.mcdev.platform.mcp
import com.demonwav.mcdev.facet.MinecraftFacet
import com.demonwav.mcdev.platform.AbstractModule
import com.demonwav.mcdev.platform.PlatformType
import com.demonwav.mcdev.platform.mcp.srg.SrgManager
import com.demonwav.mcdev.platform.mcp.mappings.MappingsManager
import com.demonwav.mcdev.platform.mcp.util.McpConstants
import com.demonwav.mcdev.translations.TranslationFileListener
import com.demonwav.mcdev.util.runWriteTaskLater
Expand All @@ -43,7 +43,7 @@ class McpModule(facet: MinecraftFacet) : AbstractModule(facet) {
private val settings: McpModuleSettings = McpModuleSettings.getInstance(module)
val accessTransformers = mutableSetOf<VirtualFile>()

var srgManager: SrgManager? = null
var mappingsManager: MappingsManager? = null
private set

override fun init() {
Expand All @@ -61,8 +61,8 @@ class McpModule(facet: MinecraftFacet) : AbstractModule(facet) {
val file = settings.mappingFile ?: return
val srgType = settings.srgType ?: return

srgManager = SrgManager.getInstance(file, srgType)
srgManager?.parse()
mappingsManager = MappingsManager.getInstance(file, srgType)
mappingsManager?.parse()
}

override val moduleType = McpModuleType
Expand All @@ -78,8 +78,8 @@ class McpModule(facet: MinecraftFacet) : AbstractModule(facet) {
val mappingFile = data.mappingFile ?: return
val srgType = data.srgType ?: return

srgManager = SrgManager.getInstance(mappingFile, srgType)
srgManager?.parse()
mappingsManager = MappingsManager.getInstance(mappingFile, srgType)
mappingsManager?.parse()
}

fun addAccessTransformerFile(file: VirtualFile) {
Expand All @@ -91,6 +91,6 @@ class McpModule(facet: MinecraftFacet) : AbstractModule(facet) {

connection.disconnect()
accessTransformers.clear()
srgManager = null
mappingsManager = null
}
}
14 changes: 7 additions & 7 deletions src/main/kotlin/platform/mcp/actions/CopyAtAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

package com.demonwav.mcdev.platform.mcp.actions

import com.demonwav.mcdev.platform.mcp.srg.McpSrgMap
import com.demonwav.mcdev.platform.mcp.mappings.Mappings
import com.demonwav.mcdev.util.ActionData
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.editor.Editor
Expand All @@ -32,12 +32,12 @@ import java.awt.Toolkit
import java.awt.datatransfer.StringSelection

class CopyAtAction : SrgActionBase() {
override fun withSrgTarget(parent: PsiElement, srgMap: McpSrgMap, e: AnActionEvent, data: ActionData) {
override fun withSrgTarget(parent: PsiElement, srgMap: Mappings, e: AnActionEvent, data: ActionData) {
when (parent) {
is PsiField -> {
val containing = parent.containingClass ?: return showBalloon("No SRG name found", e)
val classSrg = srgMap.getSrgClass(containing) ?: return showBalloon("No SRG name found", e)
val srg = srgMap.getSrgField(parent) ?: return showBalloon("No SRG name found", e)
val classSrg = srgMap.getIntermediaryClass(containing) ?: return showBalloon("No SRG name found", e)
val srg = srgMap.getIntermediaryField(parent) ?: return showBalloon("No SRG name found", e)
copyToClipboard(
data.editor,
data.element,
Expand All @@ -46,16 +46,16 @@ class CopyAtAction : SrgActionBase() {
}
is PsiMethod -> {
val containing = parent.containingClass ?: return showBalloon("No SRG name found", e)
val classSrg = srgMap.getSrgClass(containing) ?: return showBalloon("No SRG name found", e)
val srg = srgMap.getSrgMethod(parent) ?: return showBalloon("No SRG name found", e)
val classSrg = srgMap.getIntermediaryClass(containing) ?: return showBalloon("No SRG name found", e)
val srg = srgMap.getIntermediaryMethod(parent) ?: return showBalloon("No SRG name found", e)
copyToClipboard(
data.editor,
data.element,
classSrg + " " + srg.name + srg.descriptor + " # " + parent.name,
)
}
is PsiClass -> {
val classMcpToSrg = srgMap.getSrgClass(parent) ?: return showBalloon("No SRG name found", e)
val classMcpToSrg = srgMap.getIntermediaryClass(parent) ?: return showBalloon("No SRG name found", e)
copyToClipboard(data.editor, data.element, classMcpToSrg)
}
else -> showBalloon("Not a valid element", e)
Expand Down
14 changes: 7 additions & 7 deletions src/main/kotlin/platform/mcp/actions/CopyCoremodTargetAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

package com.demonwav.mcdev.platform.mcp.actions

import com.demonwav.mcdev.platform.mcp.srg.McpSrgMap
import com.demonwav.mcdev.platform.mcp.mappings.Mappings
import com.demonwav.mcdev.util.ActionData
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.editor.Editor
Expand All @@ -34,12 +34,12 @@ import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive

class CopyCoremodTargetAction : SrgActionBase() {
override fun withSrgTarget(parent: PsiElement, srgMap: McpSrgMap, e: AnActionEvent, data: ActionData) {
override fun withSrgTarget(parent: PsiElement, srgMap: Mappings, e: AnActionEvent, data: ActionData) {
when (parent) {
is PsiField -> {
val containing = parent.containingClass ?: return showBalloon("No SRG name found", e)
val classSrg = srgMap.getSrgClass(containing) ?: return showBalloon("No SRG name found", e)
val srg = srgMap.getSrgField(parent) ?: return showBalloon("No SRG name found", e)
val classSrg = srgMap.getIntermediaryClass(containing) ?: return showBalloon("No SRG name found", e)
val srg = srgMap.getIntermediaryField(parent) ?: return showBalloon("No SRG name found", e)
copyToClipboard(
data.editor,
data.element,
Expand All @@ -50,8 +50,8 @@ class CopyCoremodTargetAction : SrgActionBase() {
}
is PsiMethod -> {
val containing = parent.containingClass ?: return showBalloon("No SRG name found", e)
val classSrg = srgMap.getSrgClass(containing) ?: return showBalloon("No SRG name found", e)
val srg = srgMap.getSrgMethod(parent) ?: return showBalloon("No SRG name found", e)
val classSrg = srgMap.getIntermediaryClass(containing) ?: return showBalloon("No SRG name found", e)
val srg = srgMap.getIntermediaryMethod(parent) ?: return showBalloon("No SRG name found", e)
val srgDescriptor = srg.descriptor ?: return showBalloon("No SRG name found", e)
copyToClipboard(
data.editor,
Expand All @@ -63,7 +63,7 @@ class CopyCoremodTargetAction : SrgActionBase() {
)
}
is PsiClass -> {
val classSrg = srgMap.getSrgClass(parent) ?: return showBalloon("No SRG name found", e)
val classSrg = srgMap.getIntermediaryClass(parent) ?: return showBalloon("No SRG name found", e)
copyToClipboard(
data.editor,
data.element,
Expand Down
10 changes: 5 additions & 5 deletions src/main/kotlin/platform/mcp/actions/FindSrgMappingAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

package com.demonwav.mcdev.platform.mcp.actions

import com.demonwav.mcdev.platform.mcp.srg.McpSrgMap
import com.demonwav.mcdev.platform.mcp.mappings.Mappings
import com.demonwav.mcdev.util.ActionData
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.psi.PsiClass
Expand All @@ -30,18 +30,18 @@ import com.intellij.psi.PsiMethod

class FindSrgMappingAction : SrgActionBase() {

override fun withSrgTarget(parent: PsiElement, srgMap: McpSrgMap, e: AnActionEvent, data: ActionData) {
override fun withSrgTarget(parent: PsiElement, srgMap: Mappings, e: AnActionEvent, data: ActionData) {
when (parent) {
is PsiField -> {
val srg = srgMap.getSrgField(parent) ?: return showBalloon("No SRG name found", e)
val srg = srgMap.getIntermediaryField(parent) ?: return showBalloon("No SRG name found", e)
showSuccessBalloon(data.editor, data.element, "SRG name: " + srg.name)
}
is PsiMethod -> {
val srg = srgMap.getSrgMethod(parent) ?: return showBalloon("No SRG name found", e)
val srg = srgMap.getIntermediaryMethod(parent) ?: return showBalloon("No SRG name found", e)
showSuccessBalloon(data.editor, data.element, "SRG name: " + srg.name + srg.descriptor)
}
is PsiClass -> {
val classMcpToSrg = srgMap.getSrgClass(parent) ?: return showBalloon("No SRG name found", e)
val classMcpToSrg = srgMap.getIntermediaryClass(parent) ?: return showBalloon("No SRG name found", e)
showSuccessBalloon(data.editor, data.element, "SRG name: $classMcpToSrg")
}
else -> showBalloon("Not a valid element", e)
Expand Down
12 changes: 6 additions & 6 deletions src/main/kotlin/platform/mcp/actions/GotoAtEntryAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package com.demonwav.mcdev.platform.mcp.actions

import com.demonwav.mcdev.facet.MinecraftFacet
import com.demonwav.mcdev.platform.mcp.McpModuleType
import com.demonwav.mcdev.platform.mcp.srg.SrgManager
import com.demonwav.mcdev.platform.mcp.mappings.MappingsManager
import com.demonwav.mcdev.platform.mixin.handlers.ShadowHandler
import com.demonwav.mcdev.util.ActionData
import com.demonwav.mcdev.util.getDataFromActionEvent
Expand Down Expand Up @@ -56,12 +56,12 @@ class GotoAtEntryAction : AnAction() {
return
}

val srgManager = data.instance.getModuleOfType(McpModuleType)?.srgManager
val mappingsManager = data.instance.getModuleOfType(McpModuleType)?.mappingsManager
// Not all ATs are in MCP modules, fallback to this if possible
// TODO try to find SRG references for all modules if current module isn't found?
?: SrgManager.findAnyInstance(data.project) ?: return showBalloon(e)
?: MappingsManager.findAnyInstance(data.project) ?: return showBalloon(e)

srgManager.srgMap.onSuccess { srgMap ->
mappingsManager.mappings.onSuccess { srgMap ->
var parent = data.element.parent

if (parent is PsiMember) {
Expand All @@ -73,11 +73,11 @@ class GotoAtEntryAction : AnAction() {

when (parent) {
is PsiField -> {
val reference = srgMap.getSrgField(parent) ?: parent.simpleQualifiedMemberReference
val reference = srgMap.getIntermediaryField(parent) ?: parent.simpleQualifiedMemberReference
searchForText(e, data, reference.name)
}
is PsiMethod -> {
val reference = srgMap.getSrgMethod(parent) ?: parent.qualifiedMemberReference
val reference = srgMap.getIntermediaryMethod(parent) ?: parent.qualifiedMemberReference
searchForText(e, data, reference.name + reference.descriptor)
}
else ->
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/platform/mcp/actions/SrgActionBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
package com.demonwav.mcdev.platform.mcp.actions

import com.demonwav.mcdev.platform.mcp.McpModuleType
import com.demonwav.mcdev.platform.mcp.srg.McpSrgMap
import com.demonwav.mcdev.platform.mcp.mappings.Mappings
import com.demonwav.mcdev.platform.mixin.handlers.ShadowHandler
import com.demonwav.mcdev.util.ActionData
import com.demonwav.mcdev.util.getDataFromActionEvent
Expand Down Expand Up @@ -53,7 +53,7 @@ abstract class SrgActionBase : AnAction() {

val mcpModule = data.instance.getModuleOfType(McpModuleType) ?: return showBalloon("No mappings found", e)

mcpModule.srgManager?.srgMap?.onSuccess { srgMap ->
mcpModule.mappingsManager?.mappings?.onSuccess { srgMap ->
var parent = data.element.parent ?: return@onSuccess showBalloon("Not a valid element", e)

if (parent is PsiMember) {
Expand All @@ -73,7 +73,7 @@ abstract class SrgActionBase : AnAction() {
} ?: showBalloon("No mappings found", e)
}

abstract fun withSrgTarget(parent: PsiElement, srgMap: McpSrgMap, e: AnActionEvent, data: ActionData)
abstract fun withSrgTarget(parent: PsiElement, srgMap: Mappings, e: AnActionEvent, data: ActionData)

companion object {
fun showBalloon(message: String, e: AnActionEvent) {
Expand Down
Loading

0 comments on commit 2cc2f48

Please sign in to comment.