创建 RawPreservingProperty 接口 (#4464)

This commit is contained in:
Glavo
2025-09-14 21:06:32 +08:00
committed by GitHub
parent 19072bd617
commit 51954163d4
5 changed files with 141 additions and 26 deletions

View File

@@ -34,10 +34,7 @@ import org.hildan.fxgson.creators.ObservableSetCreator;
import org.hildan.fxgson.factories.JavaFxPropertyTypeAdapterFactory; import org.hildan.fxgson.factories.JavaFxPropertyTypeAdapterFactory;
import org.jackhuang.hmcl.Metadata; import org.jackhuang.hmcl.Metadata;
import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorServer; import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorServer;
import org.jackhuang.hmcl.util.gson.EnumOrdinalDeserializer; import org.jackhuang.hmcl.util.gson.*;
import org.jackhuang.hmcl.util.gson.FileTypeAdapter;
import org.jackhuang.hmcl.util.gson.ObservableField;
import org.jackhuang.hmcl.util.gson.PaintAdapter;
import org.jackhuang.hmcl.util.i18n.Locales; import org.jackhuang.hmcl.util.i18n.Locales;
import org.jackhuang.hmcl.util.i18n.Locales.SupportedLocale; import org.jackhuang.hmcl.util.i18n.Locales.SupportedLocale;
import org.jackhuang.hmcl.util.javafx.DirtyTracker; import org.jackhuang.hmcl.util.javafx.DirtyTracker;
@@ -260,7 +257,7 @@ public final class Config implements Observable {
} }
@SerializedName("commonDirType") @SerializedName("commonDirType")
private final ObjectProperty<EnumCommonDirectory> commonDirType = new SimpleObjectProperty<>(EnumCommonDirectory.DEFAULT); private final ObjectProperty<EnumCommonDirectory> commonDirType = new RawPreservingObjectProperty<>(EnumCommonDirectory.DEFAULT);
public ObjectProperty<EnumCommonDirectory> commonDirTypeProperty() { public ObjectProperty<EnumCommonDirectory> commonDirTypeProperty() {
return commonDirType; return commonDirType;
@@ -397,7 +394,7 @@ public final class Config implements Observable {
} }
@SerializedName("backgroundType") @SerializedName("backgroundType")
private final ObjectProperty<EnumBackgroundImage> backgroundImageType = new SimpleObjectProperty<>(EnumBackgroundImage.DEFAULT); private final ObjectProperty<EnumBackgroundImage> backgroundImageType = new RawPreservingObjectProperty<>(EnumBackgroundImage.DEFAULT);
public ObjectProperty<EnumBackgroundImage> backgroundImageTypeProperty() { public ObjectProperty<EnumBackgroundImage> backgroundImageTypeProperty() {
return backgroundImageType; return backgroundImageType;
@@ -769,9 +766,7 @@ public final class Config implements Observable {
for (var field : FIELDS) { for (var field : FIELDS) {
Observable observable = field.get(config); Observable observable = field.get(config);
if (config.tracker.isDirty(observable)) { if (config.tracker.isDirty(observable)) {
JsonElement serialized = field.serialize(config, context); field.serialize(result, config, context);
if (serialized != null && !serialized.isJsonNull())
result.add(field.getSerializedName(), serialized);
} }
} }
config.unknownFields.forEach(result::add); config.unknownFields.forEach(result::add);
@@ -791,6 +786,14 @@ public final class Config implements Observable {
var values = new LinkedHashMap<>(json.getAsJsonObject().asMap()); var values = new LinkedHashMap<>(json.getAsJsonObject().asMap());
for (ObservableField<Config> field : FIELDS) { for (ObservableField<Config> field : FIELDS) {
JsonElement value = values.remove(field.getSerializedName()); JsonElement value = values.remove(field.getSerializedName());
if (value == null) {
for (String alternateName : field.getAlternateNames()) {
value = values.remove(alternateName);
if (value != null)
break;
}
}
if (value != null) { if (value != null) {
config.tracker.markDirty(field.get(config)); config.tracker.markDirty(field.get(config));
field.deserialize(config, value, context); field.deserialize(config, value, context);

View File

@@ -17,10 +17,7 @@
*/ */
package org.jackhuang.hmcl.util.gson; package org.jackhuang.hmcl.util.gson;
import com.google.gson.JsonDeserializationContext; import com.google.gson.*;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import java.lang.reflect.Type; import java.lang.reflect.Type;
@@ -57,7 +54,14 @@ public final class EnumOrdinalDeserializer<T extends Enum<T>> implements JsonDes
@Override @Override
public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return mapping.get(json.getAsString()); if (json == null || json.isJsonNull())
return null;
String name = json.getAsString();
T value = mapping.get(name);
if (value == null)
throw new JsonParseException("No enum constant with name " + name);
return value;
} }
} }

View File

@@ -17,10 +17,7 @@
*/ */
package org.jackhuang.hmcl.util.gson; package org.jackhuang.hmcl.util.gson;
import com.google.gson.JsonDeserializationContext; import com.google.gson.*;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import javafx.beans.Observable; import javafx.beans.Observable;
import javafx.beans.property.ListProperty; import javafx.beans.property.ListProperty;
@@ -120,7 +117,7 @@ public abstract class ObservableField<T> {
return (Observable) varHandle.get(value); return (Observable) varHandle.get(value);
} }
public abstract JsonElement serialize(T value, JsonSerializationContext context); public abstract void serialize(JsonObject result, T value, JsonSerializationContext context);
public abstract void deserialize(T value, JsonElement element, JsonDeserializationContext context); public abstract void deserialize(T value, JsonElement element, JsonDeserializationContext context);
@@ -133,14 +130,36 @@ public abstract class ObservableField<T> {
} }
@Override @Override
public JsonElement serialize(T value, JsonSerializationContext context) { public void serialize(JsonObject result, T value, JsonSerializationContext context) {
return context.serialize(((Property<?>) get(value)).getValue(), elementType); Property<?> property = (Property<?>) get(value);
if (property instanceof RawPreservingProperty<?> rawPreserving) {
JsonElement rawJson = rawPreserving.getRawJson();
if (rawJson != null) {
result.add(getSerializedName(), rawJson);
return;
}
}
JsonElement serialized = context.serialize(property.getValue(), elementType);
if (serialized != null && !serialized.isJsonNull())
result.add(getSerializedName(), serialized);
} }
@Override @Override
@SuppressWarnings({"unchecked", "rawtypes"}) @SuppressWarnings({"unchecked", "rawtypes"})
public void deserialize(T value, JsonElement element, JsonDeserializationContext context) { public void deserialize(T value, JsonElement element, JsonDeserializationContext context) {
((Property) get(value)).setValue(context.deserialize(element, elementType)); Property property = (Property) get(value);
try {
property.setValue(context.deserialize(element, elementType));
} catch (Throwable e) {
if (property instanceof RawPreservingProperty<?>) {
((RawPreservingProperty<?>) property).setRawJson(element);
} else {
throw e;
}
}
} }
} }
@@ -158,8 +177,8 @@ public abstract class ObservableField<T> {
} }
@Override @Override
public JsonElement serialize(T value, JsonSerializationContext context) { public void serialize(JsonObject result, T value, JsonSerializationContext context) {
return context.serialize(get(value), collectionType); result.add(getSerializedName(), context.serialize(get(value), collectionType));
} }
@SuppressWarnings({"unchecked"}) @SuppressWarnings({"unchecked"})
@@ -193,8 +212,8 @@ public abstract class ObservableField<T> {
} }
@Override @Override
public JsonElement serialize(T value, JsonSerializationContext context) { public void serialize(JsonObject result, T value, JsonSerializationContext context) {
return context.serialize(get(value), mapType); result.add(getSerializedName(), context.serialize(get(value), mapType));
} }
@SuppressWarnings({"unchecked"}) @SuppressWarnings({"unchecked"})

