MapFormatJson.h 5.8 KB

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