Skip to content

Commit 0d9a275

Browse files
authored
[release/3.6] 检测到配置文件由高版本启动器创建时禁止保存配置文件 (#4449)
#4414
1 parent ab72ac3 commit 0d9a275

7 files changed

Lines changed: 28 additions & 14 deletions

File tree

HMCL/src/main/java/org/jackhuang/hmcl/Launcher.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ public void start(Stage primaryStage) {
105105
return;
106106
}
107107

108+
if (ConfigHolder.isUnsupportedVersion()) {
109+
showAlert(AlertType.WARNING, i18n("fatal.config_unsupported_version"));
110+
}
111+
108112
if (Metadata.HMCL_CURRENT_DIRECTORY.toString().indexOf('=') >= 0) {
109113
Main.showWarningAndContinue(i18n("fatal.illegal_char"));
110114
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050

5151
public final class Config implements Observable {
5252

53+
public static final int CURRENT_VERSION = 2;
5354
public static final int CURRENT_UI_VERSION = 0;
5455

5556
public static final Gson CONFIG_GSON = new GsonBuilder()

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ private ConfigHolder() {
4545
private static GlobalConfig globalConfigInstance;
4646
private static boolean newlyCreated;
4747
private static boolean ownerChanged = false;
48+
private static boolean unsupportedVersion = false;
4849

4950
public static Config config() {
5051
if (configInstance == null) {
@@ -72,6 +73,10 @@ public static boolean isOwnerChanged() {
7273
return ownerChanged;
7374
}
7475

76+
public static boolean isUnsupportedVersion() {
77+
return unsupportedVersion;
78+
}
79+
7580
public static void init() throws IOException {
7681
if (configInstance != null) {
7782
throw new IllegalStateException("Configuration is already loaded");
@@ -82,7 +87,8 @@ public static void init() throws IOException {
8287
LOG.info("Config location: " + configLocation);
8388

8489
configInstance = loadConfig();
85-
configInstance.addListener(source -> FileSaver.save(configLocation, configInstance.toJson()));
90+
if (!unsupportedVersion)
91+
configInstance.addListener(source -> FileSaver.save(configLocation, configInstance.toJson()));
8692

8793
globalConfigInstance = loadGlobalConfig();
8894
globalConfigInstance.addListener(source -> FileSaver.save(GLOBAL_CONFIG_PATH, globalConfigInstance.toJson()));
@@ -162,7 +168,14 @@ private static Config loadConfig() throws IOException {
162168
if (deserialized == null) {
163169
LOG.info("Config is empty");
164170
} else {
165-
ConfigUpgrader.upgradeConfig(deserialized, content);
171+
int configVersion = deserialized.getConfigVersion();
172+
if (configVersion < Config.CURRENT_VERSION) {
173+
ConfigUpgrader.upgradeConfig(deserialized, content);
174+
} else if (configVersion > Config.CURRENT_VERSION) {
175+
unsupportedVersion = true;
176+
LOG.warning(String.format("Current HMCL only support the configuration version up to %d. However, the version now is %d.", Config.CURRENT_VERSION, configVersion));
177+
}
178+
166179
return deserialized;
167180
}
168181
} catch (JsonParseException e) {

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ final class ConfigUpgrader {
3131
private ConfigUpgrader() {
3232
}
3333

34-
private static final int CURRENT_VERSION = 2;
35-
3634
/**
3735
* This method is for the compatibility with old HMCL versions.
3836
*
@@ -42,17 +40,10 @@ private ConfigUpgrader() {
4240
static void upgradeConfig(Config deserialized, String rawContent) {
4341
int configVersion = deserialized.getConfigVersion();
4442

45-
if (configVersion == CURRENT_VERSION) {
43+
if (configVersion >= Config.CURRENT_VERSION)
4644
return;
47-
}
48-
49-
if (configVersion > CURRENT_VERSION) {
50-
LOG.warning(String.format("Current HMCL only support the configuration version up to %d. However, the version now is %d.", CURRENT_VERSION, configVersion));
51-
deserialized.setConfigVersion(CURRENT_VERSION);
52-
return;
53-
}
5445

55-
LOG.info(String.format("Updating configuration from %d to %d.", configVersion, CURRENT_VERSION));
46+
LOG.info(String.format("Updating configuration from %d to %d.", configVersion, Config.CURRENT_VERSION));
5647
Map<?, ?> rawJson = Collections.unmodifiableMap(new Gson().<Map<?, ?>>fromJson(rawContent, Map.class));
5748

5849
if (configVersion < 1) {
@@ -100,6 +91,6 @@ static void upgradeConfig(Config deserialized, String rawContent) {
10091
}
10192
}
10293

103-
deserialized.setConfigVersion(CURRENT_VERSION);
94+
deserialized.setConfigVersion(Config.CURRENT_VERSION);
10495
}
10596
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,9 @@ fatal.config_loading_failure=Cannot load configuration files.\n\
387387
For macOS, try putting HMCL somewhere with permissions other than "Desktop", "Downloads", and "Documents" and try again.
388388
fatal.config_loading_failure.unix=Hello Minecraft! Launcher could not load the configuration file because it was created by user "%1$s".\n\
389389
Please open HMCL as root user (not recommended), or execute the following command in the terminal to change the ownership of the configuration file to the current user:\n%2$s
390+
fatal.config_unsupported_version=The current configuration file was created by a newer version of Hello Minecraft! Launcher, and this version of HMCL cannot load it properly.\n\
391+
Please update and restart HMCL.\n\
392+
Before updating the launcher, any settings you modify will not be saved.
390393
fatal.mac_app_translocation=Hello Minecraft! Launcher is isolated to a temporary directory by the OS due to macOS security mechanisms.\n\
391394
Please move HMCL to a different directory before attempting to open. Otherwise, your settings and game data may be lost after restarting.\n\
392395
Do you still want to continue?

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ fatal.config_change_owner_root=你正在使用 root 帳戶開啟 Hello Minecraft
376376
fatal.config_in_temp_dir=你正在暫存目錄中開啟 Hello Minecraft! Launcher,你的設定和遊戲資料可能會遺失。建議將 HMCL 移動至其他位置再開啟。\n是否繼續開啟?
377377
fatal.config_loading_failure=Hello Minecraft! Launcher 無法載入設定檔案。\n請確保 HMCL 對「%s」目錄及該目錄下的檔案擁有讀寫權限。
378378
fatal.config_loading_failure.unix=Hello Minecraft! Launcher 無法載入設定檔案,因為設定檔案是由使用者「%1$s」建立的。\n請使用 root 帳戶開啟 HMCL (不推薦),或在終端中執行以下指令將設定檔案的所有權變更為目前使用者:\n%2$s
379+
fatal.config_unsupported_version=當前設定檔案是由更高版本的 Hello Minecraft! Launcher 建立的,目前版本的 HMCL 無法正常載入。請更新並重新啟動 HMCL。\n在更新啟動器之前,你所做的所有設定更改都不會被保存。
379380
fatal.mac_app_translocation=由於 macOS 的安全機制,Hello Minecraft! Launcher 被系統隔離至暫存目錄中。\n請將 HMCL 移動到其他目錄後再嘗試開啟,否則你的設定和遊戲資料可能會在重啟後遺失。\n是否繼續開啟?
380381
fatal.migration_requires_manual_reboot=Hello Minecraft! Launcher 即將升級完成,請重新開啟 HMCL。
381382
fatal.apply_update_failure=我們很抱歉 Hello Minecraft! Launcher 無法自動完成升級程式,因為出現了一些問題。\n但你依然可以從 %s 處手動下載 HMCL 來完成升級。

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ fatal.config_change_owner_root=你正在使用 root 账户启动 Hello Minecraft
385385
fatal.config_in_temp_dir=你正在临时文件夹中启动 Hello Minecraft! Launcher,你的设置和游戏数据可能会丢失。建议将 HMCL 移动至其他位置再启动。\n你可以访问 https://docs.hmcl.net/help.html 页面寻求帮助。\n是否继续启动?
386386
fatal.config_loading_failure=Hello Minecraft! Launcher 无法加载配置文件。\n请确保 HMCL 对“%s”文件夹及该文件夹下的文件拥有读写权限。\n对于 macOS,尝试将 HMCL 放在除“桌面”“下载”“文稿”之外的有权限的地方再试。\n你可以访问 https://docs.hmcl.net/help.html 页面寻求帮助。
387387
fatal.config_loading_failure.unix=Hello Minecraft! Launcher 无法加载配置文件,因为配置文件是由用户“%1$s”创建的。\n请使用 root 账户启动 HMCL (不推荐),或在终端中执行以下命令将配置文件的所有权变更为当前用户:\n%2$s\n你可以访问 https://docs.hmcl.net/help.html 页面寻求帮助。
388+
fatal.config_unsupported_version=当前配置文件是由更高版本的 Hello Minecraft! Launcher 创建的,当前版本的 HMCL 无法正常加载。请更新并重启 HMCL。\n在更新启动器之前,你修改的所有设置都不会被保存。\n如果遇到问题,你可以点击右上角帮助按钮进行求助。
388389
fatal.mac_app_translocation=由于 macOS 的安全机制,Hello Minecraft! Launcher 被系统隔离至临时文件夹中。\n请将 HMCL 移动到其他文件夹后再尝试启动,否则你的设置和游戏数据可能会在重启后丢失。\n你可以访问 https://docs.hmcl.net/help.html 页面寻求帮助。\n是否继续启动?
389390
fatal.migration_requires_manual_reboot=Hello Minecraft! Launcher 即将完成升级,请重新打开 HMCL。\n如遇到问题,你可以访问 https://docs.hmcl.net/help.html 页面寻求帮助。
390391
fatal.apply_update_failure=我们很抱歉 Hello Minecraft! Launcher 无法自动完成升级,因为出现了一些问题。\n但你依可以从 %s 手动下载 HMCL 来完成升级。\n你可以访问 https://docs.hmcl.net/help.html 页面寻求帮助。

0 commit comments

Comments
 (0)