Behold Vilya, Ring of Air and repository for our game and virtual worldly

extensions to the distributed environment provided by Narya.


git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@1 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
Michael Bayne
2006-06-23 17:58:11 +00:00
commit a4df87e52f
317 changed files with 45818 additions and 0 deletions
@@ -0,0 +1,113 @@
//
// $Id: SceneParser.java 3749 2005-11-09 04:00:16Z mdb $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// 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.whirled.tools.xml;
import java.io.IOException;
import java.io.FileInputStream;
import org.xml.sax.SAXException;
import org.apache.commons.digester.Digester;
import com.samskivert.util.StringUtil;
import com.threerings.tools.xml.NestableRuleSet;
import com.threerings.whirled.data.AuxModel;
import com.threerings.whirled.data.SceneModel;
/**
* A simple class for parsing an editable scene instance.
*/
public class SceneParser
{
/**
* Constructs a scene parser that parses scenes with the specified XML
* path prefix.
*/
public SceneParser (String prefix)
{
// create and configure our digester
_digester = new Digester();
// create our scene rule set
SceneRuleSet set = createSceneRuleSet();
// configure our top-level path prefix
if (StringUtil.isBlank(prefix)) {
_prefix = set.getOuterElement();
} else {
_prefix = prefix + "/" + set.getOuterElement();
}
// add the scene rules
set.addRuleInstances(_prefix, _digester);
// add a rule to grab the finished scene model
_digester.addSetNext(_prefix, "setScene", SceneModel.class.getName());
}
/**
* Creates the rule set used to parse our scene.
*/
protected SceneRuleSet createSceneRuleSet ()
{
return new SceneRuleSet();
}
/**
* Adds a {@link NestableRuleSet} for parsing auxiliary scene models.
*/
public void registerAuxRuleSet (NestableRuleSet set)
{
// add their outer element to the prefix
String prefix = _prefix + "/" + set.getOuterElement();
// add the rules to generate the aux scene model
set.addRuleInstances(prefix, _digester);
// and add a rule to grab it
_digester.addSetNext(prefix, "addAuxModel", AuxModel.class.getName());
}
/**
* Parses the XML file at the specified path into a scene model
* instance.
*/
public SceneModel parseScene (String path)
throws IOException, SAXException
{
_model = null;
_digester.push(this);
_digester.parse(new FileInputStream(path));
return _model;
}
/**
* Called by the parser once the scene is parsed.
*/
public void setScene (SceneModel model)
{
_model = model;
}
protected String _prefix;
protected Digester _digester;
protected SceneModel _model;
}
@@ -0,0 +1,60 @@
//
// $Id: SceneRuleSet.java 4191 2006-06-13 22:42:20Z ray $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// 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.whirled.tools.xml;
import org.apache.commons.digester.Digester;
import com.samskivert.xml.SetPropertyFieldsRule;
import com.threerings.tools.xml.NestableRuleSet;
import com.threerings.whirled.data.SceneModel;
/**
* Used to parse a {@link SceneModel} from XML.
*/
public class SceneRuleSet implements NestableRuleSet
{
// documentation inherited from interface
public String getOuterElement ()
{
return SceneWriter.OUTER_ELEMENT;
}
// documentation inherited from interface
public void addRuleInstances (String prefix, Digester digester)
{
// this creates the appropriate instance when we encounter our tag
digester.addObjectCreate(prefix, getSceneClass().getName());
// set up rules to parse and set our fields
digester.addRule(prefix, new SetPropertyFieldsRule());
}
/**
* This indicates the class (which should extend {@link SceneModel})
* to be instantiated during the parsing process.
*/
protected Class getSceneClass ()
{
return SceneModel.class;
}
}
@@ -0,0 +1,121 @@
//
// $Id: SceneWriter.java 4191 2006-06-13 22:42:20Z ray $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// 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.whirled.tools.xml;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import com.megginson.sax.DataWriter;
import com.threerings.tools.xml.NestableWriter;
import com.threerings.whirled.Log;
import com.threerings.whirled.data.AuxModel;
import com.threerings.whirled.data.SceneModel;
/**
* Generates an XML representation of an {@link SceneModel}.
*/
public class SceneWriter
{
/** The outer element used to enclose our scene definition. */
public static final String OUTER_ELEMENT = "scene";
/**
* Registers a writer for writing auxiliary scene models of the
* supplied class.
*/
public void registerAuxWriter (Class aclass, NestableWriter writer)
{
_auxers.put(aclass, writer);
}
/**
* Writes the supplied scene out to the specified file.
*/
public void writeScene (File out, SceneModel model)
throws IOException, SAXException
{
FileWriter fout = new FileWriter(out);
DataWriter dout = new DataWriter(fout);
dout.setIndentStep(2);
dout.startDocument();
writeSceneModel(model, dout);
dout.endDocument();
fout.close();
}
/**
* Writes the data for the supplied {@link SceneModel} to the XML data
* writer supplied. The writer should already be configured with the
* appropriate indentation level so that this writer can simply output
* its elements and allow the calling code to determine where in the
* greater scene description file the scene data should live.
*/
public void writeSceneModel (SceneModel model, DataWriter writer)
throws SAXException
{
AttributesImpl attrs = new AttributesImpl();
addSceneAttributes(model, attrs);
writer.startElement("", OUTER_ELEMENT, "", attrs);
writeSceneData(model, writer);
writer.endElement(OUTER_ELEMENT);
}
/**
* Adds attributes to the top-level element before it gets written.
*/
protected void addSceneAttributes (
SceneModel model, AttributesImpl attrs)
{
attrs.addAttribute("", "name", "", "", model.name);
attrs.addAttribute("", "version", "", "",
Integer.toString(model.version));
}
/**
* Writes just the scene data which is handy for derived classes which
* may wish to add their own scene data to the scene output.
*/
protected void writeSceneData (SceneModel model, DataWriter writer)
throws SAXException
{
// write out our auxiliary scene models
for (int ii = 0; ii < model.auxModels.length; ii++) {
AuxModel amodel = model.auxModels[ii];
NestableWriter awriter = (NestableWriter)
_auxers.get(amodel.getClass());
if (awriter != null) {
awriter.write(amodel, writer);
} else {
Log.warning("No writer registered for auxiliary scene model " +
"[mclass=" + amodel.getClass() + "].");
}
}
}
protected HashMap _auxers = new HashMap();
}