TrinityCore
Loading...
Searching...
No Matches
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 "Log.h"
22#include "Util.h"
23#include "World.h"
24#include <charconv>
25
27
28namespace
29{
30struct ObjectGuidInfo
31{
32 struct FormatPadding { std::ptrdiff_t Value; constexpr operator std::ptrdiff_t() const { return Value; } };
33
34 template <std::ptrdiff_t Width>
35 static constexpr inline FormatPadding padding{ .Value = Width };
36 static constexpr inline FormatPadding no_padding{ .Value = 0 };
37
38 struct FormatBase { int32 Value; constexpr operator int32() const { return Value; } };
39
40 static constexpr inline FormatBase dec{ 10 };
41 static constexpr inline FormatBase hex{ 16 };
42
43 static fmt::appender AppendTypeName(fmt::format_context& ctx, std::string_view type)
44 {
45 return std::copy(type.begin(), type.end(), ctx.out());
46 }
47
48 template <FormatPadding Width, FormatBase Base>
49 static fmt::appender AppendComponent(fmt::format_context& ctx, uint64 component)
50 {
51 std::array<char, 20> buf;
52 auto [end, err] = std::to_chars(buf.data(), buf.data() + buf.size(), component, Base);
53
54 ASSERT(err == std::errc(), "Failed to convert guid part to string");
55
56 if constexpr (Width != 0)
57 {
58 if (std::distance(buf.data(), end) < Width)
59 std::fill_n(ctx.out(), Width - std::distance(buf.data(), end), '0');
60 }
61
62 if constexpr (Base > 10)
63 return std::transform(buf.data(), end, ctx.out(), charToUpper);
64 else
65 return std::copy(buf.data(), end, ctx.out());
66 }
67};
68}
69
70template <typename FormatContext>
71auto fmt::formatter<ObjectGuid>::format(ObjectGuid const& guid, FormatContext& ctx) const -> decltype(ctx.out())
72{
73 ctx.advance_to(ObjectGuidInfo::AppendTypeName(ctx, "GUID Full: 0x"));
74 ctx.advance_to(ObjectGuidInfo::AppendComponent<ObjectGuidInfo::padding<16>, ObjectGuidInfo::hex>(ctx, guid.GetRawValue()));
75 ctx.advance_to(ObjectGuidInfo::AppendTypeName(ctx, " Type: "));
76 ctx.advance_to(ObjectGuidInfo::AppendTypeName(ctx, guid.GetTypeName()));
77 if (uint32 entry = guid.GetEntry())
78 {
79 ctx.advance_to(ObjectGuidInfo::AppendTypeName(ctx, guid.IsPet() ? " Pet number: " : " Entry: "));
80 ctx.advance_to(ObjectGuidInfo::AppendComponent<ObjectGuidInfo::no_padding, ObjectGuidInfo::dec>(ctx, entry));
81 }
82 ctx.advance_to(ObjectGuidInfo::AppendTypeName(ctx, " Low: "));
83 ctx.advance_to(ObjectGuidInfo::AppendComponent<ObjectGuidInfo::no_padding, ObjectGuidInfo::dec>(ctx, guid.GetCounter()));
84 return ctx.out();
85}
86
87template TC_GAME_API fmt::appender fmt::formatter<ObjectGuid>::format<fmt::format_context>(ObjectGuid const&, format_context&) const;
88
89std::string_view ObjectGuid::GetTypeName(HighGuid high)
90{
91 switch (high)
92 {
93 case HighGuid::Item: return "Item";
94 case HighGuid::Player: return "Player";
95 case HighGuid::GameObject: return "Gameobject";
96 case HighGuid::Transport: return "Transport";
97 case HighGuid::Unit: return "Creature";
98 case HighGuid::Pet: return "Pet";
99 case HighGuid::Vehicle: return "Vehicle";
100 case HighGuid::DynamicObject: return "DynObject";
101 case HighGuid::Corpse: return "Corpse";
102 case HighGuid::Mo_Transport: return "MoTransport";
103 case HighGuid::Instance: return "InstanceID";
104 case HighGuid::Group: return "Group";
105 default:
106 return "<unknown>";
107 }
108}
109
110std::string ObjectGuid::ToString() const
111{
112 return Trinity::StringFormat("{}", *this);
113}
114
115std::string ObjectGuid::ToHexString() const
116{
117 return Trinity::StringFormat("0x{:016X}", _guid);
118}
119
121{
122 _packedSize = 1;
123 uint64 raw = guid.GetRawValue();
124 for (uint8 i = 0; i < 8; ++i)
125 {
126 uint8 byte = (raw >> (i * 8)) & 0xFF;
127 _packedGuid[_packedSize] = byte;
128 if (byte)
129 {
130 _packedGuid[0] |= uint8(1 << i);
131 ++_packedSize;
132 }
133 }
134}
135
137{
138 buf << uint64(guid.GetRawValue());
139 return buf;
140}
141
143{
144 guid.SetRawValue(buf.read<uint64>());
145 return buf;
146}
147
149{
150 buf.append(guid._packedGuid.data(), guid._packedSize);
151 return buf;
152}
153
155{
156 buf.appendPackGUID(guid.Guid.GetRawValue());
157 return buf;
158}
159
161{
162 buf.readPackGUID(reinterpret_cast<uint64&>(guid.Guid));
163 return buf;
164}
165
176
178{
179 TC_LOG_ERROR("misc", "{} guid overflow!! Can't continue, shutting down server. ", ObjectGuid::GetTypeName(_high));
181}
182
184{
185 if (!sWorld->IsGuidAlert() && _nextGuid > sWorld->getIntConfig(CONFIG_RESPAWN_GUIDALERTLEVEL))
186 sWorld->TriggerGuidAlert();
187 else if (!sWorld->IsGuidWarning() && _nextGuid > sWorld->getIntConfig(CONFIG_RESPAWN_GUIDWARNLEVEL))
188 sWorld->TriggerGuidWarning();
189}
#define TC_GAME_API
Definition Define.h:114
uint8_t uint8
Definition Define.h:135
int32_t int32
Definition Define.h:129
uint64_t uint64
Definition Define.h:132
uint32_t uint32
Definition Define.h:133
#define ASSERT
Definition Errors.h:68
#define TC_LOG_ERROR(filterType__,...)
Definition Log.h:165
ByteBuffer & operator<<(ByteBuffer &buf, ObjectGuid const &guid)
ByteBuffer & operator>>(ByteBuffer &buf, ObjectGuid &guid)
HighGuid
Definition ObjectGuid.h:63
struct CharToUpper charToUpper
void appendPackGUID(uint64 guid)
Definition ByteBuffer.h:464
void append(T value)
Definition ByteBuffer.h:129
void readPackGUID(uint64 &guid)
Definition ByteBuffer.h:369
ObjectGuid::LowType _nextGuid
Definition ObjectGuid.h:299
ObjectGuid::LowType Generate()
static ObjectGuid const Empty
Definition ObjectGuid.h:140
void SetRawValue(uint64 guid)
Definition ObjectGuid.h:149
std::string ToHexString() const
uint64 GetRawValue() const
Definition ObjectGuid.h:148
std::string ToString() const
uint64 _guid
Definition ObjectGuid.h:256
std::string_view GetTypeName() const
Definition ObjectGuid.h:219
LowType GetMaxCounter() const
Definition ObjectGuid.h:170
uint32 LowType
Definition ObjectGuid.h:142
std::array< uint8, PACKED_GUID_MIN_BUFFER_SIZE > _packedGuid
Definition ObjectGuid.h:282
uint8 _packedSize
Definition ObjectGuid.h:281
void Set(ObjectGuid const &guid)
static void StopNow(uint8 exitcode)
Definition World.h:673
#define sWorld
Definition World.h:900
@ CONFIG_RESPAWN_GUIDALERTLEVEL
Definition World.h:397
@ CONFIG_RESPAWN_GUIDWARNLEVEL
Definition World.h:396
@ ERROR_EXIT_CODE
Definition World.h:64
std::string StringFormat(FormatString< Args... > fmt, Args &&... args)
Default TC string format function.
ObjectGuid & Guid
Definition ObjectGuid.h:128
ObjectGuid const & Guid
Definition ObjectGuid.h:134