From 0c32a33641cdd0a2f1030d22e24c4a6de50014cf Mon Sep 17 00:00:00 2001 From: dogdie233 Date: Wed, 2 Jul 2025 19:33:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=90=AF=E5=8A=A8=E6=B8=B8=E6=88=8F5?= =?UTF-8?q?=E7=A7=92=E5=86=85=E6=B2=A1=E4=B8=8B=E8=BD=BD=E5=A5=BD=E8=B5=84?= =?UTF-8?q?=E6=BA=90=E5=8C=85=E5=88=99=E4=B8=8D=E5=86=8D=E7=AD=89=E5=BE=85?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=20=E4=B9=8B=E5=89=8D=E6=8A=8A=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E4=BB=BB=E5=8A=A1=E6=94=BE=E5=9C=A8=E4=B8=BB=E7=BA=BF?= =?UTF-8?q?=E7=A8=8B=EF=BC=8C=E7=8E=B0=E5=9C=A8=E6=8A=8A=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E6=94=BE=E5=9C=A8=E5=88=AB=E7=9A=84=E7=BA=BF?= =?UTF-8?q?=E7=A8=8B=20=E4=B8=BB=E7=BA=BF=E7=A8=8B=E6=9C=80=E5=A4=9A?= =?UTF-8?q?=E7=AD=89=E5=BE=855=E7=A7=92=E4=B8=8B=E8=BD=BD=EF=BC=8C?= =?UTF-8?q?=E8=8B=A55=E7=A7=92=E5=86=85=E6=B2=A1=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E5=AE=8C=E5=88=99=E4=B8=8D=E5=86=8D=E9=98=BB=E5=A1=9E=E4=B8=BB?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=EF=BC=8C=E6=8A=8A=E5=90=88=E5=B9=B6=E5=92=8C?= =?UTF-8?q?=E5=BA=94=E7=94=A8=E8=B5=84=E6=BA=90=E5=8C=85=E6=94=BE=E5=9C=A8?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E5=AE=8C=E6=AF=95=E5=90=8E=E7=9A=84=E5=8F=A6?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E7=BA=BF=E7=A8=8B=E6=89=A7=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit bug:这样做的话可能不会让游戏自动应用资源包,因为可能下载完之后options.txt已经加载完了 --- .../java/i18nupdatemod/I18nUpdateMod.java | 85 +++++++++++++++---- 1 file changed, 69 insertions(+), 16 deletions(-) diff --git a/src/main/java/i18nupdatemod/I18nUpdateMod.java b/src/main/java/i18nupdatemod/I18nUpdateMod.java index 1afd352..fd0b0e3 100644 --- a/src/main/java/i18nupdatemod/I18nUpdateMod.java +++ b/src/main/java/i18nupdatemod/I18nUpdateMod.java @@ -18,10 +18,13 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; +import java.util.concurrent.*; +import java.util.function.Consumer; import java.util.stream.Collectors; import java.util.stream.Stream; public class I18nUpdateMod { + private static final int DOWNLOAD_WAIT_SECONDS = 10; public static final String MOD_ID = "i18nupdatemod"; public static String MOD_VERSION; @@ -55,37 +58,72 @@ public static void init(Path minecraftPath, String minecraftVersion, String load int minecraftMajorVersion = Integer.parseInt(minecraftVersion.split("\\.")[1]); - try { + CompletableFuture downloadTask = CompletableFuture.supplyAsync(() -> { //Get asset GameAssetDetail assets = I18nConfig.getAssetDetail(minecraftVersion, loader); + if (assets.downloads.isEmpty()) { + Log.info("No resource pack found for " + minecraftVersion + " with loader " + loader); + return new DownloadTaskResult(true, assets, "", null); + } //Update resource pack List languagePacks = new ArrayList<>(); + List> downloadTasks = new ArrayList<>(); boolean convertNotNeed = assets.downloads.size() == 1 && assets.downloads.get(0).targetVersion.equals(minecraftVersion); String applyFileName = assets.downloads.get(0).fileName; for (GameAssetDetail.AssetDownloadDetail it : assets.downloads) { FileUtil.setTemporaryDirPath(Paths.get(localStorage, "." + MOD_ID, it.targetVersion)); ResourcePack languagePack = new ResourcePack(it.fileName, convertNotNeed); - languagePack.checkUpdate(it.fileUrl, it.md5Url); + downloadTasks.add(CompletableFuture.runAsync(() -> { + try { + languagePack.checkUpdate(it.fileUrl, it.md5Url); + } catch (Exception e) { + throw new RuntimeException(e); + } + })); languagePacks.add(languagePack); } - //Convert resourcepack - if (!convertNotNeed) { - FileUtil.setTemporaryDirPath(Paths.get(localStorage, "." + MOD_ID, minecraftVersion)); - applyFileName = assets.covertFileName; - ResourcePackConverter converter = new ResourcePackConverter(languagePacks, applyFileName); - converter.convert(assets.covertPackFormat, getResourcePackDescription(assets.downloads)); + // Wait for all download tasks to complete + CompletableFuture.allOf(downloadTasks.toArray(new CompletableFuture[0])).join(); + return new DownloadTaskResult(convertNotNeed, assets, applyFileName, languagePacks); + }); + + // Convert and apply resource pack + Consumer convertAndApply = (result) -> { + if (result.assets.downloads.isEmpty()) { + return; } - //Apply resource pack - GameConfig config = new GameConfig(minecraftPath.resolve("options.txt")); - config.addResourcePack("Minecraft-Mod-Language-Modpack", - (minecraftMajorVersion <= 12 ? "" : "file/") + applyFileName); - config.writeToFile(); - } catch (Exception e) { - Log.warning(String.format("Failed to update resource pack: %s", e)); -// e.printStackTrace(); + try { + //Convert resourcepack + if (!result.convertNotNeed) { + FileUtil.setTemporaryDirPath(Paths.get(localStorage, "." + MOD_ID, minecraftVersion)); + result.applyFileName = result.assets.covertFileName; + ResourcePackConverter converter = new ResourcePackConverter(result.languagePacks, result.applyFileName); + converter.convert(result.assets.covertPackFormat, getResourcePackDescription(result.assets.downloads)); + } + + //Apply resource pack + GameConfig config = new GameConfig(minecraftPath.resolve("options.txt")); + config.addResourcePack("Minecraft-Mod-Language-Modpack", + (minecraftMajorVersion <= 12 ? "" : "file/") + result.applyFileName); + config.writeToFile(); + } catch (Exception e) { + Log.warning(String.format("Failed to convert and apply resource pack: %s", e)); + //e.printStackTrace(); + } + }; + + // Handle the download task result, if it completes within the timeout, apply the resource pack + try { + DownloadTaskResult r = downloadTask.get(DOWNLOAD_WAIT_SECONDS, TimeUnit.SECONDS); // Wait for the download task to complete + convertAndApply.accept(r); + } catch (TimeoutException ex) { + downloadTask.thenAccept(convertAndApply); + Log.info("Resource pack download take too long, will apply it when download finished."); + } catch (ExecutionException | InterruptedException ex) { + Log.warning(String.format("Failed to download resource pack: %s", ex.getCause())); } } @@ -121,4 +159,19 @@ public static String getLocalStoragePos(Path minecraftPath) { Objects::nonNull ).findFirst().orElse(xdgDataHome); } + + private static class DownloadTaskResult + { + public boolean convertNotNeed; + public GameAssetDetail assets; + public String applyFileName; + public List languagePacks; + + public DownloadTaskResult(boolean convertNotNeed, GameAssetDetail assets, String applyFileName, List languagePacks) { + this.convertNotNeed = convertNotNeed; + this.assets = assets; + this.applyFileName = applyFileName; + this.languagePacks = languagePacks; + } + } } \ No newline at end of file