From 2b8d826209b36142e0610cdc2710cb76bbe3a70a Mon Sep 17 00:00:00 2001 From: friarsol Date: Fri, 21 Feb 2020 22:26:33 -0500 Subject: [PATCH] I think this should be working now! --- .../main/java/forge/download/AutoUpdater.java | 62 ++++++++++++++++--- 1 file changed, 52 insertions(+), 10 deletions(-) diff --git a/forge-gui/src/main/java/forge/download/AutoUpdater.java b/forge-gui/src/main/java/forge/download/AutoUpdater.java index 16d3e7bb9b8..c5a4d91ba45 100644 --- a/forge-gui/src/main/java/forge/download/AutoUpdater.java +++ b/forge-gui/src/main/java/forge/download/AutoUpdater.java @@ -13,19 +13,24 @@ import javax.swing.*; import java.awt.*; import java.io.IOException; import java.io.InputStream; -import java.lang.reflect.InvocationTargetException; import java.net.*; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class AutoUpdater { + private final String SNAPSHOT_VERSION_INDEX = "https://snapshots.cardforge.org/"; private final String SNAPSHOT_VERSION_URL = "https://snapshots.cardforge.org/version.txt"; private final String SNAPSHOT_PACKAGE = "https://snapshots.cardforge.org/latest/"; private final String RELEASE_VERSION_URL = "https://releases.cardforge.org/forge/forge-gui-desktop/version.txt"; private final String RELEASE_PACKAGE = "https://releases.cardforge.org/latest/"; + private final String RELEASE_MAVEN_METADATA = "https://releases.cardforge.org/forge/forge-gui-desktop/maven-metadata.xml"; + private static final boolean VERSION_FROM_METADATA = true; public static String[] updateChannels = new String[]{ "None", "Snapshot", "Release"}; private boolean isLoading; + private String updateChannel; private String version; private String buildVersion; private String versionUrlString; @@ -35,6 +40,8 @@ public class AutoUpdater { public AutoUpdater(boolean loading) { // What do I need? Preferences? Splashscreen? UI? Skins? isLoading = loading; + final ForgePreferences.FPref updatePreference = ForgePreferences.FPref.AUTO_UPDATE; + updateChannel = "release"; //this.prefs.getPref(updatePreference); } public boolean attemptToUpdate() { @@ -55,10 +62,6 @@ public class AutoUpdater { } private boolean verifyUpdateable() { - // Check the preferences - final ForgePreferences.FPref updatePreference = ForgePreferences.FPref.AUTO_UPDATE; - final String updateChannel = "release"; //this.prefs.getPref(updatePreference); - if (isLoading) { // TODO This doesn't work yet, because FSkin isn't loaded at the time. return false; @@ -95,14 +98,17 @@ public class AutoUpdater { private boolean compareBuildWithLatestChannelVersion() { try { - URL versionUrl = new URL(versionUrlString); - version = FileUtil.readFileToString(versionUrl); + retrieveVersion(); buildVersion = BuildInfo.getVersionString(); - // Return false if buildVersion == GIT + if (buildVersion.contains("SNAPSHOT") && !updateChannel.equals("snapshot")) { + System.out.println("Snapshot build versions must use snapshot update channel to work"); + return false; + } + if (buildVersion.equalsIgnoreCase("GIT") || StringUtils.isEmpty(version) ) { - // return false; + return false; } if (buildVersion.equals(version)) { @@ -116,6 +122,42 @@ public class AutoUpdater { return true; } + private void retrieveVersion() throws MalformedURLException { + if (VERSION_FROM_METADATA) { + if (!updateChannel.equals("release")) { + extractVersionFromMavenRelease(); + } else { + extractVersionFromSnapshotIndex(); + } + } else { + URL versionUrl = new URL(versionUrlString); + version = FileUtil.readFileToString(versionUrl); + } + } + + private void extractVersionFromSnapshotIndex() throws MalformedURLException { + URL metadataUrl = new URL(SNAPSHOT_VERSION_INDEX); + String index = FileUtil.readFileToString(metadataUrl); + + System.out.println(index); + Pattern p = Pattern.compile(">forge-(.*SNAPSHOT)"); + Matcher m = p.matcher(index); + while(m.find()){ + version = m.group(1); + } + } + + private void extractVersionFromMavenRelease() throws MalformedURLException { + URL metadataUrl = new URL(RELEASE_MAVEN_METADATA); + String xml = FileUtil.readFileToString(metadataUrl); + + Pattern p = Pattern.compile("(.*)"); + Matcher m = p.matcher(xml); + while(m.find()){ + version = m.group(1); + } + } + private boolean downloadUpdate() throws URISyntaxException, IOException { // We need to preload enough of a Skins to show a dialog and a button if we're in loading // splashScreen.prepareForDialogs(); @@ -126,7 +168,7 @@ public class AutoUpdater { } String message = "A new version of Forge is available (" + version + ").\n" + - "You are currently on an older version (" + buildVersion + ").\n\n" + + "You are currently on version (" + buildVersion + ").\n\n" + "Would you like to update to the new version now?"; final List options = ImmutableList.of("Update Now", "Update Later");