From 97b44bf8333121e774cbf9fe7b3a7fba9502cee1 Mon Sep 17 00:00:00 2001 From: Kelsi Date: Wed, 25 Mar 2026 18:08:08 -0700 Subject: [PATCH] refactor: consolidate duplicate enchantment name cache in inventory tooltips Extract getEnchantmentNames() to share a single SpellItemEnchantment.dbc cache between both renderItemTooltip overloads, replacing two identical 19-line lazy-load blocks with single-line references. --- include/ui/inventory_screen.hpp | 1 + src/ui/inventory_screen.cpp | 39 ++++++++++----------------------- 2 files changed, 13 insertions(+), 27 deletions(-) diff --git a/include/ui/inventory_screen.hpp b/include/ui/inventory_screen.hpp index d350f210..3e81f1cb 100644 --- a/include/ui/inventory_screen.hpp +++ b/include/ui/inventory_screen.hpp @@ -163,6 +163,7 @@ private: game::EquipSlot equipSlot, int bagIndex = -1, int bagSlotIndex = -1); void renderItemTooltip(const game::ItemDef& item, const game::Inventory* inventory = nullptr, uint64_t itemGuid = 0); + const std::unordered_map& getEnchantmentNames(); // Held item helpers void pickupFromBackpack(game::Inventory& inv, int index); diff --git a/src/ui/inventory_screen.cpp b/src/ui/inventory_screen.cpp index ed8d3bd6..4f087bd1 100644 --- a/src/ui/inventory_screen.cpp +++ b/src/ui/inventory_screen.cpp @@ -2510,12 +2510,11 @@ void InventoryScreen::renderItemSlot(game::Inventory& inventory, const game::Ite } } -void InventoryScreen::renderItemTooltip(const game::ItemDef& item, const game::Inventory* inventory, uint64_t itemGuid) { - // Shared SpellItemEnchantment name lookup — used for socket gems, permanent and temp enchants. - static std::unordered_map s_enchLookupB; - static bool s_enchLookupLoadedB = false; - if (!s_enchLookupLoadedB && assetManager_) { - s_enchLookupLoadedB = true; +const std::unordered_map& InventoryScreen::getEnchantmentNames() { + static std::unordered_map s_cache; + static bool s_loaded = false; + if (!s_loaded && assetManager_) { + s_loaded = true; auto dbc = assetManager_->loadDBC("SpellItemEnchantment.dbc"); if (dbc && dbc->isLoaded()) { const auto* lay = pipeline::getActiveDBCLayout() @@ -2527,10 +2526,15 @@ void InventoryScreen::renderItemTooltip(const game::ItemDef& item, const game::I uint32_t eid = dbc->getUInt32(r, 0); if (eid == 0 || nf >= fc) continue; std::string en = dbc->getString(r, nf); - if (!en.empty()) s_enchLookupB[eid] = std::move(en); + if (!en.empty()) s_cache[eid] = std::move(en); } } } + return s_cache; +} + +void InventoryScreen::renderItemTooltip(const game::ItemDef& item, const game::Inventory* inventory, uint64_t itemGuid) { + const auto& s_enchLookupB = getEnchantmentNames(); ImGui::BeginTooltip(); @@ -3213,26 +3217,7 @@ void InventoryScreen::renderItemTooltip(const game::ItemDef& item, const game::I // Tooltip overload for ItemQueryResponseData (used by loot window, etc.) // --------------------------------------------------------------------------- void InventoryScreen::renderItemTooltip(const game::ItemQueryResponseData& info, const game::Inventory* inventory, uint64_t itemGuid) { - // Shared SpellItemEnchantment name lookup — used for socket gems, socket bonus, and enchants. - static std::unordered_map s_enchLookup; - static bool s_enchLookupLoaded = false; - if (!s_enchLookupLoaded && assetManager_) { - s_enchLookupLoaded = true; - auto dbc = assetManager_->loadDBC("SpellItemEnchantment.dbc"); - if (dbc && dbc->isLoaded()) { - const auto* lay = pipeline::getActiveDBCLayout() - ? pipeline::getActiveDBCLayout()->getLayout("SpellItemEnchantment") : nullptr; - uint32_t nf = lay ? lay->field("Name") : 8u; - if (nf == 0xFFFFFFFF) nf = 8; - uint32_t fc = dbc->getFieldCount(); - for (uint32_t r = 0; r < dbc->getRecordCount(); ++r) { - uint32_t eid = dbc->getUInt32(r, 0); - if (eid == 0 || nf >= fc) continue; - std::string en = dbc->getString(r, nf); - if (!en.empty()) s_enchLookup[eid] = std::move(en); - } - } - } + const auto& s_enchLookup = getEnchantmentNames(); ImGui::BeginTooltip();