Skip to content

Commit ede488c

Browse files
authored
cf: copy files found in downloads that skipped download (#450)
1 parent d84186d commit ede488c

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

src/main/java/me/itzg/helpers/curseforge/CurseForgeInstaller.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import me.itzg.helpers.json.ObjectMappers;
5757
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
5858
import org.apache.commons.compress.archivers.zip.ZipFile;
59+
import org.jetbrains.annotations.NotNull;
5960
import org.jetbrains.annotations.Nullable;
6061
import reactor.core.publisher.Flux;
6162
import reactor.core.publisher.Mono;
@@ -800,17 +801,21 @@ private Mono<ResolveResult> downloadOrResolveFile(InstallContext context, CurseF
800801

801802
// Will try to locate an existing file by alternate names that browser might create,
802803
// but only for non-world files of the modpack
803-
final Path locatedFile = !isWorld ? locateFileIn(cfFile.getFileName(),
804-
outputDir,
805-
downloadsRepo
806-
) : null;
804+
final Path locatedFile = !isWorld ?
805+
locateFileIn(cfFile.getFileName(), outputDir)
806+
: null;
807807

808808
if (locatedFile != null) {
809809
log.info("Mod file {} already exists", locatedFile);
810810
return FileHashVerifier.verify(locatedFile, cfFile.getHashes())
811811
.map(ResolveResult::new);
812812
}
813813
else {
814+
final Path fileInRepo = locateModInRepo(cfFile.getFileName());
815+
if (fileInRepo != null) {
816+
return copyFromDownloadsRepo(outputFile, fileInRepo);
817+
}
818+
814819
return context.cfApi.download(cfFile, outputFile, modFileDownloadStatusHandler(this.outputDir, log))
815820
.flatMap(path -> FileHashVerifier.verify(path, cfFile.getHashes()))
816821
.map(ResolveResult::new)
@@ -837,22 +842,20 @@ private Mono<ResolveResult> handleFileNeedingManualDownload(CurseForgeMod modInf
837842
return Mono.just(new ResolveResult(outputFile).setDownloadNeeded(true));
838843
}
839844
else {
840-
return Mono.just(resolved)
841-
.publishOn(Schedulers.boundedElastic())
842-
.flatMap(path -> {
843-
try {
844-
log.info("Mod file {} obtained from downloads repo",
845-
this.outputDir.relativize(outputFile)
846-
);
847-
//noinspection BlockingMethodInNonBlockingContext because IntelliJ is confused
848-
return Mono.just(Files.copy(resolved, outputFile));
849-
} catch (IOException e) {
850-
return Mono.error(new GenericException("Failed to copy file from downloads repo", e));
851-
}
845+
return copyFromDownloadsRepo(outputFile, resolved);
846+
}
847+
}
852848

849+
private @NotNull Mono<ResolveResult> copyFromDownloadsRepo(Path outputFile, Path resolved) {
850+
return
851+
Mono.fromCallable(() -> {
852+
log.info("Mod file {} obtained from downloads repo",
853+
this.outputDir.relativize(outputFile)
854+
);
855+
return Files.copy(resolved, outputFile);
853856
})
857+
.subscribeOn(Schedulers.boundedElastic())
854858
.map(ResolveResult::new);
855-
}
856859
}
857860

858861
private PathWithInfo extractWorldZip(CurseForgeMod modInfo, Path zipPath, Path worldsDir) {

src/main/java/me/itzg/helpers/files/Manifests.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.stream.Collectors;
1313
import java.util.stream.Stream;
1414
import lombok.extern.slf4j.Slf4j;
15+
import me.itzg.helpers.errors.GenericException;
1516
import me.itzg.helpers.json.ObjectMappers;
1617
import org.jetbrains.annotations.Nullable;
1718
import org.slf4j.Logger;
@@ -90,9 +91,16 @@ public static List<String> relativizeAll(Path basePath, Path... paths) {
9091
public static List<String> relativizeAll(Path basePath, Stream<Path> pathStream) {
9192
return pathStream
9293
.filter(Objects::nonNull)
93-
.map(p ->
94-
basePath.relativize(p)
95-
.toString()
94+
.map(p -> {
95+
try {
96+
return basePath.relativize(p)
97+
.toString();
98+
} catch (IllegalArgumentException e) {
99+
throw new GenericException(String.format("Failed to relative %s against %s", p, basePath),
100+
e
101+
);
102+
}
103+
}
96104
)
97105
.collect(Collectors.toList());
98106
}

0 commit comments

Comments
 (0)