MetaString.cpp 11 KB

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