2015-02-02 22:25:30 +00:00
|
|
|
/* Copyright 2013-2015 MultiMC Contributors
|
2013-05-03 19:41:37 +00: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-07 23:36:11 +00:00
|
|
|
*
|
2013-05-03 19:41:37 +00: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.
|
|
|
|
*/
|
|
|
|
|
2014-05-11 10:37:21 +00:00
|
|
|
#include <QtXml>
|
2015-05-28 17:38:29 +00:00
|
|
|
#include "Json.h"
|
2014-05-11 10:37:21 +00:00
|
|
|
#include <QtAlgorithms>
|
|
|
|
#include <QtNetwork>
|
2013-05-03 19:41:37 +00:00
|
|
|
|
2015-02-09 00:51:14 +00:00
|
|
|
#include "Env.h"
|
2015-05-28 17:38:29 +00:00
|
|
|
#include "Exception.h"
|
2013-05-03 19:41:37 +00:00
|
|
|
|
2014-05-11 10:37:21 +00:00
|
|
|
#include "MinecraftVersionList.h"
|
2015-02-09 00:51:14 +00:00
|
|
|
#include "net/URLConstants.h"
|
2013-05-03 19:41:37 +00:00
|
|
|
|
2014-05-11 10:37:21 +00:00
|
|
|
#include "ParseUtils.h"
|
2015-01-27 21:31:07 +00:00
|
|
|
#include "ProfileUtils.h"
|
|
|
|
#include "VersionFilterData.h"
|
|
|
|
|
2014-05-11 10:37:21 +00:00
|
|
|
#include <pathutils.h>
|
2013-05-04 01:14:38 +00:00
|
|
|
|
2014-05-13 21:57:34 +00:00
|
|
|
static const char * localVersionCache = "versions/versions.dat";
|
|
|
|
|
2015-01-27 21:31:07 +00:00
|
|
|
class MCVListLoadTask : public Task
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit MCVListLoadTask(MinecraftVersionList *vlist);
|
|
|
|
virtual ~MCVListLoadTask() override{};
|
|
|
|
|
|
|
|
virtual void executeTask() override;
|
|
|
|
|
|
|
|
protected
|
|
|
|
slots:
|
|
|
|
void list_downloaded();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
QNetworkReply *vlistReply;
|
|
|
|
MinecraftVersionList *m_list;
|
|
|
|
MinecraftVersion *m_currentStable;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MCVListVersionUpdateTask : public Task
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit MCVListVersionUpdateTask(MinecraftVersionList *vlist, QString updatedVersion);
|
|
|
|
virtual ~MCVListVersionUpdateTask() override{};
|
|
|
|
virtual void executeTask() override;
|
|
|
|
|
|
|
|
protected
|
|
|
|
slots:
|
|
|
|
void json_downloaded();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
NetJobPtr specificVersionDownloadJob;
|
|
|
|
QString versionToUpdate;
|
|
|
|
MinecraftVersionList *m_list;
|
|
|
|
};
|
|
|
|
|
2015-05-28 17:38:29 +00:00
|
|
|
class ListLoadError : public Exception
|
2014-05-11 10:37:21 +00:00
|
|
|
{
|
|
|
|
public:
|
2015-05-28 17:38:29 +00:00
|
|
|
ListLoadError(QString cause) : Exception(cause) {};
|
2014-05-11 10:37:21 +00:00
|
|
|
virtual ~ListLoadError() noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
2013-05-03 19:41:37 +00:00
|
|
|
|
2013-10-07 23:36:11 +00:00
|
|
|
MinecraftVersionList::MinecraftVersionList(QObject *parent) : BaseVersionList(parent)
|
2013-05-03 19:41:37 +00:00
|
|
|
{
|
2014-05-08 17:05:07 +00:00
|
|
|
loadBuiltinList();
|
2014-05-11 10:37:21 +00:00
|
|
|
loadCachedList();
|
2013-05-03 19:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Task *MinecraftVersionList::getLoadTask()
|
|
|
|
{
|
|
|
|
return new MCVListLoadTask(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MinecraftVersionList::isLoaded()
|
|
|
|
{
|
|
|
|
return m_loaded;
|
|
|
|
}
|
|
|
|
|
2013-09-15 22:54:39 +00:00
|
|
|
const BaseVersionPtr 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();
|
|
|
|
}
|
|
|
|
|
2014-02-19 21:34:17 +00:00
|
|
|
static bool cmpVersions(BaseVersionPtr first, BaseVersionPtr second)
|
2013-05-03 19:41:37 +00:00
|
|
|
{
|
2013-10-05 23:13:40 +00:00
|
|
|
auto left = std::dynamic_pointer_cast<MinecraftVersion>(first);
|
|
|
|
auto right = std::dynamic_pointer_cast<MinecraftVersion>(second);
|
2014-05-09 23:53:32 +00:00
|
|
|
return left->m_releaseTime > right->m_releaseTime;
|
2013-05-04 01:14:38 +00:00
|
|
|
}
|
|
|
|
|
2014-02-09 10:00:34 +00:00
|
|
|
void MinecraftVersionList::sortInternal()
|
|
|
|
{
|
|
|
|
qSort(m_vlist.begin(), m_vlist.end(), cmpVersions);
|
|
|
|
}
|
|
|
|
|
2014-05-11 10:37:21 +00:00
|
|
|
void MinecraftVersionList::loadCachedList()
|
|
|
|
{
|
2014-05-13 21:57:34 +00:00
|
|
|
QFile localIndex(localVersionCache);
|
2014-05-11 10:37:21 +00:00
|
|
|
if (!localIndex.exists())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!localIndex.open(QIODevice::ReadOnly))
|
|
|
|
{
|
|
|
|
// FIXME: this is actually a very bad thing! How do we deal with this?
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << "The minecraft version cache can't be read.";
|
2014-05-11 10:37:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto data = localIndex.readAll();
|
|
|
|
try
|
|
|
|
{
|
2014-05-13 21:57:34 +00:00
|
|
|
localIndex.close();
|
|
|
|
QJsonDocument jsonDoc = QJsonDocument::fromBinaryData(data);
|
|
|
|
if (jsonDoc.isNull())
|
|
|
|
{
|
|
|
|
throw ListLoadError(tr("Error reading the version list."));
|
|
|
|
}
|
|
|
|
loadMojangList(jsonDoc, Local);
|
2014-05-11 10:37:21 +00:00
|
|
|
}
|
2015-05-28 17:38:29 +00:00
|
|
|
catch (Exception &e)
|
2014-05-11 10:37:21 +00:00
|
|
|
{
|
|
|
|
// the cache has gone bad for some reason... flush it.
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << "The minecraft version cache is corrupted. Flushing cache.";
|
2014-05-11 10:37:21 +00:00
|
|
|
localIndex.remove();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_hasLocalIndex = true;
|
|
|
|
}
|
|
|
|
|
2014-05-08 17:05:07 +00:00
|
|
|
void MinecraftVersionList::loadBuiltinList()
|
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qDebug() << "Loading builtin version list.";
|
2014-05-08 17:05:07 +00:00
|
|
|
// grab the version list data from internal resources.
|
2014-09-06 16:16:56 +00:00
|
|
|
const QJsonDocument doc =
|
2015-06-03 19:57:22 +00:00
|
|
|
Json::requireDocument(QString(":/versions/minecraft.json"), "builtin version list");
|
2014-09-06 16:16:56 +00:00
|
|
|
const QJsonObject root = doc.object();
|
2014-05-09 23:53:32 +00:00
|
|
|
|
2014-05-08 17:05:07 +00:00
|
|
|
// parse all the versions
|
2015-06-03 19:57:22 +00:00
|
|
|
for (const auto version : Json::requireArray(root.value("versions")))
|
2014-05-08 17:05:07 +00:00
|
|
|
{
|
|
|
|
QJsonObject versionObj = version.toObject();
|
|
|
|
QString versionID = versionObj.value("id").toString("");
|
|
|
|
QString versionTypeStr = versionObj.value("type").toString("");
|
2014-05-09 23:53:32 +00:00
|
|
|
if (versionID.isEmpty() || versionTypeStr.isEmpty())
|
2014-05-08 17:05:07 +00:00
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << "Parsed version is missing ID or type";
|
2014-05-08 17:05:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-05-09 23:53:32 +00:00
|
|
|
|
2014-05-11 10:37:21 +00:00
|
|
|
if (g_VersionFilterData.legacyBlacklist.contains(versionID))
|
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qWarning() << "Blacklisted legacy version ignored: " << versionID;
|
2014-05-11 10:37:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-05-09 23:53:32 +00: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 17:05:07 +00:00
|
|
|
// Parse the timestamp.
|
2014-05-11 10:37:21 +00:00
|
|
|
if (!parse_timestamp(versionObj.value("releaseTime").toString(""),
|
|
|
|
mcVersion->m_releaseTimeString, mcVersion->m_releaseTime))
|
2014-05-08 17:05:07 +00:00
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << "Error while parsing version" << versionID
|
2014-05-11 10:37:21 +00:00
|
|
|
<< ": invalid version timestamp";
|
2014-05-08 17:05:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-05-09 23:53:32 +00:00
|
|
|
// Get the download URL.
|
|
|
|
mcVersion->download_url =
|
|
|
|
"http://" + URLConstants::AWS_DOWNLOAD_VERSIONS + versionID + "/";
|
2014-05-08 17:05:07 +00:00
|
|
|
|
2014-05-11 10:37:21 +00:00
|
|
|
mcVersion->m_versionSource = Builtin;
|
2014-05-11 17:13:21 +00:00
|
|
|
mcVersion->m_type = versionTypeStr;
|
2014-05-09 23:53:32 +00:00
|
|
|
mcVersion->m_appletClass = versionObj.value("appletClass").toString("");
|
|
|
|
mcVersion->m_mainClass = versionObj.value("mainClass").toString("");
|
2014-05-11 21:48:01 +00:00
|
|
|
mcVersion->m_jarChecksum = versionObj.value("checksum").toString("");
|
2014-05-09 23:53:32 +00:00
|
|
|
mcVersion->m_processArguments = versionObj.value("processArguments").toString("legacy");
|
|
|
|
if (versionObj.contains("+traits"))
|
|
|
|
{
|
2015-06-03 19:57:22 +00:00
|
|
|
for (auto traitVal : Json::requireArray(versionObj.value("+traits")))
|
2014-05-09 23:53:32 +00:00
|
|
|
{
|
2015-06-03 19:57:22 +00:00
|
|
|
mcVersion->m_traits.insert(Json::requireString(traitVal));
|
2014-05-09 23:53:32 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-11 10:37:21 +00:00
|
|
|
m_lookup[versionID] = mcVersion;
|
2014-05-08 17:05:07 +00:00
|
|
|
m_vlist.append(mcVersion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-13 21:57:34 +00:00
|
|
|
void MinecraftVersionList::loadMojangList(QJsonDocument jsonDoc, VersionSource source)
|
2014-05-11 10:37:21 +00:00
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qDebug() << "Loading" << ((source == Remote) ? "remote" : "local") << "version list.";
|
2014-05-11 10:37:21 +00:00
|
|
|
|
|
|
|
if (!jsonDoc.isObject())
|
|
|
|
{
|
|
|
|
throw ListLoadError(tr("Error parsing version list JSON: jsonDoc is not an object"));
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject root = jsonDoc.object();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2015-06-03 19:57:22 +00:00
|
|
|
QJsonObject latest = Json::requireObject(root.value("latest"));
|
|
|
|
m_latestReleaseID = Json::requireString(latest.value("release"));
|
|
|
|
m_latestSnapshotID = Json::requireString(latest.value("snapshot"));
|
2014-05-11 10:37:21 +00:00
|
|
|
}
|
2015-05-28 17:38:29 +00:00
|
|
|
catch (Exception &err)
|
2014-05-11 10:37:21 +00:00
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical()
|
2014-05-11 10:37:21 +00:00
|
|
|
<< tr("Error parsing version list JSON: couldn't determine latest versions");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, get the array of versions.
|
|
|
|
if (!root.value("versions").isArray())
|
|
|
|
{
|
|
|
|
throw ListLoadError(tr("Error parsing version list JSON: version list object is "
|
|
|
|
"missing 'versions' array"));
|
|
|
|
}
|
|
|
|
QJsonArray versions = root.value("versions").toArray();
|
|
|
|
|
|
|
|
QList<BaseVersionPtr> tempList;
|
|
|
|
for (auto version : versions)
|
|
|
|
{
|
|
|
|
// Load the version info.
|
|
|
|
if (!version.isObject())
|
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << "Error while parsing version list : invalid JSON structure";
|
2014-05-11 10:37:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject versionObj = version.toObject();
|
|
|
|
QString versionID = versionObj.value("id").toString("");
|
|
|
|
if (versionID.isEmpty())
|
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << "Error while parsing version : version ID is missing";
|
2014-05-11 10:37:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_VersionFilterData.legacyBlacklist.contains(versionID))
|
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qWarning() << "Blacklisted legacy version ignored: " << versionID;
|
2014-05-11 10:37:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
if (!parse_timestamp(versionObj.value("releaseTime").toString(""),
|
|
|
|
mcVersion->m_releaseTimeString, mcVersion->m_releaseTime))
|
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << "Error while parsing version" << versionID
|
2014-05-11 10:37:21 +00:00
|
|
|
<< ": invalid release timestamp";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!parse_timestamp(versionObj.value("time").toString(""),
|
|
|
|
mcVersion->m_updateTimeString, mcVersion->m_updateTime))
|
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << "Error while parsing version" << versionID
|
2014-05-11 10:37:21 +00:00
|
|
|
<< ": invalid update timestamp";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mcVersion->m_releaseTime < g_VersionFilterData.legacyCutoffDate)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// depends on where we load the version from -- network request or local file?
|
|
|
|
mcVersion->m_versionSource = source;
|
|
|
|
|
|
|
|
QString dlUrl = "http://" + URLConstants::AWS_DOWNLOAD_VERSIONS + versionID + "/";
|
|
|
|
mcVersion->download_url = dlUrl;
|
|
|
|
QString versionTypeStr = versionObj.value("type").toString("");
|
|
|
|
if (versionTypeStr.isEmpty())
|
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << "Ignoring" << versionID
|
2014-07-19 21:16:02 +00:00
|
|
|
<< "because it doesn't have the version type set.";
|
2014-05-11 10:37:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// OneSix or Legacy. use filter to determine type
|
|
|
|
if (versionTypeStr == "release")
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (versionTypeStr == "snapshot") // It's a snapshot... yay
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (versionTypeStr == "old_alpha")
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (versionTypeStr == "old_beta")
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << "Ignoring" << versionID
|
2014-07-19 21:16:02 +00:00
|
|
|
<< "because it has an invalid version type.";
|
2014-05-11 10:37:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
mcVersion->m_type = versionTypeStr;
|
2015-02-02 01:14:14 +00:00
|
|
|
qDebug() << "Loaded version" << versionID << "from"
|
2014-07-19 21:16:02 +00:00
|
|
|
<< ((source == Remote) ? "remote" : "local") << "version list.";
|
2014-05-11 10:37:21 +00:00
|
|
|
tempList.append(mcVersion);
|
|
|
|
}
|
|
|
|
updateListData(tempList);
|
2014-05-11 17:13:21 +00:00
|
|
|
if(source == Remote)
|
|
|
|
{
|
|
|
|
m_loaded = true;
|
|
|
|
}
|
2014-05-11 10:37:21 +00:00
|
|
|
}
|
|
|
|
|
2015-09-26 02:04:09 +00:00
|
|
|
void MinecraftVersionList::sortVersions()
|
2013-05-04 01:14:38 +00:00
|
|
|
{
|
|
|
|
beginResetModel();
|
2014-02-09 10:00:34 +00:00
|
|
|
sortInternal();
|
2013-05-04 01:14:38 +00:00
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
2015-04-28 07:01:37 +00:00
|
|
|
QVariant MinecraftVersionList::data(const QModelIndex& index, int role) const
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
if (index.row() > count())
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
auto version = std::dynamic_pointer_cast<MinecraftVersion>(m_vlist[index.row()]);
|
|
|
|
switch (role)
|
|
|
|
{
|
|
|
|
case VersionPointerRole:
|
|
|
|
return qVariantFromValue(m_vlist[index.row()]);
|
|
|
|
|
|
|
|
case VersionRole:
|
|
|
|
return version->name();
|
|
|
|
|
|
|
|
case VersionIdRole:
|
|
|
|
return version->descriptor();
|
|
|
|
|
|
|
|
case RecommendedRole:
|
2015-05-06 07:00:21 +00:00
|
|
|
return version->descriptor() == m_latestReleaseID;
|
|
|
|
|
|
|
|
case LatestRole:
|
|
|
|
{
|
|
|
|
if(version->descriptor() != m_latestSnapshotID)
|
|
|
|
return false;
|
|
|
|
MinecraftVersionPtr latestRelease = std::dynamic_pointer_cast<MinecraftVersion>(getLatestStable());
|
|
|
|
/*
|
|
|
|
if(latestRelease && latestRelease->m_releaseTime > version->m_releaseTime)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
return true;
|
|
|
|
}
|
2015-04-28 07:01:37 +00:00
|
|
|
|
|
|
|
case TypeRole:
|
|
|
|
return version->typeString();
|
|
|
|
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BaseVersionList::RoleList MinecraftVersionList::providesRoles()
|
|
|
|
{
|
2015-05-06 07:00:21 +00:00
|
|
|
return {VersionPointerRole, VersionRole, VersionIdRole, RecommendedRole, LatestRole, TypeRole};
|
2015-04-28 07:01:37 +00:00
|
|
|
}
|
|
|
|
|
2013-09-15 22:54:39 +00:00
|
|
|
BaseVersionPtr MinecraftVersionList::getLatestStable() const
|
2013-05-07 02:08:31 +00:00
|
|
|
{
|
2014-05-11 10:37:21 +00:00
|
|
|
if(m_lookup.contains(m_latestReleaseID))
|
|
|
|
return m_lookup[m_latestReleaseID];
|
2013-09-15 22:54:39 +00:00
|
|
|
return BaseVersionPtr();
|
2013-05-03 19:41:37 +00:00
|
|
|
}
|
|
|
|
|
2015-05-02 10:44:37 +00:00
|
|
|
BaseVersionPtr MinecraftVersionList::getRecommended() const
|
|
|
|
{
|
|
|
|
return getLatestStable();
|
|
|
|
}
|
|
|
|
|
2013-10-07 23:36:11 +00:00
|
|
|
void MinecraftVersionList::updateListData(QList<BaseVersionPtr> versions)
|
2013-05-03 19:41:37 +00:00
|
|
|
{
|
|
|
|
beginResetModel();
|
2014-05-08 17:05:07 +00:00
|
|
|
for (auto version : versions)
|
|
|
|
{
|
|
|
|
auto descr = version->descriptor();
|
2014-05-11 10:37:21 +00:00
|
|
|
|
|
|
|
if (!m_lookup.contains(descr))
|
2014-05-08 17:05:07 +00:00
|
|
|
{
|
2014-05-11 17:13:21 +00:00
|
|
|
m_lookup[version->descriptor()] = version;
|
2014-05-11 10:37:21 +00:00
|
|
|
m_vlist.append(version);
|
|
|
|
continue;
|
2014-05-08 17:05:07 +00:00
|
|
|
}
|
2014-05-11 10:37:21 +00:00
|
|
|
auto orig = std::dynamic_pointer_cast<MinecraftVersion>(m_lookup[descr]);
|
|
|
|
auto added = std::dynamic_pointer_cast<MinecraftVersion>(version);
|
|
|
|
// updateListData is called after Mojang list loads. those can be local or remote
|
|
|
|
// remote comes always after local
|
|
|
|
// any other options are ignored
|
|
|
|
if (orig->m_versionSource != Local || added->m_versionSource != Remote)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// alright, it's an update. put it inside the original, for further processing.
|
|
|
|
orig->upstreamUpdate = added;
|
2014-05-08 17:05:07 +00:00
|
|
|
}
|
2014-02-09 10:00:34 +00:00
|
|
|
sortInternal();
|
2013-05-03 19:41:37 +00: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 12:46:33 +00:00
|
|
|
vlistReply = nullptr;
|
2013-05-03 19:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MCVListLoadTask::executeTask()
|
|
|
|
{
|
2013-12-23 15:46:01 +00:00
|
|
|
setStatus(tr("Loading instance version list..."));
|
2015-01-31 15:59:03 +00:00
|
|
|
auto worker = ENV.qnam();
|
2014-05-08 17:05:07 +00:00
|
|
|
vlistReply = worker->get(QNetworkRequest(
|
|
|
|
QUrl("http://" + URLConstants::AWS_DOWNLOAD_VERSIONS + "versions.json")));
|
2013-08-04 12:46:33 +00:00
|
|
|
connect(vlistReply, SIGNAL(finished()), this, SLOT(list_downloaded()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCVListLoadTask::list_downloaded()
|
|
|
|
{
|
2013-10-07 23:36:11 +00:00
|
|
|
if (vlistReply->error() != QNetworkReply::NoError)
|
2013-08-04 12:46:33 +00:00
|
|
|
{
|
|
|
|
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-10-07 23:36:11 +00:00
|
|
|
|
2014-05-11 10:37:21 +00:00
|
|
|
auto data = vlistReply->readAll();
|
2013-08-04 12:46:33 +00:00
|
|
|
vlistReply->deleteLater();
|
2014-05-11 10:37:21 +00:00
|
|
|
try
|
2013-05-03 19:41:37 +00:00
|
|
|
{
|
2014-05-13 21:57:34 +00:00
|
|
|
QJsonParseError jsonError;
|
|
|
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
|
|
|
|
if (jsonError.error != QJsonParseError::NoError)
|
|
|
|
{
|
|
|
|
throw ListLoadError(
|
|
|
|
tr("Error parsing version list JSON: %1").arg(jsonError.errorString()));
|
|
|
|
}
|
|
|
|
m_list->loadMojangList(jsonDoc, Remote);
|
2013-05-03 19:41:37 +00:00
|
|
|
}
|
2015-05-28 17:38:29 +00:00
|
|
|
catch (Exception &e)
|
2013-08-04 12:46:33 +00:00
|
|
|
{
|
2014-05-11 10:37:21 +00:00
|
|
|
emitFailed(e.cause());
|
2013-08-05 01:29:50 +00:00
|
|
|
return;
|
2013-08-04 12:46:33 +00:00
|
|
|
}
|
2013-10-07 23:36:11 +00:00
|
|
|
|
2014-05-11 10:37:21 +00:00
|
|
|
emitSucceeded();
|
|
|
|
return;
|
|
|
|
}
|
2013-10-07 23:36:11 +00:00
|
|
|
|
2014-05-11 10:37:21 +00:00
|
|
|
MCVListVersionUpdateTask::MCVListVersionUpdateTask(MinecraftVersionList *vlist,
|
|
|
|
QString updatedVersion)
|
|
|
|
: Task()
|
|
|
|
{
|
|
|
|
m_list = vlist;
|
|
|
|
versionToUpdate = updatedVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCVListVersionUpdateTask::executeTask()
|
|
|
|
{
|
|
|
|
QString urlstr = "http://" + URLConstants::AWS_DOWNLOAD_VERSIONS + versionToUpdate + "/" +
|
|
|
|
versionToUpdate + ".json";
|
|
|
|
auto job = new NetJob("Version index");
|
|
|
|
job->addNetAction(ByteArrayDownload::make(QUrl(urlstr)));
|
|
|
|
specificVersionDownloadJob.reset(job);
|
|
|
|
connect(specificVersionDownloadJob.get(), SIGNAL(succeeded()), SLOT(json_downloaded()));
|
|
|
|
connect(specificVersionDownloadJob.get(), SIGNAL(failed(QString)), SIGNAL(failed(QString)));
|
|
|
|
connect(specificVersionDownloadJob.get(), SIGNAL(progress(qint64, qint64)),
|
|
|
|
SIGNAL(progress(qint64, qint64)));
|
|
|
|
specificVersionDownloadJob->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCVListVersionUpdateTask::json_downloaded()
|
|
|
|
{
|
|
|
|
NetActionPtr DlJob = specificVersionDownloadJob->first();
|
|
|
|
auto data = std::dynamic_pointer_cast<ByteArrayDownload>(DlJob)->m_data;
|
|
|
|
specificVersionDownloadJob.reset();
|
|
|
|
|
|
|
|
QJsonParseError jsonError;
|
|
|
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
|
|
|
|
|
|
|
|
if (jsonError.error != QJsonParseError::NoError)
|
2013-08-04 12:46:33 +00:00
|
|
|
{
|
2014-05-11 10:37:21 +00:00
|
|
|
emitFailed(tr("The download version file is not valid."));
|
|
|
|
return;
|
2013-08-04 12:46:33 +00:00
|
|
|
}
|
2014-05-11 10:37:21 +00:00
|
|
|
VersionFilePtr file;
|
|
|
|
try
|
2013-08-04 12:46:33 +00:00
|
|
|
{
|
2014-05-11 10:37:21 +00:00
|
|
|
file = VersionFile::fromJson(jsonDoc, "net.minecraft.json", false);
|
2013-08-04 12:46:33 +00:00
|
|
|
}
|
2015-05-28 17:38:29 +00:00
|
|
|
catch (Exception &e)
|
2013-05-03 19:41:37 +00:00
|
|
|
{
|
2014-05-11 10:37:21 +00:00
|
|
|
emitFailed(tr("Couldn't process version file: %1").arg(e.cause()));
|
2013-08-05 01:29:50 +00:00
|
|
|
return;
|
2013-08-04 12:46:33 +00:00
|
|
|
}
|
2014-07-19 21:16:02 +00:00
|
|
|
|
2015-01-27 21:31:07 +00:00
|
|
|
// Strip LWJGL from the version file. We use our own.
|
|
|
|
ProfileUtils::removeLwjglFromPatch(file);
|
2013-10-07 23:36:11 +00:00
|
|
|
|
2014-05-11 10:37:21 +00:00
|
|
|
// TODO: recognize and add LWJGL versions here.
|
2014-05-09 23:53:32 +00:00
|
|
|
|
2014-05-11 10:37:21 +00:00
|
|
|
file->fileId = "net.minecraft";
|
2014-05-09 23:53:32 +00:00
|
|
|
|
2014-05-11 10:37:21 +00:00
|
|
|
// now dump the file to disk
|
|
|
|
auto doc = file->toJson(false);
|
2014-05-13 21:57:34 +00:00
|
|
|
auto newdata = doc.toBinaryData();
|
|
|
|
QString targetPath = "versions/" + versionToUpdate + "/" + versionToUpdate + ".dat";
|
2014-05-11 10:37:21 +00:00
|
|
|
ensureFilePathExists(targetPath);
|
|
|
|
QSaveFile vfile1(targetPath);
|
|
|
|
if (!vfile1.open(QIODevice::Truncate | QIODevice::WriteOnly))
|
|
|
|
{
|
|
|
|
emitFailed(tr("Can't open %1 for writing.").arg(targetPath));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
qint64 actual = 0;
|
|
|
|
if ((actual = vfile1.write(newdata)) != newdata.size())
|
|
|
|
{
|
|
|
|
emitFailed(tr("Failed to write into %1. Written %2 out of %3.")
|
|
|
|
.arg(targetPath)
|
|
|
|
.arg(actual)
|
|
|
|
.arg(newdata.size()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!vfile1.commit())
|
|
|
|
{
|
|
|
|
emitFailed(tr("Can't commit changes to %1").arg(targetPath));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_list->finalizeUpdate(versionToUpdate);
|
|
|
|
emitSucceeded();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Task> MinecraftVersionList::createUpdateTask(QString version)
|
|
|
|
{
|
|
|
|
return std::shared_ptr<Task>(new MCVListVersionUpdateTask(this, version));
|
|
|
|
}
|
|
|
|
|
|
|
|
void MinecraftVersionList::saveCachedList()
|
|
|
|
{
|
|
|
|
// FIXME: throw.
|
2014-05-13 21:57:34 +00:00
|
|
|
if (!ensureFilePathExists(localVersionCache))
|
2014-05-11 10:37:21 +00:00
|
|
|
return;
|
2014-05-13 21:57:34 +00:00
|
|
|
QSaveFile tfile(localVersionCache);
|
2014-05-11 10:37:21 +00:00
|
|
|
if (!tfile.open(QIODevice::WriteOnly | QIODevice::Truncate))
|
|
|
|
return;
|
|
|
|
QJsonObject toplevel;
|
|
|
|
QJsonArray entriesArr;
|
|
|
|
for (auto version : m_vlist)
|
|
|
|
{
|
|
|
|
auto mcversion = std::dynamic_pointer_cast<MinecraftVersion>(version);
|
|
|
|
// do not save the remote versions.
|
|
|
|
if (mcversion->m_versionSource != Local)
|
2013-08-04 12:46:33 +00:00
|
|
|
continue;
|
2014-05-11 10:37:21 +00:00
|
|
|
QJsonObject entryObj;
|
2013-10-07 23:36:11 +00:00
|
|
|
|
2014-05-11 10:37:21 +00:00
|
|
|
entryObj.insert("id", mcversion->descriptor());
|
2015-05-28 07:36:58 +00:00
|
|
|
entryObj.insert("version", mcversion->descriptor());
|
2014-05-11 10:37:21 +00:00
|
|
|
entryObj.insert("time", mcversion->m_updateTimeString);
|
|
|
|
entryObj.insert("releaseTime", mcversion->m_releaseTimeString);
|
|
|
|
entryObj.insert("type", mcversion->m_type);
|
|
|
|
entriesArr.append(entryObj);
|
|
|
|
}
|
|
|
|
toplevel.insert("versions", entriesArr);
|
2015-01-27 21:31:07 +00:00
|
|
|
|
2014-05-11 17:13:21 +00:00
|
|
|
{
|
|
|
|
bool someLatest = false;
|
|
|
|
QJsonObject latestObj;
|
|
|
|
if(!m_latestReleaseID.isNull())
|
|
|
|
{
|
|
|
|
latestObj.insert("release", m_latestReleaseID);
|
|
|
|
someLatest = true;
|
|
|
|
}
|
|
|
|
if(!m_latestSnapshotID.isNull())
|
|
|
|
{
|
|
|
|
latestObj.insert("snapshot", m_latestSnapshotID);
|
|
|
|
someLatest = true;
|
|
|
|
}
|
|
|
|
if(someLatest)
|
|
|
|
{
|
|
|
|
toplevel.insert("latest", latestObj);
|
|
|
|
}
|
|
|
|
}
|
2015-01-27 21:31:07 +00:00
|
|
|
|
2014-05-11 10:37:21 +00:00
|
|
|
QJsonDocument doc(toplevel);
|
2014-05-13 21:57:34 +00:00
|
|
|
QByteArray jsonData = doc.toBinaryData();
|
2014-05-11 10:37:21 +00:00
|
|
|
qint64 result = tfile.write(jsonData);
|
|
|
|
if (result == -1)
|
|
|
|
return;
|
|
|
|
if (result != jsonData.size())
|
|
|
|
return;
|
|
|
|
tfile.commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MinecraftVersionList::finalizeUpdate(QString version)
|
|
|
|
{
|
|
|
|
int idx = -1;
|
|
|
|
for (int i = 0; i < m_vlist.size(); i++)
|
|
|
|
{
|
|
|
|
if (version == m_vlist[i]->descriptor())
|
2014-05-09 23:53:32 +00:00
|
|
|
{
|
2014-05-11 10:37:21 +00:00
|
|
|
idx = i;
|
|
|
|
break;
|
2014-05-09 23:53:32 +00:00
|
|
|
}
|
2013-05-03 19:41:37 +00:00
|
|
|
}
|
2014-05-11 10:37:21 +00:00
|
|
|
if (idx == -1)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-10-07 23:36:11 +00:00
|
|
|
|
2014-05-11 10:37:21 +00:00
|
|
|
auto updatedVersion = std::dynamic_pointer_cast<MinecraftVersion>(m_vlist[idx]);
|
|
|
|
|
2014-07-19 21:16:02 +00:00
|
|
|
// reject any updates to builtin versions.
|
2014-05-11 10:37:21 +00:00
|
|
|
if (updatedVersion->m_versionSource == Builtin)
|
|
|
|
return;
|
|
|
|
|
2014-07-19 21:16:02 +00:00
|
|
|
// if we have an update for the version, replace it, make the update local
|
2014-05-11 10:37:21 +00:00
|
|
|
if (updatedVersion->upstreamUpdate)
|
|
|
|
{
|
|
|
|
auto updatedWith = updatedVersion->upstreamUpdate;
|
|
|
|
updatedWith->m_versionSource = Local;
|
|
|
|
m_vlist[idx] = updatedWith;
|
|
|
|
m_lookup[version] = updatedWith;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-19 21:16:02 +00:00
|
|
|
// otherwise, just set the version as local;
|
2014-05-11 10:37:21 +00:00
|
|
|
updatedVersion->m_versionSource = Local;
|
|
|
|
}
|
|
|
|
|
|
|
|
dataChanged(index(idx), index(idx));
|
|
|
|
|
|
|
|
saveCachedList();
|
2013-05-03 19:41:37 +00:00
|
|
|
}
|
2015-01-27 21:31:07 +00:00
|
|
|
|
2015-04-28 07:01:37 +00:00
|
|
|
#include "MinecraftVersionList.moc"
|