Created a persistent tileset name to id broker implementation; wrote an

ANT task for creating tileset bundles.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@659 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2001-11-29 00:14:11 +00:00
parent b7312882f4
commit 075ea5e9fe
2 changed files with 194 additions and 0 deletions
@@ -0,0 +1,102 @@
//
// $Id: TileSetBundlerTask.java,v 1.1 2001/11/29 00:14:11 mdb Exp $
package com.threerings.media.tools.tile.bundle;
import java.io.File;
import java.io.IOException;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import com.threerings.media.tools.tile.MapFileTileSetIDBroker;
/**
* Ant task for creating tilset bundles.
*/
public class TileSetBundlerTask extends Task
{
/**
* Sets the path to the bundler configuration file that we'll use when
* creating the bundle.
*/
public void setConfig (File config)
{
_config = config;
}
/**
* Sets the path to the bundle defintion file that we'll use to create
* our tileset bundle.
*/
public void setBundledef (File bundledef)
{
_bundledef = bundledef;
}
/**
* Sets the path to the tileset id mapping file we'll use when
* creating the bundle.
*/
public void setMapfile (File mapfile)
{
_mapfile = mapfile;
}
/**
* Sets the path to the bundle file that we'll be creating.
*/
public void setTarget (File target)
{
_target = target;
}
/**
* Performs the actual work of the task.
*/
public void execute () throws BuildException
{
// make sure everything was set up properly
ensureSet(_config, "Must specify the path to the bundler config " +
"file via the 'config' attribute.");
ensureSet(_bundledef, "Must specify the path to the bundle " +
"definition file via the 'bundledef' attribute.");
ensureSet(_mapfile, "Must specify the path to the tileset id map " +
"file via the 'mapfile' attribute.");
ensureSet(_target, "Must specify the path to the target bundle " +
"file via the 'target' attribute.");
try {
// create a tileset bundler
TileSetBundler bundler = new TileSetBundler(_config);
// create our tileset id broker
MapFileTileSetIDBroker broker =
new MapFileTileSetIDBroker(_mapfile);
// create the bundle
bundler.createBundle(broker, _bundledef, _target);
// commit changes to the tileset id mapping
broker.commit();
} catch (Exception e) {
String errmsg = "Failure creating tileset bundle: " +
e.getMessage();
throw new BuildException(errmsg, e);
}
}
protected void ensureSet (Object value, String errmsg)
throws BuildException
{
if (value == null) {
throw new BuildException(errmsg);
}
}
protected File _config;
protected File _bundledef;
protected File _mapfile;
protected File _target;
}
@@ -0,0 +1,92 @@
//
// $Id: MapFileTileSetIDBroker.java,v 1.1 2001/11/29 00:14:11 mdb Exp $
package com.threerings.media.tools.tile;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import com.samskivert.io.PersistenceException;
import com.threerings.media.tile.TileSetIDBroker;
/**
* Stores a set of tileset name to id mappings in a map file.
*/
public class MapFileTileSetIDBroker implements TileSetIDBroker
{
/**
* Creates a broker that will use the specified file as its persistent
* store. The persistent store will be created if it does not yet
* exist.
*/
public MapFileTileSetIDBroker (File mapfile)
throws PersistenceException
{
// keep this for later
_mapfile = mapfile;
// load up our map data
try {
FileInputStream fin = new FileInputStream(mapfile);
ObjectInputStream oin = new ObjectInputStream(fin);
_nextTileSetID = oin.readInt();
_map = (HashMap)oin.readObject();
oin.close();
} catch (FileNotFoundException fnfe) {
// create a blank map if our map file doesn't exist
_map = new HashMap();
} catch (Exception e) {
// other errors are more fatal
String errmsg = "Failure reading map file.";
throw new PersistenceException(errmsg, e);
}
}
// documentation inherited
public int getTileSetID (String tileSetName)
throws PersistenceException
{
Integer tsid = (Integer)_map.get(tileSetName);
if (tsid == null) {
tsid = new Integer(++_nextTileSetID);
_map.put(tileSetName, tsid);
}
return tsid.intValue();
}
// documentation inherited
public void commit ()
throws PersistenceException
{
try {
FileOutputStream fout = new FileOutputStream(_mapfile);
ObjectOutputStream oout = new ObjectOutputStream(fout);
oout.writeInt(_nextTileSetID);
oout.writeObject(_map);
oout.close();
} catch (IOException ioe) {
String errmsg = "Failure writing map file.";
throw new PersistenceException(errmsg, ioe);
}
}
/** Our persistent map file. */
protected File _mapfile;
/** The next tileset id that we'll assign. */
protected int _nextTileSetID;
/** Our mapping from tileset names to ids. */
protected HashMap _map;
}