From 2b3e9d318a282ae55a466e765dc59b7e78dcc37e Mon Sep 17 00:00:00 2001 From: Graham Date: Sun, 4 Jul 2021 14:25:23 +0100 Subject: [PATCH] Add commands for packing and unpacking pack200/packclass files Signed-off-by: Graham --- asm/build.gradle.kts | 1 + compress-cli/build.gradle.kts | 4 +++- .../openrs2/compress/cli/CompressCommand.kt | 10 +++++++- .../compress/cli/pack200/Pack200Command.kt | 24 +++++++++++++++++++ .../compress/cli/pack200/Unpack200Command.kt | 24 +++++++++++++++++++ .../cli/packclass/PackClassCommand.kt | 23 ++++++++++++++++++ .../cli/packclass/UnpackClassCommand.kt | 23 ++++++++++++++++++ 7 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 compress-cli/src/main/kotlin/org/openrs2/compress/cli/pack200/Pack200Command.kt create mode 100644 compress-cli/src/main/kotlin/org/openrs2/compress/cli/pack200/Unpack200Command.kt create mode 100644 compress-cli/src/main/kotlin/org/openrs2/compress/cli/packclass/PackClassCommand.kt create mode 100644 compress-cli/src/main/kotlin/org/openrs2/compress/cli/packclass/UnpackClassCommand.kt diff --git a/asm/build.gradle.kts b/asm/build.gradle.kts index 3b696c746e..9cb5d642c9 100644 --- a/asm/build.gradle.kts +++ b/asm/build.gradle.kts @@ -8,6 +8,7 @@ dependencies { api(libs.bundles.asm) api(libs.guice) api(libs.jackson.databind) + api(libs.netty.buffer) implementation(projects.buffer) implementation(projects.cache) diff --git a/compress-cli/build.gradle.kts b/compress-cli/build.gradle.kts index 73967c622d..4070988237 100644 --- a/compress-cli/build.gradle.kts +++ b/compress-cli/build.gradle.kts @@ -11,7 +11,9 @@ application { dependencies { api(libs.clikt) + implementation(projects.asm) implementation(projects.compress) + implementation(projects.util) } publishing { @@ -24,7 +26,7 @@ publishing { description.set( """ Tools for compressing and uncompressing headerless bzip2, - DEFLATE, gzip and LZMA files. + DEFLATE, gzip, LZMA, Pack200 and PackClass files. """.trimIndent() ) } diff --git a/compress-cli/src/main/kotlin/org/openrs2/compress/cli/CompressCommand.kt b/compress-cli/src/main/kotlin/org/openrs2/compress/cli/CompressCommand.kt index c77ea13c63..eaf4a2daf3 100644 --- a/compress-cli/src/main/kotlin/org/openrs2/compress/cli/CompressCommand.kt +++ b/compress-cli/src/main/kotlin/org/openrs2/compress/cli/CompressCommand.kt @@ -11,6 +11,10 @@ import org.openrs2.compress.cli.gzip.GunzipLaxCommand import org.openrs2.compress.cli.gzip.GzipCommand import org.openrs2.compress.cli.lzma.LzmaCommand import org.openrs2.compress.cli.lzma.UnlzmaCommand +import org.openrs2.compress.cli.pack200.Pack200Command +import org.openrs2.compress.cli.pack200.Unpack200Command +import org.openrs2.compress.cli.packclass.PackClassCommand +import org.openrs2.compress.cli.packclass.UnpackClassCommand public fun main(args: Array): Unit = CompressCommand().main(args) @@ -25,7 +29,11 @@ public class CompressCommand : NoOpCliktCommand(name = "compress") { GunzipCommand(), GunzipLaxCommand(), LzmaCommand(), - UnlzmaCommand() + UnlzmaCommand(), + Pack200Command(), + Unpack200Command(), + PackClassCommand(), + UnpackClassCommand() ) } } diff --git a/compress-cli/src/main/kotlin/org/openrs2/compress/cli/pack200/Pack200Command.kt b/compress-cli/src/main/kotlin/org/openrs2/compress/cli/pack200/Pack200Command.kt new file mode 100644 index 0000000000..ec6855dfba --- /dev/null +++ b/compress-cli/src/main/kotlin/org/openrs2/compress/cli/pack200/Pack200Command.kt @@ -0,0 +1,24 @@ +package org.openrs2.compress.cli.pack200 + +import com.github.ajalt.clikt.core.CliktCommand +import com.github.ajalt.clikt.parameters.options.option +import com.github.ajalt.clikt.parameters.types.defaultStdin +import com.github.ajalt.clikt.parameters.types.defaultStdout +import com.github.ajalt.clikt.parameters.types.inputStream +import com.github.ajalt.clikt.parameters.types.outputStream +import org.openrs2.compress.gzip.Gzip +import java.util.jar.JarInputStream +import java.util.jar.Pack200 + +public class Pack200Command : CliktCommand(name = "pack200") { + private val input by option().inputStream().defaultStdin() + private val output by option().outputStream().defaultStdout() + + override fun run() { + JarInputStream(input).use { jarInput -> + Gzip.createHeaderlessOutputStream(output).use { gzipOutput -> + Pack200.newPacker().pack(jarInput, gzipOutput) + } + } + } +} diff --git a/compress-cli/src/main/kotlin/org/openrs2/compress/cli/pack200/Unpack200Command.kt b/compress-cli/src/main/kotlin/org/openrs2/compress/cli/pack200/Unpack200Command.kt new file mode 100644 index 0000000000..d5df0b75cd --- /dev/null +++ b/compress-cli/src/main/kotlin/org/openrs2/compress/cli/pack200/Unpack200Command.kt @@ -0,0 +1,24 @@ +package org.openrs2.compress.cli.pack200 + +import com.github.ajalt.clikt.core.CliktCommand +import com.github.ajalt.clikt.parameters.options.option +import com.github.ajalt.clikt.parameters.types.defaultStdin +import com.github.ajalt.clikt.parameters.types.defaultStdout +import com.github.ajalt.clikt.parameters.types.inputStream +import com.github.ajalt.clikt.parameters.types.outputStream +import org.openrs2.compress.gzip.Gzip +import org.openrs2.util.io.DeterministicJarOutputStream +import java.util.jar.Pack200 + +public class Unpack200Command : CliktCommand(name = "unpack200") { + private val input by option().inputStream().defaultStdin() + private val output by option().outputStream().defaultStdout() + + override fun run() { + Gzip.createHeaderlessInputStream(input).use { gzipInput -> + DeterministicJarOutputStream(output).use { jarOutput -> + Pack200.newUnpacker().unpack(gzipInput, jarOutput) + } + } + } +} diff --git a/compress-cli/src/main/kotlin/org/openrs2/compress/cli/packclass/PackClassCommand.kt b/compress-cli/src/main/kotlin/org/openrs2/compress/cli/packclass/PackClassCommand.kt new file mode 100644 index 0000000000..6335eac8fc --- /dev/null +++ b/compress-cli/src/main/kotlin/org/openrs2/compress/cli/packclass/PackClassCommand.kt @@ -0,0 +1,23 @@ +package org.openrs2.compress.cli.packclass + +import com.github.ajalt.clikt.core.CliktCommand +import com.github.ajalt.clikt.parameters.options.option +import com.github.ajalt.clikt.parameters.types.defaultStdin +import com.github.ajalt.clikt.parameters.types.defaultStdout +import com.github.ajalt.clikt.parameters.types.inputStream +import com.github.ajalt.clikt.parameters.types.outputStream +import io.netty.buffer.ByteBufAllocator +import org.openrs2.asm.classpath.ClassPath +import org.openrs2.asm.io.JarLibraryReader +import org.openrs2.asm.io.PackClassLibraryWriter + +public class PackClassCommand : CliktCommand(name = "packclass") { + private val input by option().inputStream().defaultStdin() + private val output by option().outputStream().defaultStdout() + + override fun run() { + val classes = JarLibraryReader.read(input) + val classPath = ClassPath(ClassLoader.getPlatformClassLoader(), emptyList(), emptyList()) + PackClassLibraryWriter(ByteBufAllocator.DEFAULT).write(output, classPath, classes) + } +} diff --git a/compress-cli/src/main/kotlin/org/openrs2/compress/cli/packclass/UnpackClassCommand.kt b/compress-cli/src/main/kotlin/org/openrs2/compress/cli/packclass/UnpackClassCommand.kt new file mode 100644 index 0000000000..9f0bf2916b --- /dev/null +++ b/compress-cli/src/main/kotlin/org/openrs2/compress/cli/packclass/UnpackClassCommand.kt @@ -0,0 +1,23 @@ +package org.openrs2.compress.cli.packclass + +import com.github.ajalt.clikt.core.CliktCommand +import com.github.ajalt.clikt.parameters.options.option +import com.github.ajalt.clikt.parameters.types.defaultStdin +import com.github.ajalt.clikt.parameters.types.defaultStdout +import com.github.ajalt.clikt.parameters.types.inputStream +import com.github.ajalt.clikt.parameters.types.outputStream +import io.netty.buffer.ByteBufAllocator +import org.openrs2.asm.classpath.ClassPath +import org.openrs2.asm.io.JarLibraryWriter +import org.openrs2.asm.io.PackClassLibraryReader + +public class UnpackClassCommand : CliktCommand(name = "unpackclass") { + private val input by option().inputStream().defaultStdin() + private val output by option().outputStream().defaultStdout() + + override fun run() { + val classes = PackClassLibraryReader(ByteBufAllocator.DEFAULT).read(input) + val classPath = ClassPath(ClassLoader.getPlatformClassLoader(), emptyList(), emptyList()) + JarLibraryWriter.write(output, classPath, classes) + } +}