diff options
author | Debucquoy Anthony tonitch <d.tonitch@gmail.com> | 2022-10-25 13:42:04 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-25 13:42:04 +0200 |
commit | 16f3355bbbd2170dd8f836885f5c887ff65c5f7d (patch) | |
tree | cee85a561104d295f288b5fed2b53e68d9a8c882 /src/CompositeChat.cpp | |
parent | Update Core (diff) | |
download | cuberite-16f3355bbbd2170dd8f836885f5c887ff65c5f7d.tar cuberite-16f3355bbbd2170dd8f836885f5c887ff65c5f7d.tar.gz cuberite-16f3355bbbd2170dd8f836885f5c887ff65c5f7d.tar.bz2 cuberite-16f3355bbbd2170dd8f836885f5c887ff65c5f7d.tar.lz cuberite-16f3355bbbd2170dd8f836885f5c887ff65c5f7d.tar.xz cuberite-16f3355bbbd2170dd8f836885f5c887ff65c5f7d.tar.zst cuberite-16f3355bbbd2170dd8f836885f5c887ff65c5f7d.zip |
Diffstat (limited to 'src/CompositeChat.cpp')
-rw-r--r-- | src/CompositeChat.cpp | 191 |
1 files changed, 68 insertions, 123 deletions
diff --git a/src/CompositeChat.cpp b/src/CompositeChat.cpp index 46712a0f5..94ed78197 100644 --- a/src/CompositeChat.cpp +++ b/src/CompositeChat.cpp @@ -96,50 +96,52 @@ void cCompositeChat::AddShowAchievementPart(const AString & a_PlayerName, const - +/** +* Parse the input message to add colors or link then add it to the object. +* +* It detects every & of the message and the next character for it to colorize. +* It detect : in the text to detect link structures. +* +* @param a_ParseText The input text to parse +*/ void cCompositeChat::ParseText(const AString & a_ParseText) { size_t len = a_ParseText.length(); - size_t first = 0; // First character of the currently parsed block + size_t cursor = 0; AString CurrentStyle; AString CurrentText; + for (size_t i = 0; i < len; i++) { switch (a_ParseText[i]) { - case '@': + case '&': //< Color code { - // Color code - i++; - if (i >= len) + if ((i != 0) && (a_ParseText[i-1] == '\\')) { - // Not enough following text - break; + CurrentText.append(a_ParseText, cursor, i - cursor - 1).append("&"); + AddTextPart(CurrentText, CurrentStyle); + CurrentText.clear(); + cursor = ++i; + continue; } - if (a_ParseText[i] == '@') + + if (cursor < i) { - // "@@" escape, just put a "@" into the current text and keep parsing as text - if (i > first + 1) - { - CurrentText.append(a_ParseText.c_str() + first, i - first - 1); - } - first = i + 1; - continue; + CurrentText.append(a_ParseText, cursor, i - cursor); + AddTextPart(CurrentText, CurrentStyle); + CurrentText.clear(); + } + i++; + cursor = i + 1; + + if (a_ParseText[i] == 'r') + { + CurrentStyle = ""; } else { - // True color code. Create a part for the CurrentText and start parsing anew: - if (i >= first) - { - CurrentText.append(a_ParseText.c_str() + first, i - first - 1); - first = i + 1; - } - if (!CurrentText.empty()) - { - AddTextPart(CurrentText, CurrentStyle); - CurrentText.clear(); - } - AddStyle(CurrentStyle, a_ParseText.substr(i - 1, 2)); + CurrentStyle.push_back(a_ParseText[i]); } break; } @@ -157,15 +159,15 @@ void cCompositeChat::ParseText(const AString & a_ParseText) { size_t PrefixLen = Prefix.size(); if ( - (i >= first + PrefixLen) && // There is enough space in front of the colon for the prefix + (i >= cursor + PrefixLen) && // There is enough space in front of the colon for the prefix (std::string_view(a_ParseText).substr(i - PrefixLen, PrefixLen) == Prefix) // the prefix matches ) { // Add everything before this as a text part: - if (i > first + PrefixLen) + if (i > cursor+ PrefixLen) { - CurrentText.append(a_ParseText.c_str() + first, i - first - PrefixLen); - first = i - PrefixLen; + CurrentText.append(a_ParseText.c_str() + cursor, i - cursor - PrefixLen); + cursor= i - PrefixLen; } if (!CurrentText.empty()) { @@ -181,8 +183,8 @@ void cCompositeChat::ParseText(const AString & a_ParseText) break; } } - AddUrlPart(a_ParseText.substr(first, i - first), a_ParseText.substr(first, i - first), CurrentStyle); - first = i; + AddUrlPart(a_ParseText.substr(cursor, i - cursor), a_ParseText.substr(cursor, i - cursor), CurrentStyle); + cursor = i; break; } } // for Prefix - LinkPrefix[] @@ -190,9 +192,11 @@ void cCompositeChat::ParseText(const AString & a_ParseText) } // case ':' } // switch (a_ParseText[i]) } // for i - a_ParseText[] - if (first < len) + if (cursor < len) { - AddTextPart(a_ParseText.substr(first, len - first), CurrentStyle); + CurrentText.clear(); + CurrentText.append(a_ParseText, cursor, len - cursor); + AddTextPart(CurrentText, CurrentStyle); } } @@ -218,7 +222,7 @@ void cCompositeChat::UnderlineUrls(void) { [](TextPart & a_Part) { }, [](ClientTranslatedPart & a_Part) { }, - [](UrlPart & a_Part) { a_Part.Style += 'u'; }, + [](UrlPart & a_Part) { a_Part.Style += 'n'; }, [](RunCommandPart & a_Part) { }, [](SuggestCommandPart & a_Part) { }, [](ShowAchievementPart & a_Part) { }, @@ -276,29 +280,6 @@ eLogLevel cCompositeChat::MessageTypeToLogLevel(eMessageType a_MessageType) -void cCompositeChat::AddStyle(AString & a_Style, const AString & a_AddStyle) -{ - if (a_AddStyle.empty()) - { - return; - } - if (a_AddStyle[0] == '@') - { - size_t idx = a_Style.find('@'); - if ((idx != AString::npos) && (idx != a_Style.length())) - { - a_Style.erase(idx, 2); - } - a_Style.append(a_AddStyle); - return; - } - a_Style.append(a_AddStyle); -} - - - - - AString cCompositeChat::CreateJsonString(bool a_ShouldUseChatPrefixes) const { Json::Value Message; @@ -404,70 +385,34 @@ void cCompositeChat::AddChatPartStyle(Json::Value & a_Value, const AString & a_P { switch (a_PartStyle[i]) { - case 'b': - { - // bold - a_Value["bold"] = Json::Value(true); - break; - } - - case 'i': - { - // italic - a_Value["italic"] = Json::Value(true); - break; - } - - case 'u': - { - // Underlined - a_Value["underlined"] = Json::Value(true); - break; - } + case 'k': a_Value["obfuscated"] = Json::Value(true); break; + case 'l': a_Value["bold"] = Json::Value(true); break; + case 's': // Deprecated + LOGERROR("Value s in AddChatPartStyle() is deprecated"); + case 'm': a_Value["strikethrough"] = Json::Value(true); break; + case 'u': // Deprecated + LOGERROR("Value u in AddChatPartStyle() is deprecated"); + case 'n': a_Value["underlined"] = Json::Value(true); break; + case 'i': // Deprecated + LOGERROR("Value i in AddChatPartStyle() is deprecated"); + case 'o': a_Value["italic"] = Json::Value(true); break; + case '0': a_Value["color"] = Json::Value("black"); break; + case '1': a_Value["color"] = Json::Value("dark_blue"); break; + case '2': a_Value["color"] = Json::Value("dark_green"); break; + case '3': a_Value["color"] = Json::Value("dark_aqua"); break; + case '4': a_Value["color"] = Json::Value("dark_red"); break; + case '5': a_Value["color"] = Json::Value("dark_purple"); break; + case '6': a_Value["color"] = Json::Value("gold"); break; + case '7': a_Value["color"] = Json::Value("gray"); break; + case '8': a_Value["color"] = Json::Value("dark_gray"); break; + case '9': a_Value["color"] = Json::Value("blue"); break; + case 'a': a_Value["color"] = Json::Value("green"); break; + case 'b': a_Value["color"] = Json::Value("aqua"); break; + case 'c': a_Value["color"] = Json::Value("red"); break; + case 'd': a_Value["color"] = Json::Value("light_purple"); break; + case 'e': a_Value["color"] = Json::Value("yellow"); break; + case 'f': a_Value["color"] = Json::Value("white"); break; - case 's': - { - // strikethrough - a_Value["strikethrough"] = Json::Value(true); - break; - } - - case 'o': - { - // obfuscated - a_Value["obfuscated"] = Json::Value(true); - break; - } - - case '@': - { - // Color, specified by the next char: - i++; - if (i >= len) - { - // String too short, didn't contain a color - break; - } - switch (a_PartStyle[i]) - { - case '0': a_Value["color"] = Json::Value("black"); break; - case '1': a_Value["color"] = Json::Value("dark_blue"); break; - case '2': a_Value["color"] = Json::Value("dark_green"); break; - case '3': a_Value["color"] = Json::Value("dark_aqua"); break; - case '4': a_Value["color"] = Json::Value("dark_red"); break; - case '5': a_Value["color"] = Json::Value("dark_purple"); break; - case '6': a_Value["color"] = Json::Value("gold"); break; - case '7': a_Value["color"] = Json::Value("gray"); break; - case '8': a_Value["color"] = Json::Value("dark_gray"); break; - case '9': a_Value["color"] = Json::Value("blue"); break; - case 'a': a_Value["color"] = Json::Value("green"); break; - case 'b': a_Value["color"] = Json::Value("aqua"); break; - case 'c': a_Value["color"] = Json::Value("red"); break; - case 'd': a_Value["color"] = Json::Value("light_purple"); break; - case 'e': a_Value["color"] = Json::Value("yellow"); break; - case 'f': a_Value["color"] = Json::Value("white"); break; - } // switch (color) - } // case '@' } // switch (Style[i]) } // for i - a_PartStyle[] } |