2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* CMapService.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2012-11-21 12:13:33 +03:00
|
|
|
#include "StdInc.h"
|
2012-11-03 16:30:47 +03:00
|
|
|
#include "CMapService.h"
|
2012-11-20 20:53:45 +03:00
|
|
|
|
2024-02-11 23:09:01 +02:00
|
|
|
#include "../json/JsonUtils.h"
|
2013-07-28 17:49:50 +03:00
|
|
|
#include "../filesystem/Filesystem.h"
|
2013-04-07 13:48:07 +03:00
|
|
|
#include "../filesystem/CBinaryReader.h"
|
|
|
|
#include "../filesystem/CCompressedStream.h"
|
|
|
|
#include "../filesystem/CMemoryStream.h"
|
2017-06-04 19:33:28 +02:00
|
|
|
#include "../filesystem/CMemoryBuffer.h"
|
2023-07-30 19:12:25 +02:00
|
|
|
#include "../modding/CModHandler.h"
|
2024-11-09 22:29:07 +02:00
|
|
|
#include "../modding/ModDescription.h"
|
2023-07-30 19:12:25 +02:00
|
|
|
#include "../modding/ModScope.h"
|
2023-05-24 01:05:59 +02:00
|
|
|
#include "../VCMI_Lib.h"
|
2016-02-13 18:43:05 +02:00
|
|
|
|
2012-11-03 16:30:47 +03:00
|
|
|
#include "CMap.h"
|
2023-05-24 00:14:06 +02:00
|
|
|
#include "MapFormat.h"
|
2013-02-02 20:20:31 +03:00
|
|
|
|
2013-02-04 13:29:59 +03:00
|
|
|
#include "MapFormatH3M.h"
|
2013-12-31 02:09:58 +03:00
|
|
|
#include "MapFormatJson.h"
|
2013-02-04 13:29:59 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2012-11-03 16:30:47 +03:00
|
|
|
|
2024-01-01 16:37:48 +02:00
|
|
|
std::unique_ptr<CMap> CMapService::loadMap(const ResourcePath & name, IGameCallback * cb) const
|
2012-11-03 16:30:47 +03:00
|
|
|
{
|
2023-02-24 22:38:12 +02:00
|
|
|
std::string modName = VLC->modh->findResourceOrigin(name);
|
2024-10-30 13:54:35 +02:00
|
|
|
std::string encoding = VLC->modh->findResourceEncoding(name);
|
2023-02-24 22:38:12 +02:00
|
|
|
|
2012-11-11 15:23:31 +03:00
|
|
|
auto stream = getStreamFromFS(name);
|
2024-01-01 16:37:48 +02:00
|
|
|
return getMapLoader(stream, name.getName(), modName, encoding)->loadMap(cb);
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
std::unique_ptr<CMapHeader> CMapService::loadMapHeader(const ResourcePath & name) const
|
2012-11-03 16:30:47 +03:00
|
|
|
{
|
2023-02-24 22:38:12 +02:00
|
|
|
std::string modName = VLC->modh->findResourceOrigin(name);
|
2024-10-30 13:54:35 +02:00
|
|
|
std::string encoding = VLC->modh->findResourceEncoding(name);
|
2023-02-24 22:38:12 +02:00
|
|
|
|
2012-11-11 15:23:31 +03:00
|
|
|
auto stream = getStreamFromFS(name);
|
2023-02-25 17:44:15 +02:00
|
|
|
return getMapLoader(stream, name.getName(), modName, encoding)->loadMapHeader();
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
2024-01-01 16:37:48 +02:00
|
|
|
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
|
2012-11-03 16:30:47 +03:00
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
auto stream = getStreamFromMem(buffer, size);
|
2024-01-01 16:37:48 +02:00
|
|
|
std::unique_ptr<CMap> map(getMapLoader(stream, name, modName, encoding)->loadMap(cb));
|
2013-12-31 02:09:58 +03:00
|
|
|
std::unique_ptr<CMapHeader> header(map.get());
|
|
|
|
|
2017-06-04 21:42:48 +02:00
|
|
|
//might be original campaign and require patch
|
2013-12-31 02:09:58 +03:00
|
|
|
getMapPatcher(name)->patchMapHeader(header);
|
|
|
|
header.release();
|
|
|
|
|
2015-12-04 01:06:02 +02:00
|
|
|
return map;
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
2023-06-27 15:27:35 +02:00
|
|
|
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
|
2012-11-03 16:30:47 +03:00
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
auto stream = getStreamFromMem(buffer, size);
|
2023-02-25 17:44:15 +02:00
|
|
|
std::unique_ptr<CMapHeader> header = getMapLoader(stream, name, modName, encoding)->loadMapHeader();
|
2017-06-04 21:42:48 +02:00
|
|
|
|
|
|
|
//might be original campaign and require patch
|
2013-12-31 02:09:58 +03:00
|
|
|
getMapPatcher(name)->patchMapHeader(header);
|
2015-12-04 01:06:02 +02:00
|
|
|
return header;
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
void CMapService::saveMap(const std::unique_ptr<CMap> & map, boost::filesystem::path fullPath) const
|
2017-06-04 19:33:28 +02:00
|
|
|
{
|
|
|
|
CMemoryBuffer serializeBuffer;
|
|
|
|
{
|
|
|
|
CMapSaverJson saver(&serializeBuffer);
|
|
|
|
saver.saveMap(map);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
boost::filesystem::remove(fullPath);
|
2023-07-28 14:51:14 +02:00
|
|
|
std::ofstream tmp(fullPath.c_str(), std::ofstream::binary);
|
2017-06-04 19:33:28 +02:00
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
tmp.write(reinterpret_cast<const char *>(serializeBuffer.getBuffer().data()), serializeBuffer.getSize());
|
2017-06-04 19:33:28 +02:00
|
|
|
tmp.flush();
|
|
|
|
tmp.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-16 13:38:13 +02:00
|
|
|
ModCompatibilityInfo CMapService::verifyMapHeaderMods(const CMapHeader & map)
|
|
|
|
{
|
|
|
|
const auto & activeMods = VLC->modh->getActiveMods();
|
2023-09-21 04:31:08 +02:00
|
|
|
|
2024-01-10 00:38:54 +02:00
|
|
|
ModCompatibilityInfo missingMods;
|
|
|
|
ModCompatibilityInfo missingModsFiltered;
|
2023-04-16 13:38:13 +02:00
|
|
|
for(const auto & mapMod : map.mods)
|
|
|
|
{
|
|
|
|
if(vstd::contains(activeMods, mapMod.first))
|
|
|
|
{
|
|
|
|
const auto & modInfo = VLC->modh->getModInfo(mapMod.first);
|
2024-11-09 22:29:07 +02:00
|
|
|
if(modInfo.getVersion().compatible(mapMod.second.version))
|
2023-04-16 13:38:13 +02:00
|
|
|
continue;
|
|
|
|
}
|
2023-09-21 22:38:01 +02:00
|
|
|
missingMods[mapMod.first] = mapMod.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
//filter child mods
|
|
|
|
for(const auto & mapMod : missingMods)
|
|
|
|
{
|
|
|
|
if(!mapMod.second.parent.empty() && missingMods.count(mapMod.second.parent))
|
|
|
|
continue;
|
|
|
|
missingModsFiltered.insert(mapMod);
|
|
|
|
}
|
|
|
|
|
|
|
|
return missingModsFiltered;
|
2023-04-16 13:38:13 +02:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
std::unique_ptr<CInputStream> CMapService::getStreamFromFS(const ResourcePath & name)
|
2012-11-03 16:30:47 +03:00
|
|
|
{
|
2017-06-04 21:42:48 +02:00
|
|
|
return CResourceHandler::get()->load(name);
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
2023-06-27 15:27:35 +02:00
|
|
|
std::unique_ptr<CInputStream> CMapService::getStreamFromMem(const uint8_t * buffer, int size)
|
2012-11-03 16:30:47 +03:00
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
return std::unique_ptr<CInputStream>(new CMemoryStream(buffer, size));
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
2023-02-25 17:44:15 +02:00
|
|
|
std::unique_ptr<IMapLoader> CMapService::getMapLoader(std::unique_ptr<CInputStream> & stream, std::string mapName, std::string modName, std::string encoding)
|
2012-11-03 16:30:47 +03:00
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
// Read map header
|
|
|
|
CBinaryReader reader(stream.get());
|
|
|
|
ui32 header = reader.readUInt32();
|
|
|
|
reader.getStream()->seek(0);
|
|
|
|
|
2016-02-09 19:20:03 +02:00
|
|
|
//check for ZIP magic. Zip files are VCMI maps
|
|
|
|
switch(header)
|
2012-11-11 15:23:31 +03:00
|
|
|
{
|
2016-02-09 19:20:03 +02:00
|
|
|
case 0x06054b50:
|
|
|
|
case 0x04034b50:
|
|
|
|
case 0x02014b50:
|
2016-02-10 06:28:00 +02:00
|
|
|
return std::unique_ptr<IMapLoader>(new CMapLoaderJson(stream.get()));
|
2016-02-09 19:20:03 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Check which map format is used
|
|
|
|
// gzip header is 3 bytes only in size
|
|
|
|
switch(header & 0xffffff)
|
|
|
|
{
|
|
|
|
// gzip header magic number, reversed for LE
|
|
|
|
case 0x00088B1F:
|
|
|
|
stream = std::unique_ptr<CInputStream>(new CCompressedStream(std::move(stream), true));
|
2023-02-25 17:44:15 +02:00
|
|
|
return std::unique_ptr<IMapLoader>(new CMapLoaderH3M(mapName, modName, encoding, stream.get()));
|
2023-04-02 18:56:10 +02:00
|
|
|
case static_cast<int>(EMapFormat::WOG) :
|
|
|
|
case static_cast<int>(EMapFormat::AB) :
|
|
|
|
case static_cast<int>(EMapFormat::ROE) :
|
|
|
|
case static_cast<int>(EMapFormat::SOD) :
|
2024-08-31 12:39:23 +02:00
|
|
|
case static_cast<int>(EMapFormat::CHR) :
|
2023-04-10 13:01:09 +02:00
|
|
|
case static_cast<int>(EMapFormat::HOTA) :
|
2023-02-25 17:44:15 +02:00
|
|
|
return std::unique_ptr<IMapLoader>(new CMapLoaderH3M(mapName, modName, encoding, stream.get()));
|
2016-02-09 19:20:03 +02:00
|
|
|
default :
|
|
|
|
throw std::runtime_error("Unknown map format");
|
|
|
|
}
|
2012-11-11 15:23:31 +03:00
|
|
|
}
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
2013-12-31 02:09:58 +03:00
|
|
|
|
2023-09-01 23:26:14 +02:00
|
|
|
static JsonNode loadPatches(const std::string & path)
|
2014-01-01 16:30:31 +03:00
|
|
|
{
|
2023-09-01 23:26:14 +02:00
|
|
|
JsonNode node = JsonUtils::assembleFromFiles(path);
|
2014-01-01 16:30:31 +03:00
|
|
|
for (auto & entry : node.Struct())
|
|
|
|
JsonUtils::validate(entry.second, "vcmi:mapHeader", "patch for " + entry.first);
|
2023-04-03 14:15:29 +02:00
|
|
|
|
2024-02-13 14:34:16 +02:00
|
|
|
node.setModScope(ModScope::scopeMap());
|
2014-01-01 16:30:31 +03:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2013-12-31 02:09:58 +03:00
|
|
|
std::unique_ptr<IMapPatcher> CMapService::getMapPatcher(std::string scenarioName)
|
|
|
|
{
|
2024-01-09 19:08:22 +02:00
|
|
|
static const JsonNode node = loadPatches("config/mapOverrides.json");
|
2014-01-01 16:30:31 +03:00
|
|
|
|
2013-12-31 02:09:58 +03:00
|
|
|
boost::to_lower(scenarioName);
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->debug("Request to patch map %s", scenarioName);
|
2015-08-07 21:33:44 +02:00
|
|
|
return std::unique_ptr<IMapPatcher>(new CMapPatcher(node[scenarioName]));
|
2013-12-31 02:09:58 +03:00
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|