Fix issue with serialized GridPositions

This commit is contained in:
KingRainbow44 2023-04-24 03:11:09 -04:00
parent 25c72a19e0
commit 66b0e6f3c6
No known key found for this signature in database
GPG Key ID: FC2CB64B00D257BE
3 changed files with 46 additions and 12 deletions

View File

@ -7,6 +7,7 @@ import java.io.Serializable;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
@Entity
public final class GridPosition implements Serializable {
@ -38,8 +39,13 @@ public final class GridPosition implements Serializable {
this.x = xzwidth.get(0);
}
public GridPosition(String str) throws IOException {
String[] listOfParams = str.replace(" ", "").replace("(", "").replace(")", "").split(",");
@SneakyThrows
public GridPosition(String str) {
var listOfParams = str
.replace(" ", "")
.replace("(", "")
.replace(")", "")
.split(",");
if (listOfParams.length != 3)
throw new IOException("invalid size on GridPosition definition - ");
try {

View File

@ -77,6 +77,36 @@ public class JsonAdapters {
}
}
static class GridPositionAdapter extends TypeAdapter<GridPosition> {
@Override
public void write(JsonWriter out, GridPosition value) throws IOException {
out.value("(" + value.getX() + ", " + value.getZ() + ", " + value.getWidth() + ")");
}
@Override
public GridPosition read(JsonReader in) throws IOException {
if (in.peek() != JsonToken.STRING)
throw new IOException("Invalid GridPosition definition - " + in.peek().name());
// GridPosition follows the format of: (x, y, z).
// Flatten to (x,y,z) for easier parsing.
var str = in.nextString()
.replace("(", "")
.replace(")", "")
.replace(" ", "");
var split = str.split(",");
if (split.length != 3)
throw new IOException("Invalid GridPosition definition - " + in.peek().name());
return new GridPosition(
Integer.parseInt(split[0]),
Integer.parseInt(split[1]),
Integer.parseInt(split[2])
);
}
}
static class PositionAdapter extends TypeAdapter<Position> {
@Override
public Position read(JsonReader reader) throws IOException {

View File

@ -6,10 +6,7 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import emu.grasscutter.data.common.DynamicFloat;
import emu.grasscutter.utils.JsonAdapters.DynamicFloatAdapter;
import emu.grasscutter.utils.JsonAdapters.EnumTypeAdapterFactory;
import emu.grasscutter.utils.JsonAdapters.IntListAdapter;
import emu.grasscutter.utils.JsonAdapters.PositionAdapter;
import emu.grasscutter.utils.JsonAdapters.*;
import it.unimi.dsi.fastutil.ints.IntList;
import java.io.FileInputStream;
import java.io.IOException;
@ -25,12 +22,13 @@ import java.util.Map;
public final class JsonUtils {
static final Gson gson =
new GsonBuilder()
.setPrettyPrinting()
.registerTypeAdapter(DynamicFloat.class, new DynamicFloatAdapter())
.registerTypeAdapter(IntList.class, new IntListAdapter())
.registerTypeAdapter(Position.class, new PositionAdapter())
.registerTypeAdapterFactory(new EnumTypeAdapterFactory())
.create();
.setPrettyPrinting()
.registerTypeAdapter(DynamicFloat.class, new DynamicFloatAdapter())
.registerTypeAdapter(IntList.class, new IntListAdapter())
.registerTypeAdapter(Position.class, new PositionAdapter())
.registerTypeAdapter(GridPosition.class, new GridPositionAdapter())
.registerTypeAdapterFactory(new EnumTypeAdapterFactory())
.create();
/*
* Encode an object to a JSON string