CGeneralTextHandler.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 "filesystem/ResourcePath.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. class CInputStream;
  14. class JsonNode;
  15. /// Parser for any text files from H3
  16. class DLL_LINKAGE CLegacyConfigParser
  17. {
  18. std::string fileEncoding;
  19. std::unique_ptr<char[]> data;
  20. char * curr;
  21. char * end;
  22. /// extracts part of quoted string.
  23. std::string extractQuotedPart();
  24. /// extracts quoted string. Any end of lines are ignored, double-quote is considered as "escaping"
  25. std::string extractQuotedString();
  26. /// extracts non-quoted string
  27. std::string extractNormalString();
  28. /// reads "raw" string without encoding conversion
  29. std::string readRawString();
  30. public:
  31. /// read one entry from current line. Return ""/0 if end of line reached
  32. std::string readString();
  33. float readNumber();
  34. template <typename numeric>
  35. std::vector<numeric> readNumArray(size_t size)
  36. {
  37. std::vector<numeric> ret;
  38. ret.reserve(size);
  39. while (size--)
  40. ret.push_back((numeric)readNumber());
  41. return ret;
  42. }
  43. /// returns true if next entry is empty
  44. bool isNextEntryEmpty() const;
  45. /// end current line
  46. bool endLine();
  47. explicit CLegacyConfigParser(const TextPath & URI);
  48. };
  49. class CGeneralTextHandler;
  50. /// Small wrapper that provides text access API compatible with old code
  51. class DLL_LINKAGE LegacyTextContainer
  52. {
  53. CGeneralTextHandler & owner;
  54. std::string basePath;
  55. public:
  56. LegacyTextContainer(CGeneralTextHandler & owner, std::string basePath);
  57. std::string operator [](size_t index) const;
  58. };
  59. /// Small wrapper that provides help text access API compatible with old code
  60. class DLL_LINKAGE LegacyHelpContainer
  61. {
  62. CGeneralTextHandler & owner;
  63. std::string basePath;
  64. public:
  65. LegacyHelpContainer(CGeneralTextHandler & owner, std::string basePath);
  66. std::pair<std::string, std::string> operator[](size_t index) const;
  67. };
  68. class TextIdentifier
  69. {
  70. std::string identifier;
  71. public:
  72. const std::string & get() const
  73. {
  74. return identifier;
  75. }
  76. TextIdentifier(const char * id):
  77. identifier(id)
  78. {}
  79. TextIdentifier(const std::string & id):
  80. identifier(id)
  81. {}
  82. template<typename... T>
  83. TextIdentifier(const std::string & id, size_t index, T... rest):
  84. TextIdentifier(id + '.' + std::to_string(index), rest...)
  85. {}
  86. template<typename... T>
  87. TextIdentifier(const std::string & id, const std::string & id2, T... rest):
  88. TextIdentifier(id + '.' + id2, rest...)
  89. {}
  90. };
  91. /// Handles all text-related data in game
  92. class DLL_LINKAGE CGeneralTextHandler
  93. {
  94. struct StringState
  95. {
  96. /// Human-readable string that was added on registration
  97. std::string baseValue;
  98. /// Language of base string
  99. std::string baseLanguage;
  100. /// Translated human-readable string
  101. std::string overrideValue;
  102. /// Language of the override string
  103. std::string overrideLanguage;
  104. /// ID of mod that created this string
  105. std::string modContext;
  106. };
  107. /// map identifier -> localization
  108. std::unordered_map<std::string, StringState> stringsLocalizations;
  109. void readToVector(const std::string & sourceID, const std::string & sourceName);
  110. /// number of scenarios in specific campaign. TODO: move to a better location
  111. std::vector<size_t> scenariosCountPerCampaign;
  112. std::string getModLanguage(const std::string & modContext);
  113. /// add selected string to internal storage as high-priority strings
  114. void registerStringOverride(const std::string & modContext, const std::string & language, const TextIdentifier & UID, const std::string & localized);
  115. public:
  116. /// validates translation of specified language for specified mod
  117. /// returns true if localization is valid and complete
  118. /// any error messages will be written to log file
  119. bool validateTranslation(const std::string & language, const std::string & modContext, JsonNode const & file) const;
  120. /// Loads translation from provided json
  121. /// Any entries loaded by this will have priority over texts registered normally
  122. void loadTranslationOverrides(const std::string & language, const std::string & modContext, JsonNode const & file);
  123. /// add selected string to internal storage
  124. void registerString(const std::string & modContext, const TextIdentifier & UID, const std::string & localized);
  125. // returns true if identifier with such name was registered, even if not translated to current language
  126. // not required right now, can be added if necessary
  127. // bool identifierExists( const std::string identifier) const;
  128. /// returns translated version of a string that can be displayed to user
  129. template<typename ... Args>
  130. std::string translate(std::string arg1, Args ... args) const
  131. {
  132. TextIdentifier id(arg1, args ...);
  133. return deserialize(id);
  134. }
  135. /// converts identifier into user-readable string
  136. const std::string & deserialize(const TextIdentifier & identifier) const;
  137. /// Debug method, dumps all currently known texts into console using Json-like format
  138. void dumpAllTexts();
  139. LegacyTextContainer allTexts;
  140. LegacyTextContainer arraytxt;
  141. LegacyTextContainer primarySkillNames;
  142. LegacyTextContainer jktexts;
  143. LegacyTextContainer heroscrn;
  144. LegacyTextContainer overview;//text for Kingdom Overview window
  145. LegacyTextContainer colors; //names of player colors ("red",...)
  146. LegacyTextContainer capColors; //names of player colors with first letter capitalized ("Red",...)
  147. LegacyTextContainer turnDurations; //turn durations for pregame (1 Minute ... Unlimited)
  148. //towns
  149. LegacyTextContainer tcommands, hcommands, fcommands; //texts for town screen, town hall screen and fort screen
  150. LegacyTextContainer tavernInfo;
  151. LegacyTextContainer tavernRumors;
  152. LegacyTextContainer qeModCommands;
  153. LegacyHelpContainer zelp;
  154. LegacyTextContainer lossCondtions;
  155. LegacyTextContainer victoryConditions;
  156. //objects
  157. LegacyTextContainer advobtxt;
  158. LegacyTextContainer restypes; //names of resources
  159. LegacyTextContainer randsign;
  160. LegacyTextContainer seerEmpty;
  161. LegacyTextContainer seerNames;
  162. LegacyTextContainer tentColors;
  163. //sec skills
  164. LegacyTextContainer levels;
  165. //commanders
  166. LegacyTextContainer znpc00; //more or less useful content of that file
  167. std::vector<std::string> findStringsWithPrefix(const std::string & prefix);
  168. int32_t pluralText(int32_t textIndex, int32_t count) const;
  169. size_t getCampaignLength(size_t campaignID) const;
  170. CGeneralTextHandler();
  171. CGeneralTextHandler(const CGeneralTextHandler&) = delete;
  172. CGeneralTextHandler operator=(const CGeneralTextHandler&) = delete;
  173. /// Attempts to detect encoding & language of H3 files
  174. static void detectInstallParameters();
  175. /// Returns name of language preferred by user
  176. static std::string getPreferredLanguage();
  177. /// Returns name of language of Heroes III text files
  178. static std::string getInstalledLanguage();
  179. /// Returns name of encoding of Heroes III text files
  180. static std::string getInstalledEncoding();
  181. };
  182. VCMI_LIB_NAMESPACE_END