diff --git a/Mage.Plugins/Mage.Rating.Plugin/src/main/java/org/mage/plugins/rating/cards/CardsStorage.java b/Mage.Plugins/Mage.Rating.Plugin/src/main/java/org/mage/plugins/rating/cards/CardsStorage.java index 7bf50d300f..f6b1a73db1 100644 --- a/Mage.Plugins/Mage.Rating.Plugin/src/main/java/org/mage/plugins/rating/cards/CardsStorage.java +++ b/Mage.Plugins/Mage.Rating.Plugin/src/main/java/org/mage/plugins/rating/cards/CardsStorage.java @@ -14,9 +14,8 @@ public class CardsStorage { private static List uniqueCards = new ArrayList(); static { - System.out.println("sets count: " + Sets.getInstance().values().size()); for (ExpansionSet set: Sets.getInstance().values()) { - allCards.addAll(set.createCards()); + allCards.addAll(set.createCards()); } Set names = new HashSet(); for (Card card : allCards) { diff --git a/Mage/src/mage/cards/ExpansionSet.java b/Mage/src/mage/cards/ExpansionSet.java index 7a91d4d370..690db386b5 100644 --- a/Mage/src/mage/cards/ExpansionSet.java +++ b/Mage/src/mage/cards/ExpansionSet.java @@ -28,31 +28,24 @@ package mage.cards; -import java.io.File; -import java.io.IOException; -import java.io.Serializable; -import java.lang.reflect.Constructor; -import java.net.URL; -import java.util.ArrayList; -import java.util.Date; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Random; -import java.util.Set; -import java.util.UUID; -import java.util.jar.JarEntry; -import java.util.jar.JarFile; -import java.util.logging.Level; -import java.util.logging.Logger; import mage.Constants.Rarity; import mage.Constants.SetType; import mage.util.Logging; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.Serializable; +import java.lang.reflect.Constructor; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.*; +import java.util.jar.JarEntry; +import java.util.jar.JarInputStream; +import java.util.logging.Level; +import java.util.logging.Logger; + /** - * * @author BetaSteward_at_googlemail.com */ public abstract class ExpansionSet implements Serializable { @@ -107,7 +100,7 @@ public abstract class ExpansionSet implements Serializable { public Date getReleaseDate() { return releaseDate; } - + public SetType getSetType() { return setType; } @@ -115,16 +108,17 @@ public abstract class ExpansionSet implements Serializable { public Card createCard(Class clazz) { try { Constructor con = clazz.getConstructor(new Class[]{UUID.class}); - return (Card) con.newInstance(new Object[] {null}); + return (Card) con.newInstance(new Object[]{null}); } catch (Exception ex) { logger.log(Level.SEVERE, "Error creating card:" + clazz.getName(), ex); + ex.printStackTrace(); return null; } } public Set createCards() { Set created = new HashSet(); - for (Class clazz: cards) { + for (Class clazz : cards) { created.add(createCard(clazz)); } return created; @@ -136,83 +130,144 @@ public abstract class ExpansionSet implements Serializable { } public Card findCard(String name) { - for (Card card: createCards()) { + for (Card card : createCards()) { if (name.equals(card.getName())) return card; } return null; } - public Card findCard(String name, boolean random) { - List cards = new ArrayList(); - for (Card card: createCards()) { + public Card findCard(String name, boolean random) { + List cards = new ArrayList(); + for (Card card : createCards()) { if (name.equals(card.getName())) { cards.add(card); - } + } + } + if (cards.size() > 0) { + return cards.get(rnd.nextInt(cards.size())); } - if (cards.size() > 0) { - return cards.get(rnd.nextInt(cards.size())); - } return null; } public String findCard(int cardNum) { - for (Card card: createCards()) { + for (Card card : createCards()) { if (card.getCardNumber() == cardNum) return card.getClass().getCanonicalName(); } return null; } - private ArrayList getCardClassesForPackage(String packageName) { - ClassLoader classLoader = this.getClass().getClassLoader(); - assert classLoader != null; - String path = packageName.replace('.', '/'); - Enumeration resources = null; - try { - resources = classLoader.getResources(path); - } catch (IOException e) { - e.printStackTrace(); - } - List dirs = new ArrayList(); - while (resources.hasMoreElements()) { - URL resource = resources.nextElement(); - dirs.add(new File(resource.getFile())); - } - ArrayList classes = new ArrayList(); - for (File directory : dirs) { - try { - classes.addAll(findClasses(directory, packageName)); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } - } - return classes; - } + private ArrayList getCardClassesForPackage(String packageName) { + ClassLoader classLoader = this.getClass().getClassLoader(); + assert classLoader != null; + String path = packageName.replace('.', '/'); + Enumeration resources = null; + try { + resources = classLoader.getResources(path); + } catch (IOException e) { + e.printStackTrace(); + } + List dirs = new ArrayList(); + boolean isLoadingFromJar = false; + String jarPath = null; + while (resources.hasMoreElements()) { + URL resource = resources.nextElement(); + if (resource.toString().startsWith("jar:")) { + isLoadingFromJar = true; + jarPath = resource.getFile(); + break; + } + dirs.add(new File(resource.getFile())); + } + ArrayList classes = new ArrayList(); + if (isLoadingFromJar) { + if (jarPath.contains("!")) { + jarPath = jarPath.substring(0, jarPath.lastIndexOf('!')); + } + if (jarPath.startsWith("file:/")) { + jarPath = jarPath.substring(jarPath.indexOf("file:/") + "file:/".length()); + } + try { + classes.addAll(findClassesInJar(new File(jarPath), path)); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } else { // faster but doesn't work for jars + for (File directory : dirs) { + try { + classes.addAll(findClasses(directory, packageName)); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + } + return classes; + } - private static List findClasses(File directory, String packageName) throws ClassNotFoundException { - List classes = new ArrayList(); - if (!directory.exists()) { - return classes; - } - File[] files = directory.listFiles(); - for (File file : files) { - if (file.isDirectory()) { - assert !file.getName().contains("."); - classes.addAll(findClasses(file, packageName + "." + file.getName())); - } else if (file.getName().endsWith(".class")) { - Class c = Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6)); - if (CardImpl.class.isAssignableFrom(c)) - classes.add(c); - } - } - return classes; - } + private static List findClasses(File directory, String packageName) throws ClassNotFoundException { + List classes = new ArrayList(); + if (!directory.exists()) { + return classes; + } + File[] files = directory.listFiles(); + if (files == null) { + return classes; + } + for (File file : files) { + if (file.isDirectory()) { + assert !file.getName().contains("."); + classes.addAll(findClasses(file, packageName + "." + file.getName())); + } else if (file.getName().endsWith(".class")) { + Class c = Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6)); + if (CardImpl.class.isAssignableFrom(c)) { + classes.add(c); + } + } + } + return classes; + } + + private static List findClassesInJar(File file, String packageName) throws ClassNotFoundException { + List classes = new ArrayList(); + + if (!file.exists()) { + return classes; + } + + try { + URL url = file.toURL(); + URL[] urls = new URL[]{url}; + ClassLoader cl = new URLClassLoader(urls); + + JarInputStream jarFile = new JarInputStream(new FileInputStream(file)); + JarEntry jarEntry; + + while (true) { + jarEntry = jarFile.getNextJarEntry(); + if (jarEntry == null) { + break; + } + if ((jarEntry.getName().startsWith(packageName)) && (jarEntry.getName().endsWith(".class"))) { + String clazz = jarEntry.getName().replaceAll("/", "\\.").replace(".class", ""); + Class c = cl.loadClass(clazz); + if (CardImpl.class.isAssignableFrom(c)) { + classes.add(c); + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + + + return classes; + } private Map> getCardsByRarity() { Map> cardsByRarity = new HashMap>(); - for (Class clazz: cards) { + for (Class clazz : cards) { Card card = createCard(clazz); if (!cardsByRarity.containsKey(card.getRarity())) cardsByRarity.put(card.getRarity(), new ArrayList()); @@ -232,8 +287,7 @@ public abstract class ExpansionSet implements Serializable { for (int i = 0; i < numBoosterLands; i++) { addToBooster(booster, parentSet, Rarity.LAND); } - } - else { + } else { for (int i = 0; i < numBoosterLands; i++) { addToBooster(booster, this, Rarity.LAND); } @@ -247,8 +301,7 @@ public abstract class ExpansionSet implements Serializable { for (int i = 0; i < numBoosterRare; i++) { if (ratioBoosterMythic > 0 && rnd.nextInt(ratioBoosterMythic) == 1) { addToBooster(booster, this, Rarity.MYTHIC); - } - else { + } else { addToBooster(booster, this, Rarity.RARE); } }