CGeneralTextHandler.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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, 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. explicit CLegacyConfigParser(std::string URI);
  71. explicit 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. struct StringState
  119. {
  120. /// Human-readable string that was added on registration
  121. std::string baseValue;
  122. /// Language of base string
  123. std::string baseLanguage;
  124. /// Translated human-readable string
  125. std::string overrideValue;
  126. /// Language of the override string
  127. std::string overrideLanguage;
  128. /// ID of mod that created this string
  129. std::string modContext;
  130. };
  131. /// map identifier -> localization
  132. std::unordered_map<std::string, StringState> stringsLocalizations;
  133. void readToVector(const std::string & sourceID, const std::string & sourceName);
  134. /// number of scenarios in specific campaign. TODO: move to a better location
  135. std::vector<size_t> scenariosCountPerCampaign;
  136. std::string getModLanguage(const std::string & modContext);
  137. public:
  138. /// validates translation of specified language for specified mod
  139. /// returns true if localization is valid and complete
  140. /// any error messages will be written to log file
  141. bool validateTranslation(const std::string & language, const std::string & modContext, JsonNode const & file) const;
  142. /// Loads translation from provided json
  143. /// Any entries loaded by this will have priority over texts registered normally
  144. void loadTranslationOverrides(const std::string & language, const std::string & modContext, JsonNode const & file);
  145. /// add selected string to internal storage
  146. void registerString(const std::string & modContext, const TextIdentifier & UID, const std::string & localized);
  147. /// add selected string to internal storage as high-priority strings
  148. void registerStringOverride(const std::string & modContext, const std::string & language, const TextIdentifier & UID, const std::string & localized);
  149. // returns true if identifier with such name was registered, even if not translated to current language
  150. // not required right now, can be added if necessary
  151. // bool identifierExists( const std::string identifier) const;
  152. /// returns translated version of a string that can be displayed to user
  153. template<typename ... Args>
  154. std::string translate(std::string arg1, Args ... args) const
  155. {
  156. TextIdentifier id(arg1, args ...);
  157. return deserialize(id);
  158. }
  159. /// converts identifier into user-readable string
  160. const std::string & deserialize(const TextIdentifier & identifier) const;
  161. /// Debug method, dumps all currently known texts into console using Json-like format
  162. void dumpAllTexts();
  163. LegacyTextContainer allTexts;
  164. LegacyTextContainer arraytxt;
  165. LegacyTextContainer primarySkillNames;
  166. LegacyTextContainer jktexts;
  167. LegacyTextContainer heroscrn;
  168. LegacyTextContainer overview;//text for Kingdom Overview window
  169. LegacyTextContainer colors; //names of player colors ("red",...)
  170. LegacyTextContainer capColors; //names of player colors with first letter capitalized ("Red",...)
  171. LegacyTextContainer turnDurations; //turn durations for pregame (1 Minute ... Unlimited)
  172. //towns
  173. LegacyTextContainer tcommands, hcommands, fcommands; //texts for town screen, town hall screen and fort screen
  174. LegacyTextContainer tavernInfo;
  175. LegacyTextContainer tavernRumors;
  176. LegacyTextContainer qeModCommands;
  177. LegacyHelpContainer zelp;
  178. LegacyTextContainer lossCondtions;
  179. LegacyTextContainer victoryConditions;
  180. //objects
  181. LegacyTextContainer advobtxt;
  182. LegacyTextContainer restypes; //names of resources
  183. LegacyTextContainer randsign;
  184. LegacyTextContainer seerEmpty;
  185. LegacyTextContainer seerNames;
  186. LegacyTextContainer tentColors;
  187. //sec skills
  188. LegacyTextContainer levels;
  189. //commanders
  190. LegacyTextContainer znpc00; //more or less useful content of that file
  191. std::vector<std::string> findStringsWithPrefix(std::string const & prefix);
  192. int32_t pluralText(int32_t textIndex, int32_t count) const;
  193. size_t getCampaignLength(size_t campaignID) const;
  194. CGeneralTextHandler();
  195. CGeneralTextHandler(const CGeneralTextHandler&) = delete;
  196. CGeneralTextHandler operator=(const CGeneralTextHandler&) = delete;
  197. /// Attempts to detect encoding & language of H3 files
  198. static void detectInstallParameters();
  199. /// Returns name of language preferred by user
  200. static std::string getPreferredLanguage();
  201. /// Returns name of language of Heroes III text files
  202. static std::string getInstalledLanguage();
  203. /// Returns name of encoding of Heroes III text files
  204. static std::string getInstalledEncoding();
  205. };
  206. VCMI_LIB_NAMESPACE_END