summaryrefslogtreecommitdiffstats
path: root/logic/minecraft/MinecraftVersionList.cpp
blob: 1aa220e8b9407978c0de2c99da56aa91364ffd61 (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
/* Copyright 2013 MultiMC Contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "MinecraftVersionList.h"
#include "MultiMC.h"
#include "logic/net/URLConstants.h"
#include "logic/MMCJson.h"
#include "ParseUtils.h"

#include <QtXml>

#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#include <QJsonParseError>

#include <QtAlgorithms>

#include <QtNetwork>

MinecraftVersionList::MinecraftVersionList(QObject *parent) : BaseVersionList(parent)
{
	loadBuiltinList();
}

Task *MinecraftVersionList::getLoadTask()
{
	return new MCVListLoadTask(this);
}

bool MinecraftVersionList::isLoaded()
{
	return m_loaded;
}

const BaseVersionPtr MinecraftVersionList::at(int i) const
{
	return m_vlist.at(i);
}

int MinecraftVersionList::count() const
{
	return m_vlist.count();
}

static bool cmpVersions(BaseVersionPtr first, BaseVersionPtr second)
{
	auto left = std::dynamic_pointer_cast<MinecraftVersion>(first);
	auto right = std::dynamic_pointer_cast<MinecraftVersion>(second);
	return left->m_releaseTime > right->m_releaseTime;
}

void MinecraftVersionList::sortInternal()
{
	qSort(m_vlist.begin(), m_vlist.end(), cmpVersions);
}

void MinecraftVersionList::loadBuiltinList()
{
	// grab the version list data from internal resources.
	QResource versionList(":/versions/minecraft.json");
	QFile filez(versionList.absoluteFilePath());
	filez.open(QIODevice::ReadOnly);
	auto data = filez.readAll();

	// parse the data as json
	QJsonParseError jsonError;
	QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
	QJsonObject root = jsonDoc.object();

	// parse all the versions
	for (const auto version : MMCJson::ensureArray(root.value("versions")))
	{
		QJsonObject versionObj = version.toObject();
		QString versionID = versionObj.value("id").toString("");
		QString versionTypeStr = versionObj.value("type").toString("");
		if (versionID.isEmpty() || versionTypeStr.isEmpty())
		{
			QLOG_ERROR() << "Parsed version is missing ID or type";
			continue;
		}

		// Now, we construct the version object and add it to the list.
		std::shared_ptr<MinecraftVersion> mcVersion(new MinecraftVersion());
		mcVersion->m_name = mcVersion->m_descriptor = versionID;

		// Parse the timestamp.
		try
		{
			parse_timestamp(versionObj.value("releaseTime").toString(""),
							mcVersion->m_releaseTimeString, mcVersion->m_releaseTime);
		}
		catch (MMCError &e)
		{
			QLOG_ERROR() << "Error while parsing version" << versionID << ":" << e.cause();
			continue;
		}

		// Get the download URL.
		mcVersion->download_url =
			"http://" + URLConstants::AWS_DOWNLOAD_VERSIONS + versionID + "/";

		mcVersion->m_versionSource = MinecraftVersion::Builtin;
		mcVersion->m_appletClass = versionObj.value("appletClass").toString("");
		mcVersion->m_mainClass = versionObj.value("mainClass").toString("");
		mcVersion->m_processArguments = versionObj.value("processArguments").toString("legacy");
		if (versionObj.contains("+traits"))
		{
			for (auto traitVal : MMCJson::ensureArray(versionObj.value("+traits")))
			{
				mcVersion->m_traits.insert(MMCJson::ensureString(traitVal));
			}
		}
		m_vlist.append(mcVersion);
	}
}

void MinecraftVersionList::sort()
{
	beginResetModel();
	sortInternal();
	endResetModel();
}

BaseVersionPtr MinecraftVersionList::getLatestStable() const
{
	for (int i = 0; i < m_vlist.length(); i++)
	{
		auto ver = std::dynamic_pointer_cast<MinecraftVersion>(m_vlist.at(i));
		if (ver->is_latest && !ver->is_snapshot)
		{
			return m_vlist.at(i);
		}
	}
	return BaseVersionPtr();
}

void MinecraftVersionList::updateListData(QList<BaseVersionPtr> versions)
{
	beginResetModel();
	for (auto version : versions)
	{
		auto descr = version->descriptor();
		for (auto builtin_v : m_vlist)
		{
			if (descr == builtin_v->descriptor())
			{
				goto SKIP_THIS_ONE;
			}
		}
		m_vlist.append(version);
	SKIP_THIS_ONE:
	{
	}
	}
	m_loaded = true;
	sortInternal();
	endResetModel();
}

inline QDomElement getDomElementByTagName(QDomElement parent, QString tagname)
{
	QDomNodeList elementList = parent.elementsByTagName(tagname);
	if (elementList.count())
		return elementList.at(0).toElement();
	else
		return QDomElement();
}

MCVListLoadTask::MCVListLoadTask(MinecraftVersionList *vlist)
{
	m_list = vlist;
	m_currentStable = NULL;
	vlistReply = nullptr;
}

MCVListLoadTask::~MCVListLoadTask()
{
}

void MCVListLoadTask::executeTask()
{
	setStatus(tr("Loading instance version list..."));
	auto worker = MMC->qnam();
	vlistReply = worker->get(QNetworkRequest(
		QUrl("http://" + URLConstants::AWS_DOWNLOAD_VERSIONS + "versions.json")));
	connect(vlistReply, SIGNAL(finished()), this, SLOT(list_downloaded()));
}

void MCVListLoadTask::list_downloaded()
{
	if (vlistReply->error() != QNetworkReply::NoError)
	{
		vlistReply->deleteLater();
		emitFailed("Failed to load Minecraft main version list" + vlistReply->errorString());
		return;
	}

	auto foo = vlistReply->readAll();
	QJsonParseError jsonError;
	QLOG_INFO() << foo;
	QJsonDocument jsonDoc = QJsonDocument::fromJson(foo, &jsonError);
	vlistReply->deleteLater();

	if (jsonError.error != QJsonParseError::NoError)
	{
		emitFailed("Error parsing version list JSON:" + jsonError.errorString());
		return;
	}

	if (!jsonDoc.isObject())
	{
		emitFailed("Error parsing version list JSON: jsonDoc is not an object");
		return;
	}

	QJsonObject root = jsonDoc.object();

	QString latestReleaseID = "INVALID";
	QString latestSnapshotID = "INVALID";
	try
	{
		QJsonObject latest = MMCJson::ensureObject(root.value("latest"));
		latestReleaseID = MMCJson::ensureString(latest.value("release"));
		latestSnapshotID = MMCJson::ensureString(latest.value("snapshot"));
	}
	catch (MMCError &err)
	{
		QLOG_ERROR()
			<< tr("Error parsing version list JSON: couldn't determine latest versions");
	}

	// Now, get the array of versions.
	if (!root.value("versions").isArray())
	{
		emitFailed(
			"Error parsing version list JSON: version list object is missing 'versions' array");
		return;
	}
	QJsonArray versions = root.value("versions").toArray();

	QList<BaseVersionPtr> tempList;
	for (auto version : versions)
	{
		bool is_snapshot = false;
		bool is_latest = false;

		// Load the version info.
		if (!version.isObject())
		{
			QLOG_ERROR() << "Error while parsing version list : invalid JSON structure";
			continue;
		}
		QJsonObject versionObj = version.toObject();
		QString versionID = versionObj.value("id").toString("");
		if (versionID.isEmpty())
		{
			QLOG_ERROR() << "Error while parsing version : version ID is missing";
			continue;
		}
		// Get the download URL.
		QString dlUrl = "http://" + URLConstants::AWS_DOWNLOAD_VERSIONS + versionID + "/";

		// Now, we construct the version object and add it to the list.
		std::shared_ptr<MinecraftVersion> mcVersion(new MinecraftVersion());
		mcVersion->m_name = mcVersion->m_descriptor = versionID;

		try
		{
			// Parse the timestamps.
			parse_timestamp(versionObj.value("releaseTime").toString(""),
							mcVersion->m_releaseTimeString, mcVersion->m_releaseTime);

			parse_timestamp(versionObj.value("time").toString(""),
							mcVersion->m_updateTimeString, mcVersion->m_updateTime);
		}
		catch (MMCError &e)
		{
			QLOG_ERROR() << "Error while parsing version" << versionID << ":" << e.cause();
			continue;
		}

		mcVersion->m_versionSource = MinecraftVersion::Builtin;
		mcVersion->download_url = dlUrl;
		{
			QString versionTypeStr = versionObj.value("type").toString("");
			if (versionTypeStr.isEmpty())
			{
				// FIXME: log this somewhere
				continue;
			}
			// OneSix or Legacy. use filter to determine type
			if (versionTypeStr == "release")
			{
				is_latest = (versionID == latestReleaseID);
				is_snapshot = false;
			}
			else if (versionTypeStr == "snapshot") // It's a snapshot... yay
			{
				is_latest = (versionID == latestSnapshotID);
				is_snapshot = true;
			}
			else if (versionTypeStr == "old_alpha")
			{
				is_latest = false;
				is_snapshot = false;
			}
			else if (versionTypeStr == "old_beta")
			{
				is_latest = false;
				is_snapshot = false;
			}
			else
			{
				// FIXME: log this somewhere
				continue;
			}
			mcVersion->m_type = versionTypeStr;
			mcVersion->is_latest = is_latest;
			mcVersion->is_snapshot = is_snapshot;
		}
		tempList.append(mcVersion);
	}
	m_list->updateListData(tempList);

	emitSucceeded();
	return;
}