File: cmodmanager.cpp

package info (click to toggle)
vcmi 0.99%2Bdfsg%2Bgit20190113.f06c8a87-2
  • links: PTS, VCS
  • area: contrib
  • in suites: bullseye
  • size: 11,136 kB
  • sloc: cpp: 142,615; sh: 315; objc: 248; makefile: 32; ansic: 28; python: 13
file content (309 lines) | stat: -rw-r--r-- 8,158 bytes parent folder | download | duplicates (2)
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * cmodmanager.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 "cmodmanager.h"

#include "../../lib/VCMIDirs.h"
#include "../../lib/filesystem/Filesystem.h"
#include "../../lib/filesystem/CZipLoader.h"
#include "../../lib/CModHandler.h"

#include "../jsonutils.h"
#include "../launcherdirs.h"

static QString detectModArchive(QString path, QString modName)
{
	auto files = ZipArchive::listFiles(qstringToPath(path));

	QString modDirName;

	for(auto file : files)
	{
		QString filename = QString::fromUtf8(file.c_str());
		if(filename.toLower().startsWith(modName))
		{
			// archive must contain mod.json file
			if(filename.toLower() == modName + "/mod.json")
				modDirName = filename.section('/', 0, 0);
		}
		else // all files must be in <modname> directory
		{
			return "";
		}
	}
	return modDirName;
}

CModManager::CModManager(CModList * modList)
	: modList(modList)
{
	loadMods();
	loadModSettings();
}

QString CModManager::settingsPath()
{
	return pathToQString(VCMIDirs::get().userConfigPath() / "modSettings.json");
}

void CModManager::loadModSettings()
{
	modSettings = JsonUtils::JsonFromFile(settingsPath()).toMap();
	modList->setModSettings(modSettings["activeMods"]);
}

void CModManager::resetRepositories()
{
	modList->resetRepositories();
}

void CModManager::loadRepository(QString file)
{
	modList->addRepository(JsonUtils::JsonFromFile(file).toMap());
}

void CModManager::loadMods()
{
	CModHandler handler;
	handler.loadMods();
	auto installedMods = handler.getAllMods();

	for(auto modname : installedMods)
	{
		ResourceID resID(CModInfo::getModFile(modname));
		if(CResourceHandler::get()->existsResource(resID))
		{
			boost::filesystem::path name = *CResourceHandler::get()->getResourceName(resID);
			auto mod = JsonUtils::JsonFromFile(pathToQString(name));
			localMods.insert(QString::fromUtf8(modname.c_str()).toLower(), mod);
		}
	}
	modList->setLocalModList(localMods);
}

bool CModManager::addError(QString modname, QString message)
{
	recentErrors.push_back(QString("%1: %2").arg(modname).arg(message));
	return false;
}

QStringList CModManager::getErrors()
{
	QStringList ret = recentErrors;
	recentErrors.clear();
	return ret;
}

bool CModManager::installMod(QString modname, QString archivePath)
{
	return canInstallMod(modname) && doInstallMod(modname, archivePath);
}

bool CModManager::uninstallMod(QString modname)
{
	return canUninstallMod(modname) && doUninstallMod(modname);
}

bool CModManager::enableMod(QString modname)
{
	return canEnableMod(modname) && doEnableMod(modname, true);
}

bool CModManager::disableMod(QString modname)
{
	return canDisableMod(modname) && doEnableMod(modname, false);
}

bool CModManager::canInstallMod(QString modname)
{
	auto mod = modList->getMod(modname);

	if(mod.getName().contains('.'))
		return addError(modname, "Can not install submod");

	if(mod.isInstalled())
		return addError(modname, "Mod is already installed");

	if(!mod.isAvailable())
		return addError(modname, "Mod is not available");
	return true;
}

bool CModManager::canUninstallMod(QString modname)
{
	auto mod = modList->getMod(modname);

	if(mod.getName().contains('.'))
		return addError(modname, "Can not uninstall submod");

	if(!mod.isInstalled())
		return addError(modname, "Mod is not installed");

	if(mod.isEnabled())
		return addError(modname, "Mod must be disabled first");
	return true;
}

