From 9a0092ffca52d4fdc99e181dab0b28e365f5e8e9 Mon Sep 17 00:00:00 2001
From: Him188 <Him188@mamoe.net>
Date: Fri, 6 Sep 2019 22:20:16 +0800
Subject: [PATCH] Updated packets

---
 .../events/network/BeforePacketSendEvent.java |  2 +
 .../event/events/network/PacketSentEvent.java | 15 ++++++++
 .../mirai/network/RobotNetworkHandler.kt      | 11 +++++-
 .../mirai/network/packet/ClientPacket.kt      | 38 ++++++++-----------
 4 files changed, 41 insertions(+), 25 deletions(-)
 create mode 100644 mirai-core/src/main/java/net/mamoe/mirai/event/events/network/PacketSentEvent.java

diff --git a/mirai-core/src/main/java/net/mamoe/mirai/event/events/network/BeforePacketSendEvent.java b/mirai-core/src/main/java/net/mamoe/mirai/event/events/network/BeforePacketSendEvent.java
index 2996d08ee..97672e5d8 100644
--- a/mirai-core/src/main/java/net/mamoe/mirai/event/events/network/BeforePacketSendEvent.java
+++ b/mirai-core/src/main/java/net/mamoe/mirai/event/events/network/BeforePacketSendEvent.java
@@ -5,6 +5,8 @@ import net.mamoe.mirai.network.packet.ClientPacket;
 import org.jetbrains.annotations.NotNull;
 
 /**
+ * Packet 已经 {@link ClientPacket#encode()}, 即将被发送
+ *
  * @author Him188moe
  */
 public final class BeforePacketSendEvent extends ClientPacketEvent implements Cancellable {
diff --git a/mirai-core/src/main/java/net/mamoe/mirai/event/events/network/PacketSentEvent.java b/mirai-core/src/main/java/net/mamoe/mirai/event/events/network/PacketSentEvent.java
new file mode 100644
index 000000000..963812029
--- /dev/null
+++ b/mirai-core/src/main/java/net/mamoe/mirai/event/events/network/PacketSentEvent.java
@@ -0,0 +1,15 @@
+package net.mamoe.mirai.event.events.network;
+
+import net.mamoe.mirai.network.packet.ClientPacket;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Packet 已经发出
+ *
+ * @author Him188moe
+ */
+public final class PacketSentEvent extends ClientPacketEvent {
+    public PacketSentEvent(@NotNull ClientPacket packet) {
+        super(packet);
+    }
+}
diff --git a/mirai-core/src/main/java/net/mamoe/mirai/network/RobotNetworkHandler.kt b/mirai-core/src/main/java/net/mamoe/mirai/network/RobotNetworkHandler.kt
index 52f5e056b..bd533f20e 100644
--- a/mirai-core/src/main/java/net/mamoe/mirai/network/RobotNetworkHandler.kt
+++ b/mirai-core/src/main/java/net/mamoe/mirai/network/RobotNetworkHandler.kt
@@ -5,6 +5,8 @@ package net.mamoe.mirai.network
 import net.mamoe.mirai.Robot
 import net.mamoe.mirai.contact.Group
 import net.mamoe.mirai.contact.QQ
+import net.mamoe.mirai.event.events.network.BeforePacketSendEvent
+import net.mamoe.mirai.event.events.network.PacketSentEvent
 import net.mamoe.mirai.event.events.network.ServerPacketReceivedEvent
 import net.mamoe.mirai.event.events.qq.FriendMessageEvent
 import net.mamoe.mirai.event.events.robot.RobotLoginSucceedEvent
@@ -197,12 +199,17 @@ class RobotNetworkHandler(private val robot: Robot) : Closeable {
             checkNotNull(socket) { "socket closed" }
 
             try {
-                packet.encode()
-                packet.writeHex(Protocol.tail)
+                packet.encodePacket()
+
+                if (BeforePacketSendEvent(packet).broadcast().isCancelled) {
+                    return
+                }
 
                 val data = packet.toByteArray()
                 socket!!.send(DatagramPacket(data, data.size))
                 MiraiLogger info "Packet sent: $packet"
+
+                PacketSentEvent(packet).broadcast()
             } catch (e: Throwable) {
                 e.printStackTrace()
             }
diff --git a/mirai-core/src/main/java/net/mamoe/mirai/network/packet/ClientPacket.kt b/mirai-core/src/main/java/net/mamoe/mirai/network/packet/ClientPacket.kt
index e21289fd9..1f4891942 100644
--- a/mirai-core/src/main/java/net/mamoe/mirai/network/packet/ClientPacket.kt
+++ b/mirai-core/src/main/java/net/mamoe/mirai/network/packet/ClientPacket.kt
@@ -14,11 +14,13 @@ import java.security.MessageDigest
 @ExperimentalUnsignedTypes
 abstract class ClientPacket : ByteArrayDataOutputStream(), Packet {
     @Getter
-    val packageId: String
+    val idHex: String
+
+    var encoded: Boolean = false
 
     init {
         val annotation = this.javaClass.getAnnotation(PacketId::class.java)
-        packageId = annotation.value
+        idHex = annotation.value
 
         try {
             this.writeHex(Protocol.head)
@@ -32,7 +34,7 @@ abstract class ClientPacket : ByteArrayDataOutputStream(), Packet {
 
     @Throws(IOException::class)
     fun writePacketId() {
-        this.writeHex(this@ClientPacket.packageId)
+        this.writeHex(this@ClientPacket.idHex)
     }
 
     /**
@@ -42,11 +44,19 @@ abstract class ClientPacket : ByteArrayDataOutputStream(), Packet {
      * Before sending the packet, a [tail][Protocol.tail] will be added.
      */
     @Throws(IOException::class)
-    abstract fun encode()
+    protected abstract fun encode()
+
+    fun encodePacket() {
+        if (encoded) {
+            return
+        }
+        encode()
+        writeHex(Protocol.tail)
+    }
 
     @Throws(IOException::class)
     fun encodeToByteArray(): ByteArray {
-        encode()
+        encodePacket()
         return toByteArray()
     }
 
@@ -88,24 +98,6 @@ fun DataOutputStream.writeHex(hex: String) {
     }
 }
 
-@ExperimentalUnsignedTypes
-fun DataOutputStream.writeVarInt(dec: UInt) {
-    /*.判断开始 (n < 256)
-    返回 (取文本右边 (“0” + 取十六进制文本 (n), 2))
-    .判断 (n ≥ 256)
-    hex = 取文本右边 (“0” + 取十六进制文本 (n), 4)
-    返回 (取文本左边 (hex, 2) + “ ” + 取文本右边 (hex, 2))
-    .默认
-    返回 (“”)
-    .判断结束*/
-
-    when {
-        dec < 256u -> this.writeByte(dec.toByte().toInt())//drop other bits
-        dec > 256u -> this.writeShort(dec.toShort().toInt())
-        else -> throw IllegalArgumentException(dec.toString())
-    }
-}
-
 fun DataOutputStream.encryptAndWrite(byteArray: ByteArray, key: ByteArray) {
     this.write(TEA.encrypt(byteArray, key))
 }