2013-05-03 19:41:37 +00:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2013-07-28 22:59:35 +00:00
|
|
|
#include "MinecraftVersionList.h"
|
2013-08-17 11:40:51 +00:00
|
|
|
#include <logic/net/NetWorker.h>
|
2013-05-03 19:41:37 +00:00
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
#include <QtXml>
|
|
|
|
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonValue>
|
|
|
|
#include <QJsonParseError>
|
|
|
|
|
2013-05-04 01:14:38 +00:00
|
|
|
#include <QtAlgorithms>
|
|
|
|
|
2013-05-03 19:41:37 +00:00
|
|
|
#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 mcVList;
|
|
|
|
|
|
|
|
MinecraftVersionList::MinecraftVersionList(QObject *parent) :
|
|
|
|
InstVersionList(parent)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Task *MinecraftVersionList::getLoadTask()
|
|
|
|
{
|
|
|
|
return new MCVListLoadTask(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MinecraftVersionList::isLoaded()
|
|
|
|
{
|
|
|
|
return m_loaded;
|
|
|
|
}
|
|
|
|
|
2013-08-11 16:58:24 +00:00
|
|
|
const InstVersionPtr MinecraftVersionList::at(int i) const
|
2013-05-03 19:41:37 +00:00
|
|
|
{
|
|
|
|
return m_vlist.at(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
int MinecraftVersionList::count() const
|
|
|
|
{
|
|
|
|
return m_vlist.count();
|
|
|
|
}
|
|
|
|
|
2013-08-11 16:58:24 +00:00
|
|
|
bool cmpVersions(InstVersionPtr first, InstVersionPtr second)
|
2013-05-03 19:41:37 +00:00
|
|
|
{
|
2013-08-11 16:58:24 +00:00
|
|
|
const InstVersion & left = *first;
|
|
|
|
const InstVersion & right = *second;
|
|
|
|
return left > right;
|
2013-05-04 01:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MinecraftVersionList::sort()
|
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
qSort(m_vlist.begin(), m_vlist.end(), cmpVersions);
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
2013-08-11 16:58:24 +00:00
|
|
|
InstVersionPtr MinecraftVersionList::getLatestStable() const
|
2013-05-07 02:08:31 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < m_vlist.length(); i++)
|
|
|
|
{
|
2013-08-11 16:58:24 +00:00
|
|
|
auto ver = m_vlist.at(i).dynamicCast<MinecraftVersion>();
|
|
|
|
if (ver->is_latest && !ver->is_snapshot)
|
2013-05-07 02:08:31 +00:00
|
|
|
{
|
|
|
|
return m_vlist.at(i);
|
|
|
|
}
|
|
|
|
}
|
2013-08-11 16:58:24 +00:00
|
|
|
return InstVersionPtr();
|
2013-05-07 02:08:31 +00:00
|
|
|
}
|
|
|
|
|
2013-05-03 19:41:37 +00:00
|
|
|
MinecraftVersionList &MinecraftVersionList::getMainList()
|
|
|
|
{
|
|
|
|
return mcVList;
|
|
|
|
}
|
|
|
|
|
2013-08-11 16:58:24 +00:00
|
|
|
void MinecraftVersionList::updateListData(QList<InstVersionPtr > versions)
|
2013-05-03 19:41:37 +00:00
|
|
|
{
|
|
|
|
beginResetModel();
|
2013-08-11 16:58:24 +00:00
|
|
|
m_vlist = versions;
|
2013-05-03 19:41:37 +00:00
|
|
|
m_loaded = true;
|
|
|
|
endResetModel();
|
2013-05-04 01:14:38 +00:00
|
|
|
// NOW SORT!!
|
|
|
|
sort();
|
2013-05-03 19:41:37 +00: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 22:25:46 +00:00
|
|
|
return QDateTime::fromString(str, Qt::ISODate);
|
2013-05-04 01:14:38 +00:00
|
|
|
}
|
|
|
|
|
2013-05-03 19:41:37 +00:00
|
|
|
|
|
|
|
MCVListLoadTask::MCVListLoadTask(MinecraftVersionList *vlist)
|
|
|
|
{
|
|
|
|
m_list = vlist;
|
|
|
|
m_currentStable = NULL;
|
2013-08-04 12:46:33 +00:00
|
|
|
vlistReply = nullptr;
|
2013-08-11 16:58:24 +00: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");
|
|
|
|
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");
|
2013-08-11 22:39:19 +00:00
|
|
|
legacyWhitelist.insert("1.0");
|
2013-05-03 19:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MCVListLoadTask::~MCVListLoadTask()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCVListLoadTask::executeTask()
|
|
|
|
{
|
2013-08-04 12:46:33 +00:00
|
|
|
setStatus("Loading instance version list...");
|
2013-08-06 23:38:18 +00:00
|
|
|
auto & worker = NetWorker::spawn();
|
|
|
|
vlistReply = worker.get(QNetworkRequest(QUrl(QString(MCVLIST_URLBASE) + "versions.json")));
|
2013-08-04 12:46:33 +00:00
|
|
|
connect(vlistReply, SIGNAL(finished()), this, SLOT(list_downloaded()));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MCVListLoadTask::list_downloaded()
|
|
|
|
{
|
|
|
|
if(vlistReply->error() != QNetworkReply::QNetworkReply::NoError)
|
|
|
|
{
|
|
|
|
vlistReply->deleteLater();
|
2013-08-08 22:26:35 +00:00
|
|
|
emitFailed("Failed to load Minecraft main version list" + vlistReply->errorString());
|
2013-08-05 01:29:50 +00:00
|
|
|
return;
|
2013-08-04 12:46:33 +00:00
|
|
|
}
|
2013-05-03 19:41:37 +00:00
|
|
|
|
2013-08-04 12:46:33 +00:00
|
|
|
QJsonParseError jsonError;
|
|
|
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(vlistReply->readAll(), &jsonError);
|
|
|
|
vlistReply->deleteLater();
|
2013-05-03 19:41:37 +00:00
|
|
|
|
2013-08-04 12:46:33 +00:00
|
|
|
if (jsonError.error != QJsonParseError::NoError)
|
2013-05-03 19:41:37 +00:00
|
|
|
{
|
2013-08-08 22:26:35 +00:00
|
|
|
emitFailed("Error parsing version list JSON:" + jsonError.errorString());
|
2013-08-05 01:29:50 +00:00
|
|
|
return;
|
2013-05-03 19:41:37 +00:00
|
|
|
}
|
|
|
|
|
2013-08-04 12:46:33 +00:00
|
|
|
if(!jsonDoc.isObject())
|
|
|
|
{
|
2013-08-08 22:26:35 +00:00
|
|
|
emitFailed("Error parsing version list JSON: jsonDoc is not an object");
|
2013-08-05 01:29:50 +00:00
|
|
|
return;
|
2013-08-04 12:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject root = jsonDoc.object();
|
|
|
|
|
|
|
|
// Get the ID of the latest release and the latest snapshot.
|
|
|
|
if(!root.value("latest").isObject())
|
|
|
|
{
|
2013-08-08 22:26:35 +00:00
|
|
|
emitFailed("Error parsing version list JSON: version list is missing 'latest' object");
|
2013-08-05 01:29:50 +00:00
|
|
|
return;
|
2013-08-04 12:46:33 +00:00
|
|
|
}
|
2013-05-03 19:41:37 +00:00
|
|
|
|
2013-08-04 12:46:33 +00:00
|
|
|
QJsonObject latest = root.value("latest").toObject();
|
|
|
|
|
|
|
|
QString latestReleaseID = latest.value("release").toString("");
|
|
|
|
QString latestSnapshotID = latest.value("snapshot").toString("");
|
|
|
|
if(latestReleaseID.isEmpty())
|
|
|
|
{
|
2013-08-08 22:26:35 +00:00
|
|
|
emitFailed("Error parsing version list JSON: latest release field is missing");
|
2013-08-05 01:29:50 +00:00
|
|
|
return;
|
2013-08-04 12:46:33 +00:00
|
|
|
}
|
|
|
|
if(latestSnapshotID.isEmpty())
|
|
|
|
{
|
2013-08-08 22:26:35 +00:00
|
|
|
emitFailed("Error parsing version list JSON: latest snapshot field is missing");
|
2013-08-05 01:29:50 +00:00
|
|
|
return;
|
2013-08-04 12:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now, get the array of versions.
|
|
|
|
if(!root.value("versions").isArray())
|
2013-05-03 19:41:37 +00:00
|
|
|
{
|
2013-08-08 22:26:35 +00:00
|
|
|
emitFailed("Error parsing version list JSON: version list object is missing 'versions' array");
|
2013-08-05 01:29:50 +00:00
|
|
|
return;
|
2013-08-04 12:46:33 +00:00
|
|
|
}
|
|
|
|
QJsonArray versions = root.value("versions").toArray();
|
|
|
|
|
2013-08-11 16:58:24 +00:00
|
|
|
QList<InstVersionPtr > tempList;
|
2013-08-04 12:46:33 +00:00
|
|
|
for (int i = 0; i < versions.count(); i++)
|
2013-05-03 19:41:37 +00:00
|
|
|
{
|
2013-08-11 16:58:24 +00:00
|
|
|
bool is_snapshot = false;
|
|
|
|
bool is_latest = false;
|
|
|
|
|
2013-08-04 12:46:33 +00:00
|
|
|
// 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;
|
2013-08-11 16:58:24 +00:00
|
|
|
// OneSix or Legacy. use filter to determine type
|
2013-08-04 12:46:33 +00:00
|
|
|
if (versionTypeStr == "release")
|
2013-05-03 19:41:37 +00:00
|
|
|
{
|
2013-08-11 16:58:24 +00:00
|
|
|
versionType = legacyWhitelist.contains(versionID)?MinecraftVersion::Legacy:MinecraftVersion::OneSix;
|
|
|
|
is_latest = (versionID == latestReleaseID);
|
|
|
|
is_snapshot = false;
|
2013-08-04 12:46:33 +00:00
|
|
|
}
|
2013-08-11 16:58:24 +00:00
|
|
|
else if(versionTypeStr == "snapshot") // It's a snapshot... yay
|
2013-08-04 12:46:33 +00:00
|
|
|
{
|
2013-08-11 16:58:24 +00:00
|
|
|
versionType = legacyWhitelist.contains(versionID)?MinecraftVersion::Legacy:MinecraftVersion::OneSix;
|
|
|
|
is_latest = (versionID == latestSnapshotID);
|
|
|
|
is_snapshot = true;
|
2013-05-03 19:41:37 +00:00
|
|
|
}
|
2013-08-11 16:58:24 +00:00
|
|
|
else if(versionTypeStr == "old_alpha")
|
2013-08-06 23:38:18 +00:00
|
|
|
{
|
|
|
|
versionType = MinecraftVersion::Nostalgia;
|
2013-08-11 16:58:24 +00:00
|
|
|
is_latest = false;
|
|
|
|
is_snapshot = false;
|
|
|
|
}
|
|
|
|
else if(versionTypeStr == "old_beta")
|
|
|
|
{
|
|
|
|
versionType = MinecraftVersion::Legacy;
|
|
|
|
is_latest = false;
|
|
|
|
is_snapshot = false;
|
2013-08-06 23:38:18 +00:00
|
|
|
}
|
2013-05-03 19:41:37 +00:00
|
|
|
else
|
|
|
|
{
|
2013-08-04 12:46:33 +00:00
|
|
|
//FIXME: log this somewhere
|
|
|
|
continue;
|
2013-05-03 19:41:37 +00:00
|
|
|
}
|
2013-08-04 12:46:33 +00:00
|
|
|
// Get the download URL.
|
|
|
|
QString dlUrl = QString(MCVLIST_URLBASE) + versionID + "/";
|
2013-05-03 19:41:37 +00:00
|
|
|
|
2013-08-04 12:46:33 +00:00
|
|
|
// Now, we construct the version object and add it to the list.
|
2013-08-11 16:58:24 +00:00
|
|
|
QSharedPointer<MinecraftVersion> mcVersion(new MinecraftVersion());
|
|
|
|
mcVersion->name = mcVersion->descriptor = versionID;
|
|
|
|
mcVersion->timestamp = versionTime.toMSecsSinceEpoch();
|
|
|
|
mcVersion->download_url = dlUrl;
|
|
|
|
mcVersion->is_latest = is_latest;
|
|
|
|
mcVersion->is_snapshot = is_snapshot;
|
|
|
|
mcVersion->type = versionType;
|
2013-08-04 12:46:33 +00:00
|
|
|
tempList.append(mcVersion);
|
2013-05-03 19:41:37 +00:00
|
|
|
}
|
2013-08-04 12:46:33 +00:00
|
|
|
m_list->updateListData(tempList);
|
2013-05-03 19:41:37 +00:00
|
|
|
|
2013-08-08 22:26:35 +00:00
|
|
|
emitSucceeded();
|
2013-08-05 01:29:50 +00:00
|
|
|
return;
|
2013-05-03 19:41:37 +00:00
|
|
|
}
|
|
|
|
|
2013-08-04 12:46:33 +00:00
|
|
|
// FIXME: we should have a local cache of the version list and a local cache of version data
|
|
|
|
bool MCVListLoadTask::loadFromVList()
|
2013-05-03 19:41:37 +00:00
|
|
|
{
|
|
|
|
}
|