Skip to content
This repository has been archived by the owner on Mar 17, 2023. It is now read-only.

Commit

Permalink
This should provide some progress feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
3TUSK committed Aug 12, 2021
1 parent 8372c52 commit 72142a7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/main/java/org/teacon/sync/SyncedModLocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ public final class SyncedModLocator implements IModLocator {
private static final Gson GSON = new Gson();

private IModLocator parent;

private final Path modDirBase;
private final Consumer<String> progressFeed;

private final PGPKeyStore keyStore;

Expand All @@ -74,6 +75,7 @@ public final class SyncedModLocator implements IModLocator {
private final Set<Path> invalidFiles = new HashSet<>();

public SyncedModLocator() throws Exception {
this.progressFeed = Launcher.INSTANCE.environment().getProperty(Environment.Keys.PROGRESSMESSAGE.get()).orElse(msg -> {});
final Path gameDir = Launcher.INSTANCE.environment().getProperty(IEnvironment.Keys.GAMEDIR.get()).orElse(Paths.get("."));
final Path cfgPath = gameDir.resolve("remote_sync.json");
final Config cfg;
Expand All @@ -89,6 +91,7 @@ public SyncedModLocator() throws Exception {
this.modDirBase = Files.createDirectories(gameDir.resolve(cfg.modDir));
this.fetchPathsTask = CompletableFuture.supplyAsync(() -> {
try {
this.progressFeed.accept("RemoteSync: fetching mod list");
// Intentionally do not use config value to ensure that the mod list is always up-to-date
return Utils.fetch(cfg.modList, gameDir.resolve(cfg.localModList), cfg.timeout, false);
} catch (IOException e) {
Expand All @@ -109,8 +112,8 @@ public SyncedModLocator() throws Exception {
this.allowedFiles = Arrays.stream(entries).map(e -> e.name).collect(Collectors.toSet());
}).thenComposeAsync(entries -> CompletableFuture.allOf(
Arrays.stream(entries).flatMap(e -> Stream.of(
Utils.downloadIfMissingAsync(this.modDirBase.resolve(e.name), e.file, cfg.timeout, cfg.preferLocalCache),
Utils.downloadIfMissingAsync(this.modDirBase.resolve(e.name + ".sig"), e.sig, cfg.timeout, cfg.preferLocalCache)
Utils.downloadIfMissingAsync(this.modDirBase.resolve(e.name), e.file, cfg.timeout, cfg.preferLocalCache, this.progressFeed),
Utils.downloadIfMissingAsync(this.modDirBase.resolve(e.name + ".sig"), e.sig, cfg.timeout, cfg.preferLocalCache, this.progressFeed)
)).toArray(CompletableFuture[]::new)
));
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
Expand Down Expand Up @@ -190,6 +193,7 @@ public void initArguments(Map<String, ?> arguments) {
@Override
public boolean isValid(IModFile modFile) {
LOGGER.debug("Verifying {}", modFile.getFileName());
this.progressFeed.accept("RemoteSync: verifying " + modFile.getFileName());
final Path modPath = modFile.getFilePath();
final Path sigPath = modPath.resolveSibling(modFile.getFileName() + ".sig");
try (FileChannel mod = FileChannel.open(modPath, StandardOpenOption.READ)) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/teacon/sync/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

public final class Utils {

Expand Down Expand Up @@ -200,9 +201,10 @@ public static FileChannel fetch(InputStream src, Path dst) throws IOException {
* @param timeout Number of milliseconds to wait before giving up connection
* @return A {@link CompletableFuture} that represents this task.
*/
public static CompletableFuture<Void> downloadIfMissingAsync(Path target, URL src, int timeout, boolean biasedTowardLocalCache) {
public static CompletableFuture<Void> downloadIfMissingAsync(Path target, URL src, int timeout, boolean biasedTowardLocalCache, Consumer<String> progressFeedback) {
return CompletableFuture.supplyAsync(() -> {
try {
progressFeedback.accept("RemoteSync: considering " + target.getFileName());
return fetch(src, target, timeout, biasedTowardLocalCache);
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down

0 comments on commit 72142a7

Please sign in to comment.