MapFormatJson.h 6.2 KB

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