Modified map file serialization to store things in text so that we can

easily see what the mappings are and properly diff mappings that are
checked into CVS.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1739 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2002-09-23 18:53:01 +00:00
parent 4d94555e09
commit 82cd152a6f
3 changed files with 138 additions and 94 deletions
@@ -1,5 +1,5 @@
// //
// $Id: ComponentBundlerTask.java,v 1.13 2002/09/12 21:11:16 mdb Exp $ // $Id: ComponentBundlerTask.java,v 1.14 2002/09/23 18:53:01 mdb Exp $
package com.threerings.cast.bundle.tools; package com.threerings.cast.bundle.tools;
@@ -7,13 +7,14 @@ import java.awt.Image;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream; import java.io.BufferedReader;
import java.io.DataInputStream; import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
@@ -358,10 +359,9 @@ public class ComponentBundlerTask extends Task
HashMapIDBroker broker = new HashMapIDBroker(); HashMapIDBroker broker = new HashMapIDBroker();
try { try {
FileInputStream fin = new FileInputStream(mapfile); BufferedReader bin = new BufferedReader(new FileReader(mapfile));
DataInputStream din = broker.readFrom(bin);
new DataInputStream(new BufferedInputStream(fin)); bin.close();
broker.readFrom(din);
} catch (FileNotFoundException fnfe) { } catch (FileNotFoundException fnfe) {
// if the file doesn't yet exist, start with a blank broker // if the file doesn't yet exist, start with a blank broker
@@ -389,11 +389,9 @@ public class ComponentBundlerTask extends Task
} }
try { try {
FileOutputStream fout = new FileOutputStream(mapfile); BufferedWriter bout = new BufferedWriter(new FileWriter(mapfile));
DataOutputStream dout = hbroker.writeTo(bout);
new DataOutputStream(new BufferedOutputStream(fout)); bout.close();
hbroker.writeTo(dout);
dout.close();
} catch (IOException ioe) { } catch (IOException ioe) {
throw new BuildException("Unable to store component ID map " + throw new BuildException("Unable to store component ID map " +
"[mapfile=" + mapfile + "]", ioe); "[mapfile=" + mapfile + "]", ioe);
@@ -426,44 +424,70 @@ public class ComponentBundlerTask extends Task
return _nextCID != _startCID; return _nextCID != _startCID;
} }
public void writeTo (DataOutputStream dout) public void writeTo (BufferedWriter bout)
throws IOException throws IOException
{ {
// write out the size of the table // write out our most recently assigned component id
dout.writeInt(size()); String cidline = "" + _nextCID;
bout.write(cidline, 0, cidline.length());
bout.newLine();
// write out the keys and values // write out the keys and values
Iterator keys = keySet().iterator(); Iterator keys = keySet().iterator();
while (keys.hasNext()) { while (keys.hasNext()) {
Tuple key = (Tuple)keys.next(); Tuple key = (Tuple)keys.next();
Integer value = (Integer)get(key); Integer value = (Integer)get(key);
dout.writeUTF((String)key.left); String line = key.left + SEP_STR + key.right + SEP_STR + value;
dout.writeUTF((String)key.right); bout.write(line, 0, line.length());
dout.writeInt(value.intValue()); bout.newLine();
} }
// write out our most recently assigned component id
dout.writeInt(_nextCID);
} }
public void readFrom (DataInputStream din) public void readFrom (BufferedReader bin)
throws IOException throws IOException
{ {
// figure out how many keys and values we'll be reading
int size = din.readInt();
// and read them on in
for (int i = 0; i < size; i++) {
String cclass = din.readUTF();
String cname = din.readUTF();
int value = din.readInt();
put(new Tuple(cclass, cname), new Integer(value));
}
// read in our most recently assigned component id // read in our most recently assigned component id
_nextCID = din.readInt(); _nextCID = readInt(bin);
// keep track of this so that we can tell if we were modified // keep track of this so that we can tell if we were modified
_startCID = _nextCID; _startCID = _nextCID;
// now read in our keys and values
String line;
while ((line = bin.readLine()) != null) {
String orig = line;
int sidx = line.indexOf(SEP_STR);
if (sidx == -1) {
throw new IOException("Malformed line '" + orig + "'");
}
String cclass = line.substring(0, sidx);
line = line.substring(sidx + SEP_STR.length());
sidx = line.indexOf(SEP_STR);
if (sidx == -1) {
throw new IOException("Malformed line '" + orig + "'");
}
String cname = line.substring(0, sidx);
line = line.substring(sidx + SEP_STR.length());
try {
put(new Tuple(cclass, cname), Integer.valueOf(line));
} catch (NumberFormatException nfe) {
String err = "Malformed line, invalid code '" + orig + "'";
throw new IOException(err);
}
}
}
protected int readInt (BufferedReader bin)
throws IOException
{
String line = bin.readLine();
try {
return Integer.parseInt(line);
} catch (NumberFormatException nfe) {
throw new IOException("Expected number, got '" + line + "'");
}
} }
protected int _nextCID = 0; protected int _nextCID = 0;
protected int _startCID = 0; protected int _startCID = 0;
} }
@@ -481,4 +505,7 @@ public class ComponentBundlerTask extends Task
/** A list of filesets that contain tile images. */ /** A list of filesets that contain tile images. */
protected ArrayList _filesets = new ArrayList(); protected ArrayList _filesets = new ArrayList();
/** Used to separate keys and values in the map file. */
protected static final String SEP_STR = " := ";
} }
@@ -1,46 +0,0 @@
//
// $Id: DumpComponentMap.java,v 1.1 2002/04/11 01:34:09 mdb Exp $
package com.threerings.cast.bundle.tools;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import com.threerings.cast.bundle.tools.ComponentBundlerTask.HashMapIDBroker;
/**
* Dumps the serialized component map generated by the component bundling
* task.
*/
public class DumpComponentMap
{
public static void main (String[] args)
{
if (args.length < 1) {
System.err.println("Usage: DumpComponentMap mapfile");
System.exit(-1);
}
HashMapIDBroker broker = new HashMapIDBroker();
try {
FileInputStream fin = new FileInputStream(args[0]);
DataInputStream din =
new DataInputStream(new BufferedInputStream(fin));
broker.readFrom(din);
Iterator entries = broker.entrySet().iterator();
while (entries.hasNext()) {
System.out.println(entries.next());
}
} catch (Exception e) {
System.err.println("Error loading component ID map " +
"[mapfile=" + args[0] + "]: " + e);
}
}
}
@@ -1,15 +1,15 @@
// //
// $Id: MapFileTileSetIDBroker.java,v 1.5 2002/06/04 02:50:02 mdb Exp $ // $Id: MapFileTileSetIDBroker.java,v 1.6 2002/09/23 18:53:01 mdb Exp $
package com.threerings.media.tile.tools; package com.threerings.media.tile.tools;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
@@ -36,12 +36,15 @@ public class MapFileTileSetIDBroker implements TileSetIDBroker
// load up our map data // load up our map data
try { try {
FileInputStream fin = new FileInputStream(mapfile); BufferedReader bin = new BufferedReader(new FileReader(mapfile));
ObjectInputStream oin = new ObjectInputStream(fin); // read in our metadata
_nextTileSetID = oin.readInt(); _nextTileSetID = readInt(bin);
_storedTileSetID = _nextTileSetID; _storedTileSetID = _nextTileSetID;
_map = (HashMap)oin.readObject(); // read in our mappings
oin.close(); _map = new HashMap();
readMapFile(bin, _map);
bin.close();
} catch (FileNotFoundException fnfe) { } catch (FileNotFoundException fnfe) {
// create a blank map if our map file doesn't exist // create a blank map if our map file doesn't exist
@@ -54,6 +57,17 @@ public class MapFileTileSetIDBroker implements TileSetIDBroker
} }
} }
protected int readInt (BufferedReader bin)
throws IOException
{
String line = bin.readLine();
try {
return Integer.parseInt(line);
} catch (NumberFormatException nfe) {
throw new IOException("Expected number, got '" + line + "'");
}
}
// documentation inherited // documentation inherited
public int getTileSetID (String tileSetName) public int getTileSetID (String tileSetName)
throws PersistenceException throws PersistenceException
@@ -83,11 +97,14 @@ public class MapFileTileSetIDBroker implements TileSetIDBroker
} }
try { try {
FileOutputStream fout = new FileOutputStream(_mapfile); BufferedWriter bout = new BufferedWriter(new FileWriter(_mapfile));
ObjectOutputStream oout = new ObjectOutputStream(fout); // write out our metadata
oout.writeInt(_nextTileSetID); String tline = "" + _nextTileSetID;
oout.writeObject(_map); bout.write(tline, 0, tline.length());
oout.close(); bout.newLine();
// write out our mappings
writeMapFile(bout, _map);
bout.close();
} catch (IOException ioe) { } catch (IOException ioe) {
String errmsg = "Failure writing map file."; String errmsg = "Failure writing map file.";
@@ -95,6 +112,48 @@ public class MapFileTileSetIDBroker implements TileSetIDBroker
} }
} }
/**
* Reads in a mapping from strings to integers, which should have been
* written via {@link #writeMapFile}.
*/
public static void readMapFile (BufferedReader bin, HashMap map)
throws IOException
{
String line;
while ((line = bin.readLine()) != null) {
int eidx = line.indexOf(SEP_STR);
if (eidx == -1) {
throw new IOException("Malformed line, no '" + SEP_STR +
"': '" + line + "'");
}
try {
String code = line.substring(eidx+SEP_STR.length());
map.put(line.substring(0, eidx), Integer.valueOf(code));
} catch (NumberFormatException nfe) {
String errmsg = "Malformed line, invalid code: '" + line + "'";
throw new IOException(errmsg);
}
}
}
/**
* Writes out a mapping from strings to integers in a manner that can
* be read back in via {@link #readMapFile}.
*/
public static void writeMapFile (BufferedWriter bout, HashMap map)
throws IOException
{
Iterator iter = map.keySet().iterator();
while (iter.hasNext()) {
String key = (String)iter.next();
Integer value = (Integer)map.get(key);
String line = key + SEP_STR + value;
bout.write(line, 0, line.length());
bout.newLine();
}
bout.flush();
}
/** /**
* Copies the ID from the old tileset to the new tileset which is * Copies the ID from the old tileset to the new tileset which is
* useful when a tileset is renamed. This is called by the {@link * useful when a tileset is renamed. This is called by the {@link
@@ -135,4 +194,8 @@ public class MapFileTileSetIDBroker implements TileSetIDBroker
/** Our mapping from tileset names to ids. */ /** Our mapping from tileset names to ids. */
protected HashMap _map; protected HashMap _map;
/** The character we use to separate tileset name from code in the map
* file. */
protected static final String SEP_STR = " := ";
} }