File: MapFormatJson.h

package info (click to toggle)
vcmi 1.6.5%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 32,060 kB
  • sloc: cpp: 238,971; python: 265; sh: 224; xml: 157; ansic: 78; objc: 61; makefile: 49
file content (291 lines) | stat: -rw-r--r-- 6,477 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * 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 "../filesystem/CZipSaver.h"
#include "../filesystem/CZipLoader.h"
#include "../GameConstants.h"

#include "../serializer/JsonSerializeFormat.h"

VCMI_LIB_NAMESPACE_BEGIN

struct TriggeredEvent;
struct TerrainTile;
struct PlayerInfo;
class CGObjectInstance;
class AObjectTypeHandler;
class TerrainType;
class RoadType;
class RiverType;

class JsonSerializeFormat;
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;
	static const std::string TERRAIN_FILE_NAMES[2];

	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;

	/**
	 * ptr to the map header object which gets filled by data from the buffer.
	 * (when loading map and mapHeader point to the same object)
	 */
	CMapHeader * mapHeader;

	CMapFormatJson();

	static TerrainId getTerrainByCode(const std::string & code);
	static RiverId getRiverByCode(const std::string & code);
	static RoadId 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
	 */
	void readTriggeredEvents(JsonDeserializer & handler);

	/**
	 * Writes triggered events, including victory/loss conditions
	 */
	void writeTriggeredEvents(JsonSerializer & handler);

	/**
	 * Reads one of triggered events
	 */
	void readTriggeredEvent(TriggeredEvent & event, const JsonNode & source) const;

	/**
	 * Writes one of triggered events
	 */
	void writeTriggeredEvent(const TriggeredEvent & event, JsonNode & dest) const;

	void writeDisposedHeroes(JsonSerializeFormat & handler);

	void readDisposedHeroes(JsonSerializeFormat & handler);

	void serializePredefinedHeroes(JsonSerializeFormat & handler);

	void serializeRumors(JsonSerializeFormat & handler);
	
	void serializeTimedEvents(JsonSerializeFormat & handler);

	///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.
	 */
	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();

	JsonNode input;
};

class DLL_LINKAGE CMapLoaderJson : public CMapFormatJson, public IMapLoader
{
public:
	/**
	 * Constructor.
	 *
	 * @param stream a stream containing the map data
	 */
	CMapLoaderJson(CInputStream * stream);

	/**
	 * Loads the VCMI/Json map file.
	 *
	 * @return a unique ptr of the loaded map class
	 */
	std::unique_ptr<CMap> loadMap(IGameCallback * cb) 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;

		///constructs object (without configuration)
		void construct();

		///configures object
		void configure();
	};

	/**
	 * Reads the map header.
	 */
	void readHeader(const bool complete);

	/**
	 * Reads complete map.
	 */
	void readMap();
	
	/**
	 * Reads texts and translations
	 */
	void readTranslations();

	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();

	bool isExistArchive(const std::string & archiveFilename);
	JsonNode getFromArchive(const std::string & archiveFilename);

private:
	CInputStream * buffer;
	std::shared_ptr<CIOApi> ioApi;

	CZipLoader loader;///< object to handle zip archive operations
};

class DLL_LINKAGE CMapSaverJson : public CMapFormatJson, public IMapSaver
{
public:
	/**
	 * Constructor.
	 *
	 * @param stream a stream to save the map to, will contain zip archive
	 */
	CMapSaverJson(CInputOutputStream * stream);

	~CMapSaverJson();

	/**
	 * 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();
	
	/**
	 * Saves texts and translations to zip archive
	 */
	void writeTranslations();

	/**
	 * Encodes one tile into string
	 * @param tile tile to serialize
	 */
	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();

private:
	CInputOutputStream * buffer;
	std::shared_ptr<CIOApi> ioApi;
	CZipSaver saver;///< object to handle zip archive operations
};

VCMI_LIB_NAMESPACE_END