CGeneralTextHandler.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * CGeneralTextHandler.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. #include "JsonNode.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. /// Namespace that provides utilites for unicode support (UTF-8)
  14. namespace Unicode
  15. {
  16. /// evaluates size of UTF-8 character
  17. size_t DLL_LINKAGE getCharacterSize(char firstByte);
  18. /// test if character is a valid UTF-8 symbol
  19. /// maxSize - maximum number of bytes this symbol may consist from ( = remainer of string)
  20. bool DLL_LINKAGE isValidCharacter(const char * character, size_t maxSize);
  21. /// test if text contains ASCII-string (no need for unicode conversion)
  22. bool DLL_LINKAGE isValidASCII(const std::string & text);
  23. bool DLL_LINKAGE isValidASCII(const char * data, size_t size);
  24. /// test if text contains valid UTF-8 sequence
  25. bool DLL_LINKAGE isValidString(const std::string & text);
  26. bool DLL_LINKAGE isValidString(const char * data, size_t size);
  27. /// converts text to unicode from specified encoding or from one specified in settings
  28. std::string DLL_LINKAGE toUnicode(const std::string & text);
  29. std::string DLL_LINKAGE toUnicode(const std::string & text, const std::string & encoding);
  30. /// converts text from unicode to specified encoding or to one specified in settings
  31. /// NOTE: usage of these functions should be avoided if possible
  32. std::string DLL_LINKAGE fromUnicode(const std::string & text);
  33. std::string DLL_LINKAGE fromUnicode(const std::string & text, const std::string & encoding);
  34. ///delete (amount) UTF characters from right
  35. DLL_LINKAGE void trimRight(std::string & text, const size_t amount = 1);
  36. };
  37. class CInputStream;
  38. /// Parser for any text files from H3
  39. class DLL_LINKAGE CLegacyConfigParser
  40. {
  41. std::unique_ptr<char[]> data;
  42. char * curr;
  43. char * end;
  44. void init(const std::unique_ptr<CInputStream> & input);
  45. /// extracts part of quoted string.
  46. std::string extractQuotedPart();
  47. /// extracts quoted string. Any end of lines are ignored, double-quote is considered as "escaping"
  48. std::string extractQuotedString();
  49. /// extracts non-quoted string
  50. std::string extractNormalString();
  51. /// reads "raw" string without encoding conversion
  52. std::string readRawString();
  53. public:
  54. /// read one entry from current line. Return ""/0 if end of line reached
  55. std::string readString();
  56. float readNumber();
  57. template <typename numeric>
  58. std::vector<numeric> readNumArray(size_t size)
  59. {
  60. std::vector<numeric> ret;
  61. ret.reserve(size);
  62. while (size--)
  63. ret.push_back((numeric)readNumber());
  64. return ret;
  65. }
  66. /// returns true if next entry is empty
  67. bool isNextEntryEmpty() const;
  68. /// end current line
  69. bool endLine();
  70. CLegacyConfigParser(std::string URI);
  71. CLegacyConfigParser(const std::unique_ptr<CInputStream> & input);
  72. };
  73. class CGeneralTextHandler;
  74. /// Small wrapper that provides text access API compatible with old code
  75. class DLL_LINKAGE LegacyTextContainer
  76. {
  77. CGeneralTextHandler & owner;
  78. std::string basePath;
  79. public:
  80. LegacyTextContainer(CGeneralTextHandler & owner, std::string const & basePath);
  81. std::string operator [](size_t index) const;
  82. };
  83. /// Small wrapper that provides help text access API compatible with old code
  84. class DLL_LINKAGE LegacyHelpContainer
  85. {
  86. CGeneralTextHandler & owner;
  87. std::string basePath;
  88. public:
  89. LegacyHelpContainer(CGeneralTextHandler & owner, std::string const & basePath);
  90. std::pair<std::string, std::string> operator[](size_t index) const;
  91. };
  92. class TextIdentifier
  93. {
  94. std::string identifier;
  95. public:
  96. std::string const & get() const
  97. {
  98. return identifier;
  99. }
  100. TextIdentifier(const char * id):
  101. identifier(id)
  102. {}
  103. TextIdentifier(std::string const & id):
  104. identifier(id)
  105. {}
  106. template<typename ... T>
  107. TextIdentifier(std::string const & id, size_t index, T ... rest):
  108. TextIdentifier(id + '.' + std::to_string(index), rest ... )
  109. {}
  110. template<typename ... T>
  111. TextIdentifier(std::string const & id, std::string const & id2, T ... rest):
  112. TextIdentifier(id + '.' + id2, rest ... )
  113. {}
  114. };
  115. /// Handles all text-related data in game
  116. class DLL_LINKAGE CGeneralTextHandler
  117. {
  118. /// map identifier -> localization
  119. std::unordered_map<std::string, std::string> stringsLocalizations;
  120. /// map localization -> identifier
  121. std::unordered_map<std::string, std::string> stringsIdentifiers;
  122. void readToVector(const std::string & sourceID, const std::string & sourceName);
  123. /// number of scenarios in specific campaign. TODO: move to a better location
  124. std::vector<size_t> scenariosCountPerCampaign;
  125. public:
  126. /// add selected string to internal storage
  127. void registerString(const TextIdentifier & UID, const std::string & localized);
  128. // returns true if identifier with such name was registered, even if not translated to current language
  129. // not required right now, can be added if necessary
  130. // bool identifierExists( const std::string identifier) const;
  131. /// returns translated version of a string that can be displayed to user
  132. template<typename ... Args>
  133. std::string translate(std::string arg1, Args ... args) const
  134. {
  135. TextIdentifier id(arg1, args ...);
  136. return deserialize(id);
  137. }
  138. /// converts translated string into locale-independent text that can be sent to another client
  139. const std::string & serialize(const std::string & identifier) const;
  140. /// converts identifier into user-readable string
  141. const std::string & deserialize(const TextIdentifier & identifier) const;
  142. /// Debug method, dumps all currently known texts into console using Json-like format
  143. void dumpAllTexts();
  144. LegacyTextContainer allTexts;
  145. LegacyTextContainer arraytxt;
  146. LegacyTextContainer primarySkillNames;
  147. LegacyTextContainer jktexts;
  148. LegacyTextContainer heroscrn;
  149. LegacyTextContainer overview;//text for Kingdom Overview window
  150. LegacyTextContainer colors; //names of player colors ("red",...)
  151. LegacyTextContainer capColors; //names of player colors with first letter capitalized ("Red",...)
  152. LegacyTextContainer turnDurations; //turn durations for pregame (1 Minute ... Unlimited)
  153. //towns
  154. LegacyTextContainer tcommands, hcommands, fcommands; //texts for town screen, town hall screen and fort screen
  155. LegacyTextContainer tavernInfo;
  156. LegacyTextContainer tavernRumors;
  157. LegacyTextContainer qeModCommands;
  158. LegacyHelpContainer zelp;
  159. LegacyTextContainer lossCondtions;
  160. LegacyTextContainer victoryConditions;
  161. //objects
  162. LegacyTextContainer advobtxt;
  163. LegacyTextContainer xtrainfo;
  164. LegacyTextContainer restypes; //names of resources
  165. LegacyTextContainer terrainNames;
  166. LegacyTextContainer randsign;
  167. LegacyTextContainer seerEmpty;
  168. LegacyTextContainer seerNames;
  169. LegacyTextContainer tentColors;
  170. //sec skills
  171. LegacyTextContainer levels;
  172. //commanders
  173. LegacyTextContainer znpc00; //more or less useful content of that file
  174. std::vector<std::string> findStringsWithPrefix(std::string const & prefix);
  175. int32_t pluralText(const int32_t textIndex, const int32_t count) const;
  176. size_t getCampaignLength(size_t campaignID) const;
  177. CGeneralTextHandler();
  178. CGeneralTextHandler(const CGeneralTextHandler&) = delete;
  179. CGeneralTextHandler operator=(const CGeneralTextHandler&) = delete;
  180. };
  181. VCMI_LIB_NAMESPACE_END