CMapService.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * CMapService.cpp, 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. #include "StdInc.h"
  11. #include "CMapService.h"
  12. #include "MapFormatSettings.h"
  13. #include "../json/JsonUtils.h"
  14. #include "../filesystem/Filesystem.h"
  15. #include "../filesystem/CBinaryReader.h"
  16. #include "../filesystem/CCompressedStream.h"
  17. #include "../filesystem/CMemoryStream.h"
  18. #include "../filesystem/CMemoryBuffer.h"
  19. #include "../modding/CModHandler.h"
  20. #include "../modding/ModDescription.h"
  21. #include "../modding/ModScope.h"
  22. #include "../GameLibrary.h"
  23. #include "CMap.h"
  24. #include "MapFormat.h"
  25. #include "MapFormatH3M.h"
  26. #include "MapFormatJson.h"
  27. #include "../callback/EditorCallback.h"
  28. VCMI_LIB_NAMESPACE_BEGIN
  29. std::unique_ptr<CMap> CMapService::loadMap(const ResourcePath & name, IGameInfoCallback * cb) const
  30. {
  31. std::string modName = LIBRARY->modh->findResourceOrigin(name);
  32. std::string encoding = LIBRARY->modh->findResourceEncoding(name);
  33. auto stream = getStreamFromFS(name);
  34. auto loader = getMapLoader(stream, name.getName(), modName, encoding);
  35. // If JSON loader, let it know whether this is editor callback
  36. if(auto jsonLoader = dynamic_cast<CMapLoaderJson*>(loader.get()))
  37. jsonLoader->setRunningInMapEditor(dynamic_cast<EditorCallback*>(cb) != nullptr);
  38. return loader->loadMap(cb);
  39. }
  40. std::unique_ptr<CMapHeader> CMapService::loadMapHeader(const ResourcePath & name, bool isEditor) const
  41. {
  42. std::string modName = LIBRARY->modh->findResourceOrigin(name);
  43. std::string encoding = LIBRARY->modh->findResourceEncoding(name);
  44. auto stream = getStreamFromFS(name);
  45. auto loader = getMapLoader(stream, name.getName(), modName, encoding);
  46. // default: not editor; callers may pass explicit overload if needed
  47. if(auto jsonLoader = dynamic_cast<CMapLoaderJson*>(loader.get()))
  48. jsonLoader->setRunningInMapEditor(false);
  49. return loader->loadMapHeader();
  50. }
  51. std::unique_ptr<CMap> CMapService::loadMap(const uint8_t * buffer, int size, const std::string & name, const std::string & modName, const std::string & encoding, IGameInfoCallback * cb) const
  52. {
  53. auto stream = getStreamFromMem(buffer, size);
  54. auto loader = getMapLoader(stream, name, modName, encoding);
  55. if(auto jsonLoader = dynamic_cast<CMapLoaderJson*>(loader.get()))
  56. jsonLoader->setRunningInMapEditor(dynamic_cast<EditorCallback*>(cb) != nullptr);
  57. std::unique_ptr<CMap> map(loader->loadMap(cb));
  58. std::unique_ptr<CMapHeader> header(map.get());
  59. //might be original campaign and require patch
  60. getMapPatcher(name)->patchMapHeader(header);
  61. header.release();
  62. return map;
  63. }
  64. std::unique_ptr<CMapHeader> CMapService::loadMapHeader(const uint8_t * buffer, int size, const std::string & name, const std::string & modName, const std::string & encoding) const
  65. {
  66. auto stream = getStreamFromMem(buffer, size);
  67. auto loader = getMapLoader(stream, name, modName, encoding);
  68. if(auto jsonLoader = dynamic_cast<CMapLoaderJson*>(loader.get()))
  69. jsonLoader->setRunningInMapEditor(false);
  70. std::unique_ptr<CMapHeader> header = loader->loadMapHeader();
  71. //might be original campaign and require patch
  72. getMapPatcher(name)->patchMapHeader(header);
  73. return header;
  74. }
  75. void CMapService::saveMap(const std::unique_ptr<CMap> & map, boost::filesystem::path fullPath) const
  76. {
  77. CMemoryBuffer serializeBuffer;
  78. {
  79. CMapSaverJson saver(&serializeBuffer);
  80. saver.saveMap(map);
  81. }
  82. {
  83. boost::filesystem::remove(fullPath);
  84. std::ofstream tmp(fullPath.c_str(), std::ofstream::binary);
  85. tmp.write(reinterpret_cast<const char *>(serializeBuffer.getBuffer().data()), serializeBuffer.getSize());
  86. tmp.flush();
  87. tmp.close();
  88. }
  89. }
  90. ModCompatibilityInfo CMapService::verifyMapHeaderMods(const CMapHeader & map)
  91. {
  92. const auto & activeMods = LIBRARY->modh->getActiveMods();
  93. ModCompatibilityInfo missingMods;
  94. ModCompatibilityInfo missingModsFiltered;
  95. for(const auto & mapMod : map.mods)
  96. {
  97. if(vstd::contains(activeMods, mapMod.first))
  98. {
  99. const auto & modInfo = LIBRARY->modh->getModInfo(mapMod.first);
  100. if(modInfo.getVersion().compatible(mapMod.second.version))
  101. continue;
  102. }
  103. missingMods[mapMod.first] = mapMod.second;
  104. }
  105. //filter child mods
  106. for(const auto & mapMod : missingMods)
  107. {
  108. if(!mapMod.second.parent.empty() && missingMods.count(mapMod.second.parent))
  109. continue;
  110. missingModsFiltered.insert(mapMod);
  111. }
  112. return missingModsFiltered;
  113. }
  114. std::unique_ptr<CInputStream> CMapService::getStreamFromFS(const ResourcePath & name)
  115. {
  116. return CResourceHandler::get()->load(name);
  117. }
  118. std::unique_ptr<CInputStream> CMapService::getStreamFromMem(const uint8_t * buffer, int size)
  119. {
  120. return std::unique_ptr<CInputStream>(new CMemoryStream(buffer, size));
  121. }
  122. std::unique_ptr<IMapLoader> CMapService::getMapLoader(std::unique_ptr<CInputStream> & stream, std::string mapName, std::string modName, std::string encoding)
  123. {
  124. // Read map header
  125. CBinaryReader reader(stream.get());
  126. ui32 header = reader.readUInt32();
  127. reader.getStream()->seek(0);
  128. //check for ZIP magic. Zip files are VCMI maps
  129. switch(header)
  130. {
  131. case 0x06054b50:
  132. case 0x04034b50:
  133. case 0x02014b50:
  134. return std::unique_ptr<IMapLoader>(new CMapLoaderJson(stream.get(), mapName));
  135. break;
  136. default:
  137. // Check which map format is used
  138. // gzip header is 3 bytes only in size
  139. switch(header & 0xffffff)
  140. {
  141. // gzip header magic number, reversed for LE
  142. case 0x00088B1F:
  143. stream = std::unique_ptr<CInputStream>(new CCompressedStream(std::move(stream), true));
  144. return std::unique_ptr<IMapLoader>(new CMapLoaderH3M(mapName, modName, encoding, stream.get()));
  145. case static_cast<int>(EMapFormat::WOG) :
  146. case static_cast<int>(EMapFormat::AB) :
  147. case static_cast<int>(EMapFormat::ROE) :
  148. case static_cast<int>(EMapFormat::SOD) :
  149. case static_cast<int>(EMapFormat::CHR) :
  150. case static_cast<int>(EMapFormat::HOTA) :
  151. return std::unique_ptr<IMapLoader>(new CMapLoaderH3M(mapName, modName, encoding, stream.get()));
  152. default :
  153. throw std::runtime_error("Unknown map format");
  154. }
  155. }
  156. }
  157. std::unique_ptr<IMapPatcher> CMapService::getMapPatcher(std::string scenarioName)
  158. {
  159. boost::to_lower(scenarioName);
  160. logGlobal->debug("Request to patch map %s", scenarioName);
  161. return std::make_unique<CMapPatcher>(LIBRARY->mapFormat->mapOverrides(scenarioName));
  162. }
  163. VCMI_LIB_NAMESPACE_END