Skip to content

Commit c72de2f

Browse files
committed
Core/Objects: Use span/array instead of vector for raw ObjectGuid manipulations
1 parent 27860c3 commit c72de2f

File tree

5 files changed

+47
-27
lines changed

5 files changed

+47
-27
lines changed

src/server/game/Entities/Object/ObjectGuid.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -797,17 +797,17 @@ std::size_t ObjectGuid::GetHash() const
797797
return hashVal;
798798
}
799799

800-
std::vector<uint8> ObjectGuid::GetRawValue() const
800+
std::array<uint8, 16> ObjectGuid::GetRawValue() const
801801
{
802-
std::vector<uint8> raw(16);
803-
memcpy(raw.data(), this, sizeof(*this));
802+
std::array<uint8, 16> raw;
803+
memcpy(raw.data(), _data.data(), BytesSize);
804804
return raw;
805805
}
806806

807-
void ObjectGuid::SetRawValue(std::vector<uint8> const& guid)
807+
void ObjectGuid::SetRawValue(std::span<uint8 const> rawBytes)
808808
{
809-
ASSERT(guid.size() == sizeof(*this));
810-
memcpy(this, guid.data(), sizeof(*this));
809+
ASSERT(rawBytes.size() == BytesSize, SZFMTD " == " SZFMTD, rawBytes.size(), BytesSize);
810+
memcpy(_data.data(), rawBytes.data(), BytesSize);
811811
}
812812

813813
static inline uint32 GetRealmIdForObjectGuid(uint32 realmId)

src/server/game/Entities/Object/ObjectGuid.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <functional>
2626
#include <list>
2727
#include <set>
28+
#include <span>
2829
#include <stdexcept>
2930
#include <string>
3031
#include <type_traits>
@@ -263,8 +264,6 @@ class TC_GAME_API ObjectGuidFactory
263264
static ObjectGuid CreateLMMLobby(uint32 realmId, uint32 arg2, uint8 arg3, uint8 arg4, uint64 counter);
264265
};
265266

266-
#pragma pack(push, 1)
267-
268267
class TC_GAME_API ObjectGuid
269268
{
270269
friend class ObjectGuidFactory;
@@ -277,13 +276,15 @@ class TC_GAME_API ObjectGuid
277276
static ObjectGuid const FromStringFailed;
278277
static ObjectGuid const TradeItem;
279278

279+
static constexpr std::size_t BytesSize = 16;
280+
280281
using LowType = uint64;
281282

282283
ObjectGuid() = default;
283284

284285
uint64 GetRawValue(std::size_t i) const { return _data[i]; }
285-
std::vector<uint8> GetRawValue() const;
286-
void SetRawValue(std::vector<uint8> const& guid);
286+
std::array<uint8, 16> GetRawValue() const;
287+
void SetRawValue(std::span<uint8 const> rawBytes);
287288
void SetRawValue(uint64 high, uint64 low) { _data[0] = low; _data[1] = high; }
288289
void Clear() { _data = { }; }
289290

@@ -387,8 +388,6 @@ class TC_GAME_API ObjectGuid
387388
std::array<uint64, 2> _data = { };
388389
};
389390

390-
#pragma pack(pop)
391-
392391
// Some Shared defines
393392
using GuidSet = std::set<ObjectGuid>;
394393
using GuidList = std::list<ObjectGuid>;

