MapFormatJson.h 5.7 KB

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