MetaString.cpp 11 KB

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