View File

@@ -0,0 +1,57 @@
/*
* Hello Minecraft! Launcher
* Copyright (C) 2025 huangyuhui <huanghongxun2008@126.com> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.jackhuang.hmcl.util.gson;
import com.google.gson.JsonElement;
import javafx.beans.property.SimpleObjectProperty;
import org.jetbrains.annotations.Nullable;
/// @author Glavo
public class RawPreservingObjectProperty<T> extends SimpleObjectProperty<T> implements RawPreservingProperty<T> {
private JsonElement rawJson;
public RawPreservingObjectProperty() {
}
public RawPreservingObjectProperty(T initialValue) {
super(initialValue);
}
public RawPreservingObjectProperty(Object bean, String name) {
super(bean, name);
}
public RawPreservingObjectProperty(Object bean, String name, T initialValue) {
super(bean, name, initialValue);
}
@Override
public void setRawJson(JsonElement value) {
this.rawJson = value;
}
@Override
public @Nullable JsonElement getRawJson() {
return rawJson;
}
@Override
protected void invalidated() {
this.rawJson = null;
}
}

View File

@@ -0,0 +1,32 @@
/*
* Hello Minecraft! Launcher
* Copyright (C) 2025 huangyuhui <huanghongxun2008@126.com> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.jackhuang.hmcl.util.gson;
import com.google.gson.JsonElement;
import javafx.beans.property.Property;
import org.jetbrains.annotations.Nullable;
/// If a Property implementing this interface fails to deserialize a json object into a value, it will store the original JsonElement internally.
/// If the value does not change at runtime, the original JsonElement will be written back during serialization.
///
/// @author Glavo
public interface RawPreservingProperty<T> extends Property<T> {
void setRawJson(JsonElement value);
@Nullable JsonElement getRawJson();
}