refactor: make DBC name caches mutable to eliminate 13 const_cast hacks

Mark spellNameCache_, titleNameCache_, factionNameCache_, areaNameCache_,
mapNameCache_, lfgDungeonNameCache_ and their loaded flags as mutable.
Update 6 lazy-load methods to const. Removes all 13 const_cast<GameHandler*>
calls, allowing const getters to lazily populate caches without UB.
This commit is contained in:
Kelsi 2026-03-25 13:21:02 -07:00
parent fe043b5da8
commit f02fa10126
2 changed files with 39 additions and 39 deletions

View file

@ -3056,13 +3056,13 @@ private:
// Faction standings (factionId → absolute standing value)
std::unordered_map<uint32_t, int32_t> factionStandings_;
// Faction name cache (factionId → name), populated lazily from Faction.dbc
std::unordered_map<uint32_t, std::string> factionNameCache_;
mutable std::unordered_map<uint32_t, std::string> factionNameCache_;
// repListId → factionId mapping (populated with factionNameCache)
std::unordered_map<uint32_t, uint32_t> factionRepListToId_;
mutable std::unordered_map<uint32_t, uint32_t> factionRepListToId_;
// factionId → repListId reverse mapping
std::unordered_map<uint32_t, uint32_t> factionIdToRepList_;
bool factionNameCacheLoaded_ = false;
void loadFactionNameCache();
mutable std::unordered_map<uint32_t, uint32_t> factionIdToRepList_;
mutable bool factionNameCacheLoaded_ = false;
void loadFactionNameCache() const;
std::string getFactionName(uint32_t factionId) const;
// ---- Phase 4: Group ----
@ -3342,14 +3342,14 @@ private:
int32_t effectBasePoints[3] = {0, 0, 0};
float durationSec = 0.0f; // resolved from DurationIndex → SpellDuration.dbc
};
std::unordered_map<uint32_t, SpellNameEntry> spellNameCache_;
bool spellNameCacheLoaded_ = false;
mutable std::unordered_map<uint32_t, SpellNameEntry> spellNameCache_;
mutable bool spellNameCacheLoaded_ = false;
// Title cache: maps titleBit → title string (lazy-loaded from CharTitles.dbc)
// The strings use "%s" as a player-name placeholder (e.g. "Commander %s", "%s the Explorer").
std::unordered_map<uint32_t, std::string> titleNameCache_;
bool titleNameCacheLoaded_ = false;
void loadTitleNameCache();
mutable std::unordered_map<uint32_t, std::string> titleNameCache_;
mutable bool titleNameCacheLoaded_ = false;
void loadTitleNameCache() const;
// Set of title bit-indices known to the player (from SMSG_TITLE_EARNED).
std::unordered_set<uint32_t> knownTitleBits_;
// Currently selected title bit, or -1 for no title. Updated from PLAYER_CHOSEN_TITLE.
@ -3375,24 +3375,24 @@ private:
void handleRespondInspectAchievements(network::Packet& packet);
// Area name cache (lazy-loaded from WorldMapArea.dbc; maps AreaTable ID → display name)
std::unordered_map<uint32_t, std::string> areaNameCache_;
bool areaNameCacheLoaded_ = false;
void loadAreaNameCache();
mutable std::unordered_map<uint32_t, std::string> areaNameCache_;
mutable bool areaNameCacheLoaded_ = false;
void loadAreaNameCache() const;
std::string getAreaName(uint32_t areaId) const;
// Map name cache (lazy-loaded from Map.dbc; maps mapId → localized display name)
std::unordered_map<uint32_t, std::string> mapNameCache_;
bool mapNameCacheLoaded_ = false;
void loadMapNameCache();
mutable std::unordered_map<uint32_t, std::string> mapNameCache_;
mutable bool mapNameCacheLoaded_ = false;
void loadMapNameCache() const;
// LFG dungeon name cache (lazy-loaded from LFGDungeons.dbc; WotLK only)
std::unordered_map<uint32_t, std::string> lfgDungeonNameCache_;
bool lfgDungeonNameCacheLoaded_ = false;
void loadLfgDungeonDbc();
mutable std::unordered_map<uint32_t, std::string> lfgDungeonNameCache_;
mutable bool lfgDungeonNameCacheLoaded_ = false;
void loadLfgDungeonDbc() const;
std::string getLfgDungeonName(uint32_t dungeonId) const;
std::vector<TrainerTab> trainerTabs_;
void handleTrainerList(network::Packet& packet);
void loadSpellNameCache();
void loadSpellNameCache() const;
void categorizeTrainerSpells();
// Callbacks

