MetaString.h 4.3 KB

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