MetaString.cpp 11 KB

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