Skip to content

Commit

Permalink
refactor and simplify saving output logic
Browse files Browse the repository at this point in the history
  • Loading branch information
EpicPlayerA10 committed Oct 12, 2024
1 parent b5218d4 commit 79375c6
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 128 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The project is structured as follows:
- [`deobfuscator-api`](./deobfuscator-api) - The API for the deobfuscator.
- [`deobfuscator-impl`](./deobfuscator-impl) - The main deobfuscator runner.
- [`deobfuscator-transformers`](./deobfuscator-transformers) - Transformers for the deobfuscator.
- [`reverse-engineering`](./deobfuscator-impl/src/test/java/reverseengineering) - A place where you can throw your reverse-engineered classes. More info [here](./deobfuscator-impl/src/test/java/reverseengineering/README.md)
- [`reverse-engineering`](./reverse-engineering) - A place where you can throw your reverse-engineered classes (for example: complex number pool, complex string pool). You can reference them inside your transformers to make them more readable.
- [`testData`](./testData) - Tests for transformers
- [`src/java`](./testData/src/java) - You can write your java code to test transformers
- [`compiled/custom-classes`](./testData/compiled/custom-classes) - Compiled classes to test transformers. You can throw here classes from your obfuscated jars.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,21 @@ public class ClassWrapper implements Cloneable {
private final ClassNode classNode;
private final FieldCache fieldCache;
private final ConstantPool constantPool;
private final int classWriterFlags;

public ClassWrapper(String pathInJar, ClassReader classReader, int classReaderFlags, int classWriterFlags) {
public ClassWrapper(String pathInJar, ClassReader classReader, int classReaderFlags) {
this.pathInJar = pathInJar;
this.classNode = new ClassNode();
this.constantPool = new ConstantPool(classReader);
this.fieldCache = new FieldCache();
this.classWriterFlags = classWriterFlags;

classReader.accept(this.classNode, classReaderFlags);
}

private ClassWrapper(String pathInJar, ClassNode classNode, FieldCache fieldCache, ConstantPool constantPool, int classWriterFlags) {
private ClassWrapper(String pathInJar, ClassNode classNode, FieldCache fieldCache, ConstantPool constantPool) {
this.pathInJar = pathInJar;
this.classNode = classNode;
this.fieldCache = fieldCache;
this.constantPool = constantPool;
this.classWriterFlags = classWriterFlags;
}

public Optional<MethodNode> findMethod(String name, String desc) {
Expand Down Expand Up @@ -129,9 +126,9 @@ public String canonicalName() {
/**
* Compiles class to bytes.
*/
public byte[] compileToBytes(InheritanceGraph inheritanceGraph) {
public byte[] compileToBytes(InheritanceGraph inheritanceGraph, int classWriterFlags) {
try {
ClassWriter classWriter = new InheritanceClassWriter(this.classWriterFlags, inheritanceGraph);
ClassWriter classWriter = new InheritanceClassWriter(classWriterFlags, inheritanceGraph);
this.classNode.accept(classWriter);

return classWriter.toByteArray();
Expand Down Expand Up @@ -166,6 +163,6 @@ public ConstantPool getConstantPool() {

@Override
public ClassWrapper clone() {
return new ClassWrapper(this.pathInJar, ClassHelper.copy(classNode), fieldCache.clone(), constantPool.clone(), this.classWriterFlags);
return new ClassWrapper(this.pathInJar, ClassHelper.copy(classNode), fieldCache.clone(), constantPool.clone());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public record DeobfuscatorOptions(

boolean printStacktraces,
boolean continueOnError,
boolean verifyBytecode
boolean verifyBytecode,
boolean skipFiles
) {
public static DeobfuscatorOptions.Builder builder() {
return new DeobfuscatorOptions.Builder();
Expand Down Expand Up @@ -74,6 +75,7 @@ public static class Builder {
private boolean printStacktraces = true;
private boolean continueOnError = false;
private boolean verifyBytecode = false;
private boolean skipFiles = false;

private Builder() {
}
Expand Down Expand Up @@ -222,6 +224,15 @@ public DeobfuscatorOptions.Builder verifyBytecode() {
return this;
}

/**
* Skips files during saving.
*/
@Contract(" -> this")
public DeobfuscatorOptions.Builder skipFiles() {
this.skipFiles = true;
return this;
}

/**
* Build immutable {@link DeobfuscatorOptions} with options verification
*/
Expand Down Expand Up @@ -252,7 +263,8 @@ public DeobfuscatorOptions build() {
// Other config
printStacktraces,
continueOnError,
verifyBytecode
verifyBytecode,
skipFiles
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ public static boolean isClass(byte[] bytes) {
* @param pathInJar Relative path of a class in a jar
* @param bytes Class bytes
* @param classReaderFlags {@link ClassReader} flags
* @param classWriterFlags {@link ClassWriter} flags
*/
public static ClassWrapper loadClass(
String pathInJar,
byte[] bytes,
@MagicConstant(flagsFromClass = ClassReader.class) int classReaderFlags,
@MagicConstant(flagsFromClass = ClassWriter.class) int classWriterFlags
@MagicConstant(flagsFromClass = ClassReader.class) int classReaderFlags
) {
return new ClassWrapper(pathInJar, new ClassReader(bytes), classReaderFlags, classWriterFlags);
return new ClassWrapper(pathInJar, new ClassReader(bytes), classReaderFlags);
}

/**
Expand All @@ -51,18 +49,16 @@ public static ClassWrapper loadClass(
* @param pathInJar Relative path of a class in a jar
* @param bytes Class bytes
* @param classReaderFlags {@link ClassReader} flags
* @param classWriterFlags {@link ClassWriter} flags
*/
public static ClassWrapper loadUnknownClass(
String pathInJar,
byte[] bytes,
@MagicConstant(flagsFromClass = ClassReader.class) int classReaderFlags,
@MagicConstant(flagsFromClass = ClassWriter.class) int classWriterFlags
@MagicConstant(flagsFromClass = ClassReader.class) int classReaderFlags
) throws InvalidClassException {
// Fix class
bytes = fixClass(bytes);

return loadClass(pathInJar, bytes, classReaderFlags, classWriterFlags);
return loadClass(pathInJar, bytes, classReaderFlags);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,31 @@
import java.nio.file.attribute.BasicFileAttributes;
import java.util.function.BiConsumer;
import java.util.jar.JarFile;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public final class FileHelper {

private static final Logger LOGGER = LogManager.getLogger(FileHelper.class);

private FileHelper() {}
private FileHelper() {
}

public static void loadFilesFromZip(Path path, BiConsumer<String, byte[]> consumer) {
try (JarFile zipFile = new JarFile(path.toFile())) {
zipFile
.entries()
zipFile.entries()
.asIterator()
.forEachRemaining(
zipEntry -> {
try {
consumer.accept(zipEntry.getName(), zipFile.getInputStream(zipEntry).readAllBytes());
} catch (Exception e) {
LOGGER.error("Could not load ZipEntry: {}", zipEntry.getName());
LOGGER.debug("Error", e);
}
});
.forEachRemaining(zipEntry -> {
if (zipEntry.isDirectory()) return;

try {
consumer.accept(zipEntry.getName(), zipFile.getInputStream(zipEntry).readAllBytes());
} catch (Exception e) {
LOGGER.error("Could not load ZipEntry: {}", zipEntry.getName());
LOGGER.debug("Error", e);
}
});
} catch (Exception e) {
LOGGER.error("Could not load file: {}", path);
throw new RuntimeException(e);
Expand Down
Loading

0 comments on commit 79375c6

Please sign in to comment.