TrinityCore
Loading...
Searching...
No Matches
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 "ObjectDefines.h"
27#include "ObjectMgr.h"
28#include "Pet.h"
29#include "Player.h"
30#include "Transport.h"
31#include "World.h"
32
33template<class T>
35{
36 static_assert(std::is_same<Player, T>::value
37 || std::is_same<Transport, T>::value,
38 "Only Player and Transport can be registered in global HashMapHolder");
39
40 std::unique_lock<std::shared_mutex> lock(*GetLock());
41
42 GetContainer()[o->GetGUID()] = o;
43}
44
45template<class T>
47{
48 std::unique_lock<std::shared_mutex> lock(*GetLock());
49
50 GetContainer().erase(o->GetGUID());
51}
52
53template<class T>
55{
56 std::shared_lock<std::shared_mutex> lock(*GetLock());
57
58 typename MapType::iterator itr = GetContainer().find(guid);
59 return (itr != GetContainer().end()) ? itr->second : nullptr;
60}
61
62template<class T>
64{
65 static MapType _objectMap;
66 return _objectMap;
67}
68
69template<class T>
70std::shared_mutex* HashMapHolder<T>::GetLock()
71{
72 static std::shared_mutex _lock;
73 return &_lock;
74}
75
80
83
85{
86 typedef std::unordered_map<std::string, Player*> MapType;
88
89 void Insert(Player* p)
90 {
91 PlayerNameMap[p->GetName()] = p;
92 }
93
94 void Remove(Player* p)
95 {
96 PlayerNameMap.erase(p->GetName());
97 }
98
99 Player* Find(std::string_view name)
100 {
101 std::string charName(name);
102 if (!normalizePlayerName(charName))
103 return nullptr;
104
105 auto itr = PlayerNameMap.find(charName);
106 return (itr != PlayerNameMap.end()) ? itr->second : nullptr;
107 }
108} // namespace PlayerNameMapHolder
109
111{
112 switch (guid.GetHigh())
113 {
114 case HighGuid::Player: return GetPlayer(p, guid);
117 case HighGuid::GameObject: return GetGameObject(p, guid);
119 case HighGuid::Unit: return GetCreature(p, guid);
120 case HighGuid::Pet: return GetPet(p, guid);
121 case HighGuid::DynamicObject: return GetDynamicObject(p, guid);
122 case HighGuid::Corpse: return GetCorpse(p, guid);
123 default: return nullptr;
124 }
125}
126
128{
129 switch (guid.GetHigh())
130 {
131 case HighGuid::Item:
132 if (typemask & TYPEMASK_ITEM && p.GetTypeId() == TYPEID_PLAYER)
133 return ((Player const&)p).GetItemByGuid(guid);
134 break;
135 case HighGuid::Player:
136 if (typemask & TYPEMASK_PLAYER)
137 return GetPlayer(p, guid);
138 break;
142 if (typemask & TYPEMASK_GAMEOBJECT)
143 return GetGameObject(p, guid);
144 break;
145 case HighGuid::Unit:
147 if (typemask & TYPEMASK_UNIT)
148 return GetCreature(p, guid);
149 break;
150 case HighGuid::Pet:
151 if (typemask & TYPEMASK_UNIT)
152 return GetPet(p, guid);
153 break;
155 if (typemask & TYPEMASK_DYNAMICOBJECT)
156 return GetDynamicObject(p, guid);
157 break;
158 case HighGuid::Corpse:
159 break;
160 default:
161 break;
162 }
163
164 return nullptr;
165}
166
168{
169 return u.GetMap()->GetCorpse(guid);
170}
171
173{
174 return u.GetMap()->GetGameObject(guid);
175}
176
178{
179 return u.GetMap()->GetTransport(guid);
180}
181
183{
184 return u.GetMap()->GetDynamicObject(guid);
185}
186
188{
189 if (guid.IsEmpty())
190 return nullptr;
191
192 if (guid.IsPlayer())
193 return GetPlayer(u, guid);
194
195 if (guid.IsPet())
196 return GetPet(u, guid);
197
198 return GetCreature(u, guid);
199}
200
202{
203 return u.GetMap()->GetCreature(guid);
204}
205
207{
208 return u.GetMap()->GetPet(guid);
209}
210
212{
213 if (Player* player = HashMapHolder<Player>::Find(guid))
214 if (player->IsInWorld() && player->GetMap() == m)
215 return player;
216
217 return nullptr;
218}
219
221{
222 return GetPlayer(u.GetMap(), guid);
223}
224
226{
227 if (guid.IsPet())
228 return GetPet(u, guid);
229
230 if (guid.IsCreatureOrVehicle())
231 return GetCreature(u, guid);
232
233 return nullptr;
234}
235
237{
238 Player* player = HashMapHolder<Player>::Find(guid);
239 return player && player->IsInWorld() ? player : nullptr;
240}
241
243{
244 Player* player = FindConnectedPlayerByName(name);
245 if (!player || !player->IsInWorld())
246 return nullptr;
247
248 return player;
249}
250
252{
253 ObjectGuid guid = ObjectGuid::Create<HighGuid::Player>(lowguid);
254 return ObjectAccessor::FindPlayer(guid);
255}
256
261
263{
264 return PlayerNameMapHolder::Find(name);
265}
266
268{
269 std::shared_lock<std::shared_mutex> lock(*HashMapHolder<Player>::GetLock());
270
272 for (HashMapHolder<Player>::MapType::const_iterator itr = m.begin(); itr != m.end(); ++itr)
273 itr->second->SaveToDB();
274}
275
276template<>
282
283template<>
#define TC_GAME_API
Definition Define.h:114
uint32_t uint32
Definition Define.h:133
@ TYPEID_PLAYER
Definition ObjectGuid.h:39
@ TYPEMASK_ITEM
Definition ObjectGuid.h:50
@ TYPEMASK_DYNAMICOBJECT
Definition ObjectGuid.h:55
@ TYPEMASK_UNIT
Definition ObjectGuid.h:52
@ TYPEMASK_GAMEOBJECT
Definition ObjectGuid.h:54
@ TYPEMASK_PLAYER
Definition ObjectGuid.h:53
bool normalizePlayerName(std::string &name)
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:281
Pet * GetPet(ObjectGuid const &guid)
Definition Map.cpp:4435
GameObject * GetGameObject(ObjectGuid const &guid)
Definition Map.cpp:4430
Corpse * GetCorpse(ObjectGuid const &guid)
Definition Map.cpp:4392
DynamicObject * GetDynamicObject(ObjectGuid const &guid)
Definition Map.cpp:4449
Creature * GetCreature(ObjectGuid const &guid)
Definition Map.cpp:4397
Transport * GetTransport(ObjectGuid const &guid)
Definition Map.cpp:4440
bool IsEmpty() const
Definition ObjectGuid.h:172
bool IsPlayer() const
Definition ObjectGuid.h:179
uint32 LowType
Definition ObjectGuid.h:142
bool IsCreatureOrVehicle() const
Definition ObjectGuid.h:177
HighGuid GetHigh() const
Definition ObjectGuid.h:154
bool IsPet() const
Definition ObjectGuid.h:174
bool IsInWorld() const
Definition Object.h:73
TypeID GetTypeId() const
Definition Object.h:93
Definition Pet.h:40
Definition Unit.h:769
Map * GetMap() const
Definition Object.h:449
std::string const & GetName() const
Definition Object.h:382
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 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 &)
std::unordered_map< std::string, Player * > MapType
void Insert(Player *p)
void Remove(Player *p)
Player * Find(std::string_view name)
static MapType PlayerNameMap