Skip to content

Commit

Permalink
Correctly handle file invalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
comp500 committed Jul 3, 2019
1 parent 34a86ff commit a5ff63c
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/main/java/link/infra/packwiz/installer/UpdateManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,22 @@ protected void start() {
return;
}

if (manifest.packFileHash != null && packFileSource.hashIsEqual(manifest.packFileHash)) {
ui.submitProgress(new InstallProgress("Checking local files..."));

List<URI> invalidatedUris = new ArrayList<>();
Iterator<Map.Entry<URI, ManifestFile.File>> it = manifest.cachedFiles.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<URI, ManifestFile.File> entry = it.next();
if (entry.getValue().cachedLocation != null) {
if (!Files.exists(Paths.get(opts.packFolder, entry.getValue().cachedLocation))) {
URI fileUri = entry.getKey();
System.out.println("File " + fileUri.toString() + " invalidated, marked for redownloading");
invalidatedUris.add(fileUri);
}
}
}

if (manifest.packFileHash != null && packFileSource.hashIsEqual(manifest.packFileHash) && invalidatedUris.size() == 0) {
System.out.println("Modpack is already up to date!");
// todo: --force?
return;
Expand All @@ -156,7 +171,7 @@ protected void start() {

try {
processIndex(HandlerManager.getNewLoc(opts.downloadURI, pf.index.file),
HashUtils.getHash(pf.index.hashFormat, pf.index.hash), pf.index.hashFormat, manifest);
HashUtils.getHash(pf.index.hashFormat, pf.index.hash), pf.index.hashFormat, manifest, invalidatedUris);
} catch (Exception e1) {
ui.handleExceptionAndExit(e1);
}
Expand All @@ -177,8 +192,8 @@ protected void checkOptions() {
// TODO: implement
}

protected void processIndex(URI indexUri, Hash indexHash, String hashFormat, ManifestFile manifest) {
if (manifest.indexFileHash != null && manifest.indexFileHash.equals(indexHash)) {
protected void processIndex(URI indexUri, Hash indexHash, String hashFormat, ManifestFile manifest, List<URI> invalidatedUris) {
if (manifest.indexFileHash != null && manifest.indexFileHash.equals(indexHash) && invalidatedUris.size() == 0) {
System.out.println("Modpack files are already up to date!");
return;
}
Expand Down Expand Up @@ -212,26 +227,19 @@ protected void processIndex(URI indexUri, Hash indexHash, String hashFormat, Man
}

ui.submitProgress(new InstallProgress("Checking local files..."));
List<URI> invalidatedUris = new ArrayList<>();
Iterator<Map.Entry<URI, ManifestFile.File>> it = manifest.cachedFiles.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<URI, ManifestFile.File> entry = it.next();
if (entry.getValue().cachedLocation != null) {
Path filePath = Paths.get(opts.packFolder, entry.getValue().cachedLocation);
URI fileUri = entry.getKey();

if (!indexFile.files.stream().anyMatch(f -> f.file.equals(fileUri))) {
if (!indexFile.files.stream().anyMatch(f -> f.file.equals(entry.getKey()))) {
// File has been removed from the index
try {
Files.deleteIfExists(filePath);
Files.deleteIfExists(Paths.get(opts.packFolder, entry.getValue().cachedLocation));
} catch (IOException e) {
// TODO: should this be shown to the user in some way?
e.printStackTrace();
}
it.remove();
} else if (!Files.exists(filePath)) {
System.out.println("File " + fileUri.toString() + " invalidated, marked for redownloading");
invalidatedUris.add(fileUri);
}
}
}
Expand Down

0 comments on commit a5ff63c

Please sign in to comment.