diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/ContactImpl.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/ContactImpl.kt index bd6b5750c..e5e7bd4a4 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/ContactImpl.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/ContactImpl.kt @@ -44,9 +44,6 @@ internal class QQImpl(bot: QQAndroidBot, override val coroutineContext: Coroutin TODO("not implemented") } - override val isOnline: Boolean - get() = true - override suspend fun queryProfile(): Profile { TODO("not implemented") } diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/PacketFactory.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/PacketFactory.kt index b5c5f7b3c..f44f87ee6 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/PacketFactory.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/PacketFactory.kt @@ -169,7 +169,7 @@ internal object KnownPacketFactories { PacketLogger.verbose("包类型(flag2) = $flag2. (可能是 ${if (flag2 == 2) "OicqRequest" else "Uni"})") val flag3 = readByte().toInt() - check(flag3 == 0) { "Illegal flag3. Expected 0, whereas got $flag3. flag1=$flag1, flag2=$flag2" } + check(flag3 == 0) { "Illegal flag3. Expected 0, whereas got $flag3. flag1=$flag1, flag2=$flag2. Remaining=${this.readBytes().toUHexString()}" } readString(readInt() - 4)// uinAccount diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/list/FriendListPacket.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/list/FriendListPacket.kt index 677bd3b2b..60da217db 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/list/FriendListPacket.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/list/FriendListPacket.kt @@ -14,7 +14,6 @@ import net.mamoe.mirai.qqandroid.network.protocol.packet.EMPTY_BYTE_ARRAY import net.mamoe.mirai.qqandroid.network.protocol.packet.OutgoingPacket import net.mamoe.mirai.qqandroid.network.protocol.packet.OutgoingPacketFactory import net.mamoe.mirai.qqandroid.network.protocol.packet.buildOutgoingUniPacket -import net.mamoe.mirai.utils.io.debugIfFail internal class FriendList { @@ -22,7 +21,7 @@ internal class FriendList { internal object GetTroopMemberList : OutgoingPacketFactory("friendlist.GetTroopMemberListReq") { override suspend fun ByteReadPacket.decode(bot: QQAndroidBot): Response { - val res = this.debugIfFail { this.decodeUniPacket(GetTroopMemberListResp.serializer()) } + val res = this.decodeUniPacket(GetTroopMemberListResp.serializer()) return Response( res.vecTroopMember, res.nextUin diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/ContactList.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/ContactList.kt index 3e96f57eb..ae59a5f2c 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/ContactList.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/ContactList.kt @@ -2,9 +2,7 @@ package net.mamoe.mirai.contact -import net.mamoe.mirai.utils.LockFreeLinkedList -import net.mamoe.mirai.utils.MiraiInternalAPI -import net.mamoe.mirai.utils.joinToString +import net.mamoe.mirai.utils.* /** @@ -24,10 +22,12 @@ class ContactList(@MiraiInternalAPI val delegate: LockFreeLinkedLis operator fun get(id: Long): C = delegate[id] fun getOrNull(id: Long): C? = delegate.getOrNull(id) - fun containsId(id: Long): Boolean = delegate.getOrNull(id) != null + @Deprecated("Use contains instead", ReplaceWith("contains(id)")) + fun containsId(id: Long): Boolean = contains(id) val size: Int get() = delegate.size operator fun contains(element: C): Boolean = delegate.contains(element) + operator fun contains(id: Long): Boolean = delegate.getOrNull(id) != null fun containsAll(elements: Collection): Boolean = elements.all { contains(it) } fun isEmpty(): Boolean = delegate.isEmpty() inline fun forEach(block: (C) -> Unit) = delegate.forEach(block) @@ -50,5 +50,35 @@ inline fun LockFreeLinkedList.filteringGetOrNull(filter: (C) -> return null } -fun LockFreeLinkedList.getOrAdd(id: Long, supplier: () -> C): C = - filteringGetOrAdd({ it.id == id }, supplier) + +/** + * Collect all the elements into a [MutableList] then cast it as a [List] + */ +fun ContactList.toList(): List = toMutableList() + +/** + * Collect all the elements into a [MutableList]. + */ +@UseExperimental(MiraiInternalAPI::class) +fun ContactList.toMutableList(): MutableList = this.delegate.toMutableList() + +/** + * Collect all the elements into a [MutableSet] then cast it as a [Set] + */ +fun ContactList.toSet(): Set = toMutableSet() + +/** + * Collect all the elements into a [MutableSet]. + */ +@UseExperimental(MiraiInternalAPI::class) +fun ContactList.toMutableSet(): MutableSet = this.delegate.toMutableSet() + +/** + * Builds a [Sequence] containing all the elements in [this] in the same order. + * + * Note that the sequence is dynamic, that is, elements are yielded atomically only when it is required + */ +@UseExperimental(MiraiInternalAPI::class) +fun ContactList.asSequence(): Sequence { + return this.delegate.asSequence() +} \ No newline at end of file diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/QQ.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/QQ.kt index 8bfd19824..fe380d5f8 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/QQ.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/QQ.kt @@ -22,9 +22,9 @@ import net.mamoe.mirai.data.Profile */ interface QQ : Contact, CoroutineScope { /** - * 是否在线. 这个属性的值将会与服务器同步更新. + * QQ 号码 */ - val isOnline: Boolean + override val id: Long /** * 请求头像下载链接 diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/LockFreeLinkedList.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/LockFreeLinkedList.kt index 80b6c29cc..711099469 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/LockFreeLinkedList.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/LockFreeLinkedList.kt @@ -19,12 +19,12 @@ inline fun LockFreeLinkedList.joinToString( }.dropLast(separator.length) + postfix /** - * Returns a [List] containing all the elements in [this] in the same order + * Collect all the elements into a [MutableList] then cast it as a [List] */ fun LockFreeLinkedList.toList(): List = toMutableList() /** - * Returns a [MutableList] containing all the elements in [this] in the same order + * Collect all the elements into a [MutableList]. */ fun LockFreeLinkedList.toMutableList(): MutableList { val list = mutableListOf() @@ -32,6 +32,33 @@ fun LockFreeLinkedList.toMutableList(): MutableList { return list } +/** + * Collect all the elements into a [MutableSet] then cast it as a [Set] + */ +fun LockFreeLinkedList.toSet(): Set = toMutableSet() + +/** + * Collect all the elements into a [MutableSet]. + */ +fun LockFreeLinkedList.toMutableSet(): MutableSet { + val list = mutableSetOf() + this.forEach { list.add(it) } + return list +} + +/** + * Builds a [Sequence] containing all the elements in [this] in the same order. + * + * Note that the sequence is dynamic, that is, elements are yielded atomically only when it is required + */ +fun LockFreeLinkedList.asSequence(): Sequence { + return sequence { + forEach { + yield(it) + } + } +} + /** * Implementation of lock-free LinkedList. * @@ -87,6 +114,9 @@ open class LockFreeLinkedList { open operator fun plusAssign(element: E) = this.addLast(element) + /** + * 过滤并获取, 获取不到则添加一个元素. + */ inline fun filteringGetOrAdd(filter: (E) -> Boolean, noinline supplier: () -> E): E { val node = LazyNode(tail, supplier) @@ -149,6 +179,9 @@ open class LockFreeLinkedList { } } + /** + * 动态计算的大小 + */ val size: Int get() = head.countChildIterate>({ it.nextNode }, { it !is Tail }) - 1 // empty head is always included open operator fun contains(element: E): Boolean {