Skip to content

Commit

Permalink
Build configuration DFU system.
Browse files Browse the repository at this point in the history
  • Loading branch information
burningtnt committed Dec 6, 2023
1 parent 55c55e9 commit e41e969
Showing 1 changed file with 82 additions and 65 deletions.
147 changes: 82 additions & 65 deletions HMCL/src/main/java/org/jackhuang/hmcl/setting/ConfigUpgrader.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,96 +17,113 @@
*/
package org.jackhuang.hmcl.setting;

import org.jackhuang.hmcl.util.Pair;
import org.jackhuang.hmcl.util.StringUtils;
import java.util.HashMap;
import java.util.Map;

import java.util.*;
import java.util.function.BiFunction;
import java.util.logging.Level;

import static org.jackhuang.hmcl.util.Lang.tryCast;
import static org.jackhuang.hmcl.util.Logging.LOG;

final class ConfigUpgrader {
private static final int VERSION = 0;

private ConfigUpgrader() {
}

private static final int CURRENT_VERSION = 1;

/**
* This method is for the compatibility with old HMCL 3.x as well as HMCL 2.x.
* This method is for the compatibility with old HMCL versions.
*
* @param deserialized deserialized config settings
* @param rawJson raw json structure of the config settings without modification
* @return true if config version is upgraded
*/
static boolean upgradeConfig(Config deserialized, Map<?, ?> rawJson) {
boolean upgraded;
if (deserialized.getConfigVersion() < VERSION) {
deserialized.setConfigVersion(VERSION);
// TODO: Add upgrade code here.
upgraded = true;
} else {
upgraded = false;
static void upgradeConfig(Config deserialized, Map<?, ?> rawJson) {
if (deserialized.getConfigVersion() == CURRENT_VERSION) {
return;
}

upgradeV2(deserialized, rawJson);
upgradeV3(deserialized, rawJson);
int configVersion = deserialized.getConfigVersion();
if (configVersion > CURRENT_VERSION) {
LOG.log(Level.WARNING, String.format("Current HMCL only support the configuration version up to %d. However, the version now is %d.", CURRENT_VERSION, configVersion));
return;
}

LOG.log(Level.INFO, String.format("Updating configuration from %d to %d.", configVersion, CURRENT_VERSION));
Map<?, ?> unmodifiableRawJson = Collections.unmodifiableMap(rawJson);
for (Map.Entry<Integer, BiFunction<Config, Map<?, ?>, Integer>> dfu : collectDFU()) {
if (configVersion < dfu.getKey()) {
configVersion = dfu.getValue().apply(deserialized, unmodifiableRawJson);
}
}

return upgraded;
deserialized.setConfigVersion(CURRENT_VERSION);
}

/**
* Upgrade configuration of HMCL 2.x
* <p>Initialize the dfu of HMCL. Feel free to use lambda as all the lambda here would not be initialized unless HMCL needs to update the configuration.
* For each item in this list, it should be a Map.Entry.</p>
*
* @param deserialized deserialized config settings
* @param rawJson raw json structure of the config settings without modification
* <p>The key should be a version number. All the configuration with a version number which is less than the specific one will be applied to this upgrader.</p>
* <p>The value should be the upgrader. The configuration which is waited to being processed, and the raw unmodifiable value of the json from the configuration file.</p>
* <p>The return value should a target version number of this item.</p>
*
* <p>The last item must return CURRENT_VERSION, as the config file should always being updated to the latest version.</p>
*/
private static void upgradeV2(Config deserialized, Map<?, ?> rawJson) {
// Convert OfflineAccounts whose stored uuid is important.
tryCast(rawJson.get("auth"), Map.class).ifPresent(auth -> {
tryCast(auth.get("offline"), Map.class).ifPresent(offline -> {
String selected = rawJson.containsKey("selectedAccount") ? null
: tryCast(offline.get("IAuthenticator_UserName"), String.class).orElse(null);

tryCast(offline.get("uuidMap"), Map.class).ifPresent(uuidMap -> {
((Map<?, ?>) uuidMap).forEach((key, value) -> {
Map<Object, Object> storage = new HashMap<>();
storage.put("type", "offline");
storage.put("username", key);
storage.put("uuid", value);
if (key.equals(selected)) {
storage.put("selected", true);
}
deserialized.getAccountStorages().add(storage);
private static List<Map.Entry<Integer, BiFunction<Config, Map<?, ?>, Integer>>> collectDFU() {
List<Map.Entry<Integer, BiFunction<Config, Map<?, ?>, Integer>>> dfu = new ArrayList<>();

dfu.add(Pair.pair(0, (deserialized, rawJson) -> {
// Upgrade configuration of HMCL 2.x: Convert OfflineAccounts whose stored uuid is important.
tryCast(rawJson.get("auth"), Map.class).ifPresent(auth -> {
tryCast(auth.get("offline"), Map.class).ifPresent(offline -> {
String selected = rawJson.containsKey("selectedAccount") ? null
: tryCast(offline.get("IAuthenticator_UserName"), String.class).orElse(null);

tryCast(offline.get("uuidMap"), Map.class).ifPresent(uuidMap -> {
((Map<?, ?>) uuidMap).forEach((key, value) -> {
Map<Object, Object> storage = new HashMap<>();
storage.put("type", "offline");
storage.put("username", key);
storage.put("uuid", value);
if (key.equals(selected)) {
storage.put("selected", true);
}
deserialized.getAccountStorages().add(storage);
});
});
});
});
});
}

/**
* Upgrade configuration of HMCL earlier than 3.1.70
*
* @param deserialized deserialized config settings
* @param rawJson raw json structure of the config settings without modification
*/
private static void upgradeV3(Config deserialized, Map<?, ?> rawJson) {
if (!rawJson.containsKey("commonDirType"))
deserialized.setCommonDirType(deserialized.getCommonDirectory().equals(Settings.getDefaultCommonDirectory()) ? EnumCommonDirectory.DEFAULT : EnumCommonDirectory.CUSTOM);
if (!rawJson.containsKey("backgroundType"))
deserialized.setBackgroundImageType(StringUtils.isNotBlank(deserialized.getBackgroundImage()) ? EnumBackgroundImage.CUSTOM : EnumBackgroundImage.DEFAULT);
if (!rawJson.containsKey("hasProxy"))
deserialized.setHasProxy(StringUtils.isNotBlank(deserialized.getProxyHost()));
if (!rawJson.containsKey("hasProxyAuth"))
deserialized.setHasProxyAuth(StringUtils.isNotBlank(deserialized.getProxyUser()));

if (!rawJson.containsKey("downloadType")) {
tryCast(rawJson.get("downloadtype"), Number.class)
.map(Number::intValue)
.ifPresent(id -> {
if (id == 0) {
deserialized.setDownloadType("mojang");
} else if (id == 1) {
deserialized.setDownloadType("bmclapi");
}
});
// Upgrade configuration of HMCL earlier than 3.1.70
if (!rawJson.containsKey("commonDirType"))
deserialized.setCommonDirType(deserialized.getCommonDirectory().equals(Settings.getDefaultCommonDirectory()) ? EnumCommonDirectory.DEFAULT : EnumCommonDirectory.CUSTOM);
if (!rawJson.containsKey("backgroundType"))
deserialized.setBackgroundImageType(StringUtils.isNotBlank(deserialized.getBackgroundImage()) ? EnumBackgroundImage.CUSTOM : EnumBackgroundImage.DEFAULT);
if (!rawJson.containsKey("hasProxy"))
deserialized.setHasProxy(StringUtils.isNotBlank(deserialized.getProxyHost()));
if (!rawJson.containsKey("hasProxyAuth"))
deserialized.setHasProxyAuth(StringUtils.isNotBlank(deserialized.getProxyUser()));

if (!rawJson.containsKey("downloadType")) {
tryCast(rawJson.get("downloadtype"), Number.class)
.map(Number::intValue)
.ifPresent(id -> {
if (id == 0) {
deserialized.setDownloadType("mojang");
} else if (id == 1) {
deserialized.setDownloadType("bmclapi");
}
});
}
return 1;
}));

if (dfu.get(dfu.size() - 1).getKey() != CURRENT_VERSION) {
throw new IllegalStateException("The last dfu have to adapt all the config version below CURRENT_VERSION");
}

return dfu;
}
}

0 comments on commit e41e969

Please sign in to comment.