Add _MiraiInstance to allow internal instance overrides

This commit is contained in:
Him188 2021-04-07 21:47:05 +08:00
parent bb5c85965a
commit 944099a977
3 changed files with 33 additions and 1 deletions

View File

@ -186,6 +186,12 @@ public final class net/mamoe/mirai/Mirai {
public static final synthetic fun recallMessage (Lnet/mamoe/mirai/IMirai;Lnet/mamoe/mirai/Bot;Lnet/mamoe/mirai/message/data/MessageChain;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}
public final class net/mamoe/mirai/_MiraiInstance {
public static final field INSTANCE Lnet/mamoe/mirai/_MiraiInstance;
public static final fun get ()Lnet/mamoe/mirai/IMirai;
public static final fun set (Lnet/mamoe/mirai/IMirai;)V
}
public abstract interface class net/mamoe/mirai/contact/AnonymousMember : net/mamoe/mirai/contact/Member {
public abstract fun getAnonymousId ()Ljava/lang/String;
public fun nudge ()Lnet/mamoe/mirai/message/action/MemberNudge;

View File

@ -186,6 +186,12 @@ public final class net/mamoe/mirai/Mirai {
public static final synthetic fun recallMessage (Lnet/mamoe/mirai/IMirai;Lnet/mamoe/mirai/Bot;Lnet/mamoe/mirai/message/data/MessageChain;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}
public final class net/mamoe/mirai/_MiraiInstance {
public static final field INSTANCE Lnet/mamoe/mirai/_MiraiInstance;
public static final fun get ()Lnet/mamoe/mirai/IMirai;
public static final fun set (Lnet/mamoe/mirai/IMirai;)V
}
public abstract interface class net/mamoe/mirai/contact/AnonymousMember : net/mamoe/mirai/contact/Member {
public abstract fun getAnonymousId ()Ljava/lang/String;
public fun nudge ()Lnet/mamoe/mirai/message/action/MemberNudge;

View File

@ -34,7 +34,8 @@ import net.mamoe.mirai.utils.MiraiInternalApi
* [IMirai] 实例
*/
@get:JvmName("getInstance") // Java 调用: Mirai.getInstance()
public val Mirai: IMirai by lazy { findMiraiInstance() }
public val Mirai: IMirai
get() = _MiraiInstance.get()
/**
* Mirai API 接口.
@ -284,5 +285,24 @@ public interface IMirai : LowLevelApiAccessor {
public suspend inline fun IMirai.recallMessage(bot: Bot, message: MessageChain): Unit =
this.recallMessage(bot, message.source)
/**
* @since 2.6-RC
*/
@PublishedApi // for tests and potential public uses.
@Suppress("ClassName")
internal object _MiraiInstance {
private var instance: IMirai? = null
@JvmStatic
fun set(instance: IMirai) {
this.instance = instance
}
@JvmStatic
fun get(): IMirai {
return instance ?: findMiraiInstance().also { instance = it }
}
}
@JvmSynthetic
internal expect fun findMiraiInstance(): IMirai