bool CModManager::canEnableMod(QString modname)
{
	auto mod = modList->getMod(modname);

	if(mod.isEnabled())
		return addError(modname, "Mod is already enabled");

	if(!mod.isInstalled())
		return addError(modname, "Mod must be installed first");

	for(auto modEntry : mod.getValue("depends").toStringList())
	{
		if(!modList->hasMod(modEntry)) // required mod is not available
			return addError(modname, QString("Required mod %1 is missing").arg(modEntry));
		if(!modList->getMod(modEntry).isEnabled())
			return addError(modname, QString("Required mod %1 is not enabled").arg(modEntry));
	}

	for(QString modEntry : modList->getModList())
	{
		auto mod = modList->getMod(modEntry);

		// "reverse conflict" - enabled mod has this one as conflict
		if(mod.isEnabled() && mod.getValue("conflicts").toStringList().contains(modname))
			return addError(modname, QString("This mod conflicts with %1").arg(modEntry));
	}

	for(auto modEntry : mod.getValue("conflicts").toStringList())
	{
		// check if conflicting mod installed and enabled
		if(modList->hasMod(modEntry) && modList->getMod(modEntry).isEnabled())
			return addError(modname, QString("This mod conflicts with %1").arg(modEntry));
	}
	return true;
}

bool CModManager::canDisableMod(QString modname)
{
	auto mod = modList->getMod(modname);

	if(mod.isDisabled())
		return addError(modname, "Mod is already disabled");

	if(!mod.isInstalled())
		return addError(modname, "Mod must be installed first");

	for(QString modEntry : modList->getModList())
	{
		auto current = modList->getMod(modEntry);

		if(current.getValue("depends").toStringList().contains(modname) && current.isEnabled())
			return addError(modname, QString("This mod is needed to run %1").arg(modEntry));
	}
	return true;
}

static QVariant writeValue(QString path, QVariantMap input, QVariant value)
{
	if(path.size() > 1)
	{

		QString entryName = path.section('/', 0, 1);
		QString remainder = "/" + path.section('/', 2, -1);

		entryName.remove(0, 1);
		input.insert(entryName, writeValue(remainder, input.value(entryName).toMap(), value));
		return input;
	}
	else
	{
		return value;
	}
}

bool CModManager::doEnableMod(QString mod, bool on)
{
	QString path = mod;
	path = "/activeMods/" + path.replace(".", "/mods/") + "/active";

	modSettings = writeValue(path, modSettings, QVariant(on)).toMap();
	modList->setModSettings(modSettings["activeMods"]);
	modList->modChanged(mod);

	JsonUtils::JsonToFile(settingsPath(), modSettings);

	return true;
}

bool CModManager::doInstallMod(QString modname, QString archivePath)
{
	QString destDir = CLauncherDirs::get().modsPath() + "/";

	if(!QFile(archivePath).exists())
		return addError(modname, "Mod archive is missing");

	if(localMods.contains(modname))
		return addError(modname, "Mod with such name is already installed");

	QString modDirName = detectModArchive(archivePath, modname);
	if(!modDirName.size())
		return addError(modname, "Mod archive is invalid or corrupted");

	if(!ZipArchive::extract(qstringToPath(archivePath), qstringToPath(destDir)))
	{
		removeModDir(destDir + modDirName);
		return addError(modname, "Failed to extract mod data");
	}

	QVariantMap json = JsonUtils::JsonFromFile(destDir + modDirName + "/mod.json").toMap();

	localMods.insert(modname, json);
	modList->setLocalModList(localMods);
	modList->modChanged(modname);

	return true;
}

bool CModManager::doUninstallMod(QString modname)
{
	ResourceID resID(std::string("Mods/") + modname.toUtf8().data(), EResType::DIRECTORY);
	// Get location of the mod, in case-insensitive way
	QString modDir = pathToQString(*CResourceHandler::get()->getResourceName(resID));

	if(!QDir(modDir).exists())
		return addError(modname, "Data with this mod was not found");

	if(!localMods.contains(modname))
		return addError(modname, "Data with this mod was not found");

	if(!removeModDir(modDir))
		return addError(modname, "Failed to delete mod data");

	localMods.remove(modname);
	modList->setLocalModList(localMods);
	modList->modChanged(modname);

	return true;
}

bool CModManager::removeModDir(QString path)
{
	// issues 2673 and 2680 its why you do not recursively remove without sanity check
	QDir checkDir(path);
	if(!checkDir.cdUp() || QString::compare("Mods", checkDir.dirName(), Qt::CaseInsensitive))
		return false;
	if(!checkDir.cdUp() || QString::compare("vcmi", checkDir.dirName(), Qt::CaseInsensitive))
		return false;

	QDir dir(path);
	if(!dir.absolutePath().contains("vcmi", Qt::CaseInsensitive))
		return false;
	if(!dir.absolutePath().contains("Mods", Qt::CaseInsensitive))
		return false;

	return dir.removeRecursively();
}