pollymc/launcher/minecraft/update/AssetUpdateTask.cpp

115 lines
3.6 KiB
C++
Raw Normal View History

#include "AssetUpdateTask.h"
#include "minecraft/MinecraftInstance.h"
#include "minecraft/PackProfile.h"
#include "net/ChecksumValidator.h"
#include "minecraft/AssetsUtils.h"
#include "Application.h"
AssetUpdateTask::AssetUpdateTask(MinecraftInstance * inst)
{
2018-07-15 18:21:05 +05:30
m_inst = inst;
}
AssetUpdateTask::~AssetUpdateTask()
{
}
void AssetUpdateTask::executeTask()
{
2018-07-15 18:21:05 +05:30
setStatus(tr("Updating assets index..."));
auto components = m_inst->getPackProfile();
2018-07-15 18:21:05 +05:30
auto profile = components->getProfile();
auto assets = profile->getMinecraftAssets();
QUrl indexUrl = assets->url;
QString localPath = assets->id + ".json";
auto job = makeShared<NetJob>(
tr("Asset index for %1").arg(m_inst->name()),
APPLICATION->network()
);
auto metacache = APPLICATION->metacache();
2018-07-15 18:21:05 +05:30
auto entry = metacache->resolveEntry("asset_indexes", localPath);
entry->setStale(true);
auto hexSha1 = assets->sha1.toLatin1();
qDebug() << "Asset index SHA1:" << hexSha1;
auto dl = Net::Download::makeCached(indexUrl, entry);
auto rawSha1 = QByteArray::fromHex(assets->sha1.toLatin1());
dl->addValidator(new Net::ChecksumValidator(QCryptographicHash::Sha1, rawSha1));
job->addNetAction(dl);
2018-07-15 18:21:05 +05:30
downloadJob.reset(job);
2018-07-15 18:21:05 +05:30
connect(downloadJob.get(), &NetJob::succeeded, this, &AssetUpdateTask::assetIndexFinished);
connect(downloadJob.get(), &NetJob::failed, this, &AssetUpdateTask::assetIndexFailed);
connect(downloadJob.get(), &NetJob::aborted, this, [this]{ emitFailed(tr("Aborted")); });
2018-07-15 18:21:05 +05:30
connect(downloadJob.get(), &NetJob::progress, this, &AssetUpdateTask::progress);
2018-07-15 18:21:05 +05:30
qDebug() << m_inst->name() << ": Starting asset index download";
downloadJob->start();
}
bool AssetUpdateTask::canAbort() const
{
2018-07-15 18:21:05 +05:30
return true;
}
void AssetUpdateTask::assetIndexFinished()
{
2018-07-15 18:21:05 +05:30
AssetsIndex index;
qDebug() << m_inst->name() << ": Finished asset index download";
auto components = m_inst->getPackProfile();
2018-07-15 18:21:05 +05:30
auto profile = components->getProfile();
auto assets = profile->getMinecraftAssets();
2018-07-15 18:21:05 +05:30
QString asset_fname = "assets/indexes/" + assets->id + ".json";
// FIXME: this looks like a job for a generic validator based on json schema?
if (!AssetsUtils::loadAssetsIndexJson(assets->id, asset_fname, index))
2018-07-15 18:21:05 +05:30
{
auto metacache = APPLICATION->metacache();
2018-07-15 18:21:05 +05:30
auto entry = metacache->resolveEntry("asset_indexes", assets->id + ".json");
metacache->evictEntry(entry);
emitFailed(tr("Failed to read the assets index!"));
}
2018-07-15 18:21:05 +05:30
auto job = index.getDownloadJob();
if(job)
{
setStatus(tr("Getting the assets files from Mojang..."));
downloadJob = job;
connect(downloadJob.get(), &NetJob::succeeded, this, &AssetUpdateTask::emitSucceeded);
connect(downloadJob.get(), &NetJob::failed, this, &AssetUpdateTask::assetsFailed);
connect(downloadJob.get(), &NetJob::aborted, this, [this]{ emitFailed(tr("Aborted")); });
2018-07-15 18:21:05 +05:30
connect(downloadJob.get(), &NetJob::progress, this, &AssetUpdateTask::progress);
downloadJob->start();
2018-07-15 18:21:05 +05:30
return;
}
emitSucceeded();
}
void AssetUpdateTask::assetIndexFailed(QString reason)
{
2018-07-15 18:21:05 +05:30
qDebug() << m_inst->name() << ": Failed asset index download";
emitFailed(tr("Failed to download the assets index:\n%1").arg(reason));
}
void AssetUpdateTask::assetsFailed(QString reason)
{
2018-07-15 18:21:05 +05:30
emitFailed(tr("Failed to download assets:\n%1").arg(reason));
}
bool AssetUpdateTask::abort()
{
2018-07-15 18:21:05 +05:30
if(downloadJob)
{
return downloadJob->abort();
}
else
{
qWarning() << "Prematurely aborted AssetUpdateTask";
2018-07-15 18:21:05 +05:30
}
return true;
}