CGeneralTextHandler.h 7.0 KB

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