From 2399583841c5f06274903014b80a1538f6bd3791 Mon Sep 17 00:00:00 2001 From: coolGi Date: Sun, 26 Jun 2022 20:28:32 +0930 Subject: [PATCH] Added network stuff to standalone jar --- .../java/com/seibel/lod/core/JarMain.java | 28 +++--- .../com/seibel/lod/core/jar/BaseJFrame.java | 23 ++++- .../lod/core/jar/installer/GitlabGetter.java | 85 +++++++++++++++++++ .../config/ConfigWrapper.java | 7 +- 4 files changed, 120 insertions(+), 23 deletions(-) create mode 100644 src/main/java/com/seibel/lod/core/jar/installer/GitlabGetter.java diff --git a/src/main/java/com/seibel/lod/core/JarMain.java b/src/main/java/com/seibel/lod/core/JarMain.java index ae4be79c6..922979a39 100644 --- a/src/main/java/com/seibel/lod/core/JarMain.java +++ b/src/main/java/com/seibel/lod/core/JarMain.java @@ -2,15 +2,13 @@ package com.seibel.lod.core; import com.formdev.flatlaf.FlatDarkLaf; import com.formdev.flatlaf.FlatLightLaf; -import com.formdev.flatlaf.extras.FlatSVGIcon; import com.seibel.lod.core.handlers.dependencyInjection.SingletonHandler; import com.seibel.lod.core.jar.DarkModeDetector; import com.seibel.lod.core.jar.BaseJFrame; +import com.seibel.lod.core.jar.installer.GitlabGetter; import com.seibel.lod.core.jar.JarDependencySetup; -import javax.imageio.ImageIO; import javax.swing.*; -import java.awt.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.util.Locale; @@ -22,6 +20,7 @@ import java.util.Locale; */ public class JarMain { public static final boolean isDarkTheme = DarkModeDetector.isDarkMode(); + public static boolean isOffline = GitlabGetter.netIsAvailable(); public static void main(String[] args) { // Sets up the local @@ -40,7 +39,7 @@ public class JarMain { SingletonHandler.finishBinding(); System.out.println("WARNING: The standalone jar still work in progress"); - JOptionPane.showMessageDialog(null, "The GUI for the standalone jar isn't made yet\nIf you want to use the mod then put it in your mods folder", "Distant Horizons", JOptionPane.WARNING_MESSAGE); +// JOptionPane.showMessageDialog(null, "The GUI for the standalone jar isn't made yet\nIf you want to use the mod then put it in your mods folder", "Distant Horizons", JOptionPane.WARNING_MESSAGE); if (!getOperatingSystem().equals(OperatingSystem.LINUX)) { System.out.println("If you want the installer then please use linux for the time being.\nWindows and MacOS support will come later on"); @@ -53,22 +52,17 @@ public class JarMain { frame.add(jTest); jTest.addActionListener(e -> { System.out.println("test"); }); - int logoHeight = 200; - int logoWidth = (int) ((double) logoHeight *2.21); - JPanel pane = new JPanel() { - @Override - protected void paintComponent(Graphics g) { - super.paintComponent(g); - try { - g.drawImage(ImageIO.read(JarMain.accessFile("logo.png")), (frame.getWidth()/2)-(logoWidth/2), 0, logoWidth, logoHeight,this); - } catch (Exception e) {e.printStackTrace();} - } - }; - pane.setBounds(0, 0, pane.getWidth(), pane.getHeight()); - frame.add(pane); + System.out.println(GitlabGetter.downloadAsString("https://gitlab.com/api/v4/projects/18204078/releases")); + + // Fabric installer +// try { +// GitlabGetter.downloadAsFile("https://maven.fabricmc.net/net/fabricmc/fabric-installer/0.11.0/fabric-installer-0.11.0.jar", new File(System.getProperty("java.io.tmpdir") + "/fabricInstaller.jar")); +// Runtime.getRuntime().exec("java -jar " + System.getProperty("java.io.tmpdir") + "/fabricInstaller.jar"); +// } catch (Exception e) {e.printStackTrace();} + frame.addLogo(); frame.validate(); // Update to add the widgets frame.setVisible(true); // Start the ui diff --git a/src/main/java/com/seibel/lod/core/jar/BaseJFrame.java b/src/main/java/com/seibel/lod/core/jar/BaseJFrame.java index 800f5a41f..058fc1b29 100644 --- a/src/main/java/com/seibel/lod/core/jar/BaseJFrame.java +++ b/src/main/java/com/seibel/lod/core/jar/BaseJFrame.java @@ -41,7 +41,7 @@ public class BaseJFrame extends JFrame { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } - public BaseJFrame addExtraButtons() { + public BaseJFrame addExtraButtons() { // TODO: Change everything to paint rather than using J stuff // ========== LANGUAGE ========== int langBoxHeight = 25; int langBoxWidth = 100; @@ -102,6 +102,27 @@ public class BaseJFrame extends JFrame { add(darkMode); + return this; + } + + public BaseJFrame addLogo() { + int logoHeight = 200; + + JPanel logo = new JPanel() { + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + try { + BufferedImage image = ImageIO.read(JarMain.accessFile("logo.png")); + int logoWidth = (int) ((double) logoHeight * ((double) image.getWidth() / (double) image.getHeight())); // Calculate the aspect ratio and set the height correctly to not stretch it + g.drawImage(image, (getWidth()/2)-(logoWidth/2), 0, logoWidth, logoHeight,this); // Resize image and draw it + } catch (Exception e) {e.printStackTrace();} + } + }; + logo.setBounds(logo.getX(), logo.getY(), logo.getWidth(), logo.getHeight()); + + add(logo); + return this; } } diff --git a/src/main/java/com/seibel/lod/core/jar/installer/GitlabGetter.java b/src/main/java/com/seibel/lod/core/jar/installer/GitlabGetter.java new file mode 100644 index 000000000..ec7dc2595 --- /dev/null +++ b/src/main/java/com/seibel/lod/core/jar/installer/GitlabGetter.java @@ -0,0 +1,85 @@ +package com.seibel.lod.core.jar.installer; + +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; + +import javax.net.ssl.HttpsURLConnection; +import javax.swing.*; +import java.io.*; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; + +public class GitlabGetter { + public static final String GitLabApi = "https://gitlab.com/api/v4/projects/"; + public static final String projectID = "18204078"; + + public static JSONObject projectReleases = new JSONObject(); + + public static void init() { + try { + projectReleases = (JSONObject) new JSONParser().parse(downloadAsString(GitLabApi+projectID+"/releases")); + } catch (Exception e) { e.printStackTrace(); } + } + + + public static boolean netIsAvailable() { + try { + final URL url = new URL("https://gitlab.com"); + final URLConnection conn = url.openConnection(); + conn.connect(); + conn.getInputStream().close(); + return true; + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } catch (IOException e) { + return false; + } + } + + + public static void downloadAsFile(String urlS, File file) { + try { + URL url = new URL(urlS); + HttpsURLConnection connection = (HttpsURLConnection) url + .openConnection(); + long filesize = connection.getContentLengthLong(); + if (filesize == -1) { + throw new Exception("Content length must not be -1 (unknown)!"); + } + long totalDataRead = 0; + try (java.io.BufferedInputStream in = new java.io.BufferedInputStream( + connection.getInputStream())) { + java.io.FileOutputStream fos = new java.io.FileOutputStream(file); + try (java.io.BufferedOutputStream bout = new BufferedOutputStream( + fos, 1024)) { + byte[] data = new byte[1024]; + int i; + while ((i = in.read(data, 0, 1024)) >= 0) { + totalDataRead = totalDataRead + i; + bout.write(data, 0, i); +// int percent = (int) ((totalDataRead * 100) / filesize); +// System.out.println(percent); + } + } + } + } catch (Exception e) { e.printStackTrace(); } + } + + public static String downloadAsString(String urlS) { + StringBuilder stringBuilder = new StringBuilder(); + try { + URL url = new URL(urlS); + URLConnection urlConnection = url.openConnection(); + urlConnection.setConnectTimeout(1000); + urlConnection.setReadTimeout(1000); + BufferedReader bReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); + + String line; + while ((line = bReader.readLine()) != null) { + stringBuilder.append(line); + } + } catch (Exception e) { e.printStackTrace(); } + return (stringBuilder.toString()); + } +} diff --git a/src/main/java/com/seibel/lod/core/jar/wrapperInterfaces/config/ConfigWrapper.java b/src/main/java/com/seibel/lod/core/jar/wrapperInterfaces/config/ConfigWrapper.java index 6dc5336ea..56c2ea490 100644 --- a/src/main/java/com/seibel/lod/core/jar/wrapperInterfaces/config/ConfigWrapper.java +++ b/src/main/java/com/seibel/lod/core/jar/wrapperInterfaces/config/ConfigWrapper.java @@ -14,11 +14,8 @@ public class ConfigWrapper implements IConfigWrapper { public static void init() { try { - Object obj = new JSONParser().parse(JarMain.convertInputStreamToString(JarMain.accessFile("assets/lod/lang/"+ Locale.getDefault().toString().toLowerCase()+".json"))); - jsonObject = (JSONObject) obj; - } catch (ParseException e) { - e.printStackTrace(); - } + jsonObject = (JSONObject) new JSONParser().parse(JarMain.convertInputStreamToString(JarMain.accessFile("assets/lod/lang/"+ Locale.getDefault().toString().toLowerCase()+".json"))); + } catch (ParseException e) { e.printStackTrace(); } } @Override