1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-28 08:48:48 +02:00
vcmi/lib/mapping/MapFormatJson.h

279 lines
6.3 KiB
C
Raw Normal View History

/*
* MapFormatJson.h, 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
*
*/
#pragma once
#include "CMapService.h"
#include "../JsonNode.h"
2015-08-11 20:20:13 +02:00
#include "../filesystem/CZipSaver.h"
#include "../filesystem/CZipLoader.h"
#include "../GameConstants.h"
2015-08-11 20:20:13 +02:00
#include "../serializer/JsonSerializeFormat.h"
VCMI_LIB_NAMESPACE_BEGIN
struct TriggeredEvent;
struct TerrainTile;
2015-08-20 05:16:31 +02:00
struct PlayerInfo;
class CGObjectInstance;
class AObjectTypeHandler;
class TerrainType;
class RoadType;
class RiverType;
class JsonSerializeFormat;
2016-02-13 09:47:40 +02:00
class JsonDeserializer;
class JsonSerializer;
class DLL_LINKAGE CMapFormatJson
{
public:
static const int VERSION_MAJOR;
static const int VERSION_MINOR;
static const std::string HEADER_FILE_NAME;
static const std::string OBJECTS_FILE_NAME;
2016-02-22 22:43:57 +02:00
int fileVersionMajor;
int fileVersionMinor;
protected:
friend class MapObjectResolver;
std::unique_ptr<IInstanceResolver> mapObjectResolver;
/** ptr to the map object which gets filled by data from the buffer or written to buffer */
CMap * map;
/**
2015-08-14 04:22:24 +02:00
* ptr to the map header object which gets filled by data from the buffer.
* (when loading map and mapHeader point to the same object)
*/
2016-02-13 14:22:26 +02:00
CMapHeader * mapHeader;
CMapFormatJson();
2023-02-11 18:30:06 +02:00
static TerrainType * getTerrainByCode(const std::string & code);
static RiverType * getRiverByCode(const std::string & code);
static RoadType * getRoadByCode(const std::string & code);
void serializeAllowedFactions(JsonSerializeFormat & handler, std::set<FactionID> & value) const;
///common part of header saving/loading
void serializeHeader(JsonSerializeFormat & handler);
///player information saving/loading
void serializePlayerInfo(JsonSerializeFormat & handler);
/**
* Reads team settings to header
*/
void readTeams(JsonDeserializer & handler);
/**
* Saves team settings to header
*/
void writeTeams(JsonSerializer & handler);
/**
* Reads triggered events, including victory/loss conditions
*/
2016-02-13 14:22:26 +02:00
void readTriggeredEvents(JsonDeserializer & handler);
2015-08-14 04:22:24 +02:00
/**
* Writes triggered events, including victory/loss conditions
*/
void writeTriggeredEvents(JsonSerializer & handler);
2015-08-14 04:22:24 +02:00
/**
* Reads one of triggered events
*/
2023-02-11 18:30:06 +02:00
void readTriggeredEvent(TriggeredEvent & event, const JsonNode & source) const;
2015-08-14 04:22:24 +02:00
/**
* Writes one of triggered events
*/
2023-02-11 18:30:06 +02:00
void writeTriggeredEvent(const TriggeredEvent & event, JsonNode & dest) const;
2016-02-21 19:58:09 +02:00
void writeDisposedHeroes(JsonSerializeFormat & handler);
void readDisposedHeroes(JsonSerializeFormat & handler);
2016-02-21 19:58:09 +02:00
void serializePredefinedHeroes(JsonSerializeFormat & handler);
void serializeRumors(JsonSerializeFormat & handler);
2016-02-21 19:58:09 +02:00
///common part of map attributes saving/loading
void serializeOptions(JsonSerializeFormat & handler);
/**
* Loads map attributes except header ones
*/
void readOptions(JsonDeserializer & handler);
/**
* Saves map attributes except header ones
*/
void writeOptions(JsonSerializer & handler);
};
class DLL_LINKAGE CMapPatcher : public CMapFormatJson, public IMapPatcher
{
public:
/**
* Default constructor.
*
* @param stream. A stream containing the map data.
*/
2023-02-11 18:30:06 +02:00
CMapPatcher(const JsonNode & stream);
public: //IMapPatcher
/**
* Modifies supplied map header using Json data
*
*/
void patchMapHeader(std::unique_ptr<CMapHeader> & header) override;
private:
/**
* Reads subset of header that can be replaced by patching.
*/
void readPatchData();
2016-02-13 14:22:26 +02:00
JsonNode input;
};
2016-02-10 06:28:00 +02:00
class DLL_LINKAGE CMapLoaderJson : public CMapFormatJson, public IMapLoader
{
public:
/**
* Constructor.
*
* @param stream a stream containing the map data
*/
2016-02-10 06:28:00 +02:00
CMapLoaderJson(CInputStream * stream);
/**
* Loads the VCMI/Json map file.
*
* @return a unique ptr of the loaded map class
*/
std::unique_ptr<CMap> loadMap() override;
/**
* Loads the VCMI/Json map header.
*
* @return a unique ptr of the loaded map header class
*/
std::unique_ptr<CMapHeader> loadMapHeader() override;
struct MapObjectLoader
{
MapObjectLoader(CMapLoaderJson * _owner, JsonMap::value_type & json);
CMapLoaderJson * owner;
CGObjectInstance * instance;
ObjectInstanceID id;
std::string jsonKey;//full id defined by map creator
JsonNode & configuration;
2016-02-22 18:26:42 +02:00
///constructs object (without configuration)
void construct();
///configures object
void configure();
};
/**
* Reads the map header.
*/
2016-02-21 19:58:09 +02:00
void readHeader(const bool complete);
/**
* Reads complete map.
*/
void readMap();
2017-08-03 12:50:54 +02:00
static void readTerrainTile(const std::string & src, TerrainTile & tile);
void readTerrainLevel(const JsonNode & src, const int index);
void readTerrain();
/**
* Loads all map objects from zip archive
*/
void readObjects();
JsonNode getFromArchive(const std::string & archiveFilename);
2017-08-03 12:50:54 +02:00
private:
2016-02-10 06:28:00 +02:00
CInputStream * buffer;
std::shared_ptr<CIOApi> ioApi;
CZipLoader loader;///< object to handle zip archive operations
};
2015-08-08 16:30:19 +02:00
2016-02-10 06:28:00 +02:00
class DLL_LINKAGE CMapSaverJson : public CMapFormatJson, public IMapSaver
2015-08-08 16:30:19 +02:00
{
public:
2015-08-08 17:35:54 +02:00
/**
* Constructor.
2015-08-08 17:35:54 +02:00
*
* @param stream a stream to save the map to, will contain zip archive
2015-08-08 17:35:54 +02:00
*/
CMapSaverJson(CInputOutputStream * stream);
2015-08-11 20:20:13 +02:00
~CMapSaverJson();
2015-08-08 17:35:54 +02:00
/**
* Actually saves the VCMI/Json map into stream.
*/
void saveMap(const std::unique_ptr<CMap> & map) override;
/**
* Saves @data as json file with specified @filename
*/
void addToArchive(const JsonNode & data, const std::string & filename);
/**
* Saves header to zip archive
*/
void writeHeader();
/**
* Encodes one tile into string
* @param tile tile to serialize
*/
2017-08-03 12:50:54 +02:00
static std::string writeTerrainTile(const TerrainTile & tile);
/**
* Saves map level into json
* @param index z coordinate
*/
JsonNode writeTerrainLevel(const int index);
/**
* Saves all terrain into zip archive
*/
void writeTerrain();
/**
* Saves all map objects into zip archive
*/
void writeObjects();
2017-08-03 12:50:54 +02:00
private:
2016-02-10 06:28:00 +02:00
CInputOutputStream * buffer;
std::shared_ptr<CIOApi> ioApi;
CZipSaver saver;///< object to handle zip archive operations
2015-08-08 16:30:19 +02:00
};
VCMI_LIB_NAMESPACE_END