Implement some stuff, edge case checks, intellij optimisations

This commit is contained in:
comp500 2019-08-09 18:15:47 +01:00
parent ae085743be
commit a9bd83e96b
4 changed files with 85 additions and 97 deletions

View File

@ -102,7 +102,7 @@ class DownloadTask implements IOptionDetails {
public void download(String packFolder, URI indexUri) { public void download(String packFolder, URI indexUri) {
if (failure != null) return; if (failure != null) return;
if (alreadyUpToDate) 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()); Path destPath = Paths.get(packFolder, metadata.getDestURI().toString());
@ -165,7 +165,6 @@ class DownloadTask implements IOptionDetails {
cachedFile.linkedFileHash = metadata.linkedFile.getHash(); cachedFile.linkedFileHash = metadata.linkedFile.getHash();
} catch (Exception e) { } catch (Exception e) {
failure = e; failure = e;
return;
} }
} }
} }
@ -186,6 +185,13 @@ class DownloadTask implements IOptionDetails {
return isOptional() && this.newOptional; return isOptional() && this.newOptional;
} }
public boolean correctSide() {
if (metadata.linkedFile != null) {
return downloadSide.hasSide(metadata.linkedFile.side);
}
return true;
}
public String getName() { public String getName() {
return metadata.getName(); return metadata.getName();
} }

View File

