mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-15 23:20:36 +01:00
Core/Players: Prevent creating characters with duplicate name by reserving name early in the process
Closes #21656 Closes #21809
This commit is contained in:
@@ -29,6 +29,7 @@ namespace
|
||||
{
|
||||
std::unordered_map<ObjectGuid, CharacterCacheEntry> _characterCacheStore;
|
||||
std::unordered_map<std::string, CharacterCacheEntry*> _characterCacheByNameStore;
|
||||
std::unordered_set<std::string> _characterCreationNameStore;
|
||||
}
|
||||
|
||||
CharacterCache::CharacterCache()
|
||||
@@ -201,6 +202,21 @@ CharacterCacheEntry const* CharacterCache::GetCharacterCacheByName(std::string c
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::shared_ptr<std::string const> CharacterCache::TryCreateCharacterWithName(std::string const& name) const
|
||||
{
|
||||
auto itr = _characterCacheByNameStore.find(name);
|
||||
if (itr != _characterCacheByNameStore.end())
|
||||
return nullptr;
|
||||
|
||||
auto insertResult = _characterCreationNameStore.insert(name);
|
||||
if (!insertResult.second)
|
||||
return nullptr;
|
||||
|
||||
// shared_ptr with custom deleter that erases its held value from _characterCreationNameStore instead of deleting it (points to value inside the container)
|
||||
return std::shared_ptr<std::string const>(&(*insertResult.first),
|
||||
[this](std::string const* storedName) { _characterCreationNameStore.erase(*storedName); });
|
||||
}
|
||||
|
||||
ObjectGuid CharacterCache::GetCharacterGuidByName(std::string const& name) const
|
||||
{
|
||||
auto itr = _characterCacheByNameStore.find(name);
|
||||
|
||||
@@ -55,7 +55,7 @@ class TC_GAME_API CharacterCache
|
||||
bool HasCharacterCacheEntry(ObjectGuid const& guid) const;
|
||||
CharacterCacheEntry const* GetCharacterCacheByGuid(ObjectGuid const& guid) const;
|
||||
CharacterCacheEntry const* GetCharacterCacheByName(std::string const& name) const;
|
||||
|
||||
std::shared_ptr<std::string const> TryCreateCharacterWithName(std::string const& name) const;
|
||||
ObjectGuid GetCharacterGuidByName(std::string const& name) const;
|
||||
bool GetCharacterNameByGuid(ObjectGuid guid, std::string& name) const;
|
||||
uint32 GetCharacterTeamByGuid(ObjectGuid guid) const;
|
||||
|
||||
@@ -397,6 +397,14 @@ void WorldSession::HandleCharCreateOpcode(WorldPacket& recvData)
|
||||
}
|
||||
}
|
||||
|
||||
// Reserve the name for the duration of callback chain
|
||||
createInfo->NameToken = sCharacterCache->TryCreateCharacterWithName(createInfo->Name);
|
||||
if (!createInfo->NameToken)
|
||||
{
|
||||
SendCharCreate(CHAR_CREATE_NAME_IN_USE);
|
||||
return;
|
||||
}
|
||||
|
||||
PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_SEL_CHECK_NAME);
|
||||
stmt->setString(0, createInfo->Name);
|
||||
|
||||
|
||||
@@ -220,6 +220,7 @@ class CharacterCreateInfo
|
||||
|
||||
/// Server side data
|
||||
uint8 CharCount = 0;
|
||||
std::shared_ptr<std::string const> NameToken;
|
||||
};
|
||||
|
||||
struct CharacterRenameInfo
|
||||
|
||||
Reference in New Issue
Block a user