Added card frame downloader.

This commit is contained in:
LevelX2 2015-06-24 13:41:14 +02:00
parent 07a8474721
commit 72408a7f52
7 changed files with 246 additions and 49 deletions

View file

@ -27,6 +27,57 @@
*/
package mage.client;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.SplashScreen;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.beans.PropertyVetoException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.prefs.Preferences;
import javax.imageio.ImageIO;
import javax.swing.AbstractButton;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JToggleButton;
import javax.swing.JToolBar.Separator;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import mage.cards.decks.Deck;
import mage.cards.repository.CardCriteria;
import mage.cards.repository.CardInfo;
@ -42,7 +93,14 @@ import mage.client.components.tray.MageTray;
import mage.client.constants.Constants.DeckEditorMode;
import mage.client.deckeditor.DeckEditorPane;
import mage.client.deckeditor.collection.viewer.CollectionViewerPane;
import mage.client.dialog.*;
import mage.client.dialog.AboutDialog;
import mage.client.dialog.ConnectDialog;
import mage.client.dialog.ErrorDialog;
import mage.client.dialog.FeedbackDialog;
import mage.client.dialog.GameEndDialog;
import mage.client.dialog.PreferencesDialog;
import mage.client.dialog.TableWaitingDialog;
import mage.client.dialog.UserRequestDialog;
import mage.client.draft.DraftPane;
import mage.client.draft.DraftPanel;
import mage.client.game.GamePane;
@ -67,37 +125,16 @@ import mage.remote.Session;
import mage.remote.SessionImpl;
import mage.utils.MageVersion;
import mage.view.GameEndView;
import mage.view.UserRequestMessage;
import net.java.truevfs.access.TArchiveDetector;
import net.java.truevfs.access.TConfig;
import net.java.truevfs.kernel.spec.FsAccessOption;
import org.apache.log4j.Logger;
import org.mage.card.arcane.ManaSymbols;
import org.mage.plugins.card.constants.Constants;
import org.mage.plugins.card.images.DownloadPictures;
import org.mage.plugins.card.utils.impl.ImageManagerImpl;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.JToolBar.Separator;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.beans.PropertyVetoException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.prefs.Preferences;
import static mage.client.dialog.PreferencesDialog.KEY_CONNECT_FLAG;
import mage.view.UserRequestMessage;
import net.java.truevfs.access.TArchiveDetector;
import net.java.truevfs.access.TConfig;
import net.java.truevfs.kernel.spec.FsAccessOption;
/**
* @author BetaSteward_at_googlemail.com
*/
@ -521,7 +558,7 @@ public class MageFrame extends javax.swing.JFrame implements MageClient {
}
public void btnSymbolsActionPerformed(java.awt.event.ActionEvent evt) {
if (JOptionPane.showConfirmDialog(this, "Do you want to download mana symbols?") == JOptionPane.OK_OPTION) {
if (JOptionPane.showConfirmDialog(this, "Do you want to download game symbols and additional image files?", "Download additional resources", JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION) {
Plugins.getInstance().downloadSymbols();
}
}

View file

@ -1,5 +1,21 @@
package org.mage.plugins.card;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLayeredPane;
import mage.cards.MagePermanent;
import mage.cards.action.ActionCallback;
import mage.constants.Rarity;
@ -20,20 +36,13 @@ import org.mage.plugins.card.constants.Constants;
import org.mage.plugins.card.dl.DownloadGui;
import org.mage.plugins.card.dl.DownloadJob;
import org.mage.plugins.card.dl.Downloader;
import org.mage.plugins.card.dl.sources.CardFrames;
import org.mage.plugins.card.dl.sources.DirectLinksForDownload;
import org.mage.plugins.card.dl.sources.GathererSets;
import org.mage.plugins.card.dl.sources.GathererSymbols;
import org.mage.plugins.card.images.ImageCache;
import org.mage.plugins.card.info.CardInfoPaneImpl;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.util.*;
import java.util.List;
/**
* {@link CardPlugin} implementation.
*
@ -496,6 +505,11 @@ public class CardPluginImpl implements CardPlugin {
g.getDownloader().add(job);
}
it = new CardFrames(imagesPath);
for(DownloadJob job:it) {
g.getDownloader().add(job);
}
it = new DirectLinksForDownload(imagesPath);
for(DownloadJob job:it) {
g.getDownloader().add(job);

View file

@ -15,12 +15,11 @@ import java.io.OutputStream;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.BoundedRangeModel;
import javax.swing.DefaultBoundedRangeModel;
import org.mage.plugins.card.dl.beans.properties.Property;
import org.mage.plugins.card.dl.lm.AbstractLaternaBean;
import org.mage.plugins.card.utils.CardImageUtils;
/**
@ -86,9 +85,11 @@ public class DownloadJob extends AbstractLaternaBean {
*/
public void setError(String message, Exception error) {
if (message == null) {
message = error.toString();
message = "Download of " + this.getName() + "from " + this.getSource().toString() + " caused error: " + error.toString();
}
log.warn(message, error);
// log.warn(message, error);
log.warn(message);
this.state.setValue(State.ABORTED);
this.error.setValue(error);
this.message.setValue(message);
@ -132,11 +133,11 @@ public class DownloadJob extends AbstractLaternaBean {
}
public static Source fromURL(final String url) {
return fromURL(null, url);
return fromURL(CardImageUtils.getProxyFromPreferences(), url);
}
public static Source fromURL(final URL url) {
return fromURL(null, url);
return fromURL(CardImageUtils.getProxyFromPreferences(), url);
}
public static Source fromURL(final Proxy proxy, final String url) {
@ -159,6 +160,12 @@ public class DownloadJob extends AbstractLaternaBean {
public int length() throws IOException {
return getConnection().getContentLength();
}
@Override
public String toString() {
return proxy != null ? proxy.type().toString()+" " :"" + url;
}
};
}
@ -182,6 +189,11 @@ public class DownloadJob extends AbstractLaternaBean {
public int length() throws IOException {
return getConnection().getContentLength();
}
@Override
public String toString() {
return proxy != null ? proxy.type().toString()+" " :"" + url;
}
};
}

View file

@ -11,6 +11,7 @@ import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
@ -158,6 +159,14 @@ public class Downloader extends AbstractLaternaBean implements Disposable {
}
}
job.setState(State.FINISHED);
} catch(ConnectException ex) {
String message;
if (ex.getMessage() != null) {
message = ex.getMessage();
} else {
message = "Unknown error";
}
logger.warn("Error resource download " + job.getName() +" from "+ job.getSource().toString() + ": " + message);
} catch(IOException ex) {
job.setError(ex);
}

