Game crash fix on connect

whoops used yesterdays version on accident
This commit is contained in:
Goobus 2026-03-08 19:05:56 -07:00 committed by GitHub
parent 5bda4dac7a
commit 6dc6669272
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -327,28 +327,51 @@ shared_ptr<Packet> Packet::readPacket(DataInputStream *dis, bool isServer) // th
id = dis->read(); id = dis->read();
if (id == -1) return nullptr; if (id == -1) return nullptr;
// Track last few good packets for diagnosing TCP desync
static thread_local int s_lastIds[8] = {};
static thread_local int s_lastIdPos = 0;
static thread_local int s_packetCount = 0;
if ((isServer && serverReceivedPackets.find(id) == serverReceivedPackets.end()) || (!isServer && clientReceivedPackets.find(id) == clientReceivedPackets.end())) if ((isServer && serverReceivedPackets.find(id) == serverReceivedPackets.end()) || (!isServer && clientReceivedPackets.find(id) == clientReceivedPackets.end()))
{ {
// Squirrel - Fix crash on connection to server, show an in-game error instead app.DebugPrintf("*** BAD PACKET ID %d (0x%02X) isServer=%d totalPacketsRead=%d\n", id, id, isServer ? 1 : 0, s_packetCount);
return packet; app.DebugPrintf("*** Last %d good packet IDs (oldest first): ", 8);
//app.DebugPrintf("Bad packet id %d\n", id); for (int dbg = 0; dbg < 8; dbg++)
{
int idx = (s_lastIdPos + dbg) % 8;
app.DebugPrintf("%d ", s_lastIds[idx]);
}
app.DebugPrintf("\n");
// Dump the next 32 bytes from the stream to see what follows
app.DebugPrintf("*** Next bytes in stream: ");
for (int dbg = 0; dbg < 32; dbg++)
{
int b = dis->read();
if (b == -1) { app.DebugPrintf("[EOS] "); break; }
app.DebugPrintf("%02X ", b);
}
app.DebugPrintf("\n");
//__debugbreak(); //__debugbreak();
//assert(false); //assert(false);
// throw new IOException(wstring(L"Bad packet id ") + std::to_wstring(id)); // throw new IOException(wstring(L"Bad packet id ") + std::to_wstring(id));
return packet; //return packet instead of debug assert! (Invalid packet is handled in readTick)
} }
packet = getPacket(id); packet = getPacket(id);
//if (packet == nullptr) assert(false);//throw new IOException(wstring(L"Bad packet id ") + std::to_wstring(id));
if (packet == nullptr) return packet;
s_lastIds[s_lastIdPos] = id;
s_lastIdPos = (s_lastIdPos + 1) % 8;
s_packetCount++;
//if (packet == NULL) assert(false);//throw new IOException(wstring(L"Bad packet id ") + std::to_wstring(id));
if (packet == NULL) return packet;
//app.DebugPrintf("%s reading packet %d\n", isServer ? "Server" : "Client", packet->getId());
packet->read(dis); packet->read(dis);
// } // }
// catch (EOFException e) // catch (EOFException e)
// { // {
// // reached end of stream // // reached end of stream
// OutputDebugString("Reached end of stream"); // OutputDebugString("Reached end of stream");
// return NULL; // return nullptr;
// } // }
// 4J - Don't bother tracking stats in a content package // 4J - Don't bother tracking stats in a content package
@ -375,7 +398,7 @@ shared_ptr<Packet> Packet::readPacket(DataInputStream *dis, bool isServer) // th
void Packet::writePacket(shared_ptr<Packet> packet, DataOutputStream *dos) // throws IOException TODO 4J JEV, should this declare a throws? void Packet::writePacket(shared_ptr<Packet> packet, DataOutputStream *dos) // throws IOException TODO 4J JEV, should this declare a throws?
{ {
//app.DebugPrintf("Writing packet %d\n", packet->getId()); //app.DebugPrintf("NET WRITE: packet id=%d (0x%02X) estSize=%d\n", packet->getId(), packet->getId(), packet->getEstimatedSize());
dos->write(packet->getId()); dos->write(packet->getId());
packet->write(dos); packet->write(dos);
} }
@ -389,7 +412,7 @@ void Packet::writeUtf(const wstring& value, DataOutputStream *dos) // throws IOE
} }
#endif #endif
dos->writeShort((short)value.length()); dos->writeShort(static_cast<short>(value.length()));
dos->writeChars(value); dos->writeChars(value);
} }
@ -446,7 +469,7 @@ double Packet::PacketStatistics::getAverageSize()
{ {
return 0; return 0;
} }
return (double) totalSize / count; return static_cast<double>(totalSize) / count;
} }
int Packet::PacketStatistics::getTotalSize() int Packet::PacketStatistics::getTotalSize()
@ -515,7 +538,7 @@ shared_ptr<ItemInstance> Packet::readItem(DataInputStream *dis)
int count = dis->readByte(); int count = dis->readByte();
int damage = dis->readShort(); int damage = dis->readShort();
item = shared_ptr<ItemInstance>( new ItemInstance(id, count, damage) ); item = std::make_shared<ItemInstance>(id, count, damage);
// 4J Stu - Always read/write the tag // 4J Stu - Always read/write the tag
//if (Item.items[id].canBeDepleted() || Item.items[id].shouldOverrideMultiplayerNBT()) //if (Item.items[id].canBeDepleted() || Item.items[id].shouldOverrideMultiplayerNBT())
{ {
@ -528,7 +551,7 @@ shared_ptr<ItemInstance> Packet::readItem(DataInputStream *dis)
void Packet::writeItem(shared_ptr<ItemInstance> item, DataOutputStream *dos) void Packet::writeItem(shared_ptr<ItemInstance> item, DataOutputStream *dos)
{ {
if (item == NULL) if (item == nullptr)
{ {
dos->writeShort(-1); dos->writeShort(-1);
} }
@ -548,7 +571,7 @@ void Packet::writeItem(shared_ptr<ItemInstance> item, DataOutputStream *dos)
CompoundTag *Packet::readNbt(DataInputStream *dis) CompoundTag *Packet::readNbt(DataInputStream *dis)
{ {
int size = dis->readShort(); int size = dis->readShort();
if (size < 0) return NULL; if (size < 0) return nullptr;
byteArray buff(size); byteArray buff(size);
dis->readFully(buff); dis->readFully(buff);
CompoundTag *result = (CompoundTag *) NbtIo::decompress(buff); CompoundTag *result = (CompoundTag *) NbtIo::decompress(buff);
@ -558,14 +581,14 @@ CompoundTag *Packet::readNbt(DataInputStream *dis)
void Packet::writeNbt(CompoundTag *tag, DataOutputStream *dos) void Packet::writeNbt(CompoundTag *tag, DataOutputStream *dos)
{ {
if (tag == NULL) if (tag == nullptr)
{ {
dos->writeShort(-1); dos->writeShort(-1);
} }
else else
{ {
byteArray buff = NbtIo::compress(tag); byteArray buff = NbtIo::compress(tag);
dos->writeShort((short) buff.length); dos->writeShort(static_cast<short>(buff.length));
dos->write(buff); dos->write(buff);
delete [] buff.data; delete [] buff.data;
} }