TrinityCore
ObjectGuid.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 "ObjectGuid.h"
19#include "ByteBuffer.h"
20#include "Errors.h"
21#include "Hash.h"
22#include "Log.h"
23#include "Realm.h"
24#include "Util.h"
25#include "World.h"
26
27static_assert(sizeof(ObjectGuid) == sizeof(uint64) * 2, "ObjectGuid must be exactly 16 bytes");
28
29namespace
30{
31 struct ObjectGuidInfo
32 {
33 std::string Names[AsUnderlyingType(HighGuid::Count)];
34 std::string(ObjectGuidInfo::*ClientFormatFunction[AsUnderlyingType(HighGuid::Count)])(char const* typeName, ObjectGuid guid);
35 ObjectGuid(ObjectGuidInfo::*ClientParseFunction[AsUnderlyingType(HighGuid::Count)])(HighGuid type, char const* guidString);
36
37 std::string Format(ObjectGuid guid)
38 {
39 if (guid.GetHigh() >= HighGuid::Count)
40 return "Uniq-WOWGUID_TO_STRING_FAILED";
41
42 int32 type = AsUnderlyingType(guid.GetHigh());
43 if (!ClientFormatFunction[type])
44 return "Uniq-WOWGUID_TO_STRING_FAILED";
45
46 return (this->*ClientFormatFunction[type])(Names[type].c_str(), guid);
47 }
48
49 ObjectGuid Parse(std::string const& guidString)
50 {
51 std::size_t typeEnd = guidString.find('-');
52 if (typeEnd == std::string::npos)
54
55 int32 type = 0;
56 for (; type < AsUnderlyingType(HighGuid::Count); ++type)
57 {
58 if (Names[type].length() < typeEnd)
59 continue;
60
61 if (guidString.compare(0, typeEnd, Names[type]) == 0)
62 break;
63 }
64
67
68 return (this->*ClientParseFunction[type])(HighGuid(type), &guidString[typeEnd + 1]);
69 }
70
71 std::string FormatNull(char const*, ObjectGuid)
72 {
73 return "0000000000000000";
74 }
75
76 ObjectGuid ParseNull(HighGuid, char const*)
77 {
78 return ObjectGuid::Empty;
79 }
80
81 std::string FormatUniq(char const* typeName, ObjectGuid guid)
82 {
83 constexpr char const* uniqNames[] =
84 {
85 nullptr,
86 "WOWGUID_UNIQUE_PROBED_DELETE",
87 "WOWGUID_UNIQUE_JAM_TEMP",
88 "WOWGUID_TO_STRING_FAILED",
89 "WOWGUID_FROM_STRING_FAILED",
90 "WOWGUID_UNIQUE_SERVER_SELF",
91 "WOWGUID_UNIQUE_MAGIC_SELF",
92 "WOWGUID_UNIQUE_MAGIC_PET",
93 "WOWGUID_UNIQUE_INVALID_TRANSPORT",
94 "WOWGUID_UNIQUE_AMMO_ID",
95 "WOWGUID_SPELL_TARGET_TRADE_ITEM",
96 "WOWGUID_SCRIPT_TARGET_INVALID",
97 "WOWGUID_SCRIPT_TARGET_NONE",
98 nullptr,
99 "WOWGUID_FAKE_MODERATOR",
100 nullptr,
101 nullptr,
102 "WOWGUID_UNIQUE_ACCOUNT_OBJ_INITIALIZATION",
103 nullptr,
104 "WOWGUID_PENDING_PERMANENT_CHARACTER_ASSIGNMENT"
105 };
106
108 if (id >= std::size(uniqNames))
109 id = 3;
110
111 return Trinity::StringFormat("{}-{}", typeName, uniqNames[id]);
112 }
113
114 ObjectGuid ParseUniq(HighGuid /*type*/, char const* guidString)
115 {
116 constexpr char const* uniqNames[] =
117 {
118 nullptr,
119 "WOWGUID_UNIQUE_PROBED_DELETE",
120 "WOWGUID_UNIQUE_JAM_TEMP",
121 "WOWGUID_TO_STRING_FAILED",
122 "WOWGUID_FROM_STRING_FAILED",
123 "WOWGUID_UNIQUE_SERVER_SELF",
124 "WOWGUID_UNIQUE_MAGIC_SELF",
125 "WOWGUID_UNIQUE_MAGIC_PET",
126 "WOWGUID_UNIQUE_INVALID_TRANSPORT",
127 "WOWGUID_UNIQUE_AMMO_ID",
128 "WOWGUID_SPELL_TARGET_TRADE_ITEM",
129 "WOWGUID_SCRIPT_TARGET_INVALID",
130 "WOWGUID_SCRIPT_TARGET_NONE",
131 nullptr,
132 "WOWGUID_FAKE_MODERATOR",
133 nullptr,
134 nullptr,
135 "WOWGUID_UNIQUE_ACCOUNT_OBJ_INITIALIZATION"
136 };
137
138 for (std::size_t id = 0; id < std::size(uniqNames); ++id)
139 {
140 if (!uniqNames[id])
141 continue;
142
143 if (strcmp(guidString, uniqNames[id]) == 0)
145 }
146
148 }
149
150 std::string FormatPlayer(char const* typeName, ObjectGuid guid)
151 {
152 return Trinity::StringFormat("{}-{}-{:08X}", typeName, guid.GetRealmId(), guid.GetRawValue(0));
153 }
154
155 ObjectGuid ParsePlayer(HighGuid /*type*/, char const* guidString)
156 {
157 uint32 realmId = 0;
158 uint64 dbId = UI64LIT(0);
159
160 if (std::sscanf(guidString, "%u-%08" SCNx64, &realmId, &dbId) != 2)
162
163 return ObjectGuidFactory::CreatePlayer(realmId, dbId);
164 }
165
166 std::string FormatItem(char const* typeName, ObjectGuid guid)
167 {
168 return Trinity::StringFormat("{}-{}-{}-{:016X}", typeName, guid.GetRealmId(), uint32(guid.GetRawValue(1) >> 18) & 0xFFFFFF, guid.GetRawValue(0));
169 }
170
171 ObjectGuid ParseItem(HighGuid /*type*/, char const* guidString)
172 {
173 uint32 realmId = 0;
174 uint32 arg1 = 0;
175 uint64 dbId = UI64LIT(0);
176
177 if (std::sscanf(guidString, "%u-%u-%016" SCNx64, &realmId, &arg1, &dbId) != 3)
179
180 return ObjectGuidFactory::CreateItem(realmId, dbId);
181 }
182
183 std::string FormatWorldObject(char const* typeName, ObjectGuid guid)
184 {
185 return Trinity::StringFormat("{}-{}-{}-{}-{}-{}-{:010X}", typeName, guid.GetSubType(), guid.GetRealmId(), guid.GetMapId(),
186 uint32(guid.GetRawValue(0) >> 40) & 0xFFFFFF, guid.GetEntry(), guid.GetCounter());
187 }
188
189 ObjectGuid ParseWorldObject(HighGuid type, char const* guidString)
190 {
191 uint32 subType = 0;
192 uint32 realmId = 0;
193 uint32 mapId = 0;
194 uint32 serverId = 0;
195 uint32 id = 0;
196 uint64 counter = UI64LIT(0);
197 if (std::sscanf(guidString, "%u-%u-%u-%u-%u-%010" SCNx64, &subType, &realmId, &mapId, &serverId, &id, &counter) != 6)
199
200 return ObjectGuidFactory::CreateWorldObject(type, subType, realmId, mapId, serverId, id, counter);
201 }
202
203 std::string FormatTransport(char const* typeName, ObjectGuid guid)
204 {
205 return Trinity::StringFormat("{}-{}-{:016X}", typeName, uint32(guid.GetRawValue(1) >> 38) & 0xFFFFF, guid.GetRawValue(0));
206 }
207
208 ObjectGuid ParseTransport(HighGuid type, char const* guidString)
209 {
210 uint32 id = 0;
211 uint64 counter = UI64LIT(0);
212
213 if (std::sscanf(guidString, "%u-%016" SCNx64, &id, &counter) != 2)
215
216 return ObjectGuidFactory::CreateTransport(type, counter);
217 }
218
219 std::string FormatClientActor(char const* typeName, ObjectGuid guid)
220 {
221 return Trinity::StringFormat("{}-{}-{}-{}", typeName, guid.GetRealmId(), uint32(guid.GetRawValue(1) >> 26) & 0xFFFFFF, uint32(guid.GetRawValue(0)));
222 }
223
224 ObjectGuid ParseClientActor(HighGuid /*type*/, char const* guidString)
225 {
226 uint32 ownerType = 0;
227 uint32 ownerId = 0;
228 uint32 counter = 0;
229
230 if (std::sscanf(guidString, "%u-%u-%u", &ownerType, &ownerId, &counter) != 3)
232
233 return ObjectGuidFactory::CreateClientActor(ownerType, ownerId, counter);
234 }
235
236 std::string FormatChatChannel(char const* typeName, ObjectGuid guid)
237 {
238 uint32 builtIn = (guid.GetRawValue(1) >> 25) & 0x1;
239 uint32 trade = (guid.GetRawValue(1) >> 24) & 0x1;
240 uint32 zoneId = (guid.GetRawValue(1) >> 10) & 0x3FFF;
241 uint32 factionGroupMask = (guid.GetRawValue(1) >> 4) & 0x3F;
242 return Trinity::StringFormat("{}-{}-{}-{}-{}-{}-{:08X}", typeName, guid.GetRealmId(), builtIn, trade, zoneId, factionGroupMask, guid.GetRawValue(0));
243 }
244
245 ObjectGuid ParseChatChannel(HighGuid /*type*/, char const* guidString)
246 {
247 uint32 realmId = 0;
248 uint32 builtIn = 0;
249 uint32 trade = 0;
250 uint32 zoneId = 0;
251 uint32 factionGroupMask = 0;
252 uint64 id = UI64LIT(0);
253
254 if (std::sscanf(guidString, "%u-%u-%u-%u-%u-%08" SCNx64, &realmId, &builtIn, &trade, &zoneId, &factionGroupMask, &id) != 6)
256
257 return ObjectGuidFactory::CreateChatChannel(realmId, builtIn != 0, trade != 0, zoneId, factionGroupMask, id);
258 }
259
260 std::string FormatGlobal(char const* typeName, ObjectGuid guid)
261 {
262 return Trinity::StringFormat("{}-{}-{:012X}", typeName, guid.GetRawValue(1) & 0x3FFFFFFFFFFFFFF, guid.GetRawValue(0));
263 }
264
265 ObjectGuid ParseGlobal(HighGuid type, char const* guidString)
266 {
267 uint64 dbIdHigh = UI64LIT(0);
268 uint64 dbIdLow = UI64LIT(0);
269
270 if (std::sscanf(guidString, "%" SCNu64 "-%012" SCNx64, &dbIdHigh, &dbIdLow) != 2)
272
273 return ObjectGuidFactory::CreateGlobal(type, dbIdHigh, dbIdLow);
274 }
275
276 std::string FormatGuild(char const* typeName, ObjectGuid guid)
277 {
278 return Trinity::StringFormat("{}-{}-{:012X}", typeName, guid.GetRealmId(), guid.GetRawValue(0));
279 }
280
281 ObjectGuid ParseGuild(HighGuid type, char const* guidString)
282 {
283 uint32 realmId = 0;
284 uint64 dbId = UI64LIT(0);
285
286 if (std::sscanf(guidString, "%u-%012" SCNx64, &realmId, &dbId) != 2)
288
289 return ObjectGuidFactory::CreateGuild(type, realmId, dbId);
290 }
291
292 std::string FormatMobileSession(char const* typeName, ObjectGuid guid)
293 {
294 return Trinity::StringFormat("{}-{}-{}-{:08X}", typeName, guid.GetRealmId(), uint32(guid.GetRawValue(1) >> 33) & 0x1FF, guid.GetRawValue(0));
295 }
296
297 ObjectGuid ParseMobileSession(HighGuid /*type*/, char const* guidString)
298 {
299 uint32 realmId = 0;
300 uint32 arg1 = 0;
301 uint64 counter = UI64LIT(0);
302
303 if (std::sscanf(guidString, "%u-%u-%08" SCNx64, &realmId, &arg1, &counter) != 3)
305
306 return ObjectGuidFactory::CreateMobileSession(realmId, arg1, counter);
307 }
308
309 std::string FormatWebObj(char const* typeName, ObjectGuid guid)
310 {
311 return Trinity::StringFormat("{}-{}-{}-{}-{:012X}", typeName, guid.GetRealmId(), uint32(guid.GetRawValue(1) >> 37) & 0x1F,
312 uint32(guid.GetRawValue(1) >> 35) & 0x3, guid.GetRawValue(0));
313 }
314
315 ObjectGuid ParseWebObj(HighGuid /*type*/, char const* guidString)
316 {
317 uint32 realmId = 0;
318 uint32 arg1 = 0;
319 uint32 arg2 = 0;
320 uint64 counter = UI64LIT(0);
321
322 if (std::sscanf(guidString, "%u-%u-%u-%012" SCNx64, &realmId, &arg1, &arg2, &counter) != 4)
324
325 return ObjectGuidFactory::CreateWebObj(realmId, arg1, arg2, counter);
326 }
327
328 std::string FormatLFGObject(char const* typeName, ObjectGuid guid)
329 {
330 return Trinity::StringFormat("{}-{}-{}-{}-{}-{}-{}-{:06X}", typeName, uint32(guid.GetRawValue(1) >> 54) & 0xF, uint32(guid.GetRawValue(1) >> 50) & 0xF,
331 uint32(guid.GetRawValue(1) >> 46) & 0xF, uint32(guid.GetRawValue(1) >> 38) & 0xFF, uint32(guid.GetRawValue(1) >> 37) & 0x1,
332 uint32(guid.GetRawValue(1) >> 35) & 0x3, guid.GetRawValue(0));
333 }
334
335 ObjectGuid ParseLFGObject(HighGuid /*type*/, char const* guidString)
336 {
337 uint32 arg1 = 0;
338 uint32 arg2 = 0;
339 uint32 arg3 = 0;
340 uint32 arg4 = 0;
341 uint32 arg5 = 0;
342 uint32 arg6 = 0;
343 uint64 counter = UI64LIT(0);
344
345 if (std::sscanf(guidString, "%u-%u-%u-%u-%u-%u-%06" SCNx64, &arg1, &arg2, &arg3, &arg4, &arg5, &arg6, &counter) != 7)
347
348 return ObjectGuidFactory::CreateLFGObject(arg1, arg2, arg3, arg4, arg5 != 0, arg6, counter);
349 }
350
351 std::string FormatLFGList(char const* typeName, ObjectGuid guid)
352 {
353 return Trinity::StringFormat("{}-{}-{:06X}", typeName, uint32(guid.GetRawValue(1) >> 54) & 0xF, guid.GetRawValue(0));
354 }
355
356 ObjectGuid ParseLFGList(HighGuid /*type*/, char const* guidString)
357 {
358 uint32 arg1 = 0;
359 uint64 counter = UI64LIT(0);
360
361 if (std::sscanf(guidString, "%u-%06" SCNx64, &arg1, &counter) != 2)
363
364 return ObjectGuidFactory::CreateLFGList(arg1, counter);
365 }
366
367 std::string FormatClient(char const* typeName, ObjectGuid guid)
368 {
369 return Trinity::StringFormat("{}-{}-{}-{:012X}", typeName, guid.GetRealmId(), uint32(guid.GetRawValue(1) >> 10) & 0xFFFFFFFF, guid.GetRawValue(0));
370 }
371
372 ObjectGuid ParseClient(HighGuid type, char const* guidString)
373 {
374 uint32 realmId = 0;
375 uint32 arg1 = 0;
376 uint64 counter = UI64LIT(0);
377
378 if (std::sscanf(guidString, "%u-%u-%012" SCNx64, &realmId, &arg1, &counter) != 3)
380
381 return ObjectGuidFactory::CreateClient(type, realmId, arg1, counter);
382 }
383
384 std::string FormatClubFinder(char const* typeName, ObjectGuid guid)
385 {
386 uint32 type = uint32(guid.GetRawValue(1) >> 33) & 0xFF;
387 uint32 clubFinderId = uint32(guid.GetRawValue(1)) & 0xFFFFFFFF;
388 if (type == 1) // guild
389 return Trinity::StringFormat("{}-{}-{}-{}-{}", typeName, type, clubFinderId, guid.GetRealmId(), guid.GetRawValue(0) /*guildId*/);
390
391 return Trinity::StringFormat("{}-{}-{}-{:016X}", typeName, type, clubFinderId, guid.GetRawValue(0) /*clubId*/);
392 }
393
394 ObjectGuid ParseClubFinder(HighGuid /*type*/, char const* guidString)
395 {
396 uint32 type = 0;
397 uint32 consumed = 0;
398
399 if (std::sscanf(guidString, "%u-%n", &type, &consumed) != 1)
401
402 uint32 clubFinderId = 0;
403 uint32 realmId = 0;
404 uint64 dbId = UI64LIT(0);
405
406 switch (type)
407 {
408 case 0: // club
409 if (std::sscanf(guidString + consumed, "%u-%016" SCNx64, &clubFinderId, &dbId) != 2)
411 break;
412 case 1: // guild
413 if (std::sscanf(guidString + consumed, "%u-%u-%016" SCNx64, &clubFinderId, &realmId, &dbId) != 3)
415 break;
416 default:
418 }
419
420 return ObjectGuidFactory::CreateClubFinder(realmId, type, clubFinderId, dbId);
421 }
422
423 std::string FormatToolsClient(char const* typeName, ObjectGuid guid)
424 {
425 return Trinity::StringFormat("{}-{}-{}-{:010X}", typeName, guid.GetMapId(), uint32(guid.GetRawValue(0) >> 40) & 0xFFFFFF, guid.GetCounter());
426 }
427
428 ObjectGuid ParseToolsClient(HighGuid /*type*/, char const* guidString)
429 {
430 uint32 mapId = 0;
431 uint32 serverId = 0;
432 uint64 counter = UI64LIT(0);
433 if (std::sscanf(guidString, "%u-%u-%010" SCNx64, &mapId, &serverId, &counter) != 3)
435
436 return ObjectGuidFactory::CreateToolsClient(mapId, serverId, counter);
437 }
438
439 std::string FormatWorldLayer(char const* typeName, ObjectGuid guid)
440 {
441 return Trinity::StringFormat("{}-{:X}-{}-{}-{}", typeName, uint32((guid.GetRawValue(1) >> 10) & 0xFFFFFFFF), uint32(guid.GetRawValue(1) & 0x1FF),
442 uint32((guid.GetRawValue(0) >> 24) & 0xFF), uint32(guid.GetRawValue(0) & 0x7FFFFF));
443 }
444
445 ObjectGuid ParseWorldLayer(HighGuid /*type*/, char const* guidString)
446 {
447 uint32 arg1 = 0;
448 uint16 arg2 = 0;
449 uint8 arg3 = 0;
450 uint32 arg4 = 0;
451 if (std::sscanf(guidString, "%x-%hu-%hhu-%u", &arg1, &arg2, &arg3, &arg4) != 4)
453
454 return ObjectGuidFactory::CreateWorldLayer(arg1, arg2, arg3, arg4);
455 }
456
457 std::string FormatLMMLobby(char const* typeName, ObjectGuid guid)
458 {
459 return Trinity::StringFormat("{}-{}-{}-{}-{}-{:X}", typeName, guid.GetRealmId(), uint32(guid.GetRawValue(1) >> 26) & 0xFFFFFF,
460 uint32(guid.GetRawValue(1) >> 18) & 0xFF, uint32(guid.GetRawValue(1) >> 10) & 0xFF, guid.GetRawValue(0));
461 }
462
463 ObjectGuid ParseLMMLobby(HighGuid /*type*/, char const* guidString)
464 {
465 uint32 realmId = 0;
466 uint32 arg2 = 0;
467 uint8 arg3 = 0;
468 uint8 arg4 = 0;
469 uint64 arg5 = 0;
470 if (std::sscanf(guidString, "%u-%u-%hhu-%hhu-%" SCNx64, &realmId, &arg2, &arg3, &arg4, &arg5) != 5)
472
473 return ObjectGuidFactory::CreateLMMLobby(realmId, arg2, arg3, arg4, arg5);
474 }
475
476 ObjectGuidInfo();
477 } Info;
478
479 ObjectGuidInfo::ObjectGuidInfo()
480 {
481#define SET_GUID_INFO(type, format, parse) \
482 Names[AsUnderlyingType(HighGuid::type)] = #type;\
483 ClientFormatFunction[AsUnderlyingType(HighGuid::type)] = &ObjectGuidInfo::format;\
484 ClientParseFunction[AsUnderlyingType(HighGuid::type)] = &ObjectGuidInfo::parse
485
486 SET_GUID_INFO(Null, FormatNull, ParseNull);
487 SET_GUID_INFO(Uniq, FormatUniq, ParseUniq);
488 SET_GUID_INFO(Player, FormatPlayer, ParsePlayer);
489 SET_GUID_INFO(Item, FormatItem, ParseItem);
490 SET_GUID_INFO(WorldTransaction, FormatWorldObject, ParseWorldObject);
491 SET_GUID_INFO(StaticDoor, FormatTransport, ParseTransport);
492 SET_GUID_INFO(Transport, FormatTransport, ParseTransport);
493 SET_GUID_INFO(Conversation, FormatWorldObject, ParseWorldObject);
494 SET_GUID_INFO(Creature, FormatWorldObject, ParseWorldObject);
495 SET_GUID_INFO(Vehicle, FormatWorldObject, ParseWorldObject);
496 SET_GUID_INFO(Pet, FormatWorldObject, ParseWorldObject);
497 SET_GUID_INFO(GameObject, FormatWorldObject, ParseWorldObject);
498 SET_GUID_INFO(DynamicObject, FormatWorldObject, ParseWorldObject);
499 SET_GUID_INFO(AreaTrigger, FormatWorldObject, ParseWorldObject);
500 SET_GUID_INFO(Corpse, FormatWorldObject, ParseWorldObject);
501 SET_GUID_INFO(LootObject, FormatWorldObject, ParseWorldObject);
502 SET_GUID_INFO(SceneObject, FormatWorldObject, ParseWorldObject);
503 SET_GUID_INFO(Scenario, FormatWorldObject, ParseWorldObject);
504 SET_GUID_INFO(AIGroup, FormatWorldObject, ParseWorldObject);
505 SET_GUID_INFO(DynamicDoor, FormatWorldObject, ParseWorldObject);
506 SET_GUID_INFO(ClientActor, FormatClientActor, ParseClientActor);
507 SET_GUID_INFO(Vignette, FormatWorldObject, ParseWorldObject);
508 SET_GUID_INFO(CallForHelp, FormatWorldObject, ParseWorldObject);
509 SET_GUID_INFO(AIResource, FormatWorldObject, ParseWorldObject);
510 SET_GUID_INFO(AILock, FormatWorldObject, ParseWorldObject);
511 SET_GUID_INFO(AILockTicket, FormatWorldObject, ParseWorldObject);
512 SET_GUID_INFO(ChatChannel, FormatChatChannel, ParseChatChannel);
513 SET_GUID_INFO(Party, FormatGlobal, ParseGlobal);
514 SET_GUID_INFO(Guild, FormatGuild, ParseGuild);
515 SET_GUID_INFO(WowAccount, FormatGlobal, ParseGlobal);
516 SET_GUID_INFO(BNetAccount, FormatGlobal, ParseGlobal);
517 SET_GUID_INFO(GMTask, FormatGlobal, ParseGlobal);
518 SET_GUID_INFO(MobileSession, FormatMobileSession, ParseMobileSession);
519 SET_GUID_INFO(RaidGroup, FormatGlobal, ParseGlobal);
520 SET_GUID_INFO(Spell, FormatGlobal, ParseGlobal);
521 SET_GUID_INFO(Mail, FormatGlobal, ParseGlobal);
522 SET_GUID_INFO(WebObj, FormatWebObj, ParseWebObj);
523 SET_GUID_INFO(LFGObject, FormatLFGObject, ParseLFGObject);
524 SET_GUID_INFO(LFGList, FormatLFGList, ParseLFGList);
525 SET_GUID_INFO(UserRouter, FormatGlobal, ParseGlobal);
526 SET_GUID_INFO(PVPQueueGroup, FormatGlobal, ParseGlobal);
527 SET_GUID_INFO(UserClient, FormatGlobal, ParseGlobal);
528 SET_GUID_INFO(PetBattle, FormatClient, ParseClient);
529 SET_GUID_INFO(UniqUserClient, FormatClient, ParseClient);
530 SET_GUID_INFO(BattlePet, FormatGlobal, ParseGlobal);
531 SET_GUID_INFO(CommerceObj, FormatGlobal, ParseGlobal);
532 SET_GUID_INFO(ClientSession, FormatClient, ParseClient);
533 SET_GUID_INFO(Cast, FormatWorldObject, ParseWorldObject);
534 SET_GUID_INFO(ClientConnection, FormatClient, ParseClient);
535 SET_GUID_INFO(ClubFinder, FormatClubFinder, ParseClubFinder);
536 SET_GUID_INFO(ToolsClient, FormatToolsClient, ParseToolsClient);
537 SET_GUID_INFO(WorldLayer, FormatWorldLayer, ParseWorldLayer);
538 SET_GUID_INFO(ArenaTeam, FormatGuild, ParseGuild);
539 SET_GUID_INFO(LMMParty, FormatClient, ParseClient);
540 SET_GUID_INFO(LMMLobby, FormatLMMLobby, ParseLMMLobby);
541
542#undef SET_GUID_INFO
543 }
544}
545
547{
548 if (high >= HighGuid::Count)
549 return "<unknown>";
550
551 return Info.Names[uint32(high)].c_str();
552}
553
554std::string ObjectGuid::ToString() const
555{
556 return Info.Format(*this);
557}
558
559std::string ObjectGuid::ToHexString() const
560{
561 return Trinity::StringFormat("0x{:016X}{:016X}", _data[1], _data[0]);
562}
563
564ObjectGuid ObjectGuid::FromString(std::string const& guidString)
565{
566 return Info.Parse(guidString);
567}
568
569std::size_t ObjectGuid::GetHash() const
570{
571 std::size_t hashVal = 0;
572 Trinity::hash_combine(hashVal, _data[0]);
573 Trinity::hash_combine(hashVal, _data[1]);
574 return hashVal;
575}
576
577std::vector<uint8> ObjectGuid::GetRawValue() const
578{
579 std::vector<uint8> raw(16);
580 memcpy(raw.data(), this, sizeof(*this));
581 return raw;
582}
583
584void ObjectGuid::SetRawValue(std::vector<uint8> const& guid)
585{
586 ASSERT(guid.size() == sizeof(*this));
587 memcpy(this, guid.data(), sizeof(*this));
588}
589
591{
592 if (realmId)
593 return realmId;
594
595 return realm.Id.Realm;
596}
597
599{
600 return ObjectGuid();
601}
602
604{
605 return ObjectGuid(uint64(uint64(HighGuid::Uniq) << 58),
606 id);
607}
608
610{
612 | (uint64(GetRealmIdForObjectGuid(realmId)) << 42)),
613 dbId);
614}
615
617{
618 return ObjectGuid(uint64((uint64(HighGuid::Item) << 58)
619 | (uint64(GetRealmIdForObjectGuid(realmId)) << 42)),
620 dbId);
621}
622
624{
625 return ObjectGuid(uint64((uint64(type) << 58)
626 | (uint64(GetRealmIdForObjectGuid(realmId) & 0x1FFF) << 42)
627 | (uint64(mapId & 0x1FFF) << 29)
628 | (uint64(entry & 0x7FFFFF) << 6)
629 | (uint64(subType) & 0x3F)),
630 uint64((uint64(serverId & 0xFFFFFF) << 40)
631 | (counter & UI64LIT(0xFFFFFFFFFF))));
632}
633
635{
636 return ObjectGuid(uint64((uint64(type) << 58)
637 | (uint64(counter) << 38)),
638 UI64LIT(0));
639}
640
642{
644 | (uint64(ownerType & 0x1FFF) << 42)
645 | (uint64(ownerId & 0xFFFFFF) << 26)),
646 uint64(counter));
647}
648
649ObjectGuid ObjectGuidFactory::CreateChatChannel(uint32 realmId, bool builtIn, bool trade, uint16 zoneId, uint8 factionGroupMask, ObjectGuid::LowType counter)
650{
652 | (uint64(GetRealmIdForObjectGuid(realmId) & 0x1FFF) << 42)
653 | (uint64(builtIn) << 25)
654 | (uint64(trade) << 24)
655 | (uint64(zoneId & 0x3FFF) << 10)
656 | (uint64(factionGroupMask & 0x3F) << 4)),
657 counter);
658}
659
661{
662 return ObjectGuid(uint64((uint64(type) << 58)
663 | (uint64(dbIdHigh & UI64LIT(0x3FFFFFFFFFFFFFF)))),
664 dbId);
665}
666
668{
669 return ObjectGuid(uint64((uint64(type) << 58)
670 | (uint64(GetRealmIdForObjectGuid(realmId)) << 42)),
671 dbId);
672}
673
675{
677 | (uint64(GetRealmIdForObjectGuid(realmId)) << 42)
678 | (uint64(arg1 & 0x1FF) << 33)),
679 counter);
680}
681
683{
685 | (uint64(GetRealmIdForObjectGuid(realmId) & 0x1FFF) << 42)
686 | (uint64(arg1 & 0x1F) << 37)
687 | (uint64(arg2 & 0x3) << 35)),
688 counter);
689}
690
692{
694 | (uint64(arg1 & 0xF) << 54)
695 | (uint64(arg2 & 0xF) << 50)
696 | (uint64(arg3 & 0xF) << 46)
697 | (uint64(arg4 & 0xFF) << 38)
698 | (uint64(arg5 ? 1 : 0) << 37)
699 | (uint64(arg6 & 0x3) << 35)),
700 counter);
701}
702
704{
706 | (uint64(arg1 & 0xF) << 54)),
707 counter);
708}
709
711{
712 return ObjectGuid(uint64((uint64(type) << 58)
713 | (uint64(GetRealmIdForObjectGuid(realmId) & 0x1FFF) << 42)
714 | (uint64(arg1 & 0xFFFFFFFF) << 10)),
715 counter);
716}
717
719{
721 | (type == 1 ? (uint64(GetRealmIdForObjectGuid(realmId) & 0x1FFF) << 42) : UI64LIT(0))
722 | (uint64(type & 0xFF) << 33)
723 | (uint64(clubFinderId & 0xFFFFFFFF))),
724 dbId);
725}
726
728{
730 | uint64(mapId)),
731 uint64((uint64(serverId & 0xFFFFFF) << 40)
732 | (counter & UI64LIT(0xFFFFFFFFFF))));
733}
734
736{
738 | (uint64(arg1 & 0xFFFFFFFF) << 10)
739 | (uint64(arg2 & 0x1FF))),
740 uint64((uint64(arg3 & 0xFF) << 24)
741 | uint64(arg4 & 0x7FFFFF)));
742}
743
745{
747 | (uint64(GetRealmIdForObjectGuid(realmId)) << 42)
748 | (uint64(arg2 & 0xFFFFFFFF) << 26)
749 | (uint64(arg3 & 0xFF) << 18)
750 | (uint64(arg4 & 0xFF) << 10)),
751 counter);
752}
753
755ObjectGuid const ObjectGuid::FromStringFailed = ObjectGuid::Create<HighGuid::Uniq>(UI64LIT(4));
756ObjectGuid const ObjectGuid::TradeItem = ObjectGuid::Create<HighGuid::Uniq>(UI64LIT(10));
757
759{
760 uint8 lowMask = 0;
761 uint8 highMask = 0;
762 buf.FlushBits(); // flush any unwritten bits to make wpos return a meaningful value
763 std::size_t pos = buf.wpos();
764 buf << uint8(lowMask);
765 buf << uint8(highMask);
766
767 uint8 packed[8];
768 if (size_t packedSize = ByteBuffer::PackUInt64(guid._data[0], &lowMask, packed))
769 buf.append(packed, packedSize);
770 if (size_t packedSize = ByteBuffer::PackUInt64(guid._data[1], &highMask, packed))
771 buf.append(packed, packedSize);
772
773 buf.put(pos, lowMask);
774 buf.put(pos + 1, highMask);
775
776 return buf;
777}
778
780{
781 uint8 lowMask, highMask;
782 buf >> lowMask >> highMask;
783 buf.ReadPackedUInt64(lowMask, guid._data[0]);
784 buf.ReadPackedUInt64(highMask, guid._data[1]);
785 return buf;
786}
787
789{
792
795
796 return _nextGuid++;
797}
798
800{
801 TC_LOG_ERROR("misc", "{} guid overflow!! Can't continue, shutting down server. ", ObjectGuid::GetTypeName(_high));
803}
804
806{
807 if (!sWorld->IsGuidAlert() && _nextGuid > sWorld->getIntConfig(CONFIG_RESPAWN_GUIDALERTLEVEL))
808 sWorld->TriggerGuidAlert();
809 else if (!sWorld->IsGuidWarning() && _nextGuid > sWorld->getIntConfig(CONFIG_RESPAWN_GUIDWARNLEVEL))
810 sWorld->TriggerGuidWarning();
811}
uint8_t uint8
Definition: Define.h:144
int32_t int32
Definition: Define.h:138
uint64_t uint64
Definition: Define.h:141
#define UI64LIT(N)
Definition: Define.h:127
uint16_t uint16
Definition: Define.h:143
uint32_t uint32
Definition: Define.h:142
#define ASSERT
Definition: Errors.h:68
#define TC_LOG_ERROR(filterType__,...)
Definition: Log.h:165
ByteBuffer & operator<<(ByteBuffer &buf, ObjectGuid const &guid)
Definition: ObjectGuid.cpp:758
ByteBuffer & operator>>(ByteBuffer &buf, ObjectGuid &guid)
Definition: ObjectGuid.cpp:779
static uint32 GetRealmIdForObjectGuid(uint32 realmId)
Definition: ObjectGuid.cpp:590
#define SET_GUID_INFO(type, format, parse)
HighGuid
Definition: ObjectGuid.h:75
constexpr std::underlying_type< E >::type AsUnderlyingType(E enumValue)
Definition: Util.h:491
static size_t PackUInt64(uint64 value, uint8 *mask, uint8 *result)
Definition: ByteBuffer.h:605
void append(T value)
Definition: ByteBuffer.h:143
size_t wpos() const
Definition: ByteBuffer.h:412
void ReadPackedUInt64(uint64 &guid)
Definition: ByteBuffer.h:485
void put(std::size_t pos, T value)
Definition: ByteBuffer.h:220
void FlushBits()
Definition: ByteBuffer.h:155
Definition: Corpse.h:53
Definition: Guild.h:329
Definition: Item.h:170
static ObjectGuid CreateTransport(HighGuid type, uint32 counter)
Definition: ObjectGuid.cpp:634
static ObjectGuid CreateGlobal(HighGuid type, uint64 dbIdHigh, uint64 dbId)
Definition: ObjectGuid.cpp:660
static ObjectGuid CreateItem(uint32 realmId, uint64 dbId)
Definition: ObjectGuid.cpp:616
static ObjectGuid CreateWorldLayer(uint32 arg1, uint16 arg2, uint8 arg3, uint32 arg4)
Definition: ObjectGuid.cpp:735
static ObjectGuid CreateLMMLobby(uint32 realmId, uint32 arg2, uint8 arg3, uint8 arg4, uint64 counter)
Definition: ObjectGuid.cpp:744
static ObjectGuid CreateMobileSession(uint32 realmId, uint16 arg1, uint64 counter)
Definition: ObjectGuid.cpp:674
static ObjectGuid CreateClubFinder(uint32 realmId, uint8 type, uint32 clubFinderId, uint64 dbId)
Definition: ObjectGuid.cpp:718
static ObjectGuid CreateClient(HighGuid type, uint32 realmId, uint32 arg1, uint64 counter)
Definition: ObjectGuid.cpp:710
static ObjectGuid CreateClientActor(uint16 ownerType, uint16 ownerId, uint32 counter)
Definition: ObjectGuid.cpp:641
static ObjectGuid CreateToolsClient(uint16 mapId, uint32 serverId, uint64 counter)
Definition: ObjectGuid.cpp:727
static ObjectGuid CreateGuild(HighGuid type, uint32 realmId, uint64 dbId)
Definition: ObjectGuid.cpp:667
static ObjectGuid CreateUniq(uint64 id)
Definition: ObjectGuid.cpp:603
static ObjectGuid CreateWorldObject(HighGuid type, uint8 subType, uint32 realmId, uint16 mapId, uint32 serverId, uint32 entry, uint64 counter)
Definition: ObjectGuid.cpp:623
static ObjectGuid CreateChatChannel(uint32 realmId, bool builtIn, bool trade, uint16 zoneId, uint8 factionGroupMask, uint64 counter)
Definition: ObjectGuid.cpp:649
static ObjectGuid CreatePlayer(uint32 realmId, uint64 dbId)
Definition: ObjectGuid.cpp:609
static ObjectGuid CreateNull()
Definition: ObjectGuid.cpp:598
static ObjectGuid CreateLFGList(uint8 arg1, uint64 counter)
Definition: ObjectGuid.cpp:703
static ObjectGuid CreateLFGObject(uint8 arg1, uint8 arg2, uint8 arg3, uint8 arg4, bool arg5, uint8 arg6, uint64 counter)
Definition: ObjectGuid.cpp:691
static ObjectGuid CreateWebObj(uint32 realmId, uint8 arg1, uint8 arg2, uint64 counter)
Definition: ObjectGuid.cpp:682
ObjectGuid::LowType _nextGuid
Definition: ObjectGuid.h:412
void HandleCounterOverflow()
Definition: ObjectGuid.cpp:799
ObjectGuid::LowType Generate()
Definition: ObjectGuid.cpp:788
uint32 GetMapId() const
Definition: ObjectGuid.h:290
LowType GetCounter() const
Definition: ObjectGuid.h:293
static ObjectGuid const Empty
Definition: ObjectGuid.h:274
static ObjectGuid FromString(std::string const &guidString)
Definition: ObjectGuid.cpp:564
uint64 GetRawValue(std::size_t i) const
Definition: ObjectGuid.h:282
static ObjectGuid const FromStringFailed
Definition: ObjectGuid.h:275
std::string ToHexString() const
Definition: ObjectGuid.cpp:559
std::size_t GetHash() const
Definition: ObjectGuid.cpp:569
std::string ToString() const
Definition: ObjectGuid.cpp:554
LowType GetMaxCounter() const
Definition: ObjectGuid.h:317
void SetRawValue(std::vector< uint8 > const &guid)
Definition: ObjectGuid.cpp:584
std::vector< uint8 > GetRawValue() const
Definition: ObjectGuid.cpp:577
std::array< uint64, 2 > _data
Definition: ObjectGuid.h:387
uint32 GetEntry() const
Definition: ObjectGuid.h:291
static ObjectGuid const TradeItem
Definition: ObjectGuid.h:276
uint32 GetSubType() const
Definition: ObjectGuid.h:292
char const * GetTypeName() const
Definition: ObjectGuid.h:353
uint64 LowType
Definition: ObjectGuid.h:278
uint32 GetRealmId() const
Definition: ObjectGuid.h:289
HighGuid GetHigh() const
Definition: ObjectGuid.h:288
Definition: Pet.h:40
Definition: Spell.h:255
static void StopNow(uint8 exitcode)
Definition: World.h:670
#define sWorld
Definition: World.h:931
Realm realm
Definition: World.cpp:3966
@ CONFIG_RESPAWN_GUIDALERTLEVEL
Definition: World.h:432
@ CONFIG_RESPAWN_GUIDWARNLEVEL
Definition: World.h:431
@ ERROR_EXIT_CODE
Definition: World.h:75
void hash_combine(std::size_t &seed, T const &val)
Definition: Hash.h:28
std::string StringFormat(FormatString< Args... > fmt, Args &&... args)
Default TC string format function.
Definition: StringFormat.h:38
constexpr std::size_t size()
Definition: UpdateField.h:796
Definition: Mail.h:175
Battlenet::RealmHandle Id
Definition: Realm.h:82