MapFormatJson.h 4.1 KB

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