summaryrefslogtreecommitdiffstats
path: root/api/logic/minecraft/ftb/FTBInstanceProvider.cpp
blob: fe23a84eb9a7cca19c4919c6750676b6b621118d (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
#include "FTBInstanceProvider.h"

#include <QDir>
#include <QDebug>
#include <QXmlStreamReader>
#include <QRegularExpression>

#include <settings/INISettingsObject.h>
#include <FileSystem.h>

#include "Env.h"

#include "LegacyFTBInstance.h"
#include "OneSixFTBInstance.h"

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

FTBInstanceProvider::FTBInstanceProvider(SettingsObjectPtr settings)
	: BaseInstanceProvider(settings)
{
	// nil
}

QList<InstanceId> FTBInstanceProvider::discoverInstances()
{
	// nothing to load when we don't have
	if (m_globalSettings->get("TrackFTBInstances").toBool() != true)
	{
		return {};
	}
	m_records.clear();
	discoverFTBEntries();
	return m_records.keys();
}

InstancePtr FTBInstanceProvider::loadInstance(const InstanceId& id)
{
	// process the records we acquired.
	auto iter = m_records.find(id);
	if(iter == m_records.end())
	{
		qWarning() << "Cannot load instance" << id << "without a record";
		return nullptr;
	}
	auto & record = m_records[id];
	qDebug() << "Loading FTB instance from " << record.instanceDir;
	QString iconKey = record.iconKey;
	auto icons = ENV.icons();
	if(icons)
	{
		icons->addIcon(iconKey, iconKey, FS::PathCombine(record.templateDir, record.logo), IconType::Transient);
	}
	auto settingsFilePath = FS::PathCombine(record.instanceDir, "instance.cfg");
	qDebug() << "ICON get!";

	if (QFileInfo(settingsFilePath).exists())
	{
		auto instPtr = loadInstance(record);
		if (!instPtr)
		{
			qWarning() << "Couldn't load instance config:" << settingsFilePath;
			if(!QFile::remove(settingsFilePath))
			{
				qWarning() << "Couldn't remove broken instance config!";
				return nullptr;
			}
			// failed to load, but removed the poisonous file
		}
		else
		{
			return InstancePtr(instPtr);
		}
	}
	auto instPtr = createInstance(record);
	if (!instPtr)
	{
		qWarning() << "Couldn't create FTB instance!";
		return nullptr;
	}
	return InstancePtr(instPtr);
}

void FTBInstanceProvider::discoverFTBEntries()
{
	QDir dir = QDir(m_globalSettings->get("FTBLauncherLocal").toString());
	QDir dataDir = QDir(m_globalSettings->get("FTBRoot").toString());
	if (!dataDir.exists())
	{
		qDebug() << "The FTB directory specified does not exist. Please check your settings";
		return;
	}
	else if (!dir.exists())
	{
		qDebug() << "The FTB launcher data directory specified does not exist. Please check "
					"your settings";
		return;
	}
	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();
					QString logo = record.logo;
					record.iconKey = logo.remove(QRegularExpression("\\..*"));
					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();
					auto id = "FTB/" + record.dirName;
					m_records[id] = record;
				}
				break;
			}
			case QXmlStreamReader::EndElement:
				break;
			case QXmlStreamReader::Characters:
				break;
			default:
				break;
			}
		}
		f.close();
	}
}

InstancePtr FTBInstanceProvider::loadInstance(const FTBRecord & record) const
{
	InstancePtr inst;

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

	qDebug() << "Loading existing " << record.name;

	QString inst_type = m_settings->get("InstanceType").toString();
	if (inst_type == "LegacyFTB")
	{
		inst.reset(new LegacyFTBInstance(m_globalSettings, m_settings, record.instanceDir));
	}
	else if (inst_type == "OneSixFTB")
	{
		inst.reset(new OneSixFTBInstance(m_globalSettings, m_settings, record.instanceDir));
	}
	else
	{
		return nullptr;
	}
	qDebug() << "Construction " << record.instanceDir;

	SettingsObject::Lock lock(inst->settings());
	inst->init();
	qDebug() << "Init " << record.instanceDir;
	inst->setGroupInitial("FTB");
	/**
	 * FIXME: this does not respect the user's preferences. BUT, it would work nicely with the planned pack support
	 *        -> instead of changing the user values, change pack values (defaults you can look at and revert to)
	 */
	/*
	inst->setName(record.name);
	inst->setIconKey(record.iconKey);
	inst->setNotes(record.description);
	*/
	if (inst->intendedVersionId() != record.mcVersion)
	{
		inst->setIntendedVersionId(record.mcVersion);
	}
	qDebug() << "Loaded instance " << inst->name() << " from " << inst->instanceRoot();
	return inst;
}

InstancePtr FTBInstanceProvider::createInstance(const FTBRecord & record) const
{
	QDir rootDir(record.instanceDir);

	InstancePtr inst;

	qDebug() << "Converting " << record.name << " as new.";

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

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

	// all legacy versions are built in. therefore we can do this even if we don't have ALL the versions Mojang has on their servers.
	m_settings->set("InstanceType", "OneSixFTB");
	inst.reset(new OneSixFTBInstance(m_globalSettings, m_settings, record.instanceDir));

	// initialize
	{
		SettingsObject::Lock lock(inst->settings());
		inst->setIntendedVersionId(record.mcVersion);
		inst->init();
		inst->setGroupInitial("FTB");
		inst->setName(record.name);
		inst->setIconKey(record.iconKey);
		inst->setNotes(record.description);
	}
	return inst;
}