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
|
|
|
|
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"
|
2012-11-03 16:30:47 +03:00
|
|
|
#include "CMap.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
|
|
|
|
2012-11-03 16:30:47 +03:00
|
|
|
|
|
|
|
std::unique_ptr<CMap> CMapService::loadMap(const std::string & name)
|
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
auto stream = getStreamFromFS(name);
|
2013-12-31 02:09:58 +03:00
|
|
|
std::unique_ptr<CMap> map(getMapLoader(stream)->loadMap());
|
|
|
|
std::unique_ptr<CMapHeader> header(map.get());
|
|
|
|
|
|
|
|
getMapPatcher(name)->patchMapHeader(header);
|
|
|
|
header.release();
|
|
|
|
|
|
|
|
return std::move(map);
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<CMapHeader> CMapService::loadMapHeader(const std::string & name)
|
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
auto stream = getStreamFromFS(name);
|
2013-12-31 02:09:58 +03:00
|
|
|
std::unique_ptr<CMapHeader> header = getMapLoader(stream)->loadMapHeader();
|
|
|
|
getMapPatcher(name)->patchMapHeader(header);
|
|
|
|
return std::move(header);
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
2013-12-31 02:09:58 +03:00
|
|
|
std::unique_ptr<CMap> CMapService::loadMap(const ui8 * buffer, int size, const std::string & name)
|
2012-11-03 16:30:47 +03:00
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
auto stream = getStreamFromMem(buffer, size);
|
2013-12-31 02:09:58 +03:00
|
|
|
std::unique_ptr<CMap> map(getMapLoader(stream)->loadMap());
|
|
|
|
std::unique_ptr<CMapHeader> header(map.get());
|
|
|
|
|
|
|
|
getMapPatcher(name)->patchMapHeader(header);
|
|
|
|
header.release();
|
|
|
|
|
|
|
|
return std::move(map);
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
2013-12-31 02:09:58 +03:00
|
|
|
std::unique_ptr<CMapHeader> CMapService::loadMapHeader(const ui8 * buffer, int size, const std::string & name)
|
2012-11-03 16:30:47 +03:00
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
auto stream = getStreamFromMem(buffer, size);
|
2013-12-31 02:09:58 +03:00
|
|
|
std::unique_ptr<CMapHeader> header = getMapLoader(stream)->loadMapHeader();
|
|
|
|
getMapPatcher(name)->patchMapHeader(header);
|
|
|
|
return std::move(header);
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<CInputStream> CMapService::getStreamFromFS(const std::string & name)
|
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
return CResourceHandler::get()->load(ResourceID(name, EResType::MAP));
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<CInputStream> CMapService::getStreamFromMem(const ui8 * buffer, int size)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<IMapLoader> CMapService::getMapLoader(std::unique_ptr<CInputStream> & stream)
|
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
// Read map header
|
|
|
|
CBinaryReader reader(stream.get());
|
|
|
|
ui32 header = reader.readUInt32();
|
|
|
|
reader.getStream()->seek(0);
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
return std::unique_ptr<IMapLoader>(new CMapLoaderH3M(stream.get()));
|
|
|
|
case EMapFormat::WOG :
|
|
|
|
case EMapFormat::AB :
|
|
|
|
case EMapFormat::ROE :
|
|
|
|
case EMapFormat::SOD :
|
|
|
|
return std::unique_ptr<IMapLoader>(new CMapLoaderH3M(stream.get()));
|
|
|
|
default :
|
|
|
|
throw std::runtime_error("Unknown map format");
|
|
|
|
}
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
2013-12-31 02:09:58 +03:00
|
|
|
|
2014-01-01 16:30:31 +03:00
|
|
|
static JsonNode loadPatches(std::string path)
|
|
|
|
{
|
|
|
|
JsonNode node = JsonUtils::assembleFromFiles(path);
|
|
|
|
for (auto & entry : node.Struct())
|
|
|
|
JsonUtils::validate(entry.second, "vcmi:mapHeader", "patch for " + entry.first);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2013-12-31 02:09:58 +03:00
|
|
|
std::unique_ptr<IMapPatcher> CMapService::getMapPatcher(std::string scenarioName)
|
|
|
|
{
|
2014-01-01 16:30:31 +03:00
|
|
|
static JsonNode node;
|
|
|
|
|
|
|
|
if (node.isNull())
|
|
|
|
node = loadPatches("config/mapOverrides.json");
|
|
|
|
|
2013-12-31 02:09:58 +03:00
|
|
|
boost::to_lower(scenarioName);
|
|
|
|
logGlobal->debugStream() << "Request to patch map " << scenarioName;
|
|
|
|
return std::unique_ptr<IMapPatcher>(new CMapLoaderJson(node[scenarioName]));
|
|
|
|
}
|