From 43caf7b5e69465cffc893ffb582471e7bc734323 Mon Sep 17 00:00:00 2001 From: Kelsi Date: Wed, 25 Mar 2026 14:06:42 -0700 Subject: [PATCH] refactor: add Packet::writePackedGuid, remove redundant static methods Add writePackedGuid() to Packet class for read/write symmetry. Remove now-redundant UpdateObjectParser::readPackedGuid and MovementPacket::writePackedGuid static methods. Replace 6 internal readPackedGuid calls, 9 writePackedGuid calls, and 1 inline 14-line transport GUID write with Packet method calls. --- include/game/packet_parsers.hpp | 4 +- include/game/world_packets.hpp | 9 ---- include/network/packet.hpp | 1 + src/game/game_handler.cpp | 16 +++---- src/game/packet_parsers_classic.cpp | 2 +- src/game/world_packets.cpp | 66 ++++------------------------- src/network/packet.cpp | 16 +++++++ 7 files changed, 36 insertions(+), 78 deletions(-) diff --git a/include/game/packet_parsers.hpp b/include/game/packet_parsers.hpp index b229dc80..261cae66 100644 --- a/include/game/packet_parsers.hpp +++ b/include/game/packet_parsers.hpp @@ -283,12 +283,12 @@ public: /** Read a packed GUID from the packet */ virtual uint64_t readPackedGuid(network::Packet& packet) { - return UpdateObjectParser::readPackedGuid(packet); + return packet.readPackedGuid(); } /** Write a packed GUID to the packet */ virtual void writePackedGuid(network::Packet& packet, uint64_t guid) { - MovementPacket::writePackedGuid(packet, guid); + packet.writePackedGuid(guid); } }; diff --git a/include/game/world_packets.hpp b/include/game/world_packets.hpp index 849dafbb..e315b213 100644 --- a/include/game/world_packets.hpp +++ b/include/game/world_packets.hpp @@ -449,7 +449,6 @@ struct MovementInfo { */ class MovementPacket { public: - static void writePackedGuid(network::Packet& packet, uint64_t guid); static void writeMovementPayload(network::Packet& packet, const MovementInfo& info); /** @@ -526,14 +525,6 @@ public: */ static bool parse(network::Packet& packet, UpdateObjectData& data); - /** - * Read packed GUID from packet - * - * @param packet Packet to read from - * @return GUID value - */ - static uint64_t readPackedGuid(network::Packet& packet); - /** * Parse a single update block * diff --git a/include/network/packet.hpp b/include/network/packet.hpp index 2f489f8a..7463de4f 100644 --- a/include/network/packet.hpp +++ b/include/network/packet.hpp @@ -28,6 +28,7 @@ public: uint64_t readUInt64(); float readFloat(); uint64_t readPackedGuid(); + void writePackedGuid(uint64_t guid); std::string readString(); uint16_t getOpcode() const { return opcode; } diff --git a/src/game/game_handler.cpp b/src/game/game_handler.cpp index 372c1a24..f09b091c 100644 --- a/src/game/game_handler.cpp +++ b/src/game/game_handler.cpp @@ -10064,7 +10064,7 @@ void GameHandler::useEquipmentSet(uint32_t setId) { network::Packet pkt(wire); for (int slot = 0; slot < 19; ++slot) { uint64_t itemGuid = es->itemGuids[slot]; - MovementPacket::writePackedGuid(pkt, itemGuid); + pkt.writePackedGuid(itemGuid); uint8_t srcBag = 0xFF; uint8_t srcSlot = 0; if (itemGuid != 0) { @@ -10125,7 +10125,7 @@ void GameHandler::saveEquipmentSet(const std::string& name, const std::string& i pkt.writeString(iconName); for (int slot = 0; slot < 19; ++slot) { uint64_t guid = getEquipSlotGuid(slot); - MovementPacket::writePackedGuid(pkt, guid); + pkt.writePackedGuid(guid); } // Track pending save so SMSG_EQUIPMENT_SET_SAVED can add the new set locally pendingSaveSetName_ = name; @@ -15562,7 +15562,7 @@ void GameHandler::handleForceSpeedChange(network::Packet& packet, const char* na if (legacyGuidAck) { ack.writeUInt64(playerGuid); } else { - MovementPacket::writePackedGuid(ack, playerGuid); + ack.writePackedGuid(playerGuid); } ack.writeUInt32(counter); @@ -15652,7 +15652,7 @@ void GameHandler::handleForceMoveRootState(network::Packet& packet, bool rooted) if (legacyGuidAck) { ack.writeUInt64(playerGuid); // CMaNGOS expects full GUID for root/unroot ACKs } else { - MovementPacket::writePackedGuid(ack, playerGuid); + ack.writePackedGuid(playerGuid); } ack.writeUInt32(counter); @@ -15712,7 +15712,7 @@ void GameHandler::handleForceMoveFlagChange(network::Packet& packet, const char* if (legacyGuidAck) { ack.writeUInt64(playerGuid); } else { - MovementPacket::writePackedGuid(ack, playerGuid); + ack.writePackedGuid(playerGuid); } ack.writeUInt32(counter); @@ -15763,7 +15763,7 @@ void GameHandler::handleMoveSetCollisionHeight(network::Packet& packet) { if (legacyGuidAck) { ack.writeUInt64(playerGuid); } else { - MovementPacket::writePackedGuid(ack, playerGuid); + ack.writePackedGuid(playerGuid); } ack.writeUInt32(counter); @@ -15815,7 +15815,7 @@ void GameHandler::handleMoveKnockBack(network::Packet& packet) { if (legacyGuidAck) { ack.writeUInt64(playerGuid); } else { - MovementPacket::writePackedGuid(ack, playerGuid); + ack.writePackedGuid(playerGuid); } ack.writeUInt32(counter); @@ -22518,7 +22518,7 @@ void GameHandler::handleTeleportAck(network::Packet& packet) { if (legacyGuidAck) { ack.writeUInt64(playerGuid); // CMaNGOS/VMaNGOS expects full GUID for Classic/TBC } else { - MovementPacket::writePackedGuid(ack, playerGuid); + ack.writePackedGuid(playerGuid); } ack.writeUInt32(counter); ack.writeUInt32(moveTime); diff --git a/src/game/packet_parsers_classic.cpp b/src/game/packet_parsers_classic.cpp index 5b6951f4..5ad1cfd0 100644 --- a/src/game/packet_parsers_classic.cpp +++ b/src/game/packet_parsers_classic.cpp @@ -2282,7 +2282,7 @@ bool TurtlePacketParsers::parseMonsterMove(network::Packet& packet, MonsterMoveD auto looksLikeWotlkMonsterMove = [&](network::Packet& probe) -> bool { const size_t probeStart = probe.getReadPos(); - uint64_t guid = UpdateObjectParser::readPackedGuid(probe); + uint64_t guid = probe.readPackedGuid(); if (guid == 0) { probe.setReadPos(probeStart); return false; diff --git a/src/game/world_packets.cpp b/src/game/world_packets.cpp index ba676059..24f9955e 100644 --- a/src/game/world_packets.cpp +++ b/src/game/world_packets.cpp @@ -728,23 +728,6 @@ bool PongParser::parse(network::Packet& packet, PongData& data) { return true; } -void MovementPacket::writePackedGuid(network::Packet& packet, uint64_t guid) { - uint8_t mask = 0; - uint8_t guidBytes[8]; - int guidByteCount = 0; - for (int i = 0; i < 8; i++) { - uint8_t byte = static_cast((guid >> (i * 8)) & 0xFF); - if (byte != 0) { - mask |= (1 << i); - guidBytes[guidByteCount++] = byte; - } - } - packet.writeUInt8(mask); - for (int i = 0; i < guidByteCount; i++) { - packet.writeUInt8(guidBytes[i]); - } -} - void MovementPacket::writeMovementPayload(network::Packet& packet, const MovementInfo& info) { // Movement packet format (WoW 3.3.5a) payload: // uint32 flags @@ -772,20 +755,7 @@ void MovementPacket::writeMovementPayload(network::Packet& packet, const Movemen // 3.3.5a ordering: transport block appears before pitch/fall/jump. if (info.hasFlag(MovementFlags::ONTRANSPORT)) { // Write packed transport GUID - uint8_t transMask = 0; - uint8_t transGuidBytes[8]; - int transGuidByteCount = 0; - for (int i = 0; i < 8; i++) { - uint8_t byte = static_cast((info.transportGuid >> (i * 8)) & 0xFF); - if (byte != 0) { - transMask |= (1 << i); - transGuidBytes[transGuidByteCount++] = byte; - } - } - packet.writeUInt8(transMask); - for (int i = 0; i < transGuidByteCount; i++) { - packet.writeUInt8(transGuidBytes[i]); - } + packet.writePackedGuid(info.transportGuid); // Write transport local position packet.writeFloat(info.transportX); @@ -827,7 +797,7 @@ network::Packet MovementPacket::build(Opcode opcode, const MovementInfo& info, u // Movement packet format (WoW 3.3.5a): // packed GUID + movement payload - writePackedGuid(packet, playerGuid); + packet.writePackedGuid(playerGuid); writeMovementPayload(packet, info); // Detailed hex dump for debugging @@ -856,26 +826,6 @@ network::Packet MovementPacket::build(Opcode opcode, const MovementInfo& info, u return packet; } -uint64_t UpdateObjectParser::readPackedGuid(network::Packet& packet) { - // Read packed GUID format: - // First byte is a mask indicating which bytes are present - uint8_t mask = packet.readUInt8(); - - if (mask == 0) { - return 0; - } - - uint64_t guid = 0; - for (int i = 0; i < 8; ++i) { - if (mask & (1 << i)) { - uint8_t byte = packet.readUInt8(); - guid |= (static_cast(byte) << (i * 8)); - } - } - - return guid; -} - bool UpdateObjectParser::parseMovementBlock(network::Packet& packet, UpdateBlock& block) { // WoW 3.3.5a UPDATE_OBJECT movement block structure: // 1. UpdateFlags (1 byte, sometimes 2) @@ -950,7 +900,7 @@ bool UpdateObjectParser::parseMovementBlock(network::Packet& packet, UpdateBlock if (moveFlags & 0x00000200) { // MOVEMENTFLAG_ONTRANSPORT if (rem() < 1) return false; block.onTransport = true; - block.transportGuid = readPackedGuid(packet); + block.transportGuid = packet.readPackedGuid(); if (rem() < 21) return false; // 4 floats + uint32 + uint8 block.transportX = packet.readFloat(); block.transportY = packet.readFloat(); @@ -1121,7 +1071,7 @@ bool UpdateObjectParser::parseMovementBlock(network::Packet& packet, UpdateBlock else if (updateFlags & UPDATEFLAG_POSITION) { // Transport position update (UPDATEFLAG_POSITION = 0x0100) if (rem() < 1) return false; - uint64_t transportGuid = readPackedGuid(packet); + uint64_t transportGuid = packet.readPackedGuid(); if (rem() < 32) return false; // 8 floats block.x = packet.readFloat(); block.y = packet.readFloat(); @@ -1164,7 +1114,7 @@ bool UpdateObjectParser::parseMovementBlock(network::Packet& packet, UpdateBlock // Target GUID (for units with target) if (updateFlags & UPDATEFLAG_HAS_TARGET) { if (rem() < 1) return false; - /*uint64_t targetGuid =*/ readPackedGuid(packet); + /*uint64_t targetGuid =*/ packet.readPackedGuid(); } // Transport time @@ -1323,7 +1273,7 @@ bool UpdateObjectParser::parseUpdateBlock(network::Packet& packet, UpdateBlock& case UpdateType::VALUES: { // Partial update - changed fields only if (packet.getReadPos() >= packet.getSize()) return false; - block.guid = readPackedGuid(packet); + block.guid = packet.readPackedGuid(); LOG_DEBUG(" VALUES update for GUID: 0x", std::hex, block.guid, std::dec); return parseUpdateFields(packet, block); @@ -1342,7 +1292,7 @@ bool UpdateObjectParser::parseUpdateBlock(network::Packet& packet, UpdateBlock& case UpdateType::CREATE_OBJECT2: { // Create new object with full data if (packet.getReadPos() >= packet.getSize()) return false; - block.guid = readPackedGuid(packet); + block.guid = packet.readPackedGuid(); LOG_DEBUG(" CREATE_OBJECT for GUID: 0x", std::hex, block.guid, std::dec); // Read object type @@ -1418,7 +1368,7 @@ bool UpdateObjectParser::parse(network::Packet& packet, UpdateObjectData& data) } for (uint32_t i = 0; i < count; ++i) { - uint64_t guid = readPackedGuid(packet); + uint64_t guid = packet.readPackedGuid(); data.outOfRangeGuids.push_back(guid); LOG_DEBUG(" Out of range: 0x", std::hex, guid, std::dec); } diff --git a/src/network/packet.cpp b/src/network/packet.cpp index 0e125c5a..2a20298e 100644 --- a/src/network/packet.cpp +++ b/src/network/packet.cpp @@ -97,6 +97,22 @@ uint64_t Packet::readPackedGuid() { return guid; } +void Packet::writePackedGuid(uint64_t guid) { + uint8_t mask = 0; + uint8_t guidBytes[8]; + int count = 0; + for (int i = 0; i < 8; ++i) { + uint8_t byte = static_cast((guid >> (i * 8)) & 0xFF); + if (byte != 0) { + mask |= (1 << i); + guidBytes[count++] = byte; + } + } + writeUInt8(mask); + for (int i = 0; i < count; ++i) + writeUInt8(guidBytes[i]); +} + std::string Packet::readString() { std::string result; while (readPos < data.size()) {