Skip to content

Commit

Permalink
Core/Objects: Use span/array instead of vector for raw ObjectGuid man…
Browse files Browse the repository at this point in the history
…ipulations
  • Loading branch information
Shauren committed Jan 3, 2025
1 parent 27860c3 commit c72de2f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 27 deletions.
12 changes: 6 additions & 6 deletions src/server/game/Entities/Object/ObjectGuid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -797,17 +797,17 @@ std::size_t ObjectGuid::GetHash() const
return hashVal;
}

std::vector<uint8> ObjectGuid::GetRawValue() const
std::array<uint8, 16> ObjectGuid::GetRawValue() const
{
std::vector<uint8> raw(16);
memcpy(raw.data(), this, sizeof(*this));
std::array<uint8, 16> raw;
memcpy(raw.data(), _data.data(), BytesSize);
return raw;
}

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

static inline uint32 GetRealmIdForObjectGuid(uint32 realmId)
Expand Down
11 changes: 5 additions & 6 deletions src/server/game/Entities/Object/ObjectGuid.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <functional>
#include <list>
#include <set>
#include <span>
#include <stdexcept>
#include <string>
#include <type_traits>
Expand Down Expand Up @@ -263,8 +264,6 @@ class TC_GAME_API ObjectGuidFactory
static ObjectGuid CreateLMMLobby(uint32 realmId, uint32 arg2, uint8 arg3, uint8 arg4, uint64 counter);
};

#pragma pack(push, 1)

class TC_GAME_API ObjectGuid
{
friend class ObjectGuidFactory;
Expand All @@ -277,13 +276,15 @@ class TC_GAME_API ObjectGuid
static ObjectGuid const FromStringFailed;
static ObjectGuid const TradeItem;

static constexpr std::size_t BytesSize = 16;

using LowType = uint64;

ObjectGuid() = default;

uint64 GetRawValue(std::size_t i) const { return _data[i]; }
std::vector<uint8> GetRawValue() const;
void SetRawValue(std::vector<uint8> const& guid);
std::array<uint8, 16> GetRawValue() const;
void SetRawValue(std::span<uint8 const> rawBytes);
void SetRawValue(uint64 high, uint64 low) { _data[0] = low; _data[1] = high; }
void Clear() { _data = { }; }

Expand Down Expand Up @@ -387,8 +388,6 @@ class TC_GAME_API ObjectGuid
std::array<uint64, 2> _data = { };
};

#pragma pack(pop)

