NOISSUE refactor *Download into more, smaller pieces

* Download is now Download.
* Download uses Sink subclasses to process various events.
* Validators can be used to further customize the Sink behaviour.
This commit is contained in:
Petr Mrázek
2016-05-28 19:54:17 +02:00
parent a750f6e63c
commit a1abbd9e05
51 changed files with 824 additions and 765 deletions

View File

@ -25,7 +25,9 @@
#include "AssetsUtils.h"
#include "FileSystem.h"
#include "net/MD5EtagDownload.h"
#include "net/Download.h"
#include "net/ChecksumValidator.h"
namespace AssetsUtils
{
@ -191,7 +193,12 @@ NetActionPtr AssetObject::getDownloadAction()
QFileInfo objectFile(getLocalPath());
if ((!objectFile.isFile()) || (objectFile.size() != size))
{
auto objectDL = MD5EtagDownload::make(getUrl(), objectFile.filePath());
auto objectDL = Net::Download::makeFile(getUrl(), objectFile.filePath());
if(hash.size())
{
auto rawHash = QByteArray::fromHex(hash.toLatin1());
objectDL->addValidator(new Net::ChecksumValidator(QCryptographicHash::Sha1, rawHash));
}
objectDL->m_total_progress = size;
return objectDL;
}

View File

@ -1,5 +1,6 @@
#include "Library.h"
#include <net/CacheDownload.h>
#include <net/Download.h>
#include <net/ChecksumValidator.h>
#include <minecraft/forge/ForgeXzDownload.h>
#include <Env.h>
#include <FileSystem.h>
@ -74,7 +75,7 @@ QList<NetActionPtr> Library::getDownloads(OpSys system, HttpMetaCache * cache, Q
bool isLocal = (hint() == "local");
bool isForge = (hint() == "forge-pack-xz");
auto add_download = [&](QString storage, QString dl)
auto add_download = [&](QString storage, QString url, QString sha1 = QString())
{
auto entry = cache->resolveEntry("libraries", storage);
if (!entry->isStale())
@ -95,7 +96,16 @@ QList<NetActionPtr> Library::getDownloads(OpSys system, HttpMetaCache * cache, Q
}
else
{
out.append(CacheDownload::make(dl, entry));
if(sha1.size())
{
auto rawSha1 = QByteArray::fromHex(sha1.toLatin1());
auto dl = Net::Download::makeCached(url, entry);
dl->addValidator(new Net::ChecksumValidator(QCryptographicHash::Sha1, rawSha1));
out.append(dl);
}
else
out.append(Net::Download::makeCached(url, entry));
}
return true;
};
@ -105,7 +115,7 @@ QList<NetActionPtr> Library::getDownloads(OpSys system, HttpMetaCache * cache, Q
if(m_mojangDownloads->artifact)
{
auto artifact = m_mojangDownloads->artifact;
add_download(artifact->path, artifact->url);
add_download(artifact->path, artifact->url, artifact->sha1);
}
if(m_nativeClassifiers.contains(system))
{
@ -118,17 +128,17 @@ QList<NetActionPtr> Library::getDownloads(OpSys system, HttpMetaCache * cache, Q
nat64Classifier.replace("${arch}", "64");
auto nat32info = m_mojangDownloads->getDownloadInfo(nat32Classifier);
if(nat32info)
add_download(nat32info->path, nat32info->url);
add_download(nat32info->path, nat32info->url, nat32info->sha1);
auto nat64info = m_mojangDownloads->getDownloadInfo(nat64Classifier);
if(nat64info)
add_download(nat64info->path, nat64info->url);
add_download(nat64info->path, nat64info->url, nat64info->sha1);
}
else
{
auto info = m_mojangDownloads->getDownloadInfo(nativeClassifier);
if(info)
{
add_download(info->path, info->url);
add_download(info->path, info->url, info->sha1);
}
}
}

View File

@ -68,6 +68,7 @@ slots:
protected:
NetJobPtr specificVersionDownloadJob;
QByteArray versionIndexData;
std::shared_ptr<MinecraftVersion> updatedVersion;
MinecraftVersionList *m_list;
};
@ -410,7 +411,7 @@ MCVListVersionUpdateTask::MCVListVersionUpdateTask(MinecraftVersionList *vlist,
void MCVListVersionUpdateTask::executeTask()
{
auto job = new NetJob("Version index");
job->addNetAction(ByteArrayDownload::make(QUrl(updatedVersion->getUrl())));
job->addNetAction(Net::Download::makeByteArray(QUrl(updatedVersion->getUrl()), &versionIndexData));
specificVersionDownloadJob.reset(job);
connect(specificVersionDownloadJob.get(), SIGNAL(succeeded()), SLOT(json_downloaded()));
connect(specificVersionDownloadJob.get(), SIGNAL(failed(QString)), SIGNAL(failed(QString)));
@ -420,12 +421,11 @@ void MCVListVersionUpdateTask::executeTask()
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);
QJsonDocument jsonDoc = QJsonDocument::fromJson(versionIndexData, &jsonError);
versionIndexData.clear();
if (jsonError.error != QJsonParseError::NoError)
{

View File

@ -397,7 +397,7 @@ protected:
if (entry->isStale())
{
NetJob *fjob = new NetJob("Forge download");
fjob->addNetAction(CacheDownload::make(forgeVersion->url(), entry));
fjob->addNetAction(Net::Download::makeCached(forgeVersion->url(), entry));
connect(fjob, &NetJob::progress, this, &Task::setProgress);
connect(fjob, &NetJob::status, this, &Task::setStatus);
connect(fjob, &NetJob::failed, [this](QString reason)

View File

@ -131,10 +131,8 @@ void ForgeListLoadTask::executeTask()
forgeListEntry->setStale(true);
gradleForgeListEntry->setStale(true);
job->addNetAction(listDownload = CacheDownload::make(QUrl(URLConstants::FORGE_LEGACY_URL),
forgeListEntry));
job->addNetAction(gradleListDownload = CacheDownload::make(
QUrl(URLConstants::FORGE_GRADLE_URL), gradleForgeListEntry));
job->addNetAction(listDownload = Net::Download::makeCached(QUrl(URLConstants::FORGE_LEGACY_URL),forgeListEntry));
job->addNetAction(gradleListDownload = Net::Download::makeCached(QUrl(URLConstants::FORGE_GRADLE_URL), gradleForgeListEntry));
connect(listDownload.get(), SIGNAL(failed(int)), SLOT(listFailed()));
connect(gradleListDownload.get(), SIGNAL(failed(int)), SLOT(gradleListFailed()));
@ -154,15 +152,14 @@ bool ForgeListLoadTask::parseForgeList(QList<BaseVersionPtr> &out)
{
QByteArray data;
{
auto dlJob = listDownload;
auto filename = std::dynamic_pointer_cast<CacheDownload>(dlJob)->getTargetFilepath();
auto filename = listDownload->getTargetFilepath();
QFile listFile(filename);
if (!listFile.open(QIODevice::ReadOnly))
{
return false;
}
data = listFile.readAll();
dlJob.reset();
listDownload.reset();
}
QJsonParseError jsonError;
@ -266,15 +263,14 @@ bool ForgeListLoadTask::parseForgeGradleList(QList<BaseVersionPtr> &out)
QMap<int, std::shared_ptr<ForgeVersion>> lookup;
QByteArray data;
{
auto dlJob = gradleListDownload;
auto filename = std::dynamic_pointer_cast<CacheDownload>(dlJob)->getTargetFilepath();
auto filename = gradleListDownload->getTargetFilepath();
QFile listFile(filename);
if (!listFile.open(QIODevice::ReadOnly))
{
return false;
}
data = listFile.readAll();
dlJob.reset();
gradleListDownload.reset();
}
QJsonParseError jsonError;

View File

@ -81,8 +81,8 @@ protected:
NetJobPtr listJob;
ForgeVersionList *m_list;
CacheDownloadPtr listDownload;
CacheDownloadPtr gradleListDownload;
Net::Download::Ptr listDownload;
Net::Download::Ptr gradleListDownload;
private:
bool parseForgeList(QList<BaseVersionPtr> &out);

View File

@ -114,7 +114,7 @@ void LegacyUpdate::fmllibsStart()
auto entry = metacache->resolveEntry("fmllibs", lib.filename);
QString urlString = lib.ours ? URLConstants::FMLLIBS_OUR_BASE_URL + lib.filename
: URLConstants::FMLLIBS_FORGE_BASE_URL + lib.filename;
dljob->addNetAction(CacheDownload::make(QUrl(urlString), entry));
dljob->addNetAction(Net::Download::makeCached(QUrl(urlString), entry));
}
connect(dljob, &NetJob::succeeded, this, &LegacyUpdate::fmllibsFinished);
@ -372,7 +372,7 @@ void LegacyUpdate::jarStart()
auto metacache = ENV.metacache();
auto entry = metacache->resolveEntry("versions", URLConstants::getJarPath(version_id));
dljob->addNetAction(CacheDownload::make(QUrl(URLConstants::getLegacyJarUrl(version_id)), entry));
dljob->addNetAction(Net::Download::makeCached(QUrl(URLConstants::getLegacyJarUrl(version_id)), entry));
connect(dljob, SIGNAL(succeeded()), SLOT(jarFinished()));
connect(dljob, SIGNAL(failed(QString)), SLOT(jarFailed(QString)));
connect(dljob, SIGNAL(progress(qint64, qint64)), SIGNAL(progress(qint64, qint64)));

View File

@ -146,8 +146,7 @@ void LLListLoadTask::executeTask()
// verify by poking the server.
liteloaderEntry->setStale(true);
job->addNetAction(listDownload = CacheDownload::make(QUrl(URLConstants::LITELOADER_URL),
liteloaderEntry));
job->addNetAction(listDownload = Net::Download::makeCached(QUrl(URLConstants::LITELOADER_URL), liteloaderEntry));
connect(listDownload.get(), SIGNAL(failed(int)), SLOT(listFailed()));
@ -167,8 +166,7 @@ void LLListLoadTask::listDownloaded()
{
QByteArray data;
{
auto dlJob = listDownload;
auto filename = std::dynamic_pointer_cast<CacheDownload>(dlJob)->getTargetFilepath();
auto filename = listDownload->getTargetFilepath();
QFile listFile(filename);
if (!listFile.open(QIODevice::ReadOnly))
{
@ -177,7 +175,7 @@ void LLListLoadTask::listDownloaded()
}
data = listFile.readAll();
listFile.close();
dlJob.reset();
listDownload.reset();
}
QJsonParseError jsonError;

View File

@ -112,7 +112,7 @@ slots:
protected:
NetJobPtr listJob;
CacheDownloadPtr listDownload;
Net::Download::Ptr listDownload;
LiteLoaderVersionList *m_list;
};

View File

@ -31,6 +31,7 @@
#include "minecraft/MinecraftProfile.h"
#include "minecraft/Library.h"
#include "net/URLConstants.h"
#include "net/ChecksumValidator.h"
#include "minecraft/AssetsUtils.h"
#include "Exception.h"
#include "MMCZip.h"
@ -96,7 +97,13 @@ void OneSixUpdate::assetIndexStart()
auto metacache = ENV.metacache();
auto entry = metacache->resolveEntry("asset_indexes", localPath);
entry->setStale(true);
job->addNetAction(CacheDownload::make(indexUrl, entry));
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);
jarlibDownloadJob.reset(job);
connect(jarlibDownloadJob.get(), SIGNAL(succeeded()), SLOT(assetIndexFinished()));
@ -180,7 +187,7 @@ void OneSixUpdate::jarlibStart()
auto metacache = ENV.metacache();
auto entry = metacache->resolveEntry("versions", localPath);
job->addNetAction(CacheDownload::make(QUrl(urlstr), entry));
job->addNetAction(Net::Download::makeCached(QUrl(urlstr), entry));
jarlibDownloadJob.reset(job);
}
@ -293,7 +300,7 @@ void OneSixUpdate::fmllibsStart()
auto entry = metacache->resolveEntry("fmllibs", lib.filename);
QString urlString = lib.ours ? URLConstants::FMLLIBS_OUR_BASE_URL + lib.filename
: URLConstants::FMLLIBS_FORGE_BASE_URL + lib.filename;
dljob->addNetAction(CacheDownload::make(QUrl(urlString), entry));
dljob->addNetAction(Net::Download::makeCached(QUrl(urlString), entry));
}
connect(dljob, SIGNAL(succeeded()), SLOT(fmllibsFinished()));