New type of ResourceBundle - NetworkResourceBundle - This bundle grabs its resources over HTTP from a root URL rather than from a local jar file. To make use of this, we need a way to put all the contents of the bundle into an appropriate directory instead of a jar, including its metadata, so some new Bundler Tasks were created to do this. Finally, allow tile set trimming to be done to a non-raw image format through passing an optional imgFormat parameter. If no parameter is passed, it'll default ot the old behavior of using raw/FastIO. Note that to use network bundles, you can set the set_type in the resource manager.properties file. (e.g. "resource.set_type.tilesets = network") If no set_type is set for a resource set, it defaults to a normal FileResourceBundle

git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@343 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Mike Thomas
2007-11-13 01:12:18 +00:00
parent 785eb10234
commit ee73dd43de
13 changed files with 713 additions and 73 deletions
@@ -30,6 +30,7 @@ import com.samskivert.util.ListUtil;
import com.samskivert.util.StringUtil;
import com.threerings.media.image.Colorization;
import com.threerings.resource.FastImageIO;
import com.threerings.media.tile.util.TileSetTrimmer;
/**
@@ -87,7 +88,7 @@ public class TrimmedObjectTileSet extends TileSet
{
return (_bits == null) ? null : _bits[tileIdx].constraints;
}
/**
* Checks whether the tile at the specified index has the given constraint.
*/
@@ -96,7 +97,7 @@ public class TrimmedObjectTileSet extends TileSet
return (_bits == null || _bits[tileIdx].constraints == null) ? false :
ListUtil.contains(_bits[tileIdx].constraints, constraint);
}
// documentation inherited from interface RecolorableTileSet
public String[] getColorizations ()
{
@@ -167,14 +168,25 @@ public class TrimmedObjectTileSet extends TileSet
}
/**
* Creates a trimmed object tileset from the supplied source object tileset. The image path
* must be set by hand to the appropriate path based on where the image data that is written to
* the <code>destImage</code> parameter is actually stored on the file system. See {@link
* TileSetTrimmer#trimTileSet} for further information.
* Convenience function to trim the tile set to a file using FastImageIO.
*/
public static TrimmedObjectTileSet trimObjectTileSet (
ObjectTileSet source, OutputStream destImage)
throws IOException
{
return trimObjectTileSet(source, destImage, FastImageIO.FILE_SUFFIX);
}
/**
* Creates a trimmed object tileset from the supplied source object tileset. The image path
* must be set by hand to the appropriate path based on where the image data that is written to
* the <code>destImage</code> parameter is actually stored on the file system. If imgFormat is
* null, uses FastImageIO to save the file. See {@link TileSetTrimmer#trimTileSet} for further
* information.
*/
public static TrimmedObjectTileSet trimObjectTileSet (
ObjectTileSet source, OutputStream destImage, String imgFormat)
throws IOException
{
final TrimmedObjectTileSet tset = new TrimmedObjectTileSet();
tset.setName(source.getName());
@@ -199,10 +211,10 @@ public class TrimmedObjectTileSet extends TileSet
// fill in the original object metrics
for (int ii = 0; ii < tcount; ii++) {
tset._ometrics[ii] = new Rectangle();
if (source._xorigins != null) {
if (source._xorigins != null) {
tset._ometrics[ii].x = source._xorigins[ii];
}
if (source._yorigins != null) {
if (source._yorigins != null) {
tset._ometrics[ii].y = source._yorigins[ii];
}
tset._ometrics[ii].width = source._owidths[ii];
@@ -234,7 +246,7 @@ public class TrimmedObjectTileSet extends TileSet
tset._bounds[tileIndex] = new Rectangle(imageX, imageY, trimWidth, trimHeight);
}
};
TileSetTrimmer.trimTileSet(source, destImage, tmr);
TileSetTrimmer.trimTileSet(source, destImage, tmr, imgFormat);
// Log.info("Trimmed object tileset [bounds=" + StringUtil.toString(tset._bounds) +
// ", metrics=" + StringUtil.toString(tset._ometrics) + "].");
@@ -259,7 +271,7 @@ public class TrimmedObjectTileSet extends TileSet
/** The constraints associated with this object. */
public String[] constraints;
/** Generates a string representation of this instance. */
public String toString ()
{
@@ -27,6 +27,7 @@ import java.io.IOException;
import java.io.OutputStream;
import com.threerings.media.image.Colorization;
import com.threerings.resource.FastImageIO;
import com.threerings.media.tile.util.TileSetTrimmer;
/**
@@ -62,13 +63,24 @@ public class TrimmedTileSet extends TileSet
}
/**
* Creates a trimmed tileset from the supplied source tileset. The image path must be set by
* hand to the appropriate path based on where the image data that is written to the
* <code>destImage</code> parameter is actually stored on the file system. See {@link
* TileSetTrimmer#trimTileSet} for further information.
* Convenience function to trim the tile set and save it using FastImageIO.
*/
public static TrimmedTileSet trimTileSet (TileSet source, OutputStream destImage)
throws IOException
{
return trimTileSet(source, destImage, FastImageIO.FILE_SUFFIX);
}
/**
* Creates a trimmed tileset from the supplied source tileset. The image path must be set by
* hand to the appropriate path based on where the image data that is written to the
* <code>destImage</code> parameter is actually stored on the file system. The image format
* indicateds how the resulting image should be saved. If null, we save using FastImageIO
* See {@link TileSetTrimmer#trimTileSet} for further information.
*/
public static TrimmedTileSet trimTileSet (TileSet source, OutputStream destImage,
String imgFormat)
throws IOException
{
final TrimmedTileSet tset = new TrimmedTileSet();
tset.setName(source.getName());
@@ -90,7 +102,7 @@ public class TrimmedTileSet extends TileSet
tset._obounds[tileIndex] = new Rectangle(imageX, imageY, trimWidth, trimHeight);
}
};
TileSetTrimmer.trimTileSet(source, destImage, tmr);
TileSetTrimmer.trimTileSet(source, destImage, tmr, imgFormat);
return tset;
}
@@ -0,0 +1,163 @@
//
// $Id$
//
// Nenya library - tools for developing networked games
// Copyright (C) 2002-2007 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/nenya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.media.tile.bundle.tools;
import java.util.Iterator;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import org.apache.commons.io.IOUtils;
import com.threerings.media.Log;
import com.threerings.media.tile.ImageProvider;
import com.threerings.media.tile.ObjectTileSet;
import com.threerings.media.tile.TileSet;
import com.threerings.media.tile.TrimmedObjectTileSet;
import com.threerings.media.tile.bundle.BundleUtil;
import com.threerings.media.tile.bundle.TileSetBundle;
public class DirectoryTileSetBundler extends TileSetBundler
{
public DirectoryTileSetBundler (File configFile)
throws IOException
{
super(configFile);
}
public DirectoryTileSetBundler (String configPath)
throws IOException
{
super(configPath);
}
/**
* Finish the creation of a tileset bundle jar file.
*
* @param target the tileset bundle file that will be created.
* @param bundle contains the tilesets we'd like to save out to the bundle.
* @param improv the image provider.
* @param imageBase the base directory for getting images for non
* ObjectTileSet tilesets.
*/
public boolean createBundle (
File target, TileSetBundle bundle, ImageProvider improv, String imageBase)
throws IOException
{
try {
// write all of the image files to the bundle's target path, converting the
// tilesets to trimmed tilesets in the process
Iterator iditer = bundle.enumerateTileSetIds();
while (iditer.hasNext()) {
int tileSetId = ((Integer)iditer.next()).intValue();
TileSet set = bundle.getTileSet(tileSetId);
String imagePath = set.getImagePath();
// sanity checks
if (imagePath == null) {
Log.warning("Tileset contains no image path " +
"[set=" + set + "]. It ain't gonna work.");
continue;
}
// if this is an object tileset, we can't trim it!
if (set instanceof ObjectTileSet) {
// set the tileset up with an image provider; we
// need to do this so that we can trim it!
set.setImageProvider(improv);
// we're going to trim it, so adjust the path
imagePath = adjustImagePath(imagePath);
try {
// create a trimmed object tileset, which will
// write the trimmed tileset image to the destination
// output stream
File outFile = new File(target, imagePath);
outFile.getParentFile().mkdirs();
FileOutputStream fout = new FileOutputStream(outFile);
TrimmedObjectTileSet tset =
TrimmedObjectTileSet.trimObjectTileSet(
(ObjectTileSet)set, fout, "png");
tset.setImagePath(imagePath);
// replace the original set with the trimmed
// tileset in the tileset bundle
bundle.addTileSet(tileSetId, tset);
} catch (Exception e) {
e.printStackTrace(System.err);
String msg = "Error adding tileset to bundle " + imagePath +
", " + set.getName() + ": " + e;
throw (IOException) new IOException(msg).initCause(e);
}
} else {
// read the image file and convert it to our custom
// format in the bundle
File ifile = new File(imageBase, imagePath);
try {
BufferedImage image = ImageIO.read(ifile);
File outFile = new File(target, imagePath);
outFile.getParentFile().mkdirs();
FileOutputStream fout = new FileOutputStream(outFile);
FileInputStream imgin = new FileInputStream(ifile);
IOUtils.copy(imgin, fout);
} catch (Exception e) {
String msg = "Failure bundling image " + ifile +
": " + e;
throw (IOException) new IOException(msg).initCause(e);
}
}
}
// now write a serialized representation of the tileset bundle
// object to the bundle jar file
File outFile = new File(target, BundleUtil.METADATA_PATH);
outFile.getParentFile().mkdirs();
FileOutputStream fout = new FileOutputStream(outFile);
ObjectOutputStream oout = new ObjectOutputStream(fout);
oout.writeObject(bundle);
oout.flush();
return true;
} catch (Exception e) {
String errmsg = "Failed to create bundle " + target + ": " + e;
throw (IOException) new IOException(errmsg).initCause(e);
}
}
@Override // documentation inherited
protected boolean maySkipIfNewer ()
{
// Can't skip since we're copying individual files.
return false;
}
}
@@ -0,0 +1,72 @@
//
// $Id: TileSetBundlerTask.java 158 2007-02-24 00:38:17Z mdb $
//
// Nenya library - tools for developing networked games
// Copyright (C) 2002-2007 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/nenya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.media.tile.bundle.tools;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
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.threerings.media.tile.tools.MapFileTileSetIDBroker;
/**
* Ant task for creating tileset bundles that are placed in a specified directory instead
* of wrapped up in a fancy jar file.
*/
public class DirectoryTileSetBundlerTask extends TileSetBundlerTask
{
/**
* Sets the path to the directory in which we'll be putting our bundle's files.
*/
public void setDeployDir (File deployDir)
{
_deployDir = deployDir;
}
@Override // documentation inherited
public void execute () throws BuildException
{
ensureSet(_deployDir, "Must specify the path to which we want to deploy tileset files.");
super.execute();
}
@Override // documentation inherited
protected TileSetBundler createBundler ()
throws IOException
{
return new DirectoryTileSetBundler(_config);
}
@Override // documentation inherited
protected String getTargetPath (File fromDir, String path)
{
File xmlFile = new File(path.replace(fromDir.getPath(), _deployDir.getPath()));
return xmlFile.getParent();
}
/** The directory in which we want to place our tile set files for deployment. */
protected File _deployDir;
}
@@ -310,7 +310,7 @@ public class TileSetBundler
}
// see if our newest file is newer than the tileset bundle
if (newest < target.lastModified()) {
if (newest < target.lastModified() && maySkipIfNewer()) {
return false;
}
@@ -325,6 +325,14 @@ public class TileSetBundler
return createBundle(target, bundle, improv, bundleDesc.getParent());
}
/**
* Whether we're allowed to skip creation of the bundle if we're not out of date.
*/
protected boolean maySkipIfNewer ()
{
return true;
}
/**
* Finish the creation of a tileset bundle jar file.
*
@@ -334,7 +342,23 @@ public class TileSetBundler
* @param imageBase the base directory for getting images for non
* ObjectTileSet tilesets.
*/
public static boolean createBundle (
public boolean createBundle (
File target, TileSetBundle bundle, ImageProvider improv, String imageBase)
throws IOException
{
return createBundleJar(target, bundle, improv, imageBase);
}
/**
* Create a tileset bundle jar file.
*
* @param target the tileset bundle file that will be created.
* @param bundle contains the tilesets we'd like to save out to the bundle.
* @param improv the image provider.
* @param imageBase the base directory for getting images for non
* ObjectTileSet tilesets.
*/
public static boolean createBundleJar (
File target, TileSetBundle bundle, ImageProvider improv, String imageBase)
throws IOException
{
@@ -385,7 +409,7 @@ public class TileSetBundler
} catch (Exception e) {
e.printStackTrace(System.err);
String msg = "Error adding tileset to bundle " + imagePath +
String msg = "Error adding tileset to bundle " + imagePath +
", " + set.getName() + ": " + e;
throw (IOException) new IOException(msg).initCause(e);
}
@@ -22,6 +22,7 @@
package com.threerings.media.tile.bundle.tools;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.tools.ant.BuildException;
@@ -76,7 +77,7 @@ public class TileSetBundlerTask extends Task
File cfile = null;
try {
// create a tileset bundler
TileSetBundler bundler = new TileSetBundler(_config);
TileSetBundler bundler = createBundler();
// create our tileset id broker
MapFileTileSetIDBroker broker =
@@ -102,8 +103,7 @@ public class TileSetBundlerTask extends Task
"Config file should end with .xml.");
continue;
}
String bpath =
cpath.substring(0, cpath.length()-4) + ".jar";
String bpath = getTargetPath(fromDir, cpath);
File bfile = new File(bpath);
// create the bundle
@@ -127,6 +127,23 @@ public class TileSetBundlerTask extends Task
}
}
/**
* Create the bundler to use during creation.
*/
protected TileSetBundler createBundler ()
throws IOException
{
return new TileSetBundler(_config);
}
/**
* Returns the target path in which our bundler will write the tile set.
*/
protected String getTargetPath (File fromDir, String path)
{
return path.substring(0, path.length()-4) + ".jar";
}
protected void ensureSet (Object value, String errmsg)
throws BuildException
{
@@ -22,6 +22,7 @@
package com.threerings.media.tile.util;
import java.awt.Rectangle;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.awt.image.RasterFormatException;
@@ -68,6 +69,16 @@ public class TileSetTrimmer
int trimWidth, int trimHeight);
}
/**
* Convenience function to trim the tile set using FastImageIO to save the result.
*/
public static void trimTileSet (
TileSet source, OutputStream destImage, TrimMetricsReceiver tmr)
throws IOException
{
trimTileSet(source, destImage, tmr, FastImageIO.FILE_SUFFIX);
}
/**
* Generates a trimmed tileset image from the supplied source
* tileset. The source tileset must be configured with an image
@@ -80,9 +91,10 @@ public class TileSetTrimmer
* will be written.
* @param tmr a callback object that will be used to inform the caller
* of the trimmed tile metrics.
* @param imgFormat the format in which to write the image file - or if null, use FastImageIO.
*/
public static void trimTileSet (
TileSet source, OutputStream destImage, TrimMetricsReceiver tmr)
TileSet source, OutputStream destImage, TrimMetricsReceiver tmr, String imgFormat)
throws IOException
{
int tcount = source.getTileCount();
@@ -140,6 +152,10 @@ public class TileSetTrimmer
}
// write out trimmed image
FastImageIO.write(image, destImage);
if (imgFormat == null || FastImageIO.FILE_SUFFIX.equals(imgFormat)) {
FastImageIO.write(image, destImage);
} else {
ImageIO.write(image, imgFormat, destImage);
}
}
}