Core/Players: Prevent creating characters with duplicate name by reserving name early in the process

Closes #21656
Closes #21809
This commit is contained in:
Shauren
2018-04-10 20:57:34 +02:00
parent 4fa4b6df56
commit fdb2b90685
4 changed files with 26 additions and 1 deletions

View File

@@ -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);

View File

@@ -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;

View File

@@ -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);

View File

@@ -220,6 +220,7 @@ class CharacterCreateInfo
/// Server side data
uint8 CharCount = 0;
std::shared_ptr<std::string const> NameToken;
};
struct CharacterRenameInfo