From 1ca9d0fa2c817960e011284a33999870f2d136d8 Mon Sep 17 00:00:00 2001 From: huanghongxun Date: Tue, 18 Dec 2018 13:08:02 +0800 Subject: [PATCH] Backup when updating modpack --- .../jackhuang/hmcl/game/ModpackHelper.java | 6 +- .../hmcl/ui/construct/TaskListPane.java | 2 + .../ModpackInstallWizardProvider.java | 34 +++++++- .../hmcl/ui/download/ModpackPage.java | 21 +++-- .../jackhuang/hmcl/ui/versions/Versions.java | 36 +-------- .../jackhuang/hmcl/mod/ModpackUpdateTask.java | 78 ++++++++++++++++++- 6 files changed, 130 insertions(+), 47 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/game/ModpackHelper.java b/HMCL/src/main/java/org/jackhuang/hmcl/game/ModpackHelper.java index b31347e00..3efd71eb4 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/game/ModpackHelper.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/game/ModpackHelper.java @@ -129,17 +129,17 @@ public final class ModpackHelper { if (!(modpack.getManifest() instanceof CurseManifest)) throw new MismatchedModpackTypeException(CurseInstallTask.MODPACK_TYPE, getManifestType(modpack.getManifest())); - return new CurseInstallTask(profile.getDependency(), zipFile, modpack, (CurseManifest) modpack.getManifest(), name); + return new ModpackUpdateTask(profile.getRepository(), name, new CurseInstallTask(profile.getDependency(), zipFile, modpack, (CurseManifest) modpack.getManifest(), name)); case MultiMCModpackInstallTask.MODPACK_TYPE: if (!(modpack.getManifest() instanceof MultiMCInstanceConfiguration)) throw new MismatchedModpackTypeException(MultiMCModpackInstallTask.MODPACK_TYPE, getManifestType(modpack.getManifest())); - return new MultiMCModpackInstallTask(profile.getDependency(), zipFile, modpack, (MultiMCInstanceConfiguration) modpack.getManifest(), name); + return new ModpackUpdateTask(profile.getRepository(), name, new MultiMCModpackInstallTask(profile.getDependency(), zipFile, modpack, (MultiMCInstanceConfiguration) modpack.getManifest(), name)); case HMCLModpackInstallTask.MODPACK_TYPE: if (!(modpack.getManifest() instanceof HMCLModpackManifest)) throw new MismatchedModpackTypeException(HMCLModpackInstallTask.MODPACK_TYPE, getManifestType(modpack.getManifest())); - return new HMCLModpackInstallTask(profile, zipFile, modpack, name); + return new ModpackUpdateTask(profile.getRepository(), name, new HMCLModpackInstallTask(profile, zipFile, modpack, name)); default: throw new UnsupportedModpackException(); } diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/TaskListPane.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/TaskListPane.java index bc9f47e33..1d22cf112 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/TaskListPane.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/TaskListPane.java @@ -94,6 +94,8 @@ public final class TaskListPane extends StackPane { task.setName(i18n("modpack.type.curse.completion")); } else if (task instanceof ModpackInstallTask) { task.setName(i18n("modpack.installing")); + } else if (task instanceof ModpackUpdateTask) { + task.setName(i18n("modpack.update")); } else if (task instanceof CurseInstallTask) { task.setName(i18n("modpack.install", i18n("modpack.type.curse"))); } else if (task instanceof MultiMCModpackInstallTask) { 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 2c42cc635..4d0152a45 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 @@ -20,7 +20,9 @@ package org.jackhuang.hmcl.ui.download; import javafx.scene.Node; import org.jackhuang.hmcl.game.ModpackHelper; import org.jackhuang.hmcl.mod.CurseCompletionException; +import org.jackhuang.hmcl.mod.MismatchedModpackTypeException; import org.jackhuang.hmcl.mod.Modpack; +import org.jackhuang.hmcl.mod.UnsupportedModpackException; import org.jackhuang.hmcl.setting.Profile; import org.jackhuang.hmcl.task.DownloadException; import org.jackhuang.hmcl.task.Schedulers; @@ -33,6 +35,7 @@ import org.jackhuang.hmcl.util.StringUtils; import java.io.File; import java.io.FileNotFoundException; +import java.io.IOException; import java.util.Map; import static org.jackhuang.hmcl.util.Lang.tryCast; @@ -41,20 +44,32 @@ import static org.jackhuang.hmcl.util.i18n.I18n.i18n; public class ModpackInstallWizardProvider implements WizardProvider { private final Profile profile; private final File file; + private final String updateVersion; public ModpackInstallWizardProvider(Profile profile) { - this(profile, null); + this(profile, null, null); } public ModpackInstallWizardProvider(Profile profile, File modpackFile) { + this(profile, modpackFile, null); + } + + public ModpackInstallWizardProvider(Profile profile, String updateVersion) { + this(profile, null, updateVersion); + } + + public ModpackInstallWizardProvider(Profile profile, File modpackFile, String updateVersion) { this.profile = profile; this.file = modpackFile; + this.updateVersion = updateVersion; } @Override public void start(Map settings) { if (file != null) settings.put(ModpackPage.MODPACK_FILE, file); + if (updateVersion != null) + settings.put(ModpackPage.MODPACK_NAME, updateVersion); settings.put(PROFILE, profile); } @@ -67,8 +82,21 @@ public class ModpackInstallWizardProvider implements WizardProvider { String name = tryCast(settings.get(ModpackPage.MODPACK_NAME), String.class).orElse(null); if (selected == null || modpack == null || name == null) return null; - return ModpackHelper.getInstallTask(profile, selected, name, modpack) - .then(Task.of(Schedulers.javafx(), () -> profile.setSelectedVersion(name))); + if (updateVersion != null) { + try { + return ModpackHelper.getUpdateTask(profile, selected, modpack.getEncoding(), name, ModpackHelper.readModpackConfiguration(profile.getRepository().getModpackConfiguration(name))); + } catch (UnsupportedModpackException e) { + Controllers.dialog(i18n("modpack.unsupported"), i18n("message.error"), MessageBox.ERROR_MESSAGE); + } catch (MismatchedModpackTypeException e) { + Controllers.dialog(i18n("modpack.mismatched_type"), i18n("message.error"), MessageBox.ERROR_MESSAGE); + } catch (IOException e) { + Controllers.dialog(i18n("modpack.invalid"), i18n("message.error"), MessageBox.ERROR_MESSAGE); + } + return null; + } else { + return ModpackHelper.getInstallTask(profile, selected, name, modpack) + .then(Task.of(Schedulers.javafx(), () -> profile.setSelectedVersion(name))); + } } @Override diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/download/ModpackPage.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/download/ModpackPage.java index 8079c553d..18fbb905e 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/download/ModpackPage.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/download/ModpackPage.java @@ -86,6 +86,12 @@ public final class ModpackPage extends StackPane implements WizardPage { File selectedFile; + Optional name = tryCast(controller.getSettings().get(MODPACK_NAME), String.class); + if (name.isPresent()) { + txtModpackName.setText(name.get()); + txtModpackName.setDisable(true); + } + Optional filePath = tryCast(controller.getSettings().get(MODPACK_FILE), File.class); if (filePath.isPresent()) { selectedFile = filePath.get(); @@ -112,14 +118,17 @@ public final class ModpackPage extends StackPane implements WizardPage { lblName.setText(manifest.getName()); lblVersion.setText(manifest.getVersion()); lblAuthor.setText(manifest.getAuthor()); - txtModpackName.setText(manifest.getName() + (StringUtils.isBlank(manifest.getVersion()) ? "" : "-" + manifest.getVersion())); lblModpackLocation.setText(selectedFile.getAbsolutePath()); - txtModpackName.getValidators().addAll( - new Validator(i18n("install.new_game.already_exists"), str -> !profile.getRepository().hasVersion(str) && StringUtils.isNotBlank(str)), - new Validator(i18n("version.forbidden_name"), str -> !profile.getRepository().forbidsVersion(str)) - ); - txtModpackName.textProperty().addListener(e -> btnInstall.setDisable(!txtModpackName.validate())); + + if (!name.isPresent()) { + txtModpackName.setText(manifest.getName() + (StringUtils.isBlank(manifest.getVersion()) ? "" : "-" + manifest.getVersion())); + txtModpackName.getValidators().addAll( + new Validator(i18n("install.new_game.already_exists"), str -> !profile.getRepository().hasVersion(str) && StringUtils.isNotBlank(str)), + new Validator(i18n("version.forbidden_name"), str -> !profile.getRepository().forbidsVersion(str)) + ); + txtModpackName.textProperty().addListener(e -> btnInstall.setDisable(!txtModpackName.validate())); + } }, e -> { Controllers.dialog(i18n("modpack.task.install.error"), i18n("message.error"), MessageBox.ERROR_MESSAGE); Platform.runLater(controller::onEnd); diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/Versions.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/Versions.java index 3095c07e0..a7948005e 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/Versions.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/Versions.java @@ -17,32 +17,22 @@ */ package org.jackhuang.hmcl.ui.versions; -import javafx.scene.layout.Region; import javafx.stage.FileChooser; import org.jackhuang.hmcl.game.GameRepository; import org.jackhuang.hmcl.game.LauncherHelper; -import org.jackhuang.hmcl.game.ModpackHelper; -import org.jackhuang.hmcl.mod.MismatchedModpackTypeException; -import org.jackhuang.hmcl.mod.UnsupportedModpackException; import org.jackhuang.hmcl.setting.Accounts; import org.jackhuang.hmcl.setting.EnumGameDirectory; import org.jackhuang.hmcl.setting.Profile; -import org.jackhuang.hmcl.task.Schedulers; -import org.jackhuang.hmcl.task.Task; -import org.jackhuang.hmcl.task.TaskExecutor; import org.jackhuang.hmcl.ui.Controllers; import org.jackhuang.hmcl.ui.FXUtils; -import org.jackhuang.hmcl.ui.construct.DialogCloseEvent; -import org.jackhuang.hmcl.ui.construct.MessageBox; +import org.jackhuang.hmcl.ui.download.ModpackInstallWizardProvider; import org.jackhuang.hmcl.ui.export.ExportWizardProvider; import org.jackhuang.hmcl.util.Logging; -import org.jackhuang.hmcl.util.io.CompressingUtils; import org.jackhuang.hmcl.util.io.FileUtils; import org.jackhuang.hmcl.util.platform.OperatingSystem; import java.io.File; import java.io.IOException; -import java.util.concurrent.atomic.AtomicReference; import java.util.logging.Level; import static org.jackhuang.hmcl.util.i18n.I18n.i18n; @@ -82,29 +72,7 @@ public class Versions { } public static void updateVersion(Profile profile, String version) { - 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) { - Task.ofResult("encoding", () -> CompressingUtils.findSuitableEncoding(selectedFile.toPath())) - .then(Task.of(Schedulers.javafx(), var -> { - AtomicReference region = new AtomicReference<>(); - try { - TaskExecutor executor = ModpackHelper.getUpdateTask(profile, selectedFile, var.get("encoding"), version, ModpackHelper.readModpackConfiguration(profile.getRepository().getModpackConfiguration(version))) - .then(Task.of(Schedulers.javafx(), () -> region.get().fireEvent(new DialogCloseEvent()))).executor(); - region.set(Controllers.taskDialog(executor, i18n("modpack.update"), "")); - executor.start(); - } catch (UnsupportedModpackException e) { - Controllers.dialog(i18n("modpack.unsupported"), i18n("message.error"), MessageBox.ERROR_MESSAGE); - } catch (MismatchedModpackTypeException e) { - Controllers.dialog(i18n("modpack.mismatched_type"), i18n("message.error"), MessageBox.ERROR_MESSAGE); - } catch (IOException e) { - Controllers.dialog(i18n("modpack.invalid"), i18n("message.error"), MessageBox.ERROR_MESSAGE); - } - })).start(); - - } + Controllers.getDecorator().startWizard(new ModpackInstallWizardProvider(profile, version)); } public static void cleanVersion(Profile profile, String id) { diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/ModpackUpdateTask.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/ModpackUpdateTask.java index 5e7e4df39..008b64693 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/ModpackUpdateTask.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/ModpackUpdateTask.java @@ -1,4 +1,80 @@ +/** + * Hello Minecraft! Launcher + * Copyright (C) 2018 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.mod; -public class ModpackUpdateTask { +import org.jackhuang.hmcl.game.DefaultGameRepository; +import org.jackhuang.hmcl.task.SilentException; +import org.jackhuang.hmcl.task.Task; +import org.jackhuang.hmcl.util.io.FileUtils; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Collection; +import java.util.Collections; + +public class ModpackUpdateTask extends Task { + + private final DefaultGameRepository repository; + private final String id; + private final Task updateTask; + private final Path backupFolder; + + public ModpackUpdateTask(DefaultGameRepository repository, String id, Task updateTask) { + this.repository = repository; + this.id = id; + this.updateTask = updateTask; + + Path backup = repository.getBaseDirectory().toPath().resolve("backup"); + while (true) { + int num = (int)(Math.random() * 10000000); + if (!Files.exists(backup.resolve(id + "-" + num))) { + backupFolder = backup.resolve(id + "-" + num); + break; + } + } + } + + @Override + public boolean isRelyingOnDependencies() { + return false; + } + + @Override + public Collection getDependencies() { + return Collections.singleton(updateTask); + } + + @Override + public void execute() throws Exception { + FileUtils.copyDirectory(repository.getVersionRoot(id).toPath(), backupFolder); + } + + @Override + public void postExecute() throws Exception { + if (isDependenciesSucceeded()) { + // Keep backup game version for further repair. + } else { + // Restore backup + repository.removeVersionFromDisk(id); + + FileUtils.copyDirectory(backupFolder, repository.getVersionRoot(id).toPath()); + + throw new SilentException(); + } + } }