From b59ae076ec9a4580a6789a33e5e2dd5c4ee441e9 Mon Sep 17 00:00:00 2001 From: Him188 <Him188@mamoe.net> Date: Mon, 17 Feb 2020 00:16:50 +0800 Subject: [PATCH] Add context getter --- .../kotlin/net/mamoe/mirai/qqandroid/QQAndroidBot.kt | 2 +- .../src/commonMain/kotlin/net.mamoe.mirai/Bot.kt | 11 ++++++++++- .../src/commonMain/kotlin/net.mamoe.mirai/BotImpl.kt | 2 ++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/QQAndroidBot.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/QQAndroidBot.kt index 5671f2088..093f7f9c8 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/QQAndroidBot.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/QQAndroidBot.kt @@ -43,7 +43,7 @@ internal abstract class QQAndroidBotBase constructor( context: Context, account: BotAccount, configuration: BotConfiguration -) : BotImpl<QQAndroidBotNetworkHandler>(account, configuration) { +) : BotImpl<QQAndroidBotNetworkHandler>(context, account, configuration) { val client: QQAndroidClient = QQAndroidClient( context, diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/Bot.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/Bot.kt index 4430c4514..aea4f1683 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/Bot.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/Bot.kt @@ -44,7 +44,8 @@ abstract class Bot : CoroutineScope { * 复制一份此时的 [Bot] 实例列表. */ @JvmStatic - val instances: List<WeakRef<Bot>> get() = BotImpl.instances.toList() + val instances: List<WeakRef<Bot>> + get() = BotImpl.instances.toList() /** * 遍历每一个 [Bot] 实例 @@ -58,6 +59,14 @@ abstract class Bot : CoroutineScope { fun instanceWhose(qq: Long): Bot = BotImpl.instanceWhose(qq = qq) } + /** + * [Bot] 运行的 [Context]. + * + * 在 JVM 的默认实现为 `class ContextImpl : Context` + * 在 Android 实现为 `android.content.Context` + */ + abstract val context: Context + /** * 账号信息 */ diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/BotImpl.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/BotImpl.kt index 23a913e50..a8686f90f 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/BotImpl.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/BotImpl.kt @@ -32,6 +32,7 @@ import kotlin.coroutines.CoroutineContext @UseExperimental(MiraiExperimentalAPI::class) @MiraiInternalAPI abstract class BotImpl<N : BotNetworkHandler> constructor( + context: Context, account: BotAccount, val configuration: BotConfiguration ) : Bot(), CoroutineScope { @@ -39,6 +40,7 @@ abstract class BotImpl<N : BotNetworkHandler> constructor( override val coroutineContext: CoroutineContext = configuration.parentCoroutineContext + botJob + (configuration.parentCoroutineContext[CoroutineExceptionHandler] ?: CoroutineExceptionHandler { _, e -> logger.error("An exception was thrown under a coroutine of Bot", e) }) + override val context: Context by context.unsafeWeakRef() @Suppress("CanBePrimaryConstructorProperty") // for logger final override val account: BotAccount = account