pollymc/backend/lists/MinecraftVersionList.cpp

313 lines
7.9 KiB
C++
Raw Normal View History

/* 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 <QDebug>
#include <QtXml>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#include <QJsonParseError>
#include <QtAlgorithms>
#include <QtNetwork>
#include "netutils.h"
#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;
}
const InstVersion *MinecraftVersionList::at(int i) const
{
return m_vlist.at(i);
}
int MinecraftVersionList::count() const
{
return m_vlist.count();
}
void MinecraftVersionList::printToStdOut() const
{
qDebug() << "---------------- Version List ----------------";
for (int i = 0; i < m_vlist.count(); i++)
{
MinecraftVersion *version = qobject_cast<MinecraftVersion *>(m_vlist.at(i));
if (!version)
continue;
qDebug() << "Version " << version->name();
qDebug() << "\tDownload: " << version->downloadURL();
qDebug() << "\tTimestamp: " << version->timestamp();
qDebug() << "\tType: " << version->typeName();
qDebug() << "----------------------------------------------";
}
}
bool cmpVersions(const InstVersion *first, const InstVersion *second)
{
return !first->isLessThan(*second);
}
void MinecraftVersionList::sort()
{
beginResetModel();
qSort(m_vlist.begin(), m_vlist.end(), cmpVersions);
endResetModel();
}
InstVersion *MinecraftVersionList::getLatestStable() const
{
for (int i = 0; i < m_vlist.length(); i++)
{
if (((MinecraftVersion *)m_vlist.at(i))->versionType() == MinecraftVersion::CurrentStable)
{
return m_vlist.at(i);
}
}
return NULL;
}
MinecraftVersionList &MinecraftVersionList::getMainList()
{
return mcVList;
}
void MinecraftVersionList::updateListData(QList<InstVersion *> versions)
{
// First, we populate a temporary list with the copies of the versions.
QList<InstVersion *> tempList;
for (int i = 0; i < versions.length(); i++)
{
InstVersion *version = versions[i]->copyVersion(this);
Q_ASSERT(version != NULL);
tempList.append(version);
}
// Now we swap the temporary list into the actual version list.
// This applies our changes to the version list immediately and still gives us
// access to the old version 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.
beginResetModel();
m_vlist.swap(tempList);
m_loaded = true;
endResetModel();
// 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();
// 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;
2013-08-04 18:16:33 +05:30
netMgr = nullptr;
vlistReply = nullptr;
}
MCVListLoadTask::~MCVListLoadTask()
{
2013-08-04 18:16:33 +05:30
if(netMgr)
netMgr->deleteLater();
}
void MCVListLoadTask::executeTask()
{
2013-08-04 18:16:33 +05:30
// NOTE: this executes in the QThread
setStatus("Loading instance version list...");
netMgr = new QNetworkAccessManager();
vlistReply = netMgr->get(QNetworkRequest(QUrl(QString(MCVLIST_URLBASE) + "versions.json")));
connect(vlistReply, SIGNAL(finished()), this, SLOT(list_downloaded()));
exec();
}
void MCVListLoadTask::list_downloaded()
{
// NOTE: this executes in the main thread
if(vlistReply->error() != QNetworkReply::QNetworkReply::NoError)
{
qDebug() << "Failed to load Minecraft main version list" << vlistReply->errorString();
vlistReply->deleteLater();
exit(0);
}
2013-08-04 18:16:33 +05:30
QJsonParseError jsonError;
QJsonDocument jsonDoc = QJsonDocument::fromJson(vlistReply->readAll(), &jsonError);
vlistReply->deleteLater();
2013-08-04 18:16:33 +05:30
if (jsonError.error != QJsonParseError::NoError)
{
2013-08-04 18:16:33 +05:30
qDebug() << "Error parsing version list JSON:" << jsonError.errorString();
exit(0);
}
2013-08-04 18:16:33 +05:30
if(!jsonDoc.isObject())
{
qDebug() << "Error parsing version list JSON: " << "jsonDoc is not an object";
exit(0);
}
QJsonObject root = jsonDoc.object();
// Get the ID of the latest release and the latest snapshot.
if(!root.value("latest").isObject())
{
qDebug() << "Error parsing version list JSON: " << "version list is missing 'latest' object";
exit(0);
}
2013-08-04 18:16:33 +05:30
QJsonObject latest = root.value("latest").toObject();
QString latestReleaseID = latest.value("release").toString("");
QString latestSnapshotID = latest.value("snapshot").toString("");
if(latestReleaseID.isEmpty())
{
qDebug() << "Error parsing version list JSON: " << "latest release field is missing";
exit(0);
}
if(latestSnapshotID.isEmpty())
{
qDebug() << "Error parsing version list JSON: " << "latest snapshot field is missing";
exit(0);
}
// Now, get the array of versions.
if(!root.value("versions").isArray())
{
2013-08-04 18:16:33 +05:30
qDebug() << "Error parsing version list JSON: " << "version list object is missing 'versions' array";
exit(0);
}
QJsonArray versions = root.value("versions").toArray();
for (int i = 0; i < versions.count(); i++)
{
2013-08-04 18:16:33 +05:30
// 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;
}
2013-08-04 18:16:33 +05:30
// Parse the type.
MinecraftVersion::VersionType versionType;
if (versionTypeStr == "release")
{
2013-08-04 18:16:33 +05:30
// Check if this version is the current stable version.
if (versionID == latestReleaseID)
versionType = MinecraftVersion::CurrentStable;
else
versionType = MinecraftVersion::Stable;
}
else if(versionTypeStr == "snapshot")
{
versionType = MinecraftVersion::Snapshot;
}
else
{
2013-08-04 18:16:33 +05:30
//FIXME: log this somewhere
continue;
}
2013-08-04 18:16:33 +05:30
// Get the download URL.
QString dlUrl = QString(MCVLIST_URLBASE) + versionID + "/";
2013-08-04 18:16:33 +05:30
// Now, we construct the version object and add it to the list.
MinecraftVersion *mcVersion = new MinecraftVersion(versionID, versionID, versionTime.toMSecsSinceEpoch(),dlUrl, "");
mcVersion->setVersionType(versionType);
tempList.append(mcVersion);
}
2013-08-04 18:16:33 +05:30
m_list->updateListData(tempList);
// Once that's finished, we can delete the versions in our temp list.
while (!tempList.isEmpty())
delete tempList.takeFirst();
#ifdef PRINT_VERSIONS
m_list->printToStdOut();
#endif
2013-08-04 18:16:33 +05:30
exit(1);
}
2013-08-04 18:16:33 +05:30
// FIXME: we should have a local cache of the version list and a local cache of version data
bool MCVListLoadTask::loadFromVList()
{
}