Systems with no Java compiler will not try to compile files. Default abilities are now pre-compiled for compatibility with such systems. build.xml updated to reflect changes.

This commit is contained in:
garbagemule
2012-03-01 22:13:36 +01:00
parent a3fa0539ce
commit 353109c89a
5 changed files with 180 additions and 75 deletions
BIN
View File
Binary file not shown.
+21 -9
View File
@@ -30,14 +30,11 @@
<property name="msjar" value="MagicSpells.jar" /> <property name="msjar" value="MagicSpells.jar" />
<property name="msurl" value="http://dev.bukkit.org/media/files/575/866/${msjar}" /> <property name="msurl" value="http://dev.bukkit.org/media/files/575/866/${msjar}" />
<path id="classpath"> <path id="classpath">
<fileset dir="${lib}" includes="*.jar" /> <fileset dir="${lib}" includes="*.jar" />
<!--<fileset dir="${res}" includes="**/*.*" />-->
<pathelement location="${bin}" /> <pathelement location="${bin}" />
</path> </path>
<fileset id="" dir="${src}">
</fileset>
<target name="clean"> <target name="clean">
<delete dir="${bin}" /> <delete dir="${bin}" />
@@ -51,18 +48,22 @@
<mkdir dir="${lib}" /> <mkdir dir="${lib}" />
</target> </target>
<target name="copy-abilities"> <target name="prepare-abilities">
<mkdir dir="${ability-dest}" />
</target>
<!--<target name="copy-abilities">
<mkdir dir="${ability-dest}" /> <mkdir dir="${ability-dest}" />
<copy toDir="${ability-dest}"> <copy toDir="${ability-dest}">
<fileset dir="${ability-src}" /> <fileset dir="${ability-src}" />
</copy> </copy>
</target> </target>-->
<target name="cleanup-abilities"> <target name="cleanup-abilities">
<delete dir="${ability-dest}" /> <delete dir="${ability-dest}" />
</target> </target>
<!-- Compile the source and put in the bin-folder --> <!-- Builds the source code -->
<target name="build-src" depends="prepare-bin"> <target name="build-src" depends="prepare-bin">
<javac target="1.6" source="1.6" <javac target="1.6" source="1.6"
srcdir="${src}" destdir="${bin}" srcdir="${src}" destdir="${bin}"
@@ -72,6 +73,7 @@
</javac> </javac>
</target> </target>
<!-- Builds the source code and shows deprecation issues -->
<target name="build-src-with-deprecation-check" depends="prepare-bin"> <target name="build-src-with-deprecation-check" depends="prepare-bin">
<javac target="1.6" source="1.6" <javac target="1.6" source="1.6"
srcdir="${src}" destdir="${bin}" srcdir="${src}" destdir="${bin}"
@@ -81,9 +83,19 @@
<compilerarg value="-Xbootclasspath/p:${toString:classpath}"/> <compilerarg value="-Xbootclasspath/p:${toString:classpath}"/>
</javac> </javac>
</target> </target>
<!-- Builds the ability source code and places it in the resources folder -->
<target name="build-abilities" depends="prepare-abilities">
<javac target="1.6" source="1.6"
srcdir="${ability-src}" destdir="${ability-dest}"
debug="on" debuglevel="lines,vars,source"
includeantruntime="no">
<compilerarg value="-Xbootclasspath/p:${toString:classpath}"/>
</javac>
</target>
<!-- Build a .jar and copy to server's plugins-folder --> <!-- Build a .jar and copy to server's plugins-folder -->
<target name="dist" depends="build-src-with-deprecation-check, copy-abilities"> <target name="dist" depends="build-src-with-deprecation-check, build-abilities">
<delete file="${pluginname}.jar" /> <delete file="${pluginname}.jar" />
<jar jarfile="${pluginname}.jar"> <jar jarfile="${pluginname}.jar">
<!-- Include the class-files (bin) and the resources (res) --> <!-- Include the class-files (bin) and the resources (res) -->
+1 -1
View File
@@ -1,7 +1,7 @@
name: MobArena name: MobArena
author: garbagemule author: garbagemule
main: com.garbagemule.MobArena.MobArena main: com.garbagemule.MobArena.MobArena
version: 0.94.4.49 version: 0.94.4.51
softdepend: [Spout,MultiVerse,XcraftGate,Towny,Heroes,MagicSpells,Vault] softdepend: [Spout,MultiVerse,XcraftGate,Towny,Heroes,MagicSpells,Vault]
commands: commands:
ma: ma:
@@ -4,7 +4,11 @@ import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List; import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
@@ -18,14 +22,32 @@ public class FileUtils
* Note that even if the resources have different paths, they will all * Note that even if the resources have different paths, they will all
* be extracted to the given directory. * be extracted to the given directory.
* @param dir a directory * @param dir a directory
* @param resources an array of resources to extract * @param resources a list of resources to extract
* @return a list of all the files that were written * @return a list of all the files that were written
*/ */
public static List<File> extractResources(File dir, String... resources) { public static List<File> extractResources(File dir, List<String> resources) {
return extractResources(dir, "", resources);
}
public static List<File> extractResources(File dir, String path, List<String> filenames) {
List<File> files = new ArrayList<File>(); List<File> files = new ArrayList<File>();
for (String resource : resources) { // If the path is empty, just forget about it.
File file = extractResource(dir, resource); if (!path.equals("")) {
// We want no leading slashes
if (path.startsWith("/")) {
path = path.substring(1);
}
// But we do want trailing slashes
if (!path.endsWith("/")) {
path = path + "/";
}
}
// Extract each resource
for (String filename : filenames) {
File file = extractResource(dir, path + filename);
if (file != null) { if (file != null) {
files.add(file); files.add(file);
@@ -33,6 +55,18 @@ public class FileUtils
} }
return files; return files;
} }
/**
* Extracts all of the given resources to the given directory.
* Convenience method, used if one is too lazy to create a new list for
* the resource names.
* @param dir a directory
* @param resources an array of resources to extract
* @return a list of all the files that were written
*/
public static List<File> extractResources(File dir, String... resources) {
return extractResources(dir, Arrays.asList(resources));
}
/** /**
* Extracts the given resource to the given directory. * Extracts the given resource to the given directory.
@@ -77,6 +111,53 @@ public class FileUtils
return null; return null;
} }
/**
* Lists all files in the MobArena jar file that exist on the given path.
* The resulting list contains only filenames (with extensions)
* @param path the path of the jar file to list files from
* @param ext the file extension of the file type, can be null or the empty string
* @return a list of file names
*/
public static List<String> listFilesOnPath(String path, String ext) {
try {
// If the jar can't be found for some odd reason, escape.
File file = new File("plugins" + File.separator + "MobArena.jar");
if (file == null || !file.exists()) return null;
// Create a JarFile out of the.. jar file
JarFile jarFile = new JarFile(file);
List<String> result = new ArrayList<String>();
// JarEntry names never start with /
if (path.startsWith("/")) {
path = path.substring(1);
}
// If null, replace with the empty string.
if (ext == null) {
ext = "";
}
// Require that extensions start with a period
else if (!ext.startsWith(".")) {
ext = "." + ext;
}
// Loop through all entries, add the ones that match the path and extension
for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements(); ) {
String name = entries.nextElement().getName();
if (name.startsWith(path) && name.endsWith(ext)) {
result.add(getFilename(name));
}
}
return result;
}
catch (Exception e) {
e.printStackTrace();
return new ArrayList<String>(1);
}
}
private static String getFilename(String resource) { private static String getFilename(String resource) {
int slash = resource.lastIndexOf("/"); int slash = resource.lastIndexOf("/");
return (slash < 0 ? resource : resource.substring(slash + 1)); return (slash < 0 ? resource : resource.substring(slash + 1));
@@ -41,52 +41,47 @@ public class AbilityManager
public static void loadAbilities(File classDir) { public static void loadAbilities(File classDir) {
abilities = new HashMap<String,Ability>(); abilities = new HashMap<String,Ability>();
// Extract all of the source files. // Grab the source directory.
File javaDir = new File(classDir, "src"); File javaDir = new File(classDir, "src");
extractSourceFiles(javaDir);
// The custom abilities to compile will be in the 'src'-dir /* If the source directory exists, we need to verify that the system
compileAbilities(javaDir, classDir); * has a java compiler before attempting anything. If not, we need to
* skip the compiling step and just go straight to loading in the
* existing class files. */
if (javaDir.exists()) {
if (ToolProvider.getSystemJavaCompiler() != null) {
compileAbilities(javaDir, classDir);
} else {
Messenger.warning("Found plugins/MobArena/abilites/src/ folder, but no Java compiler. The source files will not be compiled!");
}
}
/* If there is only one file in the directory, make sure it isn't the
* src/ folder, in which case there will be no .class files to load.
* In the case of no .class files, extract the defaults. */
String[] files = classDir.list();
if (files.length == 0 || (files.length == 1 && files[0].equals("src"))) {
Messenger.info("No boss abilities found. Extracting defaults...");
extractDefaultAbilities(classDir);
}
// Load all the custom abilities. // Load all the custom abilities.
loadClasses(classDir); loadClasses(classDir);
} }
private static void extractSourceFiles(File javaDir) { private static void extractDefaultAbilities(File classDir) {
// Only extract the files if the folder doesn't exist. // Grab a list of all the class files.
if (javaDir.exists()) return; List<String> resources = FileUtils.listFilesOnPath("res/abilities/", ".class");
String path = "abilities/"; // Check that there is stuff to extract.
if (resources == null || resources.isEmpty()) {
Messenger.severe("Couldn't extract the default boss abilities!");
return;
}
List<String> files = new ArrayList<String>(); // Extract everything.
List<File> files = FileUtils.extractResources(classDir, "abilities/", resources);
files.add(path + "ChainLightning.java"); Messenger.info("Extracted abilities: " + fileListToString(files, "$"));
files.add(path + "LivingBomb.java");
files.add(path + "ObsidianBomb.java");
files.add(path + "Flood.java");
files.add(path + "WarpToPlayer.java");
files.add(path + "RootTarget.java");
files.add(path + "ShufflePositions.java");
files.add(path + "ShootArrow.java");
files.add(path + "ShootFireball.java");
files.add(path + "FireAura.java");
files.add(path + "LightningAura.java");
files.add(path + "DisorientDistant.java");
files.add(path + "DisorientNearby.java");
files.add(path + "DisorientTarget.java");
files.add(path + "PullDistant.java");
files.add(path + "PullNearby.java");
files.add(path + "PullTarget.java");
files.add(path + "ThrowDistant.java");
files.add(path + "ThrowNearby.java");
files.add(path + "ThrowTarget.java");
FileUtils.extractResources(javaDir, files.toArray(new String[0]));
} }
private static void compileAbilities(File javaDir, File classDir) { private static void compileAbilities(File javaDir, File classDir) {
@@ -100,27 +95,33 @@ public class AbilityManager
return; return;
} }
// Get the compiler and the file manager // Notify the console.
Messenger.info("Compiling abilities: " + fileListToString(toCompile));
// Get the compiler
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
// Generate some JavaFileObjects // Generate some JavaFileObjects
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(toCompile);
// Include the MobArena.jar on the classpath, and set the destination folder.
List<String> options = Arrays.asList("-classpath", classpath, "-d", classDir.getPath());
// Set up the compilation task.
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options, null, compilationUnits);
// Call the task.
task.call();
// And close the file manager.
try { try {
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(toCompile);
// Include the MobArena.jar on the classpath, and set the destination folder.
List<String> options = Arrays.asList("-classpath", classpath, "-d", classDir.getPath());
// Set up the compilation task.
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options, null, compilationUnits);
// Call the task.
task.call();
// And close the file manager.
fileManager.close(); fileManager.close();
} }
catch (Exception e) {} catch (Exception e) {
Messenger.severe("Compilation step failed...");
e.printStackTrace();
}
} }
private static List<File> getSourceFilesToCompile(File javaDir, File classDir) { private static List<File> getSourceFilesToCompile(File javaDir, File classDir) {
@@ -264,16 +265,27 @@ public class AbilityManager
return null; return null;
} }
/** private static String fileListToString(List<File> list) {
* Turn an array of aliases into a comma-separated string of aliases. return fileListToString(list, null);
* @param aliases an array of Strings }
* @return a comma-separated String
*/ private static String fileListToString(List<File> list, String exclude) {
/*private static String aliasString(String[] aliases) { if (list.isEmpty()) return "";
StringBuffer buffy = new StringBuffer(); StringBuffer buffy = new StringBuffer();
for (String a : aliases) {
buffy.append(a + ", "); for (File file : list) {
String name = file.getName();
int dot = name.lastIndexOf(".");
if (exclude != null && name.contains(exclude)) {
continue;
}
buffy.append(", " + name.substring(0, dot));
} }
return buffy.substring(0, buffy.length() - 2);
}*/ // Trim off the first ", ".
return buffy.substring(2);
}
} }