Export Manager

This commit is contained in:
Karlatemp 2020-09-10 12:09:53 +08:00
parent c42fd3a8a6
commit 9c832f0afa
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8
4 changed files with 139 additions and 5 deletions

View File

@ -0,0 +1,89 @@
/*
* Copyright (c) 2018-2020 Karlatemp. All rights reserved.
* @author Karlatemp <karlatemp@vip.qq.com> <https://github.com/Karlatemp>
*
* LuckPerms-Mirai/mirai-console.mirai-console.main/ExportManagerImpl.kt
*
* 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/Karlatemp/LuckPerms-Mirai/blob/master/LICENSE
*/
package net.mamoe.mirai.console.internal.plugin
import net.mamoe.mirai.console.plugin.jvm.ExportManager
internal class ExportManagerImpl(
private val rules: List<(String) -> Boolean?>
) : ExportManager {
override fun isExported(className: String): Boolean {
rules.forEach {
val result = it(className)
if (result != null) return@isExported result
}
return true
}
companion object {
fun parse(lines: Iterator<String>): ExportManagerImpl {
val rules = ArrayList<(String) -> Boolean?>()
lines.forEach { line ->
val trimed = line.trim()
if (trimed.isEmpty()) return@forEach
if (trimed[0] == '#') return@forEach
val command: String
val argument: String
kotlin.run {
val splitter = trimed.indexOf(' ')
if (splitter == -1) {
command = trimed
argument = ""
} else {
command = trimed.substring(0, splitter)
argument = trimed.substring(splitter + 1)
}
}
when (command) {
"export" -> {
if (argument.isBlank()) {
rules.add { true }
} else {
if (argument.endsWith(".")) {
rules.add {
if (it.startsWith(argument)) true else null
}
} else {
rules.add {
if (it == argument) true else null
}
}
}
}
"deny", "internal", "hidden" -> {
if (argument.isBlank()) {
rules.add { false }
} else {
if (argument.endsWith(".")) {
rules.add {
if (it.startsWith(argument)) false else null
}
} else {
rules.add {
if (it == argument) false else null
}
}
}
}
"export-all" -> {
rules.add { true }
}
"deny-all", "hidden-all" -> {
rules.add { false }
}
}
}
return ExportManagerImpl(rules)
}
}
}

View File

@ -23,7 +23,6 @@ import net.mamoe.mirai.console.plugin.jvm.*
import net.mamoe.mirai.console.util.CoroutineScopeUtils.childScope
import net.mamoe.mirai.utils.MiraiLogger
import java.io.File
import java.net.URLClassLoader
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentLinkedQueue
@ -54,15 +53,29 @@ internal object JarPluginLoaderImpl :
override fun Sequence<File>.extractPlugins(): List<JvmPlugin> {
ensureActive()
fun Sequence<Map.Entry<File, ClassLoader>>.findAllInstances(): Sequence<Map.Entry<File, JvmPlugin>> {
fun Sequence<Map.Entry<File, JvmPluginClassLoader>>.findAllInstances(): Sequence<Map.Entry<File, JvmPlugin>> {
return map { (f, pluginClassLoader) ->
f to pluginClassLoader.findServices(
val exportManagers = pluginClassLoader.findServices(
ExportManager::class
).loadAllServices()
if (exportManagers.isEmpty()) {
val rules = pluginClassLoader.getResourceAsStream("export-rules.txt")
if (rules == null)
pluginClassLoader.declaredFilter = StandardExportManagers.AllExported
else rules.bufferedReader(Charsets.UTF_8).useLines {
pluginClassLoader.declaredFilter = ExportManagerImpl.parse(it.iterator())
}
} else {
pluginClassLoader.declaredFilter = exportManagers[0]
}
f to (pluginClassLoader.findServices(
JvmPlugin::class,
KotlinPlugin::class,
AbstractJvmPlugin::class,
JavaPlugin::class
).loadAllServices()
).loadAllServices())
}.flatMap { (f, list) ->
list.associateBy { f }.asSequence()
}
}

View File

@ -12,6 +12,7 @@
package net.mamoe.mirai.console.internal.plugin
import net.mamoe.mirai.console.plugin.jvm.ExportManager
import java.net.URL
import java.net.URLClassLoader
import java.util.concurrent.ConcurrentHashMap
@ -22,6 +23,7 @@ internal class JvmPluginClassLoader(
val classloaders: Collection<JvmPluginClassLoader>
) : URLClassLoader(urls, parent) {
private val cache = ConcurrentHashMap<String, Class<*>>()
internal var declaredFilter: ExportManager? = null
companion object {
init {
@ -43,9 +45,15 @@ internal class JvmPluginClassLoader(
return null
classloaders.forEach { otherClassloader ->
if (otherClassloader === this) return@forEach
val filter = otherClassloader.declaredFilter
if (filter != null) {
if (!filter.isExported(name)) {
return@forEach
}
}
val otherClass = otherClassloader.findClass(name, true)
if (otherClass != null) return otherClass
}
return null
}
}
}

View File

@ -0,0 +1,24 @@
/*
* 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.plugin.jvm
public fun interface ExportManager {
public fun isExported(className: String): Boolean
}
public object StandardExportManagers {
public object AllExported : ExportManager {
override fun isExported(className: String): Boolean = true
}
public object AllDenied : ExportManager {
override fun isExported(className: String): Boolean = false
}
}