fix(Core/World): Crash on SendWorldText(Optional) (#19531)

* fix(Core/World): Crash on SendWorldText(Optional)

* WorldText should be sent to all sessions

* now it works
This commit is contained in:
Kitzunu
2024-07-30 22:53:11 +02:00
committed by GitHub
parent 97ee20a7aa
commit a1e9dfce41
2 changed files with 67 additions and 40 deletions

View File

@@ -102,50 +102,36 @@ bool ChatHandler::HasLowerSecurityAccount(WorldSession* target, uint32 target_ac
void ChatHandler::SendWorldText(std::string_view str)
{
std::vector<std::string_view> lines = Acore::Tokenize(str, '\n', true);
for (SessionMap::const_iterator itr = sWorld->GetAllSessions().begin(); itr != sWorld->GetAllSessions().end(); ++itr)
Player* player = m_session->GetPlayer();
if (!player || !player->IsInWorld())
return;
for (std::string_view line : lines)
{
WorldSession* session = itr->second;
if (!session)
continue;
// Player should be in world
Player* player = session->GetPlayer();
if (!player || !player->IsInWorld())
continue;
for (std::string_view line : lines)
{
WorldPacket data;
ChatHandler::BuildChatPacket(data, CHAT_MSG_SYSTEM, LANG_UNIVERSAL, nullptr, nullptr, line);
player->SendDirectMessage(&data);
}
WorldPacket data;
ChatHandler::BuildChatPacket(data, CHAT_MSG_SYSTEM, LANG_UNIVERSAL, nullptr, nullptr, line);
player->SendDirectMessage(&data);
}
}
void ChatHandler::SendWorldTextOptional(std::string_view str, uint32 flag)
{
std::vector<std::string_view> lines = Acore::Tokenize(str, '\n', true);
for (SessionMap::const_iterator itr = sWorld->GetAllSessions().begin(); itr != sWorld->GetAllSessions().end(); ++itr)
Player* player = m_session->GetPlayer();
if (!player || !player->IsInWorld())
return;
if (sWorld->getBoolConfig(CONFIG_PLAYER_SETTINGS_ENABLED))
if (player->GetPlayerSetting(AzerothcorePSSource, SETTING_ANNOUNCER_FLAGS).HasFlag(flag))
return;
for (std::string_view line : lines)
{
WorldSession* session = itr->second;
if (!session)
continue;
// Player should be in world
Player* player = session->GetPlayer();
if (!player || !player->IsInWorld())
continue;
if (sWorld->getBoolConfig(CONFIG_PLAYER_SETTINGS_ENABLED))
if (player->GetPlayerSetting(AzerothcorePSSource, SETTING_ANNOUNCER_FLAGS).HasFlag(flag))
continue;
for (std::string_view line : lines)
{
WorldPacket data;
ChatHandler::BuildChatPacket(data, CHAT_MSG_SYSTEM, LANG_UNIVERSAL, nullptr, nullptr, line);
player->SendDirectMessage(&data);
}
WorldPacket data;
ChatHandler::BuildChatPacket(data, CHAT_MSG_SYSTEM, LANG_UNIVERSAL, nullptr, nullptr, line);
player->SendDirectMessage(&data);
}
}

View File

@@ -20,6 +20,7 @@
#include "ChatCommand.h"
#include "Errors.h"
#include "Player.h"
#include "SharedDefines.h"
#include "WorldSession.h"
#include <vector>
@@ -54,24 +55,64 @@ public:
template<typename... Args>
void SendWorldText(uint32 strId, Args&&... args)
{
SendWorldText(Acore::StringFormatFmt(GetAcoreString(strId), std::forward<Args>(args)...));
// WorldText should be sent to all sessions
SessionMap::const_iterator itr;
for (itr = sWorld->GetAllSessions().begin(); itr != sWorld->GetAllSessions().end(); ++itr)
{
Player* player = itr->second->GetPlayer();
if (player && player->IsInWorld())
{
m_session = player->GetSession();
SendWorldText(Acore::StringFormatFmt(GetAcoreString(strId), std::forward<Args>(args)...));
}
}
}
template<typename... Args>
void SendWorldText(char const* fmt, Args&&... args)
{
SendWorldText(Acore::StringFormatFmt(fmt, std::forward<Args>(args)...));
// WorldTextOptional should be sent to all sessions
SessionMap::const_iterator itr;
for (itr = sWorld->GetAllSessions().begin(); itr != sWorld->GetAllSessions().end(); ++itr)
{
Player* player = itr->second->GetPlayer();
if (player && player->IsInWorld())
{
m_session = player->GetSession();
SendWorldText(Acore::StringFormatFmt(fmt, std::forward<Args>(args)...));
}
}
}
void SendWorldTextOptional(std::string_view str, uint32 flag);
template<typename... Args>
void SendWorldTextOptional(uint32 strId, uint32 flag, Args&&... args)
{
SendWorldTextOptional(Acore::StringFormatFmt(GetAcoreString(strId), std::forward<Args>(args)...), flag);
// WorldTextOptional should be sent to all sessions
SessionMap::const_iterator itr;
for (itr = sWorld->GetAllSessions().begin(); itr != sWorld->GetAllSessions().end(); ++itr)
{
Player* player = itr->second->GetPlayer();
if (player && player->IsInWorld())
{
m_session = player->GetSession();
SendWorldTextOptional(Acore::StringFormatFmt(GetAcoreString(strId), std::forward<Args>(args)...), flag);
}
}
}
template<typename... Args>
void SendWorldTextOptional(char const* fmt, uint32 flag, Args&&... args)
{
SendWorldTextOptional(Acore::StringFormatFmt(fmt, std::forward<Args>(args)...), flag);
// WorldTextOptional should be sent to all sessions
SessionMap::const_iterator itr;
for (itr = sWorld->GetAllSessions().begin(); itr != sWorld->GetAllSessions().end(); ++itr)
{
Player* player = itr->second->GetPlayer();
if (player && player->IsInWorld())
{
m_session = player->GetSession();
SendWorldTextOptional(Acore::StringFormatFmt(fmt, std::forward<Args>(args)...), flag);
}
}
}
// function with different implementation for chat/console