From 06212b851165a6bc903325cc269baf1f3fc5e75f Mon Sep 17 00:00:00 2001 From: Graham Date: Sat, 2 May 2020 23:30:19 +0100 Subject: [PATCH] Add shorthand Library read/write methods Signed-off-by: Graham --- .../java/dev/openrs2/asm/classpath/Library.kt | 25 ++++++++ .../main/java/dev/openrs2/bundler/Bundler.kt | 50 +++------------- .../java/dev/openrs2/deob/Deobfuscator.kt | 60 ++++++------------- 3 files changed, 52 insertions(+), 83 deletions(-) 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 2fb5d281e7..5d89544d7f 100644 --- a/asm/src/main/java/dev/openrs2/asm/classpath/Library.kt +++ b/asm/src/main/java/dev/openrs2/asm/classpath/Library.kt @@ -1,8 +1,13 @@ package dev.openrs2.asm.classpath +import com.github.michaelbull.logging.InlineLogger +import dev.openrs2.asm.io.LibraryReader +import dev.openrs2.asm.io.LibraryWriter import dev.openrs2.asm.remap 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 class Library() : Iterable { @@ -43,4 +48,24 @@ class Library() : Iterable { classes = classes.mapKeysTo(TreeMap()) { (_, clazz) -> clazz.name } } + + fun write(path: Path, writer: LibraryWriter, classPath: ClassPath) { + logger.info { "Writing library $path" } + + Files.newOutputStream(path).use { output -> + writer.write(output, classPath, this) + } + } + + companion object { + private val logger = InlineLogger() + + fun read(path: Path, reader: LibraryReader): Library { + logger.info { "Reading library $path" } + + return Files.newInputStream(path).use { input -> + reader.read(input) + } + } + } } diff --git a/bundler/src/main/java/dev/openrs2/bundler/Bundler.kt b/bundler/src/main/java/dev/openrs2/bundler/Bundler.kt index 3f7d529a4c..aafdc46de7 100644 --- a/bundler/src/main/java/dev/openrs2/bundler/Bundler.kt +++ b/bundler/src/main/java/dev/openrs2/bundler/Bundler.kt @@ -42,12 +42,12 @@ class Bundler @Inject constructor( fun run(input: Path, output: Path, keyStorePath: Path) { // read input jars/packs logger.info { "Reading input jars" } - 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")) + val unpacker = Library.read(input.resolve("unpackclass.pack"), JarLibraryReader) + val loader = Library.read(input.resolve("loader.jar"), JarLibraryReader) + val glLoader = Library.read(input.resolve("loader_gl.jar"), JarLibraryReader) + val gl = Library.read(input.resolve("jaggl.pack200"), Pack200LibraryReader) + val client = Library.read(input.resolve("runescape.jar"), JarLibraryReader) + val glClient = Library.read(input.resolve("runescape_gl.pack200"), Pack200LibraryReader) // bundle libraries together into a common classpath val runtime = ClassLoader.getPlatformClassLoader() @@ -139,43 +139,11 @@ class Bundler @Inject constructor( } // write unsigned client and loaders - writeJar(classPath, client, output.resolve("runescape.jar")) + client.write(output.resolve("runescape.jar"), ManifestJarLibraryWriter(unsignedManifest), classPath) val keyStore = Pkcs12KeyStore.open(keyStorePath, config.game) - writeSignedJar(classPath, loader, output.resolve("loader.jar"), keyStore) - writeSignedJar(glClassPath, glLoader, output.resolve("loader_gl.jar"), keyStore) - } - - private fun readJar(path: Path): Library { - logger.info { "Reading jar $path" } - - return Files.newInputStream(path).use { input -> - JarLibraryReader.read(input) - } - } - - private fun readPack(path: Path): Library { - logger.info { "Reading pack $path" } - - return Files.newInputStream(path).use { input -> - Pack200LibraryReader.read(input) - } - } - - private fun writeJar(classPath: ClassPath, library: Library, path: Path) { - logger.info { "Writing jar $path" } - - Files.newOutputStream(path).use { output -> - ManifestJarLibraryWriter(unsignedManifest).write(output, classPath, library) - } - } - - private fun writeSignedJar(classPath: ClassPath, library: Library, path: Path, keyStore: Pkcs12KeyStore) { - logger.info { "Writing signed jar $path" } - - Files.newOutputStream(path).use { output -> - SignedJarLibraryWriter(signedManifest, keyStore).write(output, classPath, library) - } + loader.write(output.resolve("loader.jar"), SignedJarLibraryWriter(signedManifest, keyStore), classPath) + glLoader.write(output.resolve("loader_gl.jar"), SignedJarLibraryWriter(signedManifest, keyStore), glClassPath) } companion object { diff --git a/deob/src/main/java/dev/openrs2/deob/Deobfuscator.kt b/deob/src/main/java/dev/openrs2/deob/Deobfuscator.kt index a7607db57b..32a7554d45 100644 --- a/deob/src/main/java/dev/openrs2/deob/Deobfuscator.kt +++ b/deob/src/main/java/dev/openrs2/deob/Deobfuscator.kt @@ -20,13 +20,13 @@ class Deobfuscator @Inject constructor( fun run(input: Path, output: Path) { // read input jars/packs logger.info { "Reading input jars" } - val unpackClass = readJar(input.resolve("unpackclass.pack")) + val unpackClass = Library.read(input.resolve("unpackclass.pack"), JarLibraryReader) val glUnpackClass = Library(unpackClass) - 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")) + val loader = Library.read(input.resolve("loader.jar"), JarLibraryReader) + val glLoader = Library.read(input.resolve("loader_gl.jar"), JarLibraryReader) + val gl = Library.read(input.resolve("jaggl.pack200"), Pack200LibraryReader) + val client = Library.read(input.resolve("runescape.jar"), JarLibraryReader) + val glClient = Library.read(input.resolve("runescape_gl.pack200"), Pack200LibraryReader) /* * TODO(gpe): it'd be nice to have separate signlink.jar and @@ -103,44 +103,20 @@ class Deobfuscator @Inject constructor( Files.createDirectories(output) - writeJar(classPath, client, output.resolve("runescape.jar")) - writeJar(classPath, loader, output.resolve("loader.jar")) - writeJar(classPath, signLink, output.resolve("signlink.jar")) - writeJar(classPath, unpack, output.resolve("unpack.jar")) - writeJar(classPath, unpackClass, output.resolve("unpackclass.jar")) + client.write(output.resolve("runescape.jar"), JarLibraryWriter, classPath) + loader.write(output.resolve("loader.jar"), JarLibraryWriter, classPath) + signLink.write(output.resolve("signlink.jar"), JarLibraryWriter, classPath) + unpack.write(output.resolve("unpack.jar"), JarLibraryWriter, classPath) + unpackClass.write(output.resolve("unpackclass.jar"), JarLibraryWriter, classPath) - writeJar(glClassPath, gl, output.resolve("jaggl.jar")) - writeJar(glClassPath, glClient, output.resolve("runescape_gl.jar")) - writeJar(glClassPath, glLoader, output.resolve("loader_gl.jar")) - writeJar(glClassPath, glSignLink, output.resolve("signlink_gl.jar")) - writeJar(glClassPath, glUnpack, output.resolve("unpack_gl.jar")) - writeJar(glClassPath, glUnpackClass, output.resolve("unpackclass_gl.jar")) + gl.write(output.resolve("jaggl.jar"), JarLibraryWriter, glClassPath) + glClient.write(output.resolve("runescape_gl.jar"), JarLibraryWriter, glClassPath) + glLoader.write(output.resolve("loader_gl.jar"), JarLibraryWriter, glClassPath) + glSignLink.write(output.resolve("signlink_gl.jar"), JarLibraryWriter, glClassPath) + glUnpack.write(output.resolve("unpack_gl.jar"), JarLibraryWriter, glClassPath) + glUnpackClass.write(output.resolve("unpackclass_gl.jar"), JarLibraryWriter, glClassPath) - writeJar(unsignedClassPath, unsignedClient, output.resolve("runescape_unsigned.jar")) - } - - private fun readJar(path: Path): Library { - logger.info { "Reading jar $path" } - - return Files.newInputStream(path).use { input -> - JarLibraryReader.read(input) - } - } - - private fun readPack(path: Path): Library { - logger.info { "Reading pack $path" } - - return Files.newInputStream(path).use { input -> - Pack200LibraryReader.read(input) - } - } - - private fun writeJar(classPath: ClassPath, library: Library, path: Path) { - logger.info { "Writing jar $path" } - - Files.newOutputStream(path).use { output -> - JarLibraryWriter.write(output, classPath, library) - } + unsignedClient.write(output.resolve("runescape_unsigned.jar"), JarLibraryWriter, unsignedClassPath) } companion object {