Search in sources :

Example 1 with LibraryDownloadManager

use of com.loohp.interactivechatdiscordsrvaddon.libs.LibraryDownloadManager in project InteractiveChat-DiscordSRV-Addon by LOOHP.

the class AssetsDownloader method loadLibraries.

public static void loadLibraries(File rootFolder) {
    try {
        File hashes = new File(rootFolder, "hashes.json");
        if (!hashes.exists()) {
            try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(hashes), StandardCharsets.UTF_8))) {
                pw.println("{}");
                pw.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        JSONObject json;
        try (InputStreamReader hashReader = new InputStreamReader(new FileInputStream(hashes), StandardCharsets.UTF_8)) {
            json = (JSONObject) new JSONParser().parse(hashReader);
        } catch (Throwable e) {
            new RuntimeException("Invalid hashes.json! It will be reset.", e).printStackTrace();
            json = new JSONObject();
        }
        String oldHash = InteractiveChatDiscordSrvAddon.plugin.defaultResourceHash = json.containsKey("libs") ? json.get("libs").toString() : "EMPTY";
        String oldVersion = json.containsKey("version") ? json.get("version").toString() : "EMPTY";
        File libsFolder = new File(rootFolder, "libs");
        libsFolder.mkdirs();
        LibraryDownloadManager downloadManager = new LibraryDownloadManager(libsFolder);
        String hash = downloadManager.getHash();
        if (!hash.equals(oldHash) || !InteractiveChatDiscordSrvAddon.plugin.getDescription().getVersion().equals(oldVersion)) {
            downloadManager.downloadLibraries((result, jarName) -> {
                if (result) {
                    Bukkit.getConsoleSender().sendMessage("[ICDiscordSrvAddon] Downloaded library \"" + jarName + "\"");
                } else {
                    Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[ICDiscordSrvAddon] Unable to download library \"" + jarName + "\"");
                }
            });
        }
        LibraryLoader.loadLibraries(libsFolder, (file, e) -> {
            String jarName = file.getName();
            if (e == null) {
                Bukkit.getConsoleSender().sendMessage("[ICDiscordSrvAddon] Loaded library \"" + jarName + "\"");
            } else {
                Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[ICDiscordSrvAddon] Unable to load library \"" + jarName + "\"");
                e.printStackTrace();
            }
        });
        json.put("libs", hash);
        json.put("version", InteractiveChatDiscordSrvAddon.plugin.getDescription().getVersion());
        try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(hashes), StandardCharsets.UTF_8))) {
            Gson g = new GsonBuilder().setPrettyPrinting().create();
            pw.println(g.toJson(new JsonParser().parse(json.toString())));
            pw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : LibraryDownloadManager(com.loohp.interactivechatdiscordsrvaddon.libs.LibraryDownloadManager) InputStreamReader(java.io.InputStreamReader) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) FileInputStream(java.io.FileInputStream) JSONObject(com.loohp.interactivechat.libs.org.json.simple.JSONObject) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) JSONParser(com.loohp.interactivechat.libs.org.json.simple.parser.JSONParser) File(java.io.File) PrintWriter(java.io.PrintWriter) JsonParser(com.google.gson.JsonParser)

Example 2 with LibraryDownloadManager

use of com.loohp.interactivechatdiscordsrvaddon.libs.LibraryDownloadManager in project InteractiveChat-DiscordSRV-Addon by LOOHP.

the class GUIMain method downloadAssets.

