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) {
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();
}

View File

@ -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<DownloadTask> 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<IOptionDetails> opts = tasks.stream().filter(DownloadTask::isOptional).collect(Collectors.toList());
if (tasks.stream().filter(DownloadTask::correctSide).anyMatch(DownloadTask::isNewOptional)) {
List<IOptionDetails> 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<DownloadTask> completionService = new ExecutorCompletionService<>(threadPool);
tasks.forEach(t -> {
completionService.submit(() -> {
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????
}
}

View File

@ -1,5 +1,7 @@
package link.infra.packwiz.installer.ui;
import java.util.List;
public class CLIHandler implements IUserInterface {
@Override
@ -30,4 +32,9 @@ public class CLIHandler implements IUserInterface {
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;
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,8 +18,7 @@ public class InstallWindow implements IUserInterface {
@Override
public void show() {
EventQueue.invokeLater(new Runnable() {
public void run() {
EventQueue.invokeLater(() -> {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
InstallWindow.this.initialize();
@ -40,7 +26,6 @@ public class InstallWindow implements IUserInterface {
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
@ -80,8 +65,7 @@ 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) {
btnCancel.addActionListener(event -> {
if (worker != null) {
worker.cancel(true);
}
@ -89,7 +73,6 @@ public class InstallWindow implements IUserInterface {
// 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() {
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() {
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,12 +117,11 @@ public class InstallWindow implements IUserInterface {
@Override
public void executeManager(Runnable task) {
EventQueue.invokeLater(new Runnable() {
public void run() {
worker = new SwingWorkerButWithPublicPublish<Void, InstallProgress>() {
EventQueue.invokeLater(() -> {
worker = new SwingWorkerButWithPublicPublish<>() {
@Override
protected Void doInBackground() throws Exception {
protected Void doInBackground() {
task.run();
return null;
}
@ -182,8 +156,12 @@ public class InstallWindow implements IUserInterface {
};
worker.execute();
}
});
}
@Override
public void showOptions(List<IOptionDetails> option) {
}
}