Skip to content

Commit

Permalink
Fixed the auto-update plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnemotechnician committed Apr 5, 2023
1 parent f8d4f3c commit 34ee297
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 14 deletions.
2 changes: 1 addition & 1 deletion minchat-client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
kotlin("plugin.serialization") version "1.8.0"
}

val jarName = "minchat"
val jarName = "minchat-client"
val mindustryVersion: String by rootProject
val ktorVersion: String by rootProject

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class MinchatGithubClient {
val name: String,
@SerialName("tag_name")
val tag: String,
@SerialName("body")
val description: String,
val assets: List<GithubAsset>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,30 @@ import arc.Core
import arc.graphics.Color
import arc.util.Log
import com.github.mnemotechnician.mkui.extensions.dsl.*
import io.ktor.client.call.*
import io.ktor.client.plugins.*
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.utils.io.core.*
import io.minchat.client.Minchat
import io.minchat.client.misc.userReadable
import io.minchat.client.plugin.MinchatPlugin
import io.minchat.client.ui.ModalDialog
import io.minchat.common.*
import kotlinx.coroutines.*
import mindustry.Vars
import java.io.RandomAccessFile
import io.minchat.client.misc.MinchatStyle as Style

class AutoupdatePlugin : MinchatPlugin("autoupdater") {
val maxAttempts = 3
val httpClient = HttpClient(CIO) {
expectSuccess = true
engine {
requestTimeout = 0 // Disabled
}
}

override suspend fun onLoad() {
lateinit var latestVersion: BuildVersion
Expand Down Expand Up @@ -53,7 +63,7 @@ class AutoupdatePlugin : MinchatPlugin("autoupdater") {
job = Minchat.launch {
runCatching {
// Firstly, determine where the mod is located
val file = Vars.mods.getMod(Minchat.javaClass)?.file
val file = Vars.mods.getMod(Minchat.javaClass)?.file?.file()
?: error("Cannot locate the MinChat mod file.")
if (file.isDirectory) error("MinChat mod file is a directory. This is not supported.")

Expand All @@ -73,15 +83,38 @@ class AutoupdatePlugin : MinchatPlugin("autoupdater") {
val modAsset = latestRelease?.assets?.find { it.name.startsWith("minchat-client", true) }
?: error("Cannot locate a suitable mod file. Please, try again later.")

// Download the asset and overwrite the mod file
Minchat.githubClient.httpClient.get(modAsset.downloadUrl) {
onDownload { received, length ->
Vars.ui.loadfrag.setProgress(received.toFloat() / length)
val tmpFilePath = file.resolveSibling("minchat-download-tmp")
RandomAccessFile(tmpFilePath, "rw").use { tmpFile ->
Minchat.gateway.disconnect()
Minchat.client.account = null

// Download the asset and overwrite the mod file
httpClient.prepareGet(modAsset.downloadUrl).execute {
val length = it.contentLength() ?: error("what???")
tmpFile.setLength(length)
val channel = it.bodyAsChannel()
var read = 0L

while (!channel.isClosedForRead) {
val packet = channel.readRemaining(DEFAULT_BUFFER_SIZE.toLong())
while (!packet.isEmpty) {
val bytes = packet.readBytes()
tmpFile.write(bytes)

read += bytes.size
setProgress(read.toFloat() / length)
}
}
}
}.body<ByteArray>().let { file.writeBytes(it) }

RestartPromptDialog().show()
hide()
RestartPromptDialog().show()
}
// After this, new MinChat classes can no longer be loaded.
tmpFilePath.copyTo(file, overwrite = true)
tmpFilePath.delete()
}.onFailure { exception ->
hide()
if (exception is CancellationException) return@onFailure

SimpleInfoDialog("""
Expand All @@ -90,7 +123,6 @@ class AutoupdatePlugin : MinchatPlugin("autoupdater") {
Reason: ${exception.userReadable()}
""".trimIndent()).show()
}
Vars.ui.loadfrag.hide()
}
}
}
Expand Down Expand Up @@ -163,7 +195,7 @@ class AutoupdatePlugin : MinchatPlugin("autoupdater") {
.pad(Style.layoutPad)

update {
if (scene != null && System.currentTimeMillis() - begin >= 5) {
if (scene != null && System.currentTimeMillis() - begin >= 5000) {
Core.app.exit()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import kotlinx.serialization.Serializable
val MINCHAT_VERSION = BuildVersion(
major = 0,
minor = 3,
patch = 1
patch = 0
)

/**
Expand Down
2 changes: 1 addition & 1 deletion remote/latest-stable.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"major": 0,
"minor": 3,
"patch": 1
"patch": 2
}

0 comments on commit 34ee297

Please sign in to comment.