diff --git a/src/java/com/threerings/media/tools/ResourceIndexerTask.java b/src/java/com/threerings/media/tools/ResourceIndexerTask.java new file mode 100644 index 00000000..68e57a90 --- /dev/null +++ b/src/java/com/threerings/media/tools/ResourceIndexerTask.java @@ -0,0 +1,61 @@ +package com.threerings.media.tools; + +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +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; + +/** + * Creates a file that lists all the resources in a fileset out to an index file. + */ +public class ResourceIndexerTask extends Task +{ + /** + * Adds a nested <fileset> element. + */ + public void addFileset (FileSet set) + { + _filesets.add(set); + } + + public void setIndexFile (String file) + { + _indexFile = file; + } + + @Override // documentation inherited + public void execute () throws BuildException + { + PrintWriter fout = null; + try { + fout = new PrintWriter(new FileWriter(_indexFile)); + + for (FileSet fs : _filesets) { + DirectoryScanner ds = fs.getDirectoryScanner(getProject()); + String[] srcFiles = ds.getIncludedFiles(); + for (String filename : srcFiles) { + fout.println(filename); + } + } + + } catch (IOException ioe) { + throw new BuildException(ioe); + } finally { + if (fout != null) { + fout.close(); + } + } + } + + /** A list of filesets that contain files to include in the index. */ + protected ArrayList _filesets = new ArrayList(); + + /** The name of the file to which we should write the index. */ + protected String _indexFile; +} diff --git a/src/java/com/threerings/resource/NetworkResourceBundle.java b/src/java/com/threerings/resource/NetworkResourceBundle.java index 1f184d7a..8bb79f6c 100644 --- a/src/java/com/threerings/resource/NetworkResourceBundle.java +++ b/src/java/com/threerings/resource/NetworkResourceBundle.java @@ -28,6 +28,7 @@ import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.security.AccessControlException; +import java.util.HashSet; /** * Resource bundle that retrieves its contents via HTTP over the network from a root URL. @@ -35,6 +36,11 @@ import java.security.AccessControlException; public class NetworkResourceBundle extends ResourceBundle { public NetworkResourceBundle (String root, String path) + { + this(root, path, null); + } + + public NetworkResourceBundle (String root, String path, HashSet rsrcList) { if (!root.endsWith("/")) { root += "/"; @@ -45,6 +51,8 @@ public class NetworkResourceBundle extends ResourceBundle Log.warning("Created malformed URL for resource. [root=" + root + ", path=" + path); } _ident = path; + + _rsrcList = rsrcList; } @Override // documentation inherited @@ -57,6 +65,11 @@ public class NetworkResourceBundle extends ResourceBundle public InputStream getResource (String path) throws IOException { + // If we can reject it before opening a connection, then save the network latency. + if (!inResourceList(path)) { + return null; + } + URL resourceUrl = new URL(_bundleURL, path); HttpURLConnection ucon = null; try { @@ -91,6 +104,15 @@ public class NetworkResourceBundle extends ResourceBundle return ResourceManager.loadImage(in); } + /** + * Returns whether we believe the path to be available. If we have no list, we assume + * everything may be available. + */ + protected boolean inResourceList (String path) + { + return _rsrcList == null || _rsrcList.contains(path); + } + /** * Returns a string representation of this resource bundle. */ @@ -105,4 +127,7 @@ public class NetworkResourceBundle extends ResourceBundle /** Our root url to the resources in this bundle. */ protected URL _bundleURL; + + /** A list of all the resources included in this bundle. */ + protected HashSet _rsrcList; } diff --git a/src/java/com/threerings/resource/ResourceManager.java b/src/java/com/threerings/resource/ResourceManager.java index 075c03f8..a85d0c8c 100644 --- a/src/java/com/threerings/resource/ResourceManager.java +++ b/src/java/com/threerings/resource/ResourceManager.java @@ -36,6 +36,7 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.Enumeration; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Properties; @@ -655,6 +656,16 @@ public class ResourceManager return config; } + /** + * If we have a full list of the resources available, we return it. A return value of null + * means that we do not know what's available and we'll have to try all possibilities. This + * is fine for most applications. + */ + protected HashSet getResourceList () + { + return null; + } + protected void initResourceDir (String resourceDir) { // if none was specified, check the resource_dir system property @@ -698,7 +709,7 @@ public class ResourceManager dlist.add(bundle); } else if (setType.equals(NETWORK_SET_TYPE)) { NetworkResourceBundle bundle = - new NetworkResourceBundle(_networkRootPath, path); + new NetworkResourceBundle(_networkRootPath, path, getResourceList()); set.add(bundle); } }