Fix CRLF mess again.

This commit is contained in:
rkfg 2016-03-04 10:19:05 +03:00
parent 4674b18a51
commit a612eb4585

View file

@ -1,311 +1,311 @@
package org.mage.card.arcane; package org.mage.card.arcane;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Image; import java.awt.Image;
import java.awt.Rectangle; import java.awt.Rectangle;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.FileSystems; import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult; import java.nio.file.FileVisitResult;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.PathMatcher; import java.nio.file.PathMatcher;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor; import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import mage.cards.repository.ExpansionRepository; import mage.cards.repository.ExpansionRepository;
import mage.client.dialog.PreferencesDialog; import mage.client.dialog.PreferencesDialog;
import mage.client.util.GUISizeHelper; import mage.client.util.GUISizeHelper;
import mage.client.util.ImageHelper; import mage.client.util.ImageHelper;
import mage.client.util.gui.BufferedImageBuilder; import mage.client.util.gui.BufferedImageBuilder;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.mage.plugins.card.constants.Constants; import org.mage.plugins.card.constants.Constants;
public class ManaSymbols { public class ManaSymbols {
private static final Logger LOGGER = Logger.getLogger(ManaSymbols.class); private static final Logger LOGGER = Logger.getLogger(ManaSymbols.class);
private static final Map<Integer, Map<String, BufferedImage>> manaImages = new HashMap<>(); private static final Map<Integer, Map<String, BufferedImage>> manaImages = new HashMap<>();
private static boolean smallSymbolsFound = false; private static boolean smallSymbolsFound = false;
private static boolean mediumSymbolsFound = false; private static boolean mediumSymbolsFound = false;
private static final Map<String, Image> setImages = new HashMap<>(); private static final Map<String, Image> setImages = new HashMap<>();
private static final Map<String, Dimension> setImagesExist = new HashMap<>(); private static final Map<String, Dimension> setImagesExist = new HashMap<>();
private static final Pattern REPLACE_SYMBOLS_PATTERN = Pattern.compile("\\{([^}/]*)/?([^}]*)\\}"); private static final Pattern REPLACE_SYMBOLS_PATTERN = Pattern.compile("\\{([^}/]*)/?([^}]*)\\}");
private static String cachedPath; private static String cachedPath;
private static final String[] symbols = new String[]{"0", "1", "10", "11", "12", "15", "16", "2", "3", "4", "5", "6", "7", "8", "9", "B", "BG", private static final String[] symbols = new String[]{"0", "1", "10", "11", "12", "15", "16", "2", "3", "4", "5", "6", "7", "8", "9", "B", "BG",
"BR", "G", "GU", "GW", "R", "RG", "RW", "S", "T", "U", "UB", "UR", "W", "WB", "WU", "BR", "G", "GU", "GW", "R", "RG", "RW", "S", "T", "U", "UB", "UR", "W", "WB", "WU",
"WP", "UP", "BP", "RP", "GP", "X", "C"}; "WP", "UP", "BP", "RP", "GP", "X", "C"};
public static void loadImages() { public static void loadImages() {
renameSymbols(getSymbolsPath() + File.separator + "symbols"); renameSymbols(getSymbolsPath() + File.separator + "symbols");
smallSymbolsFound = loadSymbolsImages(15); smallSymbolsFound = loadSymbolsImages(15);
mediumSymbolsFound = loadSymbolsImages(25); mediumSymbolsFound = loadSymbolsImages(25);
List<String> setCodes = ExpansionRepository.instance.getSetCodes(); List<String> setCodes = ExpansionRepository.instance.getSetCodes();
if (setCodes == null) { if (setCodes == null) {
// the cards db file is probaly not included in the client. It will be created after the first connect to a server. // the cards db file is probaly not included in the client. It will be created after the first connect to a server.
LOGGER.warn("No db information for sets found. Connect to a server to create database file on client side. Then try to restart the client."); LOGGER.warn("No db information for sets found. Connect to a server to create database file on client side. Then try to restart the client.");
return; return;
} }
for (String set : setCodes) { for (String set : setCodes) {
File file = new File(getSymbolsPath() + Constants.RESOURCE_PATH_SET + set + "-C.jpg"); File file = new File(getSymbolsPath() + Constants.RESOURCE_PATH_SET + set + "-C.jpg");
try { try {
Image image = UI.getImageIcon(file.getAbsolutePath()).getImage(); Image image = UI.getImageIcon(file.getAbsolutePath()).getImage();
int width = image.getWidth(null); int width = image.getWidth(null);
if (width > 21) { if (width > 21) {
int h = image.getHeight(null); int h = image.getHeight(null);
if (h > 0) { if (h > 0) {
Rectangle r = new Rectangle(21, (int) (h * 21.0f / width)); Rectangle r = new Rectangle(21, (int) (h * 21.0f / width));
BufferedImage resized = ImageHelper.getResizedImage(BufferedImageBuilder.bufferImage(image, BufferedImage.TYPE_INT_ARGB), r); BufferedImage resized = ImageHelper.getResizedImage(BufferedImageBuilder.bufferImage(image, BufferedImage.TYPE_INT_ARGB), r);
setImages.put(set, resized); setImages.put(set, resized);
} }
} else { } else {
setImages.put(set, image); setImages.put(set, image);
} }
} catch (Exception e) { } catch (Exception e) {
} }
String[] codes = new String[]{"C", "U", "R", "M"}; String[] codes = new String[]{"C", "U", "R", "M"};
try { try {
file = new File(getSymbolsPath() + Constants.RESOURCE_PATH_SET_SMALL); file = new File(getSymbolsPath() + Constants.RESOURCE_PATH_SET_SMALL);
if (!file.exists()) { if (!file.exists()) {
file.mkdirs(); file.mkdirs();
} }
for (String code : codes) { for (String code : codes) {
file = new File(getSymbolsPath() + Constants.RESOURCE_PATH_SET_SMALL + set + "-" + code + ".png"); file = new File(getSymbolsPath() + Constants.RESOURCE_PATH_SET_SMALL + set + "-" + code + ".png");
if (file.exists()) { if (file.exists()) {
continue; continue;
} }
file = new File(getSymbolsPath() + Constants.RESOURCE_PATH_SET + set + "-" + code + ".jpg"); file = new File(getSymbolsPath() + Constants.RESOURCE_PATH_SET + set + "-" + code + ".jpg");
Image image = UI.getImageIcon(file.getAbsolutePath()).getImage(); Image image = UI.getImageIcon(file.getAbsolutePath()).getImage();
try { try {
int width = image.getWidth(null); int width = image.getWidth(null);
int height = image.getHeight(null); int height = image.getHeight(null);
if (height > 0) { if (height > 0) {
int dx = 0; int dx = 0;
if (set.equals("M10") || set.equals("M11") || set.equals("M12")) { if (set.equals("M10") || set.equals("M11") || set.equals("M12")) {
dx = 6; dx = 6;
} }
Rectangle r = new Rectangle(15 + dx, (int) (height * (15.0f + dx) / width)); Rectangle r = new Rectangle(15 + dx, (int) (height * (15.0f + dx) / width));
BufferedImage resized = ImageHelper.getResizedImage(BufferedImageBuilder.bufferImage(image, BufferedImage.TYPE_INT_ARGB), r); BufferedImage resized = ImageHelper.getResizedImage(BufferedImageBuilder.bufferImage(image, BufferedImage.TYPE_INT_ARGB), r);
File newFile = new File(getSymbolsPath() + Constants.RESOURCE_PATH_SET_SMALL + File.separator + set + "-" + code + ".png"); File newFile = new File(getSymbolsPath() + Constants.RESOURCE_PATH_SET_SMALL + File.separator + set + "-" + code + ".png");
ImageIO.write(resized, "png", newFile); ImageIO.write(resized, "png", newFile);
} }
} catch (Exception e) { } catch (Exception e) {
if (file.exists()) { if (file.exists()) {
file.delete(); file.delete();
} }
} }
} }
} catch (Exception e) { } catch (Exception e) {
} }
} }
File file; File file;
for (String set : ExpansionRepository.instance.getSetCodes()) { for (String set : ExpansionRepository.instance.getSetCodes()) {
file = new File(getSymbolsPath() + Constants.RESOURCE_PATH_SET_SMALL); file = new File(getSymbolsPath() + Constants.RESOURCE_PATH_SET_SMALL);
if (!file.exists()) { if (!file.exists()) {
break; break;
} }
file = new File(getSymbolsPath() + Constants.RESOURCE_PATH_SET_SMALL + set + "-C.png"); file = new File(getSymbolsPath() + Constants.RESOURCE_PATH_SET_SMALL + set + "-C.png");
try { try {
Image image = UI.getImageIcon(file.getAbsolutePath()).getImage(); Image image = UI.getImageIcon(file.getAbsolutePath()).getImage();
int width = image.getWidth(null); int width = image.getWidth(null);
int height = image.getHeight(null); int height = image.getHeight(null);
setImagesExist.put(set, new Dimension(width, height)); setImagesExist.put(set, new Dimension(width, height));
} catch (Exception e) { } catch (Exception e) {
} }
} }
} }
private static boolean loadSymbolsImages(int size) { private static boolean loadSymbolsImages(int size) {
boolean fileErrors = false; boolean fileErrors = false;
HashMap<String, BufferedImage> sizedSymbols = new HashMap<>(); HashMap<String, BufferedImage> sizedSymbols = new HashMap<>();
String resourcePath = Constants.RESOURCE_PATH_MANA_SMALL; String resourcePath = Constants.RESOURCE_PATH_MANA_SMALL;
if (size > 25) { if (size > 25) {
resourcePath = Constants.RESOURCE_PATH_MANA_LARGE; resourcePath = Constants.RESOURCE_PATH_MANA_LARGE;
} else if (size > 15) { } else if (size > 15) {
resourcePath = Constants.RESOURCE_PATH_MANA_MEDIUM; resourcePath = Constants.RESOURCE_PATH_MANA_MEDIUM;
} }
for (String symbol : symbols) { for (String symbol : symbols) {
File file = new File(getSymbolsPath() + resourcePath + "/" + symbol + ".gif"); File file = new File(getSymbolsPath() + resourcePath + "/" + symbol + ".gif");
try { try {
if (size == 15 || size == 25) { if (size == 15 || size == 25) {
BufferedImage notResized = ImageIO.read(file); BufferedImage notResized = ImageIO.read(file);
sizedSymbols.put(symbol, notResized); sizedSymbols.put(symbol, notResized);
} else { } else {
Rectangle r = new Rectangle(size, size); Rectangle r = new Rectangle(size, size);
Image image = UI.getImageIcon(file.getAbsolutePath()).getImage(); Image image = UI.getImageIcon(file.getAbsolutePath()).getImage();
BufferedImage resized = ImageHelper.getResizedImage(BufferedImageBuilder.bufferImage(image, BufferedImage.TYPE_INT_ARGB), r); BufferedImage resized = ImageHelper.getResizedImage(BufferedImageBuilder.bufferImage(image, BufferedImage.TYPE_INT_ARGB), r);
sizedSymbols.put(symbol, resized); sizedSymbols.put(symbol, resized);
} }
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error for symbol:" + symbol); LOGGER.error("Error for symbol:" + symbol);
fileErrors = true; fileErrors = true;
} }
} }
manaImages.put(size, sizedSymbols); manaImages.put(size, sizedSymbols);
return !fileErrors; return !fileErrors;
} }
private static void renameSymbols(String path) { private static void renameSymbols(String path) {
final PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**/*.jpg"); final PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**/*.jpg");
try { try {
Files.walkFileTree(Paths.get(path), new SimpleFileVisitor<Path>() { Files.walkFileTree(Paths.get(path), new SimpleFileVisitor<Path>() {
@Override @Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (matcher.matches(file)) { if (matcher.matches(file)) {
Path gifPath = file.resolveSibling(file.getFileName().toString().replaceAll("\\.jpg$", ".gif")); Path gifPath = file.resolveSibling(file.getFileName().toString().replaceAll("\\.jpg$", ".gif"));
Files.move(file, gifPath, StandardCopyOption.REPLACE_EXISTING); Files.move(file, gifPath, StandardCopyOption.REPLACE_EXISTING);
} }
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
} }
}); });
} catch (IOException e) { } catch (IOException e) {
LOGGER.error("Couldn't rename mana symbols!"); LOGGER.error("Couldn't rename mana symbols!");
} }
} }
private static String getSymbolsPath() { private static String getSymbolsPath() {
return getSymbolsPath(false); return getSymbolsPath(false);
} }
private static String getSymbolsPath(boolean forHtmlCode) { private static String getSymbolsPath(boolean forHtmlCode) {
String useDefault = PreferencesDialog.getCachedValue(PreferencesDialog.KEY_CARD_IMAGES_USE_DEFAULT, "true"); String useDefault = PreferencesDialog.getCachedValue(PreferencesDialog.KEY_CARD_IMAGES_USE_DEFAULT, "true");
String path = useDefault.equals("true") ? null : PreferencesDialog.getCachedValue(PreferencesDialog.KEY_CARD_IMAGES_PATH, null); String path = useDefault.equals("true") ? null : PreferencesDialog.getCachedValue(PreferencesDialog.KEY_CARD_IMAGES_PATH, null);
if (path == null) { if (path == null) {
if (forHtmlCode) { if (forHtmlCode) {
// for html code we need to use double '//' symbols // for html code we need to use double '//' symbols
// and seems it should be hard coded - as it is not the same as using File.separator // and seems it should be hard coded - as it is not the same as using File.separator
return "plugins/images/"; return "plugins/images/";
} else { } else {
return mage.client.constants.Constants.IO.imageBaseDir; return mage.client.constants.Constants.IO.imageBaseDir;
} }
} }
if (forHtmlCode) { if (forHtmlCode) {
if (cachedPath != null) { if (cachedPath != null) {
return cachedPath; return cachedPath;
} }
if (path.contains("\\")) { if (path.contains("\\")) {
cachedPath = path.replaceAll("[\\\\]", "/"); cachedPath = path.replaceAll("[\\\\]", "/");
return cachedPath; return cachedPath;
} }
} }
return path; return path;
} }
public static void draw(Graphics g, String manaCost, int x, int y, int symbolWidth) { public static void draw(Graphics g, String manaCost, int x, int y, int symbolWidth) {
if (!manaImages.containsKey(symbolWidth)) { if (!manaImages.containsKey(symbolWidth)) {
loadSymbolsImages(symbolWidth); loadSymbolsImages(symbolWidth);
} }
Map<String, BufferedImage> sizedSymbols = manaImages.get(symbolWidth); Map<String, BufferedImage> sizedSymbols = manaImages.get(symbolWidth);
if (manaCost.length() == 0) { if (manaCost.length() == 0) {
return; return;
} }
manaCost = manaCost.replace("\\", ""); manaCost = manaCost.replace("\\", "");
manaCost = UI.getDisplayManaCost(manaCost); manaCost = UI.getDisplayManaCost(manaCost);
StringTokenizer tok = new StringTokenizer(manaCost, " "); StringTokenizer tok = new StringTokenizer(manaCost, " ");
while (tok.hasMoreTokens()) { while (tok.hasMoreTokens()) {
String symbol = tok.nextToken().substring(0); String symbol = tok.nextToken().substring(0);
// Check and load symbol in the width // Check and load symbol in the width
Image image = sizedSymbols.get(symbol); Image image = sizedSymbols.get(symbol);
if (image == null) { if (image == null) {
//log.error("Symbol not recognized \"" + symbol + "\" in mana cost: " + manaCost); //log.error("Symbol not recognized \"" + symbol + "\" in mana cost: " + manaCost);
continue; continue;
} }
g.drawImage(image, x, y, null); g.drawImage(image, x, y, null);
x += symbolWidth; x += symbolWidth;
} }
} }
public static String getStringManaCost(List<String> manaCost) { public static String getStringManaCost(List<String> manaCost) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (String s : manaCost) { for (String s : manaCost) {
sb.append(s); sb.append(s);
} }
return sb.toString().replace("{", "").replace("}", " ").trim(); return sb.toString().replace("{", "").replace("}", " ").trim();
} }
public enum Type { public enum Type {
TABLE, TABLE,
CHAT, CHAT,
DIALOG, DIALOG,
TOOLTIP, TOOLTIP,
} }
public static synchronized String replaceSymbolsWithHTML(String value, Type type) { public static synchronized String replaceSymbolsWithHTML(String value, Type type) {
value = value.replace("{source}", "|source|"); value = value.replace("{source}", "|source|");
value = value.replace("{this}", "|this|"); value = value.replace("{this}", "|this|");
String replaced = value; String replaced = value;
boolean symbolFilesFound; boolean symbolFilesFound;
int symbolSize; int symbolSize;
switch (type) { switch (type) {
case TABLE: case TABLE:
symbolSize = GUISizeHelper.symbolTableSize; symbolSize = GUISizeHelper.symbolTableSize;
break; break;
case CHAT: case CHAT:
symbolSize = GUISizeHelper.symbolChatSize; symbolSize = GUISizeHelper.symbolChatSize;
break; break;
case DIALOG: case DIALOG:
symbolSize = GUISizeHelper.symbolDialogSize; symbolSize = GUISizeHelper.symbolDialogSize;
break; break;
case TOOLTIP: case TOOLTIP:
symbolSize = GUISizeHelper.symbolTooltipSize; symbolSize = GUISizeHelper.symbolTooltipSize;
break; break;
default: default:
symbolSize = 11; symbolSize = 11;
break; break;
} }
String resourcePath = "small"; String resourcePath = "small";
symbolFilesFound = smallSymbolsFound; symbolFilesFound = smallSymbolsFound;
if (symbolSize > 25) { if (symbolSize > 25) {
resourcePath = "large"; resourcePath = "large";
} else if (symbolSize > 15) { } else if (symbolSize > 15) {
resourcePath = "medium"; resourcePath = "medium";
symbolFilesFound = mediumSymbolsFound; symbolFilesFound = mediumSymbolsFound;
} }
if (symbolFilesFound) { if (symbolFilesFound) {
replaced = REPLACE_SYMBOLS_PATTERN.matcher(value).replaceAll( replaced = REPLACE_SYMBOLS_PATTERN.matcher(value).replaceAll(
"<img src='file:" + getSymbolsPath(true) + "/symbols/" + resourcePath + "/$1$2.gif' alt='$1$2' width=" + symbolSize "<img src='file:" + getSymbolsPath(true) + "/symbols/" + resourcePath + "/$1$2.gif' alt='$1$2' width=" + symbolSize
+ " height=" + symbolSize + ">"); + " height=" + symbolSize + ">");
} }
replaced = replaced.replace("|source|", "{source}"); replaced = replaced.replace("|source|", "{source}");
replaced = replaced.replace("|this|", "{this}"); replaced = replaced.replace("|this|", "{this}");
return replaced; return replaced;
} }
public static String replaceSetCodeWithHTML(String set, String rarity, int size) { public static String replaceSetCodeWithHTML(String set, String rarity, int size) {
String _set = set; String _set = set;
if (setImagesExist.containsKey(_set)) { if (setImagesExist.containsKey(_set)) {
int factor = size / 15 + 1; int factor = size / 15 + 1;
Integer width = setImagesExist.get(_set).width * factor; Integer width = setImagesExist.get(_set).width * factor;
Integer height = setImagesExist.get(_set).height * factor; Integer height = setImagesExist.get(_set).height * factor;
return "<img src='file:" + getSymbolsPath() + "/sets/small/" + _set + "-" + rarity + ".png' alt='" + rarity + "' height='" + height + "' width='" + width + "' >"; return "<img src='file:" + getSymbolsPath() + "/sets/small/" + _set + "-" + rarity + ".png' alt='" + rarity + "' height='" + height + "' width='" + width + "' >";
} else { } else {
return set; return set;
} }
} }
public static Image getSetSymbolImage(String set) { public static Image getSetSymbolImage(String set) {
return setImages.get(set); return setImages.get(set);
} }
public static BufferedImage getSizedManaSymbol(String symbol) { public static BufferedImage getSizedManaSymbol(String symbol) {
if (!manaImages.containsKey(GUISizeHelper.symbolDialogSize)) { if (!manaImages.containsKey(GUISizeHelper.symbolDialogSize)) {
loadSymbolsImages(GUISizeHelper.symbolDialogSize); loadSymbolsImages(GUISizeHelper.symbolDialogSize);
} }
Map<String, BufferedImage> sizedSymbols = manaImages.get(GUISizeHelper.symbolDialogSize); Map<String, BufferedImage> sizedSymbols = manaImages.get(GUISizeHelper.symbolDialogSize);
return sizedSymbols.get(symbol); return sizedSymbols.get(symbol);
} }
} }