Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Logging (again) for Unread Packet Data #2705

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1083,10 +1083,8 @@ public void receiveCustomData(int dataId, @NotNull PacketBuffer buf) {
if (trait == null) {
GTLog.logger.warn("Could not find MTETrait for id: {} at position {}.", traitNetworkId, getPos());
} else {
ISyncedTileEntity.addCode(internalId, trait);
trait.receiveCustomData(internalId, buf);

// this should be fine, as nothing else is read after this
ISyncedTileEntity.checkCustomData(internalId, buf, trait);
}
} else if (dataId == COVER_ATTACHED_MTE) {
CoverSaveHandler.readCoverPlacement(buf, this);
Expand All @@ -1102,10 +1100,8 @@ public void receiveCustomData(int dataId, @NotNull PacketBuffer buf) {
Cover cover = getCoverAtSide(coverSide);
int internalId = buf.readVarInt();
if (cover != null) {
ISyncedTileEntity.addCode(internalId, cover);
cover.readCustomData(internalId, buf);

// this should be fine, as nothing else is read after this
ISyncedTileEntity.checkCustomData(internalId, buf, cover);
}
} else if (dataId == UPDATE_SOUND_MUFFLED) {
this.muffled = buf.readBoolean();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gregtech.api.metatileentity;

import gregtech.api.block.BlockStateTileEntity;
import gregtech.api.metatileentity.interfaces.IGregTechTileEntity;
import gregtech.api.metatileentity.interfaces.ISyncedTileEntity;
import gregtech.api.network.PacketDataList;

Expand Down Expand Up @@ -80,12 +79,9 @@ public final void onDataPacket(@NotNull NetworkManager net, @NotNull SPacketUpda
for (String discriminatorKey : entryTag.getKeySet()) {
ByteBuf backedBuffer = Unpooled.copiedBuffer(entryTag.getByteArray(discriminatorKey));
int dataId = Integer.parseInt(discriminatorKey);
ISyncedTileEntity.addCode(dataId, this);
receiveCustomData(dataId, new PacketBuffer(backedBuffer));

MetaTileEntity mte = null;
if (this instanceof IGregTechTileEntity gtte)
mte = gtte.getMetaTileEntity();
ISyncedTileEntity.checkCustomData(dataId, backedBuffer, mte == null ? this : mte);
ISyncedTileEntity.checkData(backedBuffer);
}
}
}
Expand All @@ -105,11 +101,8 @@ public final void handleUpdateTag(@NotNull NBTTagCompound tag) {
super.readFromNBT(tag); // deserializes Forge data and capabilities
byte[] updateData = tag.getByteArray("d");
ByteBuf backedBuffer = Unpooled.copiedBuffer(updateData);
ISyncedTileEntity.track(this);
receiveInitialSyncData(new PacketBuffer(backedBuffer));

MetaTileEntity mte = null;
if (this instanceof IGregTechTileEntity gtte)
mte = gtte.getMetaTileEntity();
ISyncedTileEntity.checkInitialData(backedBuffer, mte == null ? this : mte);
ISyncedTileEntity.checkData(backedBuffer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import net.minecraft.util.math.BlockPos;

import io.netty.buffer.ByteBuf;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import org.jetbrains.annotations.NotNull;

import java.util.function.Consumer;
Expand All @@ -20,6 +22,8 @@
public interface ISyncedTileEntity {

Consumer<PacketBuffer> NO_OP = buf -> {};
IntList datacodes = new IntArrayList();
ThreadLocal<Object> tracked = ThreadLocal.withInitial(() -> null);

/**
* Used to sync data from Server -> Client.
Expand Down Expand Up @@ -119,26 +123,25 @@ default void writeCustomData(int discriminator) {
*/
void receiveCustomData(int discriminator, @NotNull PacketBuffer buf);

static void checkCustomData(int discriminator, @NotNull ByteBuf buf, Object obj) {
if (buf.readableBytes() == 0) return;

GTLog.logger.error(
"Class {} failed to finish reading receiveCustomData with discriminator {} and {} bytes remaining",
stringify(obj), GregtechDataCodes.getNameFor(discriminator), buf.readableBytes());

buf.clear(); // clear to prevent further logging
}

static void checkInitialData(@NotNull ByteBuf buf, Object obj) {
if (buf.readableBytes() == 0) return;

GTLog.logger.error("Class {} failed to finish reading initialSyncData with {} bytes remaining",
stringify(obj), buf.readableBytes());
static void checkData(@NotNull ByteBuf buf) {
if (buf.readableBytes() != 0) {
if (datacodes.isEmpty()) {
GTLog.logger.error("Class {} failed to finish reading initialSyncData with {} bytes remaining",
stringify(tracked.get()), buf.readableBytes());
} else {
GTLog.logger.error(
"Class {} failed to finish reading receiveCustomData at code path [{}] with {} bytes remaining",
stringify(tracked.get()), getCodePath(), buf.readableBytes());
}
}

buf.clear(); // clear to prevent further logging
reset();
}

static String stringify(Object obj) {
if (obj instanceof IGregTechTileEntity gtte && gtte.getMetaTileEntity() != null)
obj = gtte.getMetaTileEntity();

StringBuilder builder = new StringBuilder(obj.getClass().getSimpleName());

BlockPos pos = null;
Expand All @@ -158,4 +161,27 @@ static String stringify(Object obj) {

return builder.toString();
}

static void addCode(int code, Object trackedObject) {
datacodes.add(code);
track(trackedObject);
}

static void track(Object trackedObject) {
tracked.set(trackedObject);
}

static String getCodePath() {
var builder = new StringBuilder();
for (int i = 0; i < datacodes.size(); i++) {
builder.append(GregtechDataCodes.getNameFor(datacodes.get(i)));
if (i < datacodes.size() - 1) builder.append(" > ");
}
return builder.toString();
}

static void reset() {
datacodes.clear();
tracked.remove();
}
}
Loading