diff --git a/.gitignore b/.gitignore index 6f5e1ab0f1b..b88ffae5749 100644 --- a/.gitignore +++ b/.gitignore @@ -37,6 +37,7 @@ Mage.Server/db Mage.Server/cache Mage.Server/mageserver.log Mage.Server/magediag.log +Mage.Server/extensions Mage.Sets/target Mage.Stats/server.log Mage.Stats/mageserver.log diff --git a/Mage.Server/src/main/java/mage/server/ExtensionPackage.java b/Mage.Server/src/main/java/mage/server/ExtensionPackage.java new file mode 100644 index 00000000000..1d49c39c5b6 --- /dev/null +++ b/Mage.Server/src/main/java/mage/server/ExtensionPackage.java @@ -0,0 +1,74 @@ +/* + * Copyright 2016 Lymia . 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 mage.server; + +import mage.cards.ExpansionSet; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Main entry point for external packages. + * @author Lymia + */ +public abstract class ExtensionPackage { + protected List expansions = new ArrayList<>(); + protected Map deckTypes = new HashMap<>(); + protected Map draftCubes = new HashMap<>(); + + /** + * @return A list of expansions included in this custom set package. + */ + public List getSets() { + return expansions; + } + + /** + * @return A list of draft cubes included in this custom set package. + */ + public Map getDraftCubes() { + return draftCubes; + } + + /** + * @return A list of deck types included in this custom set package. + */ + public Map getDeckTypes() { + return deckTypes; + } + + /** + * @return The classloader cards should be loaded from for this custom set package. + */ + public ClassLoader getClassLoader() { + return getClass().getClassLoader(); + } +} diff --git a/Mage.Server/src/main/java/mage/server/ExtensionPackageLoader.java b/Mage.Server/src/main/java/mage/server/ExtensionPackageLoader.java new file mode 100644 index 00000000000..f3f6f3c77e6 --- /dev/null +++ b/Mage.Server/src/main/java/mage/server/ExtensionPackageLoader.java @@ -0,0 +1,70 @@ +/* + * Copyright 2016 Lymia . 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 mage.server; + +import mage.server.util.PluginClassLoader; + +import java.io.File; +import java.io.IOException; +import java.util.Scanner; + +/** + * @author Lymia + */ +public class ExtensionPackageLoader { + public static ExtensionPackage loadExtension(File directory) throws IOException { + if(!directory.exists ()) throw new RuntimeException("File not found "+directory); + if(!directory.isDirectory()) throw new RuntimeException(directory+" is not a directory"); + + File entryPointFile = new File(directory, "entryPoint"); + if(!entryPointFile.exists() || !entryPointFile.isFile()) + throw new RuntimeException("Entry point definition not found."); + + File packagesDirectory = new File(directory, "packages"); + if(!packagesDirectory.exists() || !packagesDirectory.isDirectory()) + throw new RuntimeException("Packages directory not found."); + + Scanner entryPointReader = new Scanner(entryPointFile); + String entryPoint = entryPointReader.nextLine().trim(); + entryPointReader.close(); + + PluginClassLoader classLoader = new PluginClassLoader(); + for(File f : packagesDirectory.listFiles()) classLoader.addURL(f.toURI().toURL()); + + try { + return (ExtensionPackage) Class.forName(entryPoint, false, classLoader).newInstance(); + } catch (InstantiationException | IllegalAccessException e) { + throw new RuntimeException(e); + } catch (ClassNotFoundException e) { + throw new RuntimeException("Entry point class not found!", e); + } catch (ClassCastException e) { + throw new RuntimeException("Entry point not an instance of ExtensionPackage.", e); + } + } +} diff --git a/Mage.Server/src/main/java/mage/server/Main.java b/Mage.Server/src/main/java/mage/server/Main.java index e787d5b3575..32486a58465 100644 --- a/Mage.Server/src/main/java/mage/server/Main.java +++ b/Mage.Server/src/main/java/mage/server/Main.java @@ -29,12 +29,19 @@ package mage.server; import java.io.File; import java.io.FilenameFilter; +import java.io.IOException; import java.net.InetAddress; import java.net.MalformedURLException; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import javax.management.MBeanServer; + +import mage.cards.ExpansionSet; +import mage.cards.Sets; import mage.cards.repository.CardScanner; +import mage.cards.repository.PluginClassloaderRegistery; import mage.game.match.MatchType; import mage.game.tournament.TournamentType; import mage.interfaces.MageServer; @@ -82,7 +89,9 @@ public class Main { private static final String testModeArg = "-testMode="; private static final String fastDBModeArg = "-fastDbMode="; private static final String adminPasswordArg = "-adminPassword="; - private static final String pluginFolder = "plugins"; + + private static final File pluginFolder = new File("plugins"); + private static final File extensionFolder = new File("extensions"); public static PluginClassLoader classLoader = new PluginClassLoader(); public static TransporterServer server; @@ -109,6 +118,32 @@ public class Main { } } + logger.info("Loading extension packages..."); + List extensions = new ArrayList<>(); + if(!extensionFolder.exists()) if(!extensionFolder.mkdirs()) + logger.error("Could not create extensions directory."); + File[] extensionDirectories = extensionFolder.listFiles(); + if(extensionDirectories != null) for(File f : extensionDirectories) if(f.isDirectory()) + try { + logger.info(" - Loading extension from "+f); + extensions.add(ExtensionPackageLoader.loadExtension(f)); + } catch (IOException e) { + logger.error("Could not load extension in "+f+"!", e); + } + logger.info("Done."); + + if(!extensions.isEmpty()) { + logger.info("Registering custom sets..."); + for(ExtensionPackage pkg : extensions) { + for(ExpansionSet set : pkg.getSets()) { + logger.info("- Loading "+set.getName()+" ("+set.getCode()+")"); + Sets.getInstance().addSet(set); + } + PluginClassloaderRegistery.registerPluginClassloader(pkg.getClassLoader()); + } + logger.info("Done."); + } + logger.info("Loading cards..."); if (fastDbMode) { CardScanner.scanned = true; @@ -139,6 +174,19 @@ public class Main { DeckValidatorFactory.getInstance().addDeckType(plugin.getName(), loadPlugin(plugin)); } + for (ExtensionPackage pkg : extensions) { + Map draftCubes = pkg.getDraftCubes(); + for (String name : draftCubes.keySet()) { + logger.info("Loading extension: ["+name+"] "+draftCubes.get(name).toString()); + CubeFactory.getInstance().addDraftCube(name, draftCubes.get(name)); + } + Map deckTypes = pkg.getDeckTypes(); + for (String name : deckTypes.keySet()) { + logger.info("Loading extension: ["+name+"] "+deckTypes.get(name)); + DeckValidatorFactory.getInstance().addDeckType(name, deckTypes.get(name)); + } + } + logger.info("Config - max seconds idle: " + config.getMaxSecondsIdle()); logger.info("Config - max game threads: " + config.getMaxGameThreads()); logger.info("Config - max AI opponents: " + config.getMaxAiOpponents()); @@ -316,7 +364,7 @@ public class Main { private static Class loadPlugin(Plugin plugin) { try { - classLoader.addURL(new File(pluginFolder + "/" + plugin.getJar()).toURI().toURL()); + classLoader.addURL(new File(pluginFolder, plugin.getJar()).toURI().toURL()); logger.debug("Loading plugin: " + plugin.getClassName()); return Class.forName(plugin.getClassName(), true, classLoader); } catch (ClassNotFoundException ex) { @@ -329,7 +377,7 @@ public class Main { private static MatchType loadGameType(GamePlugin plugin) { try { - classLoader.addURL(new File(pluginFolder + "/" + plugin.getJar()).toURI().toURL()); + classLoader.addURL(new File(pluginFolder, plugin.getJar()).toURI().toURL()); logger.debug("Loading game type: " + plugin.getClassName()); return (MatchType) Class.forName(plugin.getTypeName(), true, classLoader).newInstance(); } catch (ClassNotFoundException ex) { @@ -342,7 +390,7 @@ public class Main { private static TournamentType loadTournamentType(GamePlugin plugin) { try { - classLoader.addURL(new File(pluginFolder + "/" + plugin.getJar()).toURI().toURL()); + classLoader.addURL(new File(pluginFolder, plugin.getJar()).toURI().toURL()); logger.debug("Loading tournament type: " + plugin.getClassName()); return (TournamentType) Class.forName(plugin.getTypeName(), true, classLoader).newInstance(); } catch (ClassNotFoundException ex) { diff --git a/Mage/src/main/java/mage/cards/CardImpl.java b/Mage/src/main/java/mage/cards/CardImpl.java index d4a18701215..3e1b8d18a9b 100644 --- a/Mage/src/main/java/mage/cards/CardImpl.java +++ b/Mage/src/main/java/mage/cards/CardImpl.java @@ -39,6 +39,7 @@ import mage.abilities.Ability; import mage.abilities.PlayLandAbility; import mage.abilities.SpellAbility; import mage.abilities.mana.ManaAbility; +import mage.cards.repository.PluginClassloaderRegistery; import mage.constants.CardType; import mage.constants.ColoredManaSymbol; import mage.constants.Rarity; @@ -160,6 +161,11 @@ public abstract class CardImpl extends MageObjectImpl implements Card { try { return createCard(Class.forName(name)); } catch (ClassNotFoundException ex) { + try { + return createCard(PluginClassloaderRegistery.forName(name)); + } catch (ClassNotFoundException ex2) { + // ignored + } logger.fatal("Error loading card: " + name, ex); return null; } diff --git a/Mage/src/main/java/mage/cards/ExpansionSet.java b/Mage/src/main/java/mage/cards/ExpansionSet.java index e7a789efd47..26e3ddb4fdc 100644 --- a/Mage/src/main/java/mage/cards/ExpansionSet.java +++ b/Mage/src/main/java/mage/cards/ExpansionSet.java @@ -374,6 +374,10 @@ public abstract class ExpansionSet implements Serializable { return null; } + public boolean isCustomSet() { + return setType == SetType.CUSTOM_SET; + } + public void removeSavedCards() { savedCards.clear(); } diff --git a/Mage/src/main/java/mage/cards/Sets.java b/Mage/src/main/java/mage/cards/Sets.java index 41b605973e8..642bef29bb3 100644 --- a/Mage/src/main/java/mage/cards/Sets.java +++ b/Mage/src/main/java/mage/cards/Sets.java @@ -30,11 +30,7 @@ package mage.cards; import java.io.FileNotFoundException; import java.io.PrintWriter; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Random; +import java.util.*; import mage.cards.decks.DeckCardInfo; import mage.cards.decks.DeckCardLists; @@ -61,10 +57,12 @@ public class Sets extends HashMap { return fINSTANCE; } + private Set customSets = new HashSet<>(); + private Sets() { ArrayList packages = new ArrayList<>(); packages.add("mage.sets"); - for (Class c : ClassScanner.findClasses(packages, ExpansionSet.class)) { + for (Class c : ClassScanner.findClasses(null, packages, ExpansionSet.class)) { try { addSet((ExpansionSet) c.getMethod("getInstance").invoke(null)); } catch (Exception ex) { @@ -72,8 +70,14 @@ public class Sets extends HashMap { } } - private void addSet(ExpansionSet set) { + public void addSet(ExpansionSet set) { + if(containsKey(set.getCode())) throw new IllegalArgumentException("Set code "+set.getCode()+" already exists."); this.put(set.getCode(), set); + if(set.isCustomSet()) customSets.add(set.getCode()); + } + + public static boolean isCustomSet(String setCode) { + return getInstance().customSets.contains(setCode); } /** diff --git a/Mage/src/main/java/mage/cards/decks/Constructed.java b/Mage/src/main/java/mage/cards/decks/Constructed.java index 7824e178feb..ad4a5413652 100644 --- a/Mage/src/main/java/mage/cards/decks/Constructed.java +++ b/Mage/src/main/java/mage/cards/decks/Constructed.java @@ -27,13 +27,10 @@ */ package mage.cards.decks; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.Map.Entry; import mage.cards.Card; +import mage.cards.Sets; import mage.cards.repository.CardInfo; import mage.cards.repository.CardRepository; import mage.constants.Rarity; @@ -52,6 +49,9 @@ public class Constructed extends DeckValidator { protected List setCodes = new ArrayList<>(); protected List rarities = new ArrayList<>(); + protected boolean allowAllCustomSets = false; + protected Set allowedCustomSetCodes = new HashSet<>(); + public Constructed() { super("Constructed"); } @@ -126,22 +126,21 @@ public class Constructed extends DeckValidator { } } - if (!setCodes.isEmpty()) { - for (Card card : deck.getCards()) { - if (!setCodes.contains(card.getExpansionSetCode())) { - if( !legalSets(card) ){ - valid = false; - } - } - } - for (Card card : deck.getSideboard()) { - if (!setCodes.contains(card.getExpansionSetCode())) { - if( !legalSets(card) ){ - valid = false; - } + for (Card card : deck.getCards()) { + if (!isSetAllowed(card.getExpansionSetCode())) { + if( !legalSets(card) ){ + valid = false; } } } + for (Card card : deck.getSideboard()) { + if (!isSetAllowed(card.getExpansionSetCode())) { + if( !legalSets(card) ){ + valid = false; + } + } + } + logger.debug("DECK validate end: " + name + " deckname: " + deck.getName() + " invalids:" + invalid.size()); return valid; } @@ -167,6 +166,16 @@ public class Constructed extends DeckValidator { return legal; } + /** + * Checks if a given set is legal in this format. + * @param code - the set code to check + * @return Whether the set is legal in this format. + */ + protected boolean isSetAllowed(String code) { + if(Sets.isCustomSet(code)) return allowAllCustomSets || allowedCustomSetCodes.contains(code); + else return setCodes.isEmpty() || setCodes.contains(code); + } + /** * Checks if the given card is legal in any of the given sets * @param card - the card to check @@ -177,7 +186,7 @@ public class Constructed extends DeckValidator { boolean legal = false; List cardInfos = CardRepository.instance.findCards(card.getName()); for (CardInfo cardInfo : cardInfos) { - if (setCodes.contains(cardInfo.getSetCode())) { + if (isSetAllowed(cardInfo.getSetCode())) { legal = true; break; } diff --git a/Mage/src/main/java/mage/cards/repository/CardScanner.java b/Mage/src/main/java/mage/cards/repository/CardScanner.java index 8ea8d39f468..fdfb30adbea 100644 --- a/Mage/src/main/java/mage/cards/repository/CardScanner.java +++ b/Mage/src/main/java/mage/cards/repository/CardScanner.java @@ -29,7 +29,10 @@ package mage.cards.repository; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; + import mage.cards.Card; import mage.cards.CardImpl; import mage.cards.ExpansionSet; @@ -55,23 +58,28 @@ public class CardScanner { scanned = true; List cardsToAdd = new ArrayList<>(); - List packages = new ArrayList<>(); + Map> packageMap = new HashMap<>(); for (ExpansionSet set : Sets.getInstance().values()) { + ClassLoader cl = set.getClass().getClassLoader(); + if(!packageMap.containsKey(cl)) packageMap.put(cl, new ArrayList()); + List packages = packageMap.get(cl); packages.add(set.getPackageName()); ExpansionRepository.instance.add(new ExpansionInfo(set)); } ExpansionRepository.instance.setContentVersion(ExpansionRepository.instance.getContentVersionConstant()); - for (Class c : ClassScanner.findClasses(packages, CardImpl.class)) { - if (!CardRepository.instance.cardExists(c.getCanonicalName())) { - Card card = CardImpl.createCard(c); - if (card != null) { - cardsToAdd.add(new CardInfo(card)); - if (card instanceof SplitCard) { - SplitCard splitCard = (SplitCard) card; - cardsToAdd.add(new CardInfo(splitCard.getLeftHalfCard())); - cardsToAdd.add(new CardInfo(splitCard.getRightHalfCard())); + for (ClassLoader cl : packageMap.keySet()) { + for (Class c : ClassScanner.findClasses(cl, packageMap.get(cl), CardImpl.class)) { + if (!CardRepository.instance.cardExists(c.getCanonicalName())) { + Card card = CardImpl.createCard(c); + if (card != null) { + cardsToAdd.add(new CardInfo(card)); + if (card instanceof SplitCard) { + SplitCard splitCard = (SplitCard) card; + cardsToAdd.add(new CardInfo(splitCard.getLeftHalfCard())); + cardsToAdd.add(new CardInfo(splitCard.getRightHalfCard())); + } } } } diff --git a/Mage/src/main/java/mage/cards/repository/PluginClassloaderRegistery.java b/Mage/src/main/java/mage/cards/repository/PluginClassloaderRegistery.java new file mode 100644 index 00000000000..f9edd1468ae --- /dev/null +++ b/Mage/src/main/java/mage/cards/repository/PluginClassloaderRegistery.java @@ -0,0 +1,54 @@ +/* + * Copyright 2016 Lymia . 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 mage.cards.repository; + +import java.util.ArrayList; +import java.util.List; + +/** + * Stores a list of classloaders for plugins that define custom sets. + * + * @author Lymia + */ +public class PluginClassloaderRegistery { + static List pluginClassloaders = new ArrayList<>(); + + public static void registerPluginClassloader(ClassLoader cl) { + pluginClassloaders.add(cl); + } + + public static Class forName(String className) throws ClassNotFoundException { + for(ClassLoader cl : pluginClassloaders) try { + return Class.forName(className, true, cl); + } catch (ClassNotFoundException c) { + // ignored + } + throw new ClassNotFoundException(); + } +} diff --git a/Mage/src/main/java/mage/constants/SetType.java b/Mage/src/main/java/mage/constants/SetType.java index b53d73dd56b..ed23a233bba 100644 --- a/Mage/src/main/java/mage/constants/SetType.java +++ b/Mage/src/main/java/mage/constants/SetType.java @@ -11,7 +11,8 @@ public enum SetType { SUPPLEMENTAL("Supplemental"), SUPPLEMENTAL_STANDARD_LEGAL("Standard Legal Supplemental"), PROMOTIONAL("Promotional"), - JOKESET("Joke Set"); + JOKESET("Joke Set"), + CUSTOM_SET("Unofficial Set"); private final String text; diff --git a/Mage/src/main/java/mage/game/events/GameEvent.java b/Mage/src/main/java/mage/game/events/GameEvent.java index 9e2626b6015..e94d1cbd70e 100644 --- a/Mage/src/main/java/mage/game/events/GameEvent.java +++ b/Mage/src/main/java/mage/game/events/GameEvent.java @@ -47,6 +47,7 @@ public class GameEvent implements Serializable { protected String data; protected Zone zone; protected ArrayList appliedEffects = new ArrayList<>(); + protected UUID customEventType = null; public enum EventType { @@ -269,15 +270,15 @@ public class GameEvent implements Serializable { NUMBER_OF_TRIGGERS, //combat events COMBAT_DAMAGE_APPLIED, - SELECTED_ATTACKER, SELECTED_BLOCKER; + SELECTED_ATTACKER, SELECTED_BLOCKER, + //custom events + CUSTOM_EVENT; } - public GameEvent(EventType type, UUID targetId, UUID sourceId, UUID playerId) { - this(type, targetId, sourceId, playerId, 0, false); - } - - public GameEvent(EventType type, UUID targetId, UUID sourceId, UUID playerId, int amount, boolean flag) { + private GameEvent(EventType type, UUID customEventType, + UUID targetId, UUID sourceId, UUID playerId, int amount, boolean flag) { this.type = type; + this.customEventType = customEventType; this.targetId = targetId; this.sourceId = sourceId; this.amount = amount; @@ -285,6 +286,22 @@ public class GameEvent implements Serializable { this.flag = flag; } + public GameEvent(EventType type, UUID targetId, UUID sourceId, UUID playerId) { + this(type, null, targetId, sourceId, playerId, 0, false); + } + + public GameEvent(EventType type, UUID targetId, UUID sourceId, UUID playerId, int amount, boolean flag) { + this(type, null, targetId, sourceId, playerId, amount, flag); + } + + public GameEvent(UUID customEventType, UUID targetId, UUID sourceId, UUID playerId) { + this(EventType.CUSTOM_EVENT, customEventType, targetId, sourceId, playerId, 0, false); + } + + public GameEvent(UUID customEventType, UUID targetId, UUID sourceId, UUID playerId, int amount, boolean flag) { + this(EventType.CUSTOM_EVENT, customEventType, targetId, sourceId, playerId, amount, flag); + } + public static GameEvent getEvent(EventType type, UUID targetId, UUID sourceId, UUID playerId, int amount) { return new GameEvent(type, targetId, sourceId, playerId, amount, false); } @@ -304,10 +321,33 @@ public class GameEvent implements Serializable { return event; } + public static GameEvent getEvent(UUID customEventType, UUID targetId, UUID sourceId, UUID playerId, int amount) { + return new GameEvent(customEventType, targetId, sourceId, playerId, amount, false); + } + + public static GameEvent getEvent(UUID customEventType, UUID targetId, UUID sourceId, UUID playerId) { + return new GameEvent(customEventType, targetId, sourceId, playerId); + } + + public static GameEvent getEvent(UUID customEventType, UUID targetId, UUID playerId) { + return new GameEvent(customEventType, targetId, null, playerId); + } + + public static GameEvent getEvent(UUID customEventType, UUID targetId, UUID playerId, String data, int amount) { + GameEvent event = getEvent(customEventType, targetId, playerId); + event.setAmount(amount); + event.setData(data); + return event; + } + public EventType getType() { return type; } + public UUID getCustomEventType() { + return customEventType; + } + public UUID getTargetId() { return targetId; } @@ -374,6 +414,10 @@ public class GameEvent implements Serializable { return appliedEffects; } + public boolean isCustomEvent(UUID customEventType) { + return type == EventType.CUSTOM_EVENT && this.customEventType.equals(customEventType); + } + public void setAppliedEffects(ArrayList appliedEffects) { if (this.appliedEffects == null) { this.appliedEffects = new ArrayList<>(); diff --git a/Mage/src/main/java/mage/util/ClassScanner.java b/Mage/src/main/java/mage/util/ClassScanner.java index f644575e976..71253e3e650 100644 --- a/Mage/src/main/java/mage/util/ClassScanner.java +++ b/Mage/src/main/java/mage/util/ClassScanner.java @@ -46,10 +46,21 @@ import java.util.jar.JarInputStream; */ public class ClassScanner { - public static List findClasses(List packages, Class type) { + private static void checkClassForInclusion(List cards, Class type, String name, ClassLoader cl) { + try { + Class clazz = Class.forName(name, true, cl); + if (clazz.getEnclosingClass() == null && type.isAssignableFrom(clazz)) { + cards.add(clazz); + } + } catch (ClassNotFoundException ex) { + // ignored + } + } + + public static List findClasses(ClassLoader classLoader, List packages, Class type) { List cards = new ArrayList<>(); try { - ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + if(classLoader == null) classLoader = Thread.currentThread().getContextClassLoader(); assert classLoader != null; HashMap dirs = new HashMap<>(); @@ -71,43 +82,35 @@ public class ClassScanner { } for (String filePath : dirs.keySet()) { - cards.addAll(findClasses(new File(filePath), dirs.get(filePath), type)); + cards.addAll(findClasses(classLoader, new File(filePath), dirs.get(filePath), type)); } for (String filePath : jars) { File file = new File(URLDecoder.decode(filePath, "UTF-8")); - cards.addAll(findClassesInJar(file, packages, type)); + cards.addAll(findClassesInJar(classLoader, file, packages, type)); } } catch (IOException ex) { } return cards; } - private static List findClasses(File directory, String packageName, Class type) { + private static List findClasses(ClassLoader classLoader, File directory, String packageName, Class type) { List cards = new ArrayList<>(); - if (!directory.exists()) { - return cards; - } + if (!directory.exists()) return cards; for (File file : directory.listFiles()) { if (file.getName().endsWith(".class")) { - try { - Class clazz = Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6)); - if (type.isAssignableFrom(clazz)) { - cards.add(clazz); - } - } catch (ClassNotFoundException ex) { - } + String name = packageName + '.' + file.getName().substring(0, file.getName().length() - 6); + checkClassForInclusion(cards, type, name, classLoader); } } return cards; } - private static List findClassesInJar(File file, List packages, Class type) { + private static List findClassesInJar(ClassLoader classLoader, File file, List packages, Class type) { List cards = new ArrayList<>(); - if (!file.exists()) { - return cards; - } + if (!file.exists()) return cards; + JarInputStream jarFile = null; try { jarFile = new JarInputStream(new FileInputStream(file)); @@ -120,22 +123,13 @@ public class ClassScanner { String className = jarEntry.getName().replace(".class", "").replace('/', '.'); int packageNameEnd = className.lastIndexOf('.'); String packageName = packageNameEnd != -1 ? className.substring(0, packageNameEnd) : ""; - if (packages.contains(packageName)) { - Class clazz; - try { - clazz = Class.forName(className); - if (type.isAssignableFrom(clazz)) { - cards.add(clazz); - } - } catch (ClassNotFoundException ex) { - } - } + if (packages.contains(packageName)) checkClassForInclusion(cards, type, className, classLoader); } } } catch (IOException ex) { } finally { try { - jarFile.close(); + if(jarFile != null) jarFile.close(); } catch (IOException ex) { } }