MapFormatJson.h 6.0 KB

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