Skip to content

Commit

Permalink
fixup! C: AutoUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
way-zer committed Sep 8, 2024
1 parent e844bf7 commit b819d14
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions core/src/mindustryX/features/AutoUpdate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ import mindustry.ui.Bar
import mindustry.ui.dialogs.BaseDialog
import mindustryX.VarsX
import mindustryX.features.ui.Format
import kotlin.system.exitProcess

object AutoUpdate {
data class Release(val tag: String, val version: String, val json: Jval) {
data class Asset(val name: String, val url: String)

fun matchCurrent(): Boolean {
return tag == "$currentBranch-build" || json.getString("body", "").contains("REPLACE $currentBranch")
}

fun findAsset(): Asset? {
val assets = json.get("assets").asArray().asIterable()
.map { Asset(it.getString("name"), it.getString("browser_download_url", "")) }
Expand All @@ -44,7 +47,8 @@ object AutoUpdate {
val repo = "TinyLake/MindustryX-work"
var versions = emptyList<Release>()
val currentBranch get() = Version.mdtXBuild.split('-', limit = 2).getOrNull(1)
var newVersion: Release? = null
var latest: Release? = null
val newVersion: Release? get() = latest?.takeIf { it.version > Version.mdtXBuild }

fun checkUpdate() {
if (versions.isNotEmpty()) return
Expand All @@ -61,22 +65,25 @@ object AutoUpdate {
}

private fun fetchSuccess() {
val new = versions.find { it.tag == "$currentBranch-build" && it.version > Version.mdtXBuild } ?: return
newVersion = new
if (Core.settings.getBool("showUpdateDialog", true)) {
if (Vars.clientLoaded) return showDialog()
val available = versions.filter { it.matchCurrent() }
latest = available.maxByOrNull { it.version } ?: return
newVersion?.takeIf { Core.settings.getBool("showUpdateDialog", true) }?.let {
if (Vars.clientLoaded) return showDialog(newVersion)
Events.on(EventType.ClientLoadEvent::class.java) {
Vars.ui.showConfirm("检测到新版MindustryX!\n打开更新列表?", ::showDialog)
}
}
}

@JvmOverloads
fun showDialog(version: Release? = newVersion) {
fun showDialog(version: Release? = latest) {
checkUpdate()
val dialog = BaseDialog("自动更新")
dialog.cont.apply {
add("当前版本号: ${Version.mdtXBuild}").labelAlign(Align.center).width(500f).row()
newVersion?.let {
add("新版本: ${it.version}").labelAlign(Align.center).width(500f).row()
}
if (versions.isEmpty()) {
add("检查更新失败,请稍后再试").row()
return@apply
Expand Down

0 comments on commit b819d14

Please sign in to comment.