1
0
Fork 0
mirror of https://github.com/PaperMC/Paper.git synced 2025-04-28 22:55:09 +02:00

Track codec writing

This commit is contained in:
Nassim Jahnke 2025-02-25 21:44:51 +01:00
parent 9be4e07a3e
commit f12d33f04e
No known key found for this signature in database
GPG key ID: EF6771C01F6EF02F
9 changed files with 146 additions and 2 deletions

View file

@ -1,6 +1,6 @@
--- a/net/minecraft/network/FriendlyByteBuf.java
+++ b/net/minecraft/network/FriendlyByteBuf.java
@@ -70,6 +_,7 @@
@@ -70,14 +_,20 @@
public class FriendlyByteBuf extends ByteBuf {
public static final int DEFAULT_NBT_QUOTA = 2097152;
private final ByteBuf source;
@ -8,8 +8,13 @@
public static final short MAX_STRING_LENGTH = 32767;
public static final int MAX_COMPONENT_STRING_LENGTH = 262144;
private static final int PUBLIC_KEY_SIZE = 256;
@@ -78,6 +_,7 @@
private static final int MAX_PUBLIC_KEY_HEADER_SIZE = 256;
private static final int MAX_PUBLIC_KEY_LENGTH = 512;
private static final Gson GSON = new Gson();
+ // Paper start - Track codec depth
+ public boolean trackCodecDepth;
+ public byte codecDepth;
+ // Paper end - Track codec depth
public FriendlyByteBuf(ByteBuf source) {
+ this.adventure$locale = PacketEncoder.ADVENTURE_LOCALE.get(); // Paper - track player's locale for server-side translations

View file

@ -0,0 +1,51 @@
--- a/net/minecraft/network/codec/ByteBufCodecs.java
+++ b/net/minecraft/network/codec/ByteBufCodecs.java
@@ -378,6 +_,48 @@
};
}
+ // Paper start - Track codec depth
+ static <B extends FriendlyByteBuf, V> StreamCodec<B, V> trackDepth(final StreamCodec<B, V> codec) {
+ return new StreamCodec<>() {
+ @Override
+ public V decode(B buffer) {
+ buffer.trackCodecDepth = true;
+ try {
+ return codec.decode(buffer);
+ } finally {
+ buffer.trackCodecDepth = false;
+ buffer.codecDepth = 0;
+ }
+ }
+
+ @Override
+ public void encode(B buffer, V value) {
+ codec.encode(buffer, value);
+ }
+ };
+ }
+
+ static <B extends FriendlyByteBuf, V> StreamCodec<B, V> increaseDepth(final StreamCodec<B, V> codec) {
+ return new StreamCodec<>() {
+ @Override
+ public V decode(B buffer) {
+ if (!buffer.trackCodecDepth) {
+ return codec.decode(buffer);
+ }
+ if (++buffer.codecDepth > 64) {
+ throw new DecoderException("Too deep");
+ }
+ return codec.decode(buffer);
+ }
+
+ @Override
+ public void encode(B buffer, V value) {
+ codec.encode(buffer, value);
+ }
+ };
+ }
+ // Paper end - Track codec depth
+
static <B extends ByteBuf, V> StreamCodec<B, Optional<V>> optional(final StreamCodec<B, V> codec) {
return new StreamCodec<B, Optional<V>>() {
@Override

View file

@ -0,0 +1,20 @@
--- a/net/minecraft/network/protocol/game/ServerboundContainerClickPacket.java
+++ b/net/minecraft/network/protocol/game/ServerboundContainerClickPacket.java
@@ -17,7 +_,7 @@
);
private static final int MAX_SLOT_COUNT = 128;
private static final StreamCodec<RegistryFriendlyByteBuf, Int2ObjectMap<ItemStack>> SLOTS_STREAM_CODEC = ByteBufCodecs.map(
- Int2ObjectOpenHashMap::new, ByteBufCodecs.SHORT.map(Short::intValue, Integer::shortValue), ItemStack.OPTIONAL_STREAM_CODEC, 128
+ Int2ObjectOpenHashMap::new, ByteBufCodecs.SHORT.map(Short::intValue, Integer::shortValue), ItemStack.OPTIONAL_STREAM_CODEC.apply(ByteBufCodecs::trackDepth), 128 // Paper - Track codec depth
);
private final int containerId;
private final int stateId;
@@ -46,7 +_,7 @@
this.buttonNum = buffer.readByte();
this.clickType = buffer.readEnum(ClickType.class);
this.changedSlots = Int2ObjectMaps.unmodifiable(SLOTS_STREAM_CODEC.decode(buffer));
- this.carriedItem = ItemStack.OPTIONAL_STREAM_CODEC.decode(buffer);
+ this.carriedItem = ItemStack.OPTIONAL_STREAM_CODEC.apply(ByteBufCodecs::trackDepth).decode(buffer); // Paper - Track codec depth
}
private void write(RegistryFriendlyByteBuf buffer) {

View file

@ -0,0 +1,11 @@
--- a/net/minecraft/network/protocol/game/ServerboundSetCreativeModeSlotPacket.java
+++ b/net/minecraft/network/protocol/game/ServerboundSetCreativeModeSlotPacket.java
@@ -11,7 +_,7 @@
public static final StreamCodec<RegistryFriendlyByteBuf, ServerboundSetCreativeModeSlotPacket> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.SHORT,
ServerboundSetCreativeModeSlotPacket::slotNum,
- ItemStack.validatedStreamCodec(ItemStack.OPTIONAL_STREAM_CODEC),
+ ItemStack.validatedStreamCodec(ItemStack.OPTIONAL_STREAM_CODEC).apply(ByteBufCodecs::trackDepth), // Paper - Track codec depth
ServerboundSetCreativeModeSlotPacket::itemStack,
ServerboundSetCreativeModeSlotPacket::new
);

