From 7de048245e8769f50116a4183b6e751f0c355704 Mon Sep 17 00:00:00 2001
From: Him188 <Him188@mamoe.net>
Date: Wed, 19 Feb 2020 20:27:55 +0800
Subject: [PATCH] Fix null packet

---
 .../qqandroid/network/QQAndroidBotNetworkHandler.kt    | 10 +++++-----
 .../qqandroid/network/protocol/packet/PacketFactory.kt | 10 +++++-----
 2 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt
index 848622d27..365f7521d 100644
--- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt
+++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt
@@ -373,7 +373,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
     }
 
     // with generic type, less mistakes
-    private suspend inline fun <P : Packet> generifiedParsePacket(input: Input) {
+    private suspend fun <P : Packet?> generifiedParsePacket(input: Input) {
         KnownPacketFactories.parseIncomingPacket(bot, input) { packetFactory: PacketFactory<P>, packet: P, commandName: String, sequenceId: Int ->
             handlePacket(packetFactory, packet, commandName, sequenceId)
             if (packet is MultiPacket<*>) {
@@ -387,7 +387,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
     /**
      * 处理解析完成的包.
      */
-    suspend fun <P : Packet> handlePacket(packetFactory: PacketFactory<P>?, packet: P, commandName: String, sequenceId: Int) {
+    suspend fun <P : Packet?> handlePacket(packetFactory: PacketFactory<P>?, packet: P, commandName: String, sequenceId: Int) {
         // highest priority: pass to listeners (attached by sendAndExpect).
         packetListeners.forEach { listener ->
             if (listener.filter(commandName, sequenceId) && packetListeners.remove(listener)) {
@@ -396,7 +396,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
         }
 
         // check top-level cancelling
-        if (PacketReceivedEvent(packet).broadcast().isCancelled) {
+        if (packet != null && PacketReceivedEvent(packet).broadcast().isCancelled) {
             return
         }
 
@@ -412,7 +412,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
             if (packet is CancellableEvent && packet.isCancelled) return
         }
 
-        if (bot.logger.isEnabled || logger.isEnabled) {
+        if (packet != null && bot.logger.isEnabled || logger.isEnabled) {
             val logMessage = "Received: ${packet.toString().replace("\n", """\n""").replace("\r", "")}"
 
             if (packet is Event) {
@@ -585,7 +585,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
     internal inner class PacketListener( // callback
         val commandName: String,
         val sequenceId: Int
-    ) : CompletableDeferred<Packet> by CompletableDeferred(supervisor) {
+    ) : CompletableDeferred<Packet?> by CompletableDeferred(supervisor) {
         fun filter(commandName: String, sequenceId: Int) = this.commandName == commandName && this.sequenceId == sequenceId
     }
 
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 b0da3edd0..55b6dae7b 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
@@ -97,10 +97,10 @@ internal abstract class IncomingPacketFactory<TPacket : Packet?>(
 }
 
 @JvmName("decode0")
-private suspend inline fun <P : Packet> OutgoingPacketFactory<P>.decode(bot: QQAndroidBot, packet: ByteReadPacket): P = packet.decode(bot)
+private suspend inline fun <P : Packet?> OutgoingPacketFactory<P>.decode(bot: QQAndroidBot, packet: ByteReadPacket): P = packet.decode(bot)
 
 @JvmName("decode1")
-private suspend inline fun <P : Packet> IncomingPacketFactory<P>.decode(bot: QQAndroidBot, packet: ByteReadPacket, sequenceId: Int): P =
+private suspend inline fun <P : Packet?> IncomingPacketFactory<P>.decode(bot: QQAndroidBot, packet: ByteReadPacket, sequenceId: Int): P =
     packet.decode(bot, sequenceId)
 
 internal val DECRYPTER_16_ZERO = ByteArray(16)
@@ -169,7 +169,7 @@ internal object KnownPacketFactories {
     // do not inline. Exceptions thrown will not be reported correctly
     @UseExperimental(MiraiInternalAPI::class)
     @Suppress("UNCHECKED_CAST")
-    suspend fun <T : Packet> parseIncomingPacket(bot: QQAndroidBot, rawInput: Input, consumer: PacketConsumer<T>) = with(rawInput) {
+    suspend fun <T : Packet?> parseIncomingPacket(bot: QQAndroidBot, rawInput: Input, consumer: PacketConsumer<T>) = with(rawInput) {
         // login
         val flag1 = readInt()
 
@@ -229,7 +229,7 @@ internal object KnownPacketFactories {
     }
 
     @UseExperimental(MiraiInternalAPI::class)
-    internal suspend fun <T : Packet> handleIncomingPacket(it: IncomingPacket<T>, bot: QQAndroidBot, flag2: Int, consumer: PacketConsumer<T>) {
+    internal suspend fun <T : Packet?> handleIncomingPacket(it: IncomingPacket<T>, bot: QQAndroidBot, flag2: Int, consumer: PacketConsumer<T>) {
         if (it.packetFactory == null) {
             bot.network.logger.debug("Received commandName: ${it.commandName}")
             PacketLogger.warning { "找不到 PacketFactory" }
@@ -337,7 +337,7 @@ internal object KnownPacketFactories {
         return IncomingPacket(packetFactory, ssoSequenceId, packet, commandName)
     }
 
-    private suspend fun <T : Packet> ByteReadPacket.parseOicqResponse(
+    private suspend fun <T : Packet?> ByteReadPacket.parseOicqResponse(
         bot: QQAndroidBot,
         packetFactory: OutgoingPacketFactory<T>,
         ssoSequenceId: Int,