summaryrefslogtreecommitdiffstats
path: root/logic/ftb/FTBPlugin.cpp
blob: d524e3fd28afd433c2c3c6c5d182672f89ad99c9 (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
#include "FTBPlugin.h"
#include "FTBVersion.h"
#include "LegacyFTBInstance.h"
#include "OneSixFTBInstance.h"
#include <logic/BaseInstance.h>
#include <logic/icons/IconList.h>
#include <logic/InstanceList.h>
#include <logic/minecraft/MinecraftVersionList.h>
#include <logic/settings/INISettingsObject.h>
#include "MultiMC.h"
#include <pathutils.h>
#include "QDebug"
#include <QXmlStreamReader>
#include <QRegularExpression>

struct FTBRecord
{
	QString dirName;
	QString name;
	QString logo;
	QString mcVersion;
	QString description;
	QString instanceDir;
	QString templateDir;
	bool operator==(const FTBRecord other) const
	{
		return instanceDir == other.instanceDir;
	}
};

inline uint qHash(FTBRecord record)
{
	return qHash(record.instanceDir);
}

QSet<FTBRecord> discoverFTBInstances()
{
	QSet<FTBRecord> records;
	QDir dir = QDir(MMC->settings()->get("FTBLauncherDataRoot").toString());
	QDir dataDir = QDir(MMC->settings()->get("FTBRoot").toString());
	if (!dataDir.exists())
	{
		qDebug() << "The FTB directory specified does not exist. Please check your settings";
		return records;
	}
	else if (!dir.exists())
	{
		qDebug() << "The FTB launcher data directory specified does not exist. Please check "
					"your settings";
		return records;
	}
	dir.cd("ModPacks");
	auto allFiles = dir.entryList(QDir::Readable | QDir::Files, QDir::Name);
	for (auto filename : allFiles)
	{
		if (!filename.endsWith(".xml"))
			continue;
		auto fpath = dir.absoluteFilePath(filename);
		QFile f(fpath);
		qDebug() << "Discovering FTB instances -- " << fpath;
		if (!f.open(QFile::ReadOnly))
			continue;

		// read the FTB packs XML.
		QXmlStreamReader reader(&f);
		while (!reader.atEnd())
		{
			switch (reader.readNext())
			{
			case QXmlStreamReader::StartElement:
			{
				if (reader.name() == "modpack")
				{
					QXmlStreamAttributes attrs = reader.attributes();
					FTBRecord record;
					record.dirName = attrs.value("dir").toString();
					record.instanceDir = dataDir.absoluteFilePath(record.dirName);
					record.templateDir = dir.absoluteFilePath(record.dirName);
					QDir test(record.instanceDir);
					qDebug() << dataDir.absolutePath() << record.instanceDir << record.dirName;
					if (!test.exists())
						continue;
					record.name = attrs.value("name").toString();
					record.logo = attrs.value("logo").toString();
					auto customVersions = attrs.value("customMCVersions");
					if (!customVersions.isNull())
					{
						QMap<QString, QString> versionMatcher;
						QString customVersionsStr = customVersions.toString();
						QStringList list = customVersionsStr.split(';');
						for (auto item : list)
						{
							auto segment = item.split('^');
							if (segment.size() != 2)
							{
								qCritical() << "FTB: Segment of size < 2 in "
											<< customVersionsStr;
								continue;
							}
							versionMatcher[segment[0]] = segment[1];
						}
						auto actualVersion = attrs.value("version").toString();
						if (versionMatcher.contains(actualVersion))
						{
							record.mcVersion = versionMatcher[actualVersion];
						}
						else
						{
							record.mcVersion = attrs.value("mcVersion").toString();
						}
					}
					else
					{
						record.mcVersion = attrs.value("mcVersion").toString();
					}
					record.description = attrs.value("description").toString();
					records.insert(record);
				}
				break;
			}
			case QXmlStreamReader::EndElement:
				break;
			case QXmlStreamReader::Characters:
				break;
			default:
				break;
			}
		}
		f.close();
	}
	return records;
}

InstancePtr loadInstance(const QString &instDir)
{
	auto m_settings = std::make_shared<INISettingsObject>(PathCombine(instDir, "instance.cfg"));

	InstancePtr inst;

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

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

	if (inst_type == "LegacyFTB")
	{
		inst.reset(new LegacyFTBInstance(MMC->settings(), m_settings, instDir));
	}
	else if (inst_type == "OneSixFTB")
	{
		inst.reset(new OneSixFTBInstance(MMC->settings(), m_settings, instDir));
	}
	inst->init();
	return inst;
}

InstancePtr createInstance(MinecraftVersionPtr version, const QString &instDir)
{
	QDir rootDir(instDir);

	InstancePtr inst;

	if (!version)
	{
		qCritical() << "Can't create instance for non-existing MC version";
		return nullptr;
	}

	qDebug() << instDir.toUtf8();
	if (!rootDir.exists() && !rootDir.mkpath("."))
	{
		qCritical() << "Can't create instance folder" << instDir;
		return nullptr;
	}

	auto m_settings = std::make_shared<INISettingsObject>(PathCombine(instDir, "instance.cfg"));
	m_settings->registerSetting("InstanceType", "Legacy");

	if (version->usesLegacyLauncher())
	{
		m_settings->set("InstanceType", "LegacyFTB");
		inst.reset(new LegacyFTBInstance(MMC->settings(),m_settings, instDir));
		inst->setIntendedVersionId(version->descriptor());
	}
	else
	{
		m_settings->set("InstanceType", "OneSixFTB");
		inst.reset(new OneSixFTBInstance(MMC->settings(),m_settings, instDir));
		inst->setIntendedVersionId(version->descriptor());
		inst->init();
	}
	return inst;
}

void FTBPlugin::loadInstances(QMap<QString, QString> &groupMap, QList<InstancePtr> &tempList)
{
	// nothing to load when we don't have
	if (MMC->settings()->get("TrackFTBInstances").toBool() != true)
	{
		return;
	}

	auto records = discoverFTBInstances();
	if (!records.size())
	{
		qDebug() << "No FTB instances to load.";
		return;
	}
	qDebug() << "Loading FTB instances! -- got " << records.size();
	// process the records we acquired.
	for (auto record : records)
	{
		qDebug() << "Loading FTB instance from " << record.instanceDir;
		QString iconKey = record.logo;
		iconKey.remove(QRegularExpression("\\..*"));
		MMC->icons()->addIcon(iconKey, iconKey, PathCombine(record.templateDir, record.logo),
							  MMCIcon::Transient);

		if (!QFileInfo(PathCombine(record.instanceDir, "instance.cfg")).exists())
		{
			qDebug() << "Converting " << record.name << " as new.";
			auto mcVersion = std::dynamic_pointer_cast<MinecraftVersion>(MMC->minecraftlist()->findVersion(record.mcVersion));
			if (!mcVersion)
			{
				qCritical() << "Can't load instance " << record.instanceDir
							<< " because minecraft version " << record.mcVersion
							<< " can't be resolved.";
				continue;
			}

			auto instPtr = createInstance(mcVersion, record.instanceDir);
			if (!instPtr)
			{
				continue;
			}

			instPtr->setGroupInitial("FTB");
			instPtr->setName(record.name);
			instPtr->setIconKey(iconKey);
			instPtr->setIntendedVersionId(record.mcVersion);
			instPtr->setNotes(record.description);
			if (!InstanceList::continueProcessInstance(instPtr, InstanceList::NoCreateError, record.instanceDir, groupMap))
				continue;
			tempList.append(InstancePtr(instPtr));
		}
		else
		{
			qDebug() << "Loading existing " << record.name;
			auto instPtr = loadInstance(record.instanceDir);
			if (!instPtr)
			{
				continue;
			}
			instPtr->setGroupInitial("FTB");
			instPtr->setName(record.name);
			instPtr->setIconKey(iconKey);
			if (instPtr->intendedVersionId() != record.mcVersion)
			{
				instPtr->setIntendedVersionId(record.mcVersion);
			}
			instPtr->setNotes(record.description);
			if (!InstanceList::continueProcessInstance(instPtr, InstanceList::NoCreateError, record.instanceDir, groupMap))
				continue;
			tempList.append(InstancePtr(instPtr));
		}
	}
}

#ifdef Q_OS_WIN32
#include <windows.h>
static const int APPDATA_BUFFER_SIZE = 1024;
#endif

void FTBPlugin::initialize()
{
	auto m_settings = MMC->settings();
	// FTB
	m_settings->registerSetting("TrackFTBInstances", false);
	QString ftbDataDefault;
#ifdef Q_OS_LINUX
	QString ftbDefault = ftbDataDefault = QDir::home().absoluteFilePath(".ftblauncher");
#elif defined(Q_OS_WIN32)
	wchar_t buf[APPDATA_BUFFER_SIZE];
	wchar_t newBuf[APPDATA_BUFFER_SIZE];
	QString ftbDefault, newFtbDefault, oldFtbDefault;
	if (!GetEnvironmentVariableW(L"LOCALAPPDATA", newBuf, APPDATA_BUFFER_SIZE))
	{
		qCritical() << "Your LOCALAPPDATA folder is missing! If you are on windows, this means "
					   "your system is broken.";
	}
	else
	{
		newFtbDefault = QDir(QString::fromWCharArray(newBuf)).absoluteFilePath("ftblauncher");
	}
	if (!GetEnvironmentVariableW(L"APPDATA", buf, APPDATA_BUFFER_SIZE))
	{
		qCritical() << "Your APPDATA folder is missing! If you are on windows, this means your "
					   "system is broken.";
	}
	else
	{
		oldFtbDefault = QDir(QString::fromWCharArray(buf)).absoluteFilePath("ftblauncher");
	}
	if (QFile::exists(QDir(newFtbDefault).absoluteFilePath("ftblaunch.cfg")))
	{
		qDebug() << "Old FTB setup";
		ftbDefault = ftbDataDefault = oldFtbDefault;
	}
	else
	{
		qDebug() << "New FTB setup";
		ftbDefault = oldFtbDefault;
		ftbDataDefault = newFtbDefault;
	}
#elif defined(Q_OS_MAC)
	QString ftbDefault = ftbDataDefault =
		PathCombine(QDir::homePath(), "Library/Application Support/ftblauncher");
#endif
	m_settings->registerSetting("FTBLauncherDataRoot", ftbDataDefault);
	m_settings->registerSetting("FTBLauncherRoot", ftbDefault);
	qDebug() << "FTB Launcher paths:" << m_settings->get("FTBLauncherDataRoot").toString()
			 << "and" << m_settings->get("FTBLauncherRoot").toString();

	m_settings->registerSetting("FTBRoot");
	if (m_settings->get("FTBRoot").isNull())
	{
		QString ftbRoot;
		QFile f(QDir(m_settings->get("FTBLauncherRoot").toString())
					.absoluteFilePath("ftblaunch.cfg"));
		qDebug() << "Attempting to read" << f.fileName();
		if (f.open(QFile::ReadOnly))
		{
			const QString data = QString::fromLatin1(f.readAll());
			QRegularExpression exp("installPath=(.*)");
			ftbRoot = QDir::cleanPath(exp.match(data).captured(1));
#ifdef Q_OS_WIN32
			if (!ftbRoot.isEmpty())
			{
				if (ftbRoot.at(0).isLetter() && ftbRoot.size() > 1 && ftbRoot.at(1) == '/')
				{
					ftbRoot.remove(1, 1);
				}
			}
#endif
			if (ftbRoot.isEmpty())
			{
				qDebug() << "Failed to get FTB root path";
			}
			else
			{
				qDebug() << "FTB is installed at" << ftbRoot;
				m_settings->set("FTBRoot", ftbRoot);
			}
		}
		else
		{
			qWarning() << "Couldn't open" << f.fileName() << ":" << f.errorString();
			qWarning() << "This is perfectly normal if you don't have FTB installed";
		}
	}
}