Skip to content

Commit

Permalink
Fixed issue with multiple download
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTroble committed Feb 16, 2021
1 parent 6ac5d3d commit ace0b0f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/troblecodings/launcher/HomePage.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void onExit() {

private void launch() {
isLaunching = true;
launch.setActivated(false);
launch.setEnabled(false);
connect.setVisible(true);
new Thread(() -> {
try {
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/com/troblecodings/launcher/node/ImageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,27 @@
import com.troblecodings.launcher.Launcher;
import com.troblecodings.launcher.assets.Assets;

public class ImageView extends Node{
public class ImageView extends Node {

protected final BufferedImage image;
protected final Runnable onButton;

public ImageView(int x1, int y1, int x2, int y2, String name) {
this(x1, y1, x2, y2, name, null);
}

public ImageView(int x1, int y1, int x2, int y2, String name, Runnable btn) {
super(x1, y1, x2, y2);
this.image = Assets.getImage(name);
this.onButton = btn;
}

@Override
public boolean update(int mousex, int mousey, int mousebtn) {
boolean b = super.update(mousex, mousey, mousebtn);
if(clicked && onButton != null)
onButton.run();
return b;
}

@Override
Expand Down
65 changes: 34 additions & 31 deletions src/main/java/com/troblecodings/launcher/util/ConnectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
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.ErrorPart;
Expand All @@ -30,8 +29,6 @@ public class ConnectionUtil {
public static final String URL = "";

private static void addHeader(HttpURLConnection connection) {
connection.setConnectTimeout(10000);
connection.setReadTimeout(10000);
connection.addRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:64.0) Gecko/20100101 Firefox/64.0");
}
Expand Down Expand Up @@ -67,12 +64,16 @@ public static boolean openConnection(final String url, final OutputStream channe
stream.close();
return true;
} catch (Exception e) {
if(e instanceof ConnectException || e instanceof SocketTimeoutException)
Launcher.INSTANCEL.setPart(new ErrorPart(Launcher.INSTANCEL.getPart(), "Connection error!", "No connection could be established!"));
else if(e instanceof MalformedURLException)
Launcher.INSTANCEL.setPart(new ErrorPart(Launcher.INSTANCEL.getPart(), "URL error!", "The URL was mallformed!"));
else if(e instanceof UnknownHostException)
Launcher.INSTANCEL.setPart(new ErrorPart(Launcher.INSTANCEL.getPart(), "Couldn't resolve host " + e.getMessage(), "Are you connected? No connection could be established!"));
if (e instanceof ConnectException || e instanceof SocketTimeoutException)
Launcher.INSTANCEL.setPart(new ErrorPart(Launcher.INSTANCEL.getPart(), "Connection error!",
"No connection could be established!"));
else if (e instanceof MalformedURLException)
Launcher.INSTANCEL
.setPart(new ErrorPart(Launcher.INSTANCEL.getPart(), "URL error!", "The URL was mallformed!"));
else if (e instanceof UnknownHostException)
Launcher.INSTANCEL
.setPart(new ErrorPart(Launcher.INSTANCEL.getPart(), "Couldn't resolve host " + e.getMessage(),
"Are you connected? No connection could be established!"));
e.printStackTrace();
return false;
}
Expand Down Expand Up @@ -101,9 +102,9 @@ public static boolean download(String url, String name, final Consumer<Long> upd
e1.printStackTrace();
}
}
try (OutputStream fos = Files.newOutputStream(pathtofile)) {
if(!openConnection(url, fos, update))

try (OutputStream fos = Files.newOutputStream(pathtofile, StandardOpenOption.CREATE)) {
if (!openConnection(url, fos, update))
return false;
} catch (Exception e) {
e.printStackTrace();
Expand All @@ -116,8 +117,6 @@ public static boolean download(String url, String name, final Consumer<Long> upd
}
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 @@ -148,25 +147,29 @@ public static boolean validate(String name, String sha1) {
}
return false;
}

public static void validateDownloadRetry(final String url, final String name, final String sha1) {
validateDownloadRetry(url, name, sha1, null);
}

// This attempts to download a file if it isn't valid
public static void validateDownloadRetry(String url, String name, String sha1) {
executors.submit(() -> {
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;
}
ConnectionUtil.download(url, name);
times++;
public static void validateDownloadRetry(final String url, final String name, final String sha1, final Consumer<Long> update) {
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;
}
});
ConnectionUtil.download(url, name, update);
times++;
}
}

}
33 changes: 14 additions & 19 deletions src/main/java/com/troblecodings/launcher/util/StartupUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.swing.JButton;
Expand Down Expand Up @@ -109,13 +107,16 @@ public static boolean prestart() throws Throwable {
String indexpath = indexes.toString() + "/" + assetIndex.getString("id") + ".json";
String indexurl = assetIndex.getString("url");
String indexsha1 = assetIndex.getString("sha1");
ConnectionUtil.validateDownloadRetry(indexurl, indexpath, indexsha1);
long sizeAsset = assetIndex.getLong("size");
ConnectionUtil.validateDownloadRetry(indexurl, indexpath, indexsha1, l -> Launcher.bar.update(l / sizeAsset));

Path ogMC = Paths.get(FileUtil.BASE_DIR + "/versions/" + object.getString("inheritsFrom") + "/"
+ object.getString("inheritsFrom") + ".jar");
Files.createDirectories(ogMC.getParent());
JSONObject clientDL = object.getJSONObject("downloads").getJSONObject("client");
ConnectionUtil.validateDownloadRetry(clientDL.getString("url"), ogMC.toString(), clientDL.getString("sha1"));
long sizeClient = clientDL.getLong("size");
ConnectionUtil.validateDownloadRetry(clientDL.getString("url"), ogMC.toString(), clientDL.getString("sha1"),
l -> Launcher.bar.update(l / sizeClient));
LIBPATHS = ogMC.toString() + ";";

JSONObject additional = object.getJSONObject("additional");
Expand All @@ -127,10 +128,7 @@ public static boolean prestart() throws Throwable {
JSONTokener tokener = new JSONTokener(Files.newInputStream(Paths.get(indexpath)));
JSONObject index = new JSONObject(tokener);
JSONObject objects = index.getJSONObject("objects");
final int maxLevel = arr.length() + objects.length() + additional.keySet().stream()
.collect(Collectors.summingInt(key -> additional.getJSONArray(key).length()));
AtomicInteger counter = new AtomicInteger(0);


// This part is to download the libs
for (Object libentry : arr) {
JSONObject libobj = (JSONObject) libentry;
Expand All @@ -142,7 +140,8 @@ public static boolean prestart() throws Throwable {
String sha1 = artifact.getString("sha1");

LIBPATHS += name + ";";
ConnectionUtil.validateDownloadRetry(url, name, sha1);
long size = artifact.getLong("size");
ConnectionUtil.validateDownloadRetry(url, name, sha1, l -> Launcher.bar.update(l / size));
} else {
JSONObject natives = libobj.getJSONObject("natives");
if (natives.has(OSSHORTNAME)) {
Expand All @@ -153,15 +152,13 @@ public static boolean prestart() throws Throwable {
String url = artifact.getString("url");
String name = FileUtil.LIB_DIR + "/" + artifact.getString("path");
String sha1 = artifact.getString("sha1");

ConnectionUtil.validateDownloadRetry(url, name, sha1);
long size = artifact.getLong("size");
ConnectionUtil.validateDownloadRetry(url, name, sha1, l -> Launcher.bar.update(l / size));

// Extract the natives
unzip(name, FileUtil.LIB_DIR);
}
}
counter.incrementAndGet();
Launcher.bar.update(counter.floatValue() / maxLevel);
}

// Asset lockup and download
Expand All @@ -179,27 +176,25 @@ public static boolean prestart() throws Throwable {
}
}
try {
long size = asset.getLong("size");
ConnectionUtil.validateDownloadRetry(baseurl + folder + "/" + hash, folderpath.toString() + "/" + hash,
hash);
hash, l -> Launcher.bar.update(l / size));
} catch (Throwable e) {
ErrorDialog.createDialog(e);
}
counter.incrementAndGet();
Launcher.bar.update(counter.floatValue() / maxLevel);
});

additional.keySet().forEach(key -> {
additional.getJSONArray(key).forEach(fileobj -> {
try {
JSONObject jfileobj = (JSONObject) fileobj;
long size = jfileobj.getLong("size");
Path path = Paths.get(FileUtil.BASE_DIR, key, jfileobj.getString("name"));
ConnectionUtil.validateDownloadRetry(jfileobj.getString("url"), path.toString(),
jfileobj.getString("sha1"));
jfileobj.getString("sha1"), l -> Launcher.bar.update(l / size));
} catch (Throwable e) {
ErrorDialog.createDialog(e);
}
counter.incrementAndGet();
Launcher.bar.update(counter.floatValue() / maxLevel);
});
});
Launcher.bar.update(0);
Expand Down

0 comments on commit ace0b0f

Please sign in to comment.