diff --git a/src/main/java/com/troblecodings/launcher/HomePage.java b/src/main/java/com/troblecodings/launcher/HomePage.java index 9f99524..1d02ed2 100644 --- a/src/main/java/com/troblecodings/launcher/HomePage.java +++ b/src/main/java/com/troblecodings/launcher/HomePage.java @@ -46,7 +46,7 @@ public void onExit() { private void launch() { isLaunching = true; - launch.setActivated(false); + launch.setEnabled(false); connect.setVisible(true); new Thread(() -> { try { diff --git a/src/main/java/com/troblecodings/launcher/node/ImageView.java b/src/main/java/com/troblecodings/launcher/node/ImageView.java index a146789..a3da882 100644 --- a/src/main/java/com/troblecodings/launcher/node/ImageView.java +++ b/src/main/java/com/troblecodings/launcher/node/ImageView.java @@ -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 diff --git a/src/main/java/com/troblecodings/launcher/util/ConnectionUtil.java b/src/main/java/com/troblecodings/launcher/util/ConnectionUtil.java index 7003da6..b8b4ed1 100644 --- a/src/main/java/com/troblecodings/launcher/util/ConnectionUtil.java +++ b/src/main/java/com/troblecodings/launcher/util/ConnectionUtil.java @@ -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; @@ -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"); } @@ -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; } @@ -101,9 +102,9 @@ public static boolean download(String url, String name, final Consumer 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(); @@ -116,8 +117,6 @@ public static boolean download(String url, String name, final Consumer 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 @@ -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 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++; + } } } diff --git a/src/main/java/com/troblecodings/launcher/util/StartupUtil.java b/src/main/java/com/troblecodings/launcher/util/StartupUtil.java index 166a383..8baee61 100644 --- a/src/main/java/com/troblecodings/launcher/util/StartupUtil.java +++ b/src/main/java/com/troblecodings/launcher/util/StartupUtil.java @@ -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; @@ -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"); @@ -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; @@ -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)) { @@ -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 @@ -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);