6351: Fix client crash when echoing client message to chat, use em dash for client messages in chat like on upstream r=def- a=Robyt3

![client-message](https://user-images.githubusercontent.com/23437060/220452193-1ce5cc5b-b79b-4632-a675-5ca70c37e7f0.png)

## Checklist

- [X] Tested the change ingame
- [X] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [ ] Written a unit test (especially base/) or added coverage to integration test
- [ ] Considered possible null pointers and out of bounds array indexing
- [ ] Changed no physics that affect existing maps
- [ ] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


Co-authored-by: Robert Müller <robytemueller@gmail.com>
This commit is contained in:
bors[bot] 2023-02-24 08:25:43 +00:00 committed by GitHub
commit 931ea6d828
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 29 deletions

View file

@ -147,7 +147,7 @@ void CChat::ConchainChatOld(IConsole::IResult *pResult, void *pUserData, IConsol
void CChat::Echo(const char *pString)
{
AddLine(-2, 0, pString);
AddLine(CLIENT_MSG, 0, pString);
}
void CChat::OnConsoleInit()
@ -156,7 +156,7 @@ void CChat::OnConsoleInit()
Console()->Register("say_team", "r[message]", CFGFLAG_CLIENT, ConSayTeam, this, "Say in team chat");
Console()->Register("chat", "s['team'|'all'] ?r[message]", CFGFLAG_CLIENT, ConChat, this, "Enable chat with all/team mode");
Console()->Register("+show_chat", "", CFGFLAG_CLIENT, ConShowChat, this, "Show chat");
Console()->Register("echo", "r[message]", CFGFLAG_CLIENT, ConEcho, this, "Echo the text in chat window");
Console()->Register("echo", "r[message]", CFGFLAG_CLIENT | CFGFLAG_STORE, ConEcho, this, "Echo the text in chat window");
Console()->Chain("cl_chat_old", ConchainChatOld, this);
}
@ -637,7 +637,7 @@ void CChat::StoreSave(const char *pText)
void CChat::AddLine(int ClientID, int Team, const char *pLine)
{
if(*pLine == 0 ||
(ClientID == -1 && !g_Config.m_ClShowChatSystem) ||
(ClientID == SERVER_MSG && !g_Config.m_ClShowChatSystem) ||
(ClientID >= 0 && (m_pClient->m_aClients[ClientID].m_aName[0] == '\0' || // unknown client
m_pClient->m_aClients[ClientID].m_ChatIgnore ||
(m_pClient->m_Snap.m_LocalClientID != ClientID && g_Config.m_ClShowChatFriends && !m_pClient->m_aClients[ClientID].m_Friend) ||
@ -698,9 +698,9 @@ void CChat::AddLine(int ClientID, int Team, const char *pLine)
ChatLogColor = color_cast<ColorRGBA>(ColorHSLA(g_Config.m_ClMessageFriendColor));
else if(pLine_->m_Team)
ChatLogColor = color_cast<ColorRGBA>(ColorHSLA(g_Config.m_ClMessageTeamColor));
else if(pLine_->m_ClientID == -1) // system
else if(pLine_->m_ClientID == SERVER_MSG)
ChatLogColor = color_cast<ColorRGBA>(ColorHSLA(g_Config.m_ClMessageSystemColor));
else if(pLine_->m_ClientID == -2) // client
else if(pLine_->m_ClientID == CLIENT_MSG)
ChatLogColor = color_cast<ColorRGBA>(ColorHSLA(g_Config.m_ClMessageClientColor));
else // regular message
ChatLogColor = color_cast<ColorRGBA>(ColorHSLA(g_Config.m_ClMessageColor));
@ -781,11 +781,16 @@ void CChat::AddLine(int ClientID, int Team, const char *pLine)
pCurrentLine->m_Highlighted = Highlighted;
if(pCurrentLine->m_ClientID < 0) // server or client message
if(pCurrentLine->m_ClientID == SERVER_MSG)
{
str_copy(pCurrentLine->m_aName, "*** ");
str_copy(pCurrentLine->m_aText, pLine);
}
else if(pCurrentLine->m_ClientID == CLIENT_MSG)
{
str_copy(pCurrentLine->m_aName, "");
str_copy(pCurrentLine->m_aText, pLine);
}
else
{
if(m_pClient->m_aClients[ClientID].m_Team == TEAM_SPECTATORS)
@ -848,7 +853,7 @@ void CChat::AddLine(int ClientID, int Team, const char *pLine)
// play sound
int64_t Now = time();
if(ClientID == -1)
if(ClientID == SERVER_MSG)
{
if(Now - m_aLastSoundPlayed[CHAT_SERVER] >= time_freq() * 3 / 10)
{
@ -859,7 +864,7 @@ void CChat::AddLine(int ClientID, int Team, const char *pLine)
}
}
}
else if(ClientID == -2) // Client message
else if(ClientID == CLIENT_MSG)
{
// No sound yet
}
@ -1063,33 +1068,24 @@ void CChat::OnPrepareLines()
// render name
ColorRGBA NameColor;
if(m_aLines[r].m_ClientID == -1) // system
{
if(m_aLines[r].m_ClientID == SERVER_MSG)
NameColor = color_cast<ColorRGBA>(ColorHSLA(g_Config.m_ClMessageSystemColor));
}
else if(m_aLines[r].m_ClientID == -2) // client
{
else if(m_aLines[r].m_ClientID == CLIENT_MSG)
NameColor = color_cast<ColorRGBA>(ColorHSLA(g_Config.m_ClMessageClientColor));
}
else if(m_aLines[r].m_Team)
{
NameColor = CalculateNameColor(ColorHSLA(g_Config.m_ClMessageTeamColor));
}
else if(m_aLines[r].m_NameColor == TEAM_RED)
NameColor = ColorRGBA(1.0f, 0.5f, 0.5f, 1.f); // red
NameColor = ColorRGBA(1.0f, 0.5f, 0.5f, 1.f);
else if(m_aLines[r].m_NameColor == TEAM_BLUE)
NameColor = ColorRGBA(0.7f, 0.7f, 1.0f, 1.f); // blue
NameColor = ColorRGBA(0.7f, 0.7f, 1.0f, 1.f);
else if(m_aLines[r].m_NameColor == TEAM_SPECTATORS)
NameColor = ColorRGBA(0.75f, 0.5f, 0.75f, 1.f); // spectator
NameColor = ColorRGBA(0.75f, 0.5f, 0.75f, 1.f);
else if(m_aLines[r].m_ClientID >= 0 && g_Config.m_ClChatTeamColors && m_pClient->m_Teams.Team(m_aLines[r].m_ClientID))
{
NameColor = color_cast<ColorRGBA>(ColorHSLA(m_pClient->m_Teams.Team(m_aLines[r].m_ClientID) / 64.0f, 1.0f, 0.75f));
}
else
NameColor = ColorRGBA(0.8f, 0.8f, 0.8f, 1.f);
TextRender()->TextColor(NameColor);
TextRender()->CreateOrAppendTextContainer(m_aLines[r].m_TextContainerIndex, &Cursor, aName);
if(m_aLines[r].m_TimesRepeated > 0)
@ -1106,13 +1102,13 @@ void CChat::OnPrepareLines()
// render line
ColorRGBA Color;
if(m_aLines[r].m_ClientID == -1) // system
if(m_aLines[r].m_ClientID == SERVER_MSG)
Color = color_cast<ColorRGBA>(ColorHSLA(g_Config.m_ClMessageSystemColor));
else if(m_aLines[r].m_ClientID == -2) // client
else if(m_aLines[r].m_ClientID == CLIENT_MSG)
Color = color_cast<ColorRGBA>(ColorHSLA(g_Config.m_ClMessageClientColor));
else if(m_aLines[r].m_Highlighted) // highlighted
else if(m_aLines[r].m_Highlighted)
Color = color_cast<ColorRGBA>(ColorHSLA(g_Config.m_ClMessageHighlightColor));
else if(m_aLines[r].m_Team) // team message
else if(m_aLines[r].m_Team)
Color = color_cast<ColorRGBA>(ColorHSLA(g_Config.m_ClMessageTeamColor));
else // regular message
Color = color_cast<ColorRGBA>(ColorHSLA(g_Config.m_ClMessageColor));

View file

@ -62,9 +62,12 @@ class CChat : public CComponent
CLine m_aLines[MAX_LINES];
int m_CurrentLine;
// chat
enum
{
// client IDs for special messages
CLIENT_MSG = -2,
SERVER_MSG = -1,
MODE_NONE = 0,
MODE_ALL,
MODE_TEAM,

View file

@ -2905,7 +2905,7 @@ void CMenus::RenderSettingsAppearance(CUIRect MainView)
Graphics()->DrawRectExt(X - RealMsgPaddingX / 2.0f, TempY - RealMsgPaddingY / 2.0f, Width + RealMsgPaddingX + RealMsgPaddingTee, RealFontSize + RealMsgPaddingY, RealBackgroundRounding, IGraphics::CORNER_ALL);
TempY += RealOffsetY;
Width = TextRender()->TextWidth(RealFontSize, "*** Echo command executed", -1, -1);
Width = TextRender()->TextWidth(RealFontSize, " Echo command executed", -1, -1);
Graphics()->DrawRectExt(X - RealMsgPaddingX / 2.0f, TempY - RealMsgPaddingY / 2.0f, Width + RealMsgPaddingX, RealFontSize + RealMsgPaddingY, RealBackgroundRounding, IGraphics::CORNER_ALL);
Graphics()->QuadsEnd();
@ -2996,7 +2996,7 @@ void CMenus::RenderSettingsAppearance(CUIRect MainView)
// Client
TextRender()->TextColor(ClientColor);
TextRender()->TextEx(&Cursor, "*** Echo command executed", -1);
TextRender()->TextEx(&Cursor, " Echo command executed", -1);
TextRender()->SetCursorPosition(&Cursor, X, Y);
TextRender()->TextColor(TextRender()->DefaultTextColor());