MapFormatJson.h 6.0 KB

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