CMapService.cpp 4.8 KB

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