MapFormatJson.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. CMapHeader * mapHeader;
  38. /**
  39. * Reads triggered events, including victory/loss conditions
  40. */
  41. void readTriggeredEvents(JsonDeserializer & handler);
  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. 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. ObjectInstanceID id;
  105. std::string jsonKey;//full id defined by map creator
  106. const JsonNode & configuration;
  107. si32 internalId;//unique part of id defined by map creator (also = quest identifier)
  108. ///constructs object (without configuration)
  109. void construct();
  110. ///configures object
  111. void configure();
  112. };
  113. si32 getIdentifier(const std::string & type, const std::string & name);
  114. /**
  115. * Reads complete map.
  116. */
  117. void readMap();
  118. /**
  119. * Reads the map header.
  120. */
  121. void readHeader();
  122. /**
  123. * Reads player information.
  124. */
  125. void readPlayerInfo(JsonDeserializer & handler);
  126. /**
  127. * Reads team settings to header
  128. * @param input serialized header
  129. */
  130. void readTeams(JsonDeserializer & handler);
  131. void readTerrainTile(const std::string & src, TerrainTile & tile);
  132. void readTerrainLevel(const JsonNode & src, const int index);
  133. void readTerrain();
  134. /**
  135. * Loads all map objects from zip archive
  136. */
  137. void readObjects();
  138. const JsonNode getFromArchive(const std::string & archiveFilename);
  139. CInputStream * buffer;
  140. std::shared_ptr<CIOApi> ioApi;
  141. CZipLoader loader;///< object to handle zip archive operations
  142. };
  143. class DLL_LINKAGE CMapSaverJson : public CMapFormatJson, public IMapSaver
  144. {
  145. public:
  146. /**
  147. * Constructor.
  148. *
  149. * @param stream a stream to save the map to, will contain zip archive
  150. */
  151. CMapSaverJson(CInputOutputStream * stream);
  152. ~CMapSaverJson();
  153. /**
  154. * Actually saves the VCMI/Json map into stream.
  155. */
  156. void saveMap(const std::unique_ptr<CMap> & map) override;
  157. private:
  158. /**
  159. * Saves @data as json file with specified @filename
  160. */
  161. void addToArchive(const JsonNode & data, const std::string & filename);
  162. /**
  163. * Saves header to zip archive
  164. */
  165. void writeHeader();
  166. /**
  167. * Saves all players info to header
  168. * @param output serialized header
  169. */
  170. void writePlayerInfo(JsonNode & output);
  171. /**
  172. * Saves one player info
  173. * @param output empty object
  174. */
  175. void writePlayerInfo(const PlayerInfo & info, JsonNode & output);
  176. /**
  177. * Saves team settings to header
  178. * @param output serialized header
  179. */
  180. void writeTeams(JsonNode & output);
  181. /**
  182. * Encodes one tile into string
  183. * @param tile tile to serialize
  184. */
  185. const std::string writeTerrainTile(const TerrainTile & tile);
  186. /**
  187. * Saves map level into json
  188. * @param index z coordinate
  189. */
  190. JsonNode writeTerrainLevel(const int index);
  191. /**
  192. * Saves all terrain into zip archive
  193. */
  194. void writeTerrain();
  195. /**
  196. * Saves all map objects into zip archive
  197. */
  198. void writeObjects();
  199. CInputOutputStream * buffer;
  200. std::shared_ptr<CIOApi> ioApi;
  201. CZipSaver saver;///< object to handle zip archive operations
  202. };