CMapService.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 and saving of VCMI/H3 map files.
  20. */
  21. class DLL_LINKAGE CMapService
  22. {
  23. public:
  24. CMapService() = default;
  25. virtual ~CMapService() = 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. std::unique_ptr<CMap> loadMap(const ResourceID & name) const;
  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. std::unique_ptr<CMapHeader> loadMapHeader(const ResourceID & name) const;
  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. std::unique_ptr<CMap> loadMap(const ui8 * buffer, int size, const std::string & name, const std::string & modName, const std::string & encoding) const;
  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. std::unique_ptr<CMapHeader> loadMapHeader(const ui8 * buffer, int size, const std::string & name, const std::string & modName, const std::string & encoding) const;
  66. void saveMap(const std::unique_ptr<CMap> & map, boost::filesystem::path fullPath) const;
  67. private:
  68. /**
  69. * Gets a map input stream object specified by a map name.
  70. *
  71. * @param name the name of the map
  72. * @return a unique ptr to the input stream class
  73. */
  74. static std::unique_ptr<CInputStream> getStreamFromFS(const ResourceID & name);
  75. /**
  76. * Gets a map input stream from a buffer.
  77. *
  78. * @param buffer a pointer to a buffer containing the map data
  79. * @param size the size of the buffer
  80. * @return a unique ptr to the input stream class
  81. */
  82. static std::unique_ptr<CInputStream> getStreamFromMem(const ui8 * buffer, int size);
  83. /**
  84. * Gets a map loader from the given stream. It performs checks to test
  85. * in which map format the map is.
  86. *
  87. * @param stream the input map stream
  88. * @return the constructed map loader
  89. */
  90. static std::unique_ptr<IMapLoader> getMapLoader(std::unique_ptr<CInputStream> & stream, std::string mapName, std::string modName, std::string encoding);
  91. /**
  92. * Gets a map patcher for specified scenario
  93. *
  94. * @param scenarioName for patcher
  95. * @return the constructed map patcher
  96. */
  97. static std::unique_ptr<IMapPatcher> getMapPatcher(std::string scenarioName);
  98. };
  99. /**
  100. * Interface for loading a map.
  101. */
  102. class DLL_LINKAGE IMapLoader
  103. {
  104. public:
  105. /**
  106. * Loads the VCMI/H3 map file.
  107. *
  108. * @return a unique ptr of the loaded map class
  109. */
  110. virtual std::unique_ptr<CMap> loadMap() = 0;
  111. /**
  112. * Loads the VCMI/H3 map header.
  113. *
  114. * @return a unique ptr of the loaded map header class
  115. */
  116. virtual std::unique_ptr<CMapHeader> loadMapHeader() = 0;
  117. virtual ~IMapLoader(){}
  118. };
  119. class DLL_LINKAGE IMapPatcher
  120. {
  121. public:
  122. /**
  123. * Modifies supplied map header using Json data
  124. *
  125. */
  126. virtual void patchMapHeader(std::unique_ptr<CMapHeader> & header) = 0;
  127. virtual ~IMapPatcher(){}
  128. };
  129. /**
  130. * Interface for saving a map.
  131. */
  132. class DLL_LINKAGE IMapSaver
  133. {
  134. public:
  135. /**
  136. * Saves the VCMI/H3 map file.
  137. *
  138. */
  139. virtual void saveMap(const std::unique_ptr<CMap> & map) = 0;
  140. virtual ~IMapSaver(){}
  141. };
  142. VCMI_LIB_NAMESPACE_END