Introduce interface MiraiConsole

This commit is contained in:
Him188 2020-06-26 19:50:13 +08:00
parent bd4b0b88ca
commit d2194c2c2b

View File

@ -13,6 +13,8 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.io.charsets.Charset import kotlinx.io.charsets.Charset
import net.mamoe.mirai.Bot import net.mamoe.mirai.Bot
import net.mamoe.mirai.console.MiraiConsole.INSTANCE
import net.mamoe.mirai.console.command.ConsoleCommandOwner
import net.mamoe.mirai.console.plugin.PluginLoader import net.mamoe.mirai.console.plugin.PluginLoader
import net.mamoe.mirai.console.plugin.center.CuiPluginCenter import net.mamoe.mirai.console.plugin.center.CuiPluginCenter
import net.mamoe.mirai.console.plugin.center.PluginCenter import net.mamoe.mirai.console.plugin.center.PluginCenter
@ -26,15 +28,57 @@ import java.io.PrintStream
import java.util.* import java.util.*
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
/**
* mirai-console 实例
*
* @see INSTANCE
*/
interface MiraiConsole {
/**
* Console 运行路径
*/
val rootDir: File
/**
* Console 前端接口
*/
val frontEnd: MiraiConsoleFrontEnd
/**
* 与前端交互所使用的 Logger
*/
val mainLogger: MiraiLogger
/**
* 内建加载器列表, 一般需要包含 [JarPluginLoader]
*/
val builtInPluginLoaders: List<PluginLoader<*, *>>
val buildDate: Date
val version: String
val pluginCenter: PluginCenter
@MiraiExperimentalAPI
fun newLogger(identity: String?): MiraiLogger
companion object INSTANCE : MiraiConsole by MiraiConsoleImpl
}
//// internal
internal object MiraiConsoleInitializer { internal object MiraiConsoleInitializer {
internal lateinit var instance: IMiraiConsole internal lateinit var instance: IMiraiConsole
/** 由前端调用 */ /** 由前端调用 */
internal fun init(instance: IMiraiConsole) { internal fun init(instance: IMiraiConsole) {
this.instance = instance this.instance = instance
MiraiConsole.initialize() MiraiConsoleImpl.initialize()
} }
} }
internal object MiraiConsoleBuildConstants { // auto-filled on build (task :mirai-console:fillBuildConstants) internal object MiraiConsoleBuildConstants { // auto-filled on build (task :mirai-console:fillBuildConstants)
@ -46,8 +90,8 @@ internal object MiraiConsoleBuildConstants { // auto-filled on build (task :mira
/** /**
* mirai 控制台实例. * mirai 控制台实例.
*/ */
object MiraiConsole : CoroutineScope, IMiraiConsole { internal object MiraiConsoleImpl : CoroutineScope, IMiraiConsole, MiraiConsole {
val pluginCenter: PluginCenter get() = CuiPluginCenter override val pluginCenter: PluginCenter get() = CuiPluginCenter
private val instance: IMiraiConsole private val instance: IMiraiConsole
get() = MiraiConsoleInitializer.instance get() = MiraiConsoleInitializer.instance
@ -57,14 +101,13 @@ object MiraiConsole : CoroutineScope, IMiraiConsole {
* *
* UTC+8 时间 * UTC+8 时间
*/ */
@JvmStatic override val buildDate: Date
val buildDate: Date
get() = MiraiConsoleBuildConstants.buildDate get() = MiraiConsoleBuildConstants.buildDate
/** /**
* `mirai-console` 版本 * `mirai-console` 版本
*/ */
const val version: String = MiraiConsoleBuildConstants.version override val version: String get() = MiraiConsoleBuildConstants.version
/** /**
* Console 运行路径 * Console 运行路径
@ -87,6 +130,9 @@ object MiraiConsole : CoroutineScope, IMiraiConsole {
override val builtInPluginLoaders: List<PluginLoader<*, *>> get() = instance.builtInPluginLoaders override val builtInPluginLoaders: List<PluginLoader<*, *>> get() = instance.builtInPluginLoaders
override val consoleCommandOwner: ConsoleCommandOwner
get() = instance.consoleCommandOwner
init { init {
DefaultLogger = { identity -> this.newLogger(identity) } DefaultLogger = { identity -> this.newLogger(identity) }
this.coroutineContext[Job]!!.invokeOnCompletion { this.coroutineContext[Job]!!.invokeOnCompletion {
@ -95,7 +141,7 @@ object MiraiConsole : CoroutineScope, IMiraiConsole {
} }
@MiraiExperimentalAPI @MiraiExperimentalAPI
fun newLogger(identity: String?): MiraiLogger = frontEnd.loggerFor(identity) override fun newLogger(identity: String?): MiraiLogger = frontEnd.loggerFor(identity)
internal fun initialize() { internal fun initialize() {
// Only for initialize // Only for initialize
@ -124,6 +170,9 @@ internal interface IMiraiConsole : CoroutineScope {
* 内建加载器列表, 一般需要包含 [JarPluginLoader] * 内建加载器列表, 一般需要包含 [JarPluginLoader]
*/ */
val builtInPluginLoaders: List<PluginLoader<*, *>> val builtInPluginLoaders: List<PluginLoader<*, *>>
@Suppress("WRONG_MODIFIER_CONTAINING_DECLARATION")
internal val consoleCommandOwner: ConsoleCommandOwner
} }
/** /**