Console Buildup

This commit is contained in:
jiahua.liu 2020-02-14 00:24:43 +08:00
parent 155ad75c9b
commit 1735eab972
7 changed files with 86 additions and 41 deletions

View File

@ -4,7 +4,16 @@ plugins {
id("java")
}
apply(plugin="com.github.johnrengelman.shadow")
apply(plugin = "com.github.johnrengelman.shadow")
apply(plugin = "java-library")
tasks.withType<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar>() {
manifest {
attributes["Main-Class"] = "net.mamoe.mirai.MiraiConsoleLoader"
}
}
val kotlinVersion: String by rootProject.ext
val atomicFuVersion: String by rootProject.ext

View File

@ -1,3 +1,5 @@
package net.mamoe.mirai
/*
* Copyright 2020 Mamoe Technologies and contributors.
*
@ -7,7 +9,7 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
import net.mamoe.mirai.plugin.PluginManager
import net.mamoe.mirai.plugins.PluginManager
object CommandManager {
private val registeredCommand: MutableMap<String, ICommand> = mutableMapOf()
@ -21,7 +23,7 @@ object CommandManager {
val allNames = mutableListOf(command.name).also { it.addAll(command.alias) }
allNames.forEach {
if (registeredCommand.containsKey(it)) {
error("Command Name(or Alias) $it is already registered, consider if same function plugin was installed")
error("net.mamoe.mirai.Command Name(or Alias) $it is already registered, consider if same function plugin was installed")
}
}
allNames.forEach {
@ -113,7 +115,7 @@ class CommandBuilder internal constructor() {
fun register(): ICommand {
if (name == null || onCommand == null) {
error("CommandBuilder not complete")
error("net.mamoe.mirai.CommandBuilder not complete")
}
if (alias == null) {
alias = listOf()

View File

@ -1,3 +1,5 @@
package net.mamoe.mirai
/*
* Copyright 2020 Mamoe Technologies and contributors.
*
@ -8,15 +10,15 @@
*/
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.UnstableDefault
import net.mamoe.mirai.Bot
import net.mamoe.mirai.alsoLogin
import net.mamoe.mirai.plugins.PluginManager
import net.mamoe.mirai.plugins.loadAsConfig
import net.mamoe.mirai.plugins.withDefaultWrite
import net.mamoe.mirai.plugins.withDefaultWriteSave
import net.mamoe.mirai.api.http.MiraiHttpAPIServer
import net.mamoe.mirai.api.http.generateSessionKey
import net.mamoe.mirai.contact.sendMessage
import net.mamoe.mirai.plugin.*
import java.io.File
import kotlin.concurrent.thread
import kotlin.math.log
object MiraiConsole {
val bots
@ -34,16 +36,18 @@ object MiraiConsole {
val pluginManager: PluginManager
get() = PluginManager
var logger: MiraiConsoleLogger = DefaultLogger
var logger: MiraiConsoleLogger =
DefaultLogger
var path: String = System.getProperty("user.dir")
val version = "0.13"
val version = "0.01"
var coreVersion = "0.13"
val build = "Beta"
fun start() {
logger("Mirai-console v${version} $build is still in testing stage, majority feature is available")
logger("Mirai-console v${version} $build 还处于测试阶段, 大部分功能可用")
logger("Mirai-console v$version $build | core version v$coreVersion is still in testing stage, majority feature is available")
logger("Mirai-console v$version $build | 核心版本 v${coreVersion}还处于测试阶段, 大部分功能可用")
logger()
logger("Mirai-console now running under " + System.getProperty("user.dir"))
logger("Mirai-console 正在 " + System.getProperty("user.dir") + "下运行")
@ -51,12 +55,17 @@ object MiraiConsole {
logger("Get news in github: https://github.com/mamoe/mirai")
logger("在Github中获取项目最新进展: https://github.com/mamoe/mirai")
logger("Mirai为开源项目请自觉遵守开源项目协议")
logger("Powered by Mamoe Technology")
logger("Powered by Mamoe Technologies and contributors")
logger()
DefaultCommands()
pluginManager.loadPlugins()
CommandListener.start()
runBlocking {
DefaultCommands()
HTTPAPIAdaptar()
pluginManager.loadPlugins()
CommandListener.start()
}
logger("Mirai-console 启动完成")
logger("\"/login qqnumber qqpassword \" to login a bot")
logger("\"/login qq号 qq密码 \" 来登陆一个BOT")
@ -66,6 +75,23 @@ object MiraiConsole {
PluginManager.disableAllPlugins()
}
object HTTPAPIAdaptar {
operator fun invoke() {
if (MiraiProperties.HTTP_API_ENABLE) {
if (MiraiProperties.HTTP_API_AUTH_KEY.startsWith("InitKey")) {
logger("请尽快更改初始生成的HTTP API AUTHKEY")
}
logger("正在启动HTTPAPI; 端口=" + MiraiProperties.HTTP_API_PORT)
MiraiHttpAPIServer.start(
MiraiProperties.HTTP_API_PORT,
MiraiProperties.HTTP_API_AUTH_KEY
)
logger("HTTPAPI启动完成; 端口=" + MiraiProperties.HTTP_API_PORT)
}
}
}
/**
* Defaults Commands are recommend to be replaced by plugin provided commands
*/
@ -195,11 +221,11 @@ object MiraiConsole {
name = "about"
description = "About Mirai-Console"
onCommand {
logger("v${version} $build is still in testing stage, majority feature is available")
logger("v$version $build is still in testing stage, majority feature is available")
logger("now running under " + System.getProperty("user.dir"))
logger("在Github中获取项目最新进展: https://github.com/mamoe/mirai")
logger("Mirai为开源项目请自觉遵守开源项目协议")
logger("Powered by Mamoe Technology")
logger("Powered by Mamoe Technologies and contributors")
true
}
}
@ -235,7 +261,7 @@ object MiraiConsole {
object DefaultLogger : MiraiConsoleLogger {
override fun invoke(any: Any?) {
if (any != null) {
println("[Mirai${version} $build]: " + any.toString())
println("[Mirai$version $build]: " + any.toString())
}
}
}
@ -246,18 +272,24 @@ object MiraiConsole {
var HTTP_API_ENABLE: Boolean by config.withDefaultWrite { true }
var HTTP_API_PORT: Int by config.withDefaultWrite { 8080 }
var HTTP_API_AUTH_KEY: String by config.withDefaultWriteSave {
"InitKey".also {
logger("Mirai HTTPAPI auth key 已随机生成 请注意修改")
} + generateSessionKey()
"InitKey" + generateSessionKey()
}
}
}
fun main() {
MiraiConsole.start()
Runtime.getRuntime().addShutdownHook(thread(start = false) {
MiraiConsole.stop()
})
class MiraiConsoleLoader {
companion object {
@JvmStatic
fun main(args: Array<String>) {
MiraiConsole.start()
Runtime.getRuntime().addShutdownHook(thread(start = false) {
MiraiConsole.stop()
})
}
}
}

View File

@ -7,7 +7,7 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package net.mamoe.mirai.plugin
package net.mamoe.mirai.plugins
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
@ -16,7 +16,6 @@ import com.alibaba.fastjson.parser.Feature
import kotlinx.serialization.*
import java.io.File
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentSkipListMap
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KClass
import kotlin.reflect.KProperty

View File

@ -7,13 +7,10 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package net.mamoe.mirai.plugin
package net.mamoe.mirai.plugins
import Command
import ICommand
import net.mamoe.mirai.ICommand
import kotlinx.coroutines.*
import kotlinx.serialization.UnstableDefault
import kotlinx.serialization.json.Json
import net.mamoe.mirai.utils.DefaultLogger
import net.mamoe.mirai.utils.MiraiLogger
import net.mamoe.mirai.utils.io.encodeToString
@ -166,7 +163,7 @@ object PluginManager {
private val pluginDescriptions: MutableMap<String, PluginDescription> = mutableMapOf()
fun onCommand(command: ICommand, args: List<String>) {
this.nameToPluginBaseMap.values.forEach {
nameToPluginBaseMap.values.forEach {
it.onCommand(command, args)
}
}
@ -182,7 +179,7 @@ object PluginManager {
val pluginsFound: MutableMap<String, PluginDescription> = mutableMapOf()
val pluginsLocation: MutableMap<String, File> = mutableMapOf()
logger.info("""开始加载${this.pluginsPath}下的插件""")
logger.info("""开始加载${pluginsPath}下的插件""")
File(pluginsPath).listFiles()?.forEach { file ->
if (file != null && file.extension == "jar") {
@ -262,11 +259,17 @@ object PluginManager {
try {
val pluginClass = try {
PluginClassLoader((pluginsLocation[description.name]!!), this.javaClass.classLoader)
PluginClassLoader(
(pluginsLocation[description.name]!!),
this.javaClass.classLoader
)
.loadClass(description.basePath)
} catch (e: ClassNotFoundException) {
logger.info("failed to find Main: " + description.basePath + " checking if it's kotlin's path")
PluginClassLoader((pluginsLocation[description.name]!!), this.javaClass.classLoader)
PluginClassLoader(
(pluginsLocation[description.name]!!),
this.javaClass.classLoader
)
.loadClass("${description.basePath}Kt")
}
return try {
@ -299,7 +302,7 @@ object PluginManager {
it.enable()
}
logger.info("""加载了${this.nameToPluginBaseMap.size}个插件""")
logger.info("""加载了${nameToPluginBaseMap.size}个插件""")
}

View File

@ -14,7 +14,7 @@ import kotlinx.coroutines.GlobalScope
import net.mamoe.mirai.event.events.BotOnlineEvent
import net.mamoe.mirai.event.subscribeAlways
import net.mamoe.mirai.event.subscribeMessages
import net.mamoe.mirai.plugin.PluginBase
import net.mamoe.mirai.plugins.PluginBase
import net.mamoe.mirai.utils.MiraiExperimentalAPI
class ImageSenderMain : PluginBase() {

Binary file not shown.