Skip to content

Commit

Permalink
Misc: Use new unique_trackable_ptr for various classes exposed to sc…
Browse files Browse the repository at this point in the history
…ripts (not actually used anywhere currently)

Co-Authored-By: Shauren <[email protected]>
Co-Authored-By: Foe <[email protected]>
  • Loading branch information
3 people committed Aug 31, 2024
1 parent ab800dd commit 62e13e1
Show file tree
Hide file tree
Showing 26 changed files with 183 additions and 132 deletions.
2 changes: 0 additions & 2 deletions src/game/BattleGround/BattleGround.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,6 @@ BattleGround::~BattleGround()
{
// remove objects and creatures
// (this is done automatically in mapmanager update, when the instance is reset after the reset time)
sBattleGroundMgr.RemoveBattleGround(GetInstanceId(), GetTypeId());

// skip template bgs as they were never added to visible bg list
BattleGroundBracketId bracketId = GetBracketId();
if (bracketId != BG_BRACKET_ID_TEMPLATE)
Expand Down
6 changes: 6 additions & 0 deletions src/game/BattleGround/BattleGround.h
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,10 @@ class BattleGround

uint32 GetPlayerSkinRefLootId() const { return m_playerSkinReflootId; }
void SetPlayerSkinRefLootId(uint32 reflootId) { m_playerSkinReflootId = reflootId; }

MaNGOS::unique_weak_ptr<BattleGround> GetWeakPtr() const { return m_weakRef; }
void SetWeakPtr(MaNGOS::unique_weak_ptr<BattleGround> weakRef) { m_weakRef = std::move(weakRef); }

protected:
// this method is called, when BG cannot spawn its own spirit guide, or something is wrong, It correctly ends BattleGround
void EndNow();
Expand Down Expand Up @@ -707,6 +711,8 @@ class BattleGround
float m_startMaxDist;

uint32 m_playerSkinReflootId;

MaNGOS::unique_weak_ptr<BattleGround> m_weakRef;
};

// helper functions for world state list fill
Expand Down
24 changes: 12 additions & 12 deletions src/game/BattleGround/BattleGroundMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1279,14 +1279,7 @@ void BattleGroundMgr::DeleteAllBattleGrounds()
{
// will also delete template bgs:
for (uint8 i = BATTLEGROUND_TYPE_NONE; i < MAX_BATTLEGROUND_TYPE_ID; ++i)
{
for (BattleGroundSet::iterator itr = m_battleGrounds[i].begin(); itr != m_battleGrounds[i].end();)
{
BattleGround* bg = itr->second;
++itr; // step from invalidate iterator pos in result element remove in ~BattleGround call
delete bg;
}
}
m_battleGrounds[i].clear();
}

/**
Expand Down Expand Up @@ -1610,7 +1603,7 @@ BattleGround* BattleGroundMgr::GetBattleGroundThroughClientInstance(uint32 insta
for (auto& itr : m_battleGrounds[bgTypeId])
{
if (itr.second->GetClientInstanceId() == instanceId)
return itr.second;
return itr.second.get();
}

return nullptr;
Expand All @@ -1632,13 +1625,13 @@ BattleGround* BattleGroundMgr::GetBattleGround(uint32 instanceId, BattleGroundTy
{
itr = m_battleGrounds[i].find(instanceId);
if (itr != m_battleGrounds[i].end())
return itr->second;
return itr->second.get();
}
return nullptr;
}

itr = m_battleGrounds[bgTypeId].find(instanceId);
return ((itr != m_battleGrounds[bgTypeId].end()) ? itr->second : nullptr);
return ((itr != m_battleGrounds[bgTypeId].end()) ? itr->second.get() : nullptr);
}

/**
Expand All @@ -1649,7 +1642,7 @@ BattleGround* BattleGroundMgr::GetBattleGround(uint32 instanceId, BattleGroundTy
BattleGround* BattleGroundMgr::GetBattleGroundTemplate(BattleGroundTypeId bgTypeId)
{
// map is sorted and we can be sure that lowest instance id has only BG template
return m_battleGrounds[bgTypeId].empty() ? nullptr : m_battleGrounds[bgTypeId].begin()->second;
return m_battleGrounds[bgTypeId].empty() ? nullptr : m_battleGrounds[bgTypeId].begin()->second.get();
}

/**
Expand Down Expand Up @@ -1827,6 +1820,13 @@ uint32 BattleGroundMgr::CreateBattleGround(BattleGroundTypeId bgTypeId, bool IsA
return bgTypeId;
}

void BattleGroundMgr::AddBattleGround(uint32 instanceId, BattleGroundTypeId bgTypeId, BattleGround* bg)
{
MaNGOS::unique_trackable_ptr<BattleGround>& ptr = m_battleGrounds[bgTypeId][instanceId];
ptr.reset(bg);
bg->SetWeakPtr(ptr);
}

/**
Method that loads battleground data from DB
*/
Expand Down
5 changes: 3 additions & 2 deletions src/game/BattleGround/BattleGroundMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
#include "Utilities/EventProcessor.h"
#include "Globals/SharedDefines.h"
#include "Server/DBCEnums.h"
#include "Util/UniqueTrackablePtr.h"
#include "BattleGround.h"

