diff --git a/src/main/java/link/infra/packwiz/installer/UpdateManager.java b/src/main/java/link/infra/packwiz/installer/UpdateManager.java index fa629d3..204da03 100644 --- a/src/main/java/link/infra/packwiz/installer/UpdateManager.java +++ b/src/main/java/link/infra/packwiz/installer/UpdateManager.java @@ -13,7 +13,6 @@ import link.infra.packwiz.installer.metadata.hash.GeneralHashingSource; import link.infra.packwiz.installer.metadata.hash.Hash; import link.infra.packwiz.installer.metadata.hash.HashUtils; import link.infra.packwiz.installer.request.HandlerManager; -import link.infra.packwiz.installer.ui.IOptionDetails; import link.infra.packwiz.installer.ui.IUserInterface; import link.infra.packwiz.installer.ui.InstallProgress; import okio.Okio; @@ -31,6 +30,7 @@ public class UpdateManager { public final Options opts; public final IUserInterface ui; + private boolean cancelled; public static class Options { public URI downloadURI = null; @@ -155,7 +155,7 @@ public class UpdateManager { } } - if (manifest.packFileHash != null && packFileSource.hashIsEqual(manifest.packFileHash) && invalidatedUris.size() == 0) { + if (manifest.packFileHash != null && packFileSource.hashIsEqual(manifest.packFileHash) && invalidatedUris.isEmpty()) { System.out.println("Modpack is already up to date!"); // todo: --force? return; @@ -171,6 +171,10 @@ public class UpdateManager { ui.handleExceptionAndExit(e1); } + if (cancelled) { + return; + } + // TODO: update MMC params, java args etc manifest.packFileHash = packFileSource.getHash(); @@ -189,7 +193,7 @@ public class UpdateManager { } protected void processIndex(URI indexUri, Hash indexHash, String hashFormat, ManifestFile manifest, List invalidatedUris) { - if (manifest.indexFileHash != null && manifest.indexFileHash.equals(indexHash) && invalidatedUris.size() == 0) { + if (manifest.indexFileHash != null && manifest.indexFileHash.equals(indexHash) && invalidatedUris.isEmpty()) { System.out.println("Modpack files are already up to date!"); return; } @@ -283,10 +287,21 @@ public class UpdateManager { // TODO: quit if there are exceptions or just remove failed tasks before presenting options List failedTasks = tasks.stream().filter(t -> t.getException() != null).collect(Collectors.toList()); + List optionTasks = tasks.stream().filter(t -> t.getException() == null).filter(DownloadTask::correctSide).filter(DownloadTask::isOptional).collect(Collectors.toList()); // If options changed, present all options again - 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); + if (optionTasks.stream().anyMatch(DownloadTask::isNewOptional)) { + // new ArrayList is requires so it's an IOptionDetails rather than a DownloadTask list + Future cancelledResult = ui.showOptions(new ArrayList<>(optionTasks)); + try { + if (cancelledResult.get()) { + cancelled = true; + // TODO: Should the UI be closed somehow?? + return; + } + } catch (InterruptedException | ExecutionException e) { + // Interrupted means cancelled??? + ui.handleExceptionAndExit(e); + } } // TODO: different thread pool type? @@ -299,26 +314,26 @@ public class UpdateManager { })); for (int i = 0; i < tasks.size(); i++) { - DownloadTask ret; + DownloadTask task; try { - ret = completionService.take().get(); + task = completionService.take().get(); } catch (InterruptedException | ExecutionException e) { // TODO: collect all exceptions, present in one dialog ui.handleException(e); - ret = null; + task = null; } // Update manifest - If there were no errors cachedFile has already been modified in place (good old pass by reference) - if (ret != null && ret.getException() != null) { - manifest.cachedFiles.put(ret.metadata.file, ret.cachedFile.getRevert()); + if (task != null && task.getException() != null) { + manifest.cachedFiles.put(task.metadata.file, task.cachedFile.getRevert()); } // TODO: show errors properly? String progress; - if (ret != null) { - if (ret.getException() != null) { - progress = "Failed to download " + ret.metadata.getName() + ": " + ret.getException().getMessage(); - ret.getException().printStackTrace(); + if (task != null) { + if (task.getException() != null) { + progress = "Failed to download " + task.metadata.getName() + ": " + task.getException().getMessage(); + task.getException().printStackTrace(); } else { - progress = "Downloaded " + ret.metadata.getName(); + progress = "Downloaded " + task.metadata.getName(); } } else { progress = "Failed to download, unknown reason"; 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 68001c0..3404bc4 100644 --- a/src/main/java/link/infra/packwiz/installer/ui/CLIHandler.java +++ b/src/main/java/link/infra/packwiz/installer/ui/CLIHandler.java @@ -1,6 +1,7 @@ package link.infra.packwiz.installer.ui; import java.util.List; +import java.util.concurrent.Future; public class CLIHandler implements IUserInterface { @@ -33,7 +34,7 @@ public class CLIHandler implements IUserInterface { } @Override - public void showOptions(List option) { + public Future 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/IUserInterface.java b/src/main/java/link/infra/packwiz/installer/ui/IUserInterface.java index 665641f..5b815cc 100644 --- a/src/main/java/link/infra/packwiz/installer/ui/IUserInterface.java +++ b/src/main/java/link/infra/packwiz/installer/ui/IUserInterface.java @@ -1,6 +1,7 @@ package link.infra.packwiz.installer.ui; import java.util.List; +import java.util.concurrent.Future; public interface IUserInterface { @@ -22,6 +23,6 @@ public interface IUserInterface { void executeManager(Runnable task); - void showOptions(List option); + Future showOptions(List option); } 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 bce96ae..7c4fe90 100644 --- a/src/main/java/link/infra/packwiz/installer/ui/InstallWindow.java +++ b/src/main/java/link/infra/packwiz/installer/ui/InstallWindow.java @@ -4,6 +4,7 @@ import javax.swing.*; import javax.swing.border.EmptyBorder; import java.awt.*; import java.util.List; +import java.util.concurrent.Future; import java.util.concurrent.atomic.AtomicBoolean; public class InstallWindow implements IUserInterface { @@ -160,8 +161,8 @@ public class InstallWindow implements IUserInterface { } @Override - public void showOptions(List option) { - + public Future showOptions(List option) { + return null; } }