Ensure that the PermissionService is loaded correctly (#307)

* Ensure that the PermissionService is loaded correctly

* Update backend/mirai-console/src/extensions/PermissionServiceProvider.kt

* Mark `@ConsoleExperimentalApi`

Co-authored-by: Him188 <Him188@mamoe.net>
This commit is contained in:
Karlatemp 2021-03-26 19:00:25 +08:00 committed by GitHub
parent 5dc1c90ac4
commit 3ae575e9eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 2 deletions

View File

@ -74,7 +74,7 @@ public abstract class AbstractSingletonExtensionPoint<E : SingletonExtension<T>,
* [SingletonExtensionSelector] 选择后的实例. * [SingletonExtensionSelector] 选择后的实例.
*/ */
@ConsoleExperimentalApi @ConsoleExperimentalApi
public val selectedInstance: T by lazy { public open val selectedInstance: T by lazy {
GlobalComponentStorage.run { this@AbstractSingletonExtensionPoint.findSingletonInstance(extensionType, builtinImplementation) } GlobalComponentStorage.run { this@AbstractSingletonExtensionPoint.findSingletonInstance(extensionType, builtinImplementation) }
} }
} }

View File

@ -25,6 +25,8 @@ import net.mamoe.mirai.console.util.ConsoleExperimentalApi
public interface PermissionServiceProvider : SingletonExtension<PermissionService<*>> { public interface PermissionServiceProvider : SingletonExtension<PermissionService<*>> {
public companion object ExtensionPoint : public companion object ExtensionPoint :
AbstractSingletonExtensionPoint<PermissionServiceProvider, PermissionService<*>>(PermissionServiceProvider::class, BuiltInPermissionService) { AbstractSingletonExtensionPoint<PermissionServiceProvider, PermissionService<*>>(PermissionServiceProvider::class, BuiltInPermissionService) {
internal var permissionServiceOk = false
@ConsoleExperimentalApi @ConsoleExperimentalApi
public val providerPlugin: Plugin? by lazy { public val providerPlugin: Plugin? by lazy {
GlobalComponentStorage.run { GlobalComponentStorage.run {
@ -33,6 +35,15 @@ public interface PermissionServiceProvider : SingletonExtension<PermissionServic
PermissionServiceProvider.getExtensions().find { it.extension.instance === instance }?.plugin PermissionServiceProvider.getExtensions().find { it.extension.instance === instance }?.plugin
} }
} }
@ConsoleExperimentalApi
override val selectedInstance: PermissionService<*>
get() {
if (!permissionServiceOk) {
error("PermissionService not yet loaded")
}
return super.selectedInstance
}
} }
} }

View File

@ -180,6 +180,7 @@ internal object MiraiConsoleImplementationBridge : CoroutineScope, MiraiConsoleI
phase("load PermissionService") { phase("load PermissionService") {
mainLogger.verbose { "Loading PermissionService..." } mainLogger.verbose { "Loading PermissionService..." }
PermissionServiceProvider.permissionServiceOk = true
PermissionService.INSTANCE.let { ps -> PermissionService.INSTANCE.let { ps ->
if (ps is BuiltInPermissionService) { if (ps is BuiltInPermissionService) {
ConsoleDataScope.addAndReloadConfig(ps.config) ConsoleDataScope.addAndReloadConfig(ps.config)