View file

@ -22035,7 +22035,7 @@ void GameHandler::closeTrainer() {
trainerTabs_.clear();
}
void GameHandler::loadSpellNameCache() {
void GameHandler::loadSpellNameCache() const {
if (spellNameCacheLoaded_) return;
spellNameCacheLoaded_ = true;
@ -22388,13 +22388,13 @@ void GameHandler::loadTalentDbc() {
static const std::string EMPTY_STRING;
const int32_t* GameHandler::getSpellEffectBasePoints(uint32_t spellId) const {
const_cast<GameHandler*>(this)->loadSpellNameCache();
loadSpellNameCache();
auto it = spellNameCache_.find(spellId);
return (it != spellNameCache_.end()) ? it->second.effectBasePoints : nullptr;
}
float GameHandler::getSpellDuration(uint32_t spellId) const {
const_cast<GameHandler*>(this)->loadSpellNameCache();
loadSpellNameCache();
auto it = spellNameCache_.find(spellId);
return (it != spellNameCache_.end()) ? it->second.durationSec : 0.0f;
}
@ -22410,7 +22410,7 @@ const std::string& GameHandler::getSpellRank(uint32_t spellId) const {
}
const std::string& GameHandler::getSpellDescription(uint32_t spellId) const {
const_cast<GameHandler*>(this)->loadSpellNameCache();
loadSpellNameCache();
auto it = spellNameCache_.find(spellId);
return (it != spellNameCache_.end()) ? it->second.description : EMPTY_STRING;
}
@ -22431,14 +22431,14 @@ std::string GameHandler::getEnchantName(uint32_t enchantId) const {
}
uint8_t GameHandler::getSpellDispelType(uint32_t spellId) const {
const_cast<GameHandler*>(this)->loadSpellNameCache();
loadSpellNameCache();
auto it = spellNameCache_.find(spellId);
return (it != spellNameCache_.end()) ? it->second.dispelType : 0;
}
bool GameHandler::isSpellInterruptible(uint32_t spellId) const {
if (spellId == 0) return true;
const_cast<GameHandler*>(this)->loadSpellNameCache();
loadSpellNameCache();
auto it = spellNameCache_.find(spellId);
if (it == spellNameCache_.end()) return true; // assume interruptible if unknown
// SPELL_ATTR_EX_NOT_INTERRUPTIBLE = bit 4 of AttributesEx (0x00000010)
@ -22447,7 +22447,7 @@ bool GameHandler::isSpellInterruptible(uint32_t spellId) const {
uint32_t GameHandler::getSpellSchoolMask(uint32_t spellId) const {
if (spellId == 0) return 0;
const_cast<GameHandler*>(this)->loadSpellNameCache();
loadSpellNameCache();
auto it = spellNameCache_.find(spellId);
return (it != spellNameCache_.end()) ? it->second.schoolMask : 0;
}
@ -25596,7 +25596,7 @@ void GameHandler::sendLootRoll(uint64_t objectGuid, uint32_t slot, uint8_t rollT
// PackedTime date — uint32 bitfield (seconds since epoch)
// uint32 realmFirst — how many on realm also got it (0 = realm first)
// ---------------------------------------------------------------------------
void GameHandler::loadTitleNameCache() {
void GameHandler::loadTitleNameCache() const {
if (titleNameCacheLoaded_) return;
titleNameCacheLoaded_ = true;
@ -25624,7 +25624,7 @@ void GameHandler::loadTitleNameCache() {
}
std::string GameHandler::getFormattedTitle(uint32_t bit) const {
const_cast<GameHandler*>(this)->loadTitleNameCache();
loadTitleNameCache();
auto it = titleNameCache_.find(bit);
if (it == titleNameCache_.end() || it->second.empty()) return {};
@ -25840,7 +25840,7 @@ void GameHandler::handleRespondInspectAchievements(network::Packet& packet) {
// Faction name cache (lazily loaded from Faction.dbc)
// ---------------------------------------------------------------------------
void GameHandler::loadFactionNameCache() {
void GameHandler::loadFactionNameCache() const {
if (factionNameCacheLoaded_) return;
factionNameCacheLoaded_ = true;
@ -25897,13 +25897,13 @@ void GameHandler::loadFactionNameCache() {
}
uint32_t GameHandler::getFactionIdByRepListId(uint32_t repListId) const {
const_cast<GameHandler*>(this)->loadFactionNameCache();
loadFactionNameCache();
auto it = factionRepListToId_.find(repListId);
return (it != factionRepListToId_.end()) ? it->second : 0u;
}
uint32_t GameHandler::getRepListIdByFactionId(uint32_t factionId) const {
const_cast<GameHandler*>(this)->loadFactionNameCache();
loadFactionNameCache();
auto it = factionIdToRepList_.find(factionId);
return (it != factionIdToRepList_.end()) ? it->second : 0xFFFFFFFFu;
}
@ -25930,7 +25930,7 @@ std::string GameHandler::getFactionName(uint32_t factionId) const {
}
const std::string& GameHandler::getFactionNamePublic(uint32_t factionId) const {
const_cast<GameHandler*>(this)->loadFactionNameCache();
loadFactionNameCache();
auto it = factionNameCache_.find(factionId);
if (it != factionNameCache_.end()) return it->second;
static const std::string empty;
@ -25941,7 +25941,7 @@ const std::string& GameHandler::getFactionNamePublic(uint32_t factionId) const {
// Area name cache (lazy-loaded from WorldMapArea.dbc)
// ---------------------------------------------------------------------------
void GameHandler::loadAreaNameCache() {
void GameHandler::loadAreaNameCache() const {
if (areaNameCacheLoaded_) return;
areaNameCacheLoaded_ = true;
@ -25971,12 +25971,12 @@ void GameHandler::loadAreaNameCache() {
std::string GameHandler::getAreaName(uint32_t areaId) const {
if (areaId == 0) return {};
const_cast<GameHandler*>(this)->loadAreaNameCache();
loadAreaNameCache();
auto it = areaNameCache_.find(areaId);
return (it != areaNameCache_.end()) ? it->second : std::string{};
}
void GameHandler::loadMapNameCache() {
void GameHandler::loadMapNameCache() const {
if (mapNameCacheLoaded_) return;
mapNameCacheLoaded_ = true;
@ -26000,7 +26000,7 @@ void GameHandler::loadMapNameCache() {
std::string GameHandler::getMapName(uint32_t mapId) const {
if (mapId == 0) return {};
const_cast<GameHandler*>(this)->loadMapNameCache();
loadMapNameCache();
auto it = mapNameCache_.find(mapId);
return (it != mapNameCache_.end()) ? it->second : std::string{};
}
@ -26009,7 +26009,7 @@ std::string GameHandler::getMapName(uint32_t mapId) const {
// LFG dungeon name cache (WotLK: LFGDungeons.dbc)
// ---------------------------------------------------------------------------
void GameHandler::loadLfgDungeonDbc() {
void GameHandler::loadLfgDungeonDbc() const {
if (lfgDungeonNameCacheLoaded_) return;
lfgDungeonNameCacheLoaded_ = true;
@ -26036,7 +26036,7 @@ void GameHandler::loadLfgDungeonDbc() {
std::string GameHandler::getLfgDungeonName(uint32_t dungeonId) const {
if (dungeonId == 0) return {};
const_cast<GameHandler*>(this)->loadLfgDungeonDbc();
loadLfgDungeonDbc();
auto it = lfgDungeonNameCache_.find(dungeonId);
return (it != lfgDungeonNameCache_.end()) ? it->second : std::string{};
}