Improve config compatibility with HMCL 2.x. Fix #411

This commit is contained in:
huanghongxun
2018-08-07 18:40:23 +08:00
parent 61fd581ef5
commit 93e5851167
4 changed files with 33 additions and 12 deletions

View File

@@ -29,6 +29,7 @@ import java.util.logging.Level;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonParseException; import com.google.gson.JsonParseException;
import org.jackhuang.hmcl.util.StringUtils;
public final class ConfigHolder { public final class ConfigHolder {
@@ -78,7 +79,8 @@ public final class ConfigHolder {
LOG.info("Config is empty"); LOG.info("Config is empty");
} else { } else {
Map<?, ?> raw = new Gson().fromJson(content, Map.class); Map<?, ?> raw = new Gson().fromJson(content, Map.class);
return upgradeConfig(deserialized, raw); upgradeConfig(deserialized, raw);
return deserialized;
} }
} catch (IOException | JsonParseException e) { } catch (IOException | JsonParseException e) {
LOG.log(Level.WARNING, "Something went wrong when loading config.", e); LOG.log(Level.WARNING, "Something went wrong when loading config.", e);
@@ -99,9 +101,21 @@ public final class ConfigHolder {
} }
} }
private static Config upgradeConfig(Config deserialized, Map<?, ?> rawJson) { /**
* This method is for the compatibility with old HMCL 3.x as well as HMCL 2.x.
* @param deserialized deserialized config settings
* @param rawJson raw json structure of the config settings without modification
* @return {@code deserialized}
*/
private static void upgradeConfig(Config deserialized, Map<?, ?> rawJson) {
// Following is for the compatibility with HMCL 2.x
if (!rawJson.containsKey("commonDirType")) if (!rawJson.containsKey("commonDirType"))
deserialized.setCommonDirType(deserialized.getCommonDirectory().equals(Settings.getDefaultCommonDirectory()) ? EnumCommonDirectory.DEFAULT : EnumCommonDirectory.CUSTOM); deserialized.setCommonDirType(deserialized.getCommonDirectory().equals(Settings.getDefaultCommonDirectory()) ? EnumCommonDirectory.DEFAULT : EnumCommonDirectory.CUSTOM);
return deserialized; 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()));
} }
} }

View File

@@ -19,12 +19,5 @@ package org.jackhuang.hmcl.setting;
public enum EnumBackgroundImage { public enum EnumBackgroundImage {
DEFAULT, DEFAULT,
CUSTOM; CUSTOM
public static EnumBackgroundImage indexOf(int index) {
if (index >= values().length || index < 0)
return DEFAULT;
else
return values()[index];
}
} }

View File

@@ -104,6 +104,18 @@ public class Theme {
public static Optional<Theme> getTheme(String name) { public static Optional<Theme> getTheme(String name) {
if (name == null) if (name == null)
return Optional.empty(); return Optional.empty();
else if (name.equalsIgnoreCase("blue"))
return Optional.of(custom("#5C6BC0"));
else if (name.equalsIgnoreCase("darker_blue"))
return Optional.of(custom("#283593"));
else if (name.equalsIgnoreCase("green"))
return Optional.of(custom("#43A047"));
else if (name.equalsIgnoreCase("orange"))
return Optional.of(custom("#E67E22"));
else if (name.equalsIgnoreCase("purple"))
return Optional.of(custom("#9C27B0"));
else if (name.equalsIgnoreCase("red"))
return Optional.of(custom("#F44336"));
if (name.startsWith("#")) if (name.startsWith("#"))
try { try {

View File

@@ -601,7 +601,9 @@ public final class VersionSetting {
} }
private int parseJsonPrimitive(JsonPrimitive primitive, int defaultValue) { private int parseJsonPrimitive(JsonPrimitive primitive, int defaultValue) {
if (primitive.isNumber()) if (primitive == null)
return defaultValue;
else if (primitive.isNumber())
return primitive.getAsInt(); return primitive.getAsInt();
else else
return Lang.parseInt(primitive.getAsString(), defaultValue); return Lang.parseInt(primitive.getAsString(), defaultValue);