MetaString.cpp 11 KB

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