Commit bd568a7c authored by Him188's avatar Him188

Suspend console

parent 4108e58e
......@@ -24,8 +24,8 @@ class MiraiGraphicalUIController : Controller(), MiraiConsoleUI {
val botList = observableListOf<BotModel>()
val consoleInfo = ConsoleInfo()
fun login(qq: String, psd: String) {
MiraiConsole.CommandListener.commandChannel.offer("/login $qq $psd")
suspend fun login(qq: String, psd: String) {
MiraiConsole.CommandListener.commandChannel.send("/login $qq $psd")
}
override fun pushLog(identity: Long, message: String) = Platform.runLater {
......
package net.mamoe.mirai.console.graphical.view
import com.jfoenix.controls.JFXTextField
import javafx.beans.property.SimpleStringProperty
import kotlinx.coroutines.runBlocking
import net.mamoe.mirai.console.graphical.controller.MiraiGraphicalUIController
import net.mamoe.mirai.console.graphical.util.jfxButton
import net.mamoe.mirai.console.graphical.util.jfxPasswordfield
......@@ -24,7 +24,9 @@ class LoginFragment : Fragment() {
}
}
jfxButton("登录").action {
controller.login(qq.value, psd.value)
runBlocking {
controller.login(qq.value, psd.value)
}
close()
}
}
......
......@@ -10,7 +10,6 @@ import com.googlecode.lanterna.terminal.DefaultTerminalFactory
import com.googlecode.lanterna.terminal.Terminal
import com.googlecode.lanterna.terminal.TerminalResizeListener
import com.googlecode.lanterna.terminal.swing.SwingTerminal
import com.googlecode.lanterna.terminal.swing.SwingTerminalFontConfiguration
import com.googlecode.lanterna.terminal.swing.SwingTerminalFrame
import kotlinx.coroutines.*
import kotlinx.coroutines.io.close
......@@ -23,7 +22,6 @@ import net.mamoe.mirai.console.MiraiConsoleTerminalUI.LoggerDrawer.redrawLogs
import net.mamoe.mirai.utils.LoginSolver
import net.mamoe.mirai.utils.createCharImg
import net.mamoe.mirai.utils.writeChannel
import java.awt.Font
import java.io.File
import java.io.OutputStream
import java.io.PrintStream
......@@ -126,12 +124,12 @@ object MiraiConsoleTerminalUI : MiraiConsoleUI {
}
fun provideInput(input: String) {
suspend fun provideInput(input: String) {
if (requesting) {
requestResult = input
requesting = false
} else {
MiraiConsole.CommandListener.commandChannel.offer(
MiraiConsole.CommandListener.commandChannel.send(
commandBuilder.toString()
)
}
......@@ -336,7 +334,9 @@ object MiraiConsoleTerminalUI : MiraiConsoleUI {
update()
}
KeyType.Enter -> {
provideInput(commandBuilder.toString())
runBlocking {
provideInput(commandBuilder.toString())
}
emptyCommand()
}
KeyType.Escape -> {
......
......@@ -42,7 +42,7 @@ object CommandManager {
registeredCommand.remove(commandName)
}
fun runCommand(fullCommand: String): Boolean {
suspend fun runCommand(fullCommand: String): Boolean {
val blocks = fullCommand.split(" ")
val commandHead = blocks[0].replace("/", "")
if (!registeredCommand.containsKey(commandHead)) {
......@@ -66,7 +66,7 @@ interface ICommand {
val name: String
val alias: List<String>
val description: String
fun onCommand(args: List<String>): Boolean
suspend fun onCommand(args: List<String>): Boolean
fun register()
}
......@@ -77,9 +77,9 @@ abstract class Command(
) : ICommand {
/**
* 最高优先级监听器
* 如果return [false] 这次指令不会被[PluginBase]的全局onCommand监听器监听
* 如果 return `false` 这次指令不会被 [PluginBase] 的全局 onCommand 监听器监听
* */
open override fun onCommand(args: List<String>): Boolean {
open override suspend fun onCommand(args: List<String>): Boolean {
return true
}
......@@ -92,9 +92,9 @@ class AnonymousCommand internal constructor(
override val name: String,
override val alias: List<String>,
override val description: String,
val onCommand: ICommand.(args: List<String>) -> Boolean
val onCommand: suspend ICommand.(args: List<String>) -> Boolean
) : ICommand {
override fun onCommand(args: List<String>): Boolean {
override suspend fun onCommand(args: List<String>): Boolean {
return onCommand.invoke(this, args)
}
......@@ -107,9 +107,9 @@ class CommandBuilder internal constructor() {
var name: String? = null
var alias: List<String>? = null
var description: String = ""
var onCommand: (ICommand.(args: List<String>) -> Boolean)? = null
var onCommand: (suspend ICommand.(args: List<String>) -> Boolean)? = null
fun onCommand(commandProcess: ICommand.(args: List<String>) -> Boolean) {
fun onCommand(commandProcess: suspend ICommand.(args: List<String>) -> Boolean) {
onCommand = commandProcess
}
......
......@@ -9,20 +9,20 @@ package net.mamoe.mirai.console
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import net.mamoe.mirai.Bot
import net.mamoe.mirai.api.http.MiraiHttpAPIServer
import net.mamoe.mirai.api.http.generateSessionKey
import net.mamoe.mirai.console.MiraiConsole.CommandListener.processNextCommandLine
import net.mamoe.mirai.console.plugins.PluginManager
import net.mamoe.mirai.console.plugins.loadAsConfig
import net.mamoe.mirai.console.plugins.withDefaultWrite
import net.mamoe.mirai.console.plugins.withDefaultWriteSave
import net.mamoe.mirai.contact.sendMessage
import net.mamoe.mirai.utils.*
import net.mamoe.mirai.utils.SimpleLogger
import java.io.File
import java.util.*
import java.util.concurrent.LinkedBlockingQueue
import kotlin.concurrent.thread
object MiraiConsole {
......@@ -288,20 +288,19 @@ object MiraiConsole {
}
}
object CommandListener {
val commandChannel: Queue<String> = LinkedBlockingQueue<String>()
fun start() {
thread {
processNextCommandLine()
}
object CommandListener : Job by {
GlobalScope.launch(start = CoroutineStart.LAZY) {
processNextCommandLine()
}
}() {
val commandChannel: Channel<String> = Channel()
tailrec fun processNextCommandLine() {
suspend fun processNextCommandLine() {
if (allDown) {
return
}
var fullCommand = commandChannel.poll()
if (fullCommand != null) {
for (command in commandChannel) {
var fullCommand = command
if (!fullCommand.startsWith("/")) {
fullCommand = "/$fullCommand"
}
......@@ -309,7 +308,6 @@ object MiraiConsole {
logger("未知指令 $fullCommand")
}
}
processNextCommandLine();
}
}
......
package net.mamoe.mirai.console
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import net.mamoe.mirai.Bot
import net.mamoe.mirai.utils.DefaultLoginSolver
import net.mamoe.mirai.utils.LoginSolver
import net.mamoe.mirai.utils.LoginSolverInputReader
import kotlin.concurrent.thread
class MiraiConsoleUIPure() : MiraiConsoleUI {
class MiraiConsoleUIPure : MiraiConsoleUI {
var requesting = false
var requestStr = ""
init {
thread {
while (true) {
val input = readLine() ?: ""
val input = readLine() ?: return@thread
if (requesting) {
requestStr = input
requesting = false
} else {
MiraiConsole.CommandListener.commandChannel.offer(input)
runBlocking {
MiraiConsole.CommandListener.commandChannel.send(input)
}
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment