diff --git a/mirai-console/frontend/mirai-console-terminal/src/MiraiConsoleImplementationTerminal.kt b/mirai-console/frontend/mirai-console-terminal/src/MiraiConsoleImplementationTerminal.kt index cb7a3b78cbd..39d43d365da 100644 --- a/mirai-console/frontend/mirai-console-terminal/src/MiraiConsoleImplementationTerminal.kt +++ b/mirai-console/frontend/mirai-console-terminal/src/MiraiConsoleImplementationTerminal.kt @@ -58,6 +58,8 @@ import org.jline.terminal.TerminalBuilder import org.jline.terminal.impl.AbstractWindowsTerminal import java.nio.file.Path import java.nio.file.Paths +import kotlin.io.path.exists +import kotlin.io.path.outputStream /** * mirai-console-terminal 后端实现 @@ -131,6 +133,10 @@ open class MiraiConsoleImplementationTerminal get() = ConsoleTerminalSettings.launchOptions override fun preStart() { + + val lock = rootPath.resolve("startup.properties") + if (lock.exists()) throw RuntimeException("已有其他实例启动,或者上次启动后没有正常关闭 Mirai Console") + registerSignalHandler() JLineInputDaemon.terminal0 = this @@ -145,6 +151,16 @@ open class MiraiConsoleImplementationTerminal } } + override fun postStart() { + launch(CoroutineName("Mirai Console Terminal Startup Lock")) { + val lock = rootPath.resolve("startup.properties") + lock.outputStream().use { output -> + System.getProperties().store(output, "startup lock") + } + lock.toFile().deleteOnExit() + } + } + override fun createNewProcessProgress(): ProcessProgress { if (terminal is NoConsole) return super.createNewProcessProgress()