CMapService.cpp 5.8 KB

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