MetaString.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. boost::replace_first(dst, "%+d", '+' + std::to_string(numbers[nums++]));
  153. else
  154. boost::replace_first(dst, "%d", std::to_string(numbers[nums++]));
  155. break;
  156. default:
  157. logGlobal->error("MetaString processing error! Received message of type %d", static_cast<int>(elem));
  158. assert(0);
  159. break;
  160. }
  161. }
  162. return dst;
  163. }
  164. DLL_LINKAGE std::string MetaString::buildList() const
  165. {
  166. size_t exSt = 0;
  167. size_t loSt = 0;
  168. size_t nums = 0;
  169. size_t textID = 0;
  170. std::string lista;
  171. for(int i = 0; i < message.size(); ++i)
  172. {
  173. if(i > 0 && (message[i] == EMessage::APPEND_RAW_STRING || message[i] == EMessage::APPEND_LOCAL_STRING))
  174. {
  175. if(exSt == exactStrings.size() - 1)
  176. lista += VLC->generaltexth->allTexts[141]; //" and "
  177. else
  178. lista += ", ";
  179. }
  180. switch(message[i])
  181. {
  182. case EMessage::APPEND_RAW_STRING:
  183. lista += exactStrings[exSt++];
  184. break;
  185. case EMessage::APPEND_LOCAL_STRING:
  186. lista += getLocalString(localStrings[loSt++]);
  187. break;
  188. case EMessage::APPEND_TEXTID_STRING:
  189. lista += VLC->generaltexth->translate(stringsTextID[textID++]);
  190. break;
  191. case EMessage::APPEND_NUMBER:
  192. lista += std::to_string(numbers[nums++]);
  193. break;
  194. case EMessage::REPLACE_RAW_STRING:
  195. lista.replace(lista.find("%s"), 2, exactStrings[exSt++]);
  196. break;
  197. case EMessage::REPLACE_LOCAL_STRING:
  198. lista.replace(lista.find("%s"), 2, getLocalString(localStrings[loSt++]));
  199. break;
  200. case EMessage::REPLACE_TEXTID_STRING:
  201. lista.replace(lista.find("%s"), 2, VLC->generaltexth->translate(stringsTextID[textID++]));
  202. break;
  203. case EMessage::REPLACE_NUMBER:
  204. lista.replace(lista.find("%d"), 2, std::to_string(numbers[nums++]));
  205. break;
  206. default:
  207. logGlobal->error("MetaString processing error! Received message of type %d", int(message[i]));
  208. }
  209. }
  210. return lista;
  211. }
  212. bool MetaString::operator == (const MetaString & other) const
  213. {
  214. return message == other.message && localStrings == other.localStrings && exactStrings == other.exactStrings && stringsTextID == other.stringsTextID && numbers == other.numbers;
  215. }
  216. void MetaString::jsonSerialize(JsonNode & dest) const
  217. {
  218. JsonNode jsonMessage;
  219. JsonNode jsonLocalStrings;
  220. JsonNode jsonExactStrings;
  221. JsonNode jsonStringsTextID;
  222. JsonNode jsonNumbers;
  223. for (const auto & entry : message )
  224. {
  225. JsonNode value;
  226. value.Float() = static_cast<int>(entry);
  227. jsonMessage.Vector().push_back(value);
  228. }
  229. for (const auto & entry : localStrings )
  230. {
  231. JsonNode value;
  232. value.Integer() = static_cast<int>(entry.first) * 10000 + entry.second;
  233. jsonLocalStrings.Vector().push_back(value);
  234. }
  235. for (const auto & entry : exactStrings )
  236. {
  237. JsonNode value;
  238. value.String() = entry;
  239. jsonExactStrings.Vector().push_back(value);
  240. }
  241. for (const auto & entry : stringsTextID )
  242. {
  243. JsonNode value;
  244. value.String() = entry;
  245. jsonStringsTextID.Vector().push_back(value);
  246. }
  247. for (const auto & entry : numbers )
  248. {
  249. JsonNode value;
  250. value.Integer() = entry;
  251. jsonNumbers.Vector().push_back(value);
  252. }
  253. dest["message"] = jsonMessage;
  254. dest["localStrings"] = jsonLocalStrings;
  255. dest["exactStrings"] = jsonExactStrings;
  256. dest["stringsTextID"] = jsonStringsTextID;
  257. dest["numbers"] = jsonNumbers;
  258. }
  259. void MetaString::jsonDeserialize(const JsonNode & source)
  260. {
  261. clear();
  262. if (source.isString())
  263. {
  264. // compatibility with fields that were converted from string to MetaString
  265. if(boost::starts_with(source.String(), "core.") || boost::starts_with(source.String(), "vcmi."))
  266. appendTextID(source.String());
  267. else
  268. appendRawString(source.String());
  269. return;
  270. }
  271. for (const auto & entry : source["message"].Vector() )
  272. message.push_back(static_cast<EMessage>(entry.Integer()));
  273. for (const auto & entry : source["localStrings"].Vector() )
  274. localStrings.push_back({ static_cast<EMetaText>(entry.Integer() / 10000), entry.Integer() % 10000 });
  275. for (const auto & entry : source["exactStrings"].Vector() )
  276. exactStrings.push_back(entry.String());
  277. for (const auto & entry : source["stringsTextID"].Vector() )
  278. stringsTextID.push_back(entry.String());
  279. for (const auto & entry : source["numbers"].Vector() )
  280. numbers.push_back(entry.Integer());
  281. }
  282. void MetaString::serializeJson(JsonSerializeFormat & handler)
  283. {
  284. if(handler.saving)
  285. jsonSerialize(const_cast<JsonNode&>(handler.getCurrent()));
  286. if(!handler.saving)
  287. jsonDeserialize(handler.getCurrent());
  288. }
  289. void MetaString::appendName(const SpellID & id)
  290. {
  291. appendTextID(id.toEntity(VLC)->getNameTextID());
  292. }
  293. void MetaString::appendName(const PlayerColor & id)
  294. {
  295. appendTextID(TextIdentifier("vcmi.capitalColors", id.getNum()).get());
  296. }
  297. void MetaString::appendName(const CreatureID & id, TQuantity count)
  298. {
  299. if(count == 1)
  300. appendNameSingular(id);
  301. else
  302. appendNamePlural(id);
  303. }
  304. void MetaString::appendNameSingular(const CreatureID & id)
  305. {
  306. appendTextID(id.toEntity(VLC)->getNameSingularTextID());
  307. }
  308. void MetaString::appendNamePlural(const CreatureID & id)
  309. {
  310. appendTextID(id.toEntity(VLC)->getNamePluralTextID());
  311. }
  312. void MetaString::replaceName(const ArtifactID & id)
  313. {
  314. replaceTextID(id.toEntity(VLC)->getNameTextID());
  315. }
  316. void MetaString::replaceName(const MapObjectID& id)
  317. {
  318. replaceTextID(VLC->objtypeh->getObjectName(id, 0));
  319. }
  320. void MetaString::replaceName(const PlayerColor & id)
  321. {
  322. replaceTextID(TextIdentifier("vcmi.capitalColors", id.getNum()).get());
  323. }
  324. void MetaString::replaceName(const SecondarySkill & id)
  325. {
  326. replaceTextID(VLC->skillh->getById(id)->getNameTextID());
  327. }
  328. void MetaString::replaceName(const SpellID & id)
  329. {
  330. replaceTextID(id.toEntity(VLC)->getNameTextID());
  331. }
  332. void MetaString::replaceName(const GameResID& id)
  333. {
  334. replaceTextID(TextIdentifier("core.restypes", id.getNum()).get());
  335. }
  336. void MetaString::replaceNameSingular(const CreatureID & id)
  337. {
  338. replaceTextID(id.toEntity(VLC)->getNameSingularTextID());
  339. }
  340. void MetaString::replaceNamePlural(const CreatureID & id)
  341. {
  342. replaceTextID(id.toEntity(VLC)->getNamePluralTextID());
  343. }
  344. void MetaString::replaceName(const CreatureID & id, TQuantity count) //adds sing or plural name;
  345. {
  346. if(count == 1)
  347. replaceNameSingular(id);
  348. else
  349. replaceNamePlural(id);
  350. }
  351. void MetaString::replaceName(const CStackBasicDescriptor & stack)
  352. {
  353. replaceName(stack.type->getId(), stack.count);
  354. }
  355. VCMI_LIB_NAMESPACE_END