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 java.util.List;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.SneakyThrows;
@Entity @Entity
public final class GridPosition implements Serializable { public final class GridPosition implements Serializable {
@ -38,8 +39,13 @@ public final class GridPosition implements Serializable {
this.x = xzwidth.get(0); this.x = xzwidth.get(0);
} }
public GridPosition(String str) throws IOException { @SneakyThrows
String[] listOfParams = str.replace(" ", "").replace("(", "").replace(")", "").split(","); public GridPosition(String str) {
var listOfParams = str
.replace(" ", "")
.replace("(", "")
.replace(")", "")
.split(",");
if (listOfParams.length != 3) if (listOfParams.length != 3)
throw new IOException("invalid size on GridPosition definition - "); throw new IOException("invalid size on GridPosition definition - ");
try { 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> { static class PositionAdapter extends TypeAdapter<Position> {
@Override @Override
public Position read(JsonReader reader) throws IOException { 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.JsonSyntaxException;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import emu.grasscutter.data.common.DynamicFloat; import emu.grasscutter.data.common.DynamicFloat;
import emu.grasscutter.utils.JsonAdapters.DynamicFloatAdapter; import emu.grasscutter.utils.JsonAdapters.*;
import emu.grasscutter.utils.JsonAdapters.EnumTypeAdapterFactory;
import emu.grasscutter.utils.JsonAdapters.IntListAdapter;
import emu.grasscutter.utils.JsonAdapters.PositionAdapter;
import it.unimi.dsi.fastutil.ints.IntList; import it.unimi.dsi.fastutil.ints.IntList;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
@ -29,6 +26,7 @@ public final class JsonUtils {
.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()) .registerTypeAdapter(Position.class, new PositionAdapter())
.registerTypeAdapter(GridPosition.class, new GridPositionAdapter())
.registerTypeAdapterFactory(new EnumTypeAdapterFactory()) .registerTypeAdapterFactory(new EnumTypeAdapterFactory())
.create(); .create();