From a1abbd9e05c80584d831b1d12c27c5f7d731cece Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sat, 28 May 2016 19:54:17 +0200 Subject: 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. --- api/logic/minecraft/AssetsUtils.cpp | 11 ++++++++-- api/logic/minecraft/Library.cpp | 24 +++++++++++++++------- api/logic/minecraft/MinecraftVersionList.cpp | 8 ++++---- api/logic/minecraft/forge/ForgeInstaller.cpp | 2 +- api/logic/minecraft/forge/ForgeVersionList.cpp | 16 ++++++--------- api/logic/minecraft/forge/ForgeVersionList.h | 4 ++-- api/logic/minecraft/legacy/LegacyUpdate.cpp | 4 ++-- .../minecraft/liteloader/LiteLoaderVersionList.cpp | 8 +++----- .../minecraft/liteloader/LiteLoaderVersionList.h | 2 +- api/logic/minecraft/onesix/OneSixUpdate.cpp | 13 +++++++++--- 10 files changed, 55 insertions(+), 37 deletions(-) (limited to 'api/logic/minecraft') diff --git a/api/logic/minecraft/AssetsUtils.cpp b/api/logic/minecraft/AssetsUtils.cpp index 7a525abe..bb630528 100644 --- a/api/logic/minecraft/AssetsUtils.cpp +++ b/api/logic/minecraft/AssetsUtils.cpp @@ -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; } diff --git a/api/logic/minecraft/Library.cpp b/api/logic/minecraft/Library.cpp index 922db84e..584c7ac5 100644 --- a/api/logic/minecraft/Library.cpp +++ b/api/logic/minecraft/Library.cpp @@ -1,5 +1,6 @@ #include "Library.h" -#include +#include +#include #include #include #include @@ -74,7 +75,7 @@ QList 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 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 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 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); } } } diff --git a/api/logic/minecraft/MinecraftVersionList.cpp b/api/logic/minecraft/MinecraftVersionList.cpp index a5cc3a39..4e4eafbc 100644 --- a/api/logic/minecraft/MinecraftVersionList.cpp +++ b/api/logic/minecraft/MinecraftVersionList.cpp @@ -68,6 +68,7 @@ slots: protected: NetJobPtr specificVersionDownloadJob; + QByteArray versionIndexData; std::shared_ptr 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(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) { diff --git a/api/logic/minecraft/forge/ForgeInstaller.cpp b/api/logic/minecraft/forge/ForgeInstaller.cpp index 353328ab..4d004bf5 100644 --- a/api/logic/minecraft/forge/ForgeInstaller.cpp +++ b/api/logic/minecraft/forge/ForgeInstaller.cpp @@ -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) diff --git a/api/logic/minecraft/forge/ForgeVersionList.cpp b/api/logic/minecraft/forge/ForgeVersionList.cpp index de185e5f..fe024780 100644 --- a/api/logic/minecraft/forge/ForgeVersionList.cpp +++ b/api/logic/minecraft/forge/ForgeVersionList.cpp @@ -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 &out) { QByteArray data; { - auto dlJob = listDownload; - auto filename = std::dynamic_pointer_cast(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 &out) QMap> lookup; QByteArray data; { - auto dlJob = gradleListDownload; - auto filename = std::dynamic_pointer_cast(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; diff --git a/api/logic/minecraft/forge/ForgeVersionList.h b/api/logic/minecraft/forge/ForgeVersionList.h index 62c08b2a..7b30bbb4 100644 --- a/api/logic/minecraft/forge/ForgeVersionList.h +++ b/api/logic/minecraft/forge/ForgeVersionList.h @@ -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 &out); diff --git a/api/logic/minecraft/legacy/LegacyUpdate.cpp b/api/logic/minecraft/legacy/LegacyUpdate.cpp index 6539b2d3..a0d1933f 100644 --- a/api/logic/minecraft/legacy/LegacyUpdate.cpp +++ b/api/logic/minecraft/legacy/LegacyUpdate.cpp @@ -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))); diff --git a/api/logic/minecraft/liteloader/LiteLoaderVersionList.cpp b/api/logic/minecraft/liteloader/LiteLoaderVersionList.cpp index b0c9736a..f8bf095f 100644 --- a/api/logic/minecraft/liteloader/LiteLoaderVersionList.cpp +++ b/api/logic/minecraft/liteloader/LiteLoaderVersionList.cpp @@ -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(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; diff --git a/api/logic/minecraft/liteloader/LiteLoaderVersionList.h b/api/logic/minecraft/liteloader/LiteLoaderVersionList.h index 1dba4b6a..ae8bee92 100644 --- a/api/logic/minecraft/liteloader/LiteLoaderVersionList.h +++ b/api/logic/minecraft/liteloader/LiteLoaderVersionList.h @@ -112,7 +112,7 @@ slots: protected: NetJobPtr listJob; - CacheDownloadPtr listDownload; + Net::Download::Ptr listDownload; LiteLoaderVersionList *m_list; }; diff --git a/api/logic/minecraft/onesix/OneSixUpdate.cpp b/api/logic/minecraft/onesix/OneSixUpdate.cpp index 1c2cd196..d3cd197d 100644 --- a/api/logic/minecraft/onesix/OneSixUpdate.cpp +++ b/api/logic/minecraft/onesix/OneSixUpdate.cpp @@ -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())); -- cgit v1.2.3