示例#1
0
 public void NbtShortTest()
 {
     object dummy;
     NbtTag test = new NbtShort( 32767 );
     Assert.Throws<InvalidCastException>( () => dummy = test.ByteArrayValue );
     Assert.Throws<InvalidCastException>( () => dummy = test.ByteValue );
     Assert.AreEqual( (double)32767, test.DoubleValue );
     Assert.AreEqual( (float)32767, test.FloatValue );
     Assert.Throws<InvalidCastException>( () => dummy = test.IntArrayValue );
     Assert.AreEqual( 32767, test.IntValue );
     Assert.AreEqual( 32767L, test.LongValue );
     Assert.AreEqual( 32767, test.ShortValue );
     Assert.AreEqual( "32767", test.StringValue );
 }
示例#2
0
		public void Write_ShortTag()
		{
			// Arrange
			MemoryStream stream = new MemoryStream();
			NbtWriter writer = new NbtWriter(stream);
			NbtShort tag = new NbtShort("asdf", 12345);
			byte[] expected = new byte[] { 0x02, 0x00, 0x04, 0x61, 0x73, 0x64, 0x66, 0x30, 0x39 };

			// Act
			writer.Write(tag);
			byte[] result = stream.ToArray();

			// Assert
			CollectionAssert.AreEqual(expected, result);
		}
示例#3
0
文件: MiscTests.cs 项目: fragmer/fNbt
        public void CopyConstructorTest()
        {
            NbtByte byteTag = new NbtByte("byteTag", 1);
            NbtByte byteTagClone = (NbtByte)byteTag.Clone();
            Assert.AreNotSame(byteTag, byteTagClone);
            Assert.AreEqual(byteTag.Name, byteTagClone.Name);
            Assert.AreEqual(byteTag.Value, byteTagClone.Value);
            Assert.Throws<ArgumentNullException>(() => new NbtByte((NbtByte)null));

            NbtByteArray byteArrTag = new NbtByteArray("byteArrTag", new byte[] { 1, 2, 3, 4 });
            NbtByteArray byteArrTagClone = (NbtByteArray)byteArrTag.Clone();
            Assert.AreNotSame(byteArrTag, byteArrTagClone);
            Assert.AreEqual(byteArrTag.Name, byteArrTagClone.Name);
            Assert.AreNotSame(byteArrTag.Value, byteArrTagClone.Value);
            CollectionAssert.AreEqual(byteArrTag.Value, byteArrTagClone.Value);
            Assert.Throws<ArgumentNullException>(() => new NbtByteArray((NbtByteArray)null));

            NbtCompound compTag = new NbtCompound("compTag", new NbtTag[] { new NbtByte("innerTag", 1) });
            NbtCompound compTagClone = (NbtCompound)compTag.Clone();
            Assert.AreNotSame(compTag, compTagClone);
            Assert.AreEqual(compTag.Name, compTagClone.Name);
            Assert.AreNotSame(compTag["innerTag"], compTagClone["innerTag"]);
            Assert.AreEqual(compTag["innerTag"].Name, compTagClone["innerTag"].Name);
            Assert.AreEqual(compTag["innerTag"].ByteValue, compTagClone["innerTag"].ByteValue);
            Assert.Throws<ArgumentNullException>(() => new NbtCompound((NbtCompound)null));

            NbtDouble doubleTag = new NbtDouble("doubleTag", 1);
            NbtDouble doubleTagClone = (NbtDouble)doubleTag.Clone();
            Assert.AreNotSame(doubleTag, doubleTagClone);
            Assert.AreEqual(doubleTag.Name, doubleTagClone.Name);
            Assert.AreEqual(doubleTag.Value, doubleTagClone.Value);
            Assert.Throws<ArgumentNullException>(() => new NbtDouble((NbtDouble)null));

            NbtFloat floatTag = new NbtFloat("floatTag", 1);
            NbtFloat floatTagClone = (NbtFloat)floatTag.Clone();
            Assert.AreNotSame(floatTag, floatTagClone);
            Assert.AreEqual(floatTag.Name, floatTagClone.Name);
            Assert.AreEqual(floatTag.Value, floatTagClone.Value);
            Assert.Throws<ArgumentNullException>(() => new NbtFloat((NbtFloat)null));

            NbtInt intTag = new NbtInt("intTag", 1);
            NbtInt intTagClone = (NbtInt)intTag.Clone();
            Assert.AreNotSame(intTag, intTagClone);
            Assert.AreEqual(intTag.Name, intTagClone.Name);
            Assert.AreEqual(intTag.Value, intTagClone.Value);
            Assert.Throws<ArgumentNullException>(() => new NbtInt((NbtInt)null));

            NbtIntArray intArrTag = new NbtIntArray("intArrTag", new[] { 1, 2, 3, 4 });
            NbtIntArray intArrTagClone = (NbtIntArray)intArrTag.Clone();
            Assert.AreNotSame(intArrTag, intArrTagClone);
            Assert.AreEqual(intArrTag.Name, intArrTagClone.Name);
            Assert.AreNotSame(intArrTag.Value, intArrTagClone.Value);
            CollectionAssert.AreEqual(intArrTag.Value, intArrTagClone.Value);
            Assert.Throws<ArgumentNullException>(() => new NbtIntArray((NbtIntArray)null));

            NbtList listTag = new NbtList("listTag", new NbtTag[] { new NbtByte(1) });
            NbtList listTagClone = (NbtList)listTag.Clone();
            Assert.AreNotSame(listTag, listTagClone);
            Assert.AreEqual(listTag.Name, listTagClone.Name);
            Assert.AreNotSame(listTag[0], listTagClone[0]);
            Assert.AreEqual(listTag[0].ByteValue, listTagClone[0].ByteValue);
            Assert.Throws<ArgumentNullException>(() => new NbtList((NbtList)null));

            NbtLong longTag = new NbtLong("longTag", 1);
            NbtLong longTagClone = (NbtLong)longTag.Clone();
            Assert.AreNotSame(longTag, longTagClone);
            Assert.AreEqual(longTag.Name, longTagClone.Name);
            Assert.AreEqual(longTag.Value, longTagClone.Value);
            Assert.Throws<ArgumentNullException>(() => new NbtLong((NbtLong)null));

            NbtShort shortTag = new NbtShort("shortTag", 1);
            NbtShort shortTagClone = (NbtShort)shortTag.Clone();
            Assert.AreNotSame(shortTag, shortTagClone);
            Assert.AreEqual(shortTag.Name, shortTagClone.Name);
            Assert.AreEqual(shortTag.Value, shortTagClone.Value);
            Assert.Throws<ArgumentNullException>(() => new NbtShort((NbtShort)null));

            NbtString stringTag = new NbtString("stringTag", "foo");
            NbtString stringTagClone = (NbtString)stringTag.Clone();
            Assert.AreNotSame(stringTag, stringTagClone);
            Assert.AreEqual(stringTag.Name, stringTagClone.Name);
            Assert.AreEqual(stringTag.Value, stringTagClone.Value);
            Assert.Throws<ArgumentNullException>(() => new NbtString((NbtString)null));
        }
