Fix version checking, add file validation

This commit is contained in:
Him188 2020-05-06 15:51:34 +08:00
parent b732b9731a
commit 8721c60493
3 changed files with 30 additions and 21 deletions

View File

@ -46,7 +46,7 @@ internal object ConsoleUpdater {
if (file.name.contains("mirai-console")) { if (file.name.contains("mirai-console")) {
when (consoleType) { when (consoleType) {
ConsoleType.Pure -> { ConsoleType.Pure -> {
if (!file.name.contains("pure")) { if (!file.name.contains("graphical")) {
return file return file
} }
} }
@ -68,7 +68,7 @@ internal object ConsoleUpdater {
suspend fun versionCheck(type: ConsoleType, strategy: VersionUpdateStrategy) { suspend fun versionCheck(type: ConsoleType, strategy: VersionUpdateStrategy) {
this.consoleType = type this.consoleType = type
println("Fetching Newest Console Version of $type") println("Fetching Newest Console Version of $type")
val current = CoreUpdater.getCurrentVersion() val current = getCurrentVersion()
if (current != "0.0.0" && strategy == VersionUpdateStrategy.KEEP) { if (current != "0.0.0" && strategy == VersionUpdateStrategy.KEEP) {
println("Stay on current version.") println("Stay on current version.")
return return
@ -97,10 +97,7 @@ internal object ConsoleUpdater {
fun getCurrentVersion(): String { fun getCurrentVersion(): String {
val file = getFile() val file = getFile()
if (file != null) { if (file != null) {
val numberVersion = """([0-9])*\.([0-9])*\.([0-9])*""".toRegex().find(file.name)?.value return file.name.substringAfter(getProjectName() + "-").substringBefore(".jar")
if (numberVersion != null) {
return numberVersion + file.name.substringAfter(numberVersion).substringBefore(".jar")
}
} }
return "0.0.0" return "0.0.0"
} }

View File

@ -44,7 +44,6 @@ internal object CoreUpdater {
getContent("mirai-core-qqandroid-jvm-$newest.jar") 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")) //.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 { fun getCurrentVersion(): String {
val file = getProtocolLib() ?: return "0.0.0" val file = getProtocolLib() ?: return "0.0.0"
val numberVersion = """([0-9])*\.([0-9])*\.([0-9])*""".toRegex().find(file.name)?.value return file.name.substringBefore(".jar").substringAfter("mirai-core-qqandroid-jvm-")
if (numberVersion != null) {
return numberVersion + file.name.substringAfter(numberVersion).substringBefore(".jar")
}
return "0.0.0"
} }

View File

@ -122,6 +122,7 @@ object WrapperCli : CliktCommand(name = "mirai-warpper") {
} }
WrapperMain.start(ConsoleType.Graphical) WrapperMain.start(ConsoleType.Graphical)
} else { } else {
WrapperMain.preStartInNonNative(console, update) WrapperMain.preStartInNonNative(console, update)
} }
@ -192,22 +193,38 @@ object WrapperMain {
} }
internal fun start(type: ConsoleType) { internal fun start(type: ConsoleType) {
val loader = MiraiClassLoader( val loader = MiraiClassLoader(
CoreUpdater.getProtocolLib()!!, CoreUpdater.getProtocolLib()!!,
ConsoleUpdater.getFile()!!, ConsoleUpdater.getFile()!!,
WrapperMain::class.java.classLoader WrapperMain::class.java.classLoader
) )
loader.loadClass("net.mamoe.mirai.BotFactoryJvm") try {
loader.loadClass( loader.loadClass("net.mamoe.mirai.BotFactoryJvm")
when (type) { } catch (e: ClassNotFoundException) {
ConsoleType.Pure -> "net.mamoe.mirai.console.pure.MiraiConsolePureLoader" System.err.println("Found mirai-core file broken, re-downloading...")
ConsoleType.Graphical -> "net.mamoe.mirai.console.graphical.MiraiConsoleGraphicalLoader" loader.close()
else -> return CoreUpdater.getProtocolLib()?.delete()
} WrapperCli.run()
).getMethod("load", String::class.java, String::class.java) return
.invoke(null, CoreUpdater.getCurrentVersion(), ConsoleUpdater.getCurrentVersion()) }
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()
}
} }
} }