CGeneralTextHandler.h 7.1 KB

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