Skip to content

Commit 2ec2180

Browse files
authored
Improve Logging for Unread Packet Data (GregTechCEu#2698)
1 parent 7985696 commit 2ec2180

File tree

7 files changed

+126
-43
lines changed

7 files changed

+126
-43
lines changed

src/main/java/gregtech/api/capability/GregtechDataCodes.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
package gregtech.api.capability;
22

3+
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
4+
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
5+
import org.apache.commons.lang3.ArrayUtils;
6+
7+
import java.lang.reflect.Field;
8+
import java.lang.reflect.Modifier;
9+
310
public class GregtechDataCodes {
411

12+
public static final int UPDATE_PRIVATE = assignId();
13+
public static final int LOCK_FILL = assignId();
514
private static int nextId = 0;
615

716
public static int assignId() {
@@ -180,4 +189,36 @@ public static int assignId() {
180189
// ME Parts
181190
public static final int UPDATE_AUTO_PULL = assignId();
182191
public static final int UPDATE_ONLINE_STATUS = assignId();
192+
193+
// Everything below MUST be last in the class!
194+
public static final Int2ObjectMap<String> NAMES = new Int2ObjectArrayMap<>();
195+
196+
static {
197+
registerFields(GregtechDataCodes.class);
198+
}
199+
200+
public static String getNameFor(int id) {
201+
return NAMES.getOrDefault(id, "Unknown_DataCode:" + id);
202+
}
203+
204+
/**
205+
* Registers all fields from the passed in class to the name registry.
206+
* Optionally, you can pass in a list of valid ids to check against so that errant ids are not added
207+
*
208+
* @param clazz Class to iterate fields
209+
* @param validIds optional array of valid ids to check against class fields
210+
*/
211+
public static void registerFields(Class<?> clazz, int... validIds) {
212+
try {
213+
for (Field field : clazz.getDeclaredFields()) {
214+
if (field.getType() != Integer.TYPE) continue;
215+
if (!Modifier.isStatic(field.getModifiers())) continue;
216+
if (!Modifier.isFinal(field.getModifiers())) continue;
217+
int id = field.getInt(null);
218+
if (!ArrayUtils.isEmpty(validIds) && !ArrayUtils.contains(validIds, id))
219+
continue;
220+
NAMES.put(id, field.getName() + ":" + id);
221+
}
222+
} catch (IllegalAccessException ignored) {}
223+
}
183224
}

src/main/java/gregtech/api/cover/CoverSaveHandler.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package gregtech.api.cover;
22

3+
import gregtech.api.metatileentity.interfaces.ISyncedTileEntity;
34
import gregtech.api.util.GTLog;
45

56
import net.minecraft.nbt.NBTTagCompound;
@@ -66,6 +67,7 @@ public static void receiveInitialSyncData(@NotNull PacketBuffer buf, @NotNull Co
6667
} else {
6768
Cover cover = definition.createCover(coverHolder, facing);
6869
cover.readInitialSyncData(buf);
70+
ISyncedTileEntity.checkInitialData(buf, cover);
6971
coverHolder.addCover(facing, cover);
7072
}
7173
}
@@ -107,6 +109,7 @@ public static void readCoverPlacement(@NotNull PacketBuffer buf, @NotNull CoverH
107109
coverHolder.addCover(placementSide, cover);
108110

109111
cover.readInitialSyncData(buf);
112+
ISyncedTileEntity.checkInitialData(buf, cover);
110113
}
111114
coverHolder.scheduleRenderUpdate();
112115
}

src/main/java/gregtech/api/metatileentity/MetaTileEntity.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,10 @@ public void receiveInitialSyncData(@NotNull PacketBuffer buf) {
10431043
MTETrait trait = mteTraitByNetworkId.get(traitNetworkId);
10441044
if (trait == null) {
10451045
GTLog.logger.warn("Could not find MTETrait for id: {} at position {}.", traitNetworkId, getPos());
1046-
} else trait.receiveInitialData(buf);
1046+
} else {
1047+
trait.receiveInitialSyncData(buf);
1048+
ISyncedTileEntity.checkInitialData(buf, trait);
1049+
}
10471050
}
10481051
CoverSaveHandler.receiveInitialSyncData(buf, this);
10491052
this.muffled = buf.readBoolean();
@@ -1076,10 +1079,14 @@ public void receiveCustomData(int dataId, @NotNull PacketBuffer buf) {
10761079
scheduleRenderUpdate();
10771080
} else if (dataId == SYNC_MTE_TRAITS) {
10781081
int traitNetworkId = buf.readVarInt();
1082+
int internalId = buf.readVarInt();
10791083
MTETrait trait = mteTraitByNetworkId.get(traitNetworkId);
10801084
if (trait == null) {
10811085
GTLog.logger.warn("Could not find MTETrait for id: {} at position {}.", traitNetworkId, getPos());
1082-
} else trait.receiveCustomData(buf.readVarInt(), buf);
1086+
} else {
1087+
trait.receiveCustomData(internalId, buf);
1088+
ISyncedTileEntity.checkCustomData(internalId, buf, trait);
1089+
}
10831090
} else if (dataId == COVER_ATTACHED_MTE) {
10841091
CoverSaveHandler.readCoverPlacement(buf, this);
10851092
} else if (dataId == COVER_REMOVED_MTE) {
@@ -1095,6 +1102,7 @@ public void receiveCustomData(int dataId, @NotNull PacketBuffer buf) {
10951102
int internalId = buf.readVarInt();
10961103
if (cover != null) {
10971104
cover.readCustomData(internalId, buf);
1105+
ISyncedTileEntity.checkCustomData(internalId, buf, cover);
10981106
}
10991107
} else if (dataId == UPDATE_SOUND_MUFFLED) {
11001108
this.muffled = buf.readBoolean();

src/main/java/gregtech/api/metatileentity/SyncedTileEntityBase.java

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import gregtech.api.metatileentity.interfaces.IGregTechTileEntity;
55
import gregtech.api.metatileentity.interfaces.ISyncedTileEntity;
66
import gregtech.api.network.PacketDataList;
7-
import gregtech.api.util.GTLog;
87

98
import net.minecraft.block.state.IBlockState;
109
import net.minecraft.nbt.NBTBase;
@@ -80,20 +79,13 @@ public final void onDataPacket(@NotNull NetworkManager net, @NotNull SPacketUpda
8079
NBTTagCompound entryTag = (NBTTagCompound) entryBase;
8180
for (String discriminatorKey : entryTag.getKeySet()) {
8281
ByteBuf backedBuffer = Unpooled.copiedBuffer(entryTag.getByteArray(discriminatorKey));
83-
receiveCustomData(Integer.parseInt(discriminatorKey), new PacketBuffer(backedBuffer));
84-
if (backedBuffer.readableBytes() != 0) {
85-
String className = null;
86-
if (this instanceof IGregTechTileEntity gtte) {
87-
MetaTileEntity mte = gtte.getMetaTileEntity();
88-
if (mte != null) className = mte.getClass().getName();
89-
}
90-
if (className == null) {
91-
className = this.getClass().getName();
92-
}
93-
GTLog.logger.error(
94-
"Class {} failed to finish reading receiveCustomData with discriminator {} and {} bytes remaining",
95-
className, discriminatorKey, backedBuffer.readableBytes());
96-
}
82+
int dataId = Integer.parseInt(discriminatorKey);
83+
receiveCustomData(dataId, new PacketBuffer(backedBuffer));
84+
85+
MetaTileEntity mte = null;
86+
if (this instanceof IGregTechTileEntity gtte)
87+
mte = gtte.getMetaTileEntity();
88+
ISyncedTileEntity.checkCustomData(dataId, backedBuffer, mte == null ? this : mte);
9789
}
9890
}
9991
}
@@ -114,18 +106,10 @@ public final void handleUpdateTag(@NotNull NBTTagCompound tag) {
114106
byte[] updateData = tag.getByteArray("d");
115107
ByteBuf backedBuffer = Unpooled.copiedBuffer(updateData);
116108
receiveInitialSyncData(new PacketBuffer(backedBuffer));
117-
if (backedBuffer.readableBytes() != 0) {
118-
String className = null;
119-
if (this instanceof IGregTechTileEntity gtte) {
120-
MetaTileEntity mte = gtte.getMetaTileEntity();
121-
if (mte != null) className = mte.getClass().getName();
122-
}
123-
if (className == null) {
124-
className = this.getClass().getName();
125-
}
126109

127-
GTLog.logger.error("Class {} failed to finish reading initialSyncData with {} bytes remaining",
128-
className, backedBuffer.readableBytes());
129-
}
110+
MetaTileEntity mte = null;
111+
if (this instanceof IGregTechTileEntity gtte)
112+
mte = gtte.getMetaTileEntity();
113+
ISyncedTileEntity.checkInitialData(backedBuffer, mte == null ? this : mte);
130114
}
131115
}

