Move Library#read methods to dedicated classes

Signed-off-by: Scu11 <scu11@openrs2.dev>
Scu11 5 years ago committed by Graham
parent 26348b8a2e
commit 6e877b52ce
  1. 55
      asm/src/main/java/dev/openrs2/asm/classpath/Library.kt
  2. 32
      asm/src/main/java/dev/openrs2/asm/io/JarLibraryReader.kt
  3. 7
      asm/src/main/java/dev/openrs2/asm/io/LibraryReader.kt
  4. 33
      asm/src/main/java/dev/openrs2/asm/io/Pack200LibraryReader.kt
  5. 31
      bundler/src/main/java/dev/openrs2/bundler/Bundler.kt
  6. 31
      deob/src/main/java/dev/openrs2/deob/Deobfuscator.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<ClassNode> {
private var classes = TreeMap<String, ClassNode>()
@ -51,51 +43,4 @@ class Library() : Iterable<ClassNode> {
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)
}
}
}
}

@ -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"
}
}

@ -0,0 +1,7 @@
package dev.openrs2.asm.io
import dev.openrs2.asm.classpath.Library
interface LibraryReader {
fun read(): Library
}

@ -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"
}
}

@ -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" }

@ -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" }

Loading…
Cancel
Save