Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Added second variable sorter fix #6

Merged
merged 1 commit into from
Mar 31, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.Type;

import com.google.common.io.ByteStreams;

Expand Down Expand Up @@ -124,7 +125,9 @@ public void processJar(String inFile, String outFile) throws IOException

// Add Out Util class:
String[] extras = {
Util.class.getCanonicalName().replace('.', '/') + ".class"
Type.getInternalName(Util.class) + ".class",
Type.getInternalName(Util.class) + "$1.class",
Type.getInternalName(Util.Indexed.class) + ".class"
};
for (String name : extras)
{
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/net/minecraftforge/lex/fffixer/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,41 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

public class Util
{
public static interface Indexed
{
public int getIndex();
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public static Iterator sortIndexed(Iterator itr)
{
List list = new ArrayList();

while(itr.hasNext())
list.add(itr.next());

Collections.sort(list, new Comparator() {
@Override
public int compare(Object o1, Object o2)
{
if(o1 instanceof Indexed) {
if(o2 instanceof Indexed)
return ((Indexed)o1).getIndex() - ((Indexed)o2).getIndex();
else return -1;
}
else if(o2 instanceof Indexed) return 1;
else return 0;
}
});
return list.iterator();
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T extends Comparable> Iterator<T> sortComparable(Iterator<T> itr)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@
* Simple solution is to hijack the Iterator to make it use a properly sorted one.
* Thanks to fry for finding this issue with class names, which then led me to look for var names.
*
*
* Code injected in aa:
* implements net.minecraftfroge.lex.fffixer.Util.Indexed
* public final int getIndex()
* {
* return this.c;
* }
*
* Code injected in aC:
* implements net.minecraftfroge.lex.fffixer.Util.Indexed
* public final int getIndex()
* {
* return (this.d instanceof Indexed)?((Indexed)this.d).getIndex():-1;
* }
*
* Code injected in bB:
* var = net.minecraftfroge.lex.fffixer.Util.sortIndexed(var)
*
* Code Injected in d:
* var = net.minecraftfroge.lex.fffixer.Util.sortComparable(var);
*
Expand Down Expand Up @@ -45,10 +63,90 @@ public VariableNumberFixer(FFFixerImpl inst)
@Override
public void process(ClassNode node)
{
if (node.name.equals("aa")) fix_aa(node);
if (node.name.equals("aC")) fix_aC(node);
if (node.name.equals("bB")) fix_bB(node);
if (node.name.equals("d" )) fix_d (node);
if (node.name.equals("de")) fixIntPair(node);
}

private void fix_aa(ClassNode node)
{
FFFixerImpl.log.info("Adding index getter to aa");
node.interfaces.add(Type.getInternalName(Util.Indexed.class));

MethodNode mn = new MethodNode(ACC_PUBLIC | ACC_FINAL, "getIndex", "()I", null, null);
mn.visitCode();
mn.visitVarInsn(ALOAD, 0);
mn.visitFieldInsn(GETFIELD, "aa", "c", "I");
mn.visitInsn(IRETURN);
mn.visitEnd();
node.methods.add(mn);

inst.setWorkDone();
}

private void fix_aC(ClassNode node)
{
FFFixerImpl.log.info("Adding index getter to aC");
node.interfaces.add(Type.getInternalName(Util.Indexed.class));

String idx = Type.getInternalName(Util.Indexed.class);
MethodNode mn = new MethodNode(ACC_PUBLIC | ACC_FINAL, "getIndex", "()I", null, null);
mn.visitCode();
mn.visitVarInsn(ALOAD, 0);
mn.visitFieldInsn(GETFIELD, "aC", "d", "LaJ;");
mn.visitTypeInsn(INSTANCEOF, idx);
Label l0 = new Label();
mn.visitJumpInsn(IFEQ, l0);
mn.visitVarInsn(ALOAD, 0);
mn.visitFieldInsn(GETFIELD, "aC", "d", "LaJ;");
mn.visitTypeInsn(CHECKCAST, idx);
mn.visitMethodInsn(INVOKEINTERFACE, idx, "getIndex", "()I", true);
Label l1 = new Label();
mn.visitJumpInsn(GOTO, l1);
mn.visitLabel(l0);
mn.visitInsn(ICONST_M1);
mn.visitLabel(l1);
mn.visitInsn(IRETURN);
mn.visitEnd();
node.methods.add(mn);

inst.setWorkDone();
}

private void fix_bB(ClassNode node)
{
MethodNode mtd = FFFixerImpl.getMethod(node, "a", "(Ljava/util/List;I)Ljava/lang/String;");

Iterator<AbstractInsnNode> itr = mtd.instructions.iterator();
while(itr.hasNext())
{
AbstractInsnNode insn = itr.next();
if (insn instanceof MethodInsnNode)
{
MethodInsnNode v = (MethodInsnNode)insn;
// first iterator call
if(
v.getOpcode() == INVOKEINTERFACE &&
v.owner.equals("java/util/List") &&
v.name.equals("iterator"))
{
FFFixerImpl.log.info("Injecting Var Order Fix in bB");
mtd.instructions.insert(insn, new MethodInsnNode(
INVOKESTATIC,
Type.getInternalName(Util.class),
"sortIndexed",
"(Ljava/util/Iterator;)Ljava/util/Iterator;",
false));

inst.setWorkDone();
return;
}
}
}
}

private void fix_d(ClassNode node)
{
MethodNode mtd = FFFixerImpl.getMethod(node, "b", "(Lcu;Lq;)V");
Expand Down Expand Up @@ -128,4 +226,4 @@ private void fixIntPair(ClassNode node)

inst.setWorkDone();
}
}
}