CMapService.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * CMapService.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. class ResourceID;
  12. class CMap;
  13. class CMapHeader;
  14. class CInputStream;
  15. class IMapLoader;
  16. class IMapPatcher;
  17. /**
  18. * The map service provides loading of VCMI/H3 map files. It can
  19. * be extended to save maps later as well.
  20. */
  21. class DLL_LINKAGE IMapService
  22. {
  23. public:
  24. IMapService() = default;
  25. virtual ~IMapService() = default;
  26. /**
  27. * Loads the VCMI/H3 map file specified by the name.
  28. *
  29. * @param name the name of the map
  30. * @return a unique ptr to the loaded map class
  31. */
  32. virtual std::unique_ptr<CMap> loadMap(const ResourceID & name) const = 0;
  33. /**
  34. * Loads the VCMI/H3 map header specified by the name.
  35. *
  36. * @param name the name of the map
  37. * @return a unique ptr to the loaded map header class
  38. */
  39. virtual std::unique_ptr<CMapHeader> loadMapHeader(const ResourceID & name) const = 0;
  40. /**
  41. * Loads the VCMI/H3 map file from a buffer. This method is temporarily
  42. * in use to ease the transition to use the new map service.
  43. *
  44. * TODO Replace method params with a CampaignMapInfo struct which contains
  45. * a campaign loading object + name of map.
  46. *
  47. * @param buffer a pointer to a buffer containing the map data
  48. * @param size the size of the buffer
  49. * @param name indicates name of file that will be used during map header patching
  50. * @return a unique ptr to the loaded map class
  51. */
  52. virtual std::unique_ptr<CMap> loadMap(const ui8 * buffer, int size, const std::string & name) const = 0;
  53. /**
  54. * Loads the VCMI/H3 map header from a buffer. This method is temporarily
  55. * in use to ease the transition to use the new map service.
  56. *
  57. * TODO Replace method params with a CampaignMapInfo struct which contains
  58. * a campaign loading object + name of map.
  59. *
  60. * @param buffer a pointer to a buffer containing the map header data
  61. * @param size the size of the buffer
  62. * @param name indicates name of file that will be used during map header patching
  63. * @return a unique ptr to the loaded map class
  64. */
  65. virtual std::unique_ptr<CMapHeader> loadMapHeader(const ui8 * buffer, int size, const std::string & name) const = 0;
  66. virtual void saveMap(const std::unique_ptr<CMap> & map, boost::filesystem::path fullPath) const = 0;
  67. };
  68. class DLL_LINKAGE CMapService : public IMapService
  69. {
  70. public:
  71. CMapService() = default;
  72. virtual ~CMapService() = default;
  73. std::unique_ptr<CMap> loadMap(const ResourceID & name) const override;
  74. std::unique_ptr<CMapHeader> loadMapHeader(const ResourceID & name) const override;
  75. std::unique_ptr<CMap> loadMap(const ui8 * buffer, int size, const std::string & name) const override;
  76. std::unique_ptr<CMapHeader> loadMapHeader(const ui8 * buffer, int size, const std::string & name) const override;
  77. void saveMap(const std::unique_ptr<CMap> & map, boost::filesystem::path fullPath) const override;
  78. private:
  79. /**
  80. * Gets a map input stream object specified by a map name.
  81. *
  82. * @param name the name of the map
  83. * @return a unique ptr to the input stream class
  84. */
  85. static std::unique_ptr<CInputStream> getStreamFromFS(const ResourceID & name);
  86. /**
  87. * Gets a map input stream from a buffer.
  88. *
  89. * @param buffer a pointer to a buffer containing the map data
  90. * @param size the size of the buffer
  91. * @return a unique ptr to the input stream class
  92. */
  93. static std::unique_ptr<CInputStream> getStreamFromMem(const ui8 * buffer, int size);
  94. /**
  95. * Gets a map loader from the given stream. It performs checks to test
  96. * in which map format the map is.
  97. *
  98. * @param stream the input map stream
  99. * @return the constructed map loader
  100. */
  101. static std::unique_ptr<IMapLoader> getMapLoader(std::unique_ptr<CInputStream> & stream);
  102. /**
  103. * Gets a map patcher for specified scenario
  104. *
  105. * @param scenarioName for patcher
  106. * @return the constructed map patcher
  107. */
  108. static std::unique_ptr<IMapPatcher> getMapPatcher(std::string scenarioName);
  109. };
  110. /**
  111. * Interface for loading a map.
  112. */
  113. class DLL_LINKAGE IMapLoader
  114. {
  115. public:
  116. /**
  117. * Loads the VCMI/H3 map file.
  118. *
  119. * @return a unique ptr of the loaded map class
  120. */
  121. virtual std::unique_ptr<CMap> loadMap() = 0;
  122. /**
  123. * Loads the VCMI/H3 map header.
  124. *
  125. * @return a unique ptr of the loaded map header class
  126. */
  127. virtual std::unique_ptr<CMapHeader> loadMapHeader() = 0;
  128. virtual ~IMapLoader(){}
  129. };
  130. class DLL_LINKAGE IMapPatcher
  131. {
  132. public:
  133. /**
  134. * Modifies supplied map header using Json data
  135. *
  136. */
  137. virtual void patchMapHeader(std::unique_ptr<CMapHeader> & header) = 0;
  138. virtual ~IMapPatcher(){}
  139. };
  140. /**
  141. * Interface for saving a map.
  142. */
  143. class DLL_LINKAGE IMapSaver
  144. {
  145. public:
  146. /**
  147. * Saves the VCMI/H3 map file.
  148. *
  149. */
  150. virtual void saveMap(const std::unique_ptr<CMap> & map) = 0;
  151. virtual ~IMapSaver(){}
  152. };