Added hidden base type UntypedEntity above Entity, added Player entity as UntypedEntity. Added options field to NBT Schema.

This commit is contained in:
Justin Aquadro 2011-04-07 02:44:17 +00:00
parent 428c576925
commit 0860bae2bc
5 changed files with 290 additions and 36 deletions

View file

@ -10,9 +10,9 @@ namespace Substrate.Entities
{
public static readonly NBTCompoundNode SheepSchema = MobSchema.MergeInto(new NBTCompoundNode("")
{
new NBTStringNode("id", "Pig"),
new NBTStringNode("id", "Sheep"),
new NBTScalerNode("Sheared", NBT_Type.TAG_BYTE),
new NBTScalerNode("Color", NBT_Type.TAG_BYTE),
new NBTScalerNode("Color", NBT_Type.TAG_BYTE, NBTOptions.CREATE_ON_MISSING),
});
private bool _sheared;

View file

@ -18,7 +18,7 @@ namespace Substrate
int RemoveEntities (Predicate<Entity> match);
}
public class Entity : INBTObject<Entity>, ICopyable<Entity>
public class UntypedEntity : INBTObject<UntypedEntity>, ICopyable<UntypedEntity>
{
public class Vector3
{
@ -33,9 +33,8 @@ namespace Substrate
public double Yaw { get; set; }
}
public static readonly NBTCompoundNode BaseSchema = new NBTCompoundNode("")
public static readonly NBTCompoundNode UTBaseSchema = new NBTCompoundNode("")
{
new NBTScalerNode("id", NBT_Type.TAG_STRING),
new NBTListNode("Pos", NBT_Type.TAG_DOUBLE, 3),
new NBTListNode("Motion", NBT_Type.TAG_DOUBLE, 3),
new NBTListNode("Rotation", NBT_Type.TAG_FLOAT, 2),
@ -45,7 +44,6 @@ namespace Substrate
new NBTScalerNode("OnGround", NBT_Type.TAG_BYTE),
};
private string _id;
private Vector3 _pos;
private Vector3 _motion;
private Orientation _rotation;
@ -55,13 +53,6 @@ namespace Substrate
private short _air;
private byte _onGround;
private string _world;
public string ID
{
get { return _id; }
}
public Vector3 Position
{
get { return _pos; }
@ -104,15 +95,12 @@ namespace Substrate
set { _onGround = (byte)(value ? 1 : 0); }
}
public Entity (string id)
public UntypedEntity ()
{
_id = id;
}
public Entity (Entity e)
public UntypedEntity (UntypedEntity e)
{
_id = e._id;
_pos = new Vector3();
_pos.X = e._pos.X;
_pos.Y = e._pos.Y;
@ -131,21 +119,18 @@ namespace Substrate
_fire = e._fire;
_air = e._air;
_onGround = e._onGround;
_world = e._world;
}
#region INBTObject<Entity> Members
public virtual Entity LoadTree (NBT_Value tree)
public UntypedEntity LoadTree (NBT_Value tree)
{
NBT_Compound ctree = tree as NBT_Compound;
if (ctree == null) {
return null;
}
_id = ctree["id"].ToNBTString();
NBT_List pos = ctree["Pos"].ToNBTList();
_pos = new Vector3();
_pos.X = pos[0].ToNBTDouble();
@ -167,17 +152,10 @@ namespace Substrate
_air = ctree["Air"].ToNBTShort();
_onGround = ctree["OnGround"].ToNBTByte();
NBT_Value world;
ctree.TryGetValue("World", out world);
if (world != null) {
_world = world.ToNBTString();
}
return this;
}
public virtual Entity LoadTreeSafe (NBT_Value tree)
public UntypedEntity LoadTreeSafe (NBT_Value tree)
{
if (!ValidateTree(tree)) {
return null;
@ -186,10 +164,9 @@ namespace Substrate
return LoadTree(tree);
}
public virtual NBT_Value BuildTree ()
public NBT_Value BuildTree ()
{
NBT_Compound tree = new NBT_Compound();
tree["id"] = new NBT_String(_id);
NBT_List pos = new NBT_List(NBT_Type.TAG_DOUBLE);
pos.Add(new NBT_Double(_pos.X));
@ -213,14 +190,86 @@ namespace Substrate
tree["Air"] = new NBT_Short(_air);
tree["OnGround"] = new NBT_Byte(_onGround);
if (_world != null) {
tree["World"] = new NBT_String(_world);
return tree;
}
public bool ValidateTree (NBT_Value tree)
{
return new NBTVerifier(tree, UTBaseSchema).Verify();
}
#endregion
#region ICopyable<Entity> Members
public UntypedEntity Copy ()
{
return new UntypedEntity(this);
}
#endregion
}
public class Entity : UntypedEntity, INBTObject<Entity>, ICopyable<Entity>
{
public static readonly NBTCompoundNode BaseSchema = UTBaseSchema.MergeInto(new NBTCompoundNode("")
{
new NBTScalerNode("id", NBT_Type.TAG_STRING),
});
private string _id;
public string ID
{
get { return _id; }
}
public Entity (string id)
: base()
{
_id = id;
}
public Entity (Entity e)
: base(e)
{
_id = e._id;
}
#region INBTObject<Entity> Members
public virtual new Entity LoadTree (NBT_Value tree)
{
NBT_Compound ctree = tree as NBT_Compound;
if (ctree == null || base.LoadTree(tree) == null) {
return null;
}
_id = ctree["id"].ToNBTString();
return this;
}
public virtual new Entity LoadTreeSafe (NBT_Value tree)
{
if (!ValidateTree(tree)) {
return null;
}
return LoadTree(tree);
}
public virtual new NBT_Value BuildTree ()
{
NBT_Compound tree = base.BuildTree() as NBT_Compound;
tree["id"] = new NBT_String(_id);
return tree;
}
public virtual bool ValidateTree (NBT_Value tree)
public virtual new bool ValidateTree (NBT_Value tree)
{
return new NBTVerifier(tree, BaseSchema).Verify();
}
@ -230,7 +279,7 @@ namespace Substrate
#region ICopyable<Entity> Members
public virtual Entity Copy ()
public virtual new Entity Copy ()
{
return new Entity(this);
}

View file

@ -4,20 +4,39 @@ using System.Text;
namespace Substrate.NBT
{
[Flags]
public enum NBTOptions
{
OPTIONAL = 0x1,
CREATE_ON_MISSING = 0x2,
}
public abstract class NBTSchemaNode
{
private string _name;
private NBTOptions _options;
public string Name
{
get { return _name; }
}
public NBTOptions Options
{
get { return _options; }
}
public NBTSchemaNode (string name)
{
_name = name;
}
public NBTSchemaNode (string name, NBTOptions options)
{
_name = name;
_options = options;
}
public virtual NBT_Value BuildDefaultTree ()
{
return null;
@ -39,6 +58,12 @@ namespace Substrate.NBT
_type = type;
}
public NBTScalerNode (string name, NBT_Type type, NBTOptions options)
: base(name, options)
{
_type = type;
}
public override NBT_Value BuildDefaultTree ()
{
switch (_type) {
@ -83,6 +108,11 @@ namespace Substrate.NBT
get { return _value; }
}
public NBTStringNode (string name, NBTOptions options)
: base(name, options)
{
}
public NBTStringNode (string name, string value)
: base(name)
{
@ -120,12 +150,24 @@ namespace Substrate.NBT
_length = 0;
}
public NBTArrayNode (string name, NBTOptions options)
: base(name, options)
{
_length = 0;
}
public NBTArrayNode (string name, int length)
: base(name)
{
_length = length;
}
public NBTArrayNode (string name, int length, NBTOptions options)
: base(name, options)
{
_length = length;
}
public override NBT_Value BuildDefaultTree ()
{
return new NBT_ByteArray(new byte[_length]);
@ -159,6 +201,12 @@ namespace Substrate.NBT
_type = type;
}
public NBTListNode (string name, NBT_Type type, NBTOptions options)
: base(name, options)
{
_type = type;
}
public NBTListNode (string name, NBT_Type type, int length)
: base(name)
{
@ -166,6 +214,13 @@ namespace Substrate.NBT
_length = length;
}
public NBTListNode (string name, NBT_Type type, int length, NBTOptions options)
: base(name, options)
{
_type = type;
_length = length;
}
public NBTListNode (string name, NBT_Type type, NBTSchemaNode subschema)
: base(name)
{
@ -173,6 +228,13 @@ namespace Substrate.NBT
_subschema = subschema;
}
public NBTListNode (string name, NBT_Type type, NBTSchemaNode subschema, NBTOptions options)
: base(name, options)
{
_type = type;
_subschema = subschema;
}
public NBTListNode (string name, NBT_Type type, int length, NBTSchemaNode subschema)
: base(name)
{
@ -181,6 +243,14 @@ namespace Substrate.NBT
_subschema = subschema;
}
public NBTListNode (string name, NBT_Type type, int length, NBTSchemaNode subschema, NBTOptions options)
: base(name, options)
{
_type = type;
_length = length;
_subschema = subschema;
}
public override NBT_Value BuildDefaultTree ()
{
if (_length == 0) {
@ -263,12 +333,24 @@ namespace Substrate.NBT
_subnodes = new List<NBTSchemaNode>();
}
public NBTCompoundNode (NBTOptions options)
: base("", options)
{
_subnodes = new List<NBTSchemaNode>();
}
public NBTCompoundNode (string name)
: base(name)
{
_subnodes = new List<NBTSchemaNode>();
}
public NBTCompoundNode (string name, NBTOptions options)
: base(name, options)
{
_subnodes = new List<NBTSchemaNode>();
}
public NBTCompoundNode (string name, NBTSchemaNode subschema)
: base(name)
{
@ -283,6 +365,19 @@ namespace Substrate.NBT
}
}
public NBTCompoundNode (string name, NBTSchemaNode subschema, NBTOptions options)
: base(name, options)
{
NBTCompoundNode schema = subschema as NBTCompoundNode;
if (schema == null) {
return;
}
foreach (NBTSchemaNode node in schema._subnodes) {
_subnodes.Add(node);
}
}
public NBTCompoundNode MergeInto (NBTCompoundNode tree)
{
foreach (NBTSchemaNode node in _subnodes) {

View file

@ -58,6 +58,8 @@ namespace Substrate.NBT
public event InvalidTagTypeHandler InvalidTagType;
public event InvalidTagValueHandler InvalidTagValue;
private Dictionary<string, NBT_Value> _scratch = new Dictionary<string,NBT_Value>();
public NBTVerifier () { }
public NBTVerifier (NBT_Value root, NBTSchemaNode schema)
@ -207,9 +209,25 @@ namespace Substrate.NBT
NBT_Value value;
ctag.TryGetValue(node.Name, out value);
if (value == null) {
if ((node.Options & NBTOptions.CREATE_ON_MISSING) == NBTOptions.CREATE_ON_MISSING) {
_scratch[node.Name] = schema.BuildDefaultTree();
continue;
}
else if ((node.Options & NBTOptions.OPTIONAL) == NBTOptions.OPTIONAL) {
continue;
}
}
pass = Verify(value, node) && pass;
}
foreach (KeyValuePair<string, NBT_Value> item in _scratch) {
ctag[item.Key] = item.Value;
}
_scratch.Clear();
return pass;
}

View file

@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Substrate
{
using NBT;
using Utility;
public class Player : UntypedEntity, INBTObject<Player>, ICopyable<Player>
{
public static readonly NBTCompoundNode PlayerSchema = UTBaseSchema.MergeInto(new NBTCompoundNode("")
{
new NBTScalerNode("Dimension", NBT_Type.TAG_INT),
new NBTListNode("Inventory", NBT_Type.TAG_COMPOUND, ItemCollection.InventorySchema),
new NBTScalerNode("World", NBT_Type.TAG_STRING, NBTOptions.OPTIONAL),
new NBTScalerNode("Sleeping", NBT_Type.TAG_BYTE, NBTOptions.CREATE_ON_MISSING),
new NBTScalerNode("SleepTimer", NBT_Type.TAG_SHORT, NBTOptions.CREATE_ON_MISSING),
new NBTScalerNode("SpawnX", NBT_Type.TAG_INT, NBTOptions.OPTIONAL),
new NBTScalerNode("SpawnY", NBT_Type.TAG_INT, NBTOptions.OPTIONAL),
new NBTScalerNode("SpawnZ", NBT_Type.TAG_INT, NBTOptions.OPTIONAL),
});
private int _dimension;
private byte _sleeping;
private short _sleepTimer;
private int? _spawnX;
private int? _spawnY;
private int? _spawnZ;
private string? _world;
private ItemCollection _inventory;
public Player ()
: base()
{
}
public Player (Player p)
: base(p)
{
}
#region INBTObject<Player> Members
public virtual new Player LoadTree (NBT_Value tree)
{
NBT_Compound ctree = tree as NBT_Compound;
if (ctree == null || base.LoadTree(tree) == null) {
return null;
}
return this;
}
public virtual new Player LoadTreeSafe (NBT_Value tree)
{
if (!ValidateTree(tree)) {
return null;
}
return LoadTree(tree);
}
public virtual new NBT_Value BuildTree ()
{
NBT_Compound tree = base.BuildTree() as NBT_Compound;
return tree;
}
public virtual new bool ValidateTree (NBT_Value tree)
{
return new NBTVerifier(tree, PlayerSchema).Verify();
}
#endregion
#region ICopyable<Entity> Members
public virtual new Player Copy ()
{
return new Player(this);
}
#endregion
}
}