From 133ed1f09d3951c32934d92fb32ddc6d7e6fce88 Mon Sep 17 00:00:00 2001 From: yueh Date: Mon, 31 Aug 2020 23:09:28 +0200 Subject: [PATCH 1/3] Remove GZipStreams from packets. (#4664) This should now be handled by the normal network manager (hopefully). Also renamed the `CompressedNBTPacket` to `MEInterfaceUpdatePacket` as that is the only use case. --- .../InterfaceTerminalContainer.java | 4 +- .../appeng/core/sync/BasePacketHandler.java | 4 +- .../packets/MEFluidInventoryUpdatePacket.java | 63 ++----------------- ...cket.java => MEInterfaceUpdatePacket.java} | 50 ++------------- .../sync/packets/MEInventoryUpdatePacket.java | 62 ++---------------- 5 files changed, 20 insertions(+), 163 deletions(-) rename src/main/java/appeng/core/sync/packets/{CompressedNBTPacket.java => MEInterfaceUpdatePacket.java} (57%) diff --git a/src/main/java/appeng/container/implementations/InterfaceTerminalContainer.java b/src/main/java/appeng/container/implementations/InterfaceTerminalContainer.java index cc893c57b..648262f0b 100644 --- a/src/main/java/appeng/container/implementations/InterfaceTerminalContainer.java +++ b/src/main/java/appeng/container/implementations/InterfaceTerminalContainer.java @@ -42,7 +42,7 @@ import appeng.api.networking.security.IActionHost; import appeng.container.AEBaseContainer; import appeng.container.ContainerLocator; import appeng.core.sync.network.NetworkHandler; -import appeng.core.sync.packets.CompressedNBTPacket; +import appeng.core.sync.packets.MEInterfaceUpdatePacket; import appeng.helpers.DualityInterface; import appeng.helpers.IInterfaceHost; import appeng.helpers.InventoryAction; @@ -177,7 +177,7 @@ public final class InterfaceTerminalContainer extends AEBaseContainer { if (!this.data.isEmpty()) { try { - NetworkHandler.instance().sendTo(new CompressedNBTPacket(this.data), + NetworkHandler.instance().sendTo(new MEInterfaceUpdatePacket(this.data), (ServerPlayerEntity) this.getPlayerInv().player); } catch (final IOException e) { // :P diff --git a/src/main/java/appeng/core/sync/BasePacketHandler.java b/src/main/java/appeng/core/sync/BasePacketHandler.java index 82c39a3a0..541712fce 100644 --- a/src/main/java/appeng/core/sync/BasePacketHandler.java +++ b/src/main/java/appeng/core/sync/BasePacketHandler.java @@ -29,7 +29,6 @@ import appeng.core.sync.packets.BlockTransitionEffectPacket; import appeng.core.sync.packets.ClickPacket; import appeng.core.sync.packets.CompassRequestPacket; import appeng.core.sync.packets.CompassResponsePacket; -import appeng.core.sync.packets.CompressedNBTPacket; import appeng.core.sync.packets.ConfigButtonPacket; import appeng.core.sync.packets.ConfigValuePacket; import appeng.core.sync.packets.CraftRequestPacket; @@ -39,6 +38,7 @@ import appeng.core.sync.packets.ItemTransitionEffectPacket; import appeng.core.sync.packets.JEIRecipePacket; import appeng.core.sync.packets.LightningPacket; import appeng.core.sync.packets.MEFluidInventoryUpdatePacket; +import appeng.core.sync.packets.MEInterfaceUpdatePacket; import appeng.core.sync.packets.MEInventoryUpdatePacket; import appeng.core.sync.packets.MatterCannonPacket; import appeng.core.sync.packets.MockExplosionPacket; @@ -101,7 +101,7 @@ public class BasePacketHandler { PACKET_ASSEMBLER_ANIMATION(AssemblerAnimationPacket.class, AssemblerAnimationPacket::new), - PACKET_COMPRESSED_NBT(CompressedNBTPacket.class, CompressedNBTPacket::new), + PACKET_ME_INTERFACE_UPDATE(MEInterfaceUpdatePacket.class, MEInterfaceUpdatePacket::new), PACKET_PAINTED_ENTITY(PaintedEntityPacket.class, PaintedEntityPacket::new), diff --git a/src/main/java/appeng/core/sync/packets/MEFluidInventoryUpdatePacket.java b/src/main/java/appeng/core/sync/packets/MEFluidInventoryUpdatePacket.java index 10caeebd1..1f79b1af4 100644 --- a/src/main/java/appeng/core/sync/packets/MEFluidInventoryUpdatePacket.java +++ b/src/main/java/appeng/core/sync/packets/MEFluidInventoryUpdatePacket.java @@ -19,13 +19,9 @@ package appeng.core.sync.packets; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; import java.nio.BufferOverflowException; import java.util.LinkedList; import java.util.List; -import java.util.zip.GZIPInputStream; -import java.util.zip.GZIPOutputStream; import javax.annotation.Nullable; @@ -41,7 +37,6 @@ import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.fml.network.NetworkDirection; import appeng.api.storage.data.IAEFluidStack; -import appeng.core.AELog; import appeng.core.sync.BasePacket; import appeng.core.sync.network.INetworkInfo; import appeng.fluids.client.gui.FluidTerminalScreen; @@ -55,8 +50,6 @@ import appeng.fluids.util.AEFluidStack; public class MEFluidInventoryUpdatePacket extends BasePacket { private static final int UNCOMPRESSED_PACKET_BYTE_LIMIT = 16 * 1024 * 1024; private static final int OPERATION_BYTE_LIMIT = 2 * 1024; - private static final int TEMP_BUFFER_SIZE = 1024; - private static final int STREAM_MASK = 0xff; // input. @Nullable @@ -66,47 +59,18 @@ public class MEFluidInventoryUpdatePacket extends BasePacket { @Nullable private final PacketBuffer data; - @Nullable - private final GZIPOutputStream compressFrame; private int writtenBytes = 0; private boolean empty = true; public MEFluidInventoryUpdatePacket(final PacketBuffer stream) { this.data = null; - this.compressFrame = null; this.list = new LinkedList<>(); this.ref = stream.readByte(); - try (final GZIPInputStream gzReader = new GZIPInputStream(new InputStream() { - @Override - public int read() { - if (stream.readableBytes() <= 0) { - return -1; - } - - return stream.readByte() & STREAM_MASK; - } - })) { - - final PacketBuffer uncompressed = new PacketBuffer(Unpooled.buffer(stream.readableBytes())); - final byte[] tmp = new byte[TEMP_BUFFER_SIZE]; - - while (gzReader.available() != 0) { - final int bytes = gzReader.read(tmp); - - if (bytes > 0) { - uncompressed.writeBytes(tmp, 0, bytes); - } - } - - while (uncompressed.readableBytes() > 0) { - this.list.add(AEFluidStack.fromPacket(uncompressed)); - } - } catch (IOException e) { - throw new RuntimeException("Failed to decompress packet.", e); + while (stream.readableBytes() > 0) { + this.list.add(AEFluidStack.fromPacket(stream)); } - this.empty = this.list.isEmpty(); } @@ -121,14 +85,6 @@ public class MEFluidInventoryUpdatePacket extends BasePacket { this.data = new PacketBuffer(Unpooled.buffer(OPERATION_BYTE_LIMIT)); this.data.writeInt(this.getPacketID()); this.data.writeByte(this.ref); - - this.compressFrame = new GZIPOutputStream(new OutputStream() { - @Override - public void write(final int value) { - MEFluidInventoryUpdatePacket.this.data.writeByte(value); - } - }); - this.list = null; } @@ -145,28 +101,19 @@ public class MEFluidInventoryUpdatePacket extends BasePacket { @Nullable @Override public IPacket toPacket(NetworkDirection direction) { - try { - this.compressFrame.close(); - - this.configureWrite(this.data); - return super.toPacket(direction); - } catch (final IOException e) { - AELog.debug(e); - } - - return null; + this.configureWrite(this.data); + return super.toPacket(direction); } public void appendFluid(final IAEFluidStack fs) throws IOException, BufferOverflowException { final PacketBuffer tmp = new PacketBuffer(Unpooled.buffer(OPERATION_BYTE_LIMIT)); fs.writeToPacket(tmp); - this.compressFrame.flush(); if (this.writtenBytes + tmp.readableBytes() > UNCOMPRESSED_PACKET_BYTE_LIMIT) { throw new BufferOverflowException(); } else { this.writtenBytes += tmp.readableBytes(); - this.compressFrame.write(tmp.array(), 0, tmp.readableBytes()); + this.data.writeBytes(tmp.array(), 0, tmp.readableBytes()); this.empty = false; } } diff --git a/src/main/java/appeng/core/sync/packets/CompressedNBTPacket.java b/src/main/java/appeng/core/sync/packets/MEInterfaceUpdatePacket.java similarity index 57% rename from src/main/java/appeng/core/sync/packets/CompressedNBTPacket.java rename to src/main/java/appeng/core/sync/packets/MEInterfaceUpdatePacket.java index 42dedfa75..f15023710 100644 --- a/src/main/java/appeng/core/sync/packets/CompressedNBTPacket.java +++ b/src/main/java/appeng/core/sync/packets/MEInterfaceUpdatePacket.java @@ -18,13 +18,7 @@ package appeng.core.sync.packets; -import java.io.DataInputStream; -import java.io.DataOutputStream; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.zip.GZIPInputStream; -import java.util.zip.GZIPOutputStream; import io.netty.buffer.Unpooled; @@ -32,7 +26,6 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.gui.screen.Screen; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.nbt.CompoundNBT; -import net.minecraft.nbt.CompressedStreamTools; import net.minecraft.network.PacketBuffer; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; @@ -41,55 +34,24 @@ import appeng.client.gui.implementations.InterfaceTerminalScreen; import appeng.core.sync.BasePacket; import appeng.core.sync.network.INetworkInfo; -//TODO, this is pointless, NBT is already compressed when written to a PacketBuffer. -public class CompressedNBTPacket extends BasePacket { +public class MEInterfaceUpdatePacket extends BasePacket { // input. private final CompoundNBT in; // output... private final PacketBuffer data; - private final GZIPOutputStream compressFrame; - public CompressedNBTPacket(final PacketBuffer stream) { + public MEInterfaceUpdatePacket(final PacketBuffer stream) { this.data = null; - this.compressFrame = null; - - try (DataInputStream inStream = new DataInputStream(new GZIPInputStream(new InputStream() { - - @Override - public int read() { - if (stream.readableBytes() <= 0) { - return -1; - } - - return stream.readByte() & 0xff; - } - }))) { - this.in = CompressedStreamTools.read(inStream); - } catch (IOException e) { - throw new RuntimeException("Failed to decompress packet.", e); - } + this.in = stream.readCompoundTag(); } // api - public CompressedNBTPacket(final CompoundNBT din) throws IOException { - + public MEInterfaceUpdatePacket(final CompoundNBT din) throws IOException { + this.in = null; this.data = new PacketBuffer(Unpooled.buffer(2048)); this.data.writeInt(this.getPacketID()); - - this.in = din; - - this.compressFrame = new GZIPOutputStream(new OutputStream() { - - @Override - public void write(final int value) { - CompressedNBTPacket.this.data.writeByte(value); - } - }); - - CompressedStreamTools.write(din, new DataOutputStream(this.compressFrame)); - this.compressFrame.close(); - + this.data.writeCompoundTag(din); this.configureWrite(this.data); } diff --git a/src/main/java/appeng/core/sync/packets/MEInventoryUpdatePacket.java b/src/main/java/appeng/core/sync/packets/MEInventoryUpdatePacket.java index 7fac77538..4ec3f2289 100644 --- a/src/main/java/appeng/core/sync/packets/MEInventoryUpdatePacket.java +++ b/src/main/java/appeng/core/sync/packets/MEInventoryUpdatePacket.java @@ -19,12 +19,10 @@ package appeng.core.sync.packets; import java.io.IOException; -import java.io.InputStream; import java.io.OutputStream; import java.nio.BufferOverflowException; import java.util.ArrayList; import java.util.List; -import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; import javax.annotation.Nullable; @@ -45,7 +43,6 @@ import appeng.client.gui.implementations.CraftConfirmScreen; import appeng.client.gui.implementations.CraftingCPUScreen; import appeng.client.gui.implementations.MEMonitorableScreen; import appeng.client.gui.implementations.NetworkStatusScreen; -import appeng.core.AELog; import appeng.core.sync.BasePacket; import appeng.core.sync.network.INetworkInfo; import appeng.util.item.AEItemStack; @@ -53,8 +50,6 @@ import appeng.util.item.AEItemStack; public class MEInventoryUpdatePacket extends BasePacket { private static final int UNCOMPRESSED_PACKET_BYTE_LIMIT = 16 * 1024 * 1024; private static final int OPERATION_BYTE_LIMIT = 2 * 1024; - private static final int TEMP_BUFFER_SIZE = 1024; - private static final int STREAM_MASK = 0xff; // input. @Nullable @@ -64,48 +59,18 @@ public class MEInventoryUpdatePacket extends BasePacket { @Nullable private final PacketBuffer data; - @Nullable - private final GZIPOutputStream compressFrame; private int writtenBytes = 0; private boolean empty = true; public MEInventoryUpdatePacket(final PacketBuffer stream) { this.data = null; - this.compressFrame = null; this.list = new ArrayList<>(); this.ref = stream.readByte(); - // int originalBytes = stream.readableBytes(); - - try (GZIPInputStream gzReader = new GZIPInputStream(new InputStream() { - @Override - public int read() { - if (stream.readableBytes() <= 0) { - return -1; - } - - return stream.readByte() & STREAM_MASK; - } - })) { - final PacketBuffer uncompressed = new PacketBuffer(Unpooled.buffer(stream.readableBytes())); - final byte[] tmp = new byte[TEMP_BUFFER_SIZE]; - - while (gzReader.available() != 0) { - final int bytes = gzReader.read(tmp); - - if (bytes > 0) { - uncompressed.writeBytes(tmp, 0, bytes); - } - } - - while (uncompressed.readableBytes() > 0) { - this.list.add(AEItemStack.fromPacket(uncompressed)); - } - } catch (IOException e) { - throw new RuntimeException("Failed to decompress packet.", e); + while (stream.readableBytes() > 0) { + this.list.add(AEItemStack.fromPacket(stream)); } - this.empty = this.list.isEmpty(); } @@ -120,14 +85,6 @@ public class MEInventoryUpdatePacket extends BasePacket { this.data = new PacketBuffer(Unpooled.buffer(OPERATION_BYTE_LIMIT)); this.data.writeInt(this.getPacketID()); this.data.writeByte(this.ref); - - this.compressFrame = new GZIPOutputStream(new OutputStream() { - @Override - public void write(final int value) { - MEInventoryUpdatePacket.this.data.writeByte(value); - } - }); - this.list = null; } @@ -156,28 +113,19 @@ public class MEInventoryUpdatePacket extends BasePacket { @Nullable @Override public IPacket toPacket(NetworkDirection direction) { - try { - this.compressFrame.close(); - - this.configureWrite(this.data); - return super.toPacket(direction); - } catch (final IOException e) { - AELog.debug(e); - } - - return null; + this.configureWrite(this.data); + return super.toPacket(direction); } public void appendItem(final IAEItemStack is) throws IOException, BufferOverflowException { final PacketBuffer tmp = new PacketBuffer(Unpooled.buffer(OPERATION_BYTE_LIMIT)); is.writeToPacket(tmp); - this.compressFrame.flush(); if (this.writtenBytes + tmp.readableBytes() > UNCOMPRESSED_PACKET_BYTE_LIMIT) { throw new BufferOverflowException(); } else { this.writtenBytes += tmp.readableBytes(); - this.compressFrame.write(tmp.array(), 0, tmp.readableBytes()); + this.data.writeBytes(tmp.array(), 0, tmp.readableBytes()); this.empty = false; } } From ab393b0ed1ef9e17ffb97e5de31f4db7a12395ea Mon Sep 17 00:00:00 2001 From: yueh Date: Mon, 31 Aug 2020 23:09:14 +0200 Subject: [PATCH 2/3] Serialize the complete NBT data to avoid mismatches (#4665) This might increase the network traffic a bit, but the amount of mods actually using the share tag seems to be very limited. For now it is worth the risk as it solves the problem. In case we run into actual issues, there might still be other solutions, which will be way more complex and can potentially introduce additional problems. (cherry picked from commit e90dd2f9c6f1cd8b1ce271368f79f1f37df5bdce) --- src/main/java/appeng/util/item/AEItemStack.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/appeng/util/item/AEItemStack.java b/src/main/java/appeng/util/item/AEItemStack.java index e427cac7b..2ac25585f 100644 --- a/src/main/java/appeng/util/item/AEItemStack.java +++ b/src/main/java/appeng/util/item/AEItemStack.java @@ -125,7 +125,7 @@ public final class AEItemStack extends AEStack implements IAEItemS buffer.writeBoolean(this.isCraftable()); buffer.writeVarLong(this.getStackSize()); buffer.writeVarLong(this.getCountRequestable()); - buffer.writeItemStack(getDefinition()); + buffer.writeItemStack(getDefinition(), false); } @Override From 7c99fca7890e3920dd7f642148a7b9a2265e95dc Mon Sep 17 00:00:00 2001 From: shartte Date: Mon, 31 Aug 2020 23:10:25 +0200 Subject: [PATCH 3/3] Fixes removal of facades not invalidating the cached server-side collision boxes. (#4663) (cherry picked from commit 957bc4c5cc157710acd2523a5b0caa98b4ae3158) --- src/main/java/appeng/facade/FacadeContainer.java | 12 +++++++++++- src/main/java/appeng/parts/CableBusContainer.java | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/appeng/facade/FacadeContainer.java b/src/main/java/appeng/facade/FacadeContainer.java index 24e8051bc..b1156dbbe 100644 --- a/src/main/java/appeng/facade/FacadeContainer.java +++ b/src/main/java/appeng/facade/FacadeContainer.java @@ -37,15 +37,18 @@ public class FacadeContainer implements IFacadeContainer { private final int facades = 6; private final CableBusStorage storage; + private final Runnable changeCallback; - public FacadeContainer(final CableBusStorage cbs) { + public FacadeContainer(final CableBusStorage cbs, Runnable changeCallback) { this.storage = cbs; + this.changeCallback = changeCallback; } @Override public boolean addFacade(final IFacadePart a) { if (this.getFacade(a.getSide()) == null) { this.storage.setFacade(a.getSide().ordinal(), a); + this.notifyChange(); return true; } return false; @@ -56,6 +59,7 @@ public class FacadeContainer implements IFacadeContainer { if (side != null && side != AEPartLocation.INTERNAL) { if (this.storage.getFacade(side.ordinal()) != null) { this.storage.setFacade(side.ordinal(), null); + this.notifyChange(); if (host != null) { host.markForUpdate(); } @@ -84,6 +88,7 @@ public class FacadeContainer implements IFacadeContainer { for (int x = 0; x < this.facades; x++) { this.storage.setFacade(x, newFacades[x]); } + this.notifyChange(); } @Override @@ -175,4 +180,9 @@ public class FacadeContainer implements IFacadeContainer { } return true; } + + private void notifyChange() { + this.changeCallback.run(); + } + } diff --git a/src/main/java/appeng/parts/CableBusContainer.java b/src/main/java/appeng/parts/CableBusContainer.java index 963f313d9..b2ddc03fb 100644 --- a/src/main/java/appeng/parts/CableBusContainer.java +++ b/src/main/java/appeng/parts/CableBusContainer.java @@ -125,7 +125,7 @@ public class CableBusContainer extends CableBusStorage implements AEMultiTile, I @Override public IFacadeContainer getFacadeContainer() { - return new FacadeContainer(this); + return new FacadeContainer(this, this::invalidateShapes); } @Override