Skip to content

Commit

Permalink
base on 1.20.5 proper, separate SerializationContext from (De)Serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
gliscowo committed Apr 28, 2024
1 parent ead2902 commit 5b91689
Show file tree
Hide file tree
Showing 48 changed files with 1,303 additions and 1,403 deletions.
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ org.gradle.jvmargs=-Xmx2G
# Fabric Properties
# check these on https://fabricmc.net/develop
minecraft_base_version=1.20.5
minecraft_version=24w14a
yarn_mappings=24w14a+build.4
loader_version=0.15.9
minecraft_version=1.20.5
yarn_mappings=1.20.5+build.1
loader_version=0.15.10
# Mod Properties
mod_version=0.12.5
maven_group=io.wispforest
archives_base_name=owo-lib
# Dependencies
fabric_version=0.96.14+1.20.5
fabric_version=0.97.7+1.20.5

# https://maven.shedaniel.me/me/shedaniel/RoughlyEnoughItems-fabric/
rei_version=14.0.688
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.Map;
import java.util.WeakHashMap;
import java.util.function.BiConsumer;
import java.util.function.Function;

public class ConfigSynchronizer {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.wispforest.owo.mixin;

import com.mojang.serialization.DynamicOps;
import net.minecraft.util.dynamic.ForwardingDynamicOps;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(ForwardingDynamicOps.class)
public interface ForwardingDynamicOpsAccessor<T> {
@Accessor("delegate")
DynamicOps<T> owo$delegate();
}
14 changes: 6 additions & 8 deletions src/main/java/io/wispforest/owo/mixin/NbtCompoundMixin.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package io.wispforest.owo.mixin;

import io.wispforest.owo.serialization.SerializationAttribute;
import io.wispforest.owo.serialization.SerializationAttributes;
import io.wispforest.owo.serialization.util.MapCarrier;
import io.wispforest.owo.serialization.SerializationContext;
import io.wispforest.owo.serialization.endec.KeyedEndec;
import io.wispforest.owo.serialization.format.forwarding.ForwardingDeserializer;
import io.wispforest.owo.serialization.format.forwarding.ForwardingSerializer;
import io.wispforest.owo.serialization.format.nbt.NbtDeserializer;
import io.wispforest.owo.serialization.format.nbt.NbtSerializer;
import io.wispforest.owo.serialization.util.MapCarrier;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import org.jetbrains.annotations.NotNull;
Expand All @@ -29,14 +27,14 @@ public abstract class NbtCompoundMixin implements MapCarrier {
public abstract boolean contains(String key);

@Override
public <T> T getWithErrors(@NotNull KeyedEndec<T> key) {
public <T> T getWithErrors(@NotNull KeyedEndec<T> key, SerializationContext ctx) {
if (!this.has(key)) return key.defaultValue();
return key.endec().decodeFully(e -> NbtDeserializer.of(e).withAttributes(SerializationAttributes.HUMAN_READABLE), this.get(key.key()));
return key.endec().decodeFully(ctx.withAttributes(SerializationAttributes.HUMAN_READABLE), NbtDeserializer::of, this.get(key.key()));
}

@Override
public <T> void put(@NotNull KeyedEndec<T> key, @NotNull T value) {
this.put(key.key(), key.endec().encodeFully(() -> NbtSerializer.of().withAttributes(SerializationAttributes.HUMAN_READABLE), value));
public <T> void put(@NotNull KeyedEndec<T> key, @NotNull T value, SerializationContext ctx) {
this.put(key.key(), key.endec().encodeFully(ctx.withAttributes(SerializationAttributes.HUMAN_READABLE), NbtSerializer::of, value));
}

@Override
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/io/wispforest/owo/mixin/PacketByteBufMixin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.wispforest.owo.mixin;

import io.wispforest.owo.serialization.Endec;
import io.wispforest.owo.serialization.SerializationContext;
import io.wispforest.owo.serialization.format.bytebuf.ByteBufDeserializer;
import io.wispforest.owo.serialization.format.bytebuf.ByteBufSerializer;
import io.wispforest.owo.serialization.util.EndecBuffer;
Expand All @@ -11,12 +12,12 @@
@Mixin(PacketByteBuf.class)
public class PacketByteBufMixin implements EndecBuffer {
@Override
public <T> void write(Endec<T> endec, T value) {
endec.encodeFully(() -> ByteBufSerializer.of((PacketByteBuf) (Object) this), value);
public <T> void write(SerializationContext ctx, Endec<T> endec, T value) {
endec.encodeFully(ctx, () -> ByteBufSerializer.of((PacketByteBuf) (Object) this), value);
}

@Override
public <T> T read(Endec<T> endec) {
return endec.decodeFully(ByteBufDeserializer::of, (PacketByteBuf) (Object) this);
public <T> T read(SerializationContext ctx, Endec<T> endec) {
return endec.decodeFully(ctx, ByteBufDeserializer::of, (PacketByteBuf) (Object) this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
import net.minecraft.registry.RegistryOps;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
import org.spongepowered.asm.mixin.gen.Invoker;

@Mixin(RegistryOps.class)
public interface RegistryOpsAccessor {
@Invoker("caching")
static RegistryOps.RegistryInfoGetter owo$caching(RegistryOps.RegistryInfoGetter registryInfoGetter) {throw new UnsupportedOperationException();}

@Accessor("registryInfoGetter")
RegistryOps.RegistryInfoGetter owo$infoGetter();
}
49 changes: 18 additions & 31 deletions src/main/java/io/wispforest/owo/serialization/Deserializer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.wispforest.owo.serialization;

import io.wispforest.owo.serialization.format.forwarding.ForwardingDeserializer;
import org.jetbrains.annotations.Nullable;

import java.util.Iterator;
Expand All @@ -9,39 +8,27 @@

public interface Deserializer<T> {

default Deserializer<T> withAttributes(SerializationAttribute.Instance... additionalAttributes) {
if (additionalAttributes.length == 0) return this;
return ForwardingDeserializer.of(this, additionalAttributes);
default SerializationContext setupContext(SerializationContext ctx) {
return ctx;
}

boolean hasAttribute(SerializationAttribute attribute);
<A> A getAttributeValue(SerializationAttribute.WithValue<A> attribute);
byte readByte(SerializationContext ctx);
short readShort(SerializationContext ctx);
int readInt(SerializationContext ctx);
long readLong(SerializationContext ctx);
float readFloat(SerializationContext ctx);
double readDouble(SerializationContext ctx);

default <A> A requireAttributeValue(SerializationAttribute.WithValue<A> attribute) {
if (!this.hasAttribute(attribute)) {
throw new IllegalStateException("Deserializer did not provide a value for attribute '" + attribute.name + "', which is required for decoding");
}
int readVarInt(SerializationContext ctx);
long readVarLong(SerializationContext ctx);

return this.getAttributeValue(attribute);
}

byte readByte();
short readShort();
int readInt();
long readLong();
float readFloat();
double readDouble();

int readVarInt();
long readVarLong();

boolean readBoolean();
String readString();
byte[] readBytes();
<V> Optional<V> readOptional(Endec<V> endec);
boolean readBoolean(SerializationContext ctx);
String readString(SerializationContext ctx);
byte[] readBytes(SerializationContext ctx);
<V> Optional<V> readOptional(SerializationContext ctx, Endec<V> endec);

<E> Sequence<E> sequence(Endec<E> elementEndec);
<V> Map<V> map(Endec<V> valueEndec);
<E> Sequence<E> sequence(SerializationContext ctx, Endec<E> elementEndec);
<V> Map<V> map(SerializationContext ctx, Endec<V> valueEndec);
Struct struct();

<V> V tryRead(Function<Deserializer<T>, V> reader);
Expand Down Expand Up @@ -73,12 +60,12 @@ interface Struct {
* Decode the value of field {@code name} using {@code endec}. If no
* such field exists in the serialized data, an exception is thrown
*/
<F> @Nullable F field(String name, Endec<F> endec);
<F> @Nullable F field(String name, SerializationContext ctx, Endec<F> endec);

/**
* Decode the value of field {@code name} using {@code endec}. If no
* such field exists in the serialized data, {@code defaultValue} is returned
*/
<F> @Nullable F field(String name, Endec<F> endec, @Nullable F defaultValue);
<F> @Nullable F field(String name, SerializationContext ctx, Endec<F> endec, @Nullable F defaultValue);
}
}
Loading

0 comments on commit 5b91689

Please sign in to comment.