|
| 1 | +package de.oceanlabs.mcp.mcinjector.adaptors; |
| 2 | + |
| 3 | +import java.util.Collections; |
| 4 | +import java.util.Comparator; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.logging.Logger; |
| 8 | + |
| 9 | +import org.objectweb.asm.ClassVisitor; |
| 10 | +import org.objectweb.asm.MethodVisitor; |
| 11 | +import org.objectweb.asm.Opcodes; |
| 12 | +import org.objectweb.asm.tree.LocalVariableNode; |
| 13 | +import org.objectweb.asm.tree.MethodNode; |
| 14 | + |
| 15 | +import de.oceanlabs.mcp.mcinjector.MCInjectorImpl; |
| 16 | + |
| 17 | +public class LVTLvt extends ClassVisitor |
| 18 | +{ |
| 19 | + private static final Logger log = Logger.getLogger("MCInjector"); |
| 20 | + //private MCInjectorImpl mci; |
| 21 | + String className; |
| 22 | + |
| 23 | + public LVTLvt(ClassVisitor cn, MCInjectorImpl mci) |
| 24 | + { |
| 25 | + super(Opcodes.ASM5, cn); |
| 26 | + //this.mci = mci; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) |
| 31 | + { |
| 32 | + this.className = name; |
| 33 | + super.visit(version, access, name, signature, superName, interfaces); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) |
| 38 | + { |
| 39 | + return new MethodVisitor(api, cv.visitMethod(access, name, desc, signature, exceptions)) |
| 40 | + { |
| 41 | + @Override |
| 42 | + public void visitEnd() |
| 43 | + { |
| 44 | + super.visitEnd(); |
| 45 | + final MethodNode mn = MCInjectorImpl.getMethodNode(mv); |
| 46 | + if (mn.localVariables != null && mn.localVariables.size() > 0) |
| 47 | + { |
| 48 | + Collections.sort(mn.localVariables, new Comparator<LocalVariableNode>() |
| 49 | + { |
| 50 | + @Override |
| 51 | + public int compare(LocalVariableNode o1, LocalVariableNode o2) |
| 52 | + { |
| 53 | + if (o1.index < o2.index) return -1; |
| 54 | + if (o1.index > o2.index) return 1; |
| 55 | + int o1Start = mn.instructions.indexOf(o1.start); |
| 56 | + int o2Start = mn.instructions.indexOf(o2.start); |
| 57 | + if (o1Start < o2Start) return -1; |
| 58 | + if (o2Start > o2Start) return 1; |
| 59 | + return 0; |
| 60 | + } |
| 61 | + |
| 62 | + }); |
| 63 | + |
| 64 | + Map<Integer, Integer> vers = new HashMap<Integer, Integer>(); |
| 65 | + for (LocalVariableNode lvn : mn.localVariables) |
| 66 | + { |
| 67 | + if (0x2603 != lvn.name.charAt(0)) // Snowmen, added in 1.8.2? rename them |
| 68 | + continue; |
| 69 | + int ver = 0; |
| 70 | + if (vers.containsKey(lvn.index)) |
| 71 | + { |
| 72 | + ver = vers.get(lvn.index); |
| 73 | + vers.put(lvn.index, ver + 1); |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + ver = 1; |
| 78 | + vers.put(lvn.index, 2); |
| 79 | + } |
| 80 | + String name = "lvt_" + lvn.index + "_" + ver + "_"; |
| 81 | + log.info(" Renaming LVT: " + lvn.index + " " + lvn.name + " " + lvn.desc + " -> " + name); |
| 82 | + lvn.name = name; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + }; |
| 87 | + } |
| 88 | +} |
0 commit comments