Skip to content

Commit

Permalink
Enabled parallel downloading and fixed some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTroble committed Feb 15, 2021
1 parent 17aeea8 commit 6ac5d3d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
34 changes: 19 additions & 15 deletions src/main/java/com/troblecodings/launcher/util/ConnectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;

import com.troblecodings.launcher.ErrorDialog;
import com.troblecodings.launcher.ErrorPart;
import com.troblecodings.launcher.Launcher;

Expand Down Expand Up @@ -110,12 +111,13 @@ public static boolean download(String url, String name, final Consumer<Long> upd
Path normalFile = Paths.get(name);
try {
Files.move(pathtofile, normalFile, StandardCopyOption.REPLACE_EXISTING);
Files.deleteIfExists(pathtofile);
} catch (IOException e) {
e.printStackTrace();
}
return true;
}

private static ExecutorService executors = Executors.newCachedThreadPool();

// Checks if the file exist and that its sha1 hash equals the given
// returns true if all the checks pass
Expand Down Expand Up @@ -149,20 +151,22 @@ public static boolean validate(String name, String sha1) {

// This attempts to download a file if it isn't valid
public static void validateDownloadRetry(String url, String name, String sha1) {
byte times = 0;
if (url.isEmpty()) {
if (!ConnectionUtil.validate(name, sha1))
throw new VerifyError("Couldn't verify " + name);
return;
}
while (!ConnectionUtil.validate(name, sha1)) {
if (times == 3) {
Launcher.INSTANCEL.setPart(new ErrorPart(Launcher.INSTANCEL.getPart(), "Error verifying " + Paths.get(name).getFileName().toString(), "The file failed to download correctly after 3 tries!"));
break;
executors.submit(() -> {
byte times = 0;
if (url.isEmpty()) {
if (!ConnectionUtil.validate(name, sha1))
throw new VerifyError("Couldn't verify " + name);
return;
}
ConnectionUtil.download(url, name);
times++;
}
while (!ConnectionUtil.validate(name, sha1)) {
if (times == 3) {
Launcher.INSTANCEL.setPart(new ErrorPart(Launcher.INSTANCEL.getPart(), "Error verifying " + Paths.get(name).getFileName().toString(), "The file failed to download correctly after 3 tries!"));
break;
}
ConnectionUtil.download(url, name);
times++;
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static void update() {
String str = ConnectionUtil.getStringFromURL(RELEASE_API);
if (str == null)
return;
JSONArray obj = new JSONArray();
JSONArray obj = new JSONArray(str);
JSONObject newversion = obj.getJSONObject(0).getJSONArray("assets").getJSONObject(0);
String downloadURL = newversion.getString("browser_download_url");
File location = new File(StartupUtil.class.getProtectionDomain().getCodeSource().getLocation().toURI());
Expand Down

0 comments on commit 6ac5d3d

Please sign in to comment.