Fix IMirai initialize; Fix #1861; Fix some error caused by d757764308

This commit is contained in:
Karlatemp 2022-05-06 17:47:22 +08:00
parent 78d0cdd155
commit cb3bba986c
No known key found for this signature in database
GPG Key ID: C6B606FF23D8FED7
3 changed files with 9 additions and 39 deletions

View File

@ -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
}

View File

@ -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
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()
}

View File

@ -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
}