From 3cde3e16598a2a9ab5017df0f5a9e3485f9214ec Mon Sep 17 00:00:00 2001 From: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com> Date: Sun, 10 Aug 2025 19:19:40 +0800 Subject: [PATCH] manger: simplify and Using array splitting to compare full version numbers --- .../src/main/java/com/sukisu/ultra/Natives.kt | 40 ++++++++++--------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/manager/app/src/main/java/com/sukisu/ultra/Natives.kt b/manager/app/src/main/java/com/sukisu/ultra/Natives.kt index 2511a246..4b4cbb3b 100644 --- a/manager/app/src/main/java/com/sukisu/ultra/Natives.kt +++ b/manager/app/src/main/java/com/sukisu/ultra/Natives.kt @@ -34,21 +34,29 @@ object Natives { const val ROOT_UID = 0 const val ROOT_GID = 0 + // 获取完整版本号 external fun getFullVersion(): String - fun getSimpleVersionFull(): String { - val fullVersion = getFullVersion() - val startIndex = fullVersion.indexOf('v') - if (startIndex < 0) { - return fullVersion + fun isVersionLessThan(v1Full: String, v2Full: String): Boolean { + fun extractVersionParts(version: String): List { + val match = Regex("""v\d+(\.\d+)*""").find(version) + val simpleVersion = match?.value ?: version + return simpleVersion.trimStart('v').split('.').map { it.toIntOrNull() ?: 0 } } - val endIndex = fullVersion.indexOf('-', startIndex) - val versionStr = if (endIndex > startIndex) { - fullVersion.substring(startIndex, endIndex) - } else { - fullVersion.substring(startIndex) + + val v1Parts = extractVersionParts(v1Full) + val v2Parts = extractVersionParts(v2Full) + val maxLength = maxOf(v1Parts.size, v2Parts.size) + 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 { @@ -149,14 +157,8 @@ object Natives { } fun requireNewKernel(): Boolean { - if (version < MINIMAL_SUPPORTED_KERNEL) { - return true - } - val simpleVersionFull = getSimpleVersionFull() - if (simpleVersionFull.isEmpty()) { - return false - } - return simpleVersionFull < MINIMAL_SUPPORTED_KERNEL_FULL + if (version < MINIMAL_SUPPORTED_KERNEL) return true + return isVersionLessThan(getFullVersion(), MINIMAL_SUPPORTED_KERNEL_FULL) } @Immutable