mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-18 11:48:02 +00:00
Get the Updater liike 90% of the way there.
This commit is contained in:
@@ -12,8 +12,8 @@ import org.apache.commons.lang3.StringUtils;
|
|||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
@@ -27,6 +27,7 @@ public class AutoUpdater {
|
|||||||
private final String RELEASE_PACKAGE = "https://releases.cardforge.org/latest/";
|
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 final String RELEASE_MAVEN_METADATA = "https://releases.cardforge.org/forge/forge-gui-desktop/maven-metadata.xml";
|
||||||
private static final boolean VERSION_FROM_METADATA = true;
|
private static final boolean VERSION_FROM_METADATA = true;
|
||||||
|
private static final String TMP_DIR = "tmp/";
|
||||||
|
|
||||||
public static String[] updateChannels = new String[]{ "none", "snapshot", "release"};
|
public static String[] updateChannels = new String[]{ "none", "snapshot", "release"};
|
||||||
|
|
||||||
@@ -51,29 +52,39 @@ public class AutoUpdater {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
if (downloadUpdate()) {
|
if (downloadUpdate()) {
|
||||||
extractUpdate();
|
extractAndRestart();
|
||||||
restartForge();
|
|
||||||
}
|
}
|
||||||
} catch(IOException e) {
|
} catch(IOException | URISyntaxException e) {
|
||||||
return false;
|
|
||||||
} catch(URISyntaxException e) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void extractAndRestart() {
|
||||||
|
extractUpdate();
|
||||||
|
restartForge();
|
||||||
|
}
|
||||||
|
|
||||||
private boolean verifyUpdateable() {
|
private boolean verifyUpdateable() {
|
||||||
|
if (buildVersion.contains("GIT")) {
|
||||||
|
//return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
// TODO This doesn't work yet, because FSkin isn't loaded at the time.
|
// TODO This doesn't work yet, because FSkin isn't loaded at the time.
|
||||||
return false;
|
return false;
|
||||||
} else if (updateChannel.equals("none")) {
|
} else if (updateChannel.equals("none")) {
|
||||||
// User clicked on check for updates withoout a valid update channel prompt
|
String message = "You haven't set an update channel. Do you want to check a channel now?";
|
||||||
// updateChannel = newchoice or return false
|
List<String> options = ImmutableList.of("Cancel", "release", "snapshot");
|
||||||
|
int option = SOptionPane.showOptionDialog(message, "Manual Check", null, options, 0);
|
||||||
|
if (option == 0) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
updateChannel = options.get(option);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buildVersion.contains("GIT")) {
|
if (buildVersion.contains("SNAPSHOT")) {
|
||||||
return false;
|
|
||||||
} else if (buildVersion.contains("SNAPSHOT")) {
|
|
||||||
if (!updateChannel.equals("snapshot")) {
|
if (!updateChannel.equals("snapshot")) {
|
||||||
System.out.println("Snapshot build versions must use snapshot update channel to work");
|
System.out.println("Snapshot build versions must use snapshot update channel to work");
|
||||||
return false;
|
return false;
|
||||||
@@ -127,7 +138,7 @@ public class AutoUpdater {
|
|||||||
|
|
||||||
private void retrieveVersion() throws MalformedURLException {
|
private void retrieveVersion() throws MalformedURLException {
|
||||||
if (VERSION_FROM_METADATA) {
|
if (VERSION_FROM_METADATA) {
|
||||||
if (!updateChannel.equals("release")) {
|
if (updateChannel.equals("release")) {
|
||||||
extractVersionFromMavenRelease();
|
extractVersionFromMavenRelease();
|
||||||
} else {
|
} else {
|
||||||
extractVersionFromSnapshotIndex();
|
extractVersionFromSnapshotIndex();
|
||||||
@@ -162,7 +173,7 @@ public class AutoUpdater {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean downloadUpdate() throws URISyntaxException, IOException {
|
private boolean downloadUpdate() throws URISyntaxException, IOException {
|
||||||
// Change the "auto" to be more auto.
|
// TODO Change the "auto" to be more auto.
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
// We need to preload enough of a Skins to show a dialog and a button if we're in loading
|
// We need to preload enough of a Skins to show a dialog and a button if we're in loading
|
||||||
// splashScreen.prepareForDialogs();
|
// splashScreen.prepareForDialogs();
|
||||||
@@ -199,29 +210,37 @@ public class AutoUpdater {
|
|||||||
public void run() {
|
public void run() {
|
||||||
GuiBase.getInterface().download(new GuiDownloadZipService("Auto Updater", "Download the new version..", packageUrl, "tmp/", null, null) {
|
GuiBase.getInterface().download(new GuiDownloadZipService("Auto Updater", "Download the new version..", packageUrl, "tmp/", null, null) {
|
||||||
@Override
|
@Override
|
||||||
protected void copyInputStream(InputStream in, String outPath) throws IOException {
|
public void downloadAndUnzip() {
|
||||||
super.copyInputStream(in, outPath);
|
packagePath = download(version + "-upgrade.tar.bz2");
|
||||||
packagePath = outPath;
|
if (packagePath != null) {
|
||||||
|
extractAndRestart();
|
||||||
extractUpdate();
|
}
|
||||||
}
|
}
|
||||||
}, this);
|
}, this);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
SwingUtilities.invokeLater(callback);
|
SwingUtilities.invokeLater(callback);
|
||||||
|
//
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void extractUpdate() {
|
private void extractUpdate() {
|
||||||
System.out.println(packagePath);
|
// TODOD Something like https://stackoverflow.com/questions/315618/how-do-i-extract-a-tar-file-in-java
|
||||||
// Take packagepath and tar xzvf it
|
final Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
|
||||||
|
if (desktop != null) {
|
||||||
|
try {
|
||||||
|
desktop.open(new File(packagePath).getParentFile());
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
System.out.println(packagePath);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void restartForge() {
|
private void restartForge() {
|
||||||
// Do we have a way to retrigger an immediate restart?
|
if (isLoading || SOptionPane.showConfirmDialog("Forge has been downloaded. You should extract the package and restart Forge for the new version.", "Exit now?")) {
|
||||||
if (isLoading || SOptionPane.showConfirmDialog("Forge has been extracted. You should restart Forge for the new version", "Exit now?")) {
|
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -73,72 +73,7 @@ public class GuiDownloadZipService extends GuiDownloadService {
|
|||||||
String zipFilename = download("temp.zip");
|
String zipFilename = download("temp.zip");
|
||||||
if (zipFilename == null) { return; }
|
if (zipFilename == null) { return; }
|
||||||
|
|
||||||
//if assets.zip downloaded successfully, unzip into destination folder
|
extract(zipFilename);
|
||||||
try {
|
|
||||||
GuiBase.getInterface().preventSystemSleep(true); //prevent system from going into sleep mode while unzipping
|
|
||||||
|
|
||||||
if (deleteFolder != null) {
|
|
||||||
final File deleteDir = new File(deleteFolder);
|
|
||||||
if (deleteDir.exists()) {
|
|
||||||
//attempt to delete previous res directory if to be rebuilt
|
|
||||||
progressBar.reset();
|
|
||||||
progressBar.setDescription("Deleting old " + desc + "...");
|
|
||||||
if (deleteFolder.equals(destFolder)) { //move zip file to prevent deleting it
|
|
||||||
final String oldZipFilename = zipFilename;
|
|
||||||
zipFilename = deleteDir.getParentFile().getAbsolutePath() + File.separator + "temp.zip";
|
|
||||||
Files.move(new File(oldZipFilename), new File(zipFilename));
|
|
||||||
}
|
|
||||||
FileUtil.deleteDirectory(deleteDir);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
final ZipFile zipFile = new ZipFile(zipFilename);
|
|
||||||
final Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
|
||||||
|
|
||||||
progressBar.reset();
|
|
||||||
progressBar.setPercentMode(true);
|
|
||||||
progressBar.setDescription("Extracting " + desc);
|
|
||||||
progressBar.setMaximum(zipFile.size());
|
|
||||||
|
|
||||||
FileUtil.ensureDirectoryExists(destFolder);
|
|
||||||
|
|
||||||
int count = 0;
|
|
||||||
int failedCount = 0;
|
|
||||||
while (entries.hasMoreElements()) {
|
|
||||||
if (cancel) { break; }
|
|
||||||
|
|
||||||
try {
|
|
||||||
final ZipEntry entry = entries.nextElement();
|
|
||||||
|
|
||||||
final String path = destFolder + File.separator + entry.getName();
|
|
||||||
if (entry.isDirectory()) {
|
|
||||||
new File(path).mkdir();
|
|
||||||
progressBar.setValue(++count);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
copyInputStream(zipFile.getInputStream(entry), path);
|
|
||||||
progressBar.setValue(++count);
|
|
||||||
filesExtracted++;
|
|
||||||
}
|
|
||||||
catch (final Exception e) { //don't quit out completely if an entry is not UTF-8
|
|
||||||
progressBar.setValue(++count);
|
|
||||||
failedCount++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (failedCount > 0) {
|
|
||||||
Log.error("Downloading " + desc, failedCount + " " + desc + " could not be extracted");
|
|
||||||
}
|
|
||||||
|
|
||||||
zipFile.close();
|
|
||||||
new File(zipFilename).delete();
|
|
||||||
}
|
|
||||||
catch (final Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
GuiBase.getInterface().preventSystemSleep(false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String download(final String filename) {
|
public String download(final String filename) {
|
||||||
@@ -211,6 +146,75 @@ public class GuiDownloadZipService extends GuiDownloadService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void extract(String zipFilename) {
|
||||||
|
//if assets.zip downloaded successfully, unzip into destination folder
|
||||||
|
try {
|
||||||
|
GuiBase.getInterface().preventSystemSleep(true); //prevent system from going into sleep mode while unzipping
|
||||||
|
|
||||||
|
if (deleteFolder != null) {
|
||||||
|
final File deleteDir = new File(deleteFolder);
|
||||||
|
if (deleteDir.exists()) {
|
||||||
|
//attempt to delete previous res directory if to be rebuilt
|
||||||
|
progressBar.reset();
|
||||||
|
progressBar.setDescription("Deleting old " + desc + "...");
|
||||||
|
if (deleteFolder.equals(destFolder)) { //move zip file to prevent deleting it
|
||||||
|
final String oldZipFilename = zipFilename;
|
||||||
|
zipFilename = deleteDir.getParentFile().getAbsolutePath() + File.separator + "temp.zip";
|
||||||
|
Files.move(new File(oldZipFilename), new File(zipFilename));
|
||||||
|
}
|
||||||
|
FileUtil.deleteDirectory(deleteDir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final ZipFile zipFile = new ZipFile(zipFilename);
|
||||||
|
final Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
||||||
|
|
||||||
|
progressBar.reset();
|
||||||
|
progressBar.setPercentMode(true);
|
||||||
|
progressBar.setDescription("Extracting " + desc);
|
||||||
|
progressBar.setMaximum(zipFile.size());
|
||||||
|
|
||||||
|
FileUtil.ensureDirectoryExists(destFolder);
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
int failedCount = 0;
|
||||||
|
while (entries.hasMoreElements()) {
|
||||||
|
if (cancel) { break; }
|
||||||
|
|
||||||
|
try {
|
||||||
|
final ZipEntry entry = entries.nextElement();
|
||||||
|
|
||||||
|
final String path = destFolder + File.separator + entry.getName();
|
||||||
|
if (entry.isDirectory()) {
|
||||||
|
new File(path).mkdir();
|
||||||
|
progressBar.setValue(++count);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
copyInputStream(zipFile.getInputStream(entry), path);
|
||||||
|
progressBar.setValue(++count);
|
||||||
|
filesExtracted++;
|
||||||
|
}
|
||||||
|
catch (final Exception e) { //don't quit out completely if an entry is not UTF-8
|
||||||
|
progressBar.setValue(++count);
|
||||||
|
failedCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (failedCount > 0) {
|
||||||
|
Log.error("Downloading " + desc, failedCount + " " + desc + " could not be extracted");
|
||||||
|
}
|
||||||
|
|
||||||
|
zipFile.close();
|
||||||
|
new File(zipFilename).delete();
|
||||||
|
}
|
||||||
|
catch (final Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
GuiBase.getInterface().preventSystemSleep(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected void copyInputStream(final InputStream in, final String outPath) throws IOException {
|
protected void copyInputStream(final InputStream in, final String outPath) throws IOException {
|
||||||
final byte[] buffer = new byte[1024];
|
final byte[] buffer = new byte[1024];
|
||||||
int len;
|
int len;
|
||||||
|
|||||||
Reference in New Issue
Block a user