Support (but don't require) compiling to a custom destination directory.

git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@150 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Michael Bayne
2007-02-07 18:44:17 +00:00
parent aa9f74eed8
commit 8df7f7e0b3
6 changed files with 99 additions and 52 deletions
@@ -165,7 +165,7 @@ public class ComponentBundlerTask extends Task
}
};
System.out.println("Generating " + _target.getPath() + "...");
System.out.println("Generating " + _target + "...");
try {
// make sure we can create our bundle file
@@ -47,26 +47,37 @@ import com.threerings.jme.tools.xml.AnimationParser;
import com.threerings.jme.tools.xml.ModelParser;
/**
* An application for compiling 3D models defined in XML to fast-loading binary
* files.
* An application for compiling 3D models defined in XML to fast-loading binary files.
*/
public class CompileModel
{
/**
* Loads the model described by the given properties file and compiles it
* to a <code>.dat</code> file in the same directory.
* Loads the model described by the given properties file and compiles it to a
* <code>.dat</code> file in the same directory.
*
* @return the loaded model, or <code>null</code> if the compiled version
* is up-to-date
* @return the loaded model, or <code>null</code> if the compiled version is up-to-date
*/
public static Model compile (File source)
throws Exception
{
String spath = source.toString();
int didx = spath.lastIndexOf('.');
String root = (didx == -1) ? spath : spath.substring(0, didx);
File content = new File(root + ".mxml"),
target = new File(root + ".dat");
return compile(source, source.getParentFile());
}
/**
* Loads the model described by the given properties file and compiles it into a
* <code>.dat</code> file in the specified directory.
*
* @return the loaded model, or <code>null</code> if the compiled version is up-to-date
*/
public static Model compile (File source, File targetDir)
throws Exception
{
String sname = source.getName();
int didx = sname.lastIndexOf('.');
String root = (didx == -1) ? sname : sname.substring(0, didx);
File target = new File(targetDir, root + ".dat");
File content = new File(source.getParentFile(), root + ".mxml");
boolean needsUpdate = false;
if (source.lastModified() >= target.lastModified() ||
content.lastModified() >= target.lastModified()) {
@@ -78,10 +89,9 @@ public class CompileModel
FileInputStream in = new FileInputStream(source);
props.load(in);
in.close();
// locate the animations, if any
String[] anims =
StringUtil.parseStringArray(props.getProperty("animations", ""));
String[] anims = StringUtil.parseStringArray(props.getProperty("animations", ""));
File[] afiles = new File[anims.length];
File dir = source.getParentFile();
for (int ii = 0; ii < anims.length; ii++) {
@@ -93,14 +103,15 @@ public class CompileModel
if (!needsUpdate) {
return null;
}
System.out.println("Compiling " + source.getParent() + "...");
System.out.println("Compiling to " + target + "...");
// load the model content
ModelDef mdef = _mparser.parseModel(content.toString());
HashMap<String, Spatial> nodes = new HashMap<String, Spatial>();
Model model = mdef.createModel(props, nodes);
model.initPrototype();
// load the animations, if any
for (int ii = 0; ii < anims.length; ii++) {
System.out.println(" Adding " + afiles[ii] + "...");
@@ -108,32 +119,38 @@ public class CompileModel
model.addAnimation(anims[ii], adef.createAnimation(
PropertiesUtil.getSubProperties(props, anims[ii]), nodes));
}
// write and return the model
model.writeToFile(target);
File parent = target.getParentFile();
if (!parent.isDirectory() && !parent.mkdirs()) {
System.err.println("Unable to create target directory '" + parent + "'.");
} else {
model.writeToFile(target);
}
return model;
}
public static void main (String[] args)
{
if (args.length < 1) {
System.err.println("Usage: CompileModel source.properties");
System.exit(-1);
}
}
// create a dummy display system
new DummyDisplaySystem();
LoggingSystem.getLogger().setLevel(Level.WARNING);
try {
compile(new File(args[0]));
} catch (Exception e) {
System.err.println("Error compiling model: " + e);
}
}
/** A parser for the model definitions. */
protected static ModelParser _mparser = new ModelParser();
/** A parser for the animation definitions. */
protected static AnimationParser _aparser = new AnimationParser();
}
@@ -35,36 +35,40 @@ import com.jme.util.LoggingSystem;
import com.jmex.model.XMLparser.Converters.DummyDisplaySystem;
/**
* An ant task for compiling 3D models defined in XML to fast-loading binary
* files.
* An ant task for compiling 3D models defined in XML to fast-loading binary files.
*/
public class CompileModelTask extends Task
{
public void setDest (File dest)
{
_dest = dest;
}
public void addFileset (FileSet set)
{
_filesets.add(set);
}
public void init () throws BuildException
{
// create a dummy display system
new DummyDisplaySystem();
LoggingSystem.getLogger().setLevel(Level.WARNING);
}
public void execute ()
throws BuildException
{
for (int ii = 0, nn = _filesets.size(); ii < nn; ii++) {
FileSet fs = _filesets.get(ii);
String baseDir = getProject().getBaseDir().getPath();
for (FileSet fs : _filesets) {
DirectoryScanner ds = fs.getDirectoryScanner(getProject());
File fromDir = fs.getDir(getProject());
String[] srcFiles = ds.getIncludedFiles();
for (int f = 0; f < srcFiles.length; f++) {
File source = new File(fromDir, srcFiles[f]);
for (String file : ds.getIncludedFiles()) {
File source = new File(fromDir, file);
File destDir = (_dest == null) ? source.getParentFile() :
new File(source.getParent().replaceAll(baseDir, _dest.getPath()));
try {
CompileModel.compile(source);
CompileModel.compile(source, destDir);
} catch (Exception e) {
System.err.println("Error compiling " + source + ": " + e);
}
@@ -72,6 +76,10 @@ public class CompileModelTask extends Task
}
}
/** The directory in which we will generate our model output (in a directory tree mirroring the
* source files. */
protected File _dest;
/** A list of filesets that contain XML models. */
protected ArrayList<FileSet> _filesets = new ArrayList<FileSet>();
}
@@ -65,8 +65,7 @@ public class ConvertModelTask extends Task
public void execute () throws BuildException
{
for (int i = 0; i < _filesets.size(); i++) {
FileSet fs = (FileSet)_filesets.get(i);
for (FileSet fs : _filesets) {
DirectoryScanner ds = fs.getDirectoryScanner(getProject());
File fromDir = fs.getDir(getProject());
String[] srcFiles = ds.getIncludedFiles();
@@ -131,5 +130,5 @@ public class ConvertModelTask extends Task
}
/** A list of filesets that contain tileset bundle definitions. */
protected ArrayList _filesets = new ArrayList();
protected ArrayList<FileSet> _filesets = new ArrayList<FileSet>();
}
@@ -117,10 +117,9 @@ import com.threerings.jme.model.TextureProvider;
import com.threerings.jme.util.SpatialVisitor;
/**
* A simple viewer application that allows users to examine models and their
* animations by loading them from their uncompiled <code>.properties</code> /
* <code>.mxml</code> representations or their compiled <code>.dat</code>
* representations.
* A simple viewer application that allows users to examine models and their animations by loading
* them from their uncompiled <code>.properties</code> / <code>.mxml</code> representations or
* their compiled <code>.dat</code> representations.
*/
public class ModelViewer extends JmeCanvasApp
{
@@ -610,6 +609,7 @@ public class ModelViewer extends JmeCanvasApp
setModel(model, file);
return;
}
// if compileModel returned null, the .dat file is up-to-date
String fpath = file.toString();
int didx = fpath.lastIndexOf('.');
@@ -57,6 +57,11 @@ public class CompiledConfigTask extends Task
_target = target;
}
public void setDest (File dest)
{
_dest = dest;
}
public void addFileset (FileSet set)
{
_filesets.add(set);
@@ -79,22 +84,32 @@ public class CompiledConfigTask extends Task
// if we have a single file and target specified, do those
if (_configdef != null) {
parse(parser, _configdef, _target);
parse(parser, _configdef, _target == null ? getTarget(_configdef) : _target);
}
// deal with the filesets
for (Iterator iter = _filesets.iterator(); iter.hasNext(); ) {
FileSet fs = (FileSet)iter.next();
for (FileSet fs : _filesets) {
DirectoryScanner ds = fs.getDirectoryScanner(getProject());
File fromDir = fs.getDir(getProject());
String[] srcFiles = ds.getIncludedFiles();
for (int ii = 0; ii < srcFiles.length; ii++) {
File confdef = new File(fromDir, srcFiles[ii]);
parse(parser, confdef, null);
for (String file : ds.getIncludedFiles()) {
File source = new File(fromDir, file);
parse(parser, source, getTarget(source));
}
}
}
protected File getTarget (File source)
{
if (_dest == null) {
return null;
}
String baseDir = getProject().getBaseDir().getPath();
File target = new File(source.getPath().replaceAll(baseDir, _dest.getPath()));
target = new File(FileUtil.resuffix(target, ".xml", ".dat"));
return target;
}
protected void parse (CompiledConfigParser parser, File confdef, File target)
throws BuildException
{
@@ -109,7 +124,7 @@ public class CompiledConfigTask extends Task
target = new File(FileUtil.resuffix(confdef, ".xml", ".dat"));
}
System.out.println("Compiling " + confdef + "...");
System.out.println("Compiling " + target + "...");
Serializable config = null;
try {
// parse it on up
@@ -117,6 +132,13 @@ public class CompiledConfigTask extends Task
} catch (Exception e) {
throw new BuildException("Failure parsing config definition", e);
}
// create the target directory if necessary
File parent = target.getParentFile();
if (!parent.isDirectory() && !parent.mkdirs()) {
throw new BuildException("Failed to create parent directory '" + parent + "'.");
}
try {
// and write it on out
CompiledConfig.saveConfig(target, config);
@@ -127,6 +149,7 @@ public class CompiledConfigTask extends Task
protected File _configdef;
protected File _target;
protected File _dest;
protected String _parser;
protected ArrayList _filesets = new ArrayList();
protected ArrayList<FileSet> _filesets = new ArrayList<FileSet>();
}