Skip to content

Commit 2e5e21a

Browse files
authored
修复 MultipleSourceVersionList 不会使用备用下载源的问题 (#5585)
1 parent cceef4f commit 2e5e21a

1 file changed

Lines changed: 47 additions & 12 deletions

File tree

HMCLCore/src/main/java/org/jackhuang/hmcl/download/MultipleSourceVersionList.java

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.jackhuang.hmcl.task.Task;
2121

2222
import java.util.Arrays;
23+
import java.util.Collection;
24+
import java.util.List;
2325

2426
import static org.jackhuang.hmcl.util.logging.Logger.LOG;
2527

@@ -47,25 +49,58 @@ public Task<?> refreshAsync() {
4749

4850
private Task<?> refreshAsync(String gameVersion, int sourceIndex) {
4951
VersionList<?> versionList = backends[sourceIndex];
50-
return versionList.refreshAsync(gameVersion)
51-
.thenComposeAsync(() -> {
52+
Task<?> refreshTask = versionList.refreshAsync(gameVersion);
53+
54+
return new Task<Object>() {
55+
private Task<?> nextTask = null;
56+
57+
{
58+
setSignificance(TaskSignificance.MODERATE);
59+
setName("MultipleSourceVersionList.refreshAsync(" + sourceIndex + ")");
60+
}
61+
62+
@Override
63+
public Collection<Task<?>> getDependents() {
64+
return List.of(refreshTask);
65+
}
66+
67+
@Override
68+
public Collection<? extends Task<?>> getDependencies() {
69+
return nextTask != null ? List.of(nextTask) : List.of();
70+
}
71+
72+
@Override
73+
public boolean isRelyingOnDependents() {
74+
return false;
75+
}
76+
77+
@Override
78+
public void execute() throws Exception {
79+
if (isDependentsSucceeded()) {
5280
lock.writeLock().lock();
5381
try {
5482
versions.putAll(gameVersion, versionList.getVersions(gameVersion));
55-
} catch (Exception e) {
56-
if (sourceIndex == backends.length - 1) {
57-
LOG.warning("Failed to fetch versions list from all sources", e);
58-
throw e;
59-
} else {
60-
LOG.warning("Failed to fetch versions list and try to fetch from other source", e);
61-
return refreshAsync(gameVersion, sourceIndex + 1);
62-
}
6383
} finally {
6484
lock.writeLock().unlock();
6585
}
6686

67-
return null;
68-
});
87+
setResult(refreshTask.getResult());
88+
} else {
89+
Exception exception = refreshTask.getException();
90+
assert exception != null;
91+
92+
if (sourceIndex == backends.length - 1) {
93+
LOG.warning("Failed to fetch versions list from all sources", exception);
94+
setSignificance(TaskSignificance.MINOR);
95+
throw exception;
96+
} else {
97+
LOG.warning("Failed to fetch versions list and try to fetch from other source", exception);
98+
nextTask = refreshAsync(gameVersion, sourceIndex + 1);
99+
nextTask.storeTo(this::setResult);
100+
}
101+
}
102+
}
103+
};
69104
}
70105

71106
@Override

0 commit comments

Comments
 (0)