manger: simplify and Using array splitting to compare full version numbers

This commit is contained in:
ShirkNeko
2025-08-10 19:19:40 +08:00
parent 8dcc3f7c46
commit 3cde3e1659

View File

@@ -34,21 +34,29 @@ object Natives {
const val ROOT_UID = 0 const val ROOT_UID = 0
const val ROOT_GID = 0 const val ROOT_GID = 0
// 获取完整版本号
external fun getFullVersion(): String external fun getFullVersion(): String
fun getSimpleVersionFull(): String { fun isVersionLessThan(v1Full: String, v2Full: String): Boolean {
val fullVersion = getFullVersion() fun extractVersionParts(version: String): List<Int> {
val startIndex = fullVersion.indexOf('v') val match = Regex("""v\d+(\.\d+)*""").find(version)
if (startIndex < 0) { val simpleVersion = match?.value ?: version
return fullVersion return simpleVersion.trimStart('v').split('.').map { it.toIntOrNull() ?: 0 }
} }
val endIndex = fullVersion.indexOf('-', startIndex)
val versionStr = if (endIndex > startIndex) { val v1Parts = extractVersionParts(v1Full)
fullVersion.substring(startIndex, endIndex) val v2Parts = extractVersionParts(v2Full)
} else { val maxLength = maxOf(v1Parts.size, v2Parts.size)
fullVersion.substring(startIndex) for (i in 0 until maxLength) {
val num1 = v1Parts.getOrElse(i) { 0 }
val num2 = v2Parts.getOrElse(i) { 0 }
if (num1 != num2) return num1 < num2
} }
return "v" + (Regex("""\d+(\.\d+)*""").find(versionStr)?.value ?: versionStr) return false
}
fun getSimpleVersionFull(): String = getFullVersion().let { version ->
Regex("""v\d+(\.\d+)*""").find(version)?.value ?: version
} }
init { init {
@@ -149,14 +157,8 @@ object Natives {
} }
fun requireNewKernel(): Boolean { fun requireNewKernel(): Boolean {
if (version < MINIMAL_SUPPORTED_KERNEL) { if (version < MINIMAL_SUPPORTED_KERNEL) return true
return true return isVersionLessThan(getFullVersion(), MINIMAL_SUPPORTED_KERNEL_FULL)
}
val simpleVersionFull = getSimpleVersionFull()
if (simpleVersionFull.isEmpty()) {
return false
}
return simpleVersionFull < MINIMAL_SUPPORTED_KERNEL_FULL
} }
@Immutable @Immutable