html character serialization, normal color format support

This commit is contained in:
DrPerkyLegit 2026-04-09 20:54:16 -04:00
parent beca8065b1
commit af3df4c636
5 changed files with 91 additions and 11 deletions

View file

@ -1561,21 +1561,18 @@ void ClientConnection::handleChat(shared_ptr<ChatPacket> packet)
switch(packet->m_messageType)
{
case ChatPacket::e_ChatCustom:
if (stringArgsSize > 1) {
if (stringArgsSize >= 1) {
message = packet->m_stringArgs[0];
for (int i = 1; i < stringArgsSize; i++) { //skip the first string index, thats main message
message = replaceAll(message, L"{*ARGS_" + std::to_wstring(i) + L"*}", packet->m_stringArgs[i]);
}
std::wsmatch match;
while (std::regex_search(message, match, IDS_Pattern)) {
message = replaceAll(message, match[0], app.GetString(std::stoi(match[1].str())));
}
message = app.FormatHTMLString(m_userIndex, message);
} else if (packet->m_stringArgs.size() == 1) {
message = (packet->m_stringArgs.size() >= 1) ? packet->m_stringArgs[0] : L"";
message = app.EscapeHTMLString(message); //do this to enforce escaped string
message = app.FormatChatMessage(message); //this needs to be last cause it converts colors to html colors that would have been escaped
} else {
message = L"empty message";
}
break;
case ChatPacket::e_ChatBedOccupied:

View file

@ -6595,6 +6595,87 @@ wstring CMinecraftApp::FormatHTMLString(int iPad, const wstring &desc, int shado
return text;
}
//found list of html escapes at https://stackoverflow.com/questions/7381974/which-characters-need-to-be-escaped-in-html
wstring CMinecraftApp::EscapeHTMLString(const wstring& desc)
{
static std::unordered_map<wchar_t, wchar_t*> replacementMap = {
{L'&', L"&amp;"},
{L'<', L"&lt;"},
{L'>', L"&gt;"},
{L'\"', L"&quot;"},
{L'\'', L"&#39;"},
};
wstring finalString = L"";
for (int i = 0; i < desc.size(); i++) {
wchar_t _char = desc[i];
auto it = replacementMap.find(_char);
if (it != replacementMap.end()) finalString += it->second;
else finalString += _char;
}
return finalString;
}
wstring CMinecraftApp::FormatChatMessage(const wstring& desc)
{
static std::wstring_view colorFormatString = L"<font color=\"#%08x\" shadowcolor=\"#%08x\">";
wstring results = desc;
wchar_t replacements[64];
swprintf(replacements, 64, colorFormatString.data(), GetHTMLColour(eHTMLColor_0), 0xFFFFFFFF);
results = replaceAll(results, L"§0", replacements);
swprintf(replacements, 64, colorFormatString.data(), GetHTMLColour(eHTMLColor_1), 0xFFFFFFFF);
results = replaceAll(results, L"§1", replacements);
swprintf(replacements, 64, colorFormatString.data(), GetHTMLColour(eHTMLColor_2), 0xFFFFFFFF);
results = replaceAll(results, L"§2", replacements);
swprintf(replacements, 64, colorFormatString.data(), GetHTMLColour(eHTMLColor_3), 0xFFFFFFFF);
results = replaceAll(results, L"§3", replacements);
swprintf(replacements, 64, colorFormatString.data(), GetHTMLColour(eHTMLColor_4), 0xFFFFFFFF);
results = replaceAll(results, L"§4", replacements);
swprintf(replacements, 64, colorFormatString.data(), GetHTMLColour(eHTMLColor_5), 0xFFFFFFFF);
results = replaceAll(results, L"§5", replacements);
swprintf(replacements, 64, colorFormatString.data(), GetHTMLColour(eHTMLColor_6), 0xFFFFFFFF);
results = replaceAll(results, L"§6", replacements);
swprintf(replacements, 64, colorFormatString.data(), GetHTMLColour(eHTMLColor_7), 0xFFFFFFFF);
results = replaceAll(results, L"§7", replacements);
swprintf(replacements, 64, colorFormatString.data(), GetHTMLColour(eHTMLColor_8), 0xFFFFFFFF);
results = replaceAll(results, L"§8", replacements);
swprintf(replacements, 64, colorFormatString.data(), GetHTMLColour(eHTMLColor_9), 0xFFFFFFFF);
results = replaceAll(results, L"§9", replacements);
swprintf(replacements, 64, colorFormatString.data(), GetHTMLColour(eHTMLColor_a), 0xFFFFFFFF);
results = replaceAll(results, L"§a", replacements);
swprintf(replacements, 64, colorFormatString.data(), GetHTMLColour(eHTMLColor_b), 0xFFFFFFFF);
results = replaceAll(results, L"§b", replacements);
swprintf(replacements, 64, colorFormatString.data(), GetHTMLColour(eHTMLColor_c), 0xFFFFFFFF);
results = replaceAll(results, L"§c", replacements);
swprintf(replacements, 64, colorFormatString.data(), GetHTMLColour(eHTMLColor_d), 0xFFFFFFFF);
results = replaceAll(results, L"§d", replacements);
swprintf(replacements, 64, colorFormatString.data(), GetHTMLColour(eHTMLColor_e), 0xFFFFFFFF);
results = replaceAll(results, L"§e", replacements);
swprintf(replacements, 64, colorFormatString.data(), GetHTMLColour(eHTMLColor_f), 0xFFFFFFFF);
results = replaceAll(results, L"§f", replacements);
return results;
}
wstring CMinecraftApp::GetActionReplacement(int iPad, unsigned char ucAction)
{
unsigned int input = InputManager.GetGameJoypadMaps(InputManager.GetJoypadMapVal(iPad) ,ucAction);

View file

@ -564,7 +564,9 @@ public:
int GetHTMLColour(eMinecraftColour colour);
int GetHTMLColor(eMinecraftColour colour) { return GetHTMLColour(colour); }
int GetHTMLFontSize(EHTMLFontSize size);
wstring FormatHTMLString(int iPad, const wstring &desc, int shadowColour = 0xFFFFFFFF);
wstring FormatHTMLString(int iPad, const wstring& desc, int shadowColour = 0xFFFFFFFF);
wstring EscapeHTMLString(const wstring &desc);
wstring FormatChatMessage(const wstring& desc);
wstring GetActionReplacement(int iPad, unsigned char ucAction);
wstring GetVKReplacement(unsigned int uiVKey);
wstring GetIconReplacement(unsigned int uiIcon);

View file

@ -678,7 +678,7 @@ void PlayerConnection::handleChat(shared_ptr<ChatPacket> packet)
handleCommand(message);
return;
}
wstring formatted = L"&lt;" + player->name + L"&gt; " + message;
wstring formatted = L"<" + player->name + L"> " + message;
server->getPlayers()->broadcastAll(shared_ptr<ChatPacket>(new ChatPacket(formatted)));
chatSpamTickCount += SharedConstants::TICKS_PER_SECOND;
if (chatSpamTickCount > SharedConstants::TICKS_PER_SECOND * 10)

View file

@ -20,7 +20,7 @@ class SharedConstants
static wstring readAcceptableChars();
public:
static const int maxChatLength = 100;
static const int maxChatLength = 1024;
static wstring acceptableLetters;
static const int ILLEGAL_FILE_CHARACTERS_LENGTH = 15;