aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-01-09 22:04:38 +0100
committerLinnea Gräf <nea@nea.moe>2024-01-09 22:04:38 +0100
commit75b545a727d91df8a428fa92539d06617cd0e207 (patch)
tree7e214310556e8ef7e20780615dd78d419700acfd
parent89f3c177ab39340c4eb8b56e8609a6a7f49fdc90 (diff)
downloadneuhax-75b545a727d91df8a428fa92539d06617cd0e207.tar.gz
neuhax-75b545a727d91df8a428fa92539d06617cd0e207.tar.bz2
neuhax-75b545a727d91df8a428fa92539d06617cd0e207.zip
Improve auto update version detection
-rw-r--r--src/main/kotlin/moe/nea/sky/features/meta/AutoUpdate.kt10
-rw-r--r--src/main/kotlin/moe/nea/sky/features/meta/SemanticVersion.kt43
2 files changed, 51 insertions, 2 deletions
diff --git a/src/main/kotlin/moe/nea/sky/features/meta/AutoUpdate.kt b/src/main/kotlin/moe/nea/sky/features/meta/AutoUpdate.kt
index 070fe34..2dff1f6 100644
--- a/src/main/kotlin/moe/nea/sky/features/meta/AutoUpdate.kt
+++ b/src/main/kotlin/moe/nea/sky/features/meta/AutoUpdate.kt
@@ -5,7 +5,6 @@ import io.github.moulberry.notenoughupdates.util.MC
import io.github.moulberry.notenoughupdates.util.MinecraftExecutor
import io.github.moulberry.notenoughupdates.util.brigadier.thenExecute
import io.github.moulberry.notenoughupdates.util.brigadier.withHelp
-import moe.nea.libautoupdate.CurrentVersion
import moe.nea.libautoupdate.UpdateContext
import moe.nea.libautoupdate.UpdateSource
import moe.nea.libautoupdate.UpdateTarget
@@ -20,7 +19,7 @@ object AutoUpdate {
val updater = UpdateContext(
UpdateSource.mavenSource("https://repo.nea.moe/releases", "moe.nea", "neuhax"),
UpdateTarget.deleteAndSaveInTheSameFolder(AutoUpdate::class.java),
- CurrentVersion.ofTag(NEUHax.version),
+ SemanticVersion.fromString(NEUHax.version)!!,
MODID
)
@@ -30,6 +29,7 @@ object AutoUpdate {
var potentialUpdate = updater.checkUpdate("")
var notified = false
+ var notifyNonUpdate = false
@SubscribeEvent
fun onFirstPlayerInteraction(event: TickEvent.ClientTickEvent) {
@@ -38,6 +38,11 @@ object AutoUpdate {
val p = MC.thePlayer ?: return
val update = potentialUpdate.getNow(null) ?: return
notified = true
+ if (!update.isUpdateAvailable && notifyNonUpdate) {
+ p.showMessage {
+ text("§eNo update found.")
+ }
+ }
if (update.isUpdateAvailable)
p.showMessage {
text("§eUpdate found. From §c${update.context.currentVersion.display()} §eto §a${update.update.versionName}§e. Click to update at next restart.")
@@ -57,6 +62,7 @@ object AutoUpdate {
thenExecute {
potentialUpdate = updater.checkUpdate("")
notified = false
+ notifyNonUpdate = true
}
}.withHelp("Update NEUHAX")
}
diff --git a/src/main/kotlin/moe/nea/sky/features/meta/SemanticVersion.kt b/src/main/kotlin/moe/nea/sky/features/meta/SemanticVersion.kt
new file mode 100644
index 0000000..cee9dac
--- /dev/null
+++ b/src/main/kotlin/moe/nea/sky/features/meta/SemanticVersion.kt
@@ -0,0 +1,43 @@
+package moe.nea.sky.features.meta
+
+import com.google.gson.JsonElement
+import com.google.gson.JsonPrimitive
+
+/**
+ * Shoddy implementation of semver. Intentionally ignores things such as betas and strips away extra info.
+ */
+class SemanticVersion(val major: Int, val minor: Int, val patch: Int) : moe.nea.libautoupdate.CurrentVersion,
+ Comparable<SemanticVersion> {
+
+ companion object {
+ fun fromString(semverString: String): SemanticVersion? {
+ val match = semverRegex.matchEntire(semverString) ?: return null
+ val (_, major, minor, patch) = match.groupValues
+ return SemanticVersion(
+ major.toIntOrNull() ?: return null,
+ minor.toIntOrNull() ?: return null,
+ patch.toIntOrNull() ?: return null,
+ )
+ }
+
+ private val semverRegex = "([0-9]+)\\.([0-9]+)\\.([0-9]+)(?:\\+.*)?".toRegex()
+
+ private val comparator = Comparator.comparingInt(SemanticVersion::major)
+ .thenComparingInt(SemanticVersion::minor)
+ .thenComparing(SemanticVersion::patch)
+ }
+
+ override fun display(): String {
+ return "$major.$minor.$patch"
+ }
+
+ override fun isOlderThan(element: JsonElement?): Boolean {
+ val stringVersion = (element as? JsonPrimitive)?.asString ?: return true
+ val semverVersion = fromString(stringVersion) ?: return false
+ return this < semverVersion
+ }
+
+ override fun compareTo(other: SemanticVersion): Int {
+ return comparator.compare(this, other)
+ }
+} \ No newline at end of file