Commit c15651b9 authored by Him188's avatar Him188

Adjust property name and visibility

parent a42fbe47
...@@ -93,8 +93,11 @@ kotlin { ...@@ -93,8 +93,11 @@ kotlin {
} }
sourceSets["jvmTest"].apply { sourceSets["jvmTest"].apply {
dependencies {
}
kotlin.outputDir = file("build/classes/kotlin/jvm/test") kotlin.outputDir = file("build/classes/kotlin/jvm/test")
kotlin.setSrcDirs(listOf("src/$name/kotlin")) kotlin.setSrcDirs(listOf("src/$name/kotlin"))
} }
sourceSets.all { sourceSets.all {
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
package net.mamoe.mirai package net.mamoe.mirai
import kotlinx.coroutines.Job import kotlinx.coroutines.*
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.sync.withLock
import net.mamoe.mirai.Bot.ContactSystem import net.mamoe.mirai.Bot.ContactSystem
...@@ -19,6 +19,7 @@ import net.mamoe.mirai.utils.BotConfiguration ...@@ -19,6 +19,7 @@ import net.mamoe.mirai.utils.BotConfiguration
import net.mamoe.mirai.utils.DefaultLogger import net.mamoe.mirai.utils.DefaultLogger
import net.mamoe.mirai.utils.MiraiLogger import net.mamoe.mirai.utils.MiraiLogger
import net.mamoe.mirai.utils.internal.coerceAtLeastOrFail import net.mamoe.mirai.utils.internal.coerceAtLeastOrFail
import kotlin.coroutines.CoroutineContext
import kotlin.jvm.JvmOverloads import kotlin.jvm.JvmOverloads
data class BotAccount( data class BotAccount(
...@@ -56,7 +57,9 @@ data class BotAccount( ...@@ -56,7 +57,9 @@ data class BotAccount(
* @author NaturalHG * @author NaturalHG
* @see Contact * @see Contact
*/ */
class Bot(val account: BotAccount, val logger: MiraiLogger) { class Bot(val account: BotAccount, val logger: MiraiLogger) : CoroutineScope {
override val coroutineContext: CoroutineContext = SupervisorJob()
constructor(qq: UInt, password: String) : this(BotAccount(qq, password)) constructor(qq: UInt, password: String) : this(BotAccount(qq, password))
constructor(account: BotAccount) : this(account, DefaultLogger("Bot(" + account.id + ")")) constructor(account: BotAccount) : this(account, DefaultLogger("Bot(" + account.id + ")"))
...@@ -65,7 +68,9 @@ class Bot(val account: BotAccount, val logger: MiraiLogger) { ...@@ -65,7 +68,9 @@ class Bot(val account: BotAccount, val logger: MiraiLogger) {
var network: BotNetworkHandler<*> = TIMBotNetworkHandler(this) var network: BotNetworkHandler<*> = TIMBotNetworkHandler(this)
init { init {
instances.add(this) launch {
addInstance(this@Bot)
}
} }
override fun toString(): String = "Bot(${account.id})" override fun toString(): String = "Bot(${account.id})"
...@@ -150,13 +155,23 @@ class Bot(val account: BotAccount, val logger: MiraiLogger) { ...@@ -150,13 +155,23 @@ class Bot(val account: BotAccount, val logger: MiraiLogger) {
suspend inline fun GroupInternalId.group(): Group = getGroup(this) suspend inline fun GroupInternalId.group(): Group = getGroup(this)
suspend fun close() { suspend fun close() {
this.network.close() network.close()
this.contacts.groups.clear() this.coroutineContext.cancelChildren()
this.contacts.qqs.clear() contacts.groups.clear()
contacts.qqs.clear()
} }
companion object { companion object {
val instances: MutableList<Bot> = mutableListOf() @Suppress("ObjectPropertyName")
private val _instances: MutableList<Bot> = mutableListOf()
private val instanceLock: Mutex = Mutex()
val instances: List<Bot> get() = _instances
internal suspend fun addInstance(bot: Bot) = instanceLock.withLock {
_instances += bot
}
} }
} }
......
...@@ -25,6 +25,7 @@ import net.mamoe.mirai.utils.getGTK ...@@ -25,6 +25,7 @@ import net.mamoe.mirai.utils.getGTK
import net.mamoe.mirai.utils.internal.PositiveNumbers import net.mamoe.mirai.utils.internal.PositiveNumbers
import net.mamoe.mirai.utils.internal.coerceAtLeastOrFail import net.mamoe.mirai.utils.internal.coerceAtLeastOrFail
import kotlin.coroutines.coroutineContext import kotlin.coroutines.coroutineContext
import kotlin.jvm.JvmField
/** /**
* 构造 [BotSession] 的捷径 * 构造 [BotSession] 的捷径
...@@ -57,17 +58,23 @@ class BotSession( ...@@ -57,17 +58,23 @@ class BotSession(
/** /**
* Web api 使用 * Web api 使用
*/ */
var sKey: String = "" val sKey: String get() = _sKey
internal set(value) {
@JvmField
@Suppress("PropertyName")
internal var _sKey: String = ""
set(value) {
field = value field = value
gtk = getGTK(value) _gtk = getGTK(value)
} }
/** /**
* Web api 使用 * Web api 使用
*/ */
var gtk: Int = 0 val gtk: Int get() = _gtk
private set
@JvmField
private var _gtk: Int = 0
/** /**
* 发送一个数据包, 并期待接受一个特定的 [ServerPacket][P]. * 发送一个数据包, 并期待接受一个特定的 [ServerPacket][P].
......
...@@ -30,7 +30,7 @@ class ActionPacketHandler(session: BotSession) : PacketHandler(session) { ...@@ -30,7 +30,7 @@ class ActionPacketHandler(session: BotSession) : PacketHandler(session) {
override suspend fun onPacketReceived(packet: Packet): Unit = with(session) { override suspend fun onPacketReceived(packet: Packet): Unit = with(session) {
when (packet) { when (packet) {
is SKey -> { is SKey -> {
sKey = packet.delegate _sKey = packet.delegate
cookies = "uin=o$qqAccount;skey=$sKey;" cookies = "uin=o$qqAccount;skey=$sKey;"
...@@ -55,14 +55,14 @@ class ActionPacketHandler(session: BotSession) : PacketHandler(session) { ...@@ -55,14 +55,14 @@ class ActionPacketHandler(session: BotSession) : PacketHandler(session) {
private suspend fun requestSKey() = with(session) { private suspend fun requestSKey() = with(session) {
withContext(NetworkScope.coroutineContext) { withContext(NetworkScope.coroutineContext) {
socket.sendPacket(RequestSKeyPacket()) RequestSKeyPacket().send()
} }
} }
suspend fun requestAccountInfo() = with(session) { suspend fun requestAccountInfo() = with(session) {
withContext(NetworkScope.coroutineContext) { withContext(NetworkScope.coroutineContext) {
socket.sendPacket(RequestAccountInfoPacket(qqAccount, sessionKey)) RequestAccountInfoPacket(qqAccount, sessionKey).send()
} }
} }
......
package net.mamoe.mirai.network.protocol.tim.handler package net.mamoe.mirai.network.protocol.tim.handler
import kotlinx.coroutines.CoroutineScope
import net.mamoe.mirai.network.BotSession import net.mamoe.mirai.network.BotSession
import net.mamoe.mirai.network.protocol.tim.packet.Packet import net.mamoe.mirai.network.protocol.tim.packet.Packet
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
/** /**
* 数据包(接受/发送)处理器 * 数据包(接受/发送)处理器
*/ */
abstract class PacketHandler( abstract class PacketHandler(
val session: BotSession val session: BotSession
) { ) : CoroutineScope {
override val coroutineContext: CoroutineContext
get() = EmptyCoroutineContext
abstract suspend fun onPacketReceived(packet: Packet) abstract suspend fun onPacketReceived(packet: Packet)
interface Key<T : PacketHandler> interface Key<T : PacketHandler>
......
...@@ -35,7 +35,7 @@ class GentleImage { ...@@ -35,7 +35,7 @@ class GentleImage {
lateinit var contact: Contact lateinit var contact: Contact
// Deferred<Image?> 将导致 kotlin 内部错误 // `Deferred<Image?>` causes a runtime ClassCastException
val image: Deferred<Image> by lazy { val image: Deferred<Image> by lazy {
GlobalScope.async { GlobalScope.async {
//delay((Math.random() * 5000L).toLong()) //delay((Math.random() * 5000L).toLong())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment