Skip to content

Commit

Permalink
Merge pull request RipMeApp#1381 from Isaaku/issues/1379_network_fix
Browse files Browse the repository at this point in the history
Issues/1379 network fix
  • Loading branch information
cyian-1756 authored Aug 4, 2019
2 parents ae8c071 + e196e13 commit 580e4db
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 119 deletions.
84 changes: 36 additions & 48 deletions src/main/java/com/rarchives/ripme/ui/UpdateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class UpdateUtils {
static {
try {
mainFileName = new File(UpdateUtils.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getAbsolutePath();
} catch (URISyntaxException e) {
} catch (URISyntaxException | IllegalArgumentException e) {
mainFileName = "ripme.jar";
logger.error("Unable to get path of jar");
e.printStackTrace();
Expand Down Expand Up @@ -73,16 +73,12 @@ public static void updateProgramCLI() {
Document doc = null;
try {
logger.debug("Retrieving " + UpdateUtils.updateJsonURL);
doc = Jsoup.connect(UpdateUtils.updateJsonURL)
.timeout(10 * 1000)
.ignoreContentType(true)
.get();
doc = Jsoup.connect(UpdateUtils.updateJsonURL).timeout(10 * 1000).ignoreContentType(true).get();
} catch (IOException e) {
logger.error("Error while fetching update: ", e);
JOptionPane.showMessageDialog(null,
"<html><font color=\"red\">Error while fetching update: " + e.getMessage() + "</font></html>",
"RipMe Updater",
JOptionPane.ERROR_MESSAGE);
"RipMe Updater", JOptionPane.ERROR_MESSAGE);
return;
} finally {
logger.info("Current version: " + getThisJarVersion());
Expand All @@ -105,8 +101,8 @@ public static void updateProgramCLI() {
logger.error("Error while updating: ", e);
}
} else {
logger.debug("This version (" + UpdateUtils.getThisJarVersion() +
") is the same or newer than the website's version (" + latestVersion + ")");
logger.debug("This version (" + UpdateUtils.getThisJarVersion()
+ ") is the same or newer than the website's version (" + latestVersion + ")");
logger.info("v" + UpdateUtils.getThisJarVersion() + " is the latest version");
logger.debug("Running latest version: " + UpdateUtils.getThisJarVersion());
}
Expand All @@ -118,16 +114,12 @@ public static void updateProgramGUI(JLabel configUpdateLabel) {
Document doc = null;
try {
logger.debug("Retrieving " + UpdateUtils.updateJsonURL);
doc = Jsoup.connect(UpdateUtils.updateJsonURL)
.timeout(10 * 1000)
.ignoreContentType(true)
.get();
doc = Jsoup.connect(UpdateUtils.updateJsonURL).timeout(10 * 1000).ignoreContentType(true).get();
} catch (IOException e) {
logger.error("Error while fetching update: ", e);
JOptionPane.showMessageDialog(null,
"<html><font color=\"red\">Error while fetching update: " + e.getMessage() + "</font></html>",
"RipMe Updater",
JOptionPane.ERROR_MESSAGE);
"RipMe Updater", JOptionPane.ERROR_MESSAGE);
return;
} finally {
configUpdateLabel.setText("Current version: " + getThisJarVersion());
Expand All @@ -141,14 +133,14 @@ public static void updateProgramGUI(JLabel configUpdateLabel) {
if (UpdateUtils.isNewerVersion(latestVersion)) {
logger.info("Found newer version: " + latestVersion);
JEditorPane changeListPane = new JEditorPane("text/html", String.format(
"<html><font color=\"green\">New version (%s) is available!</font>" + "<br><br>Recent changes: %s"
+ "<br><br>Do you want to download and run the newest version?</html>",
latestVersion, changeList.replaceAll("\\n", "<br><br>")));
changeListPane.setEditable(false);
JScrollPane changeListScrollPane = new JScrollPane(changeListPane);
changeListScrollPane.setPreferredSize(new Dimension(300, 300));
int result = JOptionPane.showConfirmDialog(null, changeListScrollPane, "RipMe Updater",
JOptionPane.YES_NO_OPTION);
"<html><font color=\"green\">New version (%s) is available!</font>" + "<br><br>Recent changes: %s"
+ "<br><br>Do you want to download and run the newest version?</html>",
latestVersion, changeList.replaceAll("\\n", "<br><br>")));
changeListPane.setEditable(false);
JScrollPane changeListScrollPane = new JScrollPane(changeListPane);
changeListScrollPane.setPreferredSize(new Dimension(300, 300));
int result = JOptionPane.showConfirmDialog(null, changeListScrollPane, "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>");
Expand All @@ -159,17 +151,16 @@ public static void updateProgramGUI(JLabel configUpdateLabel) {
try {
UpdateUtils.downloadJarAndLaunch(getUpdateJarURL(latestVersion), true);
} catch (IOException e) {
JOptionPane.showMessageDialog(null,
"Error while updating: " + e.getMessage(),
"RipMe Updater",
JOptionPane.ERROR_MESSAGE);
configUpdateLabel.setText("");
JOptionPane.showMessageDialog(null, "Error while updating: " + e.getMessage(), "RipMe Updater",
JOptionPane.ERROR_MESSAGE);
configUpdateLabel.setText("");
logger.error("Error while updating: ", e);
}
} else {
logger.debug("This version (" + UpdateUtils.getThisJarVersion() +
") is the same or newer than the website's version (" + latestVersion + ")");
configUpdateLabel.setText("<html><font color=\"green\">v" + UpdateUtils.getThisJarVersion() + " is the latest version</font></html>");
logger.debug("This version (" + UpdateUtils.getThisJarVersion()
+ ") is the same or newer than the website's version (" + latestVersion + ")");
configUpdateLabel.setText("<html><font color=\"green\">v" + UpdateUtils.getThisJarVersion()
+ " is the latest version</font></html>");
logger.debug("Running latest version: " + UpdateUtils.getThisJarVersion());
}
}
Expand All @@ -191,8 +182,7 @@ private static boolean isNewerVersion(String latestVersion) {
if (newVersions[i] > oldVersions[i]) {
logger.debug("oldVersion " + getThisJarVersion() + " < latestVersion" + latestVersion);
return true;
}
else if (newVersions[i] < oldVersions[i]) {
} else if (newVersions[i] < oldVersions[i]) {
logger.debug("oldVersion " + getThisJarVersion() + " > latestVersion " + latestVersion);
return false;
}
Expand All @@ -214,7 +204,7 @@ private static int[] versionStringToInt(String version) {
}

// Code take from https://stackoverflow.com/a/30925550
public static String createSha256(File file) {
public static String createSha256(File file) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
InputStream fis = new FileInputStream(file);
Expand All @@ -232,7 +222,8 @@ public static String createSha256(File file) {
sb.append("0123456789ABCDEF".charAt((b & 0xF0) >> 4));
sb.append("0123456789ABCDEF".charAt((b & 0x0F)));
}
// As patch.py writes the hash in lowercase this must return the has in lowercase
// As patch.py writes the hash in lowercase this must return the has in
// lowercase
return sb.toString().toLowerCase();
} catch (NoSuchAlgorithmException e) {
logger.error("Got error getting file hash " + e.getMessage());
Expand All @@ -244,13 +235,10 @@ public static String createSha256(File file) {
return null;
}

private static void downloadJarAndLaunch(String updateJarURL, Boolean shouldLaunch)
throws IOException {
private static void downloadJarAndLaunch(String updateJarURL, Boolean shouldLaunch) throws IOException {
Response response;
response = Jsoup.connect(updateJarURL)
.ignoreContentType(true)
.timeout(Utils.getConfigInteger("download.timeout", 60 * 1000))
.maxBodySize(1024 * 1024 * 100)
response = Jsoup.connect(updateJarURL).ignoreContentType(true)
.timeout(Utils.getConfigInteger("download.timeout", 60 * 1000)).maxBodySize(1024 * 1024 * 100)
.execute();

try (FileOutputStream out = new FileOutputStream(updateFileName)) {
Expand All @@ -276,15 +264,13 @@ private static void downloadJarAndLaunch(String updateJarURL, Boolean shouldLaun
// Windows
final String batchFile = "update_ripme.bat";
final String batchPath = new File(batchFile).getAbsolutePath();
String script = "@echo off\r\n"
+ "timeout 1\r\n"
+ "copy " + updateFileName + " " + mainFileName + "\r\n"
+ "del " + updateFileName + "\r\n";
String script = "@echo off\r\n" + "timeout 1\r\n" + "copy " + updateFileName + " " + mainFileName + "\r\n"
+ "del " + updateFileName + "\r\n";
if (shouldLaunch) {
script += mainFileName + "\r\n";
}
script += "del " + batchPath + "\r\n";
final String[] batchExec = new String[]{batchPath};
final String[] batchExec = new String[] { batchPath };
// Create updater script
try (BufferedWriter bw = new BufferedWriter(new FileWriter(batchFile))) {
bw.write(script);
Expand All @@ -298,15 +284,17 @@ private static void downloadJarAndLaunch(String updateJarURL, Boolean shouldLaun
logger.info("Executing: " + batchFile);
Runtime.getRuntime().exec(batchExec);
} catch (IOException e) {
//TODO implement proper stack trace handling this is really just intented as a placeholder until you implement proper error handling
// TODO implement proper stack trace handling this is really just intented as a
// placeholder until you implement proper error handling
e.printStackTrace();
}
}));
logger.info("Exiting older version, should execute update script (" + batchFile + ") during exit");
System.exit(0);
} else {
// Mac / Linux
// Modifying file and launching it: *nix distributions don't have any issues with modifying/deleting files
// Modifying file and launching it: *nix distributions don't have any issues
// with modifying/deleting files
// while they are being run
File mainFile = new File(mainFileName);
String mainFilePath = mainFile.getAbsolutePath();
Expand Down
Loading

0 comments on commit 580e4db

Please sign in to comment.