#include <mutex>

typedef std::map<uint32, BattleGround*> BattleGroundSet;
typedef std::map<uint32, MaNGOS::unique_trackable_ptr<BattleGround>> BattleGroundSet;

// this container can't be deque, because deque doesn't like removing the last element - if you remove it, it invalidates next iterator and crash appears
typedef std::list<BattleGround*> BgFreeSlotQueueType;
Expand Down Expand Up @@ -228,7 +229,7 @@ class BattleGroundMgr

uint32 CreateBattleGround(BattleGroundTypeId /*bgTypeId*/, bool /*isArena*/, uint32 /*minPlayersPerTeam*/, uint32 /*maxPlayersPerTeam*/, uint32 /*levelMin*/, uint32 /*levelMax*/, char const* /*battleGroundName*/, uint32 /*mapId*/, float /*team1StartLocX*/, float /*team1StartLocY*/, float /*team1StartLocZ*/, float /*team1StartLocO*/, float /*team2StartLocX*/, float /*team2StartLocY*/, float /*team2StartLocZ*/, float /*team2StartLocO*/, float /*startMaxDist*/, uint32 /*playerSkinReflootId*/);

void AddBattleGround(uint32 instanceId, BattleGroundTypeId bgTypeId, BattleGround* bg) { m_battleGrounds[bgTypeId][instanceId] = bg; };
void AddBattleGround(uint32 instanceId, BattleGroundTypeId bgTypeId, BattleGround* bg);;
void RemoveBattleGround(uint32 instanceId, BattleGroundTypeId bgTypeId) { m_battleGrounds[bgTypeId].erase(instanceId); }

uint32 CreateClientVisibleInstanceId(BattleGroundTypeId /*bgTypeId*/, BattleGroundBracketId /*bracketId*/);
Expand Down
6 changes: 1 addition & 5 deletions src/game/Chat/Level3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2846,7 +2846,7 @@ bool ChatHandler::HandleLookupQuestCommand(char* args)
ObjectMgr::QuestMap const& qTemplates = sObjectMgr.GetQuestTemplates();
for (const auto& qTemplate : qTemplates)
{
Quest* qinfo = qTemplate.second;
Quest* qinfo = qTemplate.second.get();

std::string title; // "" for avoid repeating check default locale
sObjectMgr.GetQuestLocaleStrings(qinfo->GetQuestId(), loc_idx, &title);
Expand Down Expand Up @@ -3118,10 +3118,7 @@ bool ChatHandler::HandleGuildUninviteCommand(char* args)
return false;

if (targetGuild->DelMember(target_guid))
{
targetGuild->Disband();
delete targetGuild;
}

return true;
}
Expand Down Expand Up @@ -3186,7 +3183,6 @@ bool ChatHandler::HandleGuildDeleteCommand(char* args)
return false;

targetGuild->Disband();
delete targetGuild;

return true;
}
Expand Down
26 changes: 25 additions & 1 deletion src/game/Entities/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
#include "LuaEngine/ElunaEventMgr.h"
#endif

Object::Object(): m_updateFlag(0), m_itsNewObject(false), m_dbGuid(0)
Object::Object(): m_updateFlag(0), m_itsNewObject(false), m_dbGuid(0), m_scriptRef(this, NoopObjectDeleter())
{
m_objectTypeId = TYPEID_OBJECT;
m_objectType = TYPEMASK_OBJECT;
Expand Down Expand Up @@ -83,6 +83,30 @@ Object::~Object()
delete m_loot;
}

void Object::AddToWorld()
{
if (m_inWorld)
return;

m_inWorld = true;

// synchronize values mirror with values array (changes will send in updatecreate opcode any way
ClearUpdateMask(false); // false - we can't have update data in update queue before adding to world

// Set new ref when adding to world (except if we already have one - also set in constructor to allow scripts to work in initialization phase)
// Changing the ref when adding/removing from world prevents accessing players on different maps (possibly from another thread)
if (!m_scriptRef)
m_scriptRef.reset(this, NoopObjectDeleter());
}

