From b23374382593b7b3b407b3c3afcfdc798df9a42d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Basile=20Cl=C3=A9ment?= Date: Fri, 21 Mar 2025 09:33:14 +0100 Subject: [PATCH] Try to better reproduce pre-provider ID behavior If "override all card art with personal preference" setting is set, look for custom art for all sets instead of just the most preferred set. --- .../picture_loader/picture_loader_worker.cpp | 58 +++++++++++++------ .../ui/picture_loader/picture_loader_worker.h | 11 +++- cockatrice/src/settings/cache_settings.cpp | 2 +- cockatrice/src/settings/cache_settings.h | 2 +- 4 files changed, 53 insertions(+), 20 deletions(-) diff --git a/cockatrice/src/client/ui/picture_loader/picture_loader_worker.cpp b/cockatrice/src/client/ui/picture_loader/picture_loader_worker.cpp index 9d79d1a36..09facc5a7 100644 --- a/cockatrice/src/client/ui/picture_loader/picture_loader_worker.cpp +++ b/cockatrice/src/client/ui/picture_loader/picture_loader_worker.cpp @@ -16,11 +16,14 @@ QStringList PictureLoaderWorker::md5Blacklist = QStringList() << "db0c48db407a90 PictureLoaderWorker::PictureLoaderWorker() : QObject(nullptr), picsPath(SettingsCache::instance().getPicsPath()), customPicsPath(SettingsCache::instance().getCustomPicsPath()), - picDownload(SettingsCache::instance().getPicDownload()), downloadRunning(false), loadQueueRunning(false) + picDownload(SettingsCache::instance().getPicDownload()), downloadRunning(false), loadQueueRunning(false), + overrideAllCardArtWithPersonalPreference(SettingsCache::instance().getOverrideAllCardArtWithPersonalPreference()) { connect(this, SIGNAL(startLoadQueue()), this, SLOT(processLoadQueue()), Qt::QueuedConnection); connect(&SettingsCache::instance(), SIGNAL(picsPathChanged()), this, SLOT(picsPathChanged())); connect(&SettingsCache::instance(), SIGNAL(picDownloadChanged()), this, SLOT(picDownloadChanged())); + connect(&SettingsCache::instance(), &SettingsCache::overrideAllCardArtWithPersonalPreferenceChanged, this, + &PictureLoaderWorker::setOverrideAllCardArtWithPersonalPreference); networkManager = new QNetworkAccessManager(this); // We need a timeout to ensure requests don't hang indefinitely in case of @@ -83,11 +86,24 @@ void PictureLoaderWorker::processLoadQueue() qCDebug(PictureLoaderWorkerLog).nospace() << "[card: " << cardName << " set: " << setName << "]: Trying to load picture"; - if (CardDatabaseManager::getInstance()->isProviderIdForPreferredPrinting( - cardName, cardBeingLoaded.getCard()->getPixmapCacheKey())) { - if (cardImageExistsOnDisk(setName, correctedCardName)) { - continue; - } + // FIXME: This is a hack so that to keep old Cockatrice behavior + // (ignoring provider ID) when the "override all card art with personal + // preference" is set. + // + // Figure out a proper way to integrate the two systems at some point. + // + // Note: need to go through a member for + // overrideAllCardArtWithPersonalPreference as reading from the + // SettingsCache instance from the PictureLoaderWorker thread could + // cause race conditions. + // + // XXX: Reading from the CardDatabaseManager instance from the + // PictureLoaderWorker thread might not be safe either + bool searchCustomPics = overrideAllCardArtWithPersonalPreference || + CardDatabaseManager::getInstance()->isProviderIdForPreferredPrinting( + cardName, cardBeingLoaded.getCard()->getPixmapCacheKey()); + if (searchCustomPics && cardImageExistsOnDisk(setName, correctedCardName, searchCustomPics)) { + continue; } qCDebug(PictureLoaderWorkerLog).nospace() @@ -100,23 +116,26 @@ void PictureLoaderWorker::processLoadQueue() } } -bool PictureLoaderWorker::cardImageExistsOnDisk(QString &setName, QString &correctedCardname) +bool PictureLoaderWorker::cardImageExistsOnDisk(QString &setName, QString &correctedCardname, bool searchCustomPics) { QImage image; QImageReader imgReader; imgReader.setDecideFormatFromContent(true); QList picsPaths = QList(); - QDirIterator it(customPicsPath, QDirIterator::Subdirectories | QDirIterator::FollowSymlinks); - // Recursively check all subdirectories of the CUSTOM folder - while (it.hasNext()) { - QString thisPath(it.next()); - QFileInfo thisFileInfo(thisPath); + if (searchCustomPics) { + QDirIterator it(customPicsPath, QDirIterator::Subdirectories | QDirIterator::FollowSymlinks); - if (thisFileInfo.isFile() && - (thisFileInfo.fileName() == correctedCardname || thisFileInfo.completeBaseName() == correctedCardname || - thisFileInfo.baseName() == correctedCardname)) { - picsPaths << thisPath; // Card found in the CUSTOM directory, somewhere + // Recursively check all subdirectories of the CUSTOM folder + while (it.hasNext()) { + QString thisPath(it.next()); + QFileInfo thisFileInfo(thisPath); + + if (thisFileInfo.isFile() && + (thisFileInfo.fileName() == correctedCardname || thisFileInfo.completeBaseName() == correctedCardname || + thisFileInfo.baseName() == correctedCardname)) { + picsPaths << thisPath; // Card found in the CUSTOM directory, somewhere + } } } @@ -451,7 +470,12 @@ void PictureLoaderWorker::picsPathChanged() customPicsPath = SettingsCache::instance().getCustomPicsPath(); } +void PictureLoaderWorker::setOverrideAllCardArtWithPersonalPreference(bool _overrideAllCardArtWithPersonalPreference) +{ + overrideAllCardArtWithPersonalPreference = _overrideAllCardArtWithPersonalPreference; +} + void PictureLoaderWorker::clearNetworkCache() { networkManager->cache()->clear(); -} \ No newline at end of file +} diff --git a/cockatrice/src/client/ui/picture_loader/picture_loader_worker.h b/cockatrice/src/client/ui/picture_loader/picture_loader_worker.h index 823bcd7ec..e85ede8f1 100644 --- a/cockatrice/src/client/ui/picture_loader/picture_loader_worker.h +++ b/cockatrice/src/client/ui/picture_loader/picture_loader_worker.h @@ -42,8 +42,16 @@ private: PictureToLoad cardBeingLoaded; PictureToLoad cardBeingDownloaded; bool picDownload, downloadRunning, loadQueueRunning; + bool overrideAllCardArtWithPersonalPreference; void startNextPicDownload(); - bool cardImageExistsOnDisk(QString &setName, QString &correctedCardName); + + /** Emit the `imageLoaded` signal and return `true` if a picture is found on + disk, return `false` otherwise. + + If `searchCustomPics` is `true`, the CUSTOM folder is searched for a + matching image first; otherwise, only the set-based folders are used. */ + bool cardImageExistsOnDisk(QString &setName, QString &correctedCardName, bool searchCustomPics); + bool imageIsBlackListed(const QByteArray &); QNetworkReply *makeRequest(const QUrl &url); void cacheRedirect(const QUrl &originalUrl, const QUrl &redirectUrl); @@ -58,6 +66,7 @@ private slots: void picDownloadChanged(); void picsPathChanged(); + void setOverrideAllCardArtWithPersonalPreference(bool _overrideAllCardArtWithPersonalPreference); public slots: void processLoadQueue(); diff --git a/cockatrice/src/settings/cache_settings.cpp b/cockatrice/src/settings/cache_settings.cpp index 87215aefb..5cec81cf7 100644 --- a/cockatrice/src/settings/cache_settings.cpp +++ b/cockatrice/src/settings/cache_settings.cpp @@ -632,7 +632,7 @@ void SettingsCache::setOverrideAllCardArtWithPersonalPreference(QT_STATE_CHANGED { overrideAllCardArtWithPersonalPreference = static_cast(_overrideAllCardArt); settings->setValue("cards/overrideallcardartwithpersonalpreference", overrideAllCardArtWithPersonalPreference); - emit overrideAllCardArtWithPersonalPreferenceChanged(); + emit overrideAllCardArtWithPersonalPreferenceChanged(overrideAllCardArtWithPersonalPreference); } void SettingsCache::setBumpSetsWithCardsInDeckToTop(QT_STATE_CHANGED_T _bumpSetsWithCardsInDeckToTop) diff --git a/cockatrice/src/settings/cache_settings.h b/cockatrice/src/settings/cache_settings.h index c33efad6d..f4c00acdc 100644 --- a/cockatrice/src/settings/cache_settings.h +++ b/cockatrice/src/settings/cache_settings.h @@ -55,7 +55,7 @@ signals: void themeChanged(); void picDownloadChanged(); void displayCardNamesChanged(); - void overrideAllCardArtWithPersonalPreferenceChanged(); + void overrideAllCardArtWithPersonalPreferenceChanged(bool _overrideAllCardArtWithPersonalPreference); void bumpSetsWithCardsInDeckToTopChanged(); void printingSelectorSortOrderChanged(); void printingSelectorCardSizeChanged();