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
31template<class T>
33{
34 static_assert(std::is_same<Player, T>::value,
35 "Only Player can be registered in global HashMapHolder");
36
37 std::unique_lock<std::shared_mutex> lock(*GetLock());
38
39 GetContainer()[o->GetGUID()] = o;
40}
41
42template<class T>
44{
45 std::unique_lock<std::shared_mutex> lock(*GetLock());
46
47 GetContainer().erase(o->GetGUID());
48}
49
50template<class T>
52{
53 std::shared_lock<std::shared_mutex> lock(*GetLock());
54
55 typename MapType::iterator itr = GetContainer().find(guid);
56 return (itr != GetContainer().end()) ? itr->second : nullptr;
57}
58
59template<class T>
61{
62 static MapType _objectMap;
63 return _objectMap;
64}
65
66template<class T>
67std::shared_mutex* HashMapHolder<T>::GetLock()
68{
69 static std::shared_mutex _lock;
70 return &_lock;
71}
72
74
76{
77 typedef std::unordered_map<std::string, Player*> MapType;
79
80 void Insert(Player* p)
81 {
82 PlayerNameMap[p->GetName()] = p;
83 }
84
85 void Remove(Player* p)
86 {
87 PlayerNameMap.erase(p->GetName());
88 }
89
90 Player* Find(std::string_view name)
91 {
92 std::string charName(name);
93 if (!normalizePlayerName(charName))
94 return nullptr;
95
96 auto itr = PlayerNameMap.find(charName);
97 return (itr != PlayerNameMap.end()) ? itr->second : nullptr;
98 }
99} // namespace PlayerNameMapHolder
100
102{
103 switch (guid.GetHigh())
104 {
105 case HighGuid::Player: return GetPlayer(p, guid);
107 case HighGuid::GameObject: return GetGameObject(p, guid);
109 case HighGuid::Creature: return GetCreature(p, guid);
110 case HighGuid::Pet: return GetPet(p, guid);
111 case HighGuid::DynamicObject: return GetDynamicObject(p, guid);
112 case HighGuid::AreaTrigger: return GetAreaTrigger(p, guid);
113 case HighGuid::Corpse: return GetCorpse(p, guid);
114 case HighGuid::SceneObject: return GetSceneObject(p, guid);
115 case HighGuid::Conversation: return GetConversation(p, guid);
116 default: return nullptr;
117 }
118}
119
121{
122 switch (guid.GetHigh())
123 {
124 case HighGuid::Item:
125 if (typemask & TYPEMASK_ITEM && p.GetTypeId() == TYPEID_PLAYER)
126 return ((Player const&)p).GetItemByGuid(guid);
127 break;
128 case HighGuid::Player:
129 if (typemask & TYPEMASK_PLAYER)
130 return GetPlayer(p, guid);
131 break;
134 if (typemask & TYPEMASK_GAMEOBJECT)
135 return GetGameObject(p, guid);
136 break;
139 if (typemask & TYPEMASK_UNIT)
140 return GetCreature(p, guid);
141 break;
142 case HighGuid::Pet:
143 if (typemask & TYPEMASK_UNIT)
144 return GetPet(p, guid);
145 break;
147 if (typemask & TYPEMASK_DYNAMICOBJECT)
148 return GetDynamicObject(p, guid);
149 break;
151 if (typemask & TYPEMASK_AREATRIGGER)
152 return GetAreaTrigger(p, guid);
153 break;
155 if (typemask & TYPEMASK_SCENEOBJECT)
156 return GetSceneObject(p, guid);
157 break;
159 if (typemask & TYPEMASK_CONVERSATION)
160 return GetConversation(p, guid);
161 break;
162 case HighGuid::Corpse:
163 break;
164 default:
165 break;
166 }
167
168 return nullptr;
169}
170
172{
173 return u.GetMap()->GetCorpse(guid);
174}
175
177{
178 return u.GetMap()->GetGameObject(guid);
179}
180
182{
183 return u.GetMap()->GetTransport(guid);
184}
185
187{
188 return u.GetMap()->GetDynamicObject(guid);
189}
190
192{
193 return u.GetMap()->GetAreaTrigger(guid);
194}
195
197{
198 return u.GetMap()->GetSceneObject(guid);
199}
200
202{
203 return u.GetMap()->GetConversation(guid);
204}
205
207{
208 if (guid.IsPlayer())
209 return GetPlayer(u, guid);
210
211 if (guid.IsPet())
212 return GetPet(u, guid);
213
214 return GetCreature(u, guid);
215}
216
218{
219 return u.GetMap()->GetCreature(guid);
220}
221
223{
224 return u.GetMap()->GetPet(guid);
225}
226
228{
229 if (Player* player = HashMapHolder<Player>::Find(guid))
230 if (player->IsInWorld() && player->GetMap() == m)
231 return player;
232
233 return nullptr;
234}
235
237{
238 return GetPlayer(u.GetMap(), guid);
239}
240
242{
243 if (guid.IsPet())
244 return GetPet(u, guid);
245
246 if (guid.IsCreatureOrVehicle())
247 return GetCreature(u, guid);
248
249 return nullptr;
250}
251
253{
254 Player* player = HashMapHolder<Player>::Find(guid);
255 return player && player->IsInWorld() ? player : nullptr;
256}
257
259{
260 Player* player = PlayerNameMapHolder::Find(name);
261 if (!player || !player->IsInWorld())
262 return nullptr;
263
264 return player;
265}
266
268{
269 ObjectGuid guid = ObjectGuid::Create<HighGuid::Player>(lowguid);
270 return ObjectAccessor::FindPlayer(guid);
271}
272
274{
275 return HashMapHolder<Player>::Find(guid);
276}
277
279{
280 return PlayerNameMapHolder::Find(name);
281}
282
284{
286}
287
289{
290 std::shared_lock<std::shared_mutex> lock(*HashMapHolder<Player>::GetLock());
291
293 for (HashMapHolder<Player>::MapType::const_iterator itr = m.begin(); itr != m.end(); ++itr)
294 itr->second->SaveToDB();
295}
296
297template<>
299{
302}
303
304template<>
306{
309}
#define TC_GAME_API
Definition: Define.h:123
uint32_t uint32
Definition: Define.h:142
@ TYPEID_PLAYER
Definition: ObjectGuid.h:41
@ TYPEMASK_ITEM
Definition: ObjectGuid.h:56
@ TYPEMASK_SCENEOBJECT
Definition: ObjectGuid.h:67
@ TYPEMASK_DYNAMICOBJECT
Definition: ObjectGuid.h:64
@ TYPEMASK_UNIT
Definition: ObjectGuid.h:60
@ TYPEMASK_CONVERSATION
Definition: ObjectGuid.h:68
@ TYPEMASK_GAMEOBJECT
Definition: ObjectGuid.h:63
@ TYPEMASK_PLAYER
Definition: ObjectGuid.h:61
@ TYPEMASK_AREATRIGGER
Definition: ObjectGuid.h:66
bool normalizePlayerName(std::string &name)
Definition: ObjectMgr.cpp:154
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:189
Pet * GetPet(ObjectGuid const &guid)
Definition: Map.cpp:3494
SceneObject * GetSceneObject(ObjectGuid const &guid)
Definition: Map.cpp:3459
GameObject * GetGameObject(ObjectGuid const &guid)
Definition: Map.cpp:3489
Corpse * GetCorpse(ObjectGuid const &guid)
Definition: Map.cpp:3474
DynamicObject * GetDynamicObject(ObjectGuid const &guid)
Definition: Map.cpp:3484
Conversation * GetConversation(ObjectGuid const &guid)
Definition: Map.cpp:3464
AreaTrigger * GetAreaTrigger(ObjectGuid const &guid)
Definition: Map.cpp:3454
Creature * GetCreature(ObjectGuid const &guid)
Definition: Map.cpp:3479
Transport * GetTransport(ObjectGuid const &guid)
Definition: Map.cpp:3499
bool IsPlayer() const
Definition: ObjectGuid.h:326
uint64 LowType
Definition: ObjectGuid.h:278
bool IsCreatureOrVehicle() const
Definition: ObjectGuid.h:324
HighGuid GetHigh() const
Definition: ObjectGuid.h:288
bool IsPet() const
Definition: ObjectGuid.h:321
Definition: Object.h:150
bool IsInWorld() const
Definition: Object.h:154
TypeID GetTypeId() const
Definition: Object.h:173
Definition: Pet.h:40
Definition: Unit.h:627
Map * GetMap() const
Definition: Object.h:624
std::string const & GetName() const
Definition: Object.h:555
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