MapFormatJson.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. class TriggeredEvent;
  16. class DLL_LINKAGE CMapFormatJson
  17. {
  18. public:
  19. static const std::string HEADER_FILE_NAME;
  20. protected:
  21. /** ptr to the map object which gets filled by data from the buffer or written to buffer */
  22. CMap * map;
  23. /**
  24. * ptr to the map header object which gets filled by data from the buffer or written to buffer.
  25. * (when loading map and mapHeader point to the same object)
  26. */
  27. std::unique_ptr<CMapHeader> mapHeader;
  28. /**
  29. * Reads triggered events, including victory/loss conditions
  30. */
  31. void readTriggeredEvents(const JsonNode & input);
  32. /**
  33. * Reads one of triggered events
  34. */
  35. void readTriggeredEvent(TriggeredEvent & event, const JsonNode & source);
  36. };
  37. class DLL_LINKAGE CMapPatcher : public CMapFormatJson, public IMapPatcher
  38. {
  39. public:
  40. /**
  41. * Default constructor.
  42. *
  43. * @param stream. A stream containing the map data.
  44. */
  45. CMapPatcher(JsonNode stream);
  46. public: //IMapPatcher
  47. /**
  48. * Modifies supplied map header using Json data
  49. *
  50. */
  51. void patchMapHeader(std::unique_ptr<CMapHeader> & header) override;
  52. private:
  53. /**
  54. * Reads subset of header that can be replaced by patching.
  55. */
  56. void readPatchData();
  57. const JsonNode input;
  58. };
  59. class DLL_LINKAGE CMapLoaderJson : public CMapFormatJson, public IMapLoader
  60. {
  61. public:
  62. /**
  63. * Default constructor.
  64. *
  65. * @param stream a stream containing the map data
  66. */
  67. CMapLoaderJson(CInputStream * stream);
  68. /**
  69. * Loads the VCMI/Json map file.
  70. *
  71. * @return a unique ptr of the loaded map class
  72. */
  73. std::unique_ptr<CMap> loadMap() override;
  74. /**
  75. * Loads the VCMI/Json map header.
  76. *
  77. * @return a unique ptr of the loaded map header class
  78. */
  79. std::unique_ptr<CMapHeader> loadMapHeader() override;
  80. private:
  81. /**
  82. * Reads complete map.
  83. */
  84. void readMap();
  85. /**
  86. * Reads the map header.
  87. */
  88. void readHeader();
  89. /**
  90. * Reads player information.
  91. */
  92. void readPlayerInfo();
  93. CInputStream * input;
  94. };
  95. class DLL_LINKAGE CMapSaverJson : public CMapFormatJson, public IMapSaver
  96. {
  97. public:
  98. /**
  99. * Default constructor.
  100. *
  101. * @param stream a stream to save the map to
  102. */
  103. CMapSaverJson(CInputOutputStream * stream);
  104. ~CMapSaverJson();
  105. /**
  106. * Actually saves the VCMI/Json map into stream.
  107. *
  108. */
  109. void saveMap(const std::unique_ptr<CMap> & map) override;
  110. private:
  111. void saveHeader();
  112. CInputOutputStream * output;
  113. std::shared_ptr<CIOApi> ioApi;
  114. CZipSaver saver;
  115. };