Preserve original class and member names with @OriginalName

pull/48/head
Graham 5 years ago
parent 2e39ca26fd
commit 2367ba7ae4
  1. 17
      asm/src/main/java/dev/openrs2/asm/Transformer.java
  2. 5
      deob/pom.xml
  3. 26
      deob/src/main/java/dev/openrs2/deob/Deobfuscator.java
  4. 2
      deob/src/main/java/dev/openrs2/deob/transform/ClassForNameTransformer.java
  5. 2
      deob/src/main/java/dev/openrs2/deob/transform/OpaquePredicateTransformer.java
  6. 63
      deob/src/main/java/dev/openrs2/deob/transform/OriginalNameTransformer.java

@ -2,6 +2,7 @@ package dev.openrs2.asm;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldNode;
import org.objectweb.asm.tree.MethodNode;
public abstract class Transformer {
@ -11,9 +12,15 @@ public abstract class Transformer {
for (var clazz : library) {
transformClass(clazz);
for (var field : clazz.fields) {
transformField(clazz, field);
}
for (var method : clazz.methods) {
transformMethod(clazz, method);
if ((method.access & (Opcodes.ACC_NATIVE | Opcodes.ACC_ABSTRACT)) == 0) {
transformMethod(clazz, method);
transformCode(clazz, method);
}
}
}
@ -29,10 +36,18 @@ public abstract class Transformer {
/* empty */
}
public void transformField(ClassNode clazz, FieldNode field) {
/* empty */
}
public void transformMethod(ClassNode clazz, MethodNode method) {
/* empty */
}
public void transformCode(ClassNode clazz, MethodNode method) {
/* empty */
}
public void postTransform(Library library) {
/* empty */
}

@ -19,6 +19,11 @@
<artifactId>openrs2-asm</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>dev.openrs2</groupId>
<artifactId>openrs2-deob-annotations</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>

@ -15,6 +15,7 @@ import dev.openrs2.deob.path.ClassPath;
import dev.openrs2.deob.path.TypedRemapper;
import dev.openrs2.deob.transform.ClassForNameTransformer;
import dev.openrs2.deob.transform.OpaquePredicateTransformer;
import dev.openrs2.deob.transform.OriginalNameTransformer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -118,18 +119,33 @@ public final class Deobfuscator {
/* transform Class.forName() calls */
logger.info("Transforming Class.forName() calls");
var transformer = new ClassForNameTransformer(remapper);
Transformer transformer = new ClassForNameTransformer(remapper);
for (var library : libraries) {
transformer.transform(library);
}
var glTransformer = new ClassForNameTransformer(glRemapper);
transformer = new ClassForNameTransformer(glRemapper);
for (var library : glLibraries) {
glTransformer.transform(library);
transformer.transform(library);
}
transformer = new ClassForNameTransformer(unsignedRemapper);
transformer.transform(unsignedClient);
/* add @OriginalName annotations */
logger.info("Annotating classes and members with original names");
transformer = new OriginalNameTransformer(remapper);
for (var library : libraries) {
transformer.transform(library);
}
transformer = new OriginalNameTransformer(glRemapper);
for (var library : glLibraries) {
transformer.transform(library);
}
var unsignedTransformer = new ClassForNameTransformer(unsignedRemapper);
unsignedTransformer.transform(unsignedClient);
transformer = new OriginalNameTransformer(unsignedRemapper);
transformer.transform(unsignedClient);
/* write output jars */
logger.info("Writing output jars");

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

@ -88,7 +88,7 @@ public final class OpaquePredicateTransformer extends Transformer {
}
@Override
public void transformMethod(ClassNode clazz, MethodNode method) {
public void 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);

@ -0,0 +1,63 @@
package dev.openrs2.deob.transform;
import java.util.ArrayList;
import java.util.List;
import dev.openrs2.asm.Transformer;
import dev.openrs2.deob.annotations.OriginalName;
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.Remapper;
import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldNode;
import org.objectweb.asm.tree.MethodNode;
public final class OriginalNameTransformer extends Transformer {
private static AnnotationNode createOriginalNameAnnotation(String name) {
var annotation = new AnnotationNode(Type.getDescriptor(OriginalName.class));
annotation.values = List.of("value", name);
return annotation;
}
private final Remapper remapper;
public OriginalNameTransformer(Remapper remapper) {
this.remapper = remapper;
}
@Override
public void transformClass(ClassNode clazz) {
if (clazz.name.equals(remapper.map(clazz.name))) {
return;
}
if (clazz.invisibleAnnotations == null) {
clazz.invisibleAnnotations = new ArrayList<>();
}
clazz.invisibleAnnotations.add(createOriginalNameAnnotation(clazz.name));
}
@Override
public void transformField(ClassNode clazz, FieldNode field) {
if (field.name.equals(remapper.mapFieldName(clazz.name, field.name, field.desc))) {
return;
}
if (field.invisibleAnnotations == null) {
field.invisibleAnnotations = new ArrayList<>();
}
field.invisibleAnnotations.add(createOriginalNameAnnotation(field.name));
}
@Override
public void transformMethod(ClassNode clazz, MethodNode method) {
if (method.name.equals(remapper.mapMethodName(clazz.name, method.name, method.desc))) {
return;
}
if (method.invisibleAnnotations == null) {
method.invisibleAnnotations = new ArrayList<>();
}
method.invisibleAnnotations.add(createOriginalNameAnnotation(method.name));
}
}
Loading…
Cancel
Save