简化 GlobalConfig (#4781)
This commit is contained in:
@@ -19,131 +19,109 @@ package org.jackhuang.hmcl.setting;
|
|||||||
|
|
||||||
import com.google.gson.*;
|
import com.google.gson.*;
|
||||||
import com.google.gson.annotations.JsonAdapter;
|
import com.google.gson.annotations.JsonAdapter;
|
||||||
import javafx.beans.InvalidationListener;
|
import com.google.gson.annotations.SerializedName;
|
||||||
import javafx.beans.Observable;
|
|
||||||
import javafx.beans.property.*;
|
import javafx.beans.property.*;
|
||||||
import javafx.collections.FXCollections;
|
import javafx.collections.FXCollections;
|
||||||
import javafx.collections.ObservableSet;
|
import javafx.collections.ObservableSet;
|
||||||
import org.jackhuang.hmcl.util.javafx.ObservableHelper;
|
import org.jackhuang.hmcl.util.gson.ObservableSetting;
|
||||||
import org.jackhuang.hmcl.util.javafx.PropertyUtils;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@JsonAdapter(GlobalConfig.Serializer.class)
|
@JsonAdapter(GlobalConfig.Adapter.class)
|
||||||
public final class GlobalConfig implements Observable {
|
public final class GlobalConfig extends ObservableSetting {
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public static GlobalConfig fromJson(String json) throws JsonParseException {
|
public static GlobalConfig fromJson(String json) throws JsonParseException {
|
||||||
GlobalConfig loaded = Config.CONFIG_GSON.fromJson(json, GlobalConfig.class);
|
return Config.CONFIG_GSON.fromJson(json, GlobalConfig.class);
|
||||||
if (loaded == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
GlobalConfig instance = new GlobalConfig();
|
|
||||||
PropertyUtils.copyProperties(loaded, instance);
|
|
||||||
instance.unknownFields.putAll(loaded.unknownFields);
|
|
||||||
return instance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private final IntegerProperty agreementVersion = new SimpleIntegerProperty();
|
|
||||||
|
|
||||||
private final IntegerProperty terracottaAgreementVersion = new SimpleIntegerProperty();
|
|
||||||
|
|
||||||
private final IntegerProperty platformPromptVersion = new SimpleIntegerProperty();
|
|
||||||
|
|
||||||
private final IntegerProperty logRetention = new SimpleIntegerProperty();
|
|
||||||
|
|
||||||
private final BooleanProperty enableOfflineAccount = new SimpleBooleanProperty(false);
|
|
||||||
|
|
||||||
private final StringProperty fontAntiAliasing = new SimpleStringProperty();
|
|
||||||
|
|
||||||
private final ObservableSet<String> userJava = FXCollections.observableSet(new LinkedHashSet<>());
|
|
||||||
|
|
||||||
private final ObservableSet<String> disabledJava = FXCollections.observableSet(new LinkedHashSet<>());
|
|
||||||
|
|
||||||
private final Map<String, Object> unknownFields = new HashMap<>();
|
|
||||||
|
|
||||||
private final transient ObservableHelper helper = new ObservableHelper(this);
|
|
||||||
|
|
||||||
public GlobalConfig() {
|
public GlobalConfig() {
|
||||||
PropertyUtils.attachListener(this, helper);
|
register();
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addListener(InvalidationListener listener) {
|
|
||||||
helper.addListener(listener);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void removeListener(InvalidationListener listener) {
|
|
||||||
helper.removeListener(listener);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toJson() {
|
public String toJson() {
|
||||||
return Config.CONFIG_GSON.toJson(this);
|
return Config.CONFIG_GSON.toJson(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getAgreementVersion() {
|
@SerializedName("agreementVersion")
|
||||||
return agreementVersion.get();
|
private final IntegerProperty agreementVersion = new SimpleIntegerProperty();
|
||||||
}
|
|
||||||
|
|
||||||
public IntegerProperty agreementVersionProperty() {
|
public IntegerProperty agreementVersionProperty() {
|
||||||
return agreementVersion;
|
return agreementVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getAgreementVersion() {
|
||||||
|
return agreementVersion.get();
|
||||||
|
}
|
||||||
|
|
||||||
public void setAgreementVersion(int agreementVersion) {
|
public void setAgreementVersion(int agreementVersion) {
|
||||||
this.agreementVersion.set(agreementVersion);
|
this.agreementVersion.set(agreementVersion);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getTerracottaAgreementVersion() {
|
@SerializedName("terracottaAgreementVersion")
|
||||||
return terracottaAgreementVersion.get();
|
private final IntegerProperty terracottaAgreementVersion = new SimpleIntegerProperty();
|
||||||
}
|
|
||||||
|
|
||||||
public IntegerProperty terracottaAgreementVersionProperty() {
|
public IntegerProperty terracottaAgreementVersionProperty() {
|
||||||
return terracottaAgreementVersion;
|
return terracottaAgreementVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getTerracottaAgreementVersion() {
|
||||||
|
return terracottaAgreementVersion.get();
|
||||||
|
}
|
||||||
|
|
||||||
public void setTerracottaAgreementVersion(int terracottaAgreementVersion) {
|
public void setTerracottaAgreementVersion(int terracottaAgreementVersion) {
|
||||||
this.terracottaAgreementVersion.set(terracottaAgreementVersion);
|
this.terracottaAgreementVersion.set(terracottaAgreementVersion);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getPlatformPromptVersion() {
|
@SerializedName("platformPromptVersion")
|
||||||
return platformPromptVersion.get();
|
private final IntegerProperty platformPromptVersion = new SimpleIntegerProperty();
|
||||||
}
|
|
||||||
|
|
||||||
public IntegerProperty platformPromptVersionProperty() {
|
public IntegerProperty platformPromptVersionProperty() {
|
||||||
return platformPromptVersion;
|
return platformPromptVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getPlatformPromptVersion() {
|
||||||
|
return platformPromptVersion.get();
|
||||||
|
}
|
||||||
|
|
||||||
public void setPlatformPromptVersion(int platformPromptVersion) {
|
public void setPlatformPromptVersion(int platformPromptVersion) {
|
||||||
this.platformPromptVersion.set(platformPromptVersion);
|
this.platformPromptVersion.set(platformPromptVersion);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getLogRetention() {
|
@SerializedName("logRetention")
|
||||||
return logRetention.get();
|
private final IntegerProperty logRetention = new SimpleIntegerProperty(20);
|
||||||
}
|
|
||||||
|
|
||||||
public IntegerProperty logRetentionProperty() {
|
public IntegerProperty logRetentionProperty() {
|
||||||
return logRetention;
|
return logRetention;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getLogRetention() {
|
||||||
|
return logRetention.get();
|
||||||
|
}
|
||||||
|
|
||||||
public void setLogRetention(int logRetention) {
|
public void setLogRetention(int logRetention) {
|
||||||
this.logRetention.set(logRetention);
|
this.logRetention.set(logRetention);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isEnableOfflineAccount() {
|
@SerializedName("enableOfflineAccount")
|
||||||
return enableOfflineAccount.get();
|
private final BooleanProperty enableOfflineAccount = new SimpleBooleanProperty(false);
|
||||||
}
|
|
||||||
|
|
||||||
public BooleanProperty enableOfflineAccountProperty() {
|
public BooleanProperty enableOfflineAccountProperty() {
|
||||||
return enableOfflineAccount;
|
return enableOfflineAccount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isEnableOfflineAccount() {
|
||||||
|
return enableOfflineAccount.get();
|
||||||
|
}
|
||||||
|
|
||||||
public void setEnableOfflineAccount(boolean value) {
|
public void setEnableOfflineAccount(boolean value) {
|
||||||
enableOfflineAccount.set(value);
|
enableOfflineAccount.set(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SerializedName("fontAntiAliasing")
|
||||||
|
private final StringProperty fontAntiAliasing = new SimpleStringProperty();
|
||||||
|
|
||||||
public StringProperty fontAntiAliasingProperty() {
|
public StringProperty fontAntiAliasingProperty() {
|
||||||
return fontAntiAliasing;
|
return fontAntiAliasing;
|
||||||
}
|
}
|
||||||
@@ -156,89 +134,24 @@ public final class GlobalConfig implements Observable {
|
|||||||
this.fontAntiAliasing.set(value);
|
this.fontAntiAliasing.set(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SerializedName("userJava")
|
||||||
|
private final ObservableSet<String> userJava = FXCollections.observableSet(new LinkedHashSet<>());
|
||||||
|
|
||||||
public ObservableSet<String> getUserJava() {
|
public ObservableSet<String> getUserJava() {
|
||||||
return userJava;
|
return userJava;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SerializedName("disabledJava")
|
||||||
|
private final ObservableSet<String> disabledJava = FXCollections.observableSet(new LinkedHashSet<>());
|
||||||
|
|
||||||
public ObservableSet<String> getDisabledJava() {
|
public ObservableSet<String> getDisabledJava() {
|
||||||
return disabledJava;
|
return disabledJava;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final class Serializer implements JsonSerializer<GlobalConfig>, JsonDeserializer<GlobalConfig> {
|
static final class Adapter extends ObservableSetting.Adapter<GlobalConfig> {
|
||||||
private static final Set<String> knownFields = new HashSet<>(Arrays.asList(
|
|
||||||
"agreementVersion",
|
|
||||||
"terracottaAgreementVersion",
|
|
||||||
"platformPromptVersion",
|
|
||||||
"logRetention",
|
|
||||||
"userJava",
|
|
||||||
"disabledJava",
|
|
||||||
"enableOfflineAccount",
|
|
||||||
"fontAntiAliasing"
|
|
||||||
));
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JsonElement serialize(GlobalConfig src, Type typeOfSrc, JsonSerializationContext context) {
|
protected GlobalConfig createInstance() {
|
||||||
if (src == null) {
|
return new GlobalConfig();
|
||||||
return JsonNull.INSTANCE;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonObject jsonObject = new JsonObject();
|
|
||||||
jsonObject.add("agreementVersion", context.serialize(src.getAgreementVersion()));
|
|
||||||
jsonObject.add("terracottaAgreementVersion", context.serialize(src.getTerracottaAgreementVersion()));
|
|
||||||
jsonObject.add("platformPromptVersion", context.serialize(src.getPlatformPromptVersion()));
|
|
||||||
jsonObject.add("logRetention", context.serialize(src.getLogRetention()));
|
|
||||||
jsonObject.add("fontAntiAliasing", context.serialize(src.getFontAntiAliasing()));
|
|
||||||
if (src.enableOfflineAccount.get())
|
|
||||||
jsonObject.addProperty("enableOfflineAccount", true);
|
|
||||||
|
|
||||||
if (!src.getUserJava().isEmpty())
|
|
||||||
jsonObject.add("userJava", context.serialize(src.getUserJava()));
|
|
||||||
|
|
||||||
if (!src.getDisabledJava().isEmpty())
|
|
||||||
jsonObject.add("disabledJava", context.serialize(src.getDisabledJava()));
|
|
||||||
|
|
||||||
for (Map.Entry<String, Object> entry : src.unknownFields.entrySet()) {
|
|
||||||
jsonObject.add(entry.getKey(), context.serialize(entry.getValue()));
|
|
||||||
}
|
|
||||||
|
|
||||||
return jsonObject;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public GlobalConfig deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
|
||||||
if (!(json instanceof JsonObject)) return null;
|
|
||||||
|
|
||||||
JsonObject obj = (JsonObject) json;
|
|
||||||
|
|
||||||
GlobalConfig config = new GlobalConfig();
|
|
||||||
config.setAgreementVersion(Optional.ofNullable(obj.get("agreementVersion")).map(JsonElement::getAsInt).orElse(0));
|
|
||||||
config.setTerracottaAgreementVersion(Optional.ofNullable(obj.get("terracottaAgreementVersion")).map(JsonElement::getAsInt).orElse(0));
|
|
||||||
config.setPlatformPromptVersion(Optional.ofNullable(obj.get("platformPromptVersion")).map(JsonElement::getAsInt).orElse(0));
|
|
||||||
config.setLogRetention(Optional.ofNullable(obj.get("logRetention")).map(JsonElement::getAsInt).orElse(20));
|
|
||||||
config.setEnableOfflineAccount(Optional.ofNullable(obj.get("enableOfflineAccount")).map(JsonElement::getAsBoolean).orElse(false));
|
|
||||||
config.setFontAntiAliasing(Optional.ofNullable(obj.get("fontAntiAliasing")).map(JsonElement::getAsString).orElse(null));
|
|
||||||
|
|
||||||
JsonElement userJava = obj.get("userJava");
|
|
||||||
if (userJava != null && userJava.isJsonArray()) {
|
|
||||||
for (JsonElement element : userJava.getAsJsonArray()) {
|
|
||||||
config.userJava.add(element.getAsString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonElement disabledJava = obj.get("disabledJava");
|
|
||||||
if (disabledJava != null && disabledJava.isJsonArray()) {
|
|
||||||
for (JsonElement element : disabledJava.getAsJsonArray()) {
|
|
||||||
config.disabledJava.add(element.getAsString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
|
|
||||||
if (!knownFields.contains(entry.getKey())) {
|
|
||||||
config.unknownFields.put(entry.getKey(), context.deserialize(entry.getValue(), Object.class));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return config;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user