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