diff --git a/backend/mirai-console/src/internal/util/CommonUtils.kt b/backend/mirai-console/src/internal/util/CommonUtils.kt index 27f5d5bdd..037bd0bb7 100644 --- a/backend/mirai-console/src/internal/util/CommonUtils.kt +++ b/backend/mirai-console/src/internal/util/CommonUtils.kt @@ -27,4 +27,20 @@ internal inline fun runIgnoreException(block: () -> Unit if (e is E) return null throw e } +} + +internal fun getCallerClassloader(): ClassLoader? { + return runCatching { + /* + java.base/java.lang.Thread.getStackTrace(Thread.java:1598) + net.mamoe.mirai.console.internal.util.CommonUtils.getCallerClassloader(CommonUtils.kt:37) + net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription$Companion.loadFromResource$default(JvmPluginDescription.kt:67) + net.mamoe.mirai.console.KotlinP.(TestMiraiConosle.kt:34) + net.mamoe.mirai.console.KotlinP.(TestMiraiConosle.kt:34) + net.mamoe.mirai.console.TestMiraiConosleKt.main(TestMiraiConosle.kt:37) + net.mamoe.mirai.console.TestMiraiConosleKt.main(TestMiraiConosle.kt) + */ + val traces = Thread.currentThread().stackTrace + Class.forName(traces[3].className).classLoader + }.getOrNull() } \ No newline at end of file diff --git a/backend/mirai-console/src/plugin/jvm/JvmPluginDescription.kt b/backend/mirai-console/src/plugin/jvm/JvmPluginDescription.kt index ee17ceef4..6a4452879 100644 --- a/backend/mirai-console/src/plugin/jvm/JvmPluginDescription.kt +++ b/backend/mirai-console/src/plugin/jvm/JvmPluginDescription.kt @@ -14,12 +14,11 @@ package net.mamoe.mirai.console.plugin.jvm import kotlinx.serialization.Serializable import net.mamoe.mirai.console.compiler.common.ResolveContext import net.mamoe.mirai.console.compiler.common.ResolveContext.Kind.* +import net.mamoe.mirai.console.internal.util.getCallerClassloader import net.mamoe.mirai.console.plugin.description.PluginDependency import net.mamoe.mirai.console.plugin.description.PluginDescription import net.mamoe.mirai.console.util.SemVersion import net.mamoe.yamlkt.Yaml -import sun.reflect.CallerSensitive -import sun.reflect.Reflection /** * JVM 插件的描述. 通常作为 `plugin.yml` @@ -62,12 +61,19 @@ public interface JvmPluginDescription : PluginDescription { block: JvmPluginDescriptionBuilder.() -> Unit = {}, ): JvmPluginDescription = error("Shouldn't be called") + /** + * 从 [pluginClassloader] 读取资源文件 [filename] 并以 YAML 格式解析为 [SimpleJvmPluginDescription] + * + * @param filename [ClassLoader.getResourceAsStream] 的参数 `name` + * @param pluginClassloader 默认通过 [Thread.getStackTrace] 获取调用方 [Class] 然后获取其 [Class.getClassLoader]. + */ + @JvmOverloads @JvmStatic - @CallerSensitive - public fun loadFromResource(filename: String = "config.yaml"): JvmPluginDescription { - val callerClass = Reflection.getCallerClass() - val stream = callerClass.getResourceAsStream(filename) ?: callerClass.classLoader.getResourceAsStream(filename) - ?: error("Cannot find plugin description resource") + public fun loadFromResource( + filename: String = "plugin.yml", + pluginClassloader: ClassLoader = getCallerClassloader() ?: error("Cannot find caller classloader, please specify manually."), + ): JvmPluginDescription { + val stream = pluginClassloader.getResourceAsStream(filename) ?: error("Cannot find plugin description resource '$filename'") val bytes = stream.use { it.readBytes() }