|
| 1 | +package net.errorcraft.codecium.mixin.mojang.serialization.codecs; |
| 2 | + |
| 3 | +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; |
| 4 | +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; |
| 5 | +import com.llamalad7.mixinextras.sugar.Local; |
| 6 | +import com.mojang.datafixers.util.Pair; |
| 7 | +import com.mojang.serialization.Codec; |
| 8 | +import com.mojang.serialization.DataResult; |
| 9 | +import com.mojang.serialization.DynamicOps; |
| 10 | +import com.mojang.serialization.codecs.BaseMapCodec; |
| 11 | +import net.errorcraft.codecium.util.StringUtil; |
| 12 | +import org.spongepowered.asm.mixin.Mixin; |
| 13 | +import org.spongepowered.asm.mixin.Shadow; |
| 14 | +import org.spongepowered.asm.mixin.injection.At; |
| 15 | +import org.spongepowered.asm.mixin.injection.ModifyArg; |
| 16 | + |
| 17 | +import java.util.Optional; |
| 18 | +import java.util.function.Supplier; |
| 19 | +import java.util.function.UnaryOperator; |
| 20 | + |
| 21 | +@Mixin(value = BaseMapCodec.class, remap = false) |
| 22 | +public interface BaseMapCodecExtender<K, V> { |
| 23 | + @Shadow |
| 24 | + Codec<K> keyCodec(); |
| 25 | + |
| 26 | + @ModifyArg( |
| 27 | + method = "lambda$decode$3", |
| 28 | + at = @At( |
| 29 | + value = "INVOKE", |
| 30 | + target = "Lcom/mojang/serialization/DataResult;error(Ljava/util/function/Supplier;)Lcom/mojang/serialization/DataResult;" |
| 31 | + ) |
| 32 | + ) |
| 33 | + @SuppressWarnings({ "OptionalUsedAsFieldOrParameterType", "OptionalGetWithoutIsPresent" }) |
| 34 | + private <T> Supplier<String> duplicateFieldUseBetterErrorMessage(Supplier<String> message, @Local(argsOnly = true) final DynamicOps<T> ops, @Local(argsOnly = true) final Pair<T, T> pair, @Local final Optional<Pair<K, V>> entry) { |
| 35 | + return () -> "Duplicate field " + this.keyCodec().encodeStart(ops, entry.get().getFirst()).getOrThrow() + ": " + pair.getFirst(); |
| 36 | + } |
| 37 | + |
| 38 | + @WrapOperation( |
| 39 | + method = "lambda$decode$3", |
| 40 | + at = @At( |
| 41 | + value = "INVOKE", |
| 42 | + target = "Lcom/mojang/serialization/Codec;parse(Lcom/mojang/serialization/DynamicOps;Ljava/lang/Object;)Lcom/mojang/serialization/DataResult;", |
| 43 | + ordinal = 0 |
| 44 | + ) |
| 45 | + ) |
| 46 | + private <T> DataResult<K> addKeyToKeyErrorMessage(Codec<K> instance, final DynamicOps<T> dynamicOps, Object o, Operation<DataResult<K>> original, @Local(argsOnly = true) final Pair<T, T> pair) { |
| 47 | + return original.call(instance, dynamicOps, o) |
| 48 | + .mapError(message -> "For key " + pair.getFirst() + ": " + message); |
| 49 | + } |
| 50 | + |
| 51 | + @ModifyArg( |
| 52 | + method = "lambda$decode$3", |
| 53 | + at = @At( |
| 54 | + value = "INVOKE", |
| 55 | + target = "Lcom/mojang/serialization/DataResult;apply2stable(Ljava/util/function/BiFunction;Lcom/mojang/serialization/DataResult;)Lcom/mojang/serialization/DataResult;", |
| 56 | + ordinal = 0 |
| 57 | + ) |
| 58 | + ) |
| 59 | + private <T> DataResult<V> addKeyToValueErrorMessage(DataResult<V> second, @Local(argsOnly = true) final Pair<T, T> pair) { |
| 60 | + return second.mapError(message -> "For value " + pair.getFirst() + ": " + message); |
| 61 | + } |
| 62 | + |
| 63 | + @ModifyArg( |
| 64 | + method = "decode", |
| 65 | + at = @At( |
| 66 | + value = "INVOKE", |
| 67 | + target = "Lcom/mojang/serialization/DataResult;mapError(Ljava/util/function/UnaryOperator;)Lcom/mojang/serialization/DataResult;" |
| 68 | + ) |
| 69 | + ) |
| 70 | + private <T> UnaryOperator<String> mapErrorsUseBetterErrorMessage(UnaryOperator<String> function, @Local final T errors) { |
| 71 | + return message -> "Map has errors:\n" + StringUtil.indent(message); |
| 72 | + } |
| 73 | +} |
0 commit comments