2013-11-04 01:53:05 +00:00
|
|
|
/* Copyright 2013 MultiMC Contributors
|
2013-05-03 20:41:37 +01:00
|
|
|
*
|
|
|
|
* 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
|
2013-10-08 00:36:11 +01:00
|
|
|
*
|
2013-05-03 20:41:37 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2013-07-28 23:59:35 +01:00
|
|
|
#include "MinecraftVersionList.h"
|
2013-11-04 01:53:05 +00:00
|
|
|
#include "MultiMC.h"
|
2013-12-13 14:58:11 +00:00
|
|
|
#include "logic/net/URLConstants.h"
|
2013-05-03 20:41:37 +01:00
|
|
|
|
|
|
|
#include <QtXml>
|
|
|
|
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonValue>
|
|
|
|
#include <QJsonParseError>
|
|
|
|
|
2013-05-04 02:14:38 +01:00
|
|
|
#include <QtAlgorithms>
|
|
|
|
|
2013-05-03 20:41:37 +01:00
|
|
|
#include <QtNetwork>
|
|
|
|
|
2013-10-08 00:36:11 +01:00
|
|
|
MinecraftVersionList::MinecraftVersionList(QObject *parent) : BaseVersionList(parent)
|
2013-05-03 20:41:37 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Task *MinecraftVersionList::getLoadTask()
|
|
|
|
{
|
|
|
|
return new MCVListLoadTask(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MinecraftVersionList::isLoaded()
|
|
|
|
{
|
|
|
|
return m_loaded;
|
|
|
|
}
|
|
|
|
|
2013-09-15 23:54:39 +01:00
|
|
|
const BaseVersionPtr MinecraftVersionList::at(int i) const
|
2013-05-03 20:41:37 +01:00
|
|
|
{
|
|
|
|
return m_vlist.at(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
int MinecraftVersionList::count() const
|
|
|
|
{
|
|
|
|
return m_vlist.count();
|
|
|
|
}
|
|
|
|
|
2013-09-15 23:54:39 +01:00
|
|
|
bool cmpVersions(BaseVersionPtr first, BaseVersionPtr second)
|
2013-05-03 20:41:37 +01:00
|
|
|
{
|
2013-10-06 00:13:40 +01:00
|
|
|
auto left = std::dynamic_pointer_cast<MinecraftVersion>(first);
|
|
|
|
auto right = std::dynamic_pointer_cast<MinecraftVersion>(second);
|
2013-09-15 23:54:39 +01:00
|
|
|
return left->timestamp > right->timestamp;
|
2013-05-04 02:14:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MinecraftVersionList::sort()
|
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
qSort(m_vlist.begin(), m_vlist.end(), cmpVersions);
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
2013-09-15 23:54:39 +01:00
|
|
|
BaseVersionPtr MinecraftVersionList::getLatestStable() const
|
2013-05-07 03:08:31 +01:00
|
|
|
{
|
|
|
|
for (int i = 0; i < m_vlist.length(); i++)
|
|
|
|
{
|
2013-10-08 00:36:11 +01:00
|
|
|
auto ver = std::dynamic_pointer_cast<MinecraftVersion>(m_vlist.at(i));
|
2013-08-11 17:58:24 +01:00
|
|
|
if (ver->is_latest && !ver->is_snapshot)
|
2013-05-07 03:08:31 +01:00
|
|
|
{
|
|
|
|
return m_vlist.at(i);
|
|
|
|
}
|
|
|
|
}
|
2013-09-15 23:54:39 +01:00
|
|
|
return BaseVersionPtr();
|
2013-05-03 20:41:37 +01:00
|
|
|
}
|
|
|
|
|
2013-10-08 00:36:11 +01:00
|
|
|
void MinecraftVersionList::updateListData(QList<BaseVersionPtr> versions)
|
2013-05-03 20:41:37 +01:00
|
|
|
{
|
|
|
|
beginResetModel();
|
2013-08-11 17:58:24 +01:00
|
|
|
m_vlist = versions;
|
2013-05-03 20:41:37 +01:00
|
|
|
m_loaded = true;
|
|
|
|
endResetModel();
|
2013-05-04 02:14:38 +01:00
|
|
|
// NOW SORT!!
|
|
|
|
sort();
|
2013-05-03 20:41:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2013-05-08 23:25:46 +01:00
|
|
|
return QDateTime::fromString(str, Qt::ISODate);
|
2013-05-04 02:14:38 +01:00
|
|
|
}
|
|
|
|
|
2013-05-03 20:41:37 +01:00
|
|
|
MCVListLoadTask::MCVListLoadTask(MinecraftVersionList *vlist)
|
|
|
|
{
|
|
|
|
m_list = vlist;
|
|
|
|
m_currentStable = NULL;
|
2013-08-04 13:46:33 +01:00
|
|
|
vlistReply = nullptr;
|
2013-08-11 17:58:24 +01:00
|
|
|
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");
|
2013-10-07 22:09:50 +01:00
|
|
|
legacyWhitelist.insert("1.4.3");
|
2013-08-11 17:58:24 +01:00
|
|
|
legacyWhitelist.insert("1.4.2");
|
2013-10-07 22:09:50 +01:00
|
|
|
legacyWhitelist.insert("1.4.1");
|
|
|
|
legacyWhitelist.insert("1.4");
|
2013-08-11 17:58:24 +01:00
|
|
|
legacyWhitelist.insert("1.3.2");
|
|
|
|
legacyWhitelist.insert("1.3.1");
|
2013-10-07 22:09:50 +01:00
|
|
|
legacyWhitelist.insert("1.3");
|
2013-08-11 17:58:24 +01:00
|
|
|
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");
|
2013-08-11 23:39:19 +01:00
|
|
|
legacyWhitelist.insert("1.0");
|
2013-05-03 20:41:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MCVListLoadTask::~MCVListLoadTask()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCVListLoadTask::executeTask()
|
|
|
|
{
|
2013-08-04 13:46:33 +01:00
|
|
|
setStatus("Loading instance version list...");
|
2013-09-07 03:00:58 +01:00
|
|
|
auto worker = MMC->qnam();
|
2013-12-13 14:58:11 +00:00
|
|
|
vlistReply = worker->get(QNetworkRequest(QUrl("http://" + URLConstants::AWS_DOWNLOAD_VERSIONS + "versions.json")));
|
2013-08-04 13:46:33 +01:00
|
|
|
connect(vlistReply, SIGNAL(finished()), this, SLOT(list_downloaded()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCVListLoadTask::list_downloaded()
|
|
|
|
{
|
2013-10-08 00:36:11 +01:00
|
|
|
if (vlistReply->error() != QNetworkReply::NoError)
|
2013-08-04 13:46:33 +01:00
|
|
|
{
|
|
|
|
vlistReply->deleteLater();
|
2013-08-08 23:26:35 +01:00
|
|
|
emitFailed("Failed to load Minecraft main version list" + vlistReply->errorString());
|
2013-08-05 02:29:50 +01:00
|
|
|
return;
|
2013-08-04 13:46:33 +01:00
|
|
|
}
|
2013-10-08 00:36:11 +01:00
|
|
|
|
2013-08-04 13:46:33 +01:00
|
|
|
QJsonParseError jsonError;
|
|
|
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(vlistReply->readAll(), &jsonError);
|
|
|
|
vlistReply->deleteLater();
|
2013-10-08 00:36:11 +01:00
|
|
|
|
2013-08-04 13:46:33 +01:00
|
|
|
if (jsonError.error != QJsonParseError::NoError)
|
2013-05-03 20:41:37 +01:00
|
|
|
{
|
2013-08-08 23:26:35 +01:00
|
|
|
emitFailed("Error parsing version list JSON:" + jsonError.errorString());
|
2013-08-05 02:29:50 +01:00
|
|
|
return;
|
2013-05-03 20:41:37 +01:00
|
|
|
}
|
|
|
|
|
2013-10-08 00:36:11 +01:00
|
|
|
if (!jsonDoc.isObject())
|
2013-08-04 13:46:33 +01:00
|
|
|
{
|
2013-08-08 23:26:35 +01:00
|
|
|
emitFailed("Error parsing version list JSON: jsonDoc is not an object");
|
2013-08-05 02:29:50 +01:00
|
|
|
return;
|
2013-08-04 13:46:33 +01:00
|
|
|
}
|
2013-10-08 00:36:11 +01:00
|
|
|
|
2013-08-04 13:46:33 +01:00
|
|
|
QJsonObject root = jsonDoc.object();
|
2013-10-08 00:36:11 +01:00
|
|
|
|
2013-08-04 13:46:33 +01:00
|
|
|
// Get the ID of the latest release and the latest snapshot.
|
2013-10-08 00:36:11 +01:00
|
|
|
if (!root.value("latest").isObject())
|
2013-08-04 13:46:33 +01:00
|
|
|
{
|
2013-08-08 23:26:35 +01:00
|
|
|
emitFailed("Error parsing version list JSON: version list is missing 'latest' object");
|
2013-08-05 02:29:50 +01:00
|
|
|
return;
|
2013-08-04 13:46:33 +01:00
|
|
|
}
|
2013-10-08 00:36:11 +01:00
|
|
|
|
2013-08-04 13:46:33 +01:00
|
|
|
QJsonObject latest = root.value("latest").toObject();
|
2013-10-08 00:36:11 +01:00
|
|
|
|
2013-08-04 13:46:33 +01:00
|
|
|
QString latestReleaseID = latest.value("release").toString("");
|
|
|
|
QString latestSnapshotID = latest.value("snapshot").toString("");
|
2013-10-08 00:36:11 +01:00
|
|
|
if (latestReleaseID.isEmpty())
|
2013-08-04 13:46:33 +01:00
|
|
|
{
|
2013-08-08 23:26:35 +01:00
|
|
|
emitFailed("Error parsing version list JSON: latest release field is missing");
|
2013-08-05 02:29:50 +01:00
|
|
|
return;
|
2013-08-04 13:46:33 +01:00
|
|
|
}
|
2013-10-08 00:36:11 +01:00
|
|
|
if (latestSnapshotID.isEmpty())
|
2013-08-04 13:46:33 +01:00
|
|
|
{
|
2013-08-08 23:26:35 +01:00
|
|
|
emitFailed("Error parsing version list JSON: latest snapshot field is missing");
|
2013-08-05 02:29:50 +01:00
|
|
|
return;
|
2013-08-04 13:46:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now, get the array of versions.
|
2013-10-08 00:36:11 +01:00
|
|
|
if (!root.value("versions").isArray())
|
2013-05-03 20:41:37 +01:00
|
|
|
{
|
2013-10-08 00:36:11 +01:00
|
|
|
emitFailed(
|
|
|
|
"Error parsing version list JSON: version list object is missing 'versions' array");
|
2013-08-05 02:29:50 +01:00
|
|
|
return;
|
2013-08-04 13:46:33 +01:00
|
|
|
}
|
|
|
|
QJsonArray versions = root.value("versions").toArray();
|
2013-10-08 00:36:11 +01:00
|
|
|
|
|
|
|
QList<BaseVersionPtr> tempList;
|
2013-08-04 13:46:33 +01:00
|
|
|
for (int i = 0; i < versions.count(); i++)
|
2013-05-03 20:41:37 +01:00
|
|
|
{
|
2013-08-11 17:58:24 +01:00
|
|
|
bool is_snapshot = false;
|
|
|
|
bool is_latest = false;
|
2013-10-08 00:36:11 +01:00
|
|
|
|
2013-08-04 13:46:33 +01:00
|
|
|
// Load the version info.
|
2013-10-08 00:36:11 +01:00
|
|
|
if (!versions[i].isObject())
|
2013-08-04 13:46:33 +01:00
|
|
|
{
|
2013-10-08 00:36:11 +01:00
|
|
|
// FIXME: log this somewhere
|
2013-08-04 13:46:33 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
QJsonObject version = versions[i].toObject();
|
|
|
|
QString versionID = version.value("id").toString("");
|
|
|
|
QString versionTimeStr = version.value("releaseTime").toString("");
|
|
|
|
QString versionTypeStr = version.value("type").toString("");
|
2013-10-08 00:36:11 +01:00
|
|
|
if (versionID.isEmpty() || versionTimeStr.isEmpty() || versionTypeStr.isEmpty())
|
2013-08-04 13:46:33 +01:00
|
|
|
{
|
2013-10-08 00:36:11 +01:00
|
|
|
// FIXME: log this somewhere
|
2013-08-04 13:46:33 +01:00
|
|
|
continue;
|
|
|
|
}
|
2013-10-08 00:36:11 +01:00
|
|
|
|
2013-08-04 13:46:33 +01:00
|
|
|
// Parse the timestamp.
|
|
|
|
QDateTime versionTime = timeFromS3Time(versionTimeStr);
|
2013-10-08 00:36:11 +01:00
|
|
|
if (!versionTime.isValid())
|
2013-08-04 13:46:33 +01:00
|
|
|
{
|
2013-10-08 00:36:11 +01:00
|
|
|
// FIXME: log this somewhere
|
2013-08-04 13:46:33 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Parse the type.
|
|
|
|
MinecraftVersion::VersionType versionType;
|
2013-08-11 17:58:24 +01:00
|
|
|
// OneSix or Legacy. use filter to determine type
|
2013-08-04 13:46:33 +01:00
|
|
|
if (versionTypeStr == "release")
|
2013-05-03 20:41:37 +01:00
|
|
|
{
|
2013-10-08 00:36:11 +01:00
|
|
|
versionType = legacyWhitelist.contains(versionID) ? MinecraftVersion::Legacy
|
|
|
|
: MinecraftVersion::OneSix;
|
2013-08-11 17:58:24 +01:00
|
|
|
is_latest = (versionID == latestReleaseID);
|
|
|
|
is_snapshot = false;
|
2013-08-04 13:46:33 +01:00
|
|
|
}
|
2013-10-08 00:36:11 +01:00
|
|
|
else if (versionTypeStr == "snapshot") // It's a snapshot... yay
|
2013-08-04 13:46:33 +01:00
|
|
|
{
|
2013-10-08 00:36:11 +01:00
|
|
|
versionType = legacyWhitelist.contains(versionID) ? MinecraftVersion::Legacy
|
|
|
|
: MinecraftVersion::OneSix;
|
2013-08-11 17:58:24 +01:00
|
|
|
is_latest = (versionID == latestSnapshotID);
|
|
|
|
is_snapshot = true;
|
2013-05-03 20:41:37 +01:00
|
|
|
}
|
2013-10-08 00:36:11 +01:00
|
|
|
else if (versionTypeStr == "old_alpha")
|
2013-08-07 00:38:18 +01:00
|
|
|
{
|
|
|
|
versionType = MinecraftVersion::Nostalgia;
|
2013-08-11 17:58:24 +01:00
|
|
|
is_latest = false;
|
|
|
|
is_snapshot = false;
|
|
|
|
}
|
2013-10-08 00:36:11 +01:00
|
|
|
else if (versionTypeStr == "old_beta")
|
2013-08-11 17:58:24 +01:00
|
|
|
{
|
|
|
|
versionType = MinecraftVersion::Legacy;
|
|
|
|
is_latest = false;
|
|
|
|
is_snapshot = false;
|
2013-08-07 00:38:18 +01:00
|
|
|
}
|
2013-05-03 20:41:37 +01:00
|
|
|
else
|
|
|
|
{
|
2013-10-08 00:36:11 +01:00
|
|
|
// FIXME: log this somewhere
|
2013-08-04 13:46:33 +01:00
|
|
|
continue;
|
2013-05-03 20:41:37 +01:00
|
|
|
}
|
2013-08-04 13:46:33 +01:00
|
|
|
// Get the download URL.
|
2013-12-13 14:58:11 +00:00
|
|
|
QString dlUrl = "http://" + URLConstants::AWS_DOWNLOAD_VERSIONS + versionID + "/";
|
2013-10-08 00:36:11 +01:00
|
|
|
|
2013-08-04 13:46:33 +01:00
|
|
|
// Now, we construct the version object and add it to the list.
|
2013-10-06 00:13:40 +01:00
|
|
|
std::shared_ptr<MinecraftVersion> mcVersion(new MinecraftVersion());
|
2013-09-15 23:54:39 +01:00
|
|
|
mcVersion->m_name = mcVersion->m_descriptor = versionID;
|
2013-08-11 17:58:24 +01:00
|
|
|
mcVersion->timestamp = versionTime.toMSecsSinceEpoch();
|
|
|
|
mcVersion->download_url = dlUrl;
|
|
|
|
mcVersion->is_latest = is_latest;
|
|
|
|
mcVersion->is_snapshot = is_snapshot;
|
|
|
|
mcVersion->type = versionType;
|
2013-08-04 13:46:33 +01:00
|
|
|
tempList.append(mcVersion);
|
2013-05-03 20:41:37 +01:00
|
|
|
}
|
2013-08-04 13:46:33 +01:00
|
|
|
m_list->updateListData(tempList);
|
2013-10-08 00:36:11 +01:00
|
|
|
|
2013-08-08 23:26:35 +01:00
|
|
|
emitSucceeded();
|
2013-08-05 02:29:50 +01:00
|
|
|
return;
|
2013-05-03 20:41:37 +01:00
|
|
|
}
|