File: CMapService.cpp

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 (185 lines) | stat: -rw-r--r-- 5,830 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
/*
 * CMapService.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"
#include "CMapService.h"

#include "../json/JsonUtils.h"
#include "../filesystem/Filesystem.h"
#include "../filesystem/CBinaryReader.h"
#include "../filesystem/CCompressedStream.h"
#include "../filesystem/CMemoryStream.h"
#include "../filesystem/CMemoryBuffer.h"
#include "../modding/CModHandler.h"
#include "../modding/ModDescription.h"
#include "../modding/ModScope.h"
#include "../VCMI_Lib.h"

#include "CMap.h"
#include "MapFormat.h"

#include "MapFormatH3M.h"
#include "MapFormatJson.h"

VCMI_LIB_NAMESPACE_BEGIN


std::unique_ptr<CMap> CMapService::loadMap(const ResourcePath & name, IGameCallback * cb) const
{
	std::string modName = VLC->modh->findResourceOrigin(name);
	std::string encoding = VLC->modh->findResourceEncoding(name);

	auto stream = getStreamFromFS(name);
	return getMapLoader(stream, name.getName(), modName, encoding)->loadMap(cb);
}

std::unique_ptr<CMapHeader> CMapService::loadMapHeader(const ResourcePath & name) const
{
	std::string modName = VLC->modh->findResourceOrigin(name);
	std::string encoding = VLC->modh->findResourceEncoding(name);

	auto stream = getStreamFromFS(name);
	return getMapLoader(stream, name.getName(), modName, encoding)->loadMapHeader();
}

std::unique_ptr<CMap> CMapService::loadMap(const uint8_t * buffer, int size, const std::string & name,  const std::string & modName, const std::string & encoding, IGameCallback * cb) const
{
	auto stream = getStreamFromMem(buffer, size);
	std::unique_ptr<CMap> map(getMapLoader(stream, name, modName, encoding)->loadMap(cb));
	std::unique_ptr<CMapHeader> header(map.get());

	//might be original campaign and require patch
	getMapPatcher(name)->patchMapHeader(header);
	header.release();

	return map;
}

std::unique_ptr<CMapHeader> CMapService::loadMapHeader(const uint8_t * buffer, int size, const std::string & name, const std::string & modName, const std::string & encoding) const
{
	auto stream = getStreamFromMem(buffer, size);
	std::unique_ptr<CMapHeader> header = getMapLoader(stream, name, modName, encoding)->loadMapHeader();

	//might be original campaign and require patch
	getMapPatcher(name)->patchMapHeader(header);
	return header;
}

void CMapService::saveMap(const std::unique_ptr<CMap> & map, boost::filesystem::path fullPath) const
{
	CMemoryBuffer serializeBuffer;
	{
		CMapSaverJson saver(&serializeBuffer);
		saver.saveMap(map);
	}
	{
		boost::filesystem::remove(fullPath);
		std::ofstream tmp(fullPath.c_str(), std::ofstream::binary);

		tmp.write(reinterpret_cast<const char *>(serializeBuffer.getBuffer().data()), serializeBuffer.getSize());
		tmp.flush();
		tmp.close();
	}
}

ModCompatibilityInfo CMapService::verifyMapHeaderMods(const CMapHeader & map)
{
	const auto & activeMods = VLC->modh->getActiveMods();
	
	ModCompatibilityInfo missingMods;
	ModCompatibilityInfo missingModsFiltered;
	for(const auto & mapMod : map.mods)
	{
		if(vstd::contains(activeMods, mapMod.first))
		{
			const auto & modInfo = VLC->modh->getModInfo(mapMod.first);
			if(modInfo.getVersion().compatible(mapMod.second.version))
				continue;
		}
		missingMods[mapMod.first] = mapMod.second;
	}
	
	//filter child mods
	for(const auto & mapMod : missingMods)
	{
		if(!mapMod.second.parent.empty() && missingMods.count(mapMod.second.parent))
			continue;
		missingModsFiltered.insert(mapMod);
	}
	
	return missingModsFiltered;
}

std::unique_ptr<CInputStream> CMapService::getStreamFromFS(const ResourcePath & name)
{
	return CResourceHandler::get()->load(name);
}

std::unique_ptr<CInputStream> CMapService::getStreamFromMem(const uint8_t * buffer, int size)
{
	return std::unique_ptr<CInputStream>(new CMemoryStream(buffer, size));
}

std::unique_ptr<IMapLoader> CMapService::getMapLoader(std::unique_ptr<CInputStream> & stream, std::string mapName, std::string modName, std::string encoding)
{
	// Read map header
	CBinaryReader reader(stream.get());
	ui32 header = reader.readUInt32();
	reader.getStream()->seek(0);

	//check for ZIP magic. Zip files are VCMI maps
	switch(header)
	{
	case 0x06054b50:
	case 0x04034b50:
	case 0x02014b50:
		return std::unique_ptr<IMapLoader>(new CMapLoaderJson(stream.get()));
		break;
	default:
		// Check which map format is used
		// gzip header is 3 bytes only in size
		switch(header & 0xffffff)
		{
			// gzip header magic number, reversed for LE
			case 0x00088B1F:
				stream = std::unique_ptr<CInputStream>(new CCompressedStream(std::move(stream), true));
				return std::unique_ptr<IMapLoader>(new CMapLoaderH3M(mapName, modName, encoding, stream.get()));
			case static_cast<int>(EMapFormat::WOG) :
			case static_cast<int>(EMapFormat::AB)  :
			case static_cast<int>(EMapFormat::ROE) :
			case static_cast<int>(EMapFormat::SOD) :
			case static_cast<int>(EMapFormat::CHR) :
			case static_cast<int>(EMapFormat::HOTA) :
				return std::unique_ptr<IMapLoader>(new CMapLoaderH3M(mapName, modName, encoding, stream.get()));
			default :
				throw std::runtime_error("Unknown map format");
		}
	}
}

static JsonNode loadPatches(const std::string & path)
{
	JsonNode node = JsonUtils::assembleFromFiles(path);
	for (auto & entry : node.Struct())
		JsonUtils::validate(entry.second, "vcmi:mapHeader", "patch for " + entry.first);

	node.setModScope(ModScope::scopeMap());
	return node;
}

std::unique_ptr<IMapPatcher> CMapService::getMapPatcher(std::string scenarioName)
{
	static const JsonNode node = loadPatches("config/mapOverrides.json");

	boost::to_lower(scenarioName);
	logGlobal->debug("Request to patch map %s", scenarioName);
	return std::unique_ptr<IMapPatcher>(new CMapPatcher(node[scenarioName]));
}

VCMI_LIB_NAMESPACE_END