From 4d09010cf04a302922f21623a9a456a359b3ce6a Mon Sep 17 00:00:00 2001 From: Him188 Date: Thu, 10 Oct 2019 11:28:25 +0800 Subject: [PATCH] Cleanup --- .../src/jvmMain/kotlin/net/mamoe/mirai/Bot.kt | 16 +++++++--------- .../jvmMain/kotlin/net/mamoe/mirai/BotHelper.kt | 2 +- .../kotlin/net/mamoe/mirai/contact/Contact.kt | 4 ++-- .../protocol/tim/packet/login/ClientLogin.kt | 3 +++ .../ServerLoginResponseKeyExchangePacket.kt | 6 +++--- .../kotlin/net/mamoe/mirai/utils/BotAccount.kt | 2 +- 6 files changed, 17 insertions(+), 16 deletions(-) diff --git a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/Bot.kt b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/Bot.kt index 9d5c2049b..5c1167972 100644 --- a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/Bot.kt +++ b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/Bot.kt @@ -8,9 +8,6 @@ import net.mamoe.mirai.network.protocol.tim.TIMBotNetworkHandler import net.mamoe.mirai.utils.BotAccount import net.mamoe.mirai.utils.ContactList import net.mamoe.mirai.utils.MiraiLogger -import java.io.Closeable -import java.util.* -import java.util.concurrent.atomic.AtomicInteger /** * Mirai 的机器人. 一个机器人实例登录一个 QQ 账号. @@ -40,9 +37,8 @@ import java.util.concurrent.atomic.AtomicInteger * @author NatrualHG * @see net.mamoe.mirai.contact.Contact */ -class Bot(val account: BotAccount, val logger: MiraiLogger) : Closeable { - - val id = createdBotsCount.getAndAdd(1) +class Bot(val account: BotAccount, val logger: MiraiLogger) { + val id = nextId() val contacts = ContactSystem() @@ -88,7 +84,7 @@ class Bot(val account: BotAccount, val logger: MiraiLogger) : Closeable { } } - override fun close() { + fun close() { this.network.close() this.contacts.groups.values.forEach { it.close() } this.contacts.groups.clear() @@ -96,8 +92,10 @@ class Bot(val account: BotAccount, val logger: MiraiLogger) : Closeable { } companion object { - val instances: MutableList = Collections.synchronizedList(LinkedList()) + val instances: MutableList = mutableListOf() - private val createdBotsCount = AtomicInteger(0) + private var id = 0 + private val idLock = Any() + fun nextId(): Int = synchronized(idLock) { id++ } } } \ No newline at end of file diff --git a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/BotHelper.kt b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/BotHelper.kt index 2a6c7f933..1a53542b1 100644 --- a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/BotHelper.kt +++ b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/BotHelper.kt @@ -58,7 +58,7 @@ fun Bot.green(o: Any?) = print(this, o.toString(), LoggerTextFormat.GREEN) fun Bot.debug(o: Any?) = print(this, o.toString(), LoggerTextFormat.YELLOW) -fun Bot.debugPacket(packet: ServerPacket) { +fun Bot.printPacketDebugging(packet: ServerPacket) { debug("Packet=$packet") debug("Packet size=" + packet.input.goto(0).readAllBytes().size) debug("Packet data=" + packet.input.goto(0).readAllBytes().toUHexString()) diff --git a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/contact/Contact.kt b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/contact/Contact.kt index 35d936558..c12f7c34d 100644 --- a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/contact/Contact.kt +++ b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/contact/Contact.kt @@ -26,8 +26,8 @@ abstract class Contact internal constructor(val bot: Bot, val number: Long) { return sendMessage(message.toChain()) } - suspend fun sendMessage(message: String) { - this.sendMessage(PlainText(message)) + suspend fun sendMessage(plain: String) { + this.sendMessage(PlainText(plain)) } suspend fun sendMessage(message: List) { diff --git a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/network/protocol/tim/packet/login/ClientLogin.kt b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/network/protocol/tim/packet/login/ClientLogin.kt index d6b12e67d..e5af9acb4 100644 --- a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/network/protocol/tim/packet/login/ClientLogin.kt +++ b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/network/protocol/tim/packet/login/ClientLogin.kt @@ -42,6 +42,9 @@ class ClientPasswordSubmissionPacket( } } +//实际上这些包性质都是一样的. 31 04 仅是一个序列 id, 可随机 +//但为简化处理, 特固定这个 id + @PacketId("08 36 31 04") class ClientLoginResendPacket3104(qq: Long, password: String, loginTime: Int, loginIP: String, privateKey: ByteArray, token0825: ByteArray, token00BA: ByteArray, tlv0006: ByteArray? = null) : ClientLoginResendPacket(qq, password, loginTime, loginIP, privateKey, token0825, token00BA, tlv0006) diff --git a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/network/protocol/tim/packet/login/ServerLoginResponseKeyExchangePacket.kt b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/network/protocol/tim/packet/login/ServerLoginResponseKeyExchangePacket.kt index 79f154115..273978e7d 100644 --- a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/network/protocol/tim/packet/login/ServerLoginResponseKeyExchangePacket.kt +++ b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/network/protocol/tim/packet/login/ServerLoginResponseKeyExchangePacket.kt @@ -19,14 +19,14 @@ class ServerLoginResponseKeyExchangePacket(input: DataInputStream, val flag: Fla OTHER, } - lateinit var tlv0006: ByteArray;//120bytes + lateinit var tlv0006: ByteArray//120bytes var tokenUnknown: ByteArray? = null - lateinit var privateKey: ByteArray//16bytes + lateinit var privateKeyUpdate: ByteArray//16bytes @Tested override fun decode() { this.input.skip(5) - privateKey = this.input.readNBytes(16)//22 + privateKeyUpdate = this.input.readNBytes(16)//22 //this.input.skip(2)//25 this.input.goto(25) tlv0006 = this.input.readNBytes(120) diff --git a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/BotAccount.kt b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/BotAccount.kt index 535765829..7c59fdb27 100644 --- a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/BotAccount.kt +++ b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/BotAccount.kt @@ -5,5 +5,5 @@ package net.mamoe.mirai.utils */ data class BotAccount( val qqNumber: Long,//实际上是 UInt - val password: String + val password: String//todo 不保存 password? ) \ No newline at end of file