Make handleExceptionAndExit better, fix some stuff

This commit is contained in:
comp500 2019-08-11 01:14:39 +01:00
parent 78f5d76fe9
commit ea60175514
4 changed files with 30 additions and 25 deletions

View File

@ -5,6 +5,7 @@ import link.infra.packwiz.installer.metadata.ManifestFile;
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.ui.IExceptionDetails;
import link.infra.packwiz.installer.ui.IOptionDetails;
import okio.Buffer;
import okio.Okio;
@ -19,7 +20,7 @@ import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;
class DownloadTask implements IOptionDetails {
class DownloadTask implements IOptionDetails, IExceptionDetails {
final IndexFile.File metadata;
ManifestFile.File cachedFile = null;
private Exception failure = null;
@ -30,7 +31,7 @@ class DownloadTask implements IOptionDetails {
private boolean newOptional = true;
private final UpdateManager.Options.Side downloadSide;
public DownloadTask(IndexFile.File metadata, String defaultFormat, UpdateManager.Options.Side downloadSide) {
private DownloadTask(IndexFile.File metadata, String defaultFormat, UpdateManager.Options.Side downloadSide) {
this.metadata = metadata;
if (metadata.hashFormat == null || metadata.hashFormat.length() == 0) {
metadata.hashFormat = defaultFormat;
@ -38,12 +39,12 @@ class DownloadTask implements IOptionDetails {
this.downloadSide = downloadSide;
}
public void invalidate() {
void invalidate() {
invalidated = true;
alreadyUpToDate = false;
}
public void updateFromCache(ManifestFile.File cachedFile) {
void updateFromCache(ManifestFile.File cachedFile) {
if (failure != null) return;
if (cachedFile == null) {
this.cachedFile = new ManifestFile.File();
@ -53,7 +54,7 @@ class DownloadTask implements IOptionDetails {
this.cachedFile = cachedFile;
if (!invalidated) {
Hash currHash = null;
Hash currHash;
try {
currHash = HashUtils.getHash(metadata.hashFormat, metadata.hash);
} catch (Exception e) {
@ -73,7 +74,7 @@ class DownloadTask implements IOptionDetails {
}
}
public void downloadMetadata(IndexFile parentIndexFile, URI indexUri) {
void downloadMetadata(IndexFile parentIndexFile, URI indexUri) {
if (failure != null) return;
if (metadataRequired) {
try {
@ -100,7 +101,7 @@ class DownloadTask implements IOptionDetails {
}
}
public void download(String packFolder, URI indexUri) {
void download(String packFolder, URI indexUri) {
if (failure != null) return;
// Ensure it is removed
@ -188,18 +189,18 @@ class DownloadTask implements IOptionDetails {
return failure;
}
public boolean isOptional() {
boolean isOptional() {
if (metadata.linkedFile != null) {
return metadata.linkedFile.isOptional();
}
return false;
}
public boolean isNewOptional() {
boolean isNewOptional() {
return isOptional() && this.newOptional;
}
public boolean correctSide() {
boolean correctSide() {
if (metadata.linkedFile != null) {
return metadata.linkedFile.side.hasSide(downloadSide);
}
@ -231,7 +232,7 @@ class DownloadTask implements IOptionDetails {
cachedFile.optionValue = value;
}
public static List<DownloadTask> createTasksFromIndex(IndexFile index, String defaultFormat, UpdateManager.Options.Side downloadSide) {
static List<DownloadTask> createTasksFromIndex(IndexFile index, String defaultFormat, UpdateManager.Options.Side downloadSide) {
ArrayList<DownloadTask> tasks = new ArrayList<>();
for (IndexFile.File file : index.files) {
tasks.add(new DownloadTask(file, defaultFormat, downloadSide));

View File

@ -13,6 +13,7 @@ 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.IExceptionDetails;
import link.infra.packwiz.installer.ui.IUserInterface;
import link.infra.packwiz.installer.ui.InstallProgress;
import okio.Okio;
@ -28,15 +29,15 @@ import java.util.stream.Collectors;
public class UpdateManager {
public final Options opts;
private final Options opts;
public final IUserInterface ui;
private boolean cancelled;
public static class Options {
public URI downloadURI = null;
public String manifestFile = "packwiz.json"; // TODO: make configurable
public String packFolder = ".";
public Side side = Side.CLIENT;
URI downloadURI = null;
String manifestFile = "packwiz.json"; // TODO: make configurable
String packFolder = ".";
Side side = Side.CLIENT;
public enum Side {
@SerializedName("client")
@ -88,13 +89,13 @@ public class UpdateManager {
}
}
public UpdateManager(Options opts, IUserInterface ui) {
UpdateManager(Options opts, IUserInterface ui) {
this.opts = opts;
this.ui = ui;
this.start();
}
protected void start() {
private void start() {
this.checkOptions();
ui.submitProgress(new InstallProgress("Loading manifest file..."));
@ -190,11 +191,11 @@ public class UpdateManager {
}
protected void checkOptions() {
private void checkOptions() {
// TODO: implement
}
protected void processIndex(URI indexUri, Hash indexHash, String hashFormat, ManifestFile manifest, List<URI> invalidatedUris) {
private void processIndex(URI indexUri, Hash indexHash, String hashFormat, ManifestFile manifest, List<URI> invalidatedUris) {
if (manifest.indexFileHash != null && manifest.indexFileHash.equals(indexHash) && invalidatedUris.isEmpty()) {
System.out.println("Modpack files are already up to date!");
return;
@ -287,7 +288,7 @@ public class UpdateManager {
// TODO: collect all exceptions, present in one dialog
// TODO: quit if there are exceptions or just remove failed tasks before presenting options
List<DownloadTask> failedTasks = tasks.stream().filter(t -> t.getException() != null).collect(Collectors.toList());
List<IExceptionDetails> failedTasks = tasks.stream().filter(t -> t.getException() != null).collect(Collectors.toList());
List<DownloadTask> optionTasks = tasks.stream().filter(t -> t.getException() == null).filter(DownloadTask::correctSide).filter(DownloadTask::isOptional).collect(Collectors.toList());
// If options changed, present all options again

View File

@ -8,10 +8,7 @@ public interface IUserInterface {
void show();
void handleException(Exception e);
/**
* This might not exit straight away, return after calling this!
*/
default void handleExceptionAndExit(Exception e) {
handleException(e);
System.exit(1);

View File

@ -101,6 +101,12 @@ public class InstallWindow implements IUserInterface {
JOptionPane.showMessageDialog(null, "A fatal error occurred: \n" + e.getClass().getCanonicalName() + ": " + e.getMessage(), title, JOptionPane.ERROR_MESSAGE);
System.exit(1);
});
// Pause forever, so it blocks while we wait for System.exit to take effect
try {
Thread.currentThread().join();
} catch (InterruptedException ex) {
// no u
}
}
@Override