View file

@ -0,0 +1,100 @@
/*
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com.
*/
package org.mage.plugins.card.dl.sources;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import org.mage.plugins.card.dl.DownloadJob;
import static org.mage.plugins.card.dl.DownloadJob.fromURL;
import static org.mage.plugins.card.dl.DownloadJob.toFile;
/**
*
* @author LevelX2
*/
public class CardFrames implements Iterable<DownloadJob> {
private static final String FRAMES_PATH = File.separator + "frames";
private static final File DEFAULT_OUT_DIR = new File("plugins" + File.separator + "images" + FRAMES_PATH);
private static File outDir = DEFAULT_OUT_DIR;
static final String BASE_DOWNLOAD_URL = "http://ct-magefree.rhcloud.com/resources/img/";
static final String TEXTURES_FOLDER = "textures";
static final String PT_BOXES_FOLDER = "pt";
private static final String[] TEXTURES = {"U", "R", "G", "B", "W", "A",
"BG_LAND", "BR_LAND", "WU_LAND", "WB_LAND", "UB_LAND", "GW_LAND", "RW_LAND",
"RG_LAND", "GU_LAND", "UR_LAND"
// NOT => "BW_LAND","BU_LAND","WG_LAND","WR_LAND",
};
private static final String[] PT_BOXES = {"U", "R", "G", "B", "W", "A"};
public CardFrames(String path) {
if (path == null) {
useDefaultDir();
} else {
changeOutDir(path);
}
}
@Override
public Iterator<DownloadJob> iterator() {
ArrayList<DownloadJob> jobs = new ArrayList<>();
for (String texture : TEXTURES) {
jobs.add(generateDownloadJob(TEXTURES_FOLDER, texture));
}
for (String pt_box : PT_BOXES) {
jobs.add(generateDownloadJob(PT_BOXES_FOLDER, pt_box));
}
return jobs.iterator();
}
private DownloadJob generateDownloadJob(String dirName, String name) {
File dst = new File(outDir, name + ".png");
String url = BASE_DOWNLOAD_URL + dirName + "/" + name + ".png";
return new DownloadJob("frames-" + dirName + "-" + name, fromURL(url), toFile(dst));
}
private void useDefaultDir() {
outDir = DEFAULT_OUT_DIR;
}
private void changeOutDir(String path) {
File file = new File(path + FRAMES_PATH);
if (file.exists()) {
outDir = file;
} else {
file.mkdirs();
if (file.exists()) {
outDir = file;
}
}
}
}

