2
0

CGeneralTextHandler.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. class JsonSerializeFormat;
  16. /// Parser for any text files from H3
  17. class DLL_LINKAGE CLegacyConfigParser
  18. {
  19. std::string fileEncoding;
  20. std::unique_ptr<char[]> data;
  21. char * curr;
  22. char * end;
  23. /// extracts part of quoted string.
  24. std::string extractQuotedPart();
  25. /// extracts quoted string. Any end of lines are ignored, double-quote is considered as "escaping"
  26. std::string extractQuotedString();
  27. /// extracts non-quoted string
  28. std::string extractNormalString();
  29. /// reads "raw" string without encoding conversion
  30. std::string readRawString();
  31. public:
  32. /// read one entry from current line. Return ""/0 if end of line reached
  33. std::string readString();
  34. float readNumber();
  35. template <typename numeric>
  36. std::vector<numeric> readNumArray(size_t size)
  37. {
  38. std::vector<numeric> ret;
  39. ret.reserve(size);
  40. while (size--)
  41. ret.push_back((numeric)readNumber());
  42. return ret;
  43. }
  44. /// returns true if next entry is empty
  45. bool isNextEntryEmpty() const;
  46. /// end current line
  47. bool endLine();
  48. explicit CLegacyConfigParser(const TextPath & URI);
  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 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 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. const std::string & get() const
  74. {
  75. return identifier;
  76. }
  77. TextIdentifier(const char * id):
  78. identifier(id)
  79. {}
  80. TextIdentifier(const std::string & id):
  81. identifier(id)
  82. {}
  83. template<typename... T>
  84. TextIdentifier(const std::string & id, size_t index, T... rest):
  85. TextIdentifier(id + '.' + std::to_string(index), rest...)
  86. {}
  87. template<typename... T>
  88. TextIdentifier(const std::string & id, const std::string & id2, T... rest):
  89. TextIdentifier(id + '.' + id2, rest...)
  90. {}
  91. };
  92. class DLL_LINKAGE TextLocalizationContainer
  93. {
  94. protected:
  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. template <typename Handler>
  108. void serialize(Handler & h, const int Version)
  109. {
  110. h & baseValue;
  111. h & baseLanguage;
  112. h & modContext;
  113. }
  114. };
  115. /// map identifier -> localization
  116. std::unordered_map<std::string, StringState> stringsLocalizations;
  117. std::vector<const TextLocalizationContainer *> subContainers;
  118. /// add selected string to internal storage as high-priority strings
  119. void registerStringOverride(const std::string & modContext, const std::string & language, const TextIdentifier & UID, const std::string & localized);
  120. std::string getModLanguage(const std::string & modContext);
  121. public:
  122. /// validates translation of specified language for specified mod
  123. /// returns true if localization is valid and complete
  124. /// any error messages will be written to log file
  125. bool validateTranslation(const std::string & language, const std::string & modContext, JsonNode const & file) const;
  126. /// Loads translation from provided json
  127. /// Any entries loaded by this will have priority over texts registered normally
  128. void loadTranslationOverrides(const std::string & language, const std::string & modContext, JsonNode const & file);
  129. // returns true if identifier with such name was registered, even if not translated to current language
  130. bool identifierExists(const TextIdentifier & UID) const;
  131. /// add selected string to internal storage
  132. void registerString(const std::string & modContext, const TextIdentifier & UID, const std::string & localized);
  133. void registerString(const std::string & modContext, const TextIdentifier & UID, const std::string & localized, const std::string & language);
  134. /// returns translated version of a string that can be displayed to user
  135. template<typename ... Args>
  136. std::string translate(std::string arg1, Args ... args) const
  137. {
  138. TextIdentifier id(arg1, args ...);
  139. return deserialize(id);
  140. }
  141. /// converts identifier into user-readable string
  142. const std::string & deserialize(const TextIdentifier & identifier) const;
  143. /// Debug method, dumps all currently known texts into console using Json-like format
  144. void dumpAllTexts();
  145. /// Add or override subcontainer which can store identifiers
  146. void addSubContainer(const TextLocalizationContainer & container);
  147. /// Remove subcontainer with give name
  148. void removeSubContainer(const TextLocalizationContainer & container);
  149. void jsonSerialize(JsonNode & dest) const;
  150. template <typename Handler>
  151. void serialize(Handler & h, const int Version)
  152. {
  153. std::string key;
  154. auto sz = stringsLocalizations.size();
  155. h & sz;
  156. if(h.saving)
  157. {
  158. for(auto s : stringsLocalizations)
  159. {
  160. key = s.first;
  161. h & key;
  162. h & s.second;
  163. }
  164. }
  165. else
  166. {
  167. for(size_t i = 0; i < sz; ++i)
  168. {
  169. h & key;
  170. h & stringsLocalizations[key];
  171. }
  172. }
  173. }
  174. };
  175. /// Handles all text-related data in game
  176. class DLL_LINKAGE CGeneralTextHandler: public TextLocalizationContainer
  177. {
  178. void readToVector(const std::string & sourceID, const std::string & sourceName);
  179. /// number of scenarios in specific campaign. TODO: move to a better location
  180. std::vector<size_t> scenariosCountPerCampaign;
  181. public:
  182. LegacyTextContainer allTexts;
  183. LegacyTextContainer arraytxt;
  184. LegacyTextContainer primarySkillNames;
  185. LegacyTextContainer jktexts;
  186. LegacyTextContainer heroscrn;
  187. LegacyTextContainer overview;//text for Kingdom Overview window
  188. LegacyTextContainer colors; //names of player colors ("red",...)
  189. LegacyTextContainer capColors; //names of player colors with first letter capitalized ("Red",...)
  190. LegacyTextContainer turnDurations; //turn durations for pregame (1 Minute ... Unlimited)
  191. //towns
  192. LegacyTextContainer tcommands, hcommands, fcommands; //texts for town screen, town hall screen and fort screen
  193. LegacyTextContainer tavernInfo;
  194. LegacyTextContainer tavernRumors;
  195. LegacyTextContainer qeModCommands;
  196. LegacyHelpContainer zelp;
  197. LegacyTextContainer lossCondtions;
  198. LegacyTextContainer victoryConditions;
  199. //objects
  200. LegacyTextContainer advobtxt;
  201. LegacyTextContainer restypes; //names of resources
  202. LegacyTextContainer randsign;
  203. LegacyTextContainer seerEmpty;
  204. LegacyTextContainer seerNames;
  205. LegacyTextContainer tentColors;
  206. //sec skills
  207. LegacyTextContainer levels;
  208. //commanders
  209. LegacyTextContainer znpc00; //more or less useful content of that file
  210. std::vector<std::string> findStringsWithPrefix(const std::string & prefix);
  211. int32_t pluralText(int32_t textIndex, int32_t count) const;
  212. size_t getCampaignLength(size_t campaignID) const;
  213. CGeneralTextHandler();
  214. CGeneralTextHandler(const CGeneralTextHandler&) = delete;
  215. CGeneralTextHandler operator=(const CGeneralTextHandler&) = delete;
  216. /// Attempts to detect encoding & language of H3 files
  217. static void detectInstallParameters();
  218. /// Returns name of language preferred by user
  219. static std::string getPreferredLanguage();
  220. /// Returns name of language of Heroes III text files
  221. static std::string getInstalledLanguage();
  222. /// Returns name of encoding of Heroes III text files
  223. static std::string getInstalledEncoding();
  224. };
  225. VCMI_LIB_NAMESPACE_END