Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.21.2] Update owo for latest Endec changes #326

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ dependencies {
modCompileOnly("com.terraformersmc:modmenu:${project.modmenu_version}")
modLocalRuntime("com.terraformersmc:modmenu:${project.modmenu_version}")

include api("io.wispforest:endec:0.1.5")
include api("io.wispforest.endec:netty:0.1.2")
include api("io.wispforest.endec:gson:0.1.3")
include api("io.wispforest.endec:jankson:0.1.3")
include api("io.wispforest:endec:0.1.9")
include api("io.wispforest.endec:netty:0.1.5")
include api("io.wispforest.endec:gson:0.1.6")
include api("io.wispforest.endec:jankson:0.1.6")

include api("blue.endless:jankson:${project.jankson_version}")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public <T1> DataResult<T> decode(DynamicOps<T1> ops, MapLike<T1> input) {
);
});

return structEndec.decode(context, LenientEdmDeserializer.of(EdmElement.wrapMap(map)));
return structEndec.decode(context, LenientEdmDeserializer.of(EdmElement.consumeMap(map)));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,73 +43,73 @@ public EdmElement<?> empty() {
}

public EdmElement<?> createNumeric(Number number) {
return EdmElement.wrapDouble(number.doubleValue());
return EdmElement.f64(number.doubleValue());
}

public EdmElement<?> createByte(byte b) {
return EdmElement.wrapByte(b);
return EdmElement.i8(b);
}

public EdmElement<?> createShort(short s) {
return EdmElement.wrapShort(s);
return EdmElement.i16(s);
}

public EdmElement<?> createInt(int i) {
return EdmElement.wrapInt(i);
return EdmElement.i32(i);
}

public EdmElement<?> createLong(long l) {
return EdmElement.wrapLong(l);
return EdmElement.i64(l);
}

public EdmElement<?> createFloat(float f) {
return EdmElement.wrapFloat(f);
return EdmElement.f32(f);
}

public EdmElement<?> createDouble(double d) {
return EdmElement.wrapDouble(d);
return EdmElement.f64(d);
}

// ---

public EdmElement<?> createBoolean(boolean bl) {
return EdmElement.wrapBoolean(bl);
return EdmElement.bool(bl);
}

@Override
public EdmElement<?> createString(String value) {
return EdmElement.wrapString(value);
return EdmElement.string(value);
}

@Override
public EdmElement<?> createByteList(ByteBuffer input) {
return EdmElement.wrapBytes(DataFixUtils.toArray(input));
return EdmElement.bytes(DataFixUtils.toArray(input));
}

// ---

@Override
public EdmElement<?> createList(Stream<EdmElement<?>> input) {
return EdmElement.wrapSequence(input.toList());
return EdmElement.sequence(input.toList());
}

@Override
public DataResult<EdmElement<?>> mergeToList(EdmElement<?> list, EdmElement<?> value) {
if (list == empty()) {
return DataResult.success(EdmElement.wrapSequence(List.of(value)));
return DataResult.success(EdmElement.sequence(List.of(value)));
} else if (list.value() instanceof List<?> properList) {
var newList = new ArrayList<EdmElement<?>>((Collection<? extends EdmElement<?>>) properList);
newList.add(value);

return DataResult.success(EdmElement.wrapSequence(newList));
return DataResult.success(EdmElement.sequence(newList));
} else {
return DataResult.error(() -> "Not a sequence: " + list);
}
}

@Override
public EdmElement<?> createMap(Stream<Pair<EdmElement<?>, EdmElement<?>>> map) {
return EdmElement.wrapMap(map.collect(Collectors.toMap(pair -> pair.getFirst().cast(), Pair::getSecond)));
return EdmElement.consumeMap(map.collect(Collectors.toMap(pair -> pair.getFirst().cast(), Pair::getSecond)));
}

@Override
Expand All @@ -119,12 +119,12 @@ public DataResult<EdmElement<?>> mergeToMap(EdmElement<?> map, EdmElement<?> key
}

