Skip to content

Commit

Permalink
fix: versioning of downloaded components
Browse files Browse the repository at this point in the history
  • Loading branch information
rushiiMachine committed Sep 12, 2024
1 parent c50dc12 commit c6e94d9
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package com.aliucord.manager.network.dto

import com.aliucord.manager.network.utils.SemVer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

Expand All @@ -14,6 +15,7 @@ data class BuildInfo(
val discordVersionCode: String,
@SerialName("versionName")
val discordVersionName: String,
@SerialName("aliucordHash")
val injectorVersion: String,

val injectorVersion: SemVer,
val patchesVersion: SemVer,
)
31 changes: 25 additions & 6 deletions app/src/main/kotlin/com/aliucord/manager/network/utils/SemVer.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package com.aliucord.manager.network.utils

import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

/**
* Parses a Semantic version in the format of `v1.0.0` (v[major].[minor].[patch])
*/
@Serializable(SemVer.Serializer::class)
data class SemVer(
val major: Int,
val minor: Int,
Expand Down Expand Up @@ -43,18 +51,29 @@ data class SemVer(
}

companion object {
fun parse(version: String, vPrefix: Boolean = false): SemVer? {
if (vPrefix && version.getOrNull(0) != 'v')
return null
fun parse(version: String): SemVer = parseOrNull(version)
?: throw IllegalArgumentException("Invalid semver string $version")

val str = if (vPrefix) version.substring(1) else version
val parts = str
fun parseOrNull(version: String): SemVer? {
val versionStr = version.removePrefix("v")
val parts = versionStr
.split(".")
.mapNotNull { it.toIntOrNull() }
.takeIf { it.size == 3 }
?: return null

return SemVer(parts[0], parts[1], parts[2])
return SemVer(parts[0], parts[1], parts[2], version[0] == 'v')
}
}

object Serializer : KSerializer<SemVer> {
override val descriptor = PrimitiveSerialDescriptor("SemVer", PrimitiveKind.STRING)

override fun deserialize(decoder: Decoder) =
parse(decoder.decodeString())

override fun serialize(encoder: Encoder, value: SemVer) {
encoder.encodeString(value.toString())
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.aliucord.manager.patcher

import com.aliucord.manager.network.utils.SemVer
import com.aliucord.manager.ui.screens.patchopts.PatchOptions
import kotlinx.serialization.Serializable

Expand All @@ -14,22 +15,22 @@ data class InstallMetadata(
val options: PatchOptions,

/**
* The semver version of this manager that performed the installation.
* Whether the manager is a real release or built from source.
*/
val managerVersionName: String,
val customManager: Boolean,

/**
* Whether the manager is a real release or built from source.
* The semver version of this manager that performed the installation.
*/
val customManager: Boolean,
val managerVersion: SemVer,

/**
* Version of the Aliucord release build that was injected into the APK.
* Version (commit hash) of the Aliuhook build that was injected into the APK.
*/
val aliuhookVersion: String,

/**
* Version of the injector build that was injected into the APK.
*/
val injectorVersion: String,
val injectorVersion: SemVer,
)
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class DownloadInjectorStep : DownloadStep(), IDexProvider, KoinComponent {

override suspend fun execute(container: StepRunner) {
targetVersion = container.getStep<FetchInfoStep>()
.data.injectorVersion
.data.injectorVersion.toString()

super.execute(container)
}
Expand All @@ -45,6 +45,6 @@ class DownloadInjectorStep : DownloadStep(), IDexProvider, KoinComponent {
}

override val dexCount = 1
override val dexPriority = 10
override val dexPriority = 3
override fun getDexFiles() = listOf(targetFile.readBytes())
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.aliucord.manager.patcher.steps.patch

import com.aliucord.manager.BuildConfig
import com.aliucord.manager.R
import com.aliucord.manager.network.utils.SemVer
import com.aliucord.manager.patcher.InstallMetadata
import com.aliucord.manager.patcher.StepRunner
import com.aliucord.manager.patcher.steps.StepGroup
Expand Down Expand Up @@ -33,10 +34,10 @@ class SaveMetadataStep(private val options: PatchOptions) : Step() {

val metadata = InstallMetadata(
options = options,
managerVersionName = BuildConfig.VERSION_NAME,
customManager = IS_CUSTOM_BUILD,
managerVersion = SemVer.parse(BuildConfig.VERSION_NAME),
aliuhookVersion = aliuhook.targetVersion,
injectorVersion = injector.targetVersion,
injectorVersion = SemVer.parse(injector.targetVersion),
)

ZipWriter(apk, /* append = */ true).use {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.aliucord.manager.ui.screens.patching

import android.annotation.SuppressLint
import android.app.Application
import android.content.Intent
import android.os.Build
import android.util.Log
import androidx.annotation.StringRes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class UpdaterViewModel(
viewModelScope.launch(Dispatchers.IO) {
val (version, asset) = github.getManagerReleases().getOrNull()
?.mapNotNull { release ->
val version = SemVer.parse(release.tagName, vPrefix = true)
val version = SemVer.parseOrNull(release.tagName)
?: return@mapNotNull null

val asset = release.assets.find { it.name == "aliucord-manager-${release.tagName}.apk" }
Expand All @@ -73,7 +73,7 @@ class UpdaterViewModel(
?.maxByOrNull { it.first }
?: return@launch

val currentVersion = SemVer.parse(BuildConfig.VERSION_NAME)
val currentVersion = SemVer.parseOrNull(BuildConfig.VERSION_NAME)
?: throw Error("Failed to parse app version")

if (currentVersion >= version)
Expand Down

0 comments on commit c6e94d9

Please sign in to comment.