summaryrefslogtreecommitdiffstats
path: root/api/logic/FolderInstanceProvider.cpp
blob: a0fbd46a00fcdb0abd4e2fdddfb8baa1ee29e16c (plain)
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#include "FolderInstanceProvider.h"
#include "settings/INISettingsObject.h"
#include "FileSystem.h"
#include "minecraft/MinecraftInstance.h"
#include "NullInstance.h"

#include <QDir>
#include <QDirIterator>
#include <QFileSystemWatcher>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QUuid>
#include <QTimer>

const static int GROUP_FILE_FORMAT_VERSION = 1;

struct WatchLock
{
	WatchLock(QFileSystemWatcher * watcher, const QString& instDir)
		: m_watcher(watcher), m_instDir(instDir)
	{
		m_watcher->removePath(m_instDir);
	}
	~WatchLock()
	{
		m_watcher->addPath(m_instDir);
	}
	QFileSystemWatcher * m_watcher;
	QString m_instDir;
};

FolderInstanceProvider::FolderInstanceProvider(SettingsObjectPtr settings, const QString& instDir)
	: BaseInstanceProvider(settings)
{
	m_instDir = instDir;
	if (!QDir::current().exists(m_instDir))
	{
		QDir::current().mkpath(m_instDir);
	}
	m_watcher = new QFileSystemWatcher(this);
	connect(m_watcher, &QFileSystemWatcher::directoryChanged, this, &FolderInstanceProvider::instanceDirContentsChanged);
	m_watcher->addPath(m_instDir);
}

QList< InstanceId > FolderInstanceProvider::discoverInstances()
{
	QList<InstanceId> out;
	QDirIterator iter(m_instDir, QDir::Dirs | QDir::NoDot | QDir::NoDotDot | QDir::Readable, QDirIterator::FollowSymlinks);
	while (iter.hasNext())
	{
		QString subDir = iter.next();
		QFileInfo dirInfo(subDir);
		if (!QFileInfo(FS::PathCombine(subDir, "instance.cfg")).exists())
			continue;
		// if it is a symlink, ignore it if it goes to the instance folder
		if(dirInfo.isSymLink())
		{
			QFileInfo targetInfo(dirInfo.symLinkTarget());
			QFileInfo instDirInfo(m_instDir);
			if(targetInfo.canonicalPath() == instDirInfo.canonicalFilePath())
			{
				qDebug() << "Ignoring symlink" << subDir << "that leads into the instances folder";
				continue;
			}
		}
		auto id = dirInfo.fileName();
		out.append(id);
		qDebug() << "Found instance ID" << id;
	}
	return out;
}

InstancePtr FolderInstanceProvider::loadInstance(const InstanceId& id)
{
	if(!m_groupsLoaded)
	{
		loadGroupList();
	}

	auto instanceRoot = FS::PathCombine(m_instDir, id);
	auto instanceSettings = std::make_shared<INISettingsObject>(FS::PathCombine(instanceRoot, "instance.cfg"));
	InstancePtr inst;

	instanceSettings->registerSetting("InstanceType", "Legacy");

	QString inst_type = instanceSettings->get("InstanceType").toString();

	if (inst_type == "OneSix" || inst_type == "Nostalgia")
	{
		inst.reset(new MinecraftInstance(m_globalSettings, instanceSettings, instanceRoot));
	}
	else
	{
		inst.reset(new NullInstance(m_globalSettings, instanceSettings, instanceRoot));
	}
	inst->init();
	inst->setProvider(this);
	auto iter = groupMap.find(id);
	if (iter != groupMap.end())
	{
		inst->setGroupInitial((*iter));
	}
	connect(inst.get(), &BaseInstance::groupChanged, this, &FolderInstanceProvider::groupChanged);
	qDebug() << "Loaded instance " << inst->name() << " from " << inst->instanceRoot();
	return inst;
}

void FolderInstanceProvider::saveGroupList()
{
	WatchLock foo(m_watcher, m_instDir);
	QString groupFileName = m_instDir + "/instgroups.json";
	QMap<QString, QSet<QString>> reverseGroupMap;
	for (auto iter = groupMap.begin(); iter != groupMap.end(); iter++)
	{
		QString id = iter.key();
		QString group = iter.value();
		if (group.isEmpty())
			continue;

		if (!reverseGroupMap.count(group))
		{
			QSet<QString> set;
			set.insert(id);
			reverseGroupMap[group] = set;
		}
		else
		{
			QSet<QString> &set = reverseGroupMap[group];
			set.insert(id);
		}
	}
	QJsonObject toplevel;
	toplevel.insert("formatVersion", QJsonValue(QString("1")));
	QJsonObject groupsArr;
	for (auto iter = reverseGroupMap.begin(); iter != reverseGroupMap.end(); iter++)
	{
		auto list = iter.value();
		auto name = iter.key();
		QJsonObject groupObj;
		QJsonArray instanceArr;
		groupObj.insert("hidden", QJsonValue(QString("false")));
		for (auto item : list)
		{
			instanceArr.append(QJsonValue(item));
		}
		groupObj.insert("instances", instanceArr);
		groupsArr.insert(name, groupObj);
	}
	toplevel.insert("groups", groupsArr);
	QJsonDocument doc(toplevel);
	try
	{
		FS::write(groupFileName, doc.toJson());
	}
	catch(FS::FileSystemException & e)
	{
		qCritical() << "Failed to write instance group file :" << e.cause();
	}
}