示例#4
0
		internal void Write(NbtShort tag, bool writeHeader)
		{
			Write(tag.Name, tag.Value, writeHeader);
		}
示例#5
0
		/// <summary>
		/// Writes out the specified tag.
		/// </summary>
		/// <param name="tag">The tag.</param>
		/// <exception cref="System.ArgumentNullException"><paramref name="tag"/> is <c>null</c>.</exception>
		/// <exception cref="System.ObjectDisposedException">The stream is closed.</exception>
		/// <exception cref="System.IO.IOException">An I/O error occured.</exception>
		public void Write(NbtShort tag)
		{
			if (tag == null)
				throw new ArgumentNullException("tag", "tag is null.");

			Write(tag, true);
		}
示例#6
0
 public static string ToSnbt(this NbtShort tag, SnbtOptions options) => tag.Value + OptionalSuffix(options, SHORT_SUFFIX);
示例#7
0
    public void CopyConstructorTest()
    {
        NbtByte byteTag      = new NbtByte("byteTag", 1);
        NbtByte byteTagClone = (NbtByte)byteTag.Clone();

        Assert.NotSame(byteTag, byteTagClone);
        Assert.Equal(byteTag.Name, byteTagClone.Name);
        Assert.Equal(byteTag.Value, byteTagClone.Value);
        Assert.Throws <ArgumentNullException>(() => new NbtByte((NbtByte)null));

        NbtByteArray byteArrTag      = new NbtByteArray("byteArrTag", new byte[] { 1, 2, 3, 4 });
        NbtByteArray byteArrTagClone = (NbtByteArray)byteArrTag.Clone();

        Assert.NotSame(byteArrTag, byteArrTagClone);
        Assert.Equal(byteArrTag.Name, byteArrTagClone.Name);
        Assert.NotSame(byteArrTag.Value, byteArrTagClone.Value);
        Assert.Equal(byteArrTag.Value, byteArrTagClone.Value);
        Assert.Throws <ArgumentNullException>(() => new NbtByteArray((NbtByteArray)null));

        NbtCompound compTag      = new NbtCompound("compTag", new NbtTag[] { new NbtByte("innerTag", 1) });
        NbtCompound compTagClone = (NbtCompound)compTag.Clone();

        Assert.NotSame(compTag, compTagClone);
        Assert.Equal(compTag.Name, compTagClone.Name);
        Assert.NotSame(compTag["innerTag"], compTagClone["innerTag"]);
        Assert.Equal(compTag["innerTag"].Name, compTagClone["innerTag"].Name);
        Assert.Equal(compTag["innerTag"].ByteValue, compTagClone["innerTag"].ByteValue);
        Assert.Throws <ArgumentNullException>(() => new NbtCompound((NbtCompound)null));

        NbtDouble doubleTag      = new NbtDouble("doubleTag", 1);
        NbtDouble doubleTagClone = (NbtDouble)doubleTag.Clone();

        Assert.NotSame(doubleTag, doubleTagClone);
        Assert.Equal(doubleTag.Name, doubleTagClone.Name);
        Assert.Equal(doubleTag.Value, doubleTagClone.Value);
        Assert.Throws <ArgumentNullException>(() => new NbtDouble((NbtDouble)null));

        NbtFloat floatTag      = new NbtFloat("floatTag", 1);
        NbtFloat floatTagClone = (NbtFloat)floatTag.Clone();

        Assert.NotSame(floatTag, floatTagClone);
        Assert.Equal(floatTag.Name, floatTagClone.Name);
        Assert.Equal(floatTag.Value, floatTagClone.Value);
        Assert.Throws <ArgumentNullException>(() => new NbtFloat((NbtFloat)null));

        NbtInt intTag      = new NbtInt("intTag", 1);
        NbtInt intTagClone = (NbtInt)intTag.Clone();

        Assert.NotSame(intTag, intTagClone);
        Assert.Equal(intTag.Name, intTagClone.Name);
        Assert.Equal(intTag.Value, intTagClone.Value);
        Assert.Throws <ArgumentNullException>(() => new NbtInt((NbtInt)null));

        NbtIntArray intArrTag      = new NbtIntArray("intArrTag", new[] { 1, 2, 3, 4 });
        NbtIntArray intArrTagClone = (NbtIntArray)intArrTag.Clone();

        Assert.NotSame(intArrTag, intArrTagClone);
        Assert.Equal(intArrTag.Name, intArrTagClone.Name);
        Assert.NotSame(intArrTag.Value, intArrTagClone.Value);
        Assert.Equal(intArrTag.Value, intArrTagClone.Value);
        Assert.Throws <ArgumentNullException>(() => new NbtIntArray((NbtIntArray)null));

        NbtList listTag      = new NbtList("listTag", new NbtTag[] { new NbtByte(1) });
        NbtList listTagClone = (NbtList)listTag.Clone();

        Assert.NotSame(listTag, listTagClone);
        Assert.Equal(listTag.Name, listTagClone.Name);
        Assert.NotSame(listTag[0], listTagClone[0]);
        Assert.Equal(listTag[0].ByteValue, listTagClone[0].ByteValue);
        Assert.Throws <ArgumentNullException>(() => new NbtList((NbtList)null));

        NbtLong longTag      = new NbtLong("longTag", 1);
        NbtLong longTagClone = (NbtLong)longTag.Clone();

        Assert.NotSame(longTag, longTagClone);
        Assert.Equal(longTag.Name, longTagClone.Name);
        Assert.Equal(longTag.Value, longTagClone.Value);
        Assert.Throws <ArgumentNullException>(() => new NbtLong((NbtLong)null));

        NbtShort shortTag      = new NbtShort("shortTag", 1);
        NbtShort shortTagClone = (NbtShort)shortTag.Clone();

        Assert.NotSame(shortTag, shortTagClone);
        Assert.Equal(shortTag.Name, shortTagClone.Name);
        Assert.Equal(shortTag.Value, shortTagClone.Value);
        Assert.Throws <ArgumentNullException>(() => new NbtShort((NbtShort)null));

        NbtString stringTag      = new NbtString("stringTag", "foo");
        NbtString stringTagClone = (NbtString)stringTag.Clone();

        Assert.NotSame(stringTag, stringTagClone);
        Assert.Equal(stringTag.Name, stringTagClone.Name);
        Assert.Equal(stringTag.Value, stringTagClone.Value);
        Assert.Throws <ArgumentNullException>(() => new NbtString((NbtString)null));
    }
