CGeneralTextHandler.h 5.3 KB

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