MapFormatJson.h 6.4 KB

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