コード例 #1
0
ファイル: McLevelAbout.cs プロジェクト: aphistic/libminecraft
 internal void LoadSection(NbtTag section)
 {
     // Make sure we're passed a TAG_Compound named "About"
     if (section is NbtCompound && section.Name == "About")
     {
         NbtCompound compound = (NbtCompound) section;
         Name = compound["Name"] != null ? ((NbtString) compound["Name"]).Value : "";
         Author = compound["Author"] != null ? ((NbtString) compound["Author"]).Value : "";
         if (compound["CreatedOn"] != null &&
             compound["CreatedOn"] is NbtLong)
         {
             CreatedOn = new DateTime(1970, 1, 1, 0, 0, 0, 0)
                 .AddMilliseconds(((NbtLong) compound["CreatedOn"]).Value);
         }
         else
         {
             CreatedOn = DateTime.Now;
         }
     }
 }
コード例 #2
0
ファイル: NbtSerializer.cs プロジェクト: ammaraskar/Craft.Net
        public object Deserialize(NbtTag value)
        {
            if (value is NbtByte)
                return ((NbtByte)value).Value;
            else if (value is NbtByteArray)
                return ((NbtByteArray)value).Value;
            else if (value is NbtDouble)
                return ((NbtDouble)value).Value;
            else if (value is NbtFloat)
                return ((NbtFloat)value).Value;
            else if (value is NbtInt)
                return ((NbtInt)value).Value;
            else if (value is NbtIntArray)
                return ((NbtIntArray)value).Value;
            else if (value is NbtLong)
                return ((NbtLong)value).Value;
            else if (value is NbtShort)
                return ((NbtShort)value).Value;
            else if (value is NbtString)
                return ((NbtString)value).Value;
            else if(value is NbtCompound)
            {
                var compound = value as NbtCompound;
                var properties = Type.GetProperties().Where(p =>
                    !Attribute.GetCustomAttributes(p, typeof(NbtIgnoreAttribute)).Any());
                var resultObject = Activator.CreateInstance(Type);
                foreach (var property in properties)
                {
                    if (!property.CanWrite)
                        continue;
                    string name = property.Name;
                    var nameAttributes = Attribute.GetCustomAttributes(property, typeof(TagNameAttribute));

                    if (nameAttributes.Length != 0)
                        name = ((TagNameAttribute)nameAttributes[0]).Name;
                    var node = compound.Tags.SingleOrDefault(a => a.Name == name);
                    if (node == null) continue;
                    var data = new NbtSerializer(property.PropertyType).Deserialize(node);

                    if (property.PropertyType == typeof(bool)
                        && data is byte)
                        data = (byte)data == 1;

                    property.SetValue(resultObject, data, null);
                }
                
                return resultObject;
            }
            
            throw new NotSupportedException("The node type '" + value.GetType() + "' is not supported.");
        }