Limit JvmPluginClassLoader resource loading region

Fix #205
This commit is contained in:
Karlatemp 2020-10-10 11:36:23 +08:00
parent bde3d995be
commit 5cdd32ab4c
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8
2 changed files with 29 additions and 2 deletions

View File

@ -42,7 +42,7 @@ internal object BuiltInJvmPluginLoaderImpl :
override val dataStorage: PluginDataStorage
get() = MiraiConsoleImplementationBridge.dataStorageForJvmPluginLoader
internal val classLoaders: MutableList<ClassLoader> = mutableListOf()
internal val classLoaders: MutableList<JvmPluginClassLoader> = mutableListOf()
@Suppress("EXTENSION_SHADOWED_BY_MEMBER") // doesn't matter
override fun getPluginDescription(plugin: JvmPlugin): JvmPluginDescription = plugin.description
@ -68,7 +68,7 @@ internal object BuiltInJvmPluginLoaderImpl :
val filePlugins = this.filterNot {
pluginFileToInstanceMap.containsKey(it)
}.associateWith {
URLClassLoader(arrayOf(it.toURI().toURL()), MiraiConsole::class.java.classLoader)
JvmPluginClassLoader(it, MiraiConsole::class.java.classLoader)
}.onEach { (_, classLoader) ->
classLoaders.add(classLoader)
}.asSequence().findAllInstances().onEach {

View File

@ -0,0 +1,27 @@
/*
* Copyright 2019-2020 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found via the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*
*/
package net.mamoe.mirai.console.internal.plugin
import java.io.File
import java.net.URL
import java.net.URLClassLoader
import java.util.*
internal class JvmPluginClassLoader(
file: File,
parent: ClassLoader?,
) : URLClassLoader(arrayOf(file.toURI().toURL()), parent) {
//// 只允许插件 getResource 时获取插件自身资源, #205
override fun getResources(name: String?): Enumeration<URL> = findResources(name)
override fun getResource(name: String?): URL? = findResource(name)
// getResourceAsStream 在 URLClassLoader 中通过 getResource 确定资源
// 因此无需 override getResourceAsStream
}