mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-16 18:58:00 +00:00
fix downloaded filenames for cards with multiple default URLs
This commit is contained in:
@@ -19,7 +19,7 @@ final class ImageLoader extends CacheLoader<String, BufferedImage> {
|
||||
@Override
|
||||
public BufferedImage load(String key) {
|
||||
final String path;
|
||||
final String filename;
|
||||
String filename;
|
||||
if (key.startsWith(ImageCache.TOKEN_PREFIX)) {
|
||||
filename = key.substring(ImageCache.TOKEN_PREFIX.length());
|
||||
path = NewConstants.CACHE_TOKEN_PICS_DIR;
|
||||
@@ -49,9 +49,15 @@ final class ImageLoader extends CacheLoader<String, BufferedImage> {
|
||||
|
||||
// try without set prefix
|
||||
if (null == ret && filename.contains("/")) {
|
||||
ret = _findFile(key, path, filename.substring(filename.indexOf('/') + 1));
|
||||
filename = filename.substring(filename.indexOf('/') + 1);
|
||||
ret = _findFile(key, path, filename);
|
||||
}
|
||||
|
||||
// // try lowering the art index to 0 for regular cards
|
||||
// if (null == ret && filename.contains(".full.")) {
|
||||
// ret = _findFile(key, path, filename.substring(filename.indexOf('/') + 1));
|
||||
// }
|
||||
|
||||
if (null == ret) {
|
||||
System.out.println("File not found, no image created: " + key);
|
||||
}
|
||||
@@ -59,24 +65,18 @@ final class ImageLoader extends CacheLoader<String, BufferedImage> {
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static BufferedImage getImage(final File file) {
|
||||
BufferedImage image = null;;
|
||||
try {
|
||||
image = ImageIO.read(file);
|
||||
} catch (IOException ex) {
|
||||
BugReporter.reportException(ex, "Could not read image file " + file.getAbsolutePath() + " ");
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
private static BufferedImage _findFile(String key, String path, String filename) {
|
||||
for (String ext : _FILE_EXTENSIONS) {
|
||||
File file = new File(path, filename + ext);
|
||||
//System.out.println(String.format("Searching for %s at: %s", key, file.getAbsolutePath()));
|
||||
if (file.exists()) {
|
||||
//System.out.println(String.format("Found %s at: %s", key, file.getAbsolutePath()));
|
||||
return getImage(file);
|
||||
try {
|
||||
return ImageIO.read(file);
|
||||
} catch (IOException ex) {
|
||||
BugReporter.reportException(ex, "Could not read image file " + file.getAbsolutePath() + " ");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -236,7 +236,7 @@ public class CardFactory {
|
||||
}
|
||||
else if (c.isDoubleFaced() && cp instanceof CardPrinted) {
|
||||
c.setState(CardCharacteristicName.Transformed);
|
||||
c.setImageFilename(((CardPrinted)cp).getBackFaceImageFilename());
|
||||
c.setImageFilename(((CardPrinted)cp).getImageFilename(true));
|
||||
}
|
||||
else if (c.getRules().getSplitType() == CardSplitType.Split) {
|
||||
c.setState(CardCharacteristicName.LeftSplit);
|
||||
|
||||
@@ -60,7 +60,7 @@ public class GameNew {
|
||||
if (preferences.getPrefBoolean(FPref.UI_RANDOM_CARD_ART)) {
|
||||
final int cntVariants = cardPrinted.getRules().getEditionInfo(cardPrinted.getEdition()).getCopiesCount();
|
||||
if (cntVariants > 1) {
|
||||
card.setImageFilename(cardPrinted.getImageFilename(generator.nextInt(cntVariants)));
|
||||
card.setImageFilename(cardPrinted.getImageFilename(false, generator.nextInt(cntVariants), true));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -41,13 +41,8 @@ public class GuiDownloadPicturesLQ extends GuiDownloader {
|
||||
Set<String> filenames = new HashSet<String>();
|
||||
|
||||
for (final CardPrinted c : CardDb.instance().getUniqueCards()) {
|
||||
CardRules cardRules = c.getRules();
|
||||
addDLObject(cardRules.getPictureUrl(), c.getImageFilename(), downloads, filenames);
|
||||
|
||||
String backFaceImage = c.getBackFaceImageFilename();
|
||||
if (backFaceImage != null) {
|
||||
addDLObject(cardRules.getPictureOtherSideUrl(), backFaceImage, downloads, filenames);
|
||||
}
|
||||
addDLObject(c, false, downloads, filenames);
|
||||
addDLObject(c, true, downloads, filenames);
|
||||
}
|
||||
|
||||
// Add missing tokens to the list of things to download.
|
||||
@@ -60,20 +55,27 @@ public class GuiDownloadPicturesLQ extends GuiDownloader {
|
||||
return downloads;
|
||||
}
|
||||
|
||||
private void addDLObject(String url, String filename, ArrayList<DownloadObject> downloads, Set<String> filenames) {
|
||||
if (StringUtils.isEmpty(url) || filenames.contains(filename)) {
|
||||
private void addDLObject(CardPrinted c, boolean backFace, ArrayList<DownloadObject> downloads, Set<String> filenames) {
|
||||
CardRules cardRules = c.getRules();
|
||||
String urls = backFace ? cardRules.getPictureOtherSideUrl() : cardRules.getPictureUrl();
|
||||
if (StringUtils.isEmpty(urls)) {
|
||||
return;
|
||||
}
|
||||
filenames.add(filename);
|
||||
|
||||
// remove set path prefix from card filename
|
||||
if (filename.contains("/")) {
|
||||
filename = filename.substring(filename.indexOf('/') + 1);
|
||||
int artIdx = -1;
|
||||
for (String url : urls.split("\\\\")) {
|
||||
++artIdx;
|
||||
|
||||
String filename = c.getImageFilename(backFace, artIdx, false);
|
||||
if (filenames.contains(filename)) {
|
||||
continue;
|
||||
}
|
||||
filenames.add(filename);
|
||||
|
||||
File destFile = new File(NewConstants.CACHE_CARD_PICS_DIR, filename + ".jpg");
|
||||
if (!destFile.exists()) {
|
||||
downloads.add(new DownloadObject(url, destFile));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ public class GuiDownloadSetPicturesLQ extends GuiDownloader {
|
||||
|
||||
addDLObject(c.getImageUrlPath(false), c.getImageFilename(), downloads);
|
||||
|
||||
String backFaceImage = c.getBackFaceImageFilename();
|
||||
String backFaceImage = c.getImageFilename(true);
|
||||
if (backFaceImage != null) {
|
||||
addDLObject(c.getImageUrlPath(true), backFaceImage, downloads);
|
||||
}
|
||||
|
||||
@@ -362,7 +362,7 @@ public abstract class GuiDownloader extends DefaultBoundedRangeModel implements
|
||||
Log.error("LQ Pictures", "Error downloading pictures", ex);
|
||||
}
|
||||
|
||||
// throttle -- why?
|
||||
// throttle to reduce load on the server
|
||||
try {
|
||||
Thread.sleep(r.nextInt(250) + 250);
|
||||
} catch (final InterruptedException e) {
|
||||
@@ -370,21 +370,10 @@ public abstract class GuiDownloader extends DefaultBoundedRangeModel implements
|
||||
}
|
||||
} // for
|
||||
}
|
||||
} // run
|
||||
}
|
||||
|
||||
protected abstract ArrayList<DownloadObject> getNeededImages();
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* readFile.
|
||||
* </p>
|
||||
*
|
||||
* @param urlsFile
|
||||
* a {@link java.lang.String} object.
|
||||
* @param dir
|
||||
* a {@link java.util.File} object.
|
||||
* @return an array of {@link forge.gui.download.GuiDownloader.DownloadObject} objects.
|
||||
*/
|
||||
protected static List<DownloadObject> readFile(final String urlsFile, String dir) {
|
||||
List<String> fileLines = FileUtil.readFile(urlsFile);
|
||||
final ArrayList<DownloadObject> list = new ArrayList<DownloadObject>();
|
||||
@@ -405,19 +394,8 @@ public abstract class GuiDownloader extends DefaultBoundedRangeModel implements
|
||||
list.add(new DownloadObject(line, new File(dir, replacer.matcher(last).replaceAll(" "))));
|
||||
}
|
||||
return list;
|
||||
} // readFile()
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* readFile.
|
||||
* </p>
|
||||
*
|
||||
* @param urlNamesFile
|
||||
* a {@link java.lang.String} object.
|
||||
* @param dir
|
||||
* a {@link java.util.File} object.
|
||||
* @return an array of {@link forge.gui.download.GuiDownloader.DownloadObject} objects.
|
||||
*/
|
||||
protected static ArrayList<DownloadObject> readFileWithNames(final String urlNamesFile, final String dir) {
|
||||
List<String> fileLines = FileUtil.readFile(urlNamesFile);
|
||||
final ArrayList<DownloadObject> list = new ArrayList<DownloadObject>();
|
||||
@@ -443,28 +421,10 @@ public abstract class GuiDownloader extends DefaultBoundedRangeModel implements
|
||||
protected class ProxyHandler implements ChangeListener {
|
||||
private final int type;
|
||||
|
||||
/**
|
||||
* Instantiates a new proxy handler.
|
||||
*
|
||||
* @param type
|
||||
* the type
|
||||
*/
|
||||
public ProxyHandler(final int type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.
|
||||
* ChangeEvent)
|
||||
*/
|
||||
/**
|
||||
* State changed.
|
||||
*
|
||||
* @param e
|
||||
* ChangeEvent
|
||||
*/
|
||||
@Override
|
||||
public final void stateChanged(final ChangeEvent e) {
|
||||
if (((AbstractButton) e.getSource()).isSelected()) {
|
||||
@@ -502,5 +462,5 @@ public abstract class GuiDownloader extends DefaultBoundedRangeModel implements
|
||||
public File getDestination() {
|
||||
return destination;
|
||||
}
|
||||
} // DownloadObject
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,26 +38,12 @@ import forge.card.CardInSet;
|
||||
import forge.card.CardRules;
|
||||
import forge.util.Aggregates;
|
||||
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* CardDb class.
|
||||
* </p>
|
||||
*
|
||||
* @author Forge
|
||||
* @version $Id: CardDb.java 9708 2011-08-09 19:34:12Z jendave $
|
||||
*/
|
||||
public final class CardDb {
|
||||
private static volatile CardDb commonCards = null; // 'volatile' keyword makes this working
|
||||
private static volatile CardDb variantCards = null; // 'volatile' keyword makes this working
|
||||
public final static String foilSuffix = " foil";
|
||||
private final static int foilSuffixLength = foilSuffix.length();
|
||||
|
||||
/**
|
||||
* Instance.
|
||||
*
|
||||
* @return the card db
|
||||
*/
|
||||
public static CardDb instance() {
|
||||
if (CardDb.commonCards == null) {
|
||||
throw new NullPointerException("CardDb has not yet been initialized, run setup() first");
|
||||
@@ -72,13 +58,6 @@ public final class CardDb {
|
||||
return CardDb.variantCards;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the up.
|
||||
*
|
||||
* @param list
|
||||
* the new up
|
||||
*/
|
||||
public static void setup(final Iterable<CardRules> list) {
|
||||
if (CardDb.commonCards != null) {
|
||||
throw new RuntimeException("CardDb has already been initialized, don't do it twice please");
|
||||
@@ -92,10 +71,6 @@ public final class CardDb {
|
||||
}
|
||||
}
|
||||
|
||||
// Here oracle cards
|
||||
// private final Map<String, CardRules> cards = new Hashtable<String,
|
||||
// CardRules>();
|
||||
|
||||
// Here are refs, get them by name
|
||||
private final Map<String, CardPrinted> uniqueCards;
|
||||
|
||||
@@ -154,10 +129,6 @@ public final class CardDb {
|
||||
|
||||
/**
|
||||
* Checks if is card supported.
|
||||
*
|
||||
* @param cardName0
|
||||
* the card name
|
||||
* @return true, if is card supported
|
||||
*/
|
||||
public CardPrinted tryGetCard(final String cardName0) {
|
||||
if (null == cardName0) {
|
||||
@@ -190,42 +161,15 @@ public final class CardDb {
|
||||
}
|
||||
|
||||
// Single fetch
|
||||
/**
|
||||
* Gets the card.
|
||||
*
|
||||
* @param name
|
||||
* the name
|
||||
* @return the card
|
||||
*/
|
||||
public CardPrinted getCard(final String name) {
|
||||
return this.getCard(name, false);
|
||||
}
|
||||
|
||||
// Advanced fetch by name+set
|
||||
/**
|
||||
* Gets the card.
|
||||
*
|
||||
* @param name
|
||||
* the name
|
||||
* @param set
|
||||
* the set
|
||||
* @return the card
|
||||
*/
|
||||
public CardPrinted getCard(final String name, final String set) {
|
||||
return this.getCard(name, set, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the card.
|
||||
*
|
||||
* @param name
|
||||
* the name
|
||||
* @param set
|
||||
* the set
|
||||
* @param artIndex
|
||||
* the art index
|
||||
* @return the card
|
||||
*/
|
||||
public CardPrinted getCard(final String name, final String set, final int artIndex) {
|
||||
// 1. get set
|
||||
final Map<String, CardPrinted[]> cardsFromset = this.allCardsBySet.get(set.toUpperCase());
|
||||
@@ -252,13 +196,6 @@ public final class CardDb {
|
||||
|
||||
// Fetch from Forge's Card instance. Well, there should be no errors, but
|
||||
// we'll still check
|
||||
/**
|
||||
* Gets the card.
|
||||
*
|
||||
* @param forgeCard
|
||||
* the forge card
|
||||
* @return the card
|
||||
*/
|
||||
public static CardPrinted getCard(final Card forgeCard) {
|
||||
final String name = forgeCard.getName();
|
||||
final String set = forgeCard.getCurSetCode();
|
||||
@@ -273,24 +210,10 @@ public final class CardDb {
|
||||
}
|
||||
|
||||
// returns a list of all cards from their respective latest editions
|
||||
/**
|
||||
* Gets the all unique cards.
|
||||
*
|
||||
* @return the all unique cards
|
||||
*/
|
||||
public Collection<CardPrinted> getUniqueCards() {
|
||||
return this.uniqueCards.values();
|
||||
}
|
||||
|
||||
|
||||
|
||||
// public Iterable<CardRules> getAllCardRules() { return cards.values(); }
|
||||
// // still not needed
|
||||
/**
|
||||
* Gets the all cards.
|
||||
*
|
||||
* @return the all cards
|
||||
*/
|
||||
public List<CardPrinted> getAllCards() {
|
||||
return this.allCardsFlat;
|
||||
}
|
||||
@@ -300,14 +223,6 @@ public final class CardDb {
|
||||
return Lists.newArrayList(Iterables.filter(this.allCardsFlat, predicate));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the card.
|
||||
*
|
||||
* @param name0 the name0
|
||||
* @param fromLatestSet the from latest set
|
||||
* @return the card
|
||||
*/
|
||||
public CardPrinted getCard(final String name0, final boolean fromLatestSet) {
|
||||
// Sometimes they read from decks things like "CardName|Set" - but we
|
||||
// can handle it
|
||||
@@ -344,10 +259,6 @@ public final class CardDb {
|
||||
}
|
||||
|
||||
private static class CardSorter{
|
||||
// Here oracle cards
|
||||
// private final Map<String, CardRules> cards = new Hashtable<String,
|
||||
// CardRules>();
|
||||
|
||||
// need this to obtain cardReference by name+set+artindex
|
||||
public final Map<String, Map<String, CardPrinted[]>> allCommonCardsBySet = new TreeMap<String, Map<String, CardPrinted[]>>(String.CASE_INSENSITIVE_ORDER);
|
||||
public final Map<String, Map<String, CardPrinted[]>> allSpecialCardsBySet = new TreeMap<String, Map<String, CardPrinted[]>>(String.CASE_INSENSITIVE_ORDER);
|
||||
@@ -358,17 +269,6 @@ public final class CardDb {
|
||||
public final List<CardPrinted> allCommonCardsFlat = new ArrayList<CardPrinted>();
|
||||
public final List<CardPrinted> allSpecialCardsFlat = new ArrayList<CardPrinted>();
|
||||
|
||||
/**
|
||||
* Adds the to lists.
|
||||
*
|
||||
* @param card
|
||||
* the card
|
||||
* @param cardName
|
||||
* the card name
|
||||
* @param s
|
||||
* the s
|
||||
* @return the card printed
|
||||
*/
|
||||
public CardPrinted addToLists(final CardRules card, final String cardName, final String set, CardInSet cs) {
|
||||
CardPrinted lastAdded = null;
|
||||
|
||||
@@ -396,12 +296,6 @@ public final class CardDb {
|
||||
return lastAdded;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the new card.
|
||||
*
|
||||
* @param card
|
||||
* the card
|
||||
*/
|
||||
private void addNewCard(final CardRules card) {
|
||||
if (null == card) {
|
||||
return;
|
||||
|
||||
@@ -27,7 +27,6 @@ import forge.Singletons;
|
||||
import forge.card.CardRarity;
|
||||
import forge.card.CardRules;
|
||||
import forge.card.CardSplitType;
|
||||
import forge.card.ICardFace;
|
||||
import forge.card.cardfactory.CardFactory;
|
||||
import forge.game.player.Player;
|
||||
import forge.util.Base64Coder;
|
||||
@@ -134,43 +133,67 @@ public final class CardPrinted implements Comparable<IPaperCard>, InventoryItemF
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
private String getImageName() {
|
||||
return CardSplitType.Split != card.getSplitType() ? name : card.getMainPart().getName() + card.getOtherPart().getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getImageFilename() {
|
||||
return getImageFilename(getArtIndex());
|
||||
return getImageLocator(getImageName(), getArtIndex(), true, false);
|
||||
}
|
||||
|
||||
public String getImageFilename(boolean backFace) {
|
||||
return getImageFilename(backFace, getArtIndex(), true);
|
||||
}
|
||||
|
||||
public String getImageFilename(boolean backFace, int artIdx, boolean includeSet) {
|
||||
final String nameToUse;
|
||||
if (backFace) {
|
||||
if (null == card.getOtherPart()) {
|
||||
return null;
|
||||
}
|
||||
switch (card.getSplitType()) {
|
||||
case Transform: case Flip: case Licid:
|
||||
break;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
nameToUse = card.getOtherPart().getName();
|
||||
} else {
|
||||
nameToUse = getImageName();
|
||||
}
|
||||
|
||||
return getImageLocator(nameToUse, artIdx, includeSet, false);
|
||||
}
|
||||
|
||||
public String getImageUrlPath(boolean backFace) {
|
||||
return getImageLocator(backFace ? card.getOtherPart().getName() : name, getArtIndex(), true);
|
||||
return getImageLocator(backFace ? card.getOtherPart().getName() : getImageName(), getArtIndex(), true, true);
|
||||
}
|
||||
|
||||
public String getImageFilename(int artIdx) {
|
||||
return getImageLocator(getArtIndex(), false);
|
||||
}
|
||||
private String getImageLocator(String nameToUse, int artIdx, boolean includeSet, boolean base64encode) {
|
||||
StringBuilder s = new StringBuilder();
|
||||
|
||||
s.append(toMWSFilename(nameToUse));
|
||||
|
||||
private String getImageLocator(String nameToUse, int artIdx, boolean base64encode) {
|
||||
int cntPictures = card.getEditionInfo(edition).getCopiesCount();
|
||||
nameToUse = toMWSFilename(nameToUse);
|
||||
if (cntPictures > 1 && cntPictures > artIdx) {
|
||||
nameToUse += String.valueOf(artIdx + 1);
|
||||
s.append(artIdx + 1);
|
||||
}
|
||||
nameToUse += ".full";
|
||||
return String.format("%s/%s",
|
||||
Singletons.getModel().getEditions().getCode2ByCode(edition),
|
||||
base64encode ? Base64Coder.encodeString(nameToUse + ".jpg", true) : nameToUse);
|
||||
s.append(".full");
|
||||
|
||||
final String fname;
|
||||
if (base64encode) {
|
||||
s.append(".jpg");
|
||||
fname = Base64Coder.encodeString(s.toString(), true);
|
||||
} else {
|
||||
fname = s.toString();
|
||||
}
|
||||
|
||||
private String getImageLocator(int artIdx, boolean base64encode) {
|
||||
return getImageLocator(CardSplitType.Split == card.getSplitType() ? card.getMainPart().getName() + card.getOtherPart().getName() : name,
|
||||
artIdx, base64encode);
|
||||
if (includeSet) {
|
||||
return String.format("%s/%s", Singletons.getModel().getEditions().getCode2ByCode(edition), fname);
|
||||
} else {
|
||||
return fname;
|
||||
}
|
||||
|
||||
public String getBackFaceImageFilename() {
|
||||
ICardFace backFace = card.getOtherPart();
|
||||
if (null == backFace) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return getImageLocator(card.getOtherPart().getName(), getArtIndex(), false);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -29,7 +29,6 @@ import javax.swing.JLabel;
|
||||
import javax.swing.JProgressBar;
|
||||
import javax.swing.SwingWorker;
|
||||
|
||||
import forge.gui.GuiDisplayUtil;
|
||||
import forge.properties.NewConstants;
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user