1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
|
/*
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ObjectGuid.h"
#include "ByteBuffer.h"
#include "Errors.h"
#include "Hash.h"
#include "RealmList.h"
#include "StringFormat.h"
#include "Util.h"
#include <charconv>
static_assert(sizeof(ObjectGuid) == sizeof(uint64) * 2, "ObjectGuid must be exactly 16 bytes");
namespace
{
struct ObjectGuidInfo
{
using FormatFunction = fmt::appender(fmt::format_context& ctx, std::string_view typeName, ObjectGuid const& guid);
using ParseFunction = ObjectGuid(HighGuid type, std::string_view guidString);
std::string_view Names[AsUnderlyingType(HighGuid::Count)];
std::array<FormatFunction*, AsUnderlyingType(HighGuid::Count)> ClientFormatFunction;
std::array<ParseFunction*, AsUnderlyingType(HighGuid::Count)> ClientParseFunction;
static std::string Format(ObjectGuid const& guid)
{
return Trinity::StringFormat("{}", guid);
}
ObjectGuid Parse(std::string_view guidString) const
{
std::size_t typeEnd = guidString.find('-');
if (typeEnd == std::string::npos)
{
if (guidString == "0000000000000000")
return ObjectGuid::Empty;
return ObjectGuid::FromStringFailed;
}
std::string_view typeStr = guidString.substr(0, typeEnd);
guidString.remove_prefix(typeEnd + 1);
// find correct parser for type name
for (int32 type = 0; type < AsUnderlyingType(HighGuid::Count); ++type)
if (typeStr == Names[type])
return ClientParseFunction[type](HighGuid(type), guidString);
return ObjectGuid::FromStringFailed;
}
struct FormatPadding { std::ptrdiff_t Value; constexpr operator std::ptrdiff_t() const { return Value; } };
template <std::ptrdiff_t Width>
static constexpr inline FormatPadding padding{ .Value = Width };
static constexpr inline FormatPadding no_padding{ .Value = 0 };
struct FormatBase { int32 Value; constexpr operator int32() const { return Value; } };
static constexpr inline FormatBase dec{ 10 };
static constexpr inline FormatBase hex{ 16 };
static fmt::appender AppendTypeName(fmt::format_context& ctx, std::string_view type)
{
return std::ranges::copy(type, ctx.out()).out;
}
template <FormatPadding Width, FormatBase Base>
static fmt::appender AppendComponent(fmt::format_context& ctx, uint64 component)
{
*ctx.out() = '-';
std::array<char, 20> buf;
auto [end, err] = std::to_chars(buf.data(), buf.data() + buf.size(), component, Base);
ASSERT(err == std::errc(), "Failed to convert guid part to string");
if constexpr (Width != 0)
{
if (std::ptrdiff_t written = std::distance(buf.data(), end); written < Width)
std::ranges::fill_n(ctx.out(), Width - written, '0');
}
if constexpr (Base > 10)
return std::ranges::transform(buf.data(), end, ctx.out(), charToUpper).out;
else
return std::ranges::copy(buf.data(), end, ctx.out()).out;
}
static fmt::appender AppendComponent(fmt::format_context& ctx, std::string_view component)
{
*ctx.out() = '-';
return std::ranges::copy(component, ctx.out()).out;
}
template <FormatBase Base, typename T>
static bool ParseComponent(std::string_view& input, T* component)
{
auto [end, err] = std::from_chars(input.data(), input.data() + input.length(), *component, Base);
if (err != std::errc())
return false;
input.remove_prefix(end - input.data());
if (!input.empty())
{
if (input[0] != '-')
return false;
input.remove_prefix(1);
}
return true;
}
static bool ParseDone(std::string_view const& sv) { return sv.empty(); }
static fmt::appender FormatNull(fmt::format_context& ctx, std::string_view /*typeName*/, ObjectGuid const& /*guid*/)
{
return std::ranges::fill_n(ctx.out(), 16, '0');
}
static ObjectGuid ParseNull(HighGuid, std::string_view)
{
return ObjectGuid::Empty;
}
static constexpr std::array<std::string_view, 20> UniqNames =
{
"",
"WOWGUID_UNIQUE_PROBED_DELETE",
"WOWGUID_UNIQUE_JAM_TEMP",
"WOWGUID_TO_STRING_FAILED",
"WOWGUID_FROM_STRING_FAILED",
"WOWGUID_UNIQUE_SERVER_SELF",
"WOWGUID_UNIQUE_MAGIC_SELF",
"WOWGUID_UNIQUE_MAGIC_PET",
"WOWGUID_UNIQUE_INVALID_TRANSPORT",
"WOWGUID_UNIQUE_AMMO_ID",
"WOWGUID_SPELL_TARGET_TRADE_ITEM",
"WOWGUID_SCRIPT_TARGET_INVALID",
"WOWGUID_SCRIPT_TARGET_NONE",
"",
"WOWGUID_FAKE_MODERATOR",
"",
"",
"WOWGUID_UNIQUE_ACCOUNT_OBJ_INITIALIZATION",
"",
"WOWGUID_PENDING_PERMANENT_CHARACTER_ASSIGNMENT"
};
static fmt::appender FormatUniq(fmt::format_context& ctx, std::string_view typeName, ObjectGuid const& guid)
{
ObjectGuid::LowType id = guid.GetCounter();
if (id >= UniqNames.size() || UniqNames[id].empty())
id = 3;
ctx.advance_to(AppendTypeName(ctx, typeName));
ctx.advance_to(AppendComponent(ctx, UniqNames[id]));
return ctx.out();
}
static ObjectGuid ParseUniq(HighGuid /*type*/, std::string_view guidString)
{
for (std::size_t id = 0; id < UniqNames.size(); ++id)
{
if (UniqNames[id].empty())
continue;
if (guidString == UniqNames[id])
return ObjectGuidFactory::CreateUniq(id);
}
return ObjectGuid::FromStringFailed;
}
static fmt::appender FormatPlayer(fmt::format_context& ctx, std::string_view typeName, ObjectGuid const& guid)
{
ctx.advance_to(AppendTypeName(ctx, typeName));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRealmId()));
ctx.advance_to(AppendComponent<padding<8>, hex>(ctx, guid.GetRawValue(0)));
return ctx.out();
}
static ObjectGuid ParsePlayer(HighGuid /*type*/, std::string_view guidString)
{
uint32 realmId = 0;
uint64 dbId = UI64LIT(0);
if (!ParseComponent<dec>(guidString, &realmId)
|| !ParseComponent<hex>(guidString, &dbId)
|| !ParseDone(guidString))
return ObjectGuid::FromStringFailed;
return ObjectGuidFactory::CreatePlayer(realmId, dbId);
}
static fmt::appender FormatItem(fmt::format_context& ctx, std::string_view typeName, ObjectGuid const& guid)
{
ctx.advance_to(AppendTypeName(ctx, typeName));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRealmId()));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRawValue(1) >> 18 & 0xFFFFFF));
ctx.advance_to(AppendComponent<padding<16>, hex>(ctx, guid.GetRawValue(0)));
return ctx.out();
}
static ObjectGuid ParseItem(HighGuid /*type*/, std::string_view guidString)
{
uint32 realmId = 0;
uint32 arg1 = 0;
uint64 dbId = UI64LIT(0);
if (!ParseComponent<dec>(guidString, &realmId)
|| !ParseComponent<dec>(guidString, &arg1)
|| !ParseComponent<hex>(guidString, &dbId)
|| !ParseDone(guidString))
return ObjectGuid::FromStringFailed;
return ObjectGuidFactory::CreateItem(realmId, dbId);
}
static fmt::appender FormatWorldObject(fmt::format_context& ctx, std::string_view typeName, ObjectGuid const& guid)
{
ctx.advance_to(AppendTypeName(ctx, typeName));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetSubType()));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRealmId()));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetMapId()));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRawValue(0) >> 40 & 0xFFFFFF));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetEntry()));
ctx.advance_to(AppendComponent<padding<10>, hex>(ctx, guid.GetCounter()));
return ctx.out();
}
static ObjectGuid ParseWorldObject(HighGuid type, std::string_view guidString)
{
uint8 subType = 0;
uint32 realmId = 0;
uint16 mapId = 0;
uint32 serverId = 0;
uint32 id = 0;
uint64 counter = UI64LIT(0);
if (!ParseComponent<dec>(guidString, &subType)
|| !ParseComponent<dec>(guidString, &realmId)
|| !ParseComponent<dec>(guidString, &mapId)
|| !ParseComponent<dec>(guidString, &serverId)
|| !ParseComponent<dec>(guidString, &id)
|| !ParseComponent<hex>(guidString, &counter)
|| !ParseDone(guidString))
return ObjectGuid::FromStringFailed;
return ObjectGuidFactory::CreateWorldObject(type, subType, realmId, mapId, serverId, id, counter);
}
static fmt::appender FormatTransport(fmt::format_context& ctx, std::string_view typeName, ObjectGuid const& guid)
{
ctx.advance_to(AppendTypeName(ctx, typeName));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetEntry()));
ctx.advance_to(AppendComponent<padding<16>, hex>(ctx, guid.GetRawValue(0)));
return ctx.out();
}
static ObjectGuid ParseTransport(HighGuid type, std::string_view guidString)
{
uint32 id = 0;
uint32 counter = 0;
if (!ParseComponent<dec>(guidString, &id)
|| !ParseComponent<hex>(guidString, &counter)
|| !ParseDone(guidString))
return ObjectGuid::FromStringFailed;
return ObjectGuidFactory::CreateTransport(type, counter);
}
static fmt::appender FormatClientActor(fmt::format_context& ctx, std::string_view typeName, ObjectGuid const& guid)
{
ctx.advance_to(AppendTypeName(ctx, typeName));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRealmId()));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRawValue(1) >> 26 & 0xFFFFFF));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRawValue(0) & 0xFFFFFFFF));
return ctx.out();
}
static ObjectGuid ParseClientActor(HighGuid /*type*/, std::string_view guidString)
{
uint16 ownerType = 0;
uint16 ownerId = 0;
uint32 counter = 0;
if (!ParseComponent<dec>(guidString, &ownerType)
|| !ParseComponent<dec>(guidString, &ownerId)
|| !ParseComponent<dec>(guidString, &counter)
|| !ParseDone(guidString))
return ObjectGuid::FromStringFailed;
return ObjectGuidFactory::CreateClientActor(ownerType, ownerId, counter);
}
static fmt::appender FormatChatChannel(fmt::format_context& ctx, std::string_view typeName, ObjectGuid const& guid)
{
uint32 builtIn = (guid.GetRawValue(1) >> 25) & 0x1;
uint32 trade = (guid.GetRawValue(1) >> 24) & 0x1;
uint32 zoneId = (guid.GetRawValue(1) >> 10) & 0x3FFF;
uint32 factionGroupMask = (guid.GetRawValue(1) >> 4) & 0x3F;
ctx.advance_to(AppendTypeName(ctx, typeName));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRealmId()));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, builtIn));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, trade));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, zoneId));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, factionGroupMask));
ctx.advance_to(AppendComponent<padding<8>, hex>(ctx, guid.GetRawValue(0)));
return ctx.out();
}
static ObjectGuid ParseChatChannel(HighGuid /*type*/, std::string_view guidString)
{
uint32 realmId = 0;
uint32 builtIn = 0;
uint32 trade = 0;
uint16 zoneId = 0;
uint8 factionGroupMask = 0;
uint64 id = UI64LIT(0);
if (!ParseComponent<dec>(guidString, &realmId)
|| !ParseComponent<dec>(guidString, &builtIn)
|| !ParseComponent<dec>(guidString, &trade)
|| !ParseComponent<dec>(guidString, &zoneId)
|| !ParseComponent<dec>(guidString, &factionGroupMask)
|| !ParseComponent<hex>(guidString, &id)
|| !ParseDone(guidString))
return ObjectGuid::FromStringFailed;
return ObjectGuidFactory::CreateChatChannel(realmId, builtIn != 0, trade != 0, zoneId, factionGroupMask, id);
}
static fmt::appender FormatGlobal(fmt::format_context& ctx, std::string_view typeName, ObjectGuid const& guid)
{
ctx.advance_to(AppendTypeName(ctx, typeName));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRawValue(1) & 0x3FFFFFFFFFFFFFF));
ctx.advance_to(AppendComponent<padding<12>, hex>(ctx, guid.GetRawValue(0)));
return ctx.out();
}
static ObjectGuid ParseGlobal(HighGuid type, std::string_view guidString)
{
uint64 dbIdHigh = UI64LIT(0);
uint64 dbIdLow = UI64LIT(0);
if (!ParseComponent<dec>(guidString, &dbIdHigh)
|| !ParseComponent<hex>(guidString, &dbIdLow)
|| !ParseDone(guidString))
return ObjectGuid::FromStringFailed;
return ObjectGuidFactory::CreateGlobal(type, dbIdHigh, dbIdLow);
}
static fmt::appender FormatGuild(fmt::format_context& ctx, std::string_view typeName, ObjectGuid const& guid)
{
ctx.advance_to(AppendTypeName(ctx, typeName));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRealmId()));
ctx.advance_to(AppendComponent<padding<12>, hex>(ctx, guid.GetRawValue(0)));
return ctx.out();
}
static ObjectGuid ParseGuild(HighGuid type, std::string_view guidString)
{
uint32 realmId = 0;
uint64 dbId = UI64LIT(0);
if (!ParseComponent<dec>(guidString, &realmId)
|| !ParseComponent<hex>(guidString, &dbId)
|| !ParseDone(guidString))
return ObjectGuid::FromStringFailed;
return ObjectGuidFactory::CreateGuild(type, realmId, dbId);
}
static fmt::appender FormatMobileSession(fmt::format_context& ctx, std::string_view typeName, ObjectGuid const& guid)
{
ctx.advance_to(AppendTypeName(ctx, typeName));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRealmId()));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRawValue(1) >> 33 & 0x1FF));
ctx.advance_to(AppendComponent<padding<8>, hex>(ctx, guid.GetRawValue(0)));
return ctx.out();
}
static ObjectGuid ParseMobileSession(HighGuid /*type*/, std::string_view guidString)
{
uint32 realmId = 0;
uint16 arg1 = 0;
uint64 counter = UI64LIT(0);
if (!ParseComponent<dec>(guidString, &realmId)
|| !ParseComponent<dec>(guidString, &arg1)
|| !ParseComponent<hex>(guidString, &counter)
|| !ParseDone(guidString))
return ObjectGuid::FromStringFailed;
return ObjectGuidFactory::CreateMobileSession(realmId, arg1, counter);
}
static fmt::appender FormatWebObj(fmt::format_context& ctx, std::string_view typeName, ObjectGuid const& guid)
{
ctx.advance_to(AppendTypeName(ctx, typeName));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRealmId()));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRawValue(1) >> 37 & 0x1F));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRawValue(1) >> 35 & 0x3));
ctx.advance_to(AppendComponent<padding<12>, hex>(ctx, guid.GetRawValue(0)));
return ctx.out();
}
static ObjectGuid ParseWebObj(HighGuid /*type*/, std::string_view guidString)
{
uint32 realmId = 0;
uint8 arg1 = 0;
uint8 arg2 = 0;
uint64 counter = UI64LIT(0);
if (!ParseComponent<dec>(guidString, &realmId)
|| !ParseComponent<dec>(guidString, &arg1)
|| !ParseComponent<dec>(guidString, &arg2)
|| !ParseComponent<hex>(guidString, &counter)
|| !ParseDone(guidString))
return ObjectGuid::FromStringFailed;
return ObjectGuidFactory::CreateWebObj(realmId, arg1, arg2, counter);
}
static fmt::appender FormatLFGObject(fmt::format_context& ctx, std::string_view typeName, ObjectGuid const& guid)
{
ctx.advance_to(AppendTypeName(ctx, typeName));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRawValue(1) >> 54 & 0xF));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRawValue(1) >> 50 & 0xF));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRawValue(1) >> 46 & 0xF));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRawValue(1) >> 38 & 0xFF));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRawValue(1) >> 37 & 0x1));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRawValue(1) >> 35 & 0x3));
ctx.advance_to(AppendComponent<padding<6>, hex>(ctx, guid.GetRawValue(0)));
return ctx.out();
}
static ObjectGuid ParseLFGObject(HighGuid /*type*/, std::string_view guidString)
{
uint8 arg1 = 0;
uint8 arg2 = 0;
uint8 arg3 = 0;
uint8 arg4 = 0;
uint8 arg5 = 0;
uint8 arg6 = 0;
uint64 counter = UI64LIT(0);
if (!ParseComponent<dec>(guidString, &arg1)
|| !ParseComponent<dec>(guidString, &arg2)
|| !ParseComponent<dec>(guidString, &arg3)
|| !ParseComponent<dec>(guidString, &arg4)
|| !ParseComponent<dec>(guidString, &arg5)
|| !ParseComponent<dec>(guidString, &arg6)
|| !ParseComponent<hex>(guidString, &counter)
|| !ParseDone(guidString))
return ObjectGuid::FromStringFailed;
return ObjectGuidFactory::CreateLFGObject(arg1, arg2, arg3, arg4, arg5 != 0, arg6, counter);
}
static fmt::appender FormatLFGList(fmt::format_context& ctx, std::string_view typeName, ObjectGuid const& guid)
{
ctx.advance_to(AppendTypeName(ctx, typeName));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRawValue(1) >> 54 & 0xF));
ctx.advance_to(AppendComponent<padding<6>, hex>(ctx, guid.GetRawValue(0)));
return ctx.out();
}
static ObjectGuid ParseLFGList(HighGuid /*type*/, std::string_view guidString)
{
uint8 arg1 = 0;
uint64 counter = UI64LIT(0);
if (!ParseComponent<dec>(guidString, &arg1)
|| !ParseComponent<hex>(guidString, &counter)
|| !ParseDone(guidString))
return ObjectGuid::FromStringFailed;
return ObjectGuidFactory::CreateLFGList(arg1, counter);
}
static fmt::appender FormatClient(fmt::format_context& ctx, std::string_view typeName, ObjectGuid const& guid)
{
ctx.advance_to(AppendTypeName(ctx, typeName));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRealmId()));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRawValue(1) >> 10 & 0xFFFFFFFF));
ctx.advance_to(AppendComponent<padding<12>, hex>(ctx, guid.GetRawValue(0)));
return ctx.out();
}
static ObjectGuid ParseClient(HighGuid type, std::string_view guidString)
{
uint32 realmId = 0;
uint32 arg1 = 0;
uint64 counter = UI64LIT(0);
if (!ParseComponent<dec>(guidString, &realmId)
|| !ParseComponent<dec>(guidString, &arg1)
|| !ParseComponent<hex>(guidString, &counter)
|| !ParseDone(guidString))
return ObjectGuid::FromStringFailed;
return ObjectGuidFactory::CreateClient(type, realmId, arg1, counter);
}
static fmt::appender FormatClubFinder(fmt::format_context& ctx, std::string_view typeName, ObjectGuid const& guid)
{
uint32 type = uint32(guid.GetRawValue(1) >> 33) & 0xFF;
uint32 clubFinderId = uint32(guid.GetRawValue(1)) & 0xFFFFFFFF;
ctx.advance_to(AppendTypeName(ctx, typeName));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, type));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, clubFinderId));
switch (type)
{
case 0: // club
ctx.advance_to(AppendComponent<padding<16>, hex>(ctx, guid.GetRawValue(0))); // clubId
break;
case 1: // guild
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRealmId()));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRawValue(0))); // guildId
break;
default:
break;
}
return ctx.out();
}
static ObjectGuid ParseClubFinder(HighGuid /*type*/, std::string_view guidString)
{
uint8 type = 0;
uint32 clubFinderId = 0;
uint32 realmId = 0;
uint64 dbId = UI64LIT(0);
if (!ParseComponent<dec>(guidString, &type)
|| !ParseComponent<dec>(guidString, &clubFinderId))
return ObjectGuid::FromStringFailed;
switch (type)
{
case 0: // club
if (!ParseComponent<hex>(guidString, &dbId))
return ObjectGuid::FromStringFailed;
break;
case 1: // guild
if (!ParseComponent<dec>(guidString, &realmId)
|| !ParseComponent<dec>(guidString, &dbId))
return ObjectGuid::FromStringFailed;
break;
default:
return ObjectGuid::FromStringFailed;
}
if (!ParseDone(guidString))
return ObjectGuid::FromStringFailed;
return ObjectGuidFactory::CreateClubFinder(realmId, type, clubFinderId, dbId);
}
static fmt::appender FormatToolsClient(fmt::format_context& ctx, std::string_view typeName, ObjectGuid const& guid)
{
ctx.advance_to(AppendTypeName(ctx, typeName));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetMapId()));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRawValue(0) >> 40 & 0xFFFFFF));
ctx.advance_to(AppendComponent<padding<10>, hex>(ctx, guid.GetCounter()));
return ctx.out();
}
static ObjectGuid ParseToolsClient(HighGuid /*type*/, std::string_view guidString)
{
uint16 mapId = 0;
uint32 serverId = 0;
uint64 counter = UI64LIT(0);
if (!ParseComponent<dec>(guidString, &mapId)
|| !ParseComponent<dec>(guidString, &serverId)
|| !ParseComponent<hex>(guidString, &counter)
|| !ParseDone(guidString))
return ObjectGuid::FromStringFailed;
return ObjectGuidFactory::CreateToolsClient(mapId, serverId, counter);
}
static fmt::appender FormatWorldLayer(fmt::format_context& ctx, std::string_view typeName, ObjectGuid const& guid)
{
ctx.advance_to(AppendTypeName(ctx, typeName));
ctx.advance_to(AppendComponent<padding<0>, hex>(ctx, guid.GetRawValue(1) >> 10 & 0xFFFFFFFF));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRawValue(1) & 0x1FF));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRawValue(0) >> 24 & 0xFF));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRawValue(0) & 0x7FFFFF));
return ctx.out();
}
static ObjectGuid ParseWorldLayer(HighGuid /*type*/, std::string_view guidString)
{
uint32 arg1 = 0;
uint16 arg2 = 0;
uint8 arg3 = 0;
uint32 arg4 = 0;
if (!ParseComponent<hex>(guidString, &arg1)
|| !ParseComponent<dec>(guidString, &arg2)
|| !ParseComponent<dec>(guidString, &arg3)
|| !ParseComponent<dec>(guidString, &arg4)
|| !ParseDone(guidString))
return ObjectGuid::FromStringFailed;
return ObjectGuidFactory::CreateWorldLayer(arg1, arg2, arg3, arg4);
}
static fmt::appender FormatLMMLobby(fmt::format_context& ctx, std::string_view typeName, ObjectGuid const& guid)
{
ctx.advance_to(AppendTypeName(ctx, typeName));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, guid.GetRealmId()));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, uint32(guid.GetRawValue(1) >> 26) & 0xFFFFFF));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, uint32(guid.GetRawValue(1) >> 18) & 0xFF));
ctx.advance_to(AppendComponent<no_padding, dec>(ctx, uint32(guid.GetRawValue(1) >> 10) & 0xFF));
ctx.advance_to(AppendComponent<padding<0>, hex>(ctx, guid.GetRawValue(0)));
return ctx.out();
}
static ObjectGuid ParseLMMLobby(HighGuid /*type*/, std::string_view guidString)
{
uint32 realmId = 0;
uint32 arg2 = 0;
uint8 arg3 = 0;
uint8 arg4 = 0;
uint64 arg5 = 0;
if (!ParseComponent<dec>(guidString, &realmId)
|| !ParseComponent<dec>(guidString, &arg2)
|| !ParseComponent<dec>(guidString, &arg3)
|| !ParseComponent<dec>(guidString, &arg4)
|| !ParseComponent<hex>(guidString, &arg5)
|| !ParseDone(guidString))
return ObjectGuid::FromStringFailed;
return ObjectGuidFactory::CreateLMMLobby(realmId, arg2, arg3, arg4, arg5);
}
ObjectGuidInfo();
} Info;
ObjectGuidInfo::ObjectGuidInfo()
{
#define SET_GUID_INFO(type, format, parse) \
Names[AsUnderlyingType(HighGuid::type)] = #type ## sv;\
ClientFormatFunction[AsUnderlyingType(HighGuid::type)] = &ObjectGuidInfo::format;\
ClientParseFunction[AsUnderlyingType(HighGuid::type)] = &ObjectGuidInfo::parse
SET_GUID_INFO(Null, FormatNull, ParseNull);
SET_GUID_INFO(Uniq, FormatUniq, ParseUniq);
SET_GUID_INFO(Player, FormatPlayer, ParsePlayer);
SET_GUID_INFO(Item, FormatItem, ParseItem);
SET_GUID_INFO(WorldTransaction, FormatWorldObject, ParseWorldObject);
SET_GUID_INFO(StaticDoor, FormatTransport, ParseTransport);
SET_GUID_INFO(Transport, FormatTransport, ParseTransport);
SET_GUID_INFO(Conversation, FormatWorldObject, ParseWorldObject);
SET_GUID_INFO(Creature, FormatWorldObject, ParseWorldObject);
SET_GUID_INFO(Vehicle, FormatWorldObject, ParseWorldObject);
SET_GUID_INFO(Pet, FormatWorldObject, ParseWorldObject);
SET_GUID_INFO(GameObject, FormatWorldObject, ParseWorldObject);
SET_GUID_INFO(DynamicObject, FormatWorldObject, ParseWorldObject);
SET_GUID_INFO(AreaTrigger, FormatWorldObject, ParseWorldObject);
SET_GUID_INFO(Corpse, FormatWorldObject, ParseWorldObject);
SET_GUID_INFO(LootObject, FormatWorldObject, ParseWorldObject);
SET_GUID_INFO(SceneObject, FormatWorldObject, ParseWorldObject);
SET_GUID_INFO(Scenario, FormatWorldObject, ParseWorldObject);
SET_GUID_INFO(AIGroup, FormatWorldObject, ParseWorldObject);
SET_GUID_INFO(DynamicDoor, FormatWorldObject, ParseWorldObject);
SET_GUID_INFO(ClientActor, FormatClientActor, ParseClientActor);
SET_GUID_INFO(Vignette, FormatWorldObject, ParseWorldObject);
SET_GUID_INFO(CallForHelp, FormatWorldObject, ParseWorldObject);
SET_GUID_INFO(AIResource, FormatWorldObject, ParseWorldObject);
SET_GUID_INFO(AILock, FormatWorldObject, ParseWorldObject);
SET_GUID_INFO(AILockTicket, FormatWorldObject, ParseWorldObject);
SET_GUID_INFO(ChatChannel, FormatChatChannel, ParseChatChannel);
SET_GUID_INFO(Party, FormatGlobal, ParseGlobal);
SET_GUID_INFO(Guild, FormatGuild, ParseGuild);
SET_GUID_INFO(WowAccount, FormatGlobal, ParseGlobal);
SET_GUID_INFO(BNetAccount, FormatGlobal, ParseGlobal);
SET_GUID_INFO(GMTask, FormatGlobal, ParseGlobal);
SET_GUID_INFO(MobileSession, FormatMobileSession, ParseMobileSession);
SET_GUID_INFO(RaidGroup, FormatGlobal, ParseGlobal);
SET_GUID_INFO(Spell, FormatGlobal, ParseGlobal);
SET_GUID_INFO(Mail, FormatGlobal, ParseGlobal);
SET_GUID_INFO(WebObj, FormatWebObj, ParseWebObj);
SET_GUID_INFO(LFGObject, FormatLFGObject, ParseLFGObject);
SET_GUID_INFO(LFGList, FormatLFGList, ParseLFGList);
SET_GUID_INFO(UserRouter, FormatGlobal, ParseGlobal);
SET_GUID_INFO(PVPQueueGroup, FormatGlobal, ParseGlobal);
SET_GUID_INFO(UserClient, FormatGlobal, ParseGlobal);
SET_GUID_INFO(PetBattle, FormatClient, ParseClient);
SET_GUID_INFO(UniqUserClient, FormatClient, ParseClient);
SET_GUID_INFO(BattlePet, FormatGlobal, ParseGlobal);
SET_GUID_INFO(CommerceObj, FormatGlobal, ParseGlobal);
SET_GUID_INFO(ClientSession, FormatClient, ParseClient);
SET_GUID_INFO(Cast, FormatWorldObject, ParseWorldObject);
SET_GUID_INFO(ClientConnection, FormatClient, ParseClient);
SET_GUID_INFO(ClubFinder, FormatClubFinder, ParseClubFinder);
SET_GUID_INFO(ToolsClient, FormatToolsClient, ParseToolsClient);
SET_GUID_INFO(WorldLayer, FormatWorldLayer, ParseWorldLayer);
SET_GUID_INFO(ArenaTeam, FormatGuild, ParseGuild);
SET_GUID_INFO(LMMParty, FormatClient, ParseClient);
SET_GUID_INFO(LMMLobby, FormatLMMLobby, ParseLMMLobby);
#undef SET_GUID_INFO
}
}
template <typename FormatContext>
auto fmt::formatter<ObjectGuid>::format(ObjectGuid const& guid, FormatContext& ctx) const -> decltype(ctx.out())
{
if (guid.GetHigh() >= HighGuid::Count)
return this->format(ObjectGuid::ToStringFailed, ctx);
int32 type = AsUnderlyingType(guid.GetHigh());
if (!Info.ClientFormatFunction[type])
return this->format(ObjectGuid::ToStringFailed, ctx);
return Info.ClientFormatFunction[type](ctx, Info.Names[type], guid);
}
template TC_GAME_API fmt::appender fmt::formatter<ObjectGuid>::format<fmt::format_context>(ObjectGuid const&, format_context&) const;
std::string_view ObjectGuid::GetTypeName(HighGuid high)
{
if (high >= HighGuid::Count)
return "<unknown>";
return Info.Names[uint32(high)];
}
std::string ObjectGuid::ToString() const
{
return ObjectGuidInfo::Format(*this);
}
std::string ObjectGuid::ToHexString() const
{
return Trinity::StringFormat("0x{:016X}{:016X}", _data[1], _data[0]);
}
ObjectGuid ObjectGuid::FromString(std::string_view guidString)
{
return Info.Parse(guidString);
}
std::size_t ObjectGuid::GetHash() const
{
std::size_t hashVal = 0;
Trinity::hash_combine(hashVal, _data[0]);
Trinity::hash_combine(hashVal, _data[1]);
return hashVal;
}
std::array<uint8, 16> ObjectGuid::GetRawValue() const
{
std::array<uint8, 16> raw;
memcpy(raw.data(), _data.data(), BytesSize);
return raw;
}
void ObjectGuid::SetRawValue(std::span<uint8 const> rawBytes)
{
ASSERT(rawBytes.size() == BytesSize, SZFMTD " == " SZFMTD, rawBytes.size(), BytesSize);
memcpy(_data.data(), rawBytes.data(), BytesSize);
}
static inline uint32 GetRealmIdForObjectGuid(uint32 realmId)
{
if (realmId)
return realmId;
return sRealmList->GetCurrentRealmId().Realm;
}
constexpr ObjectGuid ObjectGuidFactory::CreateNull()
{
return ObjectGuid();
}
constexpr ObjectGuid ObjectGuidFactory::CreateUniq(ObjectGuid::LowType id)
{
return ObjectGuid(uint64(uint64(HighGuid::Uniq) << 58),
id);
}
ObjectGuid ObjectGuidFactory::CreatePlayer(uint32 realmId, ObjectGuid::LowType dbId)
{
return ObjectGuid(uint64((uint64(HighGuid::Player) << 58)
| (uint64(GetRealmIdForObjectGuid(realmId)) << 42)),
dbId);
}
ObjectGuid ObjectGuidFactory::CreateItem(uint32 realmId, ObjectGuid::LowType dbId)
{
return ObjectGuid(uint64((uint64(HighGuid::Item) << 58)
| (uint64(GetRealmIdForObjectGuid(realmId)) << 42)),
dbId);
}
ObjectGuid ObjectGuidFactory::CreateWorldObject(HighGuid type, uint8 subType, uint32 realmId, uint16 mapId, uint32 serverId, uint32 entry, ObjectGuid::LowType counter)
{
return ObjectGuid(uint64((uint64(type) << 58)
| (uint64(GetRealmIdForObjectGuid(realmId) & 0x1FFF) << 42)
| (uint64(mapId & 0x1FFF) << 29)
| (uint64(entry & 0x7FFFFF) << 6)
| (uint64(subType) & 0x3F)),
uint64((uint64(serverId & 0xFFFFFF) << 40)
| (counter & UI64LIT(0xFFFFFFFFFF))));
}
ObjectGuid ObjectGuidFactory::CreateTransport(HighGuid type, uint32 counter)
{
return ObjectGuid(uint64((uint64(type) << 58)
| (uint64(counter) << 38)),
UI64LIT(0));
}
ObjectGuid ObjectGuidFactory::CreateClientActor(uint16 ownerType, uint16 ownerId, uint32 counter)
{
return ObjectGuid(uint64((uint64(HighGuid::ClientActor) << 58)
| (uint64(ownerType & 0x1FFF) << 42)
| (uint64(ownerId & 0xFFFFFF) << 26)),
uint64(counter));
}
ObjectGuid ObjectGuidFactory::CreateChatChannel(uint32 realmId, bool builtIn, bool trade, uint16 zoneId, uint8 factionGroupMask, ObjectGuid::LowType counter)
{
return ObjectGuid(uint64((uint64(HighGuid::ChatChannel) << 58)
| (uint64(GetRealmIdForObjectGuid(realmId) & 0x1FFF) << 42)
| (uint64(builtIn) << 25)
| (uint64(trade) << 24)
| (uint64(zoneId & 0x3FFF) << 10)
| (uint64(factionGroupMask & 0x3F) << 4)),
counter);
}
ObjectGuid ObjectGuidFactory::CreateGlobal(HighGuid type, ObjectGuid::LowType dbIdHigh, ObjectGuid::LowType dbId)
{
return ObjectGuid(uint64((uint64(type) << 58)
| (uint64(dbIdHigh & UI64LIT(0x3FFFFFFFFFFFFFF)))),
dbId);
}
ObjectGuid ObjectGuidFactory::CreateGuild(HighGuid type, uint32 realmId, ObjectGuid::LowType dbId)
{
return ObjectGuid(uint64((uint64(type) << 58)
| (uint64(GetRealmIdForObjectGuid(realmId)) << 42)),
dbId);
}
ObjectGuid ObjectGuidFactory::CreateMobileSession(uint32 realmId, uint16 arg1, ObjectGuid::LowType counter)
{
return ObjectGuid(uint64((uint64(HighGuid::MobileSession) << 58)
| (uint64(GetRealmIdForObjectGuid(realmId)) << 42)
| (uint64(arg1 & 0x1FF) << 33)),
counter);
}
ObjectGuid ObjectGuidFactory::CreateWebObj(uint32 realmId, uint8 arg1, uint8 arg2, ObjectGuid::LowType counter)
{
return ObjectGuid(uint64((uint64(HighGuid::WebObj) << 58)
| (uint64(GetRealmIdForObjectGuid(realmId) & 0x1FFF) << 42)
| (uint64(arg1 & 0x1F) << 37)
| (uint64(arg2 & 0x3) << 35)),
counter);
}
ObjectGuid ObjectGuidFactory::CreateLFGObject(uint8 arg1, uint8 arg2, uint8 arg3, uint8 arg4, bool arg5, uint8 arg6, ObjectGuid::LowType counter)
{
return ObjectGuid(uint64((uint64(HighGuid::LFGObject) << 58)
| (uint64(arg1 & 0xF) << 54)
| (uint64(arg2 & 0xF) << 50)
| (uint64(arg3 & 0xF) << 46)
| (uint64(arg4 & 0xFF) << 38)
| (uint64(arg5 ? 1 : 0) << 37)
| (uint64(arg6 & 0x3) << 35)),
counter);
}
ObjectGuid ObjectGuidFactory::CreateLFGList(uint8 arg1, ObjectGuid::LowType counter)
{
return ObjectGuid(uint64((uint64(HighGuid::LFGObject) << 58)
| (uint64(arg1 & 0xF) << 54)),
counter);
}
ObjectGuid ObjectGuidFactory::CreateClient(HighGuid type, uint32 realmId, uint32 arg1, ObjectGuid::LowType counter)
{
return ObjectGuid(uint64((uint64(type) << 58)
| (uint64(GetRealmIdForObjectGuid(realmId) & 0x1FFF) << 42)
| (uint64(arg1 & 0xFFFFFFFF) << 10)),
counter);
}
ObjectGuid ObjectGuidFactory::CreateClubFinder(uint32 realmId, uint8 type, uint32 clubFinderId, ObjectGuid::LowType dbId)
{
return ObjectGuid(uint64((uint64(HighGuid::ClubFinder) << 58)
| (type == 1 ? (uint64(GetRealmIdForObjectGuid(realmId) & 0x1FFF) << 42) : UI64LIT(0))
| (uint64(type & 0xFF) << 33)
| (uint64(clubFinderId & 0xFFFFFFFF))),
dbId);
}
ObjectGuid ObjectGuidFactory::CreateToolsClient(uint16 mapId, uint32 serverId, uint64 counter)
{
return ObjectGuid(uint64((uint64(HighGuid::ToolsClient) << 58)
| uint64(mapId)),
uint64((uint64(serverId & 0xFFFFFF) << 40)
| (counter & UI64LIT(0xFFFFFFFFFF))));
}
ObjectGuid ObjectGuidFactory::CreateWorldLayer(uint32 arg1, uint16 arg2, uint8 arg3, uint32 arg4)
{
return ObjectGuid(uint64((uint64(HighGuid::WorldLayer) << 58)
| (uint64(arg1 & 0xFFFFFFFF) << 10)
| (uint64(arg2 & 0x1FF))),
uint64((uint64(arg3 & 0xFF) << 24)
| uint64(arg4 & 0x7FFFFF)));
}
ObjectGuid ObjectGuidFactory::CreateLMMLobby(uint32 realmId, uint32 arg2, uint8 arg3, uint8 arg4, ObjectGuid::LowType counter)
{
return ObjectGuid(uint64((uint64(HighGuid::LMMLobby) << 58)
| (uint64(GetRealmIdForObjectGuid(realmId)) << 42)
| (uint64(arg2 & 0xFFFFFFFF) << 26)
| (uint64(arg3 & 0xFF) << 18)
| (uint64(arg4 & 0xFF) << 10)),
counter);
}
ObjectGuid const ObjectGuid::Empty = ObjectGuid::Create<HighGuid::Null>();
ObjectGuid const ObjectGuid::ToStringFailed = ObjectGuid::Create<HighGuid::Uniq>(UI64LIT(3));
ObjectGuid const ObjectGuid::FromStringFailed = ObjectGuid::Create<HighGuid::Uniq>(UI64LIT(4));
ObjectGuid const ObjectGuid::TradeItem = ObjectGuid::Create<HighGuid::Uniq>(UI64LIT(10));
ByteBuffer& operator<<(ByteBuffer& buf, ObjectGuid const& guid)
{
static constexpr std::size_t NumUInt64s = 2;
std::array<uint8, NumUInt64s + ObjectGuid::BytesSize> bytes;
memset(bytes.data(), 0, NumUInt64s);
size_t packedSize = guid._data.size();
for (std::size_t i = 0; i < guid._data.size(); ++i)
{
for (uint32 b = 0; b < 8; ++b)
{
if (uint8 byte = uint8((guid._data[i] >> (b * 8)) & 0xFF))
{
bytes[packedSize++] = byte;
bytes[i] |= uint8(1 << b);
}
}
}
buf.append(bytes.data(), packedSize);
return buf;
}
ByteBuffer& operator>>(ByteBuffer& buf, ObjectGuid& guid)
{
std::array<uint8, 2> mask;
buf.read(mask);
for (std::size_t i = 0; i < guid._data.size(); ++i)
for (uint32 b = 0; b < 8; ++b)
if (mask[i] & (uint8(1) << b))
guid._data[i] |= uint64(buf.read<uint8>()) << (b * 8);
return buf;
}
|