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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 ObservableList<ModUpdateObject> objects;
@SuppressWarnings("unchecked")
public ModUpdatesPage(ModManager modManager, List<LocalModFile.ModUpdate> 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<LocalModFile, RemoteMod.Version> 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<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 {
Optional<RemoteMod.Version> currentVersion = repository.getRemoteVersionByLocalFile(this, file);
if (!currentVersion.isPresent()) return null;

View File

@ -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;