MapFormatJson.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 "../filesystem/CZipSaver.h"
  13. #include "../filesystem/CZipLoader.h"
  14. #include "../GameConstants.h"
  15. #include "../serializer/JsonSerializeFormat.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. struct TriggeredEvent;
  18. struct TerrainTile;
  19. struct PlayerInfo;
  20. class CGObjectInstance;
  21. class AObjectTypeHandler;
  22. class TerrainType;
  23. class RoadType;
  24. class RiverType;
  25. class JsonSerializeFormat;
  26. class JsonDeserializer;
  27. class JsonSerializer;
  28. class DLL_LINKAGE CMapFormatJson
  29. {
  30. public:
  31. static const int VERSION_MAJOR;
  32. static const int VERSION_MINOR;
  33. static const std::string HEADER_FILE_NAME;
  34. static const std::string OBJECTS_FILE_NAME;
  35. static const std::string TERRAIN_FILE_NAMES[2];
  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(const std::string & code);
  50. static RiverType * getRiverByCode(const std::string & code);
  51. static RoadType * getRoadByCode(const std::string & code);
  52. void serializeAllowedFactions(JsonSerializeFormat & handler, std::set<FactionID> & value) const;
  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) const;
  77. /**
  78. * Writes one of triggered events
  79. */
  80. void writeTriggeredEvent(const TriggeredEvent & event, JsonNode & dest) const;
  81. void writeDisposedHeroes(JsonSerializeFormat & handler);
  82. void readDisposedHeroes(JsonSerializeFormat & handler);
  83. void serializePredefinedHeroes(JsonSerializeFormat & handler);
  84. void serializeRumors(JsonSerializeFormat & handler);
  85. void serializeTimedEvents(JsonSerializeFormat & handler);
  86. ///common part of map attributes saving/loading
  87. void serializeOptions(JsonSerializeFormat & handler);
  88. /**
  89. * Loads map attributes except header ones
  90. */
  91. void readOptions(JsonDeserializer & handler);
  92. /**
  93. * Saves map attributes except header ones
  94. */
  95. void writeOptions(JsonSerializer & handler);
  96. };
  97. class DLL_LINKAGE CMapPatcher : public CMapFormatJson, public IMapPatcher
  98. {
  99. public:
  100. /**
  101. * Default constructor.
  102. *
  103. * @param stream. A stream containing the map data.
  104. */
  105. CMapPatcher(const JsonNode & stream);
  106. public: //IMapPatcher
  107. /**
  108. * Modifies supplied map header using Json data
  109. *
  110. */
  111. void patchMapHeader(std::unique_ptr<CMapHeader> & header) override;
  112. private:
  113. /**
  114. * Reads subset of header that can be replaced by patching.
  115. */
  116. void readPatchData();
  117. JsonNode input;
  118. };
  119. class DLL_LINKAGE CMapLoaderJson : public CMapFormatJson, public IMapLoader
  120. {
  121. public:
  122. /**
  123. * Constructor.
  124. *
  125. * @param stream a stream containing the map data
  126. */
  127. CMapLoaderJson(CInputStream * stream);
  128. /**
  129. * Loads the VCMI/Json map file.
  130. *
  131. * @return a unique ptr of the loaded map class
  132. */
  133. std::unique_ptr<CMap> loadMap(IGameCallback * cb) override;
  134. /**
  135. * Loads the VCMI/Json map header.
  136. *
  137. * @return a unique ptr of the loaded map header class
  138. */
  139. std::unique_ptr<CMapHeader> loadMapHeader() override;
  140. struct MapObjectLoader
  141. {
  142. MapObjectLoader(CMapLoaderJson * _owner, JsonMap::value_type & json);
  143. CMapLoaderJson * owner;
  144. CGObjectInstance * instance;
  145. ObjectInstanceID id;
  146. std::string jsonKey;//full id defined by map creator
  147. JsonNode & configuration;
  148. ///constructs object (without configuration)
  149. void construct();
  150. ///configures object
  151. void configure();
  152. };
  153. /**
  154. * Reads the map header.
  155. */
  156. void readHeader(const bool complete);
  157. /**
  158. * Reads complete map.
  159. */
  160. void readMap();
  161. /**
  162. * Reads texts and translations
  163. */
  164. void readTranslations();
  165. static void readTerrainTile(const std::string & src, TerrainTile & tile);
  166. void readTerrainLevel(const JsonNode & src, const int index);
  167. void readTerrain();
  168. /**
  169. * Loads all map objects from zip archive
  170. */
  171. void readObjects();
  172. bool isExistArchive(const std::string & archiveFilename);
  173. JsonNode getFromArchive(const std::string & archiveFilename);
  174. private:
  175. CInputStream * buffer;
  176. std::shared_ptr<CIOApi> ioApi;
  177. CZipLoader loader;///< object to handle zip archive operations
  178. };
  179. class DLL_LINKAGE CMapSaverJson : public CMapFormatJson, public IMapSaver
  180. {
  181. public:
  182. /**
  183. * Constructor.
  184. *
  185. * @param stream a stream to save the map to, will contain zip archive
  186. */
  187. CMapSaverJson(CInputOutputStream * stream);
  188. ~CMapSaverJson();
  189. /**
  190. * Actually saves the VCMI/Json map into stream.
  191. */
  192. void saveMap(const std::unique_ptr<CMap> & map) override;
  193. /**
  194. * Saves @data as json file with specified @filename
  195. */
  196. void addToArchive(const JsonNode & data, const std::string & filename);
  197. /**
  198. * Saves header to zip archive
  199. */
  200. void writeHeader();
  201. /**
  202. * Saves texts and translations to zip archive
  203. */
  204. void writeTranslations();
  205. /**
  206. * Encodes one tile into string
  207. * @param tile tile to serialize
  208. */
  209. static std::string writeTerrainTile(const TerrainTile & tile);
  210. /**
  211. * Saves map level into json
  212. * @param index z coordinate
  213. */
  214. JsonNode writeTerrainLevel(const int index);
  215. /**
  216. * Saves all terrain into zip archive
  217. */
  218. void writeTerrain();
  219. /**
  220. * Saves all map objects into zip archive
  221. */
  222. void writeObjects();
  223. private:
  224. CInputOutputStream * buffer;
  225. std::shared_ptr<CIOApi> ioApi;
  226. CZipSaver saver;///< object to handle zip archive operations
  227. };
  228. VCMI_LIB_NAMESPACE_END