diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/PluginDataExtensions.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/PluginDataExtensions.kt index 57437271c..3a610c6f5 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/PluginDataExtensions.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/PluginDataExtensions.kt @@ -183,7 +183,7 @@ public object PluginDataExtensions { private fun createDelegateInstance( origin: SerializerAwareValue>, - defaultValueComputer: (K) -> V + defaultValueComputer: (K) -> V, ): MutableMap { return object : MutableMap, AbstractMap() { override val entries: MutableSet> get() = origin.value.entries diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/Value.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/Value.kt index c5ccbcd3b..95ff1850a 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/Value.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/Value.kt @@ -58,6 +58,26 @@ public class SerializableValue( ) : Value by delegate, SerializerAwareValue { public override fun toString(): String = delegate.toString() + public override fun equals(other: Any?): Boolean { + if (other === this) return true + if (other?.javaClass != this.javaClass) return false + + @Suppress("UNCHECKED_CAST") + other as SerializableValue + if (other.delegate != this.delegate) return false + // if (other.serializer != this.serializer) return false + // TODO: 2020/9/9 serializers should be checked here, but it will cause incomparable issue when putting a SerializableValue as a Key + return true + } + + override fun hashCode(): Int { + @Suppress("UnnecessaryVariable", "CanBeVal") + var result = delegate.hashCode() + // result = 31 * result + serializer.hashCode() + // TODO: 2020/9/9 serializers should be checked here, but it will cause incomparable issue when putting a SerializableValue as a Key + return result + } + public companion object { @JvmStatic @JvmName("create") diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/_PluginData.value.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/_PluginData.value.kt index 8dad18e32..7e045e30e 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/_PluginData.value.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/_PluginData.value.kt @@ -196,4 +196,21 @@ internal class LazyReferenceValueImpl : ReferenceValue, AbstractValueImpl< initialied = true valueField = value } + + override fun toString(): String { + return valueField.toString() + } + + override fun equals(other: Any?): Boolean { + if (other === this) return true + if (other?.javaClass != this.javaClass) return false + + other as LazyReferenceValueImpl<*> + if (other.valueField != valueField) return false + return true + } + + override fun hashCode(): Int { + return valueField?.hashCode() ?: 0 + } } \ No newline at end of file diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/Permission.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/Permission.kt index ef1e8a212..79ca00b77 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/Permission.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/Permission.kt @@ -17,7 +17,9 @@ import net.mamoe.mirai.console.util.ConsoleExperimentalAPI /** * 一个权限节点. * - * 由 [PermissionService] 实现不同, [Permission] 可能会有多种实例. 但一个权限总是拥有确定的 [id] + * 由 [PermissionService] 实现不同, [Permission] 可能会有多种实例. 但一个权限总是拥有确定的 [id]. + * + * 请不要手动实现这个接口. 总是从 [PermissionService.register] 获得实例. */ @ExperimentalPermission public interface Permission { @@ -26,15 +28,20 @@ public interface Permission { public val parentId: PermissionId } +@OptIn(ExperimentalPermission::class) +private val ROOT_PERMISSION_ID = PermissionId("*", "*") + /** * 所有权限的父权限. */ +@get:JvmName("getRootPermission") @ExperimentalPermission -public object RootPermission : - Permission { - override val id: PermissionId = PermissionId("*", "*") - override val description: String get() = "The parent of any permission" - override val parentId: PermissionId get() = id +public val RootPermission: Permission by lazy { + PermissionService.INSTANCE.register( + ROOT_PERMISSION_ID, + "The parent of any permission", + ROOT_PERMISSION_ID + ) } @ConsoleExperimentalAPI diff --git a/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/command/TestCommand.kt b/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/command/TestCommand.kt index 3a44fb66a..3db1b0a52 100644 --- a/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/command/TestCommand.kt +++ b/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/command/TestCommand.kt @@ -189,7 +189,7 @@ internal class TestCommand { val composite = object : CompositeCommand( ConsoleCommandOwner, - "test", + "test22", overrideContext = buildCommandArgumentContext { add(object : CommandArgumentParser { override fun parse(raw: String, sender: CommandSender): MyClass { @@ -234,7 +234,7 @@ internal class TestCommand { simple.withRegistration { // assertEquals("xxx", withTesting { simple.execute(sender, "xxx") }) - assertEquals("xxx", withTesting { println(sender.executeCommand("/test xxx")) }) + assertEquals("xxx", withTesting { assertSuccess(sender.executeCommand("/test xxx")) }) } } } diff --git a/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/permission/PermissionsBasicsTest.kt b/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/permission/PermissionsBasicsTest.kt new file mode 100644 index 000000000..f60845990 --- /dev/null +++ b/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/permission/PermissionsBasicsTest.kt @@ -0,0 +1,11 @@ +package net.mamoe.mirai.console.permission + +import org.junit.jupiter.api.Test + +internal class PermissionsBasicsTest { + + @Test + fun parentsWithSelfSequence() { + + } +} \ No newline at end of file