Fixed card class loading: Issue 32.

This commit is contained in:
magenoxx 2011-01-25 20:40:15 +03:00
parent 4c8122ebbb
commit 8be91fbeff
2 changed files with 134 additions and 82 deletions

View file

@ -14,7 +14,6 @@ public class CardsStorage {
private static List<Card> uniqueCards = new ArrayList<Card>();
static {
System.out.println("sets count: " + Sets.getInstance().values().size());
for (ExpansionSet set: Sets.getInstance().values()) {
allCards.addAll(set.createCards());
}

View file

@ -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 {
@ -118,6 +111,7 @@ public abstract class ExpansionSet implements Serializable {
return (Card) con.newInstance(new Object[]{null});
} catch (Exception ex) {
logger.log(Level.SEVERE, "Error creating card:" + clazz.getName(), ex);
ex.printStackTrace();
return null;
}
}
@ -175,11 +169,31 @@ public abstract class ExpansionSet implements Serializable {
e.printStackTrace();
}
List<File> dirs = new ArrayList<File>();
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<Class> classes = new ArrayList<Class>();
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));
@ -187,6 +201,7 @@ public abstract class ExpansionSet implements Serializable {
e.printStackTrace();
}
}
}
return classes;
}
@ -196,16 +211,56 @@ public abstract class ExpansionSet implements Serializable {
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))
if (CardImpl.class.isAssignableFrom(c)) {
classes.add(c);
}
}
}
return classes;
}
private static List<Class> findClassesInJar(File file, String packageName) throws ClassNotFoundException {
List<Class> classes = new ArrayList<Class>();
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;
}
@ -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);
}
}