From 0860bae2bc82d0427aac03a9bda9409686b8c47d Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Thu, 7 Apr 2011 02:44:17 +0000 Subject: [PATCH] Added hidden base type UntypedEntity above Entity, added Player entity as UntypedEntity. Added options field to NBT Schema. --- .../Source/Entities/EntitySheep.cs | 4 +- Substrate/SubstrateCS/Source/Entity.cs | 117 +++++++++++++----- Substrate/SubstrateCS/Source/NBT/NBTSchema.cs | 95 ++++++++++++++ .../SubstrateCS/Source/NBT/NBTVerifier.cs | 18 +++ Substrate/SubstrateCS/Source/Player.cs | 92 ++++++++++++++ 5 files changed, 290 insertions(+), 36 deletions(-) create mode 100644 Substrate/SubstrateCS/Source/Player.cs diff --git a/Substrate/SubstrateCS/Source/Entities/EntitySheep.cs b/Substrate/SubstrateCS/Source/Entities/EntitySheep.cs index 3c0e139..f0827a1 100644 --- a/Substrate/SubstrateCS/Source/Entities/EntitySheep.cs +++ b/Substrate/SubstrateCS/Source/Entities/EntitySheep.cs @@ -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; diff --git a/Substrate/SubstrateCS/Source/Entity.cs b/Substrate/SubstrateCS/Source/Entity.cs index 0c977a2..24d20c7 100644 --- a/Substrate/SubstrateCS/Source/Entity.cs +++ b/Substrate/SubstrateCS/Source/Entity.cs @@ -18,7 +18,7 @@ namespace Substrate int RemoveEntities (Predicate match); } - public class Entity : INBTObject, ICopyable + public class UntypedEntity : INBTObject, ICopyable { 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 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 Members + + public UntypedEntity Copy () + { + return new UntypedEntity(this); + } + + #endregion + } + + public class Entity : UntypedEntity, INBTObject, ICopyable + { + 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 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 Members - public virtual Entity Copy () + public virtual new Entity Copy () { return new Entity(this); } diff --git a/Substrate/SubstrateCS/Source/NBT/NBTSchema.cs b/Substrate/SubstrateCS/Source/NBT/NBTSchema.cs index 1fbc881..5f0903a 100644 --- a/Substrate/SubstrateCS/Source/NBT/NBTSchema.cs +++ b/Substrate/SubstrateCS/Source/NBT/NBTSchema.cs @@ -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(); } + public NBTCompoundNode (NBTOptions options) + : base("", options) + { + _subnodes = new List(); + } + public NBTCompoundNode (string name) : base(name) { _subnodes = new List(); } + public NBTCompoundNode (string name, NBTOptions options) + : base(name, options) + { + _subnodes = new List(); + } + 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) { diff --git a/Substrate/SubstrateCS/Source/NBT/NBTVerifier.cs b/Substrate/SubstrateCS/Source/NBT/NBTVerifier.cs index e34f8dc..576cd87 100644 --- a/Substrate/SubstrateCS/Source/NBT/NBTVerifier.cs +++ b/Substrate/SubstrateCS/Source/NBT/NBTVerifier.cs @@ -58,6 +58,8 @@ namespace Substrate.NBT public event InvalidTagTypeHandler InvalidTagType; public event InvalidTagValueHandler InvalidTagValue; + private Dictionary _scratch = new Dictionary(); + 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 item in _scratch) { + ctag[item.Key] = item.Value; + } + + _scratch.Clear(); + return pass; } diff --git a/Substrate/SubstrateCS/Source/Player.cs b/Substrate/SubstrateCS/Source/Player.cs new file mode 100644 index 0000000..027dda3 --- /dev/null +++ b/Substrate/SubstrateCS/Source/Player.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Substrate +{ + using NBT; + using Utility; + + public class Player : UntypedEntity, INBTObject, ICopyable + { + 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 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 Members + + public virtual new Player Copy () + { + return new Player(this); + } + + #endregion + } +} \ No newline at end of file