Skip to content

Commit

Permalink
Use ExceptionListWindow to present errors
Browse files Browse the repository at this point in the history
  • Loading branch information
comp500 committed Aug 11, 2019
1 parent ea60175 commit 465e497
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 54 additions & 7 deletions src/main/java/link/infra/packwiz/installer/UpdateManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class UpdateManager {
private final Options opts;
public final IUserInterface ui;
private boolean cancelled;
private boolean cancelledStartGame = false;

public static class Options {
URI downloadURI = null;
Expand All @@ -41,8 +42,10 @@ public static class Options {

public enum Side {
@SerializedName("client")
CLIENT("client"), @SerializedName("server")
SERVER("server"), @SerializedName("both")
CLIENT("client"),
@SerializedName("server")
SERVER("server"),
@SerializedName("both")
BOTH("both", new Side[] { CLIENT, SERVER });

private final String sideName;
Expand Down Expand Up @@ -176,6 +179,10 @@ private void start() {
System.out.println("Update cancelled by user!");
System.exit(1);
return;
} else if (cancelledStartGame) {
System.out.println("Update cancelled by user! Continuing to start game...");
System.exit(0);
return;
}

// TODO: update MMC params, java args etc
Expand Down Expand Up @@ -286,11 +293,30 @@ private void processIndex(URI indexUri, Hash indexHash, String hashFormat, Manif
});
tasks.forEach(f -> f.downloadMetadata(indexFile, indexUri));

// TODO: collect all exceptions, present in one dialog
// TODO: quit if there are exceptions or just remove failed tasks before presenting options
List<IExceptionDetails> failedTasks = tasks.stream().filter(t -> t.getException() != null).collect(Collectors.toList());
if (failedTasks.size() > 0) {
IExceptionDetails.ExceptionListResult exceptionListResult;
try {
exceptionListResult = ui.showExceptions(failedTasks, tasks.size(), true).get();
} catch (InterruptedException | ExecutionException e) {
// Interrupted means cancelled???
ui.handleExceptionAndExit(e);
return;
}
switch (exceptionListResult) {
case CONTINUE:
break;
case CANCEL:
cancelled = true;
return;
case IGNORE:
cancelledStartGame = true;
return;
}
}

List<DownloadTask> optionTasks = tasks.stream().filter(t -> t.getException() == null).filter(DownloadTask::correctSide).filter(DownloadTask::isOptional).collect(Collectors.toList());
List<DownloadTask> nonFailedFirstTasks = tasks.stream().filter(t -> t.getException() == null).collect(Collectors.toList());
List<DownloadTask> optionTasks = nonFailedFirstTasks.stream().filter(DownloadTask::correctSide).filter(DownloadTask::isOptional).collect(Collectors.toList());
// If options changed, present all options again
if (optionTasks.stream().anyMatch(DownloadTask::isNewOptional)) {
// new ArrayList is requires so it's an IOptionDetails rather than a DownloadTask list
Expand Down Expand Up @@ -321,7 +347,6 @@ private void processIndex(URI indexUri, Hash indexHash, String hashFormat, Manif
try {
task = completionService.take().get();
} catch (InterruptedException | ExecutionException e) {
// TODO: collect all exceptions, present in one dialog
ui.handleException(e);
task = null;
}
Expand All @@ -337,19 +362,41 @@ private void processIndex(URI indexUri, Hash indexHash, String hashFormat, Manif
manifest.cachedFiles.putIfAbsent(task.metadata.file, task.cachedFile);
}
}
// TODO: show errors properly?

String progress;
if (task != null) {
if (task.getException() != null) {
progress = "Failed to download " + task.metadata.getName() + ": " + task.getException().getMessage();
task.getException().printStackTrace();
} else {
// TODO: should this be revised for tasks that didn't actually download it?
progress = "Downloaded " + task.metadata.getName();
}
} else {
progress = "Failed to download, unknown reason";
}
ui.submitProgress(new InstallProgress(progress, i + 1, tasks.size()));
}

List<IExceptionDetails> failedTasks2ElectricBoogaloo = nonFailedFirstTasks.stream().filter(t -> t.getException() != null).collect(Collectors.toList());
if (failedTasks2ElectricBoogaloo.size() > 0) {
IExceptionDetails.ExceptionListResult exceptionListResult;
try {
exceptionListResult = ui.showExceptions(failedTasks2ElectricBoogaloo, tasks.size(), false).get();
} catch (InterruptedException | ExecutionException e) {
// Interrupted means cancelled???
ui.handleExceptionAndExit(e);
return;
}
switch (exceptionListResult) {
case CONTINUE:
break;
case CANCEL:
cancelled = true;
return;
case IGNORE:
cancelledStartGame = true;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Future<Boolean> showOptions(List<IOptionDetails> option) {
}

@Override
public Future<IExceptionDetails.ExceptionListResult> showExceptions(List<IExceptionDetails> opts, int numTotal) {
public Future<IExceptionDetails.ExceptionListResult> showExceptions(List<IExceptionDetails> opts, int numTotal, boolean allowsIgnore) {
CompletableFuture<IExceptionDetails.ExceptionListResult> future = new CompletableFuture<>();
future.complete(IExceptionDetails.ExceptionListResult.CANCEL);
return future;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ExceptionListWindow extends JDialog {
/**
* Create the dialog.
*/
ExceptionListWindow(List<IExceptionDetails> eList, CompletableFuture<ExceptionListResult> future, int numTotal, JFrame parentWindow) {
ExceptionListWindow(List<IExceptionDetails> eList, CompletableFuture<ExceptionListResult> future, int numTotal, boolean allowsIgnore, JFrame parentWindow) {
super(parentWindow, "Failed file downloads", true);

setBounds(100, 100, 540, 340);
Expand Down Expand Up @@ -110,6 +110,7 @@ class ExceptionListWindow extends JDialog {
}
{
JButton btnIgnoreUpdate = new JButton("Ignore update");
btnIgnoreUpdate.setEnabled(allowsIgnore);
btnIgnoreUpdate.setToolTipText("Start the game without attempting to update");
btnIgnoreUpdate.addActionListener(e -> {
future.complete(ExceptionListResult.IGNORE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ default void setTitle(String title) {}
// Return true if the installation was cancelled!
Future<Boolean> showOptions(List<IOptionDetails> option);

Future<IExceptionDetails.ExceptionListResult> showExceptions(List<IExceptionDetails> opts, int numTotal);
Future<IExceptionDetails.ExceptionListResult> showExceptions(List<IExceptionDetails> opts, int numTotal, boolean allowsIgnore);

}
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,10 @@ public Future<Boolean> showOptions(List<IOptionDetails> opts) {
}

@Override
public Future<IExceptionDetails.ExceptionListResult> showExceptions(List<IExceptionDetails> opts, int numTotal) {
public Future<IExceptionDetails.ExceptionListResult> showExceptions(List<IExceptionDetails> opts, int numTotal, boolean allowsIgnore) {
CompletableFuture<IExceptionDetails.ExceptionListResult> future = new CompletableFuture<>();
EventQueue.invokeLater(() -> {
ExceptionListWindow dialog = new ExceptionListWindow(opts, future, numTotal, frmPackwizlauncher);
ExceptionListWindow dialog = new ExceptionListWindow(opts, future, numTotal, allowsIgnore, frmPackwizlauncher);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
});
Expand Down

0 comments on commit 465e497

Please sign in to comment.