feat(Core/Hooks): Add OnAfterDatabaseLoadCreatureTemplates hook (#17621)

* Add OnAfterDatabaseLoadCreatureTemplates hook
- fix issue where the fast template cache was not updated correctly after a CS reload of a single creature template

* Parameter naming tweak to reduce confusion.

* Refactor to streamline fastcache creation, remove threading concerns

* Remove unneeded resize when loading creature templates

* Update fast cache resizing to highest-performing option
This commit is contained in:
KJack
2023-10-31 05:58:45 -04:00
committed by GitHub
parent 52d37a927d
commit 751ffc7c70
5 changed files with 51 additions and 18 deletions

View File

@@ -591,7 +591,7 @@ void ObjectMgr::LoadCreatureTemplates()
"ctm.Ground, ctm.Swim, ctm.Flight, ctm.Rooted, ctm.Chase, ctm.Random, ctm.InteractionPauseTimer, HoverHeight, HealthModifier, ManaModifier, ArmorModifier, ExperienceModifier, "
// 64 65 66 67 68 69 70
"RacialLeader, movementId, RegenHealth, mechanic_immune_mask, spell_school_immune_mask, flags_extra, ScriptName "
"FROM creature_template ct LEFT JOIN creature_template_movement ctm ON ct.entry = ctm.CreatureId;");
"FROM creature_template ct LEFT JOIN creature_template_movement ctm ON ct.entry = ctm.CreatureId ORDER BY entry DESC;");
if (!result)
{
@@ -600,6 +600,7 @@ void ObjectMgr::LoadCreatureTemplates()
}
_creatureTemplateStore.rehash(result->GetRowCount());
_creatureTemplateStoreFast.clear();
uint32 count = 0;
do
@@ -609,20 +610,7 @@ void ObjectMgr::LoadCreatureTemplates()
++count;
} while (result->NextRow());
// pussywizard:
{
uint32 max = 0;
for (CreatureTemplateContainer::const_iterator itr = _creatureTemplateStore.begin(); itr != _creatureTemplateStore.end(); ++itr)
if (itr->first > max)
max = itr->first;
if (max)
{
_creatureTemplateStoreFast.clear();
_creatureTemplateStoreFast.resize(max + 1, nullptr);
for (CreatureTemplateContainer::iterator itr = _creatureTemplateStore.begin(); itr != _creatureTemplateStore.end(); ++itr)
_creatureTemplateStoreFast[itr->first] = &(itr->second);
}
}
sScriptMgr->OnAfterDatabaseLoadCreatureTemplates(_creatureTemplateStoreFast);
LoadCreatureTemplateResistances();
LoadCreatureTemplateSpells();
@@ -638,12 +626,28 @@ void ObjectMgr::LoadCreatureTemplates()
LOG_INFO("server.loading", " ");
}
void ObjectMgr::LoadCreatureTemplate(Field* fields)
/**
* @brief Loads a creature template from a database result
*
* @param fields Database result
* @param triggerHook If true, will trigger the OnAfterDatabaseLoadCreatureTemplates hook. Useful if you are not calling the hook yourself.
*/
void ObjectMgr::LoadCreatureTemplate(Field* fields, bool triggerHook)
{
uint32 entry = fields[0].Get<uint32>();
CreatureTemplate& creatureTemplate = _creatureTemplateStore[entry];
// enlarge the fast cache as necessary
if (_creatureTemplateStoreFast.size() < entry + 1)
{
_creatureTemplateStoreFast.resize(entry + 1, nullptr);
}
// load a pointer to this creatureTemplate into the fast cache
_creatureTemplateStoreFast[entry] = &creatureTemplate;
// build the creatureTemplate
creatureTemplate.Entry = entry;
for (uint8 i = 0; i < MAX_DIFFICULTY - 1; ++i)
@@ -750,6 +754,13 @@ void ObjectMgr::LoadCreatureTemplate(Field* fields)
creatureTemplate.SpellSchoolImmuneMask = fields[68].Get<uint8>();
creatureTemplate.flags_extra = fields[69].Get<uint32>();
creatureTemplate.ScriptID = GetScriptId(fields[70].Get<std::string>());
// useful if the creature template load is being triggered from outside this class
if (triggerHook)
{
sScriptMgr->OnAfterDatabaseLoadCreatureTemplates(_creatureTemplateStoreFast);
}
}
void ObjectMgr::LoadCreatureTemplateResistances()