Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redesign notice handling and introduce NoticeProcessorPipeline #1369

Merged
merged 54 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
e0e5b6f
Add contracts to `cast`, `safeCast` and `castOrNull`; Add `uncheckedC…
Him188 Jun 25, 2021
7a34267
Add `out` variance to generic type of `ContactList`
Him188 Jun 26, 2021
46b0fee
Use `NormalMemberImpl` for properties of `GroupImpl`
Him188 Jun 26, 2021
7fc58e3
Use internal types in implementations of properties in `Bot`
Him188 Jun 26, 2021
980e2c8
Add `TypeSafeMap`
Him188 Jun 26, 2021
1cd421b
Redesign notice handling and introduce `NoticeProcessorPipeline`
Him188 Jun 26, 2021
35b835c
Fix incorrect coroutine scope created for contacts in `IMirai.newFrie…
Him188 Jun 27, 2021
c170919
Redesign notice handling and introduce `NoticeProcessorPipeline` part 2
Him188 Jun 27, 2021
ea2bb3a
Add `UnconsumedNoticesAlerter` and cleanup
Him188 Jun 27, 2021
22cc78e
Improve performance
Him188 Jun 27, 2021
0160be3
Remove `Group.newAnonymousMember`
Him188 Jun 28, 2021
8c171ad
Fix `ByteArray.loadAs`
Him188 Jun 28, 2021
0859b07
Redesign notice handling and introduce `NoticeProcessorPipeline` part 3:
Him188 Jun 28, 2021
eacb15c
Extract utils into multiple files
Him188 Jun 29, 2021
479f2b6
apiDump
Him188 Jun 29, 2021
c6de8c6
Delete empty ProtoBuf.kt
Him188 Jul 3, 2021
492257f
Move `bkn` from `QQAndroidBot` to `WLoginSigInfo`
Him188 Aug 13, 2021
6aef4da
Remove outdated todo in `FriendNoticeProcessor`
Him188 Aug 13, 2021
ab5f2e5
Add `MutableProcessResult`; remove locks
Him188 Aug 13, 2021
af2e71b
Migrate more transformers to `NoticeProcessorPipeline`
Him188 Aug 13, 2021
bee764b
Migrate more transformers to `NoticeProcessorPipeline`
Him188 Aug 14, 2021
a831eb7
Ensure all items in `MultiPacket` are visited
Him188 Aug 14, 2021
82118cb
Remove `.debug` in `mirai.network.debug.notice.pipeline.log.full`
Him188 Aug 16, 2021
de48274
Avoid boxing in `TypeSafeMap` and support serialization
Him188 Aug 16, 2021
cf3de70
Add simple constructor for `TypeSafeMap`
Him188 Aug 16, 2021
d01407d
Add `TraceLoggingNoticeProcessor`
Him188 Aug 16, 2021
d511611
Make `UnconsumedNoticesAlerter` disabled by default
Him188 Aug 16, 2021
4adaa13
Fix `EventBroadcasterPacketHandler.impl`
Him188 Aug 16, 2021
8852f50
Add `AbstractPipelineContext`
Him188 Aug 16, 2021
e1837bd
Add `RecordingNoticeProcessor`
Him188 Aug 16, 2021
54c9c73
Add gradle configuration "use.maven.local"
Him188 Aug 16, 2021
f892b65
Fix `ParseErrorPacket` not logged
Him188 Aug 16, 2021
665467b
Add `toString` for TypeSafeMap implementations
Him188 Aug 16, 2021
c50ac7d
Make `TypeSafeMap()` returns `TypeSafeMap.EMPTY`
Him188 Aug 25, 2021
e433d6b
Add util `readResource`
Him188 Aug 25, 2021
84ad2da
Add util `CollectionDiff`
Him188 Aug 25, 2021
e79a907
Add util `Array<Byte>.toUHexString`
Him188 Aug 25, 2021
a1c0722
Add ConstructorCallCodegen
Him188 Aug 25, 2021
b7a54d9
Redesign packet recording
Him188 Aug 25, 2021
75d3883
Fix group transfer and retrieve, add tests for notice events:
Him188 Aug 25, 2021
70f2b04
Use yamlkt 0.10.2
Him188 Aug 25, 2021
b0911f7
Fix `MemberJoinEvent` processing and add `MemberJoinTest`
Him188 Aug 27, 2021
0c8c576
Add test for message events
Him188 Aug 27, 2021
d4a02d6
Fix `RecordingNoticeProcessorTest`
Him188 Aug 27, 2021
324e15a
Get group by uin in `OnlineMessageSourceFromTempImpl`, fix the proble…
Him188 Aug 27, 2021
ff608b6
Add comments for kick event
Him188 Aug 27, 2021
003bbd4
Handle possible `MemberLeaveEvent.Kick` for `NewContact.SystemMsgNewG…
Him188 Aug 27, 2021
fb6335d
Ignore `NewContact.SystemMsgNewGroup, subType=0, groupMsgType=8`, fix…
Him188 Aug 27, 2021
5283843
Ignore `NewContact.SystemMsgNewGroup, subType=5, groupMsgType=16`, fi…
Him188 Aug 27, 2021
c7ff883
Support nested structures in desensitizer
Him188 Aug 27, 2021
7565d03
Remove more verbose default values in ConstructorCallCodegen
Him188 Aug 27, 2021
12228d7
Add `BotInvitedJoinTest`
Him188 Aug 27, 2021
4bb751d
Use `getGroupByUinOrCodeOrFail` in OnlineMessageSourceFromTempImpl fo…
Him188 Aug 27, 2021
2cc101e
Fix FriendAvatarChangedEvent
Him188 Aug 30, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,5 @@ bintray.key.txt
/build-gpg-sign
# Name for IDEA direction sorting
build-secret-keys/

**/local.*
21 changes: 21 additions & 0 deletions .run/RunRecorderKt.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!--
~ Copyright 2019-2021 Mamoe Technologies and contributors.
~
~ 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
~ Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
~
~ https://github.com/mamoe/mirai/blob/dev/LICENSE
-->

<component name="ProjectRunConfigurationManager">
<configuration default="false" name="RunRecorderKt" type="JetRunConfigurationType" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="net.mamoe.mirai.internal.bootstrap.RunRecorderKt"/>
<module name="mirai.mirai-core.jvmTest"/>
<option name="VM_PARAMETERS"
value="-Dmirai.debug.network.state.observer.logging=true -Dmirai.debug.network.show.all.components=true -Dkotlinx.coroutines.debug=on -Dmirai.debug.network.show.packet.details=true"/>
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/test"/>
<method v="2">
<option name="Make" enabled="true"/>
</method>
</configuration>
</component>
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public final class net/mamoe/mirai/contact/ContactKt {
}

public final class net/mamoe/mirai/contact/ContactList : java/util/Collection, kotlin/jvm/internal/markers/KMappedMarker {
public final field delegate Ljava/util/concurrent/ConcurrentLinkedQueue;
public final field delegate Ljava/util/Collection;
public synthetic fun add (Ljava/lang/Object;)Z
public fun add (Lnet/mamoe/mirai/contact/Contact;)Z
public fun addAll (Ljava/util/Collection;)Z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public final class net/mamoe/mirai/contact/ContactKt {
}

public final class net/mamoe/mirai/contact/ContactList : java/util/Collection, kotlin/jvm/internal/markers/KMappedMarker {
public final field delegate Ljava/util/concurrent/ConcurrentLinkedQueue;
public final field delegate Ljava/util/Collection;
public synthetic fun add (Ljava/lang/Object;)Z
public fun add (Lnet/mamoe/mirai/contact/Contact;)Z
public fun addAll (Ljava/util/Collection;)Z
Expand Down
12 changes: 8 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType

buildscript {
repositories {
// mavenLocal()
// maven(url = "https://mirrors.huaweicloud.com/repository/maven")
if (System.getProperty("use.maven.local") == "true") {
mavenLocal()
}

mavenCentral()
gradlePluginPortal()
google()
Expand Down Expand Up @@ -76,8 +78,10 @@ allprojects {
version = Versions.project

repositories {
// mavenLocal() // cheching issue cause compiler exception
// maven(url = "https://mirrors.huaweicloud.com/repository/maven")
if (System.getProperty("use.maven.local") == "true") {
mavenLocal()
}

mavenCentral()
gradlePluginPortal()
google()
Expand Down
6 changes: 4 additions & 2 deletions buildSrc/src/main/kotlin/ProjectConfigure.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import org.gradle.api.JavaVersion
import org.gradle.api.Project
import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.api.tasks.bundling.Jar
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.api.tasks.testing.Test
import org.gradle.api.tasks.bundling.Jar
import org.gradle.kotlin.dsl.*
import org.jetbrains.kotlin.gradle.dsl.*
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
Expand Down Expand Up @@ -144,7 +144,9 @@ val experimentalAnnotations = arrayOf(
"net.mamoe.mirai.message.data.ExperimentalMessageKey",
"net.mamoe.mirai.console.ConsoleFrontEndImplementation",
"net.mamoe.mirai.console.util.ConsoleInternalApi",
"net.mamoe.mirai.console.util.ConsoleExperimentalApi"
"net.mamoe.mirai.console.util.ConsoleExperimentalApi",

"kotlinx.io.core.internal.DangerousInternalIoApi",
)

fun Project.configureKotlinExperimentalUsages() {
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ object Versions {

// If you the versions below, you need to sync changes to mirai-console/buildSrc/src/main/kotlin/Versions.kt

const val yamlkt = "0.10.0"
const val yamlkt = "0.10.2"
const val intellijGradlePlugin = "1.1"
const val kotlinIntellijPlugin = "211-1.5.20-release-284-IJ7442.40" // keep to newest as kotlinCompiler
const val intellij = "2021.1.3" // don't update easily unless you want your disk space -= 500MB
Expand Down
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ systemProp.org.gradle.internal.publish.checksums.insecure=true
gnsp.disableApplyOnlyOnRootProjectEnforcement=true
# We may target 15 with Kotlin 1.5 IR
mirai.android.target.api.level=24

systemProp.use.maven.local=false
6 changes: 2 additions & 4 deletions mirai-core-api/src/commonMain/kotlin/contact/ContactList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ import java.util.concurrent.ConcurrentLinkedQueue
* @see ContactList.asSequence
*/
@Suppress("unused")
public class ContactList<C : Contact>
@MiraiInternalApi public constructor(@JvmField @MiraiInternalApi public val delegate: ConcurrentLinkedQueue<C>) :
public class ContactList<out C : Contact>
@MiraiInternalApi public constructor(@JvmField @MiraiInternalApi public val delegate: MutableCollection<@UnsafeVariance C>) :
Collection<C> by delegate {
@MiraiInternalApi
public constructor(collection: Collection<C>) : this(ConcurrentLinkedQueue(collection))

@MiraiInternalApi
public constructor() : this(ConcurrentLinkedQueue())
Expand Down
30 changes: 15 additions & 15 deletions mirai-core-api/src/commonMain/kotlin/event/events/friend.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,34 @@ import java.util.concurrent.atomic.AtomicBoolean
/**
* 好友昵称改变事件. 目前仅支持解析 (来自 PC 端的修改).
*/
public data class FriendRemarkChangeEvent internal constructor(
public data class FriendRemarkChangeEvent @MiraiInternalApi public constructor(
public override val friend: Friend,
public val oldRemark: String,
public val newRemark: String
public val newRemark: String,
) : FriendEvent, Packet, AbstractEvent(), FriendInfoChangeEvent

/**
* 成功添加了一个新好友的事件
*/
public data class FriendAddEvent @MiraiInternalApi constructor(
public data class FriendAddEvent @MiraiInternalApi public constructor(
/**
* 新好友. 已经添加到 [Bot.friends]
*/
public override val friend: Friend
public override val friend: Friend,
) : FriendEvent, Packet, AbstractEvent(), FriendInfoChangeEvent

/**
* 好友已被删除或主动删除的事件.
*/
public data class FriendDeleteEvent internal constructor(
public override val friend: Friend
public data class FriendDeleteEvent @MiraiInternalApi public constructor(
public override val friend: Friend,
) : FriendEvent, Packet, AbstractEvent(), FriendInfoChangeEvent

/**
* 一个账号请求添加机器人为好友的事件
*/
@Suppress("DEPRECATION")
public data class NewFriendRequestEvent internal constructor(
public data class NewFriendRequestEvent @MiraiInternalApi public constructor(
public override val bot: Bot,
/**
* 事件唯一识别号
Expand All @@ -76,7 +76,7 @@ public data class NewFriendRequestEvent internal constructor(
/**
* 群名片或好友昵称
*/
public val fromNick: String
public val fromNick: String,
) : BotEvent, Packet, AbstractEvent(), FriendInfoChangeEvent {
@JvmField
internal val responded: AtomicBoolean = AtomicBoolean(false)
Expand All @@ -97,25 +97,25 @@ public data class NewFriendRequestEvent internal constructor(
/**
* [Friend] 头像被修改. 在此事件广播前就已经修改完毕.
*/
public data class FriendAvatarChangedEvent internal constructor(
public override val friend: Friend
public data class FriendAvatarChangedEvent @MiraiInternalApi public constructor(
public override val friend: Friend,
) : FriendEvent, Packet, AbstractEvent()

/**
* [Friend] 昵称改变事件, 在此事件广播时好友已经完成改名
* @see BotNickChangedEvent
*/
public data class FriendNickChangedEvent internal constructor(
public data class FriendNickChangedEvent @MiraiInternalApi public constructor(
public override val friend: Friend,
public val from: String,
public val to: String
public val to: String,
) : FriendEvent, Packet, AbstractEvent(), FriendInfoChangeEvent

/**
* 好友输入状态改变的事件,当开始输入文字、退出聊天窗口或清空输入框时会触发此事件
*/
public data class FriendInputStatusChangedEvent internal constructor(
public data class FriendInputStatusChangedEvent @MiraiInternalApi public constructor(
public override val friend: Friend,
public val inputting: Boolean
public val inputting: Boolean,

) : FriendEvent, Packet, AbstractEvent()
) : FriendEvent, Packet, AbstractEvent()
3 changes: 2 additions & 1 deletion mirai-core-api/src/commonMain/kotlin/event/events/group.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@file:JvmMultifileClass
@file:JvmName("BotEventsKt")
@file:Suppress(
"unused", "FunctionName", "INVISIBLE_REFERENCE", "INVISIBLE_MEMBER", "DEPRECATION_ERROR",
"FunctionName", "INVISIBLE_REFERENCE", "INVISIBLE_MEMBER", "DEPRECATION_ERROR",
"MemberVisibilityCanBePrivate"
)

Expand Down Expand Up @@ -426,6 +426,7 @@ public data class MemberJoinRequestEvent @MiraiInternalApi constructor(
)

internal companion object {
@Suppress("unused")
@Deprecated("For binary compatibility", level = DeprecationLevel.HIDDEN)
@JvmStatic
@JvmName("copy\$default") // avoid being mangled
Expand Down
16 changes: 7 additions & 9 deletions mirai-core-api/src/commonMain/kotlin/event/events/stranger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ import net.mamoe.mirai.contact.Friend
import net.mamoe.mirai.contact.Stranger
import net.mamoe.mirai.event.AbstractEvent
import net.mamoe.mirai.internal.network.Packet
import net.mamoe.mirai.utils.MiraiInternalApi

/**
* 新增陌生人的事件
*
*/
public data class StrangerAddEvent internal constructor(
public data class StrangerAddEvent @MiraiInternalApi public constructor(
/**
* 新的陌生人. 已经添加到 [Bot.strangers]
*/
public override val stranger: Stranger
public override val stranger: Stranger,
) : StrangerEvent, Packet, AbstractEvent()


Expand All @@ -32,19 +33,16 @@ public data class StrangerAddEvent internal constructor(
*
*/
public sealed class StrangerRelationChangeEvent(
public override val stranger: Stranger
public override val stranger: Stranger,
) : StrangerEvent, Packet, AbstractEvent() {
/**
* 主动删除陌生人或陌生人被删除的事件
*
* 除主动删除外,此事件为惰性广播,无法确保实时性
* 目前被动删除仅会在陌生人二次添加时才会进行广播
* 主动删除陌生人或陌生人被删除的事件, 不一定能接收到被动删除的事件
*/
public class Deleted(
/**
* 被删除的陌生人
*/
stranger: Stranger
stranger: Stranger,
) : StrangerRelationChangeEvent(stranger)

/**
Expand All @@ -62,7 +60,7 @@ public sealed class StrangerRelationChangeEvent(
*
* 已经添加到Bot的好友列表中
*/
public val friend: Friend
public val friend: Friend,
) : StrangerRelationChangeEvent(stranger)

}
6 changes: 6 additions & 0 deletions mirai-core-utils/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ kotlin {
}
}

val commonTest by getting {
dependencies {
api(yamlkt)
}
}

if (isAndroidSDKAvailable) {
val androidMain by getting {
//
Expand Down
27 changes: 27 additions & 0 deletions mirai-core-utils/src/commonMain/kotlin/Bytes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,33 @@ public fun ByteArray.checkOffsetAndLength(offset: Int, length: Int) {
require(offset + length <= this.size) { "offset ($offset) + length ($length) > array.size (${this.size})" }
}

@JvmOverloads
@Suppress("DuplicatedCode") // false positive. foreach is not common to UByteArray and ByteArray
public fun Array<Byte>.toUHexString(
separator: String = " ",
offset: Int = 0,
length: Int = this.size - offset
): String {
require(offset >= 0) { "offset shouldn't be negative: $offset" }
require(length >= 0) { "length shouldn't be negative: $length" }
require(offset + length <= this.size) { "offset ($offset) + length ($length) > array.size (${this.size})" }

if (length == 0) {
return ""
}
val lastIndex = offset + length
return buildString(length * 2) {
[email protected] { index, it ->
if (index in offset until lastIndex) {
var ret = it.toUByte().toString(16).uppercase()
if (ret.length == 1) ret = "0$ret"
append(ret)
if (index < lastIndex - 1) append(separator)
}
}
}
}


@JvmOverloads
@Suppress("DuplicatedCode") // false positive. foreach is not common to UByteArray and ByteArray
Expand Down
24 changes: 24 additions & 0 deletions mirai-core-utils/src/commonMain/kotlin/CollectionDiff.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/

package net.mamoe.mirai.utils

public class CollectionDiff<E> {
private var save: Collection<E> = listOf()

public fun save(collection: Collection<E>) {
save = collection.toList()
}

public fun subtract(collection: Collection<E>): Collection<E> = collection subtract save

public fun subtractAndSave(collection: Collection<E>): Collection<E> {
return subtract(collection).also { save(collection) }
}
}
3 changes: 2 additions & 1 deletion mirai-core-utils/src/commonMain/kotlin/Numbers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@

package net.mamoe.mirai.utils

public fun Int.toLongUnsigned(): Long = this.toLong().and(0xFFFF_FFFF)
public fun Int.toLongUnsigned(): Long = this.toLong().and(0xFFFF_FFFF)
public fun Short.toIntUnsigned(): Int = this.toUShort().toInt()
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/

package net.mamoe.mirai.internal.utils.io
package net.mamoe.mirai.utils

internal interface JceStruct

@TestOnly
public fun readResource(url: String): String =
Thread.currentThread().contextClassLoader.getResourceAsStream(url)?.readBytes()?.decodeToString()
?: error("Could not find resource '$url'")
Loading