CGeneralTextHandler.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. class DLL_LINKAGE TextLocalizationContainer
  92. {
  93. protected:
  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. template <typename Handler>
  107. void serialize(Handler & h, const int Version)
  108. {
  109. h & baseValue;
  110. h & baseLanguage;
  111. h & modContext;
  112. }
  113. };
  114. /// map identifier -> localization
  115. std::unordered_map<std::string, StringState> stringsLocalizations;
  116. std::set<const TextLocalizationContainer *> subContainers;
  117. /// add selected string to internal storage as high-priority strings
  118. void registerStringOverride(const std::string & modContext, const std::string & language, const TextIdentifier & UID, const std::string & localized);
  119. std::string getModLanguage(const std::string & modContext);
  120. public:
  121. /// validates translation of specified language for specified mod
  122. /// returns true if localization is valid and complete
  123. /// any error messages will be written to log file
  124. bool validateTranslation(const std::string & language, const std::string & modContext, JsonNode const & file) const;
  125. /// Loads translation from provided json
  126. /// Any entries loaded by this will have priority over texts registered normally
  127. void loadTranslationOverrides(const std::string & language, const std::string & modContext, JsonNode const & file);
  128. // returns true if identifier with such name was registered, even if not translated to current language
  129. bool identifierExists(const TextIdentifier & UID) const;
  130. /// add selected string to internal storage
  131. void registerString(const std::string & modContext, const TextIdentifier & UID, const std::string & localized);
  132. /// returns translated version of a string that can be displayed to user
  133. template<typename ... Args>
  134. std::string translate(std::string arg1, Args ... args) const
  135. {
  136. TextIdentifier id(arg1, args ...);
  137. return deserialize(id);
  138. }
  139. /// converts identifier into user-readable string
  140. const std::string & deserialize(const TextIdentifier & identifier) const;
  141. /// Debug method, dumps all currently known texts into console using Json-like format
  142. void dumpAllTexts();
  143. /// Add or override subcontainer which can store identifiers
  144. void addSubContainer(const TextLocalizationContainer & container);
  145. /// Remove subcontainer with give name
  146. void removeSubContainer(const TextLocalizationContainer & container);
  147. template <typename Handler>
  148. void serialize(Handler & h, const int Version)
  149. {
  150. std::string key;
  151. auto sz = stringsLocalizations.size();
  152. h & sz;
  153. if(h.saving)
  154. {
  155. for(auto s : stringsLocalizations)
  156. {
  157. key = s.first;
  158. h & key;
  159. h & s.second;
  160. }
  161. }
  162. else
  163. {
  164. for(size_t i = 0; i < sz; ++i)
  165. {
  166. h & key;
  167. h & stringsLocalizations[key];
  168. }
  169. }
  170. }
  171. };
  172. /// Handles all text-related data in game
  173. class DLL_LINKAGE CGeneralTextHandler: public TextLocalizationContainer
  174. {
  175. void readToVector(const std::string & sourceID, const std::string & sourceName);
  176. /// number of scenarios in specific campaign. TODO: move to a better location
  177. std::vector<size_t> scenariosCountPerCampaign;
  178. public:
  179. LegacyTextContainer allTexts;
  180. LegacyTextContainer arraytxt;
  181. LegacyTextContainer primarySkillNames;
  182. LegacyTextContainer jktexts;
  183. LegacyTextContainer heroscrn;
  184. LegacyTextContainer overview;//text for Kingdom Overview window
  185. LegacyTextContainer colors; //names of player colors ("red",...)
  186. LegacyTextContainer capColors; //names of player colors with first letter capitalized ("Red",...)
  187. LegacyTextContainer turnDurations; //turn durations for pregame (1 Minute ... Unlimited)
  188. //towns
  189. LegacyTextContainer tcommands, hcommands, fcommands; //texts for town screen, town hall screen and fort screen
  190. LegacyTextContainer tavernInfo;
  191. LegacyTextContainer tavernRumors;
  192. LegacyTextContainer qeModCommands;
  193. LegacyHelpContainer zelp;
  194. LegacyTextContainer lossCondtions;
  195. LegacyTextContainer victoryConditions;
  196. //objects
  197. LegacyTextContainer advobtxt;
  198. LegacyTextContainer restypes; //names of resources
  199. LegacyTextContainer randsign;
  200. LegacyTextContainer seerEmpty;
  201. LegacyTextContainer seerNames;
  202. LegacyTextContainer tentColors;
  203. //sec skills
  204. LegacyTextContainer levels;
  205. //commanders
  206. LegacyTextContainer znpc00; //more or less useful content of that file
  207. std::vector<std::string> findStringsWithPrefix(const std::string & prefix);
  208. int32_t pluralText(int32_t textIndex, int32_t count) const;
  209. size_t getCampaignLength(size_t campaignID) const;
  210. CGeneralTextHandler();
  211. CGeneralTextHandler(const CGeneralTextHandler&) = delete;
  212. CGeneralTextHandler operator=(const CGeneralTextHandler&) = delete;
  213. /// Attempts to detect encoding & language of H3 files
  214. static void detectInstallParameters();
  215. /// Returns name of language preferred by user
  216. static std::string getPreferredLanguage();
  217. /// Returns name of language of Heroes III text files
  218. static std::string getInstalledLanguage();
  219. /// Returns name of encoding of Heroes III text files
  220. static std::string getInstalledEncoding();
  221. };
  222. VCMI_LIB_NAMESPACE_END