MapFormatJson.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. #include "../GameConstants.h"
  16. class TriggeredEvent;
  17. struct TerrainTile;
  18. struct PlayerInfo;
  19. class CGObjectInstance;
  20. class AObjectTypeHandler;
  21. class JsonDeserializer;
  22. class JsonSerializer;
  23. class DLL_LINKAGE CMapFormatJson
  24. {
  25. public:
  26. static const int VERSION_MAJOR;
  27. static const int VERSION_MINOR;
  28. static const std::string HEADER_FILE_NAME;
  29. static const std::string OBJECTS_FILE_NAME;
  30. protected:
  31. /** ptr to the map object which gets filled by data from the buffer or written to buffer */
  32. CMap * map;
  33. /**
  34. * ptr to the map header object which gets filled by data from the buffer.
  35. * (when loading map and mapHeader point to the same object)
  36. */
  37. std::unique_ptr<CMapHeader> mapHeader;
  38. /**
  39. * Reads triggered events, including victory/loss conditions
  40. */
  41. void readTriggeredEvents(const JsonNode & input);
  42. /**
  43. * Writes triggered events, including victory/loss conditions
  44. */
  45. void writeTriggeredEvents(JsonNode & output);
  46. /**
  47. * Reads one of triggered events
  48. */
  49. void readTriggeredEvent(TriggeredEvent & event, const JsonNode & source);
  50. /**
  51. * Writes one of triggered events
  52. */
  53. void writeTriggeredEvent(const TriggeredEvent & event, JsonNode & dest);
  54. };
  55. class DLL_LINKAGE CMapPatcher : public CMapFormatJson, public IMapPatcher
  56. {
  57. public:
  58. /**
  59. * Default constructor.
  60. *
  61. * @param stream. A stream containing the map data.
  62. */
  63. CMapPatcher(JsonNode stream);
  64. public: //IMapPatcher
  65. /**
  66. * Modifies supplied map header using Json data
  67. *
  68. */
  69. void patchMapHeader(std::unique_ptr<CMapHeader> & header) override;
  70. private:
  71. /**
  72. * Reads subset of header that can be replaced by patching.
  73. */
  74. void readPatchData();
  75. const JsonNode input;
  76. };
  77. class DLL_LINKAGE CMapLoaderJson : public CMapFormatJson, public IMapLoader
  78. {
  79. public:
  80. /**
  81. * Constructor.
  82. *
  83. * @param stream a stream containing the map data
  84. */
  85. CMapLoaderJson(CInputStream * stream);
  86. /**
  87. * Loads the VCMI/Json map file.
  88. *
  89. * @return a unique ptr of the loaded map class
  90. */
  91. std::unique_ptr<CMap> loadMap() override;
  92. /**
  93. * Loads the VCMI/Json map header.
  94. *
  95. * @return a unique ptr of the loaded map header class
  96. */
  97. std::unique_ptr<CMapHeader> loadMapHeader() override;
  98. private:
  99. struct MapObjectLoader
  100. {
  101. MapObjectLoader(CMapLoaderJson * _owner, const JsonMap::value_type & json);
  102. CMapLoaderJson * owner;
  103. CGObjectInstance * instance;
  104. std::shared_ptr<AObjectTypeHandler> handler;
  105. ObjectInstanceID id;
  106. std::string jsonKey;//full id defined by map creator
  107. const JsonNode & configuration;
  108. si32 internalId;//unique part of id defined by map creator (also = quest identifier)
  109. ///constructs object (without configuration)
  110. void construct();
  111. ///configures object
  112. void configure();
  113. };
  114. si32 getIdentifier(const std::string & type, const std::string & name);
  115. /**
  116. * Reads complete map.
  117. */
  118. void readMap();
  119. /**
  120. * Reads the map header.
  121. */
  122. void readHeader();
  123. /**
  124. * Reads player information.
  125. */
  126. void readPlayerInfo(JsonDeserializer & handler);
  127. /**
  128. * Reads team settings to header
  129. * @param input serialized header
  130. */
  131. void readTeams(JsonDeserializer & handler);
  132. void readTerrainTile(const std::string & src, TerrainTile & tile);
  133. void readTerrainLevel(const JsonNode & src, const int index);
  134. void readTerrain();
  135. /**
  136. * Loads all map objects from zip archive
  137. */
  138. void readObjects();
  139. const JsonNode getFromArchive(const std::string & archiveFilename);
  140. CInputStream * buffer;
  141. std::shared_ptr<CIOApi> ioApi;
  142. CZipLoader loader;///< object to handle zip archive operations
  143. };
  144. class DLL_LINKAGE CMapSaverJson : public CMapFormatJson, public IMapSaver
  145. {
  146. public:
  147. /**
  148. * Constructor.
  149. *
  150. * @param stream a stream to save the map to, will contain zip archive
  151. */
  152. CMapSaverJson(CInputOutputStream * stream);
  153. ~CMapSaverJson();
  154. /**
  155. * Actually saves the VCMI/Json map into stream.
  156. */
  157. void saveMap(const std::unique_ptr<CMap> & map) override;
  158. private:
  159. /**
  160. * Saves @data as json file with specified @filename
  161. */
  162. void addToArchive(const JsonNode & data, const std::string & filename);
  163. /**
  164. * Saves header to zip archive
  165. */
  166. void writeHeader();
  167. /**
  168. * Saves all players info to header
  169. * @param output serialized header
  170. */
  171. void writePlayerInfo(JsonNode & output);
  172. /**
  173. * Saves one player info
  174. * @param output empty object
  175. */
  176. void writePlayerInfo(const PlayerInfo & info, JsonNode & output);
  177. /**
  178. * Saves team settings to header
  179. * @param output serialized header
  180. */
  181. void writeTeams(JsonNode & output);
  182. /**
  183. * Encodes one tile into string
  184. * @param tile tile to serialize
  185. */
  186. const std::string writeTerrainTile(const TerrainTile & tile);
  187. /**
  188. * Saves map level into json
  189. * @param index z coordinate
  190. */
  191. JsonNode writeTerrainLevel(const int index);
  192. /**
  193. * Saves all terrain into zip archive
  194. */
  195. void writeTerrain();
  196. /**
  197. * Saves all map objects into zip archive
  198. */
  199. void writeObjects();
  200. CInputOutputStream * buffer;
  201. std::shared_ptr<CIOApi> ioApi;
  202. CZipSaver saver;///< object to handle zip archive operations
  203. };