diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.java b/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.java deleted file mode 100644 index e75e1012..00000000 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.java +++ /dev/null @@ -1,98 +0,0 @@ -package dev.openrs2.deob.ast; - -import java.nio.file.Path; -import java.nio.file.Paths; - -import com.github.javaparser.ParserConfiguration; -import com.github.javaparser.printer.PrettyPrinter; -import com.github.javaparser.printer.PrettyPrinterConfiguration; -import com.github.javaparser.symbolsolver.JavaSymbolSolver; -import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver; -import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver; -import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver; -import com.github.javaparser.utils.SourceRoot; -import com.google.common.collect.ImmutableList; -import dev.openrs2.deob.ast.transform.AddSubTransformer; -import dev.openrs2.deob.ast.transform.BinaryExprOrderTransformer; -import dev.openrs2.deob.ast.transform.BitMaskTransformer; -import dev.openrs2.deob.ast.transform.ComplementTransformer; -import dev.openrs2.deob.ast.transform.EncloseTransformer; -import dev.openrs2.deob.ast.transform.IfElseTransformer; -import dev.openrs2.deob.ast.transform.NegativeLiteralTransformer; -import dev.openrs2.deob.ast.transform.NewInstanceTransformer; -import dev.openrs2.deob.ast.transform.TernaryTransformer; -import dev.openrs2.deob.ast.transform.Transformer; -import dev.openrs2.deob.ast.transform.UnencloseTransformer; -import dev.openrs2.deob.ast.transform.ValueOfTransformer; - -public final class AstDeobfuscator { - private static final ImmutableList TRANSFORMERS = ImmutableList.of( - new UnencloseTransformer(), - new NegativeLiteralTransformer(), - new ComplementTransformer(), - new IfElseTransformer(), - new TernaryTransformer(), - new BinaryExprOrderTransformer(), - new AddSubTransformer(), - new BitMaskTransformer(), - new ValueOfTransformer(), - new NewInstanceTransformer(), - new EncloseTransformer() - ); - - public static void main(String[] args) { - var deobfuscator = new AstDeobfuscator(ImmutableList.of( - Paths.get("nonfree/client/src/main/java"), - Paths.get("nonfree/gl/src/main/java"), - Paths.get("nonfree/gl-dri/src/main/java"), - Paths.get("nonfree/loader/src/main/java"), - Paths.get("nonfree/signlink/src/main/java"), - Paths.get("nonfree/unpack/src/main/java"), - Paths.get("nonfree/unpacker/src/main/java") - )); - deobfuscator.run(); - } - - private final ImmutableList modules; - - public AstDeobfuscator(ImmutableList modules) { - this.modules = modules; - } - - public void run() { - var solver = new CombinedTypeSolver(new ReflectionTypeSolver(true)); - for (var module : modules) { - solver.add(new JavaParserTypeSolver(module)); - } - - var config = new ParserConfiguration() - .setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_8) - .setSymbolResolver(new JavaSymbolSolver(solver)); - - var printerConfig = new PrettyPrinterConfiguration() - .setIndentType(PrettyPrinterConfiguration.IndentType.TABS_WITH_SPACE_ALIGN) - .setIndentSize(1); - - var printer = new PrettyPrinter(printerConfig); - - for (var module : modules) { - var root = new SourceRoot(module, config); - - var results = root.tryToParseParallelized(); - for (var result : results) { - if (!result.isSuccessful()) { - throw new IllegalArgumentException(result.toString()); - } - } - - root.getCompilationUnits().forEach(unit -> { - TRANSFORMERS.forEach(transformer -> { - transformer.transform(unit); - }); - }); - - root.setPrinter(printer::print); - root.saveAll(); - } - } -} diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.kt new file mode 100644 index 00000000..7bda7d9b --- /dev/null +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.kt @@ -0,0 +1,83 @@ +package dev.openrs2.deob.ast + +import com.github.javaparser.ParserConfiguration +import com.github.javaparser.printer.PrettyPrinter +import com.github.javaparser.printer.PrettyPrinterConfiguration +import com.github.javaparser.symbolsolver.JavaSymbolSolver +import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver +import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver +import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver +import com.github.javaparser.utils.SourceRoot +import dev.openrs2.deob.ast.transform.* +import java.nio.file.Path +import java.nio.file.Paths +import java.util.function.Function + +class AstDeobfuscator(private val modules: List) { + fun run() { + val solver = CombinedTypeSolver(ReflectionTypeSolver(true)) + for (module in modules) { + solver.add(JavaParserTypeSolver(module)) + } + + val config = ParserConfiguration() + .setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_8) + .setSymbolResolver(JavaSymbolSolver(solver)) + + val printerConfig = PrettyPrinterConfiguration() + .setIndentType(PrettyPrinterConfiguration.IndentType.TABS_WITH_SPACE_ALIGN) + .setIndentSize(1) + + val printer = PrettyPrinter(printerConfig) + + for (module in modules) { + val root = SourceRoot(module, config) + + val results = root.tryToParseParallelized() + for (result in results) { + require(result.isSuccessful) { result } + } + + root.compilationUnits.forEach { unit -> + TRANSFORMERS.forEach { transformer -> + transformer.transform(unit) + } + } + + root.printer = Function(printer::print) + root.saveAll() + } + } + + companion object { + private val TRANSFORMERS = listOf( + UnencloseTransformer(), + NegativeLiteralTransformer(), + ComplementTransformer(), + IfElseTransformer(), + TernaryTransformer(), + BinaryExprOrderTransformer(), + AddSubTransformer(), + BitMaskTransformer(), + ValueOfTransformer(), + NewInstanceTransformer(), + EncloseTransformer() + ) + + @JvmStatic + fun main(args: Array) { + val deobfuscator = AstDeobfuscator( + listOf( + Paths.get("nonfree/client/src/main/java"), + Paths.get("nonfree/gl/src/main/java"), + Paths.get("nonfree/gl-dri/src/main/java"), + Paths.get("nonfree/loader/src/main/java"), + Paths.get("nonfree/signlink/src/main/java"), + Paths.get("nonfree/unpack/src/main/java"), + Paths.get("nonfree/unpacker/src/main/java") + ) + ) + deobfuscator.run() + } + } +}