diff --git a/src/openrct2/interface/chat.c b/src/openrct2/interface/chat.c index eae633a8f0..e49038cbd3 100644 --- a/src/openrct2/interface/chat.c +++ b/src/openrct2/interface/chat.c @@ -172,15 +172,47 @@ 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); + + // Find the start of the text (after format codes) + const char * ch = src; + const char * nextCh; + uint32 codepoint; + while ((codepoint = utf8_get_next(ch, &nextCh)) != 0) { + if (!utf8_is_format_code(codepoint)) { + break; + } + ch = nextCh; + } + const char * srcText = ch; + + // Copy format codes to buffer + memcpy(buffer, src, min(bufferSize, (size_t)(srcText - src))); + + // Prepend a timestamp + time_t timer; + time(&timer); + struct tm * tmInfo = localtime(&timer); + + strcatftime(buffer, bufferSize, "[%H:%M] ", tmInfo); + safe_strcat(buffer, srcText, 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/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