Browse Source

Safer chat message handling

pull/3646/head
Gleb Mazovetskiy 4 years ago committed by Anders Jenbo
parent
commit
1b8939d70c
  1. 15
      Source/plrmsg.cpp

15
Source/plrmsg.cpp

@ -5,6 +5,8 @@
*/
#include "plrmsg.h"
#include <algorithm>
#include <fmt/format.h>
#include "DiabloUI/ui_flags.hpp"
@ -12,6 +14,7 @@
#include "engine/render/text_render.hpp"
#include "inv.h"
#include "utils/language.h"
#include "utils/stdcompat/string_view.hpp"
#include "utils/utf8.hpp"
namespace devilution {
@ -26,13 +29,11 @@ _plrmsg plr_msgs[PMSG_COUNT];
/** Maps from player_num to text color, as used in chat messages. */
const UiFlags TextColorFromPlayerId[MAX_PLRS + 1] = { UiFlags::ColorWhite, UiFlags::ColorWhite, UiFlags::ColorWhite, UiFlags::ColorWhite, UiFlags::ColorWhitegold };
void PrintChatMessage(const Surface &out, int x, int y, int width, char *text, UiFlags style)
void PrintChatMessage(const Surface &out, int x, int y, int width, char *textPtr, UiFlags style)
{
int length = strlen(text);
for (int i = 0; i < length; i++) {
if (text[i] == '\n')
text[i] = ' ';
}
const size_t length = strlen(textPtr);
std::replace(textPtr, textPtr + length, '\n', ' ');
const string_view text{textPtr, length};
DrawString(out, WordWrapString(text, width), { { x, y }, { width, 0 } }, style, 1, 10);
}
@ -86,7 +87,7 @@ void SendPlrMsg(int pnum, const char *pszStr)
auto &player = Players[pnum];
assert(strlen(player._pName) < PLR_NAME_LEN);
assert(strlen(pszStr) < MAX_SEND_STR_LEN);
strcpy(pMsg->str, fmt::format(_("{:s} (lvl {:d}): {:s}"), player._pName, player._pLevel, pszStr).c_str());
CopyUtf8(pMsg->str, fmt::format(_("{:s} (lvl {:d}): {:s}"), player._pName, player._pLevel, pszStr), sizeof(pMsg->str));
}
void ClearPlrMsg()

Loading…
Cancel
Save