Decouple Library{Reader,Writer} from the Library class entirely

This will make naming libraries easier, as we won't have to thread
library names through every Library{Reader,Writer} implementation.

Signed-off-by: Graham <gpe@openrs2.dev>
Graham 5 years ago
parent 72a0642db3
commit 091c0ed29e
  1. 10
      asm/src/main/java/dev/openrs2/asm/classpath/Library.kt
  2. 6
      asm/src/main/java/dev/openrs2/asm/io/AbstractJarLibraryWriter.kt
  3. 9
      asm/src/main/java/dev/openrs2/asm/io/JarLibraryReader.kt
  4. 4
      asm/src/main/java/dev/openrs2/asm/io/LibraryReader.kt
  5. 4
      asm/src/main/java/dev/openrs2/asm/io/LibraryWriter.kt
  6. 4
      asm/src/main/java/dev/openrs2/asm/io/Pack200LibraryReader.kt
  7. 6
      asm/src/main/java/dev/openrs2/asm/io/Pack200LibraryWriter.kt
  8. 6
      asm/src/main/java/dev/openrs2/asm/io/PackClassLibraryReader.kt
  9. 4
      asm/src/main/java/dev/openrs2/asm/io/PackClassLibraryWriter.kt
  10. 6
      asm/src/main/java/dev/openrs2/asm/io/SignedJarLibraryWriter.kt

@ -48,7 +48,7 @@ class Library() : Iterable<ClassNode> {
logger.info { "Writing library $path" } logger.info { "Writing library $path" }
Files.newOutputStream(path).use { output -> Files.newOutputStream(path).use { output ->
writer.write(output, classPath, this) writer.write(output, classPath, classes.values)
} }
} }
@ -58,9 +58,15 @@ class Library() : Iterable<ClassNode> {
fun read(path: Path, reader: LibraryReader): Library { fun read(path: Path, reader: LibraryReader): Library {
logger.info { "Reading library $path" } logger.info { "Reading library $path" }
return Files.newInputStream(path).use { input -> val classes = Files.newInputStream(path).use { input ->
reader.read(input) reader.read(input)
} }
val library = Library()
for (clazz in classes) {
library.add(clazz)
}
return library
} }
} }
} }

