2015-07-27 07:33:47 +02:00
|
|
|
/*
|
|
|
|
* CMapFormatTest.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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
2015-08-08 18:16:33 +02:00
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
#include "../../lib/JsonDetail.h"
|
2017-08-11 15:27:42 +02:00
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
#include "../../lib/filesystem/CMemoryBuffer.h"
|
|
|
|
#include "../../lib/filesystem/Filesystem.h"
|
2015-08-08 18:16:33 +02:00
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
#include "../../lib/mapping/CMap.h"
|
|
|
|
#include "../../lib/rmg/CMapGenOptions.h"
|
|
|
|
#include "../../lib/rmg/CMapGenerator.h"
|
|
|
|
#include "../../lib/mapping/MapFormatJson.h"
|
2015-08-08 18:16:33 +02:00
|
|
|
|
2015-08-13 02:29:13 +02:00
|
|
|
#include "../lib/VCMIDirs.h"
|
|
|
|
|
2015-08-08 18:16:33 +02:00
|
|
|
#include "MapComparer.h"
|
2018-03-05 20:26:00 +02:00
|
|
|
#include "../JsonComparer.h"
|
2015-07-27 07:33:47 +02:00
|
|
|
|
|
|
|
static const int TEST_RANDOM_SEED = 1337;
|
|
|
|
|
2017-08-03 14:09:30 +02:00
|
|
|
static void saveTestMap(CMemoryBuffer & serializeBuffer, const std::string & filename)
|
|
|
|
{
|
|
|
|
auto path = VCMIDirs::get().userDataPath() / filename;
|
|
|
|
boost::filesystem::remove(path);
|
|
|
|
boost::filesystem::ofstream tmp(path, boost::filesystem::ofstream::binary);
|
|
|
|
|
|
|
|
tmp.write((const char *)serializeBuffer.getBuffer().data(), serializeBuffer.getSize());
|
|
|
|
tmp.flush();
|
|
|
|
tmp.close();
|
|
|
|
}
|
2017-06-14 10:56:35 +02:00
|
|
|
|
2017-07-04 13:29:50 +02:00
|
|
|
TEST(MapFormat, Random)
|
2015-07-27 07:33:47 +02:00
|
|
|
{
|
2017-07-04 13:29:50 +02:00
|
|
|
SCOPED_TRACE("MapFormat_Random start");
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
CMapGenOptions opt;
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
opt.setHeight(CMapHeader::MAP_SIZE_MIDDLE);
|
|
|
|
opt.setWidth(CMapHeader::MAP_SIZE_MIDDLE);
|
|
|
|
opt.setHasTwoLevels(true);
|
|
|
|
opt.setPlayerCount(4);
|
2015-11-15 17:29:00 +02:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
opt.setPlayerTypeForStandardPlayer(PlayerColor(0), EPlayerType::HUMAN);
|
|
|
|
opt.setPlayerTypeForStandardPlayer(PlayerColor(1), EPlayerType::AI);
|
|
|
|
opt.setPlayerTypeForStandardPlayer(PlayerColor(2), EPlayerType::AI);
|
|
|
|
opt.setPlayerTypeForStandardPlayer(PlayerColor(3), EPlayerType::AI);
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
CMapGenerator gen;
|
2015-07-27 07:33:47 +02:00
|
|
|
|
2017-08-03 14:09:30 +02:00
|
|
|
std::unique_ptr<CMap> initialMap = gen.generate(&opt, TEST_RANDOM_SEED);
|
2016-11-13 12:38:42 +02:00
|
|
|
initialMap->name = "Test";
|
2017-07-04 13:29:50 +02:00
|
|
|
SCOPED_TRACE("MapFormat_Random generated");
|
2015-07-27 07:33:47 +02:00
|
|
|
|
2015-08-13 23:16:31 +02:00
|
|
|
CMemoryBuffer serializeBuffer;
|
2015-07-27 07:33:47 +02:00
|
|
|
{
|
2015-08-13 23:16:31 +02:00
|
|
|
CMapSaverJson saver(&serializeBuffer);
|
|
|
|
saver.saveMap(initialMap);
|
2015-07-27 07:33:47 +02:00
|
|
|
}
|
2017-07-04 13:29:50 +02:00
|
|
|
SCOPED_TRACE("MapFormat_Random serialized");
|
2017-08-03 14:09:30 +02:00
|
|
|
saveTestMap(serializeBuffer, "test_random.vmap");
|
2017-07-04 13:29:50 +02:00
|
|
|
SCOPED_TRACE("MapFormat_Random saved");
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2015-08-13 23:16:31 +02:00
|
|
|
serializeBuffer.seek(0);
|
|
|
|
{
|
|
|
|
CMapLoaderJson loader(&serializeBuffer);
|
|
|
|
std::unique_ptr<CMap> serialized = loader.loadMap();
|
|
|
|
|
|
|
|
MapComparer c;
|
|
|
|
c(serialized, initialMap);
|
|
|
|
}
|
2017-07-04 13:29:50 +02:00
|
|
|
SCOPED_TRACE("MapFormat_Random finish");
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static JsonNode getFromArchive(CZipLoader & archive, const std::string & archiveFilename)
|
|
|
|
{
|
|
|
|
ResourceID resource(archiveFilename, EResType::TEXT);
|
|
|
|
|
|
|
|
if(!archive.existsResource(resource))
|
2018-10-31 08:27:54 +02:00
|
|
|
throw std::runtime_error(archiveFilename+" not found");
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
auto data = archive.load(resource)->readAll();
|
|
|
|
|
|
|
|
JsonNode res(reinterpret_cast<char*>(data.first.get()), data.second);
|
|
|
|
|
|
|
|
return std::move(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void addToArchive(CZipSaver & saver, const JsonNode & data, const std::string & filename)
|
|
|
|
{
|
2017-08-11 15:27:42 +02:00
|
|
|
auto s = data.toJson();
|
|
|
|
std::unique_ptr<COutputStream> stream = saver.addFile(filename);
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2017-08-11 15:27:42 +02:00
|
|
|
if(stream->write((const ui8*)s.c_str(), s.size()) != s.size())
|
2018-10-31 08:27:54 +02:00
|
|
|
throw std::runtime_error("CMapSaverJson::saveHeader() zip compression failed.");
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
|
|
|
|
2017-08-03 12:50:54 +02:00
|
|
|
static std::unique_ptr<CMap> loadOriginal(const JsonNode & header, const JsonNode & objects, const JsonNode & surface, const JsonNode & underground)
|
|
|
|
{
|
|
|
|
CMemoryBuffer initialBuffer;
|
|
|
|
|
|
|
|
std::shared_ptr<CIOApi> originalDataIO(new CProxyIOApi(&initialBuffer));
|
|
|
|
|
|
|
|
{
|
|
|
|
CZipSaver initialSaver(originalDataIO, "_");
|
|
|
|
|
2017-08-03 14:09:30 +02:00
|
|
|
addToArchive(initialSaver, header, CMapFormatJson::HEADER_FILE_NAME);
|
|
|
|
addToArchive(initialSaver, objects, CMapFormatJson::OBJECTS_FILE_NAME);
|
2017-08-03 12:50:54 +02:00
|
|
|
addToArchive(initialSaver, surface, "surface_terrain.json");
|
|
|
|
addToArchive(initialSaver, underground, "underground_terrain.json");
|
|
|
|
}
|
|
|
|
|
|
|
|
initialBuffer.seek(0);
|
|
|
|
|
|
|
|
CMapLoaderJson initialLoader(&initialBuffer);
|
|
|
|
|
|
|
|
return initialLoader.loadMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void loadActual(CMemoryBuffer * serializeBuffer, const std::unique_ptr<CMap> & originalMap, JsonNode & header, JsonNode & objects, JsonNode & surface, JsonNode & underground)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
CMapSaverJson saver(serializeBuffer);
|
|
|
|
saver.saveMap(originalMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<CIOApi> actualDataIO(new CProxyROIOApi(serializeBuffer));
|
|
|
|
CZipLoader actualDataLoader("", "_", actualDataIO);
|
|
|
|
|
2017-08-03 14:09:30 +02:00
|
|
|
header = getFromArchive(actualDataLoader, CMapFormatJson::HEADER_FILE_NAME);
|
|
|
|
objects = getFromArchive(actualDataLoader, CMapFormatJson::OBJECTS_FILE_NAME);
|
2017-08-03 12:50:54 +02:00
|
|
|
surface = getFromArchive(actualDataLoader, "surface_terrain.json");
|
|
|
|
underground = getFromArchive(actualDataLoader, "underground_terrain.json");
|
|
|
|
}
|
|
|
|
|
2017-07-04 13:29:50 +02:00
|
|
|
TEST(MapFormat, Objects)
|
2016-11-13 12:38:42 +02:00
|
|
|
{
|
|
|
|
static const std::string MAP_DATA_PATH = "test/ObjectPropertyTest/";
|
|
|
|
|
2017-08-03 14:09:30 +02:00
|
|
|
const JsonNode initialHeader(ResourceID(MAP_DATA_PATH+CMapFormatJson::HEADER_FILE_NAME));
|
|
|
|
const JsonNode expectedHeader(ResourceID(MAP_DATA_PATH+CMapFormatJson::HEADER_FILE_NAME));//same as initial for now
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2017-08-03 14:09:30 +02:00
|
|
|
const JsonNode initialObjects(ResourceID(MAP_DATA_PATH+CMapFormatJson::OBJECTS_FILE_NAME));
|
2016-11-13 12:38:42 +02:00
|
|
|
const JsonNode expectedObjects(ResourceID(MAP_DATA_PATH+"objects.ex.json"));
|
|
|
|
|
|
|
|
const JsonNode expectedSurface(ResourceID(MAP_DATA_PATH+"surface_terrain.json"));
|
|
|
|
const JsonNode expectedUnderground(ResourceID(MAP_DATA_PATH+"underground_terrain.json"));
|
|
|
|
|
2017-08-03 12:50:54 +02:00
|
|
|
std::unique_ptr<CMap> originalMap = loadOriginal(initialHeader, initialObjects, expectedSurface, expectedUnderground);
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2017-08-03 12:50:54 +02:00
|
|
|
CMemoryBuffer serializeBuffer;
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2017-08-03 12:50:54 +02:00
|
|
|
JsonNode actualHeader;
|
|
|
|
JsonNode actualObjects;
|
|
|
|
JsonNode actualSurface;
|
|
|
|
JsonNode actualUnderground;
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2017-08-03 12:50:54 +02:00
|
|
|
loadActual(&serializeBuffer, originalMap, actualHeader, actualObjects, actualSurface, actualUnderground);
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2017-08-03 12:50:54 +02:00
|
|
|
saveTestMap(serializeBuffer, "test_object_property.vmap");
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2017-08-03 12:50:54 +02:00
|
|
|
{
|
2018-03-05 20:26:00 +02:00
|
|
|
JsonComparer c(false);
|
|
|
|
c.compare("hdr", actualHeader, expectedHeader);
|
|
|
|
c.compare("obj", actualObjects, expectedObjects);
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-03-05 20:26:00 +02:00
|
|
|
JsonComparer c(true);
|
2017-08-03 12:50:54 +02:00
|
|
|
c.compare("surface", actualSurface, expectedSurface);
|
|
|
|
c.compare("underground", actualUnderground, expectedUnderground);
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
2017-08-03 12:50:54 +02:00
|
|
|
}
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2017-08-03 12:50:54 +02:00
|
|
|
TEST(MapFormat, Terrain)
|
|
|
|
{
|
|
|
|
static const std::string MAP_DATA_PATH = "test/TerrainTest/";
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2017-08-03 14:09:30 +02:00
|
|
|
const JsonNode expectedHeader(ResourceID(MAP_DATA_PATH+CMapFormatJson::HEADER_FILE_NAME));
|
|
|
|
const JsonNode expectedObjects(ResourceID(MAP_DATA_PATH+CMapFormatJson::OBJECTS_FILE_NAME));
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2017-08-03 12:50:54 +02:00
|
|
|
const JsonNode expectedSurface(ResourceID(MAP_DATA_PATH+"surface_terrain.json"));
|
|
|
|
const JsonNode expectedUnderground(ResourceID(MAP_DATA_PATH+"underground_terrain.json"));
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2017-08-03 14:09:30 +02:00
|
|
|
std::unique_ptr<CMap> originalMap = loadOriginal(expectedHeader, expectedObjects, expectedSurface, expectedUnderground);
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2017-08-03 12:50:54 +02:00
|
|
|
CMemoryBuffer serializeBuffer;
|
|
|
|
|
|
|
|
JsonNode actualHeader;
|
|
|
|
JsonNode actualObjects;
|
|
|
|
JsonNode actualSurface;
|
|
|
|
JsonNode actualUnderground;
|
|
|
|
|
|
|
|
loadActual(&serializeBuffer, originalMap, actualHeader, actualObjects, actualSurface, actualUnderground);
|
|
|
|
|
|
|
|
saveTestMap(serializeBuffer, "test_terrain.vmap");
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
{
|
2018-03-05 20:26:00 +02:00
|
|
|
JsonComparer c(false);
|
|
|
|
c.compare("hdr", actualHeader, expectedHeader);
|
|
|
|
c.compare("obj", actualObjects, expectedObjects);
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-03-05 20:26:00 +02:00
|
|
|
JsonComparer c(true);
|
2017-08-03 12:50:54 +02:00
|
|
|
c.compare("surface", actualSurface, expectedSurface);
|
|
|
|
c.compare("underground", actualUnderground, expectedUnderground);
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
2015-07-27 07:33:47 +02:00
|
|
|
}
|