Skip to content

Commit 49b0a99

Browse files
authored
优化检查更新日志的方式 (#4743)
1 parent 8f2c860 commit 49b0a99

File tree

1 file changed

+8
-49
lines changed

1 file changed

+8
-49
lines changed

HMCL/src/main/java/org/jackhuang/hmcl/ui/UpgradeDialog.java

Lines changed: 8 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,13 @@
2828
import org.jackhuang.hmcl.ui.construct.DialogCloseEvent;
2929
import org.jackhuang.hmcl.ui.construct.JFXHyperlink;
3030
import org.jackhuang.hmcl.upgrade.RemoteVersion;
31+
import org.jackhuang.hmcl.util.StringUtils;
3132
import org.jackhuang.hmcl.util.versioning.VersionNumber;
32-
import org.jetbrains.annotations.Nullable;
3333
import org.jsoup.Jsoup;
3434
import org.jsoup.nodes.Document;
35-
import org.jsoup.nodes.Element;
3635
import org.jsoup.nodes.Node;
37-
import org.jsoup.nodes.TextNode;
3836

39-
import java.io.IOException;
4037
import java.net.URL;
41-
import java.util.regex.Matcher;
42-
import java.util.regex.Pattern;
4338

4439
import static org.jackhuang.hmcl.Metadata.CHANGELOG_URL;
4540
import static org.jackhuang.hmcl.ui.FXUtils.onEscPressed;
@@ -48,25 +43,6 @@
4843

4944
public final class UpgradeDialog extends JFXDialogLayout {
5045

51-
private static final Pattern CHANGELOG_TITLE_PATTERN = Pattern.compile("HMCL (?<version>\\d(?:\\.\\d+)+)");
52-
53-
private static @Nullable VersionNumber extractVersionNumber(Node node) {
54-
String text;
55-
if (node instanceof Element element) {
56-
text = element.text();
57-
} else if (node instanceof TextNode textNode) {
58-
text = textNode.text();
59-
} else {
60-
return null;
61-
}
62-
63-
Matcher matcher = CHANGELOG_TITLE_PATTERN.matcher(text);
64-
if (matcher.find())
65-
return VersionNumber.asVersion(matcher.group("version"));
66-
else
67-
return null;
68-
}
69-
7046
public UpgradeDialog(RemoteVersion remoteVersion, Runnable updateRunnable) {
7147
maxWidthProperty().bind(Controllers.getScene().widthProperty().multiply(0.7));
7248
maxHeightProperty().bind(Controllers.getScene().heightProperty().multiply(0.7));
@@ -75,36 +51,19 @@ public UpgradeDialog(RemoteVersion remoteVersion, Runnable updateRunnable) {
7551
setBody(new ProgressIndicator());
7652

7753
String url = CHANGELOG_URL + remoteVersion.getChannel().channelName + ".html";
78-
boolean isPreview = remoteVersion.isPreview();
7954

8055
Task.supplyAsync(Schedulers.io(), () -> {
8156
VersionNumber targetVersion = VersionNumber.asVersion(remoteVersion.getVersion());
8257
VersionNumber currentVersion = VersionNumber.asVersion(Metadata.VERSION);
83-
if (targetVersion.compareTo(currentVersion) <= 0) {
58+
if (targetVersion.compareTo(currentVersion) <= 0)
59+
// Downgrade update, no need to display changelog
8460
return null;
85-
}
8661

8762
Document document = Jsoup.parse(new URL(url), 30 * 1000);
88-
String id = null;
89-
Node node = null;
90-
if (isPreview) {
91-
id = "nowpreview";
92-
node = document.selectFirst("#" + id);
93-
}
94-
if (node == null) {
95-
id = "nowchange";
96-
node = document.selectFirst("#" + id);
97-
}
98-
99-
if (node == null || !"h1".equals(node.nodeName()))
100-
throw new IOException("Cannot find current changelog in document");
101-
102-
VersionNumber changelogVersion = extractVersionNumber(node);
103-
if (changelogVersion == null)
104-
throw new IOException("Cannot find current changelog in document. The node: " + node);
63+
Node node = document.selectFirst("h1[data-version=\"%s\"]".formatted(targetVersion));
10564

106-
if (!targetVersion.equals(changelogVersion)) {
107-
LOG.warning("The changelog has not been updated yet. Expected: " + targetVersion + ", Actual: " + changelogVersion);
65+
if (node == null || !"h1".equals(node.nodeName())) {
66+
LOG.warning("Changelog not found");
10867
return null;
10968
}
11069

@@ -115,8 +74,8 @@ public UpgradeDialog(RemoteVersion remoteVersion, Runnable updateRunnable) {
11574

11675
do {
11776
if ("h1".equals(node.nodeName())) {
118-
changelogVersion = extractVersionNumber(node);
119-
if (changelogVersion == null || changelogVersion.compareTo(currentVersion) <= 0) {
77+
String changelogVersion = node.attr("data-version");
78+
if (StringUtils.isBlank(changelogVersion) || currentVersion.compareTo(changelogVersion) >= 0) {
12079
break;
12180
}
12281
}

0 commit comments

Comments
 (0)