CMapService.cpp 5.4 KB

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