2011-04-06 04:43:54 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
2011-04-06 22:01:22 +00:00
|
|
|
|
namespace Substrate.NBT
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
2011-04-07 02:44:17 +00:00
|
|
|
|
[Flags]
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public enum SchemaOptions
|
2011-04-07 02:44:17 +00:00
|
|
|
|
{
|
|
|
|
|
OPTIONAL = 0x1,
|
|
|
|
|
CREATE_ON_MISSING = 0x2,
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public abstract class SchemaNode
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
|
|
|
|
private string _name;
|
2011-06-20 03:51:40 +00:00
|
|
|
|
private SchemaOptions _options;
|
2011-04-06 04:43:54 +00:00
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return _name; }
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public SchemaOptions Options
|
2011-04-07 02:44:17 +00:00
|
|
|
|
{
|
|
|
|
|
get { return _options; }
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public SchemaNode (string name)
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
|
|
|
|
_name = name;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public SchemaNode (string name, SchemaOptions options)
|
2011-04-07 02:44:17 +00:00
|
|
|
|
{
|
|
|
|
|
_name = name;
|
|
|
|
|
_options = options;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public virtual TagNode BuildDefaultTree ()
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public sealed class SchemaNodeScaler : SchemaNode
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
2011-04-09 02:50:42 +00:00
|
|
|
|
private TagType _type;
|
2011-04-06 04:43:54 +00:00
|
|
|
|
|
2011-04-09 02:50:42 +00:00
|
|
|
|
public TagType Type
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
|
|
|
|
get { return _type; }
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public SchemaNodeScaler (string name, TagType type)
|
2011-04-06 04:43:54 +00:00
|
|
|
|
: base(name)
|
|
|
|
|
{
|
|
|
|
|
_type = type;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public SchemaNodeScaler (string name, TagType type, SchemaOptions options)
|
2011-04-07 02:44:17 +00:00
|
|
|
|
: base(name, options)
|
|
|
|
|
{
|
|
|
|
|
_type = type;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public override TagNode BuildDefaultTree ()
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
|
|
|
|
switch (_type) {
|
2011-04-09 02:50:42 +00:00
|
|
|
|
case TagType.TAG_STRING:
|
2011-06-20 03:51:40 +00:00
|
|
|
|
return new TagNodeString();
|
2011-04-06 04:43:54 +00:00
|
|
|
|
|
2011-04-09 02:50:42 +00:00
|
|
|
|
case TagType.TAG_BYTE:
|
2011-06-20 03:51:40 +00:00
|
|
|
|
return new TagNodeByte();
|
2011-04-06 04:43:54 +00:00
|
|
|
|
|
2011-04-09 02:50:42 +00:00
|
|
|
|
case TagType.TAG_SHORT:
|
2011-06-20 03:51:40 +00:00
|
|
|
|
return new TagNodeShort();
|
2011-04-06 04:43:54 +00:00
|
|
|
|
|
2011-04-09 02:50:42 +00:00
|
|
|
|
case TagType.TAG_INT:
|
2011-06-20 03:51:40 +00:00
|
|
|
|
return new TagNodeInt();
|
2011-04-06 04:43:54 +00:00
|
|
|
|
|
2011-04-09 02:50:42 +00:00
|
|
|
|
case TagType.TAG_LONG:
|
2011-06-20 03:51:40 +00:00
|
|
|
|
return new TagNodeLong();
|
2011-04-06 04:43:54 +00:00
|
|
|
|
|
2011-04-09 02:50:42 +00:00
|
|
|
|
case TagType.TAG_FLOAT:
|
2011-06-20 03:51:40 +00:00
|
|
|
|
return new TagNodeFloat();
|
2011-04-06 04:43:54 +00:00
|
|
|
|
|
2011-04-09 02:50:42 +00:00
|
|
|
|
case TagType.TAG_DOUBLE:
|
2011-06-20 03:51:40 +00:00
|
|
|
|
return new TagNodeDouble();
|
2011-04-06 04:43:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public sealed class SchemaNodeString : SchemaNode
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
|
|
|
|
private string _value = "";
|
|
|
|
|
private int _length;
|
|
|
|
|
|
|
|
|
|
public int Length
|
|
|
|
|
{
|
|
|
|
|
get { return _length; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Value
|
|
|
|
|
{
|
|
|
|
|
get { return _value; }
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public SchemaNodeString (string name, SchemaOptions options)
|
2011-04-07 02:44:17 +00:00
|
|
|
|
: base(name, options)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public SchemaNodeString (string name, string value)
|
2011-04-06 04:43:54 +00:00
|
|
|
|
: base(name)
|
|
|
|
|
{
|
|
|
|
|
_value = value;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public SchemaNodeString (string name, int length)
|
2011-04-06 04:43:54 +00:00
|
|
|
|
: base(name)
|
|
|
|
|
{
|
|
|
|
|
_length = length;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public override TagNode BuildDefaultTree ()
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
|
|
|
|
if (_value.Length > 0) {
|
2011-06-20 03:51:40 +00:00
|
|
|
|
return new TagNodeString(_value);
|
2011-04-06 04:43:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
return new TagNodeString();
|
2011-04-06 04:43:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public sealed class SchemaNodeArray : SchemaNode
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
|
|
|
|
private int _length;
|
|
|
|
|
|
|
|
|
|
public int Length
|
|
|
|
|
{
|
|
|
|
|
get { return _length; }
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public SchemaNodeArray (string name)
|
2011-04-06 04:43:54 +00:00
|
|
|
|
: base(name)
|
|
|
|
|
{
|
|
|
|
|
_length = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public SchemaNodeArray (string name, SchemaOptions options)
|
2011-04-07 02:44:17 +00:00
|
|
|
|
: base(name, options)
|
|
|
|
|
{
|
|
|
|
|
_length = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public SchemaNodeArray (string name, int length)
|
2011-04-06 04:43:54 +00:00
|
|
|
|
: base(name)
|
|
|
|
|
{
|
|
|
|
|
_length = length;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public SchemaNodeArray (string name, int length, SchemaOptions options)
|
2011-04-07 02:44:17 +00:00
|
|
|
|
: base(name, options)
|
|
|
|
|
{
|
|
|
|
|
_length = length;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public override TagNode BuildDefaultTree ()
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
2011-06-20 03:51:40 +00:00
|
|
|
|
return new TagNodeByteArray(new byte[_length]);
|
2011-04-06 04:43:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public sealed class SchemaNodeList : SchemaNode
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
2011-04-09 02:50:42 +00:00
|
|
|
|
private TagType _type;
|
2011-04-06 04:43:54 +00:00
|
|
|
|
private int _length;
|
2011-06-20 03:51:40 +00:00
|
|
|
|
private SchemaNode _subschema;
|
2011-04-06 04:43:54 +00:00
|
|
|
|
|
|
|
|
|
public int Length
|
|
|
|
|
{
|
|
|
|
|
get { return _length; }
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-09 02:50:42 +00:00
|
|
|
|
public TagType Type
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
|
|
|
|
get { return _type; }
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public SchemaNode SubSchema
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
|
|
|
|
get { return _subschema; }
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public SchemaNodeList (string name, TagType type)
|
2011-04-06 04:43:54 +00:00
|
|
|
|
: base(name)
|
|
|
|
|
{
|
|
|
|
|
_type = type;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public SchemaNodeList (string name, TagType type, SchemaOptions options)
|
2011-04-07 02:44:17 +00:00
|
|
|
|
: base(name, options)
|
|
|
|
|
{
|
|
|
|
|
_type = type;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public SchemaNodeList (string name, TagType type, int length)
|
2011-04-06 04:43:54 +00:00
|
|
|
|
: base(name)
|
|
|
|
|
{
|
|
|
|
|
_type = type;
|
|
|
|
|
_length = length;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public SchemaNodeList (string name, TagType type, int length, SchemaOptions options)
|
2011-04-07 02:44:17 +00:00
|
|
|
|
: base(name, options)
|
|
|
|
|
{
|
|
|
|
|
_type = type;
|
|
|
|
|
_length = length;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public SchemaNodeList (string name, TagType type, SchemaNode subschema)
|
2011-04-06 04:43:54 +00:00
|
|
|
|
: base(name)
|
|
|
|
|
{
|
|
|
|
|
_type = type;
|
|
|
|
|
_subschema = subschema;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public SchemaNodeList (string name, TagType type, SchemaNode subschema, SchemaOptions options)
|
2011-04-07 02:44:17 +00:00
|
|
|
|
: base(name, options)
|
|
|
|
|
{
|
|
|
|
|
_type = type;
|
|
|
|
|
_subschema = subschema;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public SchemaNodeList (string name, TagType type, int length, SchemaNode subschema)
|
2011-04-06 04:43:54 +00:00
|
|
|
|
: base(name)
|
|
|
|
|
{
|
|
|
|
|
_type = type;
|
|
|
|
|
_length = length;
|
|
|
|
|
_subschema = subschema;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public SchemaNodeList (string name, TagType type, int length, SchemaNode subschema, SchemaOptions options)
|
2011-04-07 02:44:17 +00:00
|
|
|
|
: base(name, options)
|
|
|
|
|
{
|
|
|
|
|
_type = type;
|
|
|
|
|
_length = length;
|
|
|
|
|
_subschema = subschema;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public override TagNode BuildDefaultTree ()
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
|
|
|
|
if (_length == 0) {
|
2011-06-20 03:51:40 +00:00
|
|
|
|
return new TagNodeList(_type);
|
2011-04-06 04:43:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
TagNodeList list = new TagNodeList(_type);
|
2011-04-06 04:43:54 +00:00
|
|
|
|
for (int i = 0; i < _length; i++) {
|
|
|
|
|
list.Add(_subschema.BuildDefaultTree());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public sealed class SchemaNodeCompound : SchemaNode, ICollection<SchemaNode>
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
2011-06-20 03:51:40 +00:00
|
|
|
|
private List<SchemaNode> _subnodes;
|
2011-04-06 04:43:54 +00:00
|
|
|
|
|
|
|
|
|
#region ICollection<NBTSchemaNode> Members
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public void Add (SchemaNode item)
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
|
|
|
|
_subnodes.Add(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Clear ()
|
|
|
|
|
{
|
|
|
|
|
_subnodes.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public bool Contains (SchemaNode item)
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
|
|
|
|
return _subnodes.Contains(item);
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public void CopyTo (SchemaNode[] array, int arrayIndex)
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
|
|
|
|
_subnodes.CopyTo(array, arrayIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Count
|
|
|
|
|
{
|
|
|
|
|
get { return _subnodes.Count; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsReadOnly
|
|
|
|
|
{
|
|
|
|
|
get { return false; }
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public bool Remove (SchemaNode item)
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
|
|
|
|
return _subnodes.Remove(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region IEnumerable<NBTSchemaNode> Members
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public IEnumerator<SchemaNode> GetEnumerator ()
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
|
|
|
|
return _subnodes.GetEnumerator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region IEnumerable Members
|
|
|
|
|
|
|
|
|
|
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator ()
|
|
|
|
|
{
|
|
|
|
|
return _subnodes.GetEnumerator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public SchemaNodeCompound ()
|
2011-04-06 04:43:54 +00:00
|
|
|
|
: base("")
|
|
|
|
|
{
|
2011-06-20 03:51:40 +00:00
|
|
|
|
_subnodes = new List<SchemaNode>();
|
2011-04-06 04:43:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public SchemaNodeCompound (SchemaOptions options)
|
2011-04-07 02:44:17 +00:00
|
|
|
|
: base("", options)
|
|
|
|
|
{
|
2011-06-20 03:51:40 +00:00
|
|
|
|
_subnodes = new List<SchemaNode>();
|
2011-04-07 02:44:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public SchemaNodeCompound (string name)
|
2011-04-06 04:43:54 +00:00
|
|
|
|
: base(name)
|
|
|
|
|
{
|
2011-06-20 03:51:40 +00:00
|
|
|
|
_subnodes = new List<SchemaNode>();
|
2011-04-06 04:43:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public SchemaNodeCompound (string name, SchemaOptions options)
|
2011-04-07 02:44:17 +00:00
|
|
|
|
: base(name, options)
|
|
|
|
|
{
|
2011-06-20 03:51:40 +00:00
|
|
|
|
_subnodes = new List<SchemaNode>();
|
2011-04-07 02:44:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public SchemaNodeCompound (string name, SchemaNode subschema)
|
2011-04-06 21:18:48 +00:00
|
|
|
|
: base(name)
|
|
|
|
|
{
|
2011-06-20 03:51:40 +00:00
|
|
|
|
_subnodes = new List<SchemaNode>();
|
2011-04-08 06:48:27 +00:00
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
SchemaNodeCompound schema = subschema as SchemaNodeCompound;
|
2011-04-06 21:18:48 +00:00
|
|
|
|
if (schema == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
foreach (SchemaNode node in schema._subnodes)
|
2011-04-06 21:18:48 +00:00
|
|
|
|
{
|
|
|
|
|
_subnodes.Add(node);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public SchemaNodeCompound (string name, SchemaNode subschema, SchemaOptions options)
|
2011-04-07 02:44:17 +00:00
|
|
|
|
: base(name, options)
|
|
|
|
|
{
|
2011-06-20 03:51:40 +00:00
|
|
|
|
_subnodes = new List<SchemaNode>();
|
2011-04-08 06:48:27 +00:00
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
SchemaNodeCompound schema = subschema as SchemaNodeCompound;
|
2011-04-07 02:44:17 +00:00
|
|
|
|
if (schema == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
foreach (SchemaNode node in schema._subnodes) {
|
2011-04-07 02:44:17 +00:00
|
|
|
|
_subnodes.Add(node);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public SchemaNodeCompound MergeInto (SchemaNodeCompound tree)
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
2011-06-20 03:51:40 +00:00
|
|
|
|
foreach (SchemaNode node in _subnodes) {
|
|
|
|
|
SchemaNode f = tree._subnodes.Find(n => n.Name == node.Name);
|
2011-04-06 04:43:54 +00:00
|
|
|
|
if (f != null) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
tree.Add(node);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tree;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 03:51:40 +00:00
|
|
|
|
public override TagNode BuildDefaultTree ()
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
2011-06-20 03:51:40 +00:00
|
|
|
|
TagNodeCompound list = new TagNodeCompound();
|
|
|
|
|
foreach (SchemaNode node in _subnodes) {
|
2011-04-06 04:43:54 +00:00
|
|
|
|
list[node.Name] = node.BuildDefaultTree();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|