src/main/java/gregtech/api/metatileentity/interfaces/ISyncedTileEntity.java

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
package gregtech.api.metatileentity.interfaces;
22

3+
import gregtech.api.capability.GregtechDataCodes;
4+
import gregtech.api.cover.Cover;
5+
import gregtech.api.cover.CoverableView;
6+
import gregtech.api.util.GTLog;
7+
38
import net.minecraft.network.PacketBuffer;
9+
import net.minecraft.tileentity.TileEntity;
10+
import net.minecraft.util.math.BlockPos;
411

12+
import io.netty.buffer.ByteBuf;
513
import org.jetbrains.annotations.NotNull;
614

715
import java.util.function.Consumer;
@@ -25,7 +33,7 @@ public interface ISyncedTileEntity {
2533
* <p>
2634
* This method is called <strong>Server-Side</strong>.
2735
* <p>
28-
* Equivalent to {@link net.minecraft.tileentity.TileEntity#getUpdateTag}.
36+
* Equivalent to {@link TileEntity#getUpdateTag}.
2937
*
3038
* @param buf the buffer to write data to
3139
*/
@@ -43,7 +51,7 @@ public interface ISyncedTileEntity {
4351
* <p>
4452
* This method is called <strong>Client-Side</strong>.
4553
* <p>
46-
* Equivalent to {@link net.minecraft.tileentity.TileEntity#handleUpdateTag}.
54+
* Equivalent to {@link TileEntity#handleUpdateTag}.
4755
*
4856
* @param buf the buffer to read data from
4957
*/
@@ -62,11 +70,11 @@ public interface ISyncedTileEntity {
6270
* <p>
6371
* This method is called <strong>Server-Side</strong>.
6472
* <p>
65-
* Equivalent to {@link net.minecraft.tileentity.TileEntity#getUpdatePacket}
73+
* Equivalent to {@link TileEntity#getUpdatePacket}
6674
*
6775
* @param discriminator the discriminator determining the packet sent.
6876
* @param dataWriter a consumer which writes packet data to a buffer.
69-
* @see gregtech.api.capability.GregtechDataCodes
77+
* @see GregtechDataCodes
7078
*/
7179
void writeCustomData(int discriminator, @NotNull Consumer<@NotNull PacketBuffer> dataWriter);
7280

@@ -82,10 +90,10 @@ public interface ISyncedTileEntity {
8290
* <p>
8391
* This method is called <strong>Server-Side</strong>.
8492
* <p>
85-
* Equivalent to {@link net.minecraft.tileentity.TileEntity#getUpdatePacket}
93+
* Equivalent to {@link TileEntity#getUpdatePacket}
8694
*
8795
* @param discriminator the discriminator determining the packet sent.
88-
* @see gregtech.api.capability.GregtechDataCodes
96+
* @see GregtechDataCodes
8997
*/
9098
default void writeCustomData(int discriminator) {
9199
writeCustomData(discriminator, NO_OP);
@@ -103,11 +111,51 @@ default void writeCustomData(int discriminator) {
103111
* <p>
104112
* This method is called <strong>Client-Side</strong>.
105113
* <p>
106-
* Equivalent to {@link net.minecraft.tileentity.TileEntity#onDataPacket}
114+
* Equivalent to {@link TileEntity#onDataPacket}
107115
*
108116
* @param discriminator the discriminator determining the packet sent.
109117
* @param buf the buffer containing the packet data.
110-
* @see gregtech.api.capability.GregtechDataCodes
118+
* @see GregtechDataCodes
111119
*/
112120
void receiveCustomData(int discriminator, @NotNull PacketBuffer buf);
121+
122+
static void checkCustomData(int discriminator, @NotNull ByteBuf buf, Object obj) {
123+
if (buf.readableBytes() == 0) return;
124+
125+
GTLog.logger.error(
126+
"Class {} failed to finish reading receiveCustomData with discriminator {} and {} bytes remaining",
127+
stringify(obj), GregtechDataCodes.getNameFor(discriminator), buf.readableBytes());
128+
129+
buf.clear(); // clear to prevent further logging
130+
}
131+
132+
static void checkInitialData(@NotNull ByteBuf buf, Object obj) {
133+
if (buf.readableBytes() == 0) return;
134+
135+
GTLog.logger.error("Class {} failed to finish reading initialSyncData with {} bytes remaining",
136+
stringify(obj), buf.readableBytes());
137+
138+
buf.clear(); // clear to prevent further logging
139+
}
140+
141+
static String stringify(Object obj) {
142+
StringBuilder builder = new StringBuilder(obj.getClass().getSimpleName());
143+
144+
BlockPos pos = null;
145+
if (obj instanceof TileEntity tileEntity) {
146+
pos = tileEntity.getPos(); // TE pos
147+
} else if (obj instanceof CoverableView view) {
148+
pos = view.getPos(); // MTE pos
149+
} else if (obj instanceof Cover cover) {
150+
pos = cover.getPos(); // Cover pos and side
151+
builder.append("[side=").append(cover.getAttachedSide()).append("]");
152+
}
153+
154+
if (pos != null) builder.append(" @ {")
155+
.append(pos.getX()).append("X, ")
156+
.append(pos.getY()).append("Y, ")
157+
.append(pos.getZ()).append("Z}");
158+
159+
return builder.toString();
160+
}
113161
}

src/main/java/gregtech/common/covers/ender/CoverAbstractEnderLink.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ public abstract class CoverAbstractEnderLink<T extends VirtualEntry> extends Cov
6060
implements CoverWithUI, ITickable, IControllable {
6161

6262
protected static final Pattern COLOR_INPUT_PATTERN = Pattern.compile("[0-9a-fA-F]*");
63-
public static final int UPDATE_PRIVATE = GregtechDataCodes.assignId();
6463

6564
protected T activeEntry = null;
6665
protected String color = VirtualEntry.DEFAULT_COLOR;
@@ -100,7 +99,7 @@ protected final UUID getOwner() {
10099
@Override
101100
public void readCustomData(int discriminator, @NotNull PacketBuffer buf) {
102101
super.readCustomData(discriminator, buf);
103-
if (discriminator == UPDATE_PRIVATE) {
102+
if (discriminator == GregtechDataCodes.UPDATE_PRIVATE) {
104103
setPrivate(buf.readBoolean());
105104
updateLink();
106105
}
@@ -246,7 +245,7 @@ private boolean isPrivate() {
246245
private void setPrivate(boolean isPrivate) {
247246
this.isPrivate = isPrivate;
248247
updateLink();
249-
writeCustomData(UPDATE_PRIVATE, buffer -> buffer.writeBoolean(this.isPrivate));
248+
writeCustomData(GregtechDataCodes.UPDATE_PRIVATE, buffer -> buffer.writeBoolean(this.isPrivate));
250249
}
251250

252251
@Override

src/main/java/gregtech/common/metatileentities/multi/multiblockpart/MetaTileEntityFluidHatch.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public class MetaTileEntityFluidHatch extends MetaTileEntityMultiblockNotifiable
5454
implements IMultiblockAbilityPart<IFluidTank>, IControllable {
5555

5656
public static final int INITIAL_INVENTORY_SIZE = 8000;
57-
public static final int LOCK_FILL = GregtechDataCodes.assignId();
5857

5958
// only holding this for convenience
6059
protected final HatchFluidTank fluidTank;
@@ -168,7 +167,7 @@ public void receiveCustomData(int dataId, PacketBuffer buf) {
168167
super.receiveCustomData(dataId, buf);
169168
if (dataId == GregtechDataCodes.WORKING_ENABLED) {
170169
this.workingEnabled = buf.readBoolean();
171-
} else if (dataId == LOCK_FILL) {
170+
} else if (dataId == GregtechDataCodes.LOCK_FILL) {
172171
this.lockedFluid = NetworkUtils.readFluidStack(buf);
173172
}
174173
}
@@ -352,7 +351,8 @@ public int fillInternal(FluidStack resource, boolean doFill) {
352351
if (doFill && locked && lockedFluid == null) {
353352
lockedFluid = resource.copy();
354353
lockedFluid.amount = 1;
355-
writeCustomData(LOCK_FILL, buffer -> NetworkUtils.writeFluidStack(buffer, lockedFluid));
354+
writeCustomData(GregtechDataCodes.LOCK_FILL,
355+
buffer -> NetworkUtils.writeFluidStack(buffer, lockedFluid));
356356
}
357357
return accepted;
358358
}

0 commit comments

Comments
 (0)