From ffe2e90cf409369205525fd48265b57614c9623a Mon Sep 17 00:00:00 2001 From: delmath Date: Wed, 15 Oct 2025 23:56:47 +0200 Subject: [PATCH 1/5] refactor: packet system --- external/data-generator | 2 +- include/network/buffer.hpp | 102 ++-- include/network/networking.hpp | 68 +-- include/network/packet.hpp | 1 + src/data/RegistryData.cpp | 6 +- src/lib/buffer.cpp | 355 ++++++-------- src/lib/packet.cpp | 21 +- src/networking/networkPacketRouter.cpp | 87 ++-- src/networking/networkWorker.cpp | 4 - src/networking/packet/chunkBatch.cpp | 250 +++++----- src/networking/packet/chunkDataSimple.cpp | 450 ++++++++---------- ...ficulty.cpp => changeDifficultyPacket.cpp} | 2 +- ...ks.cpp => clientboundKnownPacksPacket.cpp} | 2 +- .../{gameEvent.cpp => gameEventPacket.cpp} | 0 ...uest.cpp => handleCookieRequestPacket.cpp} | 2 +- ...pp => handleFinishConfigurationPacket.cpp} | 2 +- ...inStart.cpp => handleLoginStartPacket.cpp} | 0 .../{ping.cpp => handlePingPacket.cpp} | 4 +- .../{status.cpp => handleStatusPacket.cpp} | 6 +- ...ight.cpp => levelChunkWithLightPacket.cpp} | 24 +- src/networking/packet/clientbound/play.cpp | 96 ---- .../packet/clientbound/playPacket.cpp | 52 ++ ...bilities.cpp => playerAbilitiesPacket.cpp} | 2 +- ...{setHeldItem.cpp => setHeldItemPacket.cpp} | 2 +- .../synchronizePlayerPositionPacket.cpp | 54 +++ ...eAcknowledgeFinishConfigurationPacket.cpp} | 5 +- ....cpp => handleClientInformationPacket.cpp} | 2 +- ...p => handleConfirmTeleportationPacket.cpp} | 2 +- ...andshake.cpp => handleHandshakePacket.cpp} | 0 ....cpp => handleLoginAcknowledgedPacket.cpp} | 5 +- ...ks.cpp => serverboundKnownPacksPacket.cpp} | 2 +- src/networking/packet/spawnSequence.cpp | 4 +- 32 files changed, 752 insertions(+), 862 deletions(-) rename src/networking/packet/clientbound/{changeDifficulty.cpp => changeDifficultyPacket.cpp} (89%) rename src/networking/packet/clientbound/{clientboundKnowPacks.cpp => clientboundKnownPacksPacket.cpp} (91%) rename src/networking/packet/clientbound/{gameEvent.cpp => gameEventPacket.cpp} (100%) rename src/networking/packet/clientbound/{cookieRequest.cpp => handleCookieRequestPacket.cpp} (97%) rename src/networking/packet/clientbound/{FinishConfiguration.cpp => handleFinishConfigurationPacket.cpp} (93%) rename src/networking/packet/clientbound/{loginStart.cpp => handleLoginStartPacket.cpp} (100%) rename src/networking/packet/clientbound/{ping.cpp => handlePingPacket.cpp} (92%) rename src/networking/packet/clientbound/{status.cpp => handleStatusPacket.cpp} (93%) rename src/networking/packet/clientbound/{levelChunkWithLight.cpp => levelChunkWithLightPacket.cpp} (87%) delete mode 100644 src/networking/packet/clientbound/play.cpp create mode 100644 src/networking/packet/clientbound/playPacket.cpp rename src/networking/packet/clientbound/{playerAbilities.cpp => playerAbilitiesPacket.cpp} (91%) rename src/networking/packet/clientbound/{setHeldItem.cpp => setHeldItemPacket.cpp} (90%) create mode 100644 src/networking/packet/clientbound/synchronizePlayerPositionPacket.cpp rename src/networking/packet/serverbound/{FinishConfiguration.cpp => handleAcknowledgeFinishConfigurationPacket.cpp} (69%) rename src/networking/packet/serverbound/{handleClientInformation.cpp => handleClientInformationPacket.cpp} (90%) rename src/networking/packet/serverbound/{handleConfirmTeleportation.cpp => handleConfirmTeleportationPacket.cpp} (84%) rename src/networking/packet/serverbound/{handshake.cpp => handleHandshakePacket.cpp} (100%) rename src/networking/packet/serverbound/{loginAcknowledged.cpp => handleLoginAcknowledgedPacket.cpp} (91%) rename src/networking/packet/serverbound/{serverboundKnowPacks.cpp => serverboundKnownPacksPacket.cpp} (91%) diff --git a/external/data-generator b/external/data-generator index 45d7644..ec27b8b 160000 --- a/external/data-generator +++ b/external/data-generator @@ -1 +1 @@ -Subproject commit 45d7644b5a6270ef2c3b99a4b360a9cdcfca66e5 +Subproject commit ec27b8b3868966e22227e5b51b7bbadc6a03eaf2 diff --git a/include/network/buffer.hpp b/include/network/buffer.hpp index 13f8af6..b6f7617 100644 --- a/include/network/buffer.hpp +++ b/include/network/buffer.hpp @@ -13,74 +13,76 @@ class Buffer { private: std::vector _data; - size_t _pos; + size_t _pos; public: + // ============================================================ + // ========== CONSTRUCTEURS ========== + // ============================================================ Buffer(); explicit Buffer(const std::vector& data); - int readVarInt(); - void writeVarInt(int value); - void writeInt(int32_t value); - void writeIdentifierArray(const std::vector& ids); - void writeUInt(uint32_t value); + // ============================================================ + // ========== LECTURE ========== + // ============================================================ + uint8_t readByte(); + bool readBool(); + int32_t readInt(); + uint16_t readUShort(); + uint64_t readUInt64(); + int32_t readVarInt(); + int64_t readVarInt64(); std::string readString(int maxLength); - std::string readString(); // Read string without max length limit - void writeString(const std::string& str); + std::string readString(); + int64_t readInt64(); - // Array reading methods - std::vector readStringArray(); - std::vector readVarIntArray(); - template std::vector readArray(std::function reader) { + std::vector readStringArray(); + std::vector readVarIntArray(); + + template + std::vector readArray(std::function reader) { int count = readVarInt(); - if (count < 0) { - throw std::runtime_error("Negative array length"); - } + if (count < 0) throw std::runtime_error("Negative array length"); std::vector result; result.reserve(count); - - for (int i = 0; i < count; ++i) { + for (int i = 0; i < count; ++i) result.push_back(reader()); - } - return result; } - // Boolean reading methods - bool readBool(); - std::vector& getData(); - size_t remaining() const; - uint16_t readUShort(); - void writeUShort(uint16_t value); - uint64_t readUInt64(); - long readLong(); - int32_t readInt(); - void writeLong(long value); - uint8_t readByte(); - void writeByte(uint8_t byte); - void writeBytes(const std::string& data); - void writeBytes(const std::vector& data); - void writeUUID(const UUID& uuid); + // ============================================================ + // ========== ÉCRITURE ========== + // ============================================================ + void writeByte(uint8_t byte); + void writeBytes(const std::string& data); + void writeBytes(const std::vector& data); - void writeBool(bool value); - void writeNBT(const std::string& nbtData); - void writePosition(int32_t x, int32_t y, int32_t z); - void writeFloat(float value); - void writeDouble(double value); - void writeIdentifier(const std::string& id); - void writeVarLong(int64_t value); - int64_t readVarLong(); + void writeBool(bool value); + void writeInt(int32_t value); + void writeUInt(uint32_t value); + void writeUShort(uint16_t value); + void writeInt64(int64_t value); + void writeFloat(float value); + void writeDouble(double value); + + void writeVarInt(int value); + void writeVarInt64(int64_t value); - // Known Packs packet specific methods - struct KnownPack { - std::string nameSpace; - std::string id; - std::string version; - }; - std::vector readKnownPacks(); - void writeKnownPacks(const std::vector& packs); + void writeString(const std::string& str); + void writeUUID(const UUID& uuid); + void writePosition(int32_t x, int32_t y, int32_t z); + void writeIdentifierArray(const std::vector& ids); + void prependBytes(const std::string& data); + void prependByte(uint8_t byte); + void prependVarInt(int value); + + // ============================================================ + // ========== UTILITAIRES ========== + // ============================================================ + std::vector& getData(); + size_t remaining() const; }; #endif diff --git a/include/network/networking.hpp b/include/network/networking.hpp index 21e311d..d48a362 100644 --- a/include/network/networking.hpp +++ b/include/network/networking.hpp @@ -112,53 +112,29 @@ class NetworkManager { void handleIncomingData(int socket); }; -void packetRouter(Packet* packet, Server& server); -void handleHandshakePacket(Packet& packet, Server& server); -void handleStatusPacket(Packet& packet, Server& server); -void handlePingPacket(Packet& packet, Server& server); -void handleClientInformation(Packet& packet, Server& server); -void handleLoginStartPacket(Packet& packet, Server& server); -void handleLoginAcknowledged(Packet& packet, Server& server); -void handleCookieRequest(Packet& packet, Server& server); -void handleFinishConfiguration(Packet& packet, Server& server); -void handleAcknowledgeFinishConfiguration(Packet& packet, Server& server); -void writePlayPacket(Packet& packet, Server& server); -void writeSetCenterPacket(Packet& packet, Server& server); - -// Chunk batch functions -void sendChunkBatchStart(Packet& packet, Server& server); -void sendChunkBatchFinished(Packet& packet, Server& server, int batchSize); -void sendChunkBatchSequence(Packet& packet, Server& server); - -// Chunk data functions -void sendChunkData(Packet& packet, Server& server, int chunkX, int chunkZ); -void sendPlayerPositionAndLook(Packet& packet, Server& server); -void sendSpawnPosition(Packet& packet, Server& server); - -// Spawn sequence functions -void sendPlayerAbilities(Packet& packet, Server& server); -void sendSetHealth(Packet& packet, Server& server); -void sendSetExperience(Packet& packet, Server& server); -void sendUpdateTime(Packet& packet, Server& server); -void sendSetHeldItem(Packet& packet, Server& server); -void handleConfirmTeleportation(Packet& packet, Server& server); -void completeSpawnSequence(Packet& packet, Server& server); -void sendDisconnectPacket(Packet* packet, const std::string& reason, Server& server); - -Buffer generateEmptyChunkSections(); -void writeLightData(Buffer& buf, const World::ChunkData& chunkData); -void writeActualLightData(Buffer& buf, const World::ChunkData& chunkData); -void writeEmptyLightData(Buffer& buf); - -void clientboundKnownPacks(Packet& packet); -void serverboundKnownPacks(Packet& packet); - +// clientbound +void changeDifficultyPacket(Packet& packet); +void clientboundKnownPacksPacket(Packet& packet); void gameEventPacket(Packet& packet, Server& server); -void levelChunkWithLight(Packet& packet, Server& server); +void handleCookieRequestPacket(Packet& packet, Server& server); +void handleFinishConfigurationPacket(Packet& packet, Server& server); +void handleLoginStartPacket(Packet& packet, Server& server); +void handlePingPacket(Packet& packet, Server& server); +void handleStatusPacket(Packet& packet, Server& server); +void levelChunkWithLightPacket(Packet& packet, Server& server); +void playerAbilitiesPacket(Packet& packet); +void sendPlayPacket(Packet& packet, Server& server); +void setHeldItemPacket(Packet& packet); +void synchronizePlayerPositionPacket(Packet& packet, Server& server); + +// serverbound +void handleAcknowledgeFinishConfigurationPacket(Packet& packet, Server& server); +void handleClientInformationPacket(Packet& packet, Server& server); +void handleConfirmTeleportationPacket(Packet& packet, Server& server); +void handleHandshakePacket(Packet& packet, Server& server); +void handleLoginAcknowledgedPacket(Packet& packet, Server& server); +void serverboundKnownPacksPacket(Packet& packet); -// Optional Packets -void changeDifficulty(Packet& packet); -void playerAbilities(Packet& packet); -void setHeldItem(Packet& packet); +void packetRouter(Packet* packet, Server& server); #endif diff --git a/include/network/packet.hpp b/include/network/packet.hpp index 48da62a..1efbfb2 100644 --- a/include/network/packet.hpp +++ b/include/network/packet.hpp @@ -41,6 +41,7 @@ class Packet { int getVarintSize(int32_t value); void setPacketSize(int32_t value); void setPacketId(uint32_t value); + void sendPacket(int id, Buffer& data, Server& server, bool last); }; #endif diff --git a/src/data/RegistryData.cpp b/src/data/RegistryData.cpp index b2abcac..8979921 100644 --- a/src/data/RegistryData.cpp +++ b/src/data/RegistryData.cpp @@ -48,17 +48,17 @@ std::vector RegistryData::serialize() const { Buffer buffer; // Format MC 1.21.5: id + entries length + entries array - buffer.writeIdentifier(_registry_id); + buffer.writeString(_registry_id); buffer.writeVarInt(static_cast(_entries.size())); for (const auto& entry : _entries) { // Chaque entrée: key (string) + value optional (anonymousNbt) - buffer.writeIdentifier(entry.entry_id); + buffer.writeString(entry.entry_id); // Format "value optional": boolean présent + données NBT si présent if (entry.has_data && entry.data.has_value()) { buffer.writeBool(true); // Données présentes - buffer.writeNBT("{}"); // Données NBT (vide pour l'instant) + buffer.writeBytes("{}"); // Données NBT (vide pour l'instant) } else { buffer.writeBool(false); // Pas de données NBT optionnelles } diff --git a/src/lib/buffer.cpp b/src/lib/buffer.cpp index 39e0033..0d17e77 100644 --- a/src/lib/buffer.cpp +++ b/src/lib/buffer.cpp @@ -1,5 +1,4 @@ #include "network/buffer.hpp" - #include "lib/UUID.hpp" #include @@ -10,285 +9,235 @@ Buffer::Buffer() : _pos(0) {} Buffer::Buffer(const std::vector& data) : _data(data), _pos(0) {} + +// ============================================================ +// ========== MÉTHODES DE LECTURE ========== +// ============================================================ + uint8_t Buffer::readByte() { if (_pos >= _data.size()) throw std::runtime_error("Buffer underflow on byte"); return _data[_pos++]; } -void Buffer::writeByte(uint8_t byte) { _data.push_back(byte); } +bool Buffer::readBool() { + return readByte() != 0; +} -void Buffer::writeBytes(const std::string& data) { _data.insert(_data.end(), data.begin(), data.end()); } +int32_t Buffer::readInt() { + int32_t value = 0; + for (int i = 0; i < 4; ++i) + value = (value << 8) | readByte(); + return value; +} -void Buffer::writeBytes(const std::vector& data) { _data.insert(_data.end(), data.begin(), data.end()); } +uint16_t Buffer::readUShort() { + return (readByte() << 8) | readByte(); +} -void Buffer::writeUUID(const UUID& uuid) { - uint64_t msb = uuid.getMostSigBits(); - uint64_t lsb = uuid.getLeastSigBits(); - for (int i = 7; i >= 0; --i) - writeByte((msb >> (i * 8)) & 0xFF); - for (int i = 7; i >= 0; --i) - writeByte((lsb >> (i * 8)) & 0xFF); +uint64_t Buffer::readUInt64() { + uint64_t value = 0; + for (int i = 0; i < 8; ++i) + value = (value << 8) | readByte(); + return value; } -int Buffer::readVarInt() { - int value = 0, position = 0; +int32_t Buffer::readVarInt() { + int value = 0, position = 0; uint8_t currentByte; - do { currentByte = readByte(); value |= (currentByte & 0x7F) << position; position += 7; if (position >= 32) throw std::runtime_error("VarInt too big"); } while (currentByte & 0x80); + return value; +} +int64_t Buffer::readVarInt64() { + int64_t value = 0; + int position = 0; + uint8_t currentByte; + do { + currentByte = readByte(); + value |= ((int64_t)(currentByte & 0x7F)) << position; + position += 7; + if (position >= 64) throw std::runtime_error("VarLong too big"); + } while (currentByte & 0x80); return value; } -void Buffer::writeInt(int32_t value) { - writeByte(static_cast((value >> 24) & 0xFF)); - writeByte(static_cast((value >> 16) & 0xFF)); - writeByte(static_cast((value >> 8) & 0xFF)); - writeByte(static_cast(value & 0xFF)); +std::string Buffer::readString(int maxLength) { + int len = readVarInt(); + if (maxLength > 0 && len > maxLength) + throw std::runtime_error("String length exceeds maximum allowed"); + if (_pos + len > _data.size()) + throw std::runtime_error("Buffer underflow on string"); + + std::string result(reinterpret_cast(&_data[_pos]), len); + _pos += len; + return result; } -void Buffer::writeVarInt(int value) { - while (true) { - if ((value & ~0x7F) == 0) { - writeByte(static_cast(value)); - return; - } else { - writeByte(static_cast((value & 0x7F) | 0x80)); - value >>= 7; - } - } +std::string Buffer::readString() { + return readString(0); } -void Buffer::writeUInt(uint32_t value) { - writeByte(static_cast((value >> 24) & 0xFF)); - writeByte(static_cast((value >> 16) & 0xFF)); - writeByte(static_cast((value >> 8) & 0xFF)); - writeByte(static_cast(value & 0xFF)); +std::vector Buffer::readStringArray() { + int count = readVarInt(); + if (count < 0) throw std::runtime_error("Negative array length"); + + std::vector result; + result.reserve(count); + for (int i = 0; i < count; ++i) + result.push_back(readString()); + return result; } -void Buffer::writeIdentifierArray(const std::vector& ids) { - writeVarInt(static_cast(ids.size())); - for (const auto& id : ids) { - writeString(id); - } +std::vector Buffer::readVarIntArray() { + int count = readVarInt(); + if (count < 0) throw std::runtime_error("Negative array length"); + + std::vector result; + result.reserve(count); + for (int i = 0; i < count; ++i) + result.push_back(readVarInt()); + return result; } -std::string Buffer::readString(int maxLength) { - int len = readVarInt(); +int64_t Buffer::readInt64() { + int64_t value = 0; + for (int i = 0; i < 8; ++i) + value = (value << 8) | readByte(); + return value; +} - if (maxLength > 0 && len > maxLength) { - throw std::runtime_error("String length exceeds maximum allowed"); - } - if (_pos + len > _data.size()) { - throw std::runtime_error("Buffer underflow on string"); - } +// ============================================================ +// ========== MÉTHODES D'ÉCRITURE ========== +// ============================================================ - std::string result(reinterpret_cast(&_data[_pos]), len); - _pos += len; - return result; +void Buffer::writeByte(uint8_t byte) { + _data.push_back(byte); } -void Buffer::writeString(const std::string& str) { - writeVarInt(static_cast(str.size())); - _data.insert(_data.end(), str.begin(), str.end()); +void Buffer::prependByte(uint8_t byte) { + _data.insert(_data.begin(), byte); } -std::vector& Buffer::getData() { return _data; } +void Buffer::writeBytes(const std::string& data) { + _data.insert(_data.end(), data.begin(), data.end()); +} -size_t Buffer::remaining() const { return _data.size() - _pos; } +void Buffer::writeBytes(const std::vector& data) { + _data.insert(_data.end(), data.begin(), data.end()); +} -uint16_t Buffer::readUShort() { - uint16_t val = (readByte() << 8) | readByte(); - return val; +void Buffer::prependBytes(const std::string& data) { + _data.insert(_data.begin(), data.begin(), data.end()); } -uint64_t Buffer::readUInt64() { - uint64_t value = 0; - for (int i = 0; i < 8; ++i) { - value = (value << 8) | readByte(); - } - return value; +void Buffer::writeBool(bool value) { + writeByte(value ? 1 : 0); } -long Buffer::readLong() { - long value = 0; - for (int i = 0; i < 8; ++i) { - value = (value << 8) | readByte(); - } - return value; +void Buffer::writeInt(int32_t value) { + for (int i = 3; i >= 0; --i) + writeByte((value >> (i * 8)) & 0xFF); } -void Buffer::writeLong(long value) { - for (int i = 7; i >= 0; --i) { - writeByte(static_cast((value >> (i * 8)) & 0xFF)); - } +void Buffer::writeUInt(uint32_t value) { + for (int i = 3; i >= 0; --i) + writeByte((value >> (i * 8)) & 0xFF); } -void Buffer::writeBool(bool value) { writeByte(value ? 0x01 : 0x00); } - -void Buffer::writeNBT(const std::string& nbtData) { - // Format anonymousNbt pour MC 1.21.5 - compound NBT minimal mais valide - if (nbtData == "{}") { - // Compound NBT vide mais valide: - // TAG_String + nom vide + valeur vide + TAG_End - writeByte(0x08); // TAG_String - writeVarInt(0); // Nom de longueur 0 (anonyme) - writeVarInt(0); // Valeur string vide - writeByte(0x00); // TAG_End pour fermer le compound - } else { - // Pour autres données NBT, utiliser la même structure minimale - writeByte(0x08); // TAG_String - writeVarInt(0); // Nom de longueur 0 - writeVarInt(0); // Valeur string vide - writeByte(0x00); // TAG_End - } +void Buffer::writeUShort(uint16_t value) { + writeByte((value >> 8) & 0xFF); + writeByte(value & 0xFF); } -void Buffer::writePosition(int32_t x, int32_t y, int32_t z) { - int64_t packed = ((int64_t)(x & 0x3FFFFFF) << 38) | ((int64_t)(y & 0xFFF) << 26) | (int64_t)(z & 0x3FFFFFF); - writeLong(packed); +void Buffer::writeInt64(int64_t value) { + for (int i = 7; i >= 0; --i) + writeByte((value >> (i * 8)) & 0xFF); } void Buffer::writeFloat(float value) { - union { - float f; - uint32_t i; - } u; + union { float f; uint32_t i; } u; u.f = value; writeUInt(u.i); } void Buffer::writeDouble(double value) { - union { - double d; - uint64_t i; - } u; + union { double d; uint64_t i; } u; u.d = value; - writeLong(u.i); + writeInt64(u.i); } -void Buffer::writeVarLong(int64_t value) { +void Buffer::writeVarInt(int value) { while (true) { - if ((value & ~0x7FL) == 0) { - writeByte((uint8_t)value); + if ((value & ~0x7F) == 0) { + writeByte(value); return; - } else { - writeByte((uint8_t)((value & 0x7F) | 0x80)); - value >>= 7; } + writeByte((value & 0x7F) | 0x80); + value >>= 7; } } -void Buffer::writeIdentifier(const std::string& id) { writeString(id); } - -void Buffer::writeUShort(uint16_t value) { - writeByte(static_cast((value >> 8) & 0xFF)); - writeByte(static_cast(value & 0xFF)); -} - -// NEW: String reading methods -std::string Buffer::readString() { - int len = readVarInt(); - - if (_pos + len > _data.size()) { - throw std::runtime_error("Buffer underflow on string"); - } +void Buffer::prependVarInt(int value) { + std::vector tmp; - std::string result(reinterpret_cast(&_data[_pos]), len); - _pos += len; - return result; + while (true) { + if ((value & ~0x7F) == 0) { + tmp.push_back(static_cast(value)); + break; + } + tmp.push_back(static_cast((value & 0x7F) | 0x80)); + value >>= 7; + } + _data.insert(_data.begin(), tmp.begin(), tmp.end()); } -std::vector Buffer::readStringArray() { - int count = readVarInt(); - if (count < 0) { - throw std::runtime_error("Negative array length"); - } - std::vector result; - result.reserve(count); - - for (int i = 0; i < count; ++i) { - result.push_back(readString()); +void Buffer::writeVarInt64(int64_t value) { + while (true) { + if ((value & ~0x7FL) == 0) { + writeByte((uint8_t)value); + return; + } + writeByte((uint8_t)((value & 0x7F) | 0x80)); + value >>= 7; } - - return result; } -std::vector Buffer::readVarIntArray() { - int count = readVarInt(); - if (count < 0) { - throw std::runtime_error("Negative array length"); - } - std::vector result; - result.reserve(count); - - for (int i = 0; i < count; ++i) { - result.push_back(readVarInt()); - } - - return result; +void Buffer::writeString(const std::string& str) { + writeVarInt((int)str.size()); + writeBytes(str); } -// NEW: Boolean reading -bool Buffer::readBool() { - uint8_t value = readByte(); - return value != 0; +void Buffer::writeUUID(const UUID& uuid) { + uint64_t msb = uuid.getMostSigBits(); + uint64_t lsb = uuid.getLeastSigBits(); + for (int i = 7; i >= 0; --i) + writeByte((msb >> (i * 8)) & 0xFF); + for (int i = 7; i >= 0; --i) + writeByte((lsb >> (i * 8)) & 0xFF); } -int32_t Buffer::readInt() { - int32_t value = 0; - for (int i = 0; i < 4; ++i) { - value = (value << 8) | readByte(); - } - return value; +void Buffer::writePosition(int32_t x, int32_t y, int32_t z) { + int64_t packed = ((int64_t)(x & 0x3FFFFFF) << 38) | + ((int64_t)(y & 0xFFF) << 26) | + ((int64_t)(z & 0x3FFFFFF)); + writeInt64(packed); } -int64_t Buffer::readVarLong() { - int64_t value = 0; - int position = 0; - uint8_t currentByte; - - do { - currentByte = readByte(); - value |= ((int64_t)(currentByte & 0x7F)) << position; - position += 7; - if (position >= 64) throw std::runtime_error("VarLong too big"); - } while (currentByte & 0x80); - - return value; +void Buffer::writeIdentifierArray(const std::vector& ids) { + writeVarInt((int)ids.size()); + for (const auto& id : ids) + writeString(id); } -// NEW: Known Packs support -std::vector Buffer::readKnownPacks() { - int count = readVarInt(); - if (count < 0) { - throw std::runtime_error("Negative array length"); - } - std::vector result; - result.reserve(count); - - for (int i = 0; i < count; ++i) { - KnownPack pack; - pack.nameSpace = readString(); - pack.id = readString(); - pack.version = readString(); - result.push_back(pack); - } - - return result; -} +// ============================================================ +// ========== UTILITAIRES ========== +// ============================================================ -void Buffer::writeKnownPacks(const std::vector& packs) { - writeVarInt(static_cast(packs.size())); - - for (const auto& pack : packs) { - writeString(pack.nameSpace); - writeString(pack.id); - writeString(pack.version); - } -} +std::vector& Buffer::getData() { return _data; } +size_t Buffer::remaining() const { return _data.size() - _pos; } diff --git a/src/lib/packet.cpp b/src/lib/packet.cpp index 0a8734a..1c7d7e6 100644 --- a/src/lib/packet.cpp +++ b/src/lib/packet.cpp @@ -1,9 +1,9 @@ #include "lib/json.hpp" -#include "logger.hpp" #include "network/buffer.hpp" #include "network/packet.hpp" #include "network/server.hpp" #include "player.hpp" +#include "networking.hpp" #include #include @@ -228,6 +228,25 @@ int Packet::varintLen(int value) { return (len); } +void Packet::sendPacket(int id, Buffer& data, Server& server, bool last) { + if (!last) { + Packet* newPacket = new Packet(*this); + newPacket->sendPacket(id, data, server, true); + return; + } + Buffer buf; + + buf.writeVarInt(id); + buf.writeBytes(data.getData()); + buf.prependVarInt(buf.getData().size()); + + _data = buf; + _id = id; + _size = buf.getData().size(); + _returnPacket = PACKET_SEND; + server.getNetworkManager().getOutgoingQueue()->push(this); +} + Player* Packet::getPlayer() const { return (_player); } uint32_t Packet::getSize() { return (_size); } uint32_t Packet::getId() { return (_id); } diff --git a/src/networking/networkPacketRouter.cpp b/src/networking/networkPacketRouter.cpp index 9249e5d..43a26d6 100644 --- a/src/networking/networkPacketRouter.cpp +++ b/src/networking/networkPacketRouter.cpp @@ -12,7 +12,7 @@ void handleLoginState(Packet* packet, Server& server); void handleConfigurationState(Packet* packet, Server& server); void handlePlayState(Packet* packet, Server& server); void sendDisconnectPacket(Packet* packet, const std::string& reason, Server& server); -void initGameSequence(Packet* packet, Server& server); +// void initGameSequence(Packet* packet, Server& server); void sendRegistryData(Packet& packet, Server& server); void sendUpdateTags(Packet& packet, Server& server); void sendCompleteConfigurationSequence(Packet* packet, Server& server); @@ -23,21 +23,15 @@ void sendCompleteConfigurationSequence(Packet* packet, Server& server); void packetRouter(Packet* packet, Server& server) { - if (packet == nullptr) return; - if (server.getNetworkManager().getOutgoingQueue() == nullptr) return; - + if (packet == nullptr) + return; + if (server.getNetworkManager().getOutgoingQueue() == nullptr) + return; Player* player = packet->getPlayer(); - if (player == nullptr) { packet->setReturnPacket(PACKET_DISCONNECT); return; } - - g_logger->logNetwork(INFO, - "Routing packet ID: 0x" + std::to_string(packet->getId()) + " (size: " + std::to_string(packet->getSize()) + - ") for state: " + std::to_string(static_cast(player->getPlayerState())), - "PacketRouter"); - switch (player->getPlayerState()) { case PlayerState::Handshake: handleHandshakeState(packet, server); @@ -108,9 +102,9 @@ void handleLoginState(Packet* packet, Server& server) { } else if (packet->getId() == 0x03) { // Login Acknowledged g_logger->logNetwork(INFO, "Processing Login Acknowledged (0x03)", "PacketRouter"); - handleLoginAcknowledged(*packet, server); + handleLoginAcknowledgedPacket(*packet, server); Packet* p = new Packet(*packet); - clientboundKnownPacks(*p); + clientboundKnownPacksPacket(*p); outgoingPackets->push(p); } else if (packet->getId() == 0x04) { // Cookie Response (login) @@ -131,7 +125,7 @@ void handleConfigurationState(Packet* packet, Server& server) { if (packet->getId() == 0x00) { // Client Information (configuration) g_logger->logNetwork(INFO, "Received Client Information in Configuration state", "Configuration"); - handleClientInformation(*packet, server); + handleClientInformationPacket(*packet, server); } else if (packet->getId() == 0x01) { // Cookie Response (configuration) g_logger->logNetwork(INFO, "Received Cookie Response in Configuration state", "Configuration"); @@ -144,35 +138,30 @@ void handleConfigurationState(Packet* packet, Server& server) { packet->setReturnPacket(PACKET_OK); } else if (packet->getId() == 0x03) { - // Acknowledge Finish Configuration -> enter Play - g_logger->logNetwork(INFO, "Received Acknowledge Finish Configuration - transitioning to Play state", "PacketRouter"); - handleAcknowledgeFinishConfiguration(*packet, server); + g_logger->logNetwork(INFO, "Received Acknowledge Finish Configuration - transitioning to Play state", "PacketRouter"); // Acknowledge Finish Configuration -> enter Play + handleAcknowledgeFinishConfigurationPacket(*packet, server); - // 1. Send Login (play) packet - 0x2B - g_logger->logNetwork(INFO, "Sending Login (play) packet", "PacketRouter"); - Packet* playPacket = new Packet(*packet); - writePlayPacket(*playPacket, server); - server.getNetworkManager().getOutgoingQueue()->push(playPacket); + sendPlayPacket(*packet, server); // 1. Send Login (play) packet - 0x2B // 3. Send Change Difficulty - 0x42 g_logger->logNetwork(INFO, "Sending Change Difficulty packet", "PacketRouter"); Packet* difficultyPacket = new Packet(*packet); - changeDifficulty(*difficultyPacket); + changeDifficultyPacket(*difficultyPacket); server.getNetworkManager().getOutgoingQueue()->push(difficultyPacket); // 4. Send Player Abilities - 0x39 g_logger->logNetwork(INFO, "Sending Player Abilities packet", "PacketRouter"); Packet* abilitiesPacket = new Packet(*packet); - playerAbilities(*abilitiesPacket); + playerAbilitiesPacket(*abilitiesPacket); server.getNetworkManager().getOutgoingQueue()->push(abilitiesPacket); Packet* heldItemPacket = new Packet(*packet); - setHeldItem(*heldItemPacket); + setHeldItemPacket(*heldItemPacket); server.getNetworkManager().getOutgoingQueue()->push(heldItemPacket); // 2. Send player position and look - 0x41 Packet* positionPacket = new Packet(*packet); - sendPlayerPositionAndLook(*positionPacket, server); // rename packet + synchronizePlayerPositionPacket(*positionPacket, server); // rename packet server.getNetworkManager().getOutgoingQueue()->push(positionPacket); } else if (packet->getId() == 0x04) { @@ -193,7 +182,7 @@ void handleConfigurationState(Packet* packet, Server& server) { } else if (packet->getId() == 0x07) { // Serverbound Known Packs (configuration) g_logger->logNetwork(INFO, "Received Serverbound Known Packs in Configuration state", "Configuration"); - serverboundKnownPacks(*packet); + serverboundKnownPacksPacket(*packet); packet->setReturnPacket(PACKET_OK); @@ -220,7 +209,7 @@ void handleConfigurationState(Packet* packet, Server& server) { void handlePlayState(Packet* packet, Server& server) { if (packet->getId() == 0x00) { // Confirm Teleportation - handleConfirmTeleportation(*packet, server); + handleConfirmTeleportationPacket(*packet, server); // Send Game Event packet - 0x42 g_logger->logNetwork(INFO, "Sending Game Event packet", "PacketRouter"); @@ -294,33 +283,33 @@ void sendDisconnectPacket(Packet* packet, const std::string& reason, Server& ser // Game Initialization Sequence // ======================================== -void initGameSequence(Packet* packet, Server& server) { - if (packet == nullptr || server.getNetworkManager().getOutgoingQueue() == nullptr) return; +// void initGameSequence(Packet* packet, Server& server) { +// if (packet == nullptr || server.getNetworkManager().getOutgoingQueue() == nullptr) return; - Player* player = packet->getPlayer(); - if (player == nullptr) return; +// Player* player = packet->getPlayer(); +// if (player == nullptr) return; - // Player should already be in Play state at this point - g_logger->logNetwork(INFO, "Starting game sequence for player: " + player->getPlayerName(), "PacketRouter"); +// // Player should already be in Play state at this point +// g_logger->logNetwork(INFO, "Starting game sequence for player: " + player->getPlayerName(), "PacketRouter"); - try { +// try { - // 5. Send spawn position - 0x5A - // Packet* spawnPacket = new Packet(*packet); - // sendSpawnPosition(*spawnPacket, server); - // server.getNetworkManager().getOutgoingQueue()->push(spawnPacket); +// // 5. Send spawn position - 0x5A +// // Packet* spawnPacket = new Packet(*packet); +// // sendSpawnPosition(*spawnPacket, server); +// // server.getNetworkManager().getOutgoingQueue()->push(spawnPacket); - // // 6. Complete spawn sequence (abilities, health, experience, time, held item) - completeSpawnSequence(*packet, server); +// // // 6. Complete spawn sequence (abilities, health, experience, time, held item) +// completeSpawnSequence(*packet, server); - g_logger->logNetwork(INFO, "Complete game sequence sent to player: ", "PacketRouter"); +// g_logger->logNetwork(INFO, "Complete game sequence sent to player: ", "PacketRouter"); - packet->setReturnPacket(PACKET_OK); - } catch (const std::exception& e) { - g_logger->logNetwork(ERROR, "Error in game sequence: " + std::string(e.what()), "PacketRouter"); - packet->setReturnPacket(PACKET_ERROR); - } -} +// packet->setReturnPacket(PACKET_OK); +// } catch (const std::exception& e) { +// g_logger->logNetwork(ERROR, "Error in game sequence: " + std::string(e.what()), "PacketRouter"); +// packet->setReturnPacket(PACKET_ERROR); +// } +// } // ======================================== // Complete Configuration Sequence @@ -358,7 +347,7 @@ void sendCompleteConfigurationSequence(Packet* packet, Server& server) { // 4. Send Finish Configuration to complete the sequence g_logger->logNetwork(INFO, "Step 3: Sending Finish Configuration", "Configuration"); Packet* finishPacket = new Packet(*packet); - handleFinishConfiguration(*finishPacket, server); + handleFinishConfigurationPacket(*finishPacket, server); server.getNetworkManager().getOutgoingQueue()->push(finishPacket); g_logger->logNetwork(INFO, "=== Configuration Sequence Completed Successfully ===", "Configuration"); diff --git a/src/networking/networkWorker.cpp b/src/networking/networkWorker.cpp index 4909e72..75ec463 100644 --- a/src/networking/networkWorker.cpp +++ b/src/networking/networkWorker.cpp @@ -29,10 +29,6 @@ void NetworkManager::workerThreadLoop() { getServer().removePlayerFromAnyList(player); epoll_ctl(_epollFd, EPOLL_CTL_DEL, packet->getSocket(), nullptr); close(packet->getSocket()); - // g_logger->logNetwork(INFO, - // "Disconnected player socket " + - // std::to_string(packet->getSocket()), - // "Worker"); } } } catch (const std::exception& e) { diff --git a/src/networking/packet/chunkBatch.cpp b/src/networking/packet/chunkBatch.cpp index 6ff67d1..32bdab3 100644 --- a/src/networking/packet/chunkBatch.cpp +++ b/src/networking/packet/chunkBatch.cpp @@ -1,125 +1,125 @@ -#include "lib/UUID.hpp" -#include "network/buffer.hpp" -#include "network/networking.hpp" -#include "network/packet.hpp" -#include "network/server.hpp" -#include "player.hpp" - -#include - -void sendChunkBatchStart(Packet& packet, Server& server) { - std::cout << "=== Sending Chunk Batch Start ===\n"; - - // Chunk Batch Start has no fields - just the packet ID - Buffer buf; - // No data to write for this packet - - int packetId = 0x0C; // Chunk Batch Start packet ID for protocol 770 - int packetIdSize = packet.getVarintSize(packetId); - int totalPayloadSize = packetIdSize + buf.getData().size(); - - Buffer finalBuf; - finalBuf.writeVarInt(totalPayloadSize); - finalBuf.writeVarInt(packetId); - if (!buf.getData().empty()) { - finalBuf.writeBytes(buf.getData()); - } - - packet.getData() = finalBuf; - packet.setPacketSize(finalBuf.getData().size()); - packet.setReturnPacket(PACKET_SEND); - - (void)server; -} - -void sendChunkBatchFinished(Packet& packet, Server& server, int batchSize) { - std::cout << "=== Sending Chunk Batch Finished (batch size: " << batchSize << ") ===\n"; - - Buffer buf; - buf.writeVarInt(batchSize); // Number of chunks in the batch - - int packetId = 0x0B; // Chunk Batch Finished packet ID for protocol 770 - int packetIdSize = packet.getVarintSize(packetId); - int totalPayloadSize = packetIdSize + buf.getData().size(); - - Buffer finalBuf; - finalBuf.writeVarInt(totalPayloadSize); - finalBuf.writeVarInt(packetId); - finalBuf.writeBytes(buf.getData()); - - packet.getData() = finalBuf; - packet.setPacketSize(finalBuf.getData().size()); - packet.setReturnPacket(PACKET_SEND); - - (void)server; -} - -void sendChunkBatchSequence(Packet& packet, Server& server) { - Player* player = packet.getPlayer(); - ThreadSafeQueue* outgoingPackets = server.getNetworkManager().getOutgoingQueue(); - if (!player || !outgoingPackets) return; - - int playerChunkX = 0; - int playerChunkZ = 0; - int viewDistance = 3; // Reasonable size - - std::cout << "=== Starting chunk batch sequence for player: " << player->getPlayerName() << " (view distance: " << viewDistance << ") ===\n"; - - // 1. Send Chunk Batch Start - try { - Packet* batchStartPacket = new Packet(packet); - sendChunkBatchStart(*batchStartPacket, server); - outgoingPackets->push(batchStartPacket); - } catch (const std::exception& e) { - std::cerr << "Error sending chunk batch start: " << e.what() << std::endl; - return; - } - - // 2. Send chunks in smaller batches - int chunksCount = 0; - int batchSize = 0; - const int MAX_BATCH_SIZE = 16; // Limit chunks per batch - - for (int x = playerChunkX - viewDistance; x <= playerChunkX + viewDistance; x++) { - for (int z = playerChunkZ - viewDistance; z <= playerChunkZ + viewDistance; z++) { - try { - Packet* chunkPacket = new Packet(packet); - sendChunkData(*chunkPacket, server, x, z); - outgoingPackets->push(chunkPacket); - chunksCount++; - batchSize++; - - // Send batch finished and start new batch if we hit limit - if (batchSize >= MAX_BATCH_SIZE) { - // Send batch finished - Packet* batchFinishedPacket = new Packet(packet); - sendChunkBatchFinished(*batchFinishedPacket, server, batchSize); - outgoingPackets->push(batchFinishedPacket); - - // Start new batch - Packet* batchStartPacket = new Packet(packet); - sendChunkBatchStart(*batchStartPacket, server); - outgoingPackets->push(batchStartPacket); - - batchSize = 0; - } - - } catch (const std::exception& e) { - std::cerr << "Error sending chunk (" << x << ", " << z << "): " << e.what() << std::endl; - } - } - } - - // 3. Send final batch finished - if (batchSize > 0) { - try { - Packet* batchFinishedPacket = new Packet(packet); - sendChunkBatchFinished(*batchFinishedPacket, server, batchSize); - outgoingPackets->push(batchFinishedPacket); - } catch (const std::exception& e) { - std::cerr << "Error sending chunk batch finished: " << e.what() << std::endl; - } - } - - std::cout << "=== Chunk batch sequence completed: " << chunksCount << " chunks sent ===\n"; -} +// #include "lib/UUID.hpp" +// #include "network/buffer.hpp" +// #include "network/networking.hpp" +// #include "network/packet.hpp" +// #include "network/server.hpp" +// #include "player.hpp" + +// #include + +// void sendChunkBatchStart(Packet& packet, Server& server) { +// std::cout << "=== Sending Chunk Batch Start ===\n"; + +// // Chunk Batch Start has no fields - just the packet ID +// Buffer buf; +// // No data to write for this packet + +// int packetId = 0x0C; // Chunk Batch Start packet ID for protocol 770 +// int packetIdSize = packet.getVarintSize(packetId); +// int totalPayloadSize = packetIdSize + buf.getData().size(); + +// Buffer finalBuf; +// finalBuf.writeVarInt(totalPayloadSize); +// finalBuf.writeVarInt(packetId); +// if (!buf.getData().empty()) { +// finalBuf.writeBytes(buf.getData()); +// } + +// packet.getData() = finalBuf; +// packet.setPacketSize(finalBuf.getData().size()); +// packet.setReturnPacket(PACKET_SEND); + +// (void)server; +// } + +// void sendChunkBatchFinished(Packet& packet, Server& server, int batchSize) { +// std::cout << "=== Sending Chunk Batch Finished (batch size: " << batchSize << ") ===\n"; + +// Buffer buf; +// buf.writeVarInt(batchSize); // Number of chunks in the batch + +// int packetId = 0x0B; // Chunk Batch Finished packet ID for protocol 770 +// int packetIdSize = packet.getVarintSize(packetId); +// int totalPayloadSize = packetIdSize + buf.getData().size(); + +// Buffer finalBuf; +// finalBuf.writeVarInt(totalPayloadSize); +// finalBuf.writeVarInt(packetId); +// finalBuf.writeBytes(buf.getData()); + +// packet.getData() = finalBuf; +// packet.setPacketSize(finalBuf.getData().size()); +// packet.setReturnPacket(PACKET_SEND); + +// (void)server; +// } + +// void sendChunkBatchSequence(Packet& packet, Server& server) { +// Player* player = packet.getPlayer(); +// ThreadSafeQueue* outgoingPackets = server.getNetworkManager().getOutgoingQueue(); +// if (!player || !outgoingPackets) return; + +// int playerChunkX = 0; +// int playerChunkZ = 0; +// int viewDistance = 3; // Reasonable size + +// std::cout << "=== Starting chunk batch sequence for player: " << player->getPlayerName() << " (view distance: " << viewDistance << ") ===\n"; + +// // 1. Send Chunk Batch Start +// try { +// Packet* batchStartPacket = new Packet(packet); +// sendChunkBatchStart(*batchStartPacket, server); +// outgoingPackets->push(batchStartPacket); +// } catch (const std::exception& e) { +// std::cerr << "Error sending chunk batch start: " << e.what() << std::endl; +// return; +// } + +// // 2. Send chunks in smaller batches +// int chunksCount = 0; +// int batchSize = 0; +// const int MAX_BATCH_SIZE = 16; // Limit chunks per batch + +// for (int x = playerChunkX - viewDistance; x <= playerChunkX + viewDistance; x++) { +// for (int z = playerChunkZ - viewDistance; z <= playerChunkZ + viewDistance; z++) { +// try { +// Packet* chunkPacket = new Packet(packet); +// sendChunkData(*chunkPacket, server, x, z); +// outgoingPackets->push(chunkPacket); +// chunksCount++; +// batchSize++; + +// // Send batch finished and start new batch if we hit limit +// if (batchSize >= MAX_BATCH_SIZE) { +// // Send batch finished +// Packet* batchFinishedPacket = new Packet(packet); +// sendChunkBatchFinished(*batchFinishedPacket, server, batchSize); +// outgoingPackets->push(batchFinishedPacket); + +// // Start new batch +// Packet* batchStartPacket = new Packet(packet); +// sendChunkBatchStart(*batchStartPacket, server); +// outgoingPackets->push(batchStartPacket); + +// batchSize = 0; +// } + +// } catch (const std::exception& e) { +// std::cerr << "Error sending chunk (" << x << ", " << z << "): " << e.what() << std::endl; +// } +// } +// } + +// // 3. Send final batch finished +// if (batchSize > 0) { +// try { +// Packet* batchFinishedPacket = new Packet(packet); +// sendChunkBatchFinished(*batchFinishedPacket, server, batchSize); +// outgoingPackets->push(batchFinishedPacket); +// } catch (const std::exception& e) { +// std::cerr << "Error sending chunk batch finished: " << e.what() << std::endl; +// } +// } + +// std::cout << "=== Chunk batch sequence completed: " << chunksCount << " chunks sent ===\n"; +// } diff --git a/src/networking/packet/chunkDataSimple.cpp b/src/networking/packet/chunkDataSimple.cpp index d42cd9d..9003edb 100644 --- a/src/networking/packet/chunkDataSimple.cpp +++ b/src/networking/packet/chunkDataSimple.cpp @@ -1,248 +1,202 @@ -#include "lib/UUID.hpp" -#include "network/buffer.hpp" -#include "network/networking.hpp" -#include "network/packet.hpp" -#include "network/server.hpp" -#include "player.hpp" - -#include - -void sendChunkData(Packet& packet, Server& server, int chunkX, int chunkZ) { - std::cout << "=== Sending Chunk Data (" << chunkX << ", " << chunkZ << ") ===\n"; - - Buffer buf; - - try { - // Use your new chunk loading system - World::Query query = server.getWorldQuery(); - World::ChunkData chunkData = query.fetchChunk(chunkX, chunkZ); - - // Write chunk coordinates - buf.writeInt(chunkX); - buf.writeInt(chunkZ); - - // Write heightmaps (simplified) - if (!chunkData.heightmaps.empty()) { - buf.writeBytes(chunkData.heightmaps); - } else { - // Empty heightmap - buf.writeByte(0x0A); // Compound tag - buf.writeByte(0x00); // Empty name length - buf.writeByte(0x00); // End tag - } - - // Write chunk data - if (!chunkData.blockData.empty()) { - buf.writeVarInt(chunkData.blockData.size()); - buf.writeBytes(chunkData.blockData); - } else { - // Generate simple empty chunk inline - Buffer emptyData; - const int NUM_SECTIONS = 24; - - for (int section = 0; section < NUM_SECTIONS; section++) { - emptyData.writeByte(0x00); // Block count low - emptyData.writeByte(0x00); // Block count high - - // Block states - single air block - emptyData.writeByte(0); // Bits per entry - emptyData.writeVarInt(0); // Air block state - emptyData.writeVarInt(0); // No data array - - // Biomes - single plains biome - emptyData.writeByte(0); // Bits per entry - emptyData.writeVarInt(1); // Plains biome - emptyData.writeVarInt(0); // No data array - } - - buf.writeVarInt(emptyData.getData().size()); - buf.writeBytes(emptyData.getData()); - } - - // Block entities - buf.writeVarInt(0); - - // Light data (simplified version) - buf.writeVarInt(1); // Sky light mask length - buf.writeLong(0x1FFFFFF); // All sections have sky light - buf.writeVarInt(1); // Block light mask length - buf.writeLong(0); // No block light - buf.writeVarInt(1); // Empty sky light mask length - buf.writeLong(0); // No empty sky light sections - buf.writeVarInt(1); // Empty block light mask length - buf.writeLong(0); // No empty block light sections - - // Sky light arrays - for (int i = 0; i < 25; i++) { - buf.writeVarInt(2048); - for (int j = 0; j < 2048; j++) { - buf.writeByte(0xFF); - } - } - - } catch (const std::exception& e) { - std::cerr << "Error in sendChunkData: " << e.what() << std::endl; - // Return without setting packet data - this will cause the packet to be skipped - return; - } - - // Create final packet - int packetId = 0x27; - int packetIdSize = packet.getVarintSize(packetId); - int totalPayloadSize = packetIdSize + buf.getData().size(); - - Buffer finalBuf; - finalBuf.writeVarInt(totalPayloadSize); - finalBuf.writeVarInt(packetId); - finalBuf.writeBytes(buf.getData()); - - packet.getData() = finalBuf; - packet.setPacketSize(finalBuf.getData().size()); - packet.setReturnPacket(PACKET_SEND); - - (void)server; -} - -// Helper function to generate empty chunk sections -Buffer generateEmptyChunkSections() { - Buffer chunkData; - const int NUM_SECTIONS = 24; // Sections for world height -64 to 319 - - for (int section = 0; section < NUM_SECTIONS; section++) { - // Block count (non-air blocks) - 0 for empty sections - chunkData.writeByte(0x00); - chunkData.writeByte(0x00); - - // Block states palette - single entry for air - chunkData.writeByte(0); // Bits per entry (0 = single valued) - chunkData.writeVarInt(0); // Air block state ID - chunkData.writeVarInt(0); // No data array needed for single valued - - // Biomes palette - single entry for plains - chunkData.writeByte(0); // Bits per entry (0 = single valued) - chunkData.writeVarInt(1); // Plains biome ID - chunkData.writeVarInt(0); // No data array needed for single valued - } - - return chunkData; -} - -// Helper function to write light data from loaded chunk -void writeLightData(Buffer& buf, const World::ChunkData& chunkData) { - if (!chunkData.skyLight.empty() || !chunkData.blockLight.empty()) { - // Use actual light data from chunk - writeActualLightData(buf, chunkData); - } else { - // Fallback to default light data - writeEmptyLightData(buf); - } -} - -void writeActualLightData(Buffer& buf, const World::ChunkData& chunkData) { - // This is complex - you need to reconstruct the light data format - // For now, fallback to empty light data - // TODO: Implement proper light data reconstruction from chunkData.skyLight and chunkData.blockLight - writeEmptyLightData(buf); -} - -void writeEmptyLightData(Buffer& buf) { - // Sky Light Mask (BitSet) - all sections have sky light - buf.writeVarInt(1); - buf.writeLong(0x1FFFFFF); - - // Block Light Mask (BitSet) - no block light - buf.writeVarInt(1); - buf.writeLong(0); - - // Empty Sky Light Mask - buf.writeVarInt(1); - buf.writeLong(0); - - // Empty Block Light Mask - buf.writeVarInt(1); - buf.writeLong(0); - - // Sky Light arrays (2048 bytes each for sections with sky light) - for (int i = 0; i < 25; i++) { - buf.writeVarInt(2048); - for (int j = 0; j < 2048; j++) { - buf.writeByte(0xFF); // Full sky light - } - } - // No Block Light arrays since mask is 0 -} - -void sendPlayerPositionAndLook(Packet& packet, Server& server) { - std::cout << "=== Sending Player Position and Look ===\n"; - - Buffer buf; - - // Teleport ID - buf.writeVarInt(1); - - // Player position (using writeLong for double precision storage) - // Note: This is a simplified approach - ideally you'd add writeDouble to Buffer - int64_t x_bits = 0x3FE0000000000000; - int64_t y_bits = 0x4050000000000000; - int64_t z_bits = 0x3FE0000000000000; - - buf.writeLong(x_bits); - buf.writeLong(y_bits); - buf.writeLong(z_bits); - - // Velocity (all zero) - buf.writeLong(0); - buf.writeLong(0); - buf.writeLong(0); - - // Rotation (using writeInt for float storage) - buf.writeInt(0); - buf.writeInt(0); - - // Flags (0x00 = absolute positioning) - buf.writeInt(0x00); - - int packetId = 0x41; - int packetIdSize = packet.getVarintSize(packetId); - int totalPayloadSize = packetIdSize + buf.getData().size(); - - Buffer finalBuf; - finalBuf.writeVarInt(totalPayloadSize); - finalBuf.writeVarInt(packetId); - finalBuf.writeBytes(buf.getData()); - - packet.getData() = finalBuf; - packet.setPacketSize(finalBuf.getData().size()); - packet.setReturnPacket(PACKET_SEND); - - (void)server; -} - -void sendSpawnPosition(Packet& packet, Server& server) { - std::cout << "=== Sending Spawn Position ===\n"; - - Buffer buf; - - // Encode position as long (X=0, Y=64, Z=0 packed into 64 bits) - // Position format: ((x & 0x3FFFFFF) << 38) | ((z & 0x3FFFFFF) << 12) | (y & 0xFFF) - int64_t encodedPos = ((int64_t)0 << 38) | ((int64_t)0 << 12) | (64 & 0xFFF); - buf.writeLong(encodedPos); - - // Spawn angle (0.0f as int bits) - buf.writeInt(0); - - int packetId = 0x5A; // Set Default Spawn Position packet ID for protocol 770 - int packetIdSize = packet.getVarintSize(packetId); - int totalPayloadSize = packetIdSize + buf.getData().size(); - - Buffer finalBuf; - finalBuf.writeVarInt(totalPayloadSize); - finalBuf.writeVarInt(packetId); - finalBuf.writeBytes(buf.getData()); - - packet.getData() = finalBuf; - packet.setPacketSize(finalBuf.getData().size()); - packet.setReturnPacket(PACKET_SEND); - - (void)server; -} +// #include "lib/UUID.hpp" +// #include "network/buffer.hpp" +// #include "network/networking.hpp" +// #include "network/packet.hpp" +// #include "network/server.hpp" +// #include "player.hpp" + +// #include + +// void sendChunkData(Packet& packet, Server& server, int chunkX, int chunkZ) { +// std::cout << "=== Sending Chunk Data (" << chunkX << ", " << chunkZ << ") ===\n"; + +// Buffer buf; + +// try { +// // Use your new chunk loading system +// World::Query query = server.getWorldQuery(); +// World::ChunkData chunkData = query.fetchChunk(chunkX, chunkZ); + +// // Write chunk coordinates +// buf.writeInt(chunkX); +// buf.writeInt(chunkZ); + +// // Write heightmaps (simplified) +// if (!chunkData.heightmaps.empty()) { +// buf.writeBytes(chunkData.heightmaps); +// } else { +// // Empty heightmap +// buf.writeByte(0x0A); // Compound tag +// buf.writeByte(0x00); // Empty name length +// buf.writeByte(0x00); // End tag +// } + +// // Write chunk data +// if (!chunkData.blockData.empty()) { +// buf.writeVarInt(chunkData.blockData.size()); +// buf.writeBytes(chunkData.blockData); +// } else { +// // Generate simple empty chunk inline +// Buffer emptyData; +// const int NUM_SECTIONS = 24; + +// for (int section = 0; section < NUM_SECTIONS; section++) { +// emptyData.writeByte(0x00); // Block count low +// emptyData.writeByte(0x00); // Block count high + +// // Block states - single air block +// emptyData.writeByte(0); // Bits per entry +// emptyData.writeVarInt(0); // Air block state +// emptyData.writeVarInt(0); // No data array + +// // Biomes - single plains biome +// emptyData.writeByte(0); // Bits per entry +// emptyData.writeVarInt(1); // Plains biome +// emptyData.writeVarInt(0); // No data array +// } + +// buf.writeVarInt(emptyData.getData().size()); +// buf.writeBytes(emptyData.getData()); +// } + +// // Block entities +// buf.writeVarInt(0); + +// // Light data (simplified version) +// buf.writeVarInt(1); // Sky light mask length +// buf.writeInt64(0x1FFFFFF); // All sections have sky light +// buf.writeVarInt(1); // Block light mask length +// buf.writeInt64(0); // No block light +// buf.writeVarInt(1); // Empty sky light mask length +// buf.writeInt64(0); // No empty sky light sections +// buf.writeVarInt(1); // Empty block light mask length +// buf.writeInt64(0); // No empty block light sections + +// // Sky light arrays +// for (int i = 0; i < 25; i++) { +// buf.writeVarInt(2048); +// for (int j = 0; j < 2048; j++) { +// buf.writeByte(0xFF); +// } +// } + +// } catch (const std::exception& e) { +// std::cerr << "Error in sendChunkData: " << e.what() << std::endl; +// // Return without setting packet data - this will cause the packet to be skipped +// return; +// } + +// // Create final packet +// int packetId = 0x27; +// int packetIdSize = packet.getVarintSize(packetId); +// int totalPayloadSize = packetIdSize + buf.getData().size(); + +// Buffer finalBuf; +// finalBuf.writeVarInt(totalPayloadSize); +// finalBuf.writeVarInt(packetId); +// finalBuf.writeBytes(buf.getData()); + +// packet.getData() = finalBuf; +// packet.setPacketSize(finalBuf.getData().size()); +// packet.setReturnPacket(PACKET_SEND); + +// (void)server; +// } + +// // Helper function to generate empty chunk sections +// Buffer generateEmptyChunkSections() { +// Buffer chunkData; +// const int NUM_SECTIONS = 24; // Sections for world height -64 to 319 + +// for (int section = 0; section < NUM_SECTIONS; section++) { +// // Block count (non-air blocks) - 0 for empty sections +// chunkData.writeByte(0x00); +// chunkData.writeByte(0x00); + +// // Block states palette - single entry for air +// chunkData.writeByte(0); // Bits per entry (0 = single valued) +// chunkData.writeVarInt(0); // Air block state ID +// chunkData.writeVarInt(0); // No data array needed for single valued + +// // Biomes palette - single entry for plains +// chunkData.writeByte(0); // Bits per entry (0 = single valued) +// chunkData.writeVarInt(1); // Plains biome ID +// chunkData.writeVarInt(0); // No data array needed for single valued +// } + +// return chunkData; +// } + +// // Helper function to write light data from loaded chunk +// void writeLightData(Buffer& buf, const World::ChunkData& chunkData) { +// if (!chunkData.skyLight.empty() || !chunkData.blockLight.empty()) { +// // Use actual light data from chunk +// writeActualLightData(buf, chunkData); +// } else { +// // Fallback to default light data +// writeEmptyLightData(buf); +// } +// } + +// void writeActualLightData(Buffer& buf, const World::ChunkData& chunkData) { +// // This is complex - you need to reconstruct the light data format +// // For now, fallback to empty light data +// // TODO: Implement proper light data reconstruction from chunkData.skyLight and chunkData.blockLight +// writeEmptyLightData(buf); +// } + +// void writeEmptyLightData(Buffer& buf) { +// // Sky Light Mask (BitSet) - all sections have sky light +// buf.writeVarInt(1); +// buf.writeInt64(0x1FFFFFF); + +// // Block Light Mask (BitSet) - no block light +// buf.writeVarInt(1); +// buf.writeInt64(0); + +// // Empty Sky Light Mask +// buf.writeVarInt(1); +// buf.writeInt64(0); + +// // Empty Block Light Mask +// buf.writeVarInt(1); +// buf.writeInt64(0); + +// // Sky Light arrays (2048 bytes each for sections with sky light) +// for (int i = 0; i < 25; i++) { +// buf.writeVarInt(2048); +// for (int j = 0; j < 2048; j++) { +// buf.writeByte(0xFF); // Full sky light +// } +// } +// // No Block Light arrays since mask is 0 +// } + +// void sendSpawnPosition(Packet& packet, Server& server) { +// std::cout << "=== Sending Spawn Position ===\n"; + +// Buffer buf; + +// // Encode position as long (X=0, Y=64, Z=0 packed into 64 bits) +// // Position format: ((x & 0x3FFFFFF) << 38) | ((z & 0x3FFFFFF) << 12) | (y & 0xFFF) +// int64_t encodedPos = ((int64_t)0 << 38) | ((int64_t)0 << 12) | (64 & 0xFFF); +// buf.writeInt64(encodedPos); + +// // Spawn angle (0.0f as int bits) +// buf.writeInt(0); + +// int packetId = 0x5A; // Set Default Spawn Position packet ID for protocol 770 +// int packetIdSize = packet.getVarintSize(packetId); +// int totalPayloadSize = packetIdSize + buf.getData().size(); + +// Buffer finalBuf; +// finalBuf.writeVarInt(totalPayloadSize); +// finalBuf.writeVarInt(packetId); +// finalBuf.writeBytes(buf.getData()); + +// packet.getData() = finalBuf; +// packet.setPacketSize(finalBuf.getData().size()); +// packet.setReturnPacket(PACKET_SEND); + +// (void)server; +// } diff --git a/src/networking/packet/clientbound/changeDifficulty.cpp b/src/networking/packet/clientbound/changeDifficultyPacket.cpp similarity index 89% rename from src/networking/packet/clientbound/changeDifficulty.cpp rename to src/networking/packet/clientbound/changeDifficultyPacket.cpp index 817325f..a495b1d 100644 --- a/src/networking/packet/clientbound/changeDifficulty.cpp +++ b/src/networking/packet/clientbound/changeDifficultyPacket.cpp @@ -1,7 +1,7 @@ #include "buffer.hpp" #include "packet.hpp" -void changeDifficulty(Packet& packet) { +void changeDifficultyPacket(Packet& packet) { Buffer buff; buff.writeByte(0x0A); diff --git a/src/networking/packet/clientbound/clientboundKnowPacks.cpp b/src/networking/packet/clientbound/clientboundKnownPacksPacket.cpp similarity index 91% rename from src/networking/packet/clientbound/clientboundKnowPacks.cpp rename to src/networking/packet/clientbound/clientboundKnownPacksPacket.cpp index 9ab7781..1d5481b 100644 --- a/src/networking/packet/clientbound/clientboundKnowPacks.cpp +++ b/src/networking/packet/clientbound/clientboundKnownPacksPacket.cpp @@ -2,7 +2,7 @@ #include "network/packet.hpp" // If implementing data packs we should actually send datapack info with the loaded datapacks -void clientboundKnownPacks(Packet& packet) { +void clientboundKnownPacksPacket(Packet& packet) { Buffer buffer; buffer.writeByte(0x0E); diff --git a/src/networking/packet/clientbound/gameEvent.cpp b/src/networking/packet/clientbound/gameEventPacket.cpp similarity index 100% rename from src/networking/packet/clientbound/gameEvent.cpp rename to src/networking/packet/clientbound/gameEventPacket.cpp diff --git a/src/networking/packet/clientbound/cookieRequest.cpp b/src/networking/packet/clientbound/handleCookieRequestPacket.cpp similarity index 97% rename from src/networking/packet/clientbound/cookieRequest.cpp rename to src/networking/packet/clientbound/handleCookieRequestPacket.cpp index 3118504..829c429 100644 --- a/src/networking/packet/clientbound/cookieRequest.cpp +++ b/src/networking/packet/clientbound/handleCookieRequestPacket.cpp @@ -7,7 +7,7 @@ #include -void handleCookieRequest(Packet& packet, Server& server) { +void handleCookieRequestPacket(Packet& packet, Server& server) { // g_logger->logNetwork(INFO, "Received Cookie Request in Configuration state", // "Configuration"); diff --git a/src/networking/packet/clientbound/FinishConfiguration.cpp b/src/networking/packet/clientbound/handleFinishConfigurationPacket.cpp similarity index 93% rename from src/networking/packet/clientbound/FinishConfiguration.cpp rename to src/networking/packet/clientbound/handleFinishConfigurationPacket.cpp index 06e6ada..cc8ca52 100644 --- a/src/networking/packet/clientbound/FinishConfiguration.cpp +++ b/src/networking/packet/clientbound/handleFinishConfigurationPacket.cpp @@ -3,7 +3,7 @@ #include "network/server.hpp" #include "player.hpp" -void handleFinishConfiguration(Packet& packet, Server& server) { +void handleFinishConfigurationPacket(Packet& packet, Server& server) { // g_logger->logNetwork(INFO, "Starting configuration finish sequence", "Configuration"); Player* player = packet.getPlayer(); diff --git a/src/networking/packet/clientbound/loginStart.cpp b/src/networking/packet/clientbound/handleLoginStartPacket.cpp similarity index 100% rename from src/networking/packet/clientbound/loginStart.cpp rename to src/networking/packet/clientbound/handleLoginStartPacket.cpp diff --git a/src/networking/packet/clientbound/ping.cpp b/src/networking/packet/clientbound/handlePingPacket.cpp similarity index 92% rename from src/networking/packet/clientbound/ping.cpp rename to src/networking/packet/clientbound/handlePingPacket.cpp index 89a9c86..7d3bd8c 100644 --- a/src/networking/packet/clientbound/ping.cpp +++ b/src/networking/packet/clientbound/handlePingPacket.cpp @@ -13,7 +13,7 @@ void handlePingPacket(Packet& packet, Server& server) { return; } - long timestamp = packet.getData().readLong(); + long timestamp = packet.getData().readInt64(); // g_logger->logNetwork(INFO, "Received ping request with timestamp: " + // std::to_string(timestamp), "Ping"); @@ -25,7 +25,7 @@ void handlePingPacket(Packet& packet, Server& server) { Buffer buf; buf.writeVarInt(totalPayloadSize); buf.writeVarInt(packetId); - buf.writeLong(timestamp); + buf.writeInt64(timestamp); packet.getData() = buf; packet.setReturnPacket(PACKET_SEND); diff --git a/src/networking/packet/clientbound/status.cpp b/src/networking/packet/clientbound/handleStatusPacket.cpp similarity index 93% rename from src/networking/packet/clientbound/status.cpp rename to src/networking/packet/clientbound/handleStatusPacket.cpp index 7536ff3..7476c2a 100644 --- a/src/networking/packet/clientbound/status.cpp +++ b/src/networking/packet/clientbound/handleStatusPacket.cpp @@ -23,6 +23,8 @@ void handleStatusPacket(Packet& packet, Server& server) { {"description", {{"text", server.getConfig().getServerMotd()}}}}; std::string payload = jres.dump(); + Buffer buf; + int jsonLen = payload.size(); int packetId = 0x00; @@ -30,7 +32,7 @@ void handleStatusPacket(Packet& packet, Server& server) { int jsonLenVarintSize = packet.getVarintSize(jsonLen); int totalPayloadSize = packetIdVarintSize + jsonLenVarintSize + jsonLen; - Buffer buf; + buf.writeVarInt(totalPayloadSize); buf.writeVarInt(packetId); buf.writeVarInt(jsonLen); @@ -39,6 +41,4 @@ void handleStatusPacket(Packet& packet, Server& server) { packet.setReturnPacket(PACKET_SEND); packet.setPacketSize(buf.getData().size()); packet.getPlayer()->setPlayerState(PlayerState::Status); - - // g_logger->logNetwork(INFO, "JSON response ready - connection will be closed", "Status"); } diff --git a/src/networking/packet/clientbound/levelChunkWithLight.cpp b/src/networking/packet/clientbound/levelChunkWithLightPacket.cpp similarity index 87% rename from src/networking/packet/clientbound/levelChunkWithLight.cpp rename to src/networking/packet/clientbound/levelChunkWithLightPacket.cpp index c855495..1309d0c 100644 --- a/src/networking/packet/clientbound/levelChunkWithLight.cpp +++ b/src/networking/packet/clientbound/levelChunkWithLightPacket.cpp @@ -2,7 +2,7 @@ #include "network/packet.hpp" #include "player.hpp" -void levelChunkWithLight(Packet& packet, Server& server) { +void levelChunkWithLightPacket(Packet& packet, Server& server) { int chunkX = 0; int chunkZ = 0; @@ -47,7 +47,7 @@ void levelChunkWithLight(Packet& packet, Server& server) { emptyData.writeVarInt(0); // Air block state ID (should be 0) emptyData.writeVarInt(0); // Data array length (no data needed for single value) - // Biomes palette + // Biomes palette emptyData.writeByte(0); // Bits per entry (single value) emptyData.writeVarInt(0); // Safest biome ID (should always exist) emptyData.writeVarInt(0); // Data array length (no data needed for single value) @@ -63,19 +63,19 @@ void levelChunkWithLight(Packet& packet, Server& server) { // Light data (proper format) // Sky Light Mask buf.writeVarInt(1); // Sky light mask array length - buf.writeLong(0x1FFFFFF); // Mask for sections 0-24 (all have sky light) - - // Block Light Mask + buf.writeInt64(0x1FFFFFF); // Mask for sections 0-24 (all have sky light) + + // Block Light Mask buf.writeVarInt(1); // Block light mask array length - buf.writeLong(0); // No block light sections - + buf.writeInt64(0); // No block light sections + // Empty Sky Light Mask buf.writeVarInt(1); // Empty sky light mask array length - buf.writeLong(0); // No empty sky light sections - + buf.writeInt64(0); // No empty sky light sections + // Empty Block Light Mask - buf.writeVarInt(1); // Empty block light mask array length - buf.writeLong(0); // No empty block light sections + buf.writeVarInt(1); // Empty block light mask array length + buf.writeInt64(0); // No empty block light sections // Sky light data arrays (25 sections: -4 to 20) for (int i = 0; i < 25; i++) { @@ -84,7 +84,7 @@ void levelChunkWithLight(Packet& packet, Server& server) { buf.writeByte(0xFF); // Full sky light (15 << 4 | 15) } } - + // No block light arrays since mask is 0 } catch (const std::exception& e) { diff --git a/src/networking/packet/clientbound/play.cpp b/src/networking/packet/clientbound/play.cpp deleted file mode 100644 index b00ca0d..0000000 --- a/src/networking/packet/clientbound/play.cpp +++ /dev/null @@ -1,96 +0,0 @@ -#include "network/buffer.hpp" -#include "network/packet.hpp" -#include "player.hpp" - -#include - -void writePlayPacket(Packet& packet, Server& server) { - Player* player = packet.getPlayer(); - if (!player) return; - - Buffer buf; - - buf.writeByte(0x2B); - - // 1. Entity ID - buf.writeInt(player->getPlayerID()); - - // 2. Is hardcore - buf.writeBool(false); - - // 3. Dimension Names (Prefixed Array of Identifier) - buf.writeVarInt(3); // Number of dimensions - buf.writeString("minecraft:overworld"); - buf.writeString("minecraft:the_nether"); - buf.writeString("minecraft:the_end"); - - // 4. Max Players - buf.writeVarInt(20); - - // 5. View Distance - buf.writeVarInt(10); - - // 6. Simulation Distance - buf.writeVarInt(10); - - // 7. Reduced Debug Info - buf.writeBool(false); - - // 8. Enable respawn screen - buf.writeBool(true); - - // 9. Do limited crafting - buf.writeBool(false); - - // 10. Dimension Type (VarInt - registry ID) - buf.writeVarInt(0); // overworld dimension type ID - - // 11. Dimension Name (Identifier) - buf.writeString("minecraft:overworld"); - - // 12. Hashed seed - buf.writeLong(1L); - - // 13. Game mode (Unsigned Byte) - buf.writeByte(0); // Creative - - // 14. Previous Game mode (Byte) - buf.writeByte(-1); // Undefined - - // 15. Is Debug - buf.writeBool(false); - - // 16. Is Flat - buf.writeBool(true); - - // 17. Has death location - bool hasDeathLocation = false; // Set to true if player has died - buf.writeBool(hasDeathLocation); - - // 18. Death dimension name (Optional - only if hasDeathLocation is true) - // 19. Death location (Optional - only if hasDeathLocation is true) - if (hasDeathLocation) { - buf.writeString("minecraft:overworld"); // Death dimension name - buf.writeLong(0); // Death location as Position (packed long) - } - - // 20. Portal cooldown - buf.writeVarInt(0); - - // 21. Sea level - buf.writeVarInt(63); - - // 22. Enforces Secure Chat - buf.writeBool(false); - - Buffer final; - - final.writeVarInt(buf.getData().size()); - final.writeBytes(buf.getData()); - - packet.setPacketId(0x2B); - packet.getData() = final; - packet.setPacketSize(final.getData().size()); - packet.setReturnPacket(PACKET_SEND); - (void)server; -} diff --git a/src/networking/packet/clientbound/playPacket.cpp b/src/networking/packet/clientbound/playPacket.cpp new file mode 100644 index 0000000..96770d9 --- /dev/null +++ b/src/networking/packet/clientbound/playPacket.cpp @@ -0,0 +1,52 @@ +#include "network/buffer.hpp" +#include "network/packet.hpp" +#include "player.hpp" + +#include + +void sendPlayPacket(Packet& packet, Server& server) { + Player* player = packet.getPlayer(); + if (!player) { + packet.setReturnPacket(PACKET_DISCONNECT); + return; + } + + Buffer buf; + + buf.writeInt(player->getPlayerID()); // 1. Entity ID + buf.writeBool(false); // 2. Is hardcore + buf.writeVarInt(3); // 3. Dimension Names (Prefixed Array of Identifier) | Number of dimensions + buf.writeString("minecraft:overworld"); + buf.writeString("minecraft:the_nether"); + buf.writeString("minecraft:the_end"); + buf.writeVarInt(20); // 4. Max Players + buf.writeVarInt(10); // 5. View Distance + buf.writeVarInt(10); // 6. Simulation Distance + buf.writeBool(false); // 7. Reduced Debug Info + buf.writeBool(true); // 8. Enable respawn screen + buf.writeBool(false); // 9. Do limited crafting + buf.writeVarInt(0); // 10. Dimension Type (VarInt - registry ID) | overworld dimension type ID + buf.writeString("minecraft:overworld"); // 11. Dimension Name (Identifier) + buf.writeInt64(1L); // 12. Hashed seed + buf.writeByte(0); // 13. Game mode (Unsigned Byte) | Creative + buf.writeByte(-1); // 14. Previous Game mode (Byte) | Undefined + buf.writeBool(false); // 15. Is Debug + buf.writeBool(true); // 16. Is Flat + + // 17. Has death location + bool hasDeathLocation = false; // Set to true if player has died + buf.writeBool(hasDeathLocation); + + // 18. Death dimension name (Optional - only if hasDeathLocation is true) + // 19. Death location (Optional - only if hasDeathLocation is true) + if (hasDeathLocation) { + buf.writeString("minecraft:overworld"); // Death dimension name + buf.writeInt64(0); // Death location as Position (packed long) + } + + buf.writeVarInt(0); // 20. Portal cooldown + buf.writeVarInt(63); // 21. Sea level + buf.writeBool(false); // 22. Enforces Secure Chat + + packet.sendPacket(0x2B, buf, server, false); +} diff --git a/src/networking/packet/clientbound/playerAbilities.cpp b/src/networking/packet/clientbound/playerAbilitiesPacket.cpp similarity index 91% rename from src/networking/packet/clientbound/playerAbilities.cpp rename to src/networking/packet/clientbound/playerAbilitiesPacket.cpp index 802751e..0e4c0cf 100644 --- a/src/networking/packet/clientbound/playerAbilities.cpp +++ b/src/networking/packet/clientbound/playerAbilitiesPacket.cpp @@ -1,7 +1,7 @@ #include "buffer.hpp" #include "packet.hpp" -void playerAbilities(Packet& packet) { +void playerAbilitiesPacket(Packet& packet) { Buffer buff; buff.writeByte(0x39); diff --git a/src/networking/packet/clientbound/setHeldItem.cpp b/src/networking/packet/clientbound/setHeldItemPacket.cpp similarity index 90% rename from src/networking/packet/clientbound/setHeldItem.cpp rename to src/networking/packet/clientbound/setHeldItemPacket.cpp index b345a97..5670fb9 100644 --- a/src/networking/packet/clientbound/setHeldItem.cpp +++ b/src/networking/packet/clientbound/setHeldItemPacket.cpp @@ -1,7 +1,7 @@ #include "buffer.hpp" #include "packet.hpp" -void setHeldItem(Packet& packet) { +void setHeldItemPacket(Packet& packet) { Buffer buff; buff.writeByte(0x62); diff --git a/src/networking/packet/clientbound/synchronizePlayerPositionPacket.cpp b/src/networking/packet/clientbound/synchronizePlayerPositionPacket.cpp new file mode 100644 index 0000000..6b74fd5 --- /dev/null +++ b/src/networking/packet/clientbound/synchronizePlayerPositionPacket.cpp @@ -0,0 +1,54 @@ +#include "network/buffer.hpp" +#include "network/networking.hpp" +#include "network/packet.hpp" +#include "network/server.hpp" +#include "player.hpp" + +#include +#include + +void synchronizePlayerPositionPacket(Packet& packet, Server& server) { + std::cout << "=== Sending Player Position and Look ===\n"; + + Buffer buf; + + // Teleport ID + buf.writeVarInt(1); + + // Player position (using writeInt64 for double precision storage) + // Note: This is a simplified approach - ideally you'd add writeDouble to Buffer + int64_t x_bits = 0x3FE0000000000000; + int64_t y_bits = 0x4050000000000000; + int64_t z_bits = 0x3FE0000000000000; + + buf.writeInt64(x_bits); + buf.writeInt64(y_bits); + buf.writeInt64(z_bits); + + // Velocity (all zero) + buf.writeInt64(0); + buf.writeInt64(0); + buf.writeInt64(0); + + // Rotation (using writeInt for float storage) + buf.writeInt(0); + buf.writeInt(0); + + // Flags (0x00 = absolute positioning) + buf.writeInt(0x00); + + int packetId = 0x41; + int packetIdSize = packet.getVarintSize(packetId); + int totalPayloadSize = packetIdSize + buf.getData().size(); + + Buffer finalBuf; + finalBuf.writeVarInt(totalPayloadSize); + finalBuf.writeVarInt(packetId); + finalBuf.writeBytes(buf.getData()); + + packet.getData() = finalBuf; + packet.setPacketSize(finalBuf.getData().size()); + packet.setReturnPacket(PACKET_SEND); + + (void)server; +} diff --git a/src/networking/packet/serverbound/FinishConfiguration.cpp b/src/networking/packet/serverbound/handleAcknowledgeFinishConfigurationPacket.cpp similarity index 69% rename from src/networking/packet/serverbound/FinishConfiguration.cpp rename to src/networking/packet/serverbound/handleAcknowledgeFinishConfigurationPacket.cpp index 765caa2..c6b9a9b 100644 --- a/src/networking/packet/serverbound/FinishConfiguration.cpp +++ b/src/networking/packet/serverbound/handleAcknowledgeFinishConfigurationPacket.cpp @@ -1,12 +1,9 @@ #include "network/packet.hpp" #include "player.hpp" -void handleAcknowledgeFinishConfiguration(Packet& packet, Server& server) { - // g_logger->logNetwork(INFO, "Received Acknowledge Finish Configuration", "Configuration"); - +void handleAcknowledgeFinishConfigurationPacket(Packet& packet, Server& server) { Player* player = packet.getPlayer(); if (!player) { - // g_logger->logNetwork(ERROR, "Error: No player associated with packet", "Configuration"); packet.setReturnPacket(PACKET_DISCONNECT); return; } diff --git a/src/networking/packet/serverbound/handleClientInformation.cpp b/src/networking/packet/serverbound/handleClientInformationPacket.cpp similarity index 90% rename from src/networking/packet/serverbound/handleClientInformation.cpp rename to src/networking/packet/serverbound/handleClientInformationPacket.cpp index edd029c..adcacdd 100644 --- a/src/networking/packet/serverbound/handleClientInformation.cpp +++ b/src/networking/packet/serverbound/handleClientInformationPacket.cpp @@ -2,7 +2,7 @@ #include "network/server.hpp" #include "player.hpp" -void handleClientInformation(Packet& packet, Server& server) { +void handleClientInformationPacket(Packet& packet, Server& server) { PlayerConfig* config = packet.getPlayer()->getPlayerConfig(); config->setLocale(packet.getData().readString(16)); diff --git a/src/networking/packet/serverbound/handleConfirmTeleportation.cpp b/src/networking/packet/serverbound/handleConfirmTeleportationPacket.cpp similarity index 84% rename from src/networking/packet/serverbound/handleConfirmTeleportation.cpp rename to src/networking/packet/serverbound/handleConfirmTeleportationPacket.cpp index d266bb5..3be407c 100644 --- a/src/networking/packet/serverbound/handleConfirmTeleportation.cpp +++ b/src/networking/packet/serverbound/handleConfirmTeleportationPacket.cpp @@ -2,7 +2,7 @@ #include "server.hpp" #include -void handleConfirmTeleportation(Packet& packet, Server& server) { +void handleConfirmTeleportationPacket(Packet& packet, Server& server) { std::cout << "=== Received Confirm Teleportation ===\n"; // Read teleport ID from packet data diff --git a/src/networking/packet/serverbound/handshake.cpp b/src/networking/packet/serverbound/handleHandshakePacket.cpp similarity index 100% rename from src/networking/packet/serverbound/handshake.cpp rename to src/networking/packet/serverbound/handleHandshakePacket.cpp diff --git a/src/networking/packet/serverbound/loginAcknowledged.cpp b/src/networking/packet/serverbound/handleLoginAcknowledgedPacket.cpp similarity index 91% rename from src/networking/packet/serverbound/loginAcknowledged.cpp rename to src/networking/packet/serverbound/handleLoginAcknowledgedPacket.cpp index 47d9d0b..f2165c2 100644 --- a/src/networking/packet/serverbound/loginAcknowledged.cpp +++ b/src/networking/packet/serverbound/handleLoginAcknowledgedPacket.cpp @@ -1,12 +1,9 @@ -#include "logger.hpp" #include "network/networking.hpp" #include "network/packet.hpp" #include "network/server.hpp" #include "player.hpp" -#include - -void handleLoginAcknowledged(Packet& packet, Server& server) { +void handleLoginAcknowledgedPacket(Packet& packet, Server& server) { // g_logger->logNetwork(INFO, "Login Acknowledged received - transitioning to Configuration // state", "Login"); diff --git a/src/networking/packet/serverbound/serverboundKnowPacks.cpp b/src/networking/packet/serverbound/serverboundKnownPacksPacket.cpp similarity index 91% rename from src/networking/packet/serverbound/serverboundKnowPacks.cpp rename to src/networking/packet/serverbound/serverboundKnownPacksPacket.cpp index 6b6a372..5e13090 100644 --- a/src/networking/packet/serverbound/serverboundKnowPacks.cpp +++ b/src/networking/packet/serverbound/serverboundKnownPacksPacket.cpp @@ -4,7 +4,7 @@ #include -void serverboundKnownPacks(Packet& packet) { +void serverboundKnownPacksPacket(Packet& packet) { int size = packet.getData().readVarInt(); std::cout << "Received " << size << " known packs." << std::endl; diff --git a/src/networking/packet/spawnSequence.cpp b/src/networking/packet/spawnSequence.cpp index 9459368..be3867e 100644 --- a/src/networking/packet/spawnSequence.cpp +++ b/src/networking/packet/spawnSequence.cpp @@ -113,10 +113,10 @@ void sendUpdateTime(Packet& packet, Server& server) { Buffer buf; // World Age (Long) - total ticks since world creation - buf.writeLong(0); + buf.writeInt64(0); // Time of day (Long) - 0 = sunrise, 6000 = noon, 12000 = sunset, 18000 = midnight - buf.writeLong(1000); // Morning time + buf.writeInt64(1000); // Morning time // Time of day increasing (Boolean) - should client auto-advance time buf.writeByte(0x01); // true From f004dc956dc10079c9c1038885224bb23bb8372e Mon Sep 17 00:00:00 2001 From: delmath Date: Thu, 16 Oct 2025 00:47:50 +0200 Subject: [PATCH 2/5] refactor: finish packet router --- include/network/networking.hpp | 25 - include/network/packetRouter.hpp | 41 ++ src/networking/networkPacketRouter.cpp | 506 +++++++----------- src/networking/networkQueuer.cpp | 6 +- src/networking/networkWorker.cpp | 4 +- src/networking/packet/chunkBatch.cpp | 125 ----- src/networking/packet/chunkDataSimple.cpp | 202 ------- .../packet/clientbound/PlayPacket.cpp | 0 .../clientbound/changeDifficultyPacket.cpp | 11 +- .../clientboundKnownPacksPacket.cpp | 12 +- .../packet/clientbound/gameEventPacket.cpp | 13 +- .../clientbound/handleFinishConfiguration.cpp | 0 .../handleFinishConfigurationPacket.cpp | 21 +- .../clientbound/handleLoginStartPacket.cpp | 51 +- .../packet/clientbound/handlePingPacket.cpp | 20 +- .../packet/clientbound/handleStatusPacket.cpp | 14 +- .../clientbound/playerAbilitiesPacket.cpp | 11 +- .../packet/clientbound/setCenterChunck.cpp | 15 + .../packet/clientbound/setHeldItemPacket.cpp | 12 +- .../synchronizePlayerPositionPacket.cpp | 15 +- ...leAcknowledgeFinishConfigurationPacket.cpp | 8 +- .../handleConfirmTeleportation.cpp | 0 .../handleConfirmTeleportationPacket.cpp | 11 +- .../handleLoginAcknowledgedPacket.cpp | 19 +- src/networking/packet/setCenterChunck.cpp | 31 -- src/networking/packet/spawnSequence.cpp | 205 ------- 26 files changed, 278 insertions(+), 1100 deletions(-) create mode 100644 include/network/packetRouter.hpp delete mode 100644 src/networking/packet/chunkBatch.cpp delete mode 100644 src/networking/packet/chunkDataSimple.cpp create mode 100644 src/networking/packet/clientbound/PlayPacket.cpp create mode 100644 src/networking/packet/clientbound/handleFinishConfiguration.cpp create mode 100644 src/networking/packet/clientbound/setCenterChunck.cpp create mode 100644 src/networking/packet/serverbound/handleConfirmTeleportation.cpp delete mode 100644 src/networking/packet/setCenterChunck.cpp delete mode 100644 src/networking/packet/spawnSequence.cpp diff --git a/include/network/networking.hpp b/include/network/networking.hpp index d48a362..cf8ca77 100644 --- a/include/network/networking.hpp +++ b/include/network/networking.hpp @@ -112,29 +112,4 @@ class NetworkManager { void handleIncomingData(int socket); }; -// clientbound -void changeDifficultyPacket(Packet& packet); -void clientboundKnownPacksPacket(Packet& packet); -void gameEventPacket(Packet& packet, Server& server); -void handleCookieRequestPacket(Packet& packet, Server& server); -void handleFinishConfigurationPacket(Packet& packet, Server& server); -void handleLoginStartPacket(Packet& packet, Server& server); -void handlePingPacket(Packet& packet, Server& server); -void handleStatusPacket(Packet& packet, Server& server); -void levelChunkWithLightPacket(Packet& packet, Server& server); -void playerAbilitiesPacket(Packet& packet); -void sendPlayPacket(Packet& packet, Server& server); -void setHeldItemPacket(Packet& packet); -void synchronizePlayerPositionPacket(Packet& packet, Server& server); - -// serverbound -void handleAcknowledgeFinishConfigurationPacket(Packet& packet, Server& server); -void handleClientInformationPacket(Packet& packet, Server& server); -void handleConfirmTeleportationPacket(Packet& packet, Server& server); -void handleHandshakePacket(Packet& packet, Server& server); -void handleLoginAcknowledgedPacket(Packet& packet, Server& server); -void serverboundKnownPacksPacket(Packet& packet); - -void packetRouter(Packet* packet, Server& server); - #endif diff --git a/include/network/packetRouter.hpp b/include/network/packetRouter.hpp new file mode 100644 index 0000000..745dab4 --- /dev/null +++ b/include/network/packetRouter.hpp @@ -0,0 +1,41 @@ +#ifndef PACKET_ROUTER_HPP +# define PACKET_ROUTER_HPP + +# include "packet.hpp" +# include "server.hpp" + +// clientbound +void changeDifficultyPacket(Packet& packet, Server& server); +void clientboundKnownPacksPacket(Packet& packet, Server& server); +void gameEventPacket(Packet& packet, Server& server); +void handleCookieRequestPacket(Packet& packet, Server& server); +void handleFinishConfigurationPacket(Packet& packet, Server& server); +void handleLoginStartPacket(Packet& packet, Server& server); +void handlePingPacket(Packet& packet, Server& server); +void handleStatusPacket(Packet& packet, Server& server); +void levelChunkWithLightPacket(Packet& packet, Server& server); +void playerAbilitiesPacket(Packet& packet, Server& server); +void sendPlayPacket(Packet& packet, Server& server); +void setHeldItemPacket(Packet& packet, Server& server); +void synchronizePlayerPositionPacket(Packet& packet, Server& server); +void setCenterPacket(Packet& packet, Server& server); + +// serverbound +void handleAcknowledgeFinishConfigurationPacket(Packet& packet, Server& server); +void handleClientInformationPacket(Packet& packet, Server& server); +void handleConfirmTeleportationPacket(Packet& packet, Server& server); +void handleHandshakePacket(Packet& packet, Server& server); +void handleLoginAcknowledgedPacket(Packet& packet, Server& server); +void serverboundKnownPacksPacket(Packet& packet); + +void packetRouter(Packet* packet, Server& server); + +void handleHandshakeState(Packet* packet, Server& server); +void handleStatusState(Packet* packet, Server& server); +void handleLoginState(Packet* packet, Server& server); +void handleConfigurationState(Packet* packet, Server& server); +void handlePlayState(Packet* packet, Server& server); +void sendRegistryData(Packet& packet, Server& server); +void sendUpdateTags(Packet& packet, Server& server); + +#endif diff --git a/src/networking/networkPacketRouter.cpp b/src/networking/networkPacketRouter.cpp index 43a26d6..943a77b 100644 --- a/src/networking/networkPacketRouter.cpp +++ b/src/networking/networkPacketRouter.cpp @@ -1,81 +1,95 @@ #include "logger.hpp" -#include "network/networking.hpp" -#include "network/packet.hpp" -#include "network/server.hpp" +#include "networking.hpp" +#include "packet.hpp" +#include "server.hpp" #include "player.hpp" +#include "packetRouter.hpp" #include -void handleHandshakeState(Packet* packet, Server& server); -void handleStatusState(Packet* packet, Server& server); -void handleLoginState(Packet* packet, Server& server); -void handleConfigurationState(Packet* packet, Server& server); -void handlePlayState(Packet* packet, Server& server); -void sendDisconnectPacket(Packet* packet, const std::string& reason, Server& server); -// void initGameSequence(Packet* packet, Server& server); -void sendRegistryData(Packet& packet, Server& server); -void sendUpdateTags(Packet& packet, Server& server); -void sendCompleteConfigurationSequence(Packet* packet, Server& server); - // ======================================== // Main Packet Router // ======================================== void packetRouter(Packet* packet, Server& server) { - - if (packet == nullptr) - return; - if (server.getNetworkManager().getOutgoingQueue() == nullptr) - return; - Player* player = packet->getPlayer(); - if (player == nullptr) { - packet->setReturnPacket(PACKET_DISCONNECT); - return; - } - switch (player->getPlayerState()) { - case PlayerState::Handshake: - handleHandshakeState(packet, server); - break; - case PlayerState::Status: - handleStatusState(packet, server); - break; - case PlayerState::Login: - handleLoginState(packet, server); - break; - case PlayerState::Configuration: - handleConfigurationState(packet, server); - break; - case PlayerState::Play: - handlePlayState(packet, server); - break; - default: - g_logger->logNetwork( - WARN, "Unknown player state: " + std::to_string(static_cast(player->getPlayerState())) + ", disconnecting", "PacketRouter"); - packet->setReturnPacket(PACKET_DISCONNECT); - break; - } + // Basic validation checks + if (packet == nullptr) + return; + + if (server.getNetworkManager().getOutgoingQueue() == nullptr) + return; + + // Get player and validate + Player* player = packet->getPlayer(); + if (player == nullptr) { + packet->setReturnPacket(PACKET_DISCONNECT); + return; + } + + // Route packet based on player's current state + switch (player->getPlayerState()) { + case PlayerState::Handshake: + handleHandshakeState(packet, server); + break; + + case PlayerState::Status: + handleStatusState(packet, server); + break; + + case PlayerState::Login: + handleLoginState(packet, server); + break; + + case PlayerState::Configuration: + handleConfigurationState(packet, server); + break; + + case PlayerState::Play: + handlePlayState(packet, server); + break; + + default: + g_logger->logNetwork( + WARN, + "Unknown player state: " + std::to_string(static_cast(player->getPlayerState())) + ", disconnecting", + "PacketRouter" + ); + packet->setReturnPacket(PACKET_DISCONNECT); + break; + } } // ======================================== // Handshake State Handler // ======================================== -void handleHandshakeState(Packet* packet, Server& server) { handleHandshakePacket(*packet, server); } +void handleHandshakeState(Packet* packet, Server& server) { + handleHandshakePacket(*packet, server); +} // ======================================== // Status State Handler // ======================================== void handleStatusState(Packet* packet, Server& server) { - if (packet->getId() == 0x00) { - handleStatusPacket(*packet, server); - } else if (packet->getId() == 0x01) { - handlePingPacket(*packet, server); - } else { - // g_logger->logNetwork(WARN, "Unknown packet ID in Status state: 0x" + std::to_string(packet->getId()), "PacketRouter"); - packet->getPlayer()->setPlayerState(PlayerState::None); - packet->setReturnPacket(PACKET_DISCONNECT); - } + switch (packet->getId()) { + case 0x00: + // Status request packet + handleStatusPacket(*packet, server); + break; + + case 0x01: + // Ping packet + handlePingPacket(*packet, server); + break; + + default: + // Unknown packet in status state - disconnect + // g_logger->logNetwork(WARN, "Unknown packet ID in Status state: 0x" + std::to_string(packet->getId()), "PacketRouter"); + packet->getPlayer()->setPlayerState(PlayerState::None); + packet->setReturnPacket(PACKET_DISCONNECT); + break; + } } // ======================================== @@ -83,38 +97,45 @@ void handleStatusState(Packet* packet, Server& server) { // ======================================== void handleLoginState(Packet* packet, Server& server) { - ThreadSafeQueue* outgoingPackets = server.getNetworkManager().getOutgoingQueue(); - // Add safety check for packet data integrity - if (packet->getSize() > 32767) { // Max packet size - g_logger->logNetwork(ERROR, "Packet size too large: " + std::to_string(packet->getSize()), "PacketRouter"); - packet->setReturnPacket(PACKET_DISCONNECT); - return; - } - - if (packet->getId() == 0x00) { - // Login Start - g_logger->logNetwork(INFO, "Processing Login Start (0x00)", "PacketRouter"); - handleLoginStartPacket(*packet, server); - } else if (packet->getId() == 0x02) { - // Login Plugin Response - safe to ignore most of the time - g_logger->logNetwork(INFO, "Received Login Plugin Response (0x02) - acknowledging", "PacketRouter"); - packet->setReturnPacket(PACKET_OK); - } else if (packet->getId() == 0x03) { - // Login Acknowledged - g_logger->logNetwork(INFO, "Processing Login Acknowledged (0x03)", "PacketRouter"); - handleLoginAcknowledgedPacket(*packet, server); - Packet* p = new Packet(*packet); - clientboundKnownPacksPacket(*p); - outgoingPackets->push(p); - } else if (packet->getId() == 0x04) { - // Cookie Response (login) - g_logger->logNetwork(INFO, "Received Login Cookie Response (0x04) - acknowledging", "PacketRouter"); - packet->setReturnPacket(PACKET_OK); - } else { - // g_logger->logNetwork(WARN, "Unknown packet ID in Login state: 0x" + std::to_string(packet->getId()), "PacketRouter"); - packet->getPlayer()->setPlayerState(PlayerState::None); - packet->setReturnPacket(PACKET_DISCONNECT); - } + // Check packet size limit + if (packet->getSize() > 32767) { + g_logger->logNetwork(ERROR, "Packet size too large: " + std::to_string(packet->getSize()), "PacketRouter"); + packet->setReturnPacket(PACKET_DISCONNECT); + return; + } + + switch (packet->getId()) { + case 0x00: + // Login start packet + handleLoginStartPacket(*packet, server); + break; + + case 0x02: + // Login plugin response + g_logger->logNetwork(INFO, "Received Login Plugin Response (0x02) - acknowledging", "PacketRouter"); + packet->setReturnPacket(PACKET_OK); + break; + + case 0x03: + // Login acknowledged packet + handleLoginAcknowledgedPacket(*packet, server); + if (packet->getReturnPacket() == PACKET_DISCONNECT) + return; + clientboundKnownPacksPacket(*packet, server); + break; + + case 0x04: + // Login cookie response + g_logger->logNetwork(INFO, "Received Login Cookie Response (0x04) - acknowledging", "PacketRouter"); + packet->setReturnPacket(PACKET_OK); + break; + + default: + // Unknown packet in login state - disconnect + packet->getPlayer()->setPlayerState(PlayerState::None); + packet->setReturnPacket(PACKET_DISCONNECT); + break; + } } // ======================================== @@ -122,84 +143,73 @@ void handleLoginState(Packet* packet, Server& server) { // ======================================== void handleConfigurationState(Packet* packet, Server& server) { - if (packet->getId() == 0x00) { - // Client Information (configuration) - g_logger->logNetwork(INFO, "Received Client Information in Configuration state", "Configuration"); - handleClientInformationPacket(*packet, server); - } else if (packet->getId() == 0x01) { - // Cookie Response (configuration) - g_logger->logNetwork(INFO, "Received Cookie Response in Configuration state", "Configuration"); - packet->setReturnPacket(PACKET_OK); - - } else if (packet->getId() == 0x02) { - // Serverbound Plugin Message (configuration) - // g_logger->logNetwork( - // INFO, "Received Serverbound Plugin Message (0x02), size: " + std::to_string(packet->getSize()) + " bytes", "PacketRouter"); - packet->setReturnPacket(PACKET_OK); - - } else if (packet->getId() == 0x03) { - g_logger->logNetwork(INFO, "Received Acknowledge Finish Configuration - transitioning to Play state", "PacketRouter"); // Acknowledge Finish Configuration -> enter Play - handleAcknowledgeFinishConfigurationPacket(*packet, server); - - sendPlayPacket(*packet, server); // 1. Send Login (play) packet - 0x2B - - // 3. Send Change Difficulty - 0x42 - g_logger->logNetwork(INFO, "Sending Change Difficulty packet", "PacketRouter"); - Packet* difficultyPacket = new Packet(*packet); - changeDifficultyPacket(*difficultyPacket); - server.getNetworkManager().getOutgoingQueue()->push(difficultyPacket); - - // 4. Send Player Abilities - 0x39 - g_logger->logNetwork(INFO, "Sending Player Abilities packet", "PacketRouter"); - Packet* abilitiesPacket = new Packet(*packet); - playerAbilitiesPacket(*abilitiesPacket); - server.getNetworkManager().getOutgoingQueue()->push(abilitiesPacket); - - Packet* heldItemPacket = new Packet(*packet); - setHeldItemPacket(*heldItemPacket); - server.getNetworkManager().getOutgoingQueue()->push(heldItemPacket); - - // 2. Send player position and look - 0x41 - Packet* positionPacket = new Packet(*packet); - synchronizePlayerPositionPacket(*positionPacket, server); // rename packet - server.getNetworkManager().getOutgoingQueue()->push(positionPacket); - - } else if (packet->getId() == 0x04) { - // Keep Alive (configuration) - g_logger->logNetwork(INFO, "Received Keep Alive in Configuration state", "Configuration"); - packet->setReturnPacket(PACKET_OK); - - } else if (packet->getId() == 0x05) { - // Pong (configuration) - g_logger->logNetwork(INFO, "Received Pong in Configuration state", "Configuration"); - packet->setReturnPacket(PACKET_OK); - - } else if (packet->getId() == 0x06) { - // Resource Pack Response (configuration) - g_logger->logNetwork(INFO, "Received Resource Pack Response in Configuration state", "Configuration"); - packet->setReturnPacket(PACKET_OK); - - } else if (packet->getId() == 0x07) { - // Serverbound Known Packs (configuration) - g_logger->logNetwork(INFO, "Received Serverbound Known Packs in Configuration state", "Configuration"); - serverboundKnownPacksPacket(*packet); - - packet->setReturnPacket(PACKET_OK); - - // Send complete configuration sequence - sendCompleteConfigurationSequence(packet, server); - - } else if (packet->getId() == 0x08) { - // Custom Click Action (configuration) - g_logger->logNetwork(INFO, "Serverbound known packs", "Configuration"); - packet->setReturnPacket(PACKET_OK); - - } else { - // g_logger->logNetwork(WARN, "Unknown packet ID in Configuration state: 0x" + std::to_string(packet->getId()), "PacketRouter"); - sendDisconnectPacket(packet, "Unknown packet in Configuration state", server); - packet->getPlayer()->setPlayerState(PlayerState::None); - packet->setReturnPacket(PACKET_DISCONNECT); - } + switch (packet->getId()) { + case 0x00: + // Client information packet + g_logger->logNetwork(INFO, "Received Client Information in Configuration state", "Configuration"); + handleClientInformationPacket(*packet, server); + break; + + case 0x01: + // Cookie response in configuration + g_logger->logNetwork(INFO, "Received Cookie Response in Configuration state", "Configuration"); + packet->setReturnPacket(PACKET_OK); + break; + + case 0x02: + // Serverbound plugin message + g_logger->logNetwork( + INFO, + "Received Serverbound Plugin Message (0x02), size: " + std::to_string(packet->getSize()) + " bytes", + "PacketRouter" + ); + packet->setReturnPacket(PACKET_OK); + break; + + case 0x03: + // Acknowledge finish configuration - transition to play state + handleAcknowledgeFinishConfigurationPacket(*packet, server); + sendPlayPacket(*packet, server); // 1. Send Login (play) packet - 0x2B + changeDifficultyPacket(*packet, server); // 2. Send Change Difficulty - 0x42 + playerAbilitiesPacket(*packet, server); // 3. Send Player Abilities - 0x39 + setHeldItemPacket(*packet, server); // 4. Set held item + synchronizePlayerPositionPacket(*packet, server); // 5. Send player position and look - 0x41 + break; + + case 0x04: + // Keep alive in configuration + g_logger->logNetwork(INFO, "Received Keep Alive in Configuration state", "Configuration"); + packet->setReturnPacket(PACKET_OK); + break; + + case 0x05: + // Pong in configuration + g_logger->logNetwork(INFO, "Received Pong in Configuration state", "Configuration"); + packet->setReturnPacket(PACKET_OK); + break; + + case 0x06: + // Resource pack response + g_logger->logNetwork(INFO, "Received Resource Pack Response in Configuration state", "Configuration"); + packet->setReturnPacket(PACKET_OK); + break; + + case 0x07: + // Serverbound known packs - finalize configuration + g_logger->logNetwork(INFO, "Received Serverbound Known Packs in Configuration state", "Configuration"); + serverboundKnownPacksPacket(*packet); + packet->setReturnPacket(PACKET_OK); + sendRegistryData(*packet, server); // Send registry data + sendUpdateTags(*packet, server); // Send update tags + handleFinishConfigurationPacket(*packet, server); + break; + + default: + // Unknown packet in configuration state - disconnect + packet->getPlayer()->setPlayerState(PlayerState::None); + packet->setReturnPacket(PACKET_DISCONNECT); + break; + } } // ======================================== @@ -207,153 +217,19 @@ void handleConfigurationState(Packet* packet, Server& server) { // ======================================== void handlePlayState(Packet* packet, Server& server) { - if (packet->getId() == 0x00) { - // Confirm Teleportation - handleConfirmTeleportationPacket(*packet, server); - - // Send Game Event packet - 0x42 - g_logger->logNetwork(INFO, "Sending Game Event packet", "PacketRouter"); - Packet* gameEvent = new Packet(*packet); - gameEventPacket(*gameEvent, server); - server.getNetworkManager().getOutgoingQueue()->push(gameEvent); - - // 2. Send Set Center Chunk - 0x57 - // Packet* setCenterPacket = new Packet(*packet); - // writeSetCenterPacket(*setCenterPacket, server); - // server.getNetworkManager().getOutgoingQueue()->push(setCenterPacket); - - // 3. Send Level Chunk With Light - 0x22 - // Packet* levelChunkPacket = new Packet(*packet); - // levelChunkWithLight(*levelChunkPacket, server); - // server.getNetworkManager().getOutgoingQueue()->push(levelChunkPacket); - - } else if (packet->getId() == 0x2B) { - // Playere loaded - g_logger->logNetwork(DEBUG, "Player Load========================", "PacketRouter"); - packet->setReturnPacket(PACKET_OK); - } else { - // Other play packets - // g_logger->logNetwork(INFO, "Received packet ID: " + std::to_string(packet->getId()) + " - // in Play state", "Play"); - packet->setReturnPacket(PACKET_OK); - } -} - -// ======================================== -// Disconnect Packet Creation -// ======================================== - -void sendDisconnectPacket(Packet* packet, const std::string& reason, Server& server) { - ThreadSafeQueue* outgoingPackets = server.getNetworkManager().getOutgoingQueue(); - if (!packet || !outgoingPackets) return; - - Player* player = packet->getPlayer(); - if (!player) return; - - g_logger->logNetwork(INFO, "Sending disconnect packet to player with reason: " + reason, "PacketRouter"); - - try { - // Create Disconnect packet (0x02 in Configuration state) - Buffer payload; - payload.writeVarInt(0x02); // Disconnect packet ID - - // Create JSON reason - std::string jsonReason = "{\"text\":\"" + reason + "\"}"; - payload.writeString(jsonReason); - - Buffer final; - final.writeVarInt(payload.getData().size()); - final.writeBytes(payload.getData()); - - // Create new packet for disconnect - Packet* disconnectPacket = new Packet(*packet); - disconnectPacket->getData() = final; - disconnectPacket->setReturnPacket(PACKET_SEND); - disconnectPacket->setPacketSize(final.getData().size()); - - outgoingPackets->push(disconnectPacket); - - g_logger->logNetwork(INFO, "Disconnect packet queued for sending", "PacketRouter"); - } catch (const std::exception& e) { - g_logger->logNetwork(ERROR, "Error creating disconnect packet: " + std::string(e.what()), "PacketRouter"); - } -} - -// ======================================== -// Game Initialization Sequence -// ======================================== - -// void initGameSequence(Packet* packet, Server& server) { -// if (packet == nullptr || server.getNetworkManager().getOutgoingQueue() == nullptr) return; - -// Player* player = packet->getPlayer(); -// if (player == nullptr) return; - -// // Player should already be in Play state at this point -// g_logger->logNetwork(INFO, "Starting game sequence for player: " + player->getPlayerName(), "PacketRouter"); - -// try { - -// // 5. Send spawn position - 0x5A -// // Packet* spawnPacket = new Packet(*packet); -// // sendSpawnPosition(*spawnPacket, server); -// // server.getNetworkManager().getOutgoingQueue()->push(spawnPacket); - -// // // 6. Complete spawn sequence (abilities, health, experience, time, held item) -// completeSpawnSequence(*packet, server); - -// g_logger->logNetwork(INFO, "Complete game sequence sent to player: ", "PacketRouter"); - -// packet->setReturnPacket(PACKET_OK); -// } catch (const std::exception& e) { -// g_logger->logNetwork(ERROR, "Error in game sequence: " + std::string(e.what()), "PacketRouter"); -// packet->setReturnPacket(PACKET_ERROR); -// } -// } - -// ======================================== -// Complete Configuration Sequence -// ======================================== - -void sendCompleteConfigurationSequence(Packet* packet, Server& server) { - if (!packet || !server.getNetworkManager().getOutgoingQueue()) { - g_logger->logNetwork(ERROR, "Invalid packet or server in configuration sequence", "Configuration"); - return; - } - - Player* player = packet->getPlayer(); - if (!player) { - g_logger->logNetwork(ERROR, "No player found for configuration sequence", "Configuration"); - return; - } - - g_logger->logNetwork(INFO, "=== Starting Complete Configuration Sequence ===", "Configuration"); - - try { - // 1. Send Registry Data (this will queue multiple packets, one per registry) - g_logger->logNetwork(INFO, "Step 1: Sending Registry Data", "Configuration"); - sendRegistryData(*packet, server); - - // 2. Send Update Tags after Registry Data - g_logger->logNetwork(INFO, "Step 2: Sending Update Tags", "Configuration"); - sendUpdateTags(*packet, server); - - // 3. TODO: Add other configuration packets here if needed - // - Feature Flags - // - Data Packs - // - Resource Packs - // - Custom Payloads - - // 4. Send Finish Configuration to complete the sequence - g_logger->logNetwork(INFO, "Step 3: Sending Finish Configuration", "Configuration"); - Packet* finishPacket = new Packet(*packet); - handleFinishConfigurationPacket(*finishPacket, server); - server.getNetworkManager().getOutgoingQueue()->push(finishPacket); - - g_logger->logNetwork(INFO, "=== Configuration Sequence Completed Successfully ===", "Configuration"); - - } catch (const std::exception& e) { - g_logger->logNetwork(ERROR, "Error in configuration sequence: " + std::string(e.what()), "Configuration"); - packet->setReturnPacket(PACKET_ERROR); - } + switch (packet->getId()) { + case 0x00: + // Confirm teleportation packet + handleConfirmTeleportationPacket(*packet, server); + gameEventPacket(*packet, server); // Send Game Event packet - 0x42 + setCenterPacket(*packet, server); // Send Set Center Chunk - 0x57 + // TODO: Send Level Chunk With Light - 0x22 + packet->setReturnPacket(PACKET_OK); // temp + break; + + default: + // Other play state packets - acknowledge for now + packet->setReturnPacket(PACKET_OK); + break; + } } diff --git a/src/networking/networkQueuer.cpp b/src/networking/networkQueuer.cpp index 00352d0..0d6dd24 100644 --- a/src/networking/networkQueuer.cpp +++ b/src/networking/networkQueuer.cpp @@ -89,9 +89,9 @@ void NetworkManager::senderThreadLoop() { if (p == nullptr) break; try { - // g_logger->logNetwork(INFO, "Sending packet to player", "Network Manager"); - std::cout << "Sending packet 0x" << std::hex << p->getId() << " (" << std::dec << p->getData().getData().size() << " bytes)" - << std::endl; + // g_logger->logNetwork(INFO, "to player", "Network Manager"); + // std::cout << "Sending packet 0x" << std::hex << p->getId() << " (" << std::dec << p->getData().getData().size() << " bytes)" + // << std::endl; send(p->getSocket(), p->getData().getData().data(), p->getSize(), MSG_NOSIGNAL); if (p->getPlayer() && p->getPlayer()->getPlayerState() == PlayerState::None) { diff --git a/src/networking/networkWorker.cpp b/src/networking/networkWorker.cpp index 75ec463..82cc6bd 100644 --- a/src/networking/networkWorker.cpp +++ b/src/networking/networkWorker.cpp @@ -2,6 +2,7 @@ #include "network/networking.hpp" #include "network/packet.hpp" #include "network/server.hpp" +#include "packetRouter.hpp" #include "player.hpp" #include @@ -17,11 +18,8 @@ void NetworkManager::workerThreadLoop() { if (_incomingPackets.waitAndPopTimeout(packet, std::chrono::milliseconds(100))) { if (packet == nullptr) break; try { - - // g_logger->logNetwork(INFO, "Handling incoming data for player", "Worker"); packetRouter(packet, getServer()); if (packet->getReturnPacket() == PACKET_SEND) { - _outgoingPackets.push(packet); packet = nullptr; } else if (packet->getReturnPacket() == PACKET_DISCONNECT) { Player* player = packet->getPlayer(); diff --git a/src/networking/packet/chunkBatch.cpp b/src/networking/packet/chunkBatch.cpp deleted file mode 100644 index 32bdab3..0000000 --- a/src/networking/packet/chunkBatch.cpp +++ /dev/null @@ -1,125 +0,0 @@ -// #include "lib/UUID.hpp" -// #include "network/buffer.hpp" -// #include "network/networking.hpp" -// #include "network/packet.hpp" -// #include "network/server.hpp" -// #include "player.hpp" - -// #include - -// void sendChunkBatchStart(Packet& packet, Server& server) { -// std::cout << "=== Sending Chunk Batch Start ===\n"; - -// // Chunk Batch Start has no fields - just the packet ID -// Buffer buf; -// // No data to write for this packet - -// int packetId = 0x0C; // Chunk Batch Start packet ID for protocol 770 -// int packetIdSize = packet.getVarintSize(packetId); -// int totalPayloadSize = packetIdSize + buf.getData().size(); - -// Buffer finalBuf; -// finalBuf.writeVarInt(totalPayloadSize); -// finalBuf.writeVarInt(packetId); -// if (!buf.getData().empty()) { -// finalBuf.writeBytes(buf.getData()); -// } - -// packet.getData() = finalBuf; -// packet.setPacketSize(finalBuf.getData().size()); -// packet.setReturnPacket(PACKET_SEND); - -// (void)server; -// } - -// void sendChunkBatchFinished(Packet& packet, Server& server, int batchSize) { -// std::cout << "=== Sending Chunk Batch Finished (batch size: " << batchSize << ") ===\n"; - -// Buffer buf; -// buf.writeVarInt(batchSize); // Number of chunks in the batch - -// int packetId = 0x0B; // Chunk Batch Finished packet ID for protocol 770 -// int packetIdSize = packet.getVarintSize(packetId); -// int totalPayloadSize = packetIdSize + buf.getData().size(); - -// Buffer finalBuf; -// finalBuf.writeVarInt(totalPayloadSize); -// finalBuf.writeVarInt(packetId); -// finalBuf.writeBytes(buf.getData()); - -// packet.getData() = finalBuf; -// packet.setPacketSize(finalBuf.getData().size()); -// packet.setReturnPacket(PACKET_SEND); - -// (void)server; -// } - -// void sendChunkBatchSequence(Packet& packet, Server& server) { -// Player* player = packet.getPlayer(); -// ThreadSafeQueue* outgoingPackets = server.getNetworkManager().getOutgoingQueue(); -// if (!player || !outgoingPackets) return; - -// int playerChunkX = 0; -// int playerChunkZ = 0; -// int viewDistance = 3; // Reasonable size - -// std::cout << "=== Starting chunk batch sequence for player: " << player->getPlayerName() << " (view distance: " << viewDistance << ") ===\n"; - -// // 1. Send Chunk Batch Start -// try { -// Packet* batchStartPacket = new Packet(packet); -// sendChunkBatchStart(*batchStartPacket, server); -// outgoingPackets->push(batchStartPacket); -// } catch (const std::exception& e) { -// std::cerr << "Error sending chunk batch start: " << e.what() << std::endl; -// return; -// } - -// // 2. Send chunks in smaller batches -// int chunksCount = 0; -// int batchSize = 0; -// const int MAX_BATCH_SIZE = 16; // Limit chunks per batch - -// for (int x = playerChunkX - viewDistance; x <= playerChunkX + viewDistance; x++) { -// for (int z = playerChunkZ - viewDistance; z <= playerChunkZ + viewDistance; z++) { -// try { -// Packet* chunkPacket = new Packet(packet); -// sendChunkData(*chunkPacket, server, x, z); -// outgoingPackets->push(chunkPacket); -// chunksCount++; -// batchSize++; - -// // Send batch finished and start new batch if we hit limit -// if (batchSize >= MAX_BATCH_SIZE) { -// // Send batch finished -// Packet* batchFinishedPacket = new Packet(packet); -// sendChunkBatchFinished(*batchFinishedPacket, server, batchSize); -// outgoingPackets->push(batchFinishedPacket); - -// // Start new batch -// Packet* batchStartPacket = new Packet(packet); -// sendChunkBatchStart(*batchStartPacket, server); -// outgoingPackets->push(batchStartPacket); - -// batchSize = 0; -// } - -// } catch (const std::exception& e) { -// std::cerr << "Error sending chunk (" << x << ", " << z << "): " << e.what() << std::endl; -// } -// } -// } - -// // 3. Send final batch finished -// if (batchSize > 0) { -// try { -// Packet* batchFinishedPacket = new Packet(packet); -// sendChunkBatchFinished(*batchFinishedPacket, server, batchSize); -// outgoingPackets->push(batchFinishedPacket); -// } catch (const std::exception& e) { -// std::cerr << "Error sending chunk batch finished: " << e.what() << std::endl; -// } -// } - -// std::cout << "=== Chunk batch sequence completed: " << chunksCount << " chunks sent ===\n"; -// } diff --git a/src/networking/packet/chunkDataSimple.cpp b/src/networking/packet/chunkDataSimple.cpp deleted file mode 100644 index 9003edb..0000000 --- a/src/networking/packet/chunkDataSimple.cpp +++ /dev/null @@ -1,202 +0,0 @@ -// #include "lib/UUID.hpp" -// #include "network/buffer.hpp" -// #include "network/networking.hpp" -// #include "network/packet.hpp" -// #include "network/server.hpp" -// #include "player.hpp" - -// #include - -// void sendChunkData(Packet& packet, Server& server, int chunkX, int chunkZ) { -// std::cout << "=== Sending Chunk Data (" << chunkX << ", " << chunkZ << ") ===\n"; - -// Buffer buf; - -// try { -// // Use your new chunk loading system -// World::Query query = server.getWorldQuery(); -// World::ChunkData chunkData = query.fetchChunk(chunkX, chunkZ); - -// // Write chunk coordinates -// buf.writeInt(chunkX); -// buf.writeInt(chunkZ); - -// // Write heightmaps (simplified) -// if (!chunkData.heightmaps.empty()) { -// buf.writeBytes(chunkData.heightmaps); -// } else { -// // Empty heightmap -// buf.writeByte(0x0A); // Compound tag -// buf.writeByte(0x00); // Empty name length -// buf.writeByte(0x00); // End tag -// } - -// // Write chunk data -// if (!chunkData.blockData.empty()) { -// buf.writeVarInt(chunkData.blockData.size()); -// buf.writeBytes(chunkData.blockData); -// } else { -// // Generate simple empty chunk inline -// Buffer emptyData; -// const int NUM_SECTIONS = 24; - -// for (int section = 0; section < NUM_SECTIONS; section++) { -// emptyData.writeByte(0x00); // Block count low -// emptyData.writeByte(0x00); // Block count high - -// // Block states - single air block -// emptyData.writeByte(0); // Bits per entry -// emptyData.writeVarInt(0); // Air block state -// emptyData.writeVarInt(0); // No data array - -// // Biomes - single plains biome -// emptyData.writeByte(0); // Bits per entry -// emptyData.writeVarInt(1); // Plains biome -// emptyData.writeVarInt(0); // No data array -// } - -// buf.writeVarInt(emptyData.getData().size()); -// buf.writeBytes(emptyData.getData()); -// } - -// // Block entities -// buf.writeVarInt(0); - -// // Light data (simplified version) -// buf.writeVarInt(1); // Sky light mask length -// buf.writeInt64(0x1FFFFFF); // All sections have sky light -// buf.writeVarInt(1); // Block light mask length -// buf.writeInt64(0); // No block light -// buf.writeVarInt(1); // Empty sky light mask length -// buf.writeInt64(0); // No empty sky light sections -// buf.writeVarInt(1); // Empty block light mask length -// buf.writeInt64(0); // No empty block light sections - -// // Sky light arrays -// for (int i = 0; i < 25; i++) { -// buf.writeVarInt(2048); -// for (int j = 0; j < 2048; j++) { -// buf.writeByte(0xFF); -// } -// } - -// } catch (const std::exception& e) { -// std::cerr << "Error in sendChunkData: " << e.what() << std::endl; -// // Return without setting packet data - this will cause the packet to be skipped -// return; -// } - -// // Create final packet -// int packetId = 0x27; -// int packetIdSize = packet.getVarintSize(packetId); -// int totalPayloadSize = packetIdSize + buf.getData().size(); - -// Buffer finalBuf; -// finalBuf.writeVarInt(totalPayloadSize); -// finalBuf.writeVarInt(packetId); -// finalBuf.writeBytes(buf.getData()); - -// packet.getData() = finalBuf; -// packet.setPacketSize(finalBuf.getData().size()); -// packet.setReturnPacket(PACKET_SEND); - -// (void)server; -// } - -// // Helper function to generate empty chunk sections -// Buffer generateEmptyChunkSections() { -// Buffer chunkData; -// const int NUM_SECTIONS = 24; // Sections for world height -64 to 319 - -// for (int section = 0; section < NUM_SECTIONS; section++) { -// // Block count (non-air blocks) - 0 for empty sections -// chunkData.writeByte(0x00); -// chunkData.writeByte(0x00); - -// // Block states palette - single entry for air -// chunkData.writeByte(0); // Bits per entry (0 = single valued) -// chunkData.writeVarInt(0); // Air block state ID -// chunkData.writeVarInt(0); // No data array needed for single valued - -// // Biomes palette - single entry for plains -// chunkData.writeByte(0); // Bits per entry (0 = single valued) -// chunkData.writeVarInt(1); // Plains biome ID -// chunkData.writeVarInt(0); // No data array needed for single valued -// } - -// return chunkData; -// } - -// // Helper function to write light data from loaded chunk -// void writeLightData(Buffer& buf, const World::ChunkData& chunkData) { -// if (!chunkData.skyLight.empty() || !chunkData.blockLight.empty()) { -// // Use actual light data from chunk -// writeActualLightData(buf, chunkData); -// } else { -// // Fallback to default light data -// writeEmptyLightData(buf); -// } -// } - -// void writeActualLightData(Buffer& buf, const World::ChunkData& chunkData) { -// // This is complex - you need to reconstruct the light data format -// // For now, fallback to empty light data -// // TODO: Implement proper light data reconstruction from chunkData.skyLight and chunkData.blockLight -// writeEmptyLightData(buf); -// } - -// void writeEmptyLightData(Buffer& buf) { -// // Sky Light Mask (BitSet) - all sections have sky light -// buf.writeVarInt(1); -// buf.writeInt64(0x1FFFFFF); - -// // Block Light Mask (BitSet) - no block light -// buf.writeVarInt(1); -// buf.writeInt64(0); - -// // Empty Sky Light Mask -// buf.writeVarInt(1); -// buf.writeInt64(0); - -// // Empty Block Light Mask -// buf.writeVarInt(1); -// buf.writeInt64(0); - -// // Sky Light arrays (2048 bytes each for sections with sky light) -// for (int i = 0; i < 25; i++) { -// buf.writeVarInt(2048); -// for (int j = 0; j < 2048; j++) { -// buf.writeByte(0xFF); // Full sky light -// } -// } -// // No Block Light arrays since mask is 0 -// } - -// void sendSpawnPosition(Packet& packet, Server& server) { -// std::cout << "=== Sending Spawn Position ===\n"; - -// Buffer buf; - -// // Encode position as long (X=0, Y=64, Z=0 packed into 64 bits) -// // Position format: ((x & 0x3FFFFFF) << 38) | ((z & 0x3FFFFFF) << 12) | (y & 0xFFF) -// int64_t encodedPos = ((int64_t)0 << 38) | ((int64_t)0 << 12) | (64 & 0xFFF); -// buf.writeInt64(encodedPos); - -// // Spawn angle (0.0f as int bits) -// buf.writeInt(0); - -// int packetId = 0x5A; // Set Default Spawn Position packet ID for protocol 770 -// int packetIdSize = packet.getVarintSize(packetId); -// int totalPayloadSize = packetIdSize + buf.getData().size(); - -// Buffer finalBuf; -// finalBuf.writeVarInt(totalPayloadSize); -// finalBuf.writeVarInt(packetId); -// finalBuf.writeBytes(buf.getData()); - -// packet.getData() = finalBuf; -// packet.setPacketSize(finalBuf.getData().size()); -// packet.setReturnPacket(PACKET_SEND); - -// (void)server; -// } diff --git a/src/networking/packet/clientbound/PlayPacket.cpp b/src/networking/packet/clientbound/PlayPacket.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/networking/packet/clientbound/changeDifficultyPacket.cpp b/src/networking/packet/clientbound/changeDifficultyPacket.cpp index a495b1d..10476af 100644 --- a/src/networking/packet/clientbound/changeDifficultyPacket.cpp +++ b/src/networking/packet/clientbound/changeDifficultyPacket.cpp @@ -1,18 +1,11 @@ #include "buffer.hpp" #include "packet.hpp" -void changeDifficultyPacket(Packet& packet) { +void changeDifficultyPacket(Packet& packet, Server& server) { Buffer buff; - buff.writeByte(0x0A); buff.writeByte(2); // 0 Peaceful; 1 Easy; 2 Normal; 3 Hard buff.writeBool(true); // Is Difficulty locked ? - Buffer final; - final.writeVarInt(buff.getData().size()); - final.writeBytes(buff.getData()); - - packet.getData() = final; - packet.setPacketSize(final.getData().size()); - packet.setReturnPacket(PACKET_SEND); + packet.sendPacket(0x0A, buff, server, false); } diff --git a/src/networking/packet/clientbound/clientboundKnownPacksPacket.cpp b/src/networking/packet/clientbound/clientboundKnownPacksPacket.cpp index 1d5481b..7033b7d 100644 --- a/src/networking/packet/clientbound/clientboundKnownPacksPacket.cpp +++ b/src/networking/packet/clientbound/clientboundKnownPacksPacket.cpp @@ -1,21 +1,15 @@ #include "buffer.hpp" #include "network/packet.hpp" +#include "server.hpp" // If implementing data packs we should actually send datapack info with the loaded datapacks -void clientboundKnownPacksPacket(Packet& packet) { +void clientboundKnownPacksPacket(Packet& packet, Server& server) { Buffer buffer; - buffer.writeByte(0x0E); buffer.writeVarInt(1); buffer.writeString("minecraft"); buffer.writeString("core"); buffer.writeString("1.21.5"); - Buffer final; - final.writeVarInt(buffer.getData().size()); - final.writeBytes(buffer.getData()); - - packet.getData() = final; - packet.setPacketSize(final.getData().size()); - packet.setReturnPacket(PACKET_SEND); + packet.sendPacket(0x0E, buffer, server, true); } diff --git a/src/networking/packet/clientbound/gameEventPacket.cpp b/src/networking/packet/clientbound/gameEventPacket.cpp index 706d860..b82d716 100644 --- a/src/networking/packet/clientbound/gameEventPacket.cpp +++ b/src/networking/packet/clientbound/gameEventPacket.cpp @@ -10,19 +10,8 @@ void gameEventPacket(Packet& packet, Server& server) { Buffer buf; - buf.writeByte(0x22); buf.writeByte(13); buf.writeFloat(0); - - Buffer final; - - final.writeVarInt(buf.getData().size()); - final.writeBytes(buf.getData()); - - packet.setPacketId(0x22); - packet.getData() = final; - packet.setPacketSize(final.getData().size()); - packet.setReturnPacket(PACKET_SEND); - (void)server; + packet.sendPacket(0x22, buf, server, false); } diff --git a/src/networking/packet/clientbound/handleFinishConfiguration.cpp b/src/networking/packet/clientbound/handleFinishConfiguration.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/networking/packet/clientbound/handleFinishConfigurationPacket.cpp b/src/networking/packet/clientbound/handleFinishConfigurationPacket.cpp index cc8ca52..7291765 100644 --- a/src/networking/packet/clientbound/handleFinishConfigurationPacket.cpp +++ b/src/networking/packet/clientbound/handleFinishConfigurationPacket.cpp @@ -1,3 +1,4 @@ +#include "buffer.hpp" #include "network/networking.hpp" #include "network/packet.hpp" #include "network/server.hpp" @@ -13,23 +14,7 @@ void handleFinishConfigurationPacket(Packet& packet, Server& server) { return; } - // g_logger->logNetwork(INFO, "Sending Finish Configuration packet to " + - // player->getPlayerName(), "Configuration"); + Buffer buf; - // Send Finish Configuration packet (0x03) - Buffer payload; - payload.writeVarInt(0x03); // Finish Configuration packet ID - - Buffer final; - final.writeVarInt(payload.getData().size()); - final.writeBytes(payload.getData()); - - packet.getData() = final; - packet.setReturnPacket(PACKET_SEND); - packet.setPacketSize(final.getData().size()); - - // g_logger->logNetwork(INFO, "Finish Configuration packet sent, waiting for client - // acknowledgment", "Configuration"); - - (void)server; // Suppress unused parameter warning + packet.sendPacket(0x03, buf, server, true); } diff --git a/src/networking/packet/clientbound/handleLoginStartPacket.cpp b/src/networking/packet/clientbound/handleLoginStartPacket.cpp index 721af5d..d72f3b9 100644 --- a/src/networking/packet/clientbound/handleLoginStartPacket.cpp +++ b/src/networking/packet/clientbound/handleLoginStartPacket.cpp @@ -19,51 +19,10 @@ void handleLoginStartPacket(Packet& packet, Server& server) { UUID uuid = UUID::fromOfflinePlayer(username); player->setUUID(uuid); - // Build Login Success packet payload - Buffer payload; - payload.writeUUID(uuid); - payload.writeString(username); - payload.writeVarInt(0); // properties length (no properties) + Buffer buff; + buff.writeUUID(uuid); + buff.writeString(username); + buff.writeVarInt(0); // properties lenght (no properties) - // Debug: Log the raw payload bytes - std::string payloadHex = ""; - for (size_t i = 0; i < payload.getData().size(); i++) { - char hex[3]; - sprintf(hex, "%02x", payload.getData()[i]); - payloadHex += hex; - if (i < payload.getData().size() - 1) payloadHex += " "; - } - g_logger->logNetwork(INFO, "Login Success payload bytes: " + payloadHex, "Login"); - - // Calculate total packet size (packet ID + payload) - int packetId = 0x02; - int payloadSize = payload.getData().size(); - int packetIdVarintSize = packet.getVarintSize(packetId); - int totalPayloadSize = packetIdVarintSize + payloadSize; - - Buffer final; - final.writeVarInt(totalPayloadSize); // Total packet size - final.writeVarInt(packetId); // Login Success packet ID (0x02) - final.writeBytes(payload.getData()); // UUID + username + properties - - packet.getData() = final; - packet.setReturnPacket(PACKET_SEND); - packet.setPacketSize(final.getData().size()); - - // Debug: Log the complete packet bytes - std::string finalHex = ""; - for (size_t i = 0; i < final.getData().size(); i++) { - char hex[3]; - sprintf(hex, "%02x", final.getData()[i]); - finalHex += hex; - if (i < final.getData().size() - 1) finalHex += " "; - } - g_logger->logNetwork(INFO, "Complete Login Success packet bytes: " + finalHex, "Login"); - - // Don't transition to Configuration yet - wait for Login Acknowledged - g_logger->logNetwork(INFO, - "Login Success sent for user: " + username + ", UUID: " + uuid.toString() + - ", packet size: " + std::to_string(final.getData().size()), - "Login"); - (void)server; + packet.sendPacket(0x02, buff, server, true); } diff --git a/src/networking/packet/clientbound/handlePingPacket.cpp b/src/networking/packet/clientbound/handlePingPacket.cpp index 7d3bd8c..4399b16 100644 --- a/src/networking/packet/clientbound/handlePingPacket.cpp +++ b/src/networking/packet/clientbound/handlePingPacket.cpp @@ -15,25 +15,9 @@ void handlePingPacket(Packet& packet, Server& server) { long timestamp = packet.getData().readInt64(); - // g_logger->logNetwork(INFO, "Received ping request with timestamp: " + - // std::to_string(timestamp), "Ping"); - - int packetId = 0x01; - int packetIdVarintSize = packet.getVarintSize(packetId); - int totalPayloadSize = packetIdVarintSize + 8; - Buffer buf; - buf.writeVarInt(totalPayloadSize); - buf.writeVarInt(packetId); - buf.writeInt64(timestamp); - packet.getData() = buf; - packet.setReturnPacket(PACKET_SEND); - packet.setPacketSize(buf.getData().size()); - packet.getPlayer()->setPlayerState(PlayerState::None); - - // g_logger->logNetwork(INFO, "Pong response ready - echoing timestamp " + - // std::to_string(timestamp), "Ping"); + buf.writeInt64(timestamp); - (void)server; + packet.sendPacket(0x01, buf, server, true); } diff --git a/src/networking/packet/clientbound/handleStatusPacket.cpp b/src/networking/packet/clientbound/handleStatusPacket.cpp index 7476c2a..bd10bfc 100644 --- a/src/networking/packet/clientbound/handleStatusPacket.cpp +++ b/src/networking/packet/clientbound/handleStatusPacket.cpp @@ -26,19 +26,9 @@ void handleStatusPacket(Packet& packet, Server& server) { Buffer buf; int jsonLen = payload.size(); - int packetId = 0x00; - int packetIdVarintSize = packet.getVarintSize(packetId); - int jsonLenVarintSize = packet.getVarintSize(jsonLen); - int totalPayloadSize = packetIdVarintSize + jsonLenVarintSize + jsonLen; - - - buf.writeVarInt(totalPayloadSize); - buf.writeVarInt(packetId); buf.writeVarInt(jsonLen); buf.writeBytes(payload.c_str()); - packet.getData() = buf; - packet.setReturnPacket(PACKET_SEND); - packet.setPacketSize(buf.getData().size()); - packet.getPlayer()->setPlayerState(PlayerState::Status); + + packet.sendPacket(0x00, buf, server, true); } diff --git a/src/networking/packet/clientbound/playerAbilitiesPacket.cpp b/src/networking/packet/clientbound/playerAbilitiesPacket.cpp index 0e4c0cf..6997543 100644 --- a/src/networking/packet/clientbound/playerAbilitiesPacket.cpp +++ b/src/networking/packet/clientbound/playerAbilitiesPacket.cpp @@ -1,19 +1,12 @@ #include "buffer.hpp" #include "packet.hpp" -void playerAbilitiesPacket(Packet& packet) { +void playerAbilitiesPacket(Packet& packet, Server& server) { Buffer buff; - buff.writeByte(0x39); buff.writeByte(0x08); // Invulnerable 0x01; Flying 0x02; Allow Flying 0x04; Creative Mode 0x08; buff.writeFloat(0.05); // Flight speed buff.writeFloat(1); // Fov modifier - Buffer final; - final.writeVarInt(buff.getData().size()); - final.writeBytes(buff.getData()); - - packet.getData() = final; - packet.setPacketSize(final.getData().size()); - packet.setReturnPacket(PACKET_SEND); + packet.sendPacket(0x39, buff, server, false); } diff --git a/src/networking/packet/clientbound/setCenterChunck.cpp b/src/networking/packet/clientbound/setCenterChunck.cpp new file mode 100644 index 0000000..7299723 --- /dev/null +++ b/src/networking/packet/clientbound/setCenterChunck.cpp @@ -0,0 +1,15 @@ +#include "lib/UUID.hpp" +#include "network/buffer.hpp" +#include "network/networking.hpp" +#include "network/packet.hpp" +#include "network/server.hpp" +#include "player.hpp" + +void setCenterPacket(Packet& packet, Server& server) { + + Buffer buf; + buf.writeVarInt(0); + buf.writeVarInt(0); + + packet.sendPacket(0x57, buf, server, false); +} diff --git a/src/networking/packet/clientbound/setHeldItemPacket.cpp b/src/networking/packet/clientbound/setHeldItemPacket.cpp index 5670fb9..69df5af 100644 --- a/src/networking/packet/clientbound/setHeldItemPacket.cpp +++ b/src/networking/packet/clientbound/setHeldItemPacket.cpp @@ -1,17 +1,11 @@ #include "buffer.hpp" #include "packet.hpp" +#include "server.hpp" -void setHeldItemPacket(Packet& packet) { +void setHeldItemPacket(Packet& packet, Server& server) { Buffer buff; - buff.writeByte(0x62); buff.writeVarInt(3); // 0-8 hand slots --> Should get it from player data when implemented - Buffer final; - final.writeVarInt(buff.getData().size()); - final.writeBytes(buff.getData()); - - packet.getData() = final; - packet.setPacketSize(final.getData().size()); - packet.setReturnPacket(PACKET_SEND); + packet.sendPacket(0x62, buff, server, false); } diff --git a/src/networking/packet/clientbound/synchronizePlayerPositionPacket.cpp b/src/networking/packet/clientbound/synchronizePlayerPositionPacket.cpp index 6b74fd5..6b307ec 100644 --- a/src/networking/packet/clientbound/synchronizePlayerPositionPacket.cpp +++ b/src/networking/packet/clientbound/synchronizePlayerPositionPacket.cpp @@ -37,18 +37,5 @@ void synchronizePlayerPositionPacket(Packet& packet, Server& server) { // Flags (0x00 = absolute positioning) buf.writeInt(0x00); - int packetId = 0x41; - int packetIdSize = packet.getVarintSize(packetId); - int totalPayloadSize = packetIdSize + buf.getData().size(); - - Buffer finalBuf; - finalBuf.writeVarInt(totalPayloadSize); - finalBuf.writeVarInt(packetId); - finalBuf.writeBytes(buf.getData()); - - packet.getData() = finalBuf; - packet.setPacketSize(finalBuf.getData().size()); - packet.setReturnPacket(PACKET_SEND); - - (void)server; + packet.sendPacket(0x41, buf, server, true); } diff --git a/src/networking/packet/serverbound/handleAcknowledgeFinishConfigurationPacket.cpp b/src/networking/packet/serverbound/handleAcknowledgeFinishConfigurationPacket.cpp index c6b9a9b..82aa835 100644 --- a/src/networking/packet/serverbound/handleAcknowledgeFinishConfigurationPacket.cpp +++ b/src/networking/packet/serverbound/handleAcknowledgeFinishConfigurationPacket.cpp @@ -8,14 +8,8 @@ void handleAcknowledgeFinishConfigurationPacket(Packet& packet, Server& server) return; } - // Client has acknowledged finish configuration, now transition to Play state player->setPlayerState(PlayerState::Play); - - // g_logger->logNetwork(INFO, "Player " + player->getPlayerName() + " transitioned to Play state - // - ready for game packets", "Configuration"); - - // Just acknowledge the packet - the actual game sequence will be triggered separately packet.setReturnPacket(PACKET_OK); - (void)server; // Suppress unused parameter warning + (void)server; } diff --git a/src/networking/packet/serverbound/handleConfirmTeleportation.cpp b/src/networking/packet/serverbound/handleConfirmTeleportation.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/networking/packet/serverbound/handleConfirmTeleportationPacket.cpp b/src/networking/packet/serverbound/handleConfirmTeleportationPacket.cpp index 3be407c..8dbf4ab 100644 --- a/src/networking/packet/serverbound/handleConfirmTeleportationPacket.cpp +++ b/src/networking/packet/serverbound/handleConfirmTeleportationPacket.cpp @@ -1,17 +1,8 @@ #include "packet.hpp" #include "server.hpp" -#include void handleConfirmTeleportationPacket(Packet& packet, Server& server) { - std::cout << "=== Received Confirm Teleportation ===\n"; - - // Read teleport ID from packet data - int teleportId = packet.getData().readVarInt(); - - std::cout << "Player confirmed teleportation with ID: " << teleportId << std::endl; - - // Mark packet as processed + // int teleportId = packet.getData().readVarInt(); packet.setReturnPacket(PACKET_OK); - (void)server; } diff --git a/src/networking/packet/serverbound/handleLoginAcknowledgedPacket.cpp b/src/networking/packet/serverbound/handleLoginAcknowledgedPacket.cpp index f2165c2..38442b5 100644 --- a/src/networking/packet/serverbound/handleLoginAcknowledgedPacket.cpp +++ b/src/networking/packet/serverbound/handleLoginAcknowledgedPacket.cpp @@ -4,29 +4,12 @@ #include "player.hpp" void handleLoginAcknowledgedPacket(Packet& packet, Server& server) { - // g_logger->logNetwork(INFO, "Login Acknowledged received - transitioning to Configuration - // state", "Login"); - Player* player = packet.getPlayer(); if (!player) { - // g_logger->logNetwork(ERROR, "Error: No player associated with Login Acknowledged packet", - // "Login"); packet.setReturnPacket(PACKET_DISCONNECT); return; } - - // The Login Acknowledged packet has no payload (0 bytes), so nothing to read - - // The client has acknowledged the login success, now we officially transition to Configuration - // state player->setPlayerState(PlayerState::Configuration); - - // g_logger->logNetwork(INFO, "Player " + player->getPlayerName() + " successfully acknowledged - // login - transitioned to Configuration state", "Login"); - - // Just acknowledge the packet - don't send anything yet - // The client will send Client Information (packet 0x03) next in Configuration state packet.setReturnPacket(PACKET_OK); - - (void)server; // Suppress unused parameter warning + (void)server; } diff --git a/src/networking/packet/setCenterChunck.cpp b/src/networking/packet/setCenterChunck.cpp deleted file mode 100644 index 5e161be..0000000 --- a/src/networking/packet/setCenterChunck.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "lib/UUID.hpp" -#include "network/buffer.hpp" -#include "network/networking.hpp" -#include "network/packet.hpp" -#include "network/server.hpp" -#include "player.hpp" - -#include - -void writeSetCenterPacket(Packet& packet, Server& server) { - std::cout << "=== center chunk packet write init ===\n"; - - Buffer buf; - buf.writeVarInt(0); - buf.writeVarInt(0); - - int packetId = 0x57; - int packetIdSize = packet.getVarintSize(packetId); - int totalPayloadSize = packetIdSize + buf.getData().size(); - - Buffer finalBuf; - finalBuf.writeVarInt(totalPayloadSize); - finalBuf.writeVarInt(packetId); - finalBuf.writeBytes(buf.getData()); - - packet.getData() = finalBuf; - packet.setPacketSize(finalBuf.getData().size()); - packet.setReturnPacket(PACKET_SEND); - - (void)server; -} diff --git a/src/networking/packet/spawnSequence.cpp b/src/networking/packet/spawnSequence.cpp deleted file mode 100644 index be3867e..0000000 --- a/src/networking/packet/spawnSequence.cpp +++ /dev/null @@ -1,205 +0,0 @@ -#include "lib/UUID.hpp" -#include "network/buffer.hpp" -#include "network/networking.hpp" -#include "network/packet.hpp" -#include "network/server.hpp" -#include "player.hpp" - -#include - -void sendPlayerAbilities(Packet& packet, Server& server) { - std::cout << "=== Sending Player Abilities ===\n"; - - Buffer buf; - - // Flags (byte) - bit field for player abilities - uint8_t flags = 0x00; - // 0x01: Invulnerable - // 0x02: Flying - // 0x04: Allow Flying (creative mode) - // 0x08: Creative Mode (instant break) - buf.writeByte(flags); - - // Flying Speed (float) - 0.05 by default - // Convert 0.05f to IEEE 754 bits: 0x3D4CCCCD - buf.writeInt(0x3D4CCCCD); - - // Field of View Modifier (float) - 0.1 by default - // Convert 0.1f to IEEE 754 bits: 0x3DCCCCCD - buf.writeInt(0x3DCCCCCD); - - int packetId = 0x39; // Player Abilities packet ID for protocol 770 - int packetIdSize = packet.getVarintSize(packetId); - int totalPayloadSize = packetIdSize + buf.getData().size(); - - Buffer finalBuf; - finalBuf.writeVarInt(totalPayloadSize); - finalBuf.writeVarInt(packetId); - finalBuf.writeBytes(buf.getData()); - - packet.getData() = finalBuf; - packet.setPacketSize(finalBuf.getData().size()); - packet.setReturnPacket(PACKET_SEND); - - (void)server; -} - -void sendSetHealth(Packet& packet, Server& server) { - std::cout << "=== Sending Set Health ===\n"; - - Buffer buf; - - // Health (float) - 20.0 = full health - // Convert 20.0f to IEEE 754 bits: 0x41A00000 - buf.writeInt(0x41A00000); - - // Food (VarInt) - 20 = full food bar - buf.writeVarInt(20); - - // Food Saturation (float) - 5.0 by default - // Convert 5.0f to IEEE 754 bits: 0x40A00000 - buf.writeInt(0x40A00000); - - int packetId = 0x61; // Set Health packet ID for protocol 770 - int packetIdSize = packet.getVarintSize(packetId); - int totalPayloadSize = packetIdSize + buf.getData().size(); - - Buffer finalBuf; - finalBuf.writeVarInt(totalPayloadSize); - finalBuf.writeVarInt(packetId); - finalBuf.writeBytes(buf.getData()); - - packet.getData() = finalBuf; - packet.setPacketSize(finalBuf.getData().size()); - packet.setReturnPacket(PACKET_SEND); - - (void)server; -} - -void sendSetExperience(Packet& packet, Server& server) { - std::cout << "=== Sending Set Experience ===\n"; - - Buffer buf; - - // Experience bar (float) - 0.0 to 1.0 (progress to next level) - // Convert 0.0f to IEEE 754 bits: 0x00000000 - buf.writeInt(0x00000000); - - // Level (VarInt) - current experience level - buf.writeVarInt(5); - - // Total Experience (VarInt) - total experience points - buf.writeVarInt(0); - - int packetId = 0x60; // Set Experience packet ID for protocol 770 - int packetIdSize = packet.getVarintSize(packetId); - int totalPayloadSize = packetIdSize + buf.getData().size(); - - Buffer finalBuf; - finalBuf.writeVarInt(totalPayloadSize); - finalBuf.writeVarInt(packetId); - finalBuf.writeBytes(buf.getData()); - - packet.getData() = finalBuf; - packet.setPacketSize(finalBuf.getData().size()); - packet.setReturnPacket(PACKET_SEND); - - (void)server; -} - -void sendUpdateTime(Packet& packet, Server& server) { - std::cout << "=== Sending Update Time ===\n"; - - Buffer buf; - - // World Age (Long) - total ticks since world creation - buf.writeInt64(0); - - // Time of day (Long) - 0 = sunrise, 6000 = noon, 12000 = sunset, 18000 = midnight - buf.writeInt64(1000); // Morning time - - // Time of day increasing (Boolean) - should client auto-advance time - buf.writeByte(0x01); // true - - int packetId = 0x6A; // Update Time packet ID for protocol 770 - int packetIdSize = packet.getVarintSize(packetId); - int totalPayloadSize = packetIdSize + buf.getData().size(); - - Buffer finalBuf; - finalBuf.writeVarInt(totalPayloadSize); - finalBuf.writeVarInt(packetId); - finalBuf.writeBytes(buf.getData()); - - packet.getData() = finalBuf; - packet.setPacketSize(finalBuf.getData().size()); - packet.setReturnPacket(PACKET_SEND); - - (void)server; -} - -void sendSetHeldItem(Packet& packet, Server& server) { - std::cout << "=== Sending Set Held Item ===\n"; - - Buffer buf; - - // Slot (VarInt) - hotbar slot selected (0-8) - buf.writeVarInt(0); // First slot selected - - int packetId = 0x62; // Set Held Item packet ID for protocol 770 - int packetIdSize = packet.getVarintSize(packetId); - int totalPayloadSize = packetIdSize + buf.getData().size(); - - Buffer finalBuf; - finalBuf.writeVarInt(totalPayloadSize); - finalBuf.writeVarInt(packetId); - finalBuf.writeBytes(buf.getData()); - - packet.getData() = finalBuf; - packet.setPacketSize(finalBuf.getData().size()); - packet.setReturnPacket(PACKET_SEND); - - (void)server; -} - - -void completeSpawnSequence(Packet& packet, Server& server) { - Player* player = packet.getPlayer(); - ThreadSafeQueue* outgoingPackets = server.getNetworkManager().getOutgoingQueue(); - if (!player || !outgoingPackets) return; - - // std::cout << "=== Completing spawn sequence for player: " << player->getPlayerName() << " - // ===\n"; - std::cout << "=== Completing spawn sequence for player: " << " ===\n"; - - try { - // 9. Player Abilities (0x39) - Packet* abilitiesPacket = new Packet(packet); - sendPlayerAbilities(*abilitiesPacket, server); - outgoingPackets->push(abilitiesPacket); - - // 10. Set Health (0x61) - Packet* healthPacket = new Packet(packet); - sendSetHealth(*healthPacket, server); - outgoingPackets->push(healthPacket); - - // 11. Set Experience (0x60) - Packet* experiencePacket = new Packet(packet); - sendSetExperience(*experiencePacket, server); - outgoingPackets->push(experiencePacket); - - // 12. Update Time (0x6A) - Packet* timePacket = new Packet(packet); - sendUpdateTime(*timePacket, server); - outgoingPackets->push(timePacket); - - // 13. Set Held Item (0x62) - Packet* heldItemPacket = new Packet(packet); - sendSetHeldItem(*heldItemPacket, server); - outgoingPackets->push(heldItemPacket); - - std::cout << "=== Spawn sequence completed! Player should now be fully spawned ===\n"; - - } catch (const std::exception& e) { - std::cerr << "Error completing spawn sequence: " << e.what() << std::endl; - } -} From 94285bb128606f94f4e9ff6da3fd464ccc44d542 Mon Sep 17 00:00:00 2001 From: delmath Date: Mon, 20 Oct 2025 16:55:30 +0200 Subject: [PATCH 3/5] finish --- src/data/updateTags.cpp | 19 +- src/networking/networkPacketRouter.cpp | 355 +++++++----------- .../packet/clientbound/PlayPacket.cpp | 0 .../packet/clientbound/gameEventPacket.cpp | 4 +- .../clientbound/handleCookieRequestPacket.cpp | 59 +-- .../clientbound/handleFinishConfiguration.cpp | 0 .../clientbound/levelChunkWithLightPacket.cpp | 8 +- .../handleClientInformationPacket.cpp | 88 ++++- .../packet/templateClientBoundPacket.cpp | 15 +- 9 files changed, 224 insertions(+), 324 deletions(-) delete mode 100644 src/networking/packet/clientbound/PlayPacket.cpp delete mode 100644 src/networking/packet/clientbound/handleFinishConfiguration.cpp diff --git a/src/data/updateTags.cpp b/src/data/updateTags.cpp index f423aef..b54c68c 100644 --- a/src/data/updateTags.cpp +++ b/src/data/updateTags.cpp @@ -55,25 +55,12 @@ void sendUpdateTags(Packet& packet, Server& server) { "Configuration"); } - Buffer finalBuf; - int packetId = 0x0D; - int packetIdSize = packet.getVarintSize(packetId); - int totalPayloadSize = packetIdSize + tagBuffer.getData().size(); - - finalBuf.writeVarInt(totalPayloadSize); - finalBuf.writeVarInt(packetId); - finalBuf.writeBytes(tagBuffer.getData()); - - Packet* tagsPacket = new Packet(packet); - tagsPacket->getData() = finalBuf; - tagsPacket->setPacketSize(finalBuf.getData().size()); - tagsPacket->setReturnPacket(PACKET_SEND); - - outgoingPackets->push(tagsPacket); + Packet* tagsPacket = new Packet(packet); + tagsPacket->sendPacket(0x0D, tagBuffer, server, true); g_logger->logNetwork(INFO, "Update Tags packet sent: " + std::to_string(totalRegistries) + " registries, " + std::to_string(totalTags) + " tags, " + - std::to_string(totalEntries) + " entries, packet size: " + std::to_string(finalBuf.getData().size()) + " bytes", + std::to_string(totalEntries) + " entries", "Configuration"); packet.setReturnPacket(PACKET_OK); diff --git a/src/networking/networkPacketRouter.cpp b/src/networking/networkPacketRouter.cpp index 943a77b..b60af9b 100644 --- a/src/networking/networkPacketRouter.cpp +++ b/src/networking/networkPacketRouter.cpp @@ -1,9 +1,9 @@ #include "logger.hpp" -#include "networking.hpp" -#include "packet.hpp" -#include "server.hpp" +#include "network/networking.hpp" +#include "network/packet.hpp" +#include "network/server.hpp" +#include "network/packetRouter.hpp" #include "player.hpp" -#include "packetRouter.hpp" #include @@ -12,224 +12,129 @@ // ======================================== void packetRouter(Packet* packet, Server& server) { - // Basic validation checks - if (packet == nullptr) - return; - - if (server.getNetworkManager().getOutgoingQueue() == nullptr) - return; - - // Get player and validate - Player* player = packet->getPlayer(); - if (player == nullptr) { - packet->setReturnPacket(PACKET_DISCONNECT); - return; - } - - // Route packet based on player's current state - switch (player->getPlayerState()) { - case PlayerState::Handshake: - handleHandshakeState(packet, server); - break; - - case PlayerState::Status: - handleStatusState(packet, server); - break; - - case PlayerState::Login: - handleLoginState(packet, server); - break; - - case PlayerState::Configuration: - handleConfigurationState(packet, server); - break; - - case PlayerState::Play: - handlePlayState(packet, server); - break; - - default: - g_logger->logNetwork( - WARN, - "Unknown player state: " + std::to_string(static_cast(player->getPlayerState())) + ", disconnecting", - "PacketRouter" - ); - packet->setReturnPacket(PACKET_DISCONNECT); - break; - } -} - -// ======================================== -// Handshake State Handler -// ======================================== - -void handleHandshakeState(Packet* packet, Server& server) { - handleHandshakePacket(*packet, server); -} - -// ======================================== -// Status State Handler -// ======================================== - -void handleStatusState(Packet* packet, Server& server) { - switch (packet->getId()) { - case 0x00: - // Status request packet - handleStatusPacket(*packet, server); - break; - - case 0x01: - // Ping packet - handlePingPacket(*packet, server); - break; - - default: - // Unknown packet in status state - disconnect - // g_logger->logNetwork(WARN, "Unknown packet ID in Status state: 0x" + std::to_string(packet->getId()), "PacketRouter"); - packet->getPlayer()->setPlayerState(PlayerState::None); - packet->setReturnPacket(PACKET_DISCONNECT); - break; - } -} - -// ======================================== -// Login State Handler -// ======================================== - -void handleLoginState(Packet* packet, Server& server) { - // Check packet size limit - if (packet->getSize() > 32767) { - g_logger->logNetwork(ERROR, "Packet size too large: " + std::to_string(packet->getSize()), "PacketRouter"); - packet->setReturnPacket(PACKET_DISCONNECT); - return; - } - - switch (packet->getId()) { - case 0x00: - // Login start packet - handleLoginStartPacket(*packet, server); - break; - - case 0x02: - // Login plugin response - g_logger->logNetwork(INFO, "Received Login Plugin Response (0x02) - acknowledging", "PacketRouter"); - packet->setReturnPacket(PACKET_OK); - break; - - case 0x03: - // Login acknowledged packet - handleLoginAcknowledgedPacket(*packet, server); - if (packet->getReturnPacket() == PACKET_DISCONNECT) - return; - clientboundKnownPacksPacket(*packet, server); - break; - - case 0x04: - // Login cookie response - g_logger->logNetwork(INFO, "Received Login Cookie Response (0x04) - acknowledging", "PacketRouter"); - packet->setReturnPacket(PACKET_OK); - break; - - default: - // Unknown packet in login state - disconnect - packet->getPlayer()->setPlayerState(PlayerState::None); - packet->setReturnPacket(PACKET_DISCONNECT); - break; - } -} - -// ======================================== -// Configuration State Handler -// ======================================== - -void handleConfigurationState(Packet* packet, Server& server) { - switch (packet->getId()) { - case 0x00: - // Client information packet - g_logger->logNetwork(INFO, "Received Client Information in Configuration state", "Configuration"); - handleClientInformationPacket(*packet, server); - break; - - case 0x01: - // Cookie response in configuration - g_logger->logNetwork(INFO, "Received Cookie Response in Configuration state", "Configuration"); - packet->setReturnPacket(PACKET_OK); - break; - - case 0x02: - // Serverbound plugin message - g_logger->logNetwork( - INFO, - "Received Serverbound Plugin Message (0x02), size: " + std::to_string(packet->getSize()) + " bytes", - "PacketRouter" - ); - packet->setReturnPacket(PACKET_OK); - break; - - case 0x03: - // Acknowledge finish configuration - transition to play state - handleAcknowledgeFinishConfigurationPacket(*packet, server); - sendPlayPacket(*packet, server); // 1. Send Login (play) packet - 0x2B - changeDifficultyPacket(*packet, server); // 2. Send Change Difficulty - 0x42 - playerAbilitiesPacket(*packet, server); // 3. Send Player Abilities - 0x39 - setHeldItemPacket(*packet, server); // 4. Set held item - synchronizePlayerPositionPacket(*packet, server); // 5. Send player position and look - 0x41 - break; - - case 0x04: - // Keep alive in configuration - g_logger->logNetwork(INFO, "Received Keep Alive in Configuration state", "Configuration"); - packet->setReturnPacket(PACKET_OK); - break; - - case 0x05: - // Pong in configuration - g_logger->logNetwork(INFO, "Received Pong in Configuration state", "Configuration"); - packet->setReturnPacket(PACKET_OK); - break; - - case 0x06: - // Resource pack response - g_logger->logNetwork(INFO, "Received Resource Pack Response in Configuration state", "Configuration"); - packet->setReturnPacket(PACKET_OK); - break; - - case 0x07: - // Serverbound known packs - finalize configuration - g_logger->logNetwork(INFO, "Received Serverbound Known Packs in Configuration state", "Configuration"); - serverboundKnownPacksPacket(*packet); - packet->setReturnPacket(PACKET_OK); - sendRegistryData(*packet, server); // Send registry data - sendUpdateTags(*packet, server); // Send update tags - handleFinishConfigurationPacket(*packet, server); - break; - - default: - // Unknown packet in configuration state - disconnect - packet->getPlayer()->setPlayerState(PlayerState::None); - packet->setReturnPacket(PACKET_DISCONNECT); - break; - } -} - -// ======================================== -// Play State Handler -// ======================================== - -void handlePlayState(Packet* packet, Server& server) { - switch (packet->getId()) { - case 0x00: - // Confirm teleportation packet - handleConfirmTeleportationPacket(*packet, server); - gameEventPacket(*packet, server); // Send Game Event packet - 0x42 - setCenterPacket(*packet, server); // Send Set Center Chunk - 0x57 - // TODO: Send Level Chunk With Light - 0x22 - packet->setReturnPacket(PACKET_OK); // temp - break; - - default: - // Other play state packets - acknowledge for now - packet->setReturnPacket(PACKET_OK); - break; - } -} + if (packet == nullptr) return; + if (server.getNetworkManager().getOutgoingQueue() == nullptr) return; + + Player* player = packet->getPlayer(); + if (player == nullptr) { + packet->setReturnPacket(PACKET_DISCONNECT); + return; + } + + g_logger->logNetwork(INFO, + "Routing packet ID: 0x" + std::to_string(packet->getId()) + " (size: " + std::to_string(packet->getSize()) + + ") for state: " + std::to_string(static_cast(player->getPlayerState())), + "PacketRouter"); + + switch (player->getPlayerState()) { + case PlayerState::Handshake: + handleHandshakePacket(*packet, server); + break; + case PlayerState::Status: + if (packet->getId() == 0x00) { + handleStatusPacket(*packet, server); + } else if (packet->getId() == 0x01) { + handlePingPacket(*packet, server); + } else { + packet->getPlayer()->setPlayerState(PlayerState::None); + packet->setReturnPacket(PACKET_DISCONNECT); + } + break; + case PlayerState::Login: + if (packet->getSize() > 32767) { + g_logger->logNetwork(ERROR, "Packet size too large: " + std::to_string(packet->getSize()), "PacketRouter"); + packet->setReturnPacket(PACKET_DISCONNECT); + return; + } + + if (packet->getId() == 0x00) { + handleLoginStartPacket(*packet, server); + } else if (packet->getId() == 0x02) { + g_logger->logNetwork(INFO, "Received Login Plugin Response (0x02) - acknowledging", "PacketRouter"); + packet->setReturnPacket(PACKET_OK); + } else if (packet->getId() == 0x03) { + handleLoginAcknowledgedPacket(*packet, server); + clientboundKnownPacksPacket(*packet, server); + } else if (packet->getId() == 0x04) { + g_logger->logNetwork(INFO, "Received Login Cookie Response (0x04) - acknowledging", "PacketRouter"); + packet->setReturnPacket(PACKET_OK); + } else { + packet->getPlayer()->setPlayerState(PlayerState::None); + packet->setReturnPacket(PACKET_DISCONNECT); + } + break; + case PlayerState::Configuration: + if (packet->getId() == 0x00) { + // Client Information + handleClientInformationPacket(*packet, server); + } else if (packet->getId() == 0x01) { + // Cookie Response + packet->setReturnPacket(PACKET_OK); + } else if (packet->getId() == 0x02) { + // Serverbound Plugin Message + packet->setReturnPacket(PACKET_OK); + } else if (packet->getId() == 0x03) { + // Acknowledge Finish Configuration -> Enter Play State + g_logger->logNetwork(INFO, "Transitioning to Play state", "Configuration"); + handleAcknowledgeFinishConfigurationPacket(*packet, server); + + // Send play initialization packets + sendPlayPacket(*packet, server); + changeDifficultyPacket(*packet, server); + playerAbilitiesPacket(*packet, server); + setHeldItemPacket(*packet, server); + synchronizePlayerPositionPacket(*packet, server); // Last packet + } else if (packet->getId() == 0x04) { + // Keep Alive + packet->setReturnPacket(PACKET_OK); + } else if (packet->getId() == 0x05) { + // Pong + packet->setReturnPacket(PACKET_OK); + } else if (packet->getId() == 0x06) { + // Resource Pack Response + packet->setReturnPacket(PACKET_OK); + } else if (packet->getId() == 0x07) { + // Serverbound Known Packs -> Send Configuration Data + serverboundKnownPacksPacket(*packet); + + // Send configuration sequence + g_logger->logNetwork(INFO, "Sending Registry Data", "Configuration"); + sendRegistryData(*packet, server); + + g_logger->logNetwork(INFO, "Sending Update Tags", "Configuration"); + sendUpdateTags(*packet, server); + + g_logger->logNetwork(INFO, "Sending Finish Configuration", "Configuration"); + handleFinishConfigurationPacket(*packet, server); + } else if (packet->getId() == 0x08) { + // Custom Click Action + packet->setReturnPacket(PACKET_OK); + } else { + // Unknown packet - disconnect + Buffer payload; + payload.writeString("{\"text\":\"Unknown packet in Configuration state\"}"); + packet->sendPacket(0x02, payload, server, true); + packet->getPlayer()->setPlayerState(PlayerState::None); + packet->setReturnPacket(PACKET_DISCONNECT); + } + break; + case PlayerState::Play: + if (packet->getId() == 0x00) { + // Confirm Teleportation + handleConfirmTeleportationPacket(*packet, server); + gameEventPacket(*packet, server); + } else if (packet->getId() == 0x2B) { + // Player Loaded + g_logger->logNetwork(DEBUG, "Player fully loaded in game", "Play"); + packet->setReturnPacket(PACKET_OK); + } else { + // Other play packets + packet->setReturnPacket(PACKET_OK); + } + break; + default: + g_logger->logNetwork(WARN, "Unknown player state: " + std::to_string(static_cast(player->getPlayerState())), "PacketRouter"); + packet->setReturnPacket(PACKET_DISCONNECT); + break; + } +} \ No newline at end of file diff --git a/src/networking/packet/clientbound/PlayPacket.cpp b/src/networking/packet/clientbound/PlayPacket.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/networking/packet/clientbound/gameEventPacket.cpp b/src/networking/packet/clientbound/gameEventPacket.cpp index b82d716..dc6e9d1 100644 --- a/src/networking/packet/clientbound/gameEventPacket.cpp +++ b/src/networking/packet/clientbound/gameEventPacket.cpp @@ -2,7 +2,7 @@ #include "network/packet.hpp" #include "player.hpp" -#include + void gameEventPacket(Packet& packet, Server& server) { Player* player = packet.getPlayer(); @@ -13,5 +13,5 @@ void gameEventPacket(Packet& packet, Server& server) { buf.writeByte(13); buf.writeFloat(0); - packet.sendPacket(0x22, buf, server, false); + packet.sendPacket(0x22, buf, server, true); } diff --git a/src/networking/packet/clientbound/handleCookieRequestPacket.cpp b/src/networking/packet/clientbound/handleCookieRequestPacket.cpp index 829c429..775bc13 100644 --- a/src/networking/packet/clientbound/handleCookieRequestPacket.cpp +++ b/src/networking/packet/clientbound/handleCookieRequestPacket.cpp @@ -1,93 +1,44 @@ -#include "logger.hpp" #include "network/buffer.hpp" #include "network/networking.hpp" #include "network/packet.hpp" #include "network/server.hpp" #include "player.hpp" -#include - void handleCookieRequestPacket(Packet& packet, Server& server) { - // g_logger->logNetwork(INFO, "Received Cookie Request in Configuration state", - // "Configuration"); - Player* player = packet.getPlayer(); if (!player) { - // g_logger->logNetwork(ERROR, "Error: No player associated with Cookie Request packet", - // "Configuration"); packet.setReturnPacket(PACKET_DISCONNECT); return; } - // Debug: Log raw packet data - // g_logger->logNetwork(INFO, "Cookie Request packet size: " + std::to_string(packet.getSize()) - // + " bytes", "Configuration"); - // Read the cookie identifier from the request std::string cookieIdentifier; try { // Create a fresh buffer from the packet data to read from beginning Buffer cookieBuffer(packet.getData().getData()); cookieIdentifier = cookieBuffer.readString(32767); // Max string length - // g_logger->logNetwork(INFO, "Cookie Request for identifier: '" + cookieIdentifier + "'", - // "Configuration"); } catch (const std::exception& e) { - // g_logger->logNetwork(ERROR, "Failed to read cookie identifier: " + std::string(e.what()), - // "Configuration"); Send empty response instead of disconnecting + // Send empty response instead of disconnecting cookieIdentifier = "unknown"; } // Create Cookie Response packet (0x01) Buffer payload; - payload.writeVarInt(0x01); // Cookie Response packet ID payload.writeString(cookieIdentifier); // Echo back the identifier - - // For now, send empty cookie data (no stored cookie) payload.writeByte(0x00); // Has payload: false (no cookie data) - Buffer final; - final.writeVarInt(payload.getData().size()); - final.writeBytes(payload.getData()); - - packet.getData() = final; - packet.setReturnPacket(PACKET_SEND); - packet.setPacketSize(final.getData().size()); - - // g_logger->logNetwork(INFO, "Sent Cookie Response for identifier: '" + cookieIdentifier + "' - // (no data), response size: " + std::to_string(final.getData().size()), "Configuration"); - - // g_logger->logNetwork(INFO, "Cookie Response sent - now sending Finish Configuration to - // advance sequence", "Configuration"); - - (void)server; // Suppress unused parameter warning + packet.sendPacket(0x01, payload, server, false); } void sendFinishConfigurationAfterCookie(Packet& packet, Server& server) { - // g_logger->logNetwork(INFO, "Sending Finish Configuration to advance sequence", - // "Configuration"); - Player* player = packet.getPlayer(); if (!player) { - // g_logger->logNetwork(ERROR, "Error: No player for Finish Configuration", - // "Configuration"); return; } // Create Finish Configuration packet (0x03) Buffer payload; - payload.writeVarInt(0x03); // Finish Configuration packet ID - - Buffer final; - final.writeVarInt(payload.getData().size()); - final.writeBytes(payload.getData()); + // No additional data needed for Finish Configuration - // Create a new packet for Finish Configuration - Packet* finishPacket = new Packet(packet); - finishPacket->getData() = final; - finishPacket->setReturnPacket(PACKET_SEND); - finishPacket->setPacketSize(final.getData().size()); - - // g_logger->logNetwork(INFO, "Finish Configuration packet prepared", "Configuration"); - - (void)server; -} + packet.sendPacket(0x03, payload, server, false); +} \ No newline at end of file diff --git a/src/networking/packet/clientbound/handleFinishConfiguration.cpp b/src/networking/packet/clientbound/handleFinishConfiguration.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/networking/packet/clientbound/levelChunkWithLightPacket.cpp b/src/networking/packet/clientbound/levelChunkWithLightPacket.cpp index 1309d0c..ea6c547 100644 --- a/src/networking/packet/clientbound/levelChunkWithLightPacket.cpp +++ b/src/networking/packet/clientbound/levelChunkWithLightPacket.cpp @@ -93,11 +93,5 @@ void levelChunkWithLightPacket(Packet& packet, Server& server) { return; } - // Set packet data (no manual length encoding) - packet.setPacketId(0x27); // Level Chunk with Light packet ID - packet.getData() = buf; - packet.setPacketSize(buf.getData().size()); - packet.setReturnPacket(PACKET_SEND); - - (void)server; + packet.sendPacket(0x27, buf, server, false); } diff --git a/src/networking/packet/serverbound/handleClientInformationPacket.cpp b/src/networking/packet/serverbound/handleClientInformationPacket.cpp index adcacdd..0ad8839 100644 --- a/src/networking/packet/serverbound/handleClientInformationPacket.cpp +++ b/src/networking/packet/serverbound/handleClientInformationPacket.cpp @@ -1,18 +1,86 @@ #include "network/packet.hpp" #include "network/server.hpp" #include "player.hpp" +#include "logger.hpp" void handleClientInformationPacket(Packet& packet, Server& server) { - PlayerConfig* config = packet.getPlayer()->getPlayerConfig(); + Player* player = packet.getPlayer(); + if (!player) { + g_logger->logNetwork(ERROR, "No player associated with Client Information packet", "Configuration"); + packet.setReturnPacket(PACKET_DISCONNECT); + return; + } - config->setLocale(packet.getData().readString(16)); - config->setViewDistance(packet.getData().readByte()); - config->setChatMode(packet.getData().readVarInt()); - config->setChatColors(packet.getData().readByte() != 0); - config->setDisplayedSkinParts(packet.getData().readByte()); - config->setMainHand(packet.getData().readVarInt()); - config->setTextFiltering(packet.getData().readByte() != 0); - config->setServerListings(packet.getData().readByte() != 0); + PlayerConfig* config = player->getPlayerConfig(); + if (!config) { + g_logger->logNetwork(ERROR, "Player config is null for Client Information packet", "Configuration"); + packet.setReturnPacket(PACKET_DISCONNECT); + return; + } + + g_logger->logNetwork(INFO, "Processing Client Information packet for player: " + player->getPlayerName(), "Configuration"); + g_logger->logNetwork(DEBUG, "Packet size: " + std::to_string(packet.getSize()) + " bytes", "Configuration"); + g_logger->logNetwork(DEBUG, "Packet ID: 0x" + std::to_string(packet.getId()), "Configuration"); + + try { + // Log buffer state before reading + g_logger->logNetwork(DEBUG, "Buffer size: " + std::to_string(packet.getData().getData().size()), "Configuration"); + + // Read locale (string) + g_logger->logNetwork(DEBUG, "Reading locale...", "Configuration"); + std::string locale = packet.getData().readString(16); + config->setLocale(locale); + g_logger->logNetwork(INFO, "Locale: " + locale, "Configuration"); + + // Read view distance (byte) + g_logger->logNetwork(DEBUG, "Reading view distance...", "Configuration"); + uint8_t viewDistance = packet.getData().readByte(); + config->setViewDistance(viewDistance); + g_logger->logNetwork(INFO, "View distance: " + std::to_string(viewDistance), "Configuration"); + + // Read chat mode (varint) + g_logger->logNetwork(DEBUG, "Reading chat mode...", "Configuration"); + int32_t chatMode = packet.getData().readVarInt(); + config->setChatMode(chatMode); + g_logger->logNetwork(INFO, "Chat mode: " + std::to_string(chatMode), "Configuration"); + + // Read chat colors (boolean as byte) + g_logger->logNetwork(DEBUG, "Reading chat colors...", "Configuration"); + bool chatColors = packet.getData().readByte() != 0; + config->setChatColors(chatColors); + g_logger->logNetwork(INFO, "Chat colors: " + std::string(chatColors ? "true" : "false"), "Configuration"); + + // Read displayed skin parts (byte) + g_logger->logNetwork(DEBUG, "Reading skin parts...", "Configuration"); + uint8_t skinParts = packet.getData().readByte(); + config->setDisplayedSkinParts(skinParts); + g_logger->logNetwork(INFO, "Skin parts: " + std::to_string(skinParts), "Configuration"); + + // Read main hand (varint) + g_logger->logNetwork(DEBUG, "Reading main hand...", "Configuration"); + int32_t mainHand = packet.getData().readVarInt(); + config->setMainHand(mainHand); + g_logger->logNetwork(INFO, "Main hand: " + std::to_string(mainHand), "Configuration"); + + // Read enable text filtering (boolean as byte) + g_logger->logNetwork(DEBUG, "Reading text filtering...", "Configuration"); + bool textFiltering = packet.getData().readByte() != 0; + config->setTextFiltering(textFiltering); + g_logger->logNetwork(INFO, "Text filtering: " + std::string(textFiltering ? "true" : "false"), "Configuration"); + + // Read allow server listings (boolean as byte) + g_logger->logNetwork(DEBUG, "Reading server listings...", "Configuration"); + bool serverListings = packet.getData().readByte() != 0; + config->setServerListings(serverListings); + g_logger->logNetwork(INFO, "Server listings: " + std::string(serverListings ? "true" : "false"), "Configuration"); + + g_logger->logNetwork(INFO, "Client Information processed successfully", "Configuration"); + packet.setReturnPacket(PACKET_OK); + + } catch (const std::exception& e) { + g_logger->logNetwork(ERROR, "Error reading Client Information packet: " + std::string(e.what()), "Configuration"); + packet.setReturnPacket(PACKET_DISCONNECT); + } (void)server; -} +} \ No newline at end of file diff --git a/src/networking/packet/templateClientBoundPacket.cpp b/src/networking/packet/templateClientBoundPacket.cpp index 1c1df6c..3a31ffb 100644 --- a/src/networking/packet/templateClientBoundPacket.cpp +++ b/src/networking/packet/templateClientBoundPacket.cpp @@ -1,16 +1,11 @@ #include "buffer.hpp" #include "packet.hpp" +#include "server.hpp" -void templateClientBoundPacket(Packet& packet) { +void templateClientBoundPacket(Packet& packet, Server& server) { Buffer buff; - // Add packet id, and data. + // Add packet data here - Buffer final; - final.writeVarInt(buff.getData().size()); - final.writeBytes(buff.getData()); - - packet.getData() = final; - packet.setPacketSize(final.getData().size()); - packet.setReturnPacket(PACKET_SEND); -} + packet.sendPacket(0x00, buff, server, false); // Replace 0x00 with actual packet ID +} \ No newline at end of file From 07c20d9348b821ffd445b35ff341b9a43079c6c9 Mon Sep 17 00:00:00 2001 From: delmath Date: Mon, 20 Oct 2025 17:18:07 +0200 Subject: [PATCH 4/5] fix: include for cmake --- src/data/RegistryDataUtils.cpp | 8 ++++---- src/lib/packet.cpp | 2 +- src/networking/networkWorker.cpp | 2 +- .../packet/clientbound/changeDifficultyPacket.cpp | 4 ++-- .../packet/clientbound/clientboundKnownPacksPacket.cpp | 4 ++-- .../packet/clientbound/playerAbilitiesPacket.cpp | 4 ++-- src/networking/packet/clientbound/setHeldItemPacket.cpp | 6 +++--- src/networking/packet/templateClientBoundPacket.cpp | 6 +++--- 8 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/data/RegistryDataUtils.cpp b/src/data/RegistryDataUtils.cpp index ad7ea2e..fe8a330 100644 --- a/src/data/RegistryDataUtils.cpp +++ b/src/data/RegistryDataUtils.cpp @@ -1,9 +1,9 @@ -#include "RegistryDataUtils.hpp" +#include "data/RegistryDataUtils.hpp" -#include "RegistryData.hpp" -#include "RegistryIds.hpp" +#include "data/RegistryData.hpp" +#include "data/RegistryIds.hpp" #include "logger.hpp" -#include "minecraftRegistries.hpp" +#include "data/minecraftRegistries.hpp" #include "network/buffer.hpp" #include "network/networking.hpp" diff --git a/src/lib/packet.cpp b/src/lib/packet.cpp index 1c7d7e6..626cf67 100644 --- a/src/lib/packet.cpp +++ b/src/lib/packet.cpp @@ -3,7 +3,7 @@ #include "network/packet.hpp" #include "network/server.hpp" #include "player.hpp" -#include "networking.hpp" +#include "network/networking.hpp" #include #include diff --git a/src/networking/networkWorker.cpp b/src/networking/networkWorker.cpp index 82cc6bd..f906fcf 100644 --- a/src/networking/networkWorker.cpp +++ b/src/networking/networkWorker.cpp @@ -2,7 +2,7 @@ #include "network/networking.hpp" #include "network/packet.hpp" #include "network/server.hpp" -#include "packetRouter.hpp" +#include "network/packetRouter.hpp" #include "player.hpp" #include diff --git a/src/networking/packet/clientbound/changeDifficultyPacket.cpp b/src/networking/packet/clientbound/changeDifficultyPacket.cpp index 10476af..e26cb6c 100644 --- a/src/networking/packet/clientbound/changeDifficultyPacket.cpp +++ b/src/networking/packet/clientbound/changeDifficultyPacket.cpp @@ -1,5 +1,5 @@ -#include "buffer.hpp" -#include "packet.hpp" +#include "network/buffer.hpp" +#include "network/packet.hpp" void changeDifficultyPacket(Packet& packet, Server& server) { Buffer buff; diff --git a/src/networking/packet/clientbound/clientboundKnownPacksPacket.cpp b/src/networking/packet/clientbound/clientboundKnownPacksPacket.cpp index 7033b7d..1a8f81e 100644 --- a/src/networking/packet/clientbound/clientboundKnownPacksPacket.cpp +++ b/src/networking/packet/clientbound/clientboundKnownPacksPacket.cpp @@ -1,6 +1,6 @@ -#include "buffer.hpp" +#include "network/buffer.hpp" #include "network/packet.hpp" -#include "server.hpp" +#include "network/server.hpp" // If implementing data packs we should actually send datapack info with the loaded datapacks void clientboundKnownPacksPacket(Packet& packet, Server& server) { diff --git a/src/networking/packet/clientbound/playerAbilitiesPacket.cpp b/src/networking/packet/clientbound/playerAbilitiesPacket.cpp index 6997543..f8a5a2b 100644 --- a/src/networking/packet/clientbound/playerAbilitiesPacket.cpp +++ b/src/networking/packet/clientbound/playerAbilitiesPacket.cpp @@ -1,5 +1,5 @@ -#include "buffer.hpp" -#include "packet.hpp" +#include "network/buffer.hpp" +#include "network/packet.hpp" void playerAbilitiesPacket(Packet& packet, Server& server) { Buffer buff; diff --git a/src/networking/packet/clientbound/setHeldItemPacket.cpp b/src/networking/packet/clientbound/setHeldItemPacket.cpp index 69df5af..f01636a 100644 --- a/src/networking/packet/clientbound/setHeldItemPacket.cpp +++ b/src/networking/packet/clientbound/setHeldItemPacket.cpp @@ -1,6 +1,6 @@ -#include "buffer.hpp" -#include "packet.hpp" -#include "server.hpp" +#include "network/buffer.hpp" +#include "network/packet.hpp" +#include "network/server.hpp" void setHeldItemPacket(Packet& packet, Server& server) { Buffer buff; diff --git a/src/networking/packet/templateClientBoundPacket.cpp b/src/networking/packet/templateClientBoundPacket.cpp index 3a31ffb..fdf7622 100644 --- a/src/networking/packet/templateClientBoundPacket.cpp +++ b/src/networking/packet/templateClientBoundPacket.cpp @@ -1,6 +1,6 @@ -#include "buffer.hpp" -#include "packet.hpp" -#include "server.hpp" +#include "network/buffer.hpp" +#include "network/packet.hpp" +#include "network/server.hpp" void templateClientBoundPacket(Packet& packet, Server& server) { Buffer buff; From 77e396a9d0e396cfe3091b8ff2a4aedb0f15af73 Mon Sep 17 00:00:00 2001 From: SkyVence Date: Mon, 20 Oct 2025 23:14:40 +0200 Subject: [PATCH 5/5] cmakelist --- CMakeLists.txt | 29 +++++++++++++++++++++++++++++ include/data/RegistryData.hpp | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..26814b7 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,29 @@ +cmake_minimum_required(VERSION "3.16") + +set(PRODUCTION_BUILD OFF CACHE BOOL "Make this a production build" FORCE) + +set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") +set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Release>") + +if(PRODUCTION_BUILD) + set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE) +else() + set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE) +endif() + +project("mc-server") +set(CMAKE_CXX_STANDARD 20) + +# Add external library here -> +add_subdirectory("thirdParty/asio") + +file (GLOB_RECURSE MY_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp") + +add_executable(${CMAKE_PROJECT_NAME} "${MY_SOURCES}") + +if(MSVC) + target_compile_definitions("${CMAKE_PROJECT_NAME}" PUBLIC _CRT_SECURE_NO_WARNINGS) +endif() + +target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include/") +target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE asio) diff --git a/include/data/RegistryData.hpp b/include/data/RegistryData.hpp index e02cfa8..792334a 100644 --- a/include/data/RegistryData.hpp +++ b/include/data/RegistryData.hpp @@ -1,7 +1,7 @@ #ifndef MC_CPP_SERVER_DATA_REGISTRY_HPP #define MC_CPP_SERVER_DATA_REGISTRY_HPP -#include "nbt.hpp" +#include "lib/nbt.hpp" #include #include