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
@@ -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
{