1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-30 08:57:00 +02:00
vcmi/lib/mapping/CMapService.cpp
Ivan Savenko 7e02f6b670 Support for overriding victory/defeat conditions from h3m map or
campaign:
- new file MapFormatJson that implements small subset of Json map
format, as described on wiki
- vcmi will read overrides from file config/mapOverrides.json (currently
empty)
- Json writer for logical expressions

TODO: write data for map overrides
2013-12-30 23:09:58 +00:00

96 lines
2.9 KiB
C++

#include "StdInc.h"
#include "CMapService.h"
#include "../filesystem/Filesystem.h"
#include "../filesystem/CBinaryReader.h"
#include "../filesystem/CCompressedStream.h"
#include "../filesystem/CMemoryStream.h"
#include "CMap.h"
#include "MapFormatH3M.h"
#include "MapFormatJson.h"
std::unique_ptr<CMap> CMapService::loadMap(const std::string & name)
{
auto stream = getStreamFromFS(name);
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);
}
std::unique_ptr<CMapHeader> CMapService::loadMapHeader(const std::string & name)
{
auto stream = getStreamFromFS(name);
std::unique_ptr<CMapHeader> header = getMapLoader(stream)->loadMapHeader();
getMapPatcher(name)->patchMapHeader(header);
return std::move(header);
}
std::unique_ptr<CMap> CMapService::loadMap(const ui8 * buffer, int size, const std::string & name)
{
auto stream = getStreamFromMem(buffer, size);
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);
}
std::unique_ptr<CMapHeader> CMapService::loadMapHeader(const ui8 * buffer, int size, const std::string & name)
{
auto stream = getStreamFromMem(buffer, size);
std::unique_ptr<CMapHeader> header = getMapLoader(stream)->loadMapHeader();
getMapPatcher(name)->patchMapHeader(header);
return std::move(header);
}
std::unique_ptr<CInputStream> CMapService::getStreamFromFS(const std::string & name)
{
return CResourceHandler::get()->load(ResourceID(name, EResType::MAP));
}
std::unique_ptr<CInputStream> CMapService::getStreamFromMem(const ui8 * buffer, int size)
{
return std::unique_ptr<CInputStream>(new CMemoryStream(buffer, size));
}
std::unique_ptr<IMapLoader> CMapService::getMapLoader(std::unique_ptr<CInputStream> & stream)
{
// 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");
}
}
std::unique_ptr<IMapPatcher> CMapService::getMapPatcher(std::string scenarioName)
{
boost::to_lower(scenarioName);
logGlobal->debugStream() << "Request to patch map " << scenarioName;
JsonNode node = JsonUtils::assembleFromFiles("config/mapOverrides.json");
return std::unique_ptr<IMapPatcher>(new CMapLoaderJson(node[scenarioName]));
}