// Some Shared defines
using GuidSet = std::set<ObjectGuid>;
using GuidList = std::list<ObjectGuid>;
Expand Down
12 changes: 10 additions & 2 deletions src/server/game/Entities/Pet/Pet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,11 @@ void Pet::_LoadAuras(PreparedQueryResult auraResult, PreparedQueryResult effectR
uint32 effectIndex = fields[3].GetUInt8();
if (effectIndex < MAX_SPELL_EFFECTS)
{
casterGuid.SetRawValue(fields[0].GetBinary());
std::span<uint8 const> rawGuidBytes = fields[0].GetBinaryView();
if (rawGuidBytes.size() != ObjectGuid::BytesSize)
continue;

casterGuid.SetRawValue(rawGuidBytes);
if (casterGuid.IsEmpty())
casterGuid = GetGUID();

Expand All @@ -1211,7 +1215,11 @@ void Pet::_LoadAuras(PreparedQueryResult auraResult, PreparedQueryResult effectR
{
Field* fields = auraResult->Fetch();
// NULL guid stored - pet is the caster of the spell - see Pet::_SaveAuras
casterGuid.SetRawValue(fields[0].GetBinary());
std::span<uint8 const> rawGuidBytes = fields[0].GetBinaryView();
if (rawGuidBytes.size() != ObjectGuid::BytesSize)
continue;

casterGuid.SetRawValue(rawGuidBytes);
if (casterGuid.IsEmpty())
casterGuid = GetGUID();

Expand Down
26 changes: 22 additions & 4 deletions src/server/game/Entities/Player/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18733,8 +18733,17 @@ void Player::_LoadAuras(PreparedQueryResult auraResult, PreparedQueryResult effe
uint32 effectIndex = fields[4].GetUInt8();
if (effectIndex < MAX_SPELL_EFFECTS)
{
casterGuid.SetRawValue(fields[0].GetBinary());
itemGuid.SetRawValue(fields[1].GetBinary());
std::span<uint8 const> rawGuidBytes = fields[0].GetBinaryView();
if (rawGuidBytes.size() != ObjectGuid::BytesSize)
continue;

casterGuid.SetRawValue(rawGuidBytes);

rawGuidBytes = fields[1].GetBinaryView();
if (rawGuidBytes.size() != ObjectGuid::BytesSize)
continue;

itemGuid.SetRawValue(rawGuidBytes);
AuraKey key{ casterGuid, itemGuid, fields[2].GetUInt32(), fields[3].GetUInt32() };
AuraLoadEffectInfo& info = effectInfo[key];
info.Amounts[effectIndex] = fields[5].GetInt32();
Expand All @@ -18753,8 +18762,17 @@ void Player::_LoadAuras(PreparedQueryResult auraResult, PreparedQueryResult effe
do
{
Field* fields = auraResult->Fetch();
casterGuid.SetRawValue(fields[0].GetBinary());
itemGuid.SetRawValue(fields[1].GetBinary());
std::span<uint8 const> rawGuidBytes = fields[0].GetBinaryView();
if (rawGuidBytes.size() != ObjectGuid::BytesSize)
continue;

casterGuid.SetRawValue(rawGuidBytes);

rawGuidBytes = fields[1].GetBinaryView();
if (rawGuidBytes.size() != ObjectGuid::BytesSize)
continue;

itemGuid.SetRawValue(rawGuidBytes);
AuraKey key{ casterGuid, itemGuid, fields[2].GetUInt32(), fields[3].GetUInt32() };
uint32 recalculateMask = fields[4].GetUInt32();
Difficulty difficulty = Difficulty(fields[5].GetUInt8());
Expand Down
13 changes: 4 additions & 9 deletions src/server/game/Groups/Group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,8 @@ bool Group::Create(Player* leader)
stmt->setUInt8(index++, uint8(m_lootMethod));
stmt->setUInt64(index++, m_looterGuid.GetCounter());
stmt->setUInt8(index++, uint8(m_lootThreshold));
stmt->setBinary(index++, m_targetIcons[0].GetRawValue());
stmt->setBinary(index++, m_targetIcons[1].GetRawValue());
stmt->setBinary(index++, m_targetIcons[2].GetRawValue());
stmt->setBinary(index++, m_targetIcons[3].GetRawValue());
stmt->setBinary(index++, m_targetIcons[4].GetRawValue());
stmt->setBinary(index++, m_targetIcons[5].GetRawValue());
stmt->setBinary(index++, m_targetIcons[6].GetRawValue());
stmt->setBinary(index++, m_targetIcons[7].GetRawValue());
for (uint8 i = 0; i < TARGET_ICONS_COUNT; ++i)
stmt->setBinary(index++, m_targetIcons[i].GetRawValue());
stmt->setUInt16(index++, m_groupFlags);
stmt->setUInt32(index++, uint8(m_dungeonDifficulty));
stmt->setUInt32(index++, uint8(m_raidDifficulty));
Expand Down Expand Up @@ -232,7 +226,8 @@ void Group::LoadGroupFromDB(Field* fields)
m_lootThreshold = ItemQualities(fields[3].GetUInt8());

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

m_groupFlags = GroupFlags(fields[12].GetUInt16());
if (m_groupFlags & GROUP_FLAG_RAID)
Expand Down

0 comments on commit c72de2f

Please sign in to comment.