** Cleanup in ObjectAccessor/HashMapHolder:

* Correctly lock the HashMapHolder on access.
* Use ObjectAccessor wrappers (GetPlayers, GetCreatures, GetGameObjects) instead of raw HashMapHolder<Type>::GetContainer.
* General code style cleanup.

--HG--
branch : trunk
This commit is contained in:
XTZGZoReX committed 2010-03-13 16:17:25 +01:00
1 parent 76afec204d
commit f8c5b9a4a7
6 files changed
+154 -179

No files matched your search

+92 -124
View File
@@ -45,7 +45,10 @@
INSTANTIATE_SINGLETON_2(ObjectAccessor, CLASS_LOCK);
INSTANTIATE_CLASS_MUTEX(ObjectAccessor, ACE_Thread_Mutex);
ObjectAccessor::ObjectAccessor() {}
ObjectAccessor::ObjectAccessor()
{
}
ObjectAccessor::~ObjectAccessor()
{
for (Player2CorpsesMapType::const_iterator itr = i_player2corpse.begin(); itr != i_player2corpse.end(); ++itr)
@@ -55,71 +58,57 @@ ObjectAccessor::~ObjectAccessor()
}
}
Creature*
ObjectAccessor::GetCreatureOrPetOrVehicle(WorldObject const &u, uint64 guid)
Creature* ObjectAccessor::GetCreatureOrPetOrVehicle(WorldObject const& u, uint64 guid)
{
if(IS_PLAYER_GUID(guid))
if (IS_PLAYER_GUID(guid))
return NULL;
if(IS_PET_GUID(guid))
if (IS_PET_GUID(guid))
return GetPet(guid);
return u.IsInWorld() ? u.GetMap()->GetCreature(guid) : NULL;
}
/*
Unit*
ObjectAccessor::GetUnit(WorldObject const &u, uint64 guid)
Corpse* ObjectAccessor::GetCorpse(WorldObject const& u, uint64 guid)
{
if(!guid)
Corpse* ret = GetObjectInWorld(guid, (Corpse*)NULL);
if (!ret)
return NULL;
if(IS_PLAYER_GUID(guid))
return FindPlayer(guid);
if (ret->GetMapId() != u.GetMapId())
return NULL;
return GetCreatureOrPetOrVehicle(u, guid);
}
*/
if (ret->GetInstanceId() != u.GetInstanceId())
return NULL;
Corpse*
ObjectAccessor::GetCorpse(WorldObject const &u, uint64 guid)
{
Corpse * ret = GetObjectInWorld(guid, (Corpse*)NULL);
if(!ret)
return NULL;
if(ret->GetMapId() != u.GetMapId())
return NULL;
if(ret->GetInstanceId() != u.GetInstanceId())
return NULL;
return ret;
}
WorldObject* ObjectAccessor::GetWorldObject(WorldObject const &p, uint64 guid)
WorldObject* ObjectAccessor::GetWorldObject(WorldObject const& p, uint64 guid)
{
switch(GUID_HIPART(guid))
switch (GUID_HIPART(guid))
{
case HIGHGUID_PLAYER: return FindPlayer(guid);
case HIGHGUID_GAMEOBJECT: return p.GetMap()->GetGameObject(guid);
case HIGHGUID_PLAYER: return FindPlayer(guid);
case HIGHGUID_GAMEOBJECT: return p.GetMap()->GetGameObject(guid);
case HIGHGUID_VEHICLE:
case HIGHGUID_UNIT: return p.GetMap()->GetCreature(guid);
case HIGHGUID_PET: return GetPet(guid);
case HIGHGUID_DYNAMICOBJECT:return p.GetMap()->GetDynamicObject(guid);
case HIGHGUID_TRANSPORT: return NULL;
case HIGHGUID_CORPSE: return GetCorpse(p,guid);
case HIGHGUID_MO_TRANSPORT: return NULL;
default: break;
case HIGHGUID_UNIT: return p.GetMap()->GetCreature(guid);
case HIGHGUID_PET: return GetPet(guid);
case HIGHGUID_DYNAMICOBJECT: return p.GetMap()->GetDynamicObject(guid);
case HIGHGUID_TRANSPORT: return NULL;
case HIGHGUID_CORPSE: return GetCorpse(p,guid);
case HIGHGUID_MO_TRANSPORT: return NULL;
default: return NULL;
}
return NULL;
}
Object* ObjectAccessor::GetObjectByTypeMask(WorldObject const &p, uint64 guid, uint32 typemask)
Object* ObjectAccessor::GetObjectByTypeMask(WorldObject const& p, uint64 guid, uint32 typemask)
{
switch(GUID_HIPART(guid))
switch (GUID_HIPART(guid))
{
case HIGHGUID_ITEM:
if(typemask & TYPEMASK_ITEM && p.GetTypeId() == TYPEID_PLAYER)
return ((Player const &)p).GetItemByGuid( guid );
if (typemask & TYPEMASK_ITEM && p.GetTypeId() == TYPEID_PLAYER)
return ((Player const&)p).GetItemByGuid(guid);
break;
case HIGHGUID_PLAYER:
if(typemask & TYPEMASK_PLAYER)
@@ -151,135 +140,133 @@ Object* ObjectAccessor::GetObjectByTypeMask(WorldObject const &p, uint64 guid, u
return NULL;
}
Player*
ObjectAccessor::FindPlayer(uint64 guid)
Player* ObjectAccessor::FindPlayer(uint64 guid)
{
Player * plr = GetObjectInWorld(guid, (Player*)NULL);
if(!plr || !plr->IsInWorld())
Player* plr = GetObjectInWorld(guid, (Player*)NULL);
if (!plr || !plr->IsInWorld())
return NULL;
return plr;
}
Player*
ObjectAccessor::FindPlayerByName(const char *name)
Player* ObjectAccessor::FindPlayerByName(const char* name)
{
//TODO: Player Guard
Guard guard(*HashMapHolder<Player>::GetLock());
HashMapHolder<Player>::MapType& m = HashMapHolder<Player>::GetContainer();
HashMapHolder<Player>::MapType::iterator iter = m.begin();
for (; iter != m.end(); ++iter)
if(iter->second->IsInWorld() && ( ::strcmp(name, iter->second->GetName()) == 0 ))
for (HashMapHolder<Player>::MapType::iterator iter = m.begin(); iter != m.end(); ++iter)
if (iter->second->IsInWorld() && strcmp(name, iter->second->GetName()) == 0)
return iter->second;
return NULL;
}
void
ObjectAccessor::SaveAllPlayers()
void ObjectAccessor::SaveAllPlayers()
{
Guard guard(*HashMapHolder<Player>::GetLock());
HashMapHolder<Player>::MapType& m = HashMapHolder<Player>::GetContainer();
HashMapHolder<Player>::MapType::iterator itr = m.begin();
for (; itr != m.end(); ++itr)
for (HashMapHolder<Player>::MapType::iterator itr = m.begin(); itr != m.end(); ++itr)
itr->second->SaveToDB();
}
Pet*
ObjectAccessor::GetPet(uint64 guid)
Pet* ObjectAccessor::GetPet(uint64 guid)
{
return GetObjectInWorld(guid, (Pet*)NULL);
}
Corpse*
ObjectAccessor::GetCorpseForPlayerGUID(uint64 guid)
Corpse* ObjectAccessor::GetCorpseForPlayerGUID(uint64 guid)
{
Guard guard(i_corpseGuard);
Player2CorpsesMapType::iterator iter = i_player2corpse.find(guid);
if( iter == i_player2corpse.end() ) return NULL;
if (iter == i_player2corpse.end())
return NULL;
assert(iter->second->GetType() != CORPSE_BONES);
return iter->second;
}
void
ObjectAccessor::RemoveCorpse(Corpse *corpse)
void ObjectAccessor::RemoveCorpse(Corpse* corpse)
{
assert(corpse && corpse->GetType() != CORPSE_BONES);
if(corpse->FindMap())
if (corpse->FindMap())
corpse->FindMap()->Remove(corpse, false);
else
corpse->RemoveFromWorld();
Guard guard(i_corpseGuard);
Player2CorpsesMapType::iterator iter = i_player2corpse.find(corpse->GetOwnerGUID());
if( iter == i_player2corpse.end() ) // i do not know when it happens but it happens
return;
// Critical section
{
Guard guard(i_corpseGuard);
// build mapid*cellid -> guid_set map
CellPair cell_pair = Trinity::ComputeCellPair(corpse->GetPositionX(), corpse->GetPositionY());
uint32 cell_id = (cell_pair.y_coord*TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
Player2CorpsesMapType::iterator iter = i_player2corpse.find(corpse->GetOwnerGUID());
if (iter == i_player2corpse.end()) // TODO: Fix this
return;
objmgr.DeleteCorpseCellData(corpse->GetMapId(), cell_id, corpse->GetOwnerGUID());
// build mapid*cellid -> guid_set map
CellPair cell_pair = Trinity::ComputeCellPair(corpse->GetPositionX(), corpse->GetPositionY());
uint32 cell_id = (cell_pair.y_coord * TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
i_player2corpse.erase(iter);
objmgr.DeleteCorpseCellData(corpse->GetMapId(), cell_id, corpse->GetOwnerGUID());
i_player2corpse.erase(iter);
}
}
void
ObjectAccessor::AddCorpse(Corpse *corpse)
void ObjectAccessor::AddCorpse(Corpse* corpse)
{
assert(corpse && corpse->GetType() != CORPSE_BONES);
Guard guard(i_corpseGuard);
assert(i_player2corpse.find(corpse->GetOwnerGUID()) == i_player2corpse.end());
i_player2corpse[corpse->GetOwnerGUID()] = corpse;
// Critical section
{
Guard guard(i_corpseGuard);
// build mapid*cellid -> guid_set map
CellPair cell_pair = Trinity::ComputeCellPair(corpse->GetPositionX(), corpse->GetPositionY());
uint32 cell_id = (cell_pair.y_coord*TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
assert(i_player2corpse.find(corpse->GetOwnerGUID()) == i_player2corpse.end());
i_player2corpse[corpse->GetOwnerGUID()] = corpse;
objmgr.AddCorpseCellData(corpse->GetMapId(), cell_id, corpse->GetOwnerGUID(), corpse->GetInstanceId());
// build mapid*cellid -> guid_set map
CellPair cell_pair = Trinity::ComputeCellPair(corpse->GetPositionX(), corpse->GetPositionY());
uint32 cell_id = (cell_pair.y_coord * TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
objmgr.AddCorpseCellData(corpse->GetMapId(), cell_id, corpse->GetOwnerGUID(), corpse->GetInstanceId());
}
}
void
ObjectAccessor::AddCorpsesToGrid(GridPair const& gridpair,GridType& grid,Map* map)
void ObjectAccessor::AddCorpsesToGrid(GridPair const& gridpair, GridType& grid, Map* map)
{
Guard guard(i_corpseGuard);
for (Player2CorpsesMapType::iterator iter = i_player2corpse.begin(); iter != i_player2corpse.end(); ++iter)
if(iter->second->GetGrid() == gridpair)
{
// verify, if the corpse in our instance (add only corpses which are)
if (map->Instanceable())
if (iter->second->GetGrid() == gridpair)
{
if (iter->second->GetInstanceId() == map->GetInstanceId())
// verify, if the corpse in our instance (add only corpses which are)
if (map->Instanceable())
{
grid.AddWorldObject(iter->second);
if (iter->second->GetInstanceId() == map->GetInstanceId())
grid.AddWorldObject(iter->second);
}
}
else
{
grid.AddWorldObject(iter->second);
else
grid.AddWorldObject(iter->second);
}
}
}
Corpse*
ObjectAccessor::ConvertCorpseForPlayer(uint64 player_guid, bool insignia)
Corpse* ObjectAccessor::ConvertCorpseForPlayer(uint64 player_guid, bool insignia)
{
Corpse *corpse = GetCorpseForPlayerGUID(player_guid);
Corpse* corpse = GetCorpseForPlayerGUID(player_guid);
if(!corpse)
{
//in fact this function is called from several places
//even when player doesn't have a corpse, not an error
// TODO: really, now...
//sLog.outError("Try remove corpse that not in map for GUID %ul", player_guid);
return NULL;
}
DEBUG_LOG("Deleting Corpse and spawning bones.");
Map *map = corpse->FindMap();
Map* map = corpse->FindMap();
// remove corpse from player_guid -> corpse map
RemoveCorpse(corpse);
@@ -298,7 +285,7 @@ ObjectAccessor::ConvertCorpseForPlayer(uint64 player_guid, bool insignia)
delete corpse;
return NULL;
Corpse *bones = NULL;
Corpse* bones = NULL;
// create the bones only if the map and the grid is loaded at the corpse's location
// ignore bones creating option in case insignia
if (map && (insignia ||
@@ -314,7 +301,7 @@ ObjectAccessor::ConvertCorpseForPlayer(uint64 player_guid, bool insignia)
bones->SetGrid(corpse->GetGrid());
// bones->m_time = m_time; // don't overwrite time
// bones->m_inWorld = m_inWorld; // don't overwrite world state
// bones->m_inWorld = m_inWorld; // don't overwrite in-world state
// bones->m_type = m_type; // don't overwrite type
bones->Relocate(corpse->GetPositionX(), corpse->GetPositionY(), corpse->GetPositionZ(), corpse->GetOrientation());
bones->SetPhaseMask(corpse->GetPhaseMask(), false);
@@ -324,7 +311,7 @@ ObjectAccessor::ConvertCorpseForPlayer(uint64 player_guid, bool insignia)
for (int i = 0; i < EQUIPMENT_SLOT_END; ++i)
{
if(corpse->GetUInt32Value(CORPSE_FIELD_ITEM + i))
if (corpse->GetUInt32Value(CORPSE_FIELD_ITEM + i))
bones->SetUInt32Value(CORPSE_FIELD_ITEM + i, 0);
}
@@ -338,13 +325,15 @@ ObjectAccessor::ConvertCorpseForPlayer(uint64 player_guid, bool insignia)
return bones;
}
void
ObjectAccessor::Update(uint32 diff)
void ObjectAccessor::Update(uint32 diff)
{
UpdateDataMapType update_players;
// Critical section
{
Guard guard(i_updateGuard);
while(!i_objects.empty())
while (!i_objects.empty())
{
Object* obj = *i_objects.begin();
assert(obj && obj->IsInWorld());
@@ -362,28 +351,7 @@ ObjectAccessor::Update(uint32 diff)
}
}
/*void ObjectAccessor::UpdateVisibilityForPlayer( Player* player )
{
WorldObject const* viewPoint = player->GetViewPoint();
Map* m = player->GetMap();
CellPair p = Trinity::ComputeCellPair(player->GetPositionX(), player->GetPositionY());
Cell cell(p);
m->UpdatePlayerVisibility(player, cell, p);
if (player!=viewPoint)
{
CellPair pView(Trinity::ComputeCellPair(viewPoint->GetPositionX(), viewPoint->GetPositionY()));
Cell cellView(pView);
m->UpdateObjectsVisibilityFor(player, cellView, pView);
}
else
m->UpdateObjectsVisibilityFor(player, cell, p);
}*/
/// Define the static member of HashMapHolder
/// Define the static members of HashMapHolder
template <class T> UNORDERED_MAP< uint64, T* > HashMapHolder<T>::m_objectMap;
template <class T> ACE_Thread_Mutex HashMapHolder<T>::i_lock;