From 091c0ed29ecff57d00f0ddca158486aa77fa8986 Mon Sep 17 00:00:00 2001 From: Graham Date: Sun, 7 Jun 2020 15:16:45 +0100 Subject: [PATCH] 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 --- asm/src/main/java/dev/openrs2/asm/classpath/Library.kt | 10 ++++++++-- .../dev/openrs2/asm/io/AbstractJarLibraryWriter.kt | 6 +++--- .../main/java/dev/openrs2/asm/io/JarLibraryReader.kt | 9 ++++----- asm/src/main/java/dev/openrs2/asm/io/LibraryReader.kt | 4 ++-- asm/src/main/java/dev/openrs2/asm/io/LibraryWriter.kt | 4 ++-- .../java/dev/openrs2/asm/io/Pack200LibraryReader.kt | 4 ++-- .../java/dev/openrs2/asm/io/Pack200LibraryWriter.kt | 6 +++--- .../java/dev/openrs2/asm/io/PackClassLibraryReader.kt | 6 +++--- .../java/dev/openrs2/asm/io/PackClassLibraryWriter.kt | 4 ++-- .../java/dev/openrs2/asm/io/SignedJarLibraryWriter.kt | 6 +++--- 10 files changed, 32 insertions(+), 27 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 4dd3b4e5..a0a93772 100644 --- a/asm/src/main/java/dev/openrs2/asm/classpath/Library.kt +++ b/asm/src/main/java/dev/openrs2/asm/classpath/Library.kt @@ -48,7 +48,7 @@ class Library() : Iterable { logger.info { "Writing library $path" } Files.newOutputStream(path).use { output -> - writer.write(output, classPath, this) + writer.write(output, classPath, classes.values) } } @@ -58,9 +58,15 @@ class Library() : Iterable { fun read(path: Path, reader: LibraryReader): Library { logger.info { "Reading library $path" } - return Files.newInputStream(path).use { input -> + val classes = Files.newInputStream(path).use { input -> reader.read(input) } + + val library = Library() + for (clazz in classes) { + library.add(clazz) + } + return library } } } diff --git a/asm/src/main/java/dev/openrs2/asm/io/AbstractJarLibraryWriter.kt b/asm/src/main/java/dev/openrs2/asm/io/AbstractJarLibraryWriter.kt index a0eefc6e..b07053dc 100644 --- a/asm/src/main/java/dev/openrs2/asm/io/AbstractJarLibraryWriter.kt +++ b/asm/src/main/java/dev/openrs2/asm/io/AbstractJarLibraryWriter.kt @@ -3,19 +3,19 @@ package dev.openrs2.asm.io import dev.openrs2.asm.ClassVersionUtils import dev.openrs2.asm.NopClassVisitor import dev.openrs2.asm.classpath.ClassPath -import dev.openrs2.asm.classpath.Library import dev.openrs2.asm.classpath.StackFrameClassWriter import org.objectweb.asm.ClassWriter import org.objectweb.asm.Opcodes +import org.objectweb.asm.tree.ClassNode import org.objectweb.asm.util.CheckClassAdapter import java.io.OutputStream import java.util.jar.JarEntry import java.util.jar.JarOutputStream abstract class AbstractJarLibraryWriter : LibraryWriter { - override fun write(output: OutputStream, classPath: ClassPath, library: Library) { + override fun write(output: OutputStream, classPath: ClassPath, classes: Iterable) { createJarOutputStream(output).use { jar -> - for (clazz in library) { + for (clazz in classes) { val writer = if (ClassVersionUtils.gte(clazz.version, Opcodes.V1_7)) { StackFrameClassWriter(classPath) } else { diff --git a/asm/src/main/java/dev/openrs2/asm/io/JarLibraryReader.kt b/asm/src/main/java/dev/openrs2/asm/io/JarLibraryReader.kt index 645f761b..e19c58da 100644 --- a/asm/src/main/java/dev/openrs2/asm/io/JarLibraryReader.kt +++ b/asm/src/main/java/dev/openrs2/asm/io/JarLibraryReader.kt @@ -1,7 +1,6 @@ package dev.openrs2.asm.io import dev.openrs2.asm.classpath.JsrInliner -import dev.openrs2.asm.classpath.Library import dev.openrs2.util.io.entries import org.objectweb.asm.ClassReader import org.objectweb.asm.tree.ClassNode @@ -11,8 +10,8 @@ import java.util.jar.JarInputStream object JarLibraryReader : LibraryReader { private const val CLASS_SUFFIX = ".class" - override fun read(input: InputStream): Library { - val library = Library() + override fun read(input: InputStream): Iterable { + val classes = mutableListOf() JarInputStream(input).use { jar -> for (entry in jar.entries) { @@ -24,10 +23,10 @@ object JarLibraryReader : LibraryReader { val reader = ClassReader(jar) reader.accept(JsrInliner(clazz), ClassReader.SKIP_DEBUG or ClassReader.SKIP_FRAMES) - library.add(clazz) + classes += clazz } } - return library + return classes } } diff --git a/asm/src/main/java/dev/openrs2/asm/io/LibraryReader.kt b/asm/src/main/java/dev/openrs2/asm/io/LibraryReader.kt index 444c4624..37802ea3 100644 --- a/asm/src/main/java/dev/openrs2/asm/io/LibraryReader.kt +++ b/asm/src/main/java/dev/openrs2/asm/io/LibraryReader.kt @@ -1,8 +1,8 @@ package dev.openrs2.asm.io -import dev.openrs2.asm.classpath.Library +import org.objectweb.asm.tree.ClassNode import java.io.InputStream interface LibraryReader { - fun read(input: InputStream): Library + fun read(input: InputStream): Iterable } diff --git a/asm/src/main/java/dev/openrs2/asm/io/LibraryWriter.kt b/asm/src/main/java/dev/openrs2/asm/io/LibraryWriter.kt index 0b150950..89dbd1ea 100644 --- a/asm/src/main/java/dev/openrs2/asm/io/LibraryWriter.kt +++ b/asm/src/main/java/dev/openrs2/asm/io/LibraryWriter.kt @@ -1,9 +1,9 @@ package dev.openrs2.asm.io import dev.openrs2.asm.classpath.ClassPath -import dev.openrs2.asm.classpath.Library +import org.objectweb.asm.tree.ClassNode import java.io.OutputStream interface LibraryWriter { - fun write(output: OutputStream, classPath: ClassPath, library: Library) + fun write(output: OutputStream, classPath: ClassPath, classes: Iterable) } diff --git a/asm/src/main/java/dev/openrs2/asm/io/Pack200LibraryReader.kt b/asm/src/main/java/dev/openrs2/asm/io/Pack200LibraryReader.kt index 9d7d5131..7ccb12fd 100644 --- a/asm/src/main/java/dev/openrs2/asm/io/Pack200LibraryReader.kt +++ b/asm/src/main/java/dev/openrs2/asm/io/Pack200LibraryReader.kt @@ -1,7 +1,7 @@ package dev.openrs2.asm.io -import dev.openrs2.asm.classpath.Library import dev.openrs2.compress.gzip.Gzip +import org.objectweb.asm.tree.ClassNode import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream import java.io.InputStream @@ -9,7 +9,7 @@ import java.util.jar.JarOutputStream import java.util.jar.Pack200 object Pack200LibraryReader : LibraryReader { - override fun read(input: InputStream): Library { + override fun read(input: InputStream): Iterable { ByteArrayOutputStream().use { tempOutput -> Gzip.createHeaderlessInputStream(input).use { gzipInput -> JarOutputStream(tempOutput).use { jarOutput -> diff --git a/asm/src/main/java/dev/openrs2/asm/io/Pack200LibraryWriter.kt b/asm/src/main/java/dev/openrs2/asm/io/Pack200LibraryWriter.kt index 7fd40c3a..deb4da9c 100644 --- a/asm/src/main/java/dev/openrs2/asm/io/Pack200LibraryWriter.kt +++ b/asm/src/main/java/dev/openrs2/asm/io/Pack200LibraryWriter.kt @@ -1,8 +1,8 @@ package dev.openrs2.asm.io import dev.openrs2.asm.classpath.ClassPath -import dev.openrs2.asm.classpath.Library import dev.openrs2.compress.gzip.Gzip +import org.objectweb.asm.tree.ClassNode import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream import java.io.OutputStream @@ -10,9 +10,9 @@ import java.util.jar.JarInputStream import java.util.jar.Pack200 object Pack200LibraryWriter : LibraryWriter { - override fun write(output: OutputStream, classPath: ClassPath, library: Library) { + override fun write(output: OutputStream, classPath: ClassPath, classes: Iterable) { ByteArrayOutputStream().use { tempOutput -> - JarLibraryWriter.write(tempOutput, classPath, library) + JarLibraryWriter.write(tempOutput, classPath, classes) return JarInputStream(ByteArrayInputStream(tempOutput.toByteArray())).use { jarInput -> Gzip.createHeaderlessOutputStream(output).use { gzipOutput -> diff --git a/asm/src/main/java/dev/openrs2/asm/io/PackClassLibraryReader.kt b/asm/src/main/java/dev/openrs2/asm/io/PackClassLibraryReader.kt index 94d8bb7d..0e8b70b6 100644 --- a/asm/src/main/java/dev/openrs2/asm/io/PackClassLibraryReader.kt +++ b/asm/src/main/java/dev/openrs2/asm/io/PackClassLibraryReader.kt @@ -1,11 +1,11 @@ package dev.openrs2.asm.io -import dev.openrs2.asm.classpath.Library +import org.objectweb.asm.tree.ClassNode import java.io.InputStream object PackClassLibraryReader : LibraryReader { - override fun read(input: InputStream): Library { + override fun read(input: InputStream): Iterable { // TODO(gpe): implement - return Library() + return emptyList() } } diff --git a/asm/src/main/java/dev/openrs2/asm/io/PackClassLibraryWriter.kt b/asm/src/main/java/dev/openrs2/asm/io/PackClassLibraryWriter.kt index 0879e8d9..625c0cc0 100644 --- a/asm/src/main/java/dev/openrs2/asm/io/PackClassLibraryWriter.kt +++ b/asm/src/main/java/dev/openrs2/asm/io/PackClassLibraryWriter.kt @@ -1,11 +1,11 @@ package dev.openrs2.asm.io import dev.openrs2.asm.classpath.ClassPath -import dev.openrs2.asm.classpath.Library +import org.objectweb.asm.tree.ClassNode import java.io.OutputStream object PackClassLibraryWriter : LibraryWriter { - override fun write(output: OutputStream, classPath: ClassPath, library: Library) { + override fun write(output: OutputStream, classPath: ClassPath, classes: Iterable) { // TODO(gpe): implement } } diff --git a/asm/src/main/java/dev/openrs2/asm/io/SignedJarLibraryWriter.kt b/asm/src/main/java/dev/openrs2/asm/io/SignedJarLibraryWriter.kt index 525bf4f6..b188f5cb 100644 --- a/asm/src/main/java/dev/openrs2/asm/io/SignedJarLibraryWriter.kt +++ b/asm/src/main/java/dev/openrs2/asm/io/SignedJarLibraryWriter.kt @@ -1,10 +1,10 @@ package dev.openrs2.asm.io import dev.openrs2.asm.classpath.ClassPath -import dev.openrs2.asm.classpath.Library import dev.openrs2.crypto.Pkcs12KeyStore import dev.openrs2.util.io.DeterministicJarOutputStream import dev.openrs2.util.io.entries +import org.objectweb.asm.tree.ClassNode import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream import java.io.InputStream @@ -17,11 +17,11 @@ class SignedJarLibraryWriter( private val manifest: Manifest, private val keyStore: Pkcs12KeyStore ) : LibraryWriter { - override fun write(output: OutputStream, classPath: ClassPath, library: Library) { + override fun write(output: OutputStream, classPath: ClassPath, classes: Iterable) { val unsignedJar = Files.createTempFile(TEMP_PREFIX, JAR_SUFFIX) try { Files.newOutputStream(unsignedJar).use { unsignedOutput -> - ManifestJarLibraryWriter(manifest).write(unsignedOutput, classPath, library) + ManifestJarLibraryWriter(manifest).write(unsignedOutput, classPath, classes) } ByteArrayOutputStream().use { signedOutput ->