Skip to content

Commit

Permalink
Removed unused maven module, did some small code refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
narumii committed Aug 25, 2024
1 parent 1e39f23 commit 90c38ad
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.function.BiConsumer;
import java.util.jar.JarFile;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -36,22 +39,26 @@ public static void loadFilesFromZip(Path path, BiConsumer<String, byte[]> consum
}
}

public static void deleteDirectory(File file) {
if (!file.exists()) {
return;
}
if (file.isDirectory()) {
File[] files = file.listFiles();
if (files != null) {
for (File f : files) {
deleteDirectory(f);
}
}
}
public static void deleteDirectory(Path dir) {
try {
Files.delete(file.toPath());
} catch (IOException e) {
throw new RuntimeException(e);
if (Files.notExists(dir))
return;

Files.walkFileTree(dir, new SimpleFileVisitor<>() {
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
});
} catch (Exception e) {
throw new RuntimeException("Can't delete directory", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private Deobfuscator(Builder builder) throws FileNotFoundException {
throw new FileNotFoundException("No input files provided");
}

if (builder.outputJar != null && builder.outputJar.toFile().exists())
if (builder.outputJar != null && Files.exists(builder.outputJar))
LOGGER.warn("Output file already exist, data will be overwritten");

this.inputJar = builder.inputJar;
Expand Down Expand Up @@ -194,8 +194,7 @@ private void saveClassesToDir() {
private void saveToJar() {
LOGGER.info("Saving output file: {}", outputJar);

try (ZipOutputStream zipOutputStream =
new ZipOutputStream(new FileOutputStream(outputJar.toFile()))) {
try (ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(outputJar))) {
zipOutputStream.setLevel(9);

context
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uwu.narumii.deobfuscator.base;

import java.nio.charset.StandardCharsets;
import org.jetbrains.java.decompiler.main.extern.IResultSaver;

import java.io.File;
Expand Down Expand Up @@ -40,24 +41,18 @@ public void copyFile(String source, String path, String entryName) {
*/
@Override
public void saveClassFile(String path, String qualifiedName, String entryName, String content, int[] mapping) {
Path saveTo;
if (this.inputType == TestDeobfuscationBase.InputType.CUSTOM_JAR) {
saveTo = Path.of(TestDeobfuscationBase.RESULTS_CLASSES_PATH.toString(), inputType.directory(), this.jarRelativePath, entryName + ".dec");
} else {
saveTo = Path.of(TestDeobfuscationBase.RESULTS_CLASSES_PATH.toString(), inputType.directory(), entryName + ".dec");
}

File fileSaveTo = saveTo.toFile();
Path saveTo = this.inputType == TestDeobfuscationBase.InputType.CUSTOM_JAR
? TestDeobfuscationBase.RESULTS_CLASSES_PATH.resolve(inputType.directory()).resolve(this.jarRelativePath).resolve(entryName + ".dec")
: TestDeobfuscationBase.RESULTS_CLASSES_PATH.resolve(inputType.directory()).resolve(entryName + ".dec");

try {
if (fileSaveTo.exists()) {
if (Files.exists(saveTo)) {
// Assert decompiled code
String oldCode = Files.readString(saveTo);
assertEquals(oldCode, content);
} else {
// Save content
fileSaveTo.getParentFile().mkdirs();

Files.createDirectories(saveTo.getParent());
Files.writeString(saveTo, content);

// Mark that result saver saved content instead of asserting it
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package uwu.narumii.deobfuscator.base;

import java.nio.file.Files;
import java.nio.file.Path;
import org.jetbrains.java.decompiler.main.extern.IContextSource;
import org.jetbrains.java.decompiler.main.extern.IResultSaver;
import org.jetbrains.java.decompiler.util.InterpreterUtil;
Expand All @@ -14,17 +16,17 @@
import java.util.List;

public class SingleClassContextSource implements IContextSource {
private final File file;
private final Path file;
private final String relativePath;
private final String qualifiedName;
private final byte[] contents;

public SingleClassContextSource(File file, String relativePath) {
public SingleClassContextSource(Path file, String relativePath) {
this.file = file;
this.relativePath = relativePath;
try {
// Get qualified name
this.contents = InterpreterUtil.getBytes(file);
this.contents = Files.readAllBytes(file);
ClassWrapper classWrapper = ClassHelper.loadClass(relativePath, this.contents, ClassReader.SKIP_FRAMES, 0);
this.qualifiedName = classWrapper.name();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
@Timeout(60)
public abstract class TestDeobfuscationBase {
public static final Path TEST_DATA_PATH = Path.of("..", "testData");
public static final Path COMPILED_CLASSES_PATH = Path.of(TEST_DATA_PATH.toString(), "compiled");
public static final Path DEOBFUSCATED_CLASSES_PATH = Path.of(TEST_DATA_PATH.toString(), "deobfuscated");
public static final Path RESULTS_CLASSES_PATH = Path.of(TEST_DATA_PATH.toString(), "results");
public static final Path COMPILED_CLASSES_PATH = TEST_DATA_PATH.resolve("compiled");
public static final Path DEOBFUSCATED_CLASSES_PATH = TEST_DATA_PATH.resolve("deobfuscated");
public static final Path RESULTS_CLASSES_PATH = TEST_DATA_PATH.resolve("results");

private final List<RegisteredTest> registeredTests = new ArrayList<>();

Expand Down Expand Up @@ -66,7 +66,7 @@ public static void setup() {
@DisplayName("Test deobfuscation")
public Stream<DynamicTest> testDeobfuscation() {
this.registeredTests.clear();
FileHelper.deleteDirectory(DEOBFUSCATED_CLASSES_PATH.toFile());
FileHelper.deleteDirectory(DEOBFUSCATED_CLASSES_PATH);

this.registerAll();
return this.registeredTests.stream().map(RegisteredTest::buildTest);
Expand All @@ -88,7 +88,7 @@ private void runTest() {
Deobfuscator.Builder deobfuscatorBuilder = Deobfuscator.builder()
.transformers(this.transformers.toArray(new Supplier[0]));

File inputDir = null;
Path inputDir = null;
String jarSource = null;

// Get sources paths
Expand All @@ -102,16 +102,14 @@ private void runTest() {
Path relativePath = Path.of(this.inputType.directory(), sources[0] + ".jar");

// Add jar input
Path inputJarPath = Path.of(COMPILED_CLASSES_PATH.toString(), relativePath.toString());
Path inputJarPath = COMPILED_CLASSES_PATH.resolve(relativePath);
deobfuscatorBuilder.inputJar(inputJarPath);

inputDir = Path.of(DEOBFUSCATED_CLASSES_PATH.toString(), relativePath.toString()).toFile();
inputDir = DEOBFUSCATED_CLASSES_PATH.resolve(relativePath);
} else {
for (String sourceName : sources) {
Path relativePath = Path.of(this.inputType.directory(), sourceName + ".class");
Path compiledClassPath = Path.of(COMPILED_CLASSES_PATH.toString(), relativePath.toString());

if (!compiledClassPath.toFile().exists()) {
Path compiledClassPath = COMPILED_CLASSES_PATH.resolve(this.inputType.directory()).resolve(sourceName + ".class");
if (Files.notExists(compiledClassPath)) {
throw new IllegalArgumentException("File not found: " + compiledClassPath.toAbsolutePath().normalize());
}

Expand All @@ -120,14 +118,12 @@ private void runTest() {
}
}

Path outputDir = Path.of(DEOBFUSCATED_CLASSES_PATH.toString(), this.inputType.directory());

Deobfuscator deobfuscator;
try {
// Build deobfuscator
deobfuscator = deobfuscatorBuilder
.outputJar(null)
.outputDir(outputDir)
.outputDir(DEOBFUSCATED_CLASSES_PATH.resolve(this.inputType.directory()))
.build();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
Expand All @@ -140,10 +136,9 @@ private void runTest() {
List<IContextSource> contextSources = new ArrayList<>();
if (this.inputType != InputType.CUSTOM_JAR) {
for (String sourceName : sources) {
Path relativePath = Path.of(this.inputType.directory(), sourceName + ".class");
Path deobfuscatedClassPath = Path.of(DEOBFUSCATED_CLASSES_PATH.toString(), relativePath.toString());

contextSources.add(new SingleClassContextSource(deobfuscatedClassPath.toFile(), sourceName));
contextSources.add(new SingleClassContextSource(
DEOBFUSCATED_CLASSES_PATH.resolve(this.inputType.directory()).resolve(sourceName + ".class"),
sourceName));
}
}

Expand All @@ -158,7 +153,7 @@ private void runTest() {
* @param inputDir Optionally you can give a whole directory to decompile.
* @param jarRelativePath Specifies a relative path in save directory. Only used for jars
*/
private void assertOutput(List<IContextSource> contextSources, @Nullable File inputDir, @Nullable String jarRelativePath) {
private void assertOutput(List<IContextSource> contextSources, @Nullable Path inputDir, @Nullable String jarRelativePath) {
AssertingResultSaver assertingResultSaver = new AssertingResultSaver(this.inputType, jarRelativePath);

// Decompile classes
Expand All @@ -168,7 +163,7 @@ private void assertOutput(List<IContextSource> contextSources, @Nullable File in

// Add sources
if (this.inputType == InputType.CUSTOM_JAR) {
decompilerBuilder.inputs(inputDir);
decompilerBuilder.inputs(inputDir.toFile()); //fuck you path > file
} else {
for (IContextSource contextSource : contextSources) {
decompilerBuilder.inputs(contextSource);
Expand Down
28 changes: 0 additions & 28 deletions deobfuscator-transformers-analyzer/pom.xml

This file was deleted.

1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
<module>deobfuscator-api</module>
<module>deobfuscator-impl</module>
<module>deobfuscator-transformers</module>
<module>deobfuscator-transformers-analyzer</module>
</modules>

<properties>
Expand Down

0 comments on commit 90c38ad

Please sign in to comment.