Skip to content

Commit f03a4ff

Browse files
Glavo3gf8jv4dv
andauthored
[release/3.6] 添加“提前预览 HMCL 版本”选项 (#4668)
#4223 #4598 #4599 --------- Co-authored-by: 3gf8jv4dv <3gf8jv4dv@gmail.com>
1 parent de9a105 commit f03a4ff

File tree

10 files changed

+78
-22
lines changed

10 files changed

+78
-22
lines changed

HMCL/src/main/java/org/jackhuang/hmcl/setting/Config.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ public static Config fromJson(String json) throws JsonParseException {
181181
@SerializedName("promptedVersion")
182182
private StringProperty promptedVersion = new SimpleStringProperty();
183183

184+
@SerializedName("acceptPreviewUpdate")
185+
private BooleanProperty acceptPreviewUpdate = new SimpleBooleanProperty(false);
186+
184187
@SerializedName("_version")
185188
private IntegerProperty configVersion = new SimpleIntegerProperty(0);
186189

@@ -671,6 +674,18 @@ public void setPromptedVersion(String promptedVersion) {
671674
this.promptedVersion.set(promptedVersion);
672675
}
673676

677+
public boolean isAcceptPreviewUpdate() {
678+
return acceptPreviewUpdate.get();
679+
}
680+
681+
public BooleanProperty acceptPreviewUpdateProperty() {
682+
return acceptPreviewUpdate;
683+
}
684+
685+
public void setAcceptPreviewUpdate(boolean acceptPreviewUpdate) {
686+
this.acceptPreviewUpdate.set(acceptPreviewUpdate);
687+
}
688+
674689
public ObservableMap<String, Object> getShownTips() {
675690
return shownTips;
676691
}

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,30 @@ public UpgradeDialog(RemoteVersion remoteVersion, Runnable updateRunnable) {
4848
setBody(new ProgressIndicator());
4949

5050
String url = CHANGELOG_URL + remoteVersion.getChannel().channelName + ".html";
51+
boolean isPreview = remoteVersion.isPreview();
5152
Task.supplyAsync(Schedulers.io(), () -> {
5253
Document document = Jsoup.parse(new URL(url), 30 * 1000);
53-
Node node = document.selectFirst("#nowchange");
54+
String id = null;
55+
Node node = null;
56+
if (isPreview) {
57+
id = "nowpreview";
58+
node = document.selectFirst("#" + id);
59+
}
60+
if (node == null) {
61+
id = "nowchange";
62+
node = document.selectFirst("#" + id);
63+
}
64+
5465
if (node == null || !"h1".equals(node.nodeName()))
55-
throw new IOException("Cannot find #nowchange in document");
66+
throw new IOException("Cannot find current changelog in document");
5667

5768
HTMLRenderer renderer = new HTMLRenderer(uri -> {
5869
LOG.info("Open link: " + uri);
5970
FXUtils.openLink(uri.toString());
6071
});
6172

6273
do {
63-
if ("h1".equals(node.nodeName()) && !"nowchange".equals(node.attr("id"))) {
74+
if ("h1".equals(node.nodeName()) && !id.equals(node.attr("id"))) {
6475
break;
6576
}
6677
renderer.appendNode(node);

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,12 @@ public SettingsPage() {
106106
chkUpdateStable.setUserData(UpdateChannel.STABLE);
107107
ObjectProperty<UpdateChannel> updateChannel = selectedItemPropertyFor(updateChannelGroup, UpdateChannel.class);
108108
updateChannel.set(UpdateChannel.getChannel());
109-
updateChannel.addListener((a, b, newValue) -> {
110-
UpdateChecker.requestCheckUpdate(newValue);
111-
});
109+
110+
InvalidationListener checkUpdateListener = e -> {
111+
UpdateChecker.requestCheckUpdate(updateChannel.get(), previewPane.isSelected());
112+
};
113+
updateChannel.addListener(checkUpdateListener);
114+
previewPane.selectedProperty().addListener(checkUpdateListener);
112115
// ====
113116
}
114117

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.jackhuang.hmcl.ui.construct.ComponentList;
3838
import org.jackhuang.hmcl.ui.construct.ComponentSublist;
3939
import org.jackhuang.hmcl.ui.construct.MultiFileItem;
40+
import org.jackhuang.hmcl.ui.construct.OptionToggleButton;
4041
import org.jackhuang.hmcl.util.i18n.I18n;
4142
import org.jackhuang.hmcl.util.i18n.Locales.SupportedLocale;
4243

@@ -56,6 +57,7 @@ public abstract class SettingsView extends StackPane {
5657
protected final JFXRadioButton chkUpdateStable;
5758
protected final JFXRadioButton chkUpdateDev;
5859
protected final JFXButton btnUpdate;
60+
protected final OptionToggleButton previewPane;
5961
protected final ScrollPane scroll;
6062

6163
public SettingsView() {
@@ -145,6 +147,15 @@ public SettingsView() {
145147
settingsPane.getContent().add(updatePane);
146148
}
147149

150+
{
151+
previewPane = new OptionToggleButton();
152+
previewPane.setTitle(i18n("update.preview"));
153+
previewPane.selectedProperty().bindBidirectional(config().acceptPreviewUpdateProperty());
154+
FXUtils.installFastTooltip(previewPane, i18n("update.preview.tooltip"));
155+
156+
settingsPane.getContent().add(previewPane);
157+
}
158+
148159
{
149160
fileCommonLocation = new MultiFileItem<>();
150161
fileCommonLocationSublist = new ComponentSublist();

HMCL/src/main/java/org/jackhuang/hmcl/upgrade/RemoteVersion.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@
2929

3030
public final class RemoteVersion {
3131

32-
public static RemoteVersion fetch(UpdateChannel channel, String url) throws IOException {
32+
public static RemoteVersion fetch(UpdateChannel channel, boolean preview, String url) throws IOException {
3333
try {
3434
JsonObject response = JsonUtils.fromNonNullJson(NetworkUtils.doGet(NetworkUtils.toURL(url)), JsonObject.class);
3535
String version = Optional.ofNullable(response.get("version")).map(JsonElement::getAsString).orElseThrow(() -> new IOException("version is missing"));
3636
String jarUrl = Optional.ofNullable(response.get("jar")).map(JsonElement::getAsString).orElse(null);
3737
String jarHash = Optional.ofNullable(response.get("jarsha1")).map(JsonElement::getAsString).orElse(null);
3838
boolean force = Optional.ofNullable(response.get("force")).map(JsonElement::getAsBoolean).orElse(false);
3939
if (jarUrl != null && jarHash != null) {
40-
return new RemoteVersion(channel, version, jarUrl, Type.JAR, new IntegrityCheck("SHA-1", jarHash), force);
40+
return new RemoteVersion(channel, version, jarUrl, Type.JAR, new IntegrityCheck("SHA-1", jarHash), preview, force);
4141
} else {
4242
throw new IOException("No download url is available");
4343
}
@@ -51,14 +51,16 @@ public static RemoteVersion fetch(UpdateChannel channel, String url) throws IOEx
5151
private final String url;
5252
private final Type type;
5353
private final IntegrityCheck integrityCheck;
54+
private final boolean preview;
5455
private final boolean force;
5556

56-
public RemoteVersion(UpdateChannel channel, String version, String url, Type type, IntegrityCheck integrityCheck, boolean force) {
57+
public RemoteVersion(UpdateChannel channel, String version, String url, Type type, IntegrityCheck integrityCheck, boolean preview, boolean force) {
5758
this.channel = channel;
5859
this.version = version;
5960
this.url = url;
6061
this.type = type;
6162
this.integrityCheck = integrityCheck;
63+
this.preview = preview;
6264
this.force = force;
6365
}
6466

@@ -82,6 +84,10 @@ public IntegrityCheck getIntegrityCheck() {
8284
return integrityCheck;
8385
}
8486

87+
public boolean isPreview() {
88+
return preview;
89+
}
90+
8591
public boolean isForce() {
8692
return force;
8793
}

HMCL/src/main/java/org/jackhuang/hmcl/upgrade/UpdateChecker.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@
2727
import org.jackhuang.hmcl.util.versioning.VersionNumber;
2828

2929
import java.io.IOException;
30+
import java.util.LinkedHashMap;
3031

31-
import static org.jackhuang.hmcl.util.Lang.mapOf;
32-
import static org.jackhuang.hmcl.util.Lang.thread;
32+
import static org.jackhuang.hmcl.setting.ConfigHolder.config;
33+
import static org.jackhuang.hmcl.util.Lang.*;
3334
import static org.jackhuang.hmcl.util.logging.Logger.LOG;
34-
import static org.jackhuang.hmcl.util.Pair.pair;
3535

3636
public final class UpdateChecker {
37-
private UpdateChecker() {}
37+
private UpdateChecker() {
38+
}
3839

3940
private static final ObjectProperty<RemoteVersion> latestVersion = new SimpleObjectProperty<>();
4041
private static final BooleanBinding outdated = Bindings.createBooleanBinding(
@@ -55,7 +56,7 @@ private UpdateChecker() {}
5556
private static final ReadOnlyBooleanWrapper checkingUpdate = new ReadOnlyBooleanWrapper(false);
5657

5758
public static void init() {
58-
requestCheckUpdate(UpdateChannel.getChannel());
59+
requestCheckUpdate(UpdateChannel.getChannel(), config().isAcceptPreviewUpdate());
5960
}
6061

6162
public static RemoteVersion getLatestVersion() {
@@ -82,24 +83,25 @@ public static ReadOnlyBooleanProperty checkingUpdateProperty() {
8283
return checkingUpdate.getReadOnlyProperty();
8384
}
8485

85-
private static RemoteVersion checkUpdate(UpdateChannel channel) throws IOException {
86+
private static RemoteVersion checkUpdate(UpdateChannel channel, boolean preview) throws IOException {
8687
if (!IntegrityChecker.DISABLE_SELF_INTEGRITY_CHECK && !IntegrityChecker.isSelfVerified()) {
8788
throw new IOException("Self verification failed");
8889
}
8990

90-
String url = NetworkUtils.withQuery(Metadata.HMCL_UPDATE_URL, mapOf(
91-
pair("version", Metadata.VERSION),
92-
pair("channel", channel.channelName)));
91+
LinkedHashMap<String, String> query = new LinkedHashMap<>();
92+
query.put("version", Metadata.VERSION);
93+
query.put("channel", preview ? channel.channelName + "-preview" : channel.channelName);
9394

94-
return RemoteVersion.fetch(channel, url);
95+
String url = NetworkUtils.withQuery(Metadata.HMCL_UPDATE_URL, query);
96+
return RemoteVersion.fetch(channel, preview, url);
9597
}
9698

9799
private static boolean isDevelopmentVersion(String version) {
98100
return version.contains("@") || // eg. @develop@
99101
version.contains("SNAPSHOT"); // eg. 3.5.SNAPSHOT
100102
}
101103

102-
public static void requestCheckUpdate(UpdateChannel channel) {
104+
public static void requestCheckUpdate(UpdateChannel channel, boolean preview) {
103105
Platform.runLater(() -> {
104106
if (isCheckingUpdate())
105107
return;
@@ -108,8 +110,8 @@ public static void requestCheckUpdate(UpdateChannel channel) {
108110
thread(() -> {
109111
RemoteVersion result = null;
110112
try {
111-
result = checkUpdate(channel);
112-
LOG.info("Latest version (" + channel + ") is " + result);
113+
result = checkUpdate(channel, preview);
114+
LOG.info("Latest version (" + channel + ", preview=" + preview + ") is " + result);
113115
} catch (IOException e) {
114116
LOG.warning("Failed to check for update", e);
115117
}

HMCL/src/main/resources/assets/lang/I18N.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,8 @@ update.note=Beta and Nightly channels may have more features or fixes, but they
14231423
update.latest=This is the latest version
14241424
update.no_browser=Cannot open in system browser. But we copied the link to your clipboard, and you can open it manually.
14251425
update.tooltip=Update
1426+
update.preview=Preview HMCL releases early
1427+
update.preview.tooltip=Enable this option to receive new versions of HMCL early for testing before their official release.
14261428

14271429
version=Games
14281430
version.name=Instance Name

HMCL/src/main/resources/assets/lang/I18N_es.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,8 @@ update.note=Advertencia: Las versiones beta y las versiones nocturnas pueden ten
14191419
update.latest=Esta es la última versión.
14201420
update.no_browser=No se puede abrir en el navegador del sistema. Pero, hemos copiado el enlace a su portapapeles y puede abrirlo manualmente.
14211421
update.tooltip=Actualización
1422+
update.preview=Vista previa de actualizaciones anticipadas
1423+
update.preview.tooltip=Activa esta opción para recibir nuevas versiones del lanzador antes de su lanzamiento oficial para probarlas
14221424

14231425
version=Juegos
14241426
version.name=Nombre de instancia

HMCL/src/main/resources/assets/lang/I18N_zh.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,8 @@ update.note=開發版與預覽版包含更多的功能以及錯誤修復,但
12071207
update.latest=目前版本為最新版本
12081208
update.no_browser=無法開啟瀏覽器。網址已經複製到剪貼簿了,你可以手動複製網址開啟頁面。
12091209
update.tooltip=更新
1210+
update.preview=提前測試 HMCL 預覽版本
1211+
update.preview.tooltip=啟用此選項,你將可以提前取得 HMCL 的新版本,以便在正式發布前進行測試。
12101212

12111213
version=遊戲
12121214
version.name=遊戲實例名稱

HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,8 @@ update.note=开发版与预览版包含更多的功能以及错误修复,但
12171217
update.latest=当前版本为最新版本
12181218
update.no_browser=无法打开浏览器。网址已经复制到剪贴板,你可以手动粘贴网址打开页面。
12191219
update.tooltip=更新
1220+
update.preview=提前预览 HMCL 版本
1221+
update.preview.tooltip=启用此选项,你将可以提前获取 HMCL 的新版本,以便在正式发布前进行测试。
12201222

12211223
version=游戏
12221224
version.name=游戏实例名称

0 commit comments

Comments
 (0)