refactor: extract getUnitByGuid() to replace 10 entity lookup + dynamic_cast patterns

Add GameHandler::getUnitByGuid() that combines entityManager.getEntity()
with dynamic_cast<Unit*>. Replaces 10 two-line lookup+cast blocks with
single-line calls.
This commit is contained in:
Kelsi 2026-03-25 13:12:51 -07:00
parent c8617d20c8
commit fe043b5da8
2 changed files with 16 additions and 20 deletions

View file

@ -2517,6 +2517,7 @@ private:
void triggerQuestAcceptResync(uint32_t questId, uint64_t npcGuid, const char* reason); void triggerQuestAcceptResync(uint32_t questId, uint64_t npcGuid, const char* reason);
bool hasQuestInLog(uint32_t questId) const; bool hasQuestInLog(uint32_t questId) const;
std::string guidToUnitId(uint64_t guid) const; std::string guidToUnitId(uint64_t guid) const;
Unit* getUnitByGuid(uint64_t guid);
std::string getQuestTitle(uint32_t questId) const; std::string getQuestTitle(uint32_t questId) const;
const QuestLogEntry* findQuestLogEntry(uint32_t questId) const; const QuestLogEntry* findQuestLogEntry(uint32_t questId) const;
int findQuestLogSlotIndexFromServer(uint32_t questId) const; int findQuestLogSlotIndexFromServer(uint32_t questId) const;

View file

