Skip to content

Commit 5524783

Browse files
committed
refactor: rename commonpath to commonDirectory and implement migration logic
1 parent 9bd107d commit 5524783

3 files changed

Lines changed: 36 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public ObjectProperty<EnumCommonDirectory> commonDirTypeProperty() {
159159
}
160160

161161
/// The custom common Minecraft directory path.
162-
@SerializedName("commonpath")
162+
@SerializedName("commonDirectory")
163163
private final StringProperty commonDirectory = new SimpleStringProperty();
164164

165165
/// Returns the custom common Minecraft directory property.

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public static GUID getLegacyProfileId(String profileName) {
108108
AccountStorages::new);
109109
JsonElement legacyAllowAutoAgent = jsonObject.remove("allowAutoAgent");
110110
JsonElement legacyDisableAutoGameOptions = jsonObject.remove("disableAutoGameOptions");
111+
migrateLegacyCommonDirectory(jsonObject);
111112
migrateLegacyLanguage(jsonObject);
112113
migrateLegacySelectedVersions(jsonObject);
113114
@Nullable GameDirectories migratedGameDirectories = extractGameDirectoriesFromConfigJson(jsonObject);
@@ -220,6 +221,20 @@ static void migrateLegacyLanguage(JsonObject json) {
220221
});
221222
}
222223

224+
/// Migrates the legacy `commonpath` field into the current `commonDirectory` field.
225+
static void migrateLegacyCommonDirectory(JsonObject json) {
226+
Objects.requireNonNull(json);
227+
228+
JsonElement legacyCommonDirectory = json.remove("commonpath");
229+
if (json.has("commonDirectory")
230+
|| !(legacyCommonDirectory instanceof JsonPrimitive primitive)
231+
|| !primitive.isString()) {
232+
return;
233+
}
234+
235+
json.addProperty("commonDirectory", primitive.getAsString());
236+
}
237+
223238
/// Migrates the legacy workspace-wide automatic Java agent permission into game setting presets.
224239
static void migrateLegacyAllowAutoAgent(
225240
LauncherSettings launcherSettings,

HMCL/src/test/java/org/jackhuang/hmcl/setting/LauncherSettingsMigrationTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,26 @@ public void migratesLegacyTraditionalChineseLanguage() {
6767
assertEquals("zh-Hant", launcherSettings.languageProperty().get().getName());
6868
}
6969

70+
/// Tests migrating the legacy common directory field into the current launcher settings field.
71+
@Test
72+
public void migratesLegacyCommonPathToCommonDirectory() {
73+
JsonObject settings = JsonParser.parseString("""
74+
{
75+
"commonpath": "/home/user/.minecraft"
76+
}
77+
""").getAsJsonObject();
78+
79+
LegacyConfigMigrator.migrateLegacyCommonDirectory(settings);
80+
LauncherSettings launcherSettings = Objects.requireNonNull(LauncherSettings.fromJson(settings));
81+
JsonObject serialized = JsonParser.parseString(launcherSettings.toJson()).getAsJsonObject();
82+
83+
assertFalse(settings.has("commonpath"));
84+
assertEquals("/home/user/.minecraft", settings.get("commonDirectory").getAsString());
85+
assertEquals("/home/user/.minecraft", launcherSettings.commonDirectoryProperty().get());
86+
assertFalse(serialized.has("commonpath"));
87+
assertEquals("/home/user/.minecraft", serialized.get("commonDirectory").getAsString());
88+
}
89+
7090
/// Tests that launcher settings serialization preserves a patch-version schema and unknown fields.
7191
@Test
7292
public void preservesPatchSchemaAndUnknownFields() {

0 commit comments

Comments
 (0)