Update JvmPluginDescription.loadFromResource for all JDKs

This commit is contained in:
Him188 2020-11-12 09:58:47 +08:00
parent 1d6dccb793
commit 3e36fa42fe
2 changed files with 29 additions and 7 deletions

View File

@ -28,3 +28,19 @@ internal inline fun <reified E : Throwable> runIgnoreException(block: () -> Unit
throw e 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.<init>(TestMiraiConosle.kt:34)
net.mamoe.mirai.console.KotlinP.<clinit>(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()
}

View File

@ -14,12 +14,11 @@ package net.mamoe.mirai.console.plugin.jvm
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import net.mamoe.mirai.console.compiler.common.ResolveContext import net.mamoe.mirai.console.compiler.common.ResolveContext
import net.mamoe.mirai.console.compiler.common.ResolveContext.Kind.* 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.PluginDependency
import net.mamoe.mirai.console.plugin.description.PluginDescription import net.mamoe.mirai.console.plugin.description.PluginDescription
import net.mamoe.mirai.console.util.SemVersion import net.mamoe.mirai.console.util.SemVersion
import net.mamoe.yamlkt.Yaml import net.mamoe.yamlkt.Yaml
import sun.reflect.CallerSensitive
import sun.reflect.Reflection
/** /**
* JVM 插件的描述. 通常作为 `plugin.yml` * JVM 插件的描述. 通常作为 `plugin.yml`
@ -62,12 +61,19 @@ public interface JvmPluginDescription : PluginDescription {
block: JvmPluginDescriptionBuilder.() -> Unit = {}, block: JvmPluginDescriptionBuilder.() -> Unit = {},
): JvmPluginDescription = error("Shouldn't be called") ): JvmPluginDescription = error("Shouldn't be called")
/**
* [pluginClassloader] 读取资源文件 [filename] 并以 YAML 格式解析为 [SimpleJvmPluginDescription]
*
* @param filename [ClassLoader.getResourceAsStream] 的参数 `name`
* @param pluginClassloader 默认通过 [Thread.getStackTrace] 获取调用方 [Class] 然后获取其 [Class.getClassLoader].
*/
@JvmOverloads
@JvmStatic @JvmStatic
@CallerSensitive public fun loadFromResource(
public fun loadFromResource(filename: String = "config.yaml"): JvmPluginDescription { filename: String = "plugin.yml",
val callerClass = Reflection.getCallerClass() pluginClassloader: ClassLoader = getCallerClassloader() ?: error("Cannot find caller classloader, please specify manually."),
val stream = callerClass.getResourceAsStream(filename) ?: callerClass.classLoader.getResourceAsStream(filename) ): JvmPluginDescription {
?: error("Cannot find plugin description resource") val stream = pluginClassloader.getResourceAsStream(filename) ?: error("Cannot find plugin description resource '$filename'")
val bytes = stream.use { it.readBytes() } val bytes = stream.use { it.readBytes() }