Skip to content

Commit 06b9148

Browse files
committed
Refactor legacy config loading to use JsonUtils and improve logging messages
1 parent f6a4a10 commit 06b9148

1 file changed

Lines changed: 4 additions & 5 deletions

File tree

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.gson.*;
2121
import org.jackhuang.hmcl.Metadata;
2222
import org.jackhuang.hmcl.util.StringUtils;
23+
import org.jackhuang.hmcl.util.gson.JsonUtils;
2324
import org.jackhuang.hmcl.util.io.JarUtils;
2425
import org.jetbrains.annotations.Contract;
2526
import org.jetbrains.annotations.NotNullByDefault;
@@ -57,7 +58,7 @@ private LegacyConfigMigrator() {
5758

5859
/// Loads a legacy config file and applies legacy schema upgrades in memory.
5960
private static @Nullable LoadedConfig loadLegacyConfig(Path path) throws IOException, JsonParseException {
60-
JsonObject jsonObject = readJsonObject(path);
61+
JsonObject jsonObject = JsonUtils.fromJsonFile(path, JsonObject.class);
6162
if (jsonObject == null) {
6263
return null;
6364
}
@@ -71,7 +72,6 @@ private LegacyConfigMigrator() {
7172
: 0;
7273

7374
if (configVersion > LEGACY_CURRENT_CONFIG_VERSION) {
74-
LOG.warning(String.format("Current HMCL only support the legacy configuration version up to %d. However, the version now is %d.", LEGACY_CURRENT_CONFIG_VERSION, configVersion));
7575
Config deserialized = Config.fromJson(jsonObject);
7676
if (deserialized == null) {
7777
return null;
@@ -103,13 +103,13 @@ private LegacyConfigMigrator() {
103103
try {
104104
@Nullable LoadedConfig loadedConfig = loadLegacyConfig(path);
105105
if (loadedConfig == null) {
106-
LOG.info("Legacy config is empty: " + path);
106+
LOG.info("Legacy config is empty");
107107
return null;
108108
}
109109

110110
return new MigrationResult(path, loadedConfig);
111111
} catch (JsonParseException e) {
112-
LOG.warning("Malformed legacy config: " + path, e);
112+
LOG.warning("Malformed legacy config file: " + path, e);
113113
return null;
114114
}
115115
}
@@ -296,7 +296,6 @@ record LoadedConfig(Config config, String contentForMigration, boolean unsupport
296296

297297
/// Result of locating and loading a legacy config file without modifying it.
298298
///
299-
/// @param path The legacy config path.
300299
/// @param loadedConfig The loaded config data.
301300
record MigrationResult(Path path, LoadedConfig loadedConfig) {
302301
}

0 commit comments

Comments
 (0)