|
25 | 25 |
|
26 | 26 | package org.geysermc.geyser.session.auth; |
27 | 27 |
|
| 28 | +import com.google.gson.Gson; |
28 | 29 | import com.google.gson.JsonDeserializationContext; |
29 | 30 | import com.google.gson.JsonDeserializer; |
30 | 31 | import com.google.gson.JsonElement; |
31 | 32 | import com.google.gson.JsonParseException; |
| 33 | +import com.google.gson.TypeAdapter; |
| 34 | +import com.google.gson.TypeAdapterFactory; |
32 | 35 | import com.google.gson.annotations.JsonAdapter; |
33 | 36 | import com.google.gson.annotations.SerializedName; |
| 37 | +import com.google.gson.reflect.TypeToken; |
| 38 | +import com.google.gson.stream.JsonReader; |
| 39 | +import com.google.gson.stream.JsonWriter; |
34 | 40 | import lombok.Getter; |
35 | 41 | import lombok.Setter; |
36 | 42 | import org.geysermc.floodgate.util.DeviceOs; |
37 | 43 | import org.geysermc.floodgate.util.InputMode; |
38 | 44 | import org.geysermc.floodgate.util.UiProfile; |
39 | 45 |
|
| 46 | +import java.io.IOException; |
40 | 47 | import java.lang.reflect.Type; |
41 | 48 | import java.nio.charset.StandardCharsets; |
42 | 49 | import java.util.UUID; |
@@ -85,6 +92,7 @@ public final class BedrockClientData { |
85 | 92 | @SerializedName(value = "DeviceModel") |
86 | 93 | private String deviceModel; |
87 | 94 | @SerializedName(value = "DeviceOS") |
| 95 | + @JsonAdapter(value = IntToEnumTypeFactory.class) |
88 | 96 | private DeviceOs deviceOs; |
89 | 97 | @SerializedName(value = "UIProfile") |
90 | 98 | private UiProfile uiProfile; |
@@ -139,4 +147,42 @@ public byte[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationCon |
139 | 147 | return json.getAsString().getBytes(StandardCharsets.UTF_8); |
140 | 148 | } |
141 | 149 | } |
| 150 | + |
| 151 | + /** |
| 152 | + * Creates a JSON parser that reads the incoming enum ordinal and converts it to the enum constant. |
| 153 | + */ |
| 154 | + private static final class IntToEnumTypeFactory implements TypeAdapterFactory { |
| 155 | + @Override |
| 156 | + public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { |
| 157 | + //noinspection unchecked |
| 158 | + Class<T> rawType = (Class<T>) type.getRawType(); |
| 159 | + if (!rawType.isEnum()) { |
| 160 | + return null; |
| 161 | + } |
| 162 | + |
| 163 | + T[] constants = rawType.getEnumConstants(); |
| 164 | + return new TypeAdapter<>() { |
| 165 | + @Override |
| 166 | + public void write(JsonWriter out, T value) { |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public T read(JsonReader in) throws IOException { |
| 171 | + int ordinal = in.nextInt(); |
| 172 | + if (ordinal < 0 || ordinal >= constants.length) { |
| 173 | + return null; |
| 174 | + } |
| 175 | + return constants[ordinal]; |
| 176 | + } |
| 177 | + }; |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | +// private static final class IntToEnumDeserializer implements JsonDeserializer<Enum<?>> { |
| 182 | +// @Override |
| 183 | +// public Enum<?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { |
| 184 | +// System.out.println(typeOfT.getClass().getTypeParameters()[0]); |
| 185 | +// return DeviceOs.fromId(json.getAsInt()); |
| 186 | +// } |
| 187 | +// } |
142 | 188 | } |
0 commit comments