Skip to content
This repository has been archived by the owner on Feb 10, 2023. It is now read-only.

Commit

Permalink
Rework core/console updating, implement command args
Browse files Browse the repository at this point in the history
  • Loading branch information
Him188 committed May 6, 2020
1 parent 0b1089c commit fa8eb46
Show file tree
Hide file tree
Showing 8 changed files with 272 additions and 141 deletions.
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import org.gradle.kotlin.dsl.DependencyHandlerScope

object Versions {
object Mirai {
const val core = "0.39.1"
const val core = "1.0-RC"
const val console = "0.4.11"
const val consoleGraphical = "0.0.7"
const val consoleWrapper = "0.2.0"
const val consoleWrapper = "1.0.0"
}

object Kotlin {
Expand Down
26 changes: 24 additions & 2 deletions mirai-console-wrapper/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,35 @@ dependencies {
api(kotlin("reflect", Versions.Kotlin.stdlib))

api(kotlinx("coroutines-core", Versions.Kotlin.coroutines))
api(kotlinx("coroutines-swing",Versions.Kotlin.coroutines))
api(kotlinx("coroutines-swing", Versions.Kotlin.coroutines))

api(ktor("client-cio", Versions.Kotlin.ktor))
api(ktor("client-core", Versions.Kotlin.ktor))
api(ktor("network", Versions.Kotlin.ktor))

api("com.github.ajalt:clikt:2.6.0")

testApi(kotlin("stdlib", Versions.Kotlin.stdlib))
testApi(kotlin("test-junit5"))
}

version = Versions.Mirai.consoleWrapper

description = "Console with plugin support for mirai"
description = "Console with plugin support for mirai"


val compileKotlin: org.jetbrains.kotlin.gradle.tasks.KotlinCompile by tasks
compileKotlin.kotlinOptions {
jvmTarget = "1.8"
}
val compileTestKotlin: org.jetbrains.kotlin.gradle.tasks.KotlinCompile by tasks
compileTestKotlin.kotlinOptions {
jvmTarget = "1.8"
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
tasks.withType(JavaCompile::class.java) {
options.encoding = "UTF8"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*
* Copyright 2020 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package net.mamoe.mirai.console.wrapper

import io.ktor.client.request.get
Expand All @@ -14,92 +22,78 @@ const val CONSOLE_GRAPHICAL = "Graphical"
internal object ConsoleUpdater {

@Suppress("SpellCheckingInspection")
private object Links : HashMap<String, Map<String, String>>() {
private object Links : HashMap<ConsoleType, Map<String, String>>() {
init {
put(
CONSOLE_PURE, mapOf(
ConsoleType.Pure, mapOf(
"version" to "/net/mamoe/mirai-console/"
)
)
put(
CONSOLE_GRAPHICAL, mapOf(
ConsoleType.Graphical, mapOf(
"version" to "/net/mamoe/mirai-console-graphical/"
)
)
}
}


var consoleType = CONSOLE_PURE
var consoleType = ConsoleType.Pure

fun getFile(): File? {
contentPath.listFiles()?.forEach { file ->
if (file != null && file.extension == "jar") {
if (file.name.contains("mirai-console")) {
when (consoleType) {
CONSOLE_PURE -> {
if(!file.name.contains("graphical")) {
ConsoleType.Pure -> {
if (!file.name.contains("graphical")) {
return file
}
}
CONSOLE_GRAPHICAL -> {
if(file.name.contains("graphical")) {
ConsoleType.Graphical -> {
if (file.name.contains("graphical")) {
return file
}
}
else -> {

}
}
}
}
}
return null
}

suspend fun versionCheck(type: String) {
suspend fun versionCheck(type: ConsoleType, strategy: VersionUpdateStrategy) {
this.consoleType = type
println("Fetching Newest Console Version of $type")
val newest = getNewestVersion()
val current = getCurrentVersion()
println("Local Console-$type Version: $current | Newest Console-$type Version: $newest")
val current = CoreUpdater.getCurrentVersion()
if (current != "0.0.0" && strategy == VersionUpdateStrategy.KEEP) {
println("Stay on current version.")
return
}

val newest = getNewestVersion(
strategy,
Links[consoleType]!!["version"] ?: error("Unknown Console Type")
)
println("Local Console-$type Version: $current | Newest $strategy Console-$type Version: $newest")
if (current != newest) {
println("Updating Console-$type from V$current -> V$newest, this is a force update")
println("Updating Console-$type from V$current -> V$newest")
this.getFile()?.delete()
/**
MiraiDownloader.addTask(
"https://raw.githubusercontent.com/mamoe/mirai-repo/master/shadow/${getProjectName()}/${getProjectName()}-$newest.jar",getContent("${getProjectName()}-$newest.jar")
"https://raw.githubusercontent.com/mamoe/mirai-repo/master/shadow/${getProjectName()}/${getProjectName()}-$newest.jar",getContent("${getProjectName()}-$newest.jar")
)
*/
*/
MiraiDownloader.addTask(
"https://pan.jasonczc.cn/?/mirai/${getProjectName()}/${getProjectName()}-$newest.mp4", getContent("${getProjectName()}-$newest.jar")
"https://pan.jasonczc.cn/?/mirai/${getProjectName()}/${getProjectName()}-$newest.mp4",
getContent("${getProjectName()}-$newest.jar")
)
}
}


private suspend fun getNewestVersion(): String {
try {
return """>([0-9])*\.([0-9])*\.([0-9])*/""".toRegex().findAll(
Http.get<String> {
url {
protocol = URLProtocol.HTTPS
host = "jcenter.bintray.com"
path(Links[consoleType]!!["version"] ?: error("Unknown Console Type"))
}
}
).asSequence()
.map { it.value.drop(1).dropLast(1) }
.maxBy {
it.split('.').foldRightIndexed(0) { index: Int, s: String, acc: Int ->
acc + 100.0.pow(2 - index).toInt() * (s.toIntOrNull() ?: 0)
}
}!!
} catch (e: Exception) {
println("Failed to fetch newest Console version, please seek for help")
e.printStackTrace()
println("Failed to fetch newest Console version, please seek for help")
exitProcess(1)
}
}

fun getCurrentVersion(): String {
val file = getFile()
if (file != null) {
Expand All @@ -112,11 +106,70 @@ internal object ConsoleUpdater {
}

private fun getProjectName(): String {
return if (consoleType == CONSOLE_PURE) {
return if (consoleType == ConsoleType.Pure) {
"mirai-console"
} else {
"mirai-console-${consoleType.toLowerCase()}"
"mirai-console-${consoleType.toString().toLowerCase()}"
}
}

}


suspend fun getNewestVersion(strategy: VersionUpdateStrategy, path: String): String {
try {
return Regex("""rel="nofollow">[0-9][0-9]*(\.[0-9]*)*.*/<""", RegexOption.IGNORE_CASE).findAll(
Http.get<String> {
url {
protocol = URLProtocol.HTTPS
host = "jcenter.bintray.com"
path(path)
}
})
.asSequence()
.map { it.value.substringAfter('>').substringBefore('/') }
.toList()
.let { list ->
if (list.filter { it.startsWith("1.") }.takeIf { it.isNotEmpty() }?.all { it.contains("-") } == true) {
// 只有 1.xxx-EA 版本, 那么也将他看作是正式版
list.filter { it.startsWith("1.") }
} else when (strategy) {
VersionUpdateStrategy.KEEP,
VersionUpdateStrategy.STABLE
-> {
list.filterNot { it.contains("-") } // e.g. "-EA"
}
VersionUpdateStrategy.EA -> {
list
}
}
}
.latestVersion()
} catch (e: Exception) {
println("Failed to fetch newest Console version, please seek for help")
e.printStackTrace()
println("Failed to fetch newest Console version, please seek for help")
exitProcess(1)
}
}

internal fun List<String>.latestVersion(): String {
return sortByVersion().first()
}

internal fun List<String>.sortByVersion(): List<String> {
return sortedByDescending { version ->
version.split('.').let {
if (it.size == 2) it + "0"
else it
}.reversed().foldIndexed(0.0) { index: Int, acc: Double, s: String ->
acc + 1000.0.pow(index) * s.convertPatchVersionToWeight()
}
}
}

internal fun String.convertPatchVersionToWeight(): Double {
return this.split('-').reversed().foldIndexed(0.0) { index: Int, acc: Double, s: String ->
acc + 10.0.pow(index) * (s.toIntOrNull()?.toDouble() ?: -0.5)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@

package net.mamoe.mirai.console.wrapper

import io.ktor.client.request.get
import io.ktor.http.URLProtocol
import java.io.File
import kotlin.math.pow
import kotlin.system.exitProcess

internal object CoreUpdater {

Expand All @@ -29,13 +25,18 @@ internal object CoreUpdater {
}


suspend fun versionCheck() {
suspend fun versionCheck(strategy: VersionUpdateStrategy) {
println("Fetching Newest Core Version .. ")
val newest = getNewestVersion()
val current = getCurrentVersion()
println("Local Core Version: $current | Newest Core Version: $newest")
if (current != "0.0.0" && strategy == VersionUpdateStrategy.KEEP) {
println("Stay on current version.")
return
}

val newest = getNewestVersion(strategy, "net/mamoe/mirai-core-qqandroid/")
println("Local Core Version: $current | Newest $strategy Core Version: $newest")
if (current != newest) {
println("Updating shadowed-core from V$current -> V$newest, this is a force update")
println("Updating shadowed-core from V$current -> V$newest")
this.getProtocolLib()?.delete()
MiraiDownloader
.addTask(
Expand All @@ -47,33 +48,6 @@ internal object CoreUpdater {
}
}

/**
* 判断最新版本
* */
private suspend fun getNewestVersion(): String {
try {
return """>([0-9])*\.([0-9])*\.([0-9])*/""".toRegex().findAll(
Http.get<String> {
url {
protocol = URLProtocol.HTTPS
host = "jcenter.bintray.com"
path("net/mamoe/mirai-core-qqandroid/")
}
}).asSequence()
.map { it.value.drop(1).dropLast(1) }
.maxBy {
it.split('.').foldRightIndexed(0) { index: Int, s: String, acc: Int ->
acc + 100.0.pow(2 - index).toInt() * (s.toIntOrNull() ?: 0)
}
}!!
} catch (e: Exception) {
println("Failed to fetch newest Core version, please seek for help")
e.printStackTrace()
println("Failed to fetch newest Core version, please seek for help")
exitProcess(1)
}
}

/**
* 判断当前版本
* 默认返回 "0.0.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*
* Copyright 2020 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
@file:Suppress("EXPERIMENTAL_API_USAGE")

package net.mamoe.mirai.console.wrapper
Expand Down Expand Up @@ -137,7 +145,6 @@ internal suspend fun ByteReadChannel.saveToContent(filepath: String) {
}



internal fun getContent(filepath: String):File{
return File(contentPath, filepath)
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*
* Copyright 2020 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package net.mamoe.mirai.console.wrapper

import kotlinx.coroutines.*
Expand Down Expand Up @@ -146,6 +154,7 @@ class MiraiDownloaderProgressBarInUI(): MiraiDownloadProgressBar{
override fun ad(){
WrapperMain.uiLog("[Mirai国内镜像] 感谢崔Cloud慷慨提供更新服务器")
}

private val barLen = 20

override fun update(rate: Float, message: String) {
Expand Down
Loading

0 comments on commit fa8eb46

Please sign in to comment.