summaryrefslogtreecommitdiffstats
path: root/plugins/stdinstance/stdinstversionlist.cpp
blob: 214edbe72fa13e124fcb09846ca790b25433c5f8 (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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/* 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 "stdinstversionlist.h"

#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>

#include <QtXml/QDomDocument>

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

#include <QDateTime>
#include <QMap>
#include <QMapIterator>
#include <QStringList>
#include <QUrl>

#include <QRegExp>

#include <QDebug>

#include <instversion.h>

#include "stdinstversion.h"

#define MCDL_URLBASE "http://assets.minecraft.net/"
#define ASSETS_URLBASE "http://s3.amazonaws.com/MinecraftDownload/"
#define MCN_URLBASE "http://sonicrules.org/mcnweb.py"

// When this is defined, prints the entire version list to qDebug() after loading.
//#define PRINT_VERSIONS


StdInstVersionList vList;

StdInstVersionList::StdInstVersionList(QObject *parent) :
	InstVersionList(parent)
{
	loaded = false;
}

Task *StdInstVersionList::getLoadTask()
{
	return new StdInstVListLoadTask(this);
}

bool StdInstVersionList::isLoaded()
{
	return loaded;
}

const InstVersion *StdInstVersionList::at(int i) const
{
	return m_vlist.at(i);
}

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

void StdInstVersionList::printToStdOut()
{
	qDebug() << "---------------- Version List ----------------";
	
	for (int i = 0; i < m_vlist.count(); i++)
	{
		StdInstVersion *version = qobject_cast<StdInstVersion *>(m_vlist.at(i));
		
		if (!version)
			continue;
		
		qDebug() << "Version " << version->name();
		qDebug() << "\tDownload: " << version->downloadURL();
		qDebug() << "\tTimestamp: " << version->timestamp();
		qDebug() << "\tType: " << version->typeName();
		qDebug() << "----------------------------------------------";
	}
}


StdInstVListLoadTask::StdInstVListLoadTask(StdInstVersionList *vlist) :
	Task(vlist)
{
	m_list = vlist;
	processedMCDLReply = false;
	processedAssetsReply = false;
	processedMCNReply = false;
	
	currentStable = NULL;
	foundCurrentInAssets = false;
}

void StdInstVListLoadTask::executeTask()
{
	setSubStatus();
	
	// Initialize the network access manager.
	QNetworkAccessManager netMgr;
	
	mcdlReply = netMgr.get(QNetworkRequest(QUrl(ASSETS_URLBASE)));
	assetsReply = netMgr.get(QNetworkRequest(QUrl(MCDL_URLBASE)));
	mcnReply = netMgr.get(QNetworkRequest(QUrl(QString(MCN_URLBASE) + "?pversion=1&list=True")));
	
	connect(mcdlReply, SIGNAL(finished()),
			SLOT(processMCDLReply()));
	connect(mcnReply, SIGNAL(finished()),
			SLOT(processMCNReply()));
	
	exec();
	finalize();
}

void StdInstVListLoadTask::finalize()
{
	// First, we need to do some cleanup. We loaded MCNostalgia versions into 
	// mcnList and all the others into tempList. MCNostalgia provides some versions
	// that are on assets.minecraft.net and we want to ignore those, so we remove
	// and delete them from mcnList.
	
	// To start, we get a list of the descriptors in tmpList.
	QStringList tlistDescriptors;
	for (int i = 0; i < tempList.count(); i++)
		tlistDescriptors.append(tempList.at(i)->descriptor());
	
	// Now, we go through our MCNostalgia version list and remove anything with
	// a descriptor that matches one we already have in tempList.
	// We'll need a list of items we're going to remove.
	for (int i = 0; i < mcnList.count(); i++)
		if (tlistDescriptors.contains(mcnList.at(i)->descriptor()))
			delete mcnList.takeAt(i--); // We need to decrement here because we're removing an item.
	
	// Now that the duplicates are gone, we need to merge the two lists. This is
	// simple enough.
	tempList.append(mcnList);
	
	// We're done with mcnList now, but the items have been moved over to 
	// tempList, so we don't need to delete them.
	
	// Now we swap the list we loaded into the actual version list.
	// This applies our changes to the version list immediately and still gives us 
	// access to the old list so that we can delete the objects in it and free their memory.
	// By doing this, we cause the version list to update as quickly as possible.
	m_list->beginResetModel();
	m_list->m_vlist.swap(tempList);
	m_list->endResetModel();
	
	m_list->loaded = true;
	
	// We called swap, so all the data that was in the version list previously is now in 
	// tempList (and vice-versa). Now we just free the memory.
	while (!tempList.isEmpty())
		delete tempList.takeFirst();
	
#ifdef PRINT_VERSIONS
	m_list->printToStdOut();
#endif
}

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

inline QDateTime timeFromS3Time(QString str)
{
    const QString fmt("yyyy-MM-dd'T'HH:mm:ss'.000Z'");
    return QDateTime::fromString(str, fmt);
}

void StdInstVListLoadTask::processMCDLReply()
{
	switch (mcdlReply->error())
	{
	case QNetworkReply::NoError:
	{
		// Get the XML string.
		QString xmlString = mcdlReply->readAll();
		
		QString xmlErrorMsg;
		
		QDomDocument doc;
		if (!doc.setContent(xmlString, false, &xmlErrorMsg))
		{
			// TODO: Display error message to the user.
			qDebug(QString("Failed to process Minecraft download site. XML error: %s").
				   arg(xmlErrorMsg).toUtf8());
		}
		
		QDomNodeList contents = doc.elementsByTagName("Contents");
		
		for (int i = 0; i < contents.length(); i++)
		{
			QDomElement element = contents.at(i).toElement();
			
			if (element.isNull())
				continue;
			
			QDomElement keyElement = getDomElementByTagName(element, "Key");
			QDomElement lastmodElement = getDomElementByTagName(element, "LastModified");
			QDomElement etagElement = getDomElementByTagName(element, "ETag");
			
			if (keyElement.isNull() || lastmodElement.isNull() || etagElement.isNull())
				continue;
			
			QString key = keyElement.text();
			QString lastModStr = lastmodElement.text();
			QString etagStr = etagElement.text();
			QString dlUrl = "http://s3.amazonaws.com/MinecraftDownload/";
			
			if (key != "minecraft.jar")
				continue;
			
			QDateTime versionTimestamp = timeFromS3Time(lastModStr);
			if (!versionTimestamp.isValid())
			{
				qDebug(QString("Failed to parse timestamp for current stable version %1").
					   arg(lastModStr).toUtf8());
				versionTimestamp = QDateTime::currentDateTime();
			}
			
			currentStable = new StdInstVersion("LatestStable", "Current",
											   versionTimestamp.toMSecsSinceEpoch(),
											   "http://s3.amazonaws.com/MinecraftDownload/",
											   true, etagStr, m_list);
			
			setSubStatus("Loaded latest version info.");
		}
		break;
	}
		
	default:
		// TODO: Network error handling.
		break;
	}
	
	if (!currentStable)
		qDebug("Failed to get current stable version.");
	
	
	processedMCDLReply = true;
	updateStuff();
	
	// If the assets request isn't finished yet, connect the slot to allow it 
	// to process when the request is done. Otherwise, simply call the 
	// processAssetsReply slot directly.
	if (!assetsReply->isFinished())
		connect(assetsReply, SIGNAL(finished()),
				SLOT(processAssetsReply()));
	else if (!processedAssetsReply)
		processAssetsReply();
}

void StdInstVListLoadTask::processAssetsReply()
{
	switch (assetsReply->error())
	{
	case QNetworkReply::NoError:
	{
		// Get the XML string.
		QString xmlString = assetsReply->readAll();
		
		QString xmlErrorMsg;
		
		QDomDocument doc;
		if (!doc.setContent(xmlString, false, &xmlErrorMsg))
		{
			// TODO: Display error message to the user.
			qDebug(QString("Failed to process assets.minecraft.net. XML error: %s").
				   arg(xmlErrorMsg).toUtf8());
		}
		
		QDomNodeList contents = doc.elementsByTagName("Contents");
		
		QRegExp mcRegex("/minecraft.jar$");
		QRegExp snapshotRegex("[0-9][0-9]w[0-9][0-9][a-z]|pre|rc");
		
		for (int i = 0; i < contents.length(); i++)
		{
			QDomElement element = contents.at(i).toElement();
			
			if (element.isNull())
				continue;
			
			QDomElement keyElement = getDomElementByTagName(element, "Key");
			QDomElement lastmodElement = getDomElementByTagName(element, "LastModified");
			QDomElement etagElement = getDomElementByTagName(element, "ETag");
			
			if (keyElement.isNull() || lastmodElement.isNull() || etagElement.isNull())
				continue;
			
			QString key = keyElement.text();
			QString lastModStr = lastmodElement.text();
			QString etagStr = etagElement.text();
			
			if (!key.contains(mcRegex))
				continue;
			
			QString versionDirName = key.left(key.length() - 14);
			QString dlUrl = QString("http://assets.minecraft.net/%1/").arg(versionDirName);
			
			QString versionName = versionDirName.replace("_", ".");
			
			QDateTime versionTimestamp = timeFromS3Time(lastModStr);
			if (!versionTimestamp.isValid())
			{
				qDebug(QString("Failed to parse timestamp for version %1 %2").
					   arg(versionName, lastModStr).toUtf8());
				versionTimestamp = QDateTime::currentDateTime();
			}
			
			if (currentStable)
			{
				if (etagStr == currentStable->etag())
				{
					StdInstVersion *version = new StdInstVersion(
								versionName, versionName,
								versionTimestamp.toMSecsSinceEpoch(),
								currentStable->downloadURL(), true, etagStr, m_list);
					version->setVersionType(StdInstVersion::CurrentStable);
					tempList.push_back(version);
					foundCurrentInAssets = true;
				}
				else
				{
					bool older = versionTimestamp.toMSecsSinceEpoch() < currentStable->timestamp();
					bool newer = versionTimestamp.toMSecsSinceEpoch() > currentStable->timestamp();
					bool isSnapshot = versionName.contains(snapshotRegex);
					
					StdInstVersion *version = new StdInstVersion(
								versionName, versionName, 
								versionTimestamp.toMSecsSinceEpoch(),
								dlUrl, false, etagStr, m_list);
					
					if (newer)
					{
						version->setVersionType(StdInstVersion::Snapshot);
					}
					else if (older && isSnapshot)
					{
						version->setVersionType(StdInstVersion::OldSnapshot);
					}
					else if (older)
					{
						version->setVersionType(StdInstVersion::Stable);
					}
					else
					{
						// Shouldn't happen, but just in case...
						version->setVersionType(StdInstVersion::CurrentStable);
					}
					
					tempList.push_back(version);
				}
			}
			else // If there isn't a current stable version.
			{
				bool isSnapshot = versionName.contains(snapshotRegex);
				
				StdInstVersion *version = new StdInstVersion(
							versionName, versionName, 
							versionTimestamp.toMSecsSinceEpoch(),
							dlUrl, false, etagStr, m_list);
				version->setVersionType(isSnapshot? StdInstVersion::Snapshot :
													StdInstVersion::Stable);
				tempList.push_back(version);
			}
		}
		
		setSubStatus("Loaded assets.minecraft.net");
		break;
	}
		
	default:
		// TODO: Network error handling.
		break;
	}
	
	processedAssetsReply = true;
	updateStuff();
}


QString mcnToAssetsVersion(QString mcnVersion);

void StdInstVListLoadTask::processMCNReply()
{
	switch (assetsReply->error())
	{
	case QNetworkReply::NoError:
	{
		QJsonParseError pError;
		QJsonDocument jsonDoc = QJsonDocument::fromJson(mcnReply->readAll(), &pError);
		
		if (pError.error != QJsonParseError::NoError)
		{
			// Handle errors.
			qDebug() << "Failed to parse MCNostalgia response. JSON parser error: " << 
						pError.errorString();
			break;
		}
		
		
		// Load data.
		QRegExp indevRegex("in(f)?dev");
		QJsonArray vlistArray = jsonDoc.object().value("order").toArray();
		
		for (int i = 0; i < vlistArray.size(); i++)
		{
			QString rawVersion = vlistArray.at(i).toString();
			if (rawVersion.isEmpty() || rawVersion.contains(indevRegex))
				continue;
			
			QString niceVersion = mcnToAssetsVersion(rawVersion);
			if (niceVersion.isEmpty())
				continue;
			
			StdInstVersion *version = StdInstVersion::mcnVersion(rawVersion, niceVersion);
			mcnList.prepend(version);
		}
		
		setSubStatus("Loaded MCNostalgia");
		break;
	}
		
	default:
		// TODO: Network error handling.
		break;
	}
	
	
	processedMCNReply = true;
	updateStuff();
}

void StdInstVListLoadTask::setSubStatus(const QString &msg)
{
	if (msg.isEmpty())
		setStatus("Loading instance version list...");
	else
		setStatus("Loading instance version list: " + msg);
}

void StdInstVListLoadTask::updateStuff()
{
	const int totalReqs = 3;
	int reqsComplete = 0;
	
	if (processedMCDLReply)
		reqsComplete++;
	if (processedAssetsReply)
		reqsComplete++;
	if (processedMCNReply)
		reqsComplete++;
	
	calcProgress(reqsComplete, totalReqs);
	
	if (reqsComplete >= totalReqs)
	{
		quit();
	}
}

class MCNostalgiaVNameMap
{
public:
	QMap <QString, QString> mapping;
	MCNostalgiaVNameMap()
	{
		// An empty string means that it should be ignored
		mapping["1.4.6_pre"] = "";
		mapping["1.4.5_pre"] = "";
		mapping["1.4.3_pre"] = "1.4.3";
		mapping["1.4.2_pre"] = "";
		mapping["1.4.1_pre"] = "1.4.1";
		mapping["1.4_pre"] = "1.4";
		mapping["1.3.2_pre"] = "";
		mapping["1.3.1_pre"] = "";
		mapping["1.3_pre"] = "";
		mapping["1.2_pre"] = "1.2";
	}
} mcnVNMap;

QString mcnToAssetsVersion(QString mcnVersion)
{
	QMap<QString, QString>::iterator iter = mcnVNMap.mapping.find(mcnVersion);
	if (iter != mcnVNMap.mapping.end())
	{
		return iter.value();
	}
	return mcnVersion;
}