MapFormatJson.h 7.0 KB

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