From d6fc7b337b654bfd8e59384b8c34c9bd65d4bb2f Mon Sep 17 00:00:00 2001 From: Graham Date: Mon, 23 Dec 2019 21:11:34 +0000 Subject: [PATCH] Convert Decompiler to Kotlin --- .../dev/openrs2/decompiler/Decompiler.java | 76 ------------------- .../java/dev/openrs2/decompiler/Decompiler.kt | 61 +++++++++++++++ 2 files changed, 61 insertions(+), 76 deletions(-) delete mode 100644 decompiler/src/main/java/dev/openrs2/decompiler/Decompiler.java create mode 100644 decompiler/src/main/java/dev/openrs2/decompiler/Decompiler.kt diff --git a/decompiler/src/main/java/dev/openrs2/decompiler/Decompiler.java b/decompiler/src/main/java/dev/openrs2/decompiler/Decompiler.java deleted file mode 100644 index e7a28028ce..0000000000 --- a/decompiler/src/main/java/dev/openrs2/decompiler/Decompiler.java +++ /dev/null @@ -1,76 +0,0 @@ -package dev.openrs2.decompiler; - -import java.io.Closeable; -import java.io.IOException; -import java.nio.file.Path; -import java.nio.file.Paths; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import kotlin.jvm.functions.Function1; -import org.jetbrains.java.decompiler.main.Fernflower; -import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences; - -public final class Decompiler implements Closeable { - private static final ImmutableMap OPTIONS = ImmutableMap.of( - IFernflowerPreferences.ASCII_STRING_CHARACTERS, "1", - IFernflowerPreferences.INDENT_STRING, "\t", - IFernflowerPreferences.SYNTHETIC_NOT_SET, "1" - ); - - private static Path getDestination(String archive) { - archive = archive.replaceAll("(?:_gl)?[.]jar$", ""); - switch (archive) { - case "runescape": - archive = "client"; - break; - case "jaggl": - archive = "gl"; - break; - case "jaggl_dri": - archive = "gl-dri"; - break; - } - return Paths.get("nonfree").resolve(archive).resolve("src/main/java"); - } - - public static void main(String[] args) throws IOException { - var deobOutput = Paths.get("nonfree/code/deob"); - var sources = ImmutableList.of( - deobOutput.resolve("runescape_gl.jar"), - deobOutput.resolve("jaggl.jar"), - deobOutput.resolve("jaggl_dri.jar"), - deobOutput.resolve("loader_gl.jar"), - deobOutput.resolve("signlink_gl.jar"), - deobOutput.resolve("unpack_gl.jar"), - deobOutput.resolve("unpacker_gl.jar") - ); - - try (var decompiler = new Decompiler(sources, Decompiler::getDestination)) { - decompiler.run(); - } - } - - private final DecompilerIo io; - private final Fernflower fernflower; - private final ImmutableList sources; - - public Decompiler(ImmutableList sources, Function1 destination) { - this.io = new DecompilerIo(destination); - this.fernflower = new Fernflower(io, io, OPTIONS, Slf4jFernflowerLogger.INSTANCE); - this.sources = sources; - } - - public void run() { - for (var source : sources) { - fernflower.addSource(source.toFile()); - } - - fernflower.decompileContext(); - } - - @Override - public void close() throws IOException { - io.close(); - } -} diff --git a/decompiler/src/main/java/dev/openrs2/decompiler/Decompiler.kt b/decompiler/src/main/java/dev/openrs2/decompiler/Decompiler.kt new file mode 100644 index 0000000000..ca38566f9d --- /dev/null +++ b/decompiler/src/main/java/dev/openrs2/decompiler/Decompiler.kt @@ -0,0 +1,61 @@ +package dev.openrs2.decompiler + +import org.jetbrains.java.decompiler.main.Fernflower +import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences +import java.io.Closeable +import java.nio.file.Path +import java.nio.file.Paths + +class Decompiler( + private val sources: List, + destination: (String) -> Path +) : Closeable { + private val io = DecompilerIo(destination) + private val fernflower = Fernflower(io, io, OPTIONS, Slf4jFernflowerLogger) + + fun run() { + for (source in sources) { + fernflower.addSource(source.toFile()) + } + fernflower.decompileContext() + } + + override fun close() { + io.close() + } + + companion object { + private val OPTIONS = mapOf( + IFernflowerPreferences.ASCII_STRING_CHARACTERS to "1", + IFernflowerPreferences.INDENT_STRING to "\t", + IFernflowerPreferences.SYNTHETIC_NOT_SET to "1" + ) + + private fun getDestination(archive: String): Path { + var dir = archive.replace(Regex("(?:_gl)?[.]jar$"), "") + when (dir) { + "runescape" -> dir = "client" + "jaggl" -> dir = "gl" + "jaggl_dri" -> dir = "gl-dri" + } + return Paths.get("nonfree").resolve(dir).resolve("src/main/java") + } + + @JvmStatic + fun main(args: Array) { + val deobOutput = Paths.get("nonfree/code/deob") + val sources = listOf( + deobOutput.resolve("runescape_gl.jar"), + deobOutput.resolve("jaggl.jar"), + deobOutput.resolve("jaggl_dri.jar"), + deobOutput.resolve("loader_gl.jar"), + deobOutput.resolve("signlink_gl.jar"), + deobOutput.resolve("unpack_gl.jar"), + deobOutput.resolve("unpacker_gl.jar") + ) + Decompiler(sources, this::getDestination).use { + it.run() + } + } + } +}