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/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"
|
2022-06-28 10:05:30 +02:00
|
|
|
#include "mock/ZoneOptionsFake.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);
|
2023-07-28 14:51:14 +02:00
|
|
|
std::ofstream tmp(path.c_str(), std::ofstream::binary);
|
2017-08-03 14:09:30 +02:00
|
|
|
|
|
|
|
tmp.write((const char *)serializeBuffer.getBuffer().data(), serializeBuffer.getSize());
|
|
|
|
tmp.flush();
|
|
|
|
tmp.close();
|
|
|
|
}
|
2017-06-14 10:56:35 +02:00
|
|
|
|
2024-01-03 02:26:15 +02:00
|
|
|
TEST(MapFormat, DISABLED_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;
|
2021-07-05 19:04:17 +02:00
|
|
|
CRmgTemplate tmpl;
|
2024-01-17 14:50:00 +02:00
|
|
|
auto zoneOptions = std::make_shared<ZoneOptionsFake>();
|
2021-07-05 19:04:17 +02:00
|
|
|
|
2023-10-29 22:25:39 +02:00
|
|
|
const_cast<CRmgTemplate::CPlayerCountRange &>(tmpl.getHumanPlayers()).addRange(1, 4);
|
2022-06-28 10:05:30 +02:00
|
|
|
const_cast<CRmgTemplate::Zones &>(tmpl.getZones())[0] = zoneOptions;
|
2021-07-05 19:04:17 +02:00
|
|
|
|
2022-06-28 10:05:30 +02:00
|
|
|
zoneOptions->setOwner(1);
|
2021-07-05 19:04:17 +02:00
|
|
|
opt.setMapTemplate(&tmpl);
|
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);
|
2023-10-28 20:30:38 +02:00
|
|
|
opt.setHumanOrCpuPlayerCount(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
|
|
|
|
2024-01-10 21:30:12 +02:00
|
|
|
CMapGenerator gen(opt, nullptr, TEST_RANDOM_SEED);
|
2015-07-27 07:33:47 +02:00
|
|
|
|
2022-05-28 15:03:50 +02:00
|
|
|
std::unique_ptr<CMap> initialMap = gen.generate();
|
2023-09-29 00:28:09 +02:00
|
|
|
initialMap->name.appendRawString("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);
|
2024-01-10 21:30:12 +02:00
|
|
|
std::unique_ptr<CMap> serialized = loader.loadMap(nullptr);
|
2015-08-13 23:16:31 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2023-09-04 12:03:15 +02:00
|
|
|
JsonPath resource = JsonPath::builtin(archiveFilename);
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
if(!archive.existsResource(resource))
|
2021-07-15 23:32:13 +02:00
|
|
|
throw std::runtime_error(archiveFilename + " not found");
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
auto data = archive.load(resource)->readAll();
|
|
|
|
|
2024-07-17 15:20:18 +02:00
|
|
|
JsonNode res(reinterpret_cast<const std::byte *>(data.first.get()), data.second, resource.getName());
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2019-01-19 12:52:02 +02:00
|
|
|
return res;
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void addToArchive(CZipSaver & saver, const JsonNode & data, const std::string & filename)
|
|
|
|
{
|
2024-02-12 01:22:16 +02:00
|
|
|
auto s = data.toString();
|
2017-08-11 15:27:42 +02:00
|
|
|
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);
|
|
|
|
|
2024-01-10 21:30:12 +02:00
|
|
|
return initialLoader.loadMap(nullptr);
|
2017-08-03 12:50:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2024-01-03 02:26:15 +02:00
|
|
|
TEST(MapFormat, DISABLED_Objects)
|
2016-11-13 12:38:42 +02:00
|
|
|
{
|
|
|
|
static const std::string MAP_DATA_PATH = "test/ObjectPropertyTest/";
|
|
|
|
|
2023-09-01 23:26:14 +02:00
|
|
|
const JsonNode initialHeader(JsonPath::builtin(MAP_DATA_PATH + CMapFormatJson::HEADER_FILE_NAME));
|
|
|
|
const JsonNode expectedHeader(JsonPath::builtin(MAP_DATA_PATH + CMapFormatJson::HEADER_FILE_NAME));//same as initial for now
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2023-09-01 23:26:14 +02:00
|
|
|
const JsonNode initialObjects(JsonPath::builtin(MAP_DATA_PATH + CMapFormatJson::OBJECTS_FILE_NAME));
|
|
|
|
const JsonNode expectedObjects(JsonPath::builtin(MAP_DATA_PATH + "objects.ex.json"));
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2023-09-01 23:26:14 +02:00
|
|
|
const JsonNode expectedSurface(JsonPath::builtin(MAP_DATA_PATH + "surface_terrain.json"));
|
|
|
|
const JsonNode expectedUnderground(JsonPath::builtin(MAP_DATA_PATH + "underground_terrain.json"));
|
2016-11-13 12:38:42 +02:00
|
|
|
|
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
|
|
|
|
2024-01-03 02:26:15 +02:00
|
|
|
TEST(MapFormat, DISABLED_Terrain)
|
2017-08-03 12:50:54 +02:00
|
|
|
{
|
|
|
|
static const std::string MAP_DATA_PATH = "test/TerrainTest/";
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2023-09-01 23:26:14 +02:00
|
|
|
const JsonNode expectedHeader(JsonPath::builtin(MAP_DATA_PATH + CMapFormatJson::HEADER_FILE_NAME));
|
|
|
|
const JsonNode expectedObjects(JsonPath::builtin(MAP_DATA_PATH + CMapFormatJson::OBJECTS_FILE_NAME));
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2023-09-01 23:26:14 +02:00
|
|
|
const JsonNode expectedSurface(JsonPath::builtin(MAP_DATA_PATH + "surface_terrain.json"));
|
|
|
|
const JsonNode expectedUnderground(JsonPath::builtin(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
|
|
|
}
|