Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/server/bnetserver/REST/LoginRESTService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ void LoginRESTService::MigrateLegacyPasswordHashes() const
LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_BNET_LOGON);
stmt->setInt8(0, AsUnderlyingType(SrpVersion::v1));
stmt->setBinary(1, salt);
stmt->setBinary(2, verifier);
stmt->setBinary(2, std::move(verifier));
stmt->setUInt32(3, id);
tx->Append(stmt);

Expand Down
27 changes: 13 additions & 14 deletions src/server/database/Database/Field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,26 +124,20 @@ char const* Field::GetCString() const

std::string Field::GetString() const
{
if (!_value)
return "";

char const* string = GetCString();
if (!string)
return "";
std::string result;
if (char const* string = GetCString())
result.assign(string, _length);

return std::string(string, _length);
return result;
}

std::string_view Field::GetStringView() const
{
if (!_value)
return {};

char const* const string = GetCString();
if (!string)
return {};
std::string_view result;
if (char const* const string = GetCString())
result = { string, _length };

return { string, _length };
return result;
}

std::vector<uint8> Field::GetBinary() const
Expand All @@ -157,6 +151,11 @@ std::vector<uint8> Field::GetBinary() const
return result;
}

std::span<uint8 const> Field::GetBinaryView() const
{
return { reinterpret_cast<uint8 const*>(_value), _length };
}

