Skip to content

Commit

Permalink
Misc: Use new unique_trackable_ptr for various classes exposed to scr…
Browse files Browse the repository at this point in the history
…ipts (not actually used anywhere currently)
  • Loading branch information
Shauren authored and killerwife committed Aug 16, 2024
1 parent 21b45c2 commit 219c347
Show file tree
Hide file tree
Showing 27 changed files with 157 additions and 118 deletions.
1 change: 0 additions & 1 deletion src/game/BattleGround/BattleGround.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,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();
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 @@ -673,6 +673,10 @@ class BattleGround
void SetPlayerSkinRefLootId(uint32 reflootId) { m_playerSkinReflootId = reflootId; }

virtual void AlterTeleportLocation(Player* player, ObjectGuid& transportGuid, float& x, float& y, float& z, float& ori) {}

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 @@ -765,6 +769,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 @@ -1285,14 +1285,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 @@ -1637,7 +1630,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 @@ -1659,13 +1652,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 @@ -1676,7 +1669,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 @@ -1893,6 +1886,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 @@ -215,7 +216,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 @@ -3259,7 +3259,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 @@ -3531,10 +3531,7 @@ bool ChatHandler::HandleGuildUninviteCommand(char* args)
return false;

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

return true;
}
Expand Down Expand Up @@ -3599,7 +3596,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 @@ -48,7 +48,7 @@
#include "MotionGenerators/PathFinder.h"
#include "Movement/MoveSpline.h"

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 @@ -81,6 +81,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
21 changes: 6 additions & 15 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"

#include <set>
Expand Down Expand Up @@ -389,22 +390,9 @@ class Object
virtual ~Object();

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

m_inWorld = true;
virtual void AddToWorld();

// 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 RemoveFromWorld();

ObjectGuid const& GetObjectGuid() const { return GetGuidValue(OBJECT_FIELD_GUID); }
uint32 GetGUIDLow() const { return GetObjectGuid().GetCounter(); }
Expand Down Expand Up @@ -683,6 +671,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
7 changes: 0 additions & 7 deletions src/game/Entities/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4513,16 +4513,9 @@ 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
7 changes: 3 additions & 4 deletions src/game/Entities/Unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,6 @@ Unit::~Unit()

delete m_combatData;
delete m_charmInfo;
delete m_vehicleInfo;
delete movespline;

// those should be already removed at "RemoveFromWorld()" call
Expand Down Expand Up @@ -6173,6 +6172,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 Expand Up @@ -12351,19 +12351,18 @@ void Unit::SetVehicleId(uint32 entry, uint32 overwriteNpcEntry)
if (m_vehicleInfo && entry == m_vehicleInfo->GetVehicleEntry()->m_ID)
return;

delete m_vehicleInfo;
m_vehicleInfo = nullptr;

if (entry)
{
VehicleEntry const* ventry = sVehicleStore.LookupEntry(entry);
MANGOS_ASSERT(ventry != nullptr);

m_vehicleInfo = new VehicleInfo(this, ventry, overwriteNpcEntry);
m_vehicleInfo.reset(new VehicleInfo(this, ventry, overwriteNpcEntry));
m_updateFlag |= UPDATEFLAG_VEHICLE;
}
else
{
m_vehicleInfo = nullptr;
m_updateFlag &= ~UPDATEFLAG_VEHICLE;
}

Expand Down
5 changes: 3 additions & 2 deletions src/game/Entities/Unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -1538,7 +1538,8 @@ class Unit : public WorldObject
virtual bool Mount(uint32 displayid, const Aura* aura = nullptr);
virtual bool Unmount(const Aura* aura = nullptr);

VehicleInfo* GetVehicleInfo() const { return m_vehicleInfo; }
VehicleInfo* GetVehicleInfo() const { return m_vehicleInfo.get(); }
MaNGOS::unique_weak_ptr<VehicleInfo> GetVehicleInfoWeakPtr() const { return m_vehicleInfo; }
bool IsVehicle() const { return m_vehicleInfo != nullptr; }
void SetVehicleId(uint32 entry, uint32 overwriteNpcEntry);
Unit const* FindRootVehicle(const Unit* whichVehicle = nullptr) const;
Expand Down Expand Up @@ -2732,7 +2733,7 @@ class Unit : public WorldObject

bool m_canDualWield = false;

VehicleInfo* m_vehicleInfo;
MaNGOS::unique_trackable_ptr<VehicleInfo> m_vehicleInfo;
void DisableSpline();
void EndSpline();
bool m_isCreatureLinkingTrigger;
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 @@ -172,9 +172,6 @@ ObjectMgr::ObjectMgr() :

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

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

Expand Down Expand Up @@ -4871,9 +4868,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 @@ -4943,7 +4937,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 @@ -4953,7 +4948,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 @@ -5533,7 +5528,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 @@ -6036,7 +6031,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 @@ -36,6 +36,7 @@
#include "Globals/Conditions.h"
#include "Maps/SpawnGroupDefines.h"
#include "Entities/Vehicle.h"
#include "Util/UniqueTrackablePtr.h"

#include <map>
#include <climits>
Expand Down Expand Up @@ -516,7 +517,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 @@ -584,7 +585,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 @@ -70,7 +70,7 @@ GroupMemberStatus GetGroupMemberStatus(const Player* member = nullptr)
Group::Group() : m_Id(0), m_leaderLastOnline(0), m_groupFlags(GROUP_FLAG_NORMAL),
m_dungeonDifficulty(REGULAR_DIFFICULTY), m_raidDifficulty(REGULAR_DIFFICULTY),
m_bgGroup(nullptr), m_bfGroup(nullptr), m_lootMethod(FREE_FOR_ALL), m_lootThreshold(ITEM_QUALITY_UNCOMMON),
m_subGroupsCounts(nullptr)
m_subGroupsCounts(nullptr), m_scriptRef(this, NoopGroupDeleter())
{
}

Expand Down
Loading

0 comments on commit 219c347

Please sign in to comment.