mirror of
https://github.com/mamoe/mirai.git
synced 2025-03-11 04:40:10 +08:00
Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
8735cd2e51
@ -25,8 +25,9 @@ import kotlin.coroutines.CoroutineContext
|
|||||||
|
|
||||||
object MiraiHttpAPIServer : CoroutineScope {
|
object MiraiHttpAPIServer : CoroutineScope {
|
||||||
|
|
||||||
private val logger = DefaultLogger("Mirai HTTP API")
|
var logger = DefaultLogger("Mirai HTTP API")
|
||||||
override val coroutineContext: CoroutineContext = CoroutineExceptionHandler { _, throwable -> logger.error(throwable) }
|
override val coroutineContext: CoroutineContext =
|
||||||
|
CoroutineExceptionHandler { _, throwable -> logger.error(throwable) }
|
||||||
|
|
||||||
init {
|
init {
|
||||||
SessionManager.authKey = generateSessionKey()//用于验证的key, 使用和SessionKey相同的方法生成, 但意义不同
|
SessionManager.authKey = generateSessionKey()//用于验证的key, 使用和SessionKey相同的方法生成, 但意义不同
|
||||||
|
@ -18,6 +18,7 @@ import net.mamoe.mirai.api.http.MiraiHttpAPIServer
|
|||||||
import net.mamoe.mirai.api.http.generateSessionKey
|
import net.mamoe.mirai.api.http.generateSessionKey
|
||||||
import net.mamoe.mirai.contact.sendMessage
|
import net.mamoe.mirai.contact.sendMessage
|
||||||
import net.mamoe.mirai.utils.MiraiLogger
|
import net.mamoe.mirai.utils.MiraiLogger
|
||||||
|
import net.mamoe.mirai.utils.SimpleLogger
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.PrintStream
|
import java.io.PrintStream
|
||||||
import kotlin.concurrent.thread
|
import kotlin.concurrent.thread
|
||||||
@ -38,26 +39,21 @@ object MiraiConsole {
|
|||||||
val pluginManager: PluginManager
|
val pluginManager: PluginManager
|
||||||
get() = PluginManager
|
get() = PluginManager
|
||||||
|
|
||||||
var logger = UIPushLogger(0)
|
var logger = UIPushLogger
|
||||||
|
|
||||||
var path: String = System.getProperty("user.dir")
|
var path: String = System.getProperty("user.dir")
|
||||||
|
|
||||||
val version = "0.01"
|
val version = "0.01"
|
||||||
var coreVersion = "0.13"
|
var coreVersion = "0.15"
|
||||||
val build = "Beta"
|
val build = "Beta"
|
||||||
|
|
||||||
fun start() {
|
fun start() {
|
||||||
logger("Mirai-console v$version $build | core version v$coreVersion is still in testing stage, majority feature is available")
|
MiraiConsoleUI.start()
|
||||||
logger("Mirai-console v$version $build | 核心版本 v${coreVersion}还处于测试阶段, 大部分功能可用")
|
logger("Mirai-console [v$version $build | core version v$coreVersion] is still in testing stage, majority feature is available")
|
||||||
logger()
|
|
||||||
logger("Mirai-console now running under " + System.getProperty("user.dir"))
|
logger("Mirai-console now running under " + System.getProperty("user.dir"))
|
||||||
logger("Mirai-console 正在 " + System.getProperty("user.dir") + "下运行")
|
|
||||||
logger()
|
|
||||||
logger("Get news in github: https://github.com/mamoe/mirai")
|
logger("Get news in github: https://github.com/mamoe/mirai")
|
||||||
logger("在Github中获取项目最新进展: https://github.com/mamoe/mirai")
|
|
||||||
logger("Mirai为开源项目,请自觉遵守开源项目协议")
|
logger("Mirai为开源项目,请自觉遵守开源项目协议")
|
||||||
logger("Powered by Mamoe Technologies and contributors")
|
logger("Powered by Mamoe Technologies and contributors")
|
||||||
logger()
|
|
||||||
|
|
||||||
runBlocking {
|
runBlocking {
|
||||||
DefaultCommands()
|
DefaultCommands()
|
||||||
@ -83,11 +79,14 @@ object MiraiConsole {
|
|||||||
logger("请尽快更改初始生成的HTTP API AUTHKEY")
|
logger("请尽快更改初始生成的HTTP API AUTHKEY")
|
||||||
}
|
}
|
||||||
logger("正在启动HTTPAPI; 端口=" + MiraiProperties.HTTP_API_PORT)
|
logger("正在启动HTTPAPI; 端口=" + MiraiProperties.HTTP_API_PORT)
|
||||||
|
MiraiHttpAPIServer.logger = SimpleLogger("HTTP API") { _, message, e ->
|
||||||
|
logger("[Mirai HTTP API]", 0, message)
|
||||||
|
}
|
||||||
MiraiHttpAPIServer.start(
|
MiraiHttpAPIServer.start(
|
||||||
MiraiProperties.HTTP_API_PORT,
|
MiraiProperties.HTTP_API_PORT,
|
||||||
MiraiProperties.HTTP_API_AUTH_KEY
|
MiraiProperties.HTTP_API_AUTH_KEY
|
||||||
)
|
)
|
||||||
logger("HTTPAPI启动完成; 端口=" + MiraiProperties.HTTP_API_PORT)
|
logger("HTTPAPI启动完成; 端口= " + MiraiProperties.HTTP_API_PORT)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -255,11 +254,14 @@ object MiraiConsole {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class UIPushLogger(val identity: Long) {
|
object UIPushLogger {
|
||||||
operator fun invoke(any: Any? = null) {
|
operator fun invoke(any: Any? = null) {
|
||||||
MiraiConsoleUI.start()
|
invoke("[Mirai$version $build]", 0L, any)
|
||||||
|
}
|
||||||
|
|
||||||
|
operator fun invoke(identityStr: String, identity: Long, any: Any? = null) {
|
||||||
if (any != null) {
|
if (any != null) {
|
||||||
MiraiConsoleUI.pushLog(identity, "[Mirai$version $build]: $any")
|
MiraiConsoleUI.pushLog(identity, "$identityStr: $any")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,8 @@ import com.googlecode.lanterna.terminal.Terminal
|
|||||||
import com.googlecode.lanterna.terminal.TerminalResizeListener
|
import com.googlecode.lanterna.terminal.TerminalResizeListener
|
||||||
import com.googlecode.lanterna.terminal.swing.SwingTerminal
|
import com.googlecode.lanterna.terminal.swing.SwingTerminal
|
||||||
import com.googlecode.lanterna.terminal.swing.SwingTerminalFrame
|
import com.googlecode.lanterna.terminal.swing.SwingTerminalFrame
|
||||||
|
import kotlinx.coroutines.*
|
||||||
|
import net.mamoe.mirai.MiraiConsoleUI.LoggerDrawer.cleanPage
|
||||||
import net.mamoe.mirai.MiraiConsoleUI.LoggerDrawer.drawLog
|
import net.mamoe.mirai.MiraiConsoleUI.LoggerDrawer.drawLog
|
||||||
import net.mamoe.mirai.MiraiConsoleUI.LoggerDrawer.redrawLogs
|
import net.mamoe.mirai.MiraiConsoleUI.LoggerDrawer.redrawLogs
|
||||||
import net.mamoe.mirai.utils.currentTimeSeconds
|
import net.mamoe.mirai.utils.currentTimeSeconds
|
||||||
@ -21,18 +23,35 @@ import java.util.*
|
|||||||
import kotlin.concurrent.thread
|
import kotlin.concurrent.thread
|
||||||
import kotlin.math.ceil
|
import kotlin.math.ceil
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 此文件不推荐任何人看
|
||||||
|
* 可能导致
|
||||||
|
* 1:心肌梗死
|
||||||
|
* 2:呼吸困难
|
||||||
|
* 3:想要重写但是发现改任何一个看似不合理的地方都会崩
|
||||||
|
*
|
||||||
|
* @author NaturalHG
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
@SuppressWarnings("UNCHECKED")
|
||||||
object MiraiConsoleUI {
|
object MiraiConsoleUI {
|
||||||
|
val cacheLogSize = 50
|
||||||
|
|
||||||
val log = mutableMapOf<Long, LimitLinkedQueue<String>>().also { it[0L] = LimitLinkedQueue(50) }
|
val log = mutableMapOf<Long, LimitLinkedQueue<String>>().also {
|
||||||
|
it[0L] = LimitLinkedQueue(cacheLogSize)
|
||||||
|
}
|
||||||
|
val botAdminCount = mutableMapOf<Long, Long>()
|
||||||
|
|
||||||
|
|
||||||
private val screens = mutableListOf(0L)
|
private val screens = mutableListOf(0L, 2821869985L)
|
||||||
private var currentScreenId = 0
|
private var currentScreenId = 0
|
||||||
|
|
||||||
|
|
||||||
fun addBotScreen(uin: Long) {
|
fun addBotScreen(uin: Long) {
|
||||||
screens.add(uin)
|
screens.add(uin)
|
||||||
log[uin] = LimitLinkedQueue()
|
log[uin] = LimitLinkedQueue(cacheLogSize)
|
||||||
|
botAdminCount[uin] = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -68,12 +87,46 @@ object MiraiConsoleUI {
|
|||||||
}
|
}
|
||||||
textGraphics = terminal.newTextGraphics()
|
textGraphics = terminal.newTextGraphics()
|
||||||
|
|
||||||
|
/*
|
||||||
|
var lastRedrawTime = 0L
|
||||||
|
var lastNewWidth = 0
|
||||||
|
var lastNewHeight = 0
|
||||||
|
|
||||||
terminal.addResizeListener(TerminalResizeListener { terminal1: Terminal, newSize: TerminalSize ->
|
terminal.addResizeListener(TerminalResizeListener { terminal1: Terminal, newSize: TerminalSize ->
|
||||||
|
try {
|
||||||
|
if (lastNewHeight == newSize.rows
|
||||||
|
&&
|
||||||
|
lastNewWidth == newSize.columns
|
||||||
|
) {
|
||||||
|
return@TerminalResizeListener
|
||||||
|
}
|
||||||
|
lastNewHeight = newSize.rows
|
||||||
|
lastNewWidth = newSize.columns
|
||||||
terminal.clearScreen()
|
terminal.clearScreen()
|
||||||
inited = false
|
if(terminal !is SwingTerminalFrame) {
|
||||||
|
Thread.sleep(300)
|
||||||
|
}
|
||||||
update()
|
update()
|
||||||
redrawCommand()
|
redrawCommand()
|
||||||
redrawLogs(log[screens[currentScreenId]]!!)
|
redrawLogs(log[screens[currentScreenId]]!!)
|
||||||
|
}catch (ignored:Exception){
|
||||||
|
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
*/
|
||||||
|
var lastJob: Job? = null
|
||||||
|
terminal.addResizeListener(TerminalResizeListener { terminal1: Terminal, newSize: TerminalSize ->
|
||||||
|
lastJob = GlobalScope.launch {
|
||||||
|
delay(300)
|
||||||
|
if (lastJob == coroutineContext[Job]) {
|
||||||
|
terminal.clearScreen()
|
||||||
|
//inited = false
|
||||||
|
update()
|
||||||
|
redrawCommand()
|
||||||
|
redrawLogs(log[screens[currentScreenId]]!!)
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
if (terminal !is SwingTerminalFrame) {
|
if (terminal !is SwingTerminalFrame) {
|
||||||
@ -104,10 +157,14 @@ object MiraiConsoleUI {
|
|||||||
when (keyStroke.keyType) {
|
when (keyStroke.keyType) {
|
||||||
KeyType.ArrowLeft -> {
|
KeyType.ArrowLeft -> {
|
||||||
currentScreenId = getLeftScreenId()
|
currentScreenId = getLeftScreenId()
|
||||||
|
clearRows(2)
|
||||||
|
cleanPage()
|
||||||
update()
|
update()
|
||||||
}
|
}
|
||||||
KeyType.ArrowRight -> {
|
KeyType.ArrowRight -> {
|
||||||
currentScreenId = getRightScreenId()
|
currentScreenId = getRightScreenId()
|
||||||
|
clearRows(2)
|
||||||
|
cleanPage()
|
||||||
update()
|
update()
|
||||||
}
|
}
|
||||||
KeyType.Enter -> {
|
KeyType.Enter -> {
|
||||||
@ -128,7 +185,7 @@ object MiraiConsoleUI {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getLeftScreenId(): Int {
|
private fun getLeftScreenId(): Int {
|
||||||
var newId = currentScreenId - 1
|
var newId = currentScreenId - 1
|
||||||
if (newId < 0) {
|
if (newId < 0) {
|
||||||
newId = screens.size - 1
|
newId = screens.size - 1
|
||||||
@ -136,7 +193,7 @@ object MiraiConsoleUI {
|
|||||||
return newId
|
return newId
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getRightScreenId(): Int {
|
private fun getRightScreenId(): Int {
|
||||||
var newId = 1 + currentScreenId
|
var newId = 1 + currentScreenId
|
||||||
if (newId >= screens.size) {
|
if (newId >= screens.size) {
|
||||||
newId = 0
|
newId = 0
|
||||||
@ -144,7 +201,7 @@ object MiraiConsoleUI {
|
|||||||
return newId
|
return newId
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getScreenName(id: Int): String {
|
private fun getScreenName(id: Int): String {
|
||||||
return when (screens[id]) {
|
return when (screens[id]) {
|
||||||
0L -> {
|
0L -> {
|
||||||
"Console Screen"
|
"Console Screen"
|
||||||
@ -156,7 +213,6 @@ object MiraiConsoleUI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var inited = false
|
|
||||||
fun clearRows(row: Int) {
|
fun clearRows(row: Int) {
|
||||||
textGraphics.putString(0, row, " ".repeat(terminal.terminalSize.columns))
|
textGraphics.putString(0, row, " ".repeat(terminal.terminalSize.columns))
|
||||||
}
|
}
|
||||||
@ -167,7 +223,7 @@ object MiraiConsoleUI {
|
|||||||
val width = terminal.terminalSize.columns
|
val width = terminal.terminalSize.columns
|
||||||
val height = terminal.terminalSize.rows
|
val height = terminal.terminalSize.rows
|
||||||
terminal.setBackgroundColor(TextColor.ANSI.DEFAULT)
|
terminal.setBackgroundColor(TextColor.ANSI.DEFAULT)
|
||||||
if (!inited) {
|
|
||||||
val mainTitle = "Mirai Console v0.01 Core v0.15"
|
val mainTitle = "Mirai Console v0.01 Core v0.15"
|
||||||
textGraphics.foregroundColor = TextColor.ANSI.WHITE
|
textGraphics.foregroundColor = TextColor.ANSI.WHITE
|
||||||
textGraphics.backgroundColor = TextColor.ANSI.GREEN
|
textGraphics.backgroundColor = TextColor.ANSI.GREEN
|
||||||
@ -180,12 +236,11 @@ object MiraiConsoleUI {
|
|||||||
textGraphics.putString(2, height - 3, "|>>>")
|
textGraphics.putString(2, height - 3, "|>>>")
|
||||||
textGraphics.putString(width - 3, height - 3, "|")
|
textGraphics.putString(width - 3, height - 3, "|")
|
||||||
textGraphics.putString(2, height - 2, "-".repeat(width - 4))
|
textGraphics.putString(2, height - 2, "-".repeat(width - 4))
|
||||||
inited = true
|
|
||||||
}
|
|
||||||
textGraphics.foregroundColor = TextColor.ANSI.DEFAULT
|
textGraphics.foregroundColor = TextColor.ANSI.DEFAULT
|
||||||
textGraphics.backgroundColor = TextColor.ANSI.DEFAULT
|
textGraphics.backgroundColor = TextColor.ANSI.DEFAULT
|
||||||
val leftName = getScreenName(getLeftScreenId())
|
val leftName = getScreenName(getLeftScreenId())
|
||||||
clearRows(2)
|
// clearRows(2)
|
||||||
textGraphics.putString((width - title.length) / 2 - "$leftName << ".length, 2, "$leftName << ")
|
textGraphics.putString((width - title.length) / 2 - "$leftName << ".length, 2, "$leftName << ")
|
||||||
textGraphics.foregroundColor = TextColor.ANSI.WHITE
|
textGraphics.foregroundColor = TextColor.ANSI.WHITE
|
||||||
textGraphics.backgroundColor = TextColor.ANSI.YELLOW
|
textGraphics.backgroundColor = TextColor.ANSI.YELLOW
|
||||||
@ -230,14 +285,14 @@ object MiraiConsoleUI {
|
|||||||
var currentHeight = 6
|
var currentHeight = 6
|
||||||
|
|
||||||
fun drawLog(string: String, flush: Boolean = true) {
|
fun drawLog(string: String, flush: Boolean = true) {
|
||||||
val maxHeight = terminal.terminalSize.rows - 6
|
val maxHeight = terminal.terminalSize.rows - 4
|
||||||
val heightNeed = (string.length / (terminal.terminalSize.columns - 6)) + 1
|
val heightNeed = (string.length / (terminal.terminalSize.columns - 6)) + 1
|
||||||
if (currentHeight + heightNeed > maxHeight) {
|
if (currentHeight + heightNeed > maxHeight) {
|
||||||
cleanPage()
|
cleanPage()
|
||||||
}
|
}
|
||||||
textGraphics.foregroundColor = TextColor.ANSI.GREEN
|
textGraphics.foregroundColor = TextColor.ANSI.GREEN
|
||||||
textGraphics.backgroundColor = TextColor.ANSI.DEFAULT
|
textGraphics.backgroundColor = TextColor.ANSI.DEFAULT
|
||||||
val width = terminal.terminalSize.columns - 6
|
val width = terminal.terminalSize.columns - 7
|
||||||
var x = string
|
var x = string
|
||||||
while (true) {
|
while (true) {
|
||||||
if (x == "") break
|
if (x == "") break
|
||||||
@ -264,7 +319,7 @@ object MiraiConsoleUI {
|
|||||||
|
|
||||||
|
|
||||||
fun cleanPage() {
|
fun cleanPage() {
|
||||||
for (index in 6 until terminal.terminalSize.rows - 6) {
|
for (index in 6 until terminal.terminalSize.rows - 4) {
|
||||||
clearRows(index)
|
clearRows(index)
|
||||||
}
|
}
|
||||||
currentHeight = 6
|
currentHeight = 6
|
||||||
@ -272,20 +327,21 @@ object MiraiConsoleUI {
|
|||||||
|
|
||||||
|
|
||||||
fun redrawLogs(toDraw: List<String>) {
|
fun redrawLogs(toDraw: List<String>) {
|
||||||
this.cleanPage()
|
//this.cleanPage()
|
||||||
|
currentHeight = 6
|
||||||
var logsToDraw = 0
|
var logsToDraw = 0
|
||||||
var vara = 0
|
var vara = 0
|
||||||
toDraw.reversed().forEach {
|
toDraw.forEach {
|
||||||
val heightNeed = (it.length / (terminal.terminalSize.columns - 6)) + 1
|
val heightNeed = (it.length / (terminal.terminalSize.columns - 6)) + 1
|
||||||
vara += heightNeed
|
vara += heightNeed
|
||||||
if (currentHeight + vara < terminal.terminalSize.rows - 6) {
|
if (currentHeight + vara < terminal.terminalSize.rows - 4) {
|
||||||
logsToDraw++
|
logsToDraw++
|
||||||
} else {
|
} else {
|
||||||
return
|
return@forEach
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (index in (toDraw.size - logsToDraw) until toDraw.size - 1) {
|
for (index in 0 until logsToDraw) {
|
||||||
drawLog(toDraw[index], false)
|
drawLog(toDraw[logsToDraw - index - 1], false)
|
||||||
}
|
}
|
||||||
if (terminal is SwingTerminalFrame) {
|
if (terminal is SwingTerminalFrame) {
|
||||||
terminal.flush()
|
terminal.flush()
|
||||||
@ -316,7 +372,7 @@ object MiraiConsoleUI {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun addCommandChar(
|
private fun addCommandChar(
|
||||||
c: Char
|
c: Char
|
||||||
) {
|
) {
|
||||||
if (commandBuilder.isEmpty() && c != '/') {
|
if (commandBuilder.isEmpty() && c != '/') {
|
||||||
@ -332,7 +388,7 @@ object MiraiConsoleUI {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun deleteCommandChar() {
|
private fun deleteCommandChar() {
|
||||||
if (!commandBuilder.isEmpty()) {
|
if (!commandBuilder.isEmpty()) {
|
||||||
commandBuilder = StringBuilder(commandBuilder.toString().substring(0, commandBuilder.length - 1))
|
commandBuilder = StringBuilder(commandBuilder.toString().substring(0, commandBuilder.length - 1))
|
||||||
}
|
}
|
||||||
@ -345,7 +401,7 @@ object MiraiConsoleUI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fun emptyCommand() {
|
private fun emptyCommand() {
|
||||||
commandBuilder = StringBuilder()
|
commandBuilder = StringBuilder()
|
||||||
redrawCommand()
|
redrawCommand()
|
||||||
if (terminal is SwingTerminal) {
|
if (terminal is SwingTerminal) {
|
||||||
|
@ -11,8 +11,10 @@ package net.mamoe.mirai.plugins
|
|||||||
|
|
||||||
import net.mamoe.mirai.ICommand
|
import net.mamoe.mirai.ICommand
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
|
import net.mamoe.mirai.MiraiConsole
|
||||||
import net.mamoe.mirai.utils.DefaultLogger
|
import net.mamoe.mirai.utils.DefaultLogger
|
||||||
import net.mamoe.mirai.utils.MiraiLogger
|
import net.mamoe.mirai.utils.MiraiLogger
|
||||||
|
import net.mamoe.mirai.utils.SimpleLogger
|
||||||
import net.mamoe.mirai.utils.io.encodeToString
|
import net.mamoe.mirai.utils.io.encodeToString
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
@ -156,7 +158,9 @@ object PluginManager {
|
|||||||
File(it).mkdirs()
|
File(it).mkdirs()
|
||||||
}
|
}
|
||||||
|
|
||||||
val logger = DefaultLogger("Mirai Plugin Manager")
|
val logger = SimpleLogger("Plugin Manager") { _, message, e ->
|
||||||
|
MiraiConsole.logger("[Plugin Manager]", 0, message)
|
||||||
|
}
|
||||||
|
|
||||||
//已完成加载的
|
//已完成加载的
|
||||||
private val nameToPluginBaseMap: MutableMap<String, PluginBase> = mutableMapOf()
|
private val nameToPluginBaseMap: MutableMap<String, PluginBase> = mutableMapOf()
|
||||||
|
Loading…
Reference in New Issue
Block a user