diff --git a/src/openrct2/interface/chat.c b/src/openrct2/interface/chat.c index eae633a8f0..ff55b78e22 100644 --- a/src/openrct2/interface/chat.c +++ b/src/openrct2/interface/chat.c @@ -172,15 +172,44 @@ void chat_draw(rct_drawpixelinfo * dpi) } } -void chat_history_add(const char *src) +void chat_history_add(const char * src) { + size_t bufferSize = strlen(src) + 64; + utf8 * buffer = (utf8 *)calloc(1, bufferSize); + + // Prepend colour marker (based on text, default to white) + const char * ch = src; + uint32 colour = FORMAT_WHITE; + uint32 codepoint; + while ((codepoint = utf8_get_next(ch, &ch)) != 0) { + if (utf8_is_colour_code(codepoint)) { + colour = codepoint; + break; + } + } + utf8_write_codepoint(buffer, colour); + + // Prepend a timestamp + time_t timer; + time(&timer); + struct tm * tmInfo = localtime(&timer); + + strcatftime(buffer, bufferSize, "[%H:%M] ", tmInfo); + safe_strcat(buffer, src, bufferSize); + + // Add to history list sint32 index = _chatHistoryIndex % CHAT_HISTORY_SIZE; memset(_chatHistory[index], 0, CHAT_INPUT_SIZE); - memcpy(_chatHistory[index], src, min(strlen(src), CHAT_INPUT_SIZE - 1)); + memcpy(_chatHistory[index], buffer, min(strlen(buffer), CHAT_INPUT_SIZE - 1)); _chatHistoryTime[index] = platform_get_ticks(); _chatHistoryIndex++; - Mixer_Play_Effect(SOUND_NEWS_ITEM, 0, SDL_MIX_MAXVOLUME, 0, 1.5f, true); + + // Log to file (src only as logging does its own timestamp) network_append_chat_log(src); + + free(buffer); + + Mixer_Play_Effect(SOUND_NEWS_ITEM, 0, SDL_MIX_MAXVOLUME, 0, 1.5f, true); } void chat_input(sint32 c) diff --git a/src/openrct2/network/network.cpp b/src/openrct2/network/network.cpp index 7d2791c642..bb98b15f3f 100644 --- a/src/openrct2/network/network.cpp +++ b/src/openrct2/network/network.cpp @@ -574,13 +574,9 @@ const char* Network::FormatChat(NetworkPlayer* fromplayer, const char* text) char* lineCh = formatted; formatted[0] = 0; if (fromplayer) { - time_t timer; - time(&timer); - auto tmInfo = localtime(&timer); lineCh = utf8_write_codepoint(lineCh, FORMAT_OUTLINE); lineCh = utf8_write_codepoint(lineCh, FORMAT_BABYBLUE); - lineCh += strftime(lineCh, sizeof(formatted) - (lineCh - formatted), "[%H:%M] ", tmInfo); - safe_strcat(lineCh, (const char *) fromplayer->Name.c_str(), sizeof(formatted) - (lineCh - formatted)); + safe_strcpy(lineCh, (const char *) fromplayer->Name.c_str(), sizeof(formatted) - (lineCh - formatted)); safe_strcat(lineCh, ": ", sizeof(formatted) - (lineCh - formatted)); lineCh = strchr(lineCh, '\0'); } @@ -588,7 +584,6 @@ const char* Network::FormatChat(NetworkPlayer* fromplayer, const char* text) lineCh = utf8_write_codepoint(lineCh, FORMAT_WHITE); char* ptrtext = lineCh; safe_strcpy(lineCh, text, 800); - utf8_remove_format_codes((utf8*)ptrtext, true); return formatted; } diff --git a/src/openrct2/util/util.c b/src/openrct2/util/util.c index d22d4b9245..12cdc99e27 100644 --- a/src/openrct2/util/util.c +++ b/src/openrct2/util/util.c @@ -14,6 +14,7 @@ *****************************************************************************/ #pragma endregion +#include #include "../common.h" #include "../core/Guard.hpp" #include "../localisation/localisation.h" @@ -531,3 +532,17 @@ money32 add_clamp_money32(money32 value, money32 value_to_add) } #undef add_clamp_body + +/** + * strftime wrapper which appends to an existing string. + */ +size_t strcatftime(char * buffer, size_t bufferSize, const char * format, const struct tm * tp) +{ + size_t stringLen = strnlen(buffer, bufferSize); + if (stringLen < bufferSize) { + char * dst = buffer + stringLen; + size_t dstMaxSize = bufferSize - stringLen; + return strftime(dst, dstMaxSize, format, tp); + } + return 0; +} diff --git a/src/openrct2/util/util.h b/src/openrct2/util/util.h index cc27af8690..16a1eb64a0 100644 --- a/src/openrct2/util/util.h +++ b/src/openrct2/util/util.h @@ -17,6 +17,7 @@ #ifndef _UTIL_H_ #define _UTIL_H_ +#include #include "../common.h" sint32 squaredmetres_to_squaredfeet(sint32 squaredMetres); @@ -62,4 +63,6 @@ sint16 add_clamp_sint16(sint16 value, sint16 value_to_add); sint32 add_clamp_sint32(sint32 value, sint32 value_to_add); money32 add_clamp_money32(money32 value, money32 value_to_add); +size_t strcatftime(char * buffer, size_t bufferSize, const char * format, const struct tm * tp); + #endif