Skip to content

Commit ae6b9bd

Browse files
committed
Core/Entities: Fixed m_stringIds[0] invalidation when reloading creature_template with a gm command
(cherry picked from commit bec5bdb61b1b78ae7f750019b9a187b489ebb496)
1 parent 5655aa4 commit ae6b9bd

File tree

5 files changed

+23
-22
lines changed

5 files changed

+23
-22
lines changed

src/server/game/Entities/Creature/Creature.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,12 @@ bool ForcedDespawnDelayEvent::Execute(uint64 /*e_time*/, uint32 /*p_time*/)
251251
}
252252

253253
Creature::Creature(bool isWorldObject): Unit(isWorldObject), MapObject(), m_groupLootTimer(0), lootingGroupLowGUID(0), m_PlayerDamageReq(0), m_lootRecipient(), m_lootRecipientGroup(0), _pickpocketLootRestore(0),
254-
m_corpseRemoveTime(0), m_respawnTime(0), m_respawnDelay(300), m_corpseDelay(60), m_ignoreCorpseDecayRatio(false), m_wanderDistance(0.0f), m_boundaryCheckTime(2500), m_combatPulseTime(0), m_combatPulseDelay(0), m_reactState(REACT_AGGRESSIVE),
255-
m_defaultMovementType(IDLE_MOTION_TYPE), m_spawnId(0), m_equipmentId(0), m_originalEquipmentId(0), m_AlreadyCallAssistance(false), m_AlreadySearchedAssistance(false), m_cannotReachTarget(false), m_cannotReachTimer(0),
256-
m_meleeDamageSchoolMask(SPELL_SCHOOL_MASK_NORMAL), m_originalEntry(0), m_homePosition(), m_transportHomePosition(), m_creatureInfo(nullptr), m_creatureData(nullptr), _waypointPathId(0), _currentWaypointNodeInfo(0, 0),
254+
m_corpseRemoveTime(0), m_respawnTime(0), m_respawnDelay(300), m_corpseDelay(60), m_ignoreCorpseDecayRatio(false), m_wanderDistance(0.0f),
255+
m_boundaryCheckTime(2500), m_combatPulseTime(0), m_combatPulseDelay(0), m_reactState(REACT_AGGRESSIVE),
256+
m_defaultMovementType(IDLE_MOTION_TYPE), m_spawnId(0), m_equipmentId(0), m_originalEquipmentId(0),
257+
m_AlreadyCallAssistance(false), m_AlreadySearchedAssistance(false), m_cannotReachTarget(false), m_cannotReachTimer(0),
258+
m_meleeDamageSchoolMask(SPELL_SCHOOL_MASK_NORMAL), m_originalEntry(0), m_homePosition(), m_transportHomePosition(),
259+
m_creatureInfo(nullptr), m_creatureData(nullptr), m_stringIds(), _waypointPathId(0), _currentWaypointNodeInfo(0, 0),
257260
m_formation(nullptr), m_triggerJustAppeared(true), m_respawnCompatibilityMode(false), _lastDamagedTime(0),
258261
_regenerateHealth(true), _regenerateHealthLock(false), _isMissingCanSwimFlagOutOfCombat(false)
259262
{
@@ -635,7 +638,7 @@ bool Creature::UpdateEntry(uint32 entry, CreatureData const* data /*= nullptr*/,
635638
//We must update last scriptId or it looks like we reloaded a script, breaking some things such as gossip temporarily
636639
LastUsedScriptID = GetScriptId();
637640

638-
m_stringIds[AsUnderlyingType(StringIdType::Template)] = cInfo->StringId;
641+
m_stringIds[AsUnderlyingType(StringIdType::Template)] = &cInfo->StringId;
639642

640643
return true;
641644
}
@@ -1687,7 +1690,7 @@ bool Creature::LoadFromDB(ObjectGuid::LowType spawnId, Map* map, bool addToMap,
16871690
// checked at creature_template loading
16881691
m_defaultMovementType = MovementGeneratorType(data->movementType);
16891692

1690-
m_stringIds[AsUnderlyingType(StringIdType::Spawn)] = data->StringId;
1693+
m_stringIds[AsUnderlyingType(StringIdType::Spawn)] = &data->StringId;
16911694

16921695
if (addToMap && !GetMap()->AddToMap(this))
16931696
return false;
@@ -2787,20 +2790,20 @@ void Creature::InheritStringIds(Creature const* parent)
27872790

27882791
bool Creature::HasStringId(std::string_view id) const
27892792
{
2790-
return std::find(m_stringIds.begin(), m_stringIds.end(), id) != m_stringIds.end();
2793+
return std::ranges::any_of(m_stringIds, [id](std::string const* stringId) { return stringId && *stringId == id; });
27912794
}
27922795

27932796
void Creature::SetScriptStringId(std::string id)
27942797
{
27952798
if (!id.empty())
27962799
{
27972800
m_scriptStringId.emplace(std::move(id));
2798-
m_stringIds[AsUnderlyingType(StringIdType::Script)] = *m_scriptStringId;
2801+
m_stringIds[AsUnderlyingType(StringIdType::Script)] = &*m_scriptStringId;
27992802
}
28002803
else
28012804
{
28022805
m_scriptStringId.reset();
2803-
m_stringIds[AsUnderlyingType(StringIdType::Script)] = {};
2806+
m_stringIds[AsUnderlyingType(StringIdType::Script)] = nullptr;
28042807
}
28052808
}
28062809

src/server/game/Entities/Creature/Creature.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,7 @@ class TC_GAME_API Creature : public Unit, public GridObject<Creature>, public Ma
201201
void InheritStringIds(Creature const* parent);
202202
bool HasStringId(std::string_view id) const;
203203
void SetScriptStringId(std::string id);
204-
std::array<std::string_view, 3> const& GetStringIds() const { return m_stringIds; }
205-
std::string_view GetStringId(StringIdType type) const { return m_stringIds[size_t(type)]; }
204+
std::string_view GetStringId(StringIdType type) const { return m_stringIds[size_t(type)] ? std::string_view(*m_stringIds[size_t(type)]) : std::string_view(); }
206205

207206
// override WorldObject function for proper name localization
208207
std::string const& GetNameForLocaleIdx(LocaleConstant locale_idx) const override;
@@ -426,7 +425,7 @@ class TC_GAME_API Creature : public Unit, public GridObject<Creature>, public Ma
426425

427426
CreatureTemplate const* m_creatureInfo; // Can differ from sObjectMgr->GetCreatureTemplate(GetEntry()) in difficulty mode > 0
428427
CreatureData const* m_creatureData;
429-
std::array<std::string_view, 3> m_stringIds;
428+
std::array<std::string const*, 3> m_stringIds;
430429
Optional<std::string> m_scriptStringId;
431430

432431
uint16 m_LootMode; // Bitmask (default: LOOT_MODE_DEFAULT) that determines what loot will be lootable

src/server/game/Entities/GameObject/GameObject.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ QuaternionData QuaternionData::fromEulerAnglesZYX(float Z, float Y, float X)
108108
}
109109

110110
GameObject::GameObject() : WorldObject(false), MapObject(),
111-
m_model(nullptr), m_goValue(), m_AI(nullptr), m_respawnCompatibilityMode(false)
111+
m_model(nullptr), m_goValue(), m_stringIds(), m_AI(nullptr), m_respawnCompatibilityMode(false)
112112
{
113113
m_objectType |= TYPEMASK_GAMEOBJECT;
114114
m_objectTypeId = TYPEID_GAMEOBJECT;
@@ -418,7 +418,7 @@ bool GameObject::Create(ObjectGuid::LowType guidlow, uint32 name_id, Map* map, u
418418

419419
LastUsedScriptID = GetGOInfo()->ScriptId;
420420

421-
m_stringIds[AsUnderlyingType(StringIdType::Template)] = goinfo->StringId;
421+
m_stringIds[AsUnderlyingType(StringIdType::Template)] = &goinfo->StringId;
422422

423423
AIM_Initialize();
424424

@@ -1145,7 +1145,7 @@ bool GameObject::LoadFromDB(ObjectGuid::LowType spawnId, Map* map, bool addToMap
11451145

11461146
m_goData = data;
11471147

1148-
m_stringIds[AsUnderlyingType(StringIdType::Spawn)] = data->StringId;
1148+
m_stringIds[AsUnderlyingType(StringIdType::Spawn)] = &data->StringId;
11491149

11501150
if (addToMap && !GetMap()->AddToMap(this))
11511151
return false;
@@ -2286,20 +2286,20 @@ void GameObject::InheritStringIds(GameObject const* parent)
22862286

22872287
bool GameObject::HasStringId(std::string_view id) const
22882288
{
2289-
return std::find(m_stringIds.begin(), m_stringIds.end(), id) != m_stringIds.end();
2289+
return std::ranges::any_of(m_stringIds, [id](std::string const* stringId) { return stringId && *stringId == id; });
22902290
}
22912291

22922292
void GameObject::SetScriptStringId(std::string id)
22932293
{
22942294
if (!id.empty())
22952295
{
22962296
m_scriptStringId.emplace(std::move(id));
2297-
m_stringIds[AsUnderlyingType(StringIdType::Script)] = *m_scriptStringId;
2297+
m_stringIds[AsUnderlyingType(StringIdType::Script)] = &*m_scriptStringId;
22982298
}
22992299
else
23002300
{
23012301
m_scriptStringId.reset();
2302-
m_stringIds[AsUnderlyingType(StringIdType::Script)] = {};
2302+
m_stringIds[AsUnderlyingType(StringIdType::Script)] = nullptr;
23032303
}
23042304
}
23052305

src/server/game/Entities/GameObject/GameObject.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,7 @@ class TC_GAME_API GameObject : public WorldObject, public GridObject<GameObject>
277277
void InheritStringIds(GameObject const* parent);
278278
bool HasStringId(std::string_view id) const;
279279
void SetScriptStringId(std::string id);
280-
std::array<std::string_view, 3> const& GetStringIds() const { return m_stringIds; }
281-
std::string_view GetStringId(StringIdType type) const { return m_stringIds[size_t(type)]; }
280+
std::string_view GetStringId(StringIdType type) const { return m_stringIds[size_t(type)] ? std::string_view(*m_stringIds[size_t(type)]) : std::string_view(); }
282281

283282
void SetDisplayId(uint32 displayid);
284283
uint32 GetDisplayId() const { return GetUInt32Value(GAMEOBJECT_DISPLAYID); }
@@ -345,7 +344,7 @@ class TC_GAME_API GameObject : public WorldObject, public GridObject<GameObject>
345344
GameObjectTemplateAddon const* m_goTemplateAddon;
346345
GameObjectData const* m_goData;
347346
GameObjectValue m_goValue;
348-
std::array<std::string_view, 3> m_stringIds;
347+
std::array<std::string const*, 3> m_stringIds;
349348
Optional<std::string> m_scriptStringId;
350349

351350
int64 m_packedRotation;

src/server/scripts/Commands/cs_npc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,8 @@ class npc_commandscript : public CommandScript
507507
handler->PSendSysMessage(LANG_NPCINFO_ARMOR, target->GetArmor());
508508
handler->PSendSysMessage(LANG_NPCINFO_POSITION, target->GetPositionX(), target->GetPositionY(), target->GetPositionZ());
509509
handler->PSendSysMessage(LANG_OBJECTINFO_AIINFO, target->GetAIName().c_str(), target->GetScriptName().c_str());
510-
handler->PSendSysMessage(LANG_OBJECTINFO_STRINGIDS, STRING_VIEW_FMT_ARG(target->GetStringIds()[0]),
511-
STRING_VIEW_FMT_ARG(target->GetStringIds()[1]), STRING_VIEW_FMT_ARG(target->GetStringIds()[2]));
510+
handler->PSendSysMessage(LANG_OBJECTINFO_STRINGIDS, STRING_VIEW_FMT_ARG(target->GetStringId(StringIdType::Template)),
511+
STRING_VIEW_FMT_ARG(target->GetStringId(StringIdType::Spawn)), STRING_VIEW_FMT_ARG(target->GetStringId(StringIdType::Script)));
512512
handler->PSendSysMessage(LANG_NPCINFO_REACTSTATE, DescribeReactState(target->GetReactState()));
513513
if (CreatureAI const* ai = target->AI())
514514
handler->PSendSysMessage(LANG_OBJECTINFO_AITYPE, Trinity::GetTypeName(*ai).c_str());

0 commit comments

Comments
 (0)