From 753ba956fea8123fc3903b4cffa049fd20fb53cd Mon Sep 17 00:00:00 2001 From: Glavo Date: Sat, 14 Jan 2023 02:37:11 +0800 Subject: [PATCH] fix #1705: Do not re-enable mod after update (#2001) * fix #1705: Do not re-enable mod after update * update * Do not select disabled mods by default --- .../hmcl/ui/versions/ModUpdatesPage.java | 56 +++++++++++-------- .../org/jackhuang/hmcl/mod/LocalModFile.java | 4 ++ .../org/jackhuang/hmcl/mod/ModManager.java | 6 +- 3 files changed, 42 insertions(+), 24 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/ModUpdatesPage.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/ModUpdatesPage.java index 092afed4b..2b5c0f9df 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/ModUpdatesPage.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/ModUpdatesPage.java @@ -61,6 +61,7 @@ public class ModUpdatesPage extends BorderPane implements DecoratorPage { private final ModManager modManager; private final ObservableList objects; + @SuppressWarnings("unchecked") public ModUpdatesPage(ModManager modManager, List updates) { this.modManager = modManager; @@ -138,7 +139,7 @@ public class ModUpdatesPage extends BorderPane implements DecoratorPage { fireEvent(new PageCloseEvent()); if (!task.getFailedMods().isEmpty()) { Controllers.dialog(i18n("mods.check_updates.failed") + "\n" + - task.getFailedMods().stream().map(LocalModFile::getFileName).collect(Collectors.joining("\n")), + task.getFailedMods().stream().map(LocalModFile::getFileName).collect(Collectors.joining("\n")), i18n("install.failed"), MessageDialogPane.MessageType.ERROR); } @@ -192,7 +193,7 @@ public class ModUpdatesPage extends BorderPane implements DecoratorPage { public ModUpdateObject(LocalModFile.ModUpdate data) { this.data = data; - enabled.set(true); + enabled.set(!data.getLocalMod().getModManager().isDisabled(data.getLocalMod().getFile())); fileName.set(data.getLocalMod().getFileName()); currentVersion.set(data.getCurrentVersion().getVersion()); targetVersion.set(data.getCandidates().get(0).getVersion()); @@ -274,28 +275,37 @@ public class ModUpdatesPage extends BorderPane implements DecoratorPage { setStage("mods.check_updates.update"); getProperties().put("total", mods.size()); - dependents = mods.stream() - .map(mod -> { - return Task - .runAsync(Schedulers.javafx(), () -> mod.getKey().setOld(true)) - .thenComposeAsync(() -> { - FileDownloadTask task = new FileDownloadTask( - new URL(mod.getValue().getFile().getUrl()), - modManager.getModsDirectory().resolve(mod.getValue().getFile().getFilename()).toFile()); + this.dependents = new ArrayList<>(); + for (Pair mod : mods) { + LocalModFile local = mod.getKey(); + RemoteMod.Version remote = mod.getValue(); + boolean isDisabled = local.getModManager().isDisabled(local.getFile()); - task.setName(mod.getValue().getName()); - return task; - }) - .whenComplete(Schedulers.javafx(), exception -> { - if (exception != null) { - // restore state if failed - mod.getKey().setOld(false); - failedMods.add(mod.getKey()); - } - }) - .withCounter("mods.check_updates.update"); - }) - .collect(Collectors.toList()); + dependents.add(Task + .runAsync(Schedulers.javafx(), () -> local.setOld(true)) + .thenComposeAsync(() -> { + String fileName = remote.getFile().getFilename(); + if (isDisabled) + fileName += ModManager.DISABLED_EXTENSION; + + FileDownloadTask task = new FileDownloadTask( + new URL(remote.getFile().getUrl()), + modManager.getModsDirectory().resolve(fileName).toFile()); + + task.setName(remote.getName()); + return task; + }) + .whenComplete(Schedulers.javafx(), exception -> { + if (exception != null) { + // restore state if failed + local.setOld(false); + if (isDisabled) + local.disable(); + failedMods.add(local); + } + }) + .withCounter("mods.check_updates.update")); + } } public List getFailedMods() { diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/LocalModFile.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/LocalModFile.java index b39d3757b..d43dc843f 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/LocalModFile.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/LocalModFile.java @@ -170,6 +170,10 @@ public final class LocalModFile implements Comparable { } } + public void disable() throws IOException { + file = modManager.disableMod(file); + } + public ModUpdate checkUpdates(String gameVersion, RemoteModRepository repository) throws IOException { Optional currentVersion = repository.getRemoteVersionByLocalFile(this, file); if (!currentVersion.isPresent()) return null; diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/ModManager.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/ModManager.java index b8b548b2f..538eeabf2 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/ModManager.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/ModManager.java @@ -234,7 +234,11 @@ public final class ModManager { public Path disableMod(Path file) throws IOException { if (isOld(file)) return file; // no need to disable an old mod. - Path disabled = file.resolveSibling(StringUtils.addSuffix(FileUtils.getName(file), DISABLED_EXTENSION)); + + String fileName = FileUtils.getName(file); + if (fileName.endsWith(DISABLED_EXTENSION)) return file; + + Path disabled = file.resolveSibling(fileName + DISABLED_EXTENSION); if (Files.exists(file)) Files.move(file, disabled, StandardCopyOption.REPLACE_EXISTING); return disabled;