simple download

This commit is contained in:
jiahua.liu 2020-03-06 13:52:44 +08:00
parent 97ab8b4472
commit 24e7bf41a9
3 changed files with 20 additions and 48 deletions

View File

@ -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"

View File

@ -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

View File

@ -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()
}
}