CMapService.h 5.2 KB

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