Fringe configuration class and supporting classes to read in the xml

specification file and turn it into a serializable object.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1181 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2002-04-03 22:52:44 +00:00
parent 50012b1cd1
commit 006c25a8d8
3 changed files with 285 additions and 0 deletions
@@ -0,0 +1,90 @@
//
// $Id: FringeConfiguration.java,v 1.1 2002/04/03 22:52:44 ray Exp $
package com.threerings.miso.scene;
import java.io.Serializable;
import java.util.ArrayList;
import com.samskivert.util.HashIntMap;
/**
* Used to manage data about which base tilesets fringe on which others
* and how they fringe.
*/
public class FringeConfiguration implements Serializable
{
/**
* The path (relative to the resource directory) at which the fringe
* configuration should be loaded and stored.
*/
public static final String CONFIG_PATH = "config/miso/scene/fringeconf.dat";
public static class FringeRecord implements Serializable
{
/** The tileset id of the base tileset to which this applies. */
public int base_tsid;
/** The fringe priority of this base tileset. */
public int priority;
/** A list of the possible tilesets that can be used for fringing. */
public ArrayList tilesets = new ArrayList();
/** Used when parsing the tilesets definitions. */
public void addTileset (FringeTileSetRecord record)
{
tilesets.add(record);
}
}
/**
* Used to parse the tileset fringe definitions.
*/
public static class FringeTileSetRecord implements Serializable
{
/** The tileset id of the fringe tileset. */
public int fringe_tsid;
/** Is this a mask? */
public boolean mask;
}
/**
* Adds a parsed FringeRecord to this instance. This is used when parsing
* the fringerecords from xml.
*/
public void addFringeRecord (FringeRecord frec)
{
_frecs.put(frec.base_tsid, frec);
}
/**
* Does the first tileset fringe upon the second?
*/
public boolean fringesOn (int first, int second)
{
FringeRecord f1 = (FringeRecord) _frecs.get(first);
// we better have a fringe record for the first
if (null != f1) {
// it had better have some tilesets defined
if (f1.tilesets.size() > 0) {
FringeRecord f2 = (FringeRecord) _frecs.get(second);
// and we only fringe if second doesn't exist or has a lower
// priority
if ((null == f2) || (f1.priority > f2.priority)) {
return true;
}
}
}
return false;
}
/** The mapping from base tileset id to fringerecord. */
protected HashIntMap _frecs = new HashIntMap();
}
@@ -0,0 +1,76 @@
//
// $Id: CompileFringeConfigurationTask.java,v 1.1 2002/04/03 22:52:44 ray Exp $
package com.threerings.miso.scene.tools;
import java.io.File;
import java.io.Serializable;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import com.samskivert.io.PersistenceException;
import com.threerings.miso.scene.tools.xml.FringeConfigurationParser;
import com.threerings.media.tile.tools.MapFileTileSetIDBroker;
import com.threerings.util.CompiledConfig;
/**
* Compile fringe configuration.
*/
public class CompileFringeConfigurationTask extends Task
{
public void setTileSetMap (File tsetmap)
{
_tsetmap = tsetmap;
}
public void setFringeDef (File fringedef)
{
_fringedef = fringedef;
}
public void setTarget (File target)
{
_target = target;
}
public void execute () throws BuildException
{
// make sure the source file exists
if (!_fringedef.exists()) {
throw new BuildException("Fringe definition file not found " +
"[path=" + _fringedef.getPath() + "].");
}
// set up the tileid broker
MapFileTileSetIDBroker broker;
try {
broker = new MapFileTileSetIDBroker(_tsetmap);
} catch (PersistenceException pe) {
throw new BuildException("Couldn't set up tileset mapping " +
"[path=" + _tsetmap.getPath() +
", error=" + pe.getCause() + "].");
}
FringeConfigurationParser parser = new FringeConfigurationParser(
broker);
Serializable config;
try {
config = parser.parseConfig(_fringedef);
} catch (Exception e) {
throw new BuildException("Failure parsing config definition", e);
}
try {
// and write it on out
CompiledConfig.saveConfig(_target, config);
} catch (Exception e) {
throw new BuildException("Failure writing serialized config", e);
}
}
protected File _tsetmap;
protected File _fringedef;
protected File _target;
}
@@ -0,0 +1,119 @@
//
// $Id: FringeConfigurationParser.java,v 1.1 2002/04/03 22:52:44 ray Exp $
package com.threerings.miso.scene.tools.xml;
import java.io.Serializable;
import org.xml.sax.Attributes;
import org.apache.commons.digester.Digester;
import org.apache.commons.digester.Rule;
import com.samskivert.util.StringUtil;
import com.samskivert.xml.SetPropertyFieldsRule;
import com.threerings.tools.xml.CompiledConfigParser;
import com.threerings.miso.Log;
import com.threerings.miso.scene.FringeConfiguration;
import com.threerings.miso.scene.FringeConfiguration.FringeRecord;
import com.threerings.miso.scene.FringeConfiguration.FringeTileSetRecord;
import com.threerings.media.tile.TileSetIDBroker;
/**
* Parses fringe config definitions.
*/
public class FringeConfigurationParser extends CompiledConfigParser
{
public FringeConfigurationParser (TileSetIDBroker broker)
{
_idBroker = broker;
}
// documentation inherited
protected Serializable createConfigObject ()
{
return new FringeConfiguration();
}
// documentation inherited
protected void addRules (Digester digest)
{
// configure top-level constraints
String prefix = "fringes";
digest.addRule(prefix, new SetPropertyFieldsRule(digest));
// create and configure fringe config instances
prefix += "/base";
digest.addObjectCreate(prefix, FringeRecord.class.getName());
digest.addSetNext(
prefix, "addFringeRecord", FringeRecord.class.getName());
digest.addRule(prefix, new Rule(digest) {
// parse the fringe record, converting tileset names to tileset
// ids
public void begin (Attributes attrs)
throws Exception
{
FringeRecord frec = (FringeRecord) digester.peek();
for (int ii=0; ii < attrs.getLength(); ii++) {
String name = attrs.getLocalName(ii);
if (StringUtil.blank(name)) {
name = attrs.getQName(ii);
}
String value = attrs.getValue(ii);
if ("name".equals(name)) {
if (_idBroker.tileSetMapped(value)) {
frec.base_tsid = _idBroker.getTileSetID(value);
} else {
Log.warning("Skipping unknown base " +
"tileset [name=" + value + "].");
}
} else if ("priority".equals(name)) {
frec.priority = Integer.parseInt(value);
}
}
}
});
// create the tileset records in each fringe record
prefix += "/tileset";
digest.addObjectCreate(prefix, FringeTileSetRecord.class.getName());
digest.addSetNext(
prefix, "addTileset", FringeTileSetRecord.class.getName());
digest.addRule(prefix, new Rule(digest) {
// parse the fringe tilesetrecord, converting tileset names to ids
public void begin (Attributes attrs)
throws Exception
{
FringeTileSetRecord f = (FringeTileSetRecord) digester.peek();
for (int ii=0; ii < attrs.getLength(); ii++) {
String name = attrs.getLocalName(ii);
if (StringUtil.blank(name)) {
name = attrs.getQName(ii);
}
String value = attrs.getValue(ii);
if ("name".equals(name)) {
if (_idBroker.tileSetMapped(value)) {
f.fringe_tsid = _idBroker.getTileSetID(value);
} else {
Log.warning("Skipping unknown fringe " +
"tileset [name=" + value + "].");
}
} else if ("mask".equals(name)) {
f.mask = Boolean.getBoolean(value);
}
}
}
});
}
protected TileSetIDBroker _idBroker;
}