1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-06 06:32:56 +01:00

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.
This commit is contained in:
Ted John
2017-06-08 19:14:01 +01:00
parent eab0dc7cdb
commit f2a5d10c31
4 changed files with 51 additions and 9 deletions

View File

@@ -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)

View File

@@ -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;
}

View File

@@ -14,6 +14,7 @@
*****************************************************************************/
#pragma endregion
#include <time.h>
#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;
}

View File

@@ -17,6 +17,7 @@
#ifndef _UTIL_H_
#define _UTIL_H_
#include <time.h>
#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