From fe5ce262e9e05ac785b08971eb65835a1d07b60d Mon Sep 17 00:00:00 2001 From: "jiahua.liu" Date: Fri, 6 Mar 2020 17:39:53 +0800 Subject: [PATCH] version loader --- .../mirai/console/wrapper/ConsoleUpdator.kt | 8 +-- .../mirai/console/wrapper/CoreUpdator.kt | 3 +- .../mirai/console/wrapper/WrapperMain.kt | 66 +++++++++++++++---- mirai-console/build.gradle.kts | 3 + .../net/mamoe/mirai/console/MiraiConsole.kt | 17 ++--- .../console/pure/MiraiConsolePureLoader.kt | 11 +++- 6 files changed, 82 insertions(+), 26 deletions(-) diff --git a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/ConsoleUpdator.kt b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/ConsoleUpdator.kt index c76647687..114504cab 100644 --- a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/ConsoleUpdator.kt +++ b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/ConsoleUpdator.kt @@ -7,6 +7,9 @@ import kotlin.math.pow import kotlin.system.exitProcess const val CONSOLE_PURE = "Pure" +const val CONSOLE_TERMINAL = "Terminal" +const val CONSOLE_GRAPHICAL = "Graphical" + object ConsoleUpdator{ @@ -77,7 +80,7 @@ object ConsoleUpdator{ } } - private fun getCurrentVersion():String{ + fun getCurrentVersion():String{ val file = getFile() if(file != null) { val numberVersion = """([0-9])*\.([0-9])*\.([0-9])*""".toRegex().find(file.name)?.value @@ -104,7 +107,4 @@ object ConsoleUpdator{ LibManager.clearLibs() LibManager.addDependencyRequest("net/mamoe",getProjectName(),version) } - - - } \ No newline at end of file diff --git a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/CoreUpdator.kt b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/CoreUpdator.kt index f2481e28d..e43965011 100644 --- a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/CoreUpdator.kt +++ b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/CoreUpdator.kt @@ -40,6 +40,7 @@ object CoreUpdator { } + suspend fun versionCheck(){ println("Fetching Newest Core Version .. ") val newest = getNewestVersion() @@ -91,7 +92,7 @@ object CoreUpdator { * 判断当前版本 * 默认返回 "0.0.0" */ - private fun getCurrentVersion(): String { + fun getCurrentVersion(): String { val file = getProtocolLib() if (file == null || getCore() == null) return "0.0.0" val numberVersion = """([0-9])*\.([0-9])*\.([0-9])*""".toRegex().find(file.name)?.value diff --git a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/WrapperMain.kt b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/WrapperMain.kt index 1ee66cd04..ee649b342 100644 --- a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/WrapperMain.kt +++ b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/WrapperMain.kt @@ -18,6 +18,7 @@ import io.ktor.utils.io.jvm.javaio.copyTo import kotlinx.coroutines.* import java.io.File import java.net.URLClassLoader +import java.util.* import kotlin.system.exitProcess @@ -33,12 +34,27 @@ object WrapperMain { @JvmStatic fun main(args: Array) { println("You are running Mirai-Console-Wrapper under " + System.getProperty("user.dir")) - println("Starting version check...") - /** - * ask for type - */ - val type = CONSOLE_PURE + var type = WrapperProperties.determineConsoleType(WrapperProperties.content) + if(type!=null){ + println("Starting Mirai Console $type, reset by clear /content/") + }else{ + println("Please select Console Type") + println("请选择 Console 版本") + println("=> Pure : pure console") + println("=> Graphical : [Not Supported Yet] graphical UI except unix") + println("=> Terminal : [Not Supported Yet] console in unix") + val scanner = Scanner(System.`in`) + while (type == null){ + var input = scanner.next() + input = input.toUpperCase()[0] + input.toLowerCase().substring(1) + println("Selecting $input") + type = WrapperProperties.determineConsoleType(input) + } + WrapperProperties.content = type + } + + println("Starting version check...") runBlocking { launch { CoreUpdator.versionCheck() @@ -59,11 +75,15 @@ object WrapperMain { ConsoleUpdator.getFile()!!, this.javaClass.classLoader ) - loader.loadClass("net.mamoe.mirai.BotFactoryJvm") - loader.loadClass( - "net.mamoe.mirai.console.pure.MiraiConsolePureLoader" - ).getMethod("main", Array(0) {"null"}.javaClass) - .invoke(null,args) + when(type) { + CONSOLE_PURE -> { + loader.loadClass("net.mamoe.mirai.BotFactoryJvm") + loader.loadClass( + "net.mamoe.mirai.console.pure.MiraiConsolePureLoader" + ).getMethod("load", String::class.java,String::class.java) + .invoke(null,CoreUpdator.getCurrentVersion(),ConsoleUpdator.getCurrentVersion()) + } + } } } @@ -76,4 +96,28 @@ class MiraiClassLoader( core.toURI().toURL(), protocol.toURI().toURL(), console.toURI().toURL() -), parent) \ No newline at end of file +), parent) + + +object WrapperProperties{ + val contentFile by lazy{ + File(contentPath.absolutePath + "/.wrapper.txt").also { + if(!it.exists())it.createNewFile() + } + } + + var content + get() = contentFile.readText() + set(value) = contentFile.writeText(value) + + + fun determineConsoleType( + type:String + ):String?{ + if(type == CONSOLE_PURE || type == CONSOLE_GRAPHICAL || type == CONSOLE_TERMINAL){ + return type + } + return null + } + +} \ No newline at end of file diff --git a/mirai-console/build.gradle.kts b/mirai-console/build.gradle.kts index 021e958f2..150eaf874 100644 --- a/mirai-console/build.gradle.kts +++ b/mirai-console/build.gradle.kts @@ -8,6 +8,9 @@ plugins { id("com.jfrog.bintray") } + +apply(plugin = "com.github.johnrengelman.shadow") + val kotlinVersion: String by rootProject.ext val atomicFuVersion: String by rootProject.ext val coroutinesVersion: String by rootProject.ext diff --git a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt index 42a55aaad..ec5e2e9bb 100644 --- a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt +++ b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt @@ -24,12 +24,10 @@ import net.mamoe.mirai.utils.cryptor.ECDH object MiraiConsole { /** - * 发布的版本号 统一修改位置 + * 发布的版本名 */ - const val version = "0.3.1" - const val coreVersion = "v0.23.0" - const val build = "Alpha" - + const val build = "Pkmon" + lateinit var version:String /** * 获取从Console登陆上的Bot, Bots @@ -61,8 +59,11 @@ object MiraiConsole { */ var start = false + fun start( - frontEnd: MiraiConsoleUI + frontEnd: MiraiConsoleUI, + coreVersion: String = "0.0.0", + consoleVersion: String = "0.0.0" ) { if (start) { return @@ -70,14 +71,14 @@ object MiraiConsole { start = true /* 初始化前端 */ + this.version = consoleVersion this.frontEnd = frontEnd - frontEnd.pushVersion(version, build, "Loading") + this.frontEnd.pushVersion(consoleVersion, build, coreVersion) logger("Mirai-console now running under $path") logger("Get news in github: https://github.com/mamoe/mirai") logger("Mirai为开源项目,请自觉遵守开源项目协议") logger("Powered by Mamoe Technologies and contributors") - /* 加载ECDH */ try { ECDH() diff --git a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsolePureLoader.kt b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsolePureLoader.kt index 407fb3353..fb2f5b648 100644 --- a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsolePureLoader.kt +++ b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsolePureLoader.kt @@ -15,8 +15,15 @@ import kotlin.concurrent.thread class MiraiConsolePureLoader { companion object { @JvmStatic - fun main(args: Array) { - MiraiConsole.start(MiraiConsoleUIPure()) + fun load( + coreVersion: String, + consoleVersion: String + ) { + MiraiConsole.start( + MiraiConsoleUIPure(), + coreVersion, + consoleVersion + ) Runtime.getRuntime().addShutdownHook(thread(start = false) { MiraiConsole.stop() })