2
0

CGeneralTextHandler.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 "JsonNode.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. /// Namespace that provides utilites for unicode support (UTF-8)
  14. namespace Unicode
  15. {
  16. /// evaluates size of UTF-8 character
  17. size_t DLL_LINKAGE getCharacterSize(char firstByte);
  18. /// test if character is a valid UTF-8 symbol
  19. /// maxSize - maximum number of bytes this symbol may consist from ( = remainer of string)
  20. bool DLL_LINKAGE isValidCharacter(const char * character, size_t maxSize);
  21. /// test if text contains ASCII-string (no need for unicode conversion)
  22. bool DLL_LINKAGE isValidASCII(const std::string & text);
  23. bool DLL_LINKAGE isValidASCII(const char * data, size_t size);
  24. /// test if text contains valid UTF-8 sequence
  25. bool DLL_LINKAGE isValidString(const std::string & text);
  26. bool DLL_LINKAGE isValidString(const char * data, size_t size);
  27. /// converts text to unicode from specified encoding or from one specified in settings
  28. std::string DLL_LINKAGE toUnicode(const std::string & text);
  29. std::string DLL_LINKAGE toUnicode(const std::string & text, const std::string & encoding);
  30. /// converts text from unicode to specified encoding or to one specified in settings
  31. /// NOTE: usage of these functions should be avoided if possible
  32. std::string DLL_LINKAGE fromUnicode(const std::string & text);
  33. std::string DLL_LINKAGE fromUnicode(const std::string & text, const std::string & encoding);
  34. ///delete (amount) UTF characters from right
  35. DLL_LINKAGE void trimRight(std::string & text, const size_t amount = 1);
  36. };
  37. class CInputStream;
  38. /// Parser for any text files from H3
  39. class DLL_LINKAGE CLegacyConfigParser
  40. {
  41. std::unique_ptr<char[]> data;
  42. char * curr;
  43. char * end;
  44. void init(const std::unique_ptr<CInputStream> & input);
  45. /// extracts part of quoted string.
  46. std::string extractQuotedPart();
  47. /// extracts quoted string. Any end of lines are ignored, double-quote is considered as "escaping"
  48. std::string extractQuotedString();
  49. /// extracts non-quoted string
  50. std::string extractNormalString();
  51. /// reads "raw" string without encoding conversion
  52. std::string readRawString();
  53. public:
  54. /// read one entry from current line. Return ""/0 if end of line reached
  55. std::string readString();
  56. float readNumber();
  57. template <typename numeric>
  58. std::vector<numeric> readNumArray(size_t size)
  59. {
  60. std::vector<numeric> ret;
  61. ret.reserve(size);
  62. while (size--)
  63. ret.push_back((numeric)readNumber());
  64. return ret;
  65. }
  66. /// returns true if next entry is empty
  67. bool isNextEntryEmpty() const;
  68. /// end current line
  69. bool endLine();
  70. CLegacyConfigParser(std::string URI);
  71. CLegacyConfigParser(const std::unique_ptr<CInputStream> & input);
  72. };
  73. class DLL_LINKAGE CGeneralTextHandler //Handles general texts
  74. {
  75. public:
  76. JsonNode localizedTexts;
  77. std::vector<std::string> allTexts;
  78. std::vector<std::string> arraytxt;
  79. std::vector<std::string> primarySkillNames;
  80. std::vector<std::string> jktexts;
  81. std::vector<std::string> heroscrn;
  82. std::vector<std::string> overview;//text for Kingdom Overview window
  83. std::vector<std::string> colors; //names of player colors ("red",...)
  84. std::vector<std::string> capColors; //names of player colors with first letter capitalized ("Red",...)
  85. std::vector<std::string> turnDurations; //turn durations for pregame (1 Minute ... Unlimited)
  86. //towns
  87. std::vector<std::string> tcommands, hcommands, fcommands; //texts for town screen, town hall screen and fort screen
  88. std::vector<std::string> tavernInfo;
  89. std::vector<std::string> tavernRumors;
  90. std::vector<std::string> qeModCommands;
  91. std::vector<std::pair<std::string,std::string>> zelp;
  92. std::vector<std::string> lossCondtions;
  93. std::vector<std::string> victoryConditions;
  94. //objects
  95. std::vector<std::string> creGens; //names of creatures' generators
  96. std::vector<std::string> creGens4; //names of multiple creatures' generators
  97. std::vector<std::string> advobtxt;
  98. std::vector<std::string> xtrainfo;
  99. std::vector<std::string> restypes; //names of resources
  100. std::map<TerrainId, std::string> terrainNames;
  101. std::vector<std::string> randsign;
  102. std::vector<std::pair<std::string,std::string>> mines; //first - name; second - event description
  103. std::vector<std::string> seerEmpty;
  104. std::vector<std::vector<std::vector<std::string>>> quests; //[quest][type][index]
  105. //type: quest, progress, complete, rollover, log OR time limit //index: 0-2 seer hut, 3-5 border guard
  106. std::vector<std::string> seerNames;
  107. std::vector<std::string> tentColors;
  108. //sec skills
  109. std::vector<std::string> levels;
  110. std::vector<std::string> zcrexp; //more or less useful content of that file
  111. //commanders
  112. std::vector<std::string> znpc00; //more or less useful content of that file
  113. //campaigns
  114. std::vector<std::string> campaignMapNames;
  115. std::vector<std::vector<std::string>> campaignRegionNames;
  116. static void readToVector(std::string sourceName, std::vector<std::string> &dest);
  117. int32_t pluralText(const int32_t textIndex, const int32_t count) const;
  118. CGeneralTextHandler();
  119. CGeneralTextHandler(const CGeneralTextHandler&) = delete;
  120. CGeneralTextHandler operator=(const CGeneralTextHandler&) = delete;
  121. };
  122. VCMI_LIB_NAMESPACE_END