diff options
author | Andrei Karas <akaras@inbox.ru> | 2016-01-16 22:21:30 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2016-01-16 22:34:33 +0300 |
commit | 0ad55dae62f1c8295517bf87844368575a813d57 (patch) | |
tree | 1611caa884e1b2fa5e0b4071feb436ec0e13d3d5 | |
parent | 71ef8f1355524d9af8a3c8231e128cbc71365a13 (diff) | |
download | manaplus-0ad55dae62f1c8295517bf87844368575a813d57.tar.gz manaplus-0ad55dae62f1c8295517bf87844368575a813d57.tar.bz2 manaplus-0ad55dae62f1c8295517bf87844368575a813d57.tar.xz manaplus-0ad55dae62f1c8295517bf87844368575a813d57.zip |
Add function for replace item links in chat.
-rw-r--r-- | src/gui/widgets/tabs/chat/chattab.cpp | 37 | ||||
-rw-r--r-- | src/utils/stringutils.cpp | 54 | ||||
-rw-r--r-- | src/utils/stringutils.h | 2 |
3 files changed, 55 insertions, 38 deletions
diff --git a/src/gui/widgets/tabs/chat/chattab.cpp b/src/gui/widgets/tabs/chat/chattab.cpp index 94e66e955..cb1566092 100644 --- a/src/gui/widgets/tabs/chat/chattab.cpp +++ b/src/gui/widgets/tabs/chat/chattab.cpp @@ -379,42 +379,7 @@ void ChatTab::chatInput(const std::string &message) if (msg.empty()) return; - // Check for item link - size_t start = msg.find('['); - size_t sz = msg.size(); - while (start + 1 < sz && start != std::string::npos - && msg[start + 1] != '@') - { - const size_t end = msg.find(']', start); - if (start + 1 != end && end != std::string::npos) - { - // Catch multiple embeds and ignore them - // so it doesn't crash the client. - while ((msg.find('[', start + 1) != std::string::npos) && - (msg.find('[', start + 1) < end)) - { - start = msg.find('[', start + 1); - } - - std::string temp; - if (start + 1 < sz && end < sz && end > start + 1) - { - temp = msg.substr(start + 1, end - start - 1); - - const ItemInfo &itemInfo = ItemDB::get(temp); - if (itemInfo.getId() != 0) - { - msg.insert(end, "@@"); - msg.insert(start + 1, "|"); - msg.insert(start + 1, toString(itemInfo.getId())); - msg.insert(start + 1, "@@"); - sz = msg.size(); - } - } - } - start = msg.find('[', start + 1); - } - + replaceItemLinks(msg); replaceVars(msg); switch (msg[0]) diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp index 8b616d2f7..e000b83c7 100644 --- a/src/utils/stringutils.cpp +++ b/src/utils/stringutils.cpp @@ -22,6 +22,12 @@ #include "utils/stringutils.h" +#ifndef DYECMD +#include "resources/iteminfo.h" + +#include "resources/db/itemdb.h" +#endif + #include <algorithm> #include <sstream> @@ -204,8 +210,11 @@ size_t findI(std::string text, const StringVect &list) return std::string::npos; } -unsigned int base = 94; -unsigned int start = 33; +namespace +{ + unsigned int base = 94; + unsigned int start = 33; +} // namespace const std::string encodeStr(unsigned int value, const unsigned int size) { @@ -960,3 +969,44 @@ std::string timeDiffToString(int timeDiff) } return str; } + +void replaceItemLinks(std::string &msg) +{ +#ifndef DYECMD + // Check for item link + size_t start2 = msg.find('['); + size_t sz = msg.size(); + while (start2 + 1 < sz && start2 != std::string::npos + && msg[start2 + 1] != '@') + { + const size_t end = msg.find(']', start2); + if (start2 + 1 != end && end != std::string::npos) + { + // Catch multiple embeds and ignore them + // so it doesn't crash the client. + while ((msg.find('[', start2 + 1) != std::string::npos) && + (msg.find('[', start2 + 1) < end)) + { + start2 = msg.find('[', start2 + 1); + } + + std::string temp; + if (start2 + 1 < sz && end < sz && end > start2 + 1) + { + temp = msg.substr(start2 + 1, end - start2 - 1); + + const ItemInfo &itemInfo = ItemDB::get(temp); + if (itemInfo.getId() != 0) + { + msg.insert(end, "@@"); + msg.insert(start2 + 1, "|"); + msg.insert(start2 + 1, toString(itemInfo.getId())); + msg.insert(start2 + 1, "@@"); + sz = msg.size(); + } + } + } + start2 = msg.find('[', start2 + 1); + } +#endif +} diff --git a/src/utils/stringutils.h b/src/utils/stringutils.h index 310bbc957..230445b75 100644 --- a/src/utils/stringutils.h +++ b/src/utils/stringutils.h @@ -258,4 +258,6 @@ std::string timeToStr(const int time); std::string timeDiffToString(int timeDiff); +void replaceItemLinks(std::string &msg); + #endif // UTILS_STRINGUTILS_H |