CMapService.h 5.1 KB

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