From d678fc1a59cc15702e057d5f69fc2850f6a91b25 Mon Sep 17 00:00:00 2001 From: Lymia Aluysia Date: Sat, 24 Sep 2016 15:16:20 -0500 Subject: [PATCH] Add custom set loader code. --- .../java/mage/server/CustomSetLoader.java | 70 ++++++++++++++++++ .../java/mage/server/CustomSetPackage.java | 74 +++++++++++++++++++ .../src/main/java/mage/server/Main.java | 52 ++++++++++++- 3 files changed, 192 insertions(+), 4 deletions(-) create mode 100644 Mage.Server/src/main/java/mage/server/CustomSetLoader.java create mode 100644 Mage.Server/src/main/java/mage/server/CustomSetPackage.java diff --git a/Mage.Server/src/main/java/mage/server/CustomSetLoader.java b/Mage.Server/src/main/java/mage/server/CustomSetLoader.java new file mode 100644 index 0000000000..acf2a4efbe --- /dev/null +++ b/Mage.Server/src/main/java/mage/server/CustomSetLoader.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 CustomSetLoader { + public static CustomSetPackage loadCustomSet(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 (CustomSetPackage) Class.forName(entryPoint, false, classLoader).newInstance(); + } catch (InstantiationException | IllegalAccessException e) { + throw new RuntimeException(e); + } catch (ClassNotFoundException e) { + throw new RuntimeException("Entry point file not found!", e); + } catch (ClassCastException e) { + throw new RuntimeException("Entry point not an instance of CustomSetPackage.", e); + } + } +} diff --git a/Mage.Server/src/main/java/mage/server/CustomSetPackage.java b/Mage.Server/src/main/java/mage/server/CustomSetPackage.java new file mode 100644 index 0000000000..bd09487c2d --- /dev/null +++ b/Mage.Server/src/main/java/mage/server/CustomSetPackage.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 containing custom sets. + * @author Lymia + */ +public abstract class CustomSetPackage { + 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/Main.java b/Mage.Server/src/main/java/mage/server/Main.java index e787d5b357..829a19db97 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 customSetsFolder = new File("customSets"); public static PluginClassLoader classLoader = new PluginClassLoader(); public static TransporterServer server; @@ -109,6 +118,31 @@ public class Main { } } + logger.info("Loading custom set packages..."); + List customSets = new ArrayList<>(); + if(!customSetsFolder.exists()) if(!customSetsFolder.mkdirs()) + logger.error("Could not create custom sets directory."); + File[] customSetDirectories = customSetsFolder.listFiles(); + if(customSetDirectories != null) for(File f : customSetDirectories) if(f.isDirectory()) + try { + customSets.add(CustomSetLoader.loadCustomSet(f)); + } catch (IOException e) { + logger.error("Could not load custom package in "+f+"!", e); + } + logger.info("Done."); + + if(!customSets.isEmpty()) { + logger.info("Registering custom sets..."); + for(CustomSetPackage pkg : customSets) { + 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 +173,16 @@ public class Main { DeckValidatorFactory.getInstance().addDeckType(plugin.getName(), loadPlugin(plugin)); } + for (CustomSetPackage pkg : customSets) { + Map draftCubes = pkg.getDraftCubes(); + for (String name : draftCubes.keySet()) + CubeFactory.getInstance().addDraftCube(name, draftCubes.get(name)); + + Map deckTypes = pkg.getDeckTypes(); + for (String name : deckTypes.keySet()) + 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 +360,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 +373,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 +386,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) {