2016-08-14 06:03:31 +05:30
|
|
|
#include "AssetUpdateTask.h"
|
2021-11-22 03:51:12 +05:30
|
|
|
|
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"
|
|
|
|
|
2021-11-22 03:51:12 +05:30
|
|
|
#include "Application.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";
|
2021-12-31 09:57:59 +05:30
|
|
|
auto job = new NetJob(
|
|
|
|
tr("Asset index for %1").arg(m_inst->name()),
|
|
|
|
APPLICATION->network()
|
|
|
|
);
|
2016-08-14 06:03:31 +05:30
|
|
|
|
2021-11-22 03:51:12 +05:30
|
|
|
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);
|
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);
|
2022-06-23 04:26:24 +05:30
|
|
|
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);
|
2016-08-14 06:03:31 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
qDebug() << m_inst->name() << ": Starting asset index download";
|
2021-12-31 09:57:59 +05:30
|
|
|
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
|
|
|
{
|
2021-11-22 03:51:12 +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!"));
|
|
|
|
}
|
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);
|
2022-06-23 04:26:24 +05:30
|
|
|
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);
|
2021-12-31 09:57:59 +05:30
|
|
|
downloadJob->start();
|
2018-07-15 18:21:05 +05:30
|
|
|
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
|
|
|
}
|