CMapService.h 5.3 KB

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