From a3c2642b93ff7bdde4bb949af1dff1b7e86dccc0 Mon Sep 17 00:00:00 2001 From: huanghongxun Date: Fri, 29 Oct 2021 01:21:34 +0800 Subject: [PATCH] feat(download): customize name of the mod to be downloaded. --- .../hmcl/ui/download/DownloadPage.java | 53 +++++++++++-------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/download/DownloadPage.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/download/DownloadPage.java index ce158c41a..e485ed66e 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/download/DownloadPage.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/download/DownloadPage.java @@ -52,6 +52,7 @@ import org.jackhuang.hmcl.ui.wizard.Navigation; import org.jackhuang.hmcl.ui.wizard.WizardController; import org.jackhuang.hmcl.ui.wizard.WizardProvider; import org.jackhuang.hmcl.util.io.NetworkUtils; +import org.jackhuang.hmcl.util.platform.OperatingSystem; import org.jetbrains.annotations.Nullable; import java.nio.file.Path; @@ -166,30 +167,40 @@ public class DownloadPage extends DecoratorAnimatedPage implements DecoratorPage if (version == null) version = profile.getSelectedVersion(); Path runDirectory = profile.getRepository().hasVersion(version) ? profile.getRepository().getRunDirectory(version).toPath() : profile.getRepository().getBaseDirectory().toPath(); - Path dest = runDirectory.resolve(subdirectoryName).resolve(file.getFile().getFilename()); - TaskExecutorDialogPane downloadingPane = new TaskExecutorDialogPane(it -> { - }); - - TaskExecutor executor = Task.composeAsync(() -> { - FileDownloadTask task = new FileDownloadTask(NetworkUtils.toURL(file.getFile().getUrl()), dest.toFile()); - task.setName(file.getName()); - return task; - }).whenComplete(Schedulers.javafx(), exception -> { - if (exception != null) { - if (exception instanceof CancellationException) { - Controllers.showToast(i18n("message.cancelled")); - } else { - Controllers.dialog(DownloadProviders.localizeErrorMessage(exception), i18n("install.failed.downloading"), MessageDialogPane.MessageType.ERROR); - } - } else { - Controllers.showToast(i18n("install.success")); + Controllers.prompt(i18n("archive.name"), (result, resolve, reject) -> { + if (!OperatingSystem.isNameValid(result)) { + reject.accept(i18n("install.new_game.malformed")); + return; } - }).executor(false); + Path dest = runDirectory.resolve(subdirectoryName).resolve(result); + + TaskExecutorDialogPane downloadingPane = new TaskExecutorDialogPane(it -> { + }); + + TaskExecutor executor = Task.composeAsync(() -> { + FileDownloadTask task = new FileDownloadTask(NetworkUtils.toURL(file.getFile().getUrl()), dest.toFile()); + task.setName(file.getName()); + return task; + }).whenComplete(Schedulers.javafx(), exception -> { + if (exception != null) { + if (exception instanceof CancellationException) { + Controllers.showToast(i18n("message.cancelled")); + } else { + Controllers.dialog(DownloadProviders.localizeErrorMessage(exception), i18n("install.failed.downloading"), MessageDialogPane.MessageType.ERROR); + } + } else { + Controllers.showToast(i18n("install.success")); + } + }).executor(false); + + downloadingPane.setExecutor(executor, true); + Controllers.dialog(downloadingPane); + executor.start(); + + resolve.run(); + }, file.getFile().getFilename()); - downloadingPane.setExecutor(executor, true); - Controllers.dialog(downloadingPane); - executor.start(); } private void loadVersions(Profile profile) {