MetaString.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. APPEND_EOL
  49. };
  50. std::vector<EMessage> message;
  51. std::vector<std::pair<EMetaText,ui32> > localStrings;
  52. std::vector<std::string> exactStrings;
  53. std::vector<std::string> stringsTextID;
  54. std::vector<int64_t> numbers;
  55. std::string getLocalString(const std::pair<EMetaText, ui32> & txt) const;
  56. public:
  57. /// Creates MetaString and appends provided raw string to it
  58. static MetaString createFromRawString(const std::string & value);
  59. /// Creates MetaString and appends provided text ID string to it
  60. static MetaString createFromTextID(const std::string & value);
  61. /// Appends local string to resulting string
  62. void appendLocalString(EMetaText type, ui32 serial);
  63. /// Appends raw string, without translation to resulting string
  64. void appendRawString(const std::string & value);
  65. /// Appends text ID that will be translated in output
  66. void appendTextID(const std::string & value);
  67. /// Appends specified number to resulting string
  68. void appendNumber(int64_t value);
  69. void appendName(const ArtifactID& id);
  70. void appendName(const SpellID& id);
  71. void appendName(const PlayerColor& id);
  72. void appendName(const CreatureID & id, TQuantity count);
  73. void appendName(const GameResID& id);
  74. void appendNameSingular(const CreatureID & id);
  75. void appendNamePlural(const CreatureID & id);
  76. void appendEOL();
  77. /// Replaces first '%s' placeholder in string with specified local string
  78. void replaceLocalString(EMetaText type, ui32 serial);
  79. /// Replaces first '%s' placeholder in string with specified fixed, untranslated string
  80. void replaceRawString(const std::string & txt);
  81. /// Replaces first '%s' placeholder with string ID that will be translated in output
  82. void replaceTextID(const std::string & value);
  83. /// Replaces first '%d' placeholder in string with specified number
  84. void replaceNumber(int64_t txt);
  85. /// Replaces first '%+d' placeholder in string with specified number using '+' sign as prefix
  86. void replacePositiveNumber(int64_t txt);
  87. void replaceName(const ArtifactID & id);
  88. void replaceName(const MapObjectID& id);
  89. void replaceName(const PlayerColor& id);
  90. void replaceName(const SecondarySkill& id);
  91. void replaceName(const SpellID& id);
  92. void replaceName(const GameResID& id);
  93. /// Replaces first '%s' placeholder with singular or plural name depending on creatures count
  94. void replaceName(const CreatureID & id, TQuantity count);
  95. void replaceNameSingular(const CreatureID & id);
  96. void replaceNamePlural(const CreatureID & id);
  97. /// Replaces first '%s' placeholder with singular or plural name depending on creatures count
  98. void replaceName(const CStackBasicDescriptor & stack);
  99. /// erases any existing content in the string
  100. void clear();
  101. ///used to handle loot from creature bank
  102. std::string buildList() const;
  103. /// Convert all stored values into a single, user-readable string
  104. std::string toString() const;
  105. /// Returns true if current string is empty
  106. bool empty() const;
  107. bool operator == (const MetaString & other) const;
  108. void jsonSerialize(JsonNode & dest) const;
  109. void jsonDeserialize(const JsonNode & dest);
  110. void serializeJson(JsonSerializeFormat & handler);
  111. template <typename Handler> void serialize(Handler & h)
  112. {
  113. h & exactStrings;
  114. h & localStrings;
  115. h & stringsTextID;
  116. h & message;
  117. h & numbers;
  118. }
  119. };
  120. VCMI_LIB_NAMESPACE_END