Add debug logging toggle for sync flow

This commit is contained in:
Kam committed 2025-10-19 22:06:11 -04:00
1 parent 111f16647d
commit b25f2b786e
24 files changed
+710 -124

No files matched your search

@@ -16,7 +16,7 @@ import java.util.concurrent.ConcurrentHashMap;
public abstract class LargeAbstractPacket extends AbstractPacket {
private static final Map<UUID, PacketStorage> packetChunks = new ConcurrentHashMap<>();
private static final int CHUNK_SIZE = 10000;
public static final int CHUNK_SIZE = 10000;
/**
* Create multiple FMLProxyPacket objects, each containing up to CHUNK_SIZE bytes.
@@ -71,6 +71,7 @@ import kamkeel.npcs.network.packets.player.item.MagicCyclesPacket;
import kamkeel.npcs.network.packets.player.profile.ProfileChangePacket;
import kamkeel.npcs.network.packets.player.profile.ProfileCreatePacket;
import kamkeel.npcs.network.packets.player.profile.ProfileGetInfoPacket;
import kamkeel.npcs.network.packets.player.SyncRevisionInfoPacket;
import kamkeel.npcs.network.packets.player.profile.ProfileGetPacket;
import kamkeel.npcs.network.packets.player.profile.ProfileRemovePacket;
import kamkeel.npcs.network.packets.player.profile.ProfileRenamePacket;
@@ -575,6 +576,8 @@ public class PacketHandler {
PLAYER_PACKET.registerPacket(new ScreenSizePacket());
PLAYER_PACKET.registerPacket(new SyncRevisionInfoPacket());
// CustomGUI Packets
PLAYER_PACKET.registerPacket(new CustomGuiButtonPacket());
PLAYER_PACKET.registerPacket(new CustomGuiClosePacket());
@@ -28,5 +28,8 @@ public enum EnumPlayerPacket {
ProfileCreate,
ProfileRemove,
ProfileRename,
ProfileChange, ProfileGet, ProfileGetInfo,
ProfileChange,
ProfileGet,
ProfileGetInfo,
SyncRevisionInfo,
}
@@ -5,20 +5,38 @@ import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import kamkeel.npcs.network.AbstractPacket;
import kamkeel.npcs.network.PacketChannel;
import kamkeel.npcs.network.PacketClient;
import kamkeel.npcs.network.PacketHandler;
import kamkeel.npcs.network.enums.EnumDataPacket;
import kamkeel.npcs.network.enums.EnumSyncType;
import kamkeel.npcs.network.packets.player.SyncRevisionInfoPacket;
import kamkeel.npcs.util.ByteBufUtils;
import net.minecraft.entity.player.EntityPlayer;
import noppes.npcs.client.ClientCacheHandler;
import noppes.npcs.config.ConfigMain;
import java.io.IOException;
import java.util.EnumMap;
import java.util.Map;
public final class LoginPacket extends AbstractPacket {
public static final String packetName = "CNPC+|Login";
private String serverCacheKey = "";
private final EnumMap<EnumSyncType, Integer> revisionSnapshot = new EnumMap<>(EnumSyncType.class);
public LoginPacket() {
}
public LoginPacket(String serverCacheKey, EnumMap<EnumSyncType, Integer> revisionSnapshot) {
if (serverCacheKey != null) {
this.serverCacheKey = serverCacheKey;
}
if (revisionSnapshot != null) {
this.revisionSnapshot.putAll(revisionSnapshot);
}
}
@Override
public Enum getType() {
return EnumDataPacket.LOGIN;
@@ -33,6 +51,12 @@ public final class LoginPacket extends AbstractPacket {
public void sendData(ByteBuf out) throws IOException {
out.writeBoolean(ConfigMain.PartiesEnabled);
out.writeBoolean(ConfigMain.ProfilesEnabled);
ByteBufUtils.writeString(out, serverCacheKey);
out.writeShort(revisionSnapshot.size());
for (Map.Entry<EnumSyncType, Integer> entry : revisionSnapshot.entrySet()) {
out.writeInt(entry.getKey().ordinal());
out.writeInt(entry.getValue());
}
}
@SideOnly(Side.CLIENT)
@@ -40,5 +64,21 @@ public final class LoginPacket extends AbstractPacket {
public void receiveData(ByteBuf in, EntityPlayer player) throws IOException {
ClientCacheHandler.allowParties = in.readBoolean();
ClientCacheHandler.allowProfiles = in.readBoolean();
String serverKey = ByteBufUtils.readString(in);
int revisionCount = in.readUnsignedShort();
EnumMap<EnumSyncType, Integer> serverRevisions = new EnumMap<>(EnumSyncType.class);
for (int i = 0; i < revisionCount; i++) {
EnumSyncType type = EnumSyncType.values()[in.readInt()];
int revision = in.readInt();
serverRevisions.put(type, revision);
}
ClientCacheHandler.setActiveServer(serverKey, serverRevisions);
PacketClient.sendClient(new SyncRevisionInfoPacket(
serverKey,
ClientCacheHandler.getCachedRevisionsForServer(serverKey)
));
}
}
@@ -1,5 +1,6 @@
package kamkeel.npcs.network.packets.data.large;
import cpw.mods.fml.common.network.internal.FMLProxyPacket;
import cpw.mods.fml.relauncher.Side;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
@@ -17,6 +18,9 @@ import noppes.npcs.CustomNpcs;
import noppes.npcs.LogWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* A large sync packet that sends chunked data to the client for a given SyncType
@@ -28,6 +32,9 @@ public final class SyncPacket extends LargeAbstractPacket {
private EnumSyncAction enumSyncAction;
private NBTTagCompound syncData;
private int operationID;
private int revision = -1;
private byte[] cachedPayload;
private byte[][] cachedChunks;
public SyncPacket() {
}
@@ -36,10 +43,24 @@ public final class SyncPacket extends LargeAbstractPacket {
* Constructs a new LargeSyncPacket.
*/
public SyncPacket(EnumSyncType enumSyncType, EnumSyncAction enumSyncAction, int catId, NBTTagCompound syncData) {
this(enumSyncType, enumSyncAction, catId, -1, syncData);
}
public SyncPacket(EnumSyncType enumSyncType, EnumSyncAction enumSyncAction, int catId, int revision, NBTTagCompound syncData) {
this.enumSyncType = enumSyncType;
this.enumSyncAction = enumSyncAction;
this.syncData = syncData;
this.operationID = catId;
this.revision = revision;
}
public SyncPacket(EnumSyncType enumSyncType, SyncController.CachedSyncPayload payload) {
this.enumSyncType = enumSyncType;
this.enumSyncAction = EnumSyncAction.RELOAD;
this.operationID = -1;
this.revision = payload.getRevision();
this.cachedPayload = payload.getPayload();
this.cachedChunks = payload.getChunks();
}
@Override
@@ -54,20 +75,55 @@ public final class SyncPacket extends LargeAbstractPacket {
@Override
protected byte[] getData() throws IOException {
ByteBuf buffer = Unpooled.buffer();
// 1) Write SyncType
buffer.writeInt(enumSyncType.ordinal());
// 2) Write SyncAction
buffer.writeInt(enumSyncAction.ordinal());
// 3) Optional Category ID
buffer.writeInt(operationID);
// 4) Write the NBTTagCompound
ByteBufUtils.writeBigNBT(buffer, syncData);
if (cachedPayload != null) {
return cachedPayload;
}
// Copy the buffer’s readable bytes into a byte[]
byte[] bytes = new byte[buffer.readableBytes()];
buffer.readBytes(bytes);
return bytes;
ByteBuf buffer = Unpooled.buffer();
try {
buffer.writeInt(enumSyncType.ordinal());
buffer.writeInt(enumSyncAction.ordinal());
buffer.writeInt(operationID);
buffer.writeInt(revision);
ByteBufUtils.writeBigNBT(buffer, syncData);
byte[] bytes = new byte[buffer.readableBytes()];
buffer.readBytes(bytes);
return bytes;
} finally {
buffer.release();
}
}
@Override
public List<FMLProxyPacket> generatePackets() {
if (cachedChunks == null) {
return super.generatePackets();
}
List<FMLProxyPacket> packets = new ArrayList<>(cachedChunks.length);
PacketChannel packetChannel = getChannel();
EnumDataPacket dataPacket = (EnumDataPacket) getType();
UUID packetId = UUID.randomUUID();
int totalSize = cachedPayload.length;
int offset = 0;
for (byte[] chunk : cachedChunks) {
ByteBuf chunkBuf = Unpooled.buffer();
chunkBuf.writeInt(packetChannel.getChannelType().ordinal());
chunkBuf.writeInt(dataPacket.ordinal());
chunkBuf.writeLong(packetId.getMostSignificantBits());
chunkBuf.writeLong(packetId.getLeastSignificantBits());
chunkBuf.writeInt(totalSize);
chunkBuf.writeInt(offset);
chunkBuf.writeInt(cachedChunks.length);
chunkBuf.writeBytes(chunk);
packets.add(new FMLProxyPacket(chunkBuf, packetChannel.getChannelName()));
offset += chunk.length;
}
return packets;
}
@Override
@@ -79,28 +135,35 @@ public final class SyncPacket extends LargeAbstractPacket {
int syncTypeOrdinal = data.readInt();
int syncActionOrdinal = data.readInt();
int categoryID = data.readInt();
int incomingRevision = data.readInt();
EnumSyncType type = EnumSyncType.values()[syncTypeOrdinal];
EnumSyncAction action = EnumSyncAction.values()[syncActionOrdinal];
try {
NBTTagCompound tag = ByteBufUtils.readBigNBT(data);
// Now do your client-side logic (similar to your old clientSync() or clientSyncUpdate() approach)
handleSyncPacketClient(type, action, categoryID, tag);
handleSyncPacketClient(type, action, categoryID, incomingRevision, tag);
} catch (RuntimeException e) {
LogWriter.error(String.format("Attempted to Sync %s but it was too big", type.toString()));
}
}
private void handleSyncPacketClient(EnumSyncType enumSyncType, EnumSyncAction enumSyncAction, int id, NBTTagCompound data) {
private void handleSyncPacketClient(
EnumSyncType enumSyncType,
EnumSyncAction enumSyncAction,
int id,
int incomingRevision,
NBTTagCompound data
) {
switch (enumSyncAction) {
case RELOAD:
SyncController.clientSync(enumSyncType, data);
SyncController.clientSync(enumSyncType, incomingRevision, data);
break;
case UPDATE:
SyncController.clientUpdate(enumSyncType, id, data);
SyncController.clientUpdate(enumSyncType, id, incomingRevision, data);
break;
case REMOVE:
SyncController.clientSyncRemove(enumSyncType, id);
SyncController.clientSyncRemove(enumSyncType, id, incomingRevision);
break;
}
}
@@ -0,0 +1,77 @@
package kamkeel.npcs.network.packets.player;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import kamkeel.npcs.controllers.SyncController;
import kamkeel.npcs.network.AbstractPacket;
import kamkeel.npcs.network.PacketChannel;
import kamkeel.npcs.network.PacketHandler;
import kamkeel.npcs.network.enums.EnumPlayerPacket;
import kamkeel.npcs.network.enums.EnumSyncType;
import kamkeel.npcs.util.ByteBufUtils;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import java.io.IOException;
import java.util.EnumMap;
import java.util.Map;
public class SyncRevisionInfoPacket extends AbstractPacket {
public static final String packetName = "Player|SyncRevisionInfo";
private String serverKey = "";
private final EnumMap<EnumSyncType, Integer> revisions = new EnumMap<>(EnumSyncType.class);
public SyncRevisionInfoPacket() {
}
public SyncRevisionInfoPacket(String serverKey, EnumMap<EnumSyncType, Integer> revisions) {
if (serverKey != null) {
this.serverKey = serverKey;
}
if (revisions != null) {
this.revisions.putAll(revisions);
}
}
@Override
public Enum getType() {
return EnumPlayerPacket.SyncRevisionInfo;
}
@Override
public PacketChannel getChannel() {
return PacketHandler.PLAYER_PACKET;
}
@Override
@SideOnly(Side.CLIENT)
public void sendData(ByteBuf out) throws IOException {
ByteBufUtils.writeString(out, serverKey);
out.writeShort(revisions.size());
for (Map.Entry<EnumSyncType, Integer> entry : revisions.entrySet()) {
out.writeInt(entry.getKey().ordinal());
out.writeInt(entry.getValue());
}
}
@Override
public void receiveData(ByteBuf in, EntityPlayer player) throws IOException {
if (!(player instanceof EntityPlayerMP)) {
return;
}
String incomingServerKey = ByteBufUtils.readString(in);
int revisionCount = in.readUnsignedShort();
EnumMap<EnumSyncType, Integer> incomingRevisions = new EnumMap<>(EnumSyncType.class);
for (int i = 0; i < revisionCount; i++) {
EnumSyncType type = EnumSyncType.values()[in.readInt()];
int revision = in.readInt();
incomingRevisions.put(type, revision);
}
SyncController.handleClientRevisionReport((EntityPlayerMP) player, incomingServerKey, incomingRevisions);
}
}