CGeneralTextHandler.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. const 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. /// Handles all text-related data in game
  93. class DLL_LINKAGE CGeneralTextHandler
  94. {
  95. /// map identifier -> localization
  96. std::unordered_map<std::string, std::string> stringsLocalizations;
  97. /// map localization -> identifier
  98. std::unordered_map<std::string, std::string> stringsIdentifiers;
  99. /// add selected string to internal storage
  100. void registerString(const std::string & UID, const std::string & localized);
  101. void registerH3String(const std::string & file, size_t index, const std::string & localized);
  102. void readToVector(std::string sourceID, std::string sourceName);
  103. /// number of scenarios in specific campaign. TODO: move to a better location
  104. std::vector<size_t> scenariosCountPerCampaign;
  105. public:
  106. // returns true if identifier with such name was registered, even if not translated to current language
  107. // not required right now, can be added if necessary
  108. // bool identifierExists( const std::string identifier) const;
  109. /// returns translated version of a string that can be displayed to user
  110. const std::string & translate(const std::string & identifier) const;
  111. /// returns translated version of a string that can be displayed to user, H3-array compatibility version
  112. const std::string & translate(const std::string & identifier, size_t index) const;
  113. /// converts translated string into locale-independent text that can be sent to another client
  114. const std::string & serialize(const std::string & identifier) const;
  115. /// converts identifier into user-readable string, may be identical to 'translate' but reserved for serialization calls
  116. const std::string & deserialize(const std::string & identifier) const;
  117. /// Debug method, dumps all currently known texts into console using Json-like format
  118. void dumpAllTexts();
  119. LegacyTextContainer allTexts;
  120. LegacyTextContainer arraytxt;
  121. LegacyTextContainer primarySkillNames;
  122. LegacyTextContainer jktexts;
  123. LegacyTextContainer heroscrn;
  124. LegacyTextContainer overview;//text for Kingdom Overview window
  125. LegacyTextContainer colors; //names of player colors ("red",...)
  126. LegacyTextContainer capColors; //names of player colors with first letter capitalized ("Red",...)
  127. LegacyTextContainer turnDurations; //turn durations for pregame (1 Minute ... Unlimited)
  128. //towns
  129. LegacyTextContainer tcommands, hcommands, fcommands; //texts for town screen, town hall screen and fort screen
  130. LegacyTextContainer tavernInfo;
  131. LegacyTextContainer tavernRumors;
  132. LegacyTextContainer qeModCommands;
  133. LegacyHelpContainer zelp;
  134. LegacyTextContainer lossCondtions;
  135. LegacyTextContainer victoryConditions;
  136. //objects
  137. LegacyTextContainer creGens; //names of creatures' generators
  138. LegacyTextContainer creGens4; //names of multiple creatures' generators
  139. LegacyTextContainer advobtxt;
  140. LegacyTextContainer xtrainfo;
  141. LegacyTextContainer restypes; //names of resources
  142. LegacyTextContainer terrainNames;
  143. LegacyTextContainer randsign;
  144. LegacyTextContainer seerEmpty;
  145. LegacyTextContainer seerNames;
  146. LegacyTextContainer tentColors;
  147. //sec skills
  148. LegacyTextContainer levels;
  149. //commanders
  150. LegacyTextContainer znpc00; //more or less useful content of that file
  151. std::vector<std::string> findStringsWithPrefix(std::string const & prefix);
  152. int32_t pluralText(const int32_t textIndex, const int32_t count) const;
  153. size_t getCampaignLength(size_t campaignID) const;
  154. CGeneralTextHandler();
  155. CGeneralTextHandler(const CGeneralTextHandler&) = delete;
  156. CGeneralTextHandler operator=(const CGeneralTextHandler&) = delete;
  157. };
  158. VCMI_LIB_NAMESPACE_END