From 6e877b52cec07a9ed5e692995644d5dc8efcfbad Mon Sep 17 00:00:00 2001 From: Scu11 Date: Tue, 14 Apr 2020 15:27:21 +0100 Subject: [PATCH] Move Library#read methods to dedicated classes Signed-off-by: Scu11 --- .../java/dev/openrs2/asm/classpath/Library.kt | 55 ------------------- .../dev/openrs2/asm/io/JarLibraryReader.kt | 32 +++++++++++ .../java/dev/openrs2/asm/io/LibraryReader.kt | 7 +++ .../openrs2/asm/io/Pack200LibraryReader.kt | 33 +++++++++++ .../main/java/dev/openrs2/bundler/Bundler.kt | 31 +++++++++-- .../java/dev/openrs2/deob/Deobfuscator.kt | 31 +++++++++-- 6 files changed, 122 insertions(+), 67 deletions(-) create mode 100644 asm/src/main/java/dev/openrs2/asm/io/JarLibraryReader.kt create mode 100644 asm/src/main/java/dev/openrs2/asm/io/LibraryReader.kt create mode 100644 asm/src/main/java/dev/openrs2/asm/io/Pack200LibraryReader.kt diff --git a/asm/src/main/java/dev/openrs2/asm/classpath/Library.kt b/asm/src/main/java/dev/openrs2/asm/classpath/Library.kt index 03a9bc64..2fb5d281 100644 --- a/asm/src/main/java/dev/openrs2/asm/classpath/Library.kt +++ b/asm/src/main/java/dev/openrs2/asm/classpath/Library.kt @@ -1,17 +1,9 @@ package dev.openrs2.asm.classpath -import com.github.michaelbull.logging.InlineLogger import dev.openrs2.asm.remap -import dev.openrs2.compress.gzip.Gzip -import org.objectweb.asm.ClassReader import org.objectweb.asm.commons.Remapper import org.objectweb.asm.tree.ClassNode -import java.nio.file.Files -import java.nio.file.Path import java.util.TreeMap -import java.util.jar.JarInputStream -import java.util.jar.JarOutputStream -import java.util.jar.Pack200 class Library() : Iterable { private var classes = TreeMap() @@ -51,51 +43,4 @@ class Library() : Iterable { classes = classes.mapKeysTo(TreeMap()) { (_, clazz) -> clazz.name } } - - companion object { - private val logger = InlineLogger() - private const val CLASS_SUFFIX = ".class" - private const val TEMP_PREFIX = "tmp" - private const val JAR_SUFFIX = ".jar" - - fun readJar(path: Path): Library { - logger.info { "Reading jar $path" } - - val library = Library() - - JarInputStream(Files.newInputStream(path)).use { `in` -> - while (true) { - val entry = `in`.nextJarEntry ?: break - if (!entry.name.endsWith(CLASS_SUFFIX)) { - continue - } - - val clazz = ClassNode() - val reader = ClassReader(`in`) - reader.accept(JsrInliner(clazz), ClassReader.SKIP_DEBUG or ClassReader.SKIP_FRAMES) - - library.add(clazz) - } - } - - return library - } - - fun readPack(path: Path): Library { - logger.info { "Reading pack $path" } - - val temp = Files.createTempFile(TEMP_PREFIX, JAR_SUFFIX) - try { - Gzip.createHeaderlessInputStream(Files.newInputStream(path)).use { `in` -> - JarOutputStream(Files.newOutputStream(temp)).use { out -> - Pack200.newUnpacker().unpack(`in`, out) - } - } - - return readJar(temp) - } finally { - Files.deleteIfExists(temp) - } - } - } } diff --git a/asm/src/main/java/dev/openrs2/asm/io/JarLibraryReader.kt b/asm/src/main/java/dev/openrs2/asm/io/JarLibraryReader.kt new file mode 100644 index 00000000..e9924f2d --- /dev/null +++ b/asm/src/main/java/dev/openrs2/asm/io/JarLibraryReader.kt @@ -0,0 +1,32 @@ +package dev.openrs2.asm.io + +import dev.openrs2.asm.classpath.JsrInliner +import dev.openrs2.asm.classpath.Library +import org.objectweb.asm.ClassReader +import org.objectweb.asm.tree.ClassNode +import java.util.jar.JarInputStream + +class JarLibraryReader(private val input: JarInputStream) : LibraryReader { + override fun read(): Library { + val library = Library() + + while (true) { + val entry = input.nextJarEntry ?: break + if (!entry.name.endsWith(CLASS_SUFFIX)) { + continue + } + + val clazz = ClassNode() + val reader = ClassReader(input) + reader.accept(JsrInliner(clazz), ClassReader.SKIP_DEBUG or ClassReader.SKIP_FRAMES) + + library.add(clazz) + } + + return library + } + + private companion object { + private const val CLASS_SUFFIX = ".class" + } +} diff --git a/asm/src/main/java/dev/openrs2/asm/io/LibraryReader.kt b/asm/src/main/java/dev/openrs2/asm/io/LibraryReader.kt new file mode 100644 index 00000000..05b9638b --- /dev/null +++ b/asm/src/main/java/dev/openrs2/asm/io/LibraryReader.kt @@ -0,0 +1,7 @@ +package dev.openrs2.asm.io + +import dev.openrs2.asm.classpath.Library + +interface LibraryReader { + fun read(): Library +} diff --git a/asm/src/main/java/dev/openrs2/asm/io/Pack200LibraryReader.kt b/asm/src/main/java/dev/openrs2/asm/io/Pack200LibraryReader.kt new file mode 100644 index 00000000..477c2e28 --- /dev/null +++ b/asm/src/main/java/dev/openrs2/asm/io/Pack200LibraryReader.kt @@ -0,0 +1,33 @@ +package dev.openrs2.asm.io + +import dev.openrs2.asm.classpath.Library +import dev.openrs2.compress.gzip.Gzip +import java.io.InputStream +import java.nio.file.Files +import java.util.jar.JarInputStream +import java.util.jar.JarOutputStream +import java.util.jar.Pack200 + +class Pack200LibraryReader(private val input: InputStream) : LibraryReader { + override fun read(): Library { + val temp = Files.createTempFile(TEMP_PREFIX, JAR_SUFFIX) + try { + Gzip.createHeaderlessInputStream(input).use { gzipInput -> + JarOutputStream(Files.newOutputStream(temp)).use { output -> + Pack200.newUnpacker().unpack(gzipInput, output) + } + } + + return JarInputStream(Files.newInputStream(temp)).use { tempInput -> + JarLibraryReader(tempInput).read() + } + } finally { + Files.deleteIfExists(temp) + } + } + + private companion object { + private const val TEMP_PREFIX = "tmp" + private const val JAR_SUFFIX = ".jar" + } +} diff --git a/bundler/src/main/java/dev/openrs2/bundler/Bundler.kt b/bundler/src/main/java/dev/openrs2/bundler/Bundler.kt index 8f568761..9ae5cc48 100644 --- a/bundler/src/main/java/dev/openrs2/bundler/Bundler.kt +++ b/bundler/src/main/java/dev/openrs2/bundler/Bundler.kt @@ -4,7 +4,9 @@ import com.github.michaelbull.logging.InlineLogger import dev.openrs2.asm.classpath.ClassPath import dev.openrs2.asm.classpath.Library import dev.openrs2.asm.transform.Transformer +import dev.openrs2.asm.io.JarLibraryReader import dev.openrs2.asm.io.JarLibraryWriter +import dev.openrs2.asm.io.Pack200LibraryReader import dev.openrs2.asm.io.SignedJarLibraryWriter import dev.openrs2.bundler.transform.ResourceTransformer import dev.openrs2.conf.Config @@ -14,6 +16,7 @@ import java.nio.file.Files import java.nio.file.Path import java.util.jar.Attributes import java.util.jar.Attributes.Name.MANIFEST_VERSION +import java.util.jar.JarInputStream import java.util.jar.Manifest import javax.inject.Inject import javax.inject.Singleton @@ -38,12 +41,12 @@ class Bundler @Inject constructor( fun run(input: Path, output: Path, keyStorePath: Path) { // read input jars/packs logger.info { "Reading input jars" } - val unpacker = Library.readJar(input.resolve("unpackclass.pack")) - val loader = Library.readJar(input.resolve("loader.jar")) - val glLoader = Library.readJar(input.resolve("loader_gl.jar")) - val gl = Library.readPack(input.resolve("jaggl.pack200")) - val client = Library.readJar(input.resolve("runescape.jar")) - val glClient = Library.readPack(input.resolve("runescape_gl.pack200")) + val unpacker = readJar(input.resolve("unpackclass.pack")) + val loader = readJar(input.resolve("loader.jar")) + val glLoader = readJar(input.resolve("loader_gl.jar")) + val gl = readPack(input.resolve("jaggl.pack200")) + val client = readJar(input.resolve("runescape.jar")) + val glClient = readPack(input.resolve("runescape_gl.pack200")) // bundle libraries together into a common classpath val runtime = ClassLoader.getPlatformClassLoader() @@ -126,6 +129,22 @@ class Bundler @Inject constructor( writeSignedJar(glClassPath, glLoader, output.resolve("loader_gl.jar"), keyStore) } + private fun readJar(path: Path): Library { + logger.info { "Reading jar $path" } + + return JarInputStream(Files.newInputStream(path)).use { input -> + JarLibraryReader(input).read() + } + } + + private fun readPack(path: Path): Library { + logger.info { "Reading pack $path" } + + return Files.newInputStream(path).use { input -> + Pack200LibraryReader(input).read() + } + } + private fun writeJar(classPath: ClassPath, library: Library, path: Path) { logger.info { "Writing jar $path" } diff --git a/deob/src/main/java/dev/openrs2/deob/Deobfuscator.kt b/deob/src/main/java/dev/openrs2/deob/Deobfuscator.kt index aebb5dce..4af984b3 100644 --- a/deob/src/main/java/dev/openrs2/deob/Deobfuscator.kt +++ b/deob/src/main/java/dev/openrs2/deob/Deobfuscator.kt @@ -3,12 +3,15 @@ package dev.openrs2.deob import com.github.michaelbull.logging.InlineLogger import dev.openrs2.asm.classpath.ClassPath import dev.openrs2.asm.classpath.Library +import dev.openrs2.asm.io.JarLibraryReader import dev.openrs2.asm.io.JarLibraryWriter +import dev.openrs2.asm.io.Pack200LibraryReader import dev.openrs2.asm.transform.Transformer import dev.openrs2.deob.remap.PrefixRemapper import dev.openrs2.util.io.DeterministicJarOutputStream import java.nio.file.Files import java.nio.file.Path +import java.util.jar.JarInputStream import javax.inject.Inject import javax.inject.Singleton @@ -19,13 +22,13 @@ class Deobfuscator @Inject constructor( fun run(input: Path, output: Path) { // read input jars/packs logger.info { "Reading input jars" } - val unpackClass = Library.readJar(input.resolve("unpackclass.pack")) + val unpackClass = readJar(input.resolve("unpackclass.pack")) val glUnpackClass = Library(unpackClass) - val loader = Library.readJar(input.resolve("loader.jar")) - val glLoader = Library.readJar(input.resolve("loader_gl.jar")) - val gl = Library.readPack(input.resolve("jaggl.pack200")) - val client = Library.readJar(input.resolve("runescape.jar")) - val glClient = Library.readPack(input.resolve("runescape_gl.pack200")) + val loader = readJar(input.resolve("loader.jar")) + val glLoader = readJar(input.resolve("loader_gl.jar")) + val gl = readPack(input.resolve("jaggl.pack200")) + val client = readJar(input.resolve("runescape.jar")) + val glClient = readPack(input.resolve("runescape_gl.pack200")) /* * TODO(gpe): it'd be nice to have separate signlink.jar and @@ -118,6 +121,22 @@ class Deobfuscator @Inject constructor( writeJar(unsignedClassPath, unsignedClient, output.resolve("runescape_unsigned.jar")) } + private fun readJar(path: Path): Library { + logger.info { "Reading jar $path" } + + return JarInputStream(Files.newInputStream(path)).use { input -> + JarLibraryReader(input).read() + } + } + + private fun readPack(path: Path): Library { + logger.info { "Reading pack $path" } + + return Files.newInputStream(path).use { input -> + Pack200LibraryReader(input).read() + } + } + private fun writeJar(classPath: ClassPath, library: Library, path: Path) { logger.info { "Writing jar $path" }