Skip to content

Commit

Permalink
AutoUpdater fixes & improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
onixiya1337 committed Dec 20, 2023
1 parent 3298856 commit b95a77b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ mcVersion=1.8.9
modid=farmhelperv2
modName=FarmHelper
version=2.2.8
shouldRelease=true
shouldRelease=false
66 changes: 44 additions & 22 deletions src/main/java/com/jelly/farmhelperv2/gui/AutoUpdaterGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class AutoUpdaterGUI extends GuiScreen {
private GuiButton downloadBtn;
private GuiButton closeBtn;
private ScrollableList scrollableList;
static Pattern versionPattern = Pattern.compile("(\\d+\\.\\d+\\.\\d+)(?:-(pre(\\d*))?)?");

public AutoUpdaterGUI() {
super();
Expand Down Expand Up @@ -108,19 +109,7 @@ public static void getLatestVersion() { // don't skid or i'll find you - yuro
downloadURL = latestRelease.get("assets").getAsJsonArray().get(0).getAsJsonObject().get("browser_download_url").getAsString();
}

Matcher extractSymVer = Pattern.compile("\\d+\\.\\d+\\.\\d+").matcher(latestVersion);
if (!extractSymVer.find()) return;
String versionWithoutCommit = extractSymVer.group(0);
System.out.println("Latest version without commit: " + versionWithoutCommit);
String versionWithoutPre = versionWithoutCommit.replaceAll("-pre", "");

if (!versionWithoutCommit.contains("-pre") && FarmHelper.VERSION.equals(versionWithoutCommit + "-pre")) { // ignore Intellij warning
System.out.println("This Farm Helper pre-release version is older than the latest release version.");
isOutdated = true;
}

if (compareVersions(versionWithoutPre) > 0)
isOutdated = true;
isOutdated = isOutdated();

if (isOutdated) {
if (message.isEmpty()) {
Expand All @@ -133,12 +122,8 @@ public static void getLatestVersion() { // don't skid or i'll find you - yuro
.replace("= ", "§f= ")
.replace("- ", "§c- ") + " \n \n"; // fix for top and bottom padding
releaseMessage = Arrays.asList(cleanedMessage.split("\n"));
return;
}
if (compareVersions(versionWithoutPre) == 0) {
System.out.println("Farm Helper is up to date.");
} else if (compareVersions(versionWithoutPre) < 0) {
System.out.println("Farm Helper is newer than the latest full release version.");
} else {
System.out.println("Farm Helper is up to date!");
}
} else {
System.out.println("No releases found for the repository.");
Expand Down Expand Up @@ -202,10 +187,47 @@ private static List<String> createListWithSingleElement(String element) {
return list;
}

// Helper method to compare the latest with the current version
private static int compareVersions(String latestVersion) {
private static boolean isOutdated() {
Matcher latestVersionMatcher = versionPattern.matcher(latestVersion);
Matcher currentVersionMatcher = versionPattern.matcher(FarmHelper.VERSION);

if (!latestVersionMatcher.find() || latestVersionMatcher.group(1) == null || latestVersionMatcher.group(1).isEmpty()) {
System.out.println("Failed to parse latest version.");
return false;
}
if (!currentVersionMatcher.find() || currentVersionMatcher.group(1) == null || currentVersionMatcher.group(1).isEmpty()) {
System.out.println("Failed to parse current version.");
return false;
}
int latestPre = 0; // -pre version
if (latestVersionMatcher.group(2) == null || latestVersionMatcher.group(2).isEmpty())
latestPre = 1337; // without -pre version (full release)
if (latestVersionMatcher.group(3) != null && !latestVersionMatcher.group(3).isEmpty())
latestPre = Integer.parseInt(latestVersionMatcher.group(3)); // -preX version

int currentPre = 0; // -pre version
if (currentVersionMatcher.group(2) == null || currentVersionMatcher.group(2).isEmpty())
currentPre = 1337; // without -pre version (full release)
if (currentVersionMatcher.group(3) != null && !currentVersionMatcher.group(3).isEmpty())
currentPre = Integer.parseInt(currentVersionMatcher.group(3)); // -preX version

if (compareSymVer(latestVersionMatcher.group(1)) > 0)
return true;
else if (compareSymVer(latestVersionMatcher.group(1)) == 0)
return latestPre > currentPre;

return false;
}

private static int compareSymVer(String latestVersion) {
Matcher currentMatcher = versionPattern.matcher(FarmHelper.VERSION);

if (!currentMatcher.find()) {
return 0;
}

String[] parts1 = latestVersion.split("\\.");
String[] parts2 = FarmHelper.VERSION.replaceAll("-pre", "").split("\\.");
String[] parts2 = currentMatcher.group(1).split("\\.");

for (int i = 0; i < Math.min(parts1.length, parts2.length); i++) {
int part1 = Integer.parseInt(parts1[i]);
Expand Down

0 comments on commit b95a77b

Please sign in to comment.