Prohibit premature usages of PermissionService

This commit is contained in:
Him188 2022-02-22 19:53:08 +00:00
parent 044457ae24
commit 77e2a5c3b4
2 changed files with 12 additions and 1 deletions

View File

@ -86,6 +86,10 @@ internal class MiraiConsoleImplementationBridge(
// used internally
val globalComponentStorage: GlobalComponentStorageImpl by lazy { GlobalComponentStorageImpl() }
// tentative workaround for https://github.com/mamoe/mirai/pull/1889#pullrequestreview-887903183
@Volatile
var permissionSeviceLoaded: Boolean = false
override val mainLogger: MiraiLogger by lazy { createLogger("main") }
init {
@ -187,6 +191,7 @@ internal class MiraiConsoleImplementationBridge(
phase("load PermissionService") {
mainLogger.verbose { "Loading PermissionService..." }
permissionSeviceLoaded = true
PermissionService.INSTANCE.let { ps ->
if (ps is BuiltInPermissionService) {
consoleDataScope.addAndReloadConfig(ps.config)

View File

@ -11,6 +11,7 @@
package net.mamoe.mirai.console.permission
import net.mamoe.mirai.console.MiraiConsoleImplementation
import net.mamoe.mirai.console.compiler.common.ResolveContext
import net.mamoe.mirai.console.compiler.common.ResolveContext.Kind.COMMAND_NAME
import net.mamoe.mirai.console.extension.instance
@ -147,7 +148,12 @@ public interface PermissionService<P : Permission> {
@get:JvmName("getInstance")
@JvmStatic
public val INSTANCE: PermissionService<out Permission>
get() = GlobalComponentStorage.getPreferredExtension(PermissionServiceProvider).instance
get() {
if (!MiraiConsoleImplementation.getBridge().permissionSeviceLoaded) {
error("PermissionService is not yet ready.")
}
return GlobalComponentStorage.getPreferredExtension(PermissionServiceProvider).instance
}
/**
* 获取一个权限, 失败时抛出 [NoSuchElementException]