2014-04-05 10:39:10 +02:00
|
|
|
package com.rarchives.ripme.ui;
|
|
|
|
|
2014-04-11 08:00:11 +02:00
|
|
|
import java.io.BufferedWriter;
|
2014-04-05 11:11:04 +02:00
|
|
|
import java.io.File;
|
2014-04-05 10:39:10 +02:00
|
|
|
import java.io.FileOutputStream;
|
2014-04-11 08:00:11 +02:00
|
|
|
import java.io.FileWriter;
|
2014-04-05 10:39:10 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import javax.swing.JLabel;
|
2014-04-06 01:57:54 +02:00
|
|
|
import javax.swing.JOptionPane;
|
2014-04-05 10:39:10 +02:00
|
|
|
|
2014-04-05 11:11:04 +02:00
|
|
|
import org.apache.log4j.Logger;
|
2014-04-05 10:39:10 +02:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.jsoup.Connection.Response;
|
|
|
|
import org.jsoup.Jsoup;
|
|
|
|
import org.jsoup.nodes.Document;
|
|
|
|
|
2014-05-03 10:13:14 +02:00
|
|
|
import com.rarchives.ripme.utils.Utils;
|
|
|
|
|
2014-04-05 10:39:10 +02:00
|
|
|
public class UpdateUtils {
|
|
|
|
|
2014-04-05 11:11:04 +02:00
|
|
|
private static final Logger logger = Logger.getLogger(UpdateUtils.class);
|
2016-04-24 06:04:16 +02:00
|
|
|
private static final String DEFAULT_VERSION = "1.2.13";
|
2014-04-05 10:39:10 +02:00
|
|
|
private static final String updateJsonURL = "http://rarchives.com/ripme.json";
|
|
|
|
private static final String updateJarURL = "http://rarchives.com/ripme.jar";
|
2014-04-05 11:11:04 +02:00
|
|
|
private static final String mainFileName = "ripme.jar";
|
|
|
|
private static final String updateFileName = "ripme.jar.update";
|
2014-04-05 10:39:10 +02:00
|
|
|
|
2014-04-05 11:11:04 +02:00
|
|
|
public static String getThisJarVersion() {
|
|
|
|
String thisVersion = UpdateUtils.class.getPackage().getImplementationVersion();
|
|
|
|
if (thisVersion == null) {
|
|
|
|
// Version is null if we're not running from the JAR
|
|
|
|
thisVersion = DEFAULT_VERSION; ; // Super-high version number
|
|
|
|
}
|
|
|
|
return thisVersion;
|
|
|
|
}
|
|
|
|
|
2014-04-05 10:39:10 +02:00
|
|
|
public static void updateProgram(JLabel configUpdateLabel) {
|
2014-04-06 01:57:54 +02:00
|
|
|
configUpdateLabel.setText("Checking for update...");
|
2014-04-05 10:39:10 +02:00
|
|
|
|
|
|
|
Document doc = null;
|
|
|
|
try {
|
2015-02-10 08:29:29 +01:00
|
|
|
logger.debug("Retrieving " + UpdateUtils.updateJsonURL);
|
2014-04-05 10:39:10 +02:00
|
|
|
doc = Jsoup.connect(UpdateUtils.updateJsonURL)
|
2014-06-12 09:38:19 +02:00
|
|
|
.timeout(10 * 1000)
|
|
|
|
.ignoreContentType(true)
|
|
|
|
.get();
|
2014-04-05 10:39:10 +02:00
|
|
|
} catch (IOException e) {
|
2014-04-05 11:11:04 +02:00
|
|
|
logger.error("Error while fetching update: ", e);
|
2014-04-06 01:57:54 +02:00
|
|
|
JOptionPane.showMessageDialog(null,
|
|
|
|
"<html><font color=\"red\">Error while fetching update: " + e.getMessage() + "</font></html>",
|
|
|
|
"RipMe Updater",
|
|
|
|
JOptionPane.ERROR_MESSAGE);
|
2014-04-05 10:39:10 +02:00
|
|
|
return;
|
2014-04-06 01:57:54 +02:00
|
|
|
} finally {
|
|
|
|
configUpdateLabel.setText("Current version: " + getThisJarVersion());
|
2014-04-05 10:39:10 +02:00
|
|
|
}
|
|
|
|
String jsonString = doc.body().html().replaceAll(""", "\"");
|
|
|
|
JSONObject json = new JSONObject(jsonString);
|
|
|
|
JSONArray jsonChangeList = json.getJSONArray("changeList");
|
2014-04-06 01:57:54 +02:00
|
|
|
StringBuilder changeList = new StringBuilder();
|
2014-04-05 10:39:10 +02:00
|
|
|
for (int i = 0; i < jsonChangeList.length(); i++) {
|
|
|
|
String change = jsonChangeList.getString(i);
|
2014-05-03 10:13:14 +02:00
|
|
|
if (change.startsWith(UpdateUtils.getThisJarVersion() + ":")) {
|
|
|
|
break;
|
|
|
|
}
|
2014-04-06 01:57:54 +02:00
|
|
|
changeList.append("<br> + " + change);
|
2014-04-05 10:39:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
String latestVersion = json.getString("latestVersion");
|
|
|
|
if (UpdateUtils.isNewerVersion(latestVersion)) {
|
2015-02-10 08:29:29 +01:00
|
|
|
logger.info("Found newer version: " + latestVersion);
|
2014-04-06 01:57:54 +02:00
|
|
|
int result = JOptionPane.showConfirmDialog(
|
|
|
|
null,
|
|
|
|
"<html><font color=\"green\">New version (" + latestVersion + ") is available!</font>"
|
|
|
|
+ "<br><br>Recent changes:" + changeList.toString()
|
|
|
|
+ "<br><br>Do you want to download and run the newest version?</html>",
|
|
|
|
"RipMe Updater",
|
|
|
|
JOptionPane.YES_NO_OPTION);
|
|
|
|
if (result != JOptionPane.YES_OPTION) {
|
|
|
|
configUpdateLabel.setText("<html>Current Version: " + getThisJarVersion()
|
|
|
|
+ "<br><font color=\"green\">Latest version: " + latestVersion + "</font></html>");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
configUpdateLabel.setText("<html><font color=\"green\">Downloading new version...</font></html>");
|
2014-04-05 11:11:04 +02:00
|
|
|
logger.info("New version found, downloading...");
|
2014-04-05 10:39:10 +02:00
|
|
|
try {
|
2014-04-09 08:33:26 +02:00
|
|
|
UpdateUtils.downloadJarAndLaunch(updateJarURL);
|
2014-04-05 10:39:10 +02:00
|
|
|
} catch (IOException e) {
|
2014-04-06 01:57:54 +02:00
|
|
|
JOptionPane.showMessageDialog(null,
|
|
|
|
"Error while updating: " + e.getMessage(),
|
|
|
|
"RipMe Updater",
|
|
|
|
JOptionPane.ERROR_MESSAGE);
|
|
|
|
configUpdateLabel.setText("");
|
2014-04-05 11:11:04 +02:00
|
|
|
logger.error("Error while updating: ", e);
|
2014-04-05 10:39:10 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2015-02-10 08:29:29 +01:00
|
|
|
logger.debug("This version (" + UpdateUtils.getThisJarVersion() +
|
|
|
|
") is the same or newer than the website's version (" + latestVersion + ")");
|
2014-04-06 01:57:54 +02:00
|
|
|
configUpdateLabel.setText("<html><font color=\"green\">v" + UpdateUtils.getThisJarVersion() + " is the latest version</font></html>");
|
2015-02-10 08:29:29 +01:00
|
|
|
logger.debug("Running latest version: " + UpdateUtils.getThisJarVersion());
|
2014-04-05 10:39:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean isNewerVersion(String latestVersion) {
|
|
|
|
int[] oldVersions = versionStringToInt(getThisJarVersion());
|
|
|
|
int[] newVersions = versionStringToInt(latestVersion);
|
|
|
|
if (oldVersions.length < newVersions.length) {
|
|
|
|
System.err.println("Calculated: " + oldVersions + " < " + latestVersion);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < oldVersions.length; i++) {
|
|
|
|
if (newVersions[i] > oldVersions[i]) {
|
2014-04-05 11:11:04 +02:00
|
|
|
logger.debug("oldVersion " + getThisJarVersion() + " < latestVersion" + latestVersion);
|
2014-04-05 10:39:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (newVersions[i] < oldVersions[i]) {
|
2014-04-05 11:11:04 +02:00
|
|
|
logger.debug("oldVersion " + getThisJarVersion() + " > latestVersion " + latestVersion);
|
2014-04-05 10:39:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, the version numbers are exactly the same.
|
|
|
|
// Assume any additional changes to the version text means a new version
|
|
|
|
return !(latestVersion.equals(getThisJarVersion()));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int[] versionStringToInt(String version) {
|
|
|
|
String strippedVersion = version.split("-")[0];
|
|
|
|
String[] strVersions = strippedVersion.split("\\.");
|
|
|
|
int[] intVersions = new int[strVersions.length];
|
|
|
|
for (int i = 0; i < strVersions.length; i++) {
|
|
|
|
intVersions[i] = Integer.parseInt(strVersions[i]);
|
|
|
|
}
|
|
|
|
return intVersions;
|
|
|
|
}
|
2014-04-05 11:11:04 +02:00
|
|
|
|
2014-04-09 08:33:26 +02:00
|
|
|
private static void downloadJarAndLaunch(String updateJarURL)
|
2014-04-05 11:11:04 +02:00
|
|
|
throws IOException {
|
|
|
|
Response response;
|
|
|
|
response = Jsoup.connect(updateJarURL)
|
|
|
|
.ignoreContentType(true)
|
2014-05-03 10:13:14 +02:00
|
|
|
.timeout(Utils.getConfigInteger("download.timeout", 60 * 1000))
|
2014-04-05 11:11:04 +02:00
|
|
|
.maxBodySize(1024 * 1024 * 100)
|
|
|
|
.execute();
|
|
|
|
FileOutputStream out = new FileOutputStream(updateFileName);
|
|
|
|
out.write(response.bodyAsBytes());
|
|
|
|
out.close();
|
2014-04-09 08:33:26 +02:00
|
|
|
logger.info("Download of new version complete; saved to " + updateFileName);
|
2014-04-11 08:00:11 +02:00
|
|
|
|
|
|
|
// Setup updater script
|
|
|
|
final String batchFile, script;
|
|
|
|
final String[] batchExec;
|
|
|
|
String os = System.getProperty("os.name").toLowerCase();
|
|
|
|
if (os.contains("win")) {
|
|
|
|
// Windows
|
|
|
|
batchFile = "update_ripme.bat";
|
|
|
|
String batchPath = new File(batchFile).getAbsolutePath();
|
|
|
|
script = "@echo off\r\n"
|
|
|
|
+ "timeout 1" + "\r\n"
|
|
|
|
+ "copy " + updateFileName + " " + mainFileName + "\r\n"
|
|
|
|
+ "del " + updateFileName + "\r\n"
|
|
|
|
+ "ripme.jar" + "\r\n"
|
|
|
|
+ "del " + batchPath + "\r\n";
|
|
|
|
batchExec = new String[] { batchPath };
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Mac / Linux
|
|
|
|
batchFile = "update_ripme.sh";
|
|
|
|
String batchPath = new File(batchFile).getAbsolutePath();
|
|
|
|
script = "#!/bin/sh\n"
|
|
|
|
+ "sleep 1" + "\n"
|
|
|
|
+ "cd " + new File(mainFileName).getAbsoluteFile().getParent() + "\n"
|
|
|
|
+ "cp -f " + updateFileName + " " + mainFileName + "\n"
|
|
|
|
+ "rm -f " + updateFileName + "\n"
|
|
|
|
+ "java -jar \"" + new File(mainFileName).getAbsolutePath() + "\" &\n"
|
|
|
|
+ "sleep 1" + "\n"
|
|
|
|
+ "rm -f " + batchPath + "\n";
|
|
|
|
batchExec = new String[] { "sh", batchPath };
|
|
|
|
}
|
|
|
|
// Create updater script
|
|
|
|
BufferedWriter bw = new BufferedWriter(new FileWriter(batchFile));
|
|
|
|
bw.write(script);
|
|
|
|
bw.flush();
|
|
|
|
bw.close();
|
|
|
|
logger.info("Saved update script to " + batchFile);
|
|
|
|
// Run updater script on exit
|
2014-04-09 08:33:26 +02:00
|
|
|
Runtime.getRuntime().addShutdownHook(new Thread() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
2014-04-11 08:00:11 +02:00
|
|
|
logger.info("Executing: " + batchFile);
|
|
|
|
Runtime.getRuntime().exec(batchExec);
|
2014-04-09 08:33:26 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-04-11 08:00:11 +02:00
|
|
|
logger.info("Exiting older version, should execute update script (" + batchFile + ") during exit");
|
2014-04-05 11:11:04 +02:00
|
|
|
System.exit(0);
|
|
|
|
}
|
|
|
|
|
2014-04-05 10:39:10 +02:00
|
|
|
}
|