From cb3bba986c688a145f0750ce5efbcc497f4bea10 Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Fri, 6 May 2022 17:47:22 +0800 Subject: [PATCH] Fix `IMirai` initialize; Fix #1861; Fix some error caused by d75776430814c87379ae2a6d881d1b0f46c98459 --- .../kotlin/findMiraiInstance.android.kt | 19 ------------------- .../src/commonMain/kotlin/IMirai.kt | 10 +++++++++- .../src/jvmMain/kotlin/findMiraiInstance.kt | 19 ------------------- 3 files changed, 9 insertions(+), 39 deletions(-) delete mode 100644 mirai-core-api/src/androidMain/kotlin/findMiraiInstance.android.kt delete mode 100644 mirai-core-api/src/jvmMain/kotlin/findMiraiInstance.kt diff --git a/mirai-core-api/src/androidMain/kotlin/findMiraiInstance.android.kt b/mirai-core-api/src/androidMain/kotlin/findMiraiInstance.android.kt deleted file mode 100644 index 0e787acaf..000000000 --- a/mirai-core-api/src/androidMain/kotlin/findMiraiInstance.android.kt +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright 2019-2021 Mamoe Technologies and contributors. - * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. - * - * https://github.com/mamoe/mirai/blob/dev/LICENSE - */ - -package net.mamoe.mirai - -import java.util.* -import kotlin.reflect.full.companionObjectInstance - -@JvmSynthetic -internal actual fun findMiraiInstance(): IMirai { - return ServiceLoader.load(IMirai::class.java).firstOrNull() - ?: Class.forName("net.mamoe.mirai.internal.MiraiImpl").kotlin.companionObjectInstance as IMirai -} \ No newline at end of file diff --git a/mirai-core-api/src/commonMain/kotlin/IMirai.kt b/mirai-core-api/src/commonMain/kotlin/IMirai.kt index 211478569..11ceb78c0 100644 --- a/mirai-core-api/src/commonMain/kotlin/IMirai.kt +++ b/mirai-core-api/src/commonMain/kotlin/IMirai.kt @@ -31,6 +31,8 @@ import net.mamoe.mirai.message.data.* import net.mamoe.mirai.message.data.Image.Key.queryUrl import net.mamoe.mirai.message.data.MessageSource.Key.recall import net.mamoe.mirai.utils.* +import java.util.ServiceLoader +import kotlin.reflect.full.companionObjectInstance /** * [IMirai] 实例. @@ -354,4 +356,10 @@ internal object _MiraiInstance { } @JvmSynthetic -internal expect fun findMiraiInstance(): IMirai \ No newline at end of file +internal fun findMiraiInstance(): IMirai { + ServiceLoader.load(IMirai::class.java).firstOrNull()?.let { return it } + + val implClass = Class.forName("net.mamoe.mirai.internal.MiraiImpl") + (implClass.kotlin.companionObjectInstance as? IMirai)?.let { return it } + return implClass.asSubclass(IMirai::class.java).getConstructor().newInstance() +} \ No newline at end of file diff --git a/mirai-core-api/src/jvmMain/kotlin/findMiraiInstance.kt b/mirai-core-api/src/jvmMain/kotlin/findMiraiInstance.kt deleted file mode 100644 index 0e787acaf..000000000 --- a/mirai-core-api/src/jvmMain/kotlin/findMiraiInstance.kt +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright 2019-2021 Mamoe Technologies and contributors. - * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. - * - * https://github.com/mamoe/mirai/blob/dev/LICENSE - */ - -package net.mamoe.mirai - -import java.util.* -import kotlin.reflect.full.companionObjectInstance - -@JvmSynthetic -internal actual fun findMiraiInstance(): IMirai { - return ServiceLoader.load(IMirai::class.java).firstOrNull() - ?: Class.forName("net.mamoe.mirai.internal.MiraiImpl").kotlin.companionObjectInstance as IMirai -} \ No newline at end of file