Add custom set loader code.

This commit is contained in:
Lymia Aluysia 2016-09-24 15:16:20 -05:00
parent 74a017586a
commit d678fc1a59
No known key found for this signature in database
GPG key ID: DB2E204C989251F7
3 changed files with 192 additions and 4 deletions

View file

@ -0,0 +1,70 @@
/*
* Copyright 2016 Lymia <lymia@lymiahugs.com>. 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);
}
}
}

View file

@ -0,0 +1,74 @@
/*
* Copyright 2016 Lymia <lymia@lymiahugs.com>. 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<ExpansionSet> expansions = new ArrayList<>();
protected Map<String, Class> deckTypes = new HashMap<>();
protected Map<String, Class> draftCubes = new HashMap<>();
/**
* @return A list of expansions included in this custom set package.
*/
public List<ExpansionSet> getSets() {
return expansions;
}
/**
* @return A list of draft cubes included in this custom set package.
*/
public Map<String, Class> getDraftCubes() {
return draftCubes;
}
/**
* @return A list of deck types included in this custom set package.
*/
public Map<String, Class> getDeckTypes() {
return deckTypes;
}
/**
* @return The classloader cards should be loaded from for this custom set package.
*/
public ClassLoader getClassLoader() {
return getClass().getClassLoader();
}
}

View file

@ -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<CustomSetPackage> 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<String, Class> draftCubes = pkg.getDraftCubes();
for (String name : draftCubes.keySet())
CubeFactory.getInstance().addDraftCube(name, draftCubes.get(name));
Map<String, Class> 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) {