Skip to content

Commit

Permalink
Initial support for plugin-classpath v2
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz committed Aug 21, 2024
1 parent 32608b9 commit 2874720
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/main/kotlin/korge/util/PluginClasspath.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ package korge.util

import java.nio.*

class PluginClasspath(val version: Int, val entries: List<Plugin>) {
class Plugin(var name: String, var pluginXml: String, var jars: List<String>)
// classpath.kt
class PluginClasspath(val version: Int, val isJarOnly: Int, val mainPluginDescriptorContent: String?, val entries: List<Plugin>) {
class Plugin(var name: String, var pluginXml: String, var jars: List<String>) {
override fun toString(): String = "Plugin(name=$name, pluginXml=${pluginXml.length}, jars=${jars.size})"
}

fun encode(): ByteArray {
return encodeV1()
}

fun encodeV1(): ByteArray {
val bytes = ByteArray(8 * 1024 * 1024)
val buffer = ByteBuffer.wrap(bytes)
buffer.putShort(version.toShort())
buffer.put(version.toByte())
buffer.put(isJarOnly.toByte())
buffer.putShort(entries.size.toShort())
for (entry in entries) {
//println("- ENTRY: ${entry.name}, pos=${buffer.position()} : XML=${entry.pluginXml.length}")
Expand All @@ -26,8 +34,18 @@ class PluginClasspath(val version: Int, val entries: List<Plugin>) {
companion object {
fun parse(bytes: ByteArray): PluginClasspath {
val buffer = ByteBuffer.wrap(bytes)
val version = buffer.getShort().toInt()
val version = buffer.get().toInt()
val isJarOnly = buffer.get().toInt()
check(version == 1 || version == 2)
check(isJarOnly == 1)
val mainPluginDescriptorContent = if (version == 2) {
val mainPluginDescriptorContentSize = buffer.getInt()
ByteArray(mainPluginDescriptorContentSize).also { buffer.get(it) }.decodeToString()
} else {
null
}
val count = buffer.getShort().toInt()
//println(count)
val plugins = (0 until count).map {
val jarCount = buffer.getShort()
//println("--------------")
Expand All @@ -37,7 +55,7 @@ class PluginClasspath(val version: Int, val entries: List<Plugin>) {
val jars = (0 until jarCount).map { buffer.getStringWithLen(long = false) }
Plugin(name, pluginXml, jars)
}
return PluginClasspath(version, plugins)
return PluginClasspath(version, isJarOnly, mainPluginDescriptorContent, plugins)
}

fun ByteBuffer.getStringWithLen(long: Boolean): String {
Expand Down
13 changes: 13 additions & 0 deletions src/test/kotlin/korge/util/PluginClasspathRebuilderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,17 @@ class PluginClasspathRebuilderTest {
assertEquals(bytes.size, encoded.size)
assertContentEquals(bytes, encoded)
}

@Test
fun testV2() {
val bytes = PluginClasspathRebuilderTest::class.java.getResource("/plugin-classpath.2024-02.txt")!!.readBytes()
val parsed = PluginClasspath.parse(bytes)
//println(parsed.mainPluginDescriptorContent)
//for (entry in parsed.entries) {
// println(entry)
// if (entry.pluginXml.contains("lombok")) {
// println(entry.pluginXml)
// }
//}
}
}
Binary file added src/test/resources/plugin-classpath.2024-02.txt
Binary file not shown.

0 comments on commit 2874720

Please sign in to comment.