Allow TileSetRuleSets to specify a custom tileset tag name, and use that to allow UniformTileSets as <uniformTileset> in addition to the existing SwissArmyTileSets as <tileset> in the bundle tasks.

git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@281 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Charlie Groves
2007-08-02 23:18:16 +00:00
parent 1f435d582d
commit 205d57a7fe
9 changed files with 94 additions and 81 deletions
@@ -22,8 +22,6 @@
package com.threerings.cast.bundle.tools; package com.threerings.cast.bundle.tools;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
@@ -35,38 +33,37 @@ import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.jar.JarOutputStream;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.zip.Deflater; import java.util.zip.Deflater;
import javax.imageio.ImageIO;
import org.apache.commons.digester.Digester; import org.apache.commons.digester.Digester;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.FileSet;
import com.samskivert.io.PersistenceException; import com.samskivert.io.PersistenceException;
import com.samskivert.util.ComparableArrayList; import com.samskivert.util.ComparableArrayList;
import com.samskivert.util.FileUtil; import com.samskivert.util.FileUtil;
import com.samskivert.util.HashIntMap; import com.samskivert.util.HashIntMap;
import com.samskivert.util.Tuple; import com.samskivert.util.Tuple;
import com.threerings.cast.ComponentIDBroker;
import org.apache.tools.ant.BuildException; import com.threerings.cast.StandardActions;
import org.apache.tools.ant.DirectoryScanner; import com.threerings.cast.bundle.BundleUtil;
import org.apache.tools.ant.Task; import com.threerings.cast.tools.xml.ActionRuleSet;
import org.apache.tools.ant.types.FileSet;
import com.threerings.media.tile.ImageProvider; import com.threerings.media.tile.ImageProvider;
import com.threerings.media.tile.SimpleCachingImageProvider; import com.threerings.media.tile.SimpleCachingImageProvider;
import com.threerings.media.tile.TileSet; import com.threerings.media.tile.TileSet;
import com.threerings.media.tile.TrimmedTileSet; import com.threerings.media.tile.TrimmedTileSet;
import com.threerings.media.tile.tools.xml.SwissArmyTileSetRuleSet; import com.threerings.media.tile.tools.xml.SwissArmyTileSetRuleSet;
import com.threerings.media.tile.tools.xml.TileSetRuleSet;
import com.threerings.cast.ComponentIDBroker; import com.threerings.media.tile.tools.xml.UniformTileSetRuleSet;
import com.threerings.cast.StandardActions;
import com.threerings.cast.bundle.BundleUtil;
import com.threerings.cast.tools.xml.ActionRuleSet;
/** /**
* Ant task for creating component bundles. This task must be configured * Ant task for creating component bundles. This task must be configured
@@ -400,6 +397,16 @@ public class ComponentBundlerTask extends Task
} }
} }
/**
* Configures <code>ruleSet</code> and hooks it into <code>digester</code>.
*/
protected static void addTileSetRuleSet (Digester digester, TileSetRuleSet ruleSet)
{
ruleSet.setPrefix("actions" + ActionRuleSet.ACTION_PATH);
digester.addRuleSet(ruleSet);
digester.addSetNext(ruleSet.getPath(), "addTileSet", TileSet.class.getName());
}
/** /**
* Parses the action tileset definitions and puts them into a hash * Parses the action tileset definitions and puts them into a hash
* map, keyed on action name. * map, keyed on action name.
@@ -407,14 +414,10 @@ public class ComponentBundlerTask extends Task
protected HashMap parseActionTileSets () protected HashMap parseActionTileSets ()
{ {
Digester digester = new Digester(); Digester digester = new Digester();
SwissArmyTileSetRuleSet srules = new SwissArmyTileSetRuleSet(); digester.addSetProperties("actions" + ActionRuleSet.ACTION_PATH);
String aprefix = "actions" + ActionRuleSet.ACTION_PATH; addTileSetRuleSet(digester, new SwissArmyTileSetRuleSet());
srules.setPrefix(aprefix); addTileSetRuleSet(digester, new UniformTileSetRuleSet("/uniformTileset"));
digester.addRuleSet(srules);
digester.addSetProperties(aprefix);
digester.addSetNext(aprefix + SwissArmyTileSetRuleSet.TILESET_PATH,
"addTileSet", TileSet.class.getName());
HashMap actsets = new ActionMap(); HashMap actsets = new ActionMap();
digester.push(actsets); digester.push(actsets);
@@ -28,30 +28,26 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.jar.JarOutputStream;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.zip.Deflater; import java.util.zip.Deflater;
import org.apache.commons.digester.Digester;
import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task; import org.apache.tools.ant.Task;
import org.apache.commons.digester.Digester;
import com.samskivert.util.Tuple; import com.samskivert.util.Tuple;
import com.threerings.media.tile.TileSet;
import com.threerings.media.tile.tools.xml.SwissArmyTileSetRuleSet;
import com.threerings.cast.ActionSequence; import com.threerings.cast.ActionSequence;
import com.threerings.cast.ComponentClass; import com.threerings.cast.ComponentClass;
import com.threerings.cast.bundle.BundleUtil; import com.threerings.cast.bundle.BundleUtil;
import com.threerings.cast.tools.xml.ActionRuleSet; import com.threerings.cast.tools.xml.ActionRuleSet;
import com.threerings.cast.tools.xml.ClassRuleSet; import com.threerings.cast.tools.xml.ClassRuleSet;
import com.threerings.media.tile.TileSet;
import com.threerings.media.tile.tools.xml.SwissArmyTileSetRuleSet;
import com.threerings.media.tile.tools.xml.TileSetRuleSet;
import com.threerings.media.tile.tools.xml.UniformTileSetRuleSet;
/** /**
* Ant task for creating metadata bundles, which contain action sequence * Ant task for creating metadata bundles, which contain action sequence
@@ -150,6 +146,16 @@ public class MetadataBundlerTask extends Task
} }
} }
/**
* Configures <code>ruleSet</code> and hooks it into <code>digester</code>.
*/
protected static void addTileSetRuleSet (Digester digester, TileSetRuleSet ruleSet)
{
ruleSet.setPrefix("actions" + ActionRuleSet.ACTION_PATH);
digester.addRuleSet(ruleSet);
digester.addSetNext(ruleSet.getPath(), "add", Object.class.getName());
}
protected Tuple parseActions () protected Tuple parseActions ()
throws BuildException throws BuildException
{ {
@@ -158,18 +164,13 @@ public class MetadataBundlerTask extends Task
ActionRuleSet arules = new ActionRuleSet(); ActionRuleSet arules = new ActionRuleSet();
arules.setPrefix("actions"); arules.setPrefix("actions");
digester.addRuleSet(arules); digester.addRuleSet(arules);
digester.addSetNext("actions" + ActionRuleSet.ACTION_PATH, digester.addSetNext("actions" + ActionRuleSet.ACTION_PATH, "add", Object.class.getName());
"add", Object.class.getName());
ArrayList actlist = parseList(digester, _actionDef); ArrayList actlist = parseList(digester, _actionDef);
// now go through a second time reading the tileset info // now go through a second time reading the tileset info
digester = new Digester(); digester = new Digester();
SwissArmyTileSetRuleSet srules = new SwissArmyTileSetRuleSet(); addTileSetRuleSet(digester, new SwissArmyTileSetRuleSet());
srules.setPrefix("actions" + ActionRuleSet.ACTION_PATH); addTileSetRuleSet(digester, new UniformTileSetRuleSet("/uniformTileset"));
digester.addRuleSet(srules);
digester.addSetNext("actions" + ActionRuleSet.ACTION_PATH +
SwissArmyTileSetRuleSet.TILESET_PATH, "add",
Object.class.getName());
ArrayList setlist = parseList(digester, _actionDef); ArrayList setlist = parseList(digester, _actionDef);
// sanity check // sanity check
@@ -169,8 +169,7 @@ public class TileSetBundler
for (int i = 0; i < msize; i++) { for (int i = 0; i < msize; i++) {
Mapping map = (Mapping)mappings.get(i); Mapping map = (Mapping)mappings.get(i);
try { try {
TileSetRuleSet ruleset = (TileSetRuleSet) TileSetRuleSet ruleset = (TileSetRuleSet)Class.forName(map.ruleset).newInstance();
Class.forName(map.ruleset).newInstance();
// configure the ruleset // configure the ruleset
ruleset.setPrefix(map.path); ruleset.setPrefix(map.path);
@@ -178,8 +177,7 @@ public class TileSetBundler
_digester.addRuleSet(ruleset); _digester.addRuleSet(ruleset);
// and add a rule to stick the parsed tilesets onto the // and add a rule to stick the parsed tilesets onto the
// end of an array list that we'll put on the stack // end of an array list that we'll put on the stack
_digester.addSetNext(map.path + TileSetRuleSet.TILESET_PATH, _digester.addSetNext(ruleset.getPath(), "add", "java.lang.Object");
"add", "java.lang.Object");
} catch (Exception e) { } catch (Exception e) {
String errmsg = "Unable to create tileset rule set " + String errmsg = "Unable to create tileset rule set " +
@@ -66,7 +66,7 @@ public class ObjectTileSetRuleSet extends SwissArmyTileSetRuleSet
super.addRuleInstances(digester); super.addRuleInstances(digester);
digester.addRule( digester.addRule(
_prefix + TILESET_PATH + "/objectWidths", _path + "/objectWidths",
new CallMethodSpecialRule() { new CallMethodSpecialRule() {
public void parseAndSet (String bodyText, Object target) public void parseAndSet (String bodyText, Object target)
{ {
@@ -76,7 +76,7 @@ public class ObjectTileSetRuleSet extends SwissArmyTileSetRuleSet
}); });
digester.addRule( digester.addRule(
_prefix + TILESET_PATH + "/objectHeights", _path + "/objectHeights",
new CallMethodSpecialRule() { new CallMethodSpecialRule() {
public void parseAndSet (String bodyText, Object target) public void parseAndSet (String bodyText, Object target)
{ {
@@ -86,7 +86,7 @@ public class ObjectTileSetRuleSet extends SwissArmyTileSetRuleSet
}); });
digester.addRule( digester.addRule(
_prefix + TILESET_PATH + "/xOrigins", new CallMethodSpecialRule() { _path + "/xOrigins", new CallMethodSpecialRule() {
public void parseAndSet (String bodyText, Object target) public void parseAndSet (String bodyText, Object target)
{ {
int[] xorigins = StringUtil.parseIntArray(bodyText); int[] xorigins = StringUtil.parseIntArray(bodyText);
@@ -95,7 +95,7 @@ public class ObjectTileSetRuleSet extends SwissArmyTileSetRuleSet
}); });
digester.addRule( digester.addRule(
_prefix + TILESET_PATH + "/yOrigins", new CallMethodSpecialRule() { _path + "/yOrigins", new CallMethodSpecialRule() {
public void parseAndSet (String bodyText, Object target) public void parseAndSet (String bodyText, Object target)
{ {
int[] yorigins = StringUtil.parseIntArray(bodyText); int[] yorigins = StringUtil.parseIntArray(bodyText);
@@ -104,7 +104,7 @@ public class ObjectTileSetRuleSet extends SwissArmyTileSetRuleSet
}); });
digester.addRule( digester.addRule(
_prefix + TILESET_PATH + "/priorities", _path + "/priorities",
new CallMethodSpecialRule() { new CallMethodSpecialRule() {
public void parseAndSet (String bodyText, Object target) public void parseAndSet (String bodyText, Object target)
{ {
@@ -114,7 +114,7 @@ public class ObjectTileSetRuleSet extends SwissArmyTileSetRuleSet
}); });
digester.addRule( digester.addRule(
_prefix + TILESET_PATH + "/zations", new CallMethodSpecialRule() { _path + "/zations", new CallMethodSpecialRule() {
public void parseAndSet (String bodyText, Object target) public void parseAndSet (String bodyText, Object target)
{ {
String[] zations = StringUtil.parseStringArray(bodyText); String[] zations = StringUtil.parseStringArray(bodyText);
@@ -123,7 +123,7 @@ public class ObjectTileSetRuleSet extends SwissArmyTileSetRuleSet
}); });
digester.addRule( digester.addRule(
_prefix + TILESET_PATH + "/xspots", new CallMethodSpecialRule() { _path + "/xspots", new CallMethodSpecialRule() {
public void parseAndSet (String bodyText, Object target) public void parseAndSet (String bodyText, Object target)
{ {
short[] xspots = StringUtil.parseShortArray(bodyText); short[] xspots = StringUtil.parseShortArray(bodyText);
@@ -132,7 +132,7 @@ public class ObjectTileSetRuleSet extends SwissArmyTileSetRuleSet
}); });
digester.addRule( digester.addRule(
_prefix + TILESET_PATH + "/yspots", new CallMethodSpecialRule() { _path + "/yspots", new CallMethodSpecialRule() {
public void parseAndSet (String bodyText, Object target) public void parseAndSet (String bodyText, Object target)
{ {
short[] yspots = StringUtil.parseShortArray(bodyText); short[] yspots = StringUtil.parseShortArray(bodyText);
@@ -141,7 +141,7 @@ public class ObjectTileSetRuleSet extends SwissArmyTileSetRuleSet
}); });
digester.addRule( digester.addRule(
_prefix + TILESET_PATH + "/sorients", new CallMethodSpecialRule() { _path + "/sorients", new CallMethodSpecialRule() {
public void parseAndSet (String bodyText, Object target) public void parseAndSet (String bodyText, Object target)
{ {
ObjectTileSet set = (ObjectTileSet)target; ObjectTileSet set = (ObjectTileSet)target;
@@ -165,7 +165,7 @@ public class ObjectTileSetRuleSet extends SwissArmyTileSetRuleSet
}); });
digester.addRule( digester.addRule(
_prefix + TILESET_PATH + "/constraints", _path + "/constraints",
new CallMethodSpecialRule() { new CallMethodSpecialRule() {
public void parseAndSet (String bodyText, Object target) public void parseAndSet (String bodyText, Object target)
{ {
@@ -60,7 +60,7 @@ public class SwissArmyTileSetRuleSet extends TileSetRuleSet
super.addRuleInstances(digester); super.addRuleInstances(digester);
digester.addRule( digester.addRule(
_prefix + TILESET_PATH + "/widths", new CallMethodSpecialRule() { _path + "/widths", new CallMethodSpecialRule() {
public void parseAndSet (String bodyText, Object target) public void parseAndSet (String bodyText, Object target)
{ {
int[] widths = StringUtil.parseIntArray(bodyText); int[] widths = StringUtil.parseIntArray(bodyText);
@@ -69,7 +69,7 @@ public class SwissArmyTileSetRuleSet extends TileSetRuleSet
}); });
digester.addRule( digester.addRule(
_prefix + TILESET_PATH + "/heights", new CallMethodSpecialRule() { _path + "/heights", new CallMethodSpecialRule() {
public void parseAndSet (String bodyText, Object target) public void parseAndSet (String bodyText, Object target)
{ {
int[] heights = StringUtil.parseIntArray(bodyText); int[] heights = StringUtil.parseIntArray(bodyText);
@@ -78,7 +78,7 @@ public class SwissArmyTileSetRuleSet extends TileSetRuleSet
}); });
digester.addRule( digester.addRule(
_prefix + TILESET_PATH + "/tileCounts", _path + "/tileCounts",
new CallMethodSpecialRule() { new CallMethodSpecialRule() {
public void parseAndSet (String bodyText, Object target) public void parseAndSet (String bodyText, Object target)
{ {
@@ -88,7 +88,7 @@ public class SwissArmyTileSetRuleSet extends TileSetRuleSet
}); });
digester.addRule( digester.addRule(
_prefix + TILESET_PATH + "/offsetPos", new CallMethodSpecialRule() { _path + "/offsetPos", new CallMethodSpecialRule() {
public void parseAndSet (String bodyText, Object target) public void parseAndSet (String bodyText, Object target)
{ {
int[] values = StringUtil.parseIntArray(bodyText); int[] values = StringUtil.parseIntArray(bodyText);
@@ -103,7 +103,7 @@ public class SwissArmyTileSetRuleSet extends TileSetRuleSet
}); });
digester.addRule( digester.addRule(
_prefix + TILESET_PATH + "/gapSize", new CallMethodSpecialRule() { _path + "/gapSize", new CallMethodSpecialRule() {
public void parseAndSet (String bodyText, Object target) public void parseAndSet (String bodyText, Object target)
{ {
int[] values = StringUtil.parseIntArray(bodyText); int[] values = StringUtil.parseIntArray(bodyText);
@@ -45,6 +45,13 @@ public abstract class TileSetRuleSet
* a matched tileset. */ * a matched tileset. */
public static final String TILESET_PATH = "/tileset"; public static final String TILESET_PATH = "/tileset";
/**
* @return The full path used to match tilesets. Consists of the prefile plus _tilesetPath.
*/
public String getPath(){
return _path;
}
/** /**
* Instructs the tileset rule set to match tilesets with the supplied * Instructs the tileset rule set to match tilesets with the supplied
* prefix. For example, passing a prefix of * prefix. For example, passing a prefix of
@@ -65,7 +72,7 @@ public abstract class TileSetRuleSet
*/ */
public void setPrefix (String prefix) public void setPrefix (String prefix)
{ {
_prefix = prefix; _path = prefix + _tilesetPath;
} }
/** /**
@@ -79,21 +86,18 @@ public abstract class TileSetRuleSet
{ {
// this creates the appropriate instance when we encounter a // this creates the appropriate instance when we encounter a
// <tileset> tag // <tileset> tag
digester.addObjectCreate(_prefix + TILESET_PATH, digester.addObjectCreate(_path, getTileSetClass().getName());
getTileSetClass().getName());
// grab the name attribute from the <tileset> tag // grab the name attribute from the <tileset> tag
digester.addSetProperties(_prefix + TILESET_PATH); digester.addSetProperties(_path);
// grab the image path from an element // grab the image path from an element
digester.addCallMethod( digester.addCallMethod(_path + "/imagePath", "setImagePath", 0);
_prefix + TILESET_PATH + "/imagePath", "setImagePath", 0);
} }
/** /**
* The ruleset can be provided to a {@link ValidatedSetNextRule} to * The ruleset can be provided to a {@link ValidatedSetNextRule} to ensure that the tileset
* ensure that the tileset was fully parsed before doing something * was fully parsed before doing something with it.
* with it.
*/ */
public boolean isValid (Object target) public boolean isValid (Object target)
{ {
@@ -123,6 +127,9 @@ public abstract class TileSetRuleSet
*/ */
protected abstract Class getTileSetClass (); protected abstract Class getTileSetClass ();
/** The prefix at which me match our tilesets. */ /** The tileset path we append to the prefix to get the full path. */
protected String _prefix; protected String _tilesetPath = TILESET_PATH;
/** The full path at which me match our tilesets. */
protected String _path;
} }
@@ -42,16 +42,22 @@ import com.threerings.media.tile.UniformTileSet;
*/ */
public class UniformTileSetRuleSet extends TileSetRuleSet public class UniformTileSetRuleSet extends TileSetRuleSet
{ {
public UniformTileSetRuleSet(){
this(TILESET_PATH);
}
public UniformTileSetRuleSet(String tilesetPath){
_tilesetPath = tilesetPath;
}
// documentation inherited // documentation inherited
public void addRuleInstances (Digester digester) public void addRuleInstances (Digester digester)
{ {
super.addRuleInstances(digester); super.addRuleInstances(digester);
digester.addCallMethod( digester.addCallMethod(_path + "/width", "setWidth", 0,
_prefix + TILESET_PATH + "/width", "setWidth", 0,
new Class[] { java.lang.Integer.TYPE }); new Class[] { java.lang.Integer.TYPE });
digester.addCallMethod( digester.addCallMethod(_path + "/height", "setHeight", 0,
_prefix + TILESET_PATH + "/height", "setHeight", 0,
new Class[] { java.lang.Integer.TYPE }); new Class[] { java.lang.Integer.TYPE });
} }
@@ -75,7 +75,7 @@ public class XMLTileSetParser
// add a set next rule which will put tilesets with this prefix // add a set next rule which will put tilesets with this prefix
// into the array list that'll be on the top of the stack // into the array list that'll be on the top of the stack
_digester.addRule(prefix + TileSetRuleSet.TILESET_PATH, _digester.addRule(ruleset.getPath(),
new ValidatedSetNextRule("add", Object.class, new ValidatedSetNextRule("add", Object.class,
ruleset)); ruleset));
} }
@@ -45,10 +45,8 @@ public class BaseTileSetRuleSet extends SwissArmyTileSetRuleSet
{ {
super.addRuleInstances(digester); super.addRuleInstances(digester);
digester.addRule( digester.addRule(_path + "/passable", new CallMethodSpecialRule() {
_prefix + TILESET_PATH + "/passable", new CallMethodSpecialRule() { public void parseAndSet (String bodyText, Object target) {
public void parseAndSet (String bodyText, Object target)
{
int[] values = StringUtil.parseIntArray(bodyText); int[] values = StringUtil.parseIntArray(bodyText);
boolean[] passable = new boolean[values.length]; boolean[] passable = new boolean[values.length];
for (int i = 0; i < values.length; i++) { for (int i = 0; i < values.length; i++) {