diff --git a/src/main/java/com/troblecodings/launcher/ErrorPart.java b/src/main/java/com/troblecodings/launcher/ErrorPart.java new file mode 100644 index 0000000..1223818 --- /dev/null +++ b/src/main/java/com/troblecodings/launcher/ErrorPart.java @@ -0,0 +1,39 @@ +package com.troblecodings.launcher; + +import java.awt.Color; + +import com.troblecodings.launcher.node.ImageView; +import com.troblecodings.launcher.node.Label; +import com.troblecodings.launcher.node.MiddlePart; +import com.troblecodings.launcher.util.FontUtil; + +public class ErrorPart extends MiddlePart{ + + private static int counter = 0; + + public ErrorPart(final MiddlePart last, final String errorHeader, final String error) { + this.add(new ImageView(0, 0, Launcher.WIDTH, Launcher.HEIGHT, "error.png")); + + Label lab1 = new Label((Launcher.WIDTH - 412) / 2, 310, (Launcher.WIDTH + 412) / 2, 330, Color.RED, errorHeader); + lab1.setFont(FontUtil.getFont(30f)); + this.add(lab1); + + Label lab2 = new Label((Launcher.WIDTH - 412) / 2, 360, (Launcher.WIDTH + 412) / 2, 380, Color.RED, error); + this.add(lab2); + + Label num = new Label((Launcher.WIDTH - 412) / 2, 460, (Launcher.WIDTH + 412) / 2, 480, Color.GRAY, String.valueOf(counter)); + this.add(num); + + Label lab3 = new Label((Launcher.WIDTH - 412) / 2, 510, (Launcher.WIDTH + 412) / 2, 530, Color.GRAY, "Back", + () -> Launcher.INSTANCEL.setPart(last == null ? new LoginPage():last)); + lab3.setFont(FontUtil.getFont(20f)); + this.add(lab3); + counter++; + } + + @Override + public void onExit() { + counter--; + } + +} diff --git a/src/main/java/com/troblecodings/launcher/HomePage.java b/src/main/java/com/troblecodings/launcher/HomePage.java index 8abf82a..1d02ed2 100644 --- a/src/main/java/com/troblecodings/launcher/HomePage.java +++ b/src/main/java/com/troblecodings/launcher/HomePage.java @@ -22,7 +22,7 @@ public HomePage() { this.add(new ImageView(0, 0, Launcher.WIDTH, Launcher.HEIGHT, "launchback.png")); logo.scaleChange = true; this.add(logo); - + if (launch == null) launch = new Button(465, 577, 465 + 350, 677, "launchbutton.png", this::launch); launch.scaleChange = true; @@ -35,42 +35,42 @@ public HomePage() { connect.setVisible(true); } this.add(connect); - - this.add(new Label((Launcher.WIDTH - 412) / 2, 685, (Launcher.WIDTH + 412) / 2, 705, Color.GRAY, "Lizenzen & Kredits", () -> Launcher.INSTANCEL.setPart(new CreditPage(this)))); + + this.add(new Label((Launcher.WIDTH - 412) / 2, 685, (Launcher.WIDTH + 412) / 2, 705, Color.GRAY, + "Lizenzen & Kredits", () -> Launcher.INSTANCEL.setPart(new CreditPage(this)))); } @Override - public void onExit() {} - + public void onExit() { + } + private void launch() { isLaunching = true; - launch.setActivated(false); + launch.setEnabled(false); connect.setVisible(true); new Thread(() -> { try { - StartupUtil.prestart(); - if (AuthUtil.START_PARAMS != null) { - Process pro = StartupUtil.start(AuthUtil.START_PARAMS); - Launcher.INSTANCE.setVisible(false); - pro.waitFor(); - Launcher.INSTANCE.setVisible(true); - connect.setVisible(false); - launch.setActivated(true); - isLaunching = false; - return; + if (StartupUtil.prestart()) { + if (AuthUtil.START_PARAMS != null) { + Process pro = StartupUtil.start(AuthUtil.START_PARAMS); + Launcher.INSTANCE.setVisible(false); + pro.waitFor(); + Launcher.INSTANCE.setVisible(true); + connect.setVisible(false); + launch.setActivated(true); + isLaunching = false; + return; + } + LoginPage page = new LoginPage(); + LoginPage.label.setText("There was an error with your credentials!"); + Launcher.INSTANCEL.setPart(page); } - LoginPage page = new LoginPage(); - LoginPage.label.setText("There was an error with your credentials!"); - Launcher.INSTANCEL.setPart(page); - connect.setVisible(false); - launch.setActivated(true); - isLaunching = false; } catch (Throwable e) { ErrorDialog.createDialog(e); - connect.setVisible(false); - launch.setActivated(true); - isLaunching = false; } + connect.setVisible(false); + launch.setActivated(true); + isLaunching = false; }).start(); } diff --git a/src/main/java/com/troblecodings/launcher/Launcher.java b/src/main/java/com/troblecodings/launcher/Launcher.java index cc0d3b5..2693cfd 100644 --- a/src/main/java/com/troblecodings/launcher/Launcher.java +++ b/src/main/java/com/troblecodings/launcher/Launcher.java @@ -30,7 +30,6 @@ import com.troblecodings.launcher.node.ProgressBar; import com.troblecodings.launcher.util.AuthUtil; import com.troblecodings.launcher.util.FileUtil; -import com.troblecodings.launcher.util.FontUtil; import com.troblecodings.launcher.util.StartupUtil; import net.cydhra.nidhogg.exception.TooManyRequestsException; @@ -50,27 +49,12 @@ public Launcher() { * Launch the application. */ public static void main(String[] args) { - FontUtil.init(); - try { - StartupUtil.update(); - FileUtil.init(); - AuthUtil.auth(null, null); - } catch (TooManyRequestsException e) { - try { - Thread.sleep(10000); - main(args); - return; - } catch (InterruptedException e1) { - ErrorDialog.createDialog(e1); - } - } catch (RuntimeException e2) { - LoginPage.label.setText("There was an error with your saved session!"); - } catch (Throwable e1) { - ErrorDialog.createDialog(e1); - } + INSTANCEL = new Launcher(); + INSTANCE = new JFrame(); + INSTANCEL.setPart(new HomePage()); + StartupUtil.update(); EventQueue.invokeLater(() -> { try { - INSTANCE = new JFrame(); INSTANCE.setTitle("Launcher"); INSTANCE.setResizable(false); INSTANCE.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); @@ -81,15 +65,32 @@ public static void main(String[] args) { INSTANCE.setIconImage(Assets.getImage("icon.png")); INSTANCE.setBackground(new Color(0, 0, 0, 0)); - INSTANCEL = new Launcher(); INSTANCEL.setSize(WIDTH, HEIGHT); INSTANCE.add(INSTANCEL); INSTANCEL.init(); INSTANCE.setVisible(true); + INSTANCE.repaint(); } catch (Exception e) { ErrorDialog.createDialog(e); } }); + + try { + FileUtil.init(); + AuthUtil.auth(null, null); + } catch (TooManyRequestsException e) { + try { + Thread.sleep(10000); + main(args); + return; + } catch (InterruptedException e1) { + ErrorDialog.createDialog(e1); + } + } catch (RuntimeException e2) { + LoginPage.label.setText("There - an error with your saved session!"); + } catch (Throwable e1) { + ErrorDialog.createDialog(e1); + } } private static ArrayList nodes = new ArrayList(); @@ -100,26 +101,25 @@ public static void main(String[] args) { protected Button settings, home, launch; public static final ProgressBar bar = new ProgressBar(0, 630, WIDTH, 10, Color.GREEN.darker().darker()); - - public void init() { - part = new HomePage(); + public void init() { addMouseListener(this); addMouseMotionListener(this); addKeyListener(this); - + nodes.add(new ImageView(0, 0, WIDTH, HEIGHT, "background.png")); nodes.add(bar); - + Button close = new Button(WIDTH - 79, 20, WIDTH - 27, 87, "closebutton.png", this::exit); close.scaleChange = true; close.scaleFactor = 1.1f; nodes.add(close); home = new Button((WIDTH - 120) / 2, 20, (WIDTH - 120) / 2 + 120, 42, "homebuttonoff.png", this::home); - settings = new Button((WIDTH - 167) / 2, 20, (WIDTH - 167) / 2 + 167, 42, "settingbuttonoff.png", this::settings); + settings = new Button((WIDTH - 167) / 2, 20, (WIDTH - 167) / 2 + 167, 42, "settingbuttonoff.png", + this::settings); home.setEnabled(false); - + nodes.add(home); nodes.add(settings); @@ -135,13 +135,13 @@ public void init() { } setIgnoreRepaint(false); } - + public static BufferedImage buffer = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB); public static Graphics2D graph = buffer.createGraphics(); - + @Override public void paint(Graphics g) { - Graphics2D g2 = (Graphics2D)g; + Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); @@ -151,40 +151,44 @@ public void paint(Graphics g) { part.paint(graph); g.drawImage(buffer, 0, 0, this); } - + private void settings() { settings.setEnabled(false); this.setPart(new SettingsPage()); home.setEnabled(true); } - + private void home() { home.setEnabled(false); this.setPart(new HomePage()); settings.setEnabled(true); } + public MiddlePart getPart() { + return this.part; + } + public void setPart(MiddlePart part) { - this.part.onExit(); + if(this.part != null) + this.part.onExit(); System.gc(); this.part = part; INSTANCE.repaint(); } - + private void exit() { try { if (Files.notExists(FileUtil.SETTINGSPATH)) Files.createFile(FileUtil.SETTINGSPATH); - Files.write(FileUtil.SETTINGSPATH, (StartupUtil.LWIDTH + System.lineSeparator() + - StartupUtil.LHEIGHT + System.lineSeparator() + - StartupUtil.RAM + System.lineSeparator() + - SettingsPage.NEWBASEDIR).getBytes()); + Files.write(FileUtil.SETTINGSPATH, + (StartupUtil.LWIDTH + System.lineSeparator() + StartupUtil.LHEIGHT + System.lineSeparator() + + StartupUtil.RAM + System.lineSeparator() + SettingsPage.NEWBASEDIR).getBytes()); } catch (IOException e) { ErrorDialog.createDialog(e); } System.exit(0); } - + /* * I want to take a minute to express my hate against the swing / AWT API Just * horrible @@ -201,18 +205,18 @@ public void mouseEntered(MouseEvent e) { @Override public void mouseExited(MouseEvent e) { } - + private static boolean enablePress = true; @Override public void mousePressed(MouseEvent e) { - if(!enablePress) + if (!enablePress) return; enablePress = false; dx = e.getX(); dy = e.getY(); - for(Node n : nodes) { - if(n.update(e.getX(), e.getY(), e.getButton())) + for (Node n : nodes) { + if (n.update(e.getX(), e.getY(), e.getButton())) break; } part.mousePressed(e); diff --git a/src/main/java/com/troblecodings/launcher/assets/error.png b/src/main/java/com/troblecodings/launcher/assets/error.png new file mode 100644 index 0000000..951bc07 Binary files /dev/null and b/src/main/java/com/troblecodings/launcher/assets/error.png differ 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 99c1cf2..b8b4ed1 100644 --- a/src/main/java/com/troblecodings/launcher/util/ConnectionUtil.java +++ b/src/main/java/com/troblecodings/launcher/util/ConnectionUtil.java @@ -1,19 +1,28 @@ package com.troblecodings.launcher.util; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.math.BigInteger; +import java.net.ConnectException; import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.SocketTimeoutException; import java.net.URL; +import java.net.UnknownHostException; import java.nio.file.Files; 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.function.Consumer; -import com.troblecodings.launcher.ErrorDialog; +import com.troblecodings.launcher.ErrorPart; +import com.troblecodings.launcher.Launcher; public class ConnectionUtil { @@ -24,84 +33,127 @@ private static void addHeader(HttpURLConnection connection) { "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:64.0) Gecko/20100101 Firefox/64.0"); } - public static void openConnection(final String url, final OutputStream channel) throws Throwable { - openConnection(url, channel, null); + public static boolean openConnection(final String url, final OutputStream channel) { + return openConnection(url, channel, null); } - public static void openConnection(final String url, final OutputStream channel, final Consumer update) - throws Throwable { - URL urlcon = new URL(url); + public static boolean openConnection(final String url, final OutputStream channel, final Consumer update) { + try { + URL urlcon = new URL(url); - HttpURLConnection connection = (HttpURLConnection) urlcon.openConnection(); - addHeader(connection); - int resp = connection.getResponseCode(); - if (resp == HttpURLConnection.HTTP_MOVED_PERM || resp == HttpURLConnection.HTTP_MOVED_TEMP - || resp == HttpURLConnection.HTTP_SEE_OTHER) { - String newUrl = connection.getHeaderField("Location"); - connection = (HttpURLConnection) new URL(newUrl).openConnection(); + HttpURLConnection connection = (HttpURLConnection) urlcon.openConnection(); addHeader(connection); - } - InputStream stream = connection.getInputStream(); - byte[] buf = new byte[8192]; - int length = 0; - long bytesread = 0; - while ((length = stream.read(buf)) > 0) { - channel.write(buf, 0, length); - if (update != null) { - bytesread += length; - update.accept(bytesread); + int resp = connection.getResponseCode(); + if (resp == HttpURLConnection.HTTP_MOVED_PERM || resp == HttpURLConnection.HTTP_MOVED_TEMP + || resp == HttpURLConnection.HTTP_SEE_OTHER) { + String newUrl = connection.getHeaderField("Location"); + connection = (HttpURLConnection) new URL(newUrl).openConnection(); + addHeader(connection); + } + InputStream stream = connection.getInputStream(); + byte[] buf = new byte[8192]; + int length = 0; + long bytesread = 0; + while ((length = stream.read(buf)) > 0) { + channel.write(buf, 0, length); + if (update != null) { + bytesread += length; + update.accept(bytesread); + } } + 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!")); + e.printStackTrace(); + return false; } - stream.close(); } - public static String getStringFromURL(String url) throws Throwable { + public static String getStringFromURL(String url) { ByteArrayOutputStream output = new ByteArrayOutputStream(); - openConnection(url, output); + if (!openConnection(url, output)) + return null; return new String(output.toByteArray()); } // Downloads a given file from the given URL onto the machine - public static void download(String url, String name) throws Throwable { - download(url, name, null); + public static boolean download(String url, String name) { + return download(url, name, null); } - public static void download(String url, String name, final Consumer update) throws Throwable { - Path pathtofile = Paths.get(name); + public static boolean download(String url, String name, final Consumer update) { + Path pathtofile = Paths.get(name + ".tmp"); Path parent = pathtofile.getParent(); - if (parent != null) - Files.createDirectories(parent); + if (parent != null) { + try { + Files.createDirectories(parent); + } catch (IOException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + } - OutputStream fos = Files.newOutputStream(pathtofile); - openConnection(url, fos, update); - fos.close(); + try (OutputStream fos = Files.newOutputStream(pathtofile, StandardOpenOption.CREATE)) { + if (!openConnection(url, fos, update)) + return false; + } catch (Exception e) { + e.printStackTrace(); + } + Path normalFile = Paths.get(name); + try { + Files.move(pathtofile, normalFile, StandardCopyOption.REPLACE_EXISTING); + } catch (IOException e) { + e.printStackTrace(); + } + return true; } // Checks if the file exist and that its sha1 hash equals the given // returns true if all the checks pass - public static boolean validate(String name, String sha1) throws Throwable { - MessageDigest digest = MessageDigest.getInstance("SHA-1"); - Path pathtofile = Paths.get(name); - if (!Files.exists(pathtofile)) - return false; - try (DigestInputStream stream = new DigestInputStream(Files.newInputStream(pathtofile), digest)) { - while (true) { - byte[] buffer = new byte[8192]; - if (stream.read(buffer, 0, 8192) <= 0) - break; + public static boolean validate(String name, String sha1) { + try { + MessageDigest digest = MessageDigest.getInstance("SHA-1"); + Path pathtofile = Paths.get(name); + if (!Files.exists(pathtofile)) + return false; + try (DigestInputStream stream = new DigestInputStream(Files.newInputStream(pathtofile), digest)) { + while (true) { + byte[] buffer = new byte[8192]; + if (stream.read(buffer, 0, 8192) <= 0) + break; + } + byte[] digestreturn = digest.digest(); + BigInteger sha1bigintegers = new BigInteger(1, digestreturn); + String sha1result = sha1bigintegers.toString(16); + while (sha1result.length() < 40) { + sha1result = "0" + sha1result; + } + return sha1result.equalsIgnoreCase(sha1); + } catch (IOException e) { + e.printStackTrace(); } - byte[] digestreturn = digest.digest(); - BigInteger sha1bigintegers = new BigInteger(1, digestreturn); - String sha1result = sha1bigintegers.toString(16); - while (sha1result.length() < 40) { - sha1result = "0" + sha1result; - } - return sha1result.equalsIgnoreCase(sha1); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); } + 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) throws Throwable { + 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)) @@ -109,11 +161,13 @@ public static void validateDownloadRetry(String url, String name, String sha1) t return; } while (!ConnectionUtil.validate(name, sha1)) { - ConnectionUtil.download(url, name); - if (times == 5) { - ErrorDialog.createDialog(new VerifyError("Couldn't verify " + name)); + 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/FontUtil.java b/src/main/java/com/troblecodings/launcher/util/FontUtil.java index 4d8274f..59810d3 100644 --- a/src/main/java/com/troblecodings/launcher/util/FontUtil.java +++ b/src/main/java/com/troblecodings/launcher/util/FontUtil.java @@ -1,22 +1,20 @@ package com.troblecodings.launcher.util; import java.awt.Font; -import java.awt.FontFormatException; import java.awt.GraphicsEnvironment; -import java.io.IOException; import com.troblecodings.launcher.assets.Assets; public class FontUtil { - private static Font defaultFont = null; + private static Font defaultFont = init(); - public static void init() { + private static Font init() { try { - defaultFont = Font.createFont(Font.TRUETYPE_FONT, Assets.getResourceAsStream("font.otf")); - } catch (FontFormatException | IOException e) { + return Font.createFont(Font.TRUETYPE_FONT, Assets.getResourceAsStream("font.otf")); + } catch (Throwable e) { // Fallback font - defaultFont = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts()[0]; + return GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts()[0]; } } diff --git a/src/main/java/com/troblecodings/launcher/util/StartupUtil.java b/src/main/java/com/troblecodings/launcher/util/StartupUtil.java index 44cb698..8baee61 100644 --- a/src/main/java/com/troblecodings/launcher/util/StartupUtil.java +++ b/src/main/java/com/troblecodings/launcher/util/StartupUtil.java @@ -11,20 +11,20 @@ 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; import javax.swing.ProgressMonitor; import org.json.JSONArray; +import org.json.JSONException; import org.json.JSONObject; import org.json.JSONTokener; import com.troblecodings.launcher.ErrorDialog; +import com.troblecodings.launcher.ErrorPart; import com.troblecodings.launcher.Launcher; public class StartupUtil { @@ -53,7 +53,10 @@ private static String getOSShortName() { public static void update() { try { - JSONArray obj = new JSONArray(ConnectionUtil.getStringFromURL(RELEASE_API)); + String str = ConnectionUtil.getStringFromURL(RELEASE_API); + if (str == null) + return; + 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()); @@ -69,17 +72,28 @@ public static void update() { System.exit(0); } catch (Throwable e) { e.printStackTrace(); + Launcher.INSTANCEL.setPart(new ErrorPart(Launcher.INSTANCEL.getPart(), "Update error!", "General error!")); } } - public static void prestart() throws Throwable { + public static boolean prestart() throws Throwable { String clientJson = FileUtil.BASE_DIR + "/GIR.json"; ConnectionUtil.download("https://girc.eu/Launcher/GIR.json", clientJson); - if (Files.notExists(Paths.get(clientJson))) - throw new VerifyError("Couldn't download GIR.json"); + if (Files.notExists(Paths.get(clientJson))) { + Launcher.INSTANCEL.setPart(new ErrorPart(Launcher.INSTANCEL.getPart(), "Missing version information!", + "The GIR version json could not be found.")); + return false; + } String content = new String(Files.readAllBytes(Paths.get(clientJson))); - JSONObject object = new JSONObject(content); + JSONObject object; + try { + object = new JSONObject(content); + } catch (JSONException e) { + Launcher.INSTANCEL.setPart(new ErrorPart(Launcher.INSTANCEL.getPart(), "Corrupted version information!", + "The GIR version json could not be read.")); + return false; + } MAINCLASS = object.getString("mainClass"); @@ -93,13 +107,16 @@ public static void 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"); @@ -111,9 +128,6 @@ public static void 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) { @@ -126,7 +140,8 @@ public static void 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)) { @@ -137,15 +152,13 @@ public static void 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 @@ -163,30 +176,29 @@ public static void 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); + return true; } private static void unzip(String name, String base) throws Throwable {