Add multi-pass Transformer support

bzip2
Graham 5 years ago
parent c9acb16e62
commit 9b9bdeae63
  1. 43
      asm/src/main/java/dev/openrs2/asm/transform/Transformer.java
  2. 4
      deob/src/main/java/dev/openrs2/deob/transform/BitShiftTransformer.java
  3. 8
      deob/src/main/java/dev/openrs2/deob/transform/CanvasTransformer.java
  4. 4
      deob/src/main/java/dev/openrs2/deob/transform/ClassForNameTransformer.java
  5. 4
      deob/src/main/java/dev/openrs2/deob/transform/CounterTransformer.java
  6. 4
      deob/src/main/java/dev/openrs2/deob/transform/ExceptionTracingTransformer.java
  7. 3
      deob/src/main/java/dev/openrs2/deob/transform/FieldOrderTransformer.java
  8. 4
      deob/src/main/java/dev/openrs2/deob/transform/OpaquePredicateTransformer.java
  9. 14
      deob/src/main/java/dev/openrs2/deob/transform/OriginalNameTransformer.java

@ -11,23 +11,28 @@ public abstract class Transformer {
public void transform(ClassPath classPath) throws AnalyzerException { public void transform(ClassPath classPath) throws AnalyzerException {
preTransform(classPath); preTransform(classPath);
for (var library : classPath.getLibraries()) { boolean changed;
for (var clazz : library) { do {
transformClass(clazz); changed = false;
for (var field : clazz.fields) { for (var library : classPath.getLibraries()) {
transformField(clazz, field); for (var clazz : library) {
} changed |= transformClass(clazz);
for (var field : clazz.fields) {
changed |= transformField(clazz, field);
}
for (var method : clazz.methods) { for (var method : clazz.methods) {
transformMethod(clazz, method); changed |= transformMethod(clazz, method);
if ((method.access & (Opcodes.ACC_NATIVE | Opcodes.ACC_ABSTRACT)) == 0) { if ((method.access & (Opcodes.ACC_NATIVE | Opcodes.ACC_ABSTRACT)) == 0) {
transformCode(clazz, method); changed |= transformCode(clazz, method);
}
} }
} }
} }
} } while (changed);
postTransform(classPath); postTransform(classPath);
} }
@ -36,20 +41,20 @@ public abstract class Transformer {
/* empty */ /* empty */
} }
public void transformClass(ClassNode clazz) throws AnalyzerException { public boolean transformClass(ClassNode clazz) throws AnalyzerException {
/* empty */ return false;
} }
public void transformField(ClassNode clazz, FieldNode field) throws AnalyzerException { public boolean transformField(ClassNode clazz, FieldNode field) throws AnalyzerException {
/* empty */ return false;
} }
public void transformMethod(ClassNode clazz, MethodNode method) throws AnalyzerException { public boolean transformMethod(ClassNode clazz, MethodNode method) throws AnalyzerException {
/* empty */ return false;
} }
public void transformCode(ClassNode clazz, MethodNode method) throws AnalyzerException { public boolean transformCode(ClassNode clazz, MethodNode method) throws AnalyzerException {
/* empty */ return false;
} }
public void postTransform(ClassPath classPath) throws AnalyzerException { public void postTransform(ClassPath classPath) throws AnalyzerException {

@ -26,7 +26,7 @@ public final class BitShiftTransformer extends Transformer {
} }
@Override @Override
public void transformCode(ClassNode clazz, MethodNode method) { public boolean transformCode(ClassNode clazz, MethodNode method) {
CONST_SHIFT_MATCHER.match(method).forEach(match -> { CONST_SHIFT_MATCHER.match(method).forEach(match -> {
var push = match.get(0); var push = match.get(0);
var bits = InsnNodeUtils.getIntConstant(push); var bits = InsnNodeUtils.getIntConstant(push);
@ -39,6 +39,8 @@ public final class BitShiftTransformer extends Transformer {
simplified++; simplified++;
} }
}); });
return false;
} }
@Override @Override

@ -6,15 +6,17 @@ import org.objectweb.asm.tree.ClassNode;
public final class CanvasTransformer extends Transformer { public final class CanvasTransformer extends Transformer {
@Override @Override
public void transformClass(ClassNode clazz) { public boolean transformClass(ClassNode clazz) {
if (!"java/awt/Canvas".equals(clazz.superName)) { if (!"java/awt/Canvas".equals(clazz.superName)) {
return; return false;
} }
if ((clazz.access & Opcodes.ACC_FINAL) == 0) { if ((clazz.access & Opcodes.ACC_FINAL) == 0) {
return; return false;
} }
clazz.interfaces.remove("java/awt/event/FocusListener"); clazz.interfaces.remove("java/awt/event/FocusListener");
return false;
} }
} }

@ -33,7 +33,7 @@ public final class ClassForNameTransformer extends Transformer {
} }
@Override @Override
public void transformCode(ClassNode clazz, MethodNode method) { public boolean transformCode(ClassNode clazz, MethodNode method) {
INVOKE_MATCHER.match(method).filter(ClassForNameTransformer::isClassForName).forEach(match -> { INVOKE_MATCHER.match(method).filter(ClassForNameTransformer::isClassForName).forEach(match -> {
var ldc = (LdcInsnNode) match.get(0); var ldc = (LdcInsnNode) match.get(0);
var name = remapper.map((String) ldc.cst); var name = remapper.map((String) ldc.cst);
@ -41,5 +41,7 @@ public final class ClassForNameTransformer extends Transformer {
ldc.cst = name; ldc.cst = name;
} }
}); });
return false;
} }
} }

