Commit c15651b9 authored by Him188's avatar Him188

Adjust property name and visibility

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