void FolderInstanceProvider::loadGroupList()
{
	QSet<QString> groupSet;

	QString groupFileName = m_instDir + "/instgroups.json";

	// if there's no group file, fail
	if (!QFileInfo(groupFileName).exists())
		return;

	QByteArray jsonData;
	try
	{
		jsonData = FS::read(groupFileName);
	}
	catch (FS::FileSystemException & e)
	{
		qCritical() << "Failed to read instance group file :" << e.cause();
		return;
	}

	QJsonParseError error;
	QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData, &error);

	// if the json was bad, fail
	if (error.error != QJsonParseError::NoError)
	{
		qCritical() << QString("Failed to parse instance group file: %1 at offset %2")
							.arg(error.errorString(), QString::number(error.offset))
							.toUtf8();
		return;
	}

	// if the root of the json wasn't an object, fail
	if (!jsonDoc.isObject())
	{
		qWarning() << "Invalid group file. Root entry should be an object.";
		return;
	}

	QJsonObject rootObj = jsonDoc.object();

	// Make sure the format version matches, otherwise fail.
	if (rootObj.value("formatVersion").toVariant().toInt() != GROUP_FILE_FORMAT_VERSION)
		return;

	// Get the groups. if it's not an object, fail
	if (!rootObj.value("groups").isObject())
	{
		qWarning() << "Invalid group list JSON: 'groups' should be an object.";
		return;
	}

	groupMap.clear();

	// Iterate through all the groups.
	QJsonObject groupMapping = rootObj.value("groups").toObject();
	for (QJsonObject::iterator iter = groupMapping.begin(); iter != groupMapping.end(); iter++)
	{
		QString groupName = iter.key();

		// If not an object, complain and skip to the next one.
		if (!iter.value().isObject())
		{
			qWarning() << QString("Group '%1' in the group list should "
								   "be an object.")
							   .arg(groupName)
							   .toUtf8();
			continue;
		}

		QJsonObject groupObj = iter.value().toObject();
		if (!groupObj.value("instances").isArray())
		{
			qWarning() << QString("Group '%1' in the group list is invalid. "
								   "It should contain an array "
								   "called 'instances'.")
							   .arg(groupName)
							   .toUtf8();
			continue;
		}

		// keep a list/set of groups for choosing
		groupSet.insert(groupName);

		// Iterate through the list of instances in the group.
		QJsonArray instancesArray = groupObj.value("instances").toArray();

		for (QJsonArray::iterator iter2 = instancesArray.begin(); iter2 != instancesArray.end();
			 iter2++)
		{
			groupMap[(*iter2).toString()] = groupName;
		}
	}
	m_groupsLoaded = true;
	emit groupsChanged(groupSet);
}

void FolderInstanceProvider::groupChanged()
{
	// save the groups. save all of them.
	auto instance = (BaseInstance *) QObject::sender();
	auto id = instance->id();
	groupMap[id] = instance->group();
	emit groupsChanged({instance->group()});
	saveGroupList();
}


void FolderInstanceProvider::instanceDirContentsChanged(const QString& path)
{
	Q_UNUSED(path);
	emit instancesChanged();
}

void FolderInstanceProvider::on_InstFolderChanged(const Setting &setting, QVariant value)
{
	QString newInstDir = value.toString();
	if(newInstDir != m_instDir)
	{
		if(m_groupsLoaded)
		{
			saveGroupList();
		}
		m_instDir = newInstDir;
		m_groupsLoaded = false;
		emit instancesChanged();
	}
}

template <typename T>
static void clamp(T& current, T min, T max)
{
	if (current < min)
	{
		current = min;
	}
	else if(current > max)
	{
		current = max;
	}
}

// List of numbers from min to max. Next is exponent times bigger than previous.
class ExponentialSeries
{
public:
	ExponentialSeries(unsigned min, unsigned max, unsigned exponent = 2)
	{
		m_current = m_min = min;
		m_max = max;
		m_exponent = exponent;
	}
	void reset()
	{
		m_current = m_min;
	}
	unsigned operator()()
	{
		unsigned retval = m_current;
		m_current *= m_exponent;
		clamp(m_current, m_min, m_max);
		return retval;
	}
	unsigned m_current;
	unsigned m_min;
	unsigned m_max;
	unsigned m_exponent;
};

