CMapService.cpp 5.5 KB

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