From 4b2d813445d45adb50a88a284c271b8dbb2f302c Mon Sep 17 00:00:00 2001 From: Dragon-Seeker Date: Sun, 17 Dec 2023 14:43:29 -0600 Subject: [PATCH] [endec] Fix issue with converting between formats requiring lenient Deserilization within Endec Based Codecs Such is due to how Json and Nbt formats store data and how they are converted to and from each other and Edm --- .../wispforest/owo/serialization/Endec.java | 7 +-- .../owo/serialization/StructEndec.java | 7 +-- .../format/edm/EdmDeserializer.java | 5 +- .../format/edm/LenientEdmDeserializer.java | 55 +++++++++++++++++++ 4 files changed, 62 insertions(+), 12 deletions(-) create mode 100644 src/main/java/io/wispforest/owo/serialization/format/edm/LenientEdmDeserializer.java diff --git a/src/main/java/io/wispforest/owo/serialization/Endec.java b/src/main/java/io/wispforest/owo/serialization/Endec.java index 0556c0ad..62ab6bb9 100644 --- a/src/main/java/io/wispforest/owo/serialization/Endec.java +++ b/src/main/java/io/wispforest/owo/serialization/Endec.java @@ -6,10 +6,7 @@ import com.mojang.serialization.DataResult; import com.mojang.serialization.DynamicOps; import io.wispforest.owo.serialization.endec.*; -import io.wispforest.owo.serialization.format.edm.EdmDeserializer; -import io.wispforest.owo.serialization.format.edm.EdmEndec; -import io.wispforest.owo.serialization.format.edm.EdmOps; -import io.wispforest.owo.serialization.format.edm.EdmSerializer; +import io.wispforest.owo.serialization.format.edm.*; import org.jetbrains.annotations.Nullable; import java.util.*; @@ -410,7 +407,7 @@ default Codec codec(SerializationAttribute... assumedAttributes) { @Override public DataResult> decode(DynamicOps ops, D input) { try { - return DataResult.success(new Pair<>(Endec.this.decode(new EdmDeserializer(ops.convertTo(EdmOps.INSTANCE, input), assumedAttributes)), input)); + return DataResult.success(new Pair<>(Endec.this.decode(new LenientEdmDeserializer(ops.convertTo(EdmOps.INSTANCE, input), assumedAttributes)), input)); } catch (Exception e) { return DataResult.error(e::getMessage); } diff --git a/src/main/java/io/wispforest/owo/serialization/StructEndec.java b/src/main/java/io/wispforest/owo/serialization/StructEndec.java index 76b645c0..e44a2033 100644 --- a/src/main/java/io/wispforest/owo/serialization/StructEndec.java +++ b/src/main/java/io/wispforest/owo/serialization/StructEndec.java @@ -1,10 +1,7 @@ package io.wispforest.owo.serialization; import com.mojang.serialization.*; -import io.wispforest.owo.serialization.format.edm.EdmDeserializer; -import io.wispforest.owo.serialization.format.edm.EdmElement; -import io.wispforest.owo.serialization.format.edm.EdmOps; -import io.wispforest.owo.serialization.format.edm.EdmSerializer; +import io.wispforest.owo.serialization.format.edm.*; import net.minecraft.util.Util; import java.util.HashMap; @@ -57,7 +54,7 @@ public DataResult decode(DynamicOps ops, MapLike input) { ); }); - return DataResult.success(StructEndec.this.decode(new EdmDeserializer(EdmElement.wrapMap(map)))); + return DataResult.success(StructEndec.this.decode(new LenientEdmDeserializer(EdmElement.wrapMap(map)))); } catch (Exception e) { return DataResult.error(e::getMessage); } diff --git a/src/main/java/io/wispforest/owo/serialization/format/edm/EdmDeserializer.java b/src/main/java/io/wispforest/owo/serialization/format/edm/EdmDeserializer.java index 2f27bb2a..068132b1 100644 --- a/src/main/java/io/wispforest/owo/serialization/format/edm/EdmDeserializer.java +++ b/src/main/java/io/wispforest/owo/serialization/format/edm/EdmDeserializer.java @@ -55,6 +55,7 @@ public long readLong() { public float readFloat() { return this.getValue().cast(); } + @Override public double readDouble() { return this.getValue().cast(); @@ -64,12 +65,12 @@ public double readDouble() { @Override public int readVarInt() { - return this.getValue().cast(); + return this.readInt(); } @Override public long readVarLong() { - return this.getValue().cast(); + return this.readLong(); } // --- diff --git a/src/main/java/io/wispforest/owo/serialization/format/edm/LenientEdmDeserializer.java b/src/main/java/io/wispforest/owo/serialization/format/edm/LenientEdmDeserializer.java new file mode 100644 index 00000000..9c40f0a2 --- /dev/null +++ b/src/main/java/io/wispforest/owo/serialization/format/edm/LenientEdmDeserializer.java @@ -0,0 +1,55 @@ +package io.wispforest.owo.serialization.format.edm; + +import io.wispforest.owo.serialization.SerializationAttribute; + +public class LenientEdmDeserializer extends EdmDeserializer { + + public LenientEdmDeserializer(EdmElement serialized, SerializationAttribute... extraAttributes) { + super(serialized, extraAttributes); + } + + // --- + + @Override + public byte readByte() { + return this.getValue().cast().byteValue(); + } + + @Override + public short readShort() { + return this.getValue().cast().shortValue(); + } + + @Override + public int readInt() { + return this.getValue().cast().intValue(); + } + + @Override + public long readLong() { + return this.getValue().cast().longValue(); + } + + // --- + + @Override + public float readFloat() { + return this.getValue().cast().floatValue(); + } + + @Override + public double readDouble() { + return this.getValue().cast().doubleValue(); + } + + // --- + + @Override + public boolean readBoolean() { + if(this.getValue().value() instanceof Number number){ + return number.byteValue() == 1; + } + + return super.readBoolean(); + } +}