From eab0dc7cdbfca7f1610aa2063dfc691553f41ad6 Mon Sep 17 00:00:00 2001 From: Christian Murphy Date: Mon, 20 Feb 2017 00:11:03 +0000 Subject: [PATCH 1/3] Add timestamps to chat messages Squashed commit from: - a908a4c Added config parameter "timestamp_chat" to network config section to allow chat messages to be timestamped - 820fcd7 make timestamps on by default - e6ecad2 timestamp chat messages, removed config option - 3b4439d Replace strftime temp buffer with directly using strftime into lineCh - add20d3 Move time --- src/openrct2/network/network.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/openrct2/network/network.cpp b/src/openrct2/network/network.cpp index bb98b15f3f..7d2791c642 100644 --- a/src/openrct2/network/network.cpp +++ b/src/openrct2/network/network.cpp @@ -574,9 +574,13 @@ 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); - safe_strcpy(lineCh, (const char *) fromplayer->Name.c_str(), sizeof(formatted) - (lineCh - formatted)); + lineCh += strftime(lineCh, sizeof(formatted) - (lineCh - formatted), "[%H:%M] ", tmInfo); + safe_strcat(lineCh, (const char *) fromplayer->Name.c_str(), sizeof(formatted) - (lineCh - formatted)); safe_strcat(lineCh, ": ", sizeof(formatted) - (lineCh - formatted)); lineCh = strchr(lineCh, '\0'); } @@ -584,6 +588,7 @@ 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; } From f2a5d10c3128c08a3108ed85a266a3b043d2d747 Mon Sep 17 00:00:00 2001 From: Ted John Date: Thu, 8 Jun 2017 19:14:01 +0100 Subject: [PATCH 2/3] Move timestamp to chat.c and fix issues - Timestamp now coloured correctly. - Timestamp now added on client side (client local time). - Timestamp is no longer duplicated in log file. --- src/openrct2/interface/chat.c | 35 +++++++++++++++++++++++++++++--- src/openrct2/network/network.cpp | 7 +------ src/openrct2/util/util.c | 15 ++++++++++++++ src/openrct2/util/util.h | 3 +++ 4 files changed, 51 insertions(+), 9 deletions(-) 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 From ca68e2bb8996045a5109cb2748875d762f3fdc17 Mon Sep 17 00:00:00 2001 From: Ted John Date: Sat, 10 Jun 2017 00:53:51 +0100 Subject: [PATCH 3/3] Use all leading format codes for timestamp --- src/openrct2/interface/chat.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/openrct2/interface/chat.c b/src/openrct2/interface/chat.c index ff55b78e22..e49038cbd3 100644 --- a/src/openrct2/interface/chat.c +++ b/src/openrct2/interface/chat.c @@ -177,17 +177,20 @@ 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) + // Find the start of the text (after format codes) const char * ch = src; - uint32 colour = FORMAT_WHITE; + const char * nextCh; uint32 codepoint; - while ((codepoint = utf8_get_next(ch, &ch)) != 0) { - if (utf8_is_colour_code(codepoint)) { - colour = codepoint; + while ((codepoint = utf8_get_next(ch, &nextCh)) != 0) { + if (!utf8_is_format_code(codepoint)) { break; } + ch = nextCh; } - utf8_write_codepoint(buffer, colour); + const char * srcText = ch; + + // Copy format codes to buffer + memcpy(buffer, src, min(bufferSize, (size_t)(srcText - src))); // Prepend a timestamp time_t timer; @@ -195,7 +198,7 @@ void chat_history_add(const char * src) struct tm * tmInfo = localtime(&timer); strcatftime(buffer, bufferSize, "[%H:%M] ", tmInfo); - safe_strcat(buffer, src, bufferSize); + safe_strcat(buffer, srcText, bufferSize); // Add to history list sint32 index = _chatHistoryIndex % CHAT_HISTORY_SIZE;