From c9f4e949fa102003a5ac9ec591a9e046d34224e4 Mon Sep 17 00:00:00 2001 From: Oleg Agafonov Date: Thu, 11 Feb 2021 08:12:55 +0400 Subject: [PATCH] * Images: added more download stats (now you can see a progress of prepare stage); --- .../plugins/card/dl/DownloadServiceInfo.java | 6 +++- .../card/dl/sources/ScryfallImageSource.java | 22 ++++++++++-- .../card/dl/sources/TokensMtgImageSource.java | 6 ++-- .../card/images/DownloadPicturesService.java | 35 +++++++++++++------ 4 files changed, 52 insertions(+), 17 deletions(-) diff --git a/Mage.Client/src/main/java/org/mage/plugins/card/dl/DownloadServiceInfo.java b/Mage.Client/src/main/java/org/mage/plugins/card/dl/DownloadServiceInfo.java index d125845bce..719d7d21e8 100644 --- a/Mage.Client/src/main/java/org/mage/plugins/card/dl/DownloadServiceInfo.java +++ b/Mage.Client/src/main/java/org/mage/plugins/card/dl/DownloadServiceInfo.java @@ -13,7 +13,11 @@ public interface DownloadServiceInfo { void incErrorCount(); - void updateMessage(String text); + void updateGlobalMessage(String text); + + void updateProgressMessage(String text); void showDownloadControls(boolean needToShow); + + Object getSync(); } diff --git a/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/ScryfallImageSource.java b/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/ScryfallImageSource.java index 38a347d5b8..d0b08c9985 100644 --- a/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/ScryfallImageSource.java +++ b/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/ScryfallImageSource.java @@ -145,6 +145,16 @@ public enum ScryfallImageSource implements CardImageSource { preparedUrls.clear(); + // prepare stats + int needPrepareCount = 0; + int currentPrepareCount = 0; + for (CardDownloadData card : downloadList) { + if (card.isTwoFacedCard() && card.isSecondSide()) { + needPrepareCount++; + } + } + updatePrepareStats(downloadServiceInfo, needPrepareCount, currentPrepareCount); + for (CardDownloadData card : downloadList) { // need cancel if (downloadServiceInfo.isNeedCancel()) { @@ -153,6 +163,7 @@ public enum ScryfallImageSource implements CardImageSource { // prepare the back face URL if (card.isTwoFacedCard() && card.isSecondSide()) { + currentPrepareCount++; final String defaultCode = CardLanguage.ENGLISH.getCode(); final String localizedCode = languageAliases.getOrDefault(this.getCurrentLanguage(), defaultCode); @@ -160,14 +171,14 @@ public enum ScryfallImageSource implements CardImageSource { try { url = getFaceImageUrl(proxy, card, card.isToken(), localizedCode); + preparedUrls.put(card, url); } catch (Exception e) { logger.warn("Failed to prepare image URL (back face) for " + card.getName() + " (" + card.getSet() + ") #" + card.getCollectorId() + ", Error Message: " + e.getMessage()); downloadServiceInfo.incErrorCount(); - continue; } - preparedUrls.put(card, url); + updatePrepareStats(downloadServiceInfo, needPrepareCount, currentPrepareCount); } // inc error count to stop on too many errors @@ -177,10 +188,15 @@ public enum ScryfallImageSource implements CardImageSource { return true; } + private void updatePrepareStats(DownloadServiceInfo service, int need, int current) { + synchronized (service.getSync()) { + service.updateProgressMessage(String.format("Preparing download list... %d of %d", current, need)); + } + } + @Override public CardImageUrls generateCardUrl(CardDownloadData card) throws Exception { return innerGenerateURL(card, false); - } @Override diff --git a/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/TokensMtgImageSource.java b/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/TokensMtgImageSource.java index b74e9ee11c..7592261f13 100644 --- a/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/TokensMtgImageSource.java +++ b/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/TokensMtgImageSource.java @@ -184,7 +184,7 @@ public enum TokensMtgImageSource implements CardImageSource { private HashMap> getTokensData() throws IOException { synchronized (tokensDataSync) { if (tokensData == null) { - DownloadPicturesService.getInstance().updateMessage("Find tokens data..."); + DownloadPicturesService.getInstance().updateGlobalMessage("Find tokens data..."); tokensData = new HashMap<>(); // get tokens data from resource file @@ -235,11 +235,11 @@ public enum TokensMtgImageSource implements CardImageSource { } } } - DownloadPicturesService.getInstance().updateMessage(""); + DownloadPicturesService.getInstance().updateGlobalMessage(""); DownloadPicturesService.getInstance().showDownloadControls(true); } catch (Exception ex) { LOGGER.warn("Failed to get tokens description from tokens.mtg.onl", ex); - DownloadPicturesService.getInstance().updateMessage(ex.getMessage()); + DownloadPicturesService.getInstance().updateGlobalMessage(ex.getMessage()); } } } diff --git a/Mage.Client/src/main/java/org/mage/plugins/card/images/DownloadPicturesService.java b/Mage.Client/src/main/java/org/mage/plugins/card/images/DownloadPicturesService.java index 16fcf6c613..8f19e3a250 100644 --- a/Mage.Client/src/main/java/org/mage/plugins/card/images/DownloadPicturesService.java +++ b/Mage.Client/src/main/java/org/mage/plugins/card/images/DownloadPicturesService.java @@ -124,6 +124,7 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements CardImageUtils.checkAndFixImageFiles(); } + @Override public boolean isNeedCancel() { return this.needCancel || (this.errorCount > MAX_ERRORS_COUNT_BEFORE_CANCEL); } @@ -132,6 +133,7 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements this.needCancel = needCancel; } + @Override public void incErrorCount() { this.errorCount = this.errorCount + 1; @@ -209,23 +211,23 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements } public void findMissingCards() { - updateMessage("Loading..."); + updateGlobalMessage("Loading..."); this.cardsAll.clear(); this.cardsMissing.clear(); this.cardsDownloadQueue.clear(); - updateMessage("Loading cards list..."); + updateGlobalMessage("Loading cards list..."); this.cardsAll = Collections.synchronizedList(CardRepository.instance.findCards(new CardCriteria())); - updateMessage("Finding missing images..."); + updateGlobalMessage("Finding missing images..."); this.cardsMissing = prepareMissingCards(this.cardsAll, uiDialog.getRedownloadCheckbox().isSelected()); - updateMessage("Finding available sets from selected source..."); + updateGlobalMessage("Finding available sets from selected source..."); this.uiDialog.getSetsCombo().setModel(new DefaultComboBoxModel<>(getSetsForCurrentImageSource())); reloadCardsToDownload(this.uiDialog.getSetsCombo().getSelectedItem().toString()); this.uiDialog.showDownloadControls(true); - updateMessage(""); + updateGlobalMessage(""); showDownloadControls(true); } @@ -251,10 +253,17 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements } } - public void updateMessage(String text) { + @Override + public void updateGlobalMessage(String text) { this.uiDialog.setGlobalInfo(text); } + @Override + public void updateProgressMessage(String text) { + this.uiDialog.getProgressBar().setString(text); + } + + @Override public void showDownloadControls(boolean needToShow) { // auto-size form on show this.uiDialog.showDownloadControls(needToShow); @@ -390,7 +399,7 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements uiDialog.setCurrentInfo("Missing: " + missingCardsCount + " card images / " + missingTokensCount + " token images"); int imageSum = cardCount + tokenCount; float mb = (imageSum * selectedSource.getAverageSize()) / 1024; - uiDialog.getProgressBar().setString(String.format( + updateProgressMessage(String.format( cardIndex == imageSum ? "%d of %d (%d cards/%d tokens) image downloads finished! Please close!" : "%d of %d (%d cards/%d tokens) image downloads finished! Please wait! [%.1f Mb]", @@ -631,7 +640,7 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements + " from source: " + selectedSource.getSourceName() + ", language: " + selectedSource.getCurrentLanguage().getCode() + ", threads: " + downloadThreadsAmount); - uiDialog.getProgressBar().setString("Preparing download list..."); + updateProgressMessage("Preparing download list..."); if (selectedSource.prepareDownloadList(this, cardsDownloadQueue)) { update(0, cardsDownloadQueue.size()); ExecutorService executor = Executors.newFixedThreadPool(downloadThreadsAmount); @@ -939,7 +948,7 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements if (cardIndex < needDownloadCount) { // downloading float mb = ((needDownloadCount - lastCardIndex) * selectedSource.getAverageSize()) / 1024; - uiDialog.getProgressBar().setString(String.format("%d of %d image downloads finished! Please wait! [%.1f Mb]", + updateProgressMessage(String.format("%d of %d image downloads finished! Please wait! [%.1f Mb]", lastCardIndex, needDownloadCount, mb)); } else { // finished @@ -957,7 +966,7 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements if (this.cardsDownloadQueue.isEmpty()) { // stop download - uiDialog.getProgressBar().setString("0 images remaining. Please close."); + updateProgressMessage("0 images remaining. Please close."); } else { // try download again } @@ -974,9 +983,15 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements uiDialog.getStartButton().setEnabled(true); } + @Override public Proxy getProxy() { return proxy; } + + @Override + public Object getSync() { + return sync; + } } class LoadMissingCardDataNew implements Runnable {