protected static void downloadAssets(String title, BufferedImage image, Icon icon) {
    File defaultAssetsFolder = new File("InteractiveChatDiscordSrvAddon/built-in", "Default");
    defaultAssetsFolder.mkdirs();
    File libsFolder = new File("InteractiveChatDiscordSrvAddon", "libs");
    libsFolder.mkdirs();
    JPanel panel = new JPanel();
    panel.add(GUIMain.createLabel("Select Minecraft Version: ", 13));
    JComboBox<String> options = new JComboBox<>();
    for (String version : ResourceDownloadManager.getMinecraftVersions()) {
        options.addItem(version);
    }
    panel.add(options);
    int result = JOptionPane.showOptionDialog(null, panel, title, JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, icon, null, null);
    if (result < 0) {
        return;
    }
    ResourceDownloadManager resourceDownloadManager = new ResourceDownloadManager((String) options.getSelectedItem(), defaultAssetsFolder);
    LibraryDownloadManager libraryDownloadManager = new LibraryDownloadManager(libsFolder);
    JFrame frame = new JFrame(title);
    frame.setIconImage(image);
    frame.setSize(800, 175);
    frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    panel = new JPanel();
    panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
    panel.setLayout(new GridLayout(0, 1));
    JLabel label = GUIMain.createLabel("<html>Downloading Assets:<html/>", 13);
    label.setSize(800, 125);
    panel.add(label);
    JProgressBar progressBar = new JProgressBar(0, 10000);
    panel.add(progressBar);
    frame.add(panel, BorderLayout.CENTER);
    frame.setResizable(false);
    frame.setVisible(true);
    CompletableFuture<Void> future = new CompletableFuture<>();
    new Thread(() -> {
        resourceDownloadManager.downloadResources((type, fileName, percentage) -> {
            switch(type) {
                case CLIENT_DOWNLOAD:
                    label.setText("<html>Downloading Assets:<br>Downloading client jar<html/>");
                    break;
                case EXTRACT:
                    label.setText("<html>Downloading Assets:<br>Extracting " + fileName + "<html/>");
                    break;
                case DOWNLOAD:
                    label.setText("<html>Downloading Assets:<br>Downloading " + fileName + "<html/>");
                    progressBar.setValue(Math.min(9999, (int) (percentage * 100)));
                    break;
                case DONE:
                    label.setText("<html>Done!<html/>");
                    break;
            }
        });
        libraryDownloadManager.downloadLibraries((downloadResult, jarName) -> {
            if (downloadResult) {
                label.setText("<html>Downloaded library \"" + jarName + "\"<html/>");
            }
        });
        future.complete(null);
    }).start();
    future.join();
    progressBar.setValue(9999);
    JOptionPane.showMessageDialog(null, createLabel("Assets saved at: " + defaultAssetsFolder.getAbsolutePath(), 15), title, JOptionPane.INFORMATION_MESSAGE, icon);
    frame.setVisible(false);
    frame.dispose();
}
Also used : Color(java.awt.Color) UIManager(javax.swing.UIManager) YamlConfiguration(com.loohp.interactivechat.libs.com.loohp.yamlconfiguration.YamlConfiguration) Version(com.loohp.interactivechat.updater.Version) Enumeration(java.util.Enumeration) URL(java.net.URL) Registry(com.loohp.interactivechat.registry.Registry) URISyntaxException(java.net.URISyntaxException) RenderingHints(java.awt.RenderingHints) CompletableFuture(java.util.concurrent.CompletableFuture) SwingConstants(javax.swing.SwingConstants) FileUtils(com.loohp.interactivechat.utils.FileUtils) GridLayout(java.awt.GridLayout) LinkedHashMap(java.util.LinkedHashMap) ResourceDownloadManager(com.loohp.interactivechatdiscordsrvaddon.resources.ResourceDownloadManager) Graphics2D(java.awt.Graphics2D) Map(java.util.Map) InteractiveChatRegistry(com.loohp.interactivechatdiscordsrvaddon.registry.InteractiveChatRegistry) ImageIO(javax.imageio.ImageIO) ImageIcon(javax.swing.ImageIcon) URI(java.net.URI) JComboBox(javax.swing.JComboBox) JFrame(javax.swing.JFrame) BorderLayout(java.awt.BorderLayout) LinkedList(java.util.LinkedList) JSONObject(com.loohp.interactivechat.libs.org.json.simple.JSONObject) JProgressBar(javax.swing.JProgressBar) PrintWriter(java.io.PrintWriter) Desktop(java.awt.Desktop) BufferedImage(java.awt.image.BufferedImage) Font(java.awt.Font) StringWriter(java.io.StringWriter) DecimalFormat(java.text.DecimalFormat) BorderFactory(javax.swing.BorderFactory) Icon(javax.swing.Icon) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) JOptionPane(javax.swing.JOptionPane) File(java.io.File) List(java.util.List) HTTPRequestUtils(com.loohp.interactivechat.utils.HTTPRequestUtils) JLabel(javax.swing.JLabel) LibraryDownloadManager(com.loohp.interactivechatdiscordsrvaddon.libs.LibraryDownloadManager) Entry(java.util.Map.Entry) ConfigurationSection(com.loohp.interactivechat.libs.com.loohp.yamlconfiguration.ConfigurationSection) JPanel(javax.swing.JPanel) WindowConstants(javax.swing.WindowConstants) Toolkit(java.awt.Toolkit) JPanel(javax.swing.JPanel) LibraryDownloadManager(com.loohp.interactivechatdiscordsrvaddon.libs.LibraryDownloadManager) JComboBox(javax.swing.JComboBox) JProgressBar(javax.swing.JProgressBar) JLabel(javax.swing.JLabel) GridLayout(java.awt.GridLayout) CompletableFuture(java.util.concurrent.CompletableFuture) ResourceDownloadManager(com.loohp.interactivechatdiscordsrvaddon.resources.ResourceDownloadManager) JFrame(javax.swing.JFrame) File(java.io.File)

Aggregations

JSONObject (com.loohp.interactivechat.libs.org.json.simple.JSONObject)2 LibraryDownloadManager (com.loohp.interactivechatdiscordsrvaddon.libs.LibraryDownloadManager)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 PrintWriter (java.io.PrintWriter)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 JsonParser (com.google.gson.JsonParser)1 ConfigurationSection (com.loohp.interactivechat.libs.com.loohp.yamlconfiguration.ConfigurationSection)1 YamlConfiguration (com.loohp.interactivechat.libs.com.loohp.yamlconfiguration.YamlConfiguration)1 JSONParser (com.loohp.interactivechat.libs.org.json.simple.parser.JSONParser)1 Registry (com.loohp.interactivechat.registry.Registry)1 Version (com.loohp.interactivechat.updater.Version)1 FileUtils (com.loohp.interactivechat.utils.FileUtils)1 HTTPRequestUtils (com.loohp.interactivechat.utils.HTTPRequestUtils)1 InteractiveChatRegistry (com.loohp.interactivechatdiscordsrvaddon.registry.InteractiveChatRegistry)1 ResourceDownloadManager (com.loohp.interactivechatdiscordsrvaddon.resources.ResourceDownloadManager)1 BorderLayout (java.awt.BorderLayout)1 Color (java.awt.Color)1 Desktop (java.awt.Desktop)1