From db07c93471b81062b7c463edf951cc6aa82bb3a8 Mon Sep 17 00:00:00 2001 From: Graham Date: Thu, 14 Sep 2023 23:25:27 +0100 Subject: [PATCH] Add CabLibraryReader Signed-off-by: Graham --- asm/build.gradle.kts | 1 + .../org/openrs2/asm/io/CabLibraryReader.kt | 45 +++++++++++++++++++ gradle/libs.versions.toml | 1 + 3 files changed, 47 insertions(+) create mode 100644 asm/src/main/kotlin/org/openrs2/asm/io/CabLibraryReader.kt diff --git a/asm/build.gradle.kts b/asm/build.gradle.kts index 4d29480eb6..8c40815717 100644 --- a/asm/build.gradle.kts +++ b/asm/build.gradle.kts @@ -14,6 +14,7 @@ dependencies { implementation(projects.cache) implementation(projects.compress) implementation(projects.crypto) + implementation(libs.cabParser) } publishing { diff --git a/asm/src/main/kotlin/org/openrs2/asm/io/CabLibraryReader.kt b/asm/src/main/kotlin/org/openrs2/asm/io/CabLibraryReader.kt new file mode 100644 index 0000000000..99a8b5034e --- /dev/null +++ b/asm/src/main/kotlin/org/openrs2/asm/io/CabLibraryReader.kt @@ -0,0 +1,45 @@ +package org.openrs2.asm.io + +import dorkbox.cabParser.CabParser +import dorkbox.cabParser.CabStreamSaver +import dorkbox.cabParser.structure.CabFileEntry +import org.objectweb.asm.ClassReader +import org.objectweb.asm.tree.ClassNode +import org.openrs2.asm.classpath.JsrInliner +import java.io.ByteArrayOutputStream +import java.io.InputStream +import java.io.OutputStream + +public object CabLibraryReader : LibraryReader { + private const val CLASS_SUFFIX = ".class" + + override fun read(input: InputStream): Iterable { + val classes = mutableListOf() + + ByteArrayOutputStream().use { tempOutput -> + CabParser(input, object : CabStreamSaver { + override fun closeOutputStream(outputStream: OutputStream, entry: CabFileEntry) { + if (entry.name.endsWith(CLASS_SUFFIX)) { + val clazz = ClassNode() + val reader = ClassReader(tempOutput.toByteArray()) + reader.accept(JsrInliner(clazz), ClassReader.SKIP_DEBUG or ClassReader.SKIP_FRAMES) + + classes += clazz + } + + tempOutput.reset() + } + + override fun openOutputStream(entry: CabFileEntry): OutputStream { + return tempOutput + } + + override fun saveReservedAreaData(data: ByteArray?, dataLength: Int): Boolean { + return false + } + }).extractStream() + } + + return classes + } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index ca81ff9844..19262fc750 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -29,6 +29,7 @@ bootstrapTable = { module = "org.webjars.npm:bootstrap-table", version = "1.22.1 bouncyCastle-pkix = { module = "org.bouncycastle:bcpkix-jdk15on", version = "1.70" } bouncyCastle-provider = { module = "org.bouncycastle:bcprov-jdk15on", version = "1.70" } byteUnits = { module = "com.jakewharton.byteunits:byteunits", version = "0.9.1" } +cabParser = { module = "com.dorkbox:CabParser", version = "3.4" } clikt = { module = "com.github.ajalt.clikt:clikt", version = "4.2.0" } commons-compress = { module = "org.apache.commons:commons-compress", version.ref = "commons-compress" } fastutil = { module = "it.unimi.dsi:fastutil", version = "8.5.12" }