CMapService.h 5.5 KB

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