From 24e7bf41a9b8b08048a8b26e748f1a4067ff9709 Mon Sep 17 00:00:00 2001 From: "jiahua.liu" <n@mamoe.net> Date: Fri, 6 Mar 2020 13:52:44 +0800 Subject: [PATCH] simple download --- .../mirai/console/wrapper/ConsoleUpdator.kt | 9 +--- .../mirai/console/wrapper/CoreUpdator.kt | 48 +++++-------------- .../mamoe/mirai/console/wrapper/Downloader.kt | 11 +++-- 3 files changed, 20 insertions(+), 48 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 72204655d..fe7126452 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 @@ -1,12 +1,7 @@ package net.mamoe.mirai.console.wrapper import io.ktor.client.request.get -import io.ktor.client.statement.HttpResponse import io.ktor.http.URLProtocol -import io.ktor.utils.io.ByteReadChannel -import io.ktor.utils.io.jvm.javaio.copyTo -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext import java.io.File import kotlin.math.pow import kotlin.system.exitProcess @@ -95,7 +90,7 @@ object ConsoleUpdator{ } private suspend fun downloadConsole(version:String){ - tryNTimesOrQuit(3) { + tryNTimesOrQuit(3,"Failed to download Console, please seek for help") { kotlin.runCatching { println("Downloading newest Console from Aliyun") Http.downloadRequest(Links[consoleType]!!["aliyun"] ?: error("Unknown Console Type"), version) @@ -103,7 +98,7 @@ object ConsoleUpdator{ println("Downloading newest Console from JCenter") Http.downloadRequest(Links[consoleType]!!["jcenter"] ?: error("Unknown Console Type"), version) } - .saveTo(if (consoleType == CONSOLE_PURE) { + .saveToContent(if (consoleType == CONSOLE_PURE) { "mirai-console-$version.jar" } else { "mirai-console-$consoleType-$version.jar" 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 e999d81e2..1fa69a5f4 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 @@ -12,10 +12,7 @@ package net.mamoe.mirai.console.wrapper import io.ktor.client.request.get -import io.ktor.client.statement.HttpResponse import io.ktor.http.URLProtocol -import io.ktor.utils.io.ByteReadChannel -import io.ktor.utils.io.jvm.javaio.copyTo import kotlinx.coroutines.* import java.io.File import java.net.URLClassLoader @@ -125,50 +122,28 @@ object CoreUpdator { } private suspend fun downloadCoreAndLib(version: String) { - suspend fun downloadRequest(url: String, version: String): ByteReadChannel { - return Http.get<HttpResponse>(url.replace("{version}", version)).content - } - coroutineScope { launch { - tryNTimesOrQuit(3) { + tryNTimesOrQuit(3,"Failed to download newest Protocol lib, please seek for help") { kotlin.runCatching { println("Downloading newest Protocol lib from Aliyun") - downloadRequest(Links.libAliyun, version) + Http.downloadRequest(Links.libAliyun, version) }.getOrElse { println("Downloading newest Protocol lib from JCenter") - downloadRequest(Links.libJcenter, version) - }.saveTo("mirai-core-qqandroid-jvm-$version.jar") + Http.downloadRequest(Links.libJcenter, version) + }.saveToContent("mirai-core-qqandroid-jvm-$version.jar") } } launch { - tryNTimesOrQuit(3) { - val fileStream = File(contentPath.absolutePath + "/" + "mirai-core-jvm-$version.jar").also { - withContext(Dispatchers.IO) { - it.createNewFile() - } - }.outputStream() - - val stream = try { + tryNTimesOrQuit(3,"Failed to download newest core, please seek for help") { + kotlin.runCatching { println("Downloading newest Mirai Core from Aliyun") - downloadRequest(Links.coreAliyun, version) - } catch (ignored: Exception) { - try { - println("Downloading newest Mirai Core from JCenter") - downloadRequest(Links.coreJcenter, version) - } catch (e: Exception) { - println("Failed to download Mirai Core, please seeking for help") - e.printStackTrace() - println("Failed to download Mirai Core, please seeking for help") - exitProcess(1) - } - } - - withContext(Dispatchers.IO) { - stream.copyTo(fileStream) - fileStream.flush() - } + Http.downloadRequest(Links.coreAliyun, version) + }.getOrElse { + println("Downloading newest Mirai Core from JCenter") + Http.downloadRequest(Links.coreJcenter, version) + }.saveToContent("mirai-core-jvm-$version.jar") } } } @@ -184,7 +159,6 @@ object CoreUpdator { println("Core: $coreFile") println("Protocol: $protocolFile") - val classloader = URLClassLoader( arrayOf(coreFile.toURI().toURL(), protocolFile.toURI().toURL()), this.javaClass.classLoader diff --git a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/Downloader.kt b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/Downloader.kt index d5877d77e..4f03bcb41 100644 --- a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/Downloader.kt +++ b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/Downloader.kt @@ -17,7 +17,7 @@ val Http: HttpClient get() = HttpClient(CIO) -inline fun <R> tryNTimesOrQuit(repeat: Int, block: (Int) -> R){ +inline fun <R> tryNTimesOrQuit(repeat: Int, errorHint: String, block: (Int) -> R){ var lastException: Throwable? = null repeat(repeat) { @@ -30,8 +30,9 @@ inline fun <R> tryNTimesOrQuit(repeat: Int, block: (Int) -> R){ } else lastException!!.addSuppressed(e) } } - + println(errorHint) lastException!!.printStackTrace() + println(errorHint) exitProcess(1) } @@ -43,7 +44,7 @@ suspend inline fun HttpClient.downloadRequest(url: String, version: String): Byt /** * 只要填content path后面的就可以 */ -suspend fun ByteReadChannel.saveTo(filepath:String){ +suspend fun ByteReadChannel.saveToContent(filepath:String){ val fileStream = File(contentPath.absolutePath + "/" + filepath).also { withContext(Dispatchers.IO) { it.createNewFile() @@ -51,8 +52,10 @@ suspend fun ByteReadChannel.saveTo(filepath:String){ }.outputStream() withContext(Dispatchers.IO) { - this@saveTo.copyTo(fileStream) + this@saveToContent.copyTo(fileStream) fileStream.flush() } } + +