MapFormatJson.h 5.8 KB

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