@ -1797,8 +1797,7 @@ void GameHandler::registerOpcodeHandlers() {
uint64_t guid = huTbc ? packet.readUInt64() : UpdateObjectParser::readPackedGuid(packet); uint64_t guid = huTbc ? packet.readUInt64() : UpdateObjectParser::readPackedGuid(packet);
if (packet.getRemainingSize() < 4) return; if (packet.getRemainingSize() < 4) return;
uint32_t hp = packet.readUInt32(); uint32_t hp = packet.readUInt32();
auto entity = entityManager.getEntity(guid); if (auto* unit = getUnitByGuid(guid)) unit->setHealth(hp);
if (auto* unit = dynamic_cast<Unit*>(entity.get())) unit->setHealth(hp);
if (guid != 0) { if (guid != 0) {
auto unitId = guidToUnitId(guid); auto unitId = guidToUnitId(guid);
if (!unitId.empty()) fireAddonEvent("UNIT_HEALTH", {unitId}); if (!unitId.empty()) fireAddonEvent("UNIT_HEALTH", {unitId});
@ -1811,8 +1810,7 @@ void GameHandler::registerOpcodeHandlers() {
if (packet.getRemainingSize() < 5) return; if (packet.getRemainingSize() < 5) return;
uint8_t powerType = packet.readUInt8(); uint8_t powerType = packet.readUInt8();
uint32_t value = packet.readUInt32(); uint32_t value = packet.readUInt32();
auto entity = entityManager.getEntity(guid); if (auto* unit = getUnitByGuid(guid)) unit->setPowerByType(powerType, value);
if (auto* unit = dynamic_cast<Unit*>(entity.get())) unit->setPowerByType(powerType, value);
if (guid != 0) { if (guid != 0) {
auto unitId = guidToUnitId(guid); auto unitId = guidToUnitId(guid);
if (!unitId.empty()) { if (!unitId.empty()) {
@ -2686,8 +2684,7 @@ void GameHandler::registerOpcodeHandlers() {
readyCheckResults_.clear(); readyCheckResults_.clear();
if (packet.getRemainingSize() >= 8) { if (packet.getRemainingSize() >= 8) {
uint64_t initiatorGuid = packet.readUInt64(); uint64_t initiatorGuid = packet.readUInt64();
auto entity = entityManager.getEntity(initiatorGuid); if (auto* unit = getUnitByGuid(initiatorGuid))
if (auto* unit = dynamic_cast<Unit*>(entity.get()))
readyCheckInitiator_ = unit->getName(); readyCheckInitiator_ = unit->getName();
} }
if (readyCheckInitiator_.empty() && partyData.leaderGuid != 0) { if (readyCheckInitiator_.empty() && partyData.leaderGuid != 0) {
@ -13573,8 +13570,7 @@ void GameHandler::handleDuelRequested(network::Packet& packet) {
// Resolve challenger name from entity list // Resolve challenger name from entity list
duelChallengerName_.clear(); duelChallengerName_.clear();
auto entity = entityManager.getEntity(duelChallengerGuid_); if (auto* unit = getUnitByGuid(duelChallengerGuid_)) {
if (auto* unit = dynamic_cast<Unit*>(entity.get())) {
duelChallengerName_ = unit->getName(); duelChallengerName_ = unit->getName();
} }
if (duelChallengerName_.empty()) { if (duelChallengerName_.empty()) {
@ -20574,6 +20570,11 @@ bool GameHandler::hasQuestInLog(uint32_t questId) const {
return false; return false;
} }
Unit* GameHandler::getUnitByGuid(uint64_t guid) {
auto entity = entityManager.getEntity(guid);
return entity ? dynamic_cast<Unit*>(entity.get()) : nullptr;
}
std::string GameHandler::guidToUnitId(uint64_t guid) const { std::string GameHandler::guidToUnitId(uint64_t guid) const {
if (guid == playerGuid) return "player"; if (guid == playerGuid) return "player";
if (guid == targetGuid) return "target"; if (guid == targetGuid) return "target";
@ -25129,8 +25130,7 @@ void GameHandler::handleQuestConfirmAccept(network::Packet& packet) {
} }
sharedQuestSharerName_.clear(); sharedQuestSharerName_.clear();
auto entity = entityManager.getEntity(sharedQuestSharerGuid_); if (auto* unit = getUnitByGuid(sharedQuestSharerGuid_)) {
if (auto* unit = dynamic_cast<Unit*>(entity.get())) {
sharedQuestSharerName_ = unit->getName(); sharedQuestSharerName_ = unit->getName();
} }
if (sharedQuestSharerName_.empty()) { if (sharedQuestSharerName_.empty()) {
@ -25181,8 +25181,7 @@ void GameHandler::handleSummonRequest(network::Packet& packet) {
pendingSummonRequest_= true; pendingSummonRequest_= true;
summonerName_.clear(); summonerName_.clear();
auto entity = entityManager.getEntity(summonerGuid_); if (auto* unit = getUnitByGuid(summonerGuid_)) {
if (auto* unit = dynamic_cast<Unit*>(entity.get())) {
summonerName_ = unit->getName(); summonerName_ = unit->getName();
} }
if (summonerName_.empty()) { if (summonerName_.empty()) {
@ -25247,8 +25246,7 @@ void GameHandler::handleTradeStatus(network::Packet& packet) {
} }
// Resolve name from entity list // Resolve name from entity list
tradePeerName_.clear(); tradePeerName_.clear();
auto entity = entityManager.getEntity(tradePeerGuid_); if (auto* unit = getUnitByGuid(tradePeerGuid_)) {
if (auto* unit = dynamic_cast<Unit*>(entity.get())) {
tradePeerName_ = unit->getName(); tradePeerName_ = unit->getName();
} }
if (tradePeerName_.empty()) { if (tradePeerName_.empty()) {
@ -25487,8 +25485,7 @@ void GameHandler::handleLootRoll(network::Packet& packet) {
const char* rollName = (rollType < 4) ? rollNames[rollType] : "Pass"; const char* rollName = (rollType < 4) ? rollNames[rollType] : "Pass";
std::string rollerName; std::string rollerName;
auto entity = entityManager.getEntity(rollerGuid); if (auto* unit = getUnitByGuid(rollerGuid)) {
if (auto* unit = dynamic_cast<Unit*>(entity.get())) {
rollerName = unit->getName(); rollerName = unit->getName();
} }
if (rollerName.empty()) rollerName = "Someone"; if (rollerName.empty()) rollerName = "Someone";
@ -25553,8 +25550,7 @@ void GameHandler::handleLootRollWon(network::Packet& packet) {
const char* rollName = (rollType < 3) ? rollNames[rollType] : "Roll"; const char* rollName = (rollType < 3) ? rollNames[rollType] : "Roll";
std::string winnerName; std::string winnerName;
auto entity = entityManager.getEntity(winnerGuid); if (auto* unit = getUnitByGuid(winnerGuid)) {
if (auto* unit = dynamic_cast<Unit*>(entity.get())) {
winnerName = unit->getName(); winnerName = unit->getName();
} }
if (winnerName.empty()) { if (winnerName.empty()) {
@ -25723,8 +25719,7 @@ void GameHandler::handleAchievementEarned(network::Packet& packet) {
} else { } else {
// Another player in the zone earned an achievement // Another player in the zone earned an achievement
std::string senderName; std::string senderName;
auto entity = entityManager.getEntity(guid); if (auto* unit = getUnitByGuid(guid)) {
if (auto* unit = dynamic_cast<Unit*>(entity.get())) {
senderName = unit->getName(); senderName = unit->getName();
} }
if (senderName.empty()) { if (senderName.empty()) {