tinycodinglm / mc /mc-0015.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 16)
d517dc9 verified
|
Raw History Blame Contribute Delete
3.52 kB
opencode trace
System 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.

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.

User

How does the refmap remap mixin targets across different mapping systems (Yarn, MCP, Mojang, SRG)?

Assistant

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.

Refmap structure:

json
{"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":{}}}}

Generation process:

java
// Annotation processor at compile time:
public class MixinAnnotationProcessor extends AbstractProcessor {
    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        for (Element element : roundEnv.getElementsAnnotatedWith(Mixin.class)) {
            Mixin mixin = element.getAnnotation(Mixin.class);
            Class<?> targetClass = mixin.value();
            for (ExecutableElement method : ElementFilter.methodsIn(element.getEnclosedElements())) {
                Inject inject = method.getAnnotation(Inject.class);
                if (inject != null) {
                    String targetMethod = inject.method();
                    String mappedTarget = mapMethod(targetClass, targetMethod);
                    refmap.put(method.getSimpleName(), mappedTarget);
                }
            }
        }
        writeRefmap(refmap);
    }
}

Runtime remapping:

java
// RefMapLoader reads and applies the refmap:
public class RefMapLoader {
    public String remapDescriptor(String original, Map<String, String> mapping) {
        String className = extractClassName(original);
        String methodName = extractMethodName(original);
        String desc = extractDescriptor(original);
        String mappedMethod = mapping.get(className + "." + methodName);
        if (mappedMethod != null) {
            return className.replace(className, mappedClassName) + ";" + mappedMethod + desc;
        }
        return original;
    }
}

Applied at mixin load:

java
// When a mixin is loaded:
MixinInfo mixinInfo = loadMixinInfo(mixinClassName);
RefMap refMap = loadRefMap(mixinInfo.getRefMap());
for (AnnotationNode annotation : mixinNode.visibleAnnotations) {
    if (annotation.desc.equals("Lorg/spongepowered/asm/mixin/Inject;")) {
        String method = (String) annotation.values.get("method");
        String remapped = refMap.remapMethod(method, currentEnvironment);
        annotation.values.set(indexOf("method"), remapped);
    }
}

The 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.