PrismLauncher/logic/minecraft/MinecraftVersionList.cpp

342 lines
8.7 KiB
C++
Raw Normal View History

/* 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 "MinecraftVersionList.h"
#include "MultiMC.h"
#include "logic/net/URLConstants.h"
2014-05-10 00:53:32 +01:00
#include "logic/MMCJson.h"
#include "ParseUtils.h"
#include <QtXml>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#include <QJsonParseError>
#include <QtAlgorithms>
#include <QtNetwork>
MinecraftVersionList::MinecraftVersionList(QObject *parent) : BaseVersionList(parent)
{
loadBuiltinList();
}
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();
}
2014-02-19 21:34:17 +00:00
static bool cmpVersions(BaseVersionPtr first, BaseVersionPtr second)
{
2013-10-06 00:13:40 +01:00
auto left = std::dynamic_pointer_cast<MinecraftVersion>(first);
auto right = std::dynamic_pointer_cast<MinecraftVersion>(second);
2014-05-10 00:53:32 +01:00
return left->m_releaseTime > right->m_releaseTime;
}
2014-02-09 10:00:34 +00:00
void MinecraftVersionList::sortInternal()
{
qSort(m_vlist.begin(), m_vlist.end(), cmpVersions);
}
void MinecraftVersionList::loadBuiltinList()
{
// grab the version list data from internal resources.
QResource versionList(":/versions/minecraft.json");
QFile filez(versionList.absoluteFilePath());
filez.open(QIODevice::ReadOnly);
auto data = filez.readAll();
2014-05-10 00:53:32 +01:00
// parse the data as json
QJsonParseError jsonError;
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
QJsonObject root = jsonDoc.object();
2014-05-10 00:53:32 +01:00
// parse all the versions
for (const auto version : MMCJson::ensureArray(root.value("versions")))
{
QJsonObject versionObj = version.toObject();
QString versionID = versionObj.value("id").toString("");
QString versionTypeStr = versionObj.value("type").toString("");
2014-05-10 00:53:32 +01:00
if (versionID.isEmpty() || versionTypeStr.isEmpty())
{
2014-05-10 00:53:32 +01:00
QLOG_ERROR() << "Parsed version is missing ID or type";
continue;
}
2014-05-10 00:53:32 +01:00
// Now, we construct the version object and add it to the list.
std::shared_ptr<MinecraftVersion> mcVersion(new MinecraftVersion());
mcVersion->m_name = mcVersion->m_descriptor = versionID;
// Parse the timestamp.
2014-05-10 00:53:32 +01:00
try
{
parse_timestamp(versionObj.value("releaseTime").toString(""),
mcVersion->m_releaseTimeString, mcVersion->m_releaseTime);
}
catch (MMCError &e)
{
2014-05-10 00:53:32 +01:00
QLOG_ERROR() << "Error while parsing version" << versionID << ":" << e.cause();
continue;
}
2014-05-10 00:53:32 +01:00
// Get the download URL.
mcVersion->download_url =
"http://" + URLConstants::AWS_DOWNLOAD_VERSIONS + versionID + "/";
2014-05-10 00:53:32 +01:00
mcVersion->m_versionSource = MinecraftVersion::Builtin;
mcVersion->m_appletClass = versionObj.value("appletClass").toString("");
mcVersion->m_mainClass = versionObj.value("mainClass").toString("");
mcVersion->m_processArguments = versionObj.value("processArguments").toString("legacy");
if (versionObj.contains("+traits"))
{
for (auto traitVal : MMCJson::ensureArray(versionObj.value("+traits")))
{
mcVersion->m_traits.insert(MMCJson::ensureString(traitVal));
}
}
m_vlist.append(mcVersion);
}
}
void MinecraftVersionList::sort()
{
beginResetModel();
2014-02-09 10:00:34 +00:00
sortInternal();
endResetModel();
}
BaseVersionPtr MinecraftVersionList::getLatestStable() const
{
for (int i = 0; i < m_vlist.length(); i++)
{
auto ver = std::dynamic_pointer_cast<MinecraftVersion>(m_vlist.at(i));
if (ver->is_latest && !ver->is_snapshot)
{
return m_vlist.at(i);
}
}
return BaseVersionPtr();
}
void MinecraftVersionList::updateListData(QList<BaseVersionPtr> versions)
{
beginResetModel();
for (auto version : versions)
{
auto descr = version->descriptor();
for (auto builtin_v : m_vlist)
{
if (descr == builtin_v->descriptor())
{
goto SKIP_THIS_ONE;
}
}
m_vlist.append(version);
SKIP_THIS_ONE:
{
}
}
m_loaded = true;
2014-02-09 10:00:34 +00:00
sortInternal();
endResetModel();
}
inline QDomElement getDomElementByTagName(QDomElement parent, QString tagname)
{
QDomNodeList elementList = parent.elementsByTagName(tagname);
if (elementList.count())
return elementList.at(0).toElement();
else
return QDomElement();
}
MCVListLoadTask::MCVListLoadTask(MinecraftVersionList *vlist)
{
m_list = vlist;
m_currentStable = NULL;
2013-08-04 13:46:33 +01:00
vlistReply = nullptr;
}
MCVListLoadTask::~MCVListLoadTask()
{
}
void MCVListLoadTask::executeTask()
{
2013-12-23 15:46:01 +00:00
setStatus(tr("Loading instance version list..."));
auto worker = MMC->qnam();
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()
{
if (vlistReply->error() != QNetworkReply::NoError)
2013-08-04 13:46:33 +01:00
{
vlistReply->deleteLater();
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
}
auto foo = vlistReply->readAll();
2013-08-04 13:46:33 +01:00
QJsonParseError jsonError;
QLOG_INFO() << foo;
QJsonDocument jsonDoc = QJsonDocument::fromJson(foo, &jsonError);
2013-08-04 13:46:33 +01:00
vlistReply->deleteLater();
2013-08-04 13:46:33 +01:00
if (jsonError.error != QJsonParseError::NoError)
{
emitFailed("Error parsing version list JSON:" + jsonError.errorString());
2013-08-05 02:29:50 +01:00
return;
}
if (!jsonDoc.isObject())
2013-08-04 13:46:33 +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-08-04 13:46:33 +01:00
QJsonObject root = jsonDoc.object();
QString latestReleaseID = "INVALID";
QString latestSnapshotID = "INVALID";
try
2013-08-04 13:46:33 +01:00
{
QJsonObject latest = MMCJson::ensureObject(root.value("latest"));
latestReleaseID = MMCJson::ensureString(latest.value("release"));
latestSnapshotID = MMCJson::ensureString(latest.value("snapshot"));
2013-08-04 13:46:33 +01:00
}
catch (MMCError &err)
2013-08-04 13:46:33 +01:00
{
QLOG_ERROR()
<< tr("Error parsing version list JSON: couldn't determine latest versions");
2013-08-04 13:46:33 +01:00
}
// Now, get the array of versions.
if (!root.value("versions").isArray())
{
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();
QList<BaseVersionPtr> tempList;
for (auto version : versions)
{
bool is_snapshot = false;
bool is_latest = false;
2013-08-04 13:46:33 +01:00
// Load the version info.
if (!version.isObject())
2013-08-04 13:46:33 +01:00
{
2014-05-10 00:53:32 +01:00
QLOG_ERROR() << "Error while parsing version list : invalid JSON structure";
2013-08-04 13:46:33 +01:00
continue;
}
QJsonObject versionObj = version.toObject();
QString versionID = versionObj.value("id").toString("");
2014-05-10 00:53:32 +01:00
if (versionID.isEmpty())
2013-08-04 13:46:33 +01:00
{
2014-05-10 00:53:32 +01:00
QLOG_ERROR() << "Error while parsing version : version ID is missing";
2013-08-04 13:46:33 +01:00
continue;
}
2014-05-10 00:53:32 +01:00
// Get the download URL.
QString dlUrl = "http://" + URLConstants::AWS_DOWNLOAD_VERSIONS + versionID + "/";
2014-05-10 00:53:32 +01:00
// Now, we construct the version object and add it to the list.
std::shared_ptr<MinecraftVersion> mcVersion(new MinecraftVersion());
mcVersion->m_name = mcVersion->m_descriptor = versionID;
try
{
2014-05-10 00:53:32 +01:00
// Parse the timestamps.
parse_timestamp(versionObj.value("releaseTime").toString(""),
mcVersion->m_releaseTimeString, mcVersion->m_releaseTime);
parse_timestamp(versionObj.value("time").toString(""),
mcVersion->m_updateTimeString, mcVersion->m_updateTime);
}
2014-05-10 00:53:32 +01:00
catch (MMCError &e)
{
2014-05-10 00:53:32 +01:00
QLOG_ERROR() << "Error while parsing version" << versionID << ":" << e.cause();
2013-08-04 13:46:33 +01:00
continue;
}
2014-05-10 00:53:32 +01:00
mcVersion->m_versionSource = MinecraftVersion::Builtin;
mcVersion->download_url = dlUrl;
2014-05-10 00:53:32 +01:00
{
QString versionTypeStr = versionObj.value("type").toString("");
if (versionTypeStr.isEmpty())
{
// FIXME: log this somewhere
continue;
}
// OneSix or Legacy. use filter to determine type
if (versionTypeStr == "release")
{
is_latest = (versionID == latestReleaseID);
is_snapshot = false;
}
else if (versionTypeStr == "snapshot") // It's a snapshot... yay
{
is_latest = (versionID == latestSnapshotID);
is_snapshot = true;
}
else if (versionTypeStr == "old_alpha")
{
is_latest = false;
is_snapshot = false;
}
else if (versionTypeStr == "old_beta")
{
is_latest = false;
is_snapshot = false;
}
else
{
// FIXME: log this somewhere
continue;
}
mcVersion->m_type = versionTypeStr;
mcVersion->is_latest = is_latest;
mcVersion->is_snapshot = is_snapshot;
}
2013-08-04 13:46:33 +01:00
tempList.append(mcVersion);
}
2013-08-04 13:46:33 +01:00
m_list->updateListData(tempList);
emitSucceeded();
2013-08-05 02:29:50 +01:00
return;
}