View file

@ -8,3 +8,21 @@
&& !this.effect.value().applyEffectTick(serverLevel, entity, this.amplifier)) {
entity.removeEffect(this.effect);
}
@@ -408,7 +_,7 @@
.apply(instance, MobEffectInstance.Details::create)
)
);
- public static final StreamCodec<ByteBuf, MobEffectInstance.Details> STREAM_CODEC = StreamCodec.recursive(
+ public static final StreamCodec<net.minecraft.network.FriendlyByteBuf, MobEffectInstance.Details> STREAM_CODEC = StreamCodec.recursive( // Paper - Track codec depth
codec -> StreamCodec.composite(
ByteBufCodecs.VAR_INT,
MobEffectInstance.Details::amplifier,
@@ -420,7 +_,7 @@
MobEffectInstance.Details::showParticles,
ByteBufCodecs.BOOL,
MobEffectInstance.Details::showIcon,
- codec.apply(ByteBufCodecs::optional),
+ codec.apply(ByteBufCodecs::increaseDepth).apply(ByteBufCodecs::optional), // Paper - Track codec depth
MobEffectInstance.Details::hiddenEffect,
MobEffectInstance.Details::new
)

View file

@ -1,5 +1,13 @@
--- a/net/minecraft/world/item/component/BundleContents.java
+++ b/net/minecraft/world/item/component/BundleContents.java
@@ -25,6 +_,7 @@
.flatXmap(BundleContents::checkAndCreate, bundleContents -> DataResult.success(bundleContents.items));
public static final StreamCodec<RegistryFriendlyByteBuf, BundleContents> STREAM_CODEC = ItemStack.STREAM_CODEC
.apply(ByteBufCodecs.list())
+ .apply(ByteBufCodecs::increaseDepth) // Paper - Track codec depth
.map(BundleContents::new, contents -> contents.items);
private static final Fraction BUNDLE_IN_BUNDLE_WEIGHT = Fraction.getFraction(1, 16);
private static final int NO_STACK_INDEX = -1;
@@ -76,6 +_,12 @@
return !stack.isEmpty() && stack.getItem().canFitInsideContainerItems();
}

View file

@ -0,0 +1,10 @@
--- a/net/minecraft/world/item/component/ChargedProjectiles.java
+++ b/net/minecraft/world/item/component/ChargedProjectiles.java
@@ -16,6 +_,7 @@
.xmap(ChargedProjectiles::new, chargedProjectiles -> chargedProjectiles.items);
public static final StreamCodec<RegistryFriendlyByteBuf, ChargedProjectiles> STREAM_CODEC = ItemStack.STREAM_CODEC
.apply(ByteBufCodecs.list())
+ .apply(ByteBufCodecs::increaseDepth) // Paper - Track codec depth
.map(ChargedProjectiles::new, chargedProjectiles -> chargedProjectiles.items);
private final List<ItemStack> items;

View file

@ -0,0 +1,10 @@
--- a/net/minecraft/world/item/component/ItemContainerContents.java
+++ b/net/minecraft/world/item/component/ItemContainerContents.java
@@ -22,6 +_,7 @@
.xmap(ItemContainerContents::fromSlots, ItemContainerContents::asSlots);
public static final StreamCodec<RegistryFriendlyByteBuf, ItemContainerContents> STREAM_CODEC = ItemStack.OPTIONAL_STREAM_CODEC
.apply(ByteBufCodecs.list(256))
+ .apply(ByteBufCodecs::increaseDepth) // Paper - Track codec depth
.map(ItemContainerContents::new, contents -> contents.items);
public final NonNullList<ItemStack> items;
private final int hashCode;

View file

@ -0,0 +1,11 @@
--- a/net/minecraft/world/item/component/UseRemainder.java
+++ b/net/minecraft/world/item/component/UseRemainder.java
@@ -8,7 +_,7 @@
public record UseRemainder(ItemStack convertInto) {
public static final Codec<UseRemainder> CODEC = ItemStack.CODEC.xmap(UseRemainder::new, UseRemainder::convertInto);
public static final StreamCodec<RegistryFriendlyByteBuf, UseRemainder> STREAM_CODEC = StreamCodec.composite(
- ItemStack.STREAM_CODEC, UseRemainder::convertInto, UseRemainder::new
+ ItemStack.STREAM_CODEC.apply(net.minecraft.network.codec.ByteBufCodecs::increaseDepth), UseRemainder::convertInto, UseRemainder::new // Paper - Track codec depth
);
public ItemStack convertIntoRemainder(ItemStack stack, int count, boolean hasInfiniteMaterials, UseRemainder.OnExtraCreatedRemainder onExtraCreated) {