View file

@ -8,12 +8,10 @@ package org.mage.plugins.card.dl.sources;
import com.google.common.collect.AbstractIterator;
import org.mage.plugins.card.dl.DownloadJob;
import java.io.File;
import java.util.Iterator;
import static java.lang.String.format;
import java.util.Iterator;
import org.mage.plugins.card.dl.DownloadJob;
import static org.mage.plugins.card.dl.DownloadJob.fromURL;
import static org.mage.plugins.card.dl.DownloadJob.toFile;
@ -70,7 +68,9 @@ public class GathererSymbols implements Iterable<DownloadJob> {
sym = "" + (numeric++);
} else {
sizeIndex++;
if(sizeIndex == sizes.length) return endOfData();
if(sizeIndex == sizes.length) {
return endOfData();
}
symIndex = 0;
numeric = 0;
@ -80,9 +80,17 @@ public class GathererSymbols implements Iterable<DownloadJob> {
String symbol = sym.replaceAll("/", "");
File dst = new File(dir, symbol + ".jpg");
if(symbol.equals("T")) symbol = "tap";
else if(symbol.equals("Q")) symbol = "untap";
else if(symbol.equals("S")) symbol = "snow";
switch (symbol) {
case "T":
symbol = "tap";
break;
case "Q":
symbol = "untap";
break;
case "S":
symbol = "snow";
break;
}
String url = format(urlFmt, sizes[sizeIndex], symbol);

View file

@ -1,8 +1,14 @@
package org.mage.plugins.card.utils;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.HashMap;
import java.util.prefs.Preferences;
import mage.client.MageFrame;
import mage.client.constants.Constants;
import mage.client.dialog.PreferencesDialog;
import mage.remote.Connection;
import mage.remote.Connection.ProxyType;
import net.java.truevfs.access.TFile;
import org.apache.log4j.Logger;
import org.mage.plugins.card.images.CardDownloadData;
@ -150,4 +156,15 @@ public class CardImageUtils {
return imageDir + TFile.separator + imageName;
}
public static Proxy getProxyFromPreferences() {
Preferences prefs = MageFrame.getPreferences();
Connection.ProxyType proxyType = Connection.ProxyType.valueByText(prefs.get("proxyType", "None"));
if (!proxyType.equals(ProxyType.NONE)) {
String proxyServer = prefs.get("proxyAddress", "");
int proxyPort = Integer.parseInt(prefs.get("proxyPort", "0"));
return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyServer, proxyPort));
}
return null;
}
}