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]>
  • Loading branch information
Niam5 and Shauren committed Aug 18, 2024
1 parent 12f9c69 commit 0028ca1
Show file tree
Hide file tree
Showing 30 changed files with 183 additions and 122 deletions.
4 changes: 0 additions & 4 deletions src/game/BattleGround/BattleGround.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,6 @@ BattleGround::BattleGround(): m_BuffChange(false), m_ArenaBuffSpawned(false), m_

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
7 changes: 6 additions & 1 deletion src/game/BattleGround/BattleGround.h
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ class BattleGround

/* Map pointers */
void SetBgMap(BattleGroundMap* map) { m_Map = map; }
BattleGroundMap* GetBgMap()
BattleGroundMap* GetBgMap() const
{
MANGOS_ASSERT(m_Map);
return m_Map;
Expand Down Expand Up @@ -560,6 +560,9 @@ class BattleGround
// door-events are automaticly added - but _ALL_ other must be in this vector
std::map<uint8, uint8> m_ActiveEvents;

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 @@ -641,6 +644,8 @@ class BattleGround
float m_TeamStartLocZ[PVP_TEAM_COUNT];
float m_TeamStartLocO[PVP_TEAM_COUNT];
float m_startMaxDist;

MaNGOS::unique_weak_ptr<BattleGround> m_weakRef;
};

// helper functions for world state list fill
Expand Down
17 changes: 12 additions & 5 deletions src/game/BattleGround/BattleGroundMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,7 @@ void BattleGroundMgr::DeleteAllBattleGrounds()
{
for (BattleGroundSet::iterator itr = m_BattleGrounds[i].begin(); itr != m_BattleGrounds[i].end();)
{
BattleGround* bg = itr->second;
BattleGround* bg = itr->second.get();
++itr; // step from invalidate iterator pos in result element remove in ~BattleGround call
delete bg;
}
Expand Down Expand Up @@ -1672,7 +1672,7 @@ BattleGround* BattleGroundMgr::GetBattleGroundThroughClientInstance(uint32 insta
for (BattleGroundSet::iterator itr = m_BattleGrounds[bgTypeId].begin(); itr != m_BattleGrounds[bgTypeId].end(); ++itr)
{
if (itr->second->GetClientInstanceID() == instanceId)
return itr->second;
return itr->second.get();
}
return nullptr;
}
Expand All @@ -1687,18 +1687,18 @@ 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);
}

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();
}

uint32 BattleGroundMgr::CreateClientVisibleInstanceId(BattleGroundTypeId bgTypeId, BattleGroundBracketId bracket_id)
Expand Down Expand Up @@ -1858,6 +1858,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);
}

void BattleGroundMgr::CreateInitialBattleGrounds()
{
uint32 count = 0;
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 @@ -209,7 +210,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);

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 bracket_id);
void DeleteClientVisibleInstanceId(BattleGroundTypeId bgTypeId, BattleGroundBracketId bracket_id, uint32 clientInstanceID)
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 @@ -3646,7 +3646,7 @@ bool ChatHandler::HandleLookupQuestCommand(char* args)
ObjectMgr::QuestMap const& qTemplates = sObjectMgr.GetQuestTemplates();
for (ObjectMgr::QuestMap::const_iterator iter = qTemplates.begin(); iter != qTemplates.end(); ++iter)
{
Quest* qinfo = iter->second;
Quest* qinfo = iter->second.get();

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

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

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

targetGuild->Disband();
delete targetGuild;

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

Object::Object(): m_updateFlag(0)
Object::Object(): m_updateFlag(0), m_scriptRef(this, NoopObjectDeleter())
{
m_objectTypeId = TYPEID_OBJECT;
m_objectType = TYPEMASK_OBJECT;
Expand All @@ -57,7 +57,7 @@ Object::Object(): m_updateFlag(0)

m_inWorld = false;
m_objectUpdated = false;
loot = nullptr;
loot = nullptr;
}

Object::~Object()
Expand All @@ -80,6 +80,30 @@ Object::~Object()
delete 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 @@ -27,6 +27,7 @@
#include "Entities/EntitiesMgr.h"
#include "Globals/SharedDefines.h"
#include "Camera.h"
#include "Util/UniqueTrackablePtr.h"
#include "Util/Util.h"
#ifdef BUILD_ELUNA
#include "LuaEngine/LuaValue.h"
Expand Down Expand Up @@ -315,22 +316,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 @@ -566,6 +553,8 @@ class Object

Loot* loot;

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

protected:
Object();

Expand Down Expand Up @@ -607,6 +596,9 @@ class Object
Object(const Object&); // prevent generation copy constructor
Object& operator=(Object const&); // prevent generation assigment operator

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 @@ -4496,16 +4496,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 @@ -641,7 +641,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 @@ -5941,6 +5940,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 @@ -12943,19 +12943,18 @@ void Unit::OnRelocated()
*/
void Unit::SetVehicleId(uint32 entry, uint32 overwriteNpcEntry)
{
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 @@ -1644,7 +1644,8 @@ class Unit : public WorldObject
void Unmount(bool from_aura = false);
MountCapabilityEntry const* GetMountCapability(uint32 mountType) const;

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 @@ -2540,7 +2541,7 @@ class Unit : public WorldObject
bool m_canParry;
bool m_canBlock;

VehicleInfo* m_vehicleInfo;
MaNGOS::unique_trackable_ptr<VehicleInfo> m_vehicleInfo;
void DisableSpline();
bool m_isCreatureLinkingTrigger;
bool m_isSpawningLinked;
Expand Down
6 changes: 6 additions & 0 deletions src/game/Entities/Vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -624,4 +624,10 @@ void VehicleInfo::RemoveSeatMods(Unit* passenger, uint32 seatFlags)
}
}

MaNGOS::unique_weak_ptr<VehicleInfo> VehicleInfo::GetWeakPtr() const
{
Unit* pVehicle = (Unit*)m_owner;
return pVehicle->GetVehicleInfoWeakPtr();
}

/*! @} */
2 changes: 2 additions & 0 deletions src/game/Entities/Vehicle.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class VehicleInfo : public TransportBase

void RemoveAccessoriesFromMap(); ///< Unsummones accessory in case of far-teleport or death

MaNGOS::unique_weak_ptr<VehicleInfo> GetWeakPtr() const;

private:
// Internal use to calculate the boarding position
void CalculateBoardingPositionOf(float gx, float gy, float gz, float go, float& lx, float& ly, float& lz, float& lo) const;
Expand Down
11 changes: 3 additions & 8 deletions src/game/Globals/ObjectMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,6 @@ ObjectMgr::ObjectMgr() :

ObjectMgr::~ObjectMgr()
{
for (QuestMap::iterator i = mQuestTemplates.begin(); i != mQuestTemplates.end(); ++i)
delete i->second;

for (PetLevelInfoMap::iterator i = petInfo.begin(); i != petInfo.end(); ++i)
delete[] i->second;

Expand Down Expand Up @@ -3734,9 +3731,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 @@ -3813,7 +3807,8 @@ void ObjectMgr::LoadQuests()
Field* fields = result->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 (result->NextRow());

Expand All @@ -3825,7 +3820,7 @@ void ObjectMgr::LoadQuests()

for (QuestMap::iterator iter = mQuestTemplates.begin(); iter != mQuestTemplates.end(); ++iter)
{
Quest* qinfo = iter->second;
Quest* qinfo = iter->second.get();

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

Expand Down
Loading

0 comments on commit 0028ca1

Please sign in to comment.