From a9bd83e96b81e2c62382fa4c4e6a4a184b7f2792 Mon Sep 17 00:00:00 2001 From: comp500 Date: Fri, 9 Aug 2019 18:15:47 +0100 Subject: [PATCH] Implement some stuff, edge case checks, intellij optimisations --- .../infra/packwiz/installer/DownloadTask.java | 10 +- .../packwiz/installer/UpdateManager.java | 19 +-- .../packwiz/installer/ui/CLIHandler.java | 9 +- .../packwiz/installer/ui/InstallWindow.java | 144 ++++++++---------- 4 files changed, 85 insertions(+), 97 deletions(-) diff --git a/src/main/java/link/infra/packwiz/installer/DownloadTask.java b/src/main/java/link/infra/packwiz/installer/DownloadTask.java index 9acaae7..5d9ae2a 100644 --- a/src/main/java/link/infra/packwiz/installer/DownloadTask.java +++ b/src/main/java/link/infra/packwiz/installer/DownloadTask.java @@ -102,7 +102,7 @@ class DownloadTask implements IOptionDetails { public void download(String packFolder, URI indexUri) { if (failure != null) return; if (alreadyUpToDate) return; - if (metadata.linkedFile != null && !downloadSide.hasSide(metadata.linkedFile.side)) return; + if (!correctSide()) return; Path destPath = Paths.get(packFolder, metadata.getDestURI().toString()); @@ -165,7 +165,6 @@ class DownloadTask implements IOptionDetails { cachedFile.linkedFileHash = metadata.linkedFile.getHash(); } catch (Exception e) { failure = e; - return; } } } @@ -186,6 +185,13 @@ class DownloadTask implements IOptionDetails { return isOptional() && this.newOptional; } + public boolean correctSide() { + if (metadata.linkedFile != null) { + return downloadSide.hasSide(metadata.linkedFile.side); + } + return true; + } + public String getName() { return metadata.getName(); } diff --git a/src/main/java/link/infra/packwiz/installer/UpdateManager.java b/src/main/java/link/infra/packwiz/installer/UpdateManager.java index 9f7e518..fa629d3 100644 --- a/src/main/java/link/infra/packwiz/installer/UpdateManager.java +++ b/src/main/java/link/infra/packwiz/installer/UpdateManager.java @@ -38,7 +38,7 @@ public class UpdateManager { public String packFolder = "."; public Side side = Side.CLIENT; - public static enum Side { + public enum Side { @SerializedName("client") CLIENT("client"), @SerializedName("server") SERVER("server"), @SerializedName("both") @@ -215,6 +215,7 @@ public class UpdateManager { if (!indexFileSource.hashIsEqual(indexHash)) { // TODO: throw exception + System.out.println("I was meant to put an error message here but I'll do that later"); return; } @@ -283,8 +284,8 @@ public class UpdateManager { List failedTasks = tasks.stream().filter(t -> t.getException() != null).collect(Collectors.toList()); // If options changed, present all options again - if (tasks.stream().anyMatch(DownloadTask::isNewOptional)) { - List opts = tasks.stream().filter(DownloadTask::isOptional).collect(Collectors.toList()); + if (tasks.stream().filter(DownloadTask::correctSide).anyMatch(DownloadTask::isNewOptional)) { + List opts = tasks.stream().filter(DownloadTask::correctSide).filter(DownloadTask::isOptional).collect(Collectors.toList()); ui.showOptions(opts); } @@ -292,12 +293,10 @@ public class UpdateManager { ExecutorService threadPool = Executors.newFixedThreadPool(10); CompletionService completionService = new ExecutorCompletionService<>(threadPool); - tasks.forEach(t -> { - completionService.submit(() -> { - t.download(opts.packFolder, indexUri); - return t; - }); - }); + tasks.forEach(t -> completionService.submit(() -> { + t.download(opts.packFolder, indexUri); + return t; + })); for (int i = 0; i < tasks.size(); i++) { DownloadTask ret; @@ -326,7 +325,5 @@ public class UpdateManager { } ui.submitProgress(new InstallProgress(progress, i + 1, tasks.size())); } - // option = false file hashes should be stored to disk, but not downloaded - // TODO: don't include optional files in progress???? } } diff --git a/src/main/java/link/infra/packwiz/installer/ui/CLIHandler.java b/src/main/java/link/infra/packwiz/installer/ui/CLIHandler.java index e142299..68001c0 100644 --- a/src/main/java/link/infra/packwiz/installer/ui/CLIHandler.java +++ b/src/main/java/link/infra/packwiz/installer/ui/CLIHandler.java @@ -1,5 +1,7 @@ package link.infra.packwiz.installer.ui; +import java.util.List; + public class CLIHandler implements IUserInterface { @Override @@ -29,5 +31,10 @@ public class CLIHandler implements IUserInterface { task.run(); System.out.println("Finished successfully!"); } - + + @Override + public void showOptions(List option) { + throw new RuntimeException("Optional mods not implemented for CLI! Make sure your optional mods are only on the client side!"); + } + } diff --git a/src/main/java/link/infra/packwiz/installer/ui/InstallWindow.java b/src/main/java/link/infra/packwiz/installer/ui/InstallWindow.java index e9f8546..bce96ae 100644 --- a/src/main/java/link/infra/packwiz/installer/ui/InstallWindow.java +++ b/src/main/java/link/infra/packwiz/installer/ui/InstallWindow.java @@ -1,24 +1,11 @@ package link.infra.packwiz.installer.ui; -import java.awt.BorderLayout; -import java.awt.Component; -import java.awt.EventQueue; -import java.awt.GridBagConstraints; -import java.awt.GridBagLayout; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; +import javax.swing.*; +import javax.swing.border.EmptyBorder; +import java.awt.*; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; -import javax.swing.JButton; -import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.JOptionPane; -import javax.swing.JPanel; -import javax.swing.JProgressBar; -import javax.swing.UIManager; -import javax.swing.border.EmptyBorder; - public class InstallWindow implements IUserInterface { private JFrame frmPackwizlauncher; @@ -31,15 +18,13 @@ public class InstallWindow implements IUserInterface { @Override public void show() { - EventQueue.invokeLater(new Runnable() { - public void run() { - try { - UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); - InstallWindow.this.initialize(); - InstallWindow.this.frmPackwizlauncher.setVisible(true); - } catch (Exception e) { - e.printStackTrace(); - } + EventQueue.invokeLater(() -> { + try { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + InstallWindow.this.initialize(); + InstallWindow.this.frmPackwizlauncher.setVisible(true); + } catch (Exception e) { + e.printStackTrace(); } }); } @@ -80,16 +65,14 @@ public class InstallWindow implements IUserInterface { panel_1.add(btnOptions, gbc_btnOptions); JButton btnCancel = new JButton("Cancel"); - btnCancel.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent arg0) { - if (worker != null) { - worker.cancel(true); - } - frmPackwizlauncher.dispose(); - // TODO: show window to ask user what to do - System.out.println("Update process cancelled by user!"); - System.exit(1); + btnCancel.addActionListener(event -> { + if (worker != null) { + worker.cancel(true); } + frmPackwizlauncher.dispose(); + // TODO: show window to ask user what to do + System.out.println("Update process cancelled by user!"); + System.exit(1); }); btnCancel.setAlignmentX(Component.CENTER_ALIGNMENT); GridBagConstraints gbc_btnCancel = new GridBagConstraints(); @@ -101,10 +84,8 @@ public class InstallWindow implements IUserInterface { @Override public void handleException(Exception e) { e.printStackTrace(); - EventQueue.invokeLater(new Runnable() { - public void run() { - JOptionPane.showMessageDialog(null, "An error occurred: \n" + e.getClass().getCanonicalName() + ": " + e.getMessage(), title, JOptionPane.ERROR_MESSAGE); - } + EventQueue.invokeLater(() -> { + JOptionPane.showMessageDialog(null, "An error occurred: \n" + e.getClass().getCanonicalName() + ": " + e.getMessage(), title, JOptionPane.ERROR_MESSAGE); }); } @@ -113,11 +94,9 @@ public class InstallWindow implements IUserInterface { e.printStackTrace(); // Used to prevent the done() handler of SwingWorker executing if the invokeLater hasn't happened yet aboutToCrash.set(true); - EventQueue.invokeLater(new Runnable() { - public void run() { - JOptionPane.showMessageDialog(null, "A fatal error occurred: \n" + e.getClass().getCanonicalName() + ": " + e.getMessage(), title, JOptionPane.ERROR_MESSAGE); - System.exit(1); - } + EventQueue.invokeLater(() -> { + JOptionPane.showMessageDialog(null, "A fatal error occurred: \n" + e.getClass().getCanonicalName() + ": " + e.getMessage(), title, JOptionPane.ERROR_MESSAGE); + System.exit(1); }); } @@ -125,11 +104,7 @@ public class InstallWindow implements IUserInterface { public void setTitle(String title) { this.title = title; if (frmPackwizlauncher != null) { - EventQueue.invokeLater(new Runnable() { - public void run() { - InstallWindow.this.frmPackwizlauncher.setTitle(title); - } - }); + EventQueue.invokeLater(() -> InstallWindow.this.frmPackwizlauncher.setTitle(title)); } } @@ -142,48 +117,51 @@ public class InstallWindow implements IUserInterface { @Override public void executeManager(Runnable task) { - EventQueue.invokeLater(new Runnable() { - public void run() { - worker = new SwingWorkerButWithPublicPublish() { + EventQueue.invokeLater(() -> { + worker = new SwingWorkerButWithPublicPublish<>() { - @Override - protected Void doInBackground() throws Exception { - task.run(); - return null; - } + @Override + protected Void doInBackground() { + task.run(); + return null; + } - @Override - protected void process(List chunks) { - // Only process last chunk - if (chunks.size() > 0) { - InstallProgress prog = chunks.get(chunks.size() - 1); - if (prog.hasProgress) { - progressBar.setIndeterminate(false); - progressBar.setValue(prog.progress); - progressBar.setMaximum(prog.progressTotal); - } else { - progressBar.setIndeterminate(true); - progressBar.setValue(0); - } - lblProgresslabel.setText(prog.message); + @Override + protected void process(List chunks) { + // Only process last chunk + if (chunks.size() > 0) { + InstallProgress prog = chunks.get(chunks.size() - 1); + if (prog.hasProgress) { + progressBar.setIndeterminate(false); + progressBar.setValue(prog.progress); + progressBar.setMaximum(prog.progressTotal); + } else { + progressBar.setIndeterminate(true); + progressBar.setValue(0); } + lblProgresslabel.setText(prog.message); } + } - @Override - protected void done() { - if (aboutToCrash.get()) { - return; - } - // TODO: a better way to do this? - frmPackwizlauncher.dispose(); - System.out.println("Finished successfully!"); - System.exit(0); + @Override + protected void done() { + if (aboutToCrash.get()) { + return; } - - }; - worker.execute(); - } + // TODO: a better way to do this? + frmPackwizlauncher.dispose(); + System.out.println("Finished successfully!"); + System.exit(0); + } + + }; + worker.execute(); }); } + @Override + public void showOptions(List option) { + + } + }