Changes of dubious merit.

This commit is contained in:
Justin Aquadro 2011-06-30 04:41:29 +00:00
parent ec2342d767
commit 999f4f96fa
80 changed files with 74 additions and 3788 deletions

View file

@ -7,7 +7,7 @@ namespace Substrate
/// A single Alpha-compatible block with context-independent data.
/// </summary>
/// <remarks><para>In general, you should prefer other types for accessing block data including <see cref="AlphaBlockRef"/>,
/// <see cref="BlockManager"/>, and the <see cref="AlphaBlockCollection"/> property of <see cref="Chunk"/> and<see cref="ChunkRef"/>.</para>
/// <see cref="BlockManager"/>, and the <see cref="AlphaBlockCollection"/> property of <see cref="Chunk"/> and <see cref="ChunkRef"/>.</para>
/// <para>You should use the <see cref="AlphaBlock"/> type when you need to copy individual blocks into a custom collection or
/// container, and context-depdendent data such as coordinates and lighting have no well-defined meaning. <see cref="AlphaBlock"/>
/// offers a relatively compact footprint for storing the unique identity of a block's manifestation in the world.</para>

View file

@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using Substrate.Core;
using Substrate.NBT;
using Substrate.Nbt;
namespace Substrate
{

View file

@ -9,7 +9,7 @@ namespace Substrate
/// A reference to a single Alpha-compatible block in an <see cref="AlphaBlockCollection"/>.
/// </summary>
/// <remarks><para>The <see cref="AlphaBlockRef"/> type provides a reasonably lightweight reference to an individual block in a
/// <see cref="AlphaBlockCollection"/>. The <see cref="AlphaBLockRef"/> does not store any of the data itself. If the referenced
/// <see cref="AlphaBlockCollection"/>. The <see cref="AlphaBlockRef"/> does not store any of the data itself. If the referenced
/// block in the <see cref="AlphaBlockCollection"/> is updated externally, those changes will be automatically reflected in the
/// <see cref="AlphaBlockRef"/>, and any changes made via the <see cref="AlphaBlockRef"/> will be applied directly to the corresponding
/// block within the <see cref="AlphaBlockCollection"/>. Such changes will also set the dirty status of the <see cref="AlphaBlockCollection"/>,

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate
{
using NBT;
using Nbt;
public static class BlockType
{

View file

@ -2,7 +2,7 @@
using System.IO;
using System.Collections.Generic;
using Substrate.Core;
using Substrate.NBT;
using Substrate.Nbt;
namespace Substrate
{
@ -39,7 +39,7 @@ namespace Substrate
},
};
private NBT_Tree _tree;
private NbtTree _tree;
private int _cx;
private int _cz;
@ -91,7 +91,7 @@ namespace Substrate
/// <summary>
/// Provides raw access to the underlying NBT_Tree.
/// </summary>
public NBT_Tree Tree
public NbtTree Tree
{
get { return _tree; }
}
@ -133,7 +133,7 @@ namespace Substrate
/// </summary>
/// <param name="tree">An NBT_Tree conforming to the chunk schema definition.</param>
/// <returns>A new Chunk object wrapping an existing NBT_Tree.</returns>
public static Chunk Create (NBT_Tree tree)
public static Chunk Create (NbtTree tree)
{
Chunk c = new Chunk();
@ -145,7 +145,7 @@ namespace Substrate
/// </summary>
/// <param name="tree">An NBT_Tree conforming to the chunk schema definition.</param>
/// <returns>A new Chunk object wrapping an existing NBT_Tree, or null on verification failure.</returns>
public static Chunk CreateVerified (NBT_Tree tree)
public static Chunk CreateVerified (NbtTree tree)
{
Chunk c = new Chunk();
@ -195,7 +195,7 @@ namespace Substrate
return null;
}
_tree = new NBT_Tree(ctree);
_tree = new NbtTree(ctree);
TagNodeCompound level = _tree.Root["Level"] as TagNodeCompound;
@ -312,7 +312,7 @@ namespace Substrate
level.Add("zPos", new TagNodeInt(_cz));
level.Add("TerrainPopulated", new TagNodeByte());
_tree = new NBT_Tree();
_tree = new NbtTree();
_tree.Root.Add("Level", level);
_blockManager = new AlphaBlockCollection(_blocks, _data, _blockLight, _skyLight, _heightMap, _tileEntities);

View file

@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
using Substrate.NBT;
using Substrate.Nbt;
namespace Substrate.Core
{

View file

@ -4,7 +4,7 @@ using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using Substrate.NBT;
using Substrate.Nbt;
namespace Substrate.Core
{
@ -33,7 +33,7 @@ namespace Substrate.Core
return new ChunkFile(_mapPath, cx, cz);
}
protected NBT_Tree GetChunkTree (int cx, int cz)
protected NbtTree GetChunkTree (int cx, int cz)
{
ChunkFile cf = GetChunkFile(cx, cz);
Stream nbtstr = cf.GetDataInputStream();
@ -41,10 +41,10 @@ namespace Substrate.Core
return null;
}
return new NBT_Tree(nbtstr);
return new NbtTree(nbtstr);
}
protected bool SaveChunkTree (int cx, int cz, NBT_Tree tree)
protected bool SaveChunkTree (int cx, int cz, NbtTree tree)
{
ChunkFile cf = GetChunkFile(cx, cz);
Stream zipstr = cf.GetDataOutputStream();

View file

@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Text;
using System.IO;
using Ionic.Zlib;
using Substrate.NBT;
using Substrate.Nbt;
namespace Substrate.Core
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.Entities
{
using Substrate.NBT;
using Substrate.Nbt;
public class EntityArrow : EntityThrowable
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.Entities
{
using Substrate.NBT;
using Substrate.Nbt;
public class EntityBoat : EntityTyped
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.Entities
{
using Substrate.NBT;
using Substrate.Nbt;
public class EntityChicken : EntityMob
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.Entities
{
using Substrate.NBT;
using Substrate.Nbt;
public class EntityCow : EntityMob
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.Entities
{
using Substrate.NBT;
using Substrate.Nbt;
public class EntityCreeper : EntityMob
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.Entities
{
using Substrate.NBT;
using Substrate.Nbt;
public class EntityEgg : EntityThrowable
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.Entities
{
using Substrate.NBT;
using Substrate.Nbt;
public class EntityFallingSand : EntityTyped
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.Entities
{
using Substrate.NBT;
using Substrate.Nbt;
public class EntityGhast : EntityMob
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.Entities
{
using Substrate.NBT;
using Substrate.Nbt;
public class EntityGiant : EntityMob
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.Entities
{
using Substrate.NBT;
using Substrate.Nbt;
public class EntityItem : EntityTyped
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.Entities
{
using Substrate.NBT;
using Substrate.Nbt;
public class EntityMinecart : EntityTyped
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.Entities
{
using Substrate.NBT;
using Substrate.Nbt;
public class EntityMinecartChest : EntityMinecart, IItemContainer
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.Entities
{
using Substrate.NBT;
using Substrate.Nbt;
public class EntityMinecartFurnace : EntityMinecart
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.Entities
{
using Substrate.NBT;
using Substrate.Nbt;
public class EntityMob : EntityTyped
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.Entities
{
using Substrate.NBT;
using Substrate.Nbt;
public class EntityMonster : EntityMob
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.Entities
{
using Substrate.NBT;
using Substrate.Nbt;
public class EntityPainting : EntityTyped
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.Entities
{
using Substrate.NBT;
using Substrate.Nbt;
public class EntityPig : EntityMob
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.Entities
{
using Substrate.NBT;
using Substrate.Nbt;
public class EntityPigZombie : EntityMob
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.Entities
{
using Substrate.NBT;
using Substrate.Nbt;
public class EntityPrimedTnt : EntityTyped
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.Entities
{
using Substrate.NBT;
using Substrate.Nbt;
public class EntitySheep : EntityMob
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.Entities
{
using Substrate.NBT;
using Substrate.Nbt;
public class EntitySkeleton : EntityMob
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.Entities
{
using Substrate.NBT;
using Substrate.Nbt;
public class EntitySlime : EntityMob
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.Entities
{
using Substrate.NBT;
using Substrate.Nbt;
public class EntitySnowball : EntityThrowable
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.Entities
{
using Substrate.NBT;
using Substrate.Nbt;
public class EntitySpider : EntityMob
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.Entities
{
using Substrate.NBT;
using Substrate.Nbt;
public class EntitySquid : EntityMob
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.Entities
{
using Substrate.NBT;
using Substrate.Nbt;
public class EntityThrowable : EntityTyped
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.Entities
{
using Substrate.NBT;
using Substrate.Nbt;
public class EntityWolf : EntityMob
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.Entities
{
using Substrate.NBT;
using Substrate.Nbt;
public class EntityZombie : EntityMob
{

View file

@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Text;
using Substrate.Core;
using Substrate.NBT;
using Substrate.Nbt;
namespace Substrate
{
@ -11,19 +11,6 @@ namespace Substrate
/// </summary>
public class Entity : INBTObject<Entity>, ICopyable<Entity>
{
public class Vector3
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
}
public class Orientation
{
public double Pitch { get; set; }
public double Yaw { get; set; }
}
private static readonly SchemaNodeCompound _schema = new SchemaNodeCompound("")
{
new SchemaNodeList("Pos", TagType.TAG_DOUBLE, 3),

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate
{
using NBT;
using Nbt;
public class EntityCollection : IEnumerable<EntityTyped>
{

View file

@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using Substrate.Entities;
using Substrate.NBT;
using Substrate.Nbt;
namespace Substrate
{

View file

@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Text;
using Substrate.Core;
using Substrate.NBT;
using Substrate.Nbt;
namespace Substrate
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate
{
using NBT;
using Nbt;
public static class ItemType
{

View file

@ -1,7 +1,7 @@
using System;
using System.IO;
using Substrate.Core;
using Substrate.NBT;
using Substrate.Nbt;
namespace Substrate
{
@ -203,7 +203,7 @@ namespace Substrate
return false;
}
new NBT_Tree(BuildTree() as TagNodeCompound).WriteTo(zipstr);
new NbtTree(BuildTree() as TagNodeCompound).WriteTo(zipstr);
zipstr.Close();
return true;

View file

@ -1,38 +0,0 @@
using System;
namespace Substrate.NBT
{
/// <summary>
/// Defines methods for loading or extracting an NBT tree.
/// </summary>
/// <typeparam name="T">Object type that supports this interface.</typeparam>
public interface INBTObject<T>
{
/// <summary>
/// Attempt to load an NBT tree into the object without validation.
/// </summary>
/// <param name="tree">The root node of an NBT tree.</param>
/// <returns>The object returns itself on success, or null if the tree was unparsable.</returns>
T LoadTree (TagNode tree);
/// <summary>
/// Attempt to load an NBT tree into the object with validation.
/// </summary>
/// <param name="tree">The root node of an NBT tree.</param>
/// <returns>The object returns itself on success, or null if the tree failed validation.</returns>
T LoadTreeSafe (TagNode tree);
/// <summary>
/// Builds an NBT tree from the object's data.
/// </summary>
/// <returns>The root node of an NBT tree representing the object's data.</returns>
TagNode BuildTree ();
/// <summary>
/// Validate an NBT tree, usually against an object-supplied schema.
/// </summary>
/// <param name="tree">The root node of an NBT tree.</param>
/// <returns>Status indicating whether the tree was valid for this object.</returns>
bool ValidateTree (TagNode tree);
}
}

View file

@ -1,173 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
using Substrate.Core;
namespace Substrate.NBT
{
public class JSONSerializer
{
public static string Serialize (TagNode tag)
{
return Serialize(tag, 0);
}
public static string Serialize (TagNode tag, int level)
{
StringBuilder str = new StringBuilder();
if (tag.GetTagType() == TagType.TAG_COMPOUND) {
SerializeCompound(tag as TagNodeCompound, str, level);
}
else if (tag.GetTagType() == TagType.TAG_LIST) {
SerializeList(tag as TagNodeList, str, level);
}
else {
SerializeScaler(tag, str);
}
return str.ToString();
}
private static void SerializeCompound (TagNodeCompound tag, StringBuilder str, int level)
{
if (tag.Count == 0) {
str.Append("{ }");
return;
}
str.AppendLine();
AddLine(str, "{", level);
IEnumerator<KeyValuePair<string, TagNode>> en = tag.GetEnumerator();
bool first = true;
while (en.MoveNext()) {
if (!first) {
str.Append(",");
str.AppendLine();
}
KeyValuePair<string, TagNode> item = en.Current;
Add(str, "\"" + item.Key + "\": ", level + 1);
if (item.Value.GetTagType() == TagType.TAG_COMPOUND) {
SerializeCompound(item.Value as TagNodeCompound, str, level + 1);
}
else if (item.Value.GetTagType() == TagType.TAG_LIST) {
SerializeList(item.Value as TagNodeList, str, level + 1);
}
else {
SerializeScaler(item.Value, str);
}
first = false;
}
str.AppendLine();
Add(str, "}", level);
}
private static void SerializeList (TagNodeList tag, StringBuilder str, int level)
{
if (tag.Count == 0) {
str.Append("[ ]");
return;
}
str.AppendLine();
AddLine(str, "[", level);
IEnumerator<TagNode> en = tag.GetEnumerator();
bool first = true;
while (en.MoveNext()) {
if (!first) {
str.Append(",");
}
TagNode item = en.Current;
if (item.GetTagType() == TagType.TAG_COMPOUND) {
SerializeCompound(item as TagNodeCompound, str, level + 1);
}
else if (item.GetTagType() == TagType.TAG_LIST) {
SerializeList(item as TagNodeList, str, level + 1);
}
else {
if (!first) {
str.AppendLine();
}
Indent(str, level + 1);
SerializeScaler(item, str);
}
first = false;
}
str.AppendLine();
Add(str, "]", level);
}
private static void SerializeScaler (TagNode tag, StringBuilder str)
{
switch (tag.GetTagType()) {
case TagType.TAG_STRING:
str.Append("\"" + tag.ToTagString().Data + "\"");
break;
case TagType.TAG_BYTE:
str.Append(tag.ToTagByte().Data);
break;
case TagType.TAG_SHORT:
str.Append(tag.ToTagShort().Data);
break;
case TagType.TAG_INT:
str.Append(tag.ToTagInt().Data);
break;
case TagType.TAG_LONG:
str.Append(tag.ToTagLong().Data);
break;
case TagType.TAG_FLOAT:
str.Append(tag.ToTagFloat().Data);
break;
case TagType.TAG_DOUBLE:
str.Append(tag.ToTagDouble().Data);
break;
case TagType.TAG_BYTE_ARRAY:
str.Append(Convert.ToBase64String(tag.ToTagByteArray().Data));
/*if (tag.ToTagByteArray().Length == (16 * 16 * 128 / 2)) {
str.Append(Base16.Encode(tag.ToTagByteArray().Data, 1));
}
else {
str.Append(Base16.Encode(tag.ToTagByteArray().Data, 2));
}*/
break;
}
}
private static void AddLine (StringBuilder str, string line, int level)
{
Indent(str, level);
str.AppendLine(line);
}
private static void Add (StringBuilder str, string line, int level)
{
Indent(str, level);
str.Append(line);
}
private static void Indent (StringBuilder str, int count)
{
for (int i = 0; i < count; i++) {
str.Append("\t");
}
}
}
}

View file

@ -1,534 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Compression;
using Substrate.Core;
namespace Substrate.NBT
{
/// <summary>
/// Contains the root node of an NBT tree and handles IO of tree nodes.
/// </summary>
/// <remarks>
/// NBT, or Named Byte Tag, is a tree-based data structure for storing most Minecraft data.
/// NBT_Tree is more of a helper class for NBT trees that handles reading and writing nodes to data streams.
/// Most of the API takes a TagValue or derived node as the root of the tree, rather than an NBT_Tree object itself.
/// </remarks>
public class NBT_Tree : ICopyable<NBT_Tree>
{
private Stream _stream = null;
private TagNodeCompound _root = null;
private static TagNodeNull _nulltag = new TagNodeNull();
/// <summary>
/// Gets the root node of this tree.
/// </summary>
public TagNodeCompound Root
{
get { return _root; }
}
/// <summary>
/// Constructs a wrapper around a new NBT tree with an empty root node.
/// </summary>
public NBT_Tree ()
{
_root = new TagNodeCompound();
}
/// <summary>
/// Constructs a wrapper around another NBT tree.
/// </summary>
/// <param name="tree">The root node of an NBT tree.</param>
public NBT_Tree (TagNodeCompound tree)
{
_root = tree;
}
/// <summary>
/// Constructs and wrapper around a new NBT tree parsed from a source data stream.
/// </summary>
/// <param name="s">An open, readable data stream containing NBT data.</param>
public NBT_Tree (Stream s)
{
ReadFrom(s);
}
/// <summary>
/// Rebuild the internal NBT tree from a source data stream.
/// </summary>
/// <param name="s">An open, readable data stream containing NBT data.</param>
public void ReadFrom (Stream s)
{
if (s != null) {
_stream = s;
_root = ReadRoot();
_stream = null;
}
}
/// <summary>
/// Writes out the internal NBT tree to a destination data stream.
/// </summary>
/// <param name="s">An open, writable data stream.</param>
public void WriteTo (Stream s)
{
if (s != null) {
_stream = s;
if (_root != null) {
WriteTag("", _root);
}
_stream = null;
}
}
private TagNode ReadValue (TagType type)
{
switch (type) {
case TagType.TAG_END:
return null;
case TagType.TAG_BYTE:
return ReadByte();
case TagType.TAG_SHORT:
return ReadShort();
case TagType.TAG_INT:
return ReadInt();
case TagType.TAG_LONG:
return ReadLong();
case TagType.TAG_FLOAT:
return ReadFloat();
case TagType.TAG_DOUBLE:
return ReadDouble();
case TagType.TAG_BYTE_ARRAY:
return ReadByteArray();
case TagType.TAG_STRING:
return ReadString();
case TagType.TAG_LIST:
return ReadList();
case TagType.TAG_COMPOUND:
return ReadCompound();
}
throw new Exception();
}
private TagNode ReadByte ()
{
int gzByte = _stream.ReadByte();
if (gzByte == -1) {
throw new NBTException(NBTException.MSG_GZIP_ENDOFSTREAM);
}
TagNodeByte val = new TagNodeByte((byte)gzByte);
return val;
}
private TagNode ReadShort ()
{
byte[] gzBytes = new byte[2];
_stream.Read(gzBytes, 0, 2);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
TagNodeShort val = new TagNodeShort(BitConverter.ToInt16(gzBytes, 0));
return val;
}
private TagNode ReadInt ()
{
byte[] gzBytes = new byte[4];
_stream.Read(gzBytes, 0, 4);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
TagNodeInt val = new TagNodeInt(BitConverter.ToInt32(gzBytes, 0));
return val;
}
private TagNode ReadLong ()
{
byte[] gzBytes = new byte[8];
_stream.Read(gzBytes, 0, 8);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
TagNodeLong val = new TagNodeLong(BitConverter.ToInt64(gzBytes, 0));
return val;
}
private TagNode ReadFloat ()
{
byte[] gzBytes = new byte[4];
_stream.Read(gzBytes, 0, 4);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
TagNodeFloat val = new TagNodeFloat(BitConverter.ToSingle(gzBytes, 0));
return val;
}
private TagNode ReadDouble ()
{
byte[] gzBytes = new byte[8];
_stream.Read(gzBytes, 0, 8);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
TagNodeDouble val = new TagNodeDouble(BitConverter.ToDouble(gzBytes, 0));
return val;
}
private TagNode ReadByteArray ()
{
byte[] lenBytes = new byte[4];
_stream.Read(lenBytes, 0, 4);
if (BitConverter.IsLittleEndian) {
Array.Reverse(lenBytes);
}
int length = BitConverter.ToInt32(lenBytes, 0);
if (length < 0) {
throw new NBTException(NBTException.MSG_READ_NEG);
}
byte[] data = new byte[length];
_stream.Read(data, 0, length);
TagNodeByteArray val = new TagNodeByteArray(data);
return val;
}
private TagNode ReadString ()
{
byte[] lenBytes = new byte[2];
_stream.Read(lenBytes, 0, 2);
if (BitConverter.IsLittleEndian) {
Array.Reverse(lenBytes);
}
short len = BitConverter.ToInt16(lenBytes, 0);
if (len < 0) {
throw new NBTException(NBTException.MSG_READ_NEG);
}
byte[] strBytes = new byte[len];
_stream.Read(strBytes, 0, len);
System.Text.Encoding str = Encoding.GetEncoding(28591);
TagNodeString val = new TagNodeString(str.GetString(strBytes));
return val;
}
private TagNode ReadList ()
{
int gzByte = _stream.ReadByte();
if (gzByte == -1) {
throw new NBTException(NBTException.MSG_GZIP_ENDOFSTREAM);
}
TagNodeList val = new TagNodeList((TagType)gzByte);
if (val.ValueType > (TagType)Enum.GetValues(typeof(TagType)).GetUpperBound(0)) {
throw new NBTException(NBTException.MSG_READ_TYPE);
}
byte[] lenBytes = new byte[4];
_stream.Read(lenBytes, 0, 4);
if (BitConverter.IsLittleEndian) {
Array.Reverse(lenBytes);
}
int length = BitConverter.ToInt32(lenBytes, 0);
if (length < 0) {
throw new NBTException(NBTException.MSG_READ_NEG);
}
for (int i = 0; i < length; i++) {
val.Add(ReadValue(val.ValueType));
}
return val;
}
private TagNode ReadCompound ()
{
TagNodeCompound val = new TagNodeCompound();
while (ReadTag(val)) ;
return val;
}
private TagNodeCompound ReadRoot ()
{
TagType type = (TagType)_stream.ReadByte();
if (type == TagType.TAG_COMPOUND) {
ReadString(); // name
return ReadValue(type) as TagNodeCompound;
}
return null;
}
private bool ReadTag (TagNodeCompound parent)
{
//NBT_Tag tag = new NBT_Tag();
TagType type = (TagType)_stream.ReadByte();
if (type != TagType.TAG_END) {
string name = ReadString().ToTagString().Data;
parent[name] = ReadValue(type);
return true;
}
return false;
//tag.Value = ReadValue(type);
//return tag;
}
private void WriteValue (TagNode val)
{
switch (val.GetTagType()) {
case TagType.TAG_END:
break;
case TagType.TAG_BYTE:
WriteByte(val.ToTagByte());
break;
case TagType.TAG_SHORT:
WriteShort(val.ToTagShort());
break;
case TagType.TAG_INT:
WriteInt(val.ToTagInt());
break;
case TagType.TAG_LONG:
WriteLong(val.ToTagLong());
break;
case TagType.TAG_FLOAT:
WriteFloat(val.ToTagFloat());
break;
case TagType.TAG_DOUBLE:
WriteDouble(val.ToTagDouble());
break;
case TagType.TAG_BYTE_ARRAY:
WriteByteArray(val.ToTagByteArray());
break;
case TagType.TAG_STRING:
WriteString(val.ToTagString());
break;
case TagType.TAG_LIST:
WriteList(val.ToTagList());
break;
case TagType.TAG_COMPOUND:
WriteCompound(val.ToTagCompound());
break;
}
}
private void WriteByte (TagNodeByte val)
{
_stream.WriteByte(val.Data);
}
private void WriteShort (TagNodeShort val)
{
byte[] gzBytes = BitConverter.GetBytes(val.Data);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
_stream.Write(gzBytes, 0, 2);
}
private void WriteInt (TagNodeInt val)
{
byte[] gzBytes = BitConverter.GetBytes(val.Data);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
_stream.Write(gzBytes, 0, 4);
}
private void WriteLong (TagNodeLong val)
{
byte[] gzBytes = BitConverter.GetBytes(val.Data);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
_stream.Write(gzBytes, 0, 8);
}
private void WriteFloat (TagNodeFloat val)
{
byte[] gzBytes = BitConverter.GetBytes(val.Data);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
_stream.Write(gzBytes, 0, 4);
}
private void WriteDouble (TagNodeDouble val)
{
byte[] gzBytes = BitConverter.GetBytes(val.Data);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
_stream.Write(gzBytes, 0, 8);
}
private void WriteByteArray (TagNodeByteArray val)
{
byte[] lenBytes = BitConverter.GetBytes(val.Length);
if (BitConverter.IsLittleEndian) {
Array.Reverse(lenBytes);
}
_stream.Write(lenBytes, 0, 4);
_stream.Write(val.Data, 0, val.Length);
}
private void WriteString (TagNodeString val)
{
byte[] lenBytes = BitConverter.GetBytes((short)val.Length);
if (BitConverter.IsLittleEndian) {
Array.Reverse(lenBytes);
}
_stream.Write(lenBytes, 0, 2);
System.Text.Encoding str = Encoding.GetEncoding(28591);
byte[] gzBytes = str.GetBytes(val.Data);
_stream.Write(gzBytes, 0, gzBytes.Length);
}
private void WriteList (TagNodeList val)
{
byte[] lenBytes = BitConverter.GetBytes(val.Count);
if (BitConverter.IsLittleEndian) {
Array.Reverse(lenBytes);
}
_stream.WriteByte((byte)val.ValueType);
_stream.Write(lenBytes, 0, 4);
foreach (TagNode v in val) {
WriteValue(v);
}
}
private void WriteCompound (TagNodeCompound val)
{
foreach (KeyValuePair<string, TagNode> item in val) {
WriteTag(item.Key, item.Value);
}
WriteTag(null, _nulltag);
}
private void WriteTag (string name, TagNode val)
{
_stream.WriteByte((byte)val.GetTagType());
if (val.GetTagType() != TagType.TAG_END) {
WriteString(name);
WriteValue(val);
}
}
#region ICopyable<NBT_Tree> Members
/// <summary>
/// Creates a deep copy of the NBT_Tree and underlying nodes.
/// </summary>
/// <returns>A new NBT_tree.</returns>
public NBT_Tree Copy ()
{
NBT_Tree tree = new NBT_Tree();
tree._root = _root.Copy() as TagNodeCompound;
return tree;
}
#endregion
}
// TODO: Revise exceptions?
public class NBTException : Exception
{
public const String MSG_GZIP_ENDOFSTREAM = "Gzip Error: Unexpected end of stream";
public const String MSG_READ_NEG = "Read Error: Negative length";
public const String MSG_READ_TYPE = "Read Error: Invalid value type";
public NBTException () { }
public NBTException (String msg) : base(msg) { }
public NBTException (String msg, Exception innerException) : base(msg, innerException) { }
}
public class InvalidNBTObjectException : Exception { }
public class InvalidTagException : Exception { }
public class InvalidValueException : Exception { }
}

View file

@ -1,386 +0,0 @@
using System;
using System.Collections.Generic;
using Substrate.Core;
namespace Substrate.NBT
{
/// <summary>
/// Indicates how an <see cref="NBTVerifier"/> event processor should respond to returning event handler.
/// </summary>
public enum TagEventCode
{
/// <summary>
/// The event processor should process the next event in the chian.
/// </summary>
NEXT,
/// <summary>
/// The event processor should ignore the verification failure and stop processing any remaining events.
/// </summary>
PASS,
/// <summary>
/// The event processor should fail and stop processing any remaining events.
/// </summary>
FAIL,
}
/// <summary>
/// Event arguments for <see cref="NBTVerifier"/> failure events.
/// </summary>
public class TagEventArgs : EventArgs
{
private string _tagName;
private TagNode _parent;
private TagNode _tag;
private SchemaNode _schema;
/// <summary>
/// Gets the expected name of the <see cref="TagNode"/> referenced by this event.
/// </summary>
public string TagName
{
get { return _tagName; }
}
/// <summary>
/// Gets the parent <see cref="TagNode"/> of the <see cref="TagNode"/> referenced by this event, if it exists.
/// </summary>
public TagNode Parent
{
get { return _parent; }
}
/// <summary>
/// Gets the <see cref="TagNode"/> referenced by this event.
/// </summary>
public TagNode Tag
{
get { return _tag; }
}
/// <summary>
/// Gets the <see cref="SchemaNode"/> corresponding to the <see cref="TagNode"/> referenced by this event.
/// </summary>
public SchemaNode Schema
{
get { return _schema; }
}
/// <summary>
/// Constructs a new event argument set.
/// </summary>
/// <param name="tagName">The expected name of a <see cref="TagNode"/>.</param>
public TagEventArgs (string tagName)
: base()
{
_tagName = tagName;
}
/// <summary>
/// Constructs a new event argument set.
/// </summary>
/// <param name="tagName">The expected name of a <see cref="TagNode"/>.</param>
/// <param name="tag">The <see cref="TagNode"/> involved in this event.</param>
public TagEventArgs (string tagName, TagNode tag)
: base()
{
_tag = tag;
_tagName = tagName;
}
/// <summary>
/// Constructs a new event argument set.
/// </summary>
/// <param name="schema">The <see cref="SchemaNode"/> corresponding to the <see cref="TagNode"/> involved in this event.</param>
/// <param name="tag">The <see cref="TagNode"/> involved in this event.</param>
public TagEventArgs (SchemaNode schema, TagNode tag)
: base()
{
_tag = tag;
_schema = schema;
}
}
/// <summary>
/// An event handler for intercepting and responding to verification failures of NBT trees.
/// </summary>
/// <param name="eventArgs">Information relating to a verification event.</param>
/// <returns>A <see cref="TagEventCode"/> determining how the event processor should respond.</returns>
public delegate TagEventCode VerifierEventHandler (TagEventArgs eventArgs);
/// <summary>
/// Verifies the integrity of an NBT tree against a schema definition.
/// </summary>
public class NBTVerifier
{
private TagNode _root;
private SchemaNode _schema;
/// <summary>
/// An event that gets fired whenever an expected <see cref="TagNode"/> is not found.
/// </summary>
public static event VerifierEventHandler MissingTag;
/// <summary>
/// An event that gets fired whenever an expected <see cref="TagNode"/> is of the wrong type and cannot be cast.
/// </summary>
public static event VerifierEventHandler InvalidTagType;
/// <summary>
/// An event that gets fired whenever an expected <see cref="TagNode"/> has a value that violates the schema.
/// </summary>
public static event VerifierEventHandler InvalidTagValue;
private Dictionary<string, TagNode> _scratch = new Dictionary<string,TagNode>();
/// <summary>
/// Constructs a new <see cref="NBTVerifier"/> object for a given NBT tree and schema.
/// </summary>
/// <param name="root">A <see cref="TagNode"/> representing the root of an NBT tree.</param>
/// <param name="schema">A <see cref="SchemaNode"/> representing the root of a schema definition for the NBT tree.</param>
public NBTVerifier (TagNode root, SchemaNode schema)
{
_root = root;
_schema = schema;
}
/// <summary>
/// Invokes the verifier.
/// </summary>
/// <returns>Status indicating whether the NBT tree is valid for the given schema.</returns>
public virtual bool Verify ()
{
return Verify(null, _root, _schema);
}
private bool Verify (TagNode parent, TagNode tag, SchemaNode schema)
{
if (tag == null) {
return OnMissingTag(new TagEventArgs(schema.Name));
}
SchemaNodeScaler scaler = schema as SchemaNodeScaler;
if (scaler != null) {
return VerifyScaler(tag, scaler);
}
SchemaNodeString str = schema as SchemaNodeString;
if (str != null) {
return VerifyString(tag, str);
}
SchemaNodeArray array = schema as SchemaNodeArray;
if (array != null) {
return VerifyArray(tag, array);
}
SchemaNodeList list = schema as SchemaNodeList;
if (list != null) {
return VerifyList(tag, list);
}
SchemaNodeCompound compound = schema as SchemaNodeCompound;
if (compound != null) {
return VerifyCompound(tag, compound);
}
return OnInvalidTagType(new TagEventArgs(schema.Name, tag));
}
private bool VerifyScaler (TagNode tag, SchemaNodeScaler schema)
{
if (!tag.IsCastableTo(schema.Type)) {
if (!OnInvalidTagType(new TagEventArgs(schema.Name, tag))) {
return false;
}
}
return true;
}
private bool VerifyString (TagNode tag, SchemaNodeString schema)
{
TagNodeString stag = tag as TagNodeString;
if (stag == null) {
if (!OnInvalidTagType(new TagEventArgs(schema, tag))) {
return false;
}
}
if (schema.Length > 0 && stag.Length > schema.Length) {
if (!OnInvalidTagValue(new TagEventArgs(schema, tag))) {
return false;
}
}
if (schema.Value != null && stag.Data != schema.Value) {
if (!OnInvalidTagValue(new TagEventArgs(schema, tag))) {
return false;
}
}
return true;
}
private bool VerifyArray (TagNode tag, SchemaNodeArray schema)
{
TagNodeByteArray atag = tag as TagNodeByteArray;
if (atag == null) {
if (!OnInvalidTagType(new TagEventArgs(schema, tag))) {
return false;
}
}
if (schema.Length > 0 && atag.Length != schema.Length) {
if (!OnInvalidTagValue(new TagEventArgs(schema, tag))) {
return false;
}
}
return true;
}
private bool VerifyList (TagNode tag, SchemaNodeList schema)
{
TagNodeList ltag = tag as TagNodeList;
if (ltag == null) {
if (!OnInvalidTagType(new TagEventArgs(schema, tag))) {
return false;
}
}
if (ltag.Count > 0 && ltag.ValueType != schema.Type) {
if (!OnInvalidTagValue(new TagEventArgs(schema, tag))) {
return false;
}
}
if (schema.Length > 0 && ltag.Count != schema.Length) {
if (!OnInvalidTagValue(new TagEventArgs(schema, tag))) {
return false;
}
}
// Patch up empty lists
//if (schema.Length == 0) {
// tag = new NBT_List(schema.Type);
//}
bool pass = true;
// If a subschema is set, test all items in list against it
if (schema.SubSchema != null) {
foreach (TagNode v in ltag) {
pass = Verify(tag, v, schema.SubSchema) && pass;
}
}
return pass;
}
private bool VerifyCompound (TagNode tag, SchemaNodeCompound schema)
{
TagNodeCompound ctag = tag as TagNodeCompound;
if (ctag == null) {
if (!OnInvalidTagType(new TagEventArgs(schema, tag))) {
return false;
}
}
bool pass = true;
foreach (SchemaNode node in schema) {
TagNode value;
ctag.TryGetValue(node.Name, out value);
if (value == null) {
if ((node.Options & SchemaOptions.CREATE_ON_MISSING) == SchemaOptions.CREATE_ON_MISSING) {
_scratch[node.Name] = node.BuildDefaultTree();
continue;
}
else if ((node.Options & SchemaOptions.OPTIONAL) == SchemaOptions.OPTIONAL) {
continue;
}
}
pass = Verify(tag, value, node) && pass;
}
foreach (KeyValuePair<string, TagNode> item in _scratch) {
ctag[item.Key] = item.Value;
}
_scratch.Clear();
return pass;
}
#region Event Handlers
/// <summary>
/// Processes registered events for <see cref="MissingTag"/> whenever an expected <see cref="TagNode"/> is not found.
/// </summary>
/// <param name="e">Arguments for this event.</param>
/// <returns>Status indicating whether this event can be ignored.</returns>
protected virtual bool OnMissingTag (TagEventArgs e)
{
if (MissingTag != null) {
foreach (VerifierEventHandler func in MissingTag.GetInvocationList()) {
TagEventCode code = func(e);
switch (code) {
case TagEventCode.FAIL:
return false;
case TagEventCode.PASS:
return true;
}
}
}
return false;
}
/// <summary>
/// Processes registered events for <see cref="InvalidTagType"/> whenever an expected <see cref="TagNode"/> is of the wrong type and cannot be cast.
/// </summary>
/// <param name="e">Arguments for this event.</param>
/// <returns>Status indicating whether this event can be ignored.</returns>
protected virtual bool OnInvalidTagType (TagEventArgs e)
{
if (InvalidTagType != null) {
foreach (VerifierEventHandler func in InvalidTagType.GetInvocationList()) {
TagEventCode code = func(e);
switch (code) {
case TagEventCode.FAIL:
return false;
case TagEventCode.PASS:
return true;
}
}
}
return false;
}
/// <summary>
/// Processes registered events for <see cref="InvalidTagValue"/> whenever an expected <see cref="TagNode"/> has a value that violates the schema.
/// </summary>
/// <param name="e">Arguments for this event.</param>
/// <returns>Status indicating whether this event can be ignored.</returns>
protected virtual bool OnInvalidTagValue (TagEventArgs e)
{
if (InvalidTagValue != null) {
foreach (VerifierEventHandler func in InvalidTagValue.GetInvocationList()) {
TagEventCode code = func(e);
switch (code) {
case TagEventCode.FAIL:
return false;
case TagEventCode.PASS:
return true;
}
}
}
return false;
}
#endregion
}
}

View file

@ -1,60 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Substrate.NBT
{
/// <summary>
/// A node in an NBT schema definition, used to define what values are considered valid for a given NBT node.
/// </summary>
public abstract class SchemaNode
{
private string _name;
private SchemaOptions _options;
/// <summary>
/// Gets the name of an expected NBT node.
/// </summary>
public string Name
{
get { return _name; }
}
/// <summary>
/// Gets additional schema options defined for this node.
/// </summary>
public SchemaOptions Options
{
get { return _options; }
}
/// <summary>
/// Constructs a new <see cref="SchemaNode"/> representing a <see cref="TagNode"/> named <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the corresponding <see cref="TagNode"/>.</param>
protected SchemaNode (string name)
{
_name = name;
}
/// <summary>
/// Constructs a new <see cref="SchemaNode"/> with additional options.
/// </summary>
/// <param name="name">The name of the corresponding <see cref="TagNode"/>.</param>
/// <param name="options">One or more option flags modifying the processing of this node.</param>
protected SchemaNode (string name, SchemaOptions options)
{
_name = name;
_options = options;
}
/// <summary>
/// Construct a sensible default NBT tree representative of this schema node.
/// </summary>
/// <returns>A <see cref="TagNode"/> that is valid for this schema node.</returns>
public virtual TagNode BuildDefaultTree ()
{
return null;
}
}
}

View file

@ -1,81 +0,0 @@
using System;
namespace Substrate.NBT
{
/// <summary>
/// A concrete <see cref="SchemaNode"/> representing a <see cref="TagNodeByteArray"/>.
/// </summary>
public sealed class SchemaNodeArray : SchemaNode
{
private int _length;
/// <summary>
/// Gets the expected length of the corresponding byte array.
/// </summary>
public int Length
{
get { return _length; }
}
/// <summary>
/// Indicates whether there is an expected length of the corresponding byte array.
/// </summary>
public bool HasExpectedLength
{
get { return _length > 0; }
}
/// <summary>
/// Constructs a new <see cref="SchemaNodeArray"/> representing a <see cref="TagNodeByteArray"/> named <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the corresponding <see cref="TagNodeByteArray"/>.</param>
public SchemaNodeArray (string name)
: base(name)
{
_length = 0;
}
/// <summary>
/// Constructs a new <see cref="SchemaNodeArray"/> with additional options.
/// </summary>
/// <param name="name">The name of the corresponding <see cref="TagNodeByteArray"/>.</param>
/// <param name="options">One or more option flags modifying the processing of this node.</param>
public SchemaNodeArray (string name, SchemaOptions options)
: base(name, options)
{
_length = 0;
}
/// <summary>
/// Constructs a new <see cref="SchemaNodeArray"/> representing a <see cref="TagNodeByteArray"/> named <paramref name="name"/> with expected length <paramref name="length"/>.
/// </summary>
/// <param name="name">The name of the corresponding <see cref="TagNodeByteArray"/>.</param>
/// <param name="length">The expected length of corresponding byte array.</param>
public SchemaNodeArray (string name, int length)
: base(name)
{
_length = length;
}
/// <summary>
/// Constructs a new <see cref="SchemaNodeArray"/> with additional options.
/// </summary>
/// <param name="name">The name of the corresponding <see cref="TagNodeByteArray"/>.</param>
/// <param name="length">The expected length of corresponding byte array.</param>
/// <param name="options">One or more option flags modifying the processing of this node.</param>
public SchemaNodeArray (string name, int length, SchemaOptions options)
: base(name, options)
{
_length = length;
}
/// <summary>
/// Constructs a default <see cref="TagNodeByteArray"/> satisfying the constraints of this node.
/// </summary>
/// <returns>A <see cref="TagNodeString"/> with a sensible default value.</returns>
public override TagNode BuildDefaultTree ()
{
return new TagNodeByteArray(new byte[_length]);
}
}
}

View file

@ -1,219 +0,0 @@
using System;
using System.Collections.Generic;
namespace Substrate.NBT
{
/// <summary>
/// A concrete <see cref="SchemaNode"/> representing a <see cref="TagNodeCompound"/>.
/// </summary>
public sealed class SchemaNodeCompound : SchemaNode, ICollection<SchemaNode>
{
private List<SchemaNode> _subnodes;
#region ICollection<NBTSchemaNode> Members
/// <summary>
/// Adds a <see cref="SchemaNode"/> as a child of this node.
/// </summary>
/// <param name="item">The <see cref="SchemaNode"/> to add.</param>
public void Add (SchemaNode item)
{
_subnodes.Add(item);
}
/// <summary>
/// Removes all <see cref="SchemaNode"/> objects from the node.
/// </summary>
public void Clear ()
{
_subnodes.Clear();
}
/// <summary>
/// Checks if a <see cref="SchemaNode"/> is a child of this node.
/// </summary>
/// <param name="item">The <see cref="SchemaNode"/> to check for existance.</param>
/// <returns>Status indicating if the <see cref="SchemaNode"/> exists as a child of this node.</returns>
public bool Contains (SchemaNode item)
{
return _subnodes.Contains(item);
}
/// <summary>
/// Copies all child <see cref="SchemaNode"/> objects of this node to a compatible one-dimensional array, starting at the specified index of the target array.
/// </summary>
/// <param name="array">The one-dimensional <see cref="Array"/> that is the destination of the subnodes copied. The Array must have zero-based indexing.</param>
/// <param name="arrayIndex">The zero-based index in <paramref name="array"/> at which copying begins.</param>
public void CopyTo (SchemaNode[] array, int arrayIndex)
{
_subnodes.CopyTo(array, arrayIndex);
}
/// <summary>
/// Gets the number of child <see cref="SchemaNode"/> objects in this node.
/// </summary>
public int Count
{
get { return _subnodes.Count; }
}
/// <summary>
/// Gets a value indicating whether the node is readonly.
/// </summary>
public bool IsReadOnly
{
get { return false; }
}
/// <summary>
/// Removes the first occurance of a <see cref="SchemaNode"/> from this node.
/// </summary>
/// <param name="item">The <see cref="SchemaNode"/> to remove.</param>
/// <returns>Status indicating whether a <see cref="SchemaNode"/> was removed.</returns>
public bool Remove (SchemaNode item)
{
return _subnodes.Remove(item);
}
#endregion
#region IEnumerable<SchemaNode> Members
/// <summary>
/// Iterates through all of the <see cref="SchemaNode"/> objects in this <see cref="SchemaNodeCompound"/>.
/// </summary>
/// <returns>An enumerator for this node.</returns>
public IEnumerator<SchemaNode> GetEnumerator ()
{
return _subnodes.GetEnumerator();
}
#endregion
#region IEnumerable Members
/// <summary>
/// Iterates through all of the <see cref="SchemaNode"/> objects in this <see cref="SchemaNodeCompound"/>.
/// </summary>
/// <returns>An enumerator for this node.</returns>
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator ()
{
return _subnodes.GetEnumerator();
}
#endregion
/// <summary>
/// Constructs a new <see cref="SchemaNodeCompound"/> representing a root <see cref="TagNodeCompound"/>.
/// </summary>
public SchemaNodeCompound ()
: base("")
{
_subnodes = new List<SchemaNode>();
}
/// <summary>
/// Constructs a new <see cref="SchemaNodeCompound"/> with additional options.
/// </summary>
/// <param name="options">One or more option flags modifying the processing of this node.</param>
public SchemaNodeCompound (SchemaOptions options)
: base("", options)
{
_subnodes = new List<SchemaNode>();
}
/// <summary>
/// Constructs a new <see cref="SchemaNodeCompound"/> representing a <see cref="TagNodeCompound"/> named <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the corresponding <see cref="TagNodeCompound"/>.</param>
public SchemaNodeCompound (string name)
: base(name)
{
_subnodes = new List<SchemaNode>();
}
/// <summary>
/// Constructs a new <see cref="SchemaNodeCompound"/> with additional options.
/// </summary>
/// <param name="name">The name of the corresponding <see cref="TagNodeCompound"/>.</param>
/// <param name="options">One or more option flags modifying the processing of this node.</param>
public SchemaNodeCompound (string name, SchemaOptions options)
: base(name, options)
{
_subnodes = new List<SchemaNode>();
}
/// <summary>
/// Constructs a new <see cref="SchemaNodeCompound"/> representing a <see cref="TagNodeCompound"/> named <paramref name="name"/> matching the given schema.
/// </summary>
/// <param name="name">The name of the corresponding <see cref="TagNodeCompound"/>.</param>
/// <param name="subschema">A <see cref="SchemaNodeCompound"/> representing a schema to verify against the corresponding <see cref="TagNodeCompound"/>.</param>
public SchemaNodeCompound (string name, SchemaNode subschema)
: base(name)
{
_subnodes = new List<SchemaNode>();
SchemaNodeCompound schema = subschema as SchemaNodeCompound;
if (schema == null) {
return;
}
foreach (SchemaNode node in schema._subnodes) {
_subnodes.Add(node);
}
}
/// <summary>
/// Constructs a new <see cref="SchemaNodeCompound"/> with additional options.
/// </summary>
/// <param name="name">The name of the corresponding <see cref="TagNodeCompound"/>.</param>
/// <param name="subschema">A <see cref="SchemaNodeCompound"/> representing a schema to verify against the corresponding <see cref="TagNodeCompound"/>.</param>
/// <param name="options">One or more option flags modifying the processing of this node.</param>
public SchemaNodeCompound (string name, SchemaNode subschema, SchemaOptions options)
: base(name, options)
{
_subnodes = new List<SchemaNode>();
SchemaNodeCompound schema = subschema as SchemaNodeCompound;
if (schema == null) {
return;
}
foreach (SchemaNode node in schema._subnodes) {
_subnodes.Add(node);
}
}
/// <summary>
/// Copies all the elements of this <see cref="SchemaNodeCompound"/> into <paramref name="tree"/>.
/// </summary>
/// <param name="tree">The destination <see cref="SchemaNodeCompound"/> to copy <see cref="SchemaNode"/> elements into.</param>
/// <returns>A reference to <paramref name="tree"/>.</returns>
public SchemaNodeCompound MergeInto (SchemaNodeCompound tree)
{
foreach (SchemaNode node in _subnodes) {
SchemaNode f = tree._subnodes.Find(n => n.Name == node.Name);
if (f != null) {
continue;
}
tree.Add(node);
}
return tree;
}
/// <summary>
/// Constructs a default <see cref="TagNodeCompound"/> satisfying the constraints of this node.
/// </summary>
/// <returns>A <see cref="TagNodeCompound"/> with a sensible default value. A default child <see cref="TagNode"/> is created for every <see cref="SchemaNode"/> contained in this <see cref="SchemaNodeCompound"/>.</returns>
public override TagNode BuildDefaultTree ()
{
TagNodeCompound list = new TagNodeCompound();
foreach (SchemaNode node in _subnodes) {
list[node.Name] = node.BuildDefaultTree();
}
return list;
}
}
}

View file

@ -1,172 +0,0 @@
using System;
namespace Substrate.NBT
{
/// <summary>
/// A concrete <see cref="SchemaNode"/> representing a <see cref="TagNodeList"/>.
/// </summary>
public sealed class SchemaNodeList : SchemaNode
{
private TagType _type;
private int _length;
private SchemaNode _subschema;
/// <summary>
/// Gets the expected number of items contained in the corresponding <see cref="TagNodeList"/>.
/// </summary>
public int Length
{
get { return _length; }
}
/// <summary>
/// Gets the expected <see cref="TagType"/> of the items contained in the corresponding <see cref="TagNodeList"/>.
/// </summary>
public TagType Type
{
get { return _type; }
}
/// <summary>
/// Gets a <see cref="SchemaNode"/> representing a schema that items contained in the corresponding <see cref="TagNodeList"/> should be verified against.
/// </summary>
public SchemaNode SubSchema
{
get { return _subschema; }
}
/// <summary>
/// Indicates whether there is an expected number of items of the corresponding <see cref="TagNodeList"/>.
/// </summary>
public bool HasExpectedLength
{
get { return _length > 0; }
}
/// <summary>
/// Constructs a new <see cref="SchemaNodeList"/> representing a <see cref="TagNodeList"/> named <paramref name="name"/> containing items of type <paramref name="type"/>.
/// </summary>
/// <param name="name">The name of the corresponding <see cref="TagNodeList"/>.</param>
/// <param name="type">The type of items contained in the corresponding <see cref="TagNodeList"/>.</param>
public SchemaNodeList (string name, TagType type)
: base(name)
{
_type = type;
}
/// <summary>
/// Constructs a new <see cref="SchemaNodeList"/> with additional options.
/// </summary>
/// <param name="name">The name of the corresponding <see cref="TagNodeList"/>.</param>
/// <param name="type">The type of items contained in the corresponding <see cref="TagNodeList"/>.</param>
/// <param name="options">One or more option flags modifying the processing of this node.</param>
public SchemaNodeList (string name, TagType type, SchemaOptions options)
: base(name, options)
{
_type = type;
}
/// <summary>
/// Constructs a new <see cref="SchemaNodeList"/> representing a <see cref="TagNodeList"/> named <paramref name="name"/> containing <paramref name="length"/> items of type <paramref name="type"/>.
/// </summary>
/// <param name="name">The name of the corresponding <see cref="TagNodeList"/>.</param>
/// <param name="type">The type of items contained in the corresponding <see cref="TagNodeList"/>.</param>
/// <param name="length">The number of items contained in the corresponding <see cref="TagNodeList"/>.</param>
public SchemaNodeList (string name, TagType type, int length)
: base(name)
{
_type = type;
_length = length;
}
/// <summary>
/// Constructs a new <see cref="SchemaNodeList"/> with additional options.
/// </summary>
/// <param name="name">The name of the corresponding <see cref="TagNodeList"/>.</param>
/// <param name="type">The type of items contained in the corresponding <see cref="TagNodeList"/>.</param>
/// <param name="length">The number of items contained in the corresponding <see cref="TagNodeList"/>.</param>
/// <param name="options">One or more option flags modifying the processing of this node.</param>
public SchemaNodeList (string name, TagType type, int length, SchemaOptions options)
: base(name, options)
{
_type = type;
_length = length;
}
/// <summary>
/// Constructs a new <see cref="SchemaNodeList"/> representing a <see cref="TagNodeList"/> named <paramref name="name"/> containing items of type <paramref name="type"/> matching the given schema.
/// </summary>
/// <param name="name">The name of the corresponding <see cref="TagNodeList"/>.</param>
/// <param name="type">The type of items contained in the corresponding <see cref="TagNodeList"/>.</param>
/// <param name="subschema">A <see cref="SchemaNode"/> representing a schema to verify against items contained in the corresponding <see cref="TagNodeList"/>.</param>
public SchemaNodeList (string name, TagType type, SchemaNode subschema)
: base(name)
{
_type = type;
_subschema = subschema;
}
/// <summary>
/// Constructs a new <see cref="SchemaNodeList"/> with additional options.
/// </summary>
/// <param name="name">The name of the corresponding <see cref="TagNodeList"/>.</param>
/// <param name="type">The type of items contained in the corresponding <see cref="TagNodeList"/>.</param>
/// <param name="subschema">A <see cref="SchemaNode"/> representing a schema to verify against items contained in the corresponding <see cref="TagNodeList"/>.</param>
/// <param name="options">One or more option flags modifying the processing of this node.</param>
public SchemaNodeList (string name, TagType type, SchemaNode subschema, SchemaOptions options)
: base(name, options)
{
_type = type;
_subschema = subschema;
}
/// <summary>
/// Constructs a new <see cref="SchemaNodeList"/> representing a <see cref="TagNodeList"/> named <paramref name="name"/> containing <paramref name="length"/> items of type <paramref name="type"/> matching the given schema.
/// </summary>
/// <param name="name">The name of the corresponding <see cref="TagNodeList"/>.</param>
/// <param name="type">The type of items contained in the corresponding <see cref="TagNodeList"/>.</param>
/// <param name="length">The number of items contained in the corresponding <see cref="TagNodeList"/>.</param>
/// <param name="subschema">A <see cref="SchemaNode"/> representing a schema to verify against items contained in the corresponding <see cref="TagNodeList"/>.</param>
public SchemaNodeList (string name, TagType type, int length, SchemaNode subschema)
: base(name)
{
_type = type;
_length = length;
_subschema = subschema;
}
/// <summary>
/// Constructs a new <see cref="SchemaNodeList"/> with additional options.
/// </summary>
/// <param name="name">The name of the corresponding <see cref="TagNodeList"/>.</param>
/// <param name="type">The type of items contained in the corresponding <see cref="TagNodeList"/>.</param>
/// <param name="length">The number of items contained in the corresponding <see cref="TagNodeList"/>.</param>
/// <param name="subschema">A <see cref="SchemaNode"/> representing a schema to verify against items contained in the corresponding <see cref="TagNodeList"/>.</param>
/// <param name="options">One or more option flags modifying the processing of this node.</param>
public SchemaNodeList (string name, TagType type, int length, SchemaNode subschema, SchemaOptions options)
: base(name, options)
{
_type = type;
_length = length;
_subschema = subschema;
}
/// <summary>
/// Constructs a default <see cref="TagNodeList"/> satisfying the constraints of this node.
/// </summary>
/// <returns>A <see cref="TagNodeList"/> with a sensible default value. If a length is specified, default child <see cref="TagNode"/> objects of the necessary type will be created and added to the <see cref="TagNodeList"/>.</returns>
public override TagNode BuildDefaultTree ()
{
if (_length == 0) {
return new TagNodeList(_type);
}
TagNodeList list = new TagNodeList(_type);
for (int i = 0; i < _length; i++) {
list.Add(_subschema.BuildDefaultTree());
}
return list;
}
}
}

View file

@ -1,75 +0,0 @@
using System;
namespace Substrate.NBT
{
/// <summary>
/// A concrete <see cref="SchemaNode"/> representing a scaler-type <see cref="TagNode"/>.
/// </summary>
public sealed class SchemaNodeScaler : SchemaNode
{
private TagType _type;
/// <summary>
/// Gets the scaler <see cref="TagType"/> that this node represents.
/// </summary>
public TagType Type
{
get { return _type; }
}
/// <summary>
/// Constructs a new <see cref="SchemaNodeScaler"/> representing a <see cref="TagNode"/> named <paramref name="name"/> and of type <paramref name="type"/>.
/// </summary>
/// <param name="name">The name of the corresponding <see cref="TagNode"/>.</param>
/// <param name="type">The type of the corresponding <see cref="TagNode"/>, restricted to scaler types.</param>
public SchemaNodeScaler (string name, TagType type)
: base(name)
{
_type = type;
}
/// <summary>
/// Constructs a new <see cref="SchemaNodeScaler"/> with additional options.
/// </summary>
/// <param name="name">The name of the corresponding <see cref="TagNode"/>.</param>
/// <param name="type">The type of the corresponding <see cref="TagNode"/>, restricted to scaler types.</param>
/// <param name="options">One or more option flags modifying the processing of this node.</param>
public SchemaNodeScaler (string name, TagType type, SchemaOptions options)
: base(name, options)
{
_type = type;
}
/// <summary>
/// Constructs a default <see cref="TagNode"/> according to the <see cref="TagType"/> this node represents.
/// </summary>
/// <returns>A <see cref="TagNode"/> with a sensible default value.</returns>
public override TagNode BuildDefaultTree ()
{
switch (_type) {
case TagType.TAG_STRING:
return new TagNodeString();
case TagType.TAG_BYTE:
return new TagNodeByte();
case TagType.TAG_SHORT:
return new TagNodeShort();
case TagType.TAG_INT:
return new TagNodeInt();
case TagType.TAG_LONG:
return new TagNodeLong();
case TagType.TAG_FLOAT:
return new TagNodeFloat();
case TagType.TAG_DOUBLE:
return new TagNodeDouble();
}
return null;
}
}
}

View file

@ -1,116 +0,0 @@
using System;
namespace Substrate.NBT
{
/// <summary>
/// A concrete <see cref="SchemaNode"/> representing a <see cref="TagNodeString"/>.
/// </summary>
public sealed class SchemaNodeString : SchemaNode
{
private string _value = "";
private int _length;
/// <summary>
/// Gets the maximum length of a valid string.
/// </summary>
public int Length
{
get { return _length; }
}
/// <summary>
/// Gets the expected value of a valid string.
/// </summary>
/// <remarks>A <see cref="TagNodeString"/> must be set to this value to be considered valid.</remarks>
public string Value
{
get { return _value; }
}
/// <summary>
/// Indicates whether there is a maximum-length constraint on strings in this node.
/// </summary>
public bool HasMaxLength
{
get { return _length > 0; }
}
/// <summary>
/// Constructs a new <see cref="SchemaNodeString"/> representing a <see cref="TagNodeString"/> named <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the corresponding <see cref="TagNodeString"/>.</param>
public SchemaNodeString (string name)
: base(name)
{
}
/// <summary>
/// Constructs a new <see cref="SchemaNodeString"/> with additional options.
/// </summary>
/// <param name="name">The name of the corresponding <see cref="TagNodeString"/>.</param>
/// <param name="options">One or more option flags modifying the processing of this node.</param>
public SchemaNodeString (string name, SchemaOptions options)
: base(name, options)
{
}
/// <summary>
/// Constructs a new <see cref="SchemaNodeString"/> representing a <see cref="TagNodeString"/> named <paramref name="name"/> set to <paramref name="value"/>.
/// </summary>
/// <param name="name">The name of the corresponding <see cref="TagNodeString"/>.</param>
/// <param name="value">The value that the corresponding <see cref="TagNodeString"/> must be set to.</param>
public SchemaNodeString (string name, string value)
: base(name)
{
_value = value;
}
/// <summary>
/// Constructs a new <see cref="SchemaNodeString"/> with additional options.
/// </summary>
/// <param name="name">The name of the corresponding <see cref="TagNodeString"/>.</param>
/// <param name="value">The value that the corresponding <see cref="TagNodeString"/> must be set to.</param>
/// <param name="options">One or more option flags modifying the processing of this node.</param>
public SchemaNodeString (string name, string value, SchemaOptions options)
: base(name, options)
{
_value = value;
}
/// <summary>
/// Constructs a new <see cref="SchemaNodeString"/> representing a <see cref="TagNodeString"/> named <paramref name="name"/> with maximum length <paramref name="length"/>.
/// </summary>
/// <param name="name">The name of the corresponding <see cref="TagNodeString"/>.</param>
/// <param name="length">The maximum length of strings in the corresponding <see cref="TagNodeString"/>.</param>
public SchemaNodeString (string name, int length)
: base(name)
{
_length = length;
}
/// <summary>
/// Constructs a new <see cref="SchemaNodeString"/> with additional options.
/// </summary>
/// <param name="name">The name of the corresponding <see cref="TagNodeString"/>.</param>
/// <param name="length">The maximum length of strings in the corresponding <see cref="TagNodeString"/>.</param>
/// <param name="options">One or more option flags modifying the processing of this node.</param>
public SchemaNodeString (string name, int length, SchemaOptions options)
: base(name, options)
{
_length = length;
}
/// <summary>
/// Constructs a default <see cref="TagNodeString"/> satisfying the constraints of this node.
/// </summary>
/// <returns>A <see cref="TagNodeString"/> with a sensible default value. If this node represents a particular string, the <see cref="TagNodeString"/> constructed will be set to that string.</returns>
public override TagNode BuildDefaultTree ()
{
if (_value.Length > 0) {
return new TagNodeString(_value);
}
return new TagNodeString();
}
}
}

View file

@ -1,21 +0,0 @@
using System;
namespace Substrate.NBT
{
/// <summary>
/// Additional options that modify the processing of a <see cref="SchemaNode"/>.
/// </summary>
[Flags]
public enum SchemaOptions
{
/// <summary>
/// Any <see cref="SchemaNode"/> with this option will not throw an error if the corresponding <see cref="TagNode"/> is missing.
/// </summary>
OPTIONAL = 0x1,
/// <summary>
/// If a <see cref="TagNode"/> cannot be found for a <see cref="SchemaNode"/> marked with this option, a sensible default <see cref="TagNode"/> will be created and inserted into the tree.
/// </summary>
CREATE_ON_MISSING = 0x2,
}
}

View file

@ -1,140 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
using Substrate.Core;
namespace Substrate.NBT
{
/// <summary>
/// An abstract base class representing a node in an NBT tree.
/// </summary>
public abstract class TagNode : ICopyable<TagNode>
{
/// <summary>
/// Convert this node to a null tag type if supported.
/// </summary>
/// <returns>A new null node.</returns>
public virtual TagNodeNull ToTagNull ()
{
throw new InvalidCastException();
}
/// <summary>
/// Convert this node to a byte tag type if supported.
/// </summary>
/// <returns>A new byte node.</returns>
public virtual TagNodeByte ToTagByte ()
{
throw new InvalidCastException();
}
/// <summary>
/// Convert this node to a short tag type if supported.
/// </summary>
/// <returns>A new short node.</returns>
public virtual TagNodeShort ToTagShort ()
{
throw new InvalidCastException();
}
/// <summary>
/// Convert this node to an int tag type if supported.
/// </summary>
/// <returns>A new int node.</returns>
public virtual TagNodeInt ToTagInt ()
{
throw new InvalidCastException();
}
/// <summary>
/// Convert this node to a long tag type if supported.
/// </summary>
/// <returns>A new long node.</returns>
public virtual TagNodeLong ToTagLong ()
{
throw new InvalidCastException();
}
/// <summary>
/// Convert this node to a float tag type if supported.
/// </summary>
/// <returns>A new float node.</returns>
public virtual TagNodeFloat ToTagFloat ()
{
throw new InvalidCastException();
}
/// <summary>
/// Convert this node to a double tag type if supported.
/// </summary>
/// <returns>A new double node.</returns>
public virtual TagNodeDouble ToTagDouble ()
{
throw new InvalidCastException();
}
/// <summary>
/// Convert this node to a byte array tag type if supported.
/// </summary>
/// <returns>A new byte array node.</returns>
public virtual TagNodeByteArray ToTagByteArray ()
{
throw new InvalidCastException();
}
/// <summary>
/// Convert this node to a string tag type if supported.
/// </summary>
/// <returns>A new string node.</returns>
public virtual TagNodeString ToTagString ()
{
throw new InvalidCastException();
}
/// <summary>
/// Convert this node to a list tag type if supported.
/// </summary>
/// <returns>A new list node.</returns>
public virtual TagNodeList ToTagList ()
{
throw new InvalidCastException();
}
/// <summary>
/// Convert this node to a compound tag type if supported.
/// </summary>
/// <returns>A new compound node.</returns>
public virtual TagNodeCompound ToTagCompound ()
{
throw new InvalidCastException();
}
/// <summary>
/// Gets the underlying tag type of the node.
/// </summary>
/// <returns>An NBT tag type.</returns>
public virtual TagType GetTagType ()
{
return TagType.TAG_END;
}
/// <summary>
/// Checks if this node is castable to another node of a given tag type.
/// </summary>
/// <param name="type">An NBT tag type.</param>
/// <returns>Status indicating whether this object could be cast to a node type represented by the given tag type.</returns>
public virtual bool IsCastableTo (TagType type)
{
return type == GetTagType();
}
/// <summary>
/// Makes a deep copy of the NBT node.
/// </summary>
/// <returns>A new NBT node.</returns>
public virtual TagNode Copy ()
{
return null;
}
}
}

View file

@ -1,161 +0,0 @@
using System;
namespace Substrate.NBT
{
/// <summary>
/// An NBT node representing a signed byte tag type.
/// </summary>
public sealed class TagNodeByte : TagNode
{
private byte _data = 0;
/// <summary>
/// Converts the node to itself.
/// </summary>
/// <returns>A reference to itself.</returns>
public override TagNodeByte ToTagByte ()
{
return this;
}
/// <summary>
/// Converts the node to a new short node.
/// </summary>
/// <returns>A short node representing the same data.</returns>
public override TagNodeShort ToTagShort ()
{
return new TagNodeShort(_data);
}
/// <summary>
/// Converts the node to a new int node.
/// </summary>
/// <returns>An int node representing the same data.</returns>
public override TagNodeInt ToTagInt ()
{
return new TagNodeInt(_data);
}
/// <summary>
/// Converts the node to a new long node.
/// </summary>
/// <returns>A long node representing the same data.</returns>
public override TagNodeLong ToTagLong ()
{
return new TagNodeLong(_data);
}
/// <summary>
/// Gets the tag type of the node.
/// </summary>
/// <returns>The TAG_BYTE tag type.</returns>
public override TagType GetTagType ()
{
return TagType.TAG_BYTE;
}
/// <summary>
/// Checks if the node is castable to another node of a given tag type.
/// </summary>
/// <param name="type">An NBT tag type.</param>
/// <returns>Status indicating whether this object could be cast to a node type represented by the given tag type.</returns>
public override bool IsCastableTo (TagType type)
{
return (type == TagType.TAG_BYTE ||
type == TagType.TAG_SHORT ||
type == TagType.TAG_INT ||
type == TagType.TAG_LONG);
}
/// <summary>
/// Gets or sets a byte of tag data.
/// </summary>
public byte Data
{
get { return _data; }
set { _data = value; }
}
/// <summary>
/// Constructs a new byte node with a data value of 0.
/// </summary>
public TagNodeByte () { }
/// <summary>
/// Constructs a new byte node.
/// </summary>
/// <param name="d">The value to set the node's tag data value.</param>
public TagNodeByte (byte d)
{
_data = d;
}
/// <summary>
/// Makes a deep copy of the node.
/// </summary>
/// <returns>A new byte node representing the same data.</returns>
public override TagNode Copy ()
{
return new TagNodeByte(_data);
}
/// <summary>
/// Gets a string representation of the node's data.
/// </summary>
/// <returns>String representation of the node's data.</returns>
public override string ToString ()
{
return _data.ToString();
}
/// <summary>
/// Converts a system byte to a byte node representing the same value.
/// </summary>
/// <param name="b">A byte value.</param>
/// <returns>A new byte node containing the given value.</returns>
public static implicit operator TagNodeByte (byte b)
{
return new TagNodeByte(b);
}
/// <summary>
/// Converts a byte node to a system byte representing the same value.
/// </summary>
/// <param name="b">A byte node.</param>
/// <returns>A system byte set to the node's data value.</returns>
public static implicit operator byte (TagNodeByte b)
{
return b._data;
}
/// <summary>
/// Converts a byte node to a system short representing the same value.
/// </summary>
/// <param name="b">A byte node.</param>
/// <returns>A system short set to the node's data value.</returns>
public static implicit operator short (TagNodeByte b)
{
return b._data;
}
/// <summary>
/// Converts a byte node to a system int representing the same value.
/// </summary>
/// <param name="b">A byte node.</param>
/// <returns>A system int set to the node's data value.</returns>
public static implicit operator int (TagNodeByte b)
{
return b._data;
}
/// <summary>
/// Converts a byte node to a system long representing the same value.
/// </summary>
/// <param name="b">A byte node.</param>
/// <returns>A system long set to the node's data value.</returns>
public static implicit operator long (TagNodeByte b)
{
return b._data;
}
}
}

View file

@ -1,113 +0,0 @@
using System;
namespace Substrate.NBT
{
/// <summary>
/// An NBT node representing an unsigned byte array tag type.
/// </summary>
public sealed class TagNodeByteArray : TagNode
{
private byte[] _data = null;
/// <summary>
/// Converts the node to itself.
/// </summary>
/// <returns>A reference to itself.</returns>
public override TagNodeByteArray ToTagByteArray ()
{
return this;
}
/// <summary>
/// Gets the tag type of the node.
/// </summary>
/// <returns>The TAG_BYTE_ARRAY tag type.</returns>
public override TagType GetTagType ()
{
return TagType.TAG_BYTE_ARRAY;
}
/// <summary>
/// Gets or sets a byte array of tag data.
/// </summary>
public byte[] Data
{
get { return _data; }
set { _data = value; }
}
/// <summary>
/// Gets the length of the stored byte array.
/// </summary>
public int Length
{
get { return _data.Length; }
}
/// <summary>
/// Constructs a new byte array node with a null data value.
/// </summary>
public TagNodeByteArray () { }
/// <summary>
/// Constructs a new byte array node.
/// </summary>
/// <param name="d">The value to set the node's tag data value.</param>
public TagNodeByteArray (byte[] d)
{
_data = d;
}
/// <summary>
/// Makes a deep copy of the node.
/// </summary>
/// <returns>A new byte array node representing the same data.</returns>
public override TagNode Copy ()
{
byte[] arr = new byte[_data.Length];
_data.CopyTo(arr, 0);
return new TagNodeByteArray(arr);
}
/// <summary>
/// Gets a string representation of the node's data.
/// </summary>
/// <returns>String representation of the node's data.</returns>
public override string ToString ()
{
return _data.ToString();
}
/// <summary>
/// Gets or sets a single byte at the specified index.
/// </summary>
/// <param name="index">Valid index within stored byte array.</param>
/// <returns>The byte value at the given index of the stored byte array.</returns>
public byte this[int index]
{
get { return _data[index]; }
set { _data[index] = value; }
}
/// <summary>
/// Converts a system byte array to a byte array node representing the same data.
/// </summary>
/// <param name="b">A byte array.</param>
/// <returns>A new byte array node containing the given value.</returns>
public static implicit operator TagNodeByteArray (byte[] b)
{
return new TagNodeByteArray(b);
}
/// <summary>
/// Converts a byte array node to a system byte array representing the same data.
/// </summary>
/// <param name="b">A byte array node.</param>
/// <returns>A system byte array set to the node's data.</returns>
public static implicit operator byte[] (TagNodeByteArray b)
{
return b._data;
}
}
}

View file

@ -1,265 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
namespace Substrate.NBT
{
/// <summary>
/// An NBT node representing a compound tag type containing other nodes.
/// </summary>
public sealed class TagNodeCompound : TagNode, IDictionary<string, TagNode>
{
private Dictionary<string, TagNode> _tags;
/// <summary>
/// Converts the node to itself.
/// </summary>
/// <returns>A reference to itself.</returns>
public override TagNodeCompound ToTagCompound ()
{
return this;
}
/// <summary>
/// Gets the tag type of the node.
/// </summary>
/// <returns>The TAG_STRING tag type.</returns>
public override TagType GetTagType ()
{
return TagType.TAG_COMPOUND;
}
/// <summary>
/// Gets the number of subnodes contained in node.
/// </summary>
public int Count
{
get { return _tags.Count; }
}
/// <summary>
/// Constructs a new empty compound node.
/// </summary>
public TagNodeCompound ()
{
_tags = new Dictionary<string, TagNode>();
}
/// <summary>
/// Makes a deep copy of the node.
/// </summary>
/// <returns>A new compound node containing new subnodes representing the same data.</returns>
public override TagNode Copy ()
{
TagNodeCompound list = new TagNodeCompound();
foreach (KeyValuePair<string, TagNode> item in _tags) {
list[item.Key] = item.Value.Copy();
}
return list;
}
/// <summary>
/// Gets a string representation of the node's data.
/// </summary>
/// <returns>String representation of the node's data.</returns>
public override string ToString ()
{
return _tags.ToString();
}
#region IDictionary<string,NBT_Value> Members
/// <summary>
/// Adds a named subnode to the set.
/// </summary>
/// <param name="key">The name of the subnode.</param>
/// <param name="value">The subnode to add.</param>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
/// <exception cref="ArgumentException">A subnode with the same key already exists in the set.</exception>
public void Add (string key, TagNode value)
{
_tags.Add(key, value);
}
/// <summary>
/// Checks if a subnode exists in the set with the specified name.
/// </summary>
/// <param name="key">The name of a subnode to check.</param>
/// <returns>Status indicating whether a subnode with the specified name exists.</returns>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
public bool ContainsKey (string key)
{
return _tags.ContainsKey(key);
}
/// <summary>
/// Gets a collection containing all the names of subnodes in this set.
/// </summary>
public ICollection<string> Keys
{
get { return _tags.Keys; }
}
/// <summary>
/// Removes a subnode with the specified name.
/// </summary>
/// <param name="key">The name of the subnode to remove.</param>
/// <returns>Status indicating whether a subnode was removed.</returns>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
public bool Remove (string key)
{
return _tags.Remove(key);
}
/// <summary>
/// Gets the subnode associated with the given name.
/// </summary>
/// <param name="key">The name of the subnode to get.</param>
/// <param name="value">When the function returns, contains the subnode assicated with the specified key. If no subnode was found, contains a default value.</param>
/// <returns>Status indicating whether a subnode was found.</returns>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
public bool TryGetValue (string key, out TagNode value)
{
return _tags.TryGetValue(key, out value);
}
/// <summary>
/// Gets a collection containing all the subnodes in this set.
/// </summary>
public ICollection<TagNode> Values
{
get { return _tags.Values; }
}
/// <summary>
/// Gets or sets the subnode with the associated name.
/// </summary>
/// <param name="key">The name of the subnode to get or set.</param>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
/// <exception cref="KeyNotFoundException">The property is retrieved and key does not exist in the collection.</exception>
public TagNode this[string key]
{
get
{
return _tags[key];
}
set
{
_tags[key] = value;
}
}
#endregion
#region ICollection<KeyValuePair<string,NBT_Value>> Members
/// <summary>
/// Adds a subnode to the to the set with the specified name.
/// </summary>
/// <param name="item">The <see cref="KeyValuePair{TKey, TVal}"/> structure representing the key and subnode to add to the set.</param>
/// <exception cref="ArgumentNullException">The key of <paramref name="item"/> is null.</exception>
/// <exception cref="ArgumentException">A subnode with the same key already exists in the set.</exception>
public void Add (KeyValuePair<string, TagNode> item)
{
_tags.Add(item.Key, item.Value);
}
/// <summary>
/// Removes all of the subnodes from this node.
/// </summary>
public void Clear ()
{
_tags.Clear();
}
/// <summary>
/// Checks if a specific subnode with a specific name is contained in the set.
/// </summary>
/// <param name="item">The <see cref="KeyValuePair{TKey, TValue}"/> structure representing the key and subnode to look for.</param>
/// <returns>Status indicating if the subnode and key combination exists in the set.</returns>
public bool Contains (KeyValuePair<string, TagNode> item)
{
TagNode value;
if (!_tags.TryGetValue(item.Key, out value)) {
return false;
}
return value == item.Value;
}
/// <summary>
/// Copies the elements of the <see cref="ICollection{T}"/> to an array of type <see cref="KeyValuePair{TKey, TVal}"/>, starting at the specified array index.
/// </summary>
/// <param name="array">The one-dimensional <see cref="Array"/> that is the destination of the subnodes copied. The Array must have zero-based indexing.</param>
/// <param name="arrayIndex">The zero-based index in <paramref name="array"/> at which copying begins.</param>
/// <exception cref="ArgumentNullException"><paramref name="array"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="arrayIndex"/> is less than 0.</exception>
/// <exception cref="ArgumentException">The number of elements in the source <see cref="ICollection{T}"/> is greater than the available space from <paramref name="arrayIndex"/> to the end of the destination <paramref name="array"/>.</exception>
public void CopyTo (KeyValuePair<string, TagNode>[] array, int arrayIndex)
{
if (array == null) {
throw new ArgumentNullException();
}
if (arrayIndex < 0) {
throw new ArgumentOutOfRangeException();
}
if (array.Length - arrayIndex < _tags.Count) {
throw new ArgumentException();
}
foreach (KeyValuePair<string, TagNode> item in _tags) {
array[arrayIndex] = item;
arrayIndex++;
}
}
/// <summary>
/// Gets a value indicating whether the node is readonly.
/// </summary>
public bool IsReadOnly
{
get { return false; }
}
/// <summary>
/// Removes the specified key and subnode combination from the set.
/// </summary>
/// <param name="item">The <see cref="KeyValuePair{TKey, TVal}"/> structure representing the key and value to remove from the set.</param>
/// <returns>Status indicating whether a subnode was removed.</returns>
public bool Remove (KeyValuePair<string, TagNode> item)
{
if (Contains(item)) {
_tags.Remove(item.Key);
return true;
}
return false;
}
#endregion
#region IEnumerable<KeyValuePair<string,NBT_Value>> Members
/// <summary>
/// Returns an enumerator that iterates through all of the subnodes in the set.
/// </summary>
/// <returns>An enumerator for this node.</returns>
public IEnumerator<KeyValuePair<string, TagNode>> GetEnumerator ()
{
return _tags.GetEnumerator();
}
#endregion
#region IEnumerable Members
/// <summary>
/// Returns an enumerator that iterates through all of the subnodes in the set.
/// </summary>
/// <returns>An enumerator for this node.</returns>
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator ()
{
return _tags.GetEnumerator();
}
#endregion
}
}

View file

@ -1,101 +0,0 @@
using System;
namespace Substrate.NBT
{
/// <summary>
/// An NBT node representing a double-precision floating point tag type.
/// </summary>
public sealed class TagNodeDouble : TagNode
{
private double _data = 0;
/// <summary>
/// Converts the node to itself.
/// </summary>
/// <returns>A reference to itself.</returns>
public override TagNodeDouble ToTagDouble ()
{
return this;
}
/// <summary>
/// Gets the tag type of the node.
/// </summary>
/// <returns>The TAG_DOUBLE tag type.</returns>
public override TagType GetTagType ()
{
return TagType.TAG_DOUBLE;
}
/// <summary>
/// Gets or sets a double of tag data.
/// </summary>
public double Data
{
get { return _data; }
set { _data = value; }
}
/// <summary>
/// Constructs a new double node with a data value of 0.0.
/// </summary>
public TagNodeDouble () { }
/// <summary>
/// Constructs a new double node.
/// </summary>
/// <param name="d">The value to set the node's tag data value.</param>
public TagNodeDouble (double d)
{
_data = d;
}
/// <summary>
/// Makes a deep copy of the node.
/// </summary>
/// <returns>A new double node representing the same data.</returns>
public override TagNode Copy ()
{
return new TagNodeDouble(_data);
}
/// <summary>
/// Gets a string representation of the node's data.
/// </summary>
/// <returns>String representation of the node's data.</returns>
public override string ToString ()
{
return _data.ToString();
}
/// <summary>
/// Converts a system float to a double node representing the same value.
/// </summary>
/// <param name="f">A float value.</param>
/// <returns>A new double node containing the given value.</returns>
public static implicit operator TagNodeDouble (float f)
{
return new TagNodeDouble(f);
}
/// <summary>
/// Converts a system double to a double node representing the same value.
/// </summary>
/// <param name="d">A double value.</param>
/// <returns>A new double node containing the given value.</returns>
public static implicit operator TagNodeDouble (double d)
{
return new TagNodeDouble(d);
}
/// <summary>
/// Converts a double node to a system double representing the same value.
/// </summary>
/// <param name="d">A double node.</param>
/// <returns>A system double set to the node's data value.</returns>
public static implicit operator double (TagNodeDouble d)
{
return d._data;
}
}
}

View file

@ -1,121 +0,0 @@
using System;
namespace Substrate.NBT
{
/// <summary>
/// An NBT node representing a single-precision floating point tag type.
/// </summary>
public sealed class TagNodeFloat : TagNode
{
private float _data = 0;
/// <summary>
/// Converts the node to itself.
/// </summary>
/// <returns>A reference to itself.</returns>
public override TagNodeFloat ToTagFloat ()
{
return this;
}
/// <summary>
/// Converts the node to a new double node.
/// </summary>
/// <returns>A double node representing the same data.</returns>
public override TagNodeDouble ToTagDouble ()
{
return new TagNodeDouble(_data);
}
/// <summary>
/// Gets the tag type of the node.
/// </summary>
/// <returns>The TAG_FLOAT tag type.</returns>
public override TagType GetTagType ()
{
return TagType.TAG_FLOAT;
}
/// <summary>
/// Checks if the node is castable to another node of a given tag type.
/// </summary>
/// <param name="type">An NBT tag type.</param>
/// <returns>Status indicating whether this object could be cast to a node type represented by the given tag type.</returns>
public override bool IsCastableTo (TagType type)
{
return (type == TagType.TAG_FLOAT ||
type == TagType.TAG_DOUBLE);
}
/// <summary>
/// Gets or sets a float of tag data.
/// </summary>
public float Data
{
get { return _data; }
set { _data = value; }
}
/// <summary>
/// Constructs a new float node with a data value of 0.0.
/// </summary>
public TagNodeFloat () { }
/// <summary>
/// Constructs a new float node.
/// </summary>
/// <param name="d">The value to set the node's tag data value.</param>
public TagNodeFloat (float d)
{
_data = d;
}
/// <summary>
/// Makes a deep copy of the node.
/// </summary>
/// <returns>A new float node representing the same data.</returns>
public override TagNode Copy ()
{
return new TagNodeFloat(_data);
}
/// <summary>
/// Gets a string representation of the node's data.
/// </summary>
/// <returns>String representation of the node's data.</returns>
public override string ToString ()
{
return _data.ToString();
}
/// <summary>
/// Converts a system float to a float node representing the same value.
/// </summary>
/// <param name="f">A float value.</param>
/// <returns>A new float node containing the given value.</returns>
public static implicit operator TagNodeFloat (float f)
{
return new TagNodeFloat(f);
}
/// <summary>
/// Converts a float node to a system float representing the same value.
/// </summary>
/// <param name="f">A float node.</param>
/// <returns>A system float set to the node's data value.</returns>
public static implicit operator float (TagNodeFloat f)
{
return f._data;
}
/// <summary>
/// Converts a float node to a system double representing the same value.
/// </summary>
/// <param name="f">A float node.</param>
/// <returns>A system double set to the node's data value.</returns>
public static implicit operator double (TagNodeFloat f)
{
return f._data;
}
}
}

View file

@ -1,141 +0,0 @@
using System;
namespace Substrate.NBT
{
/// <summary>
/// An NBT node representing a signed int tag type.
/// </summary>
public sealed class TagNodeInt : TagNode
{
private int _data = 0;
/// <summary>
/// Converts the node to itself.
/// </summary>
/// <returns>A reference to itself.</returns>
public override TagNodeInt ToTagInt ()
{
return this;
}
/// <summary>
/// Converts the node to a new long node.
/// </summary>
/// <returns>A long node representing the same data.</returns>
public override TagNodeLong ToTagLong ()
{
return new TagNodeLong(_data);
}
/// <summary>
/// Gets the tag type of the node.
/// </summary>
/// <returns>The TAG_INT tag type.</returns>
public override TagType GetTagType ()
{
return TagType.TAG_INT;
}
/// <summary>
/// Checks if the node is castable to another node of a given tag type.
/// </summary>
/// <param name="type">An NBT tag type.</param>
/// <returns>Status indicating whether this object could be cast to a node type represented by the given tag type.</returns>
public override bool IsCastableTo (TagType type)
{
return (type == TagType.TAG_INT ||
type == TagType.TAG_LONG);
}
/// <summary>
/// Gets or sets an int of tag data.
/// </summary>
public int Data
{
get { return _data; }
set { _data = value; }
}
/// <summary>
/// Constructs a new int node with a data value of 0.
/// </summary>
public TagNodeInt () { }
/// <summary>
/// Constructs a new int node.
/// </summary>
/// <param name="d">The value to set the node's tag data value.</param>
public TagNodeInt (int d)
{
_data = d;
}
/// <summary>
/// Makes a deep copy of the node.
/// </summary>
/// <returns>A new int node representing the same data.</returns>
public override TagNode Copy ()
{
return new TagNodeInt(_data);
}
/// <summary>
/// Gets a string representation of the node's data.
/// </summary>
/// <returns>String representation of the node's data.</returns>
public override string ToString ()
{
return _data.ToString();
}
/// <summary>
/// Converts a system byte to an int node representing the same value.
/// </summary>
/// <param name="b">A byte value.</param>
/// <returns>A new int node containing the given value.</returns>
public static implicit operator TagNodeInt (byte b)
{
return new TagNodeInt(b);
}
/// <summary>
/// Converts a system short to an int node representing the same value.
/// </summary>
/// <param name="s">A short value.</param>
/// <returns>A new int node containing the given value.</returns>
public static implicit operator TagNodeInt (short s)
{
return new TagNodeInt(s);
}
/// <summary>
/// Converts a system int to an int node representing the same value.
/// </summary>
/// <param name="i">An int value.</param>
/// <returns>A new int node containing the given value.</returns>
public static implicit operator TagNodeInt (int i)
{
return new TagNodeInt(i);
}
/// <summary>
/// Converts an int node to a system int representing the same value.
/// </summary>
/// <param name="i">An int node.</param>
/// <returns>A system int set to the node's data value.</returns>
public static implicit operator int (TagNodeInt i)
{
return i._data;
}
/// <summary>
/// Converts an int node to a system long representing the same value.
/// </summary>
/// <param name="i">An int node.</param>
/// <returns>A system long set to the node's data value.</returns>
public static implicit operator long (TagNodeInt i)
{
return i._data;
}
}
}

View file

@ -1,264 +0,0 @@
using System;
using System.Collections.Generic;
namespace Substrate.NBT
{
/// <summary>
/// An NBT node representing a list tag type containing other nodes.
/// </summary>
/// <remarks>
/// A list node contains 0 or more nodes of the same type. The nodes are unnamed
/// but can be accessed by sequential index.
/// </remarks>
public sealed class TagNodeList : TagNode, IList<TagNode>
{
private TagType _type = TagType.TAG_END;
private List<TagNode> _items = null;
/// <summary>
/// Converts the node to itself.
/// </summary>
/// <returns>A reference to itself.</returns>
public override TagNodeList ToTagList ()
{
return this;
}
/// <summary>
/// Gets the tag type of the node.
/// </summary>
/// <returns>The TAG_STRING tag type.</returns>
public override TagType GetTagType ()
{
return TagType.TAG_LIST;
}
/// <summary>
/// Gets the number of subnodes contained in the list.
/// </summary>
public int Count
{
get { return _items.Count; }
}
/// <summary>
/// Gets the tag type of the subnodes contained in the list.
/// </summary>
public TagType ValueType
{
get { return _type; }
}
/// <summary>
/// Constructs a new empty list node.
/// </summary>
/// <param name="type">The tag type of the list's subnodes.</param>
public TagNodeList (TagType type)
{
_type = type;
_items = new List<TagNode>();
}
/// <summary>
/// Constructs a new list node from a list of nodes.
/// </summary>
/// <param name="type">The tag type of the list's subnodes.</param>
/// <param name="items">A list containing node objects matching the type parameter.</param>
public TagNodeList (TagType type, List<TagNode> items)
{
_type = type;
_items = items;
}
/// <summary>
/// Makes a deep copy of the node.
/// </summary>
/// <returns>A new list node containing new subnodes representing the same data.</returns>
public override TagNode Copy ()
{
TagNodeList list = new TagNodeList(_type);
foreach (TagNode item in _items) {
list.Add(item.Copy());
}
return list;
}
/// <summary>
/// Retrieves all the subnodes that match the conditions defined by the specified predicate.
/// </summary>
/// <param name="match">The <see cref="Predicate{TagNode}"/> delegate that defines the conditions of the subnode to search for.</param>
/// <returns>A list of all subnodes matching the predicate.</returns>
public List<TagNode> FindAll (Predicate<TagNode> match)
{
return _items.FindAll(match);
}
/// <summary>
/// Removes all subnodes that match the conditions defined by the specified predicate.
/// </summary>
/// <param name="match">The <see cref="Predicate{TagNode}"/> delegate that defines the conditions of the subnode to search for.</param>
/// <returns>The number of subnodes removed from the node.</returns>
public int RemoveAll (Predicate<TagNode> match)
{
return _items.RemoveAll(match);
}
/// <summary>
/// Gets a string representation of the node's data.
/// </summary>
/// <returns>String representation of the node's data.</returns>
public override string ToString ()
{
return _items.ToString();
}
#region IList<NBT_Value> Members
/// <summary>
/// Searches for the specified subnode and returns the zero-based index of the first occurrence within the entire node's list.
/// </summary>
/// <param name="item">The subnode to locate.</param>
/// <returns>The zero-based index of the subnode within the node's list if found, or -1 otherwise.</returns>
public int IndexOf (TagNode item)
{
return _items.IndexOf(item);
}
/// <summary>
/// Inserts a subnode into the node's list at the specified index.
/// </summary>
/// <param name="index">The zero-based index at which the subnode should be inserted.</param>
/// <param name="item">The subnode to insert.</param>
/// <exception cref="ArgumentException">Thrown when a subnode being inserted has the wrong tag type.</exception>
public void Insert (int index, TagNode item)
{
if (item.GetTagType() != _type) {
throw new ArgumentException("The tag type of item is invalid for this node");
}
_items.Insert(index, item);
}
/// <summary>
/// Removes the subnode from the node's list at the specified index.
/// </summary>
/// <param name="index">The zero-based index to remove a subnode at.</param>
public void RemoveAt (int index)
{
_items.RemoveAt(index);
}
/// <summary>
/// Gets or sets the subnode in the node's list at the specified index.
/// </summary>
/// <param name="index">The zero-based index to get or set from.</param>
/// <returns>The subnode at the specified index.</returns>
/// <exception cref="ArgumentException">Thrown when a subnode being assigned has the wrong tag type.</exception>
public TagNode this[int index]
{
get
{
return _items[index];
}
set
{
if (value.GetTagType() != _type) {
throw new ArgumentException("The tag type of the assigned subnode is invalid for this node");
}
_items[index] = value;
}
}
#endregion
#region ICollection<NBT_Value> Members
/// <summary>
/// Adds a subnode to the end of the node's list.
/// </summary>
/// <param name="item">The subnode to add.</param>
/// <exception cref="ArgumentException">Thrown when a subnode being added has the wrong tag type.</exception>
public void Add (TagNode item)
{
if (item.GetTagType() != _type) {
throw new ArgumentException("The tag type of item is invalid for this node");
}
_items.Add(item);
}
/// <summary>
/// Removes all subnode's from the node's list.
/// </summary>
public void Clear ()
{
_items.Clear();
}
/// <summary>
/// Checks if a subnode is contained within the node's list.
/// </summary>
/// <param name="item">The subnode to check for existance.</param>
/// <returns>Status indicating if the subnode exists in the node's list.</returns>
public bool Contains (TagNode item)
{
return _items.Contains(item);
}
/// <summary>
/// Copies the entire node's list to a compatible one-dimensional array, starting at the specified index of the target array.
/// </summary>
/// <param name="array">The one-dimensional <see cref="Array"/> that is the destination of the subnodes copied. The Array must have zero-based indexing.</param>
/// <param name="arrayIndex">The zero-based index in <paramref name="array"/> at which copying begins.</param>
public void CopyTo (TagNode[] array, int arrayIndex)
{
_items.CopyTo(array, arrayIndex);
}
/// <summary>
/// Gets a value indicating whether the node is readonly.
/// </summary>
public bool IsReadOnly
{
get { return false; }
}
/// <summary>
/// Removes the first occurance of a subnode from the node's list.
/// </summary>
/// <param name="item">The subnode to remove.</param>
/// <returns>Status indicating whether a subnode was removed.</returns>
public bool Remove (TagNode item)
{
return _items.Remove(item);
}
#endregion
#region IEnumerable<NBT_Value> Members
/// <summary>
/// Returns an enumerator that iterates through all of the subnodes in the node's list.
/// </summary>
/// <returns>An enumerator for this node.</returns>
public IEnumerator<TagNode> GetEnumerator ()
{
return _items.GetEnumerator();
}
#endregion
#region IEnumerable Members
/// <summary>
/// Returns an enumerator that iterates through all of the subnodes in the node's list.
/// </summary>
/// <returns>An enumerator for this node.</returns>
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator ()
{
return _items.GetEnumerator();
}
#endregion
}
}

View file

@ -1,121 +0,0 @@
using System;
namespace Substrate.NBT
{
/// <summary>
/// An NBT node representing a signed long tag type.
/// </summary>
public sealed class TagNodeLong : TagNode
{
private long _data = 0;
/// <summary>
/// Converts the node to itself.
/// </summary>
/// <returns>A reference to itself.</returns>
public override TagNodeLong ToTagLong ()
{
return this;
}
/// <summary>
/// Gets the tag type of the node.
/// </summary>
/// <returns>The TAG_LONG tag type.</returns>
public override TagType GetTagType ()
{
return TagType.TAG_LONG;
}
/// <summary>
/// Gets or sets a long of tag data.
/// </summary>
public long Data
{
get { return _data; }
set { _data = value; }
}
/// <summary>
/// Constructs a new long node with a data value of 0.
/// </summary>
public TagNodeLong () { }
/// <summary>
/// Constructs a new long node.
/// </summary>
/// <param name="d">The value to set the node's tag data value.</param>
public TagNodeLong (long d)
{
_data = d;
}
/// <summary>
/// Makes a deep copy of the node.
/// </summary>
/// <returns>A new long node representing the same data.</returns>
public override TagNode Copy ()
{
return new TagNodeLong(_data);
}
/// <summary>
/// Gets a string representation of the node's data.
/// </summary>
/// <returns>String representation of the node's data.</returns>
public override string ToString ()
{
return _data.ToString();
}
/// <summary>
/// Converts a system byte to a long node representing the same value.
/// </summary>
/// <param name="b">A byte value.</param>
/// <returns>A new long node containing the given value.</returns>
public static implicit operator TagNodeLong (byte b)
{
return new TagNodeLong(b);
}
/// <summary>
/// Converts a system shprt to a long node representing the same value.
/// </summary>
/// <param name="s">A short value.</param>
/// <returns>A new long node containing the given value.</returns>
public static implicit operator TagNodeLong (short s)
{
return new TagNodeLong(s);
}
/// <summary>
/// Converts a system int to a long node representing the same value.
/// </summary>
/// <param name="i">An int value.</param>
/// <returns>A new long node containing the given value.</returns>
public static implicit operator TagNodeLong (int i)
{
return new TagNodeLong(i);
}
/// <summary>
/// Converts a system long to a long node representing the same value.
/// </summary>
/// <param name="l">A long value.</param>
/// <returns>A new long node containing the given value.</returns>
public static implicit operator TagNodeLong (long l)
{
return new TagNodeLong(l);
}
/// <summary>
/// Converts a long node to a system long representing the same value.
/// </summary>
/// <param name="l">A long node.</param>
/// <returns>A system long set to the node's data value.</returns>
public static implicit operator long (TagNodeLong l)
{
return l._data;
}
}
}

View file

@ -1,37 +0,0 @@
using System;
namespace Substrate.NBT
{
/// <summary>
/// An NBT node representing a null tag type.
/// </summary>
public sealed class TagNodeNull : TagNode
{
/// <summary>
/// Converts the node to itself.
/// </summary>
/// <returns>A reference to itself.</returns>
public override TagNodeNull ToTagNull ()
{
return this;
}
/// <summary>
/// Gets the tag type of the node.
/// </summary>
/// <returns>The TAG_END tag type.</returns>
public override TagType GetTagType ()
{
return TagType.TAG_END;
}
/// <summary>
/// Makes a deep copy of the node.
/// </summary>
/// <returns>A new null node.</returns>
public override TagNode Copy ()
{
return new TagNodeNull();
}
}
}

View file

@ -1,151 +0,0 @@
using System;
namespace Substrate.NBT
{
/// <summary>
/// An NBT node representing a signed short tag type.
/// </summary>
public sealed class TagNodeShort : TagNode
{
private short _data = 0;
/// <summary>
/// Converts the node to itself.
/// </summary>
/// <returns>A reference to itself.</returns>
public override TagNodeShort ToTagShort ()
{
return this;
}
/// <summary>
/// Converts the node to a new int node.
/// </summary>
/// <returns>An int node representing the same data.</returns>
public override TagNodeInt ToTagInt ()
{
return new TagNodeInt(_data);
}
/// <summary>
/// Converts the node to a new long node.
/// </summary>
/// <returns>A long node representing the same data.</returns>
public override TagNodeLong ToTagLong ()
{
return new TagNodeLong(_data);
}
/// <summary>
/// Gets the tag type of the node.
/// </summary>
/// <returns>The TAG_SHORT tag type.</returns>
public override TagType GetTagType ()
{
return TagType.TAG_SHORT;
}
/// <summary>
/// Checks if the node is castable to another node of a given tag type.
/// </summary>
/// <param name="type">An NBT tag type.</param>
/// <returns>Status indicating whether this object could be cast to a node type represented by the given tag type.</returns>
public override bool IsCastableTo (TagType type)
{
return (type == TagType.TAG_SHORT ||
type == TagType.TAG_INT ||
type == TagType.TAG_LONG);
}
/// <summary>
/// Gets or sets a short of tag data.
/// </summary>
public short Data
{
get { return _data; }
set { _data = value; }
}
/// <summary>
/// Constructs a new short node with a data value of 0.
/// </summary>
public TagNodeShort () { }
/// <summary>
/// Constructs a new short node.
/// </summary>
/// <param name="d">The value to set the node's tag data value.</param>
public TagNodeShort (short d)
{
_data = d;
}
/// <summary>
/// Makes a deep copy of the node.
/// </summary>
/// <returns>A new short node representing the same data.</returns>
public override TagNode Copy ()
{
return new TagNodeShort(_data);
}
/// <summary>
/// Gets a string representation of the node's data.
/// </summary>
/// <returns>String representation of the node's data.</returns>
public override string ToString ()
{
return _data.ToString();
}
/// <summary>
/// Converts a system byte to a short node representing the same value.
/// </summary>
/// <param name="b">A byte value.</param>
/// <returns>A new short node containing the given value.</returns>
public static implicit operator TagNodeShort (byte b)
{
return new TagNodeShort(b);
}
/// <summary>
/// Converts a system short to a short node representing the same value.
/// </summary>
/// <param name="s">A short value.</param>
/// <returns>A new short node containing the given value.</returns>
public static implicit operator TagNodeShort (short s)
{
return new TagNodeShort(s);
}
/// <summary>
/// Converts a short node to a system short representing the same value.
/// </summary>
/// <param name="s">A short node.</param>
/// <returns>A system short set to the node's data value.</returns>
public static implicit operator short (TagNodeShort s)
{
return s._data;
}
/// <summary>
/// Converts a short node to a system int representing the same value.
/// </summary>
/// <param name="s">A short node.</param>
/// <returns>A system int set to the node's data value.</returns>
public static implicit operator int (TagNodeShort s)
{
return s._data;
}
/// <summary>
/// Converts a short node to a system long representing the same value.
/// </summary>
/// <param name="s">A short node.</param>
/// <returns>A system long set to the node's data value.</returns>
public static implicit operator long (TagNodeShort s)
{
return s._data;
}
}
}

View file

@ -1,99 +0,0 @@
using System;
namespace Substrate.NBT
{
/// <summary>
/// An NBT node representing a string tag type.
/// </summary>
public sealed class TagNodeString : TagNode
{
private string _data = "";
/// <summary>
/// Converts the node to itself.
/// </summary>
/// <returns>A reference to itself.</returns>
public override TagNodeString ToTagString ()
{
return this;
}
/// <summary>
/// Gets the tag type of the node.
/// </summary>
/// <returns>The TAG_STRING tag type.</returns>
public override TagType GetTagType ()
{
return TagType.TAG_STRING;
}
/// <summary>
/// Gets or sets a string of tag data.
/// </summary>
public string Data
{
get { return _data; }
set { _data = value; }
}
/// <summary>
/// Gets the length of the stored string.
/// </summary>
public int Length
{
get { return _data.Length; }
}
/// <summary>
/// Constructs a new byte array node with an empty string.
/// </summary>
public TagNodeString () { }
/// <summary>
/// Constructs a new string node.
/// </summary>
/// <param name="d">The value to set the node's tag data value.</param>
public TagNodeString (string d)
{
_data = d;
}
/// <summary>
/// Makes a deep copy of the node.
/// </summary>
/// <returns>A new string node representing the same data.</returns>
public override TagNode Copy ()
{
return new TagNodeString(_data);
}
/// <summary>
/// Gets a string representation of the node's data.
/// </summary>
/// <returns>String representation of the node's data.</returns>
public override string ToString ()
{
return _data.ToString();
}
/// <summary>
/// Converts a system string to a string node representing the same data.
/// </summary>
/// <param name="s">A string.</param>
/// <returns>A new string node containing the given value.</returns>
public static implicit operator TagNodeString (string s)
{
return new TagNodeString(s);
}
/// <summary>
/// Converts a string node to a system string representing the same data.
/// </summary>
/// <param name="s">A string node.</param>
/// <returns>A system string set to the node's data.</returns>
public static implicit operator string (TagNodeString s)
{
return s._data;
}
}
}

View file

@ -1,65 +0,0 @@
using System;
namespace Substrate.NBT
{
/// <summary>
/// Defines the type of an NBT tag.
/// </summary>
public enum TagType
{
/// <summary>
/// A null tag, used to terminate lists.
/// </summary>
TAG_END = 0,
/// <summary>
/// A tag containing an 8-bit signed integer.
/// </summary>
TAG_BYTE = 1,
/// <summary>
/// A tag containing a 16-bit signed integer.
/// </summary>
TAG_SHORT = 2,
/// <summary>
/// A tag containing a 32-bit signed integer.
/// </summary>
TAG_INT = 3,
/// <summary>
/// A tag containing a 64-bit signed integer.
/// </summary>
TAG_LONG = 4,
/// <summary>
/// A tag containing a 32-bit (single precision) floating-point value.
/// </summary>
TAG_FLOAT = 5,
/// <summary>
/// A tag containing a 64-bit (double precision) floating-point value.
/// </summary>
TAG_DOUBLE = 6,
/// <summary>
/// A tag containing an array of unsigned 8-bit byte values.
/// </summary>
TAG_BYTE_ARRAY = 7,
/// <summary>
/// A tag containing a string of text.
/// </summary>
TAG_STRING = 8,
/// <summary>
/// A tag containing a sequential list of tags, where all tags of of the same type.
/// </summary>
TAG_LIST = 9,
/// <summary>
/// A tag containing a key-value store of tags, where each tag can be of any type.
/// </summary>
TAG_COMPOUND = 10
}
}

View file

@ -1,47 +0,0 @@
using System;
using Substrate.Core;
namespace Substrate.NBT
{
/// <summary>
/// A collection of static methods that can be hooked into <see cref="NBTVerifier"/> events for logging NBT errors to the console.
/// </summary>
public static class VerifierLogger
{
/// <summary>
/// Logs an occurance of a missing tag error, and advances to the next event in the event chain.
/// </summary>
/// <param name="e">Data about the NBT node being verified.</param>
/// <returns>A <see cref="TagEventCode"/> indicating whether event processing should pass, fail, or advance.</returns>
public static TagEventCode MissingTagHandler (TagEventArgs e)
{
Console.WriteLine("Missing Tag Error: '{0}'", e.TagName);
return TagEventCode.NEXT;
}
/// <summary>
/// Logs an occurance of an invalid tag type error, and advances to the next event in the event chain.
/// </summary>
/// <param name="e">Data about the NBT node being verified.</param>
/// <returns>A <see cref="TagEventCode"/> indicating whether event processing should pass, fail, or advance.</returns>
public static TagEventCode InvalidTagTypeHandler (TagEventArgs e)
{
Console.WriteLine("Invalid Tag Type Error: '{0}' has type '{1}', expected '{2}'", e.TagName, e.Tag.GetTagType(), e.Schema.ToString());
return TagEventCode.NEXT;
}
/// <summary>
/// Logs an occurance of an invalid tag value error, and advances to the next event in the event chain.
/// </summary>
/// <param name="e">Data about the NBT node being verified.</param>
/// <returns>A <see cref="TagEventCode"/> indicating whether event processing should pass, fail, or advance.</returns>
public static TagEventCode InvalidTagValueHandler (TagEventArgs e)
{
Console.WriteLine("Invalid Tag Value Error: '{0}' of type '{1}' is set to invalid value '{2}'", e.TagName, e.Tag.GetTagType(), e.Tag.ToString());
return TagEventCode.NEXT;
}
}
}

View file

@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Text;
using Substrate.Core;
using Substrate.NBT;
using Substrate.Nbt;
namespace Substrate
{

View file

@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Text;
using System.IO;
using Substrate.Core;
using Substrate.NBT;
using Substrate.Nbt;
namespace Substrate
{
@ -21,7 +21,7 @@ namespace Substrate
return new PlayerFile(_playerPath, name);
}
protected NBT_Tree GetPlayerTree (string name)
protected NbtTree GetPlayerTree (string name)
{
PlayerFile pf = GetPlayerFile(name);
Stream nbtstr = pf.GetDataInputStream();
@ -29,10 +29,10 @@ namespace Substrate
return null;
}
return new NBT_Tree(nbtstr);
return new NbtTree(nbtstr);
}
protected bool SavePlayerTree (string name, NBT_Tree tree)
protected bool SavePlayerTree (string name, NbtTree tree)
{
PlayerFile pf = GetPlayerFile(name);
Stream zipstr = pf.GetDataOutputStream();
@ -57,7 +57,7 @@ namespace Substrate
public bool SetPlayer (string name, Player player)
{
return SavePlayerTree(name, new NBT_Tree(player.BuildTree() as TagNodeCompound));
return SavePlayerTree(name, new NbtTree(player.BuildTree() as TagNodeCompound));
}
public bool PlayerExists (string name)

View file

@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using Substrate.NBT;
using Substrate.Nbt;
using Substrate.Core;
namespace Substrate
@ -156,7 +156,7 @@ namespace Substrate
return rf;
}
public NBT_Tree GetChunkTree (int lcx, int lcz)
public NbtTree GetChunkTree (int lcx, int lcz)
{
if (!LocalBoundsCheck(lcx, lcz)) {
Region alt = GetForeignRegion(lcx, lcz);
@ -169,13 +169,13 @@ namespace Substrate
return null;
}
NBT_Tree tree = new NBT_Tree(nbtstr);
NbtTree tree = new NbtTree(nbtstr);
nbtstr.Close();
return tree;
}
public bool SaveChunkTree (int lcx, int lcz, NBT_Tree tree)
public bool SaveChunkTree (int lcx, int lcz, NbtTree tree)
{
if (!LocalBoundsCheck(lcx, lcz)) {
Region alt = GetForeignRegion(lcx, lcz);

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.TileEntities
{
using Substrate.NBT;
using Substrate.Nbt;
public class TileEntityChest : TileEntity, IItemContainer
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.TileEntities
{
using Substrate.NBT;
using Substrate.Nbt;
public class TileEntityFurnace : TileEntity, IItemContainer
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.TileEntities
{
using Substrate.NBT;
using Substrate.Nbt;
public class TileEntityMobSpawner : TileEntity
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.TileEntities
{
using Substrate.NBT;
using Substrate.Nbt;
public class TileEntityMusic : TileEntity
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.TileEntities
{
using Substrate.NBT;
using Substrate.Nbt;
public class TileEntityRecordPlayer : TileEntity
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.TileEntities
{
using Substrate.NBT;
using Substrate.Nbt;
public class TileEntitySign : TileEntity
{

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Substrate.TileEntities
{
using Substrate.NBT;
using Substrate.Nbt;
public class TileEntityTrap : TileEntity, IItemContainer
{

View file

@ -1,6 +1,6 @@
using System;
using Substrate.Core;
using Substrate.NBT;
using Substrate.Nbt;
namespace Substrate
{

View file

@ -3,7 +3,7 @@ using System.Collections.Generic;
namespace Substrate
{
using NBT;
using Nbt;
using TileEntities;
/// <summary>

View file

@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Text;
using System.IO;
using Substrate.Core;
using Substrate.NBT;
using Substrate.Nbt;
namespace Substrate
{
@ -176,7 +176,7 @@ namespace Substrate
return false;
}
NBT_Tree tree = new NBT_Tree(nbtstr);
NbtTree tree = new NbtTree(nbtstr);
_level = new Level(this);
_level = _level.LoadTreeSafe(tree.Root);
@ -397,7 +397,7 @@ namespace Substrate
return false;
}
NBT_Tree tree = new NBT_Tree(nbtstr);
NbtTree tree = new NbtTree(nbtstr);
_level = new Level(this);
_level = _level.LoadTreeSafe(tree.Root);