mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-17 03:08:02 +00:00
[Desktop] Make ImageFetcher default to fetching card images from magiccards.info.
This commit is contained in:
@@ -246,6 +246,18 @@ public final class CardDb implements ICardDatabase, IDeckGenPool {
|
|||||||
return tryGetCard(request);
|
return tryGetCard(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getCardCollectorNumber(String cardName, String reqEdition) {
|
||||||
|
CardEdition edition = editions.get(reqEdition);
|
||||||
|
if (edition == null)
|
||||||
|
return -1;
|
||||||
|
for (CardInSet card : edition.getCards()) {
|
||||||
|
if (card.name.equalsIgnoreCase(cardName)) {
|
||||||
|
return card.collectorNumber;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
private PaperCard tryGetCard(CardRequest request) {
|
private PaperCard tryGetCard(CardRequest request) {
|
||||||
Collection<PaperCard> cards = allCardsByName.get(request.cardName);
|
Collection<PaperCard> cards = allCardsByName.get(request.cardName);
|
||||||
if (cards == null) { return null; }
|
if (cards == null) { return null; }
|
||||||
|
|||||||
@@ -29,15 +29,36 @@ public class ImageFetcher {
|
|||||||
FThreads.assertExecutedByEdt(true);
|
FThreads.assertExecutedByEdt(true);
|
||||||
|
|
||||||
final String prefix = imageKey.substring(0, 2);
|
final String prefix = imageKey.substring(0, 2);
|
||||||
String[] result = null;
|
final File destFile;
|
||||||
|
final String urlToDownload;
|
||||||
if (prefix.equals(ImageKeys.CARD_PREFIX)) {
|
if (prefix.equals(ImageKeys.CARD_PREFIX)) {
|
||||||
PaperCard paperCard = ImageUtil.getPaperCardFromImageKey(imageKey);
|
PaperCard paperCard = ImageUtil.getPaperCardFromImageKey(imageKey);
|
||||||
if (paperCard == null) {
|
if (paperCard == null) {
|
||||||
System.err.println("Paper card not found for: " + imageKey);
|
System.err.println("Paper card not found for: " + imageKey);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
boolean backFace = imageKey.endsWith(ImageKeys.BACKFACE_POSTFIX);
|
final boolean backFace = imageKey.endsWith(ImageKeys.BACKFACE_POSTFIX);
|
||||||
result = ImageUtil.getDownloadUrlAndDestination(ForgeConstants.CACHE_CARD_PICS_DIR, paperCard, backFace);
|
final String filename = ImageUtil.getImageKey(paperCard, backFace, false);
|
||||||
|
destFile = new File(ForgeConstants.CACHE_CARD_PICS_DIR, filename + ".jpg");
|
||||||
|
|
||||||
|
// First, try to fetch from magiccards.info, if we have the collector's number to generate a URL.
|
||||||
|
final StaticData data = StaticData.instance();
|
||||||
|
final int cardNum = data.getCommonCards().getCardCollectorNumber(paperCard.getName(), paperCard.getEdition());
|
||||||
|
if (cardNum != -1) {
|
||||||
|
final String setCode = data.getEditions().getCode2ByCode(paperCard.getEdition()).toLowerCase();
|
||||||
|
String suffix = "";
|
||||||
|
if (paperCard.getRules().getOtherPart() != null) {
|
||||||
|
suffix = (backFace ? "b" : "a");
|
||||||
|
}
|
||||||
|
urlToDownload = String.format("http://magiccards.info/scans/en/%s/%d%s.jpg", setCode, cardNum, suffix);
|
||||||
|
} else {
|
||||||
|
// Fall back to using Forge's LQ card downloaded from Wizards' website. This currently only works for older cards.
|
||||||
|
String[] result = ImageUtil.getDownloadUrlAndDestination(ForgeConstants.CACHE_CARD_PICS_DIR, paperCard, backFace);
|
||||||
|
if (result == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
urlToDownload = result[0];
|
||||||
|
}
|
||||||
} else if (prefix.equals(ImageKeys.TOKEN_PREFIX)) {
|
} else if (prefix.equals(ImageKeys.TOKEN_PREFIX)) {
|
||||||
if (tokenImages == null) {
|
if (tokenImages == null) {
|
||||||
tokenImages = new HashMap<>();
|
tokenImages = new HashMap<>();
|
||||||
@@ -45,23 +66,18 @@ public class ImageFetcher {
|
|||||||
tokenImages.put(nameUrlPair.getLeft(), nameUrlPair.getRight());
|
tokenImages.put(nameUrlPair.getLeft(), nameUrlPair.getRight());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
String filename = imageKey.substring(2) + ".jpg";
|
final String filename = imageKey.substring(2) + ".jpg";
|
||||||
String url = tokenImages.get(filename);
|
urlToDownload = tokenImages.get(filename);
|
||||||
if (url == null) {
|
if (urlToDownload == null) {
|
||||||
System.err.println("Token " + imageKey + " not found in: " + ForgeConstants.IMAGE_LIST_TOKENS_FILE);
|
System.err.println("Token " + imageKey + " not found in: " + ForgeConstants.IMAGE_LIST_TOKENS_FILE);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
result = new String[] { url, new File(ForgeConstants.CACHE_TOKEN_PICS_DIR, filename).getAbsolutePath() };
|
destFile = new File(ForgeConstants.CACHE_TOKEN_PICS_DIR, filename);
|
||||||
} else {
|
} else {
|
||||||
System.err.println("Cannot fetch image for: " + imageKey);
|
System.err.println("Cannot fetch image for: " + imageKey);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
final String destPath = destFile.getAbsolutePath();
|
||||||
if (result == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
final String urlToDownload = result[0];
|
|
||||||
final String destPath = result[1];
|
|
||||||
|
|
||||||
// Note: No synchronization is needed here because this is executed on
|
// Note: No synchronization is needed here because this is executed on
|
||||||
// EDT thread (see assert on top) and so is the notification of observers.
|
// EDT thread (see assert on top) and so is the notification of observers.
|
||||||
@@ -94,11 +110,14 @@ public class ImageFetcher {
|
|||||||
System.out.println("Attempting to fetch: " + urlToDownload);
|
System.out.println("Attempting to fetch: " + urlToDownload);
|
||||||
URL url = new URL(urlToDownload);
|
URL url = new URL(urlToDownload);
|
||||||
BufferedImage image = ImageIO.read(url.openStream());
|
BufferedImage image = ImageIO.read(url.openStream());
|
||||||
File destFile = new File(destPath);
|
// First, save to a temporary file so that nothing tries to read
|
||||||
|
// a partial download.
|
||||||
|
File destFile = new File(destPath + ".tmp");
|
||||||
destFile.mkdirs();
|
destFile.mkdirs();
|
||||||
ImageIO.write(image, "jpg", destFile);
|
ImageIO.write(image, "jpg", destFile);
|
||||||
|
// Now, rename it to the correct name.
|
||||||
|
destFile.renameTo(new File(destPath));
|
||||||
System.out.println("Saved image to " + destFile);
|
System.out.println("Saved image to " + destFile);
|
||||||
|
|
||||||
SwingUtilities.invokeLater(notifyObservers);
|
SwingUtilities.invokeLater(notifyObservers);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("Failed to download card image: " + e.getMessage());
|
System.err.println("Failed to download card image: " + e.getMessage());
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
Code=LRW
|
Code=LRW
|
||||||
Date=2007-10-12
|
Date=2007-10-12
|
||||||
Name=Lorwyn
|
Name=Lorwyn
|
||||||
Code2=LRW
|
Code2=LW
|
||||||
Type=Expansion
|
Type=Expansion
|
||||||
BoosterCovers=5
|
BoosterCovers=5
|
||||||
Booster=11 Common, 3 Uncommon, 1 Rare
|
Booster=11 Common, 3 Uncommon, 1 Rare
|
||||||
|
|||||||
Reference in New Issue
Block a user