@ -38,7 +38,7 @@ public class UpdateManager {
public String packFolder = "."; public String packFolder = ".";
public Side side = Side.CLIENT; public Side side = Side.CLIENT;
public static enum Side { public enum Side {
@SerializedName("client") @SerializedName("client")
CLIENT("client"), @SerializedName("server") CLIENT("client"), @SerializedName("server")
SERVER("server"), @SerializedName("both") SERVER("server"), @SerializedName("both")
@ -215,6 +215,7 @@ public class UpdateManager {
if (!indexFileSource.hashIsEqual(indexHash)) { if (!indexFileSource.hashIsEqual(indexHash)) {
// TODO: throw exception // TODO: throw exception
System.out.println("I was meant to put an error message here but I'll do that later");
return; return;
} }
@ -283,8 +284,8 @@ public class UpdateManager {
List<DownloadTask> failedTasks = tasks.stream().filter(t -> t.getException() != null).collect(Collectors.toList()); List<DownloadTask> failedTasks = tasks.stream().filter(t -> t.getException() != null).collect(Collectors.toList());
// If options changed, present all options again // If options changed, present all options again
if (tasks.stream().anyMatch(DownloadTask::isNewOptional)) { if (tasks.stream().filter(DownloadTask::correctSide).anyMatch(DownloadTask::isNewOptional)) {
List<IOptionDetails> opts = tasks.stream().filter(DownloadTask::isOptional).collect(Collectors.toList()); List<IOptionDetails> opts = tasks.stream().filter(DownloadTask::correctSide).filter(DownloadTask::isOptional).collect(Collectors.toList());
ui.showOptions(opts); ui.showOptions(opts);
} }
@ -292,12 +293,10 @@ public class UpdateManager {
ExecutorService threadPool = Executors.newFixedThreadPool(10); ExecutorService threadPool = Executors.newFixedThreadPool(10);
CompletionService<DownloadTask> completionService = new ExecutorCompletionService<>(threadPool); CompletionService<DownloadTask> completionService = new ExecutorCompletionService<>(threadPool);
tasks.forEach(t -> { tasks.forEach(t -> completionService.submit(() -> {
completionService.submit(() -> {
t.download(opts.packFolder, indexUri); t.download(opts.packFolder, indexUri);
return t; return t;
}); }));
});
for (int i = 0; i < tasks.size(); i++) { for (int i = 0; i < tasks.size(); i++) {
DownloadTask ret; DownloadTask ret;
@ -326,7 +325,5 @@ public class UpdateManager {
} }
ui.submitProgress(new InstallProgress(progress, i + 1, tasks.size())); 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????
} }
} }

View File

@ -1,5 +1,7 @@
package link.infra.packwiz.installer.ui; package link.infra.packwiz.installer.ui;
import java.util.List;
public class CLIHandler implements IUserInterface { public class CLIHandler implements IUserInterface {
@Override @Override
@ -30,4 +32,9 @@ public class CLIHandler implements IUserInterface {
System.out.println("Finished successfully!"); System.out.println("Finished successfully!");
} }
@Override
public void showOptions(List<IOptionDetails> option) {
throw new RuntimeException("Optional mods not implemented for CLI! Make sure your optional mods are only on the client side!");
}
} }

View File

@ -1,24 +1,11 @@
package link.infra.packwiz.installer.ui; package link.infra.packwiz.installer.ui;
import java.awt.BorderLayout; import javax.swing.*;
import java.awt.Component; import javax.swing.border.EmptyBorder;
import java.awt.EventQueue; import java.awt.*;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean; 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 { public class InstallWindow implements IUserInterface {
private JFrame frmPackwizlauncher; private JFrame frmPackwizlauncher;
@ -31,8 +18,7 @@ public class InstallWindow implements IUserInterface {
@Override @Override
public void show() { public void show() {
EventQueue.invokeLater(new Runnable() { EventQueue.invokeLater(() -> {
public void run() {
try { try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
InstallWindow.this.initialize(); InstallWindow.this.initialize();
@ -40,7 +26,6 @@ public class InstallWindow implements IUserInterface {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
}
}); });
} }
@ -80,8 +65,7 @@ public class InstallWindow implements IUserInterface {
panel_1.add(btnOptions, gbc_btnOptions); panel_1.add(btnOptions, gbc_btnOptions);
JButton btnCancel = new JButton("Cancel"); JButton btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new ActionListener() { btnCancel.addActionListener(event -> {
public void actionPerformed(ActionEvent arg0) {
if (worker != null) { if (worker != null) {
worker.cancel(true); worker.cancel(true);
} }
@ -89,7 +73,6 @@ public class InstallWindow implements IUserInterface {
// TODO: show window to ask user what to do // TODO: show window to ask user what to do
System.out.println("Update process cancelled by user!"); System.out.println("Update process cancelled by user!");
System.exit(1); System.exit(1);
}
}); });
btnCancel.setAlignmentX(Component.CENTER_ALIGNMENT); btnCancel.setAlignmentX(Component.CENTER_ALIGNMENT);
GridBagConstraints gbc_btnCancel = new GridBagConstraints(); GridBagConstraints gbc_btnCancel = new GridBagConstraints();
@ -101,10 +84,8 @@ public class InstallWindow implements IUserInterface {
@Override @Override
public void handleException(Exception e) { public void handleException(Exception e) {
e.printStackTrace(); e.printStackTrace();
EventQueue.invokeLater(new Runnable() { EventQueue.invokeLater(() -> {
public void run() {
JOptionPane.showMessageDialog(null, "An error occurred: \n" + e.getClass().getCanonicalName() + ": " + e.getMessage(), title, JOptionPane.ERROR_MESSAGE); 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(); e.printStackTrace();
// Used to prevent the done() handler of SwingWorker executing if the invokeLater hasn't happened yet // Used to prevent the done() handler of SwingWorker executing if the invokeLater hasn't happened yet
aboutToCrash.set(true); aboutToCrash.set(true);
EventQueue.invokeLater(new Runnable() { EventQueue.invokeLater(() -> {
public void run() {
JOptionPane.showMessageDialog(null, "A fatal error occurred: \n" + e.getClass().getCanonicalName() + ": " + e.getMessage(), title, JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(null, "A fatal error occurred: \n" + e.getClass().getCanonicalName() + ": " + e.getMessage(), title, JOptionPane.ERROR_MESSAGE);
System.exit(1); System.exit(1);
}
}); });
} }
@ -125,11 +104,7 @@ public class InstallWindow implements IUserInterface {
public void setTitle(String title) { public void setTitle(String title) {
this.title = title; this.title = title;
if (frmPackwizlauncher != null) { if (frmPackwizlauncher != null) {
EventQueue.invokeLater(new Runnable() { EventQueue.invokeLater(() -> InstallWindow.this.frmPackwizlauncher.setTitle(title));
public void run() {
InstallWindow.this.frmPackwizlauncher.setTitle(title);
}
});
} }
} }
@ -142,12 +117,11 @@ public class InstallWindow implements IUserInterface {
@Override @Override
public void executeManager(Runnable task) { public void executeManager(Runnable task) {
EventQueue.invokeLater(new Runnable() { EventQueue.invokeLater(() -> {
public void run() { worker = new SwingWorkerButWithPublicPublish<>() {
worker = new SwingWorkerButWithPublicPublish<Void, InstallProgress>() {
@Override @Override
protected Void doInBackground() throws Exception { protected Void doInBackground() {
task.run(); task.run();
return null; return null;
} }
@ -182,8 +156,12 @@ public class InstallWindow implements IUserInterface {
}; };
worker.execute(); worker.execute();
}
}); });
} }
@Override
public void showOptions(List<IOptionDetails> option) {
}
} }