/*
 * WHY: the whole reason why this uses an exponential backoff retry scheme is antivirus on Windows.
 * Basically, it starts messing things up while MultiMC is extracting/creating instances
 * and causes that horrible failure that is NTFS to lock files in place because they are open.
 */
class FolderInstanceStaging : public Task
{
Q_OBJECT
	const unsigned minBackoff = 1;
	const unsigned maxBackoff = 16;
public:
	FolderInstanceStaging (
		FolderInstanceProvider * parent,
		Task * child,
		const QString & stagingPath,
		const QString& instanceName,
		const QString& groupName )
	: backoff(minBackoff, maxBackoff)
	{
		m_parent = parent;
		m_child.reset(child);
		connect(child, &Task::succeeded, this, &FolderInstanceStaging::childSucceded);
		connect(child, &Task::failed, this, &FolderInstanceStaging::childFailed);
		connect(child, &Task::status, this, &FolderInstanceStaging::setStatus);
		connect(child, &Task::progress, this, &FolderInstanceStaging::setProgress);
		m_instanceName = instanceName;
		m_groupName = groupName;
		m_stagingPath = stagingPath;
		m_backoffTimer.setSingleShot(true);
		connect(&m_backoffTimer, &QTimer::timeout, this, &FolderInstanceStaging::childSucceded);
	}

protected:
	virtual void executeTask() override
	{
		m_child->start();
	}

private slots:
	void childSucceded()
	{
		unsigned sleepTime = backoff();
		if(m_parent->commitStagedInstance(m_stagingPath, m_instanceName, m_groupName))
		{
			emitSucceeded();
			return;
		}
		// we actually failed, retry?
		if(sleepTime == maxBackoff)
		{
			emitFailed(tr("Failed to commit instance, even after multiple retries. It is being blocked by something."));
			return;
		}
		qDebug() << "Failed to commit instance" << m_instanceName << "Initiating backoff:" << sleepTime;
		m_backoffTimer.start(sleepTime * 500);
	}
	void childFailed(const QString & reason)
	{
		m_parent->destroyStagingPath(m_stagingPath);
		emitFailed(reason);
	}

private:
	ExponentialSeries backoff;
	QString m_stagingPath;
	FolderInstanceProvider * m_parent;
	unique_qobject_ptr<Task> m_child;
	QString m_instanceName;
	QString m_groupName;
	QTimer m_backoffTimer;
};

#include "InstanceImportTask.h"
Task * FolderInstanceProvider::zipImportTask(const QUrl sourceUrl, const QString& instName, const QString& instGroup, const QString& instIcon)
{
	auto stagingPath = getStagedInstancePath();
	auto task = new InstanceImportTask(m_globalSettings, sourceUrl, stagingPath, instName, instIcon, instGroup);
	return new FolderInstanceStaging(this, task, stagingPath, instName, instGroup);
}

#include "InstanceCreationTask.h"
Task * FolderInstanceProvider::creationTask(BaseVersionPtr version, const QString& instName, const QString& instGroup, const QString& instIcon)
{
	auto stagingPath = getStagedInstancePath();
	auto task = new InstanceCreationTask(m_globalSettings, stagingPath, version, instName, instIcon, instGroup);
	return new FolderInstanceStaging(this, task, stagingPath, instName, instGroup);
}

#include "InstanceCopyTask.h"
Task * FolderInstanceProvider::copyTask(const InstancePtr& oldInstance, const QString& instName, const QString& instGroup, const QString& instIcon, bool copySaves)
{
	auto stagingPath = getStagedInstancePath();
	auto task = new InstanceCopyTask(m_globalSettings, stagingPath, oldInstance, instName, instIcon, instGroup, copySaves);
	return new FolderInstanceStaging(this, task, stagingPath, instName, instGroup);
}

QString FolderInstanceProvider::getStagedInstancePath()
{
	QString key = QUuid::createUuid().toString();
	QString relPath = FS::PathCombine("_MMC_TEMP/" , key);
	QDir rootPath(m_instDir);
	auto path = FS::PathCombine(m_instDir, relPath);
	if(!rootPath.mkpath(relPath))
	{
		return QString();
	}
	return path;
}

bool FolderInstanceProvider::commitStagedInstance(const QString& path, const QString& instanceName, const QString& groupName)
{
	QDir dir;
	QString instID = FS::DirNameFromString(instanceName, m_instDir);
	{
		WatchLock lock(m_watcher, m_instDir);
		QString destination = FS::PathCombine(m_instDir, instID);
		if(!dir.rename(path, destination))
		{
			qWarning() << "Failed to move" << path << "to" << destination;
			return false;
		}
		groupMap[instID] = groupName;
		emit groupsChanged({groupName});
		emit instancesChanged();
	}
	saveGroupList();
	return true;
}

bool FolderInstanceProvider::destroyStagingPath(const QString& keyPath)
{
	return FS::deletePath(keyPath);
}

#include "FolderInstanceProvider.moc"