From 8721c604939973c1797e82163bda451f1c2f88e8 Mon Sep 17 00:00:00 2001 From: Him188 Date: Wed, 6 May 2020 15:51:34 +0800 Subject: [PATCH] Fix version checking, add file validation --- .../mirai/console/wrapper/ConsoleUpdater.kt | 9 ++--- .../mirai/console/wrapper/CoreUpdater.kt | 7 +--- .../mirai/console/wrapper/WrapperMain.kt | 35 ++++++++++++++----- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/ConsoleUpdater.kt b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/ConsoleUpdater.kt index edadf26a5..0223b26cb 100644 --- a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/ConsoleUpdater.kt +++ b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/ConsoleUpdater.kt @@ -46,7 +46,7 @@ internal object ConsoleUpdater { if (file.name.contains("mirai-console")) { when (consoleType) { ConsoleType.Pure -> { - if (!file.name.contains("pure")) { + if (!file.name.contains("graphical")) { return file } } @@ -68,7 +68,7 @@ internal object ConsoleUpdater { suspend fun versionCheck(type: ConsoleType, strategy: VersionUpdateStrategy) { this.consoleType = type println("Fetching Newest Console Version of $type") - val current = CoreUpdater.getCurrentVersion() + val current = getCurrentVersion() if (current != "0.0.0" && strategy == VersionUpdateStrategy.KEEP) { println("Stay on current version.") return @@ -97,10 +97,7 @@ internal object ConsoleUpdater { fun getCurrentVersion(): String { val file = getFile() if (file != null) { - val numberVersion = """([0-9])*\.([0-9])*\.([0-9])*""".toRegex().find(file.name)?.value - if (numberVersion != null) { - return numberVersion + file.name.substringAfter(numberVersion).substringBefore(".jar") - } + return file.name.substringAfter(getProjectName() + "-").substringBefore(".jar") } return "0.0.0" } diff --git a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/CoreUpdater.kt b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/CoreUpdater.kt index e9a1ac92c..e6247c5cb 100644 --- a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/CoreUpdater.kt +++ b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/CoreUpdater.kt @@ -44,7 +44,6 @@ internal object CoreUpdater { getContent("mirai-core-qqandroid-jvm-$newest.jar") ) //.addTask("https://raw.githubusercontent.com/mamoe/mirai-repo/master/shadow/mirai-core-qqandroid/mirai-core-qqandroid-$newest.jar", getContent("mirai-core-qqandroid-jvm-$newest.jar")) - } } @@ -54,11 +53,7 @@ internal object CoreUpdater { */ fun getCurrentVersion(): String { val file = getProtocolLib() ?: return "0.0.0" - val numberVersion = """([0-9])*\.([0-9])*\.([0-9])*""".toRegex().find(file.name)?.value - if (numberVersion != null) { - return numberVersion + file.name.substringAfter(numberVersion).substringBefore(".jar") - } - return "0.0.0" + return file.name.substringBefore(".jar").substringAfter("mirai-core-qqandroid-jvm-") } 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 1ae00ffdb..297eb3e06 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 @@ -122,6 +122,7 @@ object WrapperCli : CliktCommand(name = "mirai-warpper") { } WrapperMain.start(ConsoleType.Graphical) + } else { WrapperMain.preStartInNonNative(console, update) } @@ -192,22 +193,38 @@ object WrapperMain { } internal fun start(type: ConsoleType) { + val loader = MiraiClassLoader( CoreUpdater.getProtocolLib()!!, ConsoleUpdater.getFile()!!, WrapperMain::class.java.classLoader ) - loader.loadClass("net.mamoe.mirai.BotFactoryJvm") - loader.loadClass( - when (type) { - ConsoleType.Pure -> "net.mamoe.mirai.console.pure.MiraiConsolePureLoader" - ConsoleType.Graphical -> "net.mamoe.mirai.console.graphical.MiraiConsoleGraphicalLoader" - else -> return - } - ).getMethod("load", String::class.java, String::class.java) - .invoke(null, CoreUpdater.getCurrentVersion(), ConsoleUpdater.getCurrentVersion()) + try { + loader.loadClass("net.mamoe.mirai.BotFactoryJvm") + } catch (e: ClassNotFoundException) { + System.err.println("Found mirai-core file broken, re-downloading...") + loader.close() + CoreUpdater.getProtocolLib()?.delete() + WrapperCli.run() + return + } + try { + loader.loadClass( + when (type) { + ConsoleType.Pure -> "net.mamoe.mirai.console.pure.MiraiConsolePureLoader" + ConsoleType.Graphical -> "net.mamoe.mirai.console.graphical.MiraiConsoleGraphicalLoader" + else -> return + } + ).getMethod("load", String::class.java, String::class.java) + .invoke(null, CoreUpdater.getCurrentVersion(), ConsoleUpdater.getCurrentVersion()) + } catch (e: ClassNotFoundException) { + System.err.println("Found mirai-console file broken, re-downloading...") + loader.close() + ConsoleUpdater.getFile()?.delete() + WrapperCli.run() + } } }