CMapService.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "../filesystem/Filesystem.h"
  13. #include "../filesystem/CBinaryReader.h"
  14. #include "../filesystem/CCompressedStream.h"
  15. #include "../filesystem/CMemoryStream.h"
  16. #include "../filesystem/CMemoryBuffer.h"
  17. #include "../modding/CModHandler.h"
  18. #include "../modding/ModScope.h"
  19. #include "../modding/CModInfo.h"
  20. #include "../Languages.h"
  21. #include "../VCMI_Lib.h"
  22. #include "CMap.h"
  23. #include "MapFormat.h"
  24. #include "MapFormatH3M.h"
  25. #include "MapFormatJson.h"
  26. VCMI_LIB_NAMESPACE_BEGIN
  27. std::unique_ptr<CMap> CMapService::loadMap(const ResourcePath & name, IGameCallback * cb) const
  28. {
  29. std::string modName = VLC->modh->findResourceOrigin(name);
  30. std::string language = VLC->modh->getModLanguage(modName);
  31. std::string encoding = Languages::getLanguageOptions(language).encoding;
  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 = VLC->modh->findResourceOrigin(name);
  38. std::string language = VLC->modh->getModLanguage(modName);
  39. std::string encoding = Languages::getLanguageOptions(language).encoding;
  40. auto stream = getStreamFromFS(name);
  41. return getMapLoader(stream, name.getName(), modName, encoding)->loadMapHeader();
  42. }
  43. std::unique_ptr<CMap> CMapService::loadMap(const uint8_t * buffer, int size, const std::string & name, const std::string & modName, const std::string & encoding, IGameCallback * cb) const
  44. {
  45. auto stream = getStreamFromMem(buffer, size);
  46. std::unique_ptr<CMap> map(getMapLoader(stream, name, modName, encoding)->loadMap(cb));
  47. std::unique_ptr<CMapHeader> header(map.get());
  48. //might be original campaign and require patch
  49. getMapPatcher(name)->patchMapHeader(header);
  50. header.release();
  51. return map;
  52. }
  53. 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
  54. {
  55. auto stream = getStreamFromMem(buffer, size);
  56. std::unique_ptr<CMapHeader> header = getMapLoader(stream, name, modName, encoding)->loadMapHeader();
  57. //might be original campaign and require patch
  58. getMapPatcher(name)->patchMapHeader(header);
  59. return header;
  60. }
  61. void CMapService::saveMap(const std::unique_ptr<CMap> & map, boost::filesystem::path fullPath) const
  62. {
  63. CMemoryBuffer serializeBuffer;
  64. {
  65. CMapSaverJson saver(&serializeBuffer);
  66. saver.saveMap(map);
  67. }
  68. {
  69. boost::filesystem::remove(fullPath);
  70. std::ofstream tmp(fullPath.c_str(), std::ofstream::binary);
  71. tmp.write(reinterpret_cast<const char *>(serializeBuffer.getBuffer().data()), serializeBuffer.getSize());
  72. tmp.flush();
  73. tmp.close();
  74. }
  75. }
  76. ModCompatibilityInfo CMapService::verifyMapHeaderMods(const CMapHeader & map)
  77. {
  78. const auto & activeMods = VLC->modh->getActiveMods();
  79. ModCompatibilityInfo missingMods;
  80. ModCompatibilityInfo missingModsFiltered;
  81. for(const auto & mapMod : map.mods)
  82. {
  83. if(vstd::contains(activeMods, mapMod.first))
  84. {
  85. const auto & modInfo = VLC->modh->getModInfo(mapMod.first);
  86. if(modInfo.getVerificationInfo().version.compatible(mapMod.second.version))
  87. continue;
  88. }
  89. missingMods[mapMod.first] = mapMod.second;
  90. }
  91. //filter child mods
  92. for(const auto & mapMod : missingMods)
  93. {
  94. if(!mapMod.second.parent.empty() && missingMods.count(mapMod.second.parent))
  95. continue;
  96. missingModsFiltered.insert(mapMod);
  97. }
  98. return missingModsFiltered;
  99. }
  100. std::unique_ptr<CInputStream> CMapService::getStreamFromFS(const ResourcePath & name)
  101. {
  102. return CResourceHandler::get()->load(name);
  103. }
  104. std::unique_ptr<CInputStream> CMapService::getStreamFromMem(const uint8_t * buffer, int size)
  105. {
  106. return std::unique_ptr<CInputStream>(new CMemoryStream(buffer, size));
  107. }
  108. std::unique_ptr<IMapLoader> CMapService::getMapLoader(std::unique_ptr<CInputStream> & stream, std::string mapName, std::string modName, std::string encoding)
  109. {
  110. // Read map header
  111. CBinaryReader reader(stream.get());
  112. ui32 header = reader.readUInt32();
  113. reader.getStream()->seek(0);
  114. //check for ZIP magic. Zip files are VCMI maps
  115. switch(header)
  116. {
  117. case 0x06054b50:
  118. case 0x04034b50:
  119. case 0x02014b50:
  120. return std::unique_ptr<IMapLoader>(new CMapLoaderJson(stream.get()));
  121. break;
  122. default:
  123. // Check which map format is used
  124. // gzip header is 3 bytes only in size
  125. switch(header & 0xffffff)
  126. {
  127. // gzip header magic number, reversed for LE
  128. case 0x00088B1F:
  129. stream = std::unique_ptr<CInputStream>(new CCompressedStream(std::move(stream), true));
  130. return std::unique_ptr<IMapLoader>(new CMapLoaderH3M(mapName, modName, encoding, stream.get()));
  131. case static_cast<int>(EMapFormat::WOG) :
  132. case static_cast<int>(EMapFormat::AB) :
  133. case static_cast<int>(EMapFormat::ROE) :
  134. case static_cast<int>(EMapFormat::SOD) :
  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. static JsonNode loadPatches(const std::string & path)
  143. {
  144. JsonNode node = JsonUtils::assembleFromFiles(path);
  145. for (auto & entry : node.Struct())
  146. JsonUtils::validate(entry.second, "vcmi:mapHeader", "patch for " + entry.first);
  147. node.setMeta(ModScope::scopeMap());
  148. return node;
  149. }
  150. std::unique_ptr<IMapPatcher> CMapService::getMapPatcher(std::string scenarioName)
  151. {
  152. static JsonNode node;
  153. if (node.isNull())
  154. node = loadPatches("config/mapOverrides.json");
  155. boost::to_lower(scenarioName);
  156. logGlobal->debug("Request to patch map %s", scenarioName);
  157. return std::unique_ptr<IMapPatcher>(new CMapPatcher(node[scenarioName]));
  158. }
  159. VCMI_LIB_NAMESPACE_END