Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,15 @@ public StringProperty defaultAddonSourceProperty() {
return defaultAddonSource;
}

/// Whether to update addons to preview versions
@SerializedName("defaultUpdateAddonsToPreview")
private final BooleanProperty defaultUpdateAddonsToPreview = new SimpleBooleanProperty(true);

/// Returns whether to update addons to preview versions property by default.
public BooleanProperty defaultUpdateAddonsToPreviewProperty() {
return defaultUpdateAddonsToPreview;
}

/// Whether proxy authentication is enabled.
@SerializedName("hasProxyAuth")
private final BooleanProperty hasProxyAuth = new SimpleBooleanProperty();
Expand Down
58 changes: 28 additions & 30 deletions HMCL/src/main/java/org/jackhuang/hmcl/ui/FXUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import javafx.util.Callback;
import javafx.util.Duration;
import javafx.util.StringConverter;
import javafx.util.Subscription;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.setting.StyleSheets;
import org.jackhuang.hmcl.task.CacheFileTask;
Expand Down Expand Up @@ -1072,7 +1073,7 @@ public static void unbindWindowsSize(JFXComboBox<String> comboBox, IntegerProper
heightProperty.removeListener(binding);
}

public static void bindAllEnabled(BooleanProperty allEnabled, BooleanProperty... children) {
public static List<Subscription> bindAllEnabled(BooleanProperty allEnabled, BooleanProperty... children) {
int itemCount = children.length;
int childSelectedCount = 0;
for (BooleanProperty child : children) {
Expand All @@ -1082,49 +1083,46 @@ public static void bindAllEnabled(BooleanProperty allEnabled, BooleanProperty...

allEnabled.set(childSelectedCount == itemCount);

class Listener implements InvalidationListener {
class AllListener {
private int childSelectedCount;
private boolean updating = false;
private final List<Subscription> subscriptions = new ArrayList<>(children.length + 1);

public Listener(int childSelectedCount) {
public AllListener(int childSelectedCount) {
this.childSelectedCount = childSelectedCount;
}

@Override
public void invalidated(Observable observable) {
if (updating)
return;

updating = true;
try {
boolean value = ((BooleanProperty) observable).get();

if (observable == allEnabled) {
{
subscriptions.add(allEnabled.subscribe(() -> {
if (updating) return;
updating = true;
try {
boolean value = allEnabled.get();
for (BooleanProperty child : children) {
child.setValue(value);
child.set(value);
}
childSelectedCount = value ? itemCount : 0;
} else {
if (value)
childSelectedCount++;
else
childSelectedCount--;

allEnabled.set(childSelectedCount == itemCount);
} finally {
updating = false;
}
} finally {
updating = false;
}));
for (var child : children) {
subscriptions.add(child.subscribe(() -> {
if (updating) return;
updating = true;
try {
if (child.get()) childSelectedCount++;
else childSelectedCount--;
allEnabled.set(childSelectedCount == itemCount);
} finally {
updating = false;
}
}));
}
}
}

InvalidationListener listener = new Listener(childSelectedCount);

WeakInvalidationListener weakListener = new WeakInvalidationListener(listener);
allEnabled.addListener(listener);
for (BooleanProperty child : children) {
child.addListener(weakListener);
}
return new AllListener(childSelectedCount).subscriptions;
}

public static void setIcon(Stage stage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,54 @@
*/
package org.jackhuang.hmcl.ui.instances;

import org.jackhuang.hmcl.addon.AddonUpdate;
import org.jackhuang.hmcl.download.DownloadProvider;
import org.jackhuang.hmcl.addon.LocalAddonFile;
import org.jackhuang.hmcl.addon.RemoteAddon;
import org.jackhuang.hmcl.task.Schedulers;
import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.Pair;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import static org.jackhuang.hmcl.util.logging.Logger.LOG;

public class AddonCheckUpdatesTask<T extends LocalAddonFile> extends Task<List<LocalAddonFile.AddonUpdate>> {
private final DownloadProvider downloadProvider;
private final List<Task<LocalAddonFile.AddonUpdate>> dependents;
public class AddonCheckUpdatesTask extends Task<AddonCheckUpdatesTask.Result> {
private final List<Task<Pair<AddonUpdate, @Nullable AddonUpdate>>> dependents;

public AddonCheckUpdatesTask(DownloadProvider downloadProvider, String gameVersion, Collection<T> addons) {
this.downloadProvider = downloadProvider;
public AddonCheckUpdatesTask(DownloadProvider downloadProvider, String gameVersion, Collection<? extends LocalAddonFile> addons) {
dependents = addons.stream().map(addon ->
Task.supplyAsync(Schedulers.io(), () -> {
LocalAddonFile.AddonUpdate candidate = null;
AddonUpdate candidate = null;
AddonUpdate candidateRelease = null;
for (RemoteAddon.Source source : RemoteAddon.Source.values()) {
LocalAddonFile.AddonUpdate update = null;
AddonUpdate update = null;
AddonUpdate updateRelease = null;
try {
update = addon.checkUpdates(downloadProvider, gameVersion, source);
var u = addon.checkUpdates(downloadProvider, gameVersion, source);
if (u != null) {
update = u.key();
updateRelease = u.value();
}
} catch (IOException e) {
LOG.warning(String.format("Cannot check update for addon %s.", addon.getFileName()), e);
}
if (update == null) {
continue;
}

if (candidate == null || candidate.targetVersion().datePublished().isBefore(update.targetVersion().datePublished())) {
if (update == null) continue;
if (candidate == null || candidate.targetVersion().datePublished().isBefore(update.targetVersion().datePublished()))
candidate = update;
}

if (updateRelease == null) continue;
if (candidateRelease == null || candidateRelease.targetVersion().datePublished().isBefore(updateRelease.targetVersion().datePublished()))
candidateRelease = updateRelease;
}

return candidate;
if (candidate == null) return null; // If there's no candidate for all channels, then no candidate for release channel
return Pair.pair(candidate, candidateRelease);
}).setName(addon.getFileName()).setSignificance(TaskSignificance.MAJOR).withCounter("update.checking")
).toList();

Expand Down Expand Up @@ -85,8 +94,15 @@ public boolean isRelyingOnDependents() {

@Override
public void execute() throws Exception {
setResult(dependents.stream()
.map(Task::getResult)
.filter(Objects::nonNull).toList());
List<AddonUpdate> commonUpdates = new ArrayList<>(), releaseUpdates = new ArrayList<>();
dependents.stream().map(Task::getResult).filter(Objects::nonNull)
.forEachOrdered(pair -> {
commonUpdates.add(pair.key());
if (pair.value() != null) releaseUpdates.add(pair.value());
});
setResult(new Result(List.copyOf(commonUpdates), List.copyOf(releaseUpdates)));
}

public record Result(List<AddonUpdate> commonUpdates, List<AddonUpdate> releaseUpdates) {
}
}
Loading