src/server/game/Entities/Pet/Pet.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,11 @@ void Pet::_LoadAuras(PreparedQueryResult auraResult, PreparedQueryResult effectR
11891189
uint32 effectIndex = fields[3].GetUInt8();
11901190
if (effectIndex < MAX_SPELL_EFFECTS)
11911191
{
1192-
casterGuid.SetRawValue(fields[0].GetBinary());
1192+
std::span<uint8 const> rawGuidBytes = fields[0].GetBinaryView();
1193+
if (rawGuidBytes.size() != ObjectGuid::BytesSize)
1194+
continue;
1195+
1196+
casterGuid.SetRawValue(rawGuidBytes);
11931197
if (casterGuid.IsEmpty())
11941198
casterGuid = GetGUID();
11951199

@@ -1211,7 +1215,11 @@ void Pet::_LoadAuras(PreparedQueryResult auraResult, PreparedQueryResult effectR
12111215
{
12121216
Field* fields = auraResult->Fetch();
12131217
// NULL guid stored - pet is the caster of the spell - see Pet::_SaveAuras
1214-
casterGuid.SetRawValue(fields[0].GetBinary());
1218+
std::span<uint8 const> rawGuidBytes = fields[0].GetBinaryView();
1219+
if (rawGuidBytes.size() != ObjectGuid::BytesSize)
1220+
continue;
1221+
1222+
casterGuid.SetRawValue(rawGuidBytes);
12151223
if (casterGuid.IsEmpty())
12161224
casterGuid = GetGUID();
12171225

src/server/game/Entities/Player/Player.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18733,8 +18733,17 @@ void Player::_LoadAuras(PreparedQueryResult auraResult, PreparedQueryResult effe
1873318733
uint32 effectIndex = fields[4].GetUInt8();
1873418734
if (effectIndex < MAX_SPELL_EFFECTS)
1873518735
{
18736-
casterGuid.SetRawValue(fields[0].GetBinary());
18737-
itemGuid.SetRawValue(fields[1].GetBinary());
18736+
std::span<uint8 const> rawGuidBytes = fields[0].GetBinaryView();
18737+
if (rawGuidBytes.size() != ObjectGuid::BytesSize)
18738+
continue;
18739+
18740+
casterGuid.SetRawValue(rawGuidBytes);
18741+
18742+
rawGuidBytes = fields[1].GetBinaryView();
18743+
if (rawGuidBytes.size() != ObjectGuid::BytesSize)
18744+
continue;
18745+
18746+
itemGuid.SetRawValue(rawGuidBytes);
1873818747
AuraKey key{ casterGuid, itemGuid, fields[2].GetUInt32(), fields[3].GetUInt32() };
1873918748
AuraLoadEffectInfo& info = effectInfo[key];
1874018749
info.Amounts[effectIndex] = fields[5].GetInt32();
@@ -18753,8 +18762,17 @@ void Player::_LoadAuras(PreparedQueryResult auraResult, PreparedQueryResult effe
1875318762
do
1875418763
{
1875518764
Field* fields = auraResult->Fetch();
18756-
casterGuid.SetRawValue(fields[0].GetBinary());
18757-
itemGuid.SetRawValue(fields[1].GetBinary());
18765+
std::span<uint8 const> rawGuidBytes = fields[0].GetBinaryView();
18766+
if (rawGuidBytes.size() != ObjectGuid::BytesSize)
18767+
continue;
18768+
18769+
casterGuid.SetRawValue(rawGuidBytes);
18770+
18771+
rawGuidBytes = fields[1].GetBinaryView();
18772+
if (rawGuidBytes.size() != ObjectGuid::BytesSize)
18773+
continue;
18774+
18775+
itemGuid.SetRawValue(rawGuidBytes);
1875818776
AuraKey key{ casterGuid, itemGuid, fields[2].GetUInt32(), fields[3].GetUInt32() };
1875918777
uint32 recalculateMask = fields[4].GetUInt32();
1876018778
Difficulty difficulty = Difficulty(fields[5].GetUInt8());

src/server/game/Groups/Group.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,8 @@ bool Group::Create(Player* leader)
185185
stmt->setUInt8(index++, uint8(m_lootMethod));
186186
stmt->setUInt64(index++, m_looterGuid.GetCounter());
187187
stmt->setUInt8(index++, uint8(m_lootThreshold));
188-
stmt->setBinary(index++, m_targetIcons[0].GetRawValue());
189-
stmt->setBinary(index++, m_targetIcons[1].GetRawValue());
190-
stmt->setBinary(index++, m_targetIcons[2].GetRawValue());
191-
stmt->setBinary(index++, m_targetIcons[3].GetRawValue());
192-
stmt->setBinary(index++, m_targetIcons[4].GetRawValue());
193-
stmt->setBinary(index++, m_targetIcons[5].GetRawValue());
194-
stmt->setBinary(index++, m_targetIcons[6].GetRawValue());
195-
stmt->setBinary(index++, m_targetIcons[7].GetRawValue());
188+
for (uint8 i = 0; i < TARGET_ICONS_COUNT; ++i)
189+
stmt->setBinary(index++, m_targetIcons[i].GetRawValue());
196190
stmt->setUInt16(index++, m_groupFlags);
197191
stmt->setUInt32(index++, uint8(m_dungeonDifficulty));
198192
stmt->setUInt32(index++, uint8(m_raidDifficulty));
@@ -232,7 +226,8 @@ void Group::LoadGroupFromDB(Field* fields)
232226
m_lootThreshold = ItemQualities(fields[3].GetUInt8());
233227

234228
for (uint8 i = 0; i < TARGET_ICONS_COUNT; ++i)
235-
m_targetIcons[i].SetRawValue(fields[4 + i].GetBinary());
229+
if (std::span<uint8 const> rawGuidBytes = fields[4 + i].GetBinaryView(); rawGuidBytes.size() == ObjectGuid::BytesSize)
230+
m_targetIcons[i].SetRawValue(rawGuidBytes);
236231

237232
m_groupFlags = GroupFlags(fields[12].GetUInt16());
238233
if (m_groupFlags & GROUP_FLAG_RAID)

0 commit comments

Comments
 (0)