diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/download/ModpackInstallWizardProvider.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/download/ModpackInstallWizardProvider.java index 1673eb809..c3c196d46 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/download/ModpackInstallWizardProvider.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/download/ModpackInstallWizardProvider.java @@ -122,6 +122,8 @@ public class ModpackInstallWizardProvider implements WizardProvider { public Node createPage(WizardController controller, int step, Map settings) { switch (step) { case 0: + return new ModpackSelectionPage(controller); + case 1: return new ModpackPage(controller); default: throw new IllegalStateException("error step " + step + ", settings: " + settings + ", pages: " + controller.getPages()); diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/download/ModpackSelectionPage.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/download/ModpackSelectionPage.java new file mode 100644 index 000000000..92356bf10 --- /dev/null +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/download/ModpackSelectionPage.java @@ -0,0 +1,118 @@ +/* + * Hello Minecraft! Launcher + * Copyright (C) 2019 huangyuhui and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.jackhuang.hmcl.ui.download; + +import com.jfoenix.controls.JFXButton; +import javafx.application.Platform; +import javafx.fxml.FXML; +import javafx.scene.layout.StackPane; +import javafx.stage.FileChooser; +import org.jackhuang.hmcl.task.FileDownloadTask; +import org.jackhuang.hmcl.task.Schedulers; +import org.jackhuang.hmcl.ui.Controllers; +import org.jackhuang.hmcl.ui.FXUtils; +import org.jackhuang.hmcl.ui.wizard.WizardController; +import org.jackhuang.hmcl.ui.wizard.WizardPage; +import org.jackhuang.hmcl.util.io.FileUtils; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Map; +import java.util.Optional; + +import static org.jackhuang.hmcl.ui.download.ModpackPage.MODPACK_FILE; +import static org.jackhuang.hmcl.util.Lang.tryCast; +import static org.jackhuang.hmcl.util.i18n.I18n.i18n; + +public final class ModpackSelectionPage extends StackPane implements WizardPage { + private final WizardController controller; + + @FXML private JFXButton btnLocal; + @FXML private JFXButton btnRemote; + + public ModpackSelectionPage(WizardController controller) { + this.controller = controller; + FXUtils.loadFXML(this, "/assets/fxml/download/modpack-source.fxml"); + + Optional filePath = tryCast(controller.getSettings().get(MODPACK_FILE), File.class); + if (filePath.isPresent()) { + controller.getSettings().put(MODPACK_FILE, filePath.get()); + controller.onNext(); + } + + FXUtils.applyDragListener(this, it -> "zip".equals(FileUtils.getExtension(it)), modpacks -> { + File modpack = modpacks.get(0); + controller.getSettings().put(MODPACK_FILE, modpack); + controller.onNext(); + }); + } + + @FXML + private void onChooseLocalFile() { + FileChooser chooser = new FileChooser(); + chooser.setTitle(i18n("modpack.choose")); + chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter(i18n("modpack"), "*.zip")); + File selectedFile = chooser.showOpenDialog(Controllers.getStage()); + if (selectedFile == null) { + Platform.runLater(controller::onEnd); + return; + } + + controller.getSettings().put(MODPACK_FILE, selectedFile); + controller.onNext(); + } + + @FXML + private void onChooseRemoteFile() { + Controllers.inputDialog(i18n("modpack.choose.remote.tooltip"), (urlString, resolve, reject) -> { + try { + URL url = new URL(urlString); + Path modpack = Files.createTempFile("forge-installer", ".jar"); + resolve.run(); + + Controllers.taskDialog( + new FileDownloadTask(url, modpack.toFile(), null) + .whenComplete(Schedulers.javafx(), e -> { + if (e == null) { + resolve.run(); + controller.getSettings().put(MODPACK_FILE, modpack.toFile()); + controller.onNext(); + } else { + reject.accept(e.getMessage()); + } + }).executor(true), + i18n("message.downloading") + ); + } catch (IOException e) { + reject.accept(e.getMessage()); + } + }); + } + + @Override + public void cleanup(Map settings) { + } + + @Override + public String getTitle() { + return i18n("modpack.task.install"); + } +} diff --git a/HMCL/src/main/resources/assets/fxml/download/modpack-source.fxml b/HMCL/src/main/resources/assets/fxml/download/modpack-source.fxml new file mode 100644 index 000000000..9f6c6014c --- /dev/null +++ b/HMCL/src/main/resources/assets/fxml/download/modpack-source.fxml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + diff --git a/HMCL/src/main/resources/assets/lang/I18N.properties b/HMCL/src/main/resources/assets/lang/I18N.properties index 3aee3a790..5f6feda24 100644 --- a/HMCL/src/main/resources/assets/lang/I18N.properties +++ b/HMCL/src/main/resources/assets/lang/I18N.properties @@ -218,6 +218,11 @@ message.warning=Warning modpack=Modpack modpack.choose=Choose a modpack zip. +modpack.choose.local=Import local modpack file +modpack.choose.local.detail=You can drag the modpack file into this page to install +modpack.choose.remote=Download modpack from Internet +modpack.choose.remote.detail=Requires a direct download link to the remote modpack file +modpack.choose.remote.tooltip=A direct download link to the remote modpack file modpack.desc=Describe your modpack, including precautions and changelog. Markdown and online pictures are supported. modpack.enter_name=Enter a name for this modpack. modpack.export=Export Modpack diff --git a/HMCL/src/main/resources/assets/lang/I18N_zh.properties b/HMCL/src/main/resources/assets/lang/I18N_zh.properties index f74913c80..69880397e 100644 --- a/HMCL/src/main/resources/assets/lang/I18N_zh.properties +++ b/HMCL/src/main/resources/assets/lang/I18N_zh.properties @@ -215,6 +215,11 @@ message.warning=警告 modpack=整合包 modpack.choose=選擇要安裝的遊戲整合包檔案 +modpack.choose.local=導入本地整合包檔案 +modpack.choose.local.detail=你可以直接將整合包檔案拖入本頁面以安裝 +modpack.choose.remote=從網路下載整合包 +modpack.choose.remote.detail=需要提供整合包的下載連結 +modpack.choose.remote.tooltip=要下載的整合包的連結 modpack.desc=描述你要製作的整合包,比如整合包注意事項和更新記錄,支援 Markdown(圖片請上傳至網路)。 modpack.enter_name=給遊戲取個你喜歡的名字 modpack.export=匯出整合包 diff --git a/HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties b/HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties index 33048ec7f..d3fb519cf 100644 --- a/HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties +++ b/HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties @@ -216,6 +216,11 @@ message.warning=警告 modpack=整合包 modpack.choose=选择要安装的游戏整合包文件 +modpack.choose.local=导入本地整合包文件 +modpack.choose.local.detail=你可以直接将整合包文件拖入本页面以安装 +modpack.choose.remote=从互联网下载整合包 +modpack.choose.remote.detail=需要提供整合包的下载链接 +modpack.choose.remote.tooltip=要下载的整合包的链接 modpack.desc=描述你要制作的整合包,比如整合包注意事项和更新记录,支持 Markdown(图片请用网络图)。 modpack.enter_name=给游戏起个你喜欢的名字 modpack.export=导出整合包 diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/task/FileDownloadTask.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/task/FileDownloadTask.java index 0c85c491f..52078466f 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/task/FileDownloadTask.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/task/FileDownloadTask.java @@ -323,7 +323,7 @@ public class FileDownloadTask extends Task { if (temp != null) temp.toFile().delete(); exception = e; - Logging.LOG.log(Level.WARNING, "Failed to download " + url + ", repeat times: " + repeat + 1, e); + Logging.LOG.log(Level.WARNING, "Failed to download " + url + ", repeat times: " + (repeat + 1), e); } finally { closeFiles(); }