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
@@ -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";
}