Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues in new console plugin system #1926

Merged
merged 4 commits into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions mirai-console/backend/integration-test/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/

@file:Suppress("UnusedImport")
Expand Down Expand Up @@ -84,8 +84,8 @@ mcit_test.configure {
}

val crtProject = project
subprojects {
if (project.parent == crtProject) {
allprojects {
if (project != crtProject) {
project.afterEvaluate {
val tk = tasks.named<Jar>("jar")
subplugins.add(tk)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import org.objectweb.asm.ClassWriter
import org.objectweb.asm.Opcodes
import org.objectweb.asm.Type
import java.io.File
import java.io.FileDescriptor
import java.io.FileOutputStream
import java.io.PrintStream
import java.util.concurrent.ConcurrentLinkedDeque
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream
Expand Down Expand Up @@ -66,13 +68,22 @@ internal fun main() {
.toList()

File("plugins").mkdirs()
File("modules").mkdirs()
prepareConsole()

testUnits.forEach { (it as? AbstractTestPointAsPlugin)?.generatePluginJar() }
testUnits.forEach { it.internalBCS() }

MiraiConsoleTerminalLoader.startAsDaemon()
Thread.sleep(2000L)

try {
MiraiConsoleTerminalLoader.startAsDaemon()
} catch (e: Throwable) {
val ps = PrintStream(FileOutputStream(FileDescriptor.out))
e.printStackTrace(ps)
ps.flush()
exitProcess(1)
}
if (!MiraiConsole.isActive) {
error("Failed to start console")
}
Expand Down Expand Up @@ -107,9 +118,16 @@ loggers:

readStringListFromEnv("IT_PLUGINS").forEach { path ->
val jarFile = File(path)
val target = File("plugins/${jarFile.name}").mkparents()
jarFile.copyTo(target, overwrite = true)
println("[MCIT] Copied external plugin: $jarFile")
if (jarFile.name.startsWith("module-")) {
// DYN MODULE
val target = File("modules/${jarFile.name}").mkparents()
jarFile.copyTo(target, overwrite = true)
println("[MCIT] Copied module: $jarFile")
} else {
val target = File("plugins/${jarFile.name}").mkparents()
jarFile.copyTo(target, overwrite = true)
println("[MCIT] Copied external plugin: $jarFile")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package net.mamoe.console.integrationtest.ep.dependonother
import net.mamoe.console.integrationtest.ep.mcitselftest.MCITSelfTestPlugin
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
import net.mamoe.mirai.utils.error
import net.mamoe.mirai.utils.info
import kotlin.test.assertFailsWith
import kotlin.test.assertNotEquals
Expand Down Expand Up @@ -45,7 +46,8 @@ public object PluginDependOnOther : KotlinPlugin(
logger.info { "Gson located $gsonC <${gsonC.classLoader}>" }
assertSame(gsonC, Class.forName(gsonC.name, false, pluginDepDynDownload.classLoader))
assertFailsWith<ClassNotFoundException> {
Class.forName("com.zaxxer.sparsebits.SparseBitSet") // private in dynamic-dep-download
val c = Class.forName("com.zaxxer.sparsebits.SparseBitSet") // private in dynamic-dep-download
logger.error { "C: $c, from: ${c.classLoader}" }
}
assertFailsWith<ClassNotFoundException> {
Class.forName("net.mamoe.assertion.something.not.existing")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/

@file:Suppress("UnusedImport")

plugins {
kotlin("jvm")
kotlin("plugin.serialization")
id("java")
}

version = "0.0.0"

kotlin {
explicitApiWarning()
}

dependencies {
api(project(":mirai-console.integration-test"))
api(parent!!.project("module-service-loader-typedef"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#
# Copyright 2019-2022 Mamoe Technologies and contributors.
#
# 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
# Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
#
# https://github.com/mamoe/mirai/blob/dev/LICENSE
#

net.mamoe.console.integrationtest.mod.serviceimpl.ServiceImpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
service-loader-impl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
package net.mamoe.console.integrationtest.mod.serviceimpl

import net.mamoe.console.integrationtest.mod.servicetypedef.ServiceTypedef

public class ServiceImpl : ServiceTypedef {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
service-loader-typedef
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/

package net.mamoe.console.integrationtest.mod.servicetypedef

public interface ServiceTypedef {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#
# Copyright 2019-2022 Mamoe Technologies and contributors.
#
# 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
# Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
#
# https://github.com/mamoe/mirai/blob/dev/LICENSE
#

net.mamoe.console.itest.serviceloader.PMain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from plugin
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/

@file:Suppress("UnusedImport")

plugins {
kotlin("jvm")
kotlin("plugin.serialization")
id("java")
}

version = "0.0.0"

kotlin {
explicitApiWarning()
}

dependencies {
api(project(":mirai-console.integration-test"))
api(parent!!.project("module-service-loader-typedef"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#
# Copyright 2019-2022 Mamoe Technologies and contributors.
#
# 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
# Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
#
# https://github.com/mamoe/mirai/blob/dev/LICENSE
#

net.mamoe.console.itest.serviceloader.ndep.PS
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#
# Copyright 2019-2022 Mamoe Technologies and contributors.
#
# 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
# Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
#
# https://github.com/mamoe/mirai/blob/dev/LICENSE
#

net.mamoe.console.itest.serviceloader.ndep.PMain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from 2nd plugin
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/

package net.mamoe.console.itest.serviceloader.ndep

import net.mamoe.console.integrationtest.mod.servicetypedef.ServiceTypedef
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
import net.mamoe.mirai.utils.info
import java.util.*
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull


internal class PS : ServiceTypedef

internal object PMain : KotlinPlugin(JvmPluginDescription("net.mamoe.console.itest.serviceloader-ndp", "0.0.0") {
dependsOn("net.mamoe.console.itest.serviceloader")
}) {
override fun onEnable() {
val loader = ServiceLoader.load(
Class.forName("net.mamoe.console.integrationtest.mod.servicetypedef.ServiceTypedef"),
javaClass.classLoader,
)
val services = loader.asSequence().map { it.javaClass.name }.toMutableList()
services.sort()
services.forEach { service ->
logger.info { "Service: $service" }
}
assertEquals(
mutableListOf(
"net.mamoe.console.integrationtest.mod.serviceimpl.ServiceImpl",
"net.mamoe.console.itest.serviceloader.ndep.PS",
), services
)
assertEquals(
"from 2nd plugin",
javaClass.getResourceAsStream("/test-res.txt")!!.reader().use { it.readText() }.trim(),
)
val tstRes = javaClass.classLoader.getResources("test-res.txt").asSequence().onEach {
println(it)
}.toMutableList()
// /service-loader-2dep-plugin-0.0.0.jar!/test-res.txt
// /service-loader-0.0.0.jar!/test-res.txt
// /module-service-loader-typedef-0.0.0.jar!/test-res.txt
// /module-service-loader-impl-0.0.0.jar!/test-res.txt
assertEquals(4, tstRes.size)

assertNotNull(javaClass.getResource("/net/mamoe/console/it/psl/PluginSharedLib.class").also {
println(it)
})
assertEquals(
1,
javaClass.classLoader.getResources("net/mamoe/console/it/psl/PluginSharedLib.class")
.asSequence().toList()
.also {
println(it)
}.size
)
assertNull(javaClass.getResource("/net/mamoe/mirai/console/MiraiConsole.class"))
assertNull(javaClass.getResource("/net/mamoe/mirai/Bot.class"))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/

@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")

package net.mamoe.console.itest.serviceloader

import net.mamoe.mirai.console.internal.plugin.DynLibClassLoader
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
import net.mamoe.mirai.utils.info
import java.io.File
import java.util.*
import kotlin.test.assertEquals

internal object PMain : KotlinPlugin(JvmPluginDescription("net.mamoe.console.itest.serviceloader", "0.0.0")) {
init {
val cl = PMain.javaClass.classLoader.parent as DynLibClassLoader
cl.addLib(File("modules/module-service-loader-typedef-0.0.0.jar"))
cl.addLib(File("modules/module-service-loader-impl-0.0.0.jar"))
}

override fun onEnable() {
val loader = ServiceLoader.load(
Class.forName("net.mamoe.console.integrationtest.mod.servicetypedef.ServiceTypedef"),
javaClass.classLoader,
)
val services = loader.asSequence().map { it.javaClass.name}.toMutableList()
services.forEach { service ->
logger.info { "Service: $service" }
}
assertEquals(mutableListOf("net.mamoe.console.integrationtest.mod.serviceimpl.ServiceImpl"), services)

assertEquals(
"from plugin",
javaClass.getResourceAsStream("/test-res.txt")!!.reader().use { it.readText() }.trim(),
)
val tstRes = javaClass.classLoader.getResources("test-res.txt").asSequence().onEach {
println(it)
}.toMutableList()
// /service-loader-0.0.0.jar!/test-res.txt
// /module-service-loader-typedef-0.0.0.jar!/test-res.txt
// /module-service-loader-impl-0.0.0.jar!/test-res.txt
assertEquals(3, tstRes.size)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/

@file:Suppress("UnusedImport")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal class BuiltInJvmPluginLoaderImpl(


internal val jvmPluginLoadingCtx: JvmPluginsLoadingCtx by lazy {
val classLoader = DynLibClassLoader(BuiltInJvmPluginLoaderImpl::class.java.classLoader)
val classLoader = DynLibClassLoader(BuiltInJvmPluginLoaderImpl::class.java.classLoader, "GlobalShared")
val ctx = JvmPluginsLoadingCtx(
classLoader,
mutableListOf(),
Expand Down
Loading