mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-28 09:33:55 -07:00
Parallelize picture loader.
This commit is contained in:
parent
cc7deef83e
commit
85973caa03
5 changed files with 23 additions and 16 deletions
|
|
@ -96,6 +96,7 @@ set(cockatrice_SOURCES
|
||||||
src/client/ui/phases_toolbar.cpp
|
src/client/ui/phases_toolbar.cpp
|
||||||
src/client/ui/picture_loader/picture_loader.cpp
|
src/client/ui/picture_loader/picture_loader.cpp
|
||||||
src/client/ui/picture_loader/picture_loader_worker.cpp
|
src/client/ui/picture_loader/picture_loader_worker.cpp
|
||||||
|
src/client/ui/picture_loader/picture_loader_worker_work.cpp
|
||||||
src/client/ui/picture_loader/picture_to_load.cpp
|
src/client/ui/picture_loader/picture_to_load.cpp
|
||||||
src/game/zones/pile_zone.cpp
|
src/game/zones/pile_zone.cpp
|
||||||
src/client/ui/pixel_map_generator.cpp
|
src/client/ui/pixel_map_generator.cpp
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,6 @@ PictureLoaderWorker::PictureLoaderWorker()
|
||||||
customPicsPath(SettingsCache::instance().getCustomPicsPath()),
|
customPicsPath(SettingsCache::instance().getCustomPicsPath()),
|
||||||
picDownload(SettingsCache::instance().getPicDownload()), downloadRunning(false), loadQueueRunning(false)
|
picDownload(SettingsCache::instance().getPicDownload()), downloadRunning(false), loadQueueRunning(false)
|
||||||
{
|
{
|
||||||
connect(this, SIGNAL(startLoadQueue()), this, SLOT(processLoadQueue()), Qt::QueuedConnection);
|
|
||||||
connect(&SettingsCache::instance(), SIGNAL(picsPathChanged()), this, SLOT(picsPathChanged()));
|
connect(&SettingsCache::instance(), SIGNAL(picsPathChanged()), this, SLOT(picsPathChanged()));
|
||||||
connect(&SettingsCache::instance(), SIGNAL(picDownloadChanged()), this, SLOT(picDownloadChanged()));
|
connect(&SettingsCache::instance(), SIGNAL(picDownloadChanged()), this, SLOT(picDownloadChanged()));
|
||||||
|
|
||||||
|
|
@ -42,7 +41,6 @@ PictureLoaderWorker::PictureLoaderWorker()
|
||||||
// Use a ManualRedirectPolicy since we keep track of redirects in picDownloadFinished
|
// Use a ManualRedirectPolicy since we keep track of redirects in picDownloadFinished
|
||||||
// We can't use NoLessSafeRedirectPolicy because it is not applied with AlwaysCache
|
// We can't use NoLessSafeRedirectPolicy because it is not applied with AlwaysCache
|
||||||
networkManager->setRedirectPolicy(QNetworkRequest::ManualRedirectPolicy);
|
networkManager->setRedirectPolicy(QNetworkRequest::ManualRedirectPolicy);
|
||||||
connect(networkManager, SIGNAL(finished(QNetworkReply *)), this, SLOT(picDownloadFinished(QNetworkReply *)));
|
|
||||||
|
|
||||||
cacheFilePath = SettingsCache::instance().getRedirectCachePath() + REDIRECT_CACHE_FILENAME;
|
cacheFilePath = SettingsCache::instance().getRedirectCachePath() + REDIRECT_CACHE_FILENAME;
|
||||||
loadRedirectCache();
|
loadRedirectCache();
|
||||||
|
|
@ -67,9 +65,9 @@ QNetworkReply *PictureLoaderWorker::makeRequest(const QUrl &url, PictureLoaderWo
|
||||||
QUrl cachedRedirect = getCachedRedirect(url);
|
QUrl cachedRedirect = getCachedRedirect(url);
|
||||||
if (!cachedRedirect.isEmpty()) {
|
if (!cachedRedirect.isEmpty()) {
|
||||||
qCDebug(PictureLoaderWorkerLog).nospace()
|
qCDebug(PictureLoaderWorkerLog).nospace()
|
||||||
<< "[card: " << cardBeingDownloaded.getCard()->getCorrectedName()
|
<< "PictureLoader: [card: " << worker->cardToDownload.getCard()->getCorrectedName()
|
||||||
<< " set: " << cardBeingDownloaded.getSetName() << "]: Using cached redirect for " << url.toDisplayString()
|
<< " set: " << worker->cardToDownload.getSetName() << "]: Using cached redirect for "
|
||||||
<< " to " << cachedRedirect.toDisplayString();
|
<< url.toDisplayString() << " to " << cachedRedirect.toDisplayString();
|
||||||
return makeRequest(cachedRedirect, worker); // Use the cached redirect
|
return makeRequest(cachedRedirect, worker); // Use the cached redirect
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -105,11 +103,11 @@ QNetworkReply *PictureLoaderWorker::makeRequest(const QUrl &url, PictureLoaderWo
|
||||||
<< " set: " << cardBeingDownloaded.getSetName() << "]: Caching redirect from " << url.toDisplayString()
|
<< " set: " << cardBeingDownloaded.getSetName() << "]: Caching redirect from " << url.toDisplayString()
|
||||||
<< " to " << redirectUrl.toDisplayString();
|
<< " to " << redirectUrl.toDisplayString();
|
||||||
}
|
}
|
||||||
|
|
||||||
reply->deleteLater();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
connect(reply, &QNetworkReply::finished, worker, [reply, worker]() { worker->picDownloadFinished(reply); });
|
connect(
|
||||||
|
reply, &QNetworkReply::finished, worker, [reply, worker]() { worker->picDownloadFinished(reply); },
|
||||||
|
Qt::QueuedConnection);
|
||||||
|
|
||||||
return reply;
|
return reply;
|
||||||
}
|
}
|
||||||
|
|
@ -120,6 +118,11 @@ void PictureLoaderWorker::enqueueImageLoad(const CardInfoPtr &card)
|
||||||
Q_UNUSED(worker);
|
Q_UNUSED(worker);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PictureLoaderWorker::imageLoadedSuccessfully(CardInfoPtr card, const QImage &image)
|
||||||
|
{
|
||||||
|
emit imageLoaded(card, image);
|
||||||
|
}
|
||||||
|
|
||||||
void PictureLoaderWorker::cacheRedirect(const QUrl &originalUrl, const QUrl &redirectUrl)
|
void PictureLoaderWorker::cacheRedirect(const QUrl &originalUrl, const QUrl &redirectUrl)
|
||||||
{
|
{
|
||||||
redirectCache[originalUrl] = qMakePair(redirectUrl, QDateTime::currentDateTimeUtc());
|
redirectCache[originalUrl] = qMakePair(redirectUrl, QDateTime::currentDateTimeUtc());
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@
|
||||||
#define REDIRECT_TIMESTAMP "timestamp"
|
#define REDIRECT_TIMESTAMP "timestamp"
|
||||||
#define REDIRECT_CACHE_FILENAME "cache.ini"
|
#define REDIRECT_CACHE_FILENAME "cache.ini"
|
||||||
|
|
||||||
|
class PictureLoaderWorkerWork;
|
||||||
inline Q_LOGGING_CATEGORY(PictureLoaderWorkerLog, "picture_loader.worker");
|
inline Q_LOGGING_CATEGORY(PictureLoaderWorkerLog, "picture_loader.worker");
|
||||||
|
|
||||||
class PictureLoaderWorker : public QObject
|
class PictureLoaderWorker : public QObject
|
||||||
|
|
@ -30,6 +31,7 @@ public:
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
QNetworkReply *makeRequest(const QUrl &url, PictureLoaderWorkerWork *workThread);
|
QNetworkReply *makeRequest(const QUrl &url, PictureLoaderWorkerWork *workThread);
|
||||||
|
void imageLoadedSuccessfully(CardInfoPtr card, const QImage &image);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static QStringList md5Blacklist;
|
static QStringList md5Blacklist;
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include "../../../game/cards/card_database_manager.h"
|
#include "../../../game/cards/card_database_manager.h"
|
||||||
#include "../../../settings/cache_settings.h"
|
#include "../../../settings/cache_settings.h"
|
||||||
|
#include "picture_loader_worker.h"
|
||||||
|
|
||||||
#include <QBuffer>
|
#include <QBuffer>
|
||||||
#include <QDirIterator>
|
#include <QDirIterator>
|
||||||
|
|
@ -16,13 +17,13 @@ Q_LOGGING_CATEGORY(PictureLoaderWorkerWorkLog, "picture_loader.worker");
|
||||||
// Card back returned by gatherer when card is not found
|
// Card back returned by gatherer when card is not found
|
||||||
QStringList PictureLoaderWorkerWork::md5Blacklist = QStringList() << "db0c48db407a907c16ade38de048a441";
|
QStringList PictureLoaderWorkerWork::md5Blacklist = QStringList() << "db0c48db407a907c16ade38de048a441";
|
||||||
|
|
||||||
PictureLoaderWorkerWork::PictureLoaderWorkerWork(PictureLoaderWorker *_worker, CardInfoPtr toLoad)
|
PictureLoaderWorkerWork::PictureLoaderWorkerWork(PictureLoaderWorker *_worker, const CardInfoPtr &toLoad)
|
||||||
: QThread(nullptr), worker(_worker), cardToDownload(toLoad)
|
: QThread(nullptr), worker(_worker), cardToDownload(toLoad)
|
||||||
{
|
{
|
||||||
connect(this, SIGNAL(startLoadQueue()), this, SLOT(processLoadQueue()), Qt::QueuedConnection);
|
connect(this, &PictureLoaderWorkerWork::requestImageDownload, worker, &PictureLoaderWorker::makeRequest,
|
||||||
|
Qt::QueuedConnection);
|
||||||
connect(networkManager, SIGNAL(finished(QNetworkReply *)), this, SLOT(picDownloadFinished(QNetworkReply *)));
|
connect(this, &PictureLoaderWorkerWork::imageLoaded, worker, &PictureLoaderWorker::imageLoadedSuccessfully,
|
||||||
|
Qt::QueuedConnection);
|
||||||
pictureLoaderThread = new QThread;
|
pictureLoaderThread = new QThread;
|
||||||
pictureLoaderThread->start(QThread::LowPriority);
|
pictureLoaderThread->start(QThread::LowPriority);
|
||||||
moveToThread(pictureLoaderThread);
|
moveToThread(pictureLoaderThread);
|
||||||
|
|
|
||||||
|
|
@ -21,18 +21,18 @@ class PictureLoaderWorkerWork : public QThread
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
explicit PictureLoaderWorkerWork(PictureLoaderWorker *worker, CardInfoPtr toLoad);
|
explicit PictureLoaderWorkerWork(PictureLoaderWorker *worker, const CardInfoPtr &toLoad);
|
||||||
~PictureLoaderWorkerWork() override;
|
~PictureLoaderWorkerWork() override;
|
||||||
|
PictureLoaderWorker *worker;
|
||||||
|
PictureToLoad cardToDownload;
|
||||||
public slots:
|
public slots:
|
||||||
void picDownloadFinished(QNetworkReply *reply);
|
void picDownloadFinished(QNetworkReply *reply);
|
||||||
void picDownloadFailed();
|
void picDownloadFailed();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static QStringList md5Blacklist;
|
static QStringList md5Blacklist;
|
||||||
PictureLoaderWorker *worker;
|
|
||||||
QThread *pictureLoaderThread;
|
QThread *pictureLoaderThread;
|
||||||
QNetworkAccessManager *networkManager;
|
QNetworkAccessManager *networkManager;
|
||||||
PictureToLoad cardToDownload;
|
|
||||||
bool picDownload, downloadRunning, loadQueueRunning;
|
bool picDownload, downloadRunning, loadQueueRunning;
|
||||||
void startNextPicDownload();
|
void startNextPicDownload();
|
||||||
bool cardImageExistsOnDisk(QString &setName, QString &correctedCardName);
|
bool cardImageExistsOnDisk(QString &setName, QString &correctedCardName);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue