MetaString.h 3.7 KB

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