Add multi-pass Transformer support

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

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

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

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

@ -94,7 +94,7 @@ public final class CounterTransformer extends Transformer {
}
@Override
public void transformCode(ClassNode clazz, MethodNode method) {
public boolean transformCode(ClassNode clazz, MethodNode method) {
RESET_PATTERN.match(method).forEach(match -> {
var putstatic = (FieldInsnNode) match.get(1);
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);
}
});
return false;
}
@Override

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

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

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

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

Loading…
Cancel
Save