MetaString.h 3.5 KB

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