aozhiwei 2f55ddd5d3 1
2022-12-27 09:33:27 +08:00

152 lines
5.0 KiB
C++

#include "precompile.h"
#include <regex>
#include <a8/mutable_xobject.h>
#include "mt/Text.h"
IMPL_TABLE(mt::Text)
std::map<std::string, std::vector<std::tuple<int, std::string>>> mt::Text::text_element_hash_;
static void ParseElemets(const std::string& textid,
const std::string& text,
std::vector<std::tuple<int, std::string>>& elements)
{
auto parsevar_cb =
[] (const std::string& var_name, std::vector<std::tuple<int, std::string>>& elements)
{
if (var_name == "${master.name}") {
elements.push_back(std::make_tuple(kFieldIdxMasterName, ""));
} else if (var_name == "${killer.name}") {
elements.push_back(std::make_tuple(kFieldIdxKillerName, ""));
} else if (var_name == "${dead.name}") {
elements.push_back(std::make_tuple(kFieldIdxDeadName, ""));
} else if (var_name == "${weapon.name}") {
elements.push_back(std::make_tuple(kFieldIdxWeaponName, ""));
} else if (var_name == "${weapon.text_icon}") {
elements.push_back(std::make_tuple(kFieldIdxWeaponTextIcon, ""));
} else if (var_name.find("${image.id:") == 0) {
std::vector<std::string> strings;
a8::Split(var_name, strings, ':');
std::string id = strings[1].substr(0, strings[1].length() - 1);
elements.push_back(std::make_tuple(kImageElement, id));
} else {
A8_ABORT();
}
};
std::regex re(R"(\$\{.*?\})", std::regex_constants::ECMAScript);
const char* pcurr = text.c_str();
const char* pend = text.c_str() + text.length();
while (pcurr < pend) {
#if 1
int pos = 0;
int length = 0;
bool match_ok = false;
std::string m_str;
{
const char* tmp_curr = pcurr;
while (*tmp_curr) {
if (tmp_curr[0] == '$' && tmp_curr[1] == '{') {
int i = 2;
while (tmp_curr[i] != '}') {
if (tmp_curr[i] == '\0') {
break;
}
++i;
}
if (tmp_curr[i] == '}') {
pos = tmp_curr - pcurr;
length = i + 1;
m_str = std::string(tmp_curr, length);
match_ok = true;
break;
} else {
++tmp_curr;
}
} else {
++tmp_curr;
}
}
}
if (match_ok) {
if (pos > 0) {
auto& e = a8::FastAppend(elements);
e = std::make_tuple(kTextElement, std::string(pcurr, pos));
}
parsevar_cb(m_str, elements);
pcurr += pos + length;
} else {
auto& e = a8::FastAppend(elements);
e = std::make_tuple(kTextElement, std::string(pcurr, pend - pcurr));
pcurr = pend;
}
#else
std::cmatch m;
if (std::regex_search(pcurr, m, re)) {
if (m.position() > 0) {
auto& e = a8::FastAppend(elements);
e = std::make_tuple(kTextElement, std::string(pcurr, m.position()));
}
parsevar_cb(m.str(), elements);
pcurr += m.position() + m.length();
} else {
auto& e = a8::FastAppend(elements);
e = std::make_tuple(kTextElement, std::string(pcurr, pend - pcurr));
pcurr = pend;
}
#endif
}
{
auto obj = a8::MutableXObject::CreateObject();
auto arr = a8::MutableXObject::CreateArray();
for (auto& e : elements) {
auto obj_el = a8::MutableXObject::CreateObject();
obj_el->SetVal("type", std::get<0>(e));
obj_el->SetVal("val", std::get<1>(e));
arr->Push(*obj_el);
}
obj->SetVal("text", text);
obj->SetVal("elemets", *arr);
#if 0
a8::UdpLog::Instance()->Info
("load textid:%s %s",
{
textid,
obj->ToJsonStr()
}
);
#endif
}
}
namespace mt
{
void Text::Init1()
{
if (textid().find("battle_server_dead_text_") != std::string::npos) {
std::vector<std::tuple<int, std::string>> elemets;
ParseElemets(textid(), text(), elemets);
text_element_hash_[textid()] = elemets;
}
}
std::string Text::GetText(const std::string& textid, const std::string& def_text)
{
const mt::Text* text_meta = GetByName(textid);
return text_meta ? text_meta->text() : def_text;
}
std::vector<std::tuple<int, std::string>>* Text::GetTextElements(const std::string& textid)
{
auto itr = text_element_hash_.find(textid);
return itr != text_element_hash_.end() ? &itr->second : nullptr;
}
}