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:
@@ -0,0 +1,92 @@
|
||||
//
|
||||
// $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.resource;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
/**
|
||||
* Resource bundle that retrieves its contents via HTTP over the network from a root URL.
|
||||
*/
|
||||
public class NetworkResourceBundle extends ResourceBundle
|
||||
{
|
||||
public NetworkResourceBundle (String root, String path)
|
||||
{
|
||||
try {
|
||||
_bundleURL = new URL(root + path);
|
||||
} catch (MalformedURLException mue) {
|
||||
Log.warning("Created malformed URL for resource. [root=" + root + ", path=" + path);
|
||||
}
|
||||
_ident = path;
|
||||
}
|
||||
|
||||
@Override // documentation inherited
|
||||
public String getIdent ()
|
||||
{
|
||||
return _ident;
|
||||
}
|
||||
|
||||
@Override // documentation inherited
|
||||
public InputStream getResource (String path)
|
||||
throws IOException
|
||||
{
|
||||
URL resourceUrl = new URL(_bundleURL, path);
|
||||
HttpURLConnection ucon = null;
|
||||
try {
|
||||
ucon = (HttpURLConnection) resourceUrl.openConnection();
|
||||
} catch (IOException ioe) {
|
||||
}
|
||||
|
||||
if (ucon == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
ucon.connect();
|
||||
return ucon.getInputStream();
|
||||
} catch (IOException ioe) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override // documentation inherited
|
||||
public BufferedImage getImageResource (String path, boolean useFastIO)
|
||||
throws IOException
|
||||
{
|
||||
InputStream in = getResource(path);
|
||||
if (in == null) {
|
||||
return null;
|
||||
}
|
||||
return ResourceManager.loadImage(in);
|
||||
}
|
||||
|
||||
/** Our identifier for this bundle. */
|
||||
protected String _ident;
|
||||
|
||||
/** Our root url to the resources in this bundle. */
|
||||
protected URL _bundleURL;
|
||||
}
|
||||
@@ -162,7 +162,27 @@ public class ResourceManager
|
||||
*/
|
||||
public ResourceManager (String resourceRoot, ClassLoader loader)
|
||||
{
|
||||
_rootPath = resourceRoot;
|
||||
this(resourceRoot, null, loader);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a resource manager with a root path to resources over the network. See
|
||||
* {@link #ResourceManager(String)} for further documentation.
|
||||
*/
|
||||
public ResourceManager (String resourceRoot, String networkResourceRoot)
|
||||
{
|
||||
this(resourceRoot, networkResourceRoot, ResourceManager.class.getClassLoader());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a resource manager with a root path to resources over the network and the specified
|
||||
* class loader via which to load classes. See {@link #ResourceManager(String)} for further
|
||||
* documentation.
|
||||
*/
|
||||
public ResourceManager (String fileResourceRoot, String networkResourceRoot, ClassLoader loader)
|
||||
{
|
||||
_rootPath = fileResourceRoot;
|
||||
_networkRootPath = networkResourceRoot;
|
||||
_loader = loader;
|
||||
|
||||
// check a system property to determine if we should unpack our bundles, but don't freak
|
||||
@@ -239,16 +259,7 @@ public class ResourceManager
|
||||
}
|
||||
|
||||
// load up our configuration
|
||||
Properties config = new Properties();
|
||||
try {
|
||||
config.load(new FileInputStream(new File(_rdir, configPath)));
|
||||
} catch (Exception e) {
|
||||
String errmsg = "Unable to load resource manager config [rdir=" + _rdir +
|
||||
", cpath=" + configPath + "]";
|
||||
Log.warning(errmsg + ".");
|
||||
Log.logStackTrace(e);
|
||||
throw new IOException(errmsg);
|
||||
}
|
||||
Properties config = loadConfig(configPath);
|
||||
|
||||
// resolve the configured resource sets
|
||||
List<ResourceBundle> dlist = new ArrayList<ResourceBundle>();
|
||||
@@ -259,7 +270,9 @@ public class ResourceManager
|
||||
continue;
|
||||
}
|
||||
String setName = key.substring(RESOURCE_SET_PREFIX.length());
|
||||
resolveResourceSet(setName, config.getProperty(key), dlist);
|
||||
String resourceSetType = config.getProperty(RESOURCE_SET_TYPE_PREFIX + setName,
|
||||
FILE_SET_TYPE);
|
||||
resolveResourceSet(setName, config.getProperty(key), resourceSetType, dlist);
|
||||
}
|
||||
|
||||
// if an observer was passed in, then we do not need to block the caller
|
||||
@@ -408,7 +421,14 @@ public class ResourceManager
|
||||
|
||||
// first look for this resource in our default resource bundle
|
||||
for (ResourceBundle bundle : _default) {
|
||||
in = bundle.getResource(path);
|
||||
// Try a localized version first.
|
||||
if (_localePrefix != null) {
|
||||
in = bundle.getResource(PathUtil.appendPath(_localePrefix, path));
|
||||
}
|
||||
// If that didn't work, try generic.
|
||||
if (in == null) {
|
||||
in = bundle.getResource(path);
|
||||
}
|
||||
if (in != null) {
|
||||
return in;
|
||||
}
|
||||
@@ -462,7 +482,18 @@ public class ResourceManager
|
||||
{
|
||||
// first look for this resource in our default resource bundle
|
||||
for (ResourceBundle bundle : _default) {
|
||||
BufferedImage image = bundle.getImageResource(path, false);
|
||||
// try a localized version first
|
||||
BufferedImage image = null;
|
||||
if (_localePrefix != null) {
|
||||
image =
|
||||
bundle.getImageResource(PathUtil.appendPath(_localePrefix, path), false);
|
||||
}
|
||||
|
||||
// if we didn't find that, try generic
|
||||
if (image == null) {
|
||||
image = bundle.getImageResource(path, false);
|
||||
}
|
||||
|
||||
if (image != null) {
|
||||
return image;
|
||||
}
|
||||
@@ -606,6 +637,25 @@ public class ResourceManager
|
||||
return (ResourceBundle[])_sets.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the configuration properties for our resource sets.
|
||||
*/
|
||||
protected Properties loadConfig (String configPath)
|
||||
throws IOException
|
||||
{
|
||||
Properties config = new Properties();
|
||||
try {
|
||||
config.load(new FileInputStream(new File(_rdir, configPath)));
|
||||
} catch (Exception e) {
|
||||
String errmsg = "Unable to load resource manager config [rdir=" + _rdir +
|
||||
", cpath=" + configPath + "]";
|
||||
Log.warning(errmsg + ".");
|
||||
Log.logStackTrace(e);
|
||||
throw new IOException(errmsg);
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
protected void initResourceDir (String resourceDir)
|
||||
{
|
||||
// if none was specified, check the resource_dir system property
|
||||
@@ -633,19 +683,25 @@ public class ResourceManager
|
||||
* Loads up a resource set based on the supplied definition information.
|
||||
*/
|
||||
protected void resolveResourceSet (
|
||||
String setName, String definition, List<ResourceBundle> dlist)
|
||||
String setName, String definition, String setType, List<ResourceBundle> dlist)
|
||||
{
|
||||
List<ResourceBundle> set = new ArrayList<ResourceBundle>();
|
||||
StringTokenizer tok = new StringTokenizer(definition, ":");
|
||||
while (tok.hasMoreTokens()) {
|
||||
String path = tok.nextToken().trim();
|
||||
FileResourceBundle bundle =
|
||||
new FileResourceBundle(getResourceFile(path), true, _unpack);
|
||||
set.add(bundle);
|
||||
if (bundle.isUnpacked() && bundle.sourceIsReady()) {
|
||||
continue;
|
||||
if (setType.equals(FILE_SET_TYPE)) {
|
||||
FileResourceBundle bundle =
|
||||
new FileResourceBundle(getResourceFile(path), true, _unpack);
|
||||
set.add(bundle);
|
||||
if (bundle.isUnpacked() && bundle.sourceIsReady()) {
|
||||
continue;
|
||||
}
|
||||
dlist.add(bundle);
|
||||
} else if (setType.equals(NETWORK_SET_TYPE)) {
|
||||
NetworkResourceBundle bundle =
|
||||
new NetworkResourceBundle(_networkRootPath, path);
|
||||
set.add(bundle);
|
||||
}
|
||||
dlist.add(bundle);
|
||||
}
|
||||
|
||||
// convert our array list into an array and stick it in the table
|
||||
@@ -768,6 +824,9 @@ public class ResourceManager
|
||||
* classpath. */
|
||||
protected String _rootPath;
|
||||
|
||||
/** The root path we give to network bundles for all resources they're interested in. */
|
||||
protected String _networkRootPath;
|
||||
|
||||
/** Whether or not to unpack our resource bundles. */
|
||||
protected boolean _unpack;
|
||||
|
||||
@@ -783,6 +842,15 @@ public class ResourceManager
|
||||
/** The prefix of configuration entries that describe a resource set. */
|
||||
protected static final String RESOURCE_SET_PREFIX = "resource.set.";
|
||||
|
||||
/** The prefix of configuration entries that describe a resource set. */
|
||||
protected static final String RESOURCE_SET_TYPE_PREFIX = "resource.set_type.";
|
||||
|
||||
/** The name of the default resource set. */
|
||||
protected static final String DEFAULT_RESOURCE_SET = "default";
|
||||
|
||||
/** Resource set type indicating the resources should be loaded from local files. */
|
||||
protected static final String FILE_SET_TYPE = "file";
|
||||
|
||||
/** Resource set type indicating the resources should be loaded over the network. */
|
||||
protected static final String NETWORK_SET_TYPE = "network";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user