@ -3,19 +3,19 @@ package dev.openrs2.asm.io
import dev.openrs2.asm.ClassVersionUtils import dev.openrs2.asm.ClassVersionUtils
import dev.openrs2.asm.NopClassVisitor import dev.openrs2.asm.NopClassVisitor
import dev.openrs2.asm.classpath.ClassPath import dev.openrs2.asm.classpath.ClassPath
import dev.openrs2.asm.classpath.Library
import dev.openrs2.asm.classpath.StackFrameClassWriter import dev.openrs2.asm.classpath.StackFrameClassWriter
import org.objectweb.asm.ClassWriter import org.objectweb.asm.ClassWriter
import org.objectweb.asm.Opcodes import org.objectweb.asm.Opcodes
import org.objectweb.asm.tree.ClassNode
import org.objectweb.asm.util.CheckClassAdapter import org.objectweb.asm.util.CheckClassAdapter
import java.io.OutputStream import java.io.OutputStream
import java.util.jar.JarEntry import java.util.jar.JarEntry
import java.util.jar.JarOutputStream import java.util.jar.JarOutputStream
abstract class AbstractJarLibraryWriter : LibraryWriter { abstract class AbstractJarLibraryWriter : LibraryWriter {
override fun write(output: OutputStream, classPath: ClassPath, library: Library) { override fun write(output: OutputStream, classPath: ClassPath, classes: Iterable<ClassNode>) {
createJarOutputStream(output).use { jar -> createJarOutputStream(output).use { jar ->
for (clazz in library) { for (clazz in classes) {
val writer = if (ClassVersionUtils.gte(clazz.version, Opcodes.V1_7)) { val writer = if (ClassVersionUtils.gte(clazz.version, Opcodes.V1_7)) {
StackFrameClassWriter(classPath) StackFrameClassWriter(classPath)
} else { } else {

@ -1,7 +1,6 @@
package dev.openrs2.asm.io package dev.openrs2.asm.io
import dev.openrs2.asm.classpath.JsrInliner import dev.openrs2.asm.classpath.JsrInliner
import dev.openrs2.asm.classpath.Library
import dev.openrs2.util.io.entries import dev.openrs2.util.io.entries
import org.objectweb.asm.ClassReader import org.objectweb.asm.ClassReader
import org.objectweb.asm.tree.ClassNode import org.objectweb.asm.tree.ClassNode
@ -11,8 +10,8 @@ import java.util.jar.JarInputStream
object JarLibraryReader : LibraryReader { object JarLibraryReader : LibraryReader {
private const val CLASS_SUFFIX = ".class" private const val CLASS_SUFFIX = ".class"
override fun read(input: InputStream): Library { override fun read(input: InputStream): Iterable<ClassNode> {
val library = Library() val classes = mutableListOf<ClassNode>()
JarInputStream(input).use { jar -> JarInputStream(input).use { jar ->
for (entry in jar.entries) { for (entry in jar.entries) {
@ -24,10 +23,10 @@ object JarLibraryReader : LibraryReader {
val reader = ClassReader(jar) val reader = ClassReader(jar)
reader.accept(JsrInliner(clazz), ClassReader.SKIP_DEBUG or ClassReader.SKIP_FRAMES) reader.accept(JsrInliner(clazz), ClassReader.SKIP_DEBUG or ClassReader.SKIP_FRAMES)
library.add(clazz) classes += clazz
} }
} }
return library return classes
} }
} }

@ -1,8 +1,8 @@
package dev.openrs2.asm.io package dev.openrs2.asm.io
import dev.openrs2.asm.classpath.Library import org.objectweb.asm.tree.ClassNode
import java.io.InputStream import java.io.InputStream
interface LibraryReader { interface LibraryReader {
fun read(input: InputStream): Library fun read(input: InputStream): Iterable<ClassNode>
} }

@ -1,9 +1,9 @@
package dev.openrs2.asm.io package dev.openrs2.asm.io
import dev.openrs2.asm.classpath.ClassPath import dev.openrs2.asm.classpath.ClassPath
import dev.openrs2.asm.classpath.Library import org.objectweb.asm.tree.ClassNode
import java.io.OutputStream import java.io.OutputStream
interface LibraryWriter { interface LibraryWriter {
fun write(output: OutputStream, classPath: ClassPath, library: Library) fun write(output: OutputStream, classPath: ClassPath, classes: Iterable<ClassNode>)
} }

@ -1,7 +1,7 @@
package dev.openrs2.asm.io package dev.openrs2.asm.io
import dev.openrs2.asm.classpath.Library
import dev.openrs2.compress.gzip.Gzip import dev.openrs2.compress.gzip.Gzip
import org.objectweb.asm.tree.ClassNode
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
import java.io.InputStream import java.io.InputStream
@ -9,7 +9,7 @@ import java.util.jar.JarOutputStream
import java.util.jar.Pack200 import java.util.jar.Pack200
object Pack200LibraryReader : LibraryReader { object Pack200LibraryReader : LibraryReader {
override fun read(input: InputStream): Library { override fun read(input: InputStream): Iterable<ClassNode> {
ByteArrayOutputStream().use { tempOutput -> ByteArrayOutputStream().use { tempOutput ->
Gzip.createHeaderlessInputStream(input).use { gzipInput -> Gzip.createHeaderlessInputStream(input).use { gzipInput ->
JarOutputStream(tempOutput).use { jarOutput -> JarOutputStream(tempOutput).use { jarOutput ->

@ -1,8 +1,8 @@
package dev.openrs2.asm.io package dev.openrs2.asm.io
import dev.openrs2.asm.classpath.ClassPath import dev.openrs2.asm.classpath.ClassPath
import dev.openrs2.asm.classpath.Library
import dev.openrs2.compress.gzip.Gzip import dev.openrs2.compress.gzip.Gzip
import org.objectweb.asm.tree.ClassNode
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
import java.io.OutputStream import java.io.OutputStream
@ -10,9 +10,9 @@ import java.util.jar.JarInputStream
import java.util.jar.Pack200 import java.util.jar.Pack200
object Pack200LibraryWriter : LibraryWriter { object Pack200LibraryWriter : LibraryWriter {
override fun write(output: OutputStream, classPath: ClassPath, library: Library) { override fun write(output: OutputStream, classPath: ClassPath, classes: Iterable<ClassNode>) {
ByteArrayOutputStream().use { tempOutput -> ByteArrayOutputStream().use { tempOutput ->
JarLibraryWriter.write(tempOutput, classPath, library) JarLibraryWriter.write(tempOutput, classPath, classes)
return JarInputStream(ByteArrayInputStream(tempOutput.toByteArray())).use { jarInput -> return JarInputStream(ByteArrayInputStream(tempOutput.toByteArray())).use { jarInput ->
Gzip.createHeaderlessOutputStream(output).use { gzipOutput -> Gzip.createHeaderlessOutputStream(output).use { gzipOutput ->

@ -1,11 +1,11 @@
package dev.openrs2.asm.io package dev.openrs2.asm.io
import dev.openrs2.asm.classpath.Library import org.objectweb.asm.tree.ClassNode
import java.io.InputStream import java.io.InputStream
object PackClassLibraryReader : LibraryReader { object PackClassLibraryReader : LibraryReader {
override fun read(input: InputStream): Library { override fun read(input: InputStream): Iterable<ClassNode> {
// TODO(gpe): implement // TODO(gpe): implement
return Library() return emptyList()
} }
} }

@ -1,11 +1,11 @@
package dev.openrs2.asm.io package dev.openrs2.asm.io
import dev.openrs2.asm.classpath.ClassPath import dev.openrs2.asm.classpath.ClassPath
import dev.openrs2.asm.classpath.Library import org.objectweb.asm.tree.ClassNode
import java.io.OutputStream import java.io.OutputStream
object PackClassLibraryWriter : LibraryWriter { object PackClassLibraryWriter : LibraryWriter {
override fun write(output: OutputStream, classPath: ClassPath, library: Library) { override fun write(output: OutputStream, classPath: ClassPath, classes: Iterable<ClassNode>) {
// TODO(gpe): implement // TODO(gpe): implement
} }
} }

@ -1,10 +1,10 @@
package dev.openrs2.asm.io package dev.openrs2.asm.io
import dev.openrs2.asm.classpath.ClassPath import dev.openrs2.asm.classpath.ClassPath
import dev.openrs2.asm.classpath.Library
import dev.openrs2.crypto.Pkcs12KeyStore import dev.openrs2.crypto.Pkcs12KeyStore
import dev.openrs2.util.io.DeterministicJarOutputStream import dev.openrs2.util.io.DeterministicJarOutputStream
import dev.openrs2.util.io.entries import dev.openrs2.util.io.entries
import org.objectweb.asm.tree.ClassNode
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
import java.io.InputStream import java.io.InputStream
@ -17,11 +17,11 @@ class SignedJarLibraryWriter(
private val manifest: Manifest, private val manifest: Manifest,
private val keyStore: Pkcs12KeyStore private val keyStore: Pkcs12KeyStore
) : LibraryWriter { ) : LibraryWriter {
override fun write(output: OutputStream, classPath: ClassPath, library: Library) { override fun write(output: OutputStream, classPath: ClassPath, classes: Iterable<ClassNode>) {
val unsignedJar = Files.createTempFile(TEMP_PREFIX, JAR_SUFFIX) val unsignedJar = Files.createTempFile(TEMP_PREFIX, JAR_SUFFIX)
try { try {
Files.newOutputStream(unsignedJar).use { unsignedOutput -> Files.newOutputStream(unsignedJar).use { unsignedOutput ->
ManifestJarLibraryWriter(manifest).write(unsignedOutput, classPath, library) ManifestJarLibraryWriter(manifest).write(unsignedOutput, classPath, classes)
} }
ByteArrayOutputStream().use { signedOutput -> ByteArrayOutputStream().use { signedOutput ->

Loading…
Cancel
Save