示例#8
0
        public void SetPropertyValue <T>(NbtTag tag, Expression <Func <T> > property, bool upperFirst = true)
        {
            var propertyInfo = ((MemberExpression)property.Body).Member as PropertyInfo;

            if (propertyInfo == null)
            {
                throw new ArgumentException("The lambda expression 'property' should point to a valid Property");
            }

            NbtTag nbtTag = tag[propertyInfo.Name];

            if (nbtTag == null)
            {
                nbtTag = tag[LowercaseFirst(propertyInfo.Name)];
            }

            if (nbtTag == null)
            {
                if (propertyInfo.PropertyType == typeof(bool))
                {
                    nbtTag = new NbtByte(propertyInfo.Name);
                }
                else if (propertyInfo.PropertyType == typeof(byte))
                {
                    nbtTag = new NbtByte(LowercaseFirst(propertyInfo.Name));
                }
                else if (propertyInfo.PropertyType == typeof(short))
                {
                    nbtTag = new NbtShort(LowercaseFirst(propertyInfo.Name));
                }
                else if (propertyInfo.PropertyType == typeof(int))
                {
                    nbtTag = new NbtInt(LowercaseFirst(propertyInfo.Name));
                }
                else if (propertyInfo.PropertyType == typeof(long))
                {
                    nbtTag = new NbtLong(LowercaseFirst(propertyInfo.Name));
                }
                else if (propertyInfo.PropertyType == typeof(float))
                {
                    nbtTag = new NbtFloat(LowercaseFirst(propertyInfo.Name));
                }
                else if (propertyInfo.PropertyType == typeof(double))
                {
                    nbtTag = new NbtDouble(LowercaseFirst(propertyInfo.Name));
                }
                else if (propertyInfo.PropertyType == typeof(string))
                {
                    nbtTag = new NbtString(LowercaseFirst(propertyInfo.Name), "");
                }
                else
                {
                    return;
                }
            }

            var mex    = property.Body as MemberExpression;
            var target = Expression.Lambda(mex.Expression).Compile().DynamicInvoke();

            switch (nbtTag.TagType)
            {
            case NbtTagType.Unknown:
                break;

            case NbtTagType.End:
                break;

            case NbtTagType.Byte:
                if (propertyInfo.PropertyType == typeof(bool))
                {
                    tag[nbtTag.Name] = new NbtByte(nbtTag.Name, (byte)((bool)propertyInfo.GetValue(target) ? 1 : 0));
                }
                else
                {
                    tag[nbtTag.Name] = new NbtByte(nbtTag.Name, (byte)propertyInfo.GetValue(target));
                }
                break;

            case NbtTagType.Short:
                tag[nbtTag.Name] = new NbtShort(nbtTag.Name, (short)propertyInfo.GetValue(target));
                break;

            case NbtTagType.Int:
                if (propertyInfo.PropertyType == typeof(bool))
                {
                    tag[nbtTag.Name] = new NbtInt(nbtTag.Name, (bool)propertyInfo.GetValue(target) ? 1 : 0);
                }
                else
                {
                    tag[nbtTag.Name] = new NbtInt(nbtTag.Name, (int)propertyInfo.GetValue(target));
                }
                break;

            case NbtTagType.Long:
                tag[nbtTag.Name] = new NbtLong(nbtTag.Name, (long)propertyInfo.GetValue(target));
                break;

            case NbtTagType.Float:
                tag[nbtTag.Name] = new NbtFloat(nbtTag.Name, (float)propertyInfo.GetValue(target));
                break;

            case NbtTagType.Double:
                tag[nbtTag.Name] = new NbtDouble(nbtTag.Name, (double)propertyInfo.GetValue(target));
                break;

            case NbtTagType.ByteArray:
                tag[nbtTag.Name] = new NbtByteArray(nbtTag.Name, (byte[])propertyInfo.GetValue(target));
                break;

            case NbtTagType.String:
                tag[nbtTag.Name] = new NbtString(nbtTag.Name, (string)propertyInfo.GetValue(target) ?? "");
                break;

            case NbtTagType.List:
                break;

            case NbtTagType.Compound:
                break;

            case NbtTagType.IntArray:
                tag[nbtTag.Name] = new NbtIntArray(nbtTag.Name, (int[])propertyInfo.GetValue(target));
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            //return (T) propertyInfo.GetValue(target);
        }