| 1 | package net.minecraft.network.codec;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMultimap;
|
| 4 | import com.google.common.collect.ImmutableMultimap.Builder;
|
| 5 | import com.google.gson.Gson;
|
| 6 | import com.google.gson.GsonBuilder;
|
| 7 | import com.google.gson.JsonElement;
|
| 8 | import com.google.gson.JsonSyntaxException;
|
| 9 | import com.mojang.authlib.GameProfile;
|
| 10 | import com.mojang.authlib.properties.Property;
|
| 11 | import com.mojang.authlib.properties.PropertyMap;
|
| 12 | import com.mojang.datafixers.util.Either;
|
| 13 | import com.mojang.serialization.Codec;
|
| 14 | import com.mojang.serialization.DynamicOps;
|
| 15 | import io.netty.buffer.ByteBuf;
|
| 16 | import io.netty.handler.codec.DecoderException;
|
| 17 | import io.netty.handler.codec.EncoderException;
|
| 18 | import java.security.Key;
|
| 19 | import java.security.PublicKey;
|
| 20 | import java.time.Instant;
|
| 21 | import java.util.ArrayList;
|
| 22 | import java.util.BitSet;
|
| 23 | import java.util.Collection;
|
| 24 | import java.util.Iterator;
|
| 25 | import java.util.List;
|
| 26 | import java.util.Map;
|
| 27 | import java.util.Optional;
|
| 28 | import java.util.OptionalInt;
|
| 29 | import java.util.function.BiFunction;
|
| 30 | import java.util.function.Function;
|
| 31 | import java.util.function.IntFunction;
|
| 32 | import java.util.function.Supplier;
|
| 33 | import java.util.function.ToIntFunction;
|
| 34 | import net.minecraft.core.Holder;
|
| 35 | import net.minecraft.core.HolderSet;
|
| 36 | import net.minecraft.core.IdMap;
|
| 37 | import net.minecraft.core.Registry;
|
| 38 | import net.minecraft.core.UUIDUtil;
|
| 39 | import net.minecraft.nbt.CompoundTag;
|
| 40 | import net.minecraft.nbt.EndTag;
|
| 41 | import net.minecraft.nbt.NbtAccounter;
|
| 42 | import net.minecraft.nbt.NbtOps;
|
| 43 | import net.minecraft.nbt.Tag;
|
| 44 | import net.minecraft.network.FriendlyByteBuf;
|
| 45 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 46 | import net.minecraft.network.Utf8String;
|
| 47 | import net.minecraft.network.VarInt;
|
| 48 | import net.minecraft.network.VarLong;
|
| 49 | import net.minecraft.resources.Identifier;
|
| 50 | import net.minecraft.resources.RegistryOps;
|
| 51 | import net.minecraft.resources.ResourceKey;
|
| 52 | import net.minecraft.tags.TagKey;
|
| 53 | import net.minecraft.util.ARGB;
|
| 54 | import net.minecraft.util.Crypt;
|
| 55 | import net.minecraft.util.CryptException;
|
| 56 | import net.minecraft.util.LenientJsonParser;
|
| 57 | import net.minecraft.util.Mth;
|
| 58 | import org.joml.Quaternionfc;
|
| 59 | import org.joml.Vector3fc;
|
| 60 |
|
| 61 | public interface ByteBufCodecs {
|
| 62 | int MAX_INITIAL_COLLECTION_SIZE = 65536;
|
| 63 | StreamCodec<ByteBuf, Boolean> BOOL = new StreamCodec<ByteBuf, Boolean>() {
|
| 64 | public Boolean decode(final ByteBuf input) {
|
| 65 | return input.readBoolean();
|
| 66 | }
|
| 67 |
|
| 68 | public void encode(final ByteBuf output, final Boolean value) {
|
| 69 | output.writeBoolean(value);
|
| 70 | }
|
| 71 | };
|
| 72 | StreamCodec<ByteBuf, Byte> BYTE = new StreamCodec<ByteBuf, Byte>() {
|
| 73 | public Byte decode(final ByteBuf input) {
|
| 74 | return input.readByte();
|
| 75 | }
|
| 76 |
|
| 77 | public void encode(final ByteBuf output, final Byte value) {
|
| 78 | output.writeByte(value);
|
| 79 | }
|
| 80 | };
|
| 81 | StreamCodec<ByteBuf, Float> ROTATION_BYTE = BYTE.map(Mth::unpackDegrees, Mth::packDegrees);
|
| 82 | StreamCodec<ByteBuf, Short> SHORT = new StreamCodec<ByteBuf, Short>() {
|
| 83 | public Short decode(final ByteBuf input) {
|
| 84 | return input.readShort();
|
| 85 | }
|
| 86 |
|
| 87 | public void encode(final ByteBuf output, final Short value) {
|
| 88 | output.writeShort(value);
|
| 89 | }
|
| 90 | };
|
| 91 | StreamCodec<ByteBuf, Integer> UNSIGNED_SHORT = new StreamCodec<ByteBuf, Integer>() {
|
| 92 | public Integer decode(final ByteBuf input) {
|
| 93 | return input.readUnsignedShort();
|
| 94 | }
|
| 95 |
|
| 96 | public void encode(final ByteBuf output, final Integer value) {
|
| 97 | output.writeShort(value);
|
| 98 | }
|
| 99 | };
|
| 100 | StreamCodec<ByteBuf, Integer> INT = new StreamCodec<ByteBuf, Integer>() {
|
| 101 | public Integer decode(final ByteBuf input) {
|
| 102 | return input.readInt();
|
| 103 | }
|
| 104 |
|
| 105 | public void encode(final ByteBuf output, final Integer value) {
|
| 106 | output.writeInt(value);
|
| 107 | }
|
| 108 | };
|
| 109 | StreamCodec<ByteBuf, Integer> VAR_INT = new StreamCodec<ByteBuf, Integer>() {
|
| 110 | public Integer decode(final ByteBuf input) {
|
| 111 | return VarInt.read(input);
|
| 112 | }
|
| 113 |
|
| 114 | public void encode(final ByteBuf output, final Integer value) {
|
| 115 | VarInt.write(output, value);
|
| 116 | }
|
| 117 | };
|
| 118 | StreamCodec<ByteBuf, OptionalInt> OPTIONAL_VAR_INT = VAR_INT.map(
|
| 119 | i -> i == 0 ? OptionalInt.empty() : OptionalInt.of(i - 1), o -> o.isPresent() ? o.getAsInt() + 1 : 0
|
| 120 | );
|
| 121 | StreamCodec<ByteBuf, Long> LONG = new StreamCodec<ByteBuf, Long>() {
|
| 122 | public Long decode(final ByteBuf input) {
|
| 123 | return input.readLong();
|
| 124 | }
|
| 125 |
|
| 126 | public void encode(final ByteBuf output, final Long value) {
|
| 127 | output.writeLong(value);
|
| 128 | }
|
| 129 | };
|
| 130 | StreamCodec<ByteBuf, Long> VAR_LONG = new StreamCodec<ByteBuf, Long>() {
|
| 131 | public Long decode(final ByteBuf input) {
|
| 132 | return VarLong.read(input);
|
| 133 | }
|
| 134 |
|
| 135 | public void encode(final ByteBuf output, final Long value) {
|
| 136 | VarLong.write(output, value);
|
| 137 | }
|
| 138 | };
|
| 139 | StreamCodec<ByteBuf, Float> FLOAT = new StreamCodec<ByteBuf, Float>() {
|
| 140 | public Float decode(final ByteBuf input) {
|
| 141 | return input.readFloat();
|
| 142 | }
|
| 143 |
|
| 144 | public void encode(final ByteBuf output, final Float value) {
|
| 145 | output.writeFloat(value);
|
| 146 | }
|
| 147 | };
|
| 148 | StreamCodec<ByteBuf, Double> DOUBLE = new StreamCodec<ByteBuf, Double>() {
|
| 149 | public Double decode(final ByteBuf input) {
|
| 150 | return input.readDouble();
|
| 151 | }
|
| 152 |
|
| 153 | public void encode(final ByteBuf output, final Double value) {
|
| 154 | output.writeDouble(value);
|
| 155 | }
|
| 156 | };
|
| 157 | StreamCodec<ByteBuf, byte[]> BYTE_ARRAY = new StreamCodec<ByteBuf, byte[]>() {
|
| 158 | public byte[] decode(final ByteBuf input) {
|
| 159 | return FriendlyByteBuf.readByteArray(input);
|
| 160 | }
|
| 161 |
|
| 162 | public void encode(final ByteBuf output, final byte[] value) {
|
| 163 | FriendlyByteBuf.writeByteArray(output, value);
|
| 164 | }
|
| 165 | };
|
| 166 | StreamCodec<ByteBuf, long[]> LONG_ARRAY = new StreamCodec<ByteBuf, long[]>() {
|
| 167 | public long[] decode(final ByteBuf input) {
|
| 168 | return FriendlyByteBuf.readLongArray(input);
|
| 169 | }
|
| 170 |
|
| 171 | public void encode(final ByteBuf output, final long[] value) {
|
| 172 | FriendlyByteBuf.writeLongArray(output, value);
|
| 173 | }
|
| 174 | };
|
| 175 | StreamCodec<ByteBuf, BitSet> BIT_SET = new StreamCodec<ByteBuf, BitSet>() {
|
| 176 | public BitSet decode(final ByteBuf input) {
|
| 177 | return BitSet.valueOf(FriendlyByteBuf.readByteArray(input));
|
| 178 | }
|
| 179 |
|
| 180 | public void encode(final ByteBuf output, final BitSet value) {
|
| 181 | FriendlyByteBuf.writeByteArray(output, value.toByteArray());
|
| 182 | }
|
| 183 | };
|
| 184 | StreamCodec<ByteBuf, String> STRING_UTF8 = stringUtf8(32767);
|
| 185 | StreamCodec<ByteBuf, Tag> TAG = tagCodec(NbtAccounter::defaultQuota);
|
| 186 | StreamCodec<ByteBuf, Tag> TRUSTED_TAG = tagCodec(NbtAccounter::unlimitedHeap);
|
| 187 | StreamCodec<ByteBuf, CompoundTag> COMPOUND_TAG = compoundTagCodec(NbtAccounter::defaultQuota);
|
| 188 | StreamCodec<ByteBuf, CompoundTag> TRUSTED_COMPOUND_TAG = compoundTagCodec(NbtAccounter::unlimitedHeap);
|
| 189 | StreamCodec<ByteBuf, Optional<CompoundTag>> OPTIONAL_COMPOUND_TAG = new StreamCodec<ByteBuf, Optional<CompoundTag>>() {
|
| 190 | public Optional<CompoundTag> decode(final ByteBuf input) {
|
| 191 | return Optional.ofNullable(FriendlyByteBuf.readNbt(input));
|
| 192 | }
|
| 193 |
|
| 194 | public void encode(final ByteBuf output, final Optional<CompoundTag> value) {
|
| 195 | FriendlyByteBuf.writeNbt(output, value.orElse(null));
|
| 196 | }
|
| 197 | };
|
| 198 | StreamCodec<ByteBuf, Vector3fc> VECTOR3F = new StreamCodec<ByteBuf, Vector3fc>() {
|
| 199 | public Vector3fc decode(final ByteBuf input) {
|
| 200 | return FriendlyByteBuf.readVector3f(input);
|
| 201 | }
|
| 202 |
|
| 203 | public void encode(final ByteBuf output, final Vector3fc value) {
|
| 204 | FriendlyByteBuf.writeVector3f(output, value);
|
| 205 | }
|
| 206 | };
|
| 207 | StreamCodec<ByteBuf, Quaternionfc> QUATERNIONF = new StreamCodec<ByteBuf, Quaternionfc>() {
|
| 208 | public Quaternionfc decode(final ByteBuf input) {
|
| 209 | return FriendlyByteBuf.readQuaternion(input);
|
| 210 | }
|
| 211 |
|
| 212 | public void encode(final ByteBuf output, final Quaternionfc value) {
|
| 213 | FriendlyByteBuf.writeQuaternion(output, value);
|
| 214 | }
|
| 215 | };
|
| 216 | StreamCodec<ByteBuf, Integer> CONTAINER_ID = new StreamCodec<ByteBuf, Integer>() {
|
| 217 | public Integer decode(final ByteBuf input) {
|
| 218 | return FriendlyByteBuf.readContainerId(input);
|
| 219 | }
|
| 220 |
|
| 221 | public void encode(final ByteBuf output, final Integer value) {
|
| 222 | FriendlyByteBuf.writeContainerId(output, value);
|
| 223 | }
|
| 224 | };
|
| 225 | StreamCodec<ByteBuf, PropertyMap> GAME_PROFILE_PROPERTIES = new StreamCodec<ByteBuf, PropertyMap>() {
|
| 226 | public PropertyMap decode(final ByteBuf input) {
|
| 227 | int propertyCount = ByteBufCodecs.readCount(input, 16);
|
| 228 | Builder<String, Property> result = ImmutableMultimap.builder();
|
| 229 |
|
| 230 | for (int i = 0; i < propertyCount; i++) {
|
| 231 | String name = Utf8String.read(input, 64);
|
| 232 | String value = Utf8String.read(input, 32767);
|
| 233 | String signature = FriendlyByteBuf.readNullable(input, in -> Utf8String.read(in, 1024));
|
| 234 | Property property = new Property(name, value, signature);
|
| 235 | result.put(property.name(), property);
|
| 236 | }
|
| 237 |
|
| 238 | return new PropertyMap(result.build());
|
| 239 | }
|
| 240 |
|
| 241 | public void encode(final ByteBuf output, final PropertyMap properties) {
|
| 242 | ByteBufCodecs.writeCount(output, properties.size(), 16);
|
| 243 |
|
| 244 | for (Property property : properties.values()) {
|
| 245 | Utf8String.write(output, property.name(), 64);
|
| 246 | Utf8String.write(output, property.value(), 32767);
|
| 247 | FriendlyByteBuf.writeNullable(output, property.signature(), (out, signature) -> Utf8String.write(out, signature, 1024));
|
| 248 | }
|
| 249 | }
|
| 250 | };
|
| 251 | StreamCodec<ByteBuf, String> PLAYER_NAME = stringUtf8(16);
|
| 252 | StreamCodec<ByteBuf, GameProfile> GAME_PROFILE = StreamCodec.composite(
|
| 253 | UUIDUtil.STREAM_CODEC, GameProfile::id, PLAYER_NAME, GameProfile::name, GAME_PROFILE_PROPERTIES, GameProfile::properties, GameProfile::new
|
| 254 | );
|
| 255 | StreamCodec<ByteBuf, Integer> RGB_COLOR = new StreamCodec<ByteBuf, Integer>() {
|
| 256 | public Integer decode(final ByteBuf input) {
|
| 257 | return ARGB.color(input.readByte() & 0xFF, input.readByte() & 0xFF, input.readByte() & 0xFF);
|
| 258 | }
|
| 259 |
|
| 260 | public void encode(final ByteBuf output, final Integer value) {
|
| 261 | output.writeByte(ARGB.red(value));
|
| 262 | output.writeByte(ARGB.green(value));
|
| 263 | output.writeByte(ARGB.blue(value));
|
| 264 | }
|
| 265 | };
|
| 266 | StreamCodec<ByteBuf, Instant> INSTANT = LONG.map(Instant::ofEpochMilli, Instant::toEpochMilli);
|
| 267 | int PUBLIC_KEY_SIZE = 256;
|
| 268 | int MAX_PUBLIC_KEY_HEADER_SIZE = 256;
|
| 269 | int MAX_PUBLIC_KEY_LENGTH = 512;
|
| 270 | StreamCodec<ByteBuf, PublicKey> PUBLIC_KEY = byteArray(512).map(bytes -> {
|
| 271 | try {
|
| 272 | return Crypt.byteToPublicKey(bytes);
|
| 273 | } catch (CryptException e) {
|
| 274 | throw new DecoderException("Malformed public key bytes", e);
|
| 275 | }
|
| 276 | }, Key::getEncoded);
|
| 277 |
|
| 278 | static StreamCodec<ByteBuf, byte[]> byteArray(final int maxSize) {
|
| 279 | return new StreamCodec<ByteBuf, byte[]>() {
|
| 280 | public byte[] decode(final ByteBuf input) {
|
| 281 | return FriendlyByteBuf.readByteArray(input, maxSize);
|
| 282 | }
|
| 283 |
|
| 284 | public void encode(final ByteBuf output, final byte[] value) {
|
| 285 | if (value.length > maxSize) {
|
| 286 | throw new EncoderException("ByteArray with size " + value.length + " is bigger than allowed " + maxSize);
|
| 287 | }
|
| 288 |
|
| 289 | FriendlyByteBuf.writeByteArray(output, value);
|
| 290 | }
|
| 291 | };
|
| 292 | }
|
| 293 |
|
| 294 | static StreamCodec<ByteBuf, String> stringUtf8(final int maxStringLength) {
|
| 295 | return new StreamCodec<ByteBuf, String>() {
|
| 296 | public String decode(final ByteBuf input) {
|
| 297 | return Utf8String.read(input, maxStringLength);
|
| 298 | }
|
| 299 |
|
| 300 | public void encode(final ByteBuf output, final String value) {
|
| 301 | Utf8String.write(output, value, maxStringLength);
|
| 302 | }
|
| 303 | };
|
| 304 | }
|
| 305 |
|
| 306 | static StreamCodec<ByteBuf, BitSet> fixedBitSet(final int size) {
|
| 307 | return new StreamCodec<ByteBuf, BitSet>() {
|
| 308 | public BitSet decode(final ByteBuf input) {
|
| 309 | return FriendlyByteBuf.readFixedBitSet(input, size);
|
| 310 | }
|
| 311 |
|
| 312 | public void encode(final ByteBuf output, final BitSet value) {
|
| 313 | FriendlyByteBuf.writeFixedBitSet(output, value, size);
|
| 314 | }
|
| 315 | };
|
| 316 | }
|
| 317 |
|
| 318 | static StreamCodec<ByteBuf, Optional<Tag>> optionalTagCodec(final Supplier<NbtAccounter> accounter) {
|
| 319 | return new StreamCodec<ByteBuf, Optional<Tag>>() {
|
| 320 | public Optional<Tag> decode(final ByteBuf input) {
|
| 321 | return Optional.ofNullable(FriendlyByteBuf.readNbt(input, accounter.get()));
|
| 322 | }
|
| 323 |
|
| 324 | public void encode(final ByteBuf output, final Optional<Tag> value) {
|
| 325 | FriendlyByteBuf.writeNbt(output, value.orElse(null));
|
| 326 | }
|
| 327 | };
|
| 328 | }
|
| 329 |
|
| 330 | static StreamCodec<ByteBuf, Tag> tagCodec(final Supplier<NbtAccounter> accounter) {
|
| 331 | return new StreamCodec<ByteBuf, Tag>() {
|
| 332 | public Tag decode(final ByteBuf input) {
|
| 333 | Tag result = FriendlyByteBuf.readNbt(input, accounter.get());
|
| 334 | if (result == null) {
|
| 335 | throw new DecoderException("Expected non-null compound tag");
|
| 336 | } else {
|
| 337 | return result;
|
| 338 | }
|
| 339 | }
|
| 340 |
|
| 341 | public void encode(final ByteBuf output, final Tag value) {
|
| 342 | if (value == EndTag.INSTANCE) {
|
| 343 | throw new EncoderException("Expected non-null compound tag");
|
| 344 | }
|
| 345 |
|
| 346 | FriendlyByteBuf.writeNbt(output, value);
|
| 347 | }
|
| 348 | };
|
| 349 | }
|
| 350 |
|
| 351 | static StreamCodec<ByteBuf, CompoundTag> compoundTagCodec(final Supplier<NbtAccounter> accounter) {
|
| 352 | return tagCodec(accounter).map(tag -> {
|
| 353 | if (tag instanceof CompoundTag compoundTag) {
|
| 354 | return compoundTag;
|
| 355 | } else {
|
| 356 | throw new DecoderException("Not a compound tag: " + tag);
|
| 357 | }
|
| 358 | }, compoundTag -> (Tag)compoundTag);
|
| 359 | }
|
| 360 |
|
| 361 | static <T> StreamCodec<ByteBuf, T> fromCodecTrusted(final Codec<T> codec) {
|
| 362 | return fromCodec(codec, NbtAccounter::unlimitedHeap);
|
| 363 | }
|
| 364 |
|
| 365 | static <T> StreamCodec<ByteBuf, T> fromCodec(final Codec<T> codec) {
|
| 366 | return fromCodec(codec, NbtAccounter::defaultQuota);
|
| 367 | }
|
| 368 |
|
| 369 | static <T, B extends ByteBuf, V> StreamCodec.CodecOperation<B, T, V> fromCodec(final DynamicOps<T> ops, final Codec<V> codec) {
|
| 370 | return original -> new StreamCodec<B, V>() {
|
| 371 | public V decode(final B input) {
|
| 372 | T payload = (T)original.decode(input);
|
| 373 | return (V)codec.parse(ops, payload).getOrThrow(msg -> new DecoderException("Failed to decode: " + msg + " " + payload));
|
| 374 | }
|
| 375 |
|
| 376 | public void encode(final B output, final V value) {
|
| 377 | T payload = (T)codec.encodeStart(ops, value).getOrThrow(msg -> new EncoderException("Failed to encode: " + msg + " " + value));
|
| 378 | original.encode(output, payload);
|
| 379 | }
|
| 380 | };
|
| 381 | }
|
| 382 |
|
| 383 | static <T> StreamCodec<ByteBuf, T> fromCodec(final Codec<T> codec, final Supplier<NbtAccounter> accounter) {
|
| 384 | return tagCodec(accounter).apply(fromCodec(NbtOps.INSTANCE, codec));
|
| 385 | }
|
| 386 |
|
| 387 | static <T> StreamCodec<RegistryFriendlyByteBuf, T> fromCodecWithRegistriesTrusted(final Codec<T> codec) {
|
| 388 | return fromCodecWithRegistries(codec, NbtAccounter::unlimitedHeap);
|
| 389 | }
|
| 390 |
|
| 391 | static <T> StreamCodec<RegistryFriendlyByteBuf, T> fromCodecWithRegistries(final Codec<T> codec) {
|
| 392 | return fromCodecWithRegistries(codec, NbtAccounter::defaultQuota);
|
| 393 | }
|
| 394 |
|
| 395 | static <T> StreamCodec<RegistryFriendlyByteBuf, T> fromCodecWithRegistries(final Codec<T> codec, final Supplier<NbtAccounter> accounter) {
|
| 396 | final StreamCodec<ByteBuf, Tag> tagCodec = tagCodec(accounter);
|
| 397 | return new StreamCodec<RegistryFriendlyByteBuf, T>() {
|
| 398 | public T decode(final RegistryFriendlyByteBuf input) {
|
| 399 | Tag tag = tagCodec.decode(input);
|
| 400 | RegistryOps<Tag> ops = input.registryAccess().createSerializationContext(NbtOps.INSTANCE);
|
| 401 | return codec.parse(ops, tag).getOrThrow(msg -> new DecoderException("Failed to decode: " + msg + " " + tag));
|
| 402 | }
|
| 403 |
|
| 404 | public void encode(final RegistryFriendlyByteBuf output, final T value) {
|
| 405 | RegistryOps<Tag> ops = output.registryAccess().createSerializationContext(NbtOps.INSTANCE);
|
| 406 | Tag tag = codec.encodeStart(ops, value).getOrThrow(msg -> new EncoderException("Failed to encode: " + msg + " " + value));
|
| 407 | tagCodec.encode(output, tag);
|
| 408 | }
|
| 409 | };
|
| 410 | }
|
| 411 |
|
| 412 | static <B extends ByteBuf, V> StreamCodec<B, Optional<V>> optional(final StreamCodec<? super B, V> original) {
|
| 413 | return new StreamCodec<B, Optional<V>>() {
|
| 414 | public Optional<V> decode(final B input) {
|
| 415 | return input.readBoolean() ? Optional.of(original.decode(input)) : Optional.empty();
|
| 416 | }
|
| 417 |
|
| 418 | public void encode(final B output, final Optional<V> value) {
|
| 419 | if (value.isPresent()) {
|
| 420 | output.writeBoolean(true);
|
| 421 | original.encode(output, value.get());
|
| 422 | } else {
|
| 423 | output.writeBoolean(false);
|
| 424 | }
|
| 425 | }
|
| 426 | };
|
| 427 | }
|
| 428 |
|
| 429 | static int readCount(final ByteBuf input, final int maxSize) {
|
| 430 | int count = VarInt.read(input);
|
| 431 | if (count > maxSize) {
|
| 432 | throw new DecoderException(count + " elements exceeded max size of: " + maxSize);
|
| 433 | } else {
|
| 434 | return count;
|
| 435 | }
|
| 436 | }
|
| 437 |
|
| 438 | static void writeCount(final ByteBuf output, final int count, final int maxSize) {
|
| 439 | if (count > maxSize) {
|
| 440 | throw new EncoderException(count + " elements exceeded max size of: " + maxSize);
|
| 441 | }
|
| 442 |
|
| 443 | VarInt.write(output, count);
|
| 444 | }
|
| 445 |
|
| 446 | static <B extends ByteBuf, V, C extends Collection<V>> StreamCodec<B, C> collection(
|
| 447 | final IntFunction<C> constructor, final StreamCodec<? super B, V> elementCodec
|
| 448 | ) {
|
| 449 | return collection(constructor, elementCodec, Integer.MAX_VALUE);
|
| 450 | }
|
| 451 |
|
| 452 | static <B extends ByteBuf, V, C extends Collection<V>> StreamCodec<B, C> collection(
|
| 453 | final IntFunction<C> constructor, final StreamCodec<? super B, V> elementCodec, final int maxSize
|
| 454 | ) {
|
| 455 | return new StreamCodec<B, C>() {
|
| 456 | public C decode(final B input) {
|
| 457 | int count = ByteBufCodecs.readCount(input, maxSize);
|
| 458 | C result = constructor.apply(Math.min(count, 65536));
|
| 459 |
|
| 460 | for (int i = 0; i < count; i++) {
|
| 461 | result.add(elementCodec.decode(input));
|
| 462 | }
|
| 463 |
|
| 464 | return result;
|
| 465 | }
|
| 466 |
|
| 467 | public void encode(final B output, final C value) {
|
| 468 | ByteBufCodecs.writeCount(output, value.size(), maxSize);
|
| 469 |
|
| 470 | for (V element : value) {
|
| 471 | elementCodec.encode(output, element);
|
| 472 | }
|
| 473 | }
|
| 474 | };
|
| 475 | }
|
| 476 |
|
| 477 | static <B extends ByteBuf, V, C extends Collection<V>> StreamCodec.CodecOperation<B, V, C> collection(final IntFunction<C> constructor) {
|
| 478 | return original -> collection(constructor, original);
|
| 479 | }
|
| 480 |
|
| 481 | static <B extends ByteBuf, V, C extends Collection<V>> StreamCodec.CodecOperation<B, V, C> collection(final IntFunction<C> constructor, final int maxSize) {
|
| 482 | return original -> collection(constructor, original, maxSize);
|
| 483 | }
|
| 484 |
|
| 485 | static <B extends ByteBuf, V> StreamCodec.CodecOperation<B, V, List<V>> list() {
|
| 486 | return original -> collection(ArrayList::new, original);
|
| 487 | }
|
| 488 |
|
| 489 | static <B extends ByteBuf, V> StreamCodec.CodecOperation<B, V, List<V>> list(final int maxSize) {
|
| 490 | return original -> collection(ArrayList::new, original, maxSize);
|
| 491 | }
|
| 492 |
|
| 493 | static <B extends ByteBuf, V, C extends Collection<V>> StreamCodec<B, C> fixedSizeCollection(
|
| 494 | final IntFunction<C> constructor, final StreamCodec<? super B, V> elementCodec, final int size
|
| 495 | ) {
|
| 496 | return new StreamCodec<B, C>() {
|
| 497 | public C decode(final B input) {
|
| 498 | C result = constructor.apply(size);
|
| 499 |
|
| 500 | for (int i = 0; i < size; i++) {
|
| 501 | result.add(elementCodec.decode(input));
|
| 502 | }
|
| 503 |
|
| 504 | return result;
|
| 505 | }
|
| 506 |
|
| 507 | public void encode(final B output, final C value) {
|
| 508 | if (value.size() != size) {
|
| 509 | throw new EncoderException("Invalid list size, expected " + size + ", but got " + value.size());
|
| 510 | }
|
| 511 |
|
| 512 | Iterator<V> iterator = value.iterator();
|
| 513 |
|
| 514 | for (int i = 0; i < size; i++) {
|
| 515 | elementCodec.encode(output, iterator.next());
|
| 516 | }
|
| 517 | }
|
| 518 | };
|
| 519 | }
|
| 520 |
|
| 521 | static <B extends ByteBuf, V, C extends Collection<V>> StreamCodec.CodecOperation<B, V, C> fixedSizeCollection(
|
| 522 | final IntFunction<C> constructor, final int size
|
| 523 | ) {
|
| 524 | return original -> fixedSizeCollection(constructor, original, size);
|
| 525 | }
|
| 526 |
|
| 527 | static <B extends ByteBuf, V> StreamCodec.CodecOperation<B, V, List<V>> fixedSizeList(final int size) {
|
| 528 | return original -> fixedSizeCollection(ArrayList::new, original, size);
|
| 529 | }
|
| 530 |
|
| 531 | static <B extends ByteBuf, K, V, M extends Map<K, V>> StreamCodec<B, M> map(
|
| 532 | final IntFunction<? extends M> constructor, final StreamCodec<? super B, K> keyCodec, final StreamCodec<? super B, V> valueCodec
|
| 533 | ) {
|
| 534 | return map(constructor, keyCodec, valueCodec, Integer.MAX_VALUE);
|
| 535 | }
|
| 536 |
|
| 537 | static <B extends ByteBuf, K, V, M extends Map<K, V>> StreamCodec<B, M> map(
|
| 538 | final IntFunction<? extends M> constructor, final StreamCodec<? super B, K> keyCodec, final StreamCodec<? super B, V> valueCodec, final int maxSize
|
| 539 | ) {
|
| 540 | return new StreamCodec<B, M>() {
|
| 541 | public void encode(final B output, final M map) {
|
| 542 | ByteBufCodecs.writeCount(output, map.size(), maxSize);
|
| 543 | map.forEach((k, v) -> {
|
| 544 | keyCodec.encode(output, (K)k);
|
| 545 | valueCodec.encode(output, (V)v);
|
| 546 | });
|
| 547 | }
|
| 548 |
|
| 549 | public M decode(final B input) {
|
| 550 | int count = ByteBufCodecs.readCount(input, maxSize);
|
| 551 | M result = (M)constructor.apply(Math.min(count, 65536));
|
| 552 |
|
| 553 | for (int i = 0; i < count; i++) {
|
| 554 | K key = keyCodec.decode(input);
|
| 555 | V value = valueCodec.decode(input);
|
| 556 | result.put(key, value);
|
| 557 | }
|
| 558 |
|
| 559 | return result;
|
| 560 | }
|
| 561 | };
|
| 562 | }
|
| 563 |
|
| 564 | static <B extends ByteBuf, L, R> StreamCodec<B, Either<L, R>> either(final StreamCodec<? super B, L> leftCodec, final StreamCodec<? super B, R> rightCodec) {
|
| 565 | return new StreamCodec<B, Either<L, R>>() {
|
| 566 | public Either<L, R> decode(final B input) {
|
| 567 | return input.readBoolean() ? Either.left(leftCodec.decode(input)) : Either.right(rightCodec.decode(input));
|
| 568 | }
|
| 569 |
|
| 570 | public void encode(final B output, final Either<L, R> value) {
|
| 571 | value.ifLeft(left -> {
|
| 572 | output.writeBoolean(true);
|
| 573 | leftCodec.encode(output, (L)left);
|
| 574 | }).ifRight(right -> {
|
| 575 | output.writeBoolean(false);
|
| 576 | rightCodec.encode(output, (R)right);
|
| 577 | });
|
| 578 | }
|
| 579 | };
|
| 580 | }
|
| 581 |
|
| 582 | static <B extends ByteBuf, V> StreamCodec.CodecOperation<B, V, V> lengthPrefixed(final int maxSize, final BiFunction<B, ByteBuf, B> decorator) {
|
| 583 | return original -> new StreamCodec<B, V>() {
|
| 584 | public V decode(final B input) {
|
| 585 | int size = VarInt.read(input);
|
| 586 | if (size > maxSize) {
|
| 587 | throw new DecoderException("Buffer size " + size + " is larger than allowed limit of " + maxSize);
|
| 588 | }
|
| 589 |
|
| 590 | int index = input.readerIndex();
|
| 591 | B limitedSlice = (B)((ByteBuf)decorator.apply(input, input.slice(index, size)));
|
| 592 | input.readerIndex(index + size);
|
| 593 | return (V)original.decode(limitedSlice);
|
| 594 | }
|
| 595 |
|
| 596 | public void encode(final B output, final V value) {
|
| 597 | B scratchBuffer = (B)((ByteBuf)decorator.apply(output, output.alloc().buffer()));
|
| 598 |
|
| 599 | try {
|
| 600 | original.encode(scratchBuffer, value);
|
| 601 | int size = scratchBuffer.readableBytes();
|
| 602 | if (size > maxSize) {
|
| 603 | throw new EncoderException("Buffer size " + size + " is larger than allowed limit of " + maxSize);
|
| 604 | }
|
| 605 |
|
| 606 | VarInt.write(output, size);
|
| 607 | output.writeBytes(scratchBuffer);
|
| 608 | } finally {
|
| 609 | scratchBuffer.release();
|
| 610 | }
|
| 611 | }
|
| 612 | };
|
| 613 | }
|
| 614 |
|
| 615 | static <V> StreamCodec.CodecOperation<ByteBuf, V, V> lengthPrefixed(final int maxSize) {
|
| 616 | return lengthPrefixed(maxSize, (parent, child) -> child);
|
| 617 | }
|
| 618 |
|
| 619 | static <V> StreamCodec.CodecOperation<RegistryFriendlyByteBuf, V, V> registryFriendlyLengthPrefixed(final int maxSize) {
|
| 620 | return lengthPrefixed(maxSize, (parent, child) -> new RegistryFriendlyByteBuf(child, parent.registryAccess()));
|
| 621 | }
|
| 622 |
|
| 623 | static <T> StreamCodec<ByteBuf, T> idMapper(final IntFunction<T> byId, final ToIntFunction<T> toId) {
|
| 624 | return new StreamCodec<ByteBuf, T>() {
|
| 625 | public T decode(final ByteBuf input) {
|
| 626 | int id = VarInt.read(input);
|
| 627 | return byId.apply(id);
|
| 628 | }
|
| 629 |
|
| 630 | public void encode(final ByteBuf output, final T value) {
|
| 631 | int id = toId.applyAsInt(value);
|
| 632 | VarInt.write(output, id);
|
| 633 | }
|
| 634 | };
|
| 635 | }
|
| 636 |
|
| 637 | static <T> StreamCodec<ByteBuf, T> idMapper(final IdMap<T> mapper) {
|
| 638 | return idMapper(mapper::byIdOrThrow, mapper::getIdOrThrow);
|
| 639 | }
|
| 640 |
|
| 641 | private static <T, R> StreamCodec<RegistryFriendlyByteBuf, R> registry(
|
| 642 | final ResourceKey<? extends Registry<T>> registryKey, final Function<Registry<T>, IdMap<R>> mapExtractor
|
| 643 | ) {
|
| 644 | return new StreamCodec<RegistryFriendlyByteBuf, R>() {
|
| 645 | private IdMap<R> getRegistryOrThrow(final RegistryFriendlyByteBuf input) {
|
| 646 | return mapExtractor.apply(input.registryAccess().lookupOrThrow(registryKey));
|
| 647 | }
|
| 648 |
|
| 649 | public R decode(final RegistryFriendlyByteBuf input) {
|
| 650 | int id = VarInt.read(input);
|
| 651 | return (R)this.getRegistryOrThrow(input).byIdOrThrow(id);
|
| 652 | }
|
| 653 |
|
| 654 | public void encode(final RegistryFriendlyByteBuf output, final R value) {
|
| 655 | int id = this.getRegistryOrThrow(output).getIdOrThrow(value);
|
| 656 | VarInt.write(output, id);
|
| 657 | }
|
| 658 | };
|
| 659 | }
|
| 660 |
|
| 661 | static <T> StreamCodec<RegistryFriendlyByteBuf, T> registry(final ResourceKey<? extends Registry<T>> registryKey) {
|
| 662 | return registry(registryKey, r -> r);
|
| 663 | }
|
| 664 |
|
| 665 | static <T> StreamCodec<RegistryFriendlyByteBuf, Holder<T>> holderRegistry(final ResourceKey<? extends Registry<T>> registryKey) {
|
| 666 | return registry(registryKey, Registry::asHolderIdMap);
|
| 667 | }
|
| 668 |
|
| 669 | static <T> StreamCodec<RegistryFriendlyByteBuf, Holder<T>> holder(
|
| 670 | final ResourceKey<? extends Registry<T>> registryKey, final StreamCodec<? super RegistryFriendlyByteBuf, T> directCodec
|
| 671 | ) {
|
| 672 | return new StreamCodec<RegistryFriendlyByteBuf, Holder<T>>() {
|
| 673 | private static final int DIRECT_HOLDER_ID = 0;
|
| 674 |
|
| 675 | private IdMap<Holder<T>> getRegistryOrThrow(final RegistryFriendlyByteBuf input) {
|
| 676 | return input.registryAccess().lookupOrThrow(registryKey).asHolderIdMap();
|
| 677 | }
|
| 678 |
|
| 679 | public Holder<T> decode(final RegistryFriendlyByteBuf input) {
|
| 680 | int id = VarInt.read(input);
|
| 681 | return id == 0 ? Holder.direct(directCodec.decode(input)) : (Holder)this.getRegistryOrThrow(input).byIdOrThrow(id - 1);
|
| 682 | }
|
| 683 |
|
| 684 | public void encode(final RegistryFriendlyByteBuf output, final Holder<T> holder) {
|
| 685 | switch (holder.kind()) {
|
| 686 | case REFERENCE:
|
| 687 | int id = this.getRegistryOrThrow(output).getIdOrThrow(holder);
|
| 688 | VarInt.write(output, id + 1);
|
| 689 | break;
|
| 690 | case DIRECT:
|
| 691 | VarInt.write(output, 0);
|
| 692 | directCodec.encode(output, holder.value());
|
| 693 | }
|
| 694 | }
|
| 695 | };
|
| 696 | }
|
| 697 |
|
| 698 | static <T> StreamCodec<RegistryFriendlyByteBuf, HolderSet<T>> holderSet(final ResourceKey<? extends Registry<T>> registryKey) {
|
| 699 | return new StreamCodec<RegistryFriendlyByteBuf, HolderSet<T>>() {
|
| 700 | private static final int NAMED_SET = -1;
|
| 701 | private final StreamCodec<RegistryFriendlyByteBuf, Holder<T>> holderCodec = ByteBufCodecs.holderRegistry(registryKey);
|
| 702 |
|
| 703 | public HolderSet<T> decode(final RegistryFriendlyByteBuf input) {
|
| 704 | int count = VarInt.read(input) - 1;
|
| 705 | if (count == -1) {
|
| 706 | Registry<T> registry = input.registryAccess().lookupOrThrow(registryKey);
|
| 707 | return registry.get(TagKey.create(registryKey, Identifier.STREAM_CODEC.decode(input))).orElseThrow();
|
| 708 | }
|
| 709 |
|
| 710 | List<Holder<T>> holders = new ArrayList<>(Math.min(count, 65536));
|
| 711 |
|
| 712 | for (int i = 0; i < count; i++) {
|
| 713 | holders.add(this.holderCodec.decode(input));
|
| 714 | }
|
| 715 |
|
| 716 | return HolderSet.direct(holders);
|
| 717 | }
|
| 718 |
|
| 719 | public void encode(final RegistryFriendlyByteBuf output, final HolderSet<T> value) {
|
| 720 | Optional<TagKey<T>> key = value.unwrapKey();
|
| 721 | if (key.isPresent()) {
|
| 722 | VarInt.write(output, 0);
|
| 723 | Identifier.STREAM_CODEC.encode(output, key.get().location());
|
| 724 | } else {
|
| 725 | VarInt.write(output, value.size() + 1);
|
| 726 |
|
| 727 | for (Holder<T> holder : value) {
|
| 728 | this.holderCodec.encode(output, holder);
|
| 729 | }
|
| 730 | }
|
| 731 | }
|
| 732 | };
|
| 733 | }
|
| 734 |
|
| 735 | static StreamCodec<ByteBuf, JsonElement> lenientJson(final int maxStringLength) {
|
| 736 | return new StreamCodec<ByteBuf, JsonElement>() {
|
| 737 | private static final Gson GSON = new GsonBuilder().disableHtmlEscaping().create();
|
| 738 |
|
| 739 | public JsonElement decode(final ByteBuf input) {
|
| 740 | String payload = Utf8String.read(input, maxStringLength);
|
| 741 |
|
| 742 | try {
|
| 743 | return LenientJsonParser.parse(payload);
|
| 744 | } catch (JsonSyntaxException e) {
|
| 745 | throw new DecoderException("Failed to parse JSON", e);
|
| 746 | }
|
| 747 | }
|
| 748 |
|
| 749 | public void encode(final ByteBuf output, final JsonElement value) {
|
| 750 | String payload = GSON.toJson(value);
|
| 751 | Utf8String.write(output, payload, maxStringLength);
|
| 752 | }
|
| 753 | };
|
| 754 | }
|
| 755 | }
|
| 756 |
|