void Object::RemoveFromWorld()
{
// if we remove from world then sending changes not required
ClearUpdateMask(true);
m_inWorld = false;
m_scriptRef = nullptr;
}

void Object::_InitValues()
{
m_uint32Values = new uint32[ m_valuesCount ];
Expand Down
24 changes: 8 additions & 16 deletions src/game/Entities/Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "PlayerDefines.h"
#include "Entities/ObjectVisibility.h"
#include "Grids/Cell.h"
#include "Util/UniqueTrackablePtr.h"
#include "Utilities/EventProcessor.h"
#ifdef BUILD_ELUNA
#include "LuaEngine/LuaValue.h"
Expand Down Expand Up @@ -385,22 +386,8 @@ class Object
virtual ~Object();

const bool& IsInWorld() const { return m_inWorld; }
virtual void AddToWorld()
{
if (m_inWorld)
return;

m_inWorld = true;

// synchronize values mirror with values array (changes will send in updatecreate opcode any way
ClearUpdateMask(false); // false - we can't have update data in update queue before adding to world
}
virtual void RemoveFromWorld()
{
// if we remove from world then sending changes not required
ClearUpdateMask(true);
m_inWorld = false;
}
virtual void AddToWorld();
virtual void RemoveFromWorld();

ObjectGuid const& GetObjectGuid() const { return GetGuidValue(OBJECT_FIELD_GUID); }
uint32 GetGUIDLow() const { return GetObjectGuid().GetCounter(); }
Expand Down Expand Up @@ -655,6 +642,8 @@ class Object
inline bool IsGameObject() const { return GetTypeId() == TYPEID_GAMEOBJECT; }
inline bool IsCorpse() const { return GetTypeId() == TYPEID_CORPSE; }

MaNGOS::unique_weak_ptr<Object> GetWeakPtr() const { return m_scriptRef; }

protected:
Object();

Expand Down Expand Up @@ -698,6 +687,9 @@ class Object

uint32 m_dbGuid;

struct NoopObjectDeleter { void operator()(Object*) const { /*noop - not managed*/ } };
MaNGOS::unique_trackable_ptr<Object> m_scriptRef;

public:
// for output helpfull error messages from ASSERTs
bool PrintIndexError(uint32 index, bool set) const;
Expand Down
9 changes: 1 addition & 8 deletions src/game/Entities/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4282,17 +4282,10 @@ void Player::DeleteFromDB(ObjectGuid playerguid, uint32 accountId, bool updateRe

// remove from guild
if (uint32 guildId = GetGuildIdFromDB(playerguid))
{
if (Guild* guild = sGuildMgr.GetGuildById(guildId))
{
if (guild->DelMember(playerguid))
{
guild->Disband();
delete guild;
}
}
}


// remove from arena teams
LeaveAllArenaTeams(playerguid);

Expand Down
1 change: 1 addition & 0 deletions src/game/Entities/Unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5700,6 +5700,7 @@ void Unit::RemoveAura(Aura* Aur, AuraRemoveMode mode)

// Set remove mode
Aur->SetRemoveMode(mode);
Aur->InvalidateScriptRef();

// some ShapeshiftBoosts at remove trigger removing other auras including parent Shapeshift aura
// remove aura from list before to prevent deleting it before
Expand Down
15 changes: 5 additions & 10 deletions src/game/Globals/ObjectMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,6 @@ ObjectMgr::ObjectMgr() :

ObjectMgr::~ObjectMgr()
{
for (auto& mQuestTemplate : mQuestTemplates)
delete mQuestTemplate.second;

for (auto& i : petInfo)
delete[] i.second;

Expand Down Expand Up @@ -4517,9 +4514,6 @@ void ObjectMgr::LoadGroups()
void ObjectMgr::LoadQuests()
{
// For reload case
for (QuestMap::const_iterator itr = mQuestTemplates.begin(); itr != mQuestTemplates.end(); ++itr)
delete itr->second;

mQuestTemplates.clear();

m_ExclusiveQuestGroups.clear();
Expand Down Expand Up @@ -4581,7 +4575,8 @@ void ObjectMgr::LoadQuests()
Field* fields = queryResult->Fetch();

Quest* newQuest = new Quest(fields);
mQuestTemplates[newQuest->GetQuestId()] = newQuest;
auto itr = mQuestTemplates.try_emplace(newQuest->GetQuestId(), newQuest).first;
newQuest->m_weakRef = itr->second;
}
while (queryResult->NextRow());

Expand All @@ -4591,7 +4586,7 @@ void ObjectMgr::LoadQuests()

for (auto& mQuestTemplate : mQuestTemplates)
{
Quest* qinfo = mQuestTemplate.second;
Quest* qinfo = mQuestTemplate.second.get();

// additional quest integrity checks (GO, creature_template and item_template must be loaded already)

Expand Down Expand Up @@ -5170,7 +5165,7 @@ void ObjectMgr::LoadQuests()
// Prevent any breadcrumb loops, and inform target quests of their breadcrumbs
for (auto& mQuestTemplate : mQuestTemplates)
{
Quest* qinfo = mQuestTemplate.second;
Quest* qinfo = mQuestTemplate.second.get();
uint32 qid = qinfo->GetQuestId();
uint32 breadcrumbForQuestId = qinfo->BreadcrumbForQuestId;
std::set<uint32> questSet;
Expand Down Expand Up @@ -5851,7 +5846,7 @@ void ObjectMgr::LoadConditions()

for (auto& mQuestTemplate : mQuestTemplates) // needs to be checked after loading conditions
{
Quest* qinfo = mQuestTemplate.second;
Quest* qinfo = mQuestTemplate.second.get();

if (qinfo->RequiredCondition)
{
Expand Down
5 changes: 3 additions & 2 deletions src/game/Globals/ObjectMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "Entities/ObjectGuid.h"
#include "Globals/Conditions.h"
#include "Maps/SpawnGroupDefines.h"
#include "Util/UniqueTrackablePtr.h"

#include <map>
#include <climits>
Expand Down Expand Up @@ -470,7 +471,7 @@ class ObjectMgr

typedef std::unordered_map<uint32, ArenaTeam*> ArenaTeamMap;

typedef std::unordered_map<uint32, Quest*> QuestMap;
typedef std::unordered_map<uint32, MaNGOS::unique_trackable_ptr<Quest>> QuestMap;

typedef std::unordered_map<uint32, AreaTrigger> AreaTriggerMap;

Expand Down Expand Up @@ -541,7 +542,7 @@ class ObjectMgr
Quest const* GetQuestTemplate(uint32 quest_id) const
{
QuestMap::const_iterator itr = mQuestTemplates.find(quest_id);
return itr != mQuestTemplates.end() ? itr->second : nullptr;
return itr != mQuestTemplates.end() ? itr->second.get() : nullptr;
}
QuestMap const& GetQuestTemplates() const { return mQuestTemplates; }

Expand Down
2 changes: 1 addition & 1 deletion src/game/Groups/Group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ GroupMemberStatus GetGroupMemberStatus(const Player* member = nullptr)
Group::Group() : m_Id(0), m_leaderLastOnline(0), m_groupFlags(GROUP_FLAG_NORMAL),
m_difficulty(REGULAR_DIFFICULTY),
m_bgGroup(nullptr), m_lootMethod(FREE_FOR_ALL), m_lootThreshold(ITEM_QUALITY_UNCOMMON),
m_subGroupsCounts(nullptr)
m_subGroupsCounts(nullptr), m_scriptRef(this, NoopGroupDeleter())
{
}

Expand Down
6 changes: 6 additions & 0 deletions src/game/Groups/Group.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "BattleGround/BattleGround.h"
#include "Server/DBCEnums.h"
#include "Globals/SharedDefines.h"
#include "Util/UniqueTrackablePtr.h"

class WorldSession;
class Map;
Expand Down Expand Up @@ -277,6 +278,8 @@ class Group

ObjectGuid GetTargetIcon(int index) { return m_targetIcons[index]; }

MaNGOS::unique_weak_ptr<Group> GetWeakPtr() const { return m_scriptRef; }

protected:
bool _addMember(ObjectGuid guid, const char* name, bool isAssistant = false);
bool _addMember(ObjectGuid guid, const char* name, bool isAssistant, uint8 group);
Expand Down Expand Up @@ -365,5 +368,8 @@ class Group
ObjectGuid m_currentLooterGuid;
BoundInstancesMap m_boundInstances[MAX_DIFFICULTY];
uint8* m_subGroupsCounts;

struct NoopGroupDeleter { void operator()(Group*) const { /*noop - not managed*/ } };
MaNGOS::unique_trackable_ptr<Group> m_scriptRef;
};
#endif
Loading

0 comments on commit 62e13e1

Please sign in to comment.