2
0

MetaString.cpp 10 KB

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