Commit da398108 authored by AnimeGitB's avatar AnimeGitB
Browse files

Add Position JsonAdapter for [x,y,z] format

Also add serializers for existing JsonAdapters
parent ad502a85
...@@ -10,9 +10,11 @@ import com.google.gson.TypeAdapter; ...@@ -10,9 +10,11 @@ import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory; import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter; import com.google.gson.stream.JsonWriter;
import emu.grasscutter.data.common.DynamicFloat; import emu.grasscutter.data.common.DynamicFloat;
import it.unimi.dsi.fastutil.floats.FloatArrayList;
import it.unimi.dsi.fastutil.ints.IntArrayList; import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList; import it.unimi.dsi.fastutil.ints.IntList;
import lombok.val; import lombok.val;
...@@ -65,7 +67,54 @@ public class JsonAdapters { ...@@ -65,7 +67,54 @@ public class JsonAdapters {
} }
@Override @Override
public void write(JsonWriter writer, IntList i) {}; public void write(JsonWriter writer, IntList l) throws IOException {
writer.beginArray();
for (val i : l) // .forEach() doesn't appreciate exceptions
writer.value(i);
writer.endArray();
};
}
static class PositionAdapter extends TypeAdapter<Position> {
@Override
public Position read(JsonReader reader) throws IOException {
switch (reader.peek()) {
case BEGIN_ARRAY: // "pos": [x,y,z]
reader.beginArray();
val array = new FloatArrayList(3);
while (reader.hasNext())
array.add(reader.nextInt());
reader.endArray();
return new Position(array);
case BEGIN_OBJECT: // "pos": {"x": x, "y": y, "z": z}
float x = 0f;
float y = 0f;
float z = 0f;
reader.beginObject();
for (var next = reader.peek(); next != JsonToken.END_OBJECT; next = reader.peek()) {
val name = reader.nextName();
switch (name) {
case "x", "X", "_x" -> x = (float) reader.nextDouble();
case "y", "Y", "_y" -> y = (float) reader.nextDouble();
case "z", "Z", "_z" -> z = (float) reader.nextDouble();
default -> throw new IOException("Invalid field in Position definition - " + name);
}
}
reader.endObject();
return new Position(x, y, z);
default:
throw new IOException("Invalid Position definition - " + reader.peek().name());
}
}
@Override
public void write(JsonWriter writer, Position i) throws IOException {
writer.beginArray();
writer.value(i.getX());
writer.value(i.getY());
writer.value(i.getZ());
writer.endArray();
};
} }
static class EnumTypeAdapterFactory implements TypeAdapterFactory { static class EnumTypeAdapterFactory implements TypeAdapterFactory {
...@@ -109,7 +158,9 @@ public class JsonAdapters { ...@@ -109,7 +158,9 @@ public class JsonAdapters {
throw new IOException("Invalid Enum definition - " + reader.peek().name()); throw new IOException("Invalid Enum definition - " + reader.peek().name());
} }
} }
public void write(JsonWriter writer, T value) {} public void write(JsonWriter writer, T value) throws IOException {
writer.value(value.toString());
}
}; };
} }
} }
......
...@@ -27,6 +27,7 @@ public final class JsonUtils { ...@@ -27,6 +27,7 @@ public final class JsonUtils {
.setPrettyPrinting() .setPrettyPrinting()
.registerTypeAdapter(DynamicFloat.class, new DynamicFloatAdapter()) .registerTypeAdapter(DynamicFloat.class, new DynamicFloatAdapter())
.registerTypeAdapter(IntList.class, new IntListAdapter()) .registerTypeAdapter(IntList.class, new IntListAdapter())
.registerTypeAdapter(Position.class, new PositionAdapter())
.registerTypeAdapterFactory(new EnumTypeAdapterFactory()) .registerTypeAdapterFactory(new EnumTypeAdapterFactory())
.create(); .create();
......
package emu.grasscutter.utils; package emu.grasscutter.utils;
import java.io.Serializable; import java.io.Serializable;
import java.util.List;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import com.github.davidmoten.rtreemulti.geometry.Point; import com.github.davidmoten.rtreemulti.geometry.Point;
...@@ -22,9 +23,7 @@ public class Position implements Serializable { ...@@ -22,9 +23,7 @@ public class Position implements Serializable {
@SerializedName(value="z", alternate={"_z", "Z"}) @SerializedName(value="z", alternate={"_z", "Z"})
@Getter @Setter private float z; @Getter @Setter private float z;
public Position() { public Position() {}
}
public Position(float x, float y) { public Position(float x, float y) {
set(x, y); set(x, y);
...@@ -34,6 +33,20 @@ public class Position implements Serializable { ...@@ -34,6 +33,20 @@ public class Position implements Serializable {
set(x, y, z); set(x, y, z);
} }
public Position(List<Float> xyz) {
switch (xyz.size()) {
default: // Might want to error on excess elements, but maybe we want to extend to 3+3 representation later.
case 3:
this.z = xyz.get(2); // Fall-through
case 2:
this.y = xyz.get(1); // Fall-through
case 1:
this.y = xyz.get(0); // pointless fall-through
case 0:
break;
}
}
public Position(String p) { public Position(String p) {
String[] split = p.split(","); String[] split = p.split(",");
if (split.length >= 2) { if (split.length >= 2) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment