TrinityCore
ObjectAccessor.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "ObjectAccessor.h"
19#include "Corpse.h"
20#include "Creature.h"
21#include "DynamicObject.h"
22#include "GameObject.h"
23#include "GridNotifiers.h"
24#include "Item.h"
25#include "Map.h"
26#include "ObjectMgr.h"
27#include "Pet.h"
28#include "Player.h"
29#include "Transport.h"
30#include <mutex>
31
32template<class T>
34{
35 static_assert(std::is_same<Player, T>::value,
36 "Only Player can be registered in global HashMapHolder");
37
38 std::unique_lock<std::shared_mutex> lock(*GetLock());
39
40 GetContainer()[o->GetGUID()] = o;
41}
42
43template<class T>
45{
46 std::unique_lock<std::shared_mutex> lock(*GetLock());
47
48 GetContainer().erase(o->GetGUID());
49}
50
51template<class T>
53{
54 std::shared_lock<std::shared_mutex> lock(*GetLock());
55
56 typename MapType::iterator itr = GetContainer().find(guid);
57 return (itr != GetContainer().end()) ? itr->second : nullptr;
58}
59
60template<class T>
62{
63 static MapType _objectMap;
64 return _objectMap;
65}
66
67template<class T>
68std::shared_mutex* HashMapHolder<T>::GetLock()
69{
70 static std::shared_mutex _lock;
71 return &_lock;
72}
73
75
77{
78 typedef std::unordered_map<std::string, Player*> MapType;
80
81 void Insert(Player* p)
82 {
83 PlayerNameMap[p->GetName()] = p;
84 }
85
86 void Remove(Player* p)
87 {
88 PlayerNameMap.erase(p->GetName());
89 }
90
91 Player* Find(std::string_view name)
92 {
93 std::string charName(name);
94 if (!normalizePlayerName(charName))
95 return nullptr;
96
97 auto itr = PlayerNameMap.find(charName);
98 return (itr != PlayerNameMap.end()) ? itr->second : nullptr;
99 }
100} // namespace PlayerNameMapHolder
101
103{
104 switch (guid.GetHigh())
105 {
106 case HighGuid::Player: return GetPlayer(p, guid);
108 case HighGuid::GameObject: return GetGameObject(p, guid);
110 case HighGuid::Creature: return GetCreature(p, guid);
111 case HighGuid::Pet: return GetPet(p, guid);
112 case HighGuid::DynamicObject: return GetDynamicObject(p, guid);
113 case HighGuid::AreaTrigger: return GetAreaTrigger(p, guid);
114 case HighGuid::Corpse: return GetCorpse(p, guid);
115 case HighGuid::SceneObject: return GetSceneObject(p, guid);
116 case HighGuid::Conversation: return GetConversation(p, guid);
117 default: return nullptr;
118 }
119}
120
122{
123 switch (guid.GetHigh())
124 {
125 case HighGuid::Item:
126 if (typemask & TYPEMASK_ITEM && p.GetTypeId() == TYPEID_PLAYER)
127 return ((Player const&)p).GetItemByGuid(guid);
128 break;
129 case HighGuid::Player:
130 if (typemask & TYPEMASK_PLAYER)
131 return GetPlayer(p, guid);
132 break;
135 if (typemask & TYPEMASK_GAMEOBJECT)
136 return GetGameObject(p, guid);
137 break;
140 if (typemask & TYPEMASK_UNIT)
141 return GetCreature(p, guid);
142 break;
143 case HighGuid::Pet:
144 if (typemask & TYPEMASK_UNIT)
145 return GetPet(p, guid);
146 break;
148 if (typemask & TYPEMASK_DYNAMICOBJECT)
149 return GetDynamicObject(p, guid);
150 break;
152 if (typemask & TYPEMASK_AREATRIGGER)
153 return GetAreaTrigger(p, guid);
154 break;
156 if (typemask & TYPEMASK_SCENEOBJECT)
157 return GetSceneObject(p, guid);
158 break;
160 if (typemask & TYPEMASK_CONVERSATION)
161 return GetConversation(p, guid);
162 break;
163 case HighGuid::Corpse:
164 break;
165 default:
166 break;
167 }
168
169 return nullptr;
170}
171
173{
174 return u.GetMap()->GetCorpse(guid);
175}
176
178{
179 return u.GetMap()->GetGameObject(guid);
180}
181
183{
184 return u.GetMap()->GetTransport(guid);
185}
186
188{
189 return u.GetMap()->GetDynamicObject(guid);
190}
191
193{
194 return u.GetMap()->GetAreaTrigger(guid);
195}
196
198{
199 return u.GetMap()->GetSceneObject(guid);
200}
201
203{
204 return u.GetMap()->GetConversation(guid);
205}
206
208{
209 if (guid.IsPlayer())
210 return GetPlayer(u, guid);
211
212 if (guid.IsPet())
213 return GetPet(u, guid);
214
215 return GetCreature(u, guid);
216}
217
219{
220 return u.GetMap()->GetCreature(guid);
221}
222
224{
225 return u.GetMap()->GetPet(guid);
226}
227
229{
230 if (Player* player = HashMapHolder<Player>::Find(guid))
231 if (player->IsInWorld() && player->GetMap() == m)
232 return player;
233
234 return nullptr;
235}
236
238{
239 return GetPlayer(u.GetMap(), guid);
240}
241
243{
244 if (guid.IsPet())
245 return GetPet(u, guid);
246
247 if (guid.IsCreatureOrVehicle())
248 return GetCreature(u, guid);
249
250 return nullptr;
251}
252
254{
255 Player* player = HashMapHolder<Player>::Find(guid);
256 return player && player->IsInWorld() ? player : nullptr;
257}
258
260{
261 Player* player = PlayerNameMapHolder::Find(name);
262 if (!player || !player->IsInWorld())
263 return nullptr;
264
265 return player;
266}
267
269{
270 ObjectGuid guid = ObjectGuid::Create<HighGuid::Player>(lowguid);
271 return ObjectAccessor::FindPlayer(guid);
272}
273
275{
276 return HashMapHolder<Player>::Find(guid);
277}
278
280{
281 return PlayerNameMapHolder::Find(name);
282}
283
285{
287}
288
290{
291 std::shared_lock<std::shared_mutex> lock(*HashMapHolder<Player>::GetLock());
292
294 for (HashMapHolder<Player>::MapType::const_iterator itr = m.begin(); itr != m.end(); ++itr)
295 itr->second->SaveToDB();
296}
297
298template<>
300{
303}
304
305template<>
307{
310}
#define TC_GAME_API
Definition: Define.h:123
uint32_t uint32
Definition: Define.h:142
@ TYPEID_PLAYER
Definition: ObjectGuid.h:43
@ TYPEMASK_ITEM
Definition: ObjectGuid.h:58
@ TYPEMASK_SCENEOBJECT
Definition: ObjectGuid.h:69
@ TYPEMASK_DYNAMICOBJECT
Definition: ObjectGuid.h:66
@ TYPEMASK_UNIT
Definition: ObjectGuid.h:62
@ TYPEMASK_CONVERSATION
Definition: ObjectGuid.h:70
@ TYPEMASK_GAMEOBJECT
Definition: ObjectGuid.h:65
@ TYPEMASK_PLAYER
Definition: ObjectGuid.h:63
@ TYPEMASK_AREATRIGGER
Definition: ObjectGuid.h:68
bool normalizePlayerName(std::string &name)
Definition: ObjectMgr.cpp:155
Definition: Corpse.h:53
static T * Find(ObjectGuid guid)
static std::shared_mutex * GetLock()
static void Remove(T *o)
std::unordered_map< ObjectGuid, T * > MapType
static MapType & GetContainer()
static void Insert(T *o)
Definition: Map.h:222
Pet * GetPet(ObjectGuid const &guid)
Definition: Map.cpp:3512
SceneObject * GetSceneObject(ObjectGuid const &guid)
Definition: Map.cpp:3477
GameObject * GetGameObject(ObjectGuid const &guid)
Definition: Map.cpp:3507
Corpse * GetCorpse(ObjectGuid const &guid)
Definition: Map.cpp:3492
DynamicObject * GetDynamicObject(ObjectGuid const &guid)
Definition: Map.cpp:3502
Conversation * GetConversation(ObjectGuid const &guid)
Definition: Map.cpp:3482
AreaTrigger * GetAreaTrigger(ObjectGuid const &guid)
Definition: Map.cpp:3472
Creature * GetCreature(ObjectGuid const &guid)
Definition: Map.cpp:3497
Transport * GetTransport(ObjectGuid const &guid)
Definition: Map.cpp:3517
bool IsPlayer() const
Definition: ObjectGuid.h:329
uint64 LowType
Definition: ObjectGuid.h:281
bool IsCreatureOrVehicle() const
Definition: ObjectGuid.h:327
HighGuid GetHigh() const
Definition: ObjectGuid.h:291
bool IsPet() const
Definition: ObjectGuid.h:324
Definition: Object.h:186
bool IsInWorld() const
Definition: Object.h:190
TypeID GetTypeId() const
Definition: Object.h:209
Definition: Pet.h:40
Definition: Unit.h:630
Map * GetMap() const
Definition: Object.h:749
std::string const & GetName() const
Definition: Object.h:680
TC_GAME_API WorldObject * GetWorldObject(WorldObject const &, ObjectGuid const &)
TC_GAME_API Player * FindPlayerByName(std::string_view name)
TC_GAME_API void SaveAllPlayers()
TC_GAME_API Player * FindPlayerByLowGUID(ObjectGuid::LowType lowguid)
TC_GAME_API Unit * GetUnit(WorldObject const &, ObjectGuid const &guid)
TC_GAME_API Player * FindConnectedPlayerByName(std::string_view name)
void AddObject(T *object)
TC_GAME_API GameObject * GetGameObject(WorldObject const &u, ObjectGuid const &guid)
TC_GAME_API Transport * GetTransport(WorldObject const &u, ObjectGuid const &guid)
TC_GAME_API Object * GetObjectByTypeMask(WorldObject const &, ObjectGuid const &, uint32 typemask)
TC_GAME_API AreaTrigger * GetAreaTrigger(WorldObject const &u, ObjectGuid const &guid)
TC_GAME_API Player * FindPlayer(ObjectGuid const &)
TC_GAME_API Player * GetPlayer(Map const *, ObjectGuid const &guid)
TC_GAME_API HashMapHolder< Player >::MapType const & GetPlayers()
TC_GAME_API Creature * GetCreature(WorldObject const &u, ObjectGuid const &guid)
TC_GAME_API DynamicObject * GetDynamicObject(WorldObject const &u, ObjectGuid const &guid)
TC_GAME_API Player * FindConnectedPlayer(ObjectGuid const &)
TC_GAME_API Pet * GetPet(WorldObject const &, ObjectGuid const &guid)
void RemoveObject(T *object)
TC_GAME_API Corpse * GetCorpse(WorldObject const &u, ObjectGuid const &guid)
TC_GAME_API Creature * GetCreatureOrPetOrVehicle(WorldObject const &, ObjectGuid const &)
TC_GAME_API Conversation * GetConversation(WorldObject const &u, ObjectGuid const &guid)
TC_GAME_API SceneObject * GetSceneObject(WorldObject const &u, ObjectGuid const &guid)
std::unordered_map< std::string, Player * > MapType
void Insert(Player *p)
void Remove(Player *p)
Player * Find(std::string_view name)
static MapType PlayerNameMap