MetaString.h 4.6 KB

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