MetaString.h 4.5 KB

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