summaryrefslogtreecommitdiffstats
path: root/logic/lists/MinecraftVersionList.cpp
blob: 86ba07921e2ae95b8eb8f4a04549a4128dc90a5f (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
/* Copyright 2013 Andrew Okin
 *
 * 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 <QDebug>

#include <QtXml>

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

#include <QtAlgorithms>

#include <QtNetwork>

#define MCVLIST_URLBASE "http://s3.amazonaws.com/Minecraft.Download/versions/"
#define ASSETS_URLBASE "http://assets.minecraft.net/"
#define MCN_URLBASE "http://sonicrules.org/mcnweb.py"

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

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();
}

bool cmpVersions(BaseVersionPtr first, BaseVersionPtr second)
{
	auto left = first.dynamicCast<MinecraftVersion>();
	auto right = second.dynamicCast<MinecraftVersion>();
	return left->timestamp > right->timestamp;
}

void MinecraftVersionList::sort()
{
	beginResetModel();
	qSort(m_vlist.begin(), m_vlist.end(), cmpVersions);
	endResetModel();
}

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

void MinecraftVersionList::updateListData(QList<BaseVersionPtr > versions)
{
	beginResetModel();
	m_vlist = versions;
	m_loaded = true;
	endResetModel();
	// NOW SORT!!
	sort();
}

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)
{
	return QDateTime::fromString(str, Qt::ISODate);
}


MCVListLoadTask::MCVListLoadTask(MinecraftVersionList *vlist)
{
	m_list = vlist;
	m_currentStable = NULL;
	vlistReply = nullptr;
	legacyWhitelist.insert("1.5.2");
	legacyWhitelist.insert("1.5.1");
	legacyWhitelist.insert("1.5");
	legacyWhitelist.insert("1.4.7");
	legacyWhitelist.insert("1.4.6");
	legacyWhitelist.insert("1.4.5");
	legacyWhitelist.insert("1.4.4");
	legacyWhitelist.insert("1.4.2");
	legacyWhitelist.insert("1.3.2");
	legacyWhitelist.insert("1.3.1");
	legacyWhitelist.insert("1.2.5");
	legacyWhitelist.insert("1.2.4");
	legacyWhitelist.insert("1.2.3");
	legacyWhitelist.insert("1.2.2");
	legacyWhitelist.insert("1.2.1");
	legacyWhitelist.insert("1.1");
	legacyWhitelist.insert("1.0.1");
	legacyWhitelist.insert("1.0");
}

MCVListLoadTask::~MCVListLoadTask()
{
}

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


void MCVListLoadTask::list_downloaded()
{
	if(vlistReply->error() != QNetworkReply::QNetworkReply::NoError)
	{
		vlistReply->deleteLater();
		emitFailed("Failed to load Minecraft main version list" + vlistReply->errorString());
		return;
	}
	
	QJsonParseError jsonError;
	QJsonDocument jsonDoc = QJsonDocument::fromJson(vlistReply->readAll(), &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();
	
	// Get the ID of the latest release and the latest snapshot.
	if(!root.value("latest").isObject())
	{
		emitFailed("Error parsing version list JSON: version list is missing 'latest' object");
		return;
	}
	
	QJsonObject latest = root.value("latest").toObject();
	
	QString latestReleaseID = latest.value("release").toString("");
	QString latestSnapshotID = latest.value("snapshot").toString("");
	if(latestReleaseID.isEmpty())
	{
		emitFailed("Error parsing version list JSON: latest release field is missing");
		return;
	}
	if(latestSnapshotID.isEmpty())
	{
		emitFailed("Error parsing version list JSON: latest snapshot field is missing");
		return;
	}

	// 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 (int i = 0; i < versions.count(); i++)
	{
		bool is_snapshot = false;
		bool is_latest = false;
		
		// Load the version info.
		if(!versions[i].isObject())
		{
			//FIXME: log this somewhere
			continue;
		}
		QJsonObject version = versions[i].toObject();
		QString versionID = version.value("id").toString("");
		QString versionTimeStr = version.value("releaseTime").toString("");
		QString versionTypeStr = version.value("type").toString("");
		if(versionID.isEmpty() || versionTimeStr.isEmpty() || versionTypeStr.isEmpty())
		{
			//FIXME: log this somewhere
			continue;
		}
		
		// Parse the timestamp.
		QDateTime versionTime = timeFromS3Time(versionTimeStr);
		if(!versionTime.isValid())
		{
			//FIXME: log this somewhere
			continue;
		}
		// Parse the type.
		MinecraftVersion::VersionType versionType;
		// OneSix or Legacy. use filter to determine type
		if (versionTypeStr == "release")
		{
			versionType = legacyWhitelist.contains(versionID)?MinecraftVersion::Legacy:MinecraftVersion::OneSix;
			is_latest = (versionID == latestReleaseID);
			is_snapshot = false;
		}
		else if(versionTypeStr == "snapshot") // It's a snapshot... yay
		{
			versionType = legacyWhitelist.contains(versionID)?MinecraftVersion::Legacy:MinecraftVersion::OneSix;
			is_latest = (versionID == latestSnapshotID);
			is_snapshot = true;
		}
		else if(versionTypeStr == "old_alpha")
		{
			versionType = MinecraftVersion::Nostalgia;
			is_latest = false;
			is_snapshot = false;
		}
		else if(versionTypeStr == "old_beta")
		{
			versionType = MinecraftVersion::Legacy;
			is_latest = false;
			is_snapshot = false;
		}
		else
		{
			//FIXME: log this somewhere
			continue;
		}
		// Get the download URL.
		QString dlUrl = QString(MCVLIST_URLBASE) + versionID + "/";
		
		// Now, we construct the version object and add it to the list.
		QSharedPointer<MinecraftVersion> mcVersion(new MinecraftVersion());
		mcVersion->m_name = mcVersion->m_descriptor = versionID;
		mcVersion->timestamp = versionTime.toMSecsSinceEpoch();
		mcVersion->download_url = dlUrl;
		mcVersion->is_latest = is_latest;
		mcVersion->is_snapshot = is_snapshot;
		mcVersion->type = versionType;
		tempList.append(mcVersion);
	}
	m_list->updateListData(tempList);
	
	emitSucceeded();
	return;
}