2
0

CMapService.cpp 5.4 KB

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