File: CMapService.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 (183 lines) | stat: -rw-r--r-- 5,524 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
/*
 * CMapService.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 "../modding/ModVerificationInfo.h"

VCMI_LIB_NAMESPACE_BEGIN

class ResourcePath;

class CMap;
class CMapHeader;
class CInputStream;

class IMapLoader;
class IMapPatcher;
class IGameCallback;

/**
 * The map service provides loading of VCMI/H3 map files. It can
 * be extended to save maps later as well.
 */
class DLL_LINKAGE IMapService
{
public:
	IMapService() = default;
	virtual ~IMapService() = default;
	/**
	 * Loads the VCMI/H3 map file specified by the name.
	 *
	 * @param name the name of the map
	 * @return a unique ptr to the loaded map class
	 */
	virtual std::unique_ptr<CMap> loadMap(const ResourcePath & name, IGameCallback * cb) const = 0;

	/**
	 * Loads the VCMI/H3 map header specified by the name.
	 *
	 * @param name the name of the map
	 * @return a unique ptr to the loaded map header class
	 */
	virtual std::unique_ptr<CMapHeader> loadMapHeader(const ResourcePath & name) const = 0;

	/**
	 * Loads the VCMI/H3 map file from a buffer. This method is temporarily
	 * in use to ease the transition to use the new map service.
@@ -60,8 +60,8 @@ class DLL_LINKAGE CMapService
	 * @param name indicates name of file that will be used during map header patching
	 * @return a unique ptr to the loaded map class
	 */
	virtual std::unique_ptr<CMap> loadMap(const uint8_t * buffer, int size, const std::string & name, const std::string & modName, const std::string & encoding, IGameCallback * cb) const = 0;

	/**
	 * Loads the VCMI/H3 map header from a buffer. This method is temporarily
	 * in use to ease the transition to use the new map service.
@@ -74,7 +74,27 @@ class DLL_LINKAGE CMapService
	 * @param name indicates name of file that will be used during map header patching
	 * @return a unique ptr to the loaded map class
	 */
	virtual std::unique_ptr<CMapHeader> loadMapHeader(const uint8_t * buffer, int size, const std::string & name, const std::string & modName, const std::string & encoding) const = 0;

	/**
	 * Saves map into VCMI format with name specified
	 * @param map to save
	 * @param fullPath full path to file to write, including extension
	 */
	virtual void saveMap(const std::unique_ptr<CMap> & map, boost::filesystem::path fullPath) const = 0;
};

class DLL_LINKAGE CMapService : public IMapService
{
public:
	CMapService() = default;
	virtual ~CMapService() = default;

	std::unique_ptr<CMap> loadMap(const ResourcePath & name, IGameCallback * cb) const override;
	std::unique_ptr<CMapHeader> loadMapHeader(const ResourcePath & name) const override;
	std::unique_ptr<CMap> loadMap(const uint8_t * buffer, int size, const std::string & name, const std::string & modName, const std::string & encoding, IGameCallback * cb) const override;
	std::unique_ptr<CMapHeader> loadMapHeader(const uint8_t * buffer, int size, const std::string & name, const std::string & modName, const std::string & encoding) const override;
	void saveMap(const std::unique_ptr<CMap> & map, boost::filesystem::path fullPath) const override;
	
	/**
	 * Tests if mods used in the map are currently loaded
	 * @param map const reference to map header
	 * @return data structure representing missing or incompatible mods (those which are needed from map but not loaded)
	 */
	static ModCompatibilityInfo verifyMapHeaderMods(const CMapHeader & map);

private:
	/**
	 * Gets a map input stream object specified by a map name.
	 *
	 * @param name the name of the map
	 * @return a unique ptr to the input stream class
	 */
	static std::unique_ptr<CInputStream> getStreamFromFS(const ResourcePath & name);

	/**
	 * Gets a map input stream from a buffer.
	 *
	 * @param buffer a pointer to a buffer containing the map data
	 * @param size the size of the buffer
	 * @return a unique ptr to the input stream class
	 */
	static std::unique_ptr<CInputStream> getStreamFromMem(const uint8_t * buffer, int size);

	/**
	 * Gets a map loader from the given stream. It performs checks to test
	 * in which map format the map is.
	 *
	 * @param stream the input map stream
	 * @return the constructed map loader
	 */
	static std::unique_ptr<IMapLoader> getMapLoader(std::unique_ptr<CInputStream> & stream, std::string mapName, std::string modName, std::string encoding);

	/**
	 * Gets a map patcher for specified scenario
	 *
	 * @param scenarioName for patcher
	 * @return the constructed map patcher
	 */
	static std::unique_ptr<IMapPatcher> getMapPatcher(std::string scenarioName);
};

/**
 * Interface for loading a map.
 */
class DLL_LINKAGE IMapLoader
{
public:
	/**
	 * Loads the VCMI/H3 map file.
	 *
	 * @return a unique ptr of the loaded map class
	 */
	virtual std::unique_ptr<CMap> loadMap(IGameCallback * cb) = 0;

	/**
	 * Loads the VCMI/H3 map header.
	 *
	 * @return a unique ptr of the loaded map header class
	 */
	virtual std::unique_ptr<CMapHeader> loadMapHeader() = 0;

	virtual ~IMapLoader(){}
};

class DLL_LINKAGE IMapPatcher
{
public:
	/**
	 * Modifies supplied map header using Json data
	 *
	 */
	virtual void patchMapHeader(std::unique_ptr<CMapHeader> & header) = 0;

	virtual ~IMapPatcher(){}
};

/**
 * Interface for saving a map.
 */
class DLL_LINKAGE IMapSaver
{
public:
	/**
	 * Saves the VCMI/H3 map file.
	 *
	 */
	 virtual void saveMap(const std::unique_ptr<CMap> & map) = 0;

	 virtual ~IMapSaver(){}
};

VCMI_LIB_NAMESPACE_END