if (map == empty()) {
return DataResult.success(EdmElement.wrapMap(Map.of(key.cast(), value)));
return DataResult.success(EdmElement.consumeMap(Map.of(key.cast(), value)));
} else if (map.value() instanceof Map<?, ?> properMap) {
var newMap = new HashMap<String, EdmElement<?>>((Map<String, ? extends EdmElement<?>>) properMap);
newMap.put(key.cast(), value);

return DataResult.success(EdmElement.wrapMap(newMap));
return DataResult.success(EdmElement.consumeMap(newMap));
} else {
return DataResult.error(() -> "Not a map: " + map);
}
Expand Down Expand Up @@ -189,7 +189,7 @@ public DataResult<Stream<Pair<EdmElement<?>, EdmElement<?>>>> getMapValues(EdmEl
return DataResult.success(Stream.of());
} else if (input.value() instanceof Map<?, ?> map) {
//noinspection rawtypes
return DataResult.success(map.entrySet().stream().map(entry -> new Pair(EdmElement.wrapString((String) entry.getKey()), entry.getValue())));
return DataResult.success(map.entrySet().stream().map(entry -> new Pair(EdmElement.string((String) entry.getKey()), entry.getValue())));
} else {
return DataResult.error(() -> "Not a map: " + input);
}
Expand All @@ -200,13 +200,13 @@ public DataResult<Stream<Pair<EdmElement<?>, EdmElement<?>>>> getMapValues(EdmEl
@Override
public <U> U convertTo(DynamicOps<U> outOps, EdmElement<?> input) {
if (input == this.empty()) return outOps.empty();
return switch (input.type()) {
case BYTE -> outOps.createByte(input.cast());
case SHORT -> outOps.createShort(input.cast());
case INT -> outOps.createInt(input.cast());
case LONG -> outOps.createLong(input.cast());
case FLOAT -> outOps.createFloat(input.cast());
case DOUBLE -> outOps.createDouble(input.cast());
return switch (input.type()) { // TODO: DO WE NEED TO HANDLE Unsigned Numbers specifically here or nah?
case I8, U8 -> outOps.createByte(input.cast());
case I16, U16 -> outOps.createShort(input.cast());
case I32, U32 -> outOps.createInt(input.cast());
case I64, U64 -> outOps.createLong(input.cast());
case F32 -> outOps.createFloat(input.cast());
case F64 -> outOps.createDouble(input.cast());
case BOOLEAN -> outOps.createBoolean(input.cast());
case STRING -> outOps.createString(input.cast());
case BYTES -> outOps.createByteList(ByteBuffer.wrap(input.cast()));
Expand All @@ -223,7 +223,7 @@ public EdmElement<?> remove(EdmElement<?> input, String key) {
var newMap = new HashMap<String, EdmElement<?>>((Map<? extends String, ? extends EdmElement<?>>) map);
newMap.remove(key);

return EdmElement.wrapMap(newMap);
return EdmElement.consumeMap(newMap);
} else {
return input;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.function.Supplier;

public class NbtDeserializer extends RecursiveDeserializer<NbtElement> implements SelfDescribedDeserializer<NbtElement> {

Expand Down Expand Up @@ -88,14 +89,10 @@ public byte[] readBytes(SerializationContext ctx) {

@Override
public <V> Optional<V> readOptional(SerializationContext ctx, Endec<V> endec) {
if (this.isReadingStructField()) {
return Optional.of(endec.decode(ctx, this));
} else {
var struct = this.struct();
return struct.field("present", ctx, Endec.BOOLEAN)
? Optional.of(struct.field("value", ctx, endec))
: Optional.empty();
}
var struct = this.struct();
return struct.field("present", ctx, Endec.BOOLEAN)
? Optional.of(struct.field("value", ctx, endec))
: Optional.empty();
}

// ---
Expand Down Expand Up @@ -183,8 +180,7 @@ public boolean hasNext() {
public V next() {
return NbtDeserializer.this.frame(
this.elements::next,
() -> this.valueEndec.decode(this.ctx, NbtDeserializer.this),
false
() -> this.valueEndec.decode(this.ctx, NbtDeserializer.this)
);
}
}
Expand Down Expand Up @@ -221,8 +217,7 @@ public java.util.Map.Entry<String, V> next() {
var key = this.keys.next();
return NbtDeserializer.this.frame(
() -> this.compound.get(key),
() -> java.util.Map.entry(key, this.valueEndec.decode(this.ctx, NbtDeserializer.this)),
false
() -> java.util.Map.entry(key, this.valueEndec.decode(this.ctx, NbtDeserializer.this))
);
}
}
Expand All @@ -236,25 +231,17 @@ public Struct(NbtCompound compound) {
}

@Override
public <F> @Nullable F field(String name, SerializationContext ctx, Endec<F> endec) {
public <F> @Nullable F field(String name, SerializationContext ctx, Endec<F> endec, @Nullable Supplier<F> defaultValueFactory) {
if (!this.compound.contains(name)) {
throw new IllegalStateException("Field '" + name + "' was missing from serialized data, but no default value was provided");
}

return NbtDeserializer.this.frame(
() -> this.compound.get(name),
() -> endec.decode(ctx, NbtDeserializer.this),
true
);
}
if (defaultValueFactory == null) {
throw new IllegalStateException("Field '" + name + "' was missing from serialized data, but no default value was provided");
}

@Override
public <F> @Nullable F field(String name, SerializationContext ctx, Endec<F> endec, @Nullable F defaultValue) {
if (!this.compound.contains(name)) return defaultValue;
return defaultValueFactory.get();
}
return NbtDeserializer.this.frame(
() -> this.compound.get(name),
() -> endec.decode(ctx, NbtDeserializer.this),
true
() -> endec.decode(ctx, NbtDeserializer.this)
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
import net.minecraft.nbt.*;
import net.minecraft.network.encoding.VarInts;
import net.minecraft.network.encoding.VarLongs;
import org.apache.commons.lang3.mutable.MutableObject;
import org.spongepowered.asm.mixin.Mutable;

import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import java.util.WeakHashMap;

public class NbtSerializer extends RecursiveSerializer<NbtElement> implements SelfDescribedSerializer<NbtElement> {

Expand Down Expand Up @@ -95,16 +101,25 @@ public void writeBytes(SerializationContext ctx, byte[] bytes) {
this.consume(new NbtByteArray(bytes));
}

private final Set<NbtElement> encodedOptionals = Collections.newSetFromMap(new WeakHashMap<>());

@Override
public <V> void writeOptional(SerializationContext ctx, Endec<V> endec, Optional<V> optional) {
if (this.isWritingStructField()) {
optional.ifPresent(v -> endec.encode(ctx, this, v));
} else {
MutableObject<NbtElement> frameData = new MutableObject<>();

this.frame(encoded -> {
try (var struct = this.struct()) {
struct.field("present", ctx, Endec.BOOLEAN, optional.isPresent());
optional.ifPresent(value -> struct.field("value", ctx, endec, value));
}
}

var compound = encoded.require("optional representation");

encodedOptionals.add(compound);
frameData.setValue(compound);
});

this.consume(frameData.getValue());
}

// ---
Expand Down Expand Up @@ -152,17 +167,26 @@ public void entry(String key, V value) {
NbtSerializer.this.frame(encoded -> {
this.valueEndec.encode(this.ctx, NbtSerializer.this, value);
this.result.put(key, encoded.require("map value"));
}, false);
});
}

@Override
public <F> Struct field(String name, SerializationContext ctx, Endec<F> endec, F value) {
public <F> Struct field(String name, SerializationContext ctx, Endec<F> endec, F value, boolean mayOmit) {
NbtSerializer.this.frame(encoded -> {
endec.encode(ctx, NbtSerializer.this, value);
if (encoded.wasEncoded()) {
this.result.put(name, encoded.get());

var element = encoded.require("struct field");

if (mayOmit && NbtSerializer.this.encodedOptionals.contains(element)) {
var nbtCompound = (NbtCompound) element;

if(!nbtCompound.getBoolean("present")) return;

element = nbtCompound.get("value");
}
}, true);

this.result.put(name, element);
});

return this;
}
Expand Down Expand Up @@ -199,7 +223,7 @@ public void element(V element) {
NbtSerializer.this.frame(encoded -> {
this.valueEndec.encode(this.ctx, NbtSerializer.this, element);
this.result.add(encoded.require("sequence element"));
}, false);
});
}

@Override
Expand Down
Loading