Skip to content

Commit 9a0b1d5

Browse files
shadyluaShady1
andauthored
Fix chat message length calculation with colorCoded enabled (#4370)
* Fix chat message length calculation with colorCoded enabled When colorCoded = true, the chat message length calculation was incorrectly counting hex color codes as part of the text. This caused the message width to be miscalculated, so some texts did not fit properly in the chat box. This PR updates the logic to ignore hex color codes during length calculation, ensuring that only visible characters are counted. Result Chat messages now display correctly without being cut off. Length is calculated only from visible text, not formatting codes. * Update CStaticFunctionDefinitions.cpp comments deleted. * Update CStaticFunctionDefinitions.cpp done. * Update CStaticFunctionDefinitions.cpp * Update CStaticFunctionDefinitions.cpp * Update CStaticFunctionDefinitions.cpp done --------- Co-authored-by: Shady1 <[email protected]>
1 parent 4d4d6c9 commit 9a0b1d5

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

Client/mods/deathmatch/logic/CPacketHandler.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1412,7 +1412,8 @@ void CPacketHandler::Packet_ChatEcho(NetBitStreamInterface& bitStream)
14121412
bitStream.Read(szMessage, iNumberOfBytesUsed);
14131413
szMessage[iNumberOfBytesUsed] = 0;
14141414
// actual limits enforced on the remote client, this is the maximum a string can be to be printed.
1415-
if (MbUTF8ToUTF16(szMessage).size() <=
1415+
SString textToProcess = bColorCoded ? RemoveColorCodes(szMessage) : szMessage;
1416+
if (MbUTF8ToUTF16(textToProcess).size() <=
14161417
MAX_CHATECHO_LENGTH + 6) // Extra 6 characters to fix #7125 (Teamsay + long name + long message = too long message)
14171418
{
14181419
// Strip it for bad characters

Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -260,21 +260,27 @@ bool CStaticFunctionDefinitions::ClearChatBox()
260260

261261
bool CStaticFunctionDefinitions::OutputChatBox(const char* szText, unsigned char ucRed, unsigned char ucGreen, unsigned char ucBlue, bool bColorCoded)
262262
{
263-
if (strlen(szText) <= MAX_OUTPUTCHATBOX_LENGTH)
264-
{
265-
CLuaArguments Arguments;
266-
Arguments.PushString(szText);
267-
Arguments.PushNumber(ucRed);
268-
Arguments.PushNumber(ucGreen);
269-
Arguments.PushNumber(ucBlue);
263+
if (!szText || szText[0] == '\0')
264+
return false;
265+
266+
SString textToProcess = bColorCoded ? RemoveColorCodes(szText) : szText;
267+
268+
if (textToProcess.length() > MAX_OUTPUTCHATBOX_LENGTH) {
269+
return false;
270+
}
270271

271-
bool bCancelled = !g_pClientGame->GetRootEntity()->CallEvent("onClientChatMessage", Arguments, false);
272-
if (!bCancelled)
273-
{
274-
m_pCore->ChatPrintfColor("%s", bColorCoded, ucRed, ucGreen, ucBlue, szText);
275-
return true;
276-
}
272+
CLuaArguments Arguments;
273+
Arguments.PushString(szText);
274+
Arguments.PushNumber(ucRed);
275+
Arguments.PushNumber(ucGreen);
276+
Arguments.PushNumber(ucBlue);
277+
278+
bool bCancelled = !g_pClientGame->GetRootEntity()->CallEvent("onClientChatMessage", Arguments, false);
279+
if (!bCancelled) {
280+
m_pCore->ChatPrintfColor("%s", bColorCoded, ucRed, ucGreen, ucBlue, szText);
281+
return true;
277282
}
283+
278284
return false;
279285
}
280286

0 commit comments

Comments
 (0)