MapFormatJson.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * MapFormatJSON.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 "CMapService.h"
  12. #include "../JsonNode.h"
  13. #include "../filesystem/CZipSaver.h"
  14. #include "../filesystem/CZipLoader.h"
  15. class TriggeredEvent;
  16. struct TerrainTile;
  17. class DLL_LINKAGE CMapFormatJson
  18. {
  19. public:
  20. static const int VERSION_MAJOR;
  21. static const int VERSION_MINOR;
  22. static const std::string HEADER_FILE_NAME;
  23. protected:
  24. /** ptr to the map object which gets filled by data from the buffer or written to buffer */
  25. CMap * map;
  26. /**
  27. * ptr to the map header object which gets filled by data from the buffer.
  28. * (when loading map and mapHeader point to the same object)
  29. */
  30. std::unique_ptr<CMapHeader> mapHeader;
  31. /**
  32. * Reads triggered events, including victory/loss conditions
  33. */
  34. void readTriggeredEvents(const JsonNode & input);
  35. /**
  36. * Writes triggered events, including victory/loss conditions
  37. */
  38. void writeTriggeredEvents(JsonNode & output);
  39. /**
  40. * Reads one of triggered events
  41. */
  42. void readTriggeredEvent(TriggeredEvent & event, const JsonNode & source);
  43. /**
  44. * Writes one of triggered events
  45. */
  46. void writeTriggeredEvent(const TriggeredEvent & event, JsonNode & dest);
  47. };
  48. class DLL_LINKAGE CMapPatcher : public CMapFormatJson, public IMapPatcher
  49. {
  50. public:
  51. /**
  52. * Default constructor.
  53. *
  54. * @param stream. A stream containing the map data.
  55. */
  56. CMapPatcher(JsonNode stream);
  57. public: //IMapPatcher
  58. /**
  59. * Modifies supplied map header using Json data
  60. *
  61. */
  62. void patchMapHeader(std::unique_ptr<CMapHeader> & header) override;
  63. private:
  64. /**
  65. * Reads subset of header that can be replaced by patching.
  66. */
  67. void readPatchData();
  68. const JsonNode input;
  69. };
  70. class DLL_LINKAGE CMapFormatZip : public CMapFormatJson
  71. {
  72. public:
  73. CMapFormatZip(CInputOutputStream * stream);
  74. protected:
  75. CInputOutputStream * buffer;
  76. std::shared_ptr<CIOApi> ioApi;
  77. };
  78. class DLL_LINKAGE CMapLoaderJson : public CMapFormatZip, public IMapLoader
  79. {
  80. public:
  81. /**
  82. * Default constructor.
  83. *
  84. * @param stream a stream containing the map data
  85. */
  86. CMapLoaderJson(CInputOutputStream * stream);
  87. /**
  88. * Loads the VCMI/Json map file.
  89. *
  90. * @return a unique ptr of the loaded map class
  91. */
  92. std::unique_ptr<CMap> loadMap() override;
  93. /**
  94. * Loads the VCMI/Json map header.
  95. *
  96. * @return a unique ptr of the loaded map header class
  97. */
  98. std::unique_ptr<CMapHeader> loadMapHeader() override;
  99. private:
  100. /**
  101. * Reads complete map.
  102. */
  103. void readMap();
  104. /**
  105. * Reads the map header.
  106. */
  107. void readHeader();
  108. /**
  109. * Reads player information.
  110. */
  111. void readPlayerInfo();
  112. void readTerrainTile(const std::string & src, TerrainTile & tile);
  113. void readTerrainLevel(const JsonNode & src, const int index);
  114. void readTerrain();
  115. const JsonNode readJson(const std::string & archiveFilename);
  116. CZipLoader loader;
  117. };
  118. class DLL_LINKAGE CMapSaverJson : public CMapFormatZip, public IMapSaver
  119. {
  120. public:
  121. /**
  122. * Default constructor.
  123. *
  124. * @param stream a stream to save the map to
  125. */
  126. CMapSaverJson(CInputOutputStream * stream);
  127. ~CMapSaverJson();
  128. /**
  129. * Actually saves the VCMI/Json map into stream.
  130. *
  131. */
  132. void saveMap(const std::unique_ptr<CMap> & map) override;
  133. private:
  134. /**
  135. * Save @data as json file with specified @filename
  136. *
  137. */
  138. void addToArchive(const JsonNode & data, const std::string & filename);
  139. void writeHeader();
  140. /**
  141. * Save all players info to header
  142. * @param output serialized header
  143. */
  144. void writePlayerInfo(JsonNode & output);
  145. /**
  146. * Save one player info
  147. * @param output empty object
  148. */
  149. void writePlayerInfo(const PlayerInfo & info, JsonNode & output);
  150. const std::string writeTerrainTile(const TerrainTile & tile);
  151. JsonNode writeTerrainLevel(const int index);
  152. void writeTerrain();
  153. CZipSaver saver;
  154. };