@ -94,7 +94,7 @@ public final class CounterTransformer extends Transformer {
} }
@Override @Override
public void transformCode(ClassNode clazz, MethodNode method) { public boolean transformCode(ClassNode clazz, MethodNode method) {
RESET_PATTERN.match(method).forEach(match -> { RESET_PATTERN.match(method).forEach(match -> {
var putstatic = (FieldInsnNode) match.get(1); var putstatic = (FieldInsnNode) match.get(1);
if (counters.contains(new MemberRef(putstatic.owner, putstatic.name, putstatic.desc))) { if (counters.contains(new MemberRef(putstatic.owner, putstatic.name, putstatic.desc))) {
@ -111,6 +111,8 @@ public final class CounterTransformer extends Transformer {
match.forEach(method.instructions::remove); match.forEach(method.instructions::remove);
} }
}); });
return false;
} }
@Override @Override

@ -22,7 +22,7 @@ public final class ExceptionTracingTransformer extends Transformer {
} }
@Override @Override
public void transformCode(ClassNode clazz, MethodNode method) { public boolean transformCode(ClassNode clazz, MethodNode method) {
CATCH_MATCHER.match(method).forEach(match -> { CATCH_MATCHER.match(method).forEach(match -> {
var foundTryCatch = method.tryCatchBlocks.removeIf(tryCatch -> { var foundTryCatch = method.tryCatchBlocks.removeIf(tryCatch -> {
if (!"java/lang/RuntimeException".equals(tryCatch.type)) { if (!"java/lang/RuntimeException".equals(tryCatch.type)) {
@ -36,6 +36,8 @@ public final class ExceptionTracingTransformer extends Transformer {
tracingTryCatches++; tracingTryCatches++;
} }
}); });
return false;
} }
@Override @Override

@ -45,8 +45,9 @@ public final class FieldOrderTransformer extends Transformer {
} }
@Override @Override
public void transformClass(ClassNode clazz) { public boolean transformClass(ClassNode clazz) {
sortFields(clazz, CONSTRUCTOR, Opcodes.PUTFIELD); sortFields(clazz, CONSTRUCTOR, Opcodes.PUTFIELD);
sortFields(clazz, STATIC_CONSTRUCTOR, Opcodes.PUTSTATIC); sortFields(clazz, STATIC_CONSTRUCTOR, Opcodes.PUTSTATIC);
return false;
} }
} }

@ -91,7 +91,7 @@ public final class OpaquePredicateTransformer extends Transformer {
} }
@Override @Override
public void transformCode(ClassNode clazz, MethodNode method) { public boolean transformCode(ClassNode clazz, MethodNode method) {
/* find and fix opaque predicates */ /* find and fix opaque predicates */
OPAQUE_PREDICATE_MATCHER.match(method).filter(match -> isOpaquePredicate(method, match)).forEach(match -> { OPAQUE_PREDICATE_MATCHER.match(method).filter(match -> isOpaquePredicate(method, match)).forEach(match -> {
var branch = (JumpInsnNode) match.get(1); var branch = (JumpInsnNode) match.get(1);
@ -113,6 +113,8 @@ public final class OpaquePredicateTransformer extends Transformer {
match.forEach(method.instructions::remove); match.forEach(method.instructions::remove);
stores++; stores++;
}); });
return false;
} }
@Override @Override

@ -37,26 +37,30 @@ public final class OriginalNameTransformer extends Transformer {
} }
@Override @Override
public void transformClass(ClassNode clazz) { public boolean transformClass(ClassNode clazz) {
if (clazz.invisibleAnnotations == null) { if (clazz.invisibleAnnotations == null) {
clazz.invisibleAnnotations = new ArrayList<>(); clazz.invisibleAnnotations = new ArrayList<>();
} }
clazz.invisibleAnnotations.add(createOriginalClassAnnotation(clazz.name)); clazz.invisibleAnnotations.add(createOriginalClassAnnotation(clazz.name));
return false;
} }
@Override @Override
public void transformField(ClassNode clazz, FieldNode field) { public boolean transformField(ClassNode clazz, FieldNode field) {
if (field.invisibleAnnotations == null) { if (field.invisibleAnnotations == null) {
field.invisibleAnnotations = new ArrayList<>(); field.invisibleAnnotations = new ArrayList<>();
} }
field.invisibleAnnotations.add(createOriginalMemberAnnotation(clazz.name, field.name, field.desc)); field.invisibleAnnotations.add(createOriginalMemberAnnotation(clazz.name, field.name, field.desc));
return false;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public void transformMethod(ClassNode clazz, MethodNode method) { public boolean transformMethod(ClassNode clazz, MethodNode method) {
if (method.name.equals("<init>") || method.name.equals("<clinit>")) { if (method.name.equals("<init>") || method.name.equals("<clinit>")) {
return; return false;
} }
if (method.invisibleAnnotations == null) { if (method.invisibleAnnotations == null) {
@ -75,5 +79,7 @@ public final class OriginalNameTransformer extends Transformer {
} }
annotations.add(createOriginalArgAnnotation(i)); annotations.add(createOriginalArgAnnotation(i));
} }
return false;
} }
} }

Loading…
Cancel
Save