Changed mod checking to occur at start

any element can now contain an attribute mods which should be a comma
seperated list of mods that are required
This commit is contained in:
Francesco Macagno 2015-08-20 22:43:35 -07:00
parent 19372a61a6
commit 3dc37dd969
3 changed files with 72 additions and 43 deletions

View file

@ -126,6 +126,9 @@ public class FillerManager {
return;
}
ModRequirementChecker.doModReqSanitation(base);
NodeList ores = base.getElementsByTagName("ore");
for (int i = 0; i < ores.getLength(); i++) {

View file

@ -1,42 +1,68 @@
package cr0s.warpdrive.conf;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import cpw.mods.fml.common.Loader;
import cr0s.warpdrive.WarpDrive;
public class ModRequirementChecker {
/**
* Will check the given element for any mod tags on the first level and return a string of all the ones that are not loaded, separated by commas
* Will check the given element for a mod attribute and return a string of all the ones that are not loaded, separated by commas
*
* @param e
* Element to check
* @return A string, which is empty if all the mods are loaded.
* @throws InvalidXmlException
*/
public static String checkModRequirements(Element e) throws InvalidXmlException {
public static String checkModRequirements(Element e) {
String missingMods = "";
NodeList mods = e.getElementsByTagName("mod");
for (int j = 0; j < mods.getLength(); j++) {
Element mod = (Element) mods.item(j);
for (String mod : e.getAttribute("mods").split(",")) {
if (!mod.hasAttribute("name")) {
throw new InvalidXmlException("A mod requirement is missing the name attribute!");
}
//TODO: add version check
String name = mod.getAttribute("name");
if (!Loader.isModLoaded(name)) {
missingMods = missingMods + name + ", ";
if (!Loader.isModLoaded(mod)) {
missingMods = missingMods + mod + ", ";
}
}
return missingMods;
}
/**
* Goes through every child node of the given node, and if it is an element and contains
*
* @param base
* @throws InvalidXmlException
*/
public static void doModReqSanitation(Node base) {
NodeList children = base.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
if (child instanceof Element) {
String res = checkModRequirements((Element) child);
if (!res.isEmpty()) {
base.removeChild(child);
WarpDrive.logger.info("Removed child element " + child.getBaseURI() + " of element " + base.getBaseURI() + " because these mods arent loaded: " + res);
} else {
doModReqSanitation(child);
}
}
}
}
}

View file

@ -18,76 +18,76 @@ import cr0s.warpdrive.conf.WarpDriveConfig;
public class StructureManager {
private static ArrayList<Star> stars = new ArrayList<Star>();
private static ArrayList<Planetoid> moons = new ArrayList<Planetoid>();
private static ArrayList<Planetoid> gasClouds = new ArrayList<Planetoid>();
public static void loadStructures(String structureConfDir) {
loadStructures(new File(structureConfDir));
}
public static void loadStructures(File dir) {
dir.mkdir();
if (!dir.isDirectory()) {
throw new IllegalArgumentException("File path " + dir.getPath() + " must be a directory!");
}
File[] files = dir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File file_notUsed, String name) {
return name.startsWith("structure") && name.endsWith(".xml");
}
});
for (File file : files) {
try {
WarpDrive.logger.info("Loading structure data file " + file.getPath());
loadXmlStructureFile(file);
WarpDrive.logger.info("Finished loading structure data file " + file.getPath());
} catch (Exception e) {
WarpDrive.logger.error("Error loading file " + file.getPath() + ": " + e.getMessage());
e.printStackTrace();
}
}
}
private static void loadXmlStructureFile(File f) throws SAXException, IOException, InvalidXmlException {
Document base = WarpDriveConfig.getXmlDocumentBuilder().parse(f);
String res = ModRequirementChecker.checkModRequirements(base.getDocumentElement());
String res = ModRequirementChecker.checkModRequirements(base.getDocumentElement());
if (!res.isEmpty()) {
WarpDrive.logger.info("Skippping structure data file " + f.getPath() + " because the mods " + res + " are not loaded");
return;
}
ModRequirementChecker.doModReqSanitation(base);
NodeList structures = base.getElementsByTagName("structure");
for (int i = 0; i < structures.getLength(); i++) {
Element struct = (Element) structures.item(i);
String type = struct.getAttribute("type");
String name = struct.getAttribute("name");
if(type.isEmpty())
throw new InvalidXmlException("Structure must have a type!");
int radius;
try {
radius = Integer.parseInt(struct.getAttribute("radius"));
} catch(NumberFormatException e) {
throw new InvalidXmlException("Structure radius is invalid!");
}
if (type.equalsIgnoreCase("star")) {
Star s = new Star(radius);
s.loadFromXmlElement(struct);
@ -98,7 +98,7 @@ public class StructureManager {
}
}
}
public static DeployableStructure getStructure(Random random, final String name, final String type) {
if (name == null || name.length() == 0) {
if (type == null || type.length() == 0) {
@ -114,19 +114,19 @@ public class StructureManager {
return star;
}
}
// not found or nothing defined => return null
return null;
}
public static DeployableStructure getStar(Random random, final String name) {
return getStructure(random, name, "star");
}
public static DeployableStructure getMoon(Random random, final String name) {
return getStructure(random, name, "moon");
}
public static DeployableStructure getGasCloud(Random random, final String name) {
return getStructure(random, name, "cloud");
}