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
This commit is contained in:
Glavo
2023-01-14 02:37:11 +08:00
committed by GitHub
parent 0130676b42
commit 753ba956fe
3 changed files with 42 additions and 24 deletions

View File

@@ -61,6 +61,7 @@ public class ModUpdatesPage extends BorderPane implements DecoratorPage {
private final ModManager modManager; private final ModManager modManager;
private final ObservableList<ModUpdateObject> objects; private final ObservableList<ModUpdateObject> objects;
@SuppressWarnings("unchecked")
public ModUpdatesPage(ModManager modManager, List<LocalModFile.ModUpdate> updates) { public ModUpdatesPage(ModManager modManager, List<LocalModFile.ModUpdate> updates) {
this.modManager = modManager; this.modManager = modManager;
@@ -138,7 +139,7 @@ public class ModUpdatesPage extends BorderPane implements DecoratorPage {
fireEvent(new PageCloseEvent()); fireEvent(new PageCloseEvent());
if (!task.getFailedMods().isEmpty()) { if (!task.getFailedMods().isEmpty()) {
Controllers.dialog(i18n("mods.check_updates.failed") + "\n" + 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"), i18n("install.failed"),
MessageDialogPane.MessageType.ERROR); MessageDialogPane.MessageType.ERROR);
} }
@@ -192,7 +193,7 @@ public class ModUpdatesPage extends BorderPane implements DecoratorPage {
public ModUpdateObject(LocalModFile.ModUpdate data) { public ModUpdateObject(LocalModFile.ModUpdate data) {
this.data = data; this.data = data;
enabled.set(true); enabled.set(!data.getLocalMod().getModManager().isDisabled(data.getLocalMod().getFile()));
fileName.set(data.getLocalMod().getFileName()); fileName.set(data.getLocalMod().getFileName());
currentVersion.set(data.getCurrentVersion().getVersion()); currentVersion.set(data.getCurrentVersion().getVersion());
targetVersion.set(data.getCandidates().get(0).getVersion()); targetVersion.set(data.getCandidates().get(0).getVersion());
@@ -274,28 +275,37 @@ public class ModUpdatesPage extends BorderPane implements DecoratorPage {
setStage("mods.check_updates.update"); setStage("mods.check_updates.update");
getProperties().put("total", mods.size()); getProperties().put("total", mods.size());
dependents = mods.stream() this.dependents = new ArrayList<>();
.map(mod -> { for (Pair<LocalModFile, RemoteMod.Version> mod : mods) {
return Task LocalModFile local = mod.getKey();
.runAsync(Schedulers.javafx(), () -> mod.getKey().setOld(true)) RemoteMod.Version remote = mod.getValue();
.thenComposeAsync(() -> { boolean isDisabled = local.getModManager().isDisabled(local.getFile());
FileDownloadTask task = new FileDownloadTask(
new URL(mod.getValue().getFile().getUrl()),
modManager.getModsDirectory().resolve(mod.getValue().getFile().getFilename()).toFile());
task.setName(mod.getValue().getName()); dependents.add(Task
return task; .runAsync(Schedulers.javafx(), () -> local.setOld(true))
}) .thenComposeAsync(() -> {
.whenComplete(Schedulers.javafx(), exception -> { String fileName = remote.getFile().getFilename();
if (exception != null) { if (isDisabled)
// restore state if failed fileName += ModManager.DISABLED_EXTENSION;
mod.getKey().setOld(false);
failedMods.add(mod.getKey()); FileDownloadTask task = new FileDownloadTask(
} new URL(remote.getFile().getUrl()),
}) modManager.getModsDirectory().resolve(fileName).toFile());
.withCounter("mods.check_updates.update");
}) task.setName(remote.getName());
.collect(Collectors.toList()); 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<LocalModFile> getFailedMods() { public List<LocalModFile> getFailedMods() {

View File

@@ -170,6 +170,10 @@ public final class LocalModFile implements Comparable<LocalModFile> {
} }
} }
public void disable() throws IOException {
file = modManager.disableMod(file);
}
public ModUpdate checkUpdates(String gameVersion, RemoteModRepository repository) throws IOException { public ModUpdate checkUpdates(String gameVersion, RemoteModRepository repository) throws IOException {
Optional<RemoteMod.Version> currentVersion = repository.getRemoteVersionByLocalFile(this, file); Optional<RemoteMod.Version> currentVersion = repository.getRemoteVersionByLocalFile(this, file);
if (!currentVersion.isPresent()) return null; if (!currentVersion.isPresent()) return null;

View File

@@ -234,7 +234,11 @@ public final class ModManager {
public Path disableMod(Path file) throws IOException { public Path disableMod(Path file) throws IOException {
if (isOld(file)) return file; // no need to disable an old mod. 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)) if (Files.exists(file))
Files.move(file, disabled, StandardCopyOption.REPLACE_EXISTING); Files.move(file, disabled, StandardCopyOption.REPLACE_EXISTING);
return disabled; return disabled;