mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-26 16:10:11 +08:00
Improve service finding
- Close resource stream - Skip comments
This commit is contained in:
parent
825332abfe
commit
0fa67b7699
@ -14,7 +14,6 @@ package net.mamoe.mirai.console.internal.util
|
|||||||
import net.mamoe.mirai.console.internal.data.cast
|
import net.mamoe.mirai.console.internal.data.cast
|
||||||
import net.mamoe.mirai.console.internal.data.createInstanceOrNull
|
import net.mamoe.mirai.console.internal.data.createInstanceOrNull
|
||||||
import net.mamoe.mirai.console.internal.plugin.BuiltInJvmPluginLoaderImpl
|
import net.mamoe.mirai.console.internal.plugin.BuiltInJvmPluginLoaderImpl
|
||||||
import java.io.InputStream
|
|
||||||
import java.lang.reflect.Modifier
|
import java.lang.reflect.Modifier
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.reflect.KClass
|
import kotlin.reflect.KClass
|
||||||
@ -30,9 +29,13 @@ internal object PluginServiceHelper {
|
|||||||
|
|
||||||
fun <T : Any> ClassLoader.findServices(vararg serviceTypes: KClass<out T>): ServiceList<T> =
|
fun <T : Any> ClassLoader.findServices(vararg serviceTypes: KClass<out T>): ServiceList<T> =
|
||||||
serviceTypes.flatMap { serviceType ->
|
serviceTypes.flatMap { serviceType ->
|
||||||
getResourceAsStream("META-INF/services/" + serviceType.qualifiedName!!)
|
getResourceAsStream("META-INF/services/" + serviceType.qualifiedName!!)?.let { stream ->
|
||||||
?.use(InputStream::readBytes)
|
stream.bufferedReader().useLines { lines ->
|
||||||
?.let(::String)?.lines()?.filter(String::isNotBlank).orEmpty()
|
lines.filter(String::isNotBlank)
|
||||||
|
.filter { it[0] != '#' }
|
||||||
|
.toList()
|
||||||
|
}
|
||||||
|
}.orEmpty()
|
||||||
}.let { ServiceList(this, it) }
|
}.let { ServiceList(this, it) }
|
||||||
|
|
||||||
fun <T : Any> ServiceList<T>.loadAllServices(): List<T> {
|
fun <T : Any> ServiceList<T>.loadAllServices(): List<T> {
|
||||||
|
Loading…
Reference in New Issue
Block a user