| 1 | package net.minecraft.network.codec;
|
| 2 |
|
| 3 | import io.netty.buffer.ByteBuf;
|
| 4 | import io.netty.handler.codec.DecoderException;
|
| 5 | import io.netty.handler.codec.EncoderException;
|
| 6 | import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
| 7 | import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
|
| 8 | import java.util.ArrayList;
|
| 9 | import java.util.List;
|
| 10 | import java.util.function.Function;
|
| 11 | import net.minecraft.network.VarInt;
|
| 12 |
|
| 13 | public class IdDispatchCodec<B extends ByteBuf, V, T> implements StreamCodec<B, V> {
|
| 14 | private static final int UNKNOWN_TYPE = -1;
|
| 15 | private final Function<V, ? extends T> typeGetter;
|
| 16 | private final List<IdDispatchCodec.Entry<B, V, T>> byId;
|
| 17 | private final Object2IntMap<T> toId;
|
| 18 |
|
| 19 | private IdDispatchCodec(final Function<V, ? extends T> typeGetter, final List<IdDispatchCodec.Entry<B, V, T>> byId, final Object2IntMap<T> toId) {
|
| 20 | this.typeGetter = typeGetter;
|
| 21 | this.byId = byId;
|
| 22 | this.toId = toId;
|
| 23 | }
|
| 24 |
|
| 25 | public V decode(final B input) {
|
| 26 | int id = VarInt.read(input);
|
| 27 | if (id >= 0 && id < this.byId.size()) {
|
| 28 | IdDispatchCodec.Entry<B, V, T> entry = this.byId.get(id);
|
| 29 |
|
| 30 | try {
|
| 31 | return (V)entry.serializer.decode(input);
|
| 32 | } catch (Exception e) {
|
| 33 | if (e instanceof IdDispatchCodec.DontDecorateException) {
|
| 34 | throw e;
|
| 35 | } else {
|
| 36 | throw new DecoderException("Failed to decode packet '" + entry.type + "'", e);
|
| 37 | }
|
| 38 | }
|
| 39 | } else {
|
| 40 | throw new DecoderException("Received unknown packet id " + id);
|
| 41 | }
|
| 42 | }
|
| 43 |
|
| 44 | public void encode(final B output, final V value) {
|
| 45 | T type = (T)this.typeGetter.apply(value);
|
| 46 | int id = this.toId.getOrDefault(type, -1);
|
| 47 | if (id == -1) {
|
| 48 | throw new EncoderException("Sending unknown packet '" + type + "'");
|
| 49 | }
|
| 50 |
|
| 51 | VarInt.write(output, id);
|
| 52 | IdDispatchCodec.Entry<B, V, T> entry = this.byId.get(id);
|
| 53 |
|
| 54 | try {
|
| 55 | StreamCodec<? super B, V> codec = (StreamCodec<? super B, V>)entry.serializer;
|
| 56 | codec.encode(output, value);
|
| 57 | } catch (Exception e) {
|
| 58 | if (e instanceof IdDispatchCodec.DontDecorateException) {
|
| 59 | throw e;
|
| 60 | } else {
|
| 61 | throw new EncoderException("Failed to encode packet '" + type + "'", e);
|
| 62 | }
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | public static <B extends ByteBuf, V, T> IdDispatchCodec.Builder<B, V, T> builder(final Function<V, ? extends T> typeGetter) {
|
| 67 | return new IdDispatchCodec.Builder<>(typeGetter);
|
| 68 | }
|
| 69 |
|
| 70 | public static class Builder<B extends ByteBuf, V, T> {
|
| 71 | private final List<IdDispatchCodec.Entry<B, V, T>> entries = new ArrayList<>();
|
| 72 | private final Function<V, ? extends T> typeGetter;
|
| 73 |
|
| 74 | private Builder(final Function<V, ? extends T> typeGetter) {
|
| 75 | this.typeGetter = typeGetter;
|
| 76 | }
|
| 77 |
|
| 78 | public IdDispatchCodec.Builder<B, V, T> add(final T type, final StreamCodec<? super B, ? extends V> serializer) {
|
| 79 | this.entries.add(new IdDispatchCodec.Entry<>(serializer, type));
|
| 80 | return this;
|
| 81 | }
|
| 82 |
|
| 83 | public IdDispatchCodec<B, V, T> build() {
|
| 84 | Object2IntOpenHashMap<T> toId = new Object2IntOpenHashMap<>();
|
| 85 | toId.defaultReturnValue(-2);
|
| 86 |
|
| 87 | for (IdDispatchCodec.Entry<B, V, T> entry : this.entries) {
|
| 88 | int id = toId.size();
|
| 89 | int previous = toId.putIfAbsent(entry.type, id);
|
| 90 | if (previous != -2) {
|
| 91 | throw new IllegalStateException("Duplicate registration for type " + entry.type);
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | return new IdDispatchCodec<>(this.typeGetter, List.copyOf(this.entries), toId);
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | public interface DontDecorateException {
|
| 100 | }
|
| 101 |
|
| 102 | private record Entry<B, V, T>(StreamCodec<? super B, ? extends V> serializer, T type) {
|
| 103 | }
|
| 104 | }
|
| 105 |
|