void Field::GetBinarySizeChecked(uint8* buf, size_t length) const
{
ASSERT(_value && (_length == length), "Expected %zu-byte binary blob, got %sdata (%u bytes) instead", length, _value ? "" : "no ", _length);
Expand Down
2 changes: 2 additions & 0 deletions src/server/database/Database/Field.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "Define.h"
#include "Duration.h"
#include <array>
#include <span>
#include <string>
#include <string_view>
#include <vector>
Expand Down Expand Up @@ -117,6 +118,7 @@ class TC_DATABASE_API Field
std::string GetString() const;
std::string_view GetStringView() const;
std::vector<uint8> GetBinary() const;
std::span<uint8 const> GetBinaryView() const;
template <size_t S>
std::array<uint8, S> GetBinary() const
{
Expand Down
16 changes: 11 additions & 5 deletions src/server/database/Database/PreparedStatement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,28 @@ void PreparedStatementBase::setDate(uint8 index, SystemTimePoint value)
statement_data[index].data = value;
}

void PreparedStatementBase::setString(uint8 index, std::string const& value)
void PreparedStatementBase::setString(uint8 index, std::string&& value)
{
ASSERT(index < statement_data.size());
statement_data[index].data = value;
statement_data[index].data = std::move(value);
}

void PreparedStatementBase::setStringView(uint8 index, std::string_view value)
void PreparedStatementBase::setString(uint8 index, std::string_view value)
{
ASSERT(index < statement_data.size());
statement_data[index].data.emplace<std::string>(value);
}

void PreparedStatementBase::setBinary(uint8 index, std::vector<uint8> const& value)
void PreparedStatementBase::setBinary(uint8 index, std::vector<uint8>&& value)
{
ASSERT(index < statement_data.size());
statement_data[index].data = value;
statement_data[index].data = std::move(value);
}

void PreparedStatementBase::setBinary(uint8 index, std::span<uint8 const> value)
{
ASSERT(index < statement_data.size());
statement_data[index].data.emplace<std::vector<uint8>>(value.begin(), value.end());
}

void PreparedStatementBase::setNull(uint8 index)
Expand Down
15 changes: 5 additions & 10 deletions src/server/database/Database/PreparedStatement.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "DatabaseEnvFwd.h"
#include "Define.h"
#include "Duration.h"
#include <array>
#include <span>
#include <string>
#include <variant>
#include <vector>
Expand Down Expand Up @@ -82,15 +82,10 @@ class TC_DATABASE_API PreparedStatementBase
void setFloat(uint8 index, float value);
void setDouble(uint8 index, double value);
void setDate(uint8 index, SystemTimePoint value);
void setString(uint8 index, std::string const& value);
void setStringView(uint8 index, std::string_view value);
void setBinary(uint8 index, std::vector<uint8> const& value);
template <size_t Size>
void setBinary(const uint8 index, std::array<uint8, Size> const& value)
{
std::vector<uint8> vec(value.begin(), value.end());
setBinary(index, vec);
}
void setString(uint8 index, std::string&& value);
void setString(uint8 index, std::string_view value);
void setBinary(uint8 index, std::vector<uint8>&& value);
void setBinary(uint8 index, std::span<uint8 const> value);

uint32 GetIndex() const { return m_index; }
std::vector<PreparedStatementData> const& GetParameters() const { return statement_data; }
Expand Down
8 changes: 4 additions & 4 deletions src/server/game/Accounts/AccountMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ AccountOpResult AccountMgr::CreateAccount(std::string username, std::string pass
stmt->setString(0, username);
auto [salt, verifier] = Trinity::Crypto::SRP6::MakeRegistrationData<AccountSRP6>(username, password);
stmt->setBinary(1, salt);
stmt->setBinary(2, verifier);
stmt->setBinary(2, std::move(verifier));
stmt->setString(3, email);
stmt->setString(4, email);

Expand Down Expand Up @@ -188,7 +188,7 @@ AccountOpResult AccountMgr::ChangeUsername(uint32 accountId, std::string newUser
auto [salt, verifier] = Trinity::Crypto::SRP6::MakeRegistrationData<AccountSRP6>(newUsername, newPassword);
stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_LOGON);
stmt->setBinary(0, salt);
stmt->setBinary(1, verifier);
stmt->setBinary(1, std::move(verifier));
stmt->setUInt32(2, accountId);
LoginDatabase.Execute(stmt);

Expand Down Expand Up @@ -217,7 +217,7 @@ AccountOpResult AccountMgr::ChangePassword(uint32 accountId, std::string newPass

LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_LOGON);
stmt->setBinary(0, salt);
stmt->setBinary(1, verifier);
stmt->setBinary(1, std::move(verifier));
stmt->setUInt32(2, accountId);
LoginDatabase.Execute(stmt);

Expand Down Expand Up @@ -288,7 +288,7 @@ AccountOpResult AccountMgr::ChangeRegEmail(uint32 accountId, std::string newEmai
uint32 AccountMgr::GetId(std::string_view username)
{
LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_GET_ACCOUNT_ID_BY_USERNAME);
stmt->setStringView(0, username);
stmt->setString(0, username);
PreparedQueryResult result = LoginDatabase.Query(stmt);

return (result) ? (*result)[0].GetUInt32() : 0;
Expand Down
6 changes: 3 additions & 3 deletions src/server/game/Accounts/BattlenetAccountMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ AccountOpResult Battlenet::AccountMgr::CreateBattlenetAccount(std::string email,
stmt->setString(0, email);
stmt->setInt8(1, AsUnderlyingType(SrpVersion::v2));
stmt->setBinary(2, salt);
stmt->setBinary(3, verifier);
stmt->setBinary(3, std::move(verifier));
LoginDatabase.DirectExecute(stmt);

uint32 newAccountId = GetId(email);
Expand Down Expand Up @@ -83,7 +83,7 @@ AccountOpResult Battlenet::AccountMgr::ChangePassword(uint32 accountId, std::str
LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_BNET_LOGON);
stmt->setInt8(0, AsUnderlyingType(SrpVersion::v2));
stmt->setBinary(1, salt);
stmt->setBinary(2, verifier);
stmt->setBinary(2, std::move(verifier));
stmt->setUInt32(3, accountId);
LoginDatabase.Execute(stmt);

Expand Down Expand Up @@ -160,7 +160,7 @@ AccountOpResult Battlenet::AccountMgr::UnlinkGameAccount(std::string_view gameAc
uint32 Battlenet::AccountMgr::GetId(std::string_view username)
{
LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_BNET_ACCOUNT_ID_BY_EMAIL);
stmt->setStringView(0, username);
stmt->setString(0, username);
if (PreparedQueryResult result = LoginDatabase.Query(stmt))
return (*result)[0].GetUInt32();

Expand Down
6 changes: 4 additions & 2 deletions src/server/game/Entities/Item/Item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,8 @@ void Item::SaveToDB(CharacterDatabaseTransaction trans)

if (m_itemData->Gems.size())
{
using namespace std::string_view_literals;

stmt = CharacterDatabase.GetPreparedStatement(CHAR_INS_ITEM_INSTANCE_GEMS);
stmt->setUInt64(0, GetGUID().GetCounter());
uint32 i = 0;
Expand All @@ -657,7 +659,7 @@ void Item::SaveToDB(CharacterDatabaseTransaction trans)
else
{
stmt->setUInt32(1 + i * gemFields, 0);
stmt->setString(2 + i * gemFields, "");
stmt->setString(2 + i * gemFields, ""sv);
stmt->setUInt8(3 + i * gemFields, 0);
stmt->setUInt32(4 + i * gemFields, 0);
}
Expand All @@ -666,7 +668,7 @@ void Item::SaveToDB(CharacterDatabaseTransaction trans)
for (; i < MAX_GEM_SOCKETS; ++i)
{
stmt->setUInt32(1 + i * gemFields, 0);
stmt->setString(2 + i * gemFields, "");
stmt->setString(2 + i * gemFields, ""sv);
stmt->setUInt8(3 + i * gemFields, 0);
stmt->setUInt32(4 + i * gemFields, 0);
}
Expand Down
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
38 changes: 29 additions & 9 deletions src/server/game/Entities/Player/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -980,11 +980,13 @@ void Player::Update(uint32 p_time)
// If mute expired, remove it from the DB
if (GetSession()->m_muteTime && GetSession()->m_muteTime < now)
{
using namespace std::string_view_literals;

GetSession()->m_muteTime = 0;
LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_MUTE_TIME);
stmt->setInt64(0, 0); // Set the mute time to 0
stmt->setString(1, "");
stmt->setString(2, "");
stmt->setString(1, ""sv);
stmt->setString(2, ""sv);
stmt->setUInt32(3, GetSession()->GetAccountId());
LoginDatabase.Execute(stmt);
}
Expand Down Expand Up @@ -14955,8 +14957,8 @@ void Player::AddQuest(Quest const* quest, Object* questGiver)
CharacterDatabasePreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_INS_QUEST_TRACK);
stmt->setUInt32(0, quest_id);
stmt->setUInt64(1, GetGUID().GetCounter());
stmt->setString(2, GitRevision::GetHash());
stmt->setString(3, GitRevision::GetDate());
stmt->setString(2, std::string_view(GitRevision::GetHash()));
stmt->setString(3, std::string_view(GitRevision::GetDate()));

// add to Quest Tracker
CharacterDatabase.Execute(stmt);
Expand Down Expand Up @@ -18731,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 @@ -18751,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 Expand Up @@ -28158,7 +28178,7 @@ void Player::_SaveTraits(CharacterDatabaseTransaction trans)
break;
}

stmt->setString(8, traitConfig->Name);
stmt->setString(8, *traitConfig->Name);
trans->Append(stmt);

for (UF::TraitEntry const& traitEntry : traitConfig->Entries)
Expand Down
Loading
Loading