Skip to content

Commit a962278

Browse files
committed
feat(core): Support multi-thread download
1 parent a5b4625 commit a962278

2 files changed

Lines changed: 44 additions & 12 deletions

File tree

src/main/java/i18nupdatemod/I18nUpdateMod.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
import java.util.ArrayList;
1919
import java.util.List;
2020
import java.util.Objects;
21+
import java.util.concurrent.ExecutionException;
22+
import java.util.concurrent.ExecutorService;
23+
import java.util.concurrent.Executors;
24+
import java.util.concurrent.Future;
2125
import java.util.stream.Collectors;
2226
import java.util.stream.Stream;
2327

@@ -63,14 +67,31 @@ public static void init(Path minecraftPath, String minecraftVersion, String load
6367
List<ResourcePack> languagePacks = new ArrayList<>();
6468
boolean convertNotNeed = assets.downloads.size() == 1 && assets.downloads.get(0).targetVersion.equals(minecraftVersion);
6569
String applyFileName = assets.downloads.get(0).fileName;
66-
for (GameAssetDetail.AssetDownloadDetail it : assets.downloads) {
67-
FileUtil.setTemporaryDirPath(Paths.get(localStorage, "." + MOD_ID, it.targetVersion));
68-
ResourcePack languagePack = new ResourcePack(it.fileName, convertNotNeed);
69-
languagePack.checkUpdate(it.fileUrl, it.md5Url);
70-
languagePacks.add(languagePack);
70+
ExecutorService executor = Executors.newFixedThreadPool(assets.downloads.size());
71+
try {
72+
List<Future<?>> futures = new ArrayList<>();
73+
for (GameAssetDetail.AssetDownloadDetail it : assets.downloads) {
74+
futures.add(executor.submit(() -> {
75+
FileUtil.setTemporaryDirPath(Paths.get(localStorage, "." + MOD_ID, it.targetVersion));
76+
ResourcePack languagePack = new ResourcePack(it.fileName, convertNotNeed);
77+
try {
78+
languagePack.checkUpdate(it.fileUrl, it.md5Url);
79+
} catch (Exception e) {
80+
Log.debug(String.format("Error while checking update for resource pack: %s", e));
81+
}
82+
languagePacks.add(languagePack);
83+
}));
84+
}
85+
for (Future<?> future : futures) {
86+
try {
87+
future.get();
88+
} catch (InterruptedException | ExecutionException e) {
89+
Log.debug(String.format("Error while updating resource pack: %s", e));
90+
}
91+
}
92+
} finally {
93+
executor.shutdown();
7194
}
72-
73-
//Convert resourcepack
7495
if (!convertNotNeed) {
7596
FileUtil.setTemporaryDirPath(Paths.get(localStorage, "." + MOD_ID, minecraftVersion));
7697
applyFileName = assets.covertFileName;

src/main/java/i18nupdatemod/core/ResourcePack.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,25 @@ public ResourcePack(String filename, boolean saveToGame) {
3939
}
4040
}
4141

42-
public void checkUpdate(String fileUrl, String md5Url) throws IOException, URISyntaxException, NoSuchAlgorithmException {
42+
public void checkUpdate(String fileUrl, String md5Url) throws IOException, NoSuchAlgorithmException, URISyntaxException {
4343
if (isUpToDate(md5Url)) {
4444
Log.debug("Already up to date.");
4545
return;
4646
}
47-
//In this time, we can only download full file
48-
downloadFull(fileUrl, md5Url);
49-
//In the future, we will download patch file and merge local file
47+
int retryCount = 3;
48+
while (retryCount > 0) {
49+
try {
50+
downloadFile(fileUrl, md5Url);
51+
return;
52+
} catch (Exception e) {
53+
retryCount--;
54+
if (retryCount == 0) {
55+
Log.debug(String.format("Failed to download resource pack at %s after 3 attempts: %s", fileUrl, e.getMessage()));
56+
Log.warning("Failed to download resource pack.");
57+
return;
58+
}
59+
}
60+
}
5061
}
5162

5263
private boolean isUpToDate(String md5Url) throws IOException, URISyntaxException, NoSuchAlgorithmException {
@@ -74,7 +85,7 @@ private boolean checkMd5(Path localFile, String md5Url) throws IOException, URIS
7485
return localMd5.equalsIgnoreCase(remoteMd5);
7586
}
7687

77-
private void downloadFull(String fileUrl, String md5Url) throws IOException {
88+
private void downloadFile(String fileUrl, String md5Url) throws IOException {
7889
try {
7990
Path downloadTmp = FileUtil.getTemporaryPath(filename + ".tmp");
8091
AssetUtil.download(fileUrl, downloadTmp);

0 commit comments

Comments
 (0)