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"
|
2014-05-10 00:53:32 +01:00
|
|
|
#include "logic/MMCJson.h"
|
|
|
|
#include "ParseUtils.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
|
|
|
{
|
2014-05-08 18:05:07 +01:00
|
|
|
loadBuiltinList();
|
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();
|
|
|
|
}
|
|
|
|
|
2014-02-19 21:34:17 +00:00
|
|
|
static 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);
|
2014-05-10 00:53:32 +01:00
|
|
|
return left->m_releaseTime > right->m_releaseTime;
|
2013-05-04 02:14:38 +01:00
|
|
|
}
|
|
|
|
|
2014-02-09 10:00:34 +00:00
|
|
|
void MinecraftVersionList::sortInternal()
|
|
|
|
{
|
|
|
|
qSort(m_vlist.begin(), m_vlist.end(), cmpVersions);
|
|
|
|
}
|
|
|
|
|
2014-05-08 18:05:07 +01:00
|
|
|
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
|
|
|
|
2014-05-08 18:05:07 +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
|
|
|
|
2014-05-08 18:05:07 +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-08 18:05:07 +01:00
|
|
|
{
|
2014-05-10 00:53:32 +01:00
|
|
|
QLOG_ERROR() << "Parsed version is missing ID or type";
|
2014-05-08 18:05:07 +01:00
|
|
|
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;
|
|
|
|
|
2014-05-08 18:05:07 +01:00
|
|
|
// 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-08 18:05:07 +01:00
|
|
|
{
|
2014-05-10 00:53:32 +01:00
|
|
|
QLOG_ERROR() << "Error while parsing version" << versionID << ":" << e.cause();
|
2014-05-08 18:05:07 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-05-10 00:53:32 +01:00
|
|
|
// Get the download URL.
|
|
|
|
mcVersion->download_url =
|
|
|
|
"http://" + URLConstants::AWS_DOWNLOAD_VERSIONS + versionID + "/";
|
2014-05-08 18:05:07 +01:00
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
2014-05-08 18:05:07 +01:00
|
|
|
m_vlist.append(mcVersion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-04 02:14:38 +01:00
|
|
|
void MinecraftVersionList::sort()
|
|
|
|
{
|
|
|
|
beginResetModel();
|
2014-02-09 10:00:34 +00:00
|
|
|
sortInternal();
|
2013-05-04 02:14:38 +01:00
|
|
|
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();
|
2014-05-08 18:05:07 +01:00
|
|
|
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:
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2013-05-03 20:41:37 +01:00
|
|
|
m_loaded = true;
|
2014-02-09 10:00:34 +00:00
|
|
|
sortInternal();
|
2013-05-03 20:41:37 +01:00
|
|
|
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;
|
2013-05-03 20:41:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MCVListLoadTask::~MCVListLoadTask()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCVListLoadTask::executeTask()
|
|
|
|
{
|
2013-12-23 15:46:01 +00:00
|
|
|
setStatus(tr("Loading instance version list..."));
|
2013-09-07 03:00:58 +01:00
|
|
|
auto worker = MMC->qnam();
|
2014-05-08 18:05:07 +01: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
|
|
|
|
2014-05-08 18:05:07 +01:00
|
|
|
auto foo = vlistReply->readAll();
|
2013-08-04 13:46:33 +01:00
|
|
|
QJsonParseError jsonError;
|
2014-05-08 18:05:07 +01:00
|
|
|
QLOG_INFO() << foo;
|
|
|
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(foo, &jsonError);
|
2013-08-04 13:46:33 +01:00
|
|
|
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
|
|
|
|
2014-05-08 18:05:07 +01:00
|
|
|
QString latestReleaseID = "INVALID";
|
|
|
|
QString latestSnapshotID = "INVALID";
|
|
|
|
try
|
2013-08-04 13:46:33 +01:00
|
|
|
{
|
2014-05-08 18:05:07 +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
|
|
|
}
|
2014-05-08 18:05:07 +01:00
|
|
|
catch (MMCError &err)
|
2013-08-04 13:46:33 +01:00
|
|
|
{
|
2014-05-08 18:05:07 +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.
|
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;
|
2014-05-08 18:05:07 +01:00
|
|
|
for (auto version : versions)
|
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.
|
2014-05-08 18:05:07 +01:00
|
|
|
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;
|
|
|
|
}
|
2014-05-08 18:05:07 +01:00
|
|
|
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 + "/";
|
2013-10-08 00:36:11 +01:00
|
|
|
|
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
|
2013-08-11 17:58:24 +01:00
|
|
|
{
|
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);
|
2013-08-07 00:38:18 +01:00
|
|
|
}
|
2014-05-10 00:53:32 +01:00
|
|
|
catch (MMCError &e)
|
2013-05-03 20:41:37 +01:00
|
|
|
{
|
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;
|
2013-05-03 20:41:37 +01:00
|
|
|
}
|
2013-10-08 00:36:11 +01:00
|
|
|
|
2014-05-10 00:53:32 +01:00
|
|
|
mcVersion->m_versionSource = MinecraftVersion::Builtin;
|
2013-08-11 17:58:24 +01:00
|
|
|
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-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
|
|
|
}
|