File size: 3,520 Bytes
d517dc9
 
 
 
1
2
3
4
5
{"type": "session", "harness": "opencode", "id": "mc-0015", "name": "How the refmap remaps mixin targets across mapping systems"}
{"type": "message", "message": {"role": "system", "content": "You are an expert SpongePowered Mixin internals developer. You understand how mixins work at the bytecode level using ASM, and can debug, write, and optimize mixins."}}
{"type": "message", "message": {"role": "user", "content": "How does the refmap remap mixin targets across different mapping systems (Yarn, MCP, Mojang, SRG)?"}}
{"type": "message", "message": {"role": "assistant", "content": "The refmap is a JSON file that maps human-readable mixin targets to their obfuscated names. It is generated at compile time and applied at runtime.\n\nRefmap structure:\n\n```json\n{\"refmap\":\"mymod-refmap.json\",\"minVersion\":{\"all\":\"0.8.0\"},\"migration\":{\"obf\":{\"package\":{\"net/minecraft/entity/LivingEntity\":\"net.minecraft.class_1234\"},\"class\":{\"LivingEntity\":\"class_1234\"},\"field\":{\"net/minecraft/entity/LivingEntity.health\":\"field_12345\"},\"method\":{\"net/minecraft/entity/LivingEntity.getHealth()F\":\"method_6789\"}},\"named\":{\"package\":{},\"class\":{},\"field\":{},\"method\":{}}}}\n```\n\nGeneration process:\n\n```java\n// Annotation processor at compile time:\npublic class MixinAnnotationProcessor extends AbstractProcessor {\n    @Override\n    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {\n        for (Element element : roundEnv.getElementsAnnotatedWith(Mixin.class)) {\n            Mixin mixin = element.getAnnotation(Mixin.class);\n            Class<?> targetClass = mixin.value();\n            for (ExecutableElement method : ElementFilter.methodsIn(element.getEnclosedElements())) {\n                Inject inject = method.getAnnotation(Inject.class);\n                if (inject != null) {\n                    String targetMethod = inject.method();\n                    String mappedTarget = mapMethod(targetClass, targetMethod);\n                    refmap.put(method.getSimpleName(), mappedTarget);\n                }\n            }\n        }\n        writeRefmap(refmap);\n    }\n}\n```\n\nRuntime remapping:\n\n```java\n// RefMapLoader reads and applies the refmap:\npublic class RefMapLoader {\n    public String remapDescriptor(String original, Map<String, String> mapping) {\n        String className = extractClassName(original);\n        String methodName = extractMethodName(original);\n        String desc = extractDescriptor(original);\n        String mappedMethod = mapping.get(className + \".\" + methodName);\n        if (mappedMethod != null) {\n            return className.replace(className, mappedClassName) + \";\" + mappedMethod + desc;\n        }\n        return original;\n    }\n}\n```\n\nApplied at mixin load:\n\n```java\n// When a mixin is loaded:\nMixinInfo mixinInfo = loadMixinInfo(mixinClassName);\nRefMap refMap = loadRefMap(mixinInfo.getRefMap());\nfor (AnnotationNode annotation : mixinNode.visibleAnnotations) {\n    if (annotation.desc.equals(\"Lorg/spongepowered/asm/mixin/Inject;\")) {\n        String method = (String) annotation.values.get(\"method\");\n        String remapped = refMap.remapMethod(method, currentEnvironment);\n        annotation.values.set(indexOf(\"method\"), remapped);\n    }\n}\n```\n\nThe refmap ensures mixins written against Yarn names work on obfuscated servers. The environment determines which mapping set to use, and the refmap provides the translation."}}