|
| 1 | +package dev.schlaubi.mastermind.core |
| 2 | + |
| 3 | +import androidx.compose.foundation.layout.Arrangement |
| 4 | +import androidx.compose.foundation.layout.Column |
| 5 | +import androidx.compose.foundation.layout.fillMaxSize |
| 6 | +import androidx.compose.foundation.layout.size |
| 7 | +import androidx.compose.material.icons.Icons |
| 8 | +import androidx.compose.material.icons.filled.Download |
| 9 | +import androidx.compose.material3.* |
| 10 | +import androidx.compose.runtime.* |
| 11 | +import androidx.compose.ui.Alignment |
| 12 | +import androidx.compose.ui.Modifier |
| 13 | +import androidx.compose.ui.unit.dp |
| 14 | +import androidx.compose.ui.window.ApplicationScope |
| 15 | +import androidx.compose.ui.window.Window |
| 16 | +import dev.schlaubi.mastermind.BuildConfig |
| 17 | +import dev.schlaubi.mastermind.theme.AppTheme |
| 18 | +import dev.schlaubi.mastermind.util.Loom |
| 19 | +import dev.schlaubi.mastermind.windows_helper.WindowsAPI |
| 20 | +import io.ktor.client.* |
| 21 | +import io.ktor.client.call.* |
| 22 | +import io.ktor.client.plugins.contentnegotiation.* |
| 23 | +import io.ktor.client.request.* |
| 24 | +import io.ktor.client.statement.* |
| 25 | +import io.ktor.http.* |
| 26 | +import io.ktor.serialization.kotlinx.json.* |
| 27 | +import io.ktor.utils.io.* |
| 28 | +import io.ktor.utils.io.core.remaining |
| 29 | +import kotlinx.coroutines.Dispatchers |
| 30 | +import kotlinx.coroutines.cancel |
| 31 | +import kotlinx.coroutines.launch |
| 32 | +import kotlinx.io.readByteArray |
| 33 | +import kotlinx.serialization.SerialName |
| 34 | +import kotlinx.serialization.Serializable |
| 35 | +import kotlinx.serialization.json.Json |
| 36 | +import java.nio.ByteBuffer |
| 37 | +import java.nio.channels.FileChannel |
| 38 | +import java.nio.file.StandardOpenOption |
| 39 | +import kotlin.io.path.absolutePathString |
| 40 | +import kotlin.io.path.createTempFile |
| 41 | +import kotlin.io.use |
| 42 | + |
| 43 | +private val client = HttpClient { |
| 44 | + install(ContentNegotiation) { |
| 45 | + val json = Json { |
| 46 | + ignoreUnknownKeys = true |
| 47 | + } |
| 48 | + |
| 49 | + json(json) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +@Serializable |
| 54 | +data class Release( |
| 55 | + val name: String, |
| 56 | + val assets: List<Asset> |
| 57 | +) { |
| 58 | + @Serializable |
| 59 | + data class Asset(@SerialName("browser_download_url") val url: String, val name: String) |
| 60 | +} |
| 61 | + |
| 62 | +data class Version(val major: Int, val minor: Int, val patch: Int) : Comparable<Version> { |
| 63 | + |
| 64 | + override fun compareTo(other: Version): Int = |
| 65 | + compareValuesBy(this, other, Version::major, Version::minor, Version::patch) |
| 66 | + |
| 67 | + companion object { |
| 68 | + fun parse(version: String): Version { |
| 69 | + val split = version.split(".") |
| 70 | + if (split.size != 3) return Version(-1, -1, -1) |
| 71 | + return Version(split[0].toInt(), split[1].toInt(), split[2].toInt()) |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +private suspend fun retrieveLatestVersion() = client |
| 77 | + .get("https://api.github.com/repos/MGP6775/killscript/releases/latest").body<Release>() |
| 78 | + |
| 79 | +private sealed interface State { |
| 80 | + interface HasRelease { |
| 81 | + val release: Release |
| 82 | + } |
| 83 | + |
| 84 | + object Checking : State |
| 85 | + object Closed : State |
| 86 | + data class UpdateAvailable(override val release: Release) : State, HasRelease |
| 87 | + data class Downloading( |
| 88 | + override val release: Release, |
| 89 | + val downloaded: Long = 0L, |
| 90 | + val size: Long? = null, |
| 91 | + val done: Boolean = false |
| 92 | + ) : State, HasRelease |
| 93 | +} |
| 94 | + |
| 95 | +@Composable |
| 96 | +fun ApplicationScope.Updater() { |
| 97 | + var state by remember { mutableStateOf<State>(State.Checking) } |
| 98 | + if (state is State.Closed) return |
| 99 | + |
| 100 | + LaunchedEffect(Unit) { |
| 101 | + val latest = retrieveLatestVersion() |
| 102 | + if (Version.parse(latest.name) > Version.parse(BuildConfig.APP_VERSION)) { |
| 103 | + state = State.UpdateAvailable(latest) |
| 104 | + } else { |
| 105 | + client.close() |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + val currentState = state |
| 111 | + if (currentState is State.HasRelease) { |
| 112 | + Window({ state = State.Closed }, title = "GTAKiller - Update") { |
| 113 | + AppTheme { |
| 114 | + Scaffold { |
| 115 | + Column( |
| 116 | + verticalArrangement = Arrangement.spacedBy(10.dp, Alignment.CenterVertically), |
| 117 | + horizontalAlignment = Alignment.CenterHorizontally, |
| 118 | + modifier = Modifier.fillMaxSize() |
| 119 | + ) { |
| 120 | + Text("A new Version is available: ${currentState.release.name}") |
| 121 | + |
| 122 | + if (currentState is State.UpdateAvailable) { |
| 123 | + Button({ state = State.Downloading(currentState.release) }) { |
| 124 | + Icon( |
| 125 | + Icons.Default.Download, |
| 126 | + "Download", |
| 127 | + modifier = Modifier.size(ButtonDefaults.IconSize) |
| 128 | + ) |
| 129 | + Text("Download now") |
| 130 | + } |
| 131 | + } else if (currentState is State.Downloading) { |
| 132 | + val scope = rememberCoroutineScope() |
| 133 | + DisposableEffect(currentState.release) { |
| 134 | + val asset = currentState.release.assets.first { it.name == "client.msi" } |
| 135 | + |
| 136 | + scope.launch(Dispatchers.Loom) { |
| 137 | + val file = createTempFile("gtakiller_update", ".msi") |
| 138 | + val fileChannel = |
| 139 | + FileChannel.open(file, StandardOpenOption.CREATE, StandardOpenOption.WRITE) |
| 140 | + |
| 141 | + client.prepareGet(asset.url) { |
| 142 | + contentType(ContentType.Application.OctetStream) |
| 143 | + }.execute { response -> |
| 144 | + val input = response.bodyAsChannel() |
| 145 | + val size = response.contentLength() |
| 146 | + state = currentState.copy(size = size) |
| 147 | + |
| 148 | + fileChannel.use { output -> |
| 149 | + var downloaded = 0L |
| 150 | + while (!input.isClosedForRead) { |
| 151 | + val packet = input.readRemaining(DEFAULT_BUFFER_SIZE.toLong()) |
| 152 | + while (!packet.exhausted()) { |
| 153 | + downloaded += packet.remaining |
| 154 | + state = currentState.copy(downloaded = downloaded, size = size) |
| 155 | + val bytes = packet.readByteArray() |
| 156 | + |
| 157 | + output.write(ByteBuffer.wrap(bytes)) |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + state = currentState.copy(done = true) |
| 163 | + val exitCode = |
| 164 | + WindowsAPI.spawnElevatedProcess( |
| 165 | + "msiexec", |
| 166 | + "/i", |
| 167 | + file.absolutePathString(), |
| 168 | + "/passive", |
| 169 | + "/quiet" |
| 170 | + ) |
| 171 | + if (exitCode != 0) { |
| 172 | + error("msiexec returned with exit code $exitCode") |
| 173 | + } |
| 174 | + |
| 175 | + val myBinary = ProcessHandle.current().info().command().get() |
| 176 | + WindowsAPI.spawnDetachedProcess(myBinary) |
| 177 | + |
| 178 | + exitApplication() |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + onDispose { |
| 183 | + scope.cancel() |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + if (currentState.size == null || currentState.done) { |
| 188 | + LinearProgressIndicator() |
| 189 | + } else { |
| 190 | + LinearProgressIndicator({ currentState.downloaded / currentState.size.toFloat() }) |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments