MetaString.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * MetaString.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "MetaString.h"
  12. #include "CArtHandler.h"
  13. #include "CCreatureHandler.h"
  14. #include "CCreatureSet.h"
  15. #include "texts/CGeneralTextHandler.h"
  16. #include "CSkillHandler.h"
  17. #include "GameConstants.h"
  18. #include "VCMI_Lib.h"
  19. #include "mapObjectConstructors/CObjectClassesHandler.h"
  20. #include "spells/CSpellHandler.h"
  21. #include "serializer/JsonSerializeFormat.h"
  22. VCMI_LIB_NAMESPACE_BEGIN
  23. MetaString MetaString::createFromRawString(const std::string & value)
  24. {
  25. MetaString result;
  26. result.appendRawString(value);
  27. return result;
  28. }
  29. MetaString MetaString::createFromTextID(const std::string & value)
  30. {
  31. MetaString result;
  32. result.appendTextID(value);
  33. return result;
  34. }
  35. void MetaString::appendLocalString(EMetaText type, ui32 serial)
  36. {
  37. message.push_back(EMessage::APPEND_LOCAL_STRING);
  38. localStrings.emplace_back(type, serial);
  39. }
  40. void MetaString::appendRawString(const std::string & value)
  41. {
  42. message.push_back(EMessage::APPEND_RAW_STRING);
  43. exactStrings.push_back(value);
  44. }
  45. void MetaString::appendTextID(const std::string & value)
  46. {
  47. if (!value.empty())
  48. {
  49. message.push_back(EMessage::APPEND_TEXTID_STRING);
  50. stringsTextID.push_back(value);
  51. }
  52. }
  53. void MetaString::appendNumber(int64_t value)
  54. {
  55. message.push_back(EMessage::APPEND_NUMBER);
  56. numbers.push_back(value);
  57. }
  58. void MetaString::appendEOL()
  59. {
  60. message.push_back(EMessage::APPEND_EOL);
  61. }
  62. void MetaString::replaceLocalString(EMetaText type, ui32 serial)
  63. {
  64. message.push_back(EMessage::REPLACE_LOCAL_STRING);
  65. localStrings.emplace_back(type, serial);
  66. }
  67. void MetaString::replaceRawString(const std::string &txt)
  68. {
  69. message.push_back(EMessage::REPLACE_RAW_STRING);
  70. exactStrings.push_back(txt);
  71. }
  72. void MetaString::replaceTextID(const std::string & value)
  73. {
  74. message.push_back(EMessage::REPLACE_TEXTID_STRING);
  75. stringsTextID.push_back(value);
  76. }
  77. void MetaString::replaceNumber(int64_t txt)
  78. {
  79. message.push_back(EMessage::REPLACE_NUMBER);
  80. numbers.push_back(txt);
  81. }
  82. void MetaString::replacePositiveNumber(int64_t txt)
  83. {
  84. message.push_back(EMessage::REPLACE_POSITIVE_NUMBER);
  85. numbers.push_back(txt);
  86. }
  87. void MetaString::clear()
  88. {
  89. exactStrings.clear();
  90. localStrings.clear();
  91. stringsTextID.clear();
  92. message.clear();
  93. numbers.clear();
  94. }
  95. bool MetaString::empty() const
  96. {
  97. return message.empty() || toString().empty();
  98. }
  99. std::string MetaString::getLocalString(const std::pair<EMetaText, ui32> & txt) const
  100. {
  101. EMetaText type = txt.first;
  102. int ser = txt.second;
  103. switch(type)
  104. {
  105. case EMetaText::GENERAL_TXT:
  106. return VLC->generaltexth->translate("core.genrltxt", ser);
  107. case EMetaText::ARRAY_TXT:
  108. return VLC->generaltexth->translate("core.arraytxt", ser);
  109. case EMetaText::ADVOB_TXT:
  110. return VLC->generaltexth->translate("core.advevent", ser);
  111. case EMetaText::JK_TXT:
  112. return VLC->generaltexth->translate("core.jktext", ser);
  113. default:
  114. logGlobal->error("Failed string substitution because type is %d", static_cast<int>(type));
  115. return "#@#";
  116. }
  117. }
  118. DLL_LINKAGE std::string MetaString::toString() const
  119. {
  120. std::string dst;
  121. size_t exSt = 0;
  122. size_t loSt = 0;
  123. size_t nums = 0;
  124. size_t textID = 0;
  125. dst.clear();
  126. for(const auto & elem : message)
  127. {
  128. switch(elem)
  129. {
  130. case EMessage::APPEND_RAW_STRING:
  131. dst += exactStrings.at(exSt++);
  132. break;
  133. case EMessage::APPEND_LOCAL_STRING:
  134. dst += getLocalString(localStrings.at(loSt++));
  135. break;
  136. case EMessage::APPEND_TEXTID_STRING:
  137. dst += VLC->generaltexth->translate(stringsTextID.at(textID++));
  138. break;
  139. case EMessage::APPEND_NUMBER:
  140. dst += std::to_string(numbers.at(nums++));
  141. break;
  142. case EMessage::APPEND_EOL:
  143. dst += '\n';
  144. break;
  145. case EMessage::REPLACE_RAW_STRING:
  146. boost::replace_first(dst, "%s", exactStrings.at(exSt++));
  147. break;
  148. case EMessage::REPLACE_LOCAL_STRING:
  149. boost::replace_first(dst, "%s", getLocalString(localStrings.at(loSt++)));
  150. break;
  151. case EMessage::REPLACE_TEXTID_STRING:
  152. boost::replace_first(dst, "%s", VLC->generaltexth->translate(stringsTextID.at(textID++)));
  153. break;
  154. case EMessage::REPLACE_NUMBER:
  155. boost::replace_first(dst, "%d", std::to_string(numbers.at(nums++)));
  156. break;
  157. case EMessage::REPLACE_POSITIVE_NUMBER:
  158. if (dst.find("%+d") != std::string::npos)
  159. {
  160. int64_t value = numbers.at(nums);
  161. if (value > 0)
  162. boost::replace_first(dst, "%+d", '+' + std::to_string(value));
  163. else
  164. boost::replace_first(dst, "%+d", std::to_string(value));
  165. nums++;
  166. }
  167. else
  168. boost::replace_first(dst, "%d", std::to_string(numbers.at(nums++)));
  169. break;
  170. default:
  171. logGlobal->error("MetaString processing error! Received message of type %d", static_cast<int>(elem));
  172. assert(0);
  173. break;
  174. }
  175. }
  176. return dst;
  177. }
  178. DLL_LINKAGE std::string MetaString::buildList() const
  179. {
  180. size_t exSt = 0;
  181. size_t loSt = 0;
  182. size_t nums = 0;
  183. size_t textID = 0;
  184. std::string lista;
  185. for(int i = 0; i < message.size(); ++i)
  186. {
  187. if(i > 0 && (message.at(i) == EMessage::APPEND_RAW_STRING || message.at(i) == EMessage::APPEND_LOCAL_STRING))
  188. {
  189. if(exSt == exactStrings.size() - 1)
  190. lista += VLC->generaltexth->allTexts[141]; //" and "
  191. else
  192. lista += ", ";
  193. }
  194. switch(message.at(i))
  195. {
  196. case EMessage::APPEND_RAW_STRING:
  197. lista += exactStrings.at(exSt++);
  198. break;
  199. case EMessage::APPEND_LOCAL_STRING:
  200. lista += getLocalString(localStrings.at(loSt++));
  201. break;
  202. case EMessage::APPEND_TEXTID_STRING:
  203. lista += VLC->generaltexth->translate(stringsTextID.at(textID++));
  204. break;
  205. case EMessage::APPEND_NUMBER:
  206. lista += std::to_string(numbers.at(nums++));
  207. break;
  208. case EMessage::APPEND_EOL:
  209. lista += '\n';
  210. break;
  211. case EMessage::REPLACE_RAW_STRING:
  212. lista.replace(lista.find("%s"), 2, exactStrings.at(exSt++));
  213. break;
  214. case EMessage::REPLACE_LOCAL_STRING:
  215. lista.replace(lista.find("%s"), 2, getLocalString(localStrings.at(loSt++)));
  216. break;
  217. case EMessage::REPLACE_TEXTID_STRING:
  218. lista.replace(lista.find("%s"), 2, VLC->generaltexth->translate(stringsTextID.at(textID++)));
  219. break;
  220. case EMessage::REPLACE_NUMBER:
  221. lista.replace(lista.find("%d"), 2, std::to_string(numbers.at(nums++)));
  222. break;
  223. default:
  224. logGlobal->error("MetaString processing error! Received message of type %d", int(message.at(i)));
  225. }
  226. }
  227. return lista;
  228. }
  229. bool MetaString::operator == (const MetaString & other) const
  230. {
  231. return message == other.message && localStrings == other.localStrings && exactStrings == other.exactStrings && stringsTextID == other.stringsTextID && numbers == other.numbers;
  232. }
  233. void MetaString::jsonSerialize(JsonNode & dest) const
  234. {
  235. JsonNode jsonMessage;
  236. JsonNode jsonLocalStrings;
  237. JsonNode jsonExactStrings;
  238. JsonNode jsonStringsTextID;
  239. JsonNode jsonNumbers;
  240. for (const auto & entry : message )
  241. {
  242. JsonNode value;
  243. value.Float() = static_cast<int>(entry);
  244. jsonMessage.Vector().push_back(value);
  245. }
  246. for (const auto & entry : localStrings )
  247. {
  248. JsonNode value;
  249. value.Integer() = static_cast<int>(entry.first) * 10000 + entry.second;
  250. jsonLocalStrings.Vector().push_back(value);
  251. }
  252. for (const auto & entry : exactStrings )
  253. {
  254. JsonNode value;
  255. value.String() = entry;
  256. jsonExactStrings.Vector().push_back(value);
  257. }
  258. for (const auto & entry : stringsTextID )
  259. {
  260. JsonNode value;
  261. value.String() = entry;
  262. jsonStringsTextID.Vector().push_back(value);
  263. }
  264. for (const auto & entry : numbers )
  265. {
  266. JsonNode value;
  267. value.Integer() = entry;
  268. jsonNumbers.Vector().push_back(value);
  269. }
  270. dest["message"] = jsonMessage;
  271. dest["localStrings"] = jsonLocalStrings;
  272. dest["exactStrings"] = jsonExactStrings;
  273. dest["stringsTextID"] = jsonStringsTextID;
  274. dest["numbers"] = jsonNumbers;
  275. }
  276. void MetaString::jsonDeserialize(const JsonNode & source)
  277. {
  278. clear();
  279. if (source.isString())
  280. {
  281. // compatibility with fields that were converted from string to MetaString
  282. if(boost::starts_with(source.String(), "core.") || boost::starts_with(source.String(), "vcmi."))
  283. appendTextID(source.String());
  284. else
  285. appendRawString(source.String());
  286. return;
  287. }
  288. for (const auto & entry : source["message"].Vector() )
  289. message.push_back(static_cast<EMessage>(entry.Integer()));
  290. for (const auto & entry : source["localStrings"].Vector() )
  291. localStrings.push_back({ static_cast<EMetaText>(entry.Integer() / 10000), entry.Integer() % 10000 });
  292. for (const auto & entry : source["exactStrings"].Vector() )
  293. exactStrings.push_back(entry.String());
  294. for (const auto & entry : source["stringsTextID"].Vector() )
  295. stringsTextID.push_back(entry.String());
  296. for (const auto & entry : source["numbers"].Vector() )
  297. numbers.push_back(entry.Integer());
  298. }
  299. void MetaString::serializeJson(JsonSerializeFormat & handler)
  300. {
  301. if(handler.saving)
  302. jsonSerialize(const_cast<JsonNode&>(handler.getCurrent()));
  303. if(!handler.saving)
  304. jsonDeserialize(handler.getCurrent());
  305. }
  306. void MetaString::appendName(const ArtifactID & id)
  307. {
  308. appendTextID(id.toEntity(VLC)->getNameTextID());
  309. }
  310. void MetaString::appendName(const SpellID & id)
  311. {
  312. appendTextID(id.toEntity(VLC)->getNameTextID());
  313. }
  314. void MetaString::appendName(const PlayerColor & id)
  315. {
  316. appendTextID(TextIdentifier("vcmi.capitalColors", id.getNum()).get());
  317. }
  318. void MetaString::appendName(const CreatureID & id, TQuantity count)
  319. {
  320. if(count == 1)
  321. appendNameSingular(id);
  322. else
  323. appendNamePlural(id);
  324. }
  325. void MetaString::appendName(const GameResID& id)
  326. {
  327. appendTextID(TextIdentifier("core.restypes", id.getNum()).get());
  328. }
  329. void MetaString::appendNameSingular(const CreatureID & id)
  330. {
  331. appendTextID(id.toEntity(VLC)->getNameSingularTextID());
  332. }
  333. void MetaString::appendNamePlural(const CreatureID & id)
  334. {
  335. appendTextID(id.toEntity(VLC)->getNamePluralTextID());
  336. }
  337. void MetaString::replaceName(const ArtifactID & id)
  338. {
  339. replaceTextID(id.toEntity(VLC)->getNameTextID());
  340. }
  341. void MetaString::replaceName(const MapObjectID& id)
  342. {
  343. replaceTextID(VLC->objtypeh->getObjectName(id, 0));
  344. }
  345. void MetaString::replaceName(const PlayerColor & id)
  346. {
  347. replaceTextID(TextIdentifier("vcmi.capitalColors", id.getNum()).get());
  348. }
  349. void MetaString::replaceName(const SecondarySkill & id)
  350. {
  351. replaceTextID(VLC->skillh->getById(id)->getNameTextID());
  352. }
  353. void MetaString::replaceName(const SpellID & id)
  354. {
  355. replaceTextID(id.toEntity(VLC)->getNameTextID());
  356. }
  357. void MetaString::replaceName(const GameResID& id)
  358. {
  359. replaceTextID(TextIdentifier("core.restypes", id.getNum()).get());
  360. }
  361. void MetaString::replaceNameSingular(const CreatureID & id)
  362. {
  363. replaceTextID(id.toEntity(VLC)->getNameSingularTextID());
  364. }
  365. void MetaString::replaceNamePlural(const CreatureID & id)
  366. {
  367. replaceTextID(id.toEntity(VLC)->getNamePluralTextID());
  368. }
  369. void MetaString::replaceName(const CreatureID & id, TQuantity count) //adds sing or plural name;
  370. {
  371. if(count == 1)
  372. replaceNameSingular(id);
  373. else
  374. replaceNamePlural(id);
  375. }
  376. void MetaString::replaceName(const CStackBasicDescriptor & stack)
  377. {
  378. replaceName(stack.type->getId(), stack.count);
  379. }
  380. VCMI_LIB_NAMESPACE_END