2016-08-14 06:03:31 +05:30
|
|
|
#include "Env.h"
|
|
|
|
#include "AssetUpdateTask.h"
|
2017-07-24 12:31:37 +05:30
|
|
|
#include "minecraft/MinecraftInstance.h"
|
2020-06-27 15:32:31 +05:30
|
|
|
#include "minecraft/PackProfile.h"
|
2016-08-14 06:03:31 +05:30
|
|
|
#include "net/ChecksumValidator.h"
|
|
|
|
#include "minecraft/AssetsUtils.h"
|
|
|
|
|
2017-07-24 12:31:37 +05:30
|
|
|
AssetUpdateTask::AssetUpdateTask(MinecraftInstance * inst)
|
2016-08-14 06:03:31 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
m_inst = inst;
|
2016-08-14 06:03:31 +05:30
|
|
|
}
|
2018-06-29 01:04:56 +05:30
|
|
|
|
|
|
|
AssetUpdateTask::~AssetUpdateTask()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-08-14 06:03:31 +05:30
|
|
|
void AssetUpdateTask::executeTask()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
setStatus(tr("Updating assets index..."));
|
2020-06-27 15:32:31 +05:30
|
|
|
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 = new NetJob(tr("Asset index for %1").arg(m_inst->name()));
|
2016-08-14 06:03:31 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
auto metacache = ENV.metacache();
|
|
|
|
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);
|
2016-08-14 06:03:31 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
downloadJob.reset(job);
|
2016-08-14 06:03:31 +05:30
|
|
|
|
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::progress, this, &AssetUpdateTask::progress);
|
2016-08-14 06:03:31 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
qDebug() << m_inst->name() << ": Starting asset index download";
|
|
|
|
downloadJob->start();
|
2016-08-14 06:03:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
bool AssetUpdateTask::canAbort() const
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return true;
|
2016-08-14 06:03:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void AssetUpdateTask::assetIndexFinished()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
AssetsIndex index;
|
|
|
|
qDebug() << m_inst->name() << ": Finished asset index download";
|
2016-08-14 06:03:31 +05:30
|
|
|
|
2020-06-27 15:32:31 +05:30
|
|
|
auto components = m_inst->getPackProfile();
|
2018-07-15 18:21:05 +05:30
|
|
|
auto profile = components->getProfile();
|
|
|
|
auto assets = profile->getMinecraftAssets();
|
2016-08-14 06:03:31 +05:30
|
|
|
|
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?
|
2019-02-19 05:30:03 +05:30
|
|
|
if (!AssetsUtils::loadAssetsIndexJson(assets->id, asset_fname, index))
|
2018-07-15 18:21:05 +05:30
|
|
|
{
|
|
|
|
auto metacache = ENV.metacache();
|
|
|
|
auto entry = metacache->resolveEntry("asset_indexes", assets->id + ".json");
|
|
|
|
metacache->evictEntry(entry);
|
|
|
|
emitFailed(tr("Failed to read the assets index!"));
|
|
|
|
}
|
2016-08-14 06:03:31 +05:30
|
|
|
|
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::progress, this, &AssetUpdateTask::progress);
|
|
|
|
downloadJob->start();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
emitSucceeded();
|
2016-08-14 06:03:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
2016-08-14 06:03:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void AssetUpdateTask::assetsFailed(QString reason)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
emitFailed(tr("Failed to download assets:\n%1").arg(reason));
|
2016-08-14 06:03:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
bool AssetUpdateTask::abort()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if(downloadJob)
|
|
|
|
{
|
|
|
|
return downloadJob->abort();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-02-19 05:30:03 +05:30
|
|
|
qWarning() << "Prematurely aborted AssetUpdateTask";
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
return true;
|
2016-08-14 06:03:31 +05:30
|
|
|
}
|