Skip to content

Commit

Permalink
Prohibit premature usages of PermissionService
Browse files Browse the repository at this point in the history
  • Loading branch information
Him188 committed Feb 24, 2022
1 parent 044457a commit 77e2a5c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 77e2a5c

Please sign in to comment.