Altered persistent storage of scenes to be handled by the

SceneRepository.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@117 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Walter Korman
2001-07-24 16:10:19 +00:00
parent 3e5060c7c5
commit 05f3a6aa25
15 changed files with 283 additions and 411 deletions
+6 -3
View File
@@ -1,5 +1,5 @@
#
# $Id: miso.properties,v 1.8 2001/07/23 22:31:47 shaper Exp $
# $Id: miso.properties,v 1.9 2001/07/24 16:10:19 shaper Exp $
#
# Initial test config values for miso development.
#
@@ -7,8 +7,11 @@
# the tileset manager class for working with tilesets
tilesetmgr = com.threerings.miso.tile.EditableTileSetManager
# the scene manager class for working with scenes
scenemgr = com.threerings.miso.scene.EditableSceneManager
# the scene repository class that handles scene storage
scenerepo = com.threerings.miso.scene.xml.XMLFileSceneRepository
# the directory containing scene description files
sceneroot = rsrc/scenes
# the tileset descriptions
tilesets = rsrc/config/miso/tilesets.xml
@@ -1,8 +0,0 @@
//
// $Id: CompiledSceneManager.java,v 1.5 2001/07/23 18:52:51 shaper Exp $
package com.threerings.miso.scene;
public class CompiledSceneManager extends SceneManagerImpl
{
}
@@ -1,5 +1,5 @@
//
// $Id: DisplayMisoSceneImpl.java,v 1.10 2001/07/23 22:31:47 shaper Exp $
// $Id: DisplayMisoSceneImpl.java,v 1.11 2001/07/24 16:10:19 shaper Exp $
package com.threerings.miso.scene;
@@ -97,130 +97,24 @@ public class Scene
return numTiles;
}
/**
* Populate the scene object by reading the contents from the given
* input stream.
*/
public void readFrom (InputStream in) throws IOException
public static int getNumLayers ()
{
DataInputStream dis = new DataInputStream(in);
// read scene header information
_name = dis.readUTF();
_sid = dis.readShort();
short version = dis.readShort();
// make sure we can understand the file format
if (version < 0 || version > VERSION) {
throw new IOException("Can't understand scene file format " +
" [version=" + version + "]");
}
// allocate full tile array. null tiles denote tiles in absentia.
tiles = new Tile[TILE_WIDTH][TILE_HEIGHT][NUM_LAYERS];
// read all tiles for the base layer
for (int xx = 0; xx < TILE_WIDTH; xx++) {
for (int yy = 0; yy < TILE_HEIGHT; yy++) {
// read tile details
short tsid = dis.readShort();
short tid = dis.readShort();
// retrieve tile from tile manager
tiles[xx][yy][LAYER_BASE] = _tilemgr.getTile(tsid, tid);
}
}
// read tiles for each of the subsequent layers
for (int lnum = 1; lnum < NUM_LAYERS; lnum++) {
// read the number of tiles in this layer
int numTiles = dis.readShort();
for (int ii = 0; ii < numTiles; ii++) {
// read tile details
short tsid = dis.readShort();
short tid = dis.readShort();
byte tx = dis.readByte();
byte ty = dis.readByte();
// retrieve tile from tile manager
tiles[tx][ty][lnum] = _tilemgr.getTile(tsid, tid);
}
}
// read hotspot points
short numSpots = dis.readShort();
_hotspots = new Point[numSpots];
for (int ii = 0; ii < numSpots; ii++) {
_hotspots[ii] = new Point();
_hotspots[ii].x = dis.readByte();
_hotspots[ii].y = dis.readByte();
}
// read exit points
short numExits = dis.readShort();
_exits = new ExitPoint[numExits];
for (int ii = 0; ii < numExits; ii++) {
_exits[ii] = new ExitPoint();
_exits[ii].x = dis.readByte();
_exits[ii].y = dis.readByte();
_exits[ii].sid = dis.readShort();
}
return NUM_LAYERS;
}
/**
* Write this scene object to the specified output stream.
*/
public void writeTo (OutputStream out) throws IOException
public static int getVersion ()
{
DataOutputStream dos = new DataOutputStream(out);
return VERSION;
}
// write scene header information
dos.writeUTF(_name);
dos.writeShort(_sid);
dos.writeShort(VERSION);
public static int getTileWidth ()
{
return TILE_WIDTH;
}
// write tiles for the base layer
for (int xx = 0; xx < TILE_WIDTH; xx++) {
for (int yy = 0; yy < TILE_HEIGHT; yy++) {
Tile tile = tiles[xx][yy][LAYER_BASE];
dos.writeShort(tile.tsid);
dos.writeShort(tile.tid);
}
}
// write tiles for each of the subsequent layers
for (int lnum = 1; lnum < NUM_LAYERS; lnum++) {
// write the number of tiles in this layer
dos.writeShort(getNumLayerTiles(lnum));
for (int xx = 0; xx < TILE_WIDTH; xx++) {
for (int yy = 0; yy < TILE_HEIGHT; yy++) {
Tile tile = tiles[xx][yy][lnum];
if (tile != null) {
dos.writeShort(tile.tsid);
dos.writeShort(tile.tid);
}
}
}
}
// write hotspot points
int numSpots = (_hotspots == null) ? 0 : _hotspots.length;
dos.writeShort(numSpots);
for (int ii = 0; ii < numSpots; ii++) {
dos.writeByte(_hotspots[ii].x);
dos.writeByte(_hotspots[ii].y);
}
// write exit points
int numExits = (_exits == null) ? 0 : _exits.length;
dos.writeShort(numExits);
for (int ii = 0; ii < numExits; ii++) {
dos.writeByte(_exits[ii].x);
dos.writeByte(_exits[ii].y);
dos.writeByte(_exits[ii].sid);
}
public static int getTileHeight ()
{
return TILE_HEIGHT;
}
/** The latest scene file format version. */
@@ -1,68 +0,0 @@
//
// $Id: EditableSceneManager.java,v 1.6 2001/07/23 22:31:47 shaper Exp $
package com.threerings.miso.scene;
import java.io.IOException;
import java.util.ArrayList;
import com.threerings.miso.Log;
/**
* The EditableSceneManager extends general scene manager
* functionality to allow reading scenes from and writing scenes to
* XML files.
*/
public class EditableSceneManager extends SceneManagerImpl
{
/**
* Load all scenes described in the specified XML file into the
* hashtable of scenes currently available.
*
* @param fname the file to load scenes from.
*/
public void loadScenes (String fname) throws IOException
{
ArrayList scenes = null;
_sid = 0;
try {
scenes = new XMLSceneParser().loadScenes(fname);
} catch (IOException ioe) {
Log.warning("Exception loading scenes [fname=" + fname +
", ioe=" + ioe + "].");
return;
}
// bail if we didn't find any scenes
int size = scenes.size();
if (size == 0) {
Log.warning("No scenes found [fname=" + fname + "].");
return;
}
// copy new scenes into the main scene hashtable
for (int ii = 0; ii < size; ii++) {
Scene scene = (Scene)scenes.get(ii);
_scenes.put(scene.getId(), scene);
Log.info("Adding scene to cache [scene=" + scene + "].");
}
}
/**
* Write all scenes currently available to the specified file in
* XML format.
*
* @param fname the file to write the scenes to.
*/
public void writeAllScenes (String fname)
{
try {
ArrayList scenes = getAllScenes();
new XMLSceneWriter(scenes).writeToFile(fname);
} catch (IOException ioe) {
Log.warning("Exception writing scenes to file [fname=" +
fname + ", ioe=" + ioe + "].");
}
}
}
@@ -1,40 +1,57 @@
//
// $Id: SceneManager.java,v 1.6 2001/07/23 22:31:47 shaper Exp $
// $Id: SceneManager.java,v 1.7 2001/07/24 16:10:19 shaper Exp $
package com.threerings.miso.scene;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import com.samskivert.util.IntMap;
/**
* Manages the various scenes that are displayed during the game and
* provides simplified retrieval and caching facilities.
* The SceneManager provides a single access point for retrieving and
* caching the various scenes that make up a game.
*/
public interface SceneManager
public class SceneManager
{
/**
* Return an ArrayList containing all Scene objects available.
* Initialize the SceneManager with the given scene repository.
*
* @return the list of scenes.
* @param repo the scene repository.
*/
public ArrayList getAllScenes ();
/**
* Return a new Scene object with the next unique scene id. The
* scene id is only guaranteed to be unique in the context of the
* scenes currently available via this SceneManager.
*/
public Scene getNewScene ();
public SceneManager (SceneRepository repo)
{
_repo = repo;
}
/**
* Return the Scene object for the specified scene id.
*
* @param sid the scene id.
* @return the Scene object.
*/
public Scene getScene (int sid);
public Scene getScene (int sid)
{
// TBD
return null;
}
public SceneRepository getSceneRepository ()
{
return _repo;
}
/**
* Return a String array of all layer names ordered by ascending
* layer id.
* Return a String array of all scene layer names ordered by
* ascending layer id.
*
* @return the layer names.
*/
public String[] getLayerNames ();
public String[] getLayerNames ()
{
return Scene.XLATE_LAYERS;
}
/** The repository used to read and write scenes. */
protected SceneRepository _repo;
/** The currently cached scenes. */
protected IntMap _scenes = new IntMap();
}
@@ -1,54 +0,0 @@
//
// $Id: SceneManagerImpl.java,v 1.6 2001/07/23 22:31:47 shaper Exp $
package com.threerings.miso.scene;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import com.samskivert.util.*;
import com.threerings.miso.Log;
import com.threerings.miso.tile.TileManager;
public abstract class SceneManagerImpl implements SceneManager
{
public void init (TileManager tilemgr)
{
_tilemgr = tilemgr;
}
public Scene getScene (int sid)
{
// TBD
return null;
}
public String[] getLayerNames ()
{
return Scene.XLATE_LAYERS;
}
public ArrayList getAllScenes ()
{
ArrayList list = new ArrayList();
CollectionUtil.addAll(list, _scenes.elements());
return list;
}
public Scene getNewScene ()
{
Scene scene = new Scene(_tilemgr, _sid);
_scenes.put(_sid++, scene);
return scene;
}
/** The next monotonically-increasing scene id. */
protected int _sid = 0;
/** The tile manager for use by all scenes. */
protected TileManager _tilemgr;
/** The scenes currently available. */
protected IntMap _scenes = new IntMap();
}
@@ -1,22 +0,0 @@
//
// $Id: SceneParser.java,v 1.1 2001/07/23 22:45:04 shaper Exp $
package com.threerings.miso.scene;
import java.io.IOException;
import java.util.ArrayList;
/**
* The SceneParser is a general interface to be implemented by classes
* that load scene descriptions in a particular format from a file.
*/
public interface SceneParser
{
/**
* Read scene description data from the given file and construct
* Scene objects to suit. Return an ArrayList of all Scene
* objects constructed, or a zero-length array if no scene
* descriptions were fully parsed.
*/
public ArrayList loadScenes (String fname) throws IOException;
}
@@ -0,0 +1,12 @@
//
// $Id: SceneRepository.java,v 1.1 2001/07/24 16:10:19 shaper Exp $
package com.threerings.miso.scene;
/**
* The SceneRepository interface manages persistent scene storage.
*/
public interface SceneRepository
{
// TBD
}
@@ -0,0 +1,32 @@
//
// $Id: SceneRepositoryImpl.java,v 1.1 2001/07/24 16:10:19 shaper Exp $
package com.threerings.miso.scene;
import com.samskivert.util.Config;
import com.threerings.miso.tile.TileManager;
public abstract class SceneRepositoryImpl implements SceneRepository
{
/**
* Initialize the SceneRepository with the given config and tile
* manager objects. The root scene
* directory is read from the given config object, and the tile
* manager is used to obtain tiles when constructing scene objects
* from files.
*
* @param config the config object.
* @param tilemgr the tile manager object.
*/
public void init (Config config, TileManager tilemgr)
{
_config = config;
_tilemgr = tilemgr;
}
/** The config object. */
protected Config _config;
/** The tile manager from which the scenes obtain their tiles. */
protected TileManager _tilemgr;
}
@@ -1,16 +0,0 @@
//
// $Id: XMLSceneParser.java,v 1.2 2001/07/23 22:47:03 shaper Exp $
package com.threerings.miso.scene;
import java.io.IOException;
import java.util.ArrayList;
public class XMLSceneParser implements SceneParser
{
public ArrayList loadScenes (String fname) throws IOException
{
// TBD
return null;
}
}
@@ -1,93 +0,0 @@
//
// $Id: XMLSceneWriter.java,v 1.1 2001/07/23 22:45:04 shaper Exp $
package com.threerings.miso.scene;
import java.io.*;
import java.util.ArrayList;
import com.megginson.sax.DataWriter;
import org.xml.sax.*;
import org.xml.sax.helpers.AttributesImpl;
import com.samskivert.util.StringUtil;
import com.threerings.miso.Log;
/**
* The XMLSceneWriter writes a list of Scene objects to an XML file
* using Megginson Technologies' XMLWriter class functionality.
*/
public class XMLSceneWriter extends DataWriter
{
/**
* Construct an XMLSceneWriter object that will be used to write
* the specified set of scenes to an XML file.
*
* @param scenes the scenes to be written.
*/
public XMLSceneWriter (ArrayList scenes)
{
_scenes = scenes;
setIndentStep(2);
}
/**
* Write the scenes to the specified output file in XML format.
*
* @param fname the absolute pathname of the file to write to.
*/
public void writeToFile (String fname) throws IOException
{
FileOutputStream fos = new FileOutputStream(fname);
setOutput(new OutputStreamWriter(fos));
try {
startDocument();
startElement("scenegroup");
int size = _scenes.size();
for (int ii = 0; ii < size; ii++) {
startElement("scene");
Scene scene = (Scene)_scenes.get(ii);
dataElement("name", scene.getName());
dataElement("sid", "" + scene.getId());
dataElement("version", "" + scene.VERSION);
dataElement("hotspots",
StringUtil.toString(scene.getHotSpots()));
dataElement("exits", StringUtil.toString(scene.getExits()));
startElement("tiles");
for (int jj = 0; jj < Scene.NUM_LAYERS; jj++) {
AttributesImpl attrs = new AttributesImpl();
attrs.addAttribute("", "lnum", "", "CDATA", "" + jj);
startElement("", "layer", "", attrs);
for (int yy = 0; yy < Scene.TILE_HEIGHT; yy++) {
attrs.clear();
attrs.addAttribute("", "rownum", "", "CDATA", "" + yy);
startElement("", "row", "", attrs);
// TODO: write row data.
endElement("row");
}
endElement("layer");
}
endElement("tiles");
endElement("scene");
}
endElement("scenegroup");
endDocument();
} catch (SAXException saxe) {
Log.warning("Exception writing scenes to file [fname=" +
fname + ", saxe=" + saxe + "].");
}
}
/** The scenes to be written. */
protected ArrayList _scenes;
}
@@ -0,0 +1,18 @@
//
// $Id: XMLSceneParser.java,v 1.1 2001/07/24 16:10:19 shaper Exp $
package com.threerings.miso.scene.xml;
import java.io.IOException;
import java.util.ArrayList;
import com.threerings.miso.scene.Scene;
public class XMLSceneParser
{
public Scene loadScene (String fname) throws IOException
{
// TBD
return null;
}
}
@@ -0,0 +1,74 @@
//
// $Id: XMLSceneRepository.java,v 1.1 2001/07/24 16:10:19 shaper Exp $
package com.threerings.miso.scene.xml;
import java.io.IOException;
import java.util.ArrayList;
import com.samskivert.util.Config;
import com.threerings.miso.Log;
import com.threerings.miso.scene.Scene;
import com.threerings.miso.scene.SceneRepositoryImpl;
import com.threerings.miso.tile.TileManager;
/**
* The XMLFileSceneRepository provides a mechanism for reading scenes
* from and writing scenes to XML files. These files will comprise
* the template scene files from which actual runtime game scenes will
* be constructed.
*/
public class XMLFileSceneRepository extends SceneRepositoryImpl
{
/**
* Initialize the XMLFileSceneRepository with the given config and
* tile manager objects.
*
* @param config the config object.
* @param tilemgr the tile manager object.
*/
public void init (Config config, TileManager tilemgr)
{
super.init(config, tilemgr);
_root = _config.getValue(CONFIG_ROOT, DEF_ROOT);
}
/**
* Loads and returns a Scene object for the scene described in the
* specified XML file.
*
* @param fname the full pathname to the file.
* @return the Scene object.
*/
public Scene loadScene (String fname) throws IOException
{
return _parser.loadScene(fname);
}
/**
* Writes a scene to the specified file in the scene root
* directory in XML format.
*
* @param scene the scene to save.
* @param fname the file to write the scene to.
*/
public void saveScene (Scene scene, String fname) throws IOException
{
_writer.saveScene(scene, fname);
}
/** The config key for the root scene directory. */
protected static final String CONFIG_ROOT = "miso.sceneroot";
/** The default root scene directory path. */
protected static final String DEF_ROOT = "rsrc/scenes";
/** The root scene directory path. */
protected String _root;
/** The parser object for reading scenes from files. */
protected XMLSceneParser _parser;
/** The writer object for writing scenes to files. */
protected XMLSceneWriter _writer;
}
@@ -0,0 +1,79 @@
//
// $Id: XMLSceneWriter.java,v 1.1 2001/07/24 16:10:19 shaper Exp $
package com.threerings.miso.scene.xml;
import java.io.*;
import java.util.ArrayList;
import com.megginson.sax.DataWriter;
import org.xml.sax.*;
import org.xml.sax.helpers.AttributesImpl;
import com.samskivert.util.StringUtil;
import com.threerings.miso.Log;
import com.threerings.miso.scene.Scene;
/**
* The XMLSceneWriter writes a Scene object to an XML file.
*/
public class XMLSceneWriter extends DataWriter
{
/**
* Construct an XMLSceneWriter object.
*/
public XMLSceneWriter ()
{
setIndentStep(2);
}
/**
* Write the scenes to the specified output file in XML format.
*
* @param fname the file to write to.
*/
public void saveScene (Scene scene, String fname) throws IOException
{
FileOutputStream fos = new FileOutputStream(fname);
setOutput(new OutputStreamWriter(fos));
try {
startDocument();
startElement("scene");
dataElement("name", scene.getName());
dataElement("sid", "" + scene.getId());
dataElement("version", "" + Scene.getVersion());
dataElement("hotspots",
StringUtil.toString(scene.getHotSpots()));
dataElement("exits", StringUtil.toString(scene.getExits()));
startElement("tiles");
for (int jj = 0; jj < Scene.getNumLayers(); jj++) {
AttributesImpl attrs = new AttributesImpl();
attrs.addAttribute("", "lnum", "", "CDATA", "" + jj);
startElement("", "layer", "", attrs);
for (int yy = 0; yy < Scene.getTileHeight(); yy++) {
attrs.clear();
attrs.addAttribute("", "rownum", "", "CDATA", "" + yy);
startElement("", "row", "", attrs);
// TODO: write row data.
endElement("row");
}
endElement("layer");
}
endElement("tiles");
endElement("scene");
endDocument();
} catch (SAXException saxe) {
Log.warning("Exception writing scene to file " +
"[scene=" + scene + ", [fname=" + fname +
", saxe=" + saxe + "].");
}
}
}
@@ -1,5 +1,5 @@
//
// $Id: MisoUtil.java,v 1.3 2001/07/23 22:31:48 shaper Exp $
// $Id: MisoUtil.java,v 1.4 2001/07/24 16:10:19 shaper Exp $
package com.threerings.miso.util;
@@ -12,6 +12,7 @@ import com.samskivert.util.*;
import com.threerings.media.ImageManager;
import com.threerings.miso.Log;
import com.threerings.miso.scene.*;
import com.threerings.miso.scene.xml.XMLFileSceneRepository;
import com.threerings.miso.tile.*;
import com.threerings.resource.ResourceManager;
@@ -43,11 +44,14 @@ public class MisoUtil
createSceneManager (Config config, TileManager tilemgr)
{
try {
SceneManagerImpl scenemgr = (SceneManagerImpl)
config.instantiateValue("miso.scenemgr", DEF_SCENEMGR);
scenemgr.init(tilemgr);
SceneRepositoryImpl scenerepo = (SceneRepositoryImpl)
config.instantiateValue("miso.scenerepo", DEF_SCENEREPO);
scenerepo.init(config, tilemgr);
SceneManager scenemgr = new SceneManager(scenerepo);
return scenemgr;
} catch (Exception e) {
Log.warning("Failed to instantiate scene manager [e=" + e + "].");
return null;
@@ -119,8 +123,8 @@ public class MisoUtil
}
/** The default SceneManager class name. */
protected static final String DEF_SCENEMGR =
CompiledSceneManager.class.getName();
protected static final String DEF_SCENEREPO =
XMLFileSceneRepository.class.getName();
/** The default TileSetManager class name. */
protected static final String DEF_TILESETMGR =