MetaString.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * MetaString.h, 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. #pragma once
  11. VCMI_LIB_NAMESPACE_BEGIN
  12. class JsonNode;
  13. class CreatureID;
  14. class CStackBasicDescriptor;
  15. class JsonSerializeFormat;
  16. using TQuantity = si32;
  17. /// Strings classes that can be used as replacement in MetaString
  18. enum class EMetaText : uint8_t
  19. {
  20. GENERAL_TXT = 1,
  21. OBJ_NAMES,
  22. RES_NAMES,
  23. ART_NAMES,
  24. ARRAY_TXT,
  25. CRE_PL_NAMES,
  26. CREGENS,
  27. MINE_NAMES,
  28. MINE_EVNTS,
  29. ADVOB_TXT,
  30. ART_EVNTS,
  31. SPELL_NAME,
  32. SEC_SKILL_NAME,
  33. CRE_SING_NAMES,
  34. CREGENS4,
  35. COLOR,
  36. ART_DESCR,
  37. JK_TXT
  38. };
  39. /// Class for string formatting tools that also support transfer over network with localization using language of local install
  40. /// Can be used to compose resulting text from multiple line segments and with placeholder replacement
  41. class DLL_LINKAGE MetaString
  42. {
  43. private:
  44. enum class EMessage : uint8_t
  45. {
  46. APPEND_RAW_STRING,
  47. APPEND_LOCAL_STRING,
  48. APPEND_TEXTID_STRING,
  49. APPEND_NUMBER,
  50. REPLACE_RAW_STRING,
  51. REPLACE_LOCAL_STRING,
  52. REPLACE_TEXTID_STRING,
  53. REPLACE_NUMBER,
  54. REPLACE_POSITIVE_NUMBER
  55. };
  56. std::vector<EMessage> message;
  57. std::vector<std::pair<EMetaText,ui32> > localStrings;
  58. std::vector<std::string> exactStrings;
  59. std::vector<std::string> stringsTextID;
  60. std::vector<int64_t> numbers;
  61. std::string getLocalString(const std::pair<EMetaText, ui32> & txt) const;
  62. public:
  63. /// Creates MetaString and appends provided raw string to it
  64. static MetaString createFromRawString(const std::string & value);
  65. /// Creates MetaString and appends provided text ID string to it
  66. static MetaString createFromTextID(const std::string & value);
  67. /// Appends local string to resulting string
  68. void appendLocalString(EMetaText type, ui32 serial);
  69. /// Appends raw string, without translation to resulting string
  70. void appendRawString(const std::string & value);
  71. /// Appends text ID that will be translated in output
  72. void appendTextID(const std::string & value);
  73. /// Appends specified number to resulting string
  74. void appendNumber(int64_t value);
  75. /// Replaces first '%s' placeholder in string with specified local string
  76. void replaceLocalString(EMetaText type, ui32 serial);
  77. /// Replaces first '%s' placeholder in string with specified fixed, untranslated string
  78. void replaceRawString(const std::string & txt);
  79. /// Repalces first '%s' placeholder with string ID that will be translated in output
  80. void replaceTextID(const std::string & value);
  81. /// Replaces first '%d' placeholder in string with specified number
  82. void replaceNumber(int64_t txt);
  83. /// Replaces first '%+d' placeholder in string with specified number using '+' sign as prefix
  84. void replacePositiveNumber(int64_t txt);
  85. /// Replaces first '%s' placeholder with singular or plural name depending on creatures count
  86. void replaceCreatureName(const CreatureID & id, TQuantity count);
  87. /// Replaces first '%s' placeholder with singular or plural name depending on creatures count
  88. void replaceCreatureName(const CStackBasicDescriptor & stack);
  89. /// erases any existing content in the string
  90. void clear();
  91. ///used to handle loot from creature bank
  92. std::string buildList() const;
  93. /// Convert all stored values into a single, user-readable string
  94. std::string toString() const;
  95. /// Returns true if current string is empty
  96. bool empty() const;
  97. bool operator == (const MetaString & other) const;
  98. void jsonSerialize(JsonNode & dest) const;
  99. void jsonDeserialize(const JsonNode & dest);
  100. void serializeJson(JsonSerializeFormat & handler);
  101. template <typename Handler> void serialize(Handler & h, const int version)
  102. {
  103. h & exactStrings;
  104. h & localStrings;
  105. h & stringsTextID;
  106. h & message;
  107. h & numbers;
  108. }
  109. };
